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 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);
104 * Show the first physical section index (number) of this memory block.
106 static ssize_t phys_index_show(struct device *dev,
107 struct device_attribute *attr, char *buf)
109 struct memory_block *mem = to_memory_block(dev);
110 unsigned long phys_index;
112 phys_index = mem->start_section_nr / sections_per_block;
113 return sysfs_emit(buf, "%08lx\n", phys_index);
117 * Legacy interface that we cannot remove. Always indicate "removable"
118 * with CONFIG_MEMORY_HOTREMOVE - bad heuristic.
120 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
123 return sysfs_emit(buf, "%d\n", (int)IS_ENABLED(CONFIG_MEMORY_HOTREMOVE));
127 * online, offline, going offline, etc.
129 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
132 struct memory_block *mem = to_memory_block(dev);
136 * We can probably put these states in a nice little array
137 * so that they're not open-coded
139 switch (mem->state) {
141 len = sysfs_emit(buf, "online\n");
144 len = sysfs_emit(buf, "offline\n");
146 case MEM_GOING_OFFLINE:
147 len = sysfs_emit(buf, "going-offline\n");
150 len = sysfs_emit(buf, "ERROR-UNKNOWN-%ld\n",
159 int memory_notify(unsigned long val, void *v)
161 return blocking_notifier_call_chain(&memory_chain, val, v);
164 int memory_isolate_notify(unsigned long val, void *v)
166 return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
170 * The probe routines leave the pages uninitialized, just as the bootmem code
171 * does. Make sure we do not access them, but instead use only information from
174 static bool pages_correctly_probed(unsigned long start_pfn)
176 unsigned long section_nr = pfn_to_section_nr(start_pfn);
177 unsigned long section_nr_end = section_nr + sections_per_block;
178 unsigned long pfn = start_pfn;
181 * memmap between sections is not contiguous except with
182 * SPARSEMEM_VMEMMAP. We lookup the page once per section
183 * and assume memmap is contiguous within each section
185 for (; section_nr < section_nr_end; section_nr++) {
186 if (WARN_ON_ONCE(!pfn_valid(pfn)))
189 if (!present_section_nr(section_nr)) {
190 pr_warn("section %ld pfn[%lx, %lx) not present\n",
191 section_nr, pfn, pfn + PAGES_PER_SECTION);
193 } else if (!valid_section_nr(section_nr)) {
194 pr_warn("section %ld pfn[%lx, %lx) no valid memmap\n",
195 section_nr, pfn, pfn + PAGES_PER_SECTION);
197 } else if (online_section_nr(section_nr)) {
198 pr_warn("section %ld pfn[%lx, %lx) is already online\n",
199 section_nr, pfn, pfn + PAGES_PER_SECTION);
202 pfn += PAGES_PER_SECTION;
209 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
210 * OK to have direct references to sparsemem variables in here.
213 memory_block_action(unsigned long start_section_nr, unsigned long action,
216 unsigned long start_pfn;
217 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
220 start_pfn = section_nr_to_pfn(start_section_nr);
224 if (!pages_correctly_probed(start_pfn))
227 ret = online_pages(start_pfn, nr_pages, online_type);
230 ret = offline_pages(start_pfn, nr_pages);
233 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
234 "%ld\n", __func__, start_section_nr, action, action);
241 static int memory_block_change_state(struct memory_block *mem,
242 unsigned long to_state, unsigned long from_state_req)
246 if (mem->state != from_state_req)
249 if (to_state == MEM_OFFLINE)
250 mem->state = MEM_GOING_OFFLINE;
252 ret = memory_block_action(mem->start_section_nr, to_state,
255 mem->state = ret ? from_state_req : to_state;
260 /* The device lock serializes operations on memory_subsys_[online|offline] */
261 static int memory_subsys_online(struct device *dev)
263 struct memory_block *mem = to_memory_block(dev);
266 if (mem->state == MEM_ONLINE)
270 * If we are called from state_store(), online_type will be
271 * set >= 0 Otherwise we were called from the device online
272 * attribute and need to set the online_type.
274 if (mem->online_type < 0)
275 mem->online_type = MMOP_ONLINE_KEEP;
277 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
279 /* clear online_type */
280 mem->online_type = -1;
285 static int memory_subsys_offline(struct device *dev)
287 struct memory_block *mem = to_memory_block(dev);
289 if (mem->state == MEM_OFFLINE)
292 /* Can't offline block with non-present sections */
293 if (mem->section_count != sections_per_block)
296 return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
299 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
300 const char *buf, size_t count)
302 struct memory_block *mem = to_memory_block(dev);
303 int ret, online_type;
305 ret = lock_device_hotplug_sysfs();
309 if (sysfs_streq(buf, "online_kernel"))
310 online_type = MMOP_ONLINE_KERNEL;
311 else if (sysfs_streq(buf, "online_movable"))
312 online_type = MMOP_ONLINE_MOVABLE;
313 else if (sysfs_streq(buf, "online"))
314 online_type = MMOP_ONLINE_KEEP;
315 else if (sysfs_streq(buf, "offline"))
316 online_type = MMOP_OFFLINE;
322 switch (online_type) {
323 case MMOP_ONLINE_KERNEL:
324 case MMOP_ONLINE_MOVABLE:
325 case MMOP_ONLINE_KEEP:
326 /* mem->online_type is protected by device_hotplug_lock */
327 mem->online_type = online_type;
328 ret = device_online(&mem->dev);
331 ret = device_offline(&mem->dev);
334 ret = -EINVAL; /* should never happen */
338 unlock_device_hotplug();
349 * phys_device is a bad name for this. What I really want
350 * is a way to differentiate between memory ranges that
351 * are part of physical devices that constitute
352 * a complete removable unit or fru.
353 * i.e. do these ranges belong to the same physical device,
354 * s.t. if I offline all of these sections I can then
355 * remove the physical device?
357 static ssize_t phys_device_show(struct device *dev,
358 struct device_attribute *attr, char *buf)
360 struct memory_block *mem = to_memory_block(dev);
361 return sysfs_emit(buf, "%d\n", mem->phys_device);
364 #ifdef CONFIG_MEMORY_HOTREMOVE
365 static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
366 unsigned long nr_pages, int online_type,
367 struct zone *default_zone)
371 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
372 if (zone != default_zone) {
374 strcat(buf, zone->name);
378 static ssize_t valid_zones_show(struct device *dev,
379 struct device_attribute *attr, char *buf)
381 struct memory_block *mem = to_memory_block(dev);
382 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
383 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
384 unsigned long valid_start_pfn, valid_end_pfn;
385 struct zone *default_zone;
389 * Check the existing zone. Make sure that we do that only on the
390 * online nodes otherwise the page_zone is not reliable
392 if (mem->state == MEM_ONLINE) {
394 * The block contains more than one zone can not be offlined.
395 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
397 if (!test_pages_in_a_zone(start_pfn, start_pfn + nr_pages,
398 &valid_start_pfn, &valid_end_pfn))
399 return sysfs_emit(buf, "none\n");
400 start_pfn = valid_start_pfn;
401 strcat(buf, page_zone(pfn_to_page(start_pfn))->name);
406 default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages);
407 strcat(buf, default_zone->name);
409 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
411 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
418 static DEVICE_ATTR_RO(valid_zones);
421 static DEVICE_ATTR_RO(phys_index);
422 static DEVICE_ATTR_RW(state);
423 static DEVICE_ATTR_RO(phys_device);
424 static DEVICE_ATTR_RO(removable);
427 * Show the memory block size (shared by all memory blocks).
429 static ssize_t block_size_bytes_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
432 return sysfs_emit(buf, "%lx\n", memory_block_size_bytes());
435 static DEVICE_ATTR_RO(block_size_bytes);
438 * Memory auto online policy.
441 static ssize_t auto_online_blocks_show(struct device *dev,
442 struct device_attribute *attr, char *buf)
444 if (memhp_auto_online)
445 return sysfs_emit(buf, "online\n");
447 return sysfs_emit(buf, "offline\n");
450 static ssize_t auto_online_blocks_store(struct device *dev,
451 struct device_attribute *attr,
452 const char *buf, size_t count)
454 if (sysfs_streq(buf, "online"))
455 memhp_auto_online = true;
456 else if (sysfs_streq(buf, "offline"))
457 memhp_auto_online = false;
464 static DEVICE_ATTR_RW(auto_online_blocks);
467 * Some architectures will have custom drivers to do this, and
468 * will not need to do it from userspace. The fake hot-add code
469 * as well as ppc64 will do all of their discovery in userspace
470 * and will require this interface.
472 #ifdef CONFIG_ARCH_MEMORY_PROBE
473 static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
474 const char *buf, size_t count)
478 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
480 ret = kstrtoull(buf, 0, &phys_addr);
484 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
487 ret = lock_device_hotplug_sysfs();
491 nid = memory_add_physaddr_to_nid(phys_addr);
492 ret = __add_memory(nid, phys_addr,
493 MIN_MEMORY_BLOCK_SIZE * sections_per_block);
500 unlock_device_hotplug();
504 static DEVICE_ATTR_WO(probe);
507 #ifdef CONFIG_MEMORY_FAILURE
509 * Support for offlining pages of memory
512 /* Soft offline a page */
513 static ssize_t soft_offline_page_store(struct device *dev,
514 struct device_attribute *attr,
515 const char *buf, size_t count)
519 if (!capable(CAP_SYS_ADMIN))
521 if (kstrtoull(buf, 0, &pfn) < 0)
526 /* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
527 if (!pfn_to_online_page(pfn))
529 ret = soft_offline_page(pfn_to_page(pfn), 0);
530 return ret == 0 ? count : ret;
533 /* Forcibly offline a page, including killing processes. */
534 static ssize_t hard_offline_page_store(struct device *dev,
535 struct device_attribute *attr,
536 const char *buf, size_t count)
540 if (!capable(CAP_SYS_ADMIN))
542 if (kstrtoull(buf, 0, &pfn) < 0)
545 ret = memory_failure(pfn, 0);
546 return ret ? ret : count;
549 static DEVICE_ATTR_WO(soft_offline_page);
550 static DEVICE_ATTR_WO(hard_offline_page);
554 * Note that phys_device is optional. It is here to allow for
555 * differentiation between which *physical* devices each
556 * section belongs to...
558 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
563 /* A reference for the returned memory block device is acquired. */
564 static struct memory_block *find_memory_block_by_id(unsigned long block_id)
568 dev = subsys_find_device_by_id(&memory_subsys, block_id, NULL);
569 return dev ? to_memory_block(dev) : NULL;
573 * For now, we have a linear search to go find the appropriate
574 * memory_block corresponding to a particular phys_index. If
575 * this gets to be a real problem, we can always use a radix
576 * tree or something here.
578 * This could be made generic for all device subsystems.
580 struct memory_block *find_memory_block(struct mem_section *section)
582 unsigned long block_id = base_memory_block_id(__section_nr(section));
584 return find_memory_block_by_id(block_id);
587 static struct attribute *memory_memblk_attrs[] = {
588 &dev_attr_phys_index.attr,
589 &dev_attr_state.attr,
590 &dev_attr_phys_device.attr,
591 &dev_attr_removable.attr,
592 #ifdef CONFIG_MEMORY_HOTREMOVE
593 &dev_attr_valid_zones.attr,
598 static struct attribute_group memory_memblk_attr_group = {
599 .attrs = memory_memblk_attrs,
602 static const struct attribute_group *memory_memblk_attr_groups[] = {
603 &memory_memblk_attr_group,
608 * register_memory - Setup a sysfs device for a memory block
611 int register_memory(struct memory_block *memory)
615 memory->dev.bus = &memory_subsys;
616 memory->dev.id = memory->start_section_nr / sections_per_block;
617 memory->dev.release = memory_block_release;
618 memory->dev.groups = memory_memblk_attr_groups;
619 memory->dev.offline = memory->state == MEM_OFFLINE;
621 ret = device_register(&memory->dev);
623 put_device(&memory->dev);
628 static int init_memory_block(struct memory_block **memory,
629 unsigned long block_id, unsigned long state)
631 struct memory_block *mem;
632 unsigned long start_pfn;
635 mem = find_memory_block_by_id(block_id);
637 put_device(&mem->dev);
640 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
644 mem->start_section_nr = block_id * sections_per_block;
646 start_pfn = section_nr_to_pfn(mem->start_section_nr);
647 mem->phys_device = arch_get_memory_phys_device(start_pfn);
648 mem->nid = NUMA_NO_NODE;
650 ret = register_memory(mem);
656 static int add_memory_block(unsigned long base_section_nr)
658 int ret, section_count = 0;
659 struct memory_block *mem;
662 for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
664 if (present_section_nr(nr))
667 if (section_count == 0)
669 ret = init_memory_block(&mem, base_memory_block_id(base_section_nr),
673 mem->section_count = section_count;
677 static void unregister_memory(struct memory_block *memory)
679 if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
682 /* drop the ref. we got via find_memory_block() */
683 put_device(&memory->dev);
684 device_unregister(&memory->dev);
688 * Create memory block devices for the given memory area. Start and size
689 * have to be aligned to memory block granularity. Memory block devices
690 * will be initialized as offline.
692 int create_memory_block_devices(unsigned long start, unsigned long size)
694 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
695 unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
696 struct memory_block *mem;
697 unsigned long block_id;
700 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
701 !IS_ALIGNED(size, memory_block_size_bytes())))
704 mutex_lock(&mem_sysfs_mutex);
705 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
706 ret = init_memory_block(&mem, block_id, MEM_OFFLINE);
709 mem->section_count = sections_per_block;
712 end_block_id = block_id;
713 for (block_id = start_block_id; block_id != end_block_id;
715 mem = find_memory_block_by_id(block_id);
716 mem->section_count = 0;
717 unregister_memory(mem);
720 mutex_unlock(&mem_sysfs_mutex);
725 * Remove memory block devices for the given memory area. Start and size
726 * have to be aligned to memory block granularity. Memory block devices
727 * have to be offline.
729 void remove_memory_block_devices(unsigned long start, unsigned long size)
731 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
732 const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
733 struct memory_block *mem;
734 unsigned long block_id;
736 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
737 !IS_ALIGNED(size, memory_block_size_bytes())))
740 mutex_lock(&mem_sysfs_mutex);
741 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
742 mem = find_memory_block_by_id(block_id);
743 if (WARN_ON_ONCE(!mem))
745 mem->section_count = 0;
746 unregister_memory_block_under_nodes(mem);
747 unregister_memory(mem);
749 mutex_unlock(&mem_sysfs_mutex);
752 /* return true if the memory block is offlined, otherwise, return false */
753 bool is_memblock_offlined(struct memory_block *mem)
755 return mem->state == MEM_OFFLINE;
758 static struct attribute *memory_root_attrs[] = {
759 #ifdef CONFIG_ARCH_MEMORY_PROBE
760 &dev_attr_probe.attr,
763 #ifdef CONFIG_MEMORY_FAILURE
764 &dev_attr_soft_offline_page.attr,
765 &dev_attr_hard_offline_page.attr,
768 &dev_attr_block_size_bytes.attr,
769 &dev_attr_auto_online_blocks.attr,
773 static struct attribute_group memory_root_attr_group = {
774 .attrs = memory_root_attrs,
777 static const struct attribute_group *memory_root_attr_groups[] = {
778 &memory_root_attr_group,
783 * Initialize the sysfs support for memory devices...
785 void __init memory_dev_init(void)
789 unsigned long block_sz, nr;
791 /* Validate the configured memory block size */
792 block_sz = memory_block_size_bytes();
793 if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE)
794 panic("Memory block size not suitable: 0x%lx\n", block_sz);
795 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
797 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
802 * Create entries for memory sections that were found
803 * during boot and have been initialized
805 mutex_lock(&mem_sysfs_mutex);
806 for (nr = 0; nr <= __highest_present_section_nr;
807 nr += sections_per_block) {
808 err = add_memory_block(nr);
812 mutex_unlock(&mem_sysfs_mutex);
816 panic("%s() failed: %d\n", __func__, ret);
820 * walk_memory_blocks - walk through all present memory blocks overlapped
821 * by the range [start, start + size)
823 * @start: start address of the memory range
824 * @size: size of the memory range
825 * @arg: argument passed to func
826 * @func: callback for each memory section walked
828 * This function walks through all present memory blocks overlapped by the
829 * range [start, start + size), calling func on each memory block.
831 * In case func() returns an error, walking is aborted and the error is
834 int walk_memory_blocks(unsigned long start, unsigned long size,
835 void *arg, walk_memory_blocks_func_t func)
837 const unsigned long start_block_id = phys_to_block_id(start);
838 const unsigned long end_block_id = phys_to_block_id(start + size - 1);
839 struct memory_block *mem;
840 unsigned long block_id;
846 for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
847 mem = find_memory_block_by_id(block_id);
851 ret = func(mem, arg);
852 put_device(&mem->dev);
859 struct for_each_memory_block_cb_data {
860 walk_memory_blocks_func_t func;
864 static int for_each_memory_block_cb(struct device *dev, void *data)
866 struct memory_block *mem = to_memory_block(dev);
867 struct for_each_memory_block_cb_data *cb_data = data;
869 return cb_data->func(mem, cb_data->arg);
873 * for_each_memory_block - walk through all present memory blocks
875 * @arg: argument passed to func
876 * @func: callback for each memory block walked
878 * This function walks through all present memory blocks, calling func on
881 * In case func() returns an error, walking is aborted and the error is
884 int for_each_memory_block(void *arg, walk_memory_blocks_func_t func)
886 struct for_each_memory_block_cb_data cb_data = {
891 return bus_for_each_dev(&memory_subsys, NULL, &cb_data,
892 for_each_memory_block_cb);