mm/tracing: Add check for kmem_cache

Message ID 20221123111001.34855-1-jiasheng@iscas.ac.cn
State New
Headers
Series mm/tracing: Add check for kmem_cache |

Commit Message

Jiasheng Jiang Nov. 23, 2022, 11:10 a.m. UTC
  In __kmem_cache_alloc_lru() of mm/slab.c, cachep may be NULL when error
occurs in slab_alloc().
However, trace_kmem_cache_alloc() will still use it as the parameter,
which may cause NULL poineter dereference.
Therefore, it should be better to add the check and handle the error
in the trace_kmem_cache_alloc().

Fixes: 36555751c675 ("kmemtrace: SLAB hooks.")
Signed-off-by: Jiasheng Jiang <jiasheng@iscas.ac.cn>
---
 include/trace/events/kmem.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Vlastimil Babka Nov. 23, 2022, 1:17 p.m. UTC | #1
On 11/23/22 12:10, Jiasheng Jiang wrote:
> In __kmem_cache_alloc_lru() of mm/slab.c, cachep may be NULL when error
> occurs in slab_alloc().

Are you sure? AFAICS internally in slab_alloc_node(), slab_pre_alloc_hook()
can return NULL which is assigned to cachep local variable, but only NULL
propagated back to __kmem_cache_alloc_lru() is the object, not cachep.
So trace_kmem_cache_alloc() should only see NULL s, if it was already NULL
when passed to kmem_cache_alloc(), at which point the NULL is dereferenced
and crashing (as expected) earlier than reaching the tracepoint.

> However, trace_kmem_cache_alloc() will still use it as the parameter,
> which may cause NULL poineter dereference.
> Therefore, it should be better to add the check and handle the error
> in the trace_kmem_cache_alloc().
> 
> Fixes: 36555751c675 ("kmemtrace: SLAB hooks.")
> Signed-off-by: Jiasheng Jiang <jiasheng@iscas.ac.cn>
> ---
>  include/trace/events/kmem.h | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/include/trace/events/kmem.h b/include/trace/events/kmem.h
> index 243073cfc29d..d99507d32ef5 100644
> --- a/include/trace/events/kmem.h
> +++ b/include/trace/events/kmem.h
> @@ -32,8 +32,8 @@ TRACE_EVENT(kmem_cache_alloc,
>  	TP_fast_assign(
>  		__entry->call_site	= call_site;
>  		__entry->ptr		= ptr;
> -		__entry->bytes_req	= s->object_size;
> -		__entry->bytes_alloc	= s->size;
> +		__entry->bytes_req	= s ? s->object_size : 0;
> +		__entry->bytes_alloc	= s ? s->size : 0;
>  		__entry->gfp_flags	= (__force unsigned long)gfp_flags;
>  		__entry->node		= node;
>  		__entry->accounted	= IS_ENABLED(CONFIG_MEMCG_KMEM) ?
  

Patch

diff --git a/include/trace/events/kmem.h b/include/trace/events/kmem.h
index 243073cfc29d..d99507d32ef5 100644
--- a/include/trace/events/kmem.h
+++ b/include/trace/events/kmem.h
@@ -32,8 +32,8 @@  TRACE_EVENT(kmem_cache_alloc,
 	TP_fast_assign(
 		__entry->call_site	= call_site;
 		__entry->ptr		= ptr;
-		__entry->bytes_req	= s->object_size;
-		__entry->bytes_alloc	= s->size;
+		__entry->bytes_req	= s ? s->object_size : 0;
+		__entry->bytes_alloc	= s ? s->size : 0;
 		__entry->gfp_flags	= (__force unsigned long)gfp_flags;
 		__entry->node		= node;
 		__entry->accounted	= IS_ENABLED(CONFIG_MEMCG_KMEM) ?