Skip to content

Commit 280ec6c

Browse files
xairyakpm00
authored andcommitted
kasan: rename kasan_slab_free_mempool to kasan_mempool_poison_object
Patch series "kasan: save mempool stack traces". This series updates KASAN to save alloc and free stack traces for secondary-level allocators that cache and reuse allocations internally instead of giving them back to the underlying allocator (e.g. mempool). As a part of this change, introduce and document a set of KASAN hooks: bool kasan_mempool_poison_pages(struct page *page, unsigned int order); void kasan_mempool_unpoison_pages(struct page *page, unsigned int order); bool kasan_mempool_poison_object(void *ptr); void kasan_mempool_unpoison_object(void *ptr, size_t size); and use them in the mempool code. Besides mempool, skbuff and io_uring also cache allocations and already use KASAN hooks to poison those. Their code is updated to use the new mempool hooks. The new hooks save alloc and free stack traces (for normal kmalloc and slab objects; stack traces for large kmalloc objects and page_alloc are not supported by KASAN yet), improve the readability of the users' code, and also allow the users to prevent double-free and invalid-free bugs; see the patches for the details. This patch (of 21): Rename kasan_slab_free_mempool to kasan_mempool_poison_object. kasan_slab_free_mempool is a slightly confusing name: it is unclear whether this function poisons the object when it is freed into mempool or does something when the object is freed from mempool to the underlying allocator. The new name also aligns with other mempool-related KASAN hooks added in the following patches in this series. Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/c5618685abb7cdbf9fb4897f565e7759f601da84.1703024586.git.andreyknvl@google.com Signed-off-by: Andrey Konovalov <[email protected]> Cc: Alexander Lobakin <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Breno Leitao <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: Evgenii Stepanov <[email protected]> Cc: Marco Elver <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 14059f6 commit 280ec6c

File tree

4 files changed

+8
-9
lines changed

4 files changed

+8
-9
lines changed

include/linux/kasan.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ static __always_inline void kasan_kfree_large(void *ptr)
172172
__kasan_kfree_large(ptr, _RET_IP_);
173173
}
174174

175-
void __kasan_slab_free_mempool(void *ptr, unsigned long ip);
176-
static __always_inline void kasan_slab_free_mempool(void *ptr)
175+
void __kasan_mempool_poison_object(void *ptr, unsigned long ip);
176+
static __always_inline void kasan_mempool_poison_object(void *ptr)
177177
{
178178
if (kasan_enabled())
179-
__kasan_slab_free_mempool(ptr, _RET_IP_);
179+
__kasan_mempool_poison_object(ptr, _RET_IP_);
180180
}
181181

182182
void * __must_check __kasan_slab_alloc(struct kmem_cache *s,
@@ -256,7 +256,7 @@ static inline bool kasan_slab_free(struct kmem_cache *s, void *object, bool init
256256
return false;
257257
}
258258
static inline void kasan_kfree_large(void *ptr) {}
259-
static inline void kasan_slab_free_mempool(void *ptr) {}
259+
static inline void kasan_mempool_poison_object(void *ptr) {}
260260
static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object,
261261
gfp_t flags, bool init)
262262
{

io_uring/alloc_cache.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ static inline bool io_alloc_cache_put(struct io_alloc_cache *cache,
1616
if (cache->nr_cached < cache->max_cached) {
1717
cache->nr_cached++;
1818
wq_stack_add_head(&entry->node, &cache->list);
19-
/* KASAN poisons object */
20-
kasan_slab_free_mempool(entry);
19+
kasan_mempool_poison_object(entry);
2120
return true;
2221
}
2322
return false;

mm/kasan/common.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ static inline bool ____kasan_kfree_large(void *ptr, unsigned long ip)
271271

272272
/*
273273
* The object will be poisoned by kasan_poison_pages() or
274-
* kasan_slab_free_mempool().
274+
* kasan_mempool_poison_object().
275275
*/
276276

277277
return false;
@@ -282,7 +282,7 @@ void __kasan_kfree_large(void *ptr, unsigned long ip)
282282
____kasan_kfree_large(ptr, ip);
283283
}
284284

285-
void __kasan_slab_free_mempool(void *ptr, unsigned long ip)
285+
void __kasan_mempool_poison_object(void *ptr, unsigned long ip)
286286
{
287287
struct folio *folio;
288288

mm/mempool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static inline void poison_element(mempool_t *pool, void *element)
107107
static __always_inline void kasan_poison_element(mempool_t *pool, void *element)
108108
{
109109
if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
110-
kasan_slab_free_mempool(element);
110+
kasan_mempool_poison_object(element);
111111
else if (pool->alloc == mempool_alloc_pages)
112112
kasan_poison_pages(element, (unsigned long)pool->pool_data,
113113
false);

0 commit comments

Comments
 (0)