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 void cma_sysfs_account_release_pages(struct cma
*cma
, unsigned long nr_pages
)
29 atomic64_add(nr_pages
, &cma
->nr_pages_released
);
32 static inline struct cma
*cma_from_kobj(struct kobject
*kobj
)
34 return container_of(kobj
, struct cma_kobject
, kobj
)->cma
;
37 static ssize_t
alloc_pages_success_show(struct kobject
*kobj
,
38 struct kobj_attribute
*attr
, char *buf
)
40 struct cma
*cma
= cma_from_kobj(kobj
);
42 return sysfs_emit(buf
, "%llu\n",
43 atomic64_read(&cma
->nr_pages_succeeded
));
45 CMA_ATTR_RO(alloc_pages_success
);
47 static ssize_t
alloc_pages_fail_show(struct kobject
*kobj
,
48 struct kobj_attribute
*attr
, char *buf
)
50 struct cma
*cma
= cma_from_kobj(kobj
);
52 return sysfs_emit(buf
, "%llu\n", atomic64_read(&cma
->nr_pages_failed
));
54 CMA_ATTR_RO(alloc_pages_fail
);
56 static ssize_t
release_pages_success_show(struct kobject
*kobj
,
57 struct kobj_attribute
*attr
, char *buf
)
59 struct cma
*cma
= cma_from_kobj(kobj
);
61 return sysfs_emit(buf
, "%llu\n", atomic64_read(&cma
->nr_pages_released
));
63 CMA_ATTR_RO(release_pages_success
);
65 static void cma_kobj_release(struct kobject
*kobj
)
67 struct cma
*cma
= cma_from_kobj(kobj
);
68 struct cma_kobject
*cma_kobj
= cma
->cma_kobj
;
74 static struct attribute
*cma_attrs
[] = {
75 &alloc_pages_success_attr
.attr
,
76 &alloc_pages_fail_attr
.attr
,
77 &release_pages_success_attr
.attr
,
80 ATTRIBUTE_GROUPS(cma
);
82 static const struct kobj_type cma_ktype
= {
83 .release
= cma_kobj_release
,
84 .sysfs_ops
= &kobj_sysfs_ops
,
85 .default_groups
= cma_groups
,
88 static int __init
cma_sysfs_init(void)
90 struct kobject
*cma_kobj_root
;
91 struct cma_kobject
*cma_kobj
;
95 cma_kobj_root
= kobject_create_and_add("cma", mm_kobj
);
99 for (i
= 0; i
< cma_area_count
; i
++) {
100 cma_kobj
= kzalloc(sizeof(*cma_kobj
), GFP_KERNEL
);
107 cma
->cma_kobj
= cma_kobj
;
109 err
= kobject_init_and_add(&cma_kobj
->kobj
, &cma_ktype
,
110 cma_kobj_root
, "%s", cma
->name
);
112 kobject_put(&cma_kobj
->kobj
);
121 kobject_put(&cma
->cma_kobj
->kobj
);
123 kobject_put(cma_kobj_root
);
127 subsys_initcall(cma_sysfs_init
);