2 * Memory subsystem support
4 * Written by Matt Tolentino <matthew.e.tolentino@intel.com>
5 * Dave Hansen <haveblue@us.ibm.com>
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.
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>
22 #include <linux/mutex.h>
23 #include <linux/stat.h>
24 #include <linux/slab.h>
26 #include <linux/atomic.h>
27 #include <asm/uaccess.h>
29 static DEFINE_MUTEX(mem_sysfs_mutex);
31 #define MEMORY_CLASS_NAME "memory"
33 static int sections_per_block;
35 static inline int base_memory_block_id(int section_nr)
37 return section_nr / sections_per_block;
40 static struct bus_type memory_subsys = {
41 .name = MEMORY_CLASS_NAME,
42 .dev_name = MEMORY_CLASS_NAME,
45 static BLOCKING_NOTIFIER_HEAD(memory_chain);
47 int register_memory_notifier(struct notifier_block *nb)
49 return blocking_notifier_chain_register(&memory_chain, nb);
51 EXPORT_SYMBOL(register_memory_notifier);
53 void unregister_memory_notifier(struct notifier_block *nb)
55 blocking_notifier_chain_unregister(&memory_chain, nb);
57 EXPORT_SYMBOL(unregister_memory_notifier);
59 static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
61 int register_memory_isolate_notifier(struct notifier_block *nb)
63 return atomic_notifier_chain_register(&memory_isolate_chain, nb);
65 EXPORT_SYMBOL(register_memory_isolate_notifier);
67 void unregister_memory_isolate_notifier(struct notifier_block *nb)
69 atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
71 EXPORT_SYMBOL(unregister_memory_isolate_notifier);
73 static void memory_block_release(struct device *dev)
75 struct memory_block *mem = container_of(dev, struct memory_block, dev);
80 unsigned long __weak memory_block_size_bytes(void)
82 return MIN_MEMORY_BLOCK_SIZE;
85 static unsigned long get_memory_block_size(void)
87 unsigned long block_sz;
89 block_sz = memory_block_size_bytes();
91 /* Validate blk_sz is a power of 2 and not less than section size */
92 if ((block_sz & (block_sz - 1)) || (block_sz < MIN_MEMORY_BLOCK_SIZE)) {
94 block_sz = MIN_MEMORY_BLOCK_SIZE;
101 * use this as the physical section index that this memsection
105 static ssize_t show_mem_start_phys_index(struct device *dev,
106 struct device_attribute *attr, char *buf)
108 struct memory_block *mem =
109 container_of(dev, struct memory_block, dev);
110 unsigned long phys_index;
112 phys_index = mem->start_section_nr / sections_per_block;
113 return sprintf(buf, "%08lx\n", phys_index);
116 static ssize_t show_mem_end_phys_index(struct device *dev,
117 struct device_attribute *attr, char *buf)
119 struct memory_block *mem =
120 container_of(dev, struct memory_block, dev);
121 unsigned long phys_index;
123 phys_index = mem->end_section_nr / sections_per_block;
124 return sprintf(buf, "%08lx\n", phys_index);
128 * Show whether the section of memory is likely to be hot-removable
130 static ssize_t show_mem_removable(struct device *dev,
131 struct device_attribute *attr, char *buf)
133 unsigned long i, pfn;
135 struct memory_block *mem =
136 container_of(dev, struct memory_block, dev);
138 for (i = 0; i < sections_per_block; i++) {
139 pfn = section_nr_to_pfn(mem->start_section_nr + i);
140 ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
143 return sprintf(buf, "%d\n", ret);
147 * online, offline, going offline, etc.
149 static ssize_t show_mem_state(struct device *dev,
150 struct device_attribute *attr, char *buf)
152 struct memory_block *mem =
153 container_of(dev, struct memory_block, dev);
157 * We can probably put these states in a nice little array
158 * so that they're not open-coded
160 switch (mem->state) {
162 len = sprintf(buf, "online\n");
165 len = sprintf(buf, "offline\n");
167 case MEM_GOING_OFFLINE:
168 len = sprintf(buf, "going-offline\n");
171 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
180 int memory_notify(unsigned long val, void *v)
182 return blocking_notifier_call_chain(&memory_chain, val, v);
185 int memory_isolate_notify(unsigned long val, void *v)
187 return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
191 * The probe routines leave the pages reserved, just as the bootmem code does.
192 * Make sure they're still that way.
194 static bool pages_correctly_reserved(unsigned long start_pfn)
198 unsigned long pfn = start_pfn;
201 * memmap between sections is not contiguous except with
202 * SPARSEMEM_VMEMMAP. We lookup the page once per section
203 * and assume memmap is contiguous within each section
205 for (i = 0; i < sections_per_block; i++, pfn += PAGES_PER_SECTION) {
206 if (WARN_ON_ONCE(!pfn_valid(pfn)))
208 page = pfn_to_page(pfn);
210 for (j = 0; j < PAGES_PER_SECTION; j++) {
211 if (PageReserved(page + j))
214 printk(KERN_WARNING "section number %ld page number %d "
215 "not reserved, was it already online?\n",
216 pfn_to_section_nr(pfn), j);
226 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
227 * OK to have direct references to sparsemem variables in here.
230 memory_block_action(unsigned long phys_index, unsigned long action, int online_type)
232 unsigned long start_pfn;
233 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
234 struct page *first_page;
237 first_page = pfn_to_page(phys_index << PFN_SECTION_SHIFT);
238 start_pfn = page_to_pfn(first_page);
242 if (!pages_correctly_reserved(start_pfn))
245 ret = online_pages(start_pfn, nr_pages, online_type);
248 ret = offline_pages(start_pfn, nr_pages);
251 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
252 "%ld\n", __func__, phys_index, action, action);
259 static int __memory_block_change_state(struct memory_block *mem,
260 unsigned long to_state, unsigned long from_state_req,
265 if (mem->state != from_state_req) {
270 if (to_state == MEM_OFFLINE)
271 mem->state = MEM_GOING_OFFLINE;
273 ret = memory_block_action(mem->start_section_nr, to_state, online_type);
276 mem->state = from_state_req;
280 mem->state = to_state;
281 switch (mem->state) {
283 kobject_uevent(&mem->dev.kobj, KOBJ_OFFLINE);
286 kobject_uevent(&mem->dev.kobj, KOBJ_ONLINE);
295 static int memory_block_change_state(struct memory_block *mem,
296 unsigned long to_state, unsigned long from_state_req,
301 mutex_lock(&mem->state_mutex);
302 ret = __memory_block_change_state(mem, to_state, from_state_req,
304 mutex_unlock(&mem->state_mutex);
309 store_mem_state(struct device *dev,
310 struct device_attribute *attr, const char *buf, size_t count)
312 struct memory_block *mem;
315 mem = container_of(dev, struct memory_block, dev);
317 if (!strncmp(buf, "online_kernel", min_t(int, count, 13)))
318 ret = memory_block_change_state(mem, MEM_ONLINE,
319 MEM_OFFLINE, ONLINE_KERNEL);
320 else if (!strncmp(buf, "online_movable", min_t(int, count, 14)))
321 ret = memory_block_change_state(mem, MEM_ONLINE,
322 MEM_OFFLINE, ONLINE_MOVABLE);
323 else if (!strncmp(buf, "online", min_t(int, count, 6)))
324 ret = memory_block_change_state(mem, MEM_ONLINE,
325 MEM_OFFLINE, ONLINE_KEEP);
326 else if(!strncmp(buf, "offline", min_t(int, count, 7)))
327 ret = memory_block_change_state(mem, MEM_OFFLINE,
336 * phys_device is a bad name for this. What I really want
337 * is a way to differentiate between memory ranges that
338 * are part of physical devices that constitute
339 * a complete removable unit or fru.
340 * i.e. do these ranges belong to the same physical device,
341 * s.t. if I offline all of these sections I can then
342 * remove the physical device?
344 static ssize_t show_phys_device(struct device *dev,
345 struct device_attribute *attr, char *buf)
347 struct memory_block *mem =
348 container_of(dev, struct memory_block, dev);
349 return sprintf(buf, "%d\n", mem->phys_device);
352 static DEVICE_ATTR(phys_index, 0444, show_mem_start_phys_index, NULL);
353 static DEVICE_ATTR(end_phys_index, 0444, show_mem_end_phys_index, NULL);
354 static DEVICE_ATTR(state, 0644, show_mem_state, store_mem_state);
355 static DEVICE_ATTR(phys_device, 0444, show_phys_device, NULL);
356 static DEVICE_ATTR(removable, 0444, show_mem_removable, NULL);
359 * Block size attribute stuff
362 print_block_size(struct device *dev, struct device_attribute *attr,
365 return sprintf(buf, "%lx\n", get_memory_block_size());
368 static DEVICE_ATTR(block_size_bytes, 0444, print_block_size, NULL);
371 * Some architectures will have custom drivers to do this, and
372 * will not need to do it from userspace. The fake hot-add code
373 * as well as ppc64 will do all of their discovery in userspace
374 * and will require this interface.
376 #ifdef CONFIG_ARCH_MEMORY_PROBE
378 memory_probe_store(struct device *dev, struct device_attribute *attr,
379 const char *buf, size_t count)
384 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
386 phys_addr = simple_strtoull(buf, NULL, 0);
388 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
391 for (i = 0; i < sections_per_block; i++) {
392 nid = memory_add_physaddr_to_nid(phys_addr);
393 ret = add_memory(nid, phys_addr,
394 PAGES_PER_SECTION << PAGE_SHIFT);
398 phys_addr += MIN_MEMORY_BLOCK_SIZE;
406 static DEVICE_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
409 #ifdef CONFIG_MEMORY_FAILURE
411 * Support for offlining pages of memory
414 /* Soft offline a page */
416 store_soft_offline_page(struct device *dev,
417 struct device_attribute *attr,
418 const char *buf, size_t count)
422 if (!capable(CAP_SYS_ADMIN))
424 if (strict_strtoull(buf, 0, &pfn) < 0)
429 ret = soft_offline_page(pfn_to_page(pfn), 0);
430 return ret == 0 ? count : ret;
433 /* Forcibly offline a page, including killing processes. */
435 store_hard_offline_page(struct device *dev,
436 struct device_attribute *attr,
437 const char *buf, size_t count)
441 if (!capable(CAP_SYS_ADMIN))
443 if (strict_strtoull(buf, 0, &pfn) < 0)
446 ret = memory_failure(pfn, 0, 0);
447 return ret ? ret : count;
450 static DEVICE_ATTR(soft_offline_page, S_IWUSR, NULL, store_soft_offline_page);
451 static DEVICE_ATTR(hard_offline_page, S_IWUSR, NULL, store_hard_offline_page);
455 * Note that phys_device is optional. It is here to allow for
456 * differentiation between which *physical* devices each
457 * section belongs to...
459 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
465 * A reference for the returned object is held and the reference for the
466 * hinted object is released.
468 struct memory_block *find_memory_block_hinted(struct mem_section *section,
469 struct memory_block *hint)
471 int block_id = base_memory_block_id(__section_nr(section));
472 struct device *hintdev = hint ? &hint->dev : NULL;
475 dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev);
477 put_device(&hint->dev);
480 return container_of(dev, struct memory_block, dev);
484 * For now, we have a linear search to go find the appropriate
485 * memory_block corresponding to a particular phys_index. If
486 * this gets to be a real problem, we can always use a radix
487 * tree or something here.
489 * This could be made generic for all device subsystems.
491 struct memory_block *find_memory_block(struct mem_section *section)
493 return find_memory_block_hinted(section, NULL);
496 static struct attribute *memory_memblk_attrs[] = {
497 &dev_attr_phys_index.attr,
498 &dev_attr_end_phys_index.attr,
499 &dev_attr_state.attr,
500 &dev_attr_phys_device.attr,
501 &dev_attr_removable.attr,
505 static struct attribute_group memory_memblk_attr_group = {
506 .attrs = memory_memblk_attrs,
509 static const struct attribute_group *memory_memblk_attr_groups[] = {
510 &memory_memblk_attr_group,
515 * register_memory - Setup a sysfs device for a memory block
518 int register_memory(struct memory_block *memory)
522 memory->dev.bus = &memory_subsys;
523 memory->dev.id = memory->start_section_nr / sections_per_block;
524 memory->dev.release = memory_block_release;
525 memory->dev.groups = memory_memblk_attr_groups;
527 error = device_register(&memory->dev);
531 static int init_memory_block(struct memory_block **memory,
532 struct mem_section *section, unsigned long state)
534 struct memory_block *mem;
535 unsigned long start_pfn;
539 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
543 scn_nr = __section_nr(section);
544 mem->start_section_nr =
545 base_memory_block_id(scn_nr) * sections_per_block;
546 mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
548 mem->section_count++;
549 mutex_init(&mem->state_mutex);
550 start_pfn = section_nr_to_pfn(mem->start_section_nr);
551 mem->phys_device = arch_get_memory_phys_device(start_pfn);
553 ret = register_memory(mem);
559 static int add_memory_section(int nid, struct mem_section *section,
560 struct memory_block **mem_p,
561 unsigned long state, enum mem_add_context context)
563 struct memory_block *mem = NULL;
564 int scn_nr = __section_nr(section);
567 mutex_lock(&mem_sysfs_mutex);
569 if (context == BOOT) {
570 /* same memory block ? */
572 if (scn_nr >= (*mem_p)->start_section_nr &&
573 scn_nr <= (*mem_p)->end_section_nr) {
575 kobject_get(&mem->dev.kobj);
578 mem = find_memory_block(section);
581 mem->section_count++;
582 kobject_put(&mem->dev.kobj);
584 ret = init_memory_block(&mem, section, state);
585 /* store memory_block pointer for next loop */
586 if (!ret && context == BOOT)
592 if (context == HOTPLUG &&
593 mem->section_count == sections_per_block)
594 ret = register_mem_sect_under_node(mem, nid);
597 mutex_unlock(&mem_sysfs_mutex);
602 * need an interface for the VM to add new memory regions,
603 * but without onlining it.
605 int register_new_memory(int nid, struct mem_section *section)
607 return add_memory_section(nid, section, NULL, MEM_OFFLINE, HOTPLUG);
610 #ifdef CONFIG_MEMORY_HOTREMOVE
612 unregister_memory(struct memory_block *memory)
614 BUG_ON(memory->dev.bus != &memory_subsys);
616 /* drop the ref. we got in remove_memory_block() */
617 kobject_put(&memory->dev.kobj);
618 device_unregister(&memory->dev);
621 static int remove_memory_block(unsigned long node_id,
622 struct mem_section *section, int phys_device)
624 struct memory_block *mem;
626 mutex_lock(&mem_sysfs_mutex);
627 mem = find_memory_block(section);
628 unregister_mem_sect_under_nodes(mem, __section_nr(section));
630 mem->section_count--;
631 if (mem->section_count == 0)
632 unregister_memory(mem);
634 kobject_put(&mem->dev.kobj);
636 mutex_unlock(&mem_sysfs_mutex);
640 int unregister_memory_section(struct mem_section *section)
642 if (!present_section(section))
645 return remove_memory_block(0, section, 0);
647 #endif /* CONFIG_MEMORY_HOTREMOVE */
650 * offline one memory block. If the memory block has been offlined, do nothing.
652 int offline_memory_block(struct memory_block *mem)
656 mutex_lock(&mem->state_mutex);
657 if (mem->state != MEM_OFFLINE)
658 ret = __memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE, -1);
659 mutex_unlock(&mem->state_mutex);
664 /* return true if the memory block is offlined, otherwise, return false */
665 bool is_memblock_offlined(struct memory_block *mem)
667 return mem->state == MEM_OFFLINE;
670 static struct attribute *memory_root_attrs[] = {
671 #ifdef CONFIG_ARCH_MEMORY_PROBE
672 &dev_attr_probe.attr,
675 #ifdef CONFIG_MEMORY_FAILURE
676 &dev_attr_soft_offline_page.attr,
677 &dev_attr_hard_offline_page.attr,
680 &dev_attr_block_size_bytes.attr,
684 static struct attribute_group memory_root_attr_group = {
685 .attrs = memory_root_attrs,
688 static const struct attribute_group *memory_root_attr_groups[] = {
689 &memory_root_attr_group,
694 * Initialize the sysfs support for memory devices...
696 int __init memory_dev_init(void)
701 unsigned long block_sz;
702 struct memory_block *mem = NULL;
704 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
708 block_sz = get_memory_block_size();
709 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
712 * Create entries for memory sections that were found
713 * during boot and have been initialized
715 for (i = 0; i < NR_MEM_SECTIONS; i++) {
716 if (!present_section_nr(i))
718 /* don't need to reuse memory_block if only one per block */
719 err = add_memory_section(0, __nr_to_section(i),
720 (sections_per_block == 1) ? NULL : &mem,
729 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);