GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / powerpc / platforms / powernv / memtrace.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) IBM Corporation, 2014, 2017
4  * Anton Blanchard, Rashmica Gupta.
5  */
6
7 #define pr_fmt(fmt) "memtrace: " fmt
8
9 #include <linux/bitops.h>
10 #include <linux/string.h>
11 #include <linux/memblock.h>
12 #include <linux/init.h>
13 #include <linux/moduleparam.h>
14 #include <linux/fs.h>
15 #include <linux/debugfs.h>
16 #include <linux/slab.h>
17 #include <linux/memory.h>
18 #include <linux/memory_hotplug.h>
19 #include <linux/numa.h>
20 #include <asm/machdep.h>
21 #include <asm/debugfs.h>
22
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
33 static DEFINE_MUTEX(memtrace_mutex);
34 static u64 memtrace_size;
35
36 static struct memtrace_entry *memtrace_array;
37 static unsigned int memtrace_array_nr;
38
39
40 static ssize_t memtrace_read(struct file *filp, char __user *ubuf,
41                              size_t count, loff_t *ppos)
42 {
43         struct memtrace_entry *ent = filp->private_data;
44
45         return simple_read_from_buffer(ubuf, count, ppos, ent->mem, ent->size);
46 }
47
48 static const struct file_operations memtrace_fops = {
49         .llseek = default_llseek,
50         .read   = memtrace_read,
51         .open   = simple_open,
52 };
53
54 static int check_memblock_online(struct memory_block *mem, void *arg)
55 {
56         if (mem->state != MEM_ONLINE)
57                 return -1;
58
59         return 0;
60 }
61
62 static int change_memblock_state(struct memory_block *mem, void *arg)
63 {
64         unsigned long state = (unsigned long)arg;
65
66         mem->state = state;
67
68         return 0;
69 }
70
71 static void memtrace_clear_range(unsigned long start_pfn,
72                                  unsigned long nr_pages)
73 {
74         unsigned long pfn;
75
76         /*
77          * As pages are offline, we cannot trust the memmap anymore. As HIGHMEM
78          * does not apply, avoid passing around "struct page" and use
79          * clear_page() instead directly.
80          */
81         for (pfn = start_pfn; pfn < start_pfn + nr_pages; pfn++) {
82                 if (IS_ALIGNED(pfn, PAGES_PER_SECTION))
83                         cond_resched();
84                 clear_page(__va(PFN_PHYS(pfn)));
85         }
86 }
87
88 /* called with device_hotplug_lock held */
89 static bool memtrace_offline_pages(u32 nid, u64 start_pfn, u64 nr_pages)
90 {
91         const unsigned long start = PFN_PHYS(start_pfn);
92         const unsigned long size = PFN_PHYS(nr_pages);
93
94         if (walk_memory_blocks(start, size, NULL, check_memblock_online))
95                 return false;
96
97         walk_memory_blocks(start, size, (void *)MEM_GOING_OFFLINE,
98                            change_memblock_state);
99
100         if (offline_pages(start_pfn, nr_pages)) {
101                 walk_memory_blocks(start, size, (void *)MEM_ONLINE,
102                                    change_memblock_state);
103                 return false;
104         }
105
106         walk_memory_blocks(start, size, (void *)MEM_OFFLINE,
107                            change_memblock_state);
108
109
110         return true;
111 }
112
113 static u64 memtrace_alloc_node(u32 nid, u64 size)
114 {
115         u64 start_pfn, end_pfn, nr_pages, pfn;
116         u64 base_pfn;
117         u64 bytes = memory_block_size_bytes();
118
119         if (!node_spanned_pages(nid))
120                 return 0;
121
122         start_pfn = node_start_pfn(nid);
123         end_pfn = node_end_pfn(nid);
124         nr_pages = size >> PAGE_SHIFT;
125
126         /* Trace memory needs to be aligned to the size */
127         end_pfn = round_down(end_pfn - nr_pages, nr_pages);
128
129         lock_device_hotplug();
130         for (base_pfn = end_pfn; base_pfn > start_pfn; base_pfn -= nr_pages) {
131                 if (memtrace_offline_pages(nid, base_pfn, nr_pages) == true) {
132                         /*
133                          * Clear the range while we still have a linear
134                          * mapping.
135                          */
136                         memtrace_clear_range(base_pfn, nr_pages);
137                         /*
138                          * Remove memory in memory block size chunks so that
139                          * iomem resources are always split to the same size and
140                          * we never try to remove memory that spans two iomem
141                          * resources.
142                          */
143                         end_pfn = base_pfn + nr_pages;
144                         for (pfn = base_pfn; pfn < end_pfn; pfn += bytes>> PAGE_SHIFT) {
145                                 __remove_memory(nid, pfn << PAGE_SHIFT, bytes);
146                         }
147                         unlock_device_hotplug();
148                         return base_pfn << PAGE_SHIFT;
149                 }
150         }
151         unlock_device_hotplug();
152
153         return 0;
154 }
155
156 static int memtrace_init_regions_runtime(u64 size)
157 {
158         u32 nid;
159         u64 m;
160
161         memtrace_array = kcalloc(num_online_nodes(),
162                                 sizeof(struct memtrace_entry), GFP_KERNEL);
163         if (!memtrace_array) {
164                 pr_err("Failed to allocate memtrace_array\n");
165                 return -EINVAL;
166         }
167
168         for_each_online_node(nid) {
169                 m = memtrace_alloc_node(nid, size);
170
171                 /*
172                  * A node might not have any local memory, so warn but
173                  * continue on.
174                  */
175                 if (!m) {
176                         pr_err("Failed to allocate trace memory on node %d\n", nid);
177                         continue;
178                 }
179
180                 pr_info("Allocated trace memory on node %d at 0x%016llx\n", nid, m);
181
182                 memtrace_array[memtrace_array_nr].start = m;
183                 memtrace_array[memtrace_array_nr].size = size;
184                 memtrace_array[memtrace_array_nr].nid = nid;
185                 memtrace_array_nr++;
186         }
187
188         return 0;
189 }
190
191 static struct dentry *memtrace_debugfs_dir;
192
193 static int memtrace_init_debugfs(void)
194 {
195         int ret = 0;
196         int i;
197
198         for (i = 0; i < memtrace_array_nr; i++) {
199                 struct dentry *dir;
200                 struct memtrace_entry *ent = &memtrace_array[i];
201
202                 ent->mem = ioremap(ent->start, ent->size);
203                 /* Warn but continue on */
204                 if (!ent->mem) {
205                         pr_err("Failed to map trace memory at 0x%llx\n",
206                                  ent->start);
207                         ret = -1;
208                         continue;
209                 }
210
211                 snprintf(ent->name, 16, "%08x", ent->nid);
212                 dir = debugfs_create_dir(ent->name, memtrace_debugfs_dir);
213                 if (!dir) {
214                         pr_err("Failed to create debugfs directory for node %d\n",
215                                 ent->nid);
216                         return -1;
217                 }
218
219                 ent->dir = dir;
220                 debugfs_create_file("trace", 0400, dir, ent, &memtrace_fops);
221                 debugfs_create_x64("start", 0400, dir, &ent->start);
222                 debugfs_create_x64("size", 0400, dir, &ent->size);
223         }
224
225         return ret;
226 }
227
228 static int online_mem_block(struct memory_block *mem, void *arg)
229 {
230         return device_online(&mem->dev);
231 }
232
233 /*
234  * Iterate through the chunks of memory we have removed from the kernel
235  * and attempt to add them back to the kernel.
236  */
237 static int memtrace_online(void)
238 {
239         int i, ret = 0;
240         struct memtrace_entry *ent;
241
242         for (i = memtrace_array_nr - 1; i >= 0; i--) {
243                 ent = &memtrace_array[i];
244
245                 /* We have onlined this chunk previously */
246                 if (ent->nid == NUMA_NO_NODE)
247                         continue;
248
249                 /* Remove from io mappings */
250                 if (ent->mem) {
251                         iounmap(ent->mem);
252                         ent->mem = 0;
253                 }
254
255                 if (add_memory(ent->nid, ent->start, ent->size)) {
256                         pr_err("Failed to add trace memory to node %d\n",
257                                 ent->nid);
258                         ret += 1;
259                         continue;
260                 }
261
262                 /*
263                  * If kernel isn't compiled with the auto online option
264                  * we need to online the memory ourselves.
265                  */
266                 if (!memhp_auto_online) {
267                         lock_device_hotplug();
268                         walk_memory_blocks(ent->start, ent->size, NULL,
269                                            online_mem_block);
270                         unlock_device_hotplug();
271                 }
272
273                 /*
274                  * Memory was added successfully so clean up references to it
275                  * so on reentry we can tell that this chunk was added.
276                  */
277                 debugfs_remove_recursive(ent->dir);
278                 pr_info("Added trace memory back to node %d\n", ent->nid);
279                 ent->size = ent->start = ent->nid = NUMA_NO_NODE;
280         }
281         if (ret)
282                 return ret;
283
284         /* If all chunks of memory were added successfully, reset globals */
285         kfree(memtrace_array);
286         memtrace_array = NULL;
287         memtrace_size = 0;
288         memtrace_array_nr = 0;
289         return 0;
290 }
291
292 static int memtrace_enable_set(void *data, u64 val)
293 {
294         int rc = -EAGAIN;
295         u64 bytes;
296
297         /*
298          * Don't attempt to do anything if size isn't aligned to a memory
299          * block or equal to zero.
300          */
301         bytes = memory_block_size_bytes();
302         if (val & (bytes - 1)) {
303                 pr_err("Value must be aligned with 0x%llx\n", bytes);
304                 return -EINVAL;
305         }
306
307         mutex_lock(&memtrace_mutex);
308
309         /* Re-add/online previously removed/offlined memory */
310         if (memtrace_size) {
311                 if (memtrace_online())
312                         goto out_unlock;
313         }
314
315         if (!val) {
316                 rc = 0;
317                 goto out_unlock;
318         }
319
320         /* Offline and remove memory */
321         if (memtrace_init_regions_runtime(val))
322                 goto out_unlock;
323
324         if (memtrace_init_debugfs())
325                 goto out_unlock;
326
327         memtrace_size = val;
328         rc = 0;
329 out_unlock:
330         mutex_unlock(&memtrace_mutex);
331         return rc;
332 }
333
334 static int memtrace_enable_get(void *data, u64 *val)
335 {
336         *val = memtrace_size;
337         return 0;
338 }
339
340 DEFINE_SIMPLE_ATTRIBUTE(memtrace_init_fops, memtrace_enable_get,
341                                         memtrace_enable_set, "0x%016llx\n");
342
343 static int memtrace_init(void)
344 {
345         memtrace_debugfs_dir = debugfs_create_dir("memtrace",
346                                                   powerpc_debugfs_root);
347         if (!memtrace_debugfs_dir)
348                 return -1;
349
350         debugfs_create_file("enable", 0600, memtrace_debugfs_dir,
351                             NULL, &memtrace_init_fops);
352
353         return 0;
354 }
355 machine_device_initcall(powernv, memtrace_init);