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/stat.h>
23 #include <linux/slab.h>
25 #include <linux/atomic.h>
26 #include <linux/uaccess.h>
28 #define MEMORY_CLASS_NAME "memory"
30 #define to_memory_block(dev) container_of(dev, struct memory_block, dev)
32 static int sections_per_block;
34 static inline unsigned long base_memory_block_id(unsigned long section_nr)
36 return section_nr / sections_per_block;
39 static inline unsigned long pfn_to_block_id(unsigned long pfn)
41 return base_memory_block_id(pfn_to_section_nr(pfn));
44 static inline unsigned long phys_to_block_id(unsigned long phys)
46 return pfn_to_block_id(PFN_DOWN(phys));
49 static int memory_subsys_online(struct device *dev);
50 static int memory_subsys_offline(struct device *dev);
52 static struct bus_type memory_subsys = {
53 .name = MEMORY_CLASS_NAME,
54 .dev_name = MEMORY_CLASS_NAME,
55 .online = memory_subsys_online,
56 .offline = memory_subsys_offline,
59 static BLOCKING_NOTIFIER_HEAD(memory_chain);
61 int register_memory_notifier(struct notifier_block *nb)
63 return blocking_notifier_chain_register(&memory_chain, nb);
65 EXPORT_SYMBOL(register_memory_notifier);
67 void unregister_memory_notifier(struct notifier_block *nb)
69 blocking_notifier_chain_unregister(&memory_chain, nb);
71 EXPORT_SYMBOL(unregister_memory_notifier);
73 static void memory_block_release(struct device *dev)
75 struct memory_block *mem = to_memory_block(dev);
80 unsigned long __weak memory_block_size_bytes(void)
82 return MIN_MEMORY_BLOCK_SIZE;
84 EXPORT_SYMBOL_GPL(memory_block_size_bytes);
87 * Show the first physical section index (number) of this memory block.
89 static ssize_t phys_index_show(struct device *dev,
90 struct device_attribute *attr, char *buf)
92 struct memory_block *mem = to_memory_block(dev);
93 unsigned long phys_index;
95 phys_index = mem->start_section_nr / sections_per_block;
96 return sprintf(buf, "%08lx\n", phys_index);
100 * Show whether the memory block is likely to be offlineable (or is already
101 * offline). Once offline, the memory block could be removed. The return
102 * value does, however, not indicate that there is a way to remove the
105 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
108 struct memory_block *mem = to_memory_block(dev);
112 if (mem->state != MEM_ONLINE)
115 for (i = 0; i < sections_per_block; i++) {
116 if (!present_section_nr(mem->start_section_nr + i))
118 pfn = section_nr_to_pfn(mem->start_section_nr + i);
119 ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
123 return sprintf(buf, "%d\n", ret);
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 = sprintf(buf, "online\n");
144 len = sprintf(buf, "offline\n");
146 case MEM_GOING_OFFLINE:
147 len = sprintf(buf, "going-offline\n");
150 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
159 int memory_notify(unsigned long val, void *v)
161 return blocking_notifier_call_chain(&memory_chain, val, v);
165 * The probe routines leave the pages uninitialized, just as the bootmem code
166 * does. Make sure we do not access them, but instead use only information from
169 static bool pages_correctly_probed(unsigned long start_pfn)
171 unsigned long section_nr = pfn_to_section_nr(start_pfn);
172 unsigned long section_nr_end = section_nr + sections_per_block;
173 unsigned long pfn = start_pfn;
176 * memmap between sections is not contiguous except with
177 * SPARSEMEM_VMEMMAP. We lookup the page once per section
178 * and assume memmap is contiguous within each section
180 for (; section_nr < section_nr_end; section_nr++) {
181 if (WARN_ON_ONCE(!pfn_valid(pfn)))
184 if (!present_section_nr(section_nr)) {
185 pr_warn("section %ld pfn[%lx, %lx) not present\n",
186 section_nr, pfn, pfn + PAGES_PER_SECTION);
188 } else if (!valid_section_nr(section_nr)) {
189 pr_warn("section %ld pfn[%lx, %lx) no valid memmap\n",
190 section_nr, pfn, pfn + PAGES_PER_SECTION);
192 } else if (online_section_nr(section_nr)) {
193 pr_warn("section %ld pfn[%lx, %lx) is already online\n",
194 section_nr, pfn, pfn + PAGES_PER_SECTION);
197 pfn += PAGES_PER_SECTION;
204 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
205 * OK to have direct references to sparsemem variables in here.
208 memory_block_action(unsigned long start_section_nr, unsigned long action,
209 int online_type, int nid)
211 unsigned long start_pfn;
212 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
215 start_pfn = section_nr_to_pfn(start_section_nr);
219 if (!pages_correctly_probed(start_pfn))
222 ret = online_pages(start_pfn, nr_pages, online_type, nid);
225 ret = offline_pages(start_pfn, nr_pages);
228 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
229 "%ld\n", __func__, start_section_nr, action, action);
236 static int memory_block_change_state(struct memory_block *mem,
237 unsigned long to_state, unsigned long from_state_req)
241 if (mem->state != from_state_req)
244 if (to_state == MEM_OFFLINE)
245 mem->state = MEM_GOING_OFFLINE;
247 ret = memory_block_action(mem->start_section_nr, to_state,
248 mem->online_type, mem->nid);
250 mem->state = ret ? from_state_req : to_state;
255 /* The device lock serializes operations on memory_subsys_[online|offline] */
256 static int memory_subsys_online(struct device *dev)
258 struct memory_block *mem = to_memory_block(dev);
261 if (mem->state == MEM_ONLINE)
265 * If we are called from state_store(), online_type will be
266 * set >= 0 Otherwise we were called from the device online
267 * attribute and need to set the online_type.
269 if (mem->online_type < 0)
270 mem->online_type = MMOP_ONLINE_KEEP;
272 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
274 /* clear online_type */
275 mem->online_type = -1;
280 static int memory_subsys_offline(struct device *dev)
282 struct memory_block *mem = to_memory_block(dev);
284 if (mem->state == MEM_OFFLINE)
287 /* Can't offline block with non-present sections */
288 if (mem->section_count != sections_per_block)
291 return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
294 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
295 const char *buf, size_t count)
297 struct memory_block *mem = to_memory_block(dev);
298 int ret, online_type;
300 ret = lock_device_hotplug_sysfs();
304 if (sysfs_streq(buf, "online_kernel"))
305 online_type = MMOP_ONLINE_KERNEL;
306 else if (sysfs_streq(buf, "online_movable"))
307 online_type = MMOP_ONLINE_MOVABLE;
308 else if (sysfs_streq(buf, "online"))
309 online_type = MMOP_ONLINE_KEEP;
310 else if (sysfs_streq(buf, "offline"))
311 online_type = MMOP_OFFLINE;
317 switch (online_type) {
318 case MMOP_ONLINE_KERNEL:
319 case MMOP_ONLINE_MOVABLE:
320 case MMOP_ONLINE_KEEP:
321 /* mem->online_type is protected by device_hotplug_lock */
322 mem->online_type = online_type;
323 ret = device_online(&mem->dev);
326 ret = device_offline(&mem->dev);
329 ret = -EINVAL; /* should never happen */
333 unlock_device_hotplug();
344 * phys_device is a bad name for this. What I really want
345 * is a way to differentiate between memory ranges that
346 * are part of physical devices that constitute
347 * a complete removable unit or fru.
348 * i.e. do these ranges belong to the same physical device,
349 * s.t. if I offline all of these sections I can then
350 * remove the physical device?
352 static ssize_t phys_device_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
355 struct memory_block *mem = to_memory_block(dev);
356 return sprintf(buf, "%d\n", mem->phys_device);
359 #ifdef CONFIG_MEMORY_HOTREMOVE
360 static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
361 unsigned long nr_pages, int online_type,
362 struct zone *default_zone)
366 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
367 if (zone != default_zone) {
369 strcat(buf, zone->name);
373 static ssize_t valid_zones_show(struct device *dev,
374 struct device_attribute *attr, char *buf)
376 struct memory_block *mem = to_memory_block(dev);
377 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
378 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
379 unsigned long valid_start_pfn, valid_end_pfn;
380 struct zone *default_zone;
384 * Check the existing zone. Make sure that we do that only on the
385 * online nodes otherwise the page_zone is not reliable
387 if (mem->state == MEM_ONLINE) {
389 * The block contains more than one zone can not be offlined.
390 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
392 if (!test_pages_in_a_zone(start_pfn, start_pfn + nr_pages,
393 &valid_start_pfn, &valid_end_pfn))
394 return sprintf(buf, "none\n");
395 start_pfn = valid_start_pfn;
396 strcat(buf, page_zone(pfn_to_page(start_pfn))->name);
401 default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages);
402 strcat(buf, default_zone->name);
404 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
406 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
413 static DEVICE_ATTR_RO(valid_zones);
416 static DEVICE_ATTR_RO(phys_index);
417 static DEVICE_ATTR_RW(state);
418 static DEVICE_ATTR_RO(phys_device);
419 static DEVICE_ATTR_RO(removable);
422 * Show the memory block size (shared by all memory blocks).
424 static ssize_t block_size_bytes_show(struct device *dev,
425 struct device_attribute *attr, char *buf)
427 return sprintf(buf, "%lx\n", memory_block_size_bytes());
430 static DEVICE_ATTR_RO(block_size_bytes);
433 * Memory auto online policy.
436 static ssize_t auto_online_blocks_show(struct device *dev,
437 struct device_attribute *attr, char *buf)
439 if (memhp_auto_online)
440 return sprintf(buf, "online\n");
442 return sprintf(buf, "offline\n");
445 static ssize_t auto_online_blocks_store(struct device *dev,
446 struct device_attribute *attr,
447 const char *buf, size_t count)
449 if (sysfs_streq(buf, "online"))
450 memhp_auto_online = true;
451 else if (sysfs_streq(buf, "offline"))
452 memhp_auto_online = false;
459 static DEVICE_ATTR_RW(auto_online_blocks);
462 * Some architectures will have custom drivers to do this, and
463 * will not need to do it from userspace. The fake hot-add code
464 * as well as ppc64 will do all of their discovery in userspace
465 * and will require this interface.
467 #ifdef CONFIG_ARCH_MEMORY_PROBE
468 static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
469 const char *buf, size_t count)
473 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
475 ret = kstrtoull(buf, 0, &phys_addr);
479 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
482 ret = lock_device_hotplug_sysfs();
486 nid = memory_add_physaddr_to_nid(phys_addr);
487 ret = __add_memory(nid, phys_addr,
488 MIN_MEMORY_BLOCK_SIZE * sections_per_block);
495 unlock_device_hotplug();
499 static DEVICE_ATTR_WO(probe);
502 #ifdef CONFIG_MEMORY_FAILURE
504 * Support for offlining pages of memory
507 /* Soft offline a page */
508 static ssize_t soft_offline_page_store(struct device *dev,
509 struct device_attribute *attr,
510 const char *buf, size_t count)
514 if (!capable(CAP_SYS_ADMIN))
516 if (kstrtoull(buf, 0, &pfn) < 0)
519 ret = soft_offline_page(pfn, 0);
520 return ret == 0 ? count : ret;
523 /* Forcibly offline a page, including killing processes. */
524 static ssize_t hard_offline_page_store(struct device *dev,
525 struct device_attribute *attr,
526 const char *buf, size_t count)
530 if (!capable(CAP_SYS_ADMIN))
532 if (kstrtoull(buf, 0, &pfn) < 0)
535 ret = memory_failure(pfn, 0);
536 return ret ? ret : count;
539 static DEVICE_ATTR_WO(soft_offline_page);
540 static DEVICE_ATTR_WO(hard_offline_page);
544 * Note that phys_device is optional. It is here to allow for
545 * differentiation between which *physical* devices each
546 * section belongs to...
548 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
553 /* A reference for the returned memory block device is acquired. */
554 static struct memory_block *find_memory_block_by_id(unsigned long block_id)
558 dev = subsys_find_device_by_id(&memory_subsys, block_id, NULL);
559 return dev ? to_memory_block(dev) : NULL;
563 * For now, we have a linear search to go find the appropriate
564 * memory_block corresponding to a particular phys_index. If
565 * this gets to be a real problem, we can always use a radix
566 * tree or something here.
568 * This could be made generic for all device subsystems.
570 struct memory_block *find_memory_block(struct mem_section *section)
572 unsigned long block_id = base_memory_block_id(__section_nr(section));
574 return find_memory_block_by_id(block_id);
577 static struct attribute *memory_memblk_attrs[] = {
578 &dev_attr_phys_index.attr,
579 &dev_attr_state.attr,
580 &dev_attr_phys_device.attr,
581 &dev_attr_removable.attr,
582 #ifdef CONFIG_MEMORY_HOTREMOVE
583 &dev_attr_valid_zones.attr,
588 static struct attribute_group memory_memblk_attr_group = {
589 .attrs = memory_memblk_attrs,
592 static const struct attribute_group *memory_memblk_attr_groups[] = {
593 &memory_memblk_attr_group,
598 * register_memory - Setup a sysfs device for a memory block
601 int register_memory(struct memory_block *memory)
605 memory->dev.bus = &memory_subsys;
606 memory->dev.id = memory->start_section_nr / sections_per_block;
607 memory->dev.release = memory_block_release;
608 memory->dev.groups = memory_memblk_attr_groups;
609 memory->dev.offline = memory->state == MEM_OFFLINE;
611 ret = device_register(&memory->dev);
613 put_device(&memory->dev);
618 static int init_memory_block(struct memory_block **memory,
619 unsigned long block_id, unsigned long state)
621 struct memory_block *mem;
622 unsigned long start_pfn;
625 mem = find_memory_block_by_id(block_id);
627 put_device(&mem->dev);
630 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
634 mem->start_section_nr = block_id * sections_per_block;
636 start_pfn = section_nr_to_pfn(mem->start_section_nr);
637 mem->phys_device = arch_get_memory_phys_device(start_pfn);
638 mem->nid = NUMA_NO_NODE;
640 ret = register_memory(mem);
646 static int add_memory_block(unsigned long base_section_nr)
648 int ret, section_count = 0;
649 struct memory_block *mem;
652 for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
654 if (present_section_nr(nr))
657 if (section_count == 0)
659 ret = init_memory_block(&mem, base_memory_block_id(base_section_nr),
663 mem->section_count = section_count;
667 static void unregister_memory(struct memory_block *memory)
669 if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
672 /* drop the ref. we got via find_memory_block() */
673 put_device(&memory->dev);
674 device_unregister(&memory->dev);
678 * Create memory block devices for the given memory area. Start and size
679 * have to be aligned to memory block granularity. Memory block devices
680 * will be initialized as offline.
682 * Called under device_hotplug_lock.
684 int create_memory_block_devices(unsigned long start, unsigned long size)
686 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
687 unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
688 struct memory_block *mem;
689 unsigned long block_id;
692 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
693 !IS_ALIGNED(size, memory_block_size_bytes())))
696 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
697 ret = init_memory_block(&mem, block_id, MEM_OFFLINE);
700 mem->section_count = sections_per_block;
703 end_block_id = block_id;
704 for (block_id = start_block_id; block_id != end_block_id;
706 mem = find_memory_block_by_id(block_id);
707 if (WARN_ON_ONCE(!mem))
709 mem->section_count = 0;
710 unregister_memory(mem);
717 * Remove memory block devices for the given memory area. Start and size
718 * have to be aligned to memory block granularity. Memory block devices
719 * have to be offline.
721 * Called under device_hotplug_lock.
723 void remove_memory_block_devices(unsigned long start, unsigned long size)
725 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
726 const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
727 struct memory_block *mem;
728 unsigned long block_id;
730 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
731 !IS_ALIGNED(size, memory_block_size_bytes())))
734 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
735 mem = find_memory_block_by_id(block_id);
736 if (WARN_ON_ONCE(!mem))
738 mem->section_count = 0;
739 unregister_memory_block_under_nodes(mem);
740 unregister_memory(mem);
744 /* return true if the memory block is offlined, otherwise, return false */
745 bool is_memblock_offlined(struct memory_block *mem)
747 return mem->state == MEM_OFFLINE;
750 static struct attribute *memory_root_attrs[] = {
751 #ifdef CONFIG_ARCH_MEMORY_PROBE
752 &dev_attr_probe.attr,
755 #ifdef CONFIG_MEMORY_FAILURE
756 &dev_attr_soft_offline_page.attr,
757 &dev_attr_hard_offline_page.attr,
760 &dev_attr_block_size_bytes.attr,
761 &dev_attr_auto_online_blocks.attr,
765 static struct attribute_group memory_root_attr_group = {
766 .attrs = memory_root_attrs,
769 static const struct attribute_group *memory_root_attr_groups[] = {
770 &memory_root_attr_group,
775 * Initialize the sysfs support for memory devices. At the time this function
776 * is called, we cannot have concurrent creation/deletion of memory block
777 * devices, the device_hotplug_lock is not needed.
779 void __init memory_dev_init(void)
782 unsigned long block_sz, nr;
784 /* Validate the configured memory block size */
785 block_sz = memory_block_size_bytes();
786 if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE)
787 panic("Memory block size not suitable: 0x%lx\n", block_sz);
788 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
790 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
792 panic("%s() failed to register subsystem: %d\n", __func__, ret);
795 * Create entries for memory sections that were found
796 * during boot and have been initialized
798 for (nr = 0; nr <= __highest_present_section_nr;
799 nr += sections_per_block) {
800 ret = add_memory_block(nr);
802 panic("%s() failed to add memory block: %d\n", __func__,
808 * walk_memory_blocks - walk through all present memory blocks overlapped
809 * by the range [start, start + size)
811 * @start: start address of the memory range
812 * @size: size of the memory range
813 * @arg: argument passed to func
814 * @func: callback for each memory section walked
816 * This function walks through all present memory blocks overlapped by the
817 * range [start, start + size), calling func on each memory block.
819 * In case func() returns an error, walking is aborted and the error is
822 int walk_memory_blocks(unsigned long start, unsigned long size,
823 void *arg, walk_memory_blocks_func_t func)
825 const unsigned long start_block_id = phys_to_block_id(start);
826 const unsigned long end_block_id = phys_to_block_id(start + size - 1);
827 struct memory_block *mem;
828 unsigned long block_id;
834 for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
835 mem = find_memory_block_by_id(block_id);
839 ret = func(mem, arg);
840 put_device(&mem->dev);
847 struct for_each_memory_block_cb_data {
848 walk_memory_blocks_func_t func;
852 static int for_each_memory_block_cb(struct device *dev, void *data)
854 struct memory_block *mem = to_memory_block(dev);
855 struct for_each_memory_block_cb_data *cb_data = data;
857 return cb_data->func(mem, cb_data->arg);
861 * for_each_memory_block - walk through all present memory blocks
863 * @arg: argument passed to func
864 * @func: callback for each memory block walked
866 * This function walks through all present memory blocks, calling func on
869 * In case func() returns an error, walking is aborted and the error is
872 int for_each_memory_block(void *arg, walk_memory_blocks_func_t func)
874 struct for_each_memory_block_cb_data cb_data = {
879 return bus_for_each_dev(&memory_subsys, NULL, &cb_data,
880 for_each_memory_block_cb);