mm: kasan: skip KASAN for GFP_HIGHUSER
Subsystems (10)
v2 cover mm: kasan: skip KASAN for GFP_HIGHUSER
Hi all,
This series moves __GFP_SKIP_KASAN from GFP_HIGHUSER_MOVABLE to GFP_HIGHUSER (patch 1) and adds a KUnit test for __GFP_SKIP_KASAN (patch 2).
GFP_HIGHUSER_MOVABLE already skips KASAN tagging under CONFIG_KASAN_HW_TAGS, whereas GFP_HIGHUSER does not despite sharing the same userspace-allocation contract. On an Armv9.2-A device (6.12 kernel, kasan.mode=sync), skipping KASAN on GFP_HIGHUSER cuts 32 MB /dev/dma_heap/system alloc+free latency by ~80% and improves an app-loading OpenGL benchmark by 15-23%. As a side effect, GFP_HIGHUSER shmem mappings (such as drm_gem_shmem_helper / panthor [1, 2]) stop hitting the swap-in readahead PROT_MTE tag mismatch, though the general readahead issue for other gfp masks remains separate.
Changes since v1 [3]:
- Dropped GFP_HIGHUSER_KASAN, the pipe/FUSE/kexec/RDS conversions, and page_alloc_highuser_kasan (v1 patches 1-4 and 7) following David Hildenbrand's and Tomasz Figa's feedback; kept those callers on GFP_HIGHUSER (2 patches instead of 7).
- Rewrote patch 1's commit message concisely (Lorenzo Stoakes).
- Dropped Assisted-by: trailers from the patches, keeping tool disclosure here (Lorenzo Stoakes).
- Rebased onto mm-unstable (d12eccbcb79a).
- Kept the fs/kexec/RDS maintainers from v1 on Cc for visibility since those patches were dropped.
Testing: built and booted both commits on arm64 with CONFIG_KASAN_HW_TAGS under QEMU (virt,mte=on) across kasan.mode=sync, async, and asymm (9/9 runs passed for page_alloc_skip_kasan; 3/3 failed as expected when patch 1 was reverted, with the next test still passing). Verified W=1 builds with 0 warnings across arm64 (HW_TAGS, generic, SW_TAGS, no KASAN) and x86_64 (generic, no KASAN). KUnit tests were not run on real hardware.
Tooling: an AI coding assistant was used under the author's direction to audit GFP_HIGHUSER callers, draft the KUnit test helper and changelog revisions, and run the QEMU/KUnit build and test matrix.
Jeff Xu (2):
mm: kasan: skip KASAN for GFP_HIGHUSER
mm/kasan/kunit: add page_alloc_skip_kasan test
include/linux/gfp_types.h | 4 ++-- mm/kasan/kasan_test_c.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-)
base-commit: d12eccbcb79a4fa35c3aa588eb79056af62eb859
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH v2 1/2 mm: kasan: skip KASAN for GFP_HIGHUSER
Move __GFP_SKIP_KASAN from GFP_HIGHUSER_MOVABLE to GFP_HIGHUSER so non-movable userspace page allocations also skip KASAN tagging on allocation and poisoning on free under CONFIG_KASAN_HW_TAGS.
GFP_HIGHUSER_MOVABLE already skips KASAN because user pages do not hold kernel data structures and tagging every 16-byte MTE granule on allocation and free is expensive. GFP_HIGHUSER has the same contract and differs only in being non-movable, used mainly by DMA-BUF, DRM/GPU buffers, and non-movable HugeTLB.
On an Armv9.2-A device (6.12 kernel, kasan.mode=sync), this reduces 32 MB /dev/dma_heap/system alloc+free latency by ~80% (~59% alloc, ~98% close) and improves an app-loading OpenGL benchmark by 15-23%.
Callers that access GFP_HIGHUSER pages through kmap_local_page() or page_address() (such as ramfs, pipe/FUSE buffers, kexec, and RDS) lose HW_TAGS checking on those pages. None of them store kernel structs there; this matches the trade-off already made for GFP_HIGHUSER_MOVABLE. Generic and SW_TAGS KASAN are unaffected.
--- include/linux/gfp_types.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/gfp_types.h +2 -2
@@ -376,8 +376,8 @@ enum {#define GFP_USER (__GFP_RECLAIM | __GFP_IO | __GFP_FS | __GFP_HARDWALL)#define GFP_DMA __GFP_DMA#define GFP_DMA32 __GFP_DMA32 -#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM) -#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE | __GFP_SKIP_KASAN) +#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM | __GFP_SKIP_KASAN) +#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE)#define GFP_TRANSHUGE_LIGHT ((GFP_HIGHUSER_MOVABLE | __GFP_COMP | \ __GFP_NOMEMALLOC | __GFP_NOWARN) & ~__GFP_RECLAIM)#define GFP_TRANSHUGE (GFP_TRANSHUGE_LIGHT | __GFP_DIRECT_RECLAIM)
base-commit: d12eccbcb79a4fa35c3aa588eb79056af62eb859
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Quoted 14 lines · Jeff Xu
Move
__GFP_SKIP_KASANfromGFP_HIGHUSER_MOVABLEtoGFP_HIGHUSERso non-movable userspace page allocations also skip KASAN tagging on allocation and poisoning on free underCONFIG_KASAN_HW_TAGS.
GFP_HIGHUSER_MOVABLEalready skips KASAN because user pages do not hold kernel data structures and tagging every 16-byte MTE granule on allocation and free is expensive.GFP_HIGHUSERhas the same contract and differs only in being non-movable, used mainly by DMA-BUF, DRM/GPU buffers, and non-movable HugeTLB.On an Armv9.2-A device (6.12 kernel, kasan.mode=sync), this reduces 32 MB
/dev/dma_heap/systemalloc+free latency by ~80% (~59% alloc, ~98% close) and improves an app-loading OpenGL benchmark by 15-23%.
Quoted 7 lines
Callers that access
GFP_HIGHUSERpages throughkmap_local_page()orpage_address()(such as ramfs, pipe/FUSE buffers, kexec, and RDS) loseHW_TAGSchecking on those pages. None of them store kernel structs there; this matches the trade-off already made forGFP_HIGHUSER_MOVABLE. Generic andSW_TAGSKASAN are unaffected.Signed-off-by Jeff Xu <[email protected]>
LGTM; I agree with your assessment of the risk.
We have seen poisoning pages at free-time being a performance bottleneck because the tags are kept inline with data in the cache, and setting the tags therefore causes hot data to be evicted to bring in the cold data just to set the tags. We had been experimenting with the concept of "elide poison on free" for "in-kernel MTE enabled in production" use cases to solve this. But the security analysis is a bit complex.
I anticipate that your change is probably solving most of the same performance cost in a much more straightforward manner. I'm guessing these DMA/DRM/GPU buffers are the largest in-kernel memory allocators for the Android app start use case.
Quoted 21 lines
--- include/linux/gfp_types.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)include/linux/gfp_types.h +2
-2@@ -376,8 +376,8 @@ enum {#define GFP_USER (__GFP_RECLAIM | __GFP_IO | __GFP_FS | __GFP_HARDWALL)#define GFP_DMA __GFP_DMA#define GFP_DMA32 __GFP_DMA32 -#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM) -#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE | __GFP_SKIP_KASAN) +#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM | __GFP_SKIP_KASAN) +#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE)#define GFP_TRANSHUGE_LIGHT ((GFP_HIGHUSER_MOVABLE | __GFP_COMP | \ __GFP_NOMEMALLOC | __GFP_NOWARN) & ~__GFP_RECLAIM)#define GFP_TRANSHUGE (GFP_TRANSHUGE_LIGHT | __GFP_DIRECT_RECLAIM)base-commit:
d12eccbcb79a4fa35c3aa588eb79056af62eb859
David Hildenbrand (Arm)
maintainerQuoted 30 lines · Jeff Xu
Move
__GFP_SKIP_KASANfromGFP_HIGHUSER_MOVABLEtoGFP_HIGHUSERso non-movable userspace page allocations also skip KASAN tagging on allocation and poisoning on free underCONFIG_KASAN_HW_TAGS.
GFP_HIGHUSER_MOVABLEalready skips KASAN because user pages do not hold kernel data structures and tagging every 16-byte MTE granule on allocation and free is expensive.GFP_HIGHUSERhas the same contract and differs only in being non-movable, used mainly by DMA-BUF, DRM/GPU buffers, and non-movable HugeTLB.On an Armv9.2-A device (6.12 kernel, kasan.mode=sync), this reduces 32 MB
/dev/dma_heap/systemalloc+free latency by ~80% (~59% alloc, ~98% close) and improves an app-loading OpenGL benchmark by 15-23%.Callers that access
GFP_HIGHUSERpages throughkmap_local_page()orpage_address()(such as ramfs, pipe/FUSE buffers, kexec, and RDS) loseHW_TAGSchecking on those pages. None of them store kernel structs there; this matches the trade-off already made forGFP_HIGHUSER_MOVABLE. Generic andSW_TAGSKASAN are unaffected.Signed-off-by Jeff Xu <[email protected]>--- include/linux/gfp_types.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)include/linux/gfp_types.h +0
-0
include/linux/gfp_types.h enum { · line 380
Quoted 7 lines
include/linux/gfp_types.h +2
-2@@ -376,8 +376,8 @@ enum {#define GFP_USER (__GFP_RECLAIM | __GFP_IO | __GFP_FS | __GFP_HARDWALL)#define GFP_DMA __GFP_DMA#define GFP_DMA32 __GFP_DMA32 -#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM) -#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE | __GFP_SKIP_KASAN) +#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM | __GFP_SKIP_KASAN) +#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE)
Can we please add a big fat comment above GFP_HIGHUSER, explaining why we use __GFP_SKIP_KASAN and which security impact this has?
Signature · 3 lines
Cheers, David
Quoted 8 lines · David Hildenbrand (Arm)
include/linux/gfp_types.h enum { · line 380
Quoted 4 lines · Jeff Xu
include/linux/gfp_types.h +2
-2-#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM) -#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE | __GFP_SKIP_KASAN) +#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM | __GFP_SKIP_KASAN) +#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE)Can we please add a big fat comment above
GFP_HIGHUSER, explaining why we use__GFP_SKIP_KASANand which security impact this has?
Sure, I'll add a comment above GFP_HIGHUSER in v3:
/*
* GFP_HIGHUSER (and GFP_HIGHUSER_MOVABLE derived from it) sets
* __GFP_SKIP_KASAN to skip HW_TAGS tagging on allocation and poisoning on
* free. These pages are meant for userspace data (e.g. anonymous and page
* cache pages, DMA-BUF, GPU/DRM buffers) rather than kernel data structures,
* and tagging every granule on allocation and free is expensive. *
* Security impact: under CONFIG_KASAN_HW_TAGS, these pages carry the
* match-all tag (0xff), so kernel accesses via page_address() or
* kmap_local_page() lose HW_TAGS out-of-bounds and use-after-free
* detection. Callers that store kernel data structures in these pages
* should not use GFP_HIGHUSER. Generic and SW_TAGS KASAN are unaffected. */
Thanks,
Jeff
David Hildenbrand (Arm)
maintainerQuoted 12 lines · Jeff Xu
Quoted 8 lines · David Hildenbrand (Arm)
include/linux/gfp_types.h enum { · line 380
Quoted 4 lines · Jeff Xu
include/linux/gfp_types.h +2
-2-#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM) -#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE | __GFP_SKIP_KASAN) +#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM | __GFP_SKIP_KASAN) +#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE)Can we please add a big fat comment above
GFP_HIGHUSER, explaining why we use__GFP_SKIP_KASANand which security impact this has?Sure, I'll add a comment above
GFP_HIGHUSERin v3:
Quoted 13 lines
/*
*GFP_HIGHUSER(andGFP_HIGHUSER_MOVABLEderived from it) sets
*__GFP_SKIP_KASANto skipHW_TAGStagging on allocation and poisoning on
* free. These pages are meant for userspace data (e.g. anonymous and page
* cache pages, DMA-BUF, GPU/DRM buffers) rather than kernel data structures,
* and tagging every granule on allocation and free is expensive. *
* Security impact: underCONFIG_KASAN_HW_TAGS, these pages carry the
* match-all tag (0xff), so kernel accesses viapage_address()or
*kmap_local_page()loseHW_TAGSout-of-bounds and use-after-free
* detection. Callers that store kernel data structures in these pages
* should not useGFP_HIGHUSER. Generic andSW_TAGSKASAN are unaffected. */
Yeah, that sounds good to me. But let's wait for some more review.
Signature · 3 lines
Cheers, David
Andrey Ryabinin
maintainerQuoted 10 lines · Jeff Xu
Quoted 8 lines · David Hildenbrand (Arm)
include/linux/gfp_types.h enum { · line 380
Quoted 4 lines · Jeff Xu
include/linux/gfp_types.h +2
-2-#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM) -#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE | __GFP_SKIP_KASAN) +#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM | __GFP_SKIP_KASAN) +#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE)Can we please add a big fat comment above
GFP_HIGHUSER, explaining why we use__GFP_SKIP_KASANand which security impact this has?
Quoted 8 lines
Sure, I'll add a comment above
GFP_HIGHUSERin v3:/*
*GFP_HIGHUSER(andGFP_HIGHUSER_MOVABLEderived from it) sets
*__GFP_SKIP_KASANto skipHW_TAGStagging on allocation and poisoning on
* free. These pages are meant for userspace data (e.g. anonymous and page
* cache pages, DMA-BUF, GPU/DRM buffers) rather than kernel data structures,
* and tagging every granule on allocation and free is expensive.
I think the comment should say that pages which user space
can map with PROT_MTE must not have a KASAN tag, and that __GFP_SKIP_KASAN is what guarantees it for these allocations.
Quoted 10 lines
*
* Security impact: underCONFIG_KASAN_HW_TAGS, these pages carry the
* match-all tag (0xff), so kernel accesses viapage_address()or
*kmap_local_page()loseHW_TAGSout-of-bounds and use-after-free
* detection. Callers that store kernel data structures in these pages
* should not useGFP_HIGHUSER. Generic andSW_TAGSKASAN are unaffected. */Thanks,
Jeff
Quoted 9 lines · Andrey Ryabinin
Quoted 6 lines · Jeff Xu
/*
*GFP_HIGHUSER(andGFP_HIGHUSER_MOVABLEderived from it) sets
*__GFP_SKIP_KASANto skipHW_TAGStagging on allocation and poisoning on
* free. These pages are meant for userspace data (e.g. anonymous and page
* cache pages, DMA-BUF, GPU/DRM buffers) rather than kernel data structures,
* and tagging every granule on allocation and free is expensive.
Quoted 3 lines
I think the comment should say that pages which user space
can map withPROT_MTEmust not have a KASAN tag, and that__GFP_SKIP_KASANis what guarantees it for these allocations.
Makes sense. How does this updated comment look for v3?
/*
* GFP_HIGHUSER (and GFP_HIGHUSER_MOVABLE derived from it) sets
* __GFP_SKIP_KASAN to skip HW_TAGS tagging on allocation and poisoning on
* free. Pages that userspace can map with PROT_MTE (such as anonymous,
* shmem, and HugeTLB pages) must not carry an in-kernel KASAN tag,
* otherwise userspace tags will mismatch page->flags and cause KASAN false
* positives; __GFP_SKIP_KASAN guarantees the match-all tag (0xff) for these
* allocations. Skipping tagging also avoids expensive per-granule MTE
* tagging on large userspace buffers (e.g. DMA-BUF and GPU/DRM buffers). *
* Security impact: under CONFIG_KASAN_HW_TAGS, kernel accesses to these
* pages via page_address() or kmap_local_page() use the match-all tag
* (0xff) and lose HW_TAGS out-of-bounds and use-after-free detection.
* Callers that store kernel data structures in these pages should not use
* GFP_HIGHUSER. Generic and SW_TAGS KASAN are unaffected.
*/
Thanks,
Jeff
Andrey Ryabinin
maintainerQuoted 10 lines · Jeff Xu
Move
__GFP_SKIP_KASANfromGFP_HIGHUSER_MOVABLEtoGFP_HIGHUSERso non-movable userspace page allocations also skip KASAN tagging on allocation and poisoning on free underCONFIG_KASAN_HW_TAGS.
GFP_HIGHUSER_MOVABLEalready skips KASAN because user pages do not hold kernel data structures and tagging every 16-byte MTE granule on allocation and free is expensive.GFP_HIGHUSERhas the same contract and differs only in being non-movable, used mainly by DMA-BUF, DRM/GPU buffers, and non-movable HugeTLB.
Quoted 4 lines
On an Armv9.2-A device (6.12 kernel, kasan.mode=sync), this reduces 32 MB
/dev/dma_heap/systemalloc+free latency by ~80% (~59% alloc, ~98% close) and improves an app-loading OpenGL benchmark by 15-23%.
The changelog describes this as a performance change, however for at least the HugeTLB allocated with GFP_HIGHUSER case this looks like a fix for a false positive.
Since the 25c17c4b55de ("hugetlb: arm64: add mte support") can be mapped with PROT_MTE.
Anything that can be mapped with userspace tags shouldn't have in-kernel KASAN tags,
otherwise it would lead to false positive.
Quoted 4 lines
Callers that access
GFP_HIGHUSERpages throughkmap_local_page()orpage_address()(such as ramfs, pipe/FUSE buffers, kexec, and RDS) loseHW_TAGSchecking on those pages. None of them store kernel structs there; this matches the trade-off already made forGFP_HIGHUSER_MOVABLE.
I think the main reason GFP_HIGHUSER_MOVABLE skips KASAN tags today is that is because userspace might use tags and has nothing to do with not storing kernel structs there.
Quoted 26 lines
Generic and
SW_TAGSKASAN are unaffected.Signed-off-by Jeff Xu <[email protected]>--- include/linux/gfp_types.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)include/linux/gfp_types.h +2
-2@@ -376,8 +376,8 @@ enum {#define GFP_USER (__GFP_RECLAIM | __GFP_IO | __GFP_FS | __GFP_HARDWALL)#define GFP_DMA __GFP_DMA#define GFP_DMA32 __GFP_DMA32 -#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM) -#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE | __GFP_SKIP_KASAN) +#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM | __GFP_SKIP_KASAN) +#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE)#define GFP_TRANSHUGE_LIGHT ((GFP_HIGHUSER_MOVABLE | __GFP_COMP | \ __GFP_NOMEMALLOC | __GFP_NOWARN) & ~__GFP_RECLAIM)#define GFP_TRANSHUGE (GFP_TRANSHUGE_LIGHT | __GFP_DIRECT_RECLAIM)base-commit:
d12eccbcb79a4fa35c3aa588eb79056af62eb859--
2.56.1.750.gf70dc682cf-goog
Quoted 11 lines · Andrey Ryabinin
Quoted 4 lines · Jeff Xu
On an Armv9.2-A device (6.12 kernel, kasan.mode=sync), this reduces 32 MB
/dev/dma_heap/systemalloc+free latency by ~80% (~59% alloc, ~98% close) and improves an app-loading OpenGL benchmark by 15-23%.The changelog describes this as a performance change, however for at least the HugeTLB allocated with
GFP_HIGHUSERcase this looks like a fix for a false positive.
Quoted 5 lines
Since the
25c17c4b55de("hugetlb: arm64: add mte support") can be mapped withPROT_MTE.
Anything that can be mapped with userspace tags shouldn't have in-kernel KASAN tags,
otherwise it would lead to false positive.
Good catch, I agree with you. I'll update the commit message in v3 to describe this PROT_MTE false positive fix (referencing 25c17c4b55de) alongside the DMA-BUF/GPU performance numbers.
Should I also add Fixes: 25c17c4b55de ("hugetlb: arm64: add mte support") for stable backporting?
Quoted 8 lines
Quoted 4 lines
Callers that access
GFP_HIGHUSERpages throughkmap_local_page()orpage_address()(such as ramfs, pipe/FUSE buffers, kexec, and RDS) loseHW_TAGSchecking on those pages. None of them store kernel structs there; this matches the trade-off already made forGFP_HIGHUSER_MOVABLE.I think the main reason
GFP_HIGHUSER_MOVABLEskips KASAN tags today is that is because userspace might use tags and has nothing to do with not storing kernel structs there.
Fair point. I'll reword the commit message in v3 so the PROT_MTE requirement is stated as the main reason, with the per-granule tagging overhead as the additional performance benefit.
Thanks,
Jeff
PATCH v2 2/2 mm/kasan/kunit: add page_alloc_skip_kasan test
In reply to Jeff XuAdd a KUnit test (page_alloc_skip_kasan) to verify that page allocations using __GFP_SKIP_KASAN (GFP_KERNEL | __GFP_SKIP_KASAN, GFP_HIGHUSER, and GFP_HIGHUSER_MOVABLE) under CONFIG_KASAN_HW_TAGS:
1. Reset page_kasan_tag() to KASAN_TAG_KERNEL (0xff) across all sub-pages of a high-order allocation, and
2. Return a match-all (0xff) linear-map pointer from page_address() that does not trigger a KASAN fault even when the underlying physical granules carry a mismatched tag (such as KASAN_TAG_INVALID or restored userspace PROT_MTE tags).
Tested on arm64 with CONFIG_KASAN_HW_TAGS under QEMU (virt,mte=on) with kasan.mode=sync, async and asymm.
--- mm/kasan/kasan_test_c.c | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)
mm/kasan/kasan_test_c.c +44 -0
@@ -380,6 +380,49 @@ static void page_alloc_uaf(struct kunit *test)KUNIT_EXPECT_KASAN_FAIL_READ (test, ((volatile char *)ptr)[0 ]); } +static void page_alloc_skip_kasan_helper (struct kunit *test,gfp_t gfp) +{ +struct page *pages; +size_t order =2 ; +char *ptr; +size_t i; + + pages =alloc_pages (gfp, order); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, pages); + +for (i =0 ; i < (1UL << order); i++) { +/* +* __GFP_SKIP_KASAN allocations must carry the match-all tag +* in page->flags, and so in page_address(), not a random tag. +*/ +KUNIT_EXPECT_EQ (test,page_kasan_tag (pages + i), + (u8 )KASAN_TAG_KERNEL ); + + ptr =page_address (pages + i); +KUNIT_EXPECT_EQ (test, (u8 )get_tag (ptr), (u8 )KASAN_TAG_KERNEL ); + +/* +* The memory tags of such pages are arbitrary: stale poison, +* or tags set by userspace through PROT_MTE. Accesses via +* page_address() must not fault regardless. +*/ +kasan_poison (ptr,PAGE_SIZE ,KASAN_TAG_INVALID , false); +KUNIT_EXPECT_KASAN_RESULT (test,WRITE_ONCE (*ptr,0 ), +"WRITE_ONCE(*ptr, 0)" , false); + } + +__free_pages (pages, order); +} + +static void page_alloc_skip_kasan (struct kunit *test) +{ +KASAN_TEST_NEEDS_CONFIG_ON (test,CONFIG_KASAN_HW_TAGS ); + +page_alloc_skip_kasan_helper (test,GFP_KERNEL | __GFP_SKIP_KASAN); +page_alloc_skip_kasan_helper (test,GFP_HIGHUSER ); +page_alloc_skip_kasan_helper (test,GFP_HIGHUSER_MOVABLE ); +} +static void krealloc_more_oob_helper (struct kunit *test,size_t size1,size_t size2) {
@@ -2369,6 +2412,7 @@ static struct kunit_case kasan_kunit_test_cases[] = {KUNIT_CASE (kmalloc_large_invalid_free),KUNIT_CASE (page_alloc_oob_right),KUNIT_CASE (page_alloc_uaf), +KUNIT_CASE (page_alloc_skip_kasan),KUNIT_CASE (krealloc_more_oob),KUNIT_CASE (krealloc_less_oob), KUNIT_CASE(krealloc_large_more_oob),
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Re: [PATCH v2 2/2] mm/kasan/kunit: add page_alloc_skip_kasan test
Quoted 11 lines · Jeff Xu
Add a KUnit test (
page_alloc_skip_kasan) to verify that page allocations using__GFP_SKIP_KASAN(GFP_KERNEL|__GFP_SKIP_KASAN,GFP_HIGHUSER, andGFP_HIGHUSER_MOVABLE) underCONFIG_KASAN_HW_TAGS:1. Reset
page_kasan_tag()toKASAN_TAG_KERNEL(0xff) across all sub-pages of a high-order allocation, and
2. Return a match-all (0xff) linear-map pointer frompage_address()that does not trigger a KASAN fault even when the underlying physical granules carry a mismatched tag (such asKASAN_TAG_INVALIDor restored userspacePROT_MTEtags).
Quoted 4 lines
Tested on arm64 with
CONFIG_KASAN_HW_TAGSunder QEMU (virt,mte=on) with kasan.mode=sync, async and asymm.Signed-off-by Jeff Xu <[email protected]>
Quoted 66 lines
--- mm/kasan/kasan_test_c.c | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)mm/kasan/kasan_test_c.c +44
-0@@ -380,6 +380,49 @@ static void page_alloc_uaf(struct kunit *test)KUNIT_EXPECT_KASAN_FAIL_READ (test, ((volatile char *)ptr)[0 ]); } +static void page_alloc_skip_kasan_helper (struct kunit *test,gfp_t gfp) +{ +struct page *pages; +size_t order =2 ; +char *ptr; +size_t i; + + pages =alloc_pages (gfp, order); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, pages); + +for (i =0 ; i < (1UL << order); i++) { +/* +* __GFP_SKIP_KASAN allocations must carry the match-all tag +* in page->flags, and so in page_address(), not a random tag. +*/ +KUNIT_EXPECT_EQ (test,page_kasan_tag (pages + i), + (u8 )KASAN_TAG_KERNEL ); + + ptr =page_address (pages + i); +KUNIT_EXPECT_EQ (test, (u8 )get_tag (ptr), (u8 )KASAN_TAG_KERNEL ); + +/* +* The memory tags of such pages are arbitrary: stale poison, +* or tags set by userspace through PROT_MTE. Accesses via +* page_address() must not fault regardless. +*/ +kasan_poison (ptr,PAGE_SIZE ,KASAN_TAG_INVALID , false); +KUNIT_EXPECT_KASAN_RESULT (test,WRITE_ONCE (*ptr,0 ), +"WRITE_ONCE(*ptr, 0)" , false); + } + +__free_pages (pages, order); +} + +static void page_alloc_skip_kasan (struct kunit *test) +{ +KASAN_TEST_NEEDS_CONFIG_ON (test,CONFIG_KASAN_HW_TAGS ); + +page_alloc_skip_kasan_helper (test,GFP_KERNEL | __GFP_SKIP_KASAN); +page_alloc_skip_kasan_helper (test,GFP_HIGHUSER ); +page_alloc_skip_kasan_helper (test,GFP_HIGHUSER_MOVABLE ); +} +static void krealloc_more_oob_helper (struct kunit *test,size_t size1,size_t size2) {@@ -2369,6 +2412,7 @@ static struct kunit_case kasan_kunit_test_cases[] = {KUNIT_CASE (kmalloc_large_invalid_free),KUNIT_CASE (page_alloc_oob_right),KUNIT_CASE (page_alloc_uaf), +KUNIT_CASE (page_alloc_skip_kasan),KUNIT_CASE (krealloc_more_oob),KUNIT_CASE (krealloc_less_oob), KUNIT_CASE(krealloc_large_more_oob),
David Hildenbrand (Arm)
maintainerRe: [PATCH v2 2/2] mm/kasan/kunit: add page_alloc_skip_kasan test
In reply to Jeff XuQuoted 24 lines · Jeff Xu
Add a KUnit test (
page_alloc_skip_kasan) to verify that page allocations using__GFP_SKIP_KASAN(GFP_KERNEL|__GFP_SKIP_KASAN,GFP_HIGHUSER, andGFP_HIGHUSER_MOVABLE) underCONFIG_KASAN_HW_TAGS:1. Reset
page_kasan_tag()toKASAN_TAG_KERNEL(0xff) across all sub-pages of a high-order allocation, and
2. Return a match-all (0xff) linear-map pointer frompage_address()that does not trigger a KASAN fault even when the underlying physical granules carry a mismatched tag (such asKASAN_TAG_INVALIDor restored userspacePROT_MTEtags).Tested on arm64 with
CONFIG_KASAN_HW_TAGSunder QEMU (virt,mte=on) with kasan.mode=sync, async and asymm.Signed-off-by Jeff Xu <[email protected]>--- mm/kasan/kasan_test_c.c | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)mm/kasan/kasan_test_c.c +0
-0
mm/kasan/kasan_test_c.c static void page_alloc_uaf(struct kunit *test) · line 386
Quoted 7 lines
mm/kasan/kasan_test_c.c +4
-0@@ -380,6 +380,49 @@ static void page_alloc_uaf(struct kunit *test)KUNIT_EXPECT_KASAN_FAIL_READ (test, ((volatile char *)ptr)[0 ]); } +static void page_alloc_skip_kasan_helper (struct kunit *test,gfp_t gfp) +{ +struct page *pages; +size_t order =2 ;
Why magic value two?
Should be const.
mm/kasan/kasan_test_c.c static void page_alloc_uaf(struct kunit *test) · line 393
Quoted 7 lines
mm/kasan/kasan_test_c.c +7
-0+char *ptr; +size_t i; + + pages =alloc_pages (gfp, order); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, pages); + +for (i =0 ; i < (1UL << order); i++) {
"1 << order" is sufficient.
mm/kasan/kasan_test_c.c static void page_alloc_uaf(struct kunit *test) · line 399
Quoted 6 lines
mm/kasan/kasan_test_c.c +6
-0+/* +* __GFP_SKIP_KASAN allocations must carry the match-all tag +* in page->flags, and so in page_address(), not a random tag. +*/ +KUNIT_EXPECT_EQ (test,page_kasan_tag (pages + i), + (u8 )KASAN_TAG_KERNEL );
Squeeze this into a single line.
mm/kasan/kasan_test_c.c static void page_alloc_uaf(struct kunit *test) · line 411
Quoted 12 lines
mm/kasan/kasan_test_c.c +12
-0+ + ptr =page_address (pages + i); +KUNIT_EXPECT_EQ (test, (u8 )get_tag (ptr), (u8 )KASAN_TAG_KERNEL ); + +/* +* The memory tags of such pages are arbitrary: stale poison, +* or tags set by userspace through PROT_MTE. Accesses via +* page_address() must not fault regardless. +*/ +kasan_poison (ptr,PAGE_SIZE ,KASAN_TAG_INVALID , false); +KUNIT_EXPECT_KASAN_RESULT (test,WRITE_ONCE (*ptr,0 ), +"WRITE_ONCE(*ptr, 0)" , false);
Why do we care about checking HW behavior? (match-all working)
Quoted 18 lines
mm/kasan/kasan_test_c.c +14
-0+ } + +__free_pages (pages, order); +} + +static void page_alloc_skip_kasan (struct kunit *test) +{ +KASAN_TEST_NEEDS_CONFIG_ON (test,CONFIG_KASAN_HW_TAGS ); + +page_alloc_skip_kasan_helper (test,GFP_KERNEL | __GFP_SKIP_KASAN); +page_alloc_skip_kasan_helper (test,GFP_HIGHUSER ); +page_alloc_skip_kasan_helper (test,GFP_HIGHUSER_MOVABLE ); +} +static void krealloc_more_oob_helper (struct kunit *test,size_t size1,size_t size2) {
mm/kasan/kasan_test_c.c static struct kunit_case kasan_kunit_test_cases[] = { · line 2415
Quoted 4 lines
mm/kasan/kasan_test_c.c +1
-0@@ -2369,6 +2412,7 @@ static struct kunit_case kasan_kunit_test_cases[] = {KUNIT_CASE (kmalloc_large_invalid_free),KUNIT_CASE (page_alloc_oob_right),KUNIT_CASE (page_alloc_uaf), + KUNIT_CASE(page_alloc_skip_kasan),
Don't we have to limit this case to having actual MTE-capable HW etc?
Quoted 3 lines
mm/kasan/kasan_test_c.c +0
-0KUNIT_CASE (krealloc_more_oob),KUNIT_CASE (krealloc_less_oob), KUNIT_CASE(krealloc_large_more_oob),
Signature · 3 lines
Cheers, David
Re: [PATCH v2 2/2] mm/kasan/kunit: add page_alloc_skip_kasan test
Quoted 9 lines · David Hildenbrand (Arm)
mm/kasan/kasan_test_c.c static void page_alloc_uaf(struct kunit *test) · line 386
Quoted 4 lines · Jeff Xu
mm/kasan/kasan_test_c.c +4
-0+static void page_alloc_skip_kasan_helper (struct kunit *test,gfp_t gfp) +{ +struct page *pages; +size_t order =2 ;Why magic value two?
Should be const.
Just a small non-zero order so the loop checks tag resets on sub-pages as well as the head page. I'll change it to "const unsigned int order = 2;" and add a brief comment explaining that in v3.
Quoted 9 lines
mm/kasan/kasan_test_c.c static void page_alloc_uaf(struct kunit *test) · line 393
Quoted 7 lines
mm/kasan/kasan_test_c.c +7
-0+char *ptr; +size_t i; + + pages =alloc_pages (gfp, order); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, pages); + +for (i =0 ; i < (1UL << order); i++) {"1 << order" is sufficient.
Will change to "1 << order" in v3.
Quoted 8 lines
mm/kasan/kasan_test_c.c static void page_alloc_uaf(struct kunit *test) · line 399
Quoted 6 lines
mm/kasan/kasan_test_c.c +6
-0+/* +* __GFP_SKIP_KASAN allocations must carry the match-all tag +* in page->flags, and so in page_address(), not a random tag. +*/ +KUNIT_EXPECT_EQ (test,page_kasan_tag (pages + i), + (u8 )KASAN_TAG_KERNEL );Squeeze this into a single line.
Will do in v3.
Quoted 6 lines
Quoted 6 lines
mm/kasan/kasan_test_c.c +6
-0+ + ptr =page_address (pages + i); +KUNIT_EXPECT_EQ (test, (u8 )get_tag (ptr), (u8 )KASAN_TAG_KERNEL ); + +/* +* The memory tags of such pages are arbitrary: stale poison,
Quoted 8 lines
mm/kasan/kasan_test_c.c static void page_alloc_uaf(struct kunit *test) · line 411
Quoted 6 lines
mm/kasan/kasan_test_c.c +6
-0+* or tags set by userspace through PROT_MTE. Accesses via +* page_address() must not fault regardless. +*/ +kasan_poison (ptr,PAGE_SIZE ,KASAN_TAG_INVALID , false); +KUNIT_EXPECT_KASAN_RESULT (test,WRITE_ONCE (*ptr,0 ), +"WRITE_ONCE(*ptr, 0)" , false);Why do we care about checking HW behavior? (match-all working)
Good point, match_all_ptr_tag() already tests match-all behavior in hardware. I'll drop the kasan_poison() and KUNIT_EXPECT_KASAN_RESULT() checks in v3. That also lets me drop the local ptr variable.
Quoted 7 lines
mm/kasan/kasan_test_c.c static struct kunit_case kasan_kunit_test_cases[] = { · line 2415
Quoted 5 lines
mm/kasan/kasan_test_c.c +1
-0@@ -2369,6 +2412,7 @@ static struct kunit_case kasan_kunit_test_cases[] = {KUNIT_CASE (kmalloc_large_invalid_free),KUNIT_CASE (page_alloc_oob_right),KUNIT_CASE (page_alloc_uaf), + KUNIT_CASE(page_alloc_skip_kasan),Don't we have to limit this case to having actual MTE-capable HW etc?
No extra gating is needed here: on non-MTE hardware (or with kasan=off), kasan_init_hw_tags() returns early without calling kasan_enable(), so kasan_suite_init() sees !kasan_enabled() and aborts the suite before any test case runs. For Generic and SW_TAGS builds, KASAN_TEST_NEEDS_CONFIG_ON(test, CONFIG_KASAN_HW_TAGS) skips the test, matching the rest of kasan_kunit_test_cases[].
I confirmed this by booting the HW_TAGS kernel under QEMU with mte=off and with kasan=off: in both cases the suite fails to initialize and no test case runs.
I'll add a short comment in page_alloc_skip_kasan() noting that in v3.
Thanks,
Jeff
[email protected]
AI reviewSashiko has reviewed this 2-patch series and found no issues.
Sashiko has reviewed this 2-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]