diff mbox series

[net] xdp: xdp_mem_allocator can be NULL in trace_mem_connect().

Message ID YiC0BwndXiwxGDNz@linutronix.de (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net] xdp: xdp_mem_allocator can be NULL in trace_mem_connect(). | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net
netdev/fixes_present success Fixes tag present in non-next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 55 this patch: 55
netdev/cc_maintainers success CCed 16 of 16 maintainers
netdev/build_clang success Errors and warnings before: 20 this patch: 20
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 67 this patch: 67
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 12 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Sebastian Sewior March 3, 2022, 12:26 p.m. UTC
Since the commit mentioned below __xdp_reg_mem_model() can return a NULL
pointer. This pointer is dereferenced in trace_mem_connect() which leads
to segfault. It can be reproduced with enabled trace events during ifup.

Only assign the arguments in the trace-event macro if `xa' is set.
Otherwise set the parameters to 0.

Fixes: 4a48ef70b93b8 ("xdp: Allow registering memory model without rxq reference")
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
 include/trace/events/xdp.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Toke Høiland-Jørgensen March 3, 2022, 1:59 p.m. UTC | #1
Sebastian Andrzej Siewior <bigeasy@linutronix.de> writes:

> Since the commit mentioned below __xdp_reg_mem_model() can return a NULL
> pointer. This pointer is dereferenced in trace_mem_connect() which leads
> to segfault. It can be reproduced with enabled trace events during ifup.
>
> Only assign the arguments in the trace-event macro if `xa' is set.
> Otherwise set the parameters to 0.
>
> Fixes: 4a48ef70b93b8 ("xdp: Allow registering memory model without rxq reference")
> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>

Hmm, so before the commit you mention, the tracepoint wasn't triggered
at all in the code path that now sets xdp_alloc is NULL. So I'm
wondering if we should just do the same here? Is the trace event useful
in all cases?

Alternatively, if we keep it, I think the mem.id and mem.type should be
available from rxq->mem, right?

-Toke
Sebastian Sewior March 3, 2022, 2:12 p.m. UTC | #2
On 2022-03-03 14:59:47 [+0100], Toke Høiland-Jørgensen wrote:
> Sebastian Andrzej Siewior <bigeasy@linutronix.de> writes:
> 
> > Since the commit mentioned below __xdp_reg_mem_model() can return a NULL
> > pointer. This pointer is dereferenced in trace_mem_connect() which leads
> > to segfault. It can be reproduced with enabled trace events during ifup.
> >
> > Only assign the arguments in the trace-event macro if `xa' is set.
> > Otherwise set the parameters to 0.
> >
> > Fixes: 4a48ef70b93b8 ("xdp: Allow registering memory model without rxq reference")
> > Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> 
> Hmm, so before the commit you mention, the tracepoint wasn't triggered
> at all in the code path that now sets xdp_alloc is NULL. So I'm
> wondering if we should just do the same here? Is the trace event useful
> in all cases?

Correct. It says:
|              ip-1230    [003] .....     3.053473: mem_connect: mem_id=0 mem_type=PAGE_SHARED allocator=0000000000000000 ifindex=2

> Alternatively, if we keep it, I think the mem.id and mem.type should be
> available from rxq->mem, right?

Yes, if these are the same things. In my case they are also 0:

|              ip-1245    [000] .....     3.045684: mem_connect: mem_id=0 mem_type=PAGE_SHARED allocator=0000000000000000 ifindex=2
|        ifconfig-1332    [003] .....    21.030879: mem_connect: mem_id=0 mem_type=PAGE_SHARED allocator=0000000000000000 ifindex=3

So depends on what makes sense that tp can be skipped for xa == NULL or
remain with
               __entry->mem_id         = rxq->mem.id;
               __entry->mem_type       = rxq->mem.type;
	       __entry->allocator      = xa ? xa->allocator : NULL;

if it makes sense.

> -Toke

Sebastian
Jesper Dangaard Brouer March 3, 2022, 5:31 p.m. UTC | #3
On 03/03/2022 15.12, Sebastian Andrzej Siewior wrote:
> On 2022-03-03 14:59:47 [+0100], Toke Høiland-Jørgensen wrote:
>> Sebastian Andrzej Siewior <bigeasy@linutronix.de> writes:
>>
>>> Since the commit mentioned below __xdp_reg_mem_model() can return a NULL
>>> pointer. This pointer is dereferenced in trace_mem_connect() which leads
>>> to segfault. It can be reproduced with enabled trace events during ifup.
>>>
>>> Only assign the arguments in the trace-event macro if `xa' is set.
>>> Otherwise set the parameters to 0.
>>>
>>> Fixes: 4a48ef70b93b8 ("xdp: Allow registering memory model without rxq reference")
>>> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
>>
>> Hmm, so before the commit you mention, the tracepoint wasn't triggered
>> at all in the code path that now sets xdp_alloc is NULL. So I'm
>> wondering if we should just do the same here? Is the trace event useful
>> in all cases?
> 
> Correct. It says:
> |              ip-1230    [003] .....     3.053473: mem_connect: mem_id=0 mem_type=PAGE_SHARED allocator=0000000000000000 ifindex=2
> 
>> Alternatively, if we keep it, I think the mem.id and mem.type should be
>> available from rxq->mem, right?
> 
> Yes, if these are the same things. In my case they are also 0:
> 
> |              ip-1245    [000] .....     3.045684: mem_connect: mem_id=0 mem_type=PAGE_SHARED allocator=0000000000000000 ifindex=2
> |        ifconfig-1332    [003] .....    21.030879: mem_connect: mem_id=0 mem_type=PAGE_SHARED allocator=0000000000000000 ifindex=3
> 
> So depends on what makes sense that tp can be skipped for xa == NULL or
> remain with
>                 __entry->mem_id         = rxq->mem.id;
>                 __entry->mem_type       = rxq->mem.type;
> 	       __entry->allocator      = xa ? xa->allocator : NULL;
> 
> if it makes sense.
> 

