Fix bugs for realm guest plus BBML2_NOABORT
Subsystems (2)
v2 cover Fix bugs for realm guest plus BBML2_NOABORT
Hi All,
This fixes a couple of bugs in the "large block mappings for linear map when we have BBML2_NOABORT" feature when used in conjunction with a CCA realm guest. While investigating I found and fixed some more general issues too. See commit logs for full explanations.
Applies on top of v7.0-rc4.
Changes since v1 [1]
====================
Patch 1:
- Moved page_alloc_available declaration to asm/mmu.c (per Kevin)
- Added PTE_PRESENT_VALID_KERNEL macro to hide VALID|NG confusion (per Kevin)
- Improved logic in split_kernel_leaf_mapping() to avoid warning for DEBUG_PAGEALLOC (per Sashiko)
Patch 2:
- Fixed transitional pgtables to handle present-invalid large leaves (per Sashiko)
- Hardened split_pXd() for present-invalid leaves (per Sashiko)
Patch 3:
- Converted pXd_leaf() to function to avoid multi-eval of READ_ONCE() (per Sashiko)
[1] https://lore.kernel.org/all/[email protected]/
Thanks,
Ryan
Ryan Roberts (3):
arm64: mm: Fix rodata=full block mapping support for realm guests
arm64: mm: Handle invalid large leaf mappings correctly
arm64: mm: Remove pmd_sect() and pud_sect()
arch/arm64/include/asm/mmu.h | 2 + arch/arm64/include/asm/pgtable-prot.h | 2 + arch/arm64/include/asm/pgtable.h | 28 +++++++---- arch/arm64/mm/init.c | 9 +++- arch/arm64/mm/mmu.c | 67 ++++++++++++++++++--------- arch/arm64/mm/pageattr.c | 50 +++++++++++--------- arch/arm64/mm/trans_pgd.c | 42 +++-------------- 7 files changed, 111 insertions(+), 89 deletions(-)
--
2.43.0
PATCH v2 1/3 arm64: mm: Fix rodata=full block mapping support for realm guests
Commit a166563e7ec37 ("arm64: mm: support large block mapping when rodata=full") enabled the linear map to be mapped by block/cont while still allowing granular permission changes on BBML2_NOABORT systems by lazily splitting the live mappings. This mechanism was intended to be usable by realm guests since they need to dynamically share dma buffers with the host by "decrypting" them - which for Arm CCA, means marking them as shared in the page tables.
However, it turns out that the mechanism was failing for realm guests because realms need to share their dma buffers (via __set_memory_enc_dec()) much earlier during boot than split_kernel_leaf_mapping() was able to handle. The report linked below showed that GIC's ITS was one such user. But during the investigation I found other callsites that could not meet the split_kernel_leaf_mapping() constraints.
The problem is that we block map the linear map based on the boot CPU supporting BBML2_NOABORT, then check that all the other CPUs support it too when finalizing the caps. If they don't, then we stop_machine() and split to ptes. For safety, split_kernel_leaf_mapping() previously wouldn't permit splitting until after the caps were finalized. That ensured that if any secondary cpus were running that didn't support BBML2_NOABORT, we wouldn't risk breaking them.
I've fix this problem by reducing the black-out window where we refuse to split; there are now 2 windows. The first is from T0 until the page allocator is inititialized. Splitting allocates memory for the page allocator so it must be in use. The second covers the period between starting to online the secondary cpus until the system caps are finalized (this is a very small window).
All of the problematic callers are calling __set_memory_enc_dec() before the secondary cpus come online, so this solves the problem. However, one of these callers, swiotlb_update_mem_attributes(), was trying to split before the page allocator was initialized. So I have moved this call from arch_mm_preinit() to mem_init(), which solves the ordering issue.
I've added warnings and return an error if any attempt is made to split in the black-out windows.
Note there are other issues which prevent booting all the way to user space, which will be fixed in subsequent patches.
--- arch/arm64/include/asm/mmu.h | 2 ++ arch/arm64/mm/init.c | 9 +++++++- arch/arm64/mm/mmu.c | 45 +++++++++++++++++++++++++----------- 3 files changed, 42 insertions(+), 14 deletions(-)
arch/arm64/include/asm/mmu.h +2 -0
@@ -112,5 +112,7 @@ void kpti_install_ng_mappings(void);static inline void kpti_install_ng_mappings (void ) {}#endif +extern bool page_alloc_available; +#endif /* !__ASSEMBLER__ */ #endif
arch/arm64/mm/init.c +8 -1
@@ -350,7 +350,6 @@ void __init arch_mm_preinit(void) }swiotlb_init (swiotlb, flags); -swiotlb_update_mem_attributes ();/* * Check boundaries twice: Some fundamental inconsistencies can be
@@ -377,6 +376,14 @@ void __init arch_mm_preinit(void) } } +bool page_alloc_available__ro_after_init ; + +void __init mem_init (void ) +{ + page_alloc_available = true; +swiotlb_update_mem_attributes (); +} +void free_initmem (void ) {void *lm_init_begin =lm_alias (__init_begin);
arch/arm64/mm/mmu.c +32 -13
70 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 40 lines · Ryan Roberts
Commit
a166563e7ec37("arm64: mm: support large block mapping when rodata=full") enabled the linear map to be mapped by block/cont while still allowing granular permission changes onBBML2_NOABORTsystems by lazily splitting the live mappings. This mechanism was intended to be usable by realm guests since they need to dynamically share dma buffers with the host by "decrypting" them - which for Arm CCA, means marking them as shared in the page tables.However, it turns out that the mechanism was failing for realm guests because realms need to share their dma buffers (via
__set_memory_enc_dec()) much earlier during boot thansplit_kernel_leaf_mapping()was able to handle. The report linked below showed that GIC's ITS was one such user. But during the investigation I found other callsites that could not meet thesplit_kernel_leaf_mapping()constraints.The problem is that we block map the linear map based on the boot CPU supporting
BBML2_NOABORT, then check that all the other CPUs support it too when finalizing the caps. If they don't, then westop_machine()and split to ptes. For safety,split_kernel_leaf_mapping()previously wouldn't permit splitting until after the caps were finalized. That ensured that if any secondary cpus were running that didn't supportBBML2_NOABORT, we wouldn't risk breaking them.I've fix this problem by reducing the black-out window where we refuse to split; there are now 2 windows. The first is from T0 until the page allocator is inititialized. Splitting allocates memory for the page allocator so it must be in use. The second covers the period between starting to online the secondary cpus until the system caps are finalized (this is a very small window).
All of the problematic callers are calling
__set_memory_enc_dec()before the secondary cpus come online, so this solves the problem. However, one of these callers,swiotlb_update_mem_attributes(), was trying to split before the page allocator was initialized. So I have moved this call fromarch_mm_preinit()tomem_init(), which solves the ordering issue.I've added warnings and return an error if any attempt is made to split in the black-out windows.
Quoted 8 lines
Note there are other issues which prevent booting all the way to user space, which will be fixed in subsequent patches.
Reported-by Jinjiang Tu <[email protected]>Fixes a166563e7ec37 ("arm64: mm: support large block mapping when rodata=full")Reviewed-by Kevin Brodsky <[email protected]>
I have tested with a hacked cpufeature code to enable BBML2_NOABORT for FVP MIDRs.
Suzuki
Quoted 122 lines
Signed-off-by Ryan Roberts <[email protected]>--- arch/arm64/include/asm/mmu.h | 2 ++ arch/arm64/mm/init.c | 9 +++++++- arch/arm64/mm/mmu.c | 45 +++++++++++++++++++++++++----------- 3 files changed, 42 insertions(+), 14 deletions(-)arch/arm64/include/asm/mmu.h +2
-0@@ -112,5 +112,7 @@ void kpti_install_ng_mappings(void);static inline void kpti_install_ng_mappings (void ) {}#endif +extern bool page_alloc_available; +#endif /* !__ASSEMBLER__ */ #endif arch/arm64/mm/init.c +8
-1@@ -350,7 +350,6 @@ void __init arch_mm_preinit(void) }swiotlb_init (swiotlb, flags); -swiotlb_update_mem_attributes ();/* * Check boundaries twice: Some fundamental inconsistencies can be @@ -377,6 +376,14 @@ void __init arch_mm_preinit(void) } } +bool page_alloc_available__ro_after_init ; + +void __init mem_init (void ) +{ + page_alloc_available = true; +swiotlb_update_mem_attributes (); +} +void free_initmem (void ) {void *lm_init_begin =lm_alias (__init_begin);arch/arm64/mm/mmu.c +32
-13@@ -768,30 +768,51 @@ static inline bool force_pte_mapping(void) }static DEFINE_MUTEX (pgtable_split_lock); +static bool linear_map_requires_bbml2;int split_kernel_leaf_mapping (unsigned long start,unsigned long end) {int ret; -/* -* !BBML2_NOABORT systems should not be trying to change permissions on -* anything that is not pte-mapped in the first place. Just return early -* and let the permission change code raise a warning if not already -* pte-mapped. -*/ -if (!system_supports_bbml2_noabort ()) -return 0 ; -/* * If the region is within a pte-mapped area, there is no need to try to * split. Additionally, CONFIG_DEBUG_PAGEALLOC and CONFIG_KFENCE may * change permissions from atomic context so for those cases (which are * always pte-mapped), we must not go any further because taking the -* mutex below may sleep. +* mutex below may sleep. Do not call force_pte_mapping() here because +* it could return a confusing result if called from a secondary cpu +* prior to finalizing caps. Instead, linear_map_requires_bbml2 gives us +* what we need. */ -if (force_pte_mapping () ||is_kfence_address ((void *)start)) +if (!linear_map_requires_bbml2 ||is_kfence_address ((void *)start))return 0 ; +if (!system_supports_bbml2_noabort ()) { +/* +* !BBML2_NOABORT systems should not be trying to change +* permissions on anything that is not pte-mapped in the first +* place. Just return early and let the permission change code +* raise a warning if not already pte-mapped. +*/ +if (system_capabilities_finalized ()) +return 0 ; + +/* +* Boot-time: split_kernel_leaf_mapping_locked() allocates from +* page allocator. Can't split until it's available. +*/ +if (WARN_ON (!page_alloc_available)) +return -EBUSY ; + +/* +* Boot-time: Started secondary cpus but don't know if they +* support BBML2_NOABORT yet. Can't allow splitting in this +* window in case they don't. +*/ +if (WARN_ON (num_online_cpus () >1 )) +return -EBUSY ; + } +/* * Ensure start and end are at least page-aligned since this is the * finest granularity we can split to. @@ -891,8 +912,6 @@ static int range_split_to_ptes(unsigned long start, unsigned long end, gfp_t gfpreturn ret; } -static bool linear_map_requires_bbml2__initdata ; -u32 idmap_kpti_bbml2_flag;static void __init init_idmap_kpti_bbml2_flag (void )
Catalin Marinas
maintainerRe: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
In reply to Ryan RobertsQuoted 43 lines · Ryan Roberts
int
split_kernel_leaf_mapping(unsigned long start, unsigned long end){arch/arm64/mm/mmu.c +20
-11int ret; -/* -* !BBML2_NOABORT systems should not be trying to change permissions on -* anything that is not pte-mapped in the first place. Just return early -* and let the permission change code raise a warning if not already -* pte-mapped. -*/ -if (!system_supports_bbml2_noabort ()) -return 0 ; -/* * If the region is within a pte-mapped area, there is no need to try to * split. Additionally, CONFIG_DEBUG_PAGEALLOC and CONFIG_KFENCE may * change permissions from atomic context so for those cases (which are * always pte-mapped), we must not go any further because taking the -* mutex below may sleep. +* mutex below may sleep. Do not call force_pte_mapping() here because +* it could return a confusing result if called from a secondary cpu +* prior to finalizing caps. Instead, linear_map_requires_bbml2 gives us +* what we need. */ -if (force_pte_mapping () ||is_kfence_address ((void *)start)) +if (!linear_map_requires_bbml2 ||is_kfence_address ((void *)start))return 0 ; +if (!system_supports_bbml2_noabort ()) { +/* +* !BBML2_NOABORT systems should not be trying to change +* permissions on anything that is not pte-mapped in the first +* place. Just return early and let the permission change code +* raise a warning if not already pte-mapped. +*/ +if (system_capabilities_finalized ()) +return 0 ; + +/* +* Boot-time: split_kernel_leaf_mapping_locked() allocates from +* page allocator. Can't split until it's available. +*/ +if (WARN_ON (!page_alloc_available))
arch/arm64/mm/mmu.c static inline bool force_pte_mapping(void) · line 814
Quoted 10 lines
arch/arm64/mm/mmu.c +10
-0+return -EBUSY ; + +/* +* Boot-time: Started secondary cpus but don't know if they +* support BBML2_NOABORT yet. Can't allow splitting in this +* window in case they don't. +*/ +if (WARN_ON (num_online_cpus () >1 )) +return -EBUSY ; + }
I think sashiko is over cautions here
(https://sashiko.dev/#/patchset/[email protected])
but it has a somewhat valid point from the perspective of num_online_cpus() semantics. We have have num_online_cpus() == 1 while having a secondary CPU just booted and with its MMU enabled. I don't think we can have any asynchronous tasks running at that point to trigger a spit though. Even async_init() is called after smp_init().
An option may be to attempt cpus_read_trylock() as this lock is taken by _cpu_up(). If it fails, return -EBUSY, otherwise check num_online_cpus() and unlock (and return -EBUSY if secondaries already started).
Another thing I couldn't get my head around - IIUC is_realm_world() won't return true for map_mem() yet (if in a realm). Can we have realms on hardware that does not support BBML2_NOABORT? We may not have configuration with rodata_full set (it should be complementary to realm support).
I'll add the patches to for-next/core to give them a bit of time in -next but let's see next week if we ignore this (with an updated comment) or we try to avoid the issue altogether.
Signature · 1 line
Catalin
Catalin Marinas
maintainerRe: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 5 lines · Catalin Marinas
Another thing I couldn't get my head around - IIUC
is_realm_world()won't return true formap_mem()yet (if in a realm). Can we have realms on hardware that does not supportBBML2_NOABORT? We may not have configuration withrodata_fullset (it should be complementary to realm support).
With rodata_full==false, can_set_direct_map() returns false initially but after arm64_rsi_init() it starts returning true if is_realm_world(). The side-effect is that map_mem() goes for block mappings and linear_map_requires_bbml2 set to false. Later on, linear_map_maybe_split_to_ptes() will skip the splitting.
Unless I'm missing something, is_realm_world() calls in force_pte_mapping() and can_set_direct_map() are useless. I'd remove them and either require BBML2_NOABORT with CCA or get the user to force rodata_full when running in realms. Or move arm64_rsi_init() even earlier?
Signature · 1 line
Catalin
Re: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 13 lines · Catalin Marinas
Quoted 5 lines · Catalin Marinas
Another thing I couldn't get my head around - IIUC
is_realm_world()won't return true formap_mem()yet (if in a realm). Can we have realms on hardware that does not supportBBML2_NOABORT? We may not have configuration withrodata_fullset (it should be complementary to realm support).With rodata_full==false,
can_set_direct_map()returns false initially but afterarm64_rsi_init()it starts returning true ifis_realm_world(). The side-effect is thatmap_mem()goes for block mappings andlinear_map_requires_bbml2set to false. Later on,linear_map_maybe_split_to_ptes()will skip the splitting.
Quoted 5 lines
Unless I'm missing something,
is_realm_world()calls inforce_pte_mapping()andcan_set_direct_map()are useless. I'd remove them and either requireBBML2_NOABORTwith CCA or get the user to forcerodata_fullwhen running in realms. Or movearm64_rsi_init()even earlier?
I'd need Suzuki to comment on this. As I said in the other mail, I was treating this like a pre-existing bug. But I guess linear_map_requires_bbml2 ending up wrong is a problem here. I'm not sure it's quite as simple as requiring BBML2_NOABORT with CCA as we still need can_set_direct_map() to return true if we are in a realm.
I don't know what it would take to run arm64_rsi_init() even earlier, but that would be the best option from my point of view.
Thanks,
Ryan
Catalin Marinas
maintainerRe: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 20 lines · Ryan Roberts
Quoted 13 lines · Catalin Marinas
Quoted 5 lines · Catalin Marinas
Another thing I couldn't get my head around - IIUC
is_realm_world()won't return true formap_mem()yet (if in a realm). Can we have realms on hardware that does not supportBBML2_NOABORT? We may not have configuration withrodata_fullset (it should be complementary to realm support).With rodata_full==false,
can_set_direct_map()returns false initially but afterarm64_rsi_init()it starts returning true ifis_realm_world(). The side-effect is thatmap_mem()goes for block mappings andlinear_map_requires_bbml2set to false. Later on,linear_map_maybe_split_to_ptes()will skip the splitting.Quoted 5 lines
Unless I'm missing something,
is_realm_world()calls inforce_pte_mapping()andcan_set_direct_map()are useless. I'd remove them and either requireBBML2_NOABORTwith CCA or get the user to forcerodata_fullwhen running in realms. Or movearm64_rsi_init()even earlier?
Quoted 5 lines
I'd need Suzuki to comment on this. As I said in the other mail, I was treating this like a pre-existing bug. But I guess
linear_map_requires_bbml2ending up wrong is a problem here. I'm not sure it's quite as simple as requiringBBML2_NOABORTwith CCA as we still needcan_set_direct_map()to return true if we are in a realm.
can_set_direct_map() == true is not a property of the realm but rather a requirement. In the absence of BBML2_NOABORT, I guess the test was added under the assumption that force_pte_mapping() also returns true if is_realm_world(). We might as well add a variable or static label to track whether can_set_direct_map() is possible and avoid tests that duplicate force_pte_mapping().
This won't solve the is_realm_world() changing polarity during boot but at least we know it won't suddenly make can_set_direct_map() return true when it shouldn't.
Signature · 1 line
Catalin
Re: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 21 lines · Catalin Marinas
Quoted 20 lines · Ryan Roberts
Quoted 18 lines · Catalin Marinas
Quoted 5 lines · Catalin Marinas
Another thing I couldn't get my head around - IIUC
is_realm_world()won't return true formap_mem()yet (if in a realm). Can we have realms on hardware that does not supportBBML2_NOABORT? We may not have configuration withrodata_fullset (it should be complementary to realm support).With rodata_full==false,
can_set_direct_map()returns false initially but afterarm64_rsi_init()it starts returning true ifis_realm_world(). The side-effect is thatmap_mem()goes for block mappings andlinear_map_requires_bbml2set to false. Later on,linear_map_maybe_split_to_ptes()will skip the splitting.Unless I'm missing something,
is_realm_world()calls inforce_pte_mapping()andcan_set_direct_map()are useless. I'd remove them and either requireBBML2_NOABORTwith CCA or get the user to forcerodata_fullwhen running in realms. Or movearm64_rsi_init()even earlier?
Quoted 8 lines
Quoted 5 lines
I'd need Suzuki to comment on this. As I said in the other mail, I was treating this like a pre-existing bug. But I guess
linear_map_requires_bbml2ending up wrong is a problem here. I'm not sure it's quite as simple as requiringBBML2_NOABORTwith CCA as we still needcan_set_direct_map()to return true if we are in a realm.
can_set_direct_map()== true is not a property of the realm but rather a requirement.
Yes indeed. It would be better to call it might_set_direct_map() or something like that...
Quoted 5 lines
In the absence of
BBML2_NOABORT, I guess the test was added under the assumption thatforce_pte_mapping()also returns true ifis_realm_world(). We might as well add a variable or static label to track whethercan_set_direct_map()is possible and avoid tests that duplicateforce_pte_mapping().
I'm not sure I follow. We have linear_map_requires_bbml2 which is inteded to track this shape of thing; if we have forced pte mapping then the value of can_set_direct_map() is irrelevant - we will never need to split because we are already pte-mapped. But if can_set_direct_map() initially returns false because is_realm_world() incorrectly returns false in the early boot environment, then linear_map_requires_bbml2 will be set to false, and we will incorrectly short-circuit splitting any block mappings in split_kernel_leaf_mapping().
I think we are agreed on the problem. But I don't understand how tracking can_set_direct_map() in a cached variable helps with that.
Quoted 4 lines
This won't solve the
is_realm_world()changing polarity during boot but at least we know it won't suddenly makecan_set_direct_map()return true when it shouldn't.
But is_real_world() _should_ make can_set_direct_map() return true, shouldn't it? If we are in realm-world, we need to be able to flip the NS_SHARED bit on parts of the linear map. So if we are in realm-world, we _might_ need to update he direct map and that's what can_set_direct_map() is supposed to tell us.
Catalin Marinas
maintainerRe: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 28 lines · Ryan Roberts
Quoted 27 lines · Catalin Marinas
Quoted 25 lines · Ryan Roberts
Quoted 13 lines · Catalin Marinas
Quoted 5 lines · Catalin Marinas
Another thing I couldn't get my head around - IIUC
is_realm_world()won't return true formap_mem()yet (if in a realm). Can we have realms on hardware that does not supportBBML2_NOABORT? We may not have configuration withrodata_fullset (it should be complementary to realm support).With rodata_full==false,
can_set_direct_map()returns false initially but afterarm64_rsi_init()it starts returning true ifis_realm_world(). The side-effect is thatmap_mem()goes for block mappings andlinear_map_requires_bbml2set to false. Later on,linear_map_maybe_split_to_ptes()will skip the splitting.Quoted 5 lines
Unless I'm missing something,
is_realm_world()calls inforce_pte_mapping()andcan_set_direct_map()are useless. I'd remove them and either requireBBML2_NOABORTwith CCA or get the user to forcerodata_fullwhen running in realms. Or movearm64_rsi_init()even earlier?I'd need Suzuki to comment on this. As I said in the other mail, I was treating this like a pre-existing bug. But I guess
linear_map_requires_bbml2ending up wrong is a problem here. I'm not sure it's quite as simple as requiringBBML2_NOABORTwith CCA as we still needcan_set_direct_map()to return true if we are in a realm.
Quoted 5 lines
Quoted 2 lines
can_set_direct_map()== true is not a property of the realm but rather a requirement.Yes indeed. It would be better to call it
might_set_direct_map()or something like that...
The way it is used means "is allowed to set the direct map". I guess "may set..." works as well. My reading of "might" is more like in might_sleep(), more of hint than a permission check.
If you only look at the linear_map_requires_bbml2 setting in map_mem(), yes, something like might_set_direct_map() makes sense but that's not how this function is used in the rest of the kernel (to reject the direct map change if not supported).
Quoted 8 lines
Quoted 5 lines
In the absence of
BBML2_NOABORT, I guess the test was added under the assumption thatforce_pte_mapping()also returns true ifis_realm_world(). We might as well add a variable or static label to track whethercan_set_direct_map()is possible and avoid tests that duplicateforce_pte_mapping().I'm not sure I follow. We have
linear_map_requires_bbml2which is inteded to track this shape of thing;
As the name implies, linear_map_requires_bbml2 tracks only this - BBML2_NOABORT is required because the linear map uses large blocks. Prior to your patches, that's only used as far as linear_map_maybe_split_to_ptes() and if splitting took place, this variable is no longer relevant (should be turned to false but since it's not used, it doesn't matter).
With your patches, its use was extended to runtime and I think it remains true even if linear_map_maybe_split_to_ptes() changed the block mappings. Do we need this:
arch/arm64/mm/mmu.c +1 -0
@@ -988,6 +988,7 @@ void __init linear_map_maybe_split_to_ptes(void)if (linear_map_requires_bbml2 && !system_supports_bbml2_noabort ()) {init_idmap_kpti_bbml2_flag ();stop_machine (linear_map_split_to_ptes,NULL , cpu_online_mask); + linear_map_requires_bbml2 = false; } }
Quoted 3 lines
if we have forced pte mapping then the value of
can_set_direct_map()is irrelevant - we will never need to split because we are already pte-mapped.
can_set_direct_map() is used in other places, so its value is relevant, e.g. sys_memfd_secret() is rejected if this function returns false.
Quoted 7 lines
But if
can_set_direct_map()initially returns false becauseis_realm_world()incorrectly returns false in the early boot environment, thenlinear_map_requires_bbml2will be set to false, and we will incorrectly short-circuit splitting any block mappings insplit_kernel_leaf_mapping().I think we are agreed on the problem. But I don't understand how tracking
can_set_direct_map()in a cached variable helps with that.
It's not about the map_mem() decision and linear_map_requires_bbml2 setting but rather its other uses like sys_memfd_secret().
Quoted 6 lines
Quoted 3 lines
This won't solve the
is_realm_world()changing polarity during boot but at least we know it won't suddenly makecan_set_direct_map()return true when it shouldn't.But
is_real_world()_should_ makecan_set_direct_map()return true, shouldn't it?
Yes but not directly. If is_realm_world() is true, we either have (linear_map_requires_bbml2 && system_supports_bbml2_noabort()) or linear_map_requires_bbml2 is false and we have pte mappings. Adding is_realm_world() to can_set_direct_map() does not imply any of these. It's just a hope that something before actually ensured the conditions are true.
It might be better if we rename the current function to might_set_direct_map() and introduce a new can_set_direct_map() that actually tells the truth if all the conditions are met. I suggested a variable or static label but checking some conditions related to the actual linear map work as well, just not is_realm_world() directly.
Signature · 1 line
Catalin
Re: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 32 lines · Catalin Marinas
Quoted 31 lines · Ryan Roberts
Quoted 29 lines · Catalin Marinas
Quoted 20 lines · Ryan Roberts
Quoted 13 lines · Catalin Marinas
Quoted 5 lines · Catalin Marinas
Another thing I couldn't get my head around - IIUC
is_realm_world()won't return true formap_mem()yet (if in a realm). Can we have realms on hardware that does not supportBBML2_NOABORT? We may not have configuration withrodata_fullset (it should be complementary to realm support).With rodata_full==false,
can_set_direct_map()returns false initially but afterarm64_rsi_init()it starts returning true ifis_realm_world(). The side-effect is thatmap_mem()goes for block mappings andlinear_map_requires_bbml2set to false. Later on,linear_map_maybe_split_to_ptes()will skip the splitting.Quoted 5 lines
Unless I'm missing something,
is_realm_world()calls inforce_pte_mapping()andcan_set_direct_map()are useless. I'd remove them and either requireBBML2_NOABORTwith CCA or get the user to forcerodata_fullwhen running in realms. Or movearm64_rsi_init()even earlier?Quoted 5 lines
I'd need Suzuki to comment on this. As I said in the other mail, I was treating this like a pre-existing bug. But I guess
linear_map_requires_bbml2ending up wrong is a problem here. I'm not sure it's quite as simple as requiringBBML2_NOABORTwith CCA as we still needcan_set_direct_map()to return true if we are in a realm.
can_set_direct_map()== true is not a property of the realm but rather a requirement.
Quoted 6 lines
Quoted 2 lines
Yes indeed. It would be better to call it
might_set_direct_map()or something like that...The way it is used means "is allowed to set the direct map". I guess "may set..." works as well. My reading of "might" is more like in
might_sleep(), more of hint than a permission check.
OK, I read it as "might" as in a hint that we might want to change the direct map permissions.
Quoted 5 lines
If you only look at the
linear_map_requires_bbml2setting inmap_mem(), yes, something likemight_set_direct_map()makes sense but that's not how this function is used in the rest of the kernel (to reject the direct map change if not supported).
ACK.
Quoted 17 lines
Quoted 9 lines
Quoted 6 lines
In the absence of
BBML2_NOABORT, I guess the test was added under the assumption thatforce_pte_mapping()also returns true ifis_realm_world(). We might as well add a variable or static label to track whethercan_set_direct_map()is possible and avoid tests that duplicateforce_pte_mapping().I'm not sure I follow. We have
linear_map_requires_bbml2which is inteded to track this shape of thing;As the name implies,
linear_map_requires_bbml2tracks only this -BBML2_NOABORTis required because the linear map uses large blocks. Prior to your patches, that's only used as far aslinear_map_maybe_split_to_ptes()and if splitting took place, this variable is no longer relevant (should be turned to false but since it's not used, it doesn't matter).
Quoted 3 lines
With your patches, its use was extended to runtime and I think it remains true even if
linear_map_maybe_split_to_ptes()changed the block mappings. Do we need this:
I'll admit it is ugly but it's not a bug; the system capabilitites are finalized by the time we call linear_map_maybe_split_to_ptes().
The "if (!linear_map_requires_bbml2 || is_kfence_address((void *)start))" check in split_kernel_leaf_mapping() would ideally be "if (!force_pte_mapping() || is_kfence_address((void *)start))", but it is not safe to call force_pte_mapping() from a secondary cpu prior to finalizing the system caps. I'm reusing the flag that I already had available to work around that.
Quoted 46 lines
arch/arm64/mm/mmu.c +1
-0@@ -988,6 +988,7 @@ void __init linear_map_maybe_split_to_ptes(void)if (linear_map_requires_bbml2 && !system_supports_bbml2_noabort ()) {init_idmap_kpti_bbml2_flag ();stop_machine (linear_map_split_to_ptes,NULL , cpu_online_mask); + linear_map_requires_bbml2 = false; } }Quoted 4 lines
if we have forced pte mapping then the value of
can_set_direct_map()is irrelevant - we will never need to split because we are already pte-mapped.
can_set_direct_map()is used in other places, so its value is relevant, e.g.sys_memfd_secret()is rejected if this function returns false.Quoted 7 lines
But if
can_set_direct_map()initially returns false becauseis_realm_world()incorrectly returns false in the early boot environment, thenlinear_map_requires_bbml2will be set to false, and we will incorrectly short-circuit splitting any block mappings insplit_kernel_leaf_mapping().I think we are agreed on the problem. But I don't understand how tracking
can_set_direct_map()in a cached variable helps with that.It's not about the
map_mem()decision andlinear_map_requires_bbml2setting but rather its other uses likesys_memfd_secret().Quoted 6 lines
Quoted 3 lines
This won't solve the
is_realm_world()changing polarity during boot but at least we know it won't suddenly makecan_set_direct_map()return true when it shouldn't.But
is_real_world()_should_ makecan_set_direct_map()return true, shouldn't it?Yes but not directly. If
is_realm_world()is true, we either have (linear_map_requires_bbml2&&system_supports_bbml2_noabort()) orlinear_map_requires_bbml2is false and we have pte mappings. Addingis_realm_world()tocan_set_direct_map()does not imply any of these. It's just a hope that something before actually ensured the conditions are true.
Quoted 5 lines
It might be better if we rename the current function to
might_set_direct_map()and introduce a newcan_set_direct_map()that actually tells the truth if all the conditions are met. I suggested a variable or static label but checking some conditions related to the actual linear map work as well, just notis_realm_world()directly.
I'm not sure I see the distinction between "might" and "can" with your definition. But regardless, I think we are talking about the pre-existing is_real_world() bug, so I'm not personally planning to do anything further here unless you shout.
Thanks,
Ryan
Catalin Marinas
maintainerRe: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 12 lines · Ryan Roberts
Quoted 10 lines · Catalin Marinas
As the name implies,
linear_map_requires_bbml2tracks only this -BBML2_NOABORTis required because the linear map uses large blocks. Prior to your patches, that's only used as far aslinear_map_maybe_split_to_ptes()and if splitting took place, this variable is no longer relevant (should be turned to false but since it's not used, it doesn't matter).With your patches, its use was extended to runtime and I think it remains true even if
linear_map_maybe_split_to_ptes()changed the block mappings. Do we need this:
Quoted 8 lines
I'll admit it is ugly but it's not a bug; the system capabilitites are finalized by the time we call
linear_map_maybe_split_to_ptes().The "if (!linear_map_requires_bbml2 ||
is_kfence_address((void *)start))" check insplit_kernel_leaf_mapping()would ideally be "if (!force_pte_mapping() ||is_kfence_address((void *)start))", but it is not safe to callforce_pte_mapping()from a secondary cpu prior to finalizing the system caps. I'm reusing the flag that I already had available to work around that.
The confusing part is that the flag may be false incorrectly due to the is_realm_world() evaluation. Nothing to do with this patch though and the subject even mentions the rodata=full case. We should fix it separately.
We could have set the flag to zero in linear_map_maybe_split_to_ptes(), though split_kernel_leaf_mapping() already exits early due to the !system_supports_bbml2_noabort() && system_capabilities_finalized(), so not a correctness issue.
Quoted 3 lines
But regardless, I think we are talking about the pre-existing
is_real_world()bug, so I'm not personally planning to do anything further here unless you shout.
Not for this series. Steven or Suzuki should address the other problem with is_realm_world().
Signature · 1 line
Catalin
Re: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
In reply to Catalin MarinasQuoted 6 lines · Catalin Marinas
Quoted 3 lines
if we have forced pte mapping then the value of
can_set_direct_map()is irrelevant - we will never need to split because we are already pte-mapped.
can_set_direct_map()is used in other places, so its value is relevant, e.g.sys_memfd_secret()is rejected if this function returns false.
Indeed, I have noticed this before: currently set_direct_map_*_noflush() and other functions will either fail or do nothing if none of the features (rodata=full, etc.) is enabled, even if we would be able to split the linear map using BBML2-noabort.
What would make more sense to me is to enable the use of BBML2-noabort unconditionally if !force_pte_mapping(). We can then have can_set_direct_map() return true if we have BBML2-noabort, and we no longer need to check it in map_mem().
This is a functional change that doesn't have anything to do with realms so it should probably be a separate series - happy to take care of it once the dust settles on the realm handling.
- Kevin
Catalin Marinas
maintainerRe: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 5 lines · Kevin Brodsky
Quoted 4 lines · Catalin Marinas
Quoted 3 lines
if we have forced pte mapping then the value of
can_set_direct_map()is irrelevant - we will never need to split because we are already pte-mapped.
Quoted 8 lines
Quoted 3 lines
can_set_direct_map()is used in other places, so its value is relevant, e.g.sys_memfd_secret()is rejected if this function returns false.Indeed, I have noticed this before: currently set_direct_map_*_noflush() and other functions will either fail or do nothing if none of the features (rodata=full, etc.) is enabled, even if we would be able to split the linear map using BBML2-noabort.
That's what I have been trying to say to Ryan ;), can_set_direct_map() has different meanings depending on the caller: hint that it might split or asking whether splitting is permitted. The latter is not captured. Ignoring realms, if we have BBML2_NOABORT the kernel won't force pte mappings under the assumption that split_kernel_leaf_mapping() is safe. However set_direct_map_*_noflush() won't even reach the split function because the "can" part says "no, you can't".
Quoted 4 lines
What would make more sense to me is to enable the use of BBML2-noabort unconditionally if !force_pte_mapping(). We can then have
can_set_direct_map()return true if we have BBML2-noabort, and we no longer need to check it inmap_mem().
Indeed.
Quoted 3 lines
This is a functional change that doesn't have anything to do with realms so it should probably be a separate series - happy to take care of it once the dust settles on the realm handling.
I think it can be done in parallel, it shouldn't interfere with realms. The realm part should just affect force_pte_mapping() and can_set_direct_map() should return just what's possible, not what may need to set the direct map.
Signature · 1 line
Catalin
Re: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 20 lines · Catalin Marinas
Quoted 11 lines · Kevin Brodsky
Quoted 6 lines · Catalin Marinas
Quoted 3 lines
if we have forced pte mapping then the value of
can_set_direct_map()is irrelevant - we will never need to split because we are already pte-mapped.
can_set_direct_map()is used in other places, so its value is relevant, e.g.sys_memfd_secret()is rejected if this function returns false.Indeed, I have noticed this before: currently set_direct_map_*_noflush() and other functions will either fail or do nothing if none of the features (rodata=full, etc.) is enabled, even if we would be able to split the linear map using BBML2-noabort.
That's what I have been trying to say to Ryan ;),
can_set_direct_map()has different meanings depending on the caller: hint that it might split or asking whether splitting is permitted. The latter is not captured. Ignoring realms, if we haveBBML2_NOABORTthe kernel won't force pte mappings under the assumption thatsplit_kernel_leaf_mapping()is safe. However set_direct_map_*_noflush() won't even reach the split function because the "can" part says "no, you can't".
Quoted 5 lines
Quoted 4 lines
What would make more sense to me is to enable the use of BBML2-noabort unconditionally if !force_pte_mapping(). We can then have
can_set_direct_map()return true if we have BBML2-noabort, and we no longer need to check it inmap_mem().Indeed.
I'm trying to wrap up my head for this discussion. IIUC, if none of the features is enabled, it means we don't need do anything because the direct map is not changed. For example, if vmalloc doesn't change direct map permission when rodata != full, there is no need to call set_direct_map_*_noflush(). So unconditionally checking BBML2_NOABORT will change the behavior unnecessarily. Did I miss something?
I think the only exception is secretmem if I don't miss something. Currently, secretmem is actually not supported if none of the features is enabled. But BBML2_NOABORT allows to lift the restriction.
Thanks,
Yang
Quoted 9 lines
Quoted 4 lines
This is a functional change that doesn't have anything to do with realms so it should probably be a separate series - happy to take care of it once the dust settles on the realm handling.
I think it can be done in parallel, it shouldn't interfere with realms. The realm part should just affect
force_pte_mapping()andcan_set_direct_map()should return just what's possible, not what may need to set the direct map.
Catalin Marinas
maintainerRe: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 16 lines · Yang Shi
Quoted 7 lines · Catalin Marinas
Quoted 4 lines · Kevin Brodsky
What would make more sense to me is to enable the use of BBML2-noabort unconditionally if !force_pte_mapping(). We can then have
can_set_direct_map()return true if we have BBML2-noabort, and we no longer need to check it inmap_mem().Indeed.
I'm trying to wrap up my head for this discussion. IIUC, if none of the features is enabled, it means we don't need do anything because the direct map is not changed. For example, if vmalloc doesn't change direct map permission when rodata != full, there is no need to call set_direct_map_*_noflush(). So unconditionally checking
BBML2_NOABORTwill change the behavior unnecessarily. Did I miss something?
Quoted 3 lines
I think the only exception is secretmem if I don't miss something. Currently, secretmem is actually not supported if none of the features is enabled. But
BBML2_NOABORTallows to lift the restriction.
Yes, it's secretmem only AFAICT. I think execmem will only change the linear map if rodata_full anyway.
Signature · 1 line
Catalin
Re: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 15 lines · Catalin Marinas
Quoted 14 lines · Yang Shi
Quoted 6 lines · Catalin Marinas
Quoted 4 lines · Kevin Brodsky
What would make more sense to me is to enable the use of BBML2-noabort unconditionally if !force_pte_mapping(). We can then have
can_set_direct_map()return true if we have BBML2-noabort, and we no longer need to check it inmap_mem().Indeed.
I'm trying to wrap up my head for this discussion. IIUC, if none of the features is enabled, it means we don't need do anything because the direct map is not changed. For example, if vmalloc doesn't change direct map permission when rodata != full, there is no need to call set_direct_map_*_noflush(). So unconditionally checking
BBML2_NOABORTwill change the behavior unnecessarily. Did I miss something?
Quoted 5 lines
Quoted 3 lines
I think the only exception is secretmem if I don't miss something. Currently, secretmem is actually not supported if none of the features is enabled. But
BBML2_NOABORTallows to lift the restriction.Yes, it's secretmem only AFAICT. I think execmem will only change the linear map if
rodata_fullanyway.
Yes, execmem calls set_memory_rox(), which won't change linear map permission if rodata_full is not enabled.
Thanks,
Yang
Quoted 1 line
Re: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 23 lines · Yang Shi
Quoted 22 lines · Catalin Marinas
Quoted 21 lines · Yang Shi
Quoted 7 lines · Catalin Marinas
Quoted 5 lines · Kevin Brodsky
What would make more sense to me is to enable the use of BBML2-noabort
unconditionally if !force_pte_mapping(). We can then havecan_set_direct_map()return true if we have BBML2-noabort, and we no longer need to check it inmap_mem().Indeed.
I'm trying to wrap up my head for this discussion. IIUC, if none of the features is enabled, it means we don't need do anything because the direct
map is not changed. For example, if vmalloc doesn't change direct map permission when rodata != full, there is no need to call set_direct_map_*_noflush(). So unconditionally checkingBBML2_NOABORTwill
change the behavior unnecessarily. Did I miss something?I think the only exception is secretmem if I don't miss something. Currently, secretmem is actually not supported if none of the features is
enabled. ButBBML2_NOABORTallows to lift the restriction.
Quoted 5 lines
Quoted 2 lines
Yes, it's secretmem only AFAICT. I think execmem will only change the linear map if
rodata_fullanyway.Yes, execmem calls
set_memory_rox(), which won't change linear map permission ifrodata_fullis not enabled.
That is a good point, AFAICT set_direct_map_*_noflush() are only used by execmem and secretmem. excmem only modifies the direct map if rodata=full, so the proposed change would only be useful for secretmem.
The current situation with execmem is pretty strange: if rodata!=full, but another feature is enabled (say kfence), then set_memory_rox() won't touch the direct map but we will still use set_direct_map_*_noflush() to reset it (directly or via VM_FLUSH_RESET_PERMS). Checking BBML2-noabort in can_set_direct_map() would make these unnecessary calls more likely, but it doesn't fundamentally change the situation.
It's also worth considering the series unmapping parts of the direct map for guest_memfd [1], since it gates the use of set_direct_map_*_noflush() on can_set_direct_map().
I think it makes complete sense to enable secretmem and the guest_memfd use-case if BBML2-noabort is available, regardless of the other features. The question is: are we worried about the overhead of needlessly calling set_direct_map_*_noflush() for execmem mappings? If so, it seems that the right solution is to introduce a new API to check whether set_memory_ro() and friends actually modify the direct map or not.
- Kevin
Re: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 39 lines · Kevin Brodsky
Quoted 19 lines · Yang Shi
Quoted 18 lines · Catalin Marinas
Quoted 17 lines · Yang Shi
Quoted 7 lines · Catalin Marinas
Quoted 5 lines · Kevin Brodsky
What would make more sense to me is to enable the use of BBML2-noabort
unconditionally if !force_pte_mapping(). We can then havecan_set_direct_map()return true if we have BBML2-noabort, and we no longer need to check it inmap_mem().Indeed.
I'm trying to wrap up my head for this discussion. IIUC, if none of the features is enabled, it means we don't need do anything because the direct
map is not changed. For example, if vmalloc doesn't change direct map permission when rodata != full, there is no need to call set_direct_map_*_noflush(). So unconditionally checkingBBML2_NOABORTwill
change the behavior unnecessarily. Did I miss something?Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
I think the only exception is secretmem if I don't miss something. Currently, secretmem is actually not supported if none of the features is
enabled. ButBBML2_NOABORTallows to lift the restriction.Yes, it's secretmem only AFAICT. I think execmem will only change the linear map if
rodata_fullanyway.Yes, execmem calls
set_memory_rox(), which won't change linear map permission ifrodata_fullis not enabled.That is a good point, AFAICT set_direct_map_*_noflush() are only used by execmem and secretmem. excmem only modifies the direct map if rodata=full, so the proposed change would only be useful for secretmem.
The current situation with execmem is pretty strange: if rodata!=full, but another feature is enabled (say kfence), then
set_memory_rox()won't touch the direct map but we will still use set_direct_map_*_noflush() to reset it (directly or viaVM_FLUSH_RESET_PERMS). Checking BBML2-noabort incan_set_direct_map()would make these unnecessary calls more likely, but it doesn't fundamentally change the situation.
Quoted 7 lines
It's also worth considering the series unmapping parts of the direct map for
guest_memfd[1], since it gates the use of set_direct_map_*_noflush() oncan_set_direct_map().I think it makes complete sense to enable secretmem and the
guest_memfduse-case if BBML2-noabort is available, regardless of the other features. The question is: are we worried about the overhead of
Yes, agreed.
Quoted 3 lines
needlessly calling set_direct_map_*_noflush() for execmem mappings? If so, it seems that the right solution is to introduce a new API to check whether
set_memory_ro()and friends actually modify the direct map or not.
I don't have data regarding the overhead. The set_direct_map_*_noflush() does walk the page table and they will be called for each page for the area. It sounds not cheap anyway. In addition, it may split direct map into smaller granules unnecessarily, it may result in unexpected direct map fragmentation when rodata != full.
So it seems like introducing a new API is worth it.
Thanks,
Yang
Quoted 4 lines
- Kevin
Re: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
In reply to Catalin MarinasQuoted 55 lines · Catalin Marinas
Quoted 43 lines · Ryan Roberts
int
split_kernel_leaf_mapping(unsigned long start, unsigned long end){arch/arm64/mm/mmu.c +20
-11int ret; -/* -* !BBML2_NOABORT systems should not be trying to change permissions on -* anything that is not pte-mapped in the first place. Just return early -* and let the permission change code raise a warning if not already -* pte-mapped. -*/ -if (!system_supports_bbml2_noabort ()) -return 0 ; -/* * If the region is within a pte-mapped area, there is no need to try to * split. Additionally, CONFIG_DEBUG_PAGEALLOC and CONFIG_KFENCE may * change permissions from atomic context so for those cases (which are * always pte-mapped), we must not go any further because taking the -* mutex below may sleep. +* mutex below may sleep. Do not call force_pte_mapping() here because +* it could return a confusing result if called from a secondary cpu +* prior to finalizing caps. Instead, linear_map_requires_bbml2 gives us +* what we need. */ -if (force_pte_mapping () ||is_kfence_address ((void *)start)) +if (!linear_map_requires_bbml2 ||is_kfence_address ((void *)start))return 0 ; +if (!system_supports_bbml2_noabort ()) { +/* +* !BBML2_NOABORT systems should not be trying to change +* permissions on anything that is not pte-mapped in the first +* place. Just return early and let the permission change code +* raise a warning if not already pte-mapped. +*/ +if (system_capabilities_finalized ()) +return 0 ; + +/* +* Boot-time: split_kernel_leaf_mapping_locked() allocates from +* page allocator. Can't split until it's available. +*/ +if (WARN_ON (!page_alloc_available))arch/arm64/mm/mmu.c static inline bool force_pte_mapping(void) · line 814
Quoted 10 lines
arch/arm64/mm/mmu.c +10
-0+return -EBUSY ; + +/* +* Boot-time: Started secondary cpus but don't know if they +* support BBML2_NOABORT yet. Can't allow splitting in this +* window in case they don't. +*/ +if (WARN_ON (num_online_cpus () >1 )) +return -EBUSY ; + }
Quoted 7 lines
I think sashiko is over cautions here
(https://sashiko.dev/#/patchset/[email protected])
but it has a somewhat valid point from the perspective ofnum_online_cpus()semantics. We have havenum_online_cpus()== 1 while having a secondary CPU just booted and with its MMU enabled. I don't think we can have any asynchronous tasks running at that point to trigger a spit though. Evenasync_init()is called aftersmp_init().
Yes I saw the Sashiko report, but we had previously had a (private) discussion where I thought we had already concluded that this approach is safe in practice due to the way that the boot cpu brings the secondaries online.
Quoted 4 lines
An option may be to attempt
cpus_read_trylock()as this lock is taken by_cpu_up(). If it fails, return-EBUSY, otherwise checknum_online_cpus()and unlock (and return-EBUSYif secondaries already started).
That sounds neat; I could dig deeper and have a go at something like this if you want?
Quoted 6 lines
Another thing I couldn't get my head around - IIUC
is_realm_world()won't return true formap_mem()yet (if in a realm). Can we have realms on hardware that does not supportBBML2_NOABORT? We may not have configuration withrodata_fullset (it should be complementary to realm support).
My understanding is that this is a pre-existing (and known) bug. It's not related to the "map linear map by large leaves and split dynamically" feature so wasn't attempting to fix it.
I had heard that in practice all FEAT_RME systems should support FEAT_BBML3 which would solve the problem. Not sure how true that is though.
Quoted 5 lines
I'll add the patches to for-next/core to give them a bit of time in -next but let's see next week if we ignore this (with an updated comment) or we try to avoid the issue altogether.
Thanks,
Ryan
Catalin Marinas
maintainerRe: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 64 lines · Ryan Roberts
Quoted 55 lines · Catalin Marinas
Quoted 43 lines · Ryan Roberts
int
split_kernel_leaf_mapping(unsigned long start, unsigned long end){arch/arm64/mm/mmu.c +20
-11int ret; -/* -* !BBML2_NOABORT systems should not be trying to change permissions on -* anything that is not pte-mapped in the first place. Just return early -* and let the permission change code raise a warning if not already -* pte-mapped. -*/ -if (!system_supports_bbml2_noabort ()) -return 0 ; -/* * If the region is within a pte-mapped area, there is no need to try to * split. Additionally, CONFIG_DEBUG_PAGEALLOC and CONFIG_KFENCE may * change permissions from atomic context so for those cases (which are * always pte-mapped), we must not go any further because taking the -* mutex below may sleep. +* mutex below may sleep. Do not call force_pte_mapping() here because +* it could return a confusing result if called from a secondary cpu +* prior to finalizing caps. Instead, linear_map_requires_bbml2 gives us +* what we need. */ -if (force_pte_mapping () ||is_kfence_address ((void *)start)) +if (!linear_map_requires_bbml2 ||is_kfence_address ((void *)start))return 0 ; +if (!system_supports_bbml2_noabort ()) { +/* +* !BBML2_NOABORT systems should not be trying to change +* permissions on anything that is not pte-mapped in the first +* place. Just return early and let the permission change code +* raise a warning if not already pte-mapped. +*/ +if (system_capabilities_finalized ()) +return 0 ; + +/* +* Boot-time: split_kernel_leaf_mapping_locked() allocates from +* page allocator. Can't split until it's available. +*/ +if (WARN_ON (!page_alloc_available))arch/arm64/mm/mmu.c static inline bool force_pte_mapping(void) · line 814
Quoted 10 lines
arch/arm64/mm/mmu.c +10
-0+return -EBUSY ; + +/* +* Boot-time: Started secondary cpus but don't know if they +* support BBML2_NOABORT yet. Can't allow splitting in this +* window in case they don't. +*/ +if (WARN_ON (num_online_cpus () >1 )) +return -EBUSY ; + }Quoted 7 lines
I think sashiko is over cautions here
(https://sashiko.dev/#/patchset/[email protected])
but it has a somewhat valid point from the perspective ofnum_online_cpus()semantics. We have havenum_online_cpus()== 1 while having a secondary CPU just booted and with its MMU enabled. I don't think we can have any asynchronous tasks running at that point to trigger a spit though. Evenasync_init()is called aftersmp_init().
Quoted 3 lines
Yes I saw the Sashiko report, but we had previously had a (private) discussion where I thought we had already concluded that this approach is safe in practice due to the way that the boot cpu brings the secondaries online.
Yes, I thought I'd mention it. I don't see how this could go wrong in practice as we don't expect any page splitting on the primary CPU while it waits for the secondaries to come up.
Quoted 6 lines
Quoted 3 lines
An option may be to attempt
cpus_read_trylock()as this lock is taken by_cpu_up(). If it fails, return-EBUSY, otherwise checknum_online_cpus()and unlock (and return-EBUSYif secondaries already started).That sounds neat; I could dig deeper and have a go at something like this if you want?
I don't think it's worth it. We'll probably need to keep the lock for the duration of the splitting (though that's only before the cpu features are fully initialised). It's something we can look into if we ever see an issue here. For now, the num_online_cpus() test will do.
Quoted 9 lines
Quoted 5 lines
Another thing I couldn't get my head around - IIUC
is_realm_world()won't return true formap_mem()yet (if in a realm). Can we have realms on hardware that does not supportBBML2_NOABORT? We may not have configuration withrodata_fullset (it should be complementary to realm support).My understanding is that this is a pre-existing (and known) bug. It's not related to the "map linear map by large leaves and split dynamically" feature so wasn't attempting to fix it.
Yes, it's an existing bug I noticed while trying to understand the code paths. It doesn't need any action on this series but we should try to fix it separately.
Signature · 1 line
Catalin
Re: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
In reply to Catalin MarinasQuoted 63 lines · Catalin Marinas
Quoted 43 lines · Ryan Roberts
int
split_kernel_leaf_mapping(unsigned long start, unsigned long end){
int ret;arch/arm64/mm/mmu.c +20
-11-/* -* !BBML2_NOABORT systems should not be trying to change permissions on -* anything that is not pte-mapped in the first place. Just return early -* and let the permission change code raise a warning if not already -* pte-mapped. -*/ -if (!system_supports_bbml2_noabort ()) -return 0 ; -/* * If the region is within a pte-mapped area, there is no need to try to * split. Additionally, CONFIG_DEBUG_PAGEALLOC and CONFIG_KFENCE may * change permissions from atomic context so for those cases (which are * always pte-mapped), we must not go any further because taking the -* mutex below may sleep. +* mutex below may sleep. Do not call force_pte_mapping() here because +* it could return a confusing result if called from a secondary cpu +* prior to finalizing caps. Instead, linear_map_requires_bbml2 gives us +* what we need. */ -if (force_pte_mapping () ||is_kfence_address ((void *)start)) +if (!linear_map_requires_bbml2 ||is_kfence_address ((void *)start))return 0 ; +if (!system_supports_bbml2_noabort ()) { +/* +* !BBML2_NOABORT systems should not be trying to change +* permissions on anything that is not pte-mapped in the first +* place. Just return early and let the permission change code +* raise a warning if not already pte-mapped. +*/ +if (system_capabilities_finalized ()) +return 0 ; + +/* +* Boot-time: split_kernel_leaf_mapping_locked() allocates from +* page allocator. Can't split until it's available. +*/ +if (WARN_ON (!page_alloc_available))arch/arm64/mm/mmu.c static inline bool force_pte_mapping(void) · line 814
Quoted 10 lines
arch/arm64/mm/mmu.c +10
-0+return -EBUSY ; + +/* +* Boot-time: Started secondary cpus but don't know if they +* support BBML2_NOABORT yet. Can't allow splitting in this +* window in case they don't. +*/ +if (WARN_ON (num_online_cpus () >1 )) +return -EBUSY ; + }I think sashiko is over cautions here
(https://sashiko.dev/#/patchset/[email protected])
but it has a somewhat valid point from the perspective ofnum_online_cpus()semantics. We have havenum_online_cpus()== 1 while having a secondary CPU just booted and with its MMU enabled. I don't think we can have any asynchronous tasks running at that point to trigger a spit though. Evenasync_init()is called aftersmp_init().
Quoted 6 lines
An option may be to attempt
cpus_read_trylock()as this lock is taken by_cpu_up(). If it fails, return-EBUSY, otherwise checknum_online_cpus()and unlock (and return-EBUSYif secondaries already started).Another thing I couldn't get my head around - IIUC
is_realm_world()won't return true formap_mem()yet (if in a realm).
That is correct. map_mem() comes from paginig_init(), which gets called before arm64_rsi_init(). Realm check was delayed until psci_xx_init(). We had a version which parsed the DT for PSCI conduit early enough to be able to make the SMC calls to detect the Realm. But there were concerns around it.
Quoted 1 line
Can we have realms on hardware that does not support
BBML2_NOABORT?
I can get this checked. I expect that they all will have BBML2 (v8.4 extension). But NOABORT is something that may need to be checked.
We may not have
Quoted 10 lines
configuration with
rodata_fullset (it should be complementary to realm support).I'll add the patches to for-next/core to give them a bit of time in -next but let's see next week if we ignore this (with an updated comment) or we try to avoid the issue altogether.
Thanks
Suzuki
Catalin Marinas
maintainerRe: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 66 lines · Suzuki K Poulose
Quoted 65 lines · Catalin Marinas
Quoted 41 lines · Ryan Roberts
int
split_kernel_leaf_mapping(unsigned long start, unsigned long end){
int ret;arch/arm64/mm/mmu.c +20
-11-/* -* !BBML2_NOABORT systems should not be trying to change permissions on -* anything that is not pte-mapped in the first place. Just return early -* and let the permission change code raise a warning if not already -* pte-mapped. -*/ -if (!system_supports_bbml2_noabort ()) -return 0 ; -/* * If the region is within a pte-mapped area, there is no need to try to * split. Additionally, CONFIG_DEBUG_PAGEALLOC and CONFIG_KFENCE may * change permissions from atomic context so for those cases (which are * always pte-mapped), we must not go any further because taking the -* mutex below may sleep. +* mutex below may sleep. Do not call force_pte_mapping() here because +* it could return a confusing result if called from a secondary cpu +* prior to finalizing caps. Instead, linear_map_requires_bbml2 gives us +* what we need. */ -if (force_pte_mapping () ||is_kfence_address ((void *)start)) +if (!linear_map_requires_bbml2 ||is_kfence_address ((void *)start))return 0 ; +if (!system_supports_bbml2_noabort ()) { +/* +* !BBML2_NOABORT systems should not be trying to change +* permissions on anything that is not pte-mapped in the first +* place. Just return early and let the permission change code +* raise a warning if not already pte-mapped. +*/ +if (system_capabilities_finalized ()) +return 0 ; + +/* +* Boot-time: split_kernel_leaf_mapping_locked() allocates from +* page allocator. Can't split until it's available. +*/ +if (WARN_ON (!page_alloc_available))arch/arm64/mm/mmu.c static inline bool force_pte_mapping(void) · line 814
Quoted 10 lines
arch/arm64/mm/mmu.c +10
-0+return -EBUSY ; + +/* +* Boot-time: Started secondary cpus but don't know if they +* support BBML2_NOABORT yet. Can't allow splitting in this +* window in case they don't. +*/ +if (WARN_ON (num_online_cpus () >1 )) +return -EBUSY ; + }I think sashiko is over cautions here
(https://sashiko.dev/#/patchset/[email protected])
but it has a somewhat valid point from the perspective ofnum_online_cpus()semantics. We have havenum_online_cpus()== 1 while having a secondary CPU just booted and with its MMU enabled. I don't think we can have any asynchronous tasks running at that point to trigger a spit though. Evenasync_init()is called aftersmp_init().An option may be to attempt
cpus_read_trylock()as this lock is taken by_cpu_up(). If it fails, return-EBUSY, otherwise checknum_online_cpus()and unlock (and return-EBUSYif secondaries already started).
Quoted 8 lines
Quoted 2 lines
Another thing I couldn't get my head around - IIUC
is_realm_world()won't return true formap_mem()yet (if in a realm).That is correct.
map_mem()comes frompaginig_init(), which gets called beforearm64_rsi_init(). Realm check was delayed untilpsci_xx_init(). We had a version which parsed the DT for PSCI conduit early enough to be able to make the SMC calls to detect the Realm. But there were concerns around it.
Ah, yes, I remember.
Does it mean that commit 42be24a4178f ("arm64: Enable memory encrypt for Realms") was broken without rodata=full w.r.t. the linear map? Commit a166563e7ec3 ("arm64: mm: support large block mapping when rodata=full") introduced force_pte_mapping() but it just copied the logic in the existing can_set_direct_map(). Looking at the linear_map_requires_bbml2 assignment, we get (!is_realm_world() && is_realm_world()) and it cancels out, no effect on it but we don't get pte mappings either (even if we don't have BBML2).
I think we need at least some safety checks:
1. BBML2_NOABORT support on the boot CPU - continue with the existing logic (as per Ryan's series)
2. !system_supports_bbml2_noabort() - split in linear_map_maybe_split_to_ptes(). This does not currently happen because linear_map_requires_bbml2 may be false in the absence of rodata=full. Not sure how to fix this without some variable telling us how the linear map was mapped. The requires_bbml2 flag doesn't
3. Panic in arm64_rsi_init() if !BBML2_NOABORT on the boot CPU _and_ we have block mappings already. People can avoid it with rodata=full
4. If (3) is a common case, a better alternative is to rewrite the linear map sometime after arm64_rsi_init() but before we call split_kernel_leaf_mapping().
Signature · 1 line
Catalin
Re: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 76 lines · Catalin Marinas
Quoted 66 lines · Suzuki K Poulose
Quoted 65 lines · Catalin Marinas
Quoted 41 lines · Ryan Roberts
int split_kernel_leaf_mapping(unsigned long start, unsigned long end) { int ret;arch/arm64/mm/mmu.c +20
-11-/* -* !BBML2_NOABORT systems should not be trying to change permissions on -* anything that is not pte-mapped in the first place. Just return early -* and let the permission change code raise a warning if not already -* pte-mapped. -*/ -if (!system_supports_bbml2_noabort ()) -return 0 ; -/* * If the region is within a pte-mapped area, there is no need to try to * split. Additionally, CONFIG_DEBUG_PAGEALLOC and CONFIG_KFENCE may * change permissions from atomic context so for those cases (which are * always pte-mapped), we must not go any further because taking the -* mutex below may sleep. +* mutex below may sleep. Do not call force_pte_mapping() here because +* it could return a confusing result if called from a secondary cpu +* prior to finalizing caps. Instead, linear_map_requires_bbml2 gives us +* what we need. */ -if (force_pte_mapping () ||is_kfence_address ((void *)start)) +if (!linear_map_requires_bbml2 ||is_kfence_address ((void *)start))return 0 ; +if (!system_supports_bbml2_noabort ()) { +/* +* !BBML2_NOABORT systems should not be trying to change +* permissions on anything that is not pte-mapped in the first +* place. Just return early and let the permission change code +* raise a warning if not already pte-mapped. +*/ +if (system_capabilities_finalized ()) +return 0 ; + +/* +* Boot-time: split_kernel_leaf_mapping_locked() allocates from +* page allocator. Can't split until it's available. +*/ +if (WARN_ON (!page_alloc_available))arch/arm64/mm/mmu.c static inline bool force_pte_mapping(void) · line 814
Quoted 10 lines
arch/arm64/mm/mmu.c +10
-0+return -EBUSY ; + +/* +* Boot-time: Started secondary cpus but don't know if they +* support BBML2_NOABORT yet. Can't allow splitting in this +* window in case they don't. +*/ +if (WARN_ON (num_online_cpus () >1 )) +return -EBUSY ; + }I think sashiko is over cautions here
(https://sashiko.dev/#/patchset/[email protected])
but it has a somewhat valid point from the perspective ofnum_online_cpus()semantics. We have havenum_online_cpus()== 1 while having a secondary CPU just booted and with its MMU enabled. I don't think we can have any asynchronous tasks running at that point to trigger a spit though. Evenasync_init()is called aftersmp_init().An option may be to attempt
cpus_read_trylock()as this lock is taken by_cpu_up(). If it fails, return-EBUSY, otherwise checknum_online_cpus()and unlock (and return-EBUSYif secondaries already started).Quoted 8 lines
Quoted 2 lines
Another thing I couldn't get my head around - IIUC
is_realm_world()won't return true formap_mem()yet (if in a realm).That is correct.
map_mem()comes frompaginig_init(), which gets called beforearm64_rsi_init(). Realm check was delayed untilpsci_xx_init(). We had a version which parsed the DT for PSCI conduit early enough to be able to make the SMC calls to detect the Realm. But there were concerns around it.
Quoted 4 lines
Ah, yes, I remember.
Does it mean that commit
42be24a4178f("arm64: Enable memory encrypt for Realms") was broken without rodata=full w.r.t. the linear map? Commit
Apparently, it looks like we missed this when we demoted the RSI detection later.
Quoted 6 lines
a166563e7ec3("arm64: mm: support large block mapping when rodata=full") introducedforce_pte_mapping()but it just copied the logic in the existingcan_set_direct_map(). Looking at thelinear_map_requires_bbml2assignment, we get (!is_realm_world() &&is_realm_world()) and it cancels out, no effect on it but we don't get pte mappings either (even if we don't have BBML2).
Yep, that's right.
Quoted 6 lines
I think we need at least some safety checks:
1.
BBML2_NOABORTsupport on the boot CPU - continue with the existing logic (as per Ryan's series)
Quoted 8 lines
2. !system_supports_bbml2_noabort() - split in
linear_map_maybe_split_to_ptes(). This does not currently happen becauselinear_map_requires_bbml2may be false in the absence of rodata=full. Not sure how to fix this without some variable telling us how the linear map was mapped. Therequires_bbml2flag doesn't3. Panic in
arm64_rsi_init()if !BBML2_NOABORT on the boot CPU _and_ we have block mappings already. People can avoid it with rodata=full
It looks like this will be a common case :-(
Quoted 4 lines
4. If (3) is a common case, a better alternative is to rewrite the linear map sometime after
arm64_rsi_init()but before we callsplit_kernel_leaf_mapping().
We will explore this route.
The other option is to move the RSI detection (and the PSCI probe) earlier to be able to make better decisions early on. I will play with that a bit too.
Suzuki
Quoted 1 line
Catalin Marinas
maintainerRe: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
Quoted 31 lines · Suzuki K Poulose
Quoted 6 lines · Catalin Marinas
a166563e7ec3("arm64: mm: support large block mapping when rodata=full") introducedforce_pte_mapping()but it just copied the logic in the existingcan_set_direct_map(). Looking at thelinear_map_requires_bbml2assignment, we get (!is_realm_world() &&is_realm_world()) and it cancels out, no effect on it but we don't get pte mappings either (even if we don't have BBML2).Yep, that's right.
Quoted 6 lines
I think we need at least some safety checks:
1.
BBML2_NOABORTsupport on the boot CPU - continue with the existing logic (as per Ryan's series)Quoted 8 lines
2. !system_supports_bbml2_noabort() - split in
linear_map_maybe_split_to_ptes(). This does not currently happen becauselinear_map_requires_bbml2may be false in the absence of rodata=full. Not sure how to fix this without some variable telling us how the linear map was mapped. Therequires_bbml2flag doesn't3. Panic in
arm64_rsi_init()if !BBML2_NOABORT on the boot CPU _and_ we have block mappings already. People can avoid it with rodata=fullIt looks like this will be a common case :-(
Quoted 4 lines
4. If (3) is a common case, a better alternative is to rewrite the linear map sometime after
arm64_rsi_init()but before we callsplit_kernel_leaf_mapping().
Quoted 5 lines
We will explore this route.
The other option is to move the RSI detection (and the PSCI probe) earlier to be able to make better decisions early on. I will play with that a bit too.
I thought we could reuse linear_map_split_to_ptes() but this function assumes that the primary CPU supports BBML2_NOABORT. To do this live, we'd have to clone the active kernel pgtable hierarchy, switch to it and then continue with the splitting. kasan_init_shadow() does a bit of this but not fully as it only cares about the shadow mapping.
Hmm, maybe probing the RSI early is easier ;).
Signature · 1 line
Catalin
Re: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
In reply to Suzuki K PouloseQuoted 115 lines · Suzuki K Poulose
Quoted 86 lines · Catalin Marinas
Quoted 76 lines · Suzuki K Poulose
Quoted 75 lines · Catalin Marinas
Quoted 53 lines · Ryan Roberts
int
split_kernel_leaf_mapping(unsigned long start, unsigned long end){
int ret;
- /*
- * !BBML2_NOABORT systems should not be trying to change permissions on
- * anything that is not pte-mapped in the first place. Just return early
- * and let the permission change code raise a warning if not already
- * pte-mapped.
- */
- if (!system_supports_bbml2_noabort())
- return 0;
-
/*
* If the region is within a pte-mapped area, there is no need to try to
* split. Additionally,CONFIG_DEBUG_PAGEALLOCandCONFIG_KFENCEmay
* change permissions from atomic context so for those cases (which are
* always pte-mapped), we must not go any further because taking the - * mutex below may sleep.
+ * mutex below may sleep. Do not callforce_pte_mapping()here because
+ * it could return a confusing result if called from a secondary cpu
+ * prior to finalizing caps. Instead,linear_map_requires_bbml2gives us
+ * what we need.
*/
- if (force_pte_mapping()||is_kfence_address((void *)start)) + if (!linear_map_requires_bbml2 ||is_kfence_address((void *)start))
return 0;
+ if (!system_supports_bbml2_noabort()) {
+ /*
+ * !BBML2_NOABORT systems should not be trying to change + * permissions on anything that is not pte-mapped in the first
+ * place. Just return early and let the permission change code
+ * raise a warning if not already pte-mapped.
+ */
+ if (system_capabilities_finalized())
+ return 0;
+
+ /*
+ * Boot-time:split_kernel_leaf_mapping_locked()allocates from
+ * page allocator. Can't split until it's available.
+ */
+ if (WARN_ON(!page_alloc_available))
+ return-EBUSY;
+Quoted 8 lines
+ /*
+ * Boot-time: Started secondary cpus but don't know if they + * supportBBML2_NOABORTyet. Can't allow splitting in this + * window in case they don't.
+ */
+ if (WARN_ON(num_online_cpus() > 1))
+ return-EBUSY;
+ }I think sashiko is over cautions here
(https://sashiko.dev/#/patchset/20260330161705.3349825-1- [email protected]) but it has a somewhat valid point from the perspective ofnum_online_cpus()semantics. We have havenum_online_cpus()== 1 while having a secondary CPU just booted and with its MMU enabled. I don't think we can have any asynchronous tasks running at that point to trigger a spit though. Evenasync_init()is called aftersmp_init().An option may be to attempt
cpus_read_trylock()as this lock is taken by_cpu_up(). If it fails, return-EBUSY, otherwise checknum_online_cpus()and unlock (and return-EBUSYif secondaries already started).Quoted 8 lines
Quoted 2 lines
Another thing I couldn't get my head around - IIUC
is_realm_world()won't return true formap_mem()yet (if in a realm).That is correct.
map_mem()comes frompaginig_init(), which gets called beforearm64_rsi_init(). Realm check was delayed untilpsci_xx_init(). We had a version which parsed the DT for PSCI conduit early enough to be able to make the SMC calls to detect the Realm. But there were concerns around it.Quoted 4 lines
Ah, yes, I remember.
Does it mean that commit
42be24a4178f("arm64: Enable memory encrypt for Realms") was broken without rodata=full w.r.t. the linear map? CommitApparently, it looks like we missed this when we demoted the RSI detection later.
Quoted 6 lines
a166563e7ec3("arm64: mm: support large block mapping when rodata=full") introducedforce_pte_mapping()but it just copied the logic in the existingcan_set_direct_map(). Looking at thelinear_map_requires_bbml2assignment, we get (!is_realm_world() &&is_realm_world()) and it cancels out, no effect on it but we don't get pte mappings either (even if we don't have BBML2).Yep, that's right.
Quoted 12 lines
I think we need at least some safety checks:
1.
BBML2_NOABORTsupport on the boot CPU - continue with the existing logic (as per Ryan's series)2. !system_supports_bbml2_noabort() - split in
linear_map_maybe_split_to_ptes(). This does not currently happen becauselinear_map_requires_bbml2may be false in the absence of rodata=full. Not sure how to fix this without some variable telling us how the linear map was mapped. Therequires_bbml2flag doesn't
Quoted 4 lines
Quoted 2 lines
3. Panic in
arm64_rsi_init()if !BBML2_NOABORT on the boot CPU _and_ we have block mappings already. People can avoid it with rodata=fullIt looks like this will be a common case :-(
Having another look, by default, arm64 boots with rodata=full, and users have to explicitly lower the bar by setting rodata=off or noalias. So this has been keeping us running ;-).
With rodata=off, I get the following for a Realm boot:
[ 0.000000] ------------[ cut here ]------------
[ 0.000000] WARNING: arch/arm64/mm/pageattr.c:61 at pageattr_pmd_entry+0x78/0xe0, CPU#0: swapper/0 [ 0.000000] Modules linked in: [ 0.000000] CPU: 0 UID: 0 PID: 0 Comm: swapper Not tainted 7.0.0-rc1+ #1889 PREEMPT [ 0.000000] Hardware name: linux,dummy-virt (DT) [ 0.000000] pstate: 800000c5 (Nzcv daIF -PAN -UAO -TCO -DIT -SSBS BTYPE=--) [ 0.000000] pc : pageattr_pmd_entry+0x78/0xe0 [ 0.000000] lr : walk_pgd_range+0x43c/0x970 [ 0.000000] sp : ffff800082343b70 [ 0.000000] x29: ffff800082343b70 x28: fff0000019600000 x27: fff0000019580000 [ 0.000000] x26: ffff800082343c98 x25: fff000001d57ffff x24: fff000001fffe000 [ 0.000000] x23: ffff8000810ae698 x22: fff000001fffd650 x21: fff0000019780000 [ 0.000000] x20: fff000001d580000 x19: 0000000000000000 x18: 0000000000000030 [ 0.000000] x17: 0000000000004000 x16: 000000009fffc000 x15: 0000000000000020 [ 0.000000] x14: 0000000000003be4 x13: 0000000000000020 x12: 0000000000000000 [ 0.000000] x11: 0000000000000016 x10: 0000000000000015 x9 : 0000000000000013 [ 0.000000] x8 : 0000000000000015 x7 : 0000000080000000 x6 : 0000000000000000 [ 0.000000] x5 : 0078000099400405 x4 : fff000001fffd650 x3 : ffff800082343c98 [ 0.000000] x2 : 0000000000080000 x1 : fff0000019580000 x0 : 0000000000000001 [ 0.000000] Call trace: [ 0.000000] pageattr_pmd_entry+0x78/0xe0 (P) [ 0.000000] walk_kernel_page_table_range_lockless+0x60/0xa0
[ 0.000000] update_range_prot+0x80/0x128 [ 0.000000] __set_memory_enc_dec.part.0+0x88/0x258 [ 0.000000] realm_set_memory_decrypted+0x54/0x98 [ 0.000000] set_memory_decrypted+0x38/0x58 [ 0.000000] swiotlb_update_mem_attributes+0x44/0x58 [ 0.000000] mem_init+0x24/0x38 [ 0.000000] mm_core_init+0x94/0x140 [ 0.000000] start_kernel+0x544/0xa18 [ 0.000000] __primary_switched+0x88/0x98 [ 0.000000] ---[ end trace 0000000000000000 ]---
Suzuki
Quoted 17 lines
Quoted 5 lines
4. If (3) is a common case, a better alternative is to rewrite the linear map sometime after
arm64_rsi_init()but before we callsplit_kernel_leaf_mapping().We will explore this route.
The other option is to move the RSI detection (and the PSCI probe) earlier to be able to make better decisions early on. I will play with that a bit too.
Suzuki
Quoted 1 line
Re: [PATCH v2 1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
In reply to Ryan Robertsarch/arm64/mm/init.c void __init arch_mm_preinit(void) · line 383
Quoted 5 lines · Ryan Roberts
arch/arm64/mm/init.c +5
-0+bool page_alloc_available__ro_after_init ; + +void __init mem_init (void ) +{ + page_alloc_available = true;
I knew I had seen a simpler solution to this: vmemmap_alloc_block() uses slab_is_available() to tell whether the buddy allocator is available. AFAICT this becomes true somewhere in kmem_cache_init(), which is called just after mem_init(). Probably good enough for our purpose?
- Kevin
Quoted 2 lines
+
swiotlb_update_mem_attributes();
+}
PATCH v2 2/3 arm64: mm: Handle invalid large leaf mappings correctly
In reply to Ryan RobertsIt has been possible for a long time to mark ptes in the linear map as invalid. This is done for secretmem, kfence, realm dma memory un/share, and others, by simply clearing the PTE_VALID bit. But until commit a166563e7ec37 ("arm64: mm: support large block mapping when rodata=full") large leaf mappings were never made invalid in this way.
It turns out various parts of the code base are not equipped to handle invalid large leaf mappings (in the way they are currently encoded) and I've observed a kernel panic while booting a realm guest on a BBML2_NOABORT system as a result:
[ 15.432706] software IO TLB: Memory encryption is active and system is using DMA bounce buffers [ 15.476896] Unable to handle kernel paging request at virtual address ffff000019600000 [ 15.513762] Mem abort info: [ 15.527245] ESR = 0x0000000096000046 [ 15.548553] EC = 0x25: DABT (current EL), IL = 32 bits [ 15.572146] SET = 0, FnV = 0 [ 15.592141] EA = 0, S1PTW = 0 [ 15.612694] FSC = 0x06: level 2 translation fault [ 15.640644] Data abort info: [ 15.661983] ISV = 0, ISS = 0x00000046, ISS2 = 0x00000000 [ 15.694875] CM = 0, WnR = 1, TnD = 0, TagAccess = 0 [ 15.723740] GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0 [ 15.755776] swapper pgtable: 4k pages, 48-bit VAs, pgdp=0000000081f3f000 [ 15.800410] [ffff000019600000] pgd=0000000000000000, p4d=180000009ffff403, pud=180000009fffe403, pmd=00e8000199600704 [ 15.855046] Internal error: Oops: 0000000096000046 [#1] SMP [ 15.886394] Modules linked in: [ 15.900029] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 7.0.0-rc4-dirty #4 PREEMPT [ 15.935258] Hardware name: linux,dummy-virt (DT) [ 15.955612] pstate: 21400005 (nzCv daif +PAN -UAO -TCO +DIT -SSBS BTYPE=--) [ 15.986009] pc : __pi_memcpy_generic+0x128/0x22c [ 16.006163] lr : swiotlb_bounce+0xf4/0x158 [ 16.024145] sp : ffff80008000b8f0 [ 16.038896] x29: ffff80008000b8f0 x28: 0000000000000000 x27: 0000000000000000 [ 16.069953] x26: ffffb3976d261ba8 x25: 0000000000000000 x24: ffff000019600000 [ 16.100876] x23: 0000000000000001 x22: ffff0000043430d0 x21: 0000000000007ff0 [ 16.131946] x20: 0000000084570010 x19: 0000000000000000 x18: ffff00001ffe3fcc [ 16.163073] x17: 0000000000000000 x16: 00000000003fffff x15: 646e612065766974 [ 16.194131] x14: 0000000000000000 x13: 0000000000000000 x12: 0000000000000000 [ 16.225059] x11: 0000000000000000 x10: 0000000000000010 x9 : 0000000000000018 [ 16.256113] x8 : 0000000000000018 x7 : 0000000000000000 x6 : 0000000000000000 [ 16.287203] x5 : ffff000019607ff0 x4 : ffff000004578000 x3 : ffff000019600000 [ 16.318145] x2 : 0000000000007ff0 x1 : ffff000004570010 x0 : ffff000019600000 [ 16.349071] Call trace: [ 16.360143] __pi_memcpy_generic+0x128/0x22c (P) [ 16.380310] swiotlb_tbl_map_single+0x154/0x2b4 [ 16.400282] swiotlb_map+0x5c/0x228 [ 16.415984] dma_map_phys+0x244/0x2b8 [ 16.432199] dma_map_page_attrs+0x44/0x58 [ 16.449782] virtqueue_map_page_attrs+0x38/0x44 [ 16.469596] virtqueue_map_single_attrs+0xc0/0x130 [ 16.490509] virtnet_rq_alloc.isra.0+0xa4/0x1fc [ 16.510355] try_fill_recv+0x2a4/0x584 [ 16.526989] virtnet_open+0xd4/0x238 [ 16.542775] __dev_open+0x110/0x24c [ 16.558280] __dev_change_flags+0x194/0x20c [ 16.576879] netif_change_flags+0x24/0x6c [ 16.594489] dev_change_flags+0x48/0x7c [ 16.611462] ip_auto_config+0x258/0x1114 [ 16.628727] do_one_initcall+0x80/0x1c8 [ 16.645590] kernel_init_freeable+0x208/0x2f0 [ 16.664917] kernel_init+0x24/0x1e0 [ 16.680295] ret_from_fork+0x10/0x20 [ 16.696369] Code: 927cec03 cb0e0021 8b0e0042 a9411c26 (a900340c) [ 16.723106] ---[ end trace 0000000000000000 ]--- [ 16.752866] Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b [ 16.792556] Kernel Offset: 0x3396ea200000 from 0xffff800080000000 [ 16.818966] PHYS_OFFSET: 0xfff1000080000000 [ 16.837237] CPU features: 0x0000000,00060005,13e38581,957e772f [ 16.862904] Memory Limit: none [ 16.876526] ---[ end Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b ]---
This panic occurs because the swiotlb memory was previously shared to the host (__set_memory_enc_dec()), which involves transitioning the (large) leaf mappings to invalid, sharing to the host, then marking the mappings valid again. But pageattr_p[mu]d_entry() would only update the entry if it is a section mapping, since otherwise it concluded it must be a table entry so shouldn't be modified. But p[mu]d_sect() only returns true if the entry is valid. So the result was that the large leaf entry was made invalid in the first pass then ignored in the second pass. It remains invalid until the above code tries to access it and blows up.
The simple fix would be to update pageattr_pmd_entry() to use !pmd_table() instead of pmd_sect(). That would solve this problem.
But the ptdump code also suffers from a similar issue. It checks pmd_leaf() and doesn't call into the arch-specific note_page() machinery if it returns false. As a result of this, ptdump wasn't even able to show the invalid large leaf mappings; it looked like they were valid which made this super fun to debug. the ptdump code is core-mm and pmd_table() is arm64-specific so we can't use the same trick to solve that.
But we already support the concept of "present-invalid" for user space entries. And even better, pmd_leaf() will return true for a leaf mapping that is marked present-invalid. So let's just use that encoding for present-invalid kernel mappings too. Then we can use pmd_leaf() where we previously used pmd_sect() and everything is magically fixed.
Additionally, from inspection kernel_page_present() was broken in a similar way, so I'm also updating that to use pmd_leaf().
The transitional page tables component was also similarly broken; it creates a copy of the kernel page tables, making RO leaf mappings RW in the process. It also makes invalid (but-not-none) pte mappings valid. But it was not doing this for large leaf mappings. This could have resulted in crashes at kexec- or hibernate-time. This code is fixed to flip "present-invalid" mappings back to "present-valid" at all levels.
Finally, I have hardened split_pmd()/split_pud() so that if it is passed a "present-invalid" leaf, it will maintain that property in the split leaves, since I wasn't able to convince myself that it would only ever be called for "present-valid" leaves.
--- arch/arm64/include/asm/pgtable-prot.h | 2 ++ arch/arm64/include/asm/pgtable.h | 9 +++-- arch/arm64/mm/mmu.c | 4 +++ arch/arm64/mm/pageattr.c | 50 +++++++++++++++------------ arch/arm64/mm/trans_pgd.c | 42 ++++------------------ 5 files changed, 48 insertions(+), 59 deletions(-)
arch/arm64/include/asm/pgtable-prot.h +2 -0
@@ -25,6 +25,8 @@*/ #define PTE_PRESENT_INVALID (PTE_NG)/* only when !PTE_VALID */ +#define PTE_PRESENT_VALID_KERNEL (PTE_VALID | PTE_MAYBE_NG) +#ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP #define PTE_UFFD_WP (_AT(pteval_t, 1) << 58)/* uffd-wp tracking */ #define PTE_SWP_UFFD_WP (_AT(pteval_t, 1) << 3)/* only for swp ptes */
arch/arm64/include/asm/pgtable.h +7 -2
@@ -322,9 +322,11 @@ static inline pte_t pte_mknoncont(pte_t pte)return clear_pte_bit (pte,__pgprot (PTE_CONT )); } -static inline pte_t pte_mkvalid (pte_t pte) +static inline pte_t pte_mkvalid_k (pte_t pte) { -return set_pte_bit (pte,__pgprot (PTE_VALID )); + pte =clear_pte_bit (pte,__pgprot (PTE_PRESENT_INVALID )); + pte =set_pte_bit (pte,__pgprot (PTE_PRESENT_VALID_KERNEL )); +return pte; }static inline pte_t pte_mkinvalid (pte_t pte)
@@ -594,6 +596,7 @@ static inline int pmd_protnone(pmd_t pmd)#define pmd_mkclean (pmd) pte_pmd(pte_mkclean(pmd_pte(pmd)))#define pmd_mkdirty (pmd) pte_pmd(pte_mkdirty(pmd_pte(pmd)))#define pmd_mkyoung (pmd) pte_pmd(pte_mkyoung(pmd_pte(pmd))) +#define pmd_mkvalid_k (pmd) pte_pmd(pte_mkvalid_k(pmd_pte(pmd)))#define pmd_mkinvalid (pmd) pte_pmd(pte_mkinvalid(pmd_pte(pmd)))#ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP #define pmd_uffd_wp (pmd) pte_uffd_wp(pmd_pte(pmd))
@@ -635,6 +638,8 @@ static inline pmd_t pmd_mkspecial(pmd_t pmd)#define pud_young (pud) pte_young(pud_pte(pud))#define pud_mkyoung (pud) pte_pud(pte_mkyoung(pud_pte(pud))) +#define pud_mkwrite_novma (pud) pte_pud(pte_mkwrite_novma(pud_pte(pud))) +#define pud_mkvalid_k (pud) pte_pud(pte_mkvalid_k(pud_pte(pud)))#define pud_write (pud) pte_write(pud_pte(pud))static inline pud_t pud_mkhuge (pud_t pud)
arch/arm64/mm/mmu.c +4 -0
@@ -602,6 +602,8 @@ static int split_pmd(pmd_t *pmdp, pmd_t pmd, gfp_t gfp, bool to_cont) tableprot |=PMD_TABLE_PXN ; prot =__pgprot ((pgprot_val (prot) & ~PTE_TYPE_MASK ) |PTE_TYPE_PAGE ); +if (!pmd_valid (pmd)) + prot =pte_pgprot (pte_mkinvalid (pfn_pte (0 , prot))); prot =__pgprot (pgprot_val (prot) & ~PTE_CONT );if (to_cont) prot =__pgprot (pgprot_val (prot) |PTE_CONT );
@@ -647,6 +649,8 @@ static int split_pud(pud_t *pudp, pud_t pud, gfp_t gfp, bool to_cont) tableprot |=PUD_TABLE_PXN ; prot =__pgprot ((pgprot_val (prot) & ~PMD_TYPE_MASK ) |PMD_TYPE_SECT ); +if (!pud_valid (pud)) + prot =pmd_pgprot (pmd_mkinvalid (pfn_pmd (0 , prot))); prot =__pgprot (pgprot_val (prot) & ~PTE_CONT );if (to_cont) prot =__pgprot (pgprot_val (prot) |PTE_CONT );
arch/arm64/mm/pageattr.c +28 -22
116 lines not on this page · raw message
arch/arm64/mm/trans_pgd.c +7 -35
66 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v2 3/3 arm64: mm: Remove pmd_sect() and pud_sect()
In reply to Ryan RobertsThe semantics of pXd_leaf() are very similar to pXd_sect(). The only difference is that pXd_sect() only considers it a section if PTE_VALID is set, whereas pXd_leaf() permits both "valid" and "present-invalid" types.
Using pXd_sect() has caused issues now that large leaf entries can be present-invalid since commit a166563e7ec37 ("arm64: mm: support large block mapping when rodata=full"), so let's just remove the API and standardize on pXd_leaf().
There are a few callsites of the form pXd_leaf(READ_ONCE(*pXdp)). This was previously fine for the pXd_sect() macro because it only evaluated its argument once. But pXd_leaf() evaluates its argument multiple times. So let's avoid unintended side effects by reimplementing pXd_leaf() as an inline function.
--- arch/arm64/include/asm/pgtable.h | 19 ++++++++++++------- arch/arm64/mm/mmu.c | 18 +++++++++--------- 2 files changed, 21 insertions(+), 16 deletions(-)
arch/arm64/include/asm/pgtable.h +12 -7
@@ -784,9 +784,13 @@ extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,#define pmd_table (pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \ PMD_TYPE_TABLE) -#define pmd_sect (pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \ - PMD_TYPE_SECT) -#define pmd_leaf (pmd) (pmd_present(pmd) && !pmd_table(pmd)) + +#define pmd_leaf pmd_leaf +static inline bool pmd_leaf (pmd_t pmd) +{ +return pmd_present (pmd) && !pmd_table (pmd); +} +#define pmd_bad (pmd) (!pmd_table(pmd))#define pmd_leaf_size (pmd) (pmd_cont(pmd) ? CONT_PMD_SIZE : PMD_SIZE)
@@ -804,11 +808,8 @@ static inline int pmd_trans_huge(pmd_t pmd)#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ #if defined(CONFIG_ARM64_64K_PAGES ) ||CONFIG_PGTABLE_LEVELS <3 -static inline bool pud_sect (pud_t pud) {return false; }static inline bool pud_table (pud_t pud) {return true; }#else -#define pud_sect (pud) ((pud_val(pud) & PUD_TYPE_MASK) == \ - PUD_TYPE_SECT)#define pud_table (pud) ((pud_val(pud) & PUD_TYPE_MASK) == \ PUD_TYPE_TABLE)#endif
@@ -878,7 +879,11 @@ static inline unsigned long pmd_page_vaddr(pmd_t pmd) PUD_TYPE_TABLE)#define pud_present (pud) pte_present(pud_pte(pud))#ifndef __PAGETABLE_PMD_FOLDED -#define pud_leaf (pud) (pud_present(pud) && !pud_table(pud)) +#define pud_leaf pud_leaf +static inline bool pud_leaf (pud_t pud) +{ +return pud_present (pud) && !pud_table (pud); +}#else #define pud_leaf (pud) false#endif
arch/arm64/mm/mmu.c +9 -9
@@ -204,7 +204,7 @@ static int alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,pmd_t pmd =READ_ONCE (*pmdp);pte_t *ptep; -BUG_ON (pmd_sect (pmd)); +BUG_ON (pmd_leaf (pmd));if (pmd_none (pmd)) {pmdval_t pmdval =PMD_TYPE_TABLE |PMD_TABLE_UXN |PMD_TABLE_AF ;phys_addr_t pte_phys;
@@ -303,7 +303,7 @@ static int alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,/* * Check for initial section mappings in the pgd/pud. */ -BUG_ON (pud_sect (pud)); +BUG_ON (pud_leaf (pud));if (pud_none (pud)) {pudval_t pudval =PUD_TYPE_TABLE |PUD_TABLE_UXN |PUD_TABLE_AF ;phys_addr_t pmd_phys;
@@ -1503,7 +1503,7 @@ static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr,continue ;WARN_ON (!pmd_present (pmd)); -if (pmd_sect (pmd)) { +if (pmd_leaf (pmd)) {pmd_clear (pmdp);/*
@@ -1536,7 +1536,7 @@ static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr,continue ;WARN_ON (!pud_present (pud)); -if (pud_sect (pud)) { +if (pud_leaf (pud)) {pud_clear (pudp);/*
@@ -1650,7 +1650,7 @@ static void free_empty_pmd_table(pud_t *pudp, unsigned long addr,if (pmd_none (pmd))continue ; -WARN_ON (!pmd_present (pmd) || !pmd_table (pmd) ||pmd_sect (pmd)); +WARN_ON (!pmd_present (pmd) || !pmd_table (pmd));free_empty_pte_table (pmdp, addr, next, floor, ceiling); }while (addr = next, addr < end);
@@ -1690,7 +1690,7 @@ static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr,if (pud_none (pud))continue ; -WARN_ON (!pud_present (pud) || !pud_table (pud) ||pud_sect (pud)); +WARN_ON (!pud_present (pud) || !pud_table (pud));free_empty_pmd_table (pudp, addr, next, floor, ceiling); }while (addr = next, addr < end);
@@ -1786,7 +1786,7 @@ int __meminit vmemmap_check_pmd(pmd_t *pmdp, int node, {vmemmap_verify ((pte_t *)pmdp, node, addr, next); -return pmd_sect (READ_ONCE (*pmdp)); +return pmd_leaf (READ_ONCE (*pmdp)); } int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
@@ -1850,7 +1850,7 @@ void p4d_clear_huge(p4d_t *p4dp)int pud_clear_huge (pud_t *pudp) { -if (!pud_sect (READ_ONCE (*pudp))) +if (!pud_leaf (READ_ONCE (*pudp)))return 0 ;pud_clear (pudp);return 1 ;
@@ -1858,7 +1858,7 @@ int pud_clear_huge(pud_t *pudp)int pmd_clear_huge (pmd_t *pmdp) { -if (!pmd_sect (READ_ONCE (*pmdp))) +if (!pmd_leaf (READ_ONCE (*pmdp)))return 0 ;pmd_clear (pmdp);return 1 ;
Signature · 1 line
2.43.0
Quoted 8 lines · Ryan Roberts
This fixes a couple of bugs in the "large block mappings for linear map when we have
BBML2_NOABORT" feature when used in conjunction with a CCA realm guest. While investigating I found and fixed some more general issues too. See commit logs for full explanations.Applies on top of v7.0-rc4.
[...]
Applied to arm64 (for-next/bbml2-fixes), thanks! I had some comments on the first patch, so I may rebase it or add something on top.
[1/3] arm64: mm: Fix rodata=full block mapping support for realm guests
https://git.kernel.org/arm64/c/f12b435de2f2 [2/3] arm64: mm: Handle invalid large leaf mappings correctly https://git.kernel.org/arm64/c/15bfba1ad77f [3/3] arm64: mm: Remove pmd_sect() and pud_sect() https://git.kernel.org/arm64/c/1d37713fa837