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 int base_memory_block_id(int section_nr
)
39 return section_nr
/ sections_per_block
;
42 static int memory_subsys_online(struct device
*dev
);
43 static int memory_subsys_offline(struct device
*dev
);
45 static struct bus_type memory_subsys
= {
46 .name
= MEMORY_CLASS_NAME
,
47 .dev_name
= MEMORY_CLASS_NAME
,
48 .online
= memory_subsys_online
,
49 .offline
= memory_subsys_offline
,
52 static BLOCKING_NOTIFIER_HEAD(memory_chain
);
54 int register_memory_notifier(struct notifier_block
*nb
)
56 return blocking_notifier_chain_register(&memory_chain
, nb
);
58 EXPORT_SYMBOL(register_memory_notifier
);
60 void unregister_memory_notifier(struct notifier_block
*nb
)
62 blocking_notifier_chain_unregister(&memory_chain
, nb
);
64 EXPORT_SYMBOL(unregister_memory_notifier
);
66 static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain
);
68 int register_memory_isolate_notifier(struct notifier_block
*nb
)
70 return atomic_notifier_chain_register(&memory_isolate_chain
, nb
);
72 EXPORT_SYMBOL(register_memory_isolate_notifier
);
74 void unregister_memory_isolate_notifier(struct notifier_block
*nb
)
76 atomic_notifier_chain_unregister(&memory_isolate_chain
, nb
);
78 EXPORT_SYMBOL(unregister_memory_isolate_notifier
);
80 static void memory_block_release(struct device
*dev
)
82 struct memory_block
*mem
= to_memory_block(dev
);
87 unsigned long __weak
memory_block_size_bytes(void)
89 return MIN_MEMORY_BLOCK_SIZE
;
91 EXPORT_SYMBOL_GPL(memory_block_size_bytes
);
93 static unsigned long get_memory_block_size(void)
95 unsigned long block_sz
;
97 block_sz
= memory_block_size_bytes();
99 /* Validate blk_sz is a power of 2 and not less than section size */
100 if ((block_sz
& (block_sz
- 1)) || (block_sz
< MIN_MEMORY_BLOCK_SIZE
)) {
102 block_sz
= MIN_MEMORY_BLOCK_SIZE
;
109 * use this as the physical section index that this memsection
113 static ssize_t
phys_index_show(struct device
*dev
,
114 struct device_attribute
*attr
, char *buf
)
116 struct memory_block
*mem
= to_memory_block(dev
);
117 unsigned long phys_index
;
119 phys_index
= mem
->start_section_nr
/ sections_per_block
;
120 return sprintf(buf
, "%08lx\n", phys_index
);
124 * Show whether the section of memory is likely to be hot-removable
126 static ssize_t
removable_show(struct device
*dev
, struct device_attribute
*attr
,
129 unsigned long i
, pfn
;
131 struct memory_block
*mem
= to_memory_block(dev
);
133 if (mem
->state
!= MEM_ONLINE
)
136 for (i
= 0; i
< sections_per_block
; i
++) {
137 if (!present_section_nr(mem
->start_section_nr
+ i
))
139 pfn
= section_nr_to_pfn(mem
->start_section_nr
+ i
);
140 ret
&= is_mem_section_removable(pfn
, PAGES_PER_SECTION
);
144 return sprintf(buf
, "%d\n", ret
);
148 * online, offline, going offline, etc.
150 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
153 struct memory_block
*mem
= to_memory_block(dev
);
157 * We can probably put these states in a nice little array
158 * so that they're not open-coded
160 switch (mem
->state
) {
162 len
= sprintf(buf
, "online\n");
165 len
= sprintf(buf
, "offline\n");
167 case MEM_GOING_OFFLINE
:
168 len
= sprintf(buf
, "going-offline\n");
171 len
= sprintf(buf
, "ERROR-UNKNOWN-%ld\n",
180 int memory_notify(unsigned long val
, void *v
)
182 return blocking_notifier_call_chain(&memory_chain
, val
, v
);
185 int memory_isolate_notify(unsigned long val
, void *v
)
187 return atomic_notifier_call_chain(&memory_isolate_chain
, val
, v
);
191 * The probe routines leave the pages uninitialized, just as the bootmem code
192 * does. Make sure we do not access them, but instead use only information from
195 static bool pages_correctly_probed(unsigned long start_pfn
)
197 unsigned long section_nr
= pfn_to_section_nr(start_pfn
);
198 unsigned long section_nr_end
= section_nr
+ sections_per_block
;
199 unsigned long pfn
= start_pfn
;
202 * memmap between sections is not contiguous except with
203 * SPARSEMEM_VMEMMAP. We lookup the page once per section
204 * and assume memmap is contiguous within each section
206 for (; section_nr
< section_nr_end
; section_nr
++) {
207 if (WARN_ON_ONCE(!pfn_valid(pfn
)))
210 if (!present_section_nr(section_nr
)) {
211 pr_warn("section %ld pfn[%lx, %lx) not present\n",
212 section_nr
, pfn
, pfn
+ PAGES_PER_SECTION
);
214 } else if (!valid_section_nr(section_nr
)) {
215 pr_warn("section %ld pfn[%lx, %lx) no valid memmap\n",
216 section_nr
, pfn
, pfn
+ PAGES_PER_SECTION
);
218 } else if (online_section_nr(section_nr
)) {
219 pr_warn("section %ld pfn[%lx, %lx) is already online\n",
220 section_nr
, pfn
, pfn
+ PAGES_PER_SECTION
);
223 pfn
+= PAGES_PER_SECTION
;
230 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
231 * OK to have direct references to sparsemem variables in here.
234 memory_block_action(unsigned long start_section_nr
, unsigned long action
,
237 unsigned long start_pfn
;
238 unsigned long nr_pages
= PAGES_PER_SECTION
* sections_per_block
;
241 start_pfn
= section_nr_to_pfn(start_section_nr
);
245 if (!pages_correctly_probed(start_pfn
))
248 ret
= online_pages(start_pfn
, nr_pages
, online_type
);
251 ret
= offline_pages(start_pfn
, nr_pages
);
254 WARN(1, KERN_WARNING
"%s(%ld, %ld) unknown action: "
255 "%ld\n", __func__
, start_section_nr
, action
, action
);
262 static int memory_block_change_state(struct memory_block
*mem
,
263 unsigned long to_state
, unsigned long from_state_req
)
267 if (mem
->state
!= from_state_req
)
270 if (to_state
== MEM_OFFLINE
)
271 mem
->state
= MEM_GOING_OFFLINE
;
273 ret
= memory_block_action(mem
->start_section_nr
, to_state
,
276 mem
->state
= ret
? from_state_req
: to_state
;
281 /* The device lock serializes operations on memory_subsys_[online|offline] */
282 static int memory_subsys_online(struct device
*dev
)
284 struct memory_block
*mem
= to_memory_block(dev
);
287 if (mem
->state
== MEM_ONLINE
)
291 * If we are called from state_store(), online_type will be
292 * set >= 0 Otherwise we were called from the device online
293 * attribute and need to set the online_type.
295 if (mem
->online_type
< 0)
296 mem
->online_type
= MMOP_ONLINE_KEEP
;
298 ret
= memory_block_change_state(mem
, MEM_ONLINE
, MEM_OFFLINE
);
300 /* clear online_type */
301 mem
->online_type
= -1;
306 static int memory_subsys_offline(struct device
*dev
)
308 struct memory_block
*mem
= to_memory_block(dev
);
310 if (mem
->state
== MEM_OFFLINE
)
313 /* Can't offline block with non-present sections */
314 if (mem
->section_count
!= sections_per_block
)
317 return memory_block_change_state(mem
, MEM_OFFLINE
, MEM_ONLINE
);
320 static ssize_t
state_store(struct device
*dev
, struct device_attribute
*attr
,
321 const char *buf
, size_t count
)
323 struct memory_block
*mem
= to_memory_block(dev
);
324 int ret
, online_type
;
326 ret
= lock_device_hotplug_sysfs();
330 if (sysfs_streq(buf
, "online_kernel"))
331 online_type
= MMOP_ONLINE_KERNEL
;
332 else if (sysfs_streq(buf
, "online_movable"))
333 online_type
= MMOP_ONLINE_MOVABLE
;
334 else if (sysfs_streq(buf
, "online"))
335 online_type
= MMOP_ONLINE_KEEP
;
336 else if (sysfs_streq(buf
, "offline"))
337 online_type
= MMOP_OFFLINE
;
343 switch (online_type
) {
344 case MMOP_ONLINE_KERNEL
:
345 case MMOP_ONLINE_MOVABLE
:
346 case MMOP_ONLINE_KEEP
:
347 /* mem->online_type is protected by device_hotplug_lock */
348 mem
->online_type
= online_type
;
349 ret
= device_online(&mem
->dev
);
352 ret
= device_offline(&mem
->dev
);
355 ret
= -EINVAL
; /* should never happen */
359 unlock_device_hotplug();
370 * phys_device is a bad name for this. What I really want
371 * is a way to differentiate between memory ranges that
372 * are part of physical devices that constitute
373 * a complete removable unit or fru.
374 * i.e. do these ranges belong to the same physical device,
375 * s.t. if I offline all of these sections I can then
376 * remove the physical device?
378 static ssize_t
phys_device_show(struct device
*dev
,
379 struct device_attribute
*attr
, char *buf
)
381 struct memory_block
*mem
= to_memory_block(dev
);
382 return sprintf(buf
, "%d\n", mem
->phys_device
);
385 #ifdef CONFIG_MEMORY_HOTREMOVE
386 static void print_allowed_zone(char *buf
, int nid
, unsigned long start_pfn
,
387 unsigned long nr_pages
, int online_type
,
388 struct zone
*default_zone
)
392 zone
= zone_for_pfn_range(online_type
, nid
, start_pfn
, nr_pages
);
393 if (zone
!= default_zone
) {
395 strcat(buf
, zone
->name
);
399 static ssize_t
valid_zones_show(struct device
*dev
,
400 struct device_attribute
*attr
, char *buf
)
402 struct memory_block
*mem
= to_memory_block(dev
);
403 unsigned long start_pfn
= section_nr_to_pfn(mem
->start_section_nr
);
404 unsigned long nr_pages
= PAGES_PER_SECTION
* sections_per_block
;
405 unsigned long valid_start_pfn
, valid_end_pfn
;
406 struct zone
*default_zone
;
410 * Check the existing zone. Make sure that we do that only on the
411 * online nodes otherwise the page_zone is not reliable
413 if (mem
->state
== MEM_ONLINE
) {
415 * The block contains more than one zone can not be offlined.
416 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
418 if (!test_pages_in_a_zone(start_pfn
, start_pfn
+ nr_pages
,
419 &valid_start_pfn
, &valid_end_pfn
))
420 return sprintf(buf
, "none\n");
421 start_pfn
= valid_start_pfn
;
422 strcat(buf
, page_zone(pfn_to_page(start_pfn
))->name
);
427 default_zone
= zone_for_pfn_range(MMOP_ONLINE_KEEP
, nid
, start_pfn
, nr_pages
);
428 strcat(buf
, default_zone
->name
);
430 print_allowed_zone(buf
, nid
, start_pfn
, nr_pages
, MMOP_ONLINE_KERNEL
,
432 print_allowed_zone(buf
, nid
, start_pfn
, nr_pages
, MMOP_ONLINE_MOVABLE
,
439 static DEVICE_ATTR_RO(valid_zones
);
442 static DEVICE_ATTR_RO(phys_index
);
443 static DEVICE_ATTR_RW(state
);
444 static DEVICE_ATTR_RO(phys_device
);
445 static DEVICE_ATTR_RO(removable
);
448 * Block size attribute stuff
450 static ssize_t
block_size_bytes_show(struct device
*dev
,
451 struct device_attribute
*attr
, char *buf
)
453 return sprintf(buf
, "%lx\n", get_memory_block_size());
456 static DEVICE_ATTR_RO(block_size_bytes
);
459 * Memory auto online policy.
462 static ssize_t
auto_online_blocks_show(struct device
*dev
,
463 struct device_attribute
*attr
, char *buf
)
465 if (memhp_auto_online
)
466 return sprintf(buf
, "online\n");
468 return sprintf(buf
, "offline\n");
471 static ssize_t
auto_online_blocks_store(struct device
*dev
,
472 struct device_attribute
*attr
,
473 const char *buf
, size_t count
)
475 if (sysfs_streq(buf
, "online"))
476 memhp_auto_online
= true;
477 else if (sysfs_streq(buf
, "offline"))
478 memhp_auto_online
= false;
485 static DEVICE_ATTR_RW(auto_online_blocks
);
488 * Some architectures will have custom drivers to do this, and
489 * will not need to do it from userspace. The fake hot-add code
490 * as well as ppc64 will do all of their discovery in userspace
491 * and will require this interface.
493 #ifdef CONFIG_ARCH_MEMORY_PROBE
494 static ssize_t
probe_store(struct device
*dev
, struct device_attribute
*attr
,
495 const char *buf
, size_t count
)
499 unsigned long pages_per_block
= PAGES_PER_SECTION
* sections_per_block
;
501 ret
= kstrtoull(buf
, 0, &phys_addr
);
505 if (phys_addr
& ((pages_per_block
<< PAGE_SHIFT
) - 1))
508 ret
= lock_device_hotplug_sysfs();
512 nid
= memory_add_physaddr_to_nid(phys_addr
);
513 ret
= __add_memory(nid
, phys_addr
,
514 MIN_MEMORY_BLOCK_SIZE
* sections_per_block
);
521 unlock_device_hotplug();
525 static DEVICE_ATTR_WO(probe
);
528 #ifdef CONFIG_MEMORY_FAILURE
530 * Support for offlining pages of memory
533 /* Soft offline a page */
534 static ssize_t
soft_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)
547 ret
= soft_offline_page(pfn_to_page(pfn
), 0);
548 return ret
== 0 ? count
: ret
;
551 /* Forcibly offline a page, including killing processes. */
552 static ssize_t
hard_offline_page_store(struct device
*dev
,
553 struct device_attribute
*attr
,
554 const char *buf
, size_t count
)
558 if (!capable(CAP_SYS_ADMIN
))
560 if (kstrtoull(buf
, 0, &pfn
) < 0)
563 ret
= memory_failure(pfn
, 0);
564 return ret
? ret
: count
;
567 static DEVICE_ATTR_WO(soft_offline_page
);
568 static DEVICE_ATTR_WO(hard_offline_page
);
572 * Note that phys_device is optional. It is here to allow for
573 * differentiation between which *physical* devices each
574 * section belongs to...
576 int __weak
arch_get_memory_phys_device(unsigned long start_pfn
)
582 * A reference for the returned object is held and the reference for the
583 * hinted object is released.
585 struct memory_block
*find_memory_block_hinted(struct mem_section
*section
,
586 struct memory_block
*hint
)
588 int block_id
= base_memory_block_id(__section_nr(section
));
589 struct device
*hintdev
= hint
? &hint
->dev
: NULL
;
592 dev
= subsys_find_device_by_id(&memory_subsys
, block_id
, hintdev
);
594 put_device(&hint
->dev
);
597 return to_memory_block(dev
);
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 return find_memory_block_hinted(section
, NULL
);
613 static struct attribute
*memory_memblk_attrs
[] = {
614 &dev_attr_phys_index
.attr
,
615 &dev_attr_state
.attr
,
616 &dev_attr_phys_device
.attr
,
617 &dev_attr_removable
.attr
,
618 #ifdef CONFIG_MEMORY_HOTREMOVE
619 &dev_attr_valid_zones
.attr
,
624 static struct attribute_group memory_memblk_attr_group
= {
625 .attrs
= memory_memblk_attrs
,
628 static const struct attribute_group
*memory_memblk_attr_groups
[] = {
629 &memory_memblk_attr_group
,
634 * register_memory - Setup a sysfs device for a memory block
637 int register_memory(struct memory_block
*memory
)
641 memory
->dev
.bus
= &memory_subsys
;
642 memory
->dev
.id
= memory
->start_section_nr
/ sections_per_block
;
643 memory
->dev
.release
= memory_block_release
;
644 memory
->dev
.groups
= memory_memblk_attr_groups
;
645 memory
->dev
.offline
= memory
->state
== MEM_OFFLINE
;
647 ret
= device_register(&memory
->dev
);
649 put_device(&memory
->dev
);
654 static int init_memory_block(struct memory_block
**memory
,
655 struct mem_section
*section
, unsigned long state
)
657 struct memory_block
*mem
;
658 unsigned long start_pfn
;
662 mem
= kzalloc(sizeof(*mem
), GFP_KERNEL
);
666 scn_nr
= __section_nr(section
);
667 mem
->start_section_nr
=
668 base_memory_block_id(scn_nr
) * sections_per_block
;
669 mem
->end_section_nr
= mem
->start_section_nr
+ sections_per_block
- 1;
671 start_pfn
= section_nr_to_pfn(mem
->start_section_nr
);
672 mem
->phys_device
= arch_get_memory_phys_device(start_pfn
);
674 ret
= register_memory(mem
);
680 static int add_memory_block(int base_section_nr
)
682 struct memory_block
*mem
;
683 int i
, ret
, section_count
= 0, section_nr
;
685 for (i
= base_section_nr
;
686 i
< base_section_nr
+ sections_per_block
;
688 if (!present_section_nr(i
))
690 if (section_count
== 0)
695 if (section_count
== 0)
697 ret
= init_memory_block(&mem
, __nr_to_section(section_nr
), MEM_ONLINE
);
700 mem
->section_count
= section_count
;
705 * need an interface for the VM to add new memory regions,
706 * but without onlining it.
708 int hotplug_memory_register(int nid
, struct mem_section
*section
)
711 struct memory_block
*mem
;
713 mutex_lock(&mem_sysfs_mutex
);
715 mem
= find_memory_block(section
);
717 mem
->section_count
++;
718 put_device(&mem
->dev
);
720 ret
= init_memory_block(&mem
, section
, MEM_OFFLINE
);
723 mem
->section_count
++;
727 mutex_unlock(&mem_sysfs_mutex
);
731 #ifdef CONFIG_MEMORY_HOTREMOVE
733 unregister_memory(struct memory_block
*memory
)
735 BUG_ON(memory
->dev
.bus
!= &memory_subsys
);
737 /* drop the ref. we got via find_memory_block() */
738 put_device(&memory
->dev
);
739 device_unregister(&memory
->dev
);
742 void unregister_memory_section(struct mem_section
*section
)
744 struct memory_block
*mem
;
746 if (WARN_ON_ONCE(!present_section(section
)))
749 mutex_lock(&mem_sysfs_mutex
);
752 * Some users of the memory hotplug do not want/need memblock to
753 * track all sections. Skip over those.
755 mem
= find_memory_block(section
);
759 unregister_mem_sect_under_nodes(mem
, __section_nr(section
));
761 mem
->section_count
--;
762 if (mem
->section_count
== 0)
763 unregister_memory(mem
);
765 put_device(&mem
->dev
);
768 mutex_unlock(&mem_sysfs_mutex
);
770 #endif /* CONFIG_MEMORY_HOTREMOVE */
772 /* return true if the memory block is offlined, otherwise, return false */
773 bool is_memblock_offlined(struct memory_block
*mem
)
775 return mem
->state
== MEM_OFFLINE
;
778 static struct attribute
*memory_root_attrs
[] = {
779 #ifdef CONFIG_ARCH_MEMORY_PROBE
780 &dev_attr_probe
.attr
,
783 #ifdef CONFIG_MEMORY_FAILURE
784 &dev_attr_soft_offline_page
.attr
,
785 &dev_attr_hard_offline_page
.attr
,
788 &dev_attr_block_size_bytes
.attr
,
789 &dev_attr_auto_online_blocks
.attr
,
793 static struct attribute_group memory_root_attr_group
= {
794 .attrs
= memory_root_attrs
,
797 static const struct attribute_group
*memory_root_attr_groups
[] = {
798 &memory_root_attr_group
,
803 * Initialize the sysfs support for memory devices...
805 int __init
memory_dev_init(void)
810 unsigned long block_sz
;
812 ret
= subsys_system_register(&memory_subsys
, memory_root_attr_groups
);
816 block_sz
= get_memory_block_size();
817 sections_per_block
= block_sz
/ MIN_MEMORY_BLOCK_SIZE
;
820 * Create entries for memory sections that were found
821 * during boot and have been initialized
823 mutex_lock(&mem_sysfs_mutex
);
824 for (i
= 0; i
<= __highest_present_section_nr
;
825 i
+= sections_per_block
) {
826 err
= add_memory_block(i
);
830 mutex_unlock(&mem_sysfs_mutex
);
834 printk(KERN_ERR
"%s() failed: %d\n", __func__
, ret
);