I have two bpftrace scripts [1] and [2] that use this tracepoint.
It is scripts to help driver developers detect memory leaks when using 
page_pool from their drivers.

I'm a little pressured on time, so I've not evaluated if your change 
makes sense.
In the scripts I do use both mem.id and mem.type.


[1] 
https://github.com/xdp-project/xdp-project/blob/master/areas/mem/bpftrace/xdp_mem_track01.bt

[2] 
https://github.com/xdp-project/xdp-project/blob/master/areas/mem/bpftrace/xdp_mem_track02.bt

--Jesper
Toke Høiland-Jørgensen March 7, 2022, 4:50 p.m. UTC | #4
Sebastian Andrzej Siewior <bigeasy@linutronix.de> writes:

> On 2022-03-03 14:59:47 [+0100], Toke Høiland-Jørgensen wrote:
>> Sebastian Andrzej Siewior <bigeasy@linutronix.de> writes:
>> 
>> > Since the commit mentioned below __xdp_reg_mem_model() can return a NULL
>> > pointer. This pointer is dereferenced in trace_mem_connect() which leads
>> > to segfault. It can be reproduced with enabled trace events during ifup.
>> >
>> > Only assign the arguments in the trace-event macro if `xa' is set.
>> > Otherwise set the parameters to 0.
>> >
>> > Fixes: 4a48ef70b93b8 ("xdp: Allow registering memory model without rxq reference")
>> > Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
>> 
>> Hmm, so before the commit you mention, the tracepoint wasn't triggered
>> at all in the code path that now sets xdp_alloc is NULL. So I'm
>> wondering if we should just do the same here? Is the trace event useful
>> in all cases?
>
> Correct. It says:
> |              ip-1230    [003] .....     3.053473: mem_connect: mem_id=0 mem_type=PAGE_SHARED allocator=0000000000000000 ifindex=2
>
>> Alternatively, if we keep it, I think the mem.id and mem.type should be
>> available from rxq->mem, right?
>
> Yes, if these are the same things. In my case they are also 0:
>
> |              ip-1245    [000] .....     3.045684: mem_connect: mem_id=0 mem_type=PAGE_SHARED allocator=0000000000000000 ifindex=2
> |        ifconfig-1332    [003] .....    21.030879: mem_connect: mem_id=0 mem_type=PAGE_SHARED allocator=0000000000000000 ifindex=3
>
> So depends on what makes sense that tp can be skipped for xa == NULL or
> remain with
>                __entry->mem_id         = rxq->mem.id;
>                __entry->mem_type       = rxq->mem.type;
> 	       __entry->allocator      = xa ? xa->allocator : NULL;
>
> if it makes sense.

Right, looking at the code again, the id is only assigned in the path
that doesn't return NULL from __xdp_reg_mem_model().

Given that the trace points were put in specifically to be able to pair
connect/disconnect using the IDs, I don't think there's any use to
creating the events if there's no ID, so I think we should fix it by
skipping the trace event entirely if xdp_alloc is NULL.

-Toke
Sebastian Sewior March 7, 2022, 5:59 p.m. UTC | #5
On 2022-03-07 17:50:04 [+0100], Toke Høiland-Jørgensen wrote:
> 
> Right, looking at the code again, the id is only assigned in the path
> that doesn't return NULL from __xdp_reg_mem_model().
> 
> Given that the trace points were put in specifically to be able to pair
> connect/disconnect using the IDs, I don't think there's any use to
> creating the events if there's no ID, so I think we should fix it by
> skipping the trace event entirely if xdp_alloc is NULL.

This sounds like a reasonable explanation. If nobody disagrees then I
post a new patch tomorrow and try to recycle some of what you wrote :)

> -Toke

Sebastian
Toke Høiland-Jørgensen March 7, 2022, 6:07 p.m. UTC | #6
Sebastian Andrzej Siewior <bigeasy@linutronix.de> writes:

> On 2022-03-07 17:50:04 [+0100], Toke Høiland-Jørgensen wrote:
>> 
>> Right, looking at the code again, the id is only assigned in the path
>> that doesn't return NULL from __xdp_reg_mem_model().
>> 
>> Given that the trace points were put in specifically to be able to pair
>> connect/disconnect using the IDs, I don't think there's any use to
>> creating the events if there's no ID, so I think we should fix it by
>> skipping the trace event entirely if xdp_alloc is NULL.
>
> This sounds like a reasonable explanation. If nobody disagrees then I
> post a new patch tomorrow and try to recycle some of what you wrote :)

SGTM :)

-Toke
Jakub Kicinski March 9, 2022, 5:15 p.m. UTC | #7
On Mon, 07 Mar 2022 19:07:37 +0100 Toke Høiland-Jørgensen wrote:
> Sebastian Andrzej Siewior <bigeasy@linutronix.de> writes:
> 
> > On 2022-03-07 17:50:04 [+0100], Toke Høiland-Jørgensen wrote:  
> >> 
> >> Right, looking at the code again, the id is only assigned in the path
> >> that doesn't return NULL from __xdp_reg_mem_model().
> >> 
> >> Given that the trace points were put in specifically to be able to pair
> >> connect/disconnect using the IDs, I don't think there's any use to
> >> creating the events if there's no ID, so I think we should fix it by
> >> skipping the trace event entirely if xdp_alloc is NULL.  
> >
> > This sounds like a reasonable explanation. If nobody disagrees then I
> > post a new patch tomorrow and try to recycle some of what you wrote :)  
> 
> SGTM :)

Was the patch posted? This seems to be a 5.17 thing, so it'd be really
really good if the fix was in net by tomorrow morning! :S
Sebastian Sewior March 9, 2022, 8:48 p.m. UTC | #8
On 2022-03-09 09:15:08 [-0800], Jakub Kicinski wrote:
> Was the patch posted? This seems to be a 5.17 thing, so it'd be really
> really good if the fix was in net by tomorrow morning! :S

Just posted v2.

Sebastian
Jakub Kicinski March 9, 2022, 9:03 p.m. UTC | #9
On Wed, 9 Mar 2022 21:48:08 +0100 Sebastian Andrzej Siewior wrote:
> On 2022-03-09 09:15:08 [-0800], Jakub Kicinski wrote:
> > Was the patch posted? This seems to be a 5.17 thing, so it'd be really
> > really good if the fix was in net by tomorrow morning! :S  
> 
> Just posted v2.

Perfect, thank you!
diff mbox series

Patch

diff --git a/include/trace/events/xdp.h b/include/trace/events/xdp.h
index c40fc97f94171..9196dcd08e6c7 100644
--- a/include/trace/events/xdp.h
+++ b/include/trace/events/xdp.h
@@ -362,9 +362,9 @@  TRACE_EVENT(mem_connect,
 
 	TP_fast_assign(
 		__entry->xa		= xa;
-		__entry->mem_id		= xa->mem.id;
-		__entry->mem_type	= xa->mem.type;
-		__entry->allocator	= xa->allocator;
+		__entry->mem_id		= xa ? xa->mem.id : 0;
+		__entry->mem_type	= xa ? xa->mem.type : 0;
+		__entry->allocator	= xa ? xa->allocator : NULL;
 		__entry->rxq		= rxq;
 		__entry->ifindex	= rxq->dev->ifindex;
 	),