Skip to content

Commit 1956832

Browse files
xairyakpm00
authored andcommitted
kasan: introduce kasan_mempool_unpoison_object
Introduce and document a kasan_mempool_unpoison_object hook. This hook serves as a replacement for the generic kasan_unpoison_range that the mempool code relies on right now. mempool will be updated to use the new hook in one of the following patches. For now, define the new hook to be identical to kasan_unpoison_range. One of the following patches will update it to add stack trace collection. Link: https://lkml.kernel.org/r/dae25f0e18ed8fd50efe509c5b71a0592de5c18d.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 2e7c954 commit 1956832

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

include/linux/kasan.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ bool __kasan_mempool_poison_object(void *ptr, unsigned long ip);
228228
* bugs and reports them. The caller can use the return value of this function
229229
* to find out if the allocation is buggy.
230230
*
231+
* Before the poisoned allocation can be reused, it must be unpoisoned via
232+
* kasan_mempool_unpoison_object().
233+
*
231234
* This function operates on all slab allocations including large kmalloc
232235
* allocations (the ones returned by kmalloc_large() or by kmalloc() with the
233236
* size > KMALLOC_MAX_SIZE).
@@ -241,6 +244,32 @@ static __always_inline bool kasan_mempool_poison_object(void *ptr)
241244
return true;
242245
}
243246

247+
void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip);
248+
/**
249+
* kasan_mempool_unpoison_object - Unpoison a mempool slab allocation.
250+
* @ptr: Pointer to the slab allocation.
251+
* @size: Size to be unpoisoned.
252+
*
253+
* This function is intended for kernel subsystems that cache slab allocations
254+
* to reuse them instead of freeing them back to the slab allocator (e.g.
255+
* mempool).
256+
*
257+
* This function unpoisons a slab allocation that was previously poisoned via
258+
* kasan_mempool_poison_object() without initializing its memory. For the
259+
* tag-based modes, this function does not assign a new tag to the allocation
260+
* and instead restores the original tags based on the pointer value.
261+
*
262+
* This function operates on all slab allocations including large kmalloc
263+
* allocations (the ones returned by kmalloc_large() or by kmalloc() with the
264+
* size > KMALLOC_MAX_SIZE).
265+
*/
266+
static __always_inline void kasan_mempool_unpoison_object(void *ptr,
267+
size_t size)
268+
{
269+
if (kasan_enabled())
270+
__kasan_mempool_unpoison_object(ptr, size, _RET_IP_);
271+
}
272+
244273
/*
245274
* Unlike kasan_check_read/write(), kasan_check_byte() is performed even for
246275
* the hardware tag-based mode that doesn't rely on compiler instrumentation.
@@ -301,6 +330,8 @@ static inline bool kasan_mempool_poison_object(void *ptr)
301330
{
302331
return true;
303332
}
333+
static inline void kasan_mempool_unpoison_object(void *ptr, size_t size) {}
334+
304335
static inline bool kasan_check_byte(const void *address)
305336
{
306337
return true;

mm/kasan/common.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,11 @@ bool __kasan_mempool_poison_object(void *ptr, unsigned long ip)
451451
}
452452
}
453453

454+
void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip)
455+
{
456+
kasan_unpoison(ptr, size, false);
457+
}
458+
454459
bool __kasan_check_byte(const void *address, unsigned long ip)
455460
{
456461
if (!kasan_byte_accessible(address)) {

0 commit comments

Comments
 (0)