powerpc/memtrace: Don't offline memory blocks via offline_pages()
Subsystems (2)
cover powerpc/memtrace: Don't offline memory blocks via offline_pages()
This RFC is based on linux-next and
- 2 patches from "PATCH RFC v4 00/13] virtio-mem: paravirtualized memory" -> "mm: Allow to offline unmovable PageOffline() pages via
MEM_GOING_OFFLINE" [1]
-> "mm/memory_hotplug: Introduce offline_and_remove_memory()" [2]
- "mm/memory_hotplug: Don't free usage map when removing a re-added early section" [3]
A branch with all patches (kept updated) is available at:
https://github.com/davidhildenbrand/linux.git memtrace
Stop using offline_pages() to offline memory blocks. Allocate the memory blocks using alloc_contig_pages() first and offline+remove the allcoated memory blocks using a clean MM interface. Offlining of allocated memory is made possible by using PageOffline() in combination with a memory notifier (similar to virto-mem).
Note: In the future, we might want to switch to only removing/readding the page tables of the allocated memory (while still marking it PageOffline()). However, that might have other implications, and requires work from PPC people (IOW, I won't fiddle with that :) ).
[1] https://lkml.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lkml.kernel.org/r/[email protected]
David Hildenbrand (3):
powerpc/memtrace: Enforce power of 2 for memory buffer size powerpc/memtrace: Factor out readding memory into memtrace_free_node() powerpc/memtrace: Don't offline memory blocks via offline_pages()
arch/powerpc/platforms/powernv/Kconfig | 1 + arch/powerpc/platforms/powernv/memtrace.c | 217 ++++++++++++++-------- 2 files changed, 136 insertions(+), 82 deletions(-)
Signature · 1 line
2.23.0
RFC PATCH 1/3 powerpc/memtrace: Enforce power of 2 for memory buffer size
The code mentions "Trace memory needs to be aligned to the size", and e.g., round_up() is documented to work on power of 2 only. Also, the whole search is not optimized e.g., for being aligned to memory block size only while allocating multiple memory blocks.
Let's just limit to powers of 2 that are at least the size of memory blocks - the granularity we are using for alloc/offline/unplug.
--- arch/powerpc/platforms/powernv/memtrace.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-)
arch/powerpc/platforms/powernv/memtrace.c +5 -9
@@ -268,15 +268,11 @@ static int memtrace_online(void)static int memtrace_enable_set (void *data,u64 val) { -u64 bytes; - -/* -* Don't attempt to do anything if size isn't aligned to a memory -* block or equal to zero. -*/ - bytes =memory_block_size_bytes (); -if (val & (bytes -1 )) { -pr_err ("Value must be aligned with 0x%llx\n" , bytes); +const unsigned long bytes =memory_block_size_bytes (); + +if (val && (!is_power_of_2 (val) || val < bytes)) { +pr_err ("Value must be 0 or a power of 2 (at least 0x%lx)\n" , + bytes);return -EINVAL ; }
Signature · 1 line
2.23.0
RFC PATCH 2/3 powerpc/memtrace: Factor out readding memory into memtrace_free_node()
In reply to David HildenbrandWhile at it, move it, we want to reuse it soon.
--- arch/powerpc/platforms/powernv/memtrace.c | 44 ++++++++++++++--------- 1 file changed, 27 insertions(+), 17 deletions(-)
arch/powerpc/platforms/powernv/memtrace.c +27 -17
@@ -50,6 +50,32 @@ static const struct file_operations memtrace_fops = { .open = simple_open, }; +static int online_mem_block (struct memory_block *mem,void *arg) +{ +return device_online (&mem->dev); +} + +static int memtrace_free_node (int nid,unsigned long start,unsigned long size) +{ +int ret; + + ret =add_memory (nid, start, size); +if (!ret) { +/* +* If the kernel isn't compiled with the auto online option, we +* will try to online ourselves. We'll ignore any errors here - +* user space can try to online itself later (after all, the +* memory was added successfully). +*/ +if (!memhp_auto_online) { +lock_device_hotplug (); +walk_memory_blocks (start, size,NULL , online_mem_block); +unlock_device_hotplug (); + } + } +return ret; +} +static int check_memblock_online (struct memory_block *mem,void *arg) {if (mem->state !=MEM_ONLINE )
@@ -202,11 +228,6 @@ static int memtrace_init_debugfs(void)return ret; } -static int online_mem_block (struct memory_block *mem,void *arg) -{ -return device_online (&mem->dev); -} -/* * Iterate through the chunks of memory we have removed from the kernel * and attempt to add them back to the kernel.
@@ -229,24 +250,13 @@ static int memtrace_online(void) ent->mem =0 ; } -if (add_memory (ent->nid, ent->start, ent->size)) { +if (memtrace_free_node (ent->nid, ent->start, ent->size)) {pr_err ("Failed to add trace memory to node %d\n" , ent->nid); ret +=1 ;continue ; } -/* -* If kernel isn't compiled with the auto online option -* we need to online the memory ourselves. -*/ -if (!memhp_auto_online) { -lock_device_hotplug (); -walk_memory_blocks (ent->start, ent->size,NULL , - online_mem_block); -unlock_device_hotplug (); - } -/* * Memory was added successfully so clean up references to it * so on reentry we can tell that this chunk was added.
Signature · 1 line
2.23.0
RFC PATCH 3/3 powerpc/memtrace: Don't offline memory blocks via offline_pages()
In reply to David Hildenbrandoffline_pages() should not be called outside of the MM core. Especially, having to manually fiddle with the memory block states is a sign that this is not a good idea. To offline memory block devices cleanly, device_offline() should be used. This is the only remaining caller of offline_pages(), except the official device_offline() way.
E.g., when trying to allocate right now we trigger messages like
[ 11.227817] page:c00c000000182000 refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 [ 11.228056] raw: 007ffff000000000 c000000001538860 c000000001538860 0000000000000000 [ 11.228070] raw: 0000000000000000 0000000000000001 00000001ffffffff 0000000000000000 [ 11.228097] page dumped because: unmovable page
and theoretically we might end up looping quite a long time trying to offline memory, which would have to be canceled by the user manually (CTRL-C).
Memtrace needs to identify+allocate multiple consecutive memory blocks. It also has to remove the memory blocks to remove all page tables (HW requirement).
Let's use alloc_contig_pages() to allocate memory that spans multiple memory block devices. We can then set all pages PageOffline() to allow these pages to get isolated. A temporary memory notifier can then make offlining of these pages succeed by dropping its reference to the pages on MEM_GOING_OFFLINE events(as documented in include/linux/page-flags.h for PageOffline() pages). Error handling is a bit tricky.
Note1: ZONE_MOVABLE memory blocks won't be considered. Not sure if that was ever really relevant. (unmovable data would end up on these memory blocks for a tiny little time frame)
Note2: We don't have to care about online_page_callback_t, as we forbid re-onlining from our memory notifier.
Note3: I was told this feature is never used along with DIMM-based memory hotunplug - otherwise bad things will happen when a DIMM would try to remove "alread-removed" memory (that is still in use).
Tested under QEMU with powernv emulation (kernel + initramfs).
$ mount -t debugfs none /sys/kernel/debug/ $ cat /sys/devices/system/memory/block_size_bytes 10000000 $ echo 0x20000000 > /sys/kernel/debug/powerpc/memtrace/enable [ 19.809790] Offlined Pages 4096 [ 19.835842] Offlined Pages 4096 [ 19.853136] memtrace: Allocated trace memory on node 0 at 0x0000000040000000
Unfortunately, QEMU does not support NUMA for powernv yet, so I cannot test that.
--- arch/powerpc/platforms/powernv/Kconfig | 1 + arch/powerpc/platforms/powernv/memtrace.c | 175 ++++++++++++++-------- 2 files changed, 112 insertions(+), 64 deletions(-)
arch/powerpc/platforms/powernv/Kconfig +1 -0
@@ -29,6 +29,7 @@ config OPAL_PRD config PPC_MEMTRACE bool "Enable removal of RAM from kernel mappings for tracing" depends on PPC_POWERNV && MEMORY_HOTREMOVE + select CONTIG_ALLOC help Enabling this option allows for the removal of memory (RAM) from the kernel mappings to be used for hardware tracing.
arch/powerpc/platforms/powernv/memtrace.c +111 -64
194 lines not on this page · raw message
Signature · 1 line
2.23.0
Quoted 27 lines · David Hildenbrand
This RFC is based on linux-next and
- 2 patches from "PATCH RFC v4 00/13] virtio-mem: paravirtualized memory" -> "mm: Allow to offline unmovablePageOffline()pages viaMEM_GOING_OFFLINE" [1]-> "
mm/memory_hotplug: Introduceoffline_and_remove_memory()" [2]
- "mm/memory_hotplug: Don't free usage map when removing a re-added early section" [3]A branch with all patches (kept updated) is available at:
https://github.com/davidhildenbrand/linux.git memtraceStop using
offline_pages()to offline memory blocks. Allocate the memory blocks usingalloc_contig_pages()first and offline+remove the allcoated memory blocks using a clean MM interface. Offlining of allocated memory is made possible by usingPageOffline()in combination with a memory notifier (similar to virto-mem).Note: In the future, we might want to switch to only removing/readding the page tables of the allocated memory (while still marking it
PageOffline()). However, that might have other implications, and requires work from PPC people (IOW, I won't fiddle with that :) ).[1] https://lkml.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]
[3] https://lkml.kernel.org/r/[email protected]
Quoted 9 lines
David Hildenbrand (3):
powerpc/memtrace: Enforce power of 2 for memory buffer size powerpc/memtrace: Factor out readding memory intomemtrace_free_node()powerpc/memtrace: Don't offline memory blocks viaoffline_pages()arch/powerpc/platforms/powernv/Kconfig | 1 + arch/powerpc/platforms/powernv/memtrace.c | 217 ++++++++++++++-------- 2 files changed, 136 insertions(+), 82 deletions(-)
(CC linuxppc-dev on the cover letter, my fancy sendmail cc-cmd.sh script missed it, sorry)
Signature · 3 lines
Thanks, David / dhildenb