1 // SPDX-License-Identifier: GPL-2.0
3 * Memory subsystem support
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 unsigned long base_memory_block_id(unsigned long section_nr)
39 return section_nr / sections_per_block;
42 static inline unsigned long pfn_to_block_id(unsigned long pfn)
44 return base_memory_block_id(pfn_to_section_nr(pfn));
47 static inline unsigned long phys_to_block_id(unsigned long phys)
49 return pfn_to_block_id(PFN_DOWN(phys));
52 static int memory_subsys_online(struct device *dev);
53 static int memory_subsys_offline(struct device *dev);
55 static struct bus_type memory_subsys = {
56 .name = MEMORY_CLASS_NAME,
57 .dev_name = MEMORY_CLASS_NAME,
58 .online = memory_subsys_online,
59 .offline = memory_subsys_offline,
62 static BLOCKING_NOTIFIER_HEAD(memory_chain);
64 int register_memory_notifier(struct notifier_block *nb)
66 return blocking_notifier_chain_register(&memory_chain, nb);
68 EXPORT_SYMBOL(register_memory_notifier);
70 void unregister_memory_notifier(struct notifier_block *nb)
72 blocking_notifier_chain_unregister(&memory_chain, nb);
74 EXPORT_SYMBOL(unregister_memory_notifier);
76 static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
78 int register_memory_isolate_notifier(struct notifier_block *nb)
80 return atomic_notifier_chain_register(&memory_isolate_chain, nb);
82 EXPORT_SYMBOL(register_memory_isolate_notifier);
84 void unregister_memory_isolate_notifier(struct notifier_block *nb)
86 atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
88 EXPORT_SYMBOL(unregister_memory_isolate_notifier);
90 static void memory_block_release(struct device *dev)
92 struct memory_block *mem = to_memory_block(dev);
97 unsigned long __weak memory_block_size_bytes(void)
99 return MIN_MEMORY_BLOCK_SIZE;
101 EXPORT_SYMBOL_GPL(memory_block_size_bytes);
103 static unsigned long get_memory_block_size(void)
105 unsigned long block_sz;
107 block_sz = memory_block_size_bytes();
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)) {
112 block_sz = MIN_MEMORY_BLOCK_SIZE;
119 * use this as the physical section index that this memsection
123 static ssize_t phys_index_show(struct device *dev,
124 struct device_attribute *attr, char *buf)
126 struct memory_block *mem = to_memory_block(dev);
127 unsigned long phys_index;
129 phys_index = mem->start_section_nr / sections_per_block;
130 return sprintf(buf, "%08lx\n", phys_index);
134 * Show whether the section of memory is likely to be hot-removable
136 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
139 struct memory_block *mem = to_memory_block(dev);
143 if (mem->state != MEM_ONLINE)
146 for (i = 0; i < sections_per_block; i++) {
147 if (!present_section_nr(mem->start_section_nr + i))
149 pfn = section_nr_to_pfn(mem->start_section_nr + i);
150 ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
154 return sprintf(buf, "%d\n", ret);
158 * online, offline, going offline, etc.
160 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
163 struct memory_block *mem = to_memory_block(dev);
167 * We can probably put these states in a nice little array
168 * so that they're not open-coded
170 switch (mem->state) {
172 len = sprintf(buf, "online\n");
175 len = sprintf(buf, "offline\n");
177 case MEM_GOING_OFFLINE:
178 len = sprintf(buf, "going-offline\n");
181 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
190 int memory_notify(unsigned long val, void *v)
192 return blocking_notifier_call_chain(&memory_chain, val, v);
195 int memory_isolate_notify(unsigned long val, void *v)
197 return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
201 * The probe routines leave the pages uninitialized, just as the bootmem code
202 * does. Make sure we do not access them, but instead use only information from
205 static bool pages_correctly_probed(unsigned long start_pfn)
207 unsigned long section_nr = pfn_to_section_nr(start_pfn);
208 unsigned long section_nr_end = section_nr + sections_per_block;
209 unsigned long pfn = start_pfn;
212 * memmap between sections is not contiguous except with
213 * SPARSEMEM_VMEMMAP. We lookup the page once per section
214 * and assume memmap is contiguous within each section
216 for (; section_nr < section_nr_end; section_nr++) {
217 if (WARN_ON_ONCE(!pfn_valid(pfn)))
220 if (!present_section_nr(section_nr)) {
221 pr_warn("section %ld pfn[%lx, %lx) not present\n",
222 section_nr, pfn, pfn + PAGES_PER_SECTION);
224 } else if (!valid_section_nr(section_nr)) {
225 pr_warn("section %ld pfn[%lx, %lx) no valid memmap\n",
226 section_nr, pfn, pfn + PAGES_PER_SECTION);
228 } else if (online_section_nr(section_nr)) {
229 pr_warn("section %ld pfn[%lx, %lx) is already online\n",
230 section_nr, pfn, pfn + PAGES_PER_SECTION);
233 pfn += PAGES_PER_SECTION;
240 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
241 * OK to have direct references to sparsemem variables in here.
244 memory_block_action(unsigned long start_section_nr, unsigned long action,
247 unsigned long start_pfn;
248 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
251 start_pfn = section_nr_to_pfn(start_section_nr);
255 if (!pages_correctly_probed(start_pfn))
258 ret = online_pages(start_pfn, nr_pages, online_type);
261 ret = offline_pages(start_pfn, nr_pages);
264 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
265 "%ld\n", __func__, start_section_nr, action, action);
272 static int memory_block_change_state(struct memory_block *mem,
273 unsigned long to_state, unsigned long from_state_req)
277 if (mem->state != from_state_req)
280 if (to_state == MEM_OFFLINE)
281 mem->state = MEM_GOING_OFFLINE;
283 ret = memory_block_action(mem->start_section_nr, to_state,
286 mem->state = ret ? from_state_req : to_state;
291 /* The device lock serializes operations on memory_subsys_[online|offline] */
292 static int memory_subsys_online(struct device *dev)
294 struct memory_block *mem = to_memory_block(dev);
297 if (mem->state == MEM_ONLINE)
301 * If we are called from state_store(), online_type will be
302 * set >= 0 Otherwise we were called from the device online
303 * attribute and need to set the online_type.
305 if (mem->online_type < 0)
306 mem->online_type = MMOP_ONLINE_KEEP;
308 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
310 /* clear online_type */
311 mem->online_type = -1;
316 static int memory_subsys_offline(struct device *dev)
318 struct memory_block *mem = to_memory_block(dev);
320 if (mem->state == MEM_OFFLINE)
323 /* Can't offline block with non-present sections */
324 if (mem->section_count != sections_per_block)
327 return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
330 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
331 const char *buf, size_t count)
333 struct memory_block *mem = to_memory_block(dev);
334 int ret, online_type;
336 ret = lock_device_hotplug_sysfs();
340 if (sysfs_streq(buf, "online_kernel"))
341 online_type = MMOP_ONLINE_KERNEL;
342 else if (sysfs_streq(buf, "online_movable"))
343 online_type = MMOP_ONLINE_MOVABLE;
344 else if (sysfs_streq(buf, "online"))
345 online_type = MMOP_ONLINE_KEEP;
346 else if (sysfs_streq(buf, "offline"))
347 online_type = MMOP_OFFLINE;
353 switch (online_type) {
354 case MMOP_ONLINE_KERNEL:
355 case MMOP_ONLINE_MOVABLE:
356 case MMOP_ONLINE_KEEP:
357 /* mem->online_type is protected by device_hotplug_lock */
358 mem->online_type = online_type;
359 ret = device_online(&mem->dev);
362 ret = device_offline(&mem->dev);
365 ret = -EINVAL; /* should never happen */
369 unlock_device_hotplug();
380 * phys_device is a bad name for this. What I really want
381 * is a way to differentiate between memory ranges that
382 * are part of physical devices that constitute
383 * a complete removable unit or fru.
384 * i.e. do these ranges belong to the same physical device,
385 * s.t. if I offline all of these sections I can then
386 * remove the physical device?
388 static ssize_t phys_device_show(struct device *dev,
389 struct device_attribute *attr, char *buf)
391 struct memory_block *mem = to_memory_block(dev);
392 return sprintf(buf, "%d\n", mem->phys_device);
395 #ifdef CONFIG_MEMORY_HOTREMOVE
396 static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
397 unsigned long nr_pages, int online_type,
398 struct zone *default_zone)
402 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
403 if (zone != default_zone) {
405 strcat(buf, zone->name);
409 static ssize_t valid_zones_show(struct device *dev,
410 struct device_attribute *attr, char *buf)
412 struct memory_block *mem = to_memory_block(dev);
413 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
414 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
415 unsigned long valid_start_pfn, valid_end_pfn;
416 struct zone *default_zone;
420 * Check the existing zone. Make sure that we do that only on the
421 * online nodes otherwise the page_zone is not reliable
423 if (mem->state == MEM_ONLINE) {
425 * The block contains more than one zone can not be offlined.
426 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
428 if (!test_pages_in_a_zone(start_pfn, start_pfn + nr_pages,
429 &valid_start_pfn, &valid_end_pfn))
430 return sprintf(buf, "none\n");
431 start_pfn = valid_start_pfn;
432 strcat(buf, page_zone(pfn_to_page(start_pfn))->name);
437 default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages);
438 strcat(buf, default_zone->name);
440 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
442 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
449 static DEVICE_ATTR_RO(valid_zones);
452 static DEVICE_ATTR_RO(phys_index);
453 static DEVICE_ATTR_RW(state);
454 static DEVICE_ATTR_RO(phys_device);
455 static DEVICE_ATTR_RO(removable);
458 * Block size attribute stuff
460 static ssize_t block_size_bytes_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
463 return sprintf(buf, "%lx\n", get_memory_block_size());
466 static DEVICE_ATTR_RO(block_size_bytes);
469 * Memory auto online policy.
472 static ssize_t auto_online_blocks_show(struct device *dev,
473 struct device_attribute *attr, char *buf)
475 if (memhp_auto_online)
476 return sprintf(buf, "online\n");
478 return sprintf(buf, "offline\n");
481 static ssize_t auto_online_blocks_store(struct device *dev,
482 struct device_attribute *attr,
483 const char *buf, size_t count)
485 if (sysfs_streq(buf, "online"))
486 memhp_auto_online = true;
487 else if (sysfs_streq(buf, "offline"))
488 memhp_auto_online = false;
495 static DEVICE_ATTR_RW(auto_online_blocks);
498 * Some architectures will have custom drivers to do this, and
499 * will not need to do it from userspace. The fake hot-add code
500 * as well as ppc64 will do all of their discovery in userspace
501 * and will require this interface.
503 #ifdef CONFIG_ARCH_MEMORY_PROBE
504 static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
505 const char *buf, size_t count)
509 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
511 ret = kstrtoull(buf, 0, &phys_addr);
515 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
518 ret = lock_device_hotplug_sysfs();
522 nid = memory_add_physaddr_to_nid(phys_addr);
523 ret = __add_memory(nid, phys_addr,
524 MIN_MEMORY_BLOCK_SIZE * sections_per_block);
531 unlock_device_hotplug();
535 static DEVICE_ATTR_WO(probe);
538 #ifdef CONFIG_MEMORY_FAILURE
540 * Support for offlining pages of memory
543 /* Soft offline a page */
544 static ssize_t soft_offline_page_store(struct device *dev,
545 struct device_attribute *attr,
546 const char *buf, size_t count)
550 if (!capable(CAP_SYS_ADMIN))
552 if (kstrtoull(buf, 0, &pfn) < 0)
557 ret = soft_offline_page(pfn_to_page(pfn), 0);
558 return ret == 0 ? count : ret;
561 /* Forcibly offline a page, including killing processes. */
562 static ssize_t hard_offline_page_store(struct device *dev,
563 struct device_attribute *attr,
564 const char *buf, size_t count)
568 if (!capable(CAP_SYS_ADMIN))
570 if (kstrtoull(buf, 0, &pfn) < 0)
573 ret = memory_failure(pfn, 0);
574 return ret ? ret : count;
577 static DEVICE_ATTR_WO(soft_offline_page);
578 static DEVICE_ATTR_WO(hard_offline_page);
582 * Note that phys_device is optional. It is here to allow for
583 * differentiation between which *physical* devices each
584 * section belongs to...
586 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
591 /* A reference for the returned memory block device is acquired. */
592 static struct memory_block *find_memory_block_by_id(unsigned long block_id)
596 dev = subsys_find_device_by_id(&memory_subsys, block_id, NULL);
597 return dev ? to_memory_block(dev) : NULL;
601 * For now, we have a linear search to go find the appropriate
602 * memory_block corresponding to a particular phys_index. If
603 * this gets to be a real problem, we can always use a radix
604 * tree or something here.
606 * This could be made generic for all device subsystems.
608 struct memory_block *find_memory_block(struct mem_section *section)
610 unsigned long block_id = base_memory_block_id(__section_nr(section));
612 return find_memory_block_by_id(block_id);
615 static struct attribute *memory_memblk_attrs[] = {
616 &dev_attr_phys_index.attr,
617 &dev_attr_state.attr,
618 &dev_attr_phys_device.attr,
619 &dev_attr_removable.attr,
620 #ifdef CONFIG_MEMORY_HOTREMOVE
621 &dev_attr_valid_zones.attr,
626 static struct attribute_group memory_memblk_attr_group = {
627 .attrs = memory_memblk_attrs,
630 static const struct attribute_group *memory_memblk_attr_groups[] = {
631 &memory_memblk_attr_group,
636 * register_memory - Setup a sysfs device for a memory block
639 int register_memory(struct memory_block *memory)
643 memory->dev.bus = &memory_subsys;
644 memory->dev.id = memory->start_section_nr / sections_per_block;
645 memory->dev.release = memory_block_release;
646 memory->dev.groups = memory_memblk_attr_groups;
647 memory->dev.offline = memory->state == MEM_OFFLINE;
649 ret = device_register(&memory->dev);
651 put_device(&memory->dev);
656 static int init_memory_block(struct memory_block **memory,
657 unsigned long block_id, unsigned long state)
659 struct memory_block *mem;
660 unsigned long start_pfn;
663 mem = find_memory_block_by_id(block_id);
665 put_device(&mem->dev);
668 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
672 mem->start_section_nr = block_id * sections_per_block;
673 mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
675 start_pfn = section_nr_to_pfn(mem->start_section_nr);
676 mem->phys_device = arch_get_memory_phys_device(start_pfn);
678 ret = register_memory(mem);
684 static int add_memory_block(unsigned long base_section_nr)
686 int ret, section_count = 0;
687 struct memory_block *mem;
690 for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
692 if (present_section_nr(nr))
695 if (section_count == 0)
697 ret = init_memory_block(&mem, base_memory_block_id(base_section_nr),
701 mem->section_count = section_count;
705 static void unregister_memory(struct memory_block *memory)
707 if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
710 /* drop the ref. we got via find_memory_block() */
711 put_device(&memory->dev);
712 device_unregister(&memory->dev);
716 * Create memory block devices for the given memory area. Start and size
717 * have to be aligned to memory block granularity. Memory block devices
718 * will be initialized as offline.
720 int create_memory_block_devices(unsigned long start, unsigned long size)
722 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
723 unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
724 struct memory_block *mem;
725 unsigned long block_id;
728 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
729 !IS_ALIGNED(size, memory_block_size_bytes())))
732 mutex_lock(&mem_sysfs_mutex);
733 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
734 ret = init_memory_block(&mem, block_id, MEM_OFFLINE);
737 mem->section_count = sections_per_block;
740 end_block_id = block_id;
741 for (block_id = start_block_id; block_id != end_block_id;
743 mem = find_memory_block_by_id(block_id);
744 mem->section_count = 0;
745 unregister_memory(mem);
748 mutex_unlock(&mem_sysfs_mutex);
753 * Remove memory block devices for the given memory area. Start and size
754 * have to be aligned to memory block granularity. Memory block devices
755 * have to be offline.
757 void remove_memory_block_devices(unsigned long start, unsigned long size)
759 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
760 const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
761 struct memory_block *mem;
762 unsigned long block_id;
764 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
765 !IS_ALIGNED(size, memory_block_size_bytes())))
768 mutex_lock(&mem_sysfs_mutex);
769 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
770 mem = find_memory_block_by_id(block_id);
771 if (WARN_ON_ONCE(!mem))
773 mem->section_count = 0;
774 unregister_memory_block_under_nodes(mem);
775 unregister_memory(mem);
777 mutex_unlock(&mem_sysfs_mutex);
780 /* return true if the memory block is offlined, otherwise, return false */
781 bool is_memblock_offlined(struct memory_block *mem)
783 return mem->state == MEM_OFFLINE;
786 static struct attribute *memory_root_attrs[] = {
787 #ifdef CONFIG_ARCH_MEMORY_PROBE
788 &dev_attr_probe.attr,
791 #ifdef CONFIG_MEMORY_FAILURE
792 &dev_attr_soft_offline_page.attr,
793 &dev_attr_hard_offline_page.attr,
796 &dev_attr_block_size_bytes.attr,
797 &dev_attr_auto_online_blocks.attr,
801 static struct attribute_group memory_root_attr_group = {
802 .attrs = memory_root_attrs,
805 static const struct attribute_group *memory_root_attr_groups[] = {
806 &memory_root_attr_group,
811 * Initialize the sysfs support for memory devices...
813 int __init memory_dev_init(void)
817 unsigned long block_sz, nr;
819 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
823 block_sz = get_memory_block_size();
824 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
827 * Create entries for memory sections that were found
828 * during boot and have been initialized
830 mutex_lock(&mem_sysfs_mutex);
831 for (nr = 0; nr <= __highest_present_section_nr;
832 nr += sections_per_block) {
833 err = add_memory_block(nr);
837 mutex_unlock(&mem_sysfs_mutex);
841 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
846 * walk_memory_blocks - walk through all present memory blocks overlapped
847 * by the range [start, start + size)
849 * @start: start address of the memory range
850 * @size: size of the memory range
851 * @arg: argument passed to func
852 * @func: callback for each memory section walked
854 * This function walks through all present memory blocks overlapped by the
855 * range [start, start + size), calling func on each memory block.
857 * In case func() returns an error, walking is aborted and the error is
860 int walk_memory_blocks(unsigned long start, unsigned long size,
861 void *arg, walk_memory_blocks_func_t func)
863 const unsigned long start_block_id = phys_to_block_id(start);
864 const unsigned long end_block_id = phys_to_block_id(start + size - 1);
865 struct memory_block *mem;
866 unsigned long block_id;
872 for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
873 mem = find_memory_block_by_id(block_id);
877 ret = func(mem, arg);
878 put_device(&mem->dev);