memtrace.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (C) IBM Corporation, 2014, 2017
  3. * Anton Blanchard, Rashmica Gupta.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #define pr_fmt(fmt) "memtrace: " fmt
  11. #include <linux/bitops.h>
  12. #include <linux/string.h>
  13. #include <linux/memblock.h>
  14. #include <linux/init.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/fs.h>
  17. #include <linux/debugfs.h>
  18. #include <linux/slab.h>
  19. #include <linux/memory.h>
  20. #include <linux/memory_hotplug.h>
  21. #include <asm/machdep.h>
  22. #include <asm/debugfs.h>
  23. /* This enables us to keep track of the memory removed from each node. */
  24. struct memtrace_entry {
  25. void *mem;
  26. u64 start;
  27. u64 size;
  28. u32 nid;
  29. struct dentry *dir;
  30. char name[16];
  31. };
  32. static u64 memtrace_size;
  33. static struct memtrace_entry *memtrace_array;
  34. static unsigned int memtrace_array_nr;
  35. static ssize_t memtrace_read(struct file *filp, char __user *ubuf,
  36. size_t count, loff_t *ppos)
  37. {
  38. struct memtrace_entry *ent = filp->private_data;
  39. return simple_read_from_buffer(ubuf, count, ppos, ent->mem, ent->size);
  40. }
  41. static const struct file_operations memtrace_fops = {
  42. .llseek = default_llseek,
  43. .read = memtrace_read,
  44. .open = simple_open,
  45. };
  46. static int check_memblock_online(struct memory_block *mem, void *arg)
  47. {
  48. if (mem->state != MEM_ONLINE)
  49. return -1;
  50. return 0;
  51. }
  52. static int change_memblock_state(struct memory_block *mem, void *arg)
  53. {
  54. unsigned long state = (unsigned long)arg;
  55. mem->state = state;
  56. return 0;
  57. }
  58. /* called with device_hotplug_lock held */
  59. static bool memtrace_offline_pages(u32 nid, u64 start_pfn, u64 nr_pages)
  60. {
  61. u64 end_pfn = start_pfn + nr_pages - 1;
  62. if (walk_memory_range(start_pfn, end_pfn, NULL,
  63. check_memblock_online))
  64. return false;
  65. walk_memory_range(start_pfn, end_pfn, (void *)MEM_GOING_OFFLINE,
  66. change_memblock_state);
  67. if (offline_pages(start_pfn, nr_pages)) {
  68. walk_memory_range(start_pfn, end_pfn, (void *)MEM_ONLINE,
  69. change_memblock_state);
  70. return false;
  71. }
  72. walk_memory_range(start_pfn, end_pfn, (void *)MEM_OFFLINE,
  73. change_memblock_state);
  74. return true;
  75. }
  76. static u64 memtrace_alloc_node(u32 nid, u64 size)
  77. {
  78. u64 start_pfn, end_pfn, nr_pages, pfn;
  79. u64 base_pfn;
  80. u64 bytes = memory_block_size_bytes();
  81. if (!node_spanned_pages(nid))
  82. return 0;
  83. start_pfn = node_start_pfn(nid);
  84. end_pfn = node_end_pfn(nid);
  85. nr_pages = size >> PAGE_SHIFT;
  86. /* Trace memory needs to be aligned to the size */
  87. end_pfn = round_down(end_pfn - nr_pages, nr_pages);
  88. lock_device_hotplug();
  89. for (base_pfn = end_pfn; base_pfn > start_pfn; base_pfn -= nr_pages) {
  90. if (memtrace_offline_pages(nid, base_pfn, nr_pages) == true) {
  91. /*
  92. * Remove memory in memory block size chunks so that
  93. * iomem resources are always split to the same size and
  94. * we never try to remove memory that spans two iomem
  95. * resources.
  96. */
  97. end_pfn = base_pfn + nr_pages;
  98. for (pfn = base_pfn; pfn < end_pfn; pfn += bytes>> PAGE_SHIFT) {
  99. __remove_memory(nid, pfn << PAGE_SHIFT, bytes);
  100. }
  101. unlock_device_hotplug();
  102. return base_pfn << PAGE_SHIFT;
  103. }
  104. }
  105. unlock_device_hotplug();
  106. return 0;
  107. }
  108. static int memtrace_init_regions_runtime(u64 size)
  109. {
  110. u32 nid;
  111. u64 m;
  112. memtrace_array = kcalloc(num_online_nodes(),
  113. sizeof(struct memtrace_entry), GFP_KERNEL);
  114. if (!memtrace_array) {
  115. pr_err("Failed to allocate memtrace_array\n");
  116. return -EINVAL;
  117. }
  118. for_each_online_node(nid) {
  119. m = memtrace_alloc_node(nid, size);
  120. /*
  121. * A node might not have any local memory, so warn but
  122. * continue on.
  123. */
  124. if (!m) {
  125. pr_err("Failed to allocate trace memory on node %d\n", nid);
  126. continue;
  127. }
  128. pr_info("Allocated trace memory on node %d at 0x%016llx\n", nid, m);
  129. memtrace_array[memtrace_array_nr].start = m;
  130. memtrace_array[memtrace_array_nr].size = size;
  131. memtrace_array[memtrace_array_nr].nid = nid;
  132. memtrace_array_nr++;
  133. }
  134. return 0;
  135. }
  136. static struct dentry *memtrace_debugfs_dir;
  137. static int memtrace_init_debugfs(void)
  138. {
  139. int ret = 0;
  140. int i;
  141. for (i = 0; i < memtrace_array_nr; i++) {
  142. struct dentry *dir;
  143. struct memtrace_entry *ent = &memtrace_array[i];
  144. ent->mem = ioremap(ent->start, ent->size);
  145. /* Warn but continue on */
  146. if (!ent->mem) {
  147. pr_err("Failed to map trace memory at 0x%llx\n",
  148. ent->start);
  149. ret = -1;
  150. continue;
  151. }
  152. snprintf(ent->name, 16, "%08x", ent->nid);
  153. dir = debugfs_create_dir(ent->name, memtrace_debugfs_dir);
  154. if (!dir) {
  155. pr_err("Failed to create debugfs directory for node %d\n",
  156. ent->nid);
  157. return -1;
  158. }
  159. ent->dir = dir;
  160. debugfs_create_file("trace", 0400, dir, ent, &memtrace_fops);
  161. debugfs_create_x64("start", 0400, dir, &ent->start);
  162. debugfs_create_x64("size", 0400, dir, &ent->size);
  163. }
  164. return ret;
  165. }
  166. static int online_mem_block(struct memory_block *mem, void *arg)
  167. {
  168. return device_online(&mem->dev);
  169. }
  170. /*
  171. * Iterate through the chunks of memory we have removed from the kernel
  172. * and attempt to add them back to the kernel.
  173. */
  174. static int memtrace_online(void)
  175. {
  176. int i, ret = 0;
  177. struct memtrace_entry *ent;
  178. for (i = memtrace_array_nr - 1; i >= 0; i--) {
  179. ent = &memtrace_array[i];
  180. /* We have onlined this chunk previously */
  181. if (ent->nid == -1)
  182. continue;
  183. /* Remove from io mappings */
  184. if (ent->mem) {
  185. iounmap(ent->mem);
  186. ent->mem = 0;
  187. }
  188. if (add_memory(ent->nid, ent->start, ent->size)) {
  189. pr_err("Failed to add trace memory to node %d\n",
  190. ent->nid);
  191. ret += 1;
  192. continue;
  193. }
  194. /*
  195. * If kernel isn't compiled with the auto online option
  196. * we need to online the memory ourselves.
  197. */
  198. if (!memhp_auto_online) {
  199. lock_device_hotplug();
  200. walk_memory_range(PFN_DOWN(ent->start),
  201. PFN_UP(ent->start + ent->size - 1),
  202. NULL, online_mem_block);
  203. unlock_device_hotplug();
  204. }
  205. /*
  206. * Memory was added successfully so clean up references to it
  207. * so on reentry we can tell that this chunk was added.
  208. */
  209. debugfs_remove_recursive(ent->dir);
  210. pr_info("Added trace memory back to node %d\n", ent->nid);
  211. ent->size = ent->start = ent->nid = -1;
  212. }
  213. if (ret)
  214. return ret;
  215. /* If all chunks of memory were added successfully, reset globals */
  216. kfree(memtrace_array);
  217. memtrace_array = NULL;
  218. memtrace_size = 0;
  219. memtrace_array_nr = 0;
  220. return 0;
  221. }
  222. static int memtrace_enable_set(void *data, u64 val)
  223. {
  224. u64 bytes;
  225. /*
  226. * Don't attempt to do anything if size isn't aligned to a memory
  227. * block or equal to zero.
  228. */
  229. bytes = memory_block_size_bytes();
  230. if (val & (bytes - 1)) {
  231. pr_err("Value must be aligned with 0x%llx\n", bytes);
  232. return -EINVAL;
  233. }
  234. /* Re-add/online previously removed/offlined memory */
  235. if (memtrace_size) {
  236. if (memtrace_online())
  237. return -EAGAIN;
  238. }
  239. if (!val)
  240. return 0;
  241. /* Offline and remove memory */
  242. if (memtrace_init_regions_runtime(val))
  243. return -EINVAL;
  244. if (memtrace_init_debugfs())
  245. return -EINVAL;
  246. memtrace_size = val;
  247. return 0;
  248. }
  249. static int memtrace_enable_get(void *data, u64 *val)
  250. {
  251. *val = memtrace_size;
  252. return 0;
  253. }
  254. DEFINE_SIMPLE_ATTRIBUTE(memtrace_init_fops, memtrace_enable_get,
  255. memtrace_enable_set, "0x%016llx\n");
  256. static int memtrace_init(void)
  257. {
  258. memtrace_debugfs_dir = debugfs_create_dir("memtrace",
  259. powerpc_debugfs_root);
  260. if (!memtrace_debugfs_dir)
  261. return -1;
  262. debugfs_create_file("enable", 0600, memtrace_debugfs_dir,
  263. NULL, &memtrace_init_fops);
  264. return 0;
  265. }
  266. machine_device_initcall(powernv, memtrace_init);