2 * Copyright (C) IBM Corporation, 2014, 2017
3 * Anton Blanchard, Rashmica Gupta.
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.
11 #define pr_fmt(fmt) "memtrace: " fmt
13 #include <linux/bitops.h>
14 #include <linux/string.h>
15 #include <linux/memblock.h>
16 #include <linux/init.h>
17 #include <linux/moduleparam.h>
19 #include <linux/debugfs.h>
20 #include <linux/slab.h>
21 #include <linux/memory.h>
22 #include <linux/memory_hotplug.h>
23 #include <asm/machdep.h>
24 #include <asm/debugfs.h>
26 /* This enables us to keep track of the memory removed from each node. */
27 struct memtrace_entry {
36 static u64 memtrace_size;
38 static struct memtrace_entry *memtrace_array;
39 static unsigned int memtrace_array_nr;
42 static ssize_t memtrace_read(struct file *filp, char __user *ubuf,
43 size_t count, loff_t *ppos)
45 struct memtrace_entry *ent = filp->private_data;
47 return simple_read_from_buffer(ubuf, count, ppos, ent->mem, ent->size);
50 static const struct file_operations memtrace_fops = {
51 .llseek = default_llseek,
52 .read = memtrace_read,
56 static int check_memblock_online(struct memory_block *mem, void *arg)
58 if (mem->state != MEM_ONLINE)
64 static int change_memblock_state(struct memory_block *mem, void *arg)
66 unsigned long state = (unsigned long)arg;
73 static bool memtrace_offline_pages(u32 nid, u64 start_pfn, u64 nr_pages)
75 u64 end_pfn = start_pfn + nr_pages - 1;
77 if (walk_memory_range(start_pfn, end_pfn, NULL,
78 check_memblock_online))
81 walk_memory_range(start_pfn, end_pfn, (void *)MEM_GOING_OFFLINE,
82 change_memblock_state);
84 if (offline_pages(start_pfn, nr_pages)) {
85 walk_memory_range(start_pfn, end_pfn, (void *)MEM_ONLINE,
86 change_memblock_state);
90 walk_memory_range(start_pfn, end_pfn, (void *)MEM_OFFLINE,
91 change_memblock_state);
97 static u64 memtrace_alloc_node(u32 nid, u64 size)
99 u64 start_pfn, end_pfn, nr_pages, pfn;
101 u64 bytes = memory_block_size_bytes();
103 if (!node_spanned_pages(nid))
106 start_pfn = node_start_pfn(nid);
107 end_pfn = node_end_pfn(nid);
108 nr_pages = size >> PAGE_SHIFT;
110 /* Trace memory needs to be aligned to the size */
111 end_pfn = round_down(end_pfn - nr_pages, nr_pages);
113 for (base_pfn = end_pfn; base_pfn > start_pfn; base_pfn -= nr_pages) {
114 if (memtrace_offline_pages(nid, base_pfn, nr_pages) == true) {
116 * Remove memory in memory block size chunks so that
117 * iomem resources are always split to the same size and
118 * we never try to remove memory that spans two iomem
121 lock_device_hotplug();
122 end_pfn = base_pfn + nr_pages;
123 for (pfn = base_pfn; pfn < end_pfn; pfn += bytes>> PAGE_SHIFT) {
124 remove_memory(nid, pfn << PAGE_SHIFT, bytes);
126 unlock_device_hotplug();
127 return base_pfn << PAGE_SHIFT;
134 static int memtrace_init_regions_runtime(u64 size)
139 memtrace_array = kcalloc(num_online_nodes(),
140 sizeof(struct memtrace_entry), GFP_KERNEL);
141 if (!memtrace_array) {
142 pr_err("Failed to allocate memtrace_array\n");
146 for_each_online_node(nid) {
147 m = memtrace_alloc_node(nid, size);
150 * A node might not have any local memory, so warn but
154 pr_err("Failed to allocate trace memory on node %d\n", nid);
158 pr_info("Allocated trace memory on node %d at 0x%016llx\n", nid, m);
160 memtrace_array[memtrace_array_nr].start = m;
161 memtrace_array[memtrace_array_nr].size = size;
162 memtrace_array[memtrace_array_nr].nid = nid;
169 static struct dentry *memtrace_debugfs_dir;
171 static int memtrace_init_debugfs(void)
176 for (i = 0; i < memtrace_array_nr; i++) {
178 struct memtrace_entry *ent = &memtrace_array[i];
180 ent->mem = ioremap(ent->start, ent->size);
181 /* Warn but continue on */
183 pr_err("Failed to map trace memory at 0x%llx\n",
189 snprintf(ent->name, 16, "%08x", ent->nid);
190 dir = debugfs_create_dir(ent->name, memtrace_debugfs_dir);
192 pr_err("Failed to create debugfs directory for node %d\n",
198 debugfs_create_file("trace", 0400, dir, ent, &memtrace_fops);
199 debugfs_create_x64("start", 0400, dir, &ent->start);
200 debugfs_create_x64("size", 0400, dir, &ent->size);
206 static int online_mem_block(struct memory_block *mem, void *arg)
208 return device_online(&mem->dev);
212 * Iterate through the chunks of memory we have removed from the kernel
213 * and attempt to add them back to the kernel.
215 static int memtrace_online(void)
218 struct memtrace_entry *ent;
220 for (i = memtrace_array_nr - 1; i >= 0; i--) {
221 ent = &memtrace_array[i];
223 /* We have onlined this chunk previously */
227 /* Remove from io mappings */
233 if (add_memory(ent->nid, ent->start, ent->size)) {
234 pr_err("Failed to add trace memory to node %d\n",
241 * If kernel isn't compiled with the auto online option
242 * we need to online the memory ourselves.
244 if (!memhp_auto_online) {
245 walk_memory_range(PFN_DOWN(ent->start),
246 PFN_UP(ent->start + ent->size - 1),
247 NULL, online_mem_block);
251 * Memory was added successfully so clean up references to it
252 * so on reentry we can tell that this chunk was added.
254 debugfs_remove_recursive(ent->dir);
255 pr_info("Added trace memory back to node %d\n", ent->nid);
256 ent->size = ent->start = ent->nid = -1;
261 /* If all chunks of memory were added successfully, reset globals */
262 kfree(memtrace_array);
263 memtrace_array = NULL;
265 memtrace_array_nr = 0;
269 static int memtrace_enable_set(void *data, u64 val)
274 * Don't attempt to do anything if size isn't aligned to a memory
275 * block or equal to zero.
277 bytes = memory_block_size_bytes();
278 if (val & (bytes - 1)) {
279 pr_err("Value must be aligned with 0x%llx\n", bytes);
283 /* Re-add/online previously removed/offlined memory */
285 if (memtrace_online())
292 /* Offline and remove memory */
293 if (memtrace_init_regions_runtime(val))
296 if (memtrace_init_debugfs())
304 static int memtrace_enable_get(void *data, u64 *val)
306 *val = memtrace_size;
310 DEFINE_SIMPLE_ATTRIBUTE(memtrace_init_fops, memtrace_enable_get,
311 memtrace_enable_set, "0x%016llx\n");
313 static int memtrace_init(void)
315 memtrace_debugfs_dir = debugfs_create_dir("memtrace",
316 powerpc_debugfs_root);
317 if (!memtrace_debugfs_dir)
320 debugfs_create_file("enable", 0600, memtrace_debugfs_dir,
321 NULL, &memtrace_init_fops);
325 machine_device_initcall(powernv, memtrace_init);