^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * CMA DebugFS Interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2015 Sasha Levin <sasha.levin@oracle.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/cma.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/mm_types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "cma.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct cma_mem {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct hlist_node node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct page *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned long n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int cma_debugfs_get(void *data, u64 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned long *p = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *val = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) DEFINE_DEBUGFS_ATTRIBUTE(cma_debugfs_fops, cma_debugfs_get, NULL, "%llu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int cma_used_get(void *data, u64 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct cma *cma = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned long used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) mutex_lock(&cma->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* pages counter is smaller than sizeof(int) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) used = bitmap_weight(cma->bitmap, (int)cma_bitmap_maxno(cma));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) mutex_unlock(&cma->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *val = (u64)used << cma->order_per_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) DEFINE_DEBUGFS_ATTRIBUTE(cma_used_fops, cma_used_get, NULL, "%llu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int cma_maxchunk_get(void *data, u64 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct cma *cma = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned long maxchunk = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned long start, end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned long bitmap_maxno = cma_bitmap_maxno(cma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) mutex_lock(&cma->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) start = find_next_zero_bit(cma->bitmap, bitmap_maxno, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (start >= bitmap_maxno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) end = find_next_bit(cma->bitmap, bitmap_maxno, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) maxchunk = max(end - start, maxchunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) mutex_unlock(&cma->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *val = (u64)maxchunk << cma->order_per_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) DEFINE_DEBUGFS_ATTRIBUTE(cma_maxchunk_fops, cma_maxchunk_get, NULL, "%llu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static void cma_add_to_cma_mem_list(struct cma *cma, struct cma_mem *mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) spin_lock(&cma->mem_head_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) hlist_add_head(&mem->node, &cma->mem_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) spin_unlock(&cma->mem_head_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static struct cma_mem *cma_get_entry_from_list(struct cma *cma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct cma_mem *mem = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) spin_lock(&cma->mem_head_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!hlist_empty(&cma->mem_head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) mem = hlist_entry(cma->mem_head.first, struct cma_mem, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) hlist_del_init(&mem->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) spin_unlock(&cma->mem_head_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static int cma_free_mem(struct cma *cma, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct cma_mem *mem = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) while (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) mem = cma_get_entry_from_list(cma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (mem == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (mem->n <= count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) cma_release(cma, mem->p, mem->n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) count -= mem->n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) kfree(mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) } else if (cma->order_per_bit == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) cma_release(cma, mem->p, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) mem->p += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) mem->n -= count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) cma_add_to_cma_mem_list(cma, mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) pr_debug("cma: cannot release partial block when order_per_bit != 0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) cma_add_to_cma_mem_list(cma, mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int cma_free_write(void *data, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) int pages = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct cma *cma = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return cma_free_mem(cma, pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) DEFINE_DEBUGFS_ATTRIBUTE(cma_free_fops, NULL, cma_free_write, "%llu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static int cma_alloc_mem(struct cma *cma, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct cma_mem *mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct page *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) mem = kzalloc(sizeof(*mem), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (!mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) p = cma_alloc(cma, count, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) kfree(mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) mem->p = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) mem->n = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) cma_add_to_cma_mem_list(cma, mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static int cma_alloc_write(void *data, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int pages = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct cma *cma = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return cma_alloc_mem(cma, pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) DEFINE_DEBUGFS_ATTRIBUTE(cma_alloc_fops, NULL, cma_alloc_write, "%llu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static void cma_debugfs_add_one(struct cma *cma, struct dentry *root_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct dentry *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) char name[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) scnprintf(name, sizeof(name), "cma-%s", cma->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) tmp = debugfs_create_dir(name, root_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) debugfs_create_file("alloc", 0200, tmp, cma, &cma_alloc_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) debugfs_create_file("free", 0200, tmp, cma, &cma_free_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) debugfs_create_file("base_pfn", 0444, tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) &cma->base_pfn, &cma_debugfs_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) debugfs_create_file("count", 0444, tmp, &cma->count, &cma_debugfs_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) debugfs_create_file("order_per_bit", 0444, tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) &cma->order_per_bit, &cma_debugfs_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) debugfs_create_file("used", 0444, tmp, cma, &cma_used_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) debugfs_create_file("maxchunk", 0444, tmp, cma, &cma_maxchunk_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) cma->dfs_bitmap.array = (u32 *)cma->bitmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) cma->dfs_bitmap.n_elements = DIV_ROUND_UP(cma_bitmap_maxno(cma),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) BITS_PER_BYTE * sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) debugfs_create_u32_array("bitmap", 0444, tmp, &cma->dfs_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static int __init cma_debugfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct dentry *cma_debugfs_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) cma_debugfs_root = debugfs_create_dir("cma", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) for (i = 0; i < cma_area_count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) cma_debugfs_add_one(&cma_areas[i], cma_debugfs_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) late_initcall(cma_debugfs_init);