• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * CMA SysFS Interface
4  *
5  * Copyright (c) 2021 Minchan Kim <minchan@kernel.org>
6  */
7 
8 #include <linux/cma.h>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 
13 #include "cma.h"
14 
15 static bool experimental;
16 
17 #define CMA_ATTR_RO(_name) \
18 	static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
19 
cma_sysfs_account_success_pages(struct cma * cma,unsigned long nr_pages)20 void cma_sysfs_account_success_pages(struct cma *cma, unsigned long nr_pages)
21 {
22 	atomic64_add(nr_pages, &cma->nr_pages_succeeded);
23 }
24 
cma_sysfs_account_fail_pages(struct cma * cma,unsigned long nr_pages)25 void cma_sysfs_account_fail_pages(struct cma *cma, unsigned long nr_pages)
26 {
27 	atomic64_add(nr_pages, &cma->nr_pages_failed);
28 }
29 
cma_from_kobj(struct kobject * kobj)30 static inline struct cma *cma_from_kobj(struct kobject *kobj)
31 {
32 	return container_of(kobj, struct cma_kobject, kobj)->cma;
33 }
34 
alloc_pages_success_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)35 static ssize_t alloc_pages_success_show(struct kobject *kobj,
36 					struct kobj_attribute *attr, char *buf)
37 {
38 	struct cma *cma = cma_from_kobj(kobj);
39 
40 	return sysfs_emit(buf, "%llu\n",
41 			  atomic64_read(&cma->nr_pages_succeeded));
42 }
43 CMA_ATTR_RO(alloc_pages_success);
44 
alloc_pages_fail_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)45 static ssize_t alloc_pages_fail_show(struct kobject *kobj,
46 				     struct kobj_attribute *attr, char *buf)
47 {
48 	struct cma *cma = cma_from_kobj(kobj);
49 
50 	return sysfs_emit(buf, "%llu\n", atomic64_read(&cma->nr_pages_failed));
51 }
52 CMA_ATTR_RO(alloc_pages_fail);
53 
cma_kobj_release(struct kobject * kobj)54 static void cma_kobj_release(struct kobject *kobj)
55 {
56 	struct cma *cma = cma_from_kobj(kobj);
57 	struct cma_kobject *cma_kobj = cma->cma_kobj;
58 
59 	kfree(cma_kobj);
60 	cma->cma_kobj = NULL;
61 }
62 
63 static struct attribute *cma_attrs[] = {
64 	&alloc_pages_success_attr.attr,
65 	&alloc_pages_fail_attr.attr,
66 	NULL,
67 };
68 ATTRIBUTE_GROUPS(cma);
69 
70 static struct kobj_type cma_ktype = {
71 	.release = cma_kobj_release,
72 	.sysfs_ops = &kobj_sysfs_ops,
73 	.default_groups = cma_groups,
74 };
75 
cma_sysfs_init(void)76 static int __init cma_sysfs_init(void)
77 {
78 	struct kobject *cma_kobj_root;
79 	struct cma_kobject *cma_kobj;
80 	struct cma *cma;
81 	int i, err;
82 
83 	if (!experimental)
84 		return 0;
85 
86 	cma_kobj_root = kobject_create_and_add("cma", mm_kobj);
87 	if (!cma_kobj_root)
88 		return -ENOMEM;
89 
90 	for (i = 0; i < cma_area_count; i++) {
91 		cma_kobj = kzalloc(sizeof(*cma_kobj), GFP_KERNEL);
92 		if (!cma_kobj) {
93 			err = -ENOMEM;
94 			goto out;
95 		}
96 
97 		cma = &cma_areas[i];
98 		cma->cma_kobj = cma_kobj;
99 		cma_kobj->cma = cma;
100 		err = kobject_init_and_add(&cma_kobj->kobj, &cma_ktype,
101 					   cma_kobj_root, "%s", cma->name);
102 		if (err) {
103 			kobject_put(&cma_kobj->kobj);
104 			goto out;
105 		}
106 	}
107 
108 	return 0;
109 out:
110 	while (--i >= 0) {
111 		cma = &cma_areas[i];
112 		kobject_put(&cma->cma_kobj->kobj);
113 	}
114 	kobject_put(cma_kobj_root);
115 
116 	return err;
117 }
118 subsys_initcall(cma_sysfs_init);
119 
120 module_param(experimental, bool, 0400);
121