Skip to content

Commit 711d349

Browse files
melverakpm00
authored andcommitted
kasan: revert eviction of stack traces in generic mode
This partially reverts commits cc478e0, 63b85ac, 08d7c94, a414d42, and 773688a to make use of variable-sized stack depot records, since eviction of stack entries from stack depot forces fixed- sized stack records. Care was taken to retain the code cleanups by the above commits. Eviction was added to generic KASAN as a response to alleviating the additional memory usage from fixed-sized stack records, but this still uses more memory than previously. With the re-introduction of variable-sized records for stack depot, we can just switch back to non-evictable stack records again, and return back to the previous performance and memory usage baseline. Before (observed after a KASAN kernel boot): pools: 597 refcounted_allocations: 17547 refcounted_frees: 6477 refcounted_in_use: 11070 freelist_size: 3497 persistent_count: 12163 persistent_bytes: 1717008 After: pools: 319 refcounted_allocations: 0 refcounted_frees: 0 refcounted_in_use: 0 freelist_size: 0 persistent_count: 29397 persistent_bytes: 5183536 As can be seen from the counters, with a generic KASAN config, refcounted allocations and evictions are no longer used. Due to using variable-sized records, I observe a reduction of 278 stack depot pools (saving 4448 KiB) with my test setup. Link: https://lkml.kernel.org/r/20240129100708.39460-2-elver@google.com Fixes: cc478e0 ("kasan: avoid resetting aux_lock") Fixes: 63b85ac ("kasan: stop leaking stack trace handles") Fixes: 08d7c94 ("kasan: memset free track in qlink_free") Fixes: a414d42 ("kasan: handle concurrent kasan_record_aux_stack calls") Fixes: 773688a ("kasan: use stack_depot_put for Generic mode") Signed-off-by: Marco Elver <elver@google.com> Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com> Tested-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com> Cc: Alexander Potapenko <glider@google.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com> Cc: Vincenzo Frascino <vincenzo.frascino@arm.com> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 31639fd commit 711d349

File tree

4 files changed

+14
-77
lines changed

4 files changed

+14
-77
lines changed

mm/kasan/common.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ void kasan_save_track(struct kasan_track *track, gfp_t flags)
6565
{
6666
depot_stack_handle_t stack;
6767

68-
stack = kasan_save_stack(flags,
69-
STACK_DEPOT_FLAG_CAN_ALLOC | STACK_DEPOT_FLAG_GET);
68+
stack = kasan_save_stack(flags, STACK_DEPOT_FLAG_CAN_ALLOC);
7069
kasan_set_track(track, stack);
7170
}
7271

@@ -266,10 +265,9 @@ bool __kasan_slab_free(struct kmem_cache *cache, void *object,
266265
return true;
267266

268267
/*
269-
* If the object is not put into quarantine, it will likely be quickly
270-
* reallocated. Thus, release its metadata now.
268+
* Note: Keep per-object metadata to allow KASAN print stack traces for
269+
* use-after-free-before-realloc bugs.
271270
*/
272-
kasan_release_object_meta(cache, object);
273271

274272
/* Let slab put the object onto the freelist. */
275273
return false;

mm/kasan/generic.c

Lines changed: 7 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -485,16 +485,6 @@ void kasan_init_object_meta(struct kmem_cache *cache, const void *object)
485485
if (alloc_meta) {
486486
/* Zero out alloc meta to mark it as invalid. */
487487
__memset(alloc_meta, 0, sizeof(*alloc_meta));
488-
489-
/*
490-
* Prepare the lock for saving auxiliary stack traces.
491-
* Temporarily disable KASAN bug reporting to allow instrumented
492-
* raw_spin_lock_init to access aux_lock, which resides inside
493-
* of a redzone.
494-
*/
495-
kasan_disable_current();
496-
raw_spin_lock_init(&alloc_meta->aux_lock);
497-
kasan_enable_current();
498488
}
499489

500490
/*
@@ -506,18 +496,8 @@ void kasan_init_object_meta(struct kmem_cache *cache, const void *object)
506496

507497
static void release_alloc_meta(struct kasan_alloc_meta *meta)
508498
{
509-
/* Evict the stack traces from stack depot. */
510-
stack_depot_put(meta->alloc_track.stack);
511-
stack_depot_put(meta->aux_stack[0]);
512-
stack_depot_put(meta->aux_stack[1]);
513-
514-
/*
515-
* Zero out alloc meta to mark it as invalid but keep aux_lock
516-
* initialized to avoid having to reinitialize it when another object
517-
* is allocated in the same slot.
518-
*/
519-
__memset(&meta->alloc_track, 0, sizeof(meta->alloc_track));
520-
__memset(meta->aux_stack, 0, sizeof(meta->aux_stack));
499+
/* Zero out alloc meta to mark it as invalid. */
500+
__memset(meta, 0, sizeof(*meta));
521501
}
522502

523503
static void release_free_meta(const void *object, struct kasan_free_meta *meta)
@@ -529,27 +509,10 @@ static void release_free_meta(const void *object, struct kasan_free_meta *meta)
529509
if (*(u8 *)kasan_mem_to_shadow(object) != KASAN_SLAB_FREE_META)
530510
return;
531511

532-
/* Evict the stack trace from the stack depot. */
533-
stack_depot_put(meta->free_track.stack);
534-
535512
/* Mark free meta as invalid. */
536513
*(u8 *)kasan_mem_to_shadow(object) = KASAN_SLAB_FREE;
537514
}
538515

