]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - mm/cma_sysfs.c
drm/amdgpu: differentiate external rev id for gfx 11.5.0
[thirdparty/kernel/stable.git] / mm / cma_sysfs.c
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
12 #include "cma.h"
13
14 #define CMA_ATTR_RO(_name) \
15 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
16
17 void cma_sysfs_account_success_pages(struct cma *cma, unsigned long nr_pages)
18 {
19 atomic64_add(nr_pages, &cma->nr_pages_succeeded);
20 }
21
22 void cma_sysfs_account_fail_pages(struct cma *cma, unsigned long nr_pages)
23 {
24 atomic64_add(nr_pages, &cma->nr_pages_failed);
25 }
26
27 void cma_sysfs_account_release_pages(struct cma *cma, unsigned long nr_pages)
28 {
29 atomic64_add(nr_pages, &cma->nr_pages_released);
30 }
31
32 static inline struct cma *cma_from_kobj(struct kobject *kobj)
33 {
34 return container_of(kobj, struct cma_kobject, kobj)->cma;
35 }
36
37 static ssize_t alloc_pages_success_show(struct kobject *kobj,
38 struct kobj_attribute *attr, char *buf)
39 {
40 struct cma *cma = cma_from_kobj(kobj);
41
42 return sysfs_emit(buf, "%llu\n",
43 atomic64_read(&cma->nr_pages_succeeded));
44 }
45 CMA_ATTR_RO(alloc_pages_success);
46
47 static ssize_t alloc_pages_fail_show(struct kobject *kobj,
48 struct kobj_attribute *attr, char *buf)
49 {
50 struct cma *cma = cma_from_kobj(kobj);
51
52 return sysfs_emit(buf, "%llu\n", atomic64_read(&cma->nr_pages_failed));
53 }
54 CMA_ATTR_RO(alloc_pages_fail);
55
56 static ssize_t release_pages_success_show(struct kobject *kobj,
57 struct kobj_attribute *attr, char *buf)
58 {
59 struct cma *cma = cma_from_kobj(kobj);
60
61 return sysfs_emit(buf, "%llu\n", atomic64_read(&cma->nr_pages_released));
62 }
63 CMA_ATTR_RO(release_pages_success);
64
65 static void cma_kobj_release(struct kobject *kobj)
66 {
67 struct cma *cma = cma_from_kobj(kobj);
68 struct cma_kobject *cma_kobj = cma->cma_kobj;
69
70 kfree(cma_kobj);
71 cma->cma_kobj = NULL;
72 }
73
74 static struct attribute *cma_attrs[] = {
75 &alloc_pages_success_attr.attr,
76 &alloc_pages_fail_attr.attr,
77 &release_pages_success_attr.attr,
78 NULL,
79 };
80 ATTRIBUTE_GROUPS(cma);
81
82 static const struct kobj_type cma_ktype = {
83 .release = cma_kobj_release,
84 .sysfs_ops = &kobj_sysfs_ops,
85 .default_groups = cma_groups,
86 };
87
88 static int __init cma_sysfs_init(void)
89 {
90 struct kobject *cma_kobj_root;
91 struct cma_kobject *cma_kobj;
92 struct cma *cma;
93 int i, err;
94
95 cma_kobj_root = kobject_create_and_add("cma", mm_kobj);
96 if (!cma_kobj_root)
97 return -ENOMEM;
98
99 for (i = 0; i < cma_area_count; i++) {
100 cma_kobj = kzalloc(sizeof(*cma_kobj), GFP_KERNEL);
101 if (!cma_kobj) {
102 err = -ENOMEM;
103 goto out;
104 }
105
106 cma = &cma_areas[i];
107 cma->cma_kobj = cma_kobj;
108 cma_kobj->cma = cma;
109 err = kobject_init_and_add(&cma_kobj->kobj, &cma_ktype,
110 cma_kobj_root, "%s", cma->name);
111 if (err) {
112 kobject_put(&cma_kobj->kobj);
113 goto out;
114 }
115 }
116
117 return 0;
118 out:
119 while (--i >= 0) {
120 cma = &cma_areas[i];
121 kobject_put(&cma->cma_kobj->kobj);
122 }
123 kobject_put(cma_kobj_root);
124
125 return err;
126 }
127 subsys_initcall(cma_sysfs_init);