Skip to content

Commit f816938

Browse files
xairyakpm00
authored andcommitted
kasan: use stack_depot_put for tag-based modes
Make tag-based KASAN modes evict stack traces from the stack depot once they are evicted from the stack ring. Internally, pass STACK_DEPOT_FLAG_GET to stack_depot_save_flags (via kasan_save_stack) to increment the refcount when saving a new entry to stack ring and call stack_depot_put when removing an entry from stack ring. Link: https://lkml.kernel.org/r/b4773e5c1b0b9df6826ec0b65c1923feadfa78e5.1700502145.git.andreyknvl@google.com Signed-off-by: Andrey Konovalov <andreyknvl@google.com> Reviewed-by: Alexander Potapenko <glider@google.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Evgenii Stepanov <eugenis@google.com> Cc: Marco Elver <elver@google.com> Cc: Oscar Salvador <osalvador@suse.de> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 7d88e4f commit f816938

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

mm/kasan/tags.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,13 @@ static void save_stack_info(struct kmem_cache *cache, void *object,
9797
gfp_t gfp_flags, bool is_free)
9898
{
9999
unsigned long flags;
100-
depot_stack_handle_t stack;
100+
depot_stack_handle_t stack, old_stack;
101101
u64 pos;
102102
struct kasan_stack_ring_entry *entry;
103103
void *old_ptr;
104104

105-
stack = kasan_save_stack(gfp_flags, STACK_DEPOT_FLAG_CAN_ALLOC);
105+
stack = kasan_save_stack(gfp_flags,
106+
STACK_DEPOT_FLAG_CAN_ALLOC | STACK_DEPOT_FLAG_GET);
106107

107108
/*
108109
* Prevent save_stack_info() from modifying stack ring
@@ -121,6 +122,8 @@ static void save_stack_info(struct kmem_cache *cache, void *object,
121122
if (!try_cmpxchg(&entry->ptr, &old_ptr, STACK_RING_BUSY_PTR))
122123
goto next; /* Busy slot. */
123124

125+
old_stack = entry->stack;
126+
124127
entry->size = cache->object_size;
125128
entry->pid = current->pid;
126129
entry->stack = stack;
@@ -129,6 +132,9 @@ static void save_stack_info(struct kmem_cache *cache, void *object,
129132
entry->ptr = object;
130133

131134
read_unlock_irqrestore(&stack_ring.lock, flags);
135+
136+
if (old_stack)
137+
stack_depot_put(old_stack);
132138
}
133139

134140
void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags)

0 commit comments

Comments
 (0)