Commit 7bd230a2 authored by Suren Baghdasaryan's avatar Suren Baghdasaryan Committed by Andrew Morton

mm/slab: enable slab allocation tagging for kmalloc and friends

Redefine kmalloc, krealloc, kzalloc, kcalloc, etc. to record allocations
and deallocations done by these functions.

[surenb@google.com: undo _noprof additions in the documentation]
  Link: https://lkml.kernel.org/r/20240326231453.1206227-7-surenb@google.com
[rdunlap@infradead.org: fix kcalloc() kernel-doc warnings]
  Link: https://lkml.kernel.org/r/20240327044649.9199-1-rdunlap@infradead.org
Link: https://lkml.kernel.org/r/20240321163705.3067592-26-surenb@google.comSigned-off-by: default avatarSuren Baghdasaryan <surenb@google.com>
Co-developed-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
Signed-off-by: default avatarRandy Dunlap <rdunlap@infradead.org>
Reviewed-by: default avatarKees Cook <keescook@chromium.org>
Tested-by: default avatarKees Cook <keescook@chromium.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Alex Gaynor <alex.gaynor@gmail.com>
Cc: Alice Ryhl <aliceryhl@google.com>
Cc: Andreas Hindborg <a.hindborg@samsung.com>
Cc: Benno Lossin <benno.lossin@proton.me>
Cc: "Björn Roy Baron" <bjorn3_gh@protonmail.com>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: Dennis Zhou <dennis@kernel.org>
Cc: Gary Guo <gary@garyguo.net>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Tejun Heo <tj@kernel.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Wedson Almeida Filho <wedsonaf@gmail.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 53ed0af4
...@@ -725,9 +725,9 @@ __FORTIFY_INLINE void *memchr_inv(const void * const POS0 p, int c, size_t size) ...@@ -725,9 +725,9 @@ __FORTIFY_INLINE void *memchr_inv(const void * const POS0 p, int c, size_t size)
return __real_memchr_inv(p, c, size); return __real_memchr_inv(p, c, size);
} }
extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup) extern void *__real_kmemdup(const void *src, size_t len, gfp_t gfp) __RENAME(kmemdup_noprof)
__realloc_size(2); __realloc_size(2);
__FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp) __FORTIFY_INLINE void *kmemdup_noprof(const void * const POS0 p, size_t size, gfp_t gfp)
{ {
const size_t p_size = __struct_size(p); const size_t p_size = __struct_size(p);
...@@ -737,6 +737,7 @@ __FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp ...@@ -737,6 +737,7 @@ __FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp
fortify_panic(FORTIFY_FUNC_kmemdup, FORTIFY_READ, p_size, size, NULL); fortify_panic(FORTIFY_FUNC_kmemdup, FORTIFY_READ, p_size, size, NULL);
return __real_kmemdup(p, size, gfp); return __real_kmemdup(p, size, gfp);
} }
#define kmemdup(...) alloc_hooks(kmemdup_noprof(__VA_ARGS__))
/** /**
* strcpy - Copy a string into another string buffer * strcpy - Copy a string into another string buffer
......
This diff is collapsed.
...@@ -282,7 +282,9 @@ extern void kfree_const(const void *x); ...@@ -282,7 +282,9 @@ extern void kfree_const(const void *x);
extern char *kstrdup(const char *s, gfp_t gfp) __malloc; extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
extern const char *kstrdup_const(const char *s, gfp_t gfp); extern const char *kstrdup_const(const char *s, gfp_t gfp);
extern char *kstrndup(const char *s, size_t len, gfp_t gfp); extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
extern void *kmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2); extern void *kmemdup_noprof(const void *src, size_t len, gfp_t gfp) __realloc_size(2);
#define kmemdup(...) alloc_hooks(kmemdup_noprof(__VA_ARGS__))
extern void *kvmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2); extern void *kvmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2);
extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp); extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
extern void *kmemdup_array(const void *src, size_t element_size, size_t count, gfp_t gfp); extern void *kmemdup_array(const void *src, size_t element_size, size_t count, gfp_t gfp);
......
...@@ -1189,7 +1189,7 @@ __do_krealloc(const void *p, size_t new_size, gfp_t flags) ...@@ -1189,7 +1189,7 @@ __do_krealloc(const void *p, size_t new_size, gfp_t flags)
return (void *)p; return (void *)p;
} }
ret = kmalloc_track_caller(new_size, flags); ret = kmalloc_node_track_caller_noprof(new_size, flags, NUMA_NO_NODE, _RET_IP_);
if (ret && p) { if (ret && p) {
/* Disable KASAN checks as the object's redzone is accessed. */ /* Disable KASAN checks as the object's redzone is accessed. */
kasan_disable_current(); kasan_disable_current();
...@@ -1213,7 +1213,7 @@ __do_krealloc(const void *p, size_t new_size, gfp_t flags) ...@@ -1213,7 +1213,7 @@ __do_krealloc(const void *p, size_t new_size, gfp_t flags)
* *
* Return: pointer to the allocated memory or %NULL in case of error * Return: pointer to the allocated memory or %NULL in case of error
*/ */
void *krealloc(const void *p, size_t new_size, gfp_t flags) void *krealloc_noprof(const void *p, size_t new_size, gfp_t flags)
{ {
void *ret; void *ret;
...@@ -1228,7 +1228,7 @@ void *krealloc(const void *p, size_t new_size, gfp_t flags) ...@@ -1228,7 +1228,7 @@ void *krealloc(const void *p, size_t new_size, gfp_t flags)
return ret; return ret;
} }
EXPORT_SYMBOL(krealloc); EXPORT_SYMBOL(krealloc_noprof);
/** /**
* kfree_sensitive - Clear sensitive information in memory before freeing * kfree_sensitive - Clear sensitive information in memory before freeing
......
...@@ -4002,7 +4002,7 @@ static __fastpath_inline void *slab_alloc_node(struct kmem_cache *s, struct list ...@@ -4002,7 +4002,7 @@ static __fastpath_inline void *slab_alloc_node(struct kmem_cache *s, struct list
return object; return object;
} }
void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags) void *kmem_cache_alloc_noprof(struct kmem_cache *s, gfp_t gfpflags)
{ {
void *ret = slab_alloc_node(s, NULL, gfpflags, NUMA_NO_NODE, _RET_IP_, void *ret = slab_alloc_node(s, NULL, gfpflags, NUMA_NO_NODE, _RET_IP_,
s->object_size); s->object_size);
...@@ -4011,9 +4011,9 @@ void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags) ...@@ -4011,9 +4011,9 @@ void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
return ret; return ret;
} }
EXPORT_SYMBOL(kmem_cache_alloc); EXPORT_SYMBOL(kmem_cache_alloc_noprof);
void *kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru, void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru,
gfp_t gfpflags) gfp_t gfpflags)
{ {
void *ret = slab_alloc_node(s, lru, gfpflags, NUMA_NO_NODE, _RET_IP_, void *ret = slab_alloc_node(s, lru, gfpflags, NUMA_NO_NODE, _RET_IP_,
...@@ -4023,7 +4023,7 @@ void *kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru, ...@@ -4023,7 +4023,7 @@ void *kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru,
return ret; return ret;
} }
EXPORT_SYMBOL(kmem_cache_alloc_lru); EXPORT_SYMBOL(kmem_cache_alloc_lru_noprof);
/** /**
* kmem_cache_alloc_node - Allocate an object on the specified node * kmem_cache_alloc_node - Allocate an object on the specified node
...@@ -4038,7 +4038,7 @@ EXPORT_SYMBOL(kmem_cache_alloc_lru); ...@@ -4038,7 +4038,7 @@ EXPORT_SYMBOL(kmem_cache_alloc_lru);
* *
* Return: pointer to the new object or %NULL in case of error * Return: pointer to the new object or %NULL in case of error
*/ */
void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node) void *kmem_cache_alloc_node_noprof(struct kmem_cache *s, gfp_t gfpflags, int node)
{ {
void *ret = slab_alloc_node(s, NULL, gfpflags, node, _RET_IP_, s->object_size); void *ret = slab_alloc_node(s, NULL, gfpflags, node, _RET_IP_, s->object_size);
...@@ -4046,7 +4046,7 @@ void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node) ...@@ -4046,7 +4046,7 @@ void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
return ret; return ret;
} }
EXPORT_SYMBOL(kmem_cache_alloc_node); EXPORT_SYMBOL(kmem_cache_alloc_node_noprof);
/* /*
* To avoid unnecessary overhead, we pass through large allocation requests * To avoid unnecessary overhead, we pass through large allocation requests
...@@ -4063,7 +4063,7 @@ static void *__kmalloc_large_node(size_t size, gfp_t flags, int node) ...@@ -4063,7 +4063,7 @@ static void *__kmalloc_large_node(size_t size, gfp_t flags, int node)
flags = kmalloc_fix_flags(flags); flags = kmalloc_fix_flags(flags);
flags |= __GFP_COMP; flags |= __GFP_COMP;
folio = (struct folio *)alloc_pages_node(node, flags, order); folio = (struct folio *)alloc_pages_node_noprof(node, flags, order);
if (folio) { if (folio) {
ptr = folio_address(folio); ptr = folio_address(folio);
lruvec_stat_mod_folio(folio, NR_SLAB_UNRECLAIMABLE_B, lruvec_stat_mod_folio(folio, NR_SLAB_UNRECLAIMABLE_B,
...@@ -4078,7 +4078,7 @@ static void *__kmalloc_large_node(size_t size, gfp_t flags, int node) ...@@ -4078,7 +4078,7 @@ static void *__kmalloc_large_node(size_t size, gfp_t flags, int node)
return ptr; return ptr;
} }
void *kmalloc_large(size_t size, gfp_t flags) void *kmalloc_large_noprof(size_t size, gfp_t flags)
{ {
void *ret = __kmalloc_large_node(size, flags, NUMA_NO_NODE); void *ret = __kmalloc_large_node(size, flags, NUMA_NO_NODE);
...@@ -4086,9 +4086,9 @@ void *kmalloc_large(size_t size, gfp_t flags) ...@@ -4086,9 +4086,9 @@ void *kmalloc_large(size_t size, gfp_t flags)
flags, NUMA_NO_NODE); flags, NUMA_NO_NODE);
return ret; return ret;
} }
EXPORT_SYMBOL(kmalloc_large); EXPORT_SYMBOL(kmalloc_large_noprof);
void *kmalloc_large_node(size_t size, gfp_t flags, int node) void *kmalloc_large_node_noprof(size_t size, gfp_t flags, int node)
{ {
void *ret = __kmalloc_large_node(size, flags, node); void *ret = __kmalloc_large_node(size, flags, node);
...@@ -4096,7 +4096,7 @@ void *kmalloc_large_node(size_t size, gfp_t flags, int node) ...@@ -4096,7 +4096,7 @@ void *kmalloc_large_node(size_t size, gfp_t flags, int node)
flags, node); flags, node);
return ret; return ret;
} }
EXPORT_SYMBOL(kmalloc_large_node); EXPORT_SYMBOL(kmalloc_large_node_noprof);
static __always_inline static __always_inline
void *__do_kmalloc_node(size_t size, gfp_t flags, int node, void *__do_kmalloc_node(size_t size, gfp_t flags, int node,
...@@ -4123,26 +4123,26 @@ void *__do_kmalloc_node(size_t size, gfp_t flags, int node, ...@@ -4123,26 +4123,26 @@ void *__do_kmalloc_node(size_t size, gfp_t flags, int node,
return ret; return ret;
} }
void *__kmalloc_node(size_t size, gfp_t flags, int node) void *__kmalloc_node_noprof(size_t size, gfp_t flags, int node)
{ {
return __do_kmalloc_node(size, flags, node, _RET_IP_); return __do_kmalloc_node(size, flags, node, _RET_IP_);
} }
EXPORT_SYMBOL(__kmalloc_node); EXPORT_SYMBOL(__kmalloc_node_noprof);
void *__kmalloc(size_t size, gfp_t flags) void *__kmalloc_noprof(size_t size, gfp_t flags)
{ {
return __do_kmalloc_node(size, flags, NUMA_NO_NODE, _RET_IP_); return __do_kmalloc_node(size, flags, NUMA_NO_NODE, _RET_IP_);
} }
EXPORT_SYMBOL(__kmalloc); EXPORT_SYMBOL(__kmalloc_noprof);
void *__kmalloc_node_track_caller(size_t size, gfp_t flags, void *kmalloc_node_track_caller_noprof(size_t size, gfp_t flags,
int node, unsigned long caller) int node, unsigned long caller)
{ {
return __do_kmalloc_node(size, flags, node, caller); return __do_kmalloc_node(size, flags, node, caller);
} }
EXPORT_SYMBOL(__kmalloc_node_track_caller); EXPORT_SYMBOL(kmalloc_node_track_caller_noprof);
void *kmalloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size) void *kmalloc_trace_noprof(struct kmem_cache *s, gfp_t gfpflags, size_t size)
{ {
void *ret = slab_alloc_node(s, NULL, gfpflags, NUMA_NO_NODE, void *ret = slab_alloc_node(s, NULL, gfpflags, NUMA_NO_NODE,
_RET_IP_, size); _RET_IP_, size);
...@@ -4152,9 +4152,9 @@ void *kmalloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size) ...@@ -4152,9 +4152,9 @@ void *kmalloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
ret = kasan_kmalloc(s, ret, size, gfpflags); ret = kasan_kmalloc(s, ret, size, gfpflags);
return ret; return ret;
} }
EXPORT_SYMBOL(kmalloc_trace); EXPORT_SYMBOL(kmalloc_trace_noprof);
void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gfpflags, void *kmalloc_node_trace_noprof(struct kmem_cache *s, gfp_t gfpflags,
int node, size_t size) int node, size_t size)
{ {
void *ret = slab_alloc_node(s, NULL, gfpflags, node, _RET_IP_, size); void *ret = slab_alloc_node(s, NULL, gfpflags, node, _RET_IP_, size);
...@@ -4164,7 +4164,7 @@ void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gfpflags, ...@@ -4164,7 +4164,7 @@ void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gfpflags,
ret = kasan_kmalloc(s, ret, size, gfpflags); ret = kasan_kmalloc(s, ret, size, gfpflags);
return ret; return ret;
} }
EXPORT_SYMBOL(kmalloc_node_trace); EXPORT_SYMBOL(kmalloc_node_trace_noprof);
static noinline void free_to_partial_list( static noinline void free_to_partial_list(
struct kmem_cache *s, struct slab *slab, struct kmem_cache *s, struct slab *slab,
...@@ -4769,7 +4769,7 @@ static int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, ...@@ -4769,7 +4769,7 @@ static int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags,
#endif /* CONFIG_SLUB_TINY */ #endif /* CONFIG_SLUB_TINY */
/* Note that interrupts must be enabled when calling this function. */ /* Note that interrupts must be enabled when calling this function. */
int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, int kmem_cache_alloc_bulk_noprof(struct kmem_cache *s, gfp_t flags, size_t size,
void **p) void **p)
{ {
int i; int i;
...@@ -4798,7 +4798,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, ...@@ -4798,7 +4798,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
return i; return i;
} }
EXPORT_SYMBOL(kmem_cache_alloc_bulk); EXPORT_SYMBOL(kmem_cache_alloc_bulk_noprof);
/* /*
......
...@@ -124,16 +124,16 @@ EXPORT_SYMBOL(kstrndup); ...@@ -124,16 +124,16 @@ EXPORT_SYMBOL(kstrndup);
* Return: newly allocated copy of @src or %NULL in case of error, * Return: newly allocated copy of @src or %NULL in case of error,
* result is physically contiguous. Use kfree() to free. * result is physically contiguous. Use kfree() to free.
*/ */
void *kmemdup(const void *src, size_t len, gfp_t gfp) void *kmemdup_noprof(const void *src, size_t len, gfp_t gfp)
{ {
void *p; void *p;
p = kmalloc_track_caller(len, gfp); p = kmalloc_node_track_caller_noprof(len, gfp, NUMA_NO_NODE, _RET_IP_);
if (p) if (p)
memcpy(p, src, len); memcpy(p, src, len);
return p; return p;
} }
EXPORT_SYMBOL(kmemdup); EXPORT_SYMBOL(kmemdup_noprof);
/** /**
* kmemdup_array - duplicate a given array. * kmemdup_array - duplicate a given array.
...@@ -609,7 +609,7 @@ EXPORT_SYMBOL(vm_mmap); ...@@ -609,7 +609,7 @@ EXPORT_SYMBOL(vm_mmap);
* *
* Return: pointer to the allocated memory of %NULL in case of failure * Return: pointer to the allocated memory of %NULL in case of failure
*/ */
void *kvmalloc_node(size_t size, gfp_t flags, int node) void *kvmalloc_node_noprof(size_t size, gfp_t flags, int node)
{ {
gfp_t kmalloc_flags = flags; gfp_t kmalloc_flags = flags;
void *ret; void *ret;
...@@ -631,7 +631,7 @@ void *kvmalloc_node(size_t size, gfp_t flags, int node) ...@@ -631,7 +631,7 @@ void *kvmalloc_node(size_t size, gfp_t flags, int node)
kmalloc_flags &= ~__GFP_NOFAIL; kmalloc_flags &= ~__GFP_NOFAIL;
} }
ret = kmalloc_node(size, kmalloc_flags, node); ret = kmalloc_node_noprof(size, kmalloc_flags, node);
/* /*
* It doesn't really make sense to fallback to vmalloc for sub page * It doesn't really make sense to fallback to vmalloc for sub page
...@@ -660,7 +660,7 @@ void *kvmalloc_node(size_t size, gfp_t flags, int node) ...@@ -660,7 +660,7 @@ void *kvmalloc_node(size_t size, gfp_t flags, int node)
flags, PAGE_KERNEL, VM_ALLOW_HUGE_VMAP, flags, PAGE_KERNEL, VM_ALLOW_HUGE_VMAP,
node, __builtin_return_address(0)); node, __builtin_return_address(0));
} }
EXPORT_SYMBOL(kvmalloc_node); EXPORT_SYMBOL(kvmalloc_node_noprof);
/** /**
* kvfree() - Free memory. * kvfree() - Free memory.
...@@ -699,7 +699,7 @@ void kvfree_sensitive(const void *addr, size_t len) ...@@ -699,7 +699,7 @@ void kvfree_sensitive(const void *addr, size_t len)
} }
EXPORT_SYMBOL(kvfree_sensitive); EXPORT_SYMBOL(kvfree_sensitive);
void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags) void *kvrealloc_noprof(const void *p, size_t oldsize, size_t newsize, gfp_t flags)
{ {
void *newp; void *newp;
...@@ -712,7 +712,7 @@ void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags) ...@@ -712,7 +712,7 @@ void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags)
kvfree(p); kvfree(p);
return newp; return newp;
} }
EXPORT_SYMBOL(kvrealloc); EXPORT_SYMBOL(kvrealloc_noprof);
/** /**
* __vmalloc_array - allocate memory for a virtually contiguous array. * __vmalloc_array - allocate memory for a virtually contiguous array.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment