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 bool valid_memtrace_range(struct memtrace_entry *dev,
51 unsigned long start, unsigned long size)
53 if ((start >= dev->start) &&
54 ((start + size) <= (dev->start + dev->size)))
60 static int memtrace_mmap(struct file *filp, struct vm_area_struct *vma)
62 unsigned long size = vma->vm_end - vma->vm_start;
63 struct memtrace_entry *dev = filp->private_data;
65 if (!valid_memtrace_range(dev, vma->vm_pgoff << PAGE_SHIFT, size))
68 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
70 if (remap_pfn_range(vma, vma->vm_start,
71 vma->vm_pgoff + (dev->start >> PAGE_SHIFT),
72 size, vma->vm_page_prot))
78 static const struct file_operations memtrace_fops = {
79 .llseek = default_llseek,
80 .read = memtrace_read,
81 .mmap = memtrace_mmap,
85 static int check_memblock_online(struct memory_block *mem, void *arg)
87 if (mem->state != MEM_ONLINE)
93 static int change_memblock_state(struct memory_block *mem, void *arg)
95 unsigned long state = (unsigned long)arg;
102 static void memtrace_clear_range(unsigned long start_pfn,
103 unsigned long nr_pages)
108 * As pages are offline, we cannot trust the memmap anymore. As HIGHMEM
109 * does not apply, avoid passing around "struct page" and use
110 * clear_page() instead directly.
112 for (pfn = start_pfn; pfn < start_pfn + nr_pages; pfn++) {
113 if (IS_ALIGNED(pfn, PAGES_PER_SECTION))
115 clear_page(__va(PFN_PHYS(pfn)));
119 /* called with device_hotplug_lock held */
120 static bool memtrace_offline_pages(u32 nid, u64 start_pfn, u64 nr_pages)
122 u64 end_pfn = start_pfn + nr_pages - 1;
124 if (walk_memory_range(start_pfn, end_pfn, NULL,
125 check_memblock_online))
128 walk_memory_range(start_pfn, end_pfn, (void *)MEM_GOING_OFFLINE,
129 change_memblock_state);
131 if (offline_pages(start_pfn, nr_pages)) {
132 walk_memory_range(start_pfn, end_pfn, (void *)MEM_ONLINE,
133 change_memblock_state);
137 walk_memory_range(start_pfn, end_pfn, (void *)MEM_OFFLINE,
138 change_memblock_state);
144 static u64 memtrace_alloc_node(u32 nid, u64 size)
146 u64 start_pfn, end_pfn, nr_pages, pfn;
148 u64 bytes = memory_block_size_bytes();
150 if (!NODE_DATA(nid) || !node_spanned_pages(nid))
153 start_pfn = node_start_pfn(nid);
154 end_pfn = node_end_pfn(nid);
155 nr_pages = size >> PAGE_SHIFT;
157 /* Trace memory needs to be aligned to the size */
158 end_pfn = round_down(end_pfn - nr_pages, nr_pages);
160 lock_device_hotplug();
161 for (base_pfn = end_pfn; base_pfn > start_pfn; base_pfn -= nr_pages) {
162 if (memtrace_offline_pages(nid, base_pfn, nr_pages) == true) {
164 * Clear the range while we still have a linear
167 memtrace_clear_range(base_pfn, nr_pages);
169 * Remove memory in memory block size chunks so that
170 * iomem resources are always split to the same size and
171 * we never try to remove memory that spans two iomem
174 end_pfn = base_pfn + nr_pages;
175 for (pfn = base_pfn; pfn < end_pfn; pfn += bytes>> PAGE_SHIFT) {
176 remove_memory(nid, pfn << PAGE_SHIFT, bytes);
178 unlock_device_hotplug();
179 return base_pfn << PAGE_SHIFT;
182 unlock_device_hotplug();
187 static int memtrace_init_regions_runtime(u64 size)
192 memtrace_array = kcalloc(num_online_nodes(),
193 sizeof(struct memtrace_entry), GFP_KERNEL);
194 if (!memtrace_array) {
195 pr_err("Failed to allocate memtrace_array\n");
199 for_each_online_node(nid) {
200 m = memtrace_alloc_node(nid, size);
203 * A node might not have any local memory, so warn but
207 pr_err("Failed to allocate trace memory on node %d\n", nid);
211 pr_info("Allocated trace memory on node %d at 0x%016llx\n", nid, m);
213 memtrace_array[memtrace_array_nr].start = m;
214 memtrace_array[memtrace_array_nr].size = size;
215 memtrace_array[memtrace_array_nr].nid = nid;
222 static struct dentry *memtrace_debugfs_dir;
224 static int memtrace_init_debugfs(void)
229 for (i = 0; i < memtrace_array_nr; i++) {
231 struct memtrace_entry *ent = &memtrace_array[i];
233 ent->mem = ioremap(ent->start, ent->size);
234 /* Warn but continue on */
236 pr_err("Failed to map trace memory at 0x%llx\n",
242 snprintf(ent->name, 16, "%08x", ent->nid);
243 dir = debugfs_create_dir(ent->name, memtrace_debugfs_dir);
248 debugfs_create_file("trace", 0400, dir, ent, &memtrace_fops);
249 debugfs_create_x64("start", 0400, dir, &ent->start);
250 debugfs_create_x64("size", 0400, dir, &ent->size);
256 static int memtrace_enable_set(void *data, u64 val)
264 /* Make sure size is aligned to a memory block */
265 if (val & (memory_block_size_bytes() - 1))
268 if (memtrace_init_regions_runtime(val))
271 if (memtrace_init_debugfs())
279 static int memtrace_enable_get(void *data, u64 *val)
281 *val = memtrace_size;
285 DEFINE_SIMPLE_ATTRIBUTE(memtrace_init_fops, memtrace_enable_get,
286 memtrace_enable_set, "0x%016llx\n");
288 static int memtrace_init(void)
290 memtrace_debugfs_dir = debugfs_create_dir("memtrace",
291 powerpc_debugfs_root);
292 if (!memtrace_debugfs_dir)
295 debugfs_create_file("enable", 0600, memtrace_debugfs_dir,
296 NULL, &memtrace_init_fops);
300 machine_device_initcall(powernv, memtrace_init);