Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * CMA SysFS Interface
0004  *
0005  * Copyright (c) 2021 Minchan Kim <minchan@kernel.org>
0006  */
0007 
0008 #include <linux/cma.h>
0009 #include <linux/kernel.h>
0010 #include <linux/slab.h>
0011 
0012 #include "cma.h"
0013 
0014 #define CMA_ATTR_RO(_name) \
0015     static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
0016 
0017 void cma_sysfs_account_success_pages(struct cma *cma, unsigned long nr_pages)
0018 {
0019     atomic64_add(nr_pages, &cma->nr_pages_succeeded);
0020 }
0021 
0022 void cma_sysfs_account_fail_pages(struct cma *cma, unsigned long nr_pages)
0023 {
0024     atomic64_add(nr_pages, &cma->nr_pages_failed);
0025 }
0026 
0027 static inline struct cma *cma_from_kobj(struct kobject *kobj)
0028 {
0029     return container_of(kobj, struct cma_kobject, kobj)->cma;
0030 }
0031 
0032 static ssize_t alloc_pages_success_show(struct kobject *kobj,
0033                     struct kobj_attribute *attr, char *buf)
0034 {
0035     struct cma *cma = cma_from_kobj(kobj);
0036 
0037     return sysfs_emit(buf, "%llu\n",
0038               atomic64_read(&cma->nr_pages_succeeded));
0039 }
0040 CMA_ATTR_RO(alloc_pages_success);
0041 
0042 static ssize_t alloc_pages_fail_show(struct kobject *kobj,
0043                      struct kobj_attribute *attr, char *buf)
0044 {
0045     struct cma *cma = cma_from_kobj(kobj);
0046 
0047     return sysfs_emit(buf, "%llu\n", atomic64_read(&cma->nr_pages_failed));
0048 }
0049 CMA_ATTR_RO(alloc_pages_fail);
0050 
0051 static void cma_kobj_release(struct kobject *kobj)
0052 {
0053     struct cma *cma = cma_from_kobj(kobj);
0054     struct cma_kobject *cma_kobj = cma->cma_kobj;
0055 
0056     kfree(cma_kobj);
0057     cma->cma_kobj = NULL;
0058 }
0059 
0060 static struct attribute *cma_attrs[] = {
0061     &alloc_pages_success_attr.attr,
0062     &alloc_pages_fail_attr.attr,
0063     NULL,
0064 };
0065 ATTRIBUTE_GROUPS(cma);
0066 
0067 static struct kobj_type cma_ktype = {
0068     .release = cma_kobj_release,
0069     .sysfs_ops = &kobj_sysfs_ops,
0070     .default_groups = cma_groups,
0071 };
0072 
0073 static int __init cma_sysfs_init(void)
0074 {
0075     struct kobject *cma_kobj_root;
0076     struct cma_kobject *cma_kobj;
0077     struct cma *cma;
0078     int i, err;
0079 
0080     cma_kobj_root = kobject_create_and_add("cma", mm_kobj);
0081     if (!cma_kobj_root)
0082         return -ENOMEM;
0083 
0084     for (i = 0; i < cma_area_count; i++) {
0085         cma_kobj = kzalloc(sizeof(*cma_kobj), GFP_KERNEL);
0086         if (!cma_kobj) {
0087             err = -ENOMEM;
0088             goto out;
0089         }
0090 
0091         cma = &cma_areas[i];
0092         cma->cma_kobj = cma_kobj;
0093         cma_kobj->cma = cma;
0094         err = kobject_init_and_add(&cma_kobj->kobj, &cma_ktype,
0095                        cma_kobj_root, "%s", cma->name);
0096         if (err) {
0097             kobject_put(&cma_kobj->kobj);
0098             goto out;
0099         }
0100     }
0101 
0102     return 0;
0103 out:
0104     while (--i >= 0) {
0105         cma = &cma_areas[i];
0106         kobject_put(&cma->cma_kobj->kobj);
0107     }
0108     kobject_put(cma_kobj_root);
0109 
0110     return err;
0111 }
0112 subsys_initcall(cma_sysfs_init);