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