1 // SPDX-License-Identifier: GPL-2.0
3 * Memory subsystem support
5 * Written by Matt Tolentino <matthew.e.tolentino@intel.com>
6 * Dave Hansen <haveblue@us.ibm.com>
8 * This file provides the necessary infrastructure to represent
9 * a SPARSEMEM-memory-model system's physical memory in /sysfs.
10 * All arch-independent code that assumes MEMORY_HOTPLUG requires
11 * SPARSEMEM should be contained here, or in mm/memory_hotplug.c.
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/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 <linux/uaccess.h>
29 static DEFINE_MUTEX(mem_sysfs_mutex);
31 #define MEMORY_CLASS_NAME "memory"
33 #define to_memory_block(dev) container_of(dev, struct memory_block, dev)
35 static int sections_per_block;
37 static inline int base_memory_block_id(int section_nr)
39 return section_nr / sections_per_block;
42 static inline int pfn_to_block_id(unsigned long pfn)
44 return base_memory_block_id(pfn_to_section_nr(pfn));
47 static int memory_subsys_online(struct device *dev);
48 static int memory_subsys_offline(struct device *dev);
50 static struct bus_type memory_subsys = {
51 .name = MEMORY_CLASS_NAME,
52 .dev_name = MEMORY_CLASS_NAME,
53 .online = memory_subsys_online,
54 .offline = memory_subsys_offline,
57 static BLOCKING_NOTIFIER_HEAD(memory_chain);
59 int register_memory_notifier(struct notifier_block *nb)
61 return blocking_notifier_chain_register(&memory_chain, nb);
63 EXPORT_SYMBOL(register_memory_notifier);
65 void unregister_memory_notifier(struct notifier_block *nb)
67 blocking_notifier_chain_unregister(&memory_chain, nb);
69 EXPORT_SYMBOL(unregister_memory_notifier);
71 static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
73 int register_memory_isolate_notifier(struct notifier_block *nb)
75 return atomic_notifier_chain_register(&memory_isolate_chain, nb);
77 EXPORT_SYMBOL(register_memory_isolate_notifier);
79 void unregister_memory_isolate_notifier(struct notifier_block *nb)
81 atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
83 EXPORT_SYMBOL(unregister_memory_isolate_notifier);
85 static void memory_block_release(struct device *dev)
87 struct memory_block *mem = to_memory_block(dev);
92 unsigned long __weak memory_block_size_bytes(void)
94 return MIN_MEMORY_BLOCK_SIZE;
96 EXPORT_SYMBOL_GPL(memory_block_size_bytes);
98 static unsigned long get_memory_block_size(void)
100 unsigned long block_sz;
102 block_sz = memory_block_size_bytes();
104 /* Validate blk_sz is a power of 2 and not less than section size */
105 if ((block_sz & (block_sz - 1)) || (block_sz < MIN_MEMORY_BLOCK_SIZE)) {
107 block_sz = MIN_MEMORY_BLOCK_SIZE;
114 * use this as the physical section index that this memsection
118 static ssize_t phys_index_show(struct device *dev,
119 struct device_attribute *attr, char *buf)
121 struct memory_block *mem = to_memory_block(dev);
122 unsigned long phys_index;
124 phys_index = mem->start_section_nr / sections_per_block;
125 return sprintf(buf, "%08lx\n", phys_index);
129 * Show whether the section of memory is likely to be hot-removable
131 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
134 unsigned long i, pfn;
136 struct memory_block *mem = to_memory_block(dev);
138 if (mem->state != MEM_ONLINE)
141 for (i = 0; i < sections_per_block; i++) {
142 if (!present_section_nr(mem->start_section_nr + i))
144 pfn = section_nr_to_pfn(mem->start_section_nr + i);
145 ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
149 return sprintf(buf, "%d\n", ret);
153 * online, offline, going offline, etc.
155 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
158 struct memory_block *mem = to_memory_block(dev);
162 * We can probably put these states in a nice little array
163 * so that they're not open-coded
165 switch (mem->state) {
167 len = sprintf(buf, "online\n");
170 len = sprintf(buf, "offline\n");
172 case MEM_GOING_OFFLINE:
173 len = sprintf(buf, "going-offline\n");
176 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
185 int memory_notify(unsigned long val, void *v)
187 return blocking_notifier_call_chain(&memory_chain, val, v);
190 int memory_isolate_notify(unsigned long val, void *v)
192 return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
196 * The probe routines leave the pages uninitialized, just as the bootmem code
197 * does. Make sure we do not access them, but instead use only information from
200 static bool pages_correctly_probed(unsigned long start_pfn)
202 unsigned long section_nr = pfn_to_section_nr(start_pfn);
203 unsigned long section_nr_end = section_nr + sections_per_block;
204 unsigned long pfn = start_pfn;
207 * memmap between sections is not contiguous except with
208 * SPARSEMEM_VMEMMAP. We lookup the page once per section
209 * and assume memmap is contiguous within each section
211 for (; section_nr < section_nr_end; section_nr++) {
212 if (WARN_ON_ONCE(!pfn_valid(pfn)))
215 if (!present_section_nr(section_nr)) {
216 pr_warn("section %ld pfn[%lx, %lx) not present\n",
217 section_nr, pfn, pfn + PAGES_PER_SECTION);
219 } else if (!valid_section_nr(section_nr)) {
220 pr_warn("section %ld pfn[%lx, %lx) no valid memmap\n",
221 section_nr, pfn, pfn + PAGES_PER_SECTION);
223 } else if (online_section_nr(section_nr)) {
224 pr_warn("section %ld pfn[%lx, %lx) is already online\n",
225 section_nr, pfn, pfn + PAGES_PER_SECTION);
228 pfn += PAGES_PER_SECTION;
235 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
236 * OK to have direct references to sparsemem variables in here.
239 memory_block_action(unsigned long start_section_nr, unsigned long action,
242 unsigned long start_pfn;
243 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
246 start_pfn = section_nr_to_pfn(start_section_nr);
250 if (!pages_correctly_probed(start_pfn))
253 ret = online_pages(start_pfn, nr_pages, online_type);
256 ret = offline_pages(start_pfn, nr_pages);
259 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
260 "%ld\n", __func__, start_section_nr, action, action);
267 static int memory_block_change_state(struct memory_block *mem,
268 unsigned long to_state, unsigned long from_state_req)
272 if (mem->state != from_state_req)
275 if (to_state == MEM_OFFLINE)
276 mem->state = MEM_GOING_OFFLINE;
278 ret = memory_block_action(mem->start_section_nr, to_state,
281 mem->state = ret ? from_state_req : to_state;
286 /* The device lock serializes operations on memory_subsys_[online|offline] */
287 static int memory_subsys_online(struct device *dev)
289 struct memory_block *mem = to_memory_block(dev);
292 if (mem->state == MEM_ONLINE)
296 * If we are called from state_store(), online_type will be
297 * set >= 0 Otherwise we were called from the device online
298 * attribute and need to set the online_type.
300 if (mem->online_type < 0)
301 mem->online_type = MMOP_ONLINE_KEEP;
303 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
305 /* clear online_type */
306 mem->online_type = -1;
311 static int memory_subsys_offline(struct device *dev)
313 struct memory_block *mem = to_memory_block(dev);
315 if (mem->state == MEM_OFFLINE)
318 /* Can't offline block with non-present sections */
319 if (mem->section_count != sections_per_block)
322 return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
325 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
326 const char *buf, size_t count)
328 struct memory_block *mem = to_memory_block(dev);
329 int ret, online_type;
331 ret = lock_device_hotplug_sysfs();
335 if (sysfs_streq(buf, "online_kernel"))
336 online_type = MMOP_ONLINE_KERNEL;
337 else if (sysfs_streq(buf, "online_movable"))
338 online_type = MMOP_ONLINE_MOVABLE;
339 else if (sysfs_streq(buf, "online"))
340 online_type = MMOP_ONLINE_KEEP;
341 else if (sysfs_streq(buf, "offline"))
342 online_type = MMOP_OFFLINE;
348 switch (online_type) {
349 case MMOP_ONLINE_KERNEL:
350 case MMOP_ONLINE_MOVABLE:
351 case MMOP_ONLINE_KEEP:
352 /* mem->online_type is protected by device_hotplug_lock */
353 mem->online_type = online_type;
354 ret = device_online(&mem->dev);
357 ret = device_offline(&mem->dev);
360 ret = -EINVAL; /* should never happen */
364 unlock_device_hotplug();
375 * phys_device is a bad name for this. What I really want
376 * is a way to differentiate between memory ranges that
377 * are part of physical devices that constitute
378 * a complete removable unit or fru.
379 * i.e. do these ranges belong to the same physical device,
380 * s.t. if I offline all of these sections I can then
381 * remove the physical device?
383 static ssize_t phys_device_show(struct device *dev,
384 struct device_attribute *attr, char *buf)
386 struct memory_block *mem = to_memory_block(dev);
387 return sprintf(buf, "%d\n", mem->phys_device);
390 #ifdef CONFIG_MEMORY_HOTREMOVE
391 static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
392 unsigned long nr_pages, int online_type,
393 struct zone *default_zone)
397 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
398 if (zone != default_zone) {
400 strcat(buf, zone->name);
404 static ssize_t valid_zones_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
407 struct memory_block *mem = to_memory_block(dev);
408 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
409 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
410 unsigned long valid_start_pfn, valid_end_pfn;
411 struct zone *default_zone;
415 * Check the existing zone. Make sure that we do that only on the
416 * online nodes otherwise the page_zone is not reliable
418 if (mem->state == MEM_ONLINE) {
420 * The block contains more than one zone can not be offlined.
421 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
423 if (!test_pages_in_a_zone(start_pfn, start_pfn + nr_pages,
424 &valid_start_pfn, &valid_end_pfn))
425 return sprintf(buf, "none\n");
426 start_pfn = valid_start_pfn;
427 strcat(buf, page_zone(pfn_to_page(start_pfn))->name);
432 default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages);
433 strcat(buf, default_zone->name);
435 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
437 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
444 static DEVICE_ATTR_RO(valid_zones);
447 static DEVICE_ATTR_RO(phys_index);
448 static DEVICE_ATTR_RW(state);
449 static DEVICE_ATTR_RO(phys_device);
450 static DEVICE_ATTR_RO(removable);
453 * Block size attribute stuff
455 static ssize_t block_size_bytes_show(struct device *dev,
456 struct device_attribute *attr, char *buf)
458 return sprintf(buf, "%lx\n", get_memory_block_size());
461 static DEVICE_ATTR_RO(block_size_bytes);
464 * Memory auto online policy.
467 static ssize_t auto_online_blocks_show(struct device *dev,
468 struct device_attribute *attr, char *buf)
470 if (memhp_auto_online)
471 return sprintf(buf, "online\n");
473 return sprintf(buf, "offline\n");
476 static ssize_t auto_online_blocks_store(struct device *dev,
477 struct device_attribute *attr,
478 const char *buf, size_t count)
480 if (sysfs_streq(buf, "online"))
481 memhp_auto_online = true;
482 else if (sysfs_streq(buf, "offline"))
483 memhp_auto_online = false;
490 static DEVICE_ATTR_RW(auto_online_blocks);
493 * Some architectures will have custom drivers to do this, and
494 * will not need to do it from userspace. The fake hot-add code
495 * as well as ppc64 will do all of their discovery in userspace
496 * and will require this interface.
498 #ifdef CONFIG_ARCH_MEMORY_PROBE
499 static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
500 const char *buf, size_t count)
504 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
506 ret = kstrtoull(buf, 0, &phys_addr);
510 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
513 ret = lock_device_hotplug_sysfs();
517 nid = memory_add_physaddr_to_nid(phys_addr);
518 ret = __add_memory(nid, phys_addr,
519 MIN_MEMORY_BLOCK_SIZE * sections_per_block);
526 unlock_device_hotplug();
530 static DEVICE_ATTR_WO(probe);
533 #ifdef CONFIG_MEMORY_FAILURE
535 * Support for offlining pages of memory
538 /* Soft offline a page */
539 static ssize_t soft_offline_page_store(struct device *dev,
540 struct device_attribute *attr,
541 const char *buf, size_t count)
545 if (!capable(CAP_SYS_ADMIN))
547 if (kstrtoull(buf, 0, &pfn) < 0)
552 ret = soft_offline_page(pfn_to_page(pfn), 0);
553 return ret == 0 ? count : ret;
556 /* Forcibly offline a page, including killing processes. */
557 static ssize_t hard_offline_page_store(struct device *dev,
558 struct device_attribute *attr,
559 const char *buf, size_t count)
563 if (!capable(CAP_SYS_ADMIN))
565 if (kstrtoull(buf, 0, &pfn) < 0)
568 ret = memory_failure(pfn, 0);
569 return ret ? ret : count;
572 static DEVICE_ATTR_WO(soft_offline_page);
573 static DEVICE_ATTR_WO(hard_offline_page);
577 * Note that phys_device is optional. It is here to allow for
578 * differentiation between which *physical* devices each
579 * section belongs to...
581 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
587 * A reference for the returned object is held and the reference for the
588 * hinted object is released.
590 static struct memory_block *find_memory_block_by_id(int block_id,
591 struct memory_block *hint)
593 struct device *hintdev = hint ? &hint->dev : NULL;
596 dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev);
598 put_device(&hint->dev);
601 return to_memory_block(dev);
604 struct memory_block *find_memory_block_hinted(struct mem_section *section,
605 struct memory_block *hint)
607 int block_id = base_memory_block_id(__section_nr(section));
609 return find_memory_block_by_id(block_id, hint);
613 * For now, we have a linear search to go find the appropriate
614 * memory_block corresponding to a particular phys_index. If
615 * this gets to be a real problem, we can always use a radix
616 * tree or something here.
618 * This could be made generic for all device subsystems.
620 struct memory_block *find_memory_block(struct mem_section *section)
622 return find_memory_block_hinted(section, NULL);
625 static struct attribute *memory_memblk_attrs[] = {
626 &dev_attr_phys_index.attr,
627 &dev_attr_state.attr,
628 &dev_attr_phys_device.attr,
629 &dev_attr_removable.attr,
630 #ifdef CONFIG_MEMORY_HOTREMOVE
631 &dev_attr_valid_zones.attr,
636 static struct attribute_group memory_memblk_attr_group = {
637 .attrs = memory_memblk_attrs,
640 static const struct attribute_group *memory_memblk_attr_groups[] = {
641 &memory_memblk_attr_group,
646 * register_memory - Setup a sysfs device for a memory block
649 int register_memory(struct memory_block *memory)
653 memory->dev.bus = &memory_subsys;
654 memory->dev.id = memory->start_section_nr / sections_per_block;
655 memory->dev.release = memory_block_release;
656 memory->dev.groups = memory_memblk_attr_groups;
657 memory->dev.offline = memory->state == MEM_OFFLINE;
659 ret = device_register(&memory->dev);
661 put_device(&memory->dev);
666 static int init_memory_block(struct memory_block **memory, int block_id,
669 struct memory_block *mem;
670 unsigned long start_pfn;
673 mem = find_memory_block_by_id(block_id, NULL);
675 put_device(&mem->dev);
678 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
682 mem->start_section_nr = block_id * sections_per_block;
683 mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
685 start_pfn = section_nr_to_pfn(mem->start_section_nr);
686 mem->phys_device = arch_get_memory_phys_device(start_pfn);
688 ret = register_memory(mem);
694 static int add_memory_block(int base_section_nr)
696 struct memory_block *mem;
697 int i, ret, section_count = 0;
699 for (i = base_section_nr;
700 i < base_section_nr + sections_per_block;
702 if (present_section_nr(i))
705 if (section_count == 0)
707 ret = init_memory_block(&mem, base_memory_block_id(base_section_nr),
711 mem->section_count = section_count;
715 static void unregister_memory(struct memory_block *memory)
717 if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
720 /* drop the ref. we got via find_memory_block() */
721 put_device(&memory->dev);
722 device_unregister(&memory->dev);
726 * Create memory block devices for the given memory area. Start and size
727 * have to be aligned to memory block granularity. Memory block devices
728 * will be initialized as offline.
730 int create_memory_block_devices(unsigned long start, unsigned long size)
732 const int start_block_id = pfn_to_block_id(PFN_DOWN(start));
733 int end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
734 struct memory_block *mem;
735 unsigned long block_id;
738 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
739 !IS_ALIGNED(size, memory_block_size_bytes())))
742 mutex_lock(&mem_sysfs_mutex);
743 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
744 ret = init_memory_block(&mem, block_id, MEM_OFFLINE);
747 mem->section_count = sections_per_block;
750 end_block_id = block_id;
751 for (block_id = start_block_id; block_id != end_block_id;
753 mem = find_memory_block_by_id(block_id, NULL);
754 mem->section_count = 0;
755 unregister_memory(mem);
758 mutex_unlock(&mem_sysfs_mutex);
762 void unregister_memory_section(struct mem_section *section)
764 struct memory_block *mem;
766 if (WARN_ON_ONCE(!present_section(section)))
769 mutex_lock(&mem_sysfs_mutex);
772 * Some users of the memory hotplug do not want/need memblock to
773 * track all sections. Skip over those.
775 mem = find_memory_block(section);
779 unregister_mem_sect_under_nodes(mem, __section_nr(section));
781 mem->section_count--;
782 if (mem->section_count == 0)
783 unregister_memory(mem);
785 put_device(&mem->dev);
788 mutex_unlock(&mem_sysfs_mutex);
791 /* return true if the memory block is offlined, otherwise, return false */
792 bool is_memblock_offlined(struct memory_block *mem)
794 return mem->state == MEM_OFFLINE;
797 static struct attribute *memory_root_attrs[] = {
798 #ifdef CONFIG_ARCH_MEMORY_PROBE
799 &dev_attr_probe.attr,
802 #ifdef CONFIG_MEMORY_FAILURE
803 &dev_attr_soft_offline_page.attr,
804 &dev_attr_hard_offline_page.attr,
807 &dev_attr_block_size_bytes.attr,
808 &dev_attr_auto_online_blocks.attr,
812 static struct attribute_group memory_root_attr_group = {
813 .attrs = memory_root_attrs,
816 static const struct attribute_group *memory_root_attr_groups[] = {
817 &memory_root_attr_group,
822 * Initialize the sysfs support for memory devices...
824 int __init memory_dev_init(void)
829 unsigned long block_sz;
831 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
835 block_sz = get_memory_block_size();
836 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
839 * Create entries for memory sections that were found
840 * during boot and have been initialized
842 mutex_lock(&mem_sysfs_mutex);
843 for (i = 0; i <= __highest_present_section_nr;
844 i += sections_per_block) {
845 err = add_memory_block(i);
849 mutex_unlock(&mem_sysfs_mutex);
853 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);