* [PATCH 0/3] kasan: common: cleanups and deduplication
@ 2026-07-04 9:20 Igor Putko
2026-07-04 9:20 ` [PATCH 1/3] kasan: deduplicate quarantine reduction logic Igor Putko
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Igor Putko @ 2026-07-04 9:20 UTC (permalink / raw)
To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino,
akpm
Cc: kasan-dev, linux-mm, linux-kernel, Igor Putko
This patch series refactors KASAN common code to improve readability
and maintainability.
Specifically, it:
- Introduces a helper for quarantine reduction to eliminate boilerplate.
- Utilizes the standard ALIGN_DOWN macro instead of manual bitwise logic.
- Replaces a magic number with the KASAN_TAG_KERNEL definition.
Igor Putko (3):
kasan: deduplicate quarantine reduction logic
kasan: common: use ALIGN_DOWN macro for stack alignment
kasan: common: replace magic number with KASAN_TAG_KERNEL
mm/kasan/common.c | 22 ++++++++++++----------
1 file changed, 12 insertions(+), 10 deletions(-)
--
2.47.3
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH 1/3] kasan: deduplicate quarantine reduction logic 2026-07-04 9:20 [PATCH 0/3] kasan: common: cleanups and deduplication Igor Putko @ 2026-07-04 9:20 ` Igor Putko 2026-07-06 14:13 ` Andrey Konovalov 2026-07-04 9:20 ` [PATCH 2/3] kasan: common: use ALIGN_DOWN macro for stack alignment Igor Putko 2026-07-04 9:20 ` [PATCH 3/3] kasan: common: replace magic number with KASAN_TAG_KERNEL Igor Putko 2 siblings, 1 reply; 7+ messages in thread From: Igor Putko @ 2026-07-04 9:20 UTC (permalink / raw) To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino, akpm Cc: kasan-dev, linux-mm, linux-kernel, Igor Putko Multiple allocation functions share the exact same conditional logic for reducing the KASAN quarantine size based on gfp flags. Introduce the kasan_quarantine_reduce_cond() helper to eliminate this boilerplate from the slab, kmalloc, kmalloc_large, and krealloc paths. Signed-off-by: Igor Putko <igorpetindev@gmail.com> --- mm/kasan/common.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index b7d05c2a6..34aa0fdce 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -340,14 +340,19 @@ static inline void unpoison_slab_object(struct kmem_cache *cache, void *object, kasan_save_alloc_info(cache, object, flags); } +static inline void kasan_quarantine_reduce_cond(gfp_t flags) +{ + if (gfpflags_allow_blocking(flags)) + kasan_quarantine_reduce(); +} + void * __must_check __kasan_slab_alloc(struct kmem_cache *cache, void *object, gfp_t flags, bool init) { u8 tag; void *tagged_object; - if (gfpflags_allow_blocking(flags)) - kasan_quarantine_reduce(); + kasan_quarantine_reduce_cond(flags); if (unlikely(object == NULL)) return NULL; @@ -402,8 +407,7 @@ static inline void poison_kmalloc_redzone(struct kmem_cache *cache, void * __must_check __kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size, gfp_t flags) { - if (gfpflags_allow_blocking(flags)) - kasan_quarantine_reduce(); + kasan_quarantine_reduce_cond(flags); if (unlikely(object == NULL)) return NULL; @@ -443,8 +447,7 @@ static inline void poison_kmalloc_large_redzone(const void *ptr, size_t size, void * __must_check __kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags) { - if (gfpflags_allow_blocking(flags)) - kasan_quarantine_reduce(); + kasan_quarantine_reduce_cond(flags); if (unlikely(ptr == NULL)) return NULL; @@ -460,8 +463,7 @@ void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flag { struct slab *slab; - if (gfpflags_allow_blocking(flags)) - kasan_quarantine_reduce(); + kasan_quarantine_reduce_cond(flags); if (unlikely(object == ZERO_SIZE_PTR)) return (void *)object; -- 2.47.3 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 1/3] kasan: deduplicate quarantine reduction logic 2026-07-04 9:20 ` [PATCH 1/3] kasan: deduplicate quarantine reduction logic Igor Putko @ 2026-07-06 14:13 ` Andrey Konovalov 0 siblings, 0 replies; 7+ messages in thread From: Andrey Konovalov @ 2026-07-06 14:13 UTC (permalink / raw) To: Igor Putko Cc: ryabinin.a.a, glider, dvyukov, vincenzo.frascino, akpm, kasan-dev, linux-mm, linux-kernel On Sat, Jul 4, 2026 at 11:20 AM Igor Putko <igorpetindev@gmail.com> wrote: > > Multiple allocation functions share the exact same conditional logic for > reducing the KASAN quarantine size based on gfp flags. > Introduce the kasan_quarantine_reduce_cond() helper to eliminate this > boilerplate from the slab, kmalloc, kmalloc_large, and krealloc paths. > > Signed-off-by: Igor Putko <igorpetindev@gmail.com> > --- > mm/kasan/common.c | 18 ++++++++++-------- > 1 file changed, 10 insertions(+), 8 deletions(-) > > diff --git a/mm/kasan/common.c b/mm/kasan/common.c > index b7d05c2a6..34aa0fdce 100644 > --- a/mm/kasan/common.c > +++ b/mm/kasan/common.c > @@ -340,14 +340,19 @@ static inline void unpoison_slab_object(struct kmem_cache *cache, void *object, > kasan_save_alloc_info(cache, object, flags); > } > > +static inline void kasan_quarantine_reduce_cond(gfp_t flags) > +{ > + if (gfpflags_allow_blocking(flags)) > + kasan_quarantine_reduce(); > +} Not sure about this change: it's hiding the condition for when kasan_quarantine_reduce() gets to execute and does not really improve the code readability otherwise. > + > void * __must_check __kasan_slab_alloc(struct kmem_cache *cache, > void *object, gfp_t flags, bool init) > { > u8 tag; > void *tagged_object; > > - if (gfpflags_allow_blocking(flags)) > - kasan_quarantine_reduce(); > + kasan_quarantine_reduce_cond(flags); > > if (unlikely(object == NULL)) > return NULL; > @@ -402,8 +407,7 @@ static inline void poison_kmalloc_redzone(struct kmem_cache *cache, > void * __must_check __kasan_kmalloc(struct kmem_cache *cache, const void *object, > size_t size, gfp_t flags) > { > - if (gfpflags_allow_blocking(flags)) > - kasan_quarantine_reduce(); > + kasan_quarantine_reduce_cond(flags); > > if (unlikely(object == NULL)) > return NULL; > @@ -443,8 +447,7 @@ static inline void poison_kmalloc_large_redzone(const void *ptr, size_t size, > void * __must_check __kasan_kmalloc_large(const void *ptr, size_t size, > gfp_t flags) > { > - if (gfpflags_allow_blocking(flags)) > - kasan_quarantine_reduce(); > + kasan_quarantine_reduce_cond(flags); > > if (unlikely(ptr == NULL)) > return NULL; > @@ -460,8 +463,7 @@ void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flag > { > struct slab *slab; > > - if (gfpflags_allow_blocking(flags)) > - kasan_quarantine_reduce(); > + kasan_quarantine_reduce_cond(flags); > > if (unlikely(object == ZERO_SIZE_PTR)) > return (void *)object; > -- > 2.47.3 > ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/3] kasan: common: use ALIGN_DOWN macro for stack alignment 2026-07-04 9:20 [PATCH 0/3] kasan: common: cleanups and deduplication Igor Putko 2026-07-04 9:20 ` [PATCH 1/3] kasan: deduplicate quarantine reduction logic Igor Putko @ 2026-07-04 9:20 ` Igor Putko 2026-07-06 14:13 ` Andrey Konovalov 2026-07-04 9:20 ` [PATCH 3/3] kasan: common: replace magic number with KASAN_TAG_KERNEL Igor Putko 2 siblings, 1 reply; 7+ messages in thread From: Igor Putko @ 2026-07-04 9:20 UTC (permalink / raw) To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino, akpm Cc: kasan-dev, linux-mm, linux-kernel, Igor Putko Replace the manual bitwise alignment logic in kasan_unpoison_task_stack_below() with the standard ALIGN_DOWN macro. This improves code readability and conforms to standard kernel idioms. Signed-off-by: Igor Putko <igorpetindev@gmail.com> --- mm/kasan/common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 34aa0fdce..5faf73c8f 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -119,7 +119,7 @@ asmlinkage void kasan_unpoison_task_stack_below(const void *watermark) * because this function is called by early resume code which hasn't * yet set up the percpu register (%gs). */ - void *base = (void *)((unsigned long)watermark & ~(THREAD_SIZE - 1)); + void *base = (void *)ALIGN_DOWN((unsigned long)watermark, THREAD_SIZE); kasan_unpoison(base, watermark - base, false); } -- 2.47.3 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 2/3] kasan: common: use ALIGN_DOWN macro for stack alignment 2026-07-04 9:20 ` [PATCH 2/3] kasan: common: use ALIGN_DOWN macro for stack alignment Igor Putko @ 2026-07-06 14:13 ` Andrey Konovalov 0 siblings, 0 replies; 7+ messages in thread From: Andrey Konovalov @ 2026-07-06 14:13 UTC (permalink / raw) To: Igor Putko Cc: ryabinin.a.a, glider, dvyukov, vincenzo.frascino, akpm, kasan-dev, linux-mm, linux-kernel On Sat, Jul 4, 2026 at 11:20 AM Igor Putko <igorpetindev@gmail.com> wrote: > > Replace the manual bitwise alignment logic in > kasan_unpoison_task_stack_below() with the > standard ALIGN_DOWN macro. This improves code > readability and conforms to standard kernel idioms. > > Signed-off-by: Igor Putko <igorpetindev@gmail.com> > --- > mm/kasan/common.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/mm/kasan/common.c b/mm/kasan/common.c > index 34aa0fdce..5faf73c8f 100644 > --- a/mm/kasan/common.c > +++ b/mm/kasan/common.c > @@ -119,7 +119,7 @@ asmlinkage void kasan_unpoison_task_stack_below(const void *watermark) > * because this function is called by early resume code which hasn't > * yet set up the percpu register (%gs). > */ > - void *base = (void *)((unsigned long)watermark & ~(THREAD_SIZE - 1)); > + void *base = (void *)ALIGN_DOWN((unsigned long)watermark, THREAD_SIZE); > > kasan_unpoison(base, watermark - base, false); > } > -- > 2.47.3 > Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com> ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 3/3] kasan: common: replace magic number with KASAN_TAG_KERNEL 2026-07-04 9:20 [PATCH 0/3] kasan: common: cleanups and deduplication Igor Putko 2026-07-04 9:20 ` [PATCH 1/3] kasan: deduplicate quarantine reduction logic Igor Putko 2026-07-04 9:20 ` [PATCH 2/3] kasan: common: use ALIGN_DOWN macro for stack alignment Igor Putko @ 2026-07-04 9:20 ` Igor Putko 2026-07-06 14:13 ` Andrey Konovalov 2 siblings, 1 reply; 7+ messages in thread From: Igor Putko @ 2026-07-04 9:20 UTC (permalink / raw) To: ryabinin.a.a, glider, andreyknvl, dvyukov, vincenzo.frascino, akpm Cc: kasan-dev, linux-mm, linux-kernel, Igor Putko Replace the literal 0xff in assign_tag() with KASAN_TAG_KERNEL to eliminate the magic number and improve code clarity. Signed-off-by: Igor Putko <igorpetindev@gmail.com> --- mm/kasan/common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 5faf73c8f..3e9b6b8f5 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -188,7 +188,7 @@ static inline u8 assign_tag(struct kmem_cache *cache, const void *object, bool init) { if (IS_ENABLED(CONFIG_KASAN_GENERIC)) - return 0xff; + return KASAN_TAG_KERNEL; /* * If the cache neither has a constructor nor has SLAB_TYPESAFE_BY_RCU -- 2.47.3 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 3/3] kasan: common: replace magic number with KASAN_TAG_KERNEL 2026-07-04 9:20 ` [PATCH 3/3] kasan: common: replace magic number with KASAN_TAG_KERNEL Igor Putko @ 2026-07-06 14:13 ` Andrey Konovalov 0 siblings, 0 replies; 7+ messages in thread From: Andrey Konovalov @ 2026-07-06 14:13 UTC (permalink / raw) To: Igor Putko Cc: ryabinin.a.a, glider, dvyukov, vincenzo.frascino, akpm, kasan-dev, linux-mm, linux-kernel On Sat, Jul 4, 2026 at 11:20 AM Igor Putko <igorpetindev@gmail.com> wrote: > > Replace the literal 0xff in assign_tag() > with KASAN_TAG_KERNEL to eliminate the > magic number and improve code clarity. > > Signed-off-by: Igor Putko <igorpetindev@gmail.com> > --- > mm/kasan/common.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/mm/kasan/common.c b/mm/kasan/common.c > index 5faf73c8f..3e9b6b8f5 100644 > --- a/mm/kasan/common.c > +++ b/mm/kasan/common.c > @@ -188,7 +188,7 @@ static inline u8 assign_tag(struct kmem_cache *cache, > const void *object, bool init) > { > if (IS_ENABLED(CONFIG_KASAN_GENERIC)) > - return 0xff; > + return KASAN_TAG_KERNEL; Not sure about this change either: the notion of a tag does no apply to the Generic mode, so the code uses 0xff in such cases (the second definition of page_kasan_tag() is another case). > > /* > * If the cache neither has a constructor nor has SLAB_TYPESAFE_BY_RCU > -- > 2.47.3 > ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-07-06 14:14 UTC | newest] Thread overview: 7+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-07-04 9:20 [PATCH 0/3] kasan: common: cleanups and deduplication Igor Putko 2026-07-04 9:20 ` [PATCH 1/3] kasan: deduplicate quarantine reduction logic Igor Putko 2026-07-06 14:13 ` Andrey Konovalov 2026-07-04 9:20 ` [PATCH 2/3] kasan: common: use ALIGN_DOWN macro for stack alignment Igor Putko 2026-07-06 14:13 ` Andrey Konovalov 2026-07-04 9:20 ` [PATCH 3/3] kasan: common: replace magic number with KASAN_TAG_KERNEL Igor Putko 2026-07-06 14:13 ` Andrey Konovalov
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox