Re: [PATCH V12] mm/debug: Add tests validating architecture page table helpers
From: Anshuman Khandual
Date: Thu Jan 30 2020 - 08:04:35 EST
On 01/28/2020 10:35 PM, Christophe Leroy wrote:
>
>
> Le 28/01/2020 Ã 02:27, Anshuman Khandual a ÃcritÂ:
>> This adds tests which will validate architecture page table helpers and
>> other accessors in their compliance with expected generic MM semantics.
>> This will help various architectures in validating changes to existing
>> page table helpers or addition of new ones.
>>
>> This test covers basic page table entry transformations including but not
>> limited to old, young, dirty, clean, write, write protect etc at various
>> level along with populating intermediate entries with next page table page
>> and validating them.
>>
>> Test page table pages are allocated from system memory with required size
>> and alignments. The mapped pfns at page table levels are derived from a
>> real pfn representing a valid kernel text symbol. This test gets called
>> right after page_alloc_init_late().
>>
>> This gets build and run when CONFIG_DEBUG_VM_PGTABLE is selected along with
>> CONFIG_VM_DEBUG. Architectures willing to subscribe this test also need to
>> select CONFIG_ARCH_HAS_DEBUG_VM_PGTABLE which for now is limited to x86 and
>> arm64. Going forward, other architectures too can enable this after fixing
>> build or runtime problems (if any) with their page table helpers.
>>
>> Folks interested in making sure that a given platform's page table helpers
>> conform to expected generic MM semantics should enable the above config
>> which will just trigger this test during boot. Any non conformity here will
>> be reported as an warning which would need to be fixed. This test will help
>> catch any changes to the agreed upon semantics expected from generic MM and
>> enable platforms to accommodate it thereafter.
>>
>
> [...]
>
>>
>> Tested-by: Christophe Leroy <christophe.leroy@xxxxxx>ÂÂÂÂÂÂÂ #PPC32
>
> Also tested on PPC64 (under QEMU): book3s/64 64k pages, book3s/64 4k pages and book3e/64
Hmm but earlier Michael Ellerman had reported some problems while
running these tests on PPC64, a soft lock up in hash__pte_update()
and a kernel BUG (radix MMU). Are those problems gone away now ?
Details in this thread - https://patchwork.kernel.org/patch/11214603/
>
>> Reviewed-by: Ingo Molnar <mingo@xxxxxxxxxx>
>> Suggested-by: Catalin Marinas <catalin.marinas@xxxxxxx>
>> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
>> Signed-off-by: Christophe Leroy <christophe.leroy@xxxxxx>
>> Signed-off-by: Anshuman Khandual <anshuman.khandual@xxxxxxx>
>> ---
>
> [...]
>
>>
>> diff --git a/Documentation/features/debug/debug-vm-pgtable/arch-support.txt b/Documentation/features/debug/debug-vm-pgtable/arch-support.txt
>> new file mode 100644
>> index 000000000000..f3f8111edbe3
>> --- /dev/null
>> +++ b/Documentation/features/debug/debug-vm-pgtable/arch-support.txt
>> @@ -0,0 +1,35 @@
>> +#
>> +# Feature name:ÂÂÂÂÂÂÂÂÂ debug-vm-pgtable
>> +#ÂÂÂÂÂÂÂÂ Kconfig:ÂÂÂÂÂÂ ARCH_HAS_DEBUG_VM_PGTABLE
>> +#ÂÂÂÂÂÂÂÂ description:ÂÂ arch supports pgtable tests for semantics compliance
>> +#
>> +ÂÂÂ -----------------------
>> +ÂÂÂ |ÂÂÂÂÂÂÂÂ arch |status|
>> +ÂÂÂ -----------------------
>> +ÂÂÂ |ÂÂÂÂÂÂ alpha: | TODO |
>> + | arc: | ok |
>> +ÂÂÂ |ÂÂÂÂÂÂÂÂ arm: | TODO |
>> + | arm64: | ok |
>> +ÂÂÂ |ÂÂÂÂÂÂÂÂ c6x: | TODO |
>> +ÂÂÂ |ÂÂÂÂÂÂÂ csky: | TODO |
>> +ÂÂÂ |ÂÂÂÂÂÂ h8300: | TODO |
>> +ÂÂÂ |ÂÂÂÂ hexagon: | TODO |
>> +ÂÂÂ |ÂÂÂÂÂÂÂ ia64: | TODO |
>> +ÂÂÂ |ÂÂÂÂÂÂÂ m68k: | TODO |
>> +ÂÂÂ |Â microblaze: | TODO |
>> +ÂÂÂ |ÂÂÂÂÂÂÂ mips: | TODO |
>> +ÂÂÂ |ÂÂÂÂÂÂ nds32: | TODO |
>> +ÂÂÂ |ÂÂÂÂÂÂ nios2: | TODO |
>> +ÂÂÂ |ÂÂÂ openrisc: | TODO |
>> +ÂÂÂ |ÂÂÂÂÂ parisc: | TODO |
>> + | powerpc/32: | ok |
>> +ÂÂÂ |Â powerpc/64: | TODO |
>
> You can change the two above lines by
>
> ÂÂÂÂpowerpc: ok
>
>> +ÂÂÂ |ÂÂÂÂÂÂ riscv: | TODO |
>> +ÂÂÂ |ÂÂÂÂÂÂÂ s390: | TODO |
>> +ÂÂÂ |ÂÂÂÂÂÂÂÂÂ sh: | TODO |
>> +ÂÂÂ |ÂÂÂÂÂÂ sparc: | TODO |
>> +ÂÂÂ |ÂÂÂÂÂÂÂÂÂ um: | TODO |
>> +ÂÂÂ |ÂÂ unicore32: | TODO |
>> + | x86: | ok |
>> +ÂÂÂ |ÂÂÂÂÂ xtensa: | TODO |
>> +ÂÂÂ -----------------------
>
>> diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
>> index 1ec34e16ed65..253dcab0bebc 100644
>> --- a/arch/powerpc/Kconfig
>> +++ b/arch/powerpc/Kconfig
>> @@ -120,6 +120,7 @@ config PPC
>> ÂÂÂÂÂ #
>> ÂÂÂÂÂ select ARCH_32BIT_OFF_T if PPC32
>> ÂÂÂÂÂ select ARCH_HAS_DEBUG_VIRTUAL
>> +ÂÂÂ select ARCH_HAS_DEBUG_VM_PGTABLE if PPC32
>
> Remove the 'if PPC32' as we now know it also work on PPC64.
But in case there is a subset of PPC64 which still does not work
(problem reported earlier) with the test, will have to adjust the
config accordingly.
>
>> ÂÂÂÂÂ select ARCH_HAS_DEVMEM_IS_ALLOWED
>> ÂÂÂÂÂ select ARCH_HAS_ELF_RANDOMIZE
>> ÂÂÂÂÂ select ARCH_HAS_FORTIFY_SOURCE
>
>> diff --git a/arch/x86/include/asm/pgtable_64.h b/arch/x86/include/asm/pgtable_64.h
>> index 0b6c4042942a..fb0e76d254b3 100644
>> --- a/arch/x86/include/asm/pgtable_64.h
>> +++ b/arch/x86/include/asm/pgtable_64.h
>> @@ -53,6 +53,12 @@ static inline void sync_initial_page_table(void) { }
>> Â Â struct mm_struct;
>> Â +#define mm_p4d_folded mm_p4d_folded
>> +static inline bool mm_p4d_folded(struct mm_struct *mm)
>> +{
>> +ÂÂÂ return !pgtable_l5_enabled();
>> +}
>> +
>
> For me this should be part of another patch, it is not directly linked to the tests.
We did discuss about this earlier and Kirril mentioned its not worth
a separate patch.
https://lore.kernel.org/linux-arm-kernel/20190913091305.rkds4f3fqv3yjhjy@box/
>
>> Â void set_pte_vaddr_p4d(p4d_t *p4d_page, unsigned long vaddr, pte_t new_pte);
>> Â void set_pte_vaddr_pud(pud_t *pud_page, unsigned long vaddr, pte_t new_pte);
>> Â diff --git a/include/asm-generic/pgtable.h b/include/asm-generic/pgtable.h
>> index 798ea36a0549..e0b04787e789 100644
>> --- a/include/asm-generic/pgtable.h
>> +++ b/include/asm-generic/pgtable.h
>> @@ -1208,6 +1208,12 @@ static inline bool arch_has_pfn_modify_check(void)
>> Â # define PAGE_KERNEL_EXEC PAGE_KERNEL
>> Â #endif
>> Â +#ifdef CONFIG_DEBUG_VM_PGTABLE
>
> Not sure it is a good idea to put that in include/asm-generic/pgtable.h
Logically that is the right place, as it is related to page table but
not something platform related.
>
> By doing this you are forcing a rebuild of almost all files, whereas only init/main.o and mm/debug_vm_pgtable.o should be rebuilt when activating this config option.
I agreed but whats the alternative ? We could move these into init/main.c
to make things simpler but will that be a right place, given its related
to generic page table.
>
>> +extern void debug_vm_pgtable(void);
>
> Please don't use the 'extern' keyword, it is useless and not to be used for functions declaration.
Really ? But, there are tons of examples doing the same thing both in
generic and platform code as well.
>
>> +#else
>> +static inline void debug_vm_pgtable(void) { }
>> +#endif
>> +
>> Â #endif /* !__ASSEMBLY__ */
>> Â Â #ifndef io_remap_pfn_range
>> diff --git a/init/main.c b/init/main.c
>> index da1bc0b60a7d..5e59e6ac0780 100644
>> --- a/init/main.c
>> +++ b/init/main.c
>> @@ -1197,6 +1197,7 @@ static noinline void __init kernel_init_freeable(void)
>> ÂÂÂÂÂ sched_init_smp();
>> Â ÂÂÂÂÂ page_alloc_init_late();
>> +ÂÂÂ debug_vm_pgtable();
>
> Wouldn't it be better to call debug_vm_pgtable() in kernel_init() between the call to async_synchronise_full() and ftrace_free_init_mem() ?
IIRC, proposed location is the earliest we could call debug_vm_pgtable().
Is there any particular benefit or reason to move it into kernel_init() ?
>
>> ÂÂÂÂÂ /* Initialize page ext after all struct pages are initialized. */
>> ÂÂÂÂÂ page_ext_init();
>> Â diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
>> index 5ffe144c9794..7cceae923c05 100644
>> --- a/lib/Kconfig.debug
>> +++ b/lib/Kconfig.debug
>> @@ -653,6 +653,12 @@ config SCHED_STACK_END_CHECK
>> ÂÂÂÂÂÂÂ data corruption or a sporadic crash at a later stage once the region
>> ÂÂÂÂÂÂÂ is examined. The runtime overhead introduced is minimal.
>> Â +config ARCH_HAS_DEBUG_VM_PGTABLE
>> +ÂÂÂ bool
>> +ÂÂÂ help
>> +ÂÂÂÂÂ An architecture should select this when it can successfully
>> +ÂÂÂÂÂ build and run DEBUG_VM_PGTABLE.
>> +
>> Â config DEBUG_VM
>> ÂÂÂÂÂ bool "Debug VM"
>> ÂÂÂÂÂ depends on DEBUG_KERNEL
>> @@ -688,6 +694,22 @@ config DEBUG_VM_PGFLAGS
>> Â ÂÂÂÂÂÂÂ If unsure, say N.
>> Â +config DEBUG_VM_PGTABLE
>> +ÂÂÂ bool "Debug arch page table for semantics compliance"
>> +ÂÂÂ depends on MMU
>> +ÂÂÂ depends on DEBUG_VM
>
> Does it really need to depend on DEBUG_VM ?
No. It seemed better to package this test along with DEBUG_VM (although I
dont remember the conversation around it) and hence this dependency.
> I think we could make it standalone and 'default y if DEBUG_VM' instead.
Which will yield the same result like before but in a different way. But
yes, this test could go about either way but unless there is a good enough
reason why change the current one.
>
>> +ÂÂÂ depends on ARCH_HAS_DEBUG_VM_PGTABLE
>> +ÂÂÂ default y
>> +ÂÂÂ help
>> +ÂÂÂÂÂ This option provides a debug method which can be used to test
>> +ÂÂÂÂÂ architecture page table helper functions on various platforms in
>> +ÂÂÂÂÂ verifying if they comply with expected generic MM semantics. This
>> +ÂÂÂÂÂ will help architecture code in making sure that any changes or
>> +ÂÂÂÂÂ new additions of these helpers still conform to expected
>> +ÂÂÂÂÂ semantics of the generic MM.
>> +
>> +ÂÂÂÂÂ If unsure, say N.
>> +
>
> Does it make sense to make it 'default y' and say 'If unsure, say N' ?
No it does. Not when it defaults 'y' unconditionally. Will drop the last
sentence "If unsure, say N". Nice catch, thank you.
>
>> Â config ARCH_HAS_DEBUG_VIRTUAL
>> ÂÂÂÂÂ bool
>> Â
>
> Christophe
>