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>
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);
86 * Memory groups, indexed by memory group id (mgid).
88 static DEFINE_XARRAY_FLAGS(memory_groups, XA_FLAGS_ALLOC);
89 #define MEMORY_GROUP_MARK_DYNAMIC XA_MARK_1
91 static BLOCKING_NOTIFIER_HEAD(memory_chain);
93 int register_memory_notifier(struct notifier_block *nb)
95 return blocking_notifier_chain_register(&memory_chain, nb);
97 EXPORT_SYMBOL(register_memory_notifier);
99 void unregister_memory_notifier(struct notifier_block *nb)
101 blocking_notifier_chain_unregister(&memory_chain, nb);
103 EXPORT_SYMBOL(unregister_memory_notifier);
105 static void memory_block_release(struct device *dev)
107 struct memory_block *mem = to_memory_block(dev);
108 /* Verify that the altmap is freed */
109 WARN_ON(mem->altmap);
113 unsigned long __weak memory_block_size_bytes(void)
115 return MIN_MEMORY_BLOCK_SIZE;
117 EXPORT_SYMBOL_GPL(memory_block_size_bytes);
119 /* Show the memory block ID, relative to the memory block size */
120 static ssize_t phys_index_show(struct device *dev,
121 struct device_attribute *attr, char *buf)
123 struct memory_block *mem = to_memory_block(dev);
125 return sysfs_emit(buf, "%08lx\n", memory_block_id(mem->start_section_nr));
129 * Legacy interface that we cannot remove. Always indicate "removable"
130 * with CONFIG_MEMORY_HOTREMOVE - bad heuristic.
132 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
135 return sysfs_emit(buf, "%d\n", (int)IS_ENABLED(CONFIG_MEMORY_HOTREMOVE));
139 * online, offline, going offline, etc.
141 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
144 struct memory_block *mem = to_memory_block(dev);
148 * We can probably put these states in a nice little array
149 * so that they're not open-coded
151 switch (mem->state) {
158 case MEM_GOING_OFFLINE:
159 output = "going-offline";
163 return sysfs_emit(buf, "ERROR-UNKNOWN-%ld\n", mem->state);
166 return sysfs_emit(buf, "%s\n", output);
169 int memory_notify(unsigned long val, void *v)
171 return blocking_notifier_call_chain(&memory_chain, val, v);
174 #if defined(CONFIG_MEMORY_FAILURE) && defined(CONFIG_MEMORY_HOTPLUG)
175 static unsigned long memblk_nr_poison(struct memory_block *mem);
177 static inline unsigned long memblk_nr_poison(struct memory_block *mem)
183 static int memory_block_online(struct memory_block *mem)
185 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
186 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
187 unsigned long nr_vmemmap_pages = 0;
191 if (memblk_nr_poison(mem))
194 zone = zone_for_pfn_range(mem->online_type, mem->nid, mem->group,
195 start_pfn, nr_pages);
198 * Although vmemmap pages have a different lifecycle than the pages
199 * they describe (they remain until the memory is unplugged), doing
200 * their initialization and accounting at memory onlining/offlining
201 * stage helps to keep accounting easier to follow - e.g vmemmaps
202 * belong to the same zone as the memory they backed.
205 nr_vmemmap_pages = mem->altmap->free;
207 if (nr_vmemmap_pages) {
208 ret = mhp_init_memmap_on_memory(start_pfn, nr_vmemmap_pages, zone);
213 ret = online_pages(start_pfn + nr_vmemmap_pages,
214 nr_pages - nr_vmemmap_pages, zone, mem->group);
216 if (nr_vmemmap_pages)
217 mhp_deinit_memmap_on_memory(start_pfn, nr_vmemmap_pages);
222 * Account once onlining succeeded. If the zone was unpopulated, it is
223 * now already properly populated.
225 if (nr_vmemmap_pages)
226 adjust_present_page_count(pfn_to_page(start_pfn), mem->group,
233 static int memory_block_offline(struct memory_block *mem)
235 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
236 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
237 unsigned long nr_vmemmap_pages = 0;
244 * Unaccount before offlining, such that unpopulated zone and kthreads
245 * can properly be torn down in offline_pages().
248 nr_vmemmap_pages = mem->altmap->free;
250 if (nr_vmemmap_pages)
251 adjust_present_page_count(pfn_to_page(start_pfn), mem->group,
254 ret = offline_pages(start_pfn + nr_vmemmap_pages,
255 nr_pages - nr_vmemmap_pages, mem->zone, mem->group);
257 /* offline_pages() failed. Account back. */
258 if (nr_vmemmap_pages)
259 adjust_present_page_count(pfn_to_page(start_pfn),
260 mem->group, nr_vmemmap_pages);
264 if (nr_vmemmap_pages)
265 mhp_deinit_memmap_on_memory(start_pfn, nr_vmemmap_pages);
272 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
273 * OK to have direct references to sparsemem variables in here.
276 memory_block_action(struct memory_block *mem, unsigned long action)
282 ret = memory_block_online(mem);
285 ret = memory_block_offline(mem);
288 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
289 "%ld\n", __func__, mem->start_section_nr, action, action);
296 static int memory_block_change_state(struct memory_block *mem,
297 unsigned long to_state, unsigned long from_state_req)
301 if (mem->state != from_state_req)
304 if (to_state == MEM_OFFLINE)
305 mem->state = MEM_GOING_OFFLINE;
307 ret = memory_block_action(mem, to_state);
308 mem->state = ret ? from_state_req : to_state;
313 /* The device lock serializes operations on memory_subsys_[online|offline] */
314 static int memory_subsys_online(struct device *dev)
316 struct memory_block *mem = to_memory_block(dev);
319 if (mem->state == MEM_ONLINE)
323 * When called via device_online() without configuring the online_type,
324 * we want to default to MMOP_ONLINE.
326 if (mem->online_type == MMOP_OFFLINE)
327 mem->online_type = MMOP_ONLINE;
329 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
330 mem->online_type = MMOP_OFFLINE;
335 static int memory_subsys_offline(struct device *dev)
337 struct memory_block *mem = to_memory_block(dev);
339 if (mem->state == MEM_OFFLINE)
342 return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
345 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
346 const char *buf, size_t count)
348 const int online_type = mhp_online_type_from_str(buf);
349 struct memory_block *mem = to_memory_block(dev);
355 ret = lock_device_hotplug_sysfs();
359 switch (online_type) {
360 case MMOP_ONLINE_KERNEL:
361 case MMOP_ONLINE_MOVABLE:
363 /* mem->online_type is protected by device_hotplug_lock */
364 mem->online_type = online_type;
365 ret = device_online(&mem->dev);
368 ret = device_offline(&mem->dev);
371 ret = -EINVAL; /* should never happen */
374 unlock_device_hotplug();
385 * Legacy interface that we cannot remove: s390x exposes the storage increment
386 * covered by a memory block, allowing for identifying which memory blocks
387 * comprise a storage increment. Since a memory block spans complete
388 * storage increments nowadays, this interface is basically unused. Other
389 * archs never exposed != 0.
391 static ssize_t phys_device_show(struct device *dev,
392 struct device_attribute *attr, char *buf)
394 struct memory_block *mem = to_memory_block(dev);
395 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
397 return sysfs_emit(buf, "%d\n",
398 arch_get_memory_phys_device(start_pfn));
401 #ifdef CONFIG_MEMORY_HOTREMOVE
402 static int print_allowed_zone(char *buf, int len, int nid,
403 struct memory_group *group,
404 unsigned long start_pfn, unsigned long nr_pages,
405 int online_type, struct zone *default_zone)
409 zone = zone_for_pfn_range(online_type, nid, group, start_pfn, nr_pages);
410 if (zone == default_zone)
413 return sysfs_emit_at(buf, len, " %s", zone->name);
416 static ssize_t valid_zones_show(struct device *dev,
417 struct device_attribute *attr, char *buf)
419 struct memory_block *mem = to_memory_block(dev);
420 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
421 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
422 struct memory_group *group = mem->group;
423 struct zone *default_zone;
428 * Check the existing zone. Make sure that we do that only on the
429 * online nodes otherwise the page_zone is not reliable
431 if (mem->state == MEM_ONLINE) {
433 * If !mem->zone, the memory block spans multiple zones and
434 * cannot get offlined.
436 default_zone = mem->zone;
438 return sysfs_emit(buf, "%s\n", "none");
439 len += sysfs_emit_at(buf, len, "%s", default_zone->name);
443 default_zone = zone_for_pfn_range(MMOP_ONLINE, nid, group,
444 start_pfn, nr_pages);
446 len += sysfs_emit_at(buf, len, "%s", default_zone->name);
447 len += print_allowed_zone(buf, len, nid, group, start_pfn, nr_pages,
448 MMOP_ONLINE_KERNEL, default_zone);
449 len += print_allowed_zone(buf, len, nid, group, start_pfn, nr_pages,
450 MMOP_ONLINE_MOVABLE, default_zone);
452 len += sysfs_emit_at(buf, len, "\n");
455 static DEVICE_ATTR_RO(valid_zones);
458 static DEVICE_ATTR_RO(phys_index);
459 static DEVICE_ATTR_RW(state);
460 static DEVICE_ATTR_RO(phys_device);
461 static DEVICE_ATTR_RO(removable);
464 * Show the memory block size (shared by all memory blocks).
466 static ssize_t block_size_bytes_show(struct device *dev,
467 struct device_attribute *attr, char *buf)
469 return sysfs_emit(buf, "%lx\n", memory_block_size_bytes());
472 static DEVICE_ATTR_RO(block_size_bytes);
475 * Memory auto online policy.
478 static ssize_t auto_online_blocks_show(struct device *dev,
479 struct device_attribute *attr, char *buf)
481 return sysfs_emit(buf, "%s\n",
482 online_type_to_str[mhp_default_online_type]);
485 static ssize_t auto_online_blocks_store(struct device *dev,
486 struct device_attribute *attr,
487 const char *buf, size_t count)
489 const int online_type = mhp_online_type_from_str(buf);
494 mhp_default_online_type = online_type;
498 static DEVICE_ATTR_RW(auto_online_blocks);
500 #ifdef CONFIG_CRASH_HOTPLUG
501 #include <linux/kexec.h>
502 static ssize_t crash_hotplug_show(struct device *dev,
503 struct device_attribute *attr, char *buf)
505 return sysfs_emit(buf, "%d\n", crash_hotplug_memory_support());
507 static DEVICE_ATTR_RO(crash_hotplug);
511 * Some architectures will have custom drivers to do this, and
512 * will not need to do it from userspace. The fake hot-add code
513 * as well as ppc64 will do all of their discovery in userspace
514 * and will require this interface.
516 #ifdef CONFIG_ARCH_MEMORY_PROBE
517 static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
518 const char *buf, size_t count)
522 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
524 ret = kstrtoull(buf, 0, &phys_addr);
528 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
531 ret = lock_device_hotplug_sysfs();
535 nid = memory_add_physaddr_to_nid(phys_addr);
536 ret = __add_memory(nid, phys_addr,
537 MIN_MEMORY_BLOCK_SIZE * sections_per_block,
545 unlock_device_hotplug();
549 static DEVICE_ATTR_WO(probe);
552 #ifdef CONFIG_MEMORY_FAILURE
554 * Support for offlining pages of memory
557 /* Soft offline a page */
558 static ssize_t soft_offline_page_store(struct device *dev,
559 struct device_attribute *attr,
560 const char *buf, size_t count)
564 if (!capable(CAP_SYS_ADMIN))
566 if (kstrtoull(buf, 0, &pfn) < 0)
569 ret = soft_offline_page(pfn, 0);
570 return ret == 0 ? count : ret;
573 /* Forcibly offline a page, including killing processes. */
574 static ssize_t hard_offline_page_store(struct device *dev,
575 struct device_attribute *attr,
576 const char *buf, size_t count)
580 if (!capable(CAP_SYS_ADMIN))
582 if (kstrtoull(buf, 0, &pfn) < 0)
585 ret = memory_failure(pfn, MF_SW_SIMULATED);
586 if (ret == -EOPNOTSUPP)
588 return ret ? ret : count;
591 static DEVICE_ATTR_WO(soft_offline_page);
592 static DEVICE_ATTR_WO(hard_offline_page);
595 /* See phys_device_show(). */
596 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
602 * A reference for the returned memory block device is acquired.
604 * Called under device_hotplug_lock.
606 static struct memory_block *find_memory_block_by_id(unsigned long block_id)
608 struct memory_block *mem;
610 mem = xa_load(&memory_blocks, block_id);
612 get_device(&mem->dev);
617 * Called under device_hotplug_lock.
619 struct memory_block *find_memory_block(unsigned long section_nr)
621 unsigned long block_id = memory_block_id(section_nr);
623 return find_memory_block_by_id(block_id);
626 static struct attribute *memory_memblk_attrs[] = {
627 &dev_attr_phys_index.attr,
628 &dev_attr_state.attr,
629 &dev_attr_phys_device.attr,
630 &dev_attr_removable.attr,
631 #ifdef CONFIG_MEMORY_HOTREMOVE
632 &dev_attr_valid_zones.attr,
637 static const struct attribute_group memory_memblk_attr_group = {
638 .attrs = memory_memblk_attrs,
641 static const struct attribute_group *memory_memblk_attr_groups[] = {
642 &memory_memblk_attr_group,
646 static int __add_memory_block(struct memory_block *memory)
650 memory->dev.bus = &memory_subsys;
651 memory->dev.id = memory->start_section_nr / sections_per_block;
652 memory->dev.release = memory_block_release;
653 memory->dev.groups = memory_memblk_attr_groups;
654 memory->dev.offline = memory->state == MEM_OFFLINE;
656 ret = device_register(&memory->dev);
658 put_device(&memory->dev);
661 ret = xa_err(xa_store(&memory_blocks, memory->dev.id, memory,
664 device_unregister(&memory->dev);
669 static struct zone *early_node_zone_for_memory_block(struct memory_block *mem,
672 const unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
673 const unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
674 struct zone *zone, *matching_zone = NULL;
675 pg_data_t *pgdat = NODE_DATA(nid);
679 * This logic only works for early memory, when the applicable zones
680 * already span the memory block. We don't expect overlapping zones on
681 * a single node for early memory. So if we're told that some PFNs
682 * of a node fall into this memory block, we can assume that all node
683 * zones that intersect with the memory block are actually applicable.
684 * No need to look at the memmap.
686 for (i = 0; i < MAX_NR_ZONES; i++) {
687 zone = pgdat->node_zones + i;
688 if (!populated_zone(zone))
690 if (!zone_intersects(zone, start_pfn, nr_pages))
692 if (!matching_zone) {
693 matching_zone = zone;
696 /* Spans multiple zones ... */
697 matching_zone = NULL;
700 return matching_zone;
705 * memory_block_add_nid() - Indicate that system RAM falling into this memory
706 * block device (partially) belongs to the given node.
707 * @mem: The memory block device.
709 * @context: The memory initialization context.
711 * Indicate that system RAM falling into this memory block (partially) belongs
712 * to the given node. If the context indicates ("early") that we are adding the
713 * node during node device subsystem initialization, this will also properly
714 * set/adjust mem->zone based on the zone ranges of the given node.
716 void memory_block_add_nid(struct memory_block *mem, int nid,
717 enum meminit_context context)
719 if (context == MEMINIT_EARLY && mem->nid != nid) {
721 * For early memory we have to determine the zone when setting
722 * the node id and handle multiple nodes spanning a single
723 * memory block by indicate via zone == NULL that we're not
724 * dealing with a single zone. So if we're setting the node id
725 * the first time, determine if there is a single zone. If we're
726 * setting the node id a second time to a different node,
727 * invalidate the single detected zone.
729 if (mem->nid == NUMA_NO_NODE)
730 mem->zone = early_node_zone_for_memory_block(mem, nid);
736 * If this memory block spans multiple nodes, we only indicate
737 * the last processed node. If we span multiple nodes (not applicable
738 * to hotplugged memory), zone == NULL will prohibit memory offlining
739 * and consequently unplug.
745 static int add_memory_block(unsigned long block_id, unsigned long state,
746 struct vmem_altmap *altmap,
747 struct memory_group *group)
749 struct memory_block *mem;
752 mem = find_memory_block_by_id(block_id);
754 put_device(&mem->dev);
757 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
761 mem->start_section_nr = block_id * sections_per_block;
763 mem->nid = NUMA_NO_NODE;
764 mem->altmap = altmap;
765 INIT_LIST_HEAD(&mem->group_next);
768 if (state == MEM_ONLINE)
770 * MEM_ONLINE at this point implies early memory. With NUMA,
771 * we'll determine the zone when setting the node id via
772 * memory_block_add_nid(). Memory hotplug updated the zone
773 * manually when memory onlining/offlining succeeds.
775 mem->zone = early_node_zone_for_memory_block(mem, NUMA_NO_NODE);
776 #endif /* CONFIG_NUMA */
778 ret = __add_memory_block(mem);
784 list_add(&mem->group_next, &group->memory_blocks);
790 static int __init add_boot_memory_block(unsigned long base_section_nr)
792 int section_count = 0;
795 for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
797 if (present_section_nr(nr))
800 if (section_count == 0)
802 return add_memory_block(memory_block_id(base_section_nr),
803 MEM_ONLINE, NULL, NULL);
806 static int add_hotplug_memory_block(unsigned long block_id,
807 struct vmem_altmap *altmap,
808 struct memory_group *group)
810 return add_memory_block(block_id, MEM_OFFLINE, altmap, group);
813 static void remove_memory_block(struct memory_block *memory)
815 if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
818 WARN_ON(xa_erase(&memory_blocks, memory->dev.id) == NULL);
821 list_del(&memory->group_next);
822 memory->group = NULL;
825 /* drop the ref. we got via find_memory_block() */
826 put_device(&memory->dev);
827 device_unregister(&memory->dev);
831 * Create memory block devices for the given memory area. Start and size
832 * have to be aligned to memory block granularity. Memory block devices
833 * will be initialized as offline.
835 * Called under device_hotplug_lock.
837 int create_memory_block_devices(unsigned long start, unsigned long size,
838 struct vmem_altmap *altmap,
839 struct memory_group *group)
841 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
842 unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
843 struct memory_block *mem;
844 unsigned long block_id;
847 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
848 !IS_ALIGNED(size, memory_block_size_bytes())))
851 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
852 ret = add_hotplug_memory_block(block_id, altmap, group);
857 end_block_id = block_id;
858 for (block_id = start_block_id; block_id != end_block_id;
860 mem = find_memory_block_by_id(block_id);
861 if (WARN_ON_ONCE(!mem))
863 remove_memory_block(mem);
870 * Remove memory block devices for the given memory area. Start and size
871 * have to be aligned to memory block granularity. Memory block devices
872 * have to be offline.
874 * Called under device_hotplug_lock.
876 void remove_memory_block_devices(unsigned long start, unsigned long size)
878 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
879 const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
880 struct memory_block *mem;
881 unsigned long block_id;
883 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
884 !IS_ALIGNED(size, memory_block_size_bytes())))
887 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
888 mem = find_memory_block_by_id(block_id);
889 if (WARN_ON_ONCE(!mem))
891 num_poisoned_pages_sub(-1UL, memblk_nr_poison(mem));
892 unregister_memory_block_under_nodes(mem);
893 remove_memory_block(mem);
897 static struct attribute *memory_root_attrs[] = {
898 #ifdef CONFIG_ARCH_MEMORY_PROBE
899 &dev_attr_probe.attr,
902 #ifdef CONFIG_MEMORY_FAILURE
903 &dev_attr_soft_offline_page.attr,
904 &dev_attr_hard_offline_page.attr,
907 &dev_attr_block_size_bytes.attr,
908 &dev_attr_auto_online_blocks.attr,
909 #ifdef CONFIG_CRASH_HOTPLUG
910 &dev_attr_crash_hotplug.attr,
915 static const struct attribute_group memory_root_attr_group = {
916 .attrs = memory_root_attrs,
919 static const struct attribute_group *memory_root_attr_groups[] = {
920 &memory_root_attr_group,
925 * Initialize the sysfs support for memory devices. At the time this function
926 * is called, we cannot have concurrent creation/deletion of memory block
927 * devices, the device_hotplug_lock is not needed.
929 void __init memory_dev_init(void)
932 unsigned long block_sz, nr;
934 /* Validate the configured memory block size */
935 block_sz = memory_block_size_bytes();
936 if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE)
937 panic("Memory block size not suitable: 0x%lx\n", block_sz);
938 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
940 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
942 panic("%s() failed to register subsystem: %d\n", __func__, ret);
945 * Create entries for memory sections that were found
946 * during boot and have been initialized
948 for (nr = 0; nr <= __highest_present_section_nr;
949 nr += sections_per_block) {
950 ret = add_boot_memory_block(nr);
952 panic("%s() failed to add memory block: %d\n", __func__,
958 * walk_memory_blocks - walk through all present memory blocks overlapped
959 * by the range [start, start + size)
961 * @start: start address of the memory range
962 * @size: size of the memory range
963 * @arg: argument passed to func
964 * @func: callback for each memory section walked
966 * This function walks through all present memory blocks overlapped by the
967 * range [start, start + size), calling func on each memory block.
969 * In case func() returns an error, walking is aborted and the error is
972 * Called under device_hotplug_lock.
974 int walk_memory_blocks(unsigned long start, unsigned long size,
975 void *arg, walk_memory_blocks_func_t func)
977 const unsigned long start_block_id = phys_to_block_id(start);
978 const unsigned long end_block_id = phys_to_block_id(start + size - 1);
979 struct memory_block *mem;
980 unsigned long block_id;
986 for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
987 mem = find_memory_block_by_id(block_id);
991 ret = func(mem, arg);
992 put_device(&mem->dev);
999 struct for_each_memory_block_cb_data {
1000 walk_memory_blocks_func_t func;
1004 static int for_each_memory_block_cb(struct device *dev, void *data)
1006 struct memory_block *mem = to_memory_block(dev);
1007 struct for_each_memory_block_cb_data *cb_data = data;
1009 return cb_data->func(mem, cb_data->arg);
1013 * for_each_memory_block - walk through all present memory blocks
1015 * @arg: argument passed to func
1016 * @func: callback for each memory block walked
1018 * This function walks through all present memory blocks, calling func on
1019 * each memory block.
1021 * In case func() returns an error, walking is aborted and the error is
1024 int for_each_memory_block(void *arg, walk_memory_blocks_func_t func)
1026 struct for_each_memory_block_cb_data cb_data = {
1031 return bus_for_each_dev(&memory_subsys, NULL, &cb_data,
1032 for_each_memory_block_cb);
1036 * This is an internal helper to unify allocation and initialization of
1037 * memory groups. Note that the passed memory group will be copied to a
1038 * dynamically allocated memory group. After this call, the passed
1039 * memory group should no longer be used.
1041 static int memory_group_register(struct memory_group group)
1043 struct memory_group *new_group;
1047 if (!node_possible(group.nid))
1050 new_group = kzalloc(sizeof(group), GFP_KERNEL);
1054 INIT_LIST_HEAD(&new_group->memory_blocks);
1056 ret = xa_alloc(&memory_groups, &mgid, new_group, xa_limit_31b,
1061 } else if (group.is_dynamic) {
1062 xa_set_mark(&memory_groups, mgid, MEMORY_GROUP_MARK_DYNAMIC);
1068 * memory_group_register_static() - Register a static memory group.
1069 * @nid: The node id.
1070 * @max_pages: The maximum number of pages we'll have in this static memory
1073 * Register a new static memory group and return the memory group id.
1074 * All memory in the group belongs to a single unit, such as a DIMM. All
1075 * memory belonging to a static memory group is added in one go to be removed
1076 * in one go -- it's static.
1078 * Returns an error if out of memory, if the node id is invalid, if no new
1079 * memory groups can be registered, or if max_pages is invalid (0). Otherwise,
1080 * returns the new memory group id.
1082 int memory_group_register_static(int nid, unsigned long max_pages)
1084 struct memory_group group = {
1087 .max_pages = max_pages,
1093 return memory_group_register(group);
1095 EXPORT_SYMBOL_GPL(memory_group_register_static);
1098 * memory_group_register_dynamic() - Register a dynamic memory group.
1099 * @nid: The node id.
1100 * @unit_pages: Unit in pages in which is memory added/removed in this dynamic
1103 * Register a new dynamic memory group and return the memory group id.
1104 * Memory within a dynamic memory group is added/removed dynamically
1107 * Returns an error if out of memory, if the node id is invalid, if no new
1108 * memory groups can be registered, or if unit_pages is invalid (0, not a
1109 * power of two, smaller than a single memory block). Otherwise, returns the
1110 * new memory group id.
1112 int memory_group_register_dynamic(int nid, unsigned long unit_pages)
1114 struct memory_group group = {
1118 .unit_pages = unit_pages,
1122 if (!unit_pages || !is_power_of_2(unit_pages) ||
1123 unit_pages < PHYS_PFN(memory_block_size_bytes()))
1125 return memory_group_register(group);
1127 EXPORT_SYMBOL_GPL(memory_group_register_dynamic);
1130 * memory_group_unregister() - Unregister a memory group.
1131 * @mgid: the memory group id
1133 * Unregister a memory group. If any memory block still belongs to this
1134 * memory group, unregistering will fail.
1136 * Returns -EINVAL if the memory group id is invalid, returns -EBUSY if some
1137 * memory blocks still belong to this memory group and returns 0 if
1138 * unregistering succeeded.
1140 int memory_group_unregister(int mgid)
1142 struct memory_group *group;
1147 group = xa_load(&memory_groups, mgid);
1150 if (!list_empty(&group->memory_blocks))
1152 xa_erase(&memory_groups, mgid);
1156 EXPORT_SYMBOL_GPL(memory_group_unregister);
1159 * This is an internal helper only to be used in core memory hotplug code to
1160 * lookup a memory group. We don't care about locking, as we don't expect a
1161 * memory group to get unregistered while adding memory to it -- because
1162 * the group and the memory is managed by the same driver.
1164 struct memory_group *memory_group_find_by_id(int mgid)
1166 return xa_load(&memory_groups, mgid);
1170 * This is an internal helper only to be used in core memory hotplug code to
1171 * walk all dynamic memory groups excluding a given memory group, either
1172 * belonging to a specific node, or belonging to any node.
1174 int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func,
1175 struct memory_group *excluded, void *arg)
1177 struct memory_group *group;
1178 unsigned long index;
1181 xa_for_each_marked(&memory_groups, index, group,
1182 MEMORY_GROUP_MARK_DYNAMIC) {
1183 if (group == excluded)
1186 if (nid != NUMA_NO_NODE && group->nid != nid)
1188 #endif /* CONFIG_NUMA */
1189 ret = func(group, arg);
1196 #if defined(CONFIG_MEMORY_FAILURE) && defined(CONFIG_MEMORY_HOTPLUG)
1197 void memblk_nr_poison_inc(unsigned long pfn)
1199 const unsigned long block_id = pfn_to_block_id(pfn);
1200 struct memory_block *mem = find_memory_block_by_id(block_id);
1203 atomic_long_inc(&mem->nr_hwpoison);
1206 void memblk_nr_poison_sub(unsigned long pfn, long i)
1208 const unsigned long block_id = pfn_to_block_id(pfn);
1209 struct memory_block *mem = find_memory_block_by_id(block_id);
1212 atomic_long_sub(i, &mem->nr_hwpoison);
1215 static unsigned long memblk_nr_poison(struct memory_block *mem)
1217 return atomic_long_read(&mem->nr_hwpoison);