blob: d552b2a1506bf843bf13fb6b1c0f621159af9ff4 [file] [log] [blame]
Minchan Kima5903592021-03-24 16:10:53 -07001// 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>
Minchan Kim2cf6f072021-03-24 16:24:08 -070011#include <linux/module.h>
Minchan Kima5903592021-03-24 16:10:53 -070012
13#include "cma.h"
14
Minchan Kim2cf6f072021-03-24 16:24:08 -070015static bool experimental;
16
Minchan Kima5903592021-03-24 16:10:53 -070017#define CMA_ATTR_RO(_name) \
18 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
19
20void cma_sysfs_account_success_pages(struct cma *cma, unsigned long nr_pages)
21{
22 atomic64_add(nr_pages, &cma->nr_pages_succeeded);
23}
24
25void cma_sysfs_account_fail_pages(struct cma *cma, unsigned long nr_pages)
26{
27 atomic64_add(nr_pages, &cma->nr_pages_failed);
28}
29
30static inline struct cma *cma_from_kobj(struct kobject *kobj)
31{
32 return container_of(kobj, struct cma_kobject, kobj)->cma;
33}
34
35static 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}
43CMA_ATTR_RO(alloc_pages_success);
44
45static 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}
52CMA_ATTR_RO(alloc_pages_fail);
53
54static 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
63static struct attribute *cma_attrs[] = {
64 &alloc_pages_success_attr.attr,
65 &alloc_pages_fail_attr.attr,
66 NULL,
67};
68ATTRIBUTE_GROUPS(cma);
69
70static struct kobj_type cma_ktype = {
71 .release = cma_kobj_release,
72 .sysfs_ops = &kobj_sysfs_ops,
73 .default_groups = cma_groups,
74};
75
76static 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
Minchan Kim2cf6f072021-03-24 16:24:08 -070083 if (!experimental)
84 return 0;
85
Minchan Kima5903592021-03-24 16:10:53 -070086 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;
109out:
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}
118subsys_initcall(cma_sysfs_init);
Minchan Kim2cf6f072021-03-24 16:24:08 -0700119
120module_param(experimental, bool, 0400);