539-
void kasan_release_object_meta(struct kmem_cache *cache, const void *object)
540-
{
541-
struct kasan_alloc_meta *alloc_meta;
542-
struct kasan_free_meta *free_meta;
543-
544-
alloc_meta = kasan_get_alloc_meta(cache, object);
545-
if (alloc_meta)
546-
release_alloc_meta(alloc_meta);
547-
548-
free_meta = kasan_get_free_meta(cache, object);
549-
if (free_meta)
550-
release_free_meta(object, free_meta);
551-
}
552-
553516
size_t kasan_metadata_size(struct kmem_cache *cache, bool in_object)
554517
{
555518
struct kasan_cache *info = &cache->kasan_info;
@@ -574,8 +537,6 @@ static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags)
574537
struct kmem_cache *cache;
575538
struct kasan_alloc_meta *alloc_meta;
576539
void *object;
577-
depot_stack_handle_t new_handle, old_handle;
578-
unsigned long flags;
579540

580541
if (is_kfence_address(addr) || !slab)
581542
return;
@@ -586,33 +547,18 @@ static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags)
586547
if (!alloc_meta)
587548
return;
588549

589-
new_handle = kasan_save_stack(0, depot_flags);
590-
591-
/*
592-
* Temporarily disable KASAN bug reporting to allow instrumented
593-
* spinlock functions to access aux_lock, which resides inside of a
594-
* redzone.
595-
*/
596-
kasan_disable_current();
597-
raw_spin_lock_irqsave(&alloc_meta->aux_lock, flags);
598-
old_handle = alloc_meta->aux_stack[1];
599550
alloc_meta->aux_stack[1] = alloc_meta->aux_stack[0];
600-
alloc_meta->aux_stack[0] = new_handle;
601-
raw_spin_unlock_irqrestore(&alloc_meta->aux_lock, flags);
602-
kasan_enable_current();
603-
604-
stack_depot_put(old_handle);
551+
alloc_meta->aux_stack[0] = kasan_save_stack(0, depot_flags);
605552
}
606553

607554
void kasan_record_aux_stack(void *addr)
608555
{
609-
return __kasan_record_aux_stack(addr,
610-
STACK_DEPOT_FLAG_CAN_ALLOC | STACK_DEPOT_FLAG_GET);
556+
return __kasan_record_aux_stack(addr, STACK_DEPOT_FLAG_CAN_ALLOC);
611557
}
612558

613559
void kasan_record_aux_stack_noalloc(void *addr)
614560
{
615-
return __kasan_record_aux_stack(addr, STACK_DEPOT_FLAG_GET);
561+
return __kasan_record_aux_stack(addr, 0);
616562
}
617563

618564
void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags)
@@ -623,7 +569,7 @@ void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags)
623569
if (!alloc_meta)
624570
return;
625571

626-
/* Evict previous stack traces (might exist for krealloc or mempool). */
572+
/* Invalidate previous stack traces (might exist for krealloc or mempool). */
627573
release_alloc_meta(alloc_meta);
628574

629575
kasan_save_track(&alloc_meta->alloc_track, flags);
@@ -637,7 +583,7 @@ void kasan_save_free_info(struct kmem_cache *cache, void *object)
637583
if (!free_meta)
638584
return;
639585

640-
/* Evict previous stack trace (might exist for mempool). */
586+
/* Invalidate previous stack trace (might exist for mempool). */
641587
release_free_meta(object, free_meta);
642588

643589
kasan_save_track(&free_meta->free_track, 0);

mm/kasan/kasan.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <linux/kasan.h>
77
#include <linux/kasan-tags.h>
88
#include <linux/kfence.h>
9-
#include <linux/spinlock.h>
109
#include <linux/stackdepot.h>
1110

1211
#if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
@@ -265,13 +264,6 @@ struct kasan_global {
265264
struct kasan_alloc_meta {
266265
struct kasan_track alloc_track;
267266
/* Free track is stored in kasan_free_meta. */
268-
/*
269-
* aux_lock protects aux_stack from accesses from concurrent
270-
* kasan_record_aux_stack calls. It is a raw spinlock to avoid sleeping
271-
* on RT kernels, as kasan_record_aux_stack_noalloc can be called from
272-
* non-sleepable contexts.
273-
*/
274-
raw_spinlock_t aux_lock;
275267
depot_stack_handle_t aux_stack[2];
276268
};
277269

@@ -398,10 +390,8 @@ struct kasan_alloc_meta *kasan_get_alloc_meta(struct kmem_cache *cache,
398390
struct kasan_free_meta *kasan_get_free_meta(struct kmem_cache *cache,
399391
const void *object);
400392
void kasan_init_object_meta(struct kmem_cache *cache, const void *object);
401-
void kasan_release_object_meta(struct kmem_cache *cache, const void *object);
402393
#else
403394
static inline void kasan_init_object_meta(struct kmem_cache *cache, const void *object) { }
404-
static inline void kasan_release_object_meta(struct kmem_cache *cache, const void *object) { }
405395
#endif
406396

407397
depot_stack_handle_t kasan_save_stack(gfp_t flags, depot_flags_t depot_flags);

mm/kasan/quarantine.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache)
145145
void *object = qlink_to_object(qlink, cache);
146146
struct kasan_free_meta *free_meta = kasan_get_free_meta(cache, object);
147147

148-
kasan_release_object_meta(cache, object);
148+
/*
149+
* Note: Keep per-object metadata to allow KASAN print stack traces for
150+
* use-after-free-before-realloc bugs.
151+
*/
149152

150153
/*
151154
* If init_on_free is enabled and KASAN's free metadata is stored in

0 commit comments

Comments
 (0)