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/stat.h>
23 #include <linux/slab.h>
24 #include <linux/xarray.h>
26 #include <linux/atomic.h>
27 #include <linux/uaccess.h>
29 #define MEMORY_CLASS_NAME "memory"
31 static const char *const online_type_to_str[] = {
32 [MMOP_OFFLINE] = "offline",
33 [MMOP_ONLINE] = "online",
34 [MMOP_ONLINE_KERNEL] = "online_kernel",
35 [MMOP_ONLINE_MOVABLE] = "online_movable",
38 int mhp_online_type_from_str(const char *str)
42 for (i = 0; i < ARRAY_SIZE(online_type_to_str); i++) {
43 if (sysfs_streq(str, online_type_to_str[i]))
49 #define to_memory_block(dev) container_of(dev, struct memory_block, dev)
51 static int sections_per_block;
53 static inline unsigned long memory_block_id(unsigned long section_nr)
55 return section_nr / sections_per_block;
58 static inline unsigned long pfn_to_block_id(unsigned long pfn)
60 return memory_block_id(pfn_to_section_nr(pfn));
63 static inline unsigned long phys_to_block_id(unsigned long phys)
65 return pfn_to_block_id(PFN_DOWN(phys));
68 static int memory_subsys_online(struct device *dev);
69 static int memory_subsys_offline(struct device *dev);
71 static struct bus_type memory_subsys = {
72 .name = MEMORY_CLASS_NAME,
73 .dev_name = MEMORY_CLASS_NAME,
74 .online = memory_subsys_online,
75 .offline = memory_subsys_offline,
79 * Memory blocks are cached in a local radix tree to avoid
80 * a costly linear search for the corresponding device on
83 static DEFINE_XARRAY(memory_blocks);
85 static BLOCKING_NOTIFIER_HEAD(memory_chain);
87 int register_memory_notifier(struct notifier_block *nb)
89 return blocking_notifier_chain_register(&memory_chain, nb);
91 EXPORT_SYMBOL(register_memory_notifier);
93 void unregister_memory_notifier(struct notifier_block *nb)
95 blocking_notifier_chain_unregister(&memory_chain, nb);
97 EXPORT_SYMBOL(unregister_memory_notifier);
99 static void memory_block_release(struct device *dev)
101 struct memory_block *mem = to_memory_block(dev);
106 unsigned long __weak memory_block_size_bytes(void)
108 return MIN_MEMORY_BLOCK_SIZE;
110 EXPORT_SYMBOL_GPL(memory_block_size_bytes);
113 * Show the first physical section index (number) of this memory block.
115 static ssize_t phys_index_show(struct device *dev,
116 struct device_attribute *attr, char *buf)
118 struct memory_block *mem = to_memory_block(dev);
119 unsigned long phys_index;
121 phys_index = mem->start_section_nr / sections_per_block;
123 return sysfs_emit(buf, "%08lx\n", phys_index);
127 * Legacy interface that we cannot remove. Always indicate "removable"
128 * with CONFIG_MEMORY_HOTREMOVE - bad heuristic.
130 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
133 return sysfs_emit(buf, "%d\n", (int)IS_ENABLED(CONFIG_MEMORY_HOTREMOVE));
137 * online, offline, going offline, etc.
139 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
142 struct memory_block *mem = to_memory_block(dev);
146 * We can probably put these states in a nice little array
147 * so that they're not open-coded
149 switch (mem->state) {
156 case MEM_GOING_OFFLINE:
157 output = "going-offline";
161 return sysfs_emit(buf, "ERROR-UNKNOWN-%ld\n", mem->state);
164 return sysfs_emit(buf, "%s\n", output);
167 int memory_notify(unsigned long val, void *v)
169 return blocking_notifier_call_chain(&memory_chain, val, v);
173 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
174 * OK to have direct references to sparsemem variables in here.
177 memory_block_action(unsigned long start_section_nr, unsigned long action,
178 int online_type, int nid)
180 unsigned long start_pfn;
181 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
184 start_pfn = section_nr_to_pfn(start_section_nr);
188 ret = online_pages(start_pfn, nr_pages, online_type, nid);
191 ret = offline_pages(start_pfn, nr_pages);
194 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
195 "%ld\n", __func__, start_section_nr, action, action);
202 static int memory_block_change_state(struct memory_block *mem,
203 unsigned long to_state, unsigned long from_state_req)
207 if (mem->state != from_state_req)
210 if (to_state == MEM_OFFLINE)
211 mem->state = MEM_GOING_OFFLINE;
213 ret = memory_block_action(mem->start_section_nr, to_state,
214 mem->online_type, mem->nid);
216 mem->state = ret ? from_state_req : to_state;
221 /* The device lock serializes operations on memory_subsys_[online|offline] */
222 static int memory_subsys_online(struct device *dev)
224 struct memory_block *mem = to_memory_block(dev);
227 if (mem->state == MEM_ONLINE)
231 * When called via device_online() without configuring the online_type,
232 * we want to default to MMOP_ONLINE.
234 if (mem->online_type == MMOP_OFFLINE)
235 mem->online_type = MMOP_ONLINE;
237 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
238 mem->online_type = MMOP_OFFLINE;
243 static int memory_subsys_offline(struct device *dev)
245 struct memory_block *mem = to_memory_block(dev);
247 if (mem->state == MEM_OFFLINE)
250 return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
253 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
254 const char *buf, size_t count)
256 const int online_type = mhp_online_type_from_str(buf);
257 struct memory_block *mem = to_memory_block(dev);
263 ret = lock_device_hotplug_sysfs();
267 switch (online_type) {
268 case MMOP_ONLINE_KERNEL:
269 case MMOP_ONLINE_MOVABLE:
271 /* mem->online_type is protected by device_hotplug_lock */
272 mem->online_type = online_type;
273 ret = device_online(&mem->dev);
276 ret = device_offline(&mem->dev);
279 ret = -EINVAL; /* should never happen */
282 unlock_device_hotplug();
293 * Legacy interface that we cannot remove: s390x exposes the storage increment
294 * covered by a memory block, allowing for identifying which memory blocks
295 * comprise a storage increment. Since a memory block spans complete
296 * storage increments nowadays, this interface is basically unused. Other
297 * archs never exposed != 0.
299 static ssize_t phys_device_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
302 struct memory_block *mem = to_memory_block(dev);
303 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
305 return sysfs_emit(buf, "%d\n",
306 arch_get_memory_phys_device(start_pfn));
309 #ifdef CONFIG_MEMORY_HOTREMOVE
310 static int print_allowed_zone(char *buf, int len, int nid,
311 unsigned long start_pfn, unsigned long nr_pages,
312 int online_type, struct zone *default_zone)
316 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
317 if (zone == default_zone)
320 return sysfs_emit_at(buf, len, " %s", zone->name);
323 static ssize_t valid_zones_show(struct device *dev,
324 struct device_attribute *attr, char *buf)
326 struct memory_block *mem = to_memory_block(dev);
327 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
328 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
329 struct zone *default_zone;
334 * Check the existing zone. Make sure that we do that only on the
335 * online nodes otherwise the page_zone is not reliable
337 if (mem->state == MEM_ONLINE) {
339 * The block contains more than one zone can not be offlined.
340 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
342 default_zone = test_pages_in_a_zone(start_pfn,
343 start_pfn + nr_pages);
345 return sysfs_emit(buf, "%s\n", "none");
346 len += sysfs_emit_at(buf, len, "%s", default_zone->name);
351 default_zone = zone_for_pfn_range(MMOP_ONLINE, nid, start_pfn,
354 len += sysfs_emit_at(buf, len, "%s", default_zone->name);
355 len += print_allowed_zone(buf, len, nid, start_pfn, nr_pages,
356 MMOP_ONLINE_KERNEL, default_zone);
357 len += print_allowed_zone(buf, len, nid, start_pfn, nr_pages,
358 MMOP_ONLINE_MOVABLE, default_zone);
360 len += sysfs_emit_at(buf, len, "\n");
363 static DEVICE_ATTR_RO(valid_zones);
366 static DEVICE_ATTR_RO(phys_index);
367 static DEVICE_ATTR_RW(state);
368 static DEVICE_ATTR_RO(phys_device);
369 static DEVICE_ATTR_RO(removable);
372 * Show the memory block size (shared by all memory blocks).
374 static ssize_t block_size_bytes_show(struct device *dev,
375 struct device_attribute *attr, char *buf)
377 return sysfs_emit(buf, "%lx\n", memory_block_size_bytes());
380 static DEVICE_ATTR_RO(block_size_bytes);
383 * Memory auto online policy.
386 static ssize_t auto_online_blocks_show(struct device *dev,
387 struct device_attribute *attr, char *buf)
389 return sysfs_emit(buf, "%s\n",
390 online_type_to_str[mhp_default_online_type]);
393 static ssize_t auto_online_blocks_store(struct device *dev,
394 struct device_attribute *attr,
395 const char *buf, size_t count)
397 const int online_type = mhp_online_type_from_str(buf);
402 mhp_default_online_type = online_type;
406 static DEVICE_ATTR_RW(auto_online_blocks);
409 * Some architectures will have custom drivers to do this, and
410 * will not need to do it from userspace. The fake hot-add code
411 * as well as ppc64 will do all of their discovery in userspace
412 * and will require this interface.
414 #ifdef CONFIG_ARCH_MEMORY_PROBE
415 static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
416 const char *buf, size_t count)
420 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
422 ret = kstrtoull(buf, 0, &phys_addr);
426 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
429 ret = lock_device_hotplug_sysfs();
433 nid = memory_add_physaddr_to_nid(phys_addr);
434 ret = __add_memory(nid, phys_addr,
435 MIN_MEMORY_BLOCK_SIZE * sections_per_block,
443 unlock_device_hotplug();
447 static DEVICE_ATTR_WO(probe);
450 #ifdef CONFIG_MEMORY_FAILURE
452 * Support for offlining pages of memory
455 /* Soft offline a page */
456 static ssize_t soft_offline_page_store(struct device *dev,
457 struct device_attribute *attr,
458 const char *buf, size_t count)
462 if (!capable(CAP_SYS_ADMIN))
464 if (kstrtoull(buf, 0, &pfn) < 0)
467 ret = soft_offline_page(pfn, 0);
468 return ret == 0 ? count : ret;
471 /* Forcibly offline a page, including killing processes. */
472 static ssize_t hard_offline_page_store(struct device *dev,
473 struct device_attribute *attr,
474 const char *buf, size_t count)
478 if (!capable(CAP_SYS_ADMIN))
480 if (kstrtoull(buf, 0, &pfn) < 0)
483 ret = memory_failure(pfn, 0);
484 return ret ? ret : count;
487 static DEVICE_ATTR_WO(soft_offline_page);
488 static DEVICE_ATTR_WO(hard_offline_page);
491 /* See phys_device_show(). */
492 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
498 * A reference for the returned memory block device is acquired.
500 * Called under device_hotplug_lock.
502 static struct memory_block *find_memory_block_by_id(unsigned long block_id)
504 struct memory_block *mem;
506 mem = xa_load(&memory_blocks, block_id);
508 get_device(&mem->dev);
513 * Called under device_hotplug_lock.
515 struct memory_block *find_memory_block(struct mem_section *section)
517 unsigned long block_id = memory_block_id(__section_nr(section));
519 return find_memory_block_by_id(block_id);
522 static struct attribute *memory_memblk_attrs[] = {
523 &dev_attr_phys_index.attr,
524 &dev_attr_state.attr,
525 &dev_attr_phys_device.attr,
526 &dev_attr_removable.attr,
527 #ifdef CONFIG_MEMORY_HOTREMOVE
528 &dev_attr_valid_zones.attr,
533 static struct attribute_group memory_memblk_attr_group = {
534 .attrs = memory_memblk_attrs,
537 static const struct attribute_group *memory_memblk_attr_groups[] = {
538 &memory_memblk_attr_group,
543 * register_memory - Setup a sysfs device for a memory block
546 int register_memory(struct memory_block *memory)
550 memory->dev.bus = &memory_subsys;
551 memory->dev.id = memory->start_section_nr / sections_per_block;
552 memory->dev.release = memory_block_release;
553 memory->dev.groups = memory_memblk_attr_groups;
554 memory->dev.offline = memory->state == MEM_OFFLINE;
556 ret = device_register(&memory->dev);
558 put_device(&memory->dev);
561 ret = xa_err(xa_store(&memory_blocks, memory->dev.id, memory,
564 put_device(&memory->dev);
565 device_unregister(&memory->dev);
570 static int init_memory_block(unsigned long block_id, unsigned long state)
572 struct memory_block *mem;
575 mem = find_memory_block_by_id(block_id);
577 put_device(&mem->dev);
580 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
584 mem->start_section_nr = block_id * sections_per_block;
586 mem->nid = NUMA_NO_NODE;
588 ret = register_memory(mem);
593 static int add_memory_block(unsigned long base_section_nr)
595 int section_count = 0;
598 for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
600 if (present_section_nr(nr))
603 if (section_count == 0)
605 return init_memory_block(memory_block_id(base_section_nr),
609 static void unregister_memory(struct memory_block *memory)
611 if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
614 WARN_ON(xa_erase(&memory_blocks, memory->dev.id) == NULL);
616 /* drop the ref. we got via find_memory_block() */
617 put_device(&memory->dev);
618 device_unregister(&memory->dev);
622 * Create memory block devices for the given memory area. Start and size
623 * have to be aligned to memory block granularity. Memory block devices
624 * will be initialized as offline.
626 * Called under device_hotplug_lock.
628 int create_memory_block_devices(unsigned long start, unsigned long size)
630 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
631 unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
632 struct memory_block *mem;
633 unsigned long block_id;
636 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
637 !IS_ALIGNED(size, memory_block_size_bytes())))
640 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
641 ret = init_memory_block(block_id, MEM_OFFLINE);
646 end_block_id = block_id;
647 for (block_id = start_block_id; block_id != end_block_id;
649 mem = find_memory_block_by_id(block_id);
650 if (WARN_ON_ONCE(!mem))
652 unregister_memory(mem);
659 * Remove memory block devices for the given memory area. Start and size
660 * have to be aligned to memory block granularity. Memory block devices
661 * have to be offline.
663 * Called under device_hotplug_lock.
665 void remove_memory_block_devices(unsigned long start, unsigned long size)
667 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
668 const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
669 struct memory_block *mem;
670 unsigned long block_id;
672 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
673 !IS_ALIGNED(size, memory_block_size_bytes())))
676 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
677 mem = find_memory_block_by_id(block_id);
678 if (WARN_ON_ONCE(!mem))
680 unregister_memory_block_under_nodes(mem);
681 unregister_memory(mem);
685 /* return true if the memory block is offlined, otherwise, return false */
686 bool is_memblock_offlined(struct memory_block *mem)
688 return mem->state == MEM_OFFLINE;
691 static struct attribute *memory_root_attrs[] = {
692 #ifdef CONFIG_ARCH_MEMORY_PROBE
693 &dev_attr_probe.attr,
696 #ifdef CONFIG_MEMORY_FAILURE
697 &dev_attr_soft_offline_page.attr,
698 &dev_attr_hard_offline_page.attr,
701 &dev_attr_block_size_bytes.attr,
702 &dev_attr_auto_online_blocks.attr,
706 static struct attribute_group memory_root_attr_group = {
707 .attrs = memory_root_attrs,
710 static const struct attribute_group *memory_root_attr_groups[] = {
711 &memory_root_attr_group,
716 * Initialize the sysfs support for memory devices. At the time this function
717 * is called, we cannot have concurrent creation/deletion of memory block
718 * devices, the device_hotplug_lock is not needed.
720 void __init memory_dev_init(void)
723 unsigned long block_sz, nr;
725 /* Validate the configured memory block size */
726 block_sz = memory_block_size_bytes();
727 if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE)
728 panic("Memory block size not suitable: 0x%lx\n", block_sz);
729 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
731 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
733 panic("%s() failed to register subsystem: %d\n", __func__, ret);
736 * Create entries for memory sections that were found
737 * during boot and have been initialized
739 for (nr = 0; nr <= __highest_present_section_nr;
740 nr += sections_per_block) {
741 ret = add_memory_block(nr);
743 panic("%s() failed to add memory block: %d\n", __func__,
749 * walk_memory_blocks - walk through all present memory blocks overlapped
750 * by the range [start, start + size)
752 * @start: start address of the memory range
753 * @size: size of the memory range
754 * @arg: argument passed to func
755 * @func: callback for each memory section walked
757 * This function walks through all present memory blocks overlapped by the
758 * range [start, start + size), calling func on each memory block.
760 * In case func() returns an error, walking is aborted and the error is
763 * Called under device_hotplug_lock.
765 int walk_memory_blocks(unsigned long start, unsigned long size,
766 void *arg, walk_memory_blocks_func_t func)
768 const unsigned long start_block_id = phys_to_block_id(start);
769 const unsigned long end_block_id = phys_to_block_id(start + size - 1);
770 struct memory_block *mem;
771 unsigned long block_id;
777 for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
778 mem = find_memory_block_by_id(block_id);
782 ret = func(mem, arg);
783 put_device(&mem->dev);
790 struct for_each_memory_block_cb_data {
791 walk_memory_blocks_func_t func;
795 static int for_each_memory_block_cb(struct device *dev, void *data)
797 struct memory_block *mem = to_memory_block(dev);
798 struct for_each_memory_block_cb_data *cb_data = data;
800 return cb_data->func(mem, cb_data->arg);
804 * for_each_memory_block - walk through all present memory blocks
806 * @arg: argument passed to func
807 * @func: callback for each memory block walked
809 * This function walks through all present memory blocks, calling func on
812 * In case func() returns an error, walking is aborted and the error is
815 int for_each_memory_block(void *arg, walk_memory_blocks_func_t func)
817 struct for_each_memory_block_cb_data cb_data = {
822 return bus_for_each_dev(&memory_subsys, NULL, &cb_data,
823 for_each_memory_block_cb);