memory hotplug: Allow memory blocks to span multiple memory sections
[firefly-linux-kernel-4.4.55.git] / drivers / base / memory.c
1 /*
2  * drivers/base/memory.c - basic Memory class support
3  *
4  * Written by Matt Tolentino <matthew.e.tolentino@intel.com>
5  *            Dave Hansen <haveblue@us.ibm.com>
6  *
7  * This file provides the necessary infrastructure to represent
8  * a SPARSEMEM-memory-model system's physical memory in /sysfs.
9  * All arch-independent code that assumes MEMORY_HOTPLUG requires
10  * SPARSEMEM should be contained here, or in mm/memory_hotplug.c.
11  */
12
13 #include <linux/sysdev.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/topology.h>
17 #include <linux/capability.h>
18 #include <linux/device.h>
19 #include <linux/memory.h>
20 #include <linux/kobject.h>
21 #include <linux/memory_hotplug.h>
22 #include <linux/mm.h>
23 #include <linux/mutex.h>
24 #include <linux/stat.h>
25 #include <linux/slab.h>
26
27 #include <asm/atomic.h>
28 #include <asm/uaccess.h>
29
30 static DEFINE_MUTEX(mem_sysfs_mutex);
31
32 #define MEMORY_CLASS_NAME       "memory"
33 #define MIN_MEMORY_BLOCK_SIZE   (1 << SECTION_SIZE_BITS)
34
35 static int sections_per_block;
36
37 static inline int base_memory_block_id(int section_nr)
38 {
39         return section_nr / sections_per_block;
40 }
41
42 static struct sysdev_class memory_sysdev_class = {
43         .name = MEMORY_CLASS_NAME,
44 };
45
46 static const char *memory_uevent_name(struct kset *kset, struct kobject *kobj)
47 {
48         return MEMORY_CLASS_NAME;
49 }
50
51 static int memory_uevent(struct kset *kset, struct kobject *obj, struct kobj_uevent_env *env)
52 {
53         int retval = 0;
54
55         return retval;
56 }
57
58 static const struct kset_uevent_ops memory_uevent_ops = {
59         .name           = memory_uevent_name,
60         .uevent         = memory_uevent,
61 };
62
63 static BLOCKING_NOTIFIER_HEAD(memory_chain);
64
65 int register_memory_notifier(struct notifier_block *nb)
66 {
67         return blocking_notifier_chain_register(&memory_chain, nb);
68 }
69 EXPORT_SYMBOL(register_memory_notifier);
70
71 void unregister_memory_notifier(struct notifier_block *nb)
72 {
73         blocking_notifier_chain_unregister(&memory_chain, nb);
74 }
75 EXPORT_SYMBOL(unregister_memory_notifier);
76
77 static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
78
79 int register_memory_isolate_notifier(struct notifier_block *nb)
80 {
81         return atomic_notifier_chain_register(&memory_isolate_chain, nb);
82 }
83 EXPORT_SYMBOL(register_memory_isolate_notifier);
84
85 void unregister_memory_isolate_notifier(struct notifier_block *nb)
86 {
87         atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
88 }
89 EXPORT_SYMBOL(unregister_memory_isolate_notifier);
90
91 /*
92  * register_memory - Setup a sysfs device for a memory block
93  */
94 static
95 int register_memory(struct memory_block *memory)
96 {
97         int error;
98
99         memory->sysdev.cls = &memory_sysdev_class;
100         memory->sysdev.id = memory->phys_index / sections_per_block;
101
102         error = sysdev_register(&memory->sysdev);
103         return error;
104 }
105
106 static void
107 unregister_memory(struct memory_block *memory)
108 {
109         BUG_ON(memory->sysdev.cls != &memory_sysdev_class);
110
111         /* drop the ref. we got in remove_memory_block() */
112         kobject_put(&memory->sysdev.kobj);
113         sysdev_unregister(&memory->sysdev);
114 }
115
116 unsigned long __weak memory_block_size_bytes(void)
117 {
118         return MIN_MEMORY_BLOCK_SIZE;
119 }
120
121 static unsigned long get_memory_block_size(void)
122 {
123         unsigned long block_sz;
124
125         block_sz = memory_block_size_bytes();
126
127         /* Validate blk_sz is a power of 2 and not less than section size */
128         if ((block_sz & (block_sz - 1)) || (block_sz < MIN_MEMORY_BLOCK_SIZE)) {
129                 WARN_ON(1);
130                 block_sz = MIN_MEMORY_BLOCK_SIZE;
131         }
132
133         return block_sz;
134 }
135
136 /*
137  * use this as the physical section index that this memsection
138  * uses.
139  */
140
141 static ssize_t show_mem_phys_index(struct sys_device *dev,
142                         struct sysdev_attribute *attr, char *buf)
143 {
144         struct memory_block *mem =
145                 container_of(dev, struct memory_block, sysdev);
146         return sprintf(buf, "%08lx\n", mem->phys_index / sections_per_block);
147 }
148
149 /*
150  * Show whether the section of memory is likely to be hot-removable
151  */
152 static ssize_t show_mem_removable(struct sys_device *dev,
153                         struct sysdev_attribute *attr, char *buf)
154 {
155         unsigned long i, pfn;
156         int ret = 1;
157         struct memory_block *mem =
158                 container_of(dev, struct memory_block, sysdev);
159
160         for (i = 0; i < sections_per_block; i++) {
161                 pfn = section_nr_to_pfn(mem->phys_index + i);
162                 ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
163         }
164
165         return sprintf(buf, "%d\n", ret);
166 }
167
168 /*
169  * online, offline, going offline, etc.
170  */
171 static ssize_t show_mem_state(struct sys_device *dev,
172                         struct sysdev_attribute *attr, char *buf)
173 {
174         struct memory_block *mem =
175                 container_of(dev, struct memory_block, sysdev);
176         ssize_t len = 0;
177
178         /*
179          * We can probably put these states in a nice little array
180          * so that they're not open-coded
181          */
182         switch (mem->state) {
183                 case MEM_ONLINE:
184                         len = sprintf(buf, "online\n");
185                         break;
186                 case MEM_OFFLINE:
187                         len = sprintf(buf, "offline\n");
188                         break;
189                 case MEM_GOING_OFFLINE:
190                         len = sprintf(buf, "going-offline\n");
191                         break;
192                 default:
193                         len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
194                                         mem->state);
195                         WARN_ON(1);
196                         break;
197         }
198
199         return len;
200 }
201
202 int memory_notify(unsigned long val, void *v)
203 {
204         return blocking_notifier_call_chain(&memory_chain, val, v);
205 }
206
207 int memory_isolate_notify(unsigned long val, void *v)
208 {
209         return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
210 }
211
212 /*
213  * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
214  * OK to have direct references to sparsemem variables in here.
215  */
216 static int
217 memory_section_action(unsigned long phys_index, unsigned long action)
218 {
219         int i;
220         unsigned long start_pfn, start_paddr;
221         struct page *first_page;
222         int ret;
223
224         first_page = pfn_to_page(phys_index << PFN_SECTION_SHIFT);
225
226         /*
227          * The probe routines leave the pages reserved, just
228          * as the bootmem code does.  Make sure they're still
229          * that way.
230          */
231         if (action == MEM_ONLINE) {
232                 for (i = 0; i < PAGES_PER_SECTION; i++) {
233                         if (PageReserved(first_page+i))
234                                 continue;
235
236                         printk(KERN_WARNING "section number %ld page number %d "
237                                 "not reserved, was it already online?\n",
238                                 phys_index, i);
239                         return -EBUSY;
240                 }
241         }
242
243         switch (action) {
244                 case MEM_ONLINE:
245                         start_pfn = page_to_pfn(first_page);
246                         ret = online_pages(start_pfn, PAGES_PER_SECTION);
247                         break;
248                 case MEM_OFFLINE:
249                         start_paddr = page_to_pfn(first_page) << PAGE_SHIFT;
250                         ret = remove_memory(start_paddr,
251                                             PAGES_PER_SECTION << PAGE_SHIFT);
252                         break;
253                 default:
254                         WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
255                              "%ld\n", __func__, phys_index, action, action);
256                         ret = -EINVAL;
257         }
258
259         return ret;
260 }
261
262 static int memory_block_change_state(struct memory_block *mem,
263                 unsigned long to_state, unsigned long from_state_req)
264 {
265         int i, ret = 0;
266
267         mutex_lock(&mem->state_mutex);
268
269         if (mem->state != from_state_req) {
270                 ret = -EINVAL;
271                 goto out;
272         }
273
274         if (to_state == MEM_OFFLINE)
275                 mem->state = MEM_GOING_OFFLINE;
276
277         for (i = 0; i < sections_per_block; i++) {
278                 ret = memory_section_action(mem->phys_index + i, to_state);
279                 if (ret)
280                         break;
281         }
282
283         if (ret) {
284                 for (i = 0; i < sections_per_block; i++)
285                         memory_section_action(mem->phys_index + i,
286                                               from_state_req);
287
288                 mem->state = from_state_req;
289         } else
290                 mem->state = to_state;
291
292 out:
293         mutex_unlock(&mem->state_mutex);
294         return ret;
295 }
296
297 static ssize_t
298 store_mem_state(struct sys_device *dev,
299                 struct sysdev_attribute *attr, const char *buf, size_t count)
300 {
301         struct memory_block *mem;
302         int ret = -EINVAL;
303
304         mem = container_of(dev, struct memory_block, sysdev);
305
306         if (!strncmp(buf, "online", min((int)count, 6)))
307                 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
308         else if(!strncmp(buf, "offline", min((int)count, 7)))
309                 ret = memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
310
311         if (ret)
312                 return ret;
313         return count;
314 }
315
316 /*
317  * phys_device is a bad name for this.  What I really want
318  * is a way to differentiate between memory ranges that
319  * are part of physical devices that constitute
320  * a complete removable unit or fru.
321  * i.e. do these ranges belong to the same physical device,
322  * s.t. if I offline all of these sections I can then
323  * remove the physical device?
324  */
325 static ssize_t show_phys_device(struct sys_device *dev,
326                                 struct sysdev_attribute *attr, char *buf)
327 {
328         struct memory_block *mem =
329                 container_of(dev, struct memory_block, sysdev);
330         return sprintf(buf, "%d\n", mem->phys_device);
331 }
332
333 static SYSDEV_ATTR(phys_index, 0444, show_mem_phys_index, NULL);
334 static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state);
335 static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL);
336 static SYSDEV_ATTR(removable, 0444, show_mem_removable, NULL);
337
338 #define mem_create_simple_file(mem, attr_name)  \
339         sysdev_create_file(&mem->sysdev, &attr_##attr_name)
340 #define mem_remove_simple_file(mem, attr_name)  \
341         sysdev_remove_file(&mem->sysdev, &attr_##attr_name)
342
343 /*
344  * Block size attribute stuff
345  */
346 static ssize_t
347 print_block_size(struct sysdev_class *class, struct sysdev_class_attribute *attr,
348                  char *buf)
349 {
350         return sprintf(buf, "%lx\n", get_memory_block_size());
351 }
352
353 static SYSDEV_CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL);
354
355 static int block_size_init(void)
356 {
357         return sysfs_create_file(&memory_sysdev_class.kset.kobj,
358                                 &attr_block_size_bytes.attr);
359 }
360
361 /*
362  * Some architectures will have custom drivers to do this, and
363  * will not need to do it from userspace.  The fake hot-add code
364  * as well as ppc64 will do all of their discovery in userspace
365  * and will require this interface.
366  */
367 #ifdef CONFIG_ARCH_MEMORY_PROBE
368 static ssize_t
369 memory_probe_store(struct class *class, struct class_attribute *attr,
370                    const char *buf, size_t count)
371 {
372         u64 phys_addr;
373         int nid;
374         int ret;
375
376         phys_addr = simple_strtoull(buf, NULL, 0);
377
378         nid = memory_add_physaddr_to_nid(phys_addr);
379         ret = add_memory(nid, phys_addr, PAGES_PER_SECTION << PAGE_SHIFT);
380
381         if (ret)
382                 count = ret;
383
384         return count;
385 }
386 static CLASS_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
387
388 static int memory_probe_init(void)
389 {
390         return sysfs_create_file(&memory_sysdev_class.kset.kobj,
391                                 &class_attr_probe.attr);
392 }
393 #else
394 static inline int memory_probe_init(void)
395 {
396         return 0;
397 }
398 #endif
399
400 #ifdef CONFIG_MEMORY_FAILURE
401 /*
402  * Support for offlining pages of memory
403  */
404
405 /* Soft offline a page */
406 static ssize_t
407 store_soft_offline_page(struct class *class,
408                         struct class_attribute *attr,
409                         const char *buf, size_t count)
410 {
411         int ret;
412         u64 pfn;
413         if (!capable(CAP_SYS_ADMIN))
414                 return -EPERM;
415         if (strict_strtoull(buf, 0, &pfn) < 0)
416                 return -EINVAL;
417         pfn >>= PAGE_SHIFT;
418         if (!pfn_valid(pfn))
419                 return -ENXIO;
420         ret = soft_offline_page(pfn_to_page(pfn), 0);
421         return ret == 0 ? count : ret;
422 }
423
424 /* Forcibly offline a page, including killing processes. */
425 static ssize_t
426 store_hard_offline_page(struct class *class,
427                         struct class_attribute *attr,
428                         const char *buf, size_t count)
429 {
430         int ret;
431         u64 pfn;
432         if (!capable(CAP_SYS_ADMIN))
433                 return -EPERM;
434         if (strict_strtoull(buf, 0, &pfn) < 0)
435                 return -EINVAL;
436         pfn >>= PAGE_SHIFT;
437         ret = __memory_failure(pfn, 0, 0);
438         return ret ? ret : count;
439 }
440
441 static CLASS_ATTR(soft_offline_page, 0644, NULL, store_soft_offline_page);
442 static CLASS_ATTR(hard_offline_page, 0644, NULL, store_hard_offline_page);
443
444 static __init int memory_fail_init(void)
445 {
446         int err;
447
448         err = sysfs_create_file(&memory_sysdev_class.kset.kobj,
449                                 &class_attr_soft_offline_page.attr);
450         if (!err)
451                 err = sysfs_create_file(&memory_sysdev_class.kset.kobj,
452                                 &class_attr_hard_offline_page.attr);
453         return err;
454 }
455 #else
456 static inline int memory_fail_init(void)
457 {
458         return 0;
459 }
460 #endif
461
462 /*
463  * Note that phys_device is optional.  It is here to allow for
464  * differentiation between which *physical* devices each
465  * section belongs to...
466  */
467 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
468 {
469         return 0;
470 }
471
472 struct memory_block *find_memory_block_hinted(struct mem_section *section,
473                                               struct memory_block *hint)
474 {
475         struct kobject *kobj;
476         struct sys_device *sysdev;
477         struct memory_block *mem;
478         char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
479         int block_id = base_memory_block_id(__section_nr(section));
480
481         kobj = hint ? &hint->sysdev.kobj : NULL;
482
483         /*
484          * This only works because we know that section == sysdev->id
485          * slightly redundant with sysdev_register()
486          */
487         sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, block_id);
488
489         kobj = kset_find_obj_hinted(&memory_sysdev_class.kset, name, kobj);
490         if (!kobj)
491                 return NULL;
492
493         sysdev = container_of(kobj, struct sys_device, kobj);
494         mem = container_of(sysdev, struct memory_block, sysdev);
495
496         return mem;
497 }
498
499 /*
500  * For now, we have a linear search to go find the appropriate
501  * memory_block corresponding to a particular phys_index. If
502  * this gets to be a real problem, we can always use a radix
503  * tree or something here.
504  *
505  * This could be made generic for all sysdev classes.
506  */
507 struct memory_block *find_memory_block(struct mem_section *section)
508 {
509         return find_memory_block_hinted(section, NULL);
510 }
511
512 static int init_memory_block(struct memory_block **memory,
513                              struct mem_section *section, unsigned long state)
514 {
515         struct memory_block *mem;
516         unsigned long start_pfn;
517         int scn_nr;
518         int ret = 0;
519
520         mem = kzalloc(sizeof(*mem), GFP_KERNEL);
521         if (!mem)
522                 return -ENOMEM;
523
524         scn_nr = __section_nr(section);
525         mem->phys_index = base_memory_block_id(scn_nr) * sections_per_block;
526         mem->state = state;
527         mem->section_count++;
528         mutex_init(&mem->state_mutex);
529         start_pfn = section_nr_to_pfn(mem->phys_index);
530         mem->phys_device = arch_get_memory_phys_device(start_pfn);
531
532         ret = register_memory(mem);
533         if (!ret)
534                 ret = mem_create_simple_file(mem, phys_index);
535         if (!ret)
536                 ret = mem_create_simple_file(mem, state);
537         if (!ret)
538                 ret = mem_create_simple_file(mem, phys_device);
539         if (!ret)
540                 ret = mem_create_simple_file(mem, removable);
541
542         *memory = mem;
543         return ret;
544 }
545
546 static int add_memory_section(int nid, struct mem_section *section,
547                         unsigned long state, enum mem_add_context context)
548 {
549         struct memory_block *mem;
550         int ret = 0;
551
552         mutex_lock(&mem_sysfs_mutex);
553
554         mem = find_memory_block(section);
555         if (mem) {
556                 mem->section_count++;
557                 kobject_put(&mem->sysdev.kobj);
558         } else
559                 ret = init_memory_block(&mem, section, state);
560
561         if (!ret) {
562                 if (context == HOTPLUG &&
563                     mem->section_count == sections_per_block)
564                         ret = register_mem_sect_under_node(mem, nid);
565         }
566
567         mutex_unlock(&mem_sysfs_mutex);
568         return ret;
569 }
570
571 int remove_memory_block(unsigned long node_id, struct mem_section *section,
572                 int phys_device)
573 {
574         struct memory_block *mem;
575
576         mutex_lock(&mem_sysfs_mutex);
577         mem = find_memory_block(section);
578
579         mem->section_count--;
580         if (mem->section_count == 0) {
581                 unregister_mem_sect_under_nodes(mem);
582                 mem_remove_simple_file(mem, phys_index);
583                 mem_remove_simple_file(mem, state);
584                 mem_remove_simple_file(mem, phys_device);
585                 mem_remove_simple_file(mem, removable);
586                 unregister_memory(mem);
587                 kfree(mem);
588         } else
589                 kobject_put(&mem->sysdev.kobj);
590
591         mutex_unlock(&mem_sysfs_mutex);
592         return 0;
593 }
594
595 /*
596  * need an interface for the VM to add new memory regions,
597  * but without onlining it.
598  */
599 int register_new_memory(int nid, struct mem_section *section)
600 {
601         return add_memory_section(nid, section, MEM_OFFLINE, HOTPLUG);
602 }
603
604 int unregister_memory_section(struct mem_section *section)
605 {
606         if (!present_section(section))
607                 return -EINVAL;
608
609         return remove_memory_block(0, section, 0);
610 }
611
612 /*
613  * Initialize the sysfs support for memory devices...
614  */
615 int __init memory_dev_init(void)
616 {
617         unsigned int i;
618         int ret;
619         int err;
620         unsigned long block_sz;
621
622         memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops;
623         ret = sysdev_class_register(&memory_sysdev_class);
624         if (ret)
625                 goto out;
626
627         block_sz = get_memory_block_size();
628         sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
629
630         /*
631          * Create entries for memory sections that were found
632          * during boot and have been initialized
633          */
634         for (i = 0; i < NR_MEM_SECTIONS; i++) {
635                 if (!present_section_nr(i))
636                         continue;
637                 err = add_memory_section(0, __nr_to_section(i), MEM_ONLINE,
638                                          BOOT);
639                 if (!ret)
640                         ret = err;
641         }
642
643         err = memory_probe_init();
644         if (!ret)
645                 ret = err;
646         err = memory_fail_init();
647         if (!ret)
648                 ret = err;
649         err = block_size_init();
650         if (!ret)
651                 ret = err;
652 out:
653         if (ret)
654                 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
655         return ret;
656 }