Skip to content

Commit 6a5f12b

Browse files
Tomasz Bursztykajukkar
authored andcommitted
net/pkt: Add a debug option to track free pkt access more easily
Now that net_pkt are accessed through a common r/w API, using below a net_pkt_cursor, let's have an option that will reset this cursor once the net_pkt is freed. Result is instead of segfaulting on r/w access, these operations will bail out properly. Subsequent, and logical (unless you have a leak which is another issue) net_pkt_unref will tell you who/where the pkt was freed. Without it, you will get a segfault for instance, but that won't tell you the exact reason. This options can help you then. Signed-off-by: Tomasz Bursztyka <[email protected]>
1 parent 6596c30 commit 6a5f12b

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

subsys/net/ip/Kconfig.debug

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ config NET_DEBUG_NET_PKT_EXTERNALS
3636
This value is used when allocating space for tracking the
3737
memory allocations.
3838

39+
config NET_DEBUG_NET_PKT_NON_FRAGILE_ACCESS
40+
bool "Reduce r/w fragility by resetting the packet cursor when freed"
41+
default n
42+
select NET_DEBUG_NET_PKT_ALLOC
43+
help
44+
This MUST not be used unless you have an hard to catch bug. This will
45+
reset the pkt cursor when it's freed, so any subsequent r/w operations
46+
will not segfault, but just bail out and hopefuly it will enable you
47+
to know who/where the packet was freed already. Do not set this, by
48+
any means, unless you are actively debugging.
49+
3950
if !NET_RAW_MODE
4051

4152
module = NET_CORE

subsys/net/ip/net_pkt.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,11 @@ void net_pkt_unref(struct net_pkt *pkt)
817817
net_pkt_frag_unref(pkt->frags);
818818
}
819819

820+
if (IS_ENABLED(CONFIG_NET_DEBUG_NET_PKT_NON_FRAGILE_ACCESS)) {
821+
pkt->buffer = NULL;
822+
net_pkt_cursor_init(pkt);
823+
}
824+
820825
k_mem_slab_free(pkt->slab, (void **)&pkt);
821826
}
822827

0 commit comments

Comments
 (0)