1 // SPDX-License-Identifier: GPL-2.0
3 * CMA DebugFS Interface
5 * Copyright (c) 2015 Sasha Levin <sasha.levin@oracle.com>
9 #include <linux/debugfs.h>
10 #include <linux/cma.h>
11 #include <linux/list.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/mm_types.h>
19 struct hlist_node node
;
24 static struct dentry
*cma_debugfs_root
;
26 static int cma_debugfs_get(void *data
, u64
*val
)
28 unsigned long *p
= data
;
34 DEFINE_SIMPLE_ATTRIBUTE(cma_debugfs_fops
, cma_debugfs_get
, NULL
, "%llu\n");
36 static int cma_used_get(void *data
, u64
*val
)
38 struct cma
*cma
= data
;
41 mutex_lock(&cma
->lock
);
42 /* pages counter is smaller than sizeof(int) */
43 used
= bitmap_weight(cma
->bitmap
, (int)cma_bitmap_maxno(cma
));
44 mutex_unlock(&cma
->lock
);
45 *val
= (u64
)used
<< cma
->order_per_bit
;
49 DEFINE_SIMPLE_ATTRIBUTE(cma_used_fops
, cma_used_get
, NULL
, "%llu\n");
51 static int cma_maxchunk_get(void *data
, u64
*val
)
53 struct cma
*cma
= data
;
54 unsigned long maxchunk
= 0;
55 unsigned long start
, end
= 0;
56 unsigned long bitmap_maxno
= cma_bitmap_maxno(cma
);
58 mutex_lock(&cma
->lock
);
60 start
= find_next_zero_bit(cma
->bitmap
, bitmap_maxno
, end
);
61 if (start
>= cma
->count
)
63 end
= find_next_bit(cma
->bitmap
, bitmap_maxno
, start
);
64 maxchunk
= max(end
- start
, maxchunk
);
66 mutex_unlock(&cma
->lock
);
67 *val
= (u64
)maxchunk
<< cma
->order_per_bit
;
71 DEFINE_SIMPLE_ATTRIBUTE(cma_maxchunk_fops
, cma_maxchunk_get
, NULL
, "%llu\n");
73 static void cma_add_to_cma_mem_list(struct cma
*cma
, struct cma_mem
*mem
)
75 spin_lock(&cma
->mem_head_lock
);
76 hlist_add_head(&mem
->node
, &cma
->mem_head
);
77 spin_unlock(&cma
->mem_head_lock
);
80 static struct cma_mem
*cma_get_entry_from_list(struct cma
*cma
)
82 struct cma_mem
*mem
= NULL
;
84 spin_lock(&cma
->mem_head_lock
);
85 if (!hlist_empty(&cma
->mem_head
)) {
86 mem
= hlist_entry(cma
->mem_head
.first
, struct cma_mem
, node
);
87 hlist_del_init(&mem
->node
);
89 spin_unlock(&cma
->mem_head_lock
);
94 static int cma_free_mem(struct cma
*cma
, int count
)
96 struct cma_mem
*mem
= NULL
;
99 mem
= cma_get_entry_from_list(cma
);
103 if (mem
->n
<= count
) {
104 cma_release(cma
, mem
->p
, mem
->n
);
107 } else if (cma
->order_per_bit
== 0) {
108 cma_release(cma
, mem
->p
, count
);
112 cma_add_to_cma_mem_list(cma
, mem
);
114 pr_debug("cma: cannot release partial block when order_per_bit != 0\n");
115 cma_add_to_cma_mem_list(cma
, mem
);
124 static int cma_free_write(void *data
, u64 val
)
127 struct cma
*cma
= data
;
129 return cma_free_mem(cma
, pages
);
131 DEFINE_SIMPLE_ATTRIBUTE(cma_free_fops
, NULL
, cma_free_write
, "%llu\n");
133 static int cma_alloc_mem(struct cma
*cma
, int count
)
138 mem
= kzalloc(sizeof(*mem
), GFP_KERNEL
);
142 p
= cma_alloc(cma
, count
, 0, false);
151 cma_add_to_cma_mem_list(cma
, mem
);
156 static int cma_alloc_write(void *data
, u64 val
)
159 struct cma
*cma
= data
;
161 return cma_alloc_mem(cma
, pages
);
163 DEFINE_SIMPLE_ATTRIBUTE(cma_alloc_fops
, NULL
, cma_alloc_write
, "%llu\n");
165 static void cma_debugfs_add_one(struct cma
*cma
, int idx
)
171 scnprintf(name
, sizeof(name
), "cma-%s", cma
->name
);
173 tmp
= debugfs_create_dir(name
, cma_debugfs_root
);
175 debugfs_create_file("alloc", 0200, tmp
, cma
, &cma_alloc_fops
);
176 debugfs_create_file("free", 0200, tmp
, cma
, &cma_free_fops
);
177 debugfs_create_file("base_pfn", 0444, tmp
,
178 &cma
->base_pfn
, &cma_debugfs_fops
);
179 debugfs_create_file("count", 0444, tmp
, &cma
->count
, &cma_debugfs_fops
);
180 debugfs_create_file("order_per_bit", 0444, tmp
,
181 &cma
->order_per_bit
, &cma_debugfs_fops
);
182 debugfs_create_file("used", 0444, tmp
, cma
, &cma_used_fops
);
183 debugfs_create_file("maxchunk", 0444, tmp
, cma
, &cma_maxchunk_fops
);
185 u32s
= DIV_ROUND_UP(cma_bitmap_maxno(cma
), BITS_PER_BYTE
* sizeof(u32
));
186 debugfs_create_u32_array("bitmap", 0444, tmp
, (u32
*)cma
->bitmap
, u32s
);
189 static int __init
cma_debugfs_init(void)
193 cma_debugfs_root
= debugfs_create_dir("cma", NULL
);
194 if (!cma_debugfs_root
)
197 for (i
= 0; i
< cma_area_count
; i
++)
198 cma_debugfs_add_one(&cma_areas
[i
], i
);
202 late_initcall(cma_debugfs_init
);