Skip to content

x86: Robustify pgtable_l5_enabled()

Ard Biesheuvel lkml 5 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v5 Latest7 patches 14 repliesIn review
  2. v46 patches 22 repliesApplied
  3. v37 patches 27 repliesApplied
Earlier versions · 2
VersionPostedPatchesRepliesStatus
v2 RFC6 patches17 repliesApplied
v1 RFC3 patches18 repliesSuperseded

Changes in v5 from the cover letter

- Add patch to fix MAX_PHYSMEM_BITS (and drop an occurrence of pgtable_l5_enabled())
- Re-order the changes and split across more patches so any potential performance hit is bisectable.

Patches in v5 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/7x86/mm: Decouple MAX_PHYSMEM_BITS from LA57 statearch/x86/include/asm/pgtable_64_types.h arch/x86/include/asm/sparsemem.h+1 -32 replies
2/7x86/mm: Use a single cache hot per-CPU variable to record pgdir_shiftarch/x86/boot/compressed/misc.h arch/x86/boot/compressed/pgtable_64.c arch/x86/boot/startup/map_kernel.c +5 more+26 -910 replies
3/7x86/mm: Define PTRS_PER_P4D in terms of pgdir_shift()arch/x86/boot/compressed/misc.h arch/x86/boot/compressed/pgtable_64.c arch/x86/boot/startup/map_kernel.c +2 more+2 -92 replies
4/7x86/mm: Derive pgtable_l5_enabled() from pgdir_shift()arch/x86/include/asm/pgtable_64_types.h+2 -120 replies
5/7x86/boot: Drop USE_EARLY_PGTABLE_L5 definitionsarch/x86/boot/compressed/misc.h arch/x86/boot/startup/sme.c arch/x86/kernel/cpu/common.c +2 more+0 -210 replies
6/7x86/boot: Drop 5-level paging related global variablearch/x86/boot/compressed/misc.h arch/x86/boot/compressed/pgtable_64.c arch/x86/boot/startup/map_kernel.c +2 more+1 -260 replies
7/7x86/boot: Remove KASAN workaround for 4/5 level paging switcharch/x86/kernel/alternative.c+0 -120 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v5 yet.

Read the v5 thread

Cover letter [PATCH v5 0/7] x86: Robustify pgtable_l5_enabled()

From: Ard Biesheuvel <[email protected]>

This is a follow-up to the discussion at [0], broken out of that series so we can progress while the SEV changes are being reviewed and tested.

The current implementation of pgtable_l5_enabled() is problematic because it has two implementations, and source files need to opt into the correct one if they contain code that might be called very early. Other related global pseudo-constants exist that assume different values based on the number of paging levels, and it is hard to reason about whether or not all memory mapping and page table code is guaranteed to observe consistent values of all of these at all times during the boot. Case in point: currently, KASAN needs to be disabled during alternatives patching because otherwise, it will reliably produce false positive reports due to such inconsistencies.

This revision of the series still provides a single implementation of pgtable_l5_enabled(), but no longer based on cpu_feature_enabled(), for a number of reasons:
- fiddling with the early CPU feature detection code is not risk-free, and may cause regressions that are difficult to debug;
- Boris objected to the use of a separate capability flag, and using the existing one is trickier, as it gets set and cleared during the boot by the feature detection code a couple of times, even if 5-level paging is not in use
- by their very nature, manipulations of level 4 and level 5 page tables occur rarely compared to lower levels, so it is not obvious that the code patching in cpu_feature_enabled() is needed.

So instead, collapse the various 5-level paging related global variables into a single byte wide pgdir_shift variable, and move it into the cache hot per-CPU section where it can be accessed cheaply. Set it from asm code so C will always see the same value, and derive pgtable_l5_enabled() and PTRS_PER_P4D from it directly, ensuring that all these quantities are always mutually consistent.

If pgtable_l5_enabled() requires more optimization, we can consider alternatives, runtime constants, etc. but whether this is actually necessary is TBD. Suggestions welcome for (micro-)benchmarks that illustrate the perf delta.

Build and boot tested using QEMU with LA57 emulation.

Changes since v4:
- Add patch to fix MAX_PHYSMEM_BITS (and drop an occurrence of pgtable_l5_enabled())
- Re-order the changes and split across more patches so any potential performance hit is bisectable.

Changes since v3:
- Drop asm-offsets patch which has been merged already
- Rebase onto tip/x86/core which now carries some related changes by Kirill
- Avoid adding new instances of '#ifdef CONFIG_X86_5LEVEL' where possible, as it is going to be removed soon
- Move cap override arrays straight to __ro_after_init
- Drop KVM changes entirely - they were wrong and unnecessary
- Drop the new "la57_hw" capability flag for now - we can always add it later if there is a need.

Changes since v2:
- Drop first patch which has been merged
- Rename existing "la57" CPU flag to "la57_hw" and use "la57" to indicate that 5 level paging is being used
- Move memset() out of identify_cpu()
- Make set/clear cap override arrays ro_after_init
- Split off asm-offsets update

[0] https://lore.kernel.org/all/[email protected]/

Cc Ingo Molnar <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Borislav Petkov <[email protected]>

Ard Biesheuvel (7):
x86/mm: Decouple MAX_PHYSMEM_BITS from LA57 state
x86/mm: Use a single cache hot per-CPU variable to record pgdir_shift x86/mm: Define PTRS_PER_P4D in terms of pgdir_shift() x86/mm: Derive pgtable_l5_enabled() from pgdir_shift() x86/boot: Drop USE_EARLY_PGTABLE_L5 definitions
x86/boot: Drop 5-level paging related global variable x86/boot: Remove KASAN workaround for 4/5 level paging switch

 arch/x86/boot/compressed/misc.h         |  8 +++---
 arch/x86/boot/compressed/pgtable_64.c   | 10 --------
 arch/x86/boot/startup/map_kernel.c      | 18 +------------
 arch/x86/boot/startup/sme.c             |  9 -------
 arch/x86/include/asm/page_64_types.h    |  2 +-
 arch/x86/include/asm/pgtable_64_types.h | 27 ++++++++------------
 arch/x86/include/asm/sparsemem.h        |  2 +-
 arch/x86/kernel/alternative.c           | 12 ---------
 arch/x86/kernel/cpu/common.c            |  3 ---
 arch/x86/kernel/head64.c                |  9 -------
 arch/x86/kernel/head_64.S               |  5 ++++
 arch/x86/mm/kasan_init_64.c             |  3 ---
 arch/x86/mm/pgtable.c                   |  4 +++
 13 files changed, 26 insertions(+), 86 deletions(-)

base-commit: 54c2c688cd9305bdbab4883b9da6ff63f4deca5d

Signature · 1 line
2.49.0.1101.gccaa498523-goog