cma_debug.c 4.8 KB

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