x86/lam: Allocate test page from AREA_LOW instead of AREA_NORMAL
PATCH 1/1 x86/lam: Allocate test page from AREA_LOW instead of AREA_NORMAL
The lam test does not set a guest memory size in x86/unittests.cfg, so QEMU falls back to the default of 128 MiB. AREA_NORMAL starts at PFN BIT(36-12), i.e. physical 64 GiB, which is never initialized in a 128 MiB guest. As a result, alloc_pages_flags(0, AREA_NORMAL) returns NULL and test_lam_user() ends up running its LAM checks against a NULL pointer, which is semantically meaningless even if the metadata-bit arithmetic happens to succeed.
Allocate from AREA_LOW instead. AREA_LOW_PFN is BIT(24-12) (16 MiB), well within a 128 MiB guest, and bits 63..47 of the resulting linear address are still zero, so the LAM48/LAM57 metadata-bit checks remain valid. Update the adjacent comment accordingly.
--- x86/lam.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
x86/lam.c +3 -3
@@ -231,13 +231,13 @@ static void test_lam_user(void)bool has_lam =this_cpu_has (X86_FEATURE_LAM );/* -* The physical address of AREA_NORMAL is within 36 bits, so that using +* The physical address of AREA_LOW is within 36 bits, so that using * identical mapping, the linear address will be considered as user mode * address from the view of LAM, and the metadata bits are not used as * address for both LAM48 and LAM57. */ - vaddr =alloc_pages_flags (0 ,AREA_NORMAL ); -static_assert ((AREA_NORMAL_PFN &GENMASK (63 ,47 )) ==0UL ); + vaddr =alloc_pages_flags (0 ,AREA_LOW ); +static_assert ((AREA_LOW_PFN &GENMASK (63 ,47 )) ==0UL );/* * Note, LAM doesn't have a global control bit to turn on/off LAM
Signature · 1 line
2.54.0
Quoted 5 lines · yexun
The lam test does not set a guest memory size in x86/unittests.cfg, so QEMU falls back to the default of 128 MiB.
AREA_NORMALstarts at PFN BIT(36-12), i.e. physical 64 GiB, which is never initialized in a 128 MiB guest. As a result,alloc_pages_flags(0, AREA_NORMAL)returns NULL
Oops, thanks for catching this.
Quoted 3 lines
and
test_lam_user()ends up running its LAM checks against a NULL pointer, which is semantically meaningless even if the metadata-bit arithmetic happens to succeed.
From the LAM hardware perspective, NULL pointer is treated as a userspace virtual address, so the LAM hardware feature validation still works.
But it's not intended to use a NULL pointer, so it's a bug that needs to be fixed.
Quoted 20 lines
Allocate from
AREA_LOWinstead.AREA_LOW_PFNis BIT(24-12) (16 MiB), well within a 128 MiB guest, and bits 63..47 of the resulting linear address are still zero, so the LAM48/LAM57 metadata-bit checks remain valid. Update the adjacent comment accordingly.Fixes 0164d7595c85 ("x86: Add test cases for LAM_{U48,U57}")Signed-off-by yexun <[email protected]>--- x86/lam.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)x86/lam.c +0
-0@@ -231,13 +231,13 @@ static void test_lam_user(void)bool has_lam =this_cpu_has (X86_FEATURE_LAM );/*
x86/lam.c static void test_lam_user(void) · line 240
Quoted 10 lines
x86/lam.c +3
-3-* The physical address of AREA_NORMAL is within 36 bits, so that using +* The physical address of AREA_LOW is within 36 bits, so that using * identical mapping, the linear address will be considered as user mode * address from the view of LAM, and the metadata bits are not used as * address for both LAM48 and LAM57. */ - vaddr =alloc_pages_flags (0 ,AREA_NORMAL ); -static_assert ((AREA_NORMAL_PFN &GENMASK (63 ,47 )) ==0UL ); + vaddr =alloc_pages_flags (0 ,AREA_LOW ); +static_assert ((AREA_LOW_PFN &GENMASK (63 ,47 )) ==0UL );
Although the error exists in the original code, the GENMASK(63, 47) is an address mask, it's not right to mask a PFN.
Quoted 3 lines
x86/lam.c +0
-0/* * Note, LAM doesn't have a global control bit to turn on/off LAM
Quoted 8 lines · Binbin Wu
Quoted 7 lines · yexun
x86/lam.c +0
-0@@ -231,13 +231,13 @@ static void test_lam_user(void)bool has_lam =this_cpu_has (X86_FEATURE_LAM );
Quoted 14 lines
x86/lam.c static void test_lam_user(void) · line 240
Quoted 11 lines
x86/lam.c +3
-3/* -* The physical address of AREA_NORMAL is within 36 bits, so that using +* The physical address of AREA_LOW is within 36 bits, so that using * identical mapping, the linear address will be considered as user mode * address from the view of LAM, and the metadata bits are not used as * address for both LAM48 and LAM57. */ - vaddr =alloc_pages_flags (0 ,AREA_NORMAL ); -static_assert ((AREA_NORMAL_PFN &GENMASK (63 ,47 )) ==0UL ); + vaddr =alloc_pages_flags (0 ,AREA_LOW ); +static_assert ((AREA_LOW_PFN &GENMASK (63 ,47 )) ==0UL );Although the error exists in the original code, the
GENMASK(63, 47)is an address mask, it's not right to mask a PFN.
Thanks for your review. What about asserting '(vaddr & GENMASK(63, 47)) == 0UL'?
Quoted 24 lines · yexun
Quoted 8 lines · Binbin Wu
Quoted 7 lines · yexun
x86/lam.c +0
-0@@ -231,13 +231,13 @@ static void test_lam_user(void) bool has_lam = this_cpu_has(X86_FEATURE_LAM);Quoted 14 lines
x86/lam.c static void test_lam_user(void) · line 240
Quoted 11 lines
x86/lam.c +3
-3/* - * The physical address of AREA_NORMAL is within 36 bits, so that using + * The physical address of AREA_LOW is within 36 bits, so that using * identical mapping, the linear address will be considered as user mode * address from the view of LAM, and the metadata bits are not used as * address for both LAM48 and LAM57. */ - vaddr = alloc_pages_flags(0, AREA_NORMAL); - static_assert((AREA_NORMAL_PFN & GENMASK(63, 47)) == 0UL); + vaddr = alloc_pages_flags(0, AREA_LOW); + static_assert((AREA_LOW_PFN & GENMASK(63, 47)) == 0UL);Although the error exists in the original code, the
GENMASK(63, 47)is an address mask, it's not right to mask a PFN.
Quoted 1 line
Thanks for your review. What about asserting '(vaddr &
GENMASK(63, 47)) == 0UL'?
LGTM.