1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2021 Minchan Kim <minchan@kernel.org>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
14 #define CMA_ATTR_RO(_name) \
15 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
17 void cma_sysfs_account_success_pages(struct cma *cma, unsigned long nr_pages)
19 atomic64_add(nr_pages, &cma->nr_pages_succeeded);
22 void cma_sysfs_account_fail_pages(struct cma *cma, unsigned long nr_pages)
24 atomic64_add(nr_pages, &cma->nr_pages_failed);
27 static inline struct cma *cma_from_kobj(struct kobject *kobj)
29 return container_of(kobj, struct cma_kobject, kobj)->cma;
32 static ssize_t alloc_pages_success_show(struct kobject *kobj,
33 struct kobj_attribute *attr, char *buf)
35 struct cma *cma = cma_from_kobj(kobj);
37 return sysfs_emit(buf, "%llu\n",
38 atomic64_read(&cma->nr_pages_succeeded));
40 CMA_ATTR_RO(alloc_pages_success);
42 static ssize_t alloc_pages_fail_show(struct kobject *kobj,
43 struct kobj_attribute *attr, char *buf)
45 struct cma *cma = cma_from_kobj(kobj);
47 return sysfs_emit(buf, "%llu\n", atomic64_read(&cma->nr_pages_failed));
49 CMA_ATTR_RO(alloc_pages_fail);
51 static void cma_kobj_release(struct kobject *kobj)
53 struct cma *cma = cma_from_kobj(kobj);
54 struct cma_kobject *cma_kobj = cma->cma_kobj;
60 static struct attribute *cma_attrs[] = {
61 &alloc_pages_success_attr.attr,
62 &alloc_pages_fail_attr.attr,
65 ATTRIBUTE_GROUPS(cma);
67 static const struct kobj_type cma_ktype = {
68 .release = cma_kobj_release,
69 .sysfs_ops = &kobj_sysfs_ops,
70 .default_groups = cma_groups,
73 static int __init cma_sysfs_init(void)
75 struct kobject *cma_kobj_root;
76 struct cma_kobject *cma_kobj;
80 cma_kobj_root = kobject_create_and_add("cma", mm_kobj);
84 for (i = 0; i < cma_area_count; i++) {
85 cma_kobj = kzalloc(sizeof(*cma_kobj), GFP_KERNEL);
92 cma->cma_kobj = cma_kobj;
94 err = kobject_init_and_add(&cma_kobj->kobj, &cma_ktype,
95 cma_kobj_root, "%s", cma->name);
97 kobject_put(&cma_kobj->kobj);
106 kobject_put(&cma->cma_kobj->kobj);
108 kobject_put(cma_kobj_root);
112 subsys_initcall(cma_sysfs_init);