Skip to content

Commit

Permalink
net: skb: add function name as part of reason
Browse files Browse the repository at this point in the history
In addition to the line number ("__LINE__"), this is to make the
function name ("__func__", which is the caller of kfree_skb_reason())
as part of reason.

The (function, line) combination is able to uniquely represent where the
sk_buff is dropped.

The existing trace_kfree_skb() is able to trace the 'location'. While it
is fine to either echo 'stacktrace' to the trigger, or trace at
userspace via ebpf, the TP_printk will only print %p.

With the function name, the TP_printk will tell the function and the
line number that the sk_buff is dropped.

Cc: Joao Martins <joao.m.martins@oracle.com>
Cc: Joe Jin <joe.jin@oracle.com>
Signed-off-by: Dongli Zhang <dongli.zhang@oracle.com>
  • Loading branch information
Dongli Zhang authored and intel-lab-lkp committed Feb 3, 2022
1 parent f4f541c commit f23dbdf
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
7 changes: 5 additions & 2 deletions include/linux/skbuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ struct sk_buff;

#define SKB_DROP_LINE_NONE 0
#define SKB_DROP_LINE __LINE__
#define SKB_DROP_FUNC_NONE "none"
#define SKB_DROP_FUNC __func__

/* To allow 64K frame to be packed as single skb without frag_list we
* require 64K/PAGE_SIZE pages plus 1 additional page to allow for
Expand Down Expand Up @@ -1090,15 +1092,16 @@ static inline bool skb_unref(struct sk_buff *skb)
return true;
}

void kfree_skb_reason(struct sk_buff *skb, unsigned int line);
void kfree_skb_reason(struct sk_buff *skb, const char *func,
unsigned int line);

/**
* kfree_skb - free an sk_buff with 'NOT_SPECIFIED' reason
* @skb: buffer to free
*/
static inline void kfree_skb(struct sk_buff *skb)
{
kfree_skb_reason(skb, SKB_DROP_LINE_NONE);
kfree_skb_reason(skb, SKB_DROP_FUNC_NONE, SKB_DROP_LINE_NONE);
}

void skb_release_head_state(struct sk_buff *skb);
Expand Down
10 changes: 6 additions & 4 deletions include/trace/events/skb.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,29 @@
TRACE_EVENT(kfree_skb,

TP_PROTO(struct sk_buff *skb, void *location,
unsigned int line),
const char *function, unsigned int line),

TP_ARGS(skb, location, line),
TP_ARGS(skb, location, function, line),

TP_STRUCT__entry(
__field(void *, skbaddr)
__field(void *, location)
__field(unsigned short, protocol)
__string(function, function)
__field(unsigned int, line)
),

TP_fast_assign(
__entry->skbaddr = skb;
__entry->location = location;
__entry->protocol = ntohs(skb->protocol);
__assign_str(function, function);
__entry->line = line;
),

TP_printk("skbaddr=%p protocol=%u location=%p line: %u",
TP_printk("skbaddr=%p protocol=%u location=%p function=%s line=%u",
__entry->skbaddr, __entry->protocol, __entry->location,
__entry->line)
__get_str(function), __entry->line)
);

TRACE_EVENT(consume_skb,
Expand Down
1 change: 1 addition & 0 deletions net/core/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -4900,6 +4900,7 @@ static __latent_entropy void net_tx_action(struct softirq_action *h)
trace_consume_skb(skb);
else
trace_kfree_skb(skb, net_tx_action,
SKB_DROP_FUNC,
SKB_DROP_LINE);

if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
Expand Down
9 changes: 6 additions & 3 deletions net/core/skbuff.c
Original file line number Diff line number Diff line change
Expand Up @@ -761,17 +761,20 @@ EXPORT_SYMBOL(__kfree_skb);
/**
* kfree_skb_reason - free an sk_buff with special reason
* @skb: buffer to free
* @func: the function where this skb is dropped
* @line: the line where this skb is dropped
*
* Drop a reference to the buffer and free it if the usage count has
* hit zero. Meanwhile, pass the drop line to 'kfree_skb' tracepoint.
* hit zero. Meanwhile, pass the drop function and line to 'kfree_skb'
* tracepoint.
*/
void kfree_skb_reason(struct sk_buff *skb, unsigned int line)
void kfree_skb_reason(struct sk_buff *skb, const char *func,
unsigned int line)
{
if (!skb_unref(skb))
return;

trace_kfree_skb(skb, __builtin_return_address(0), line);
trace_kfree_skb(skb, __builtin_return_address(0), func, line);
__kfree_skb(skb);
}
EXPORT_SYMBOL(kfree_skb_reason);
Expand Down
2 changes: 1 addition & 1 deletion net/ipv4/tcp_ipv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -2149,7 +2149,7 @@ int tcp_v4_rcv(struct sk_buff *skb)

discard_it:
/* Discard frame. */
kfree_skb_reason(skb, drop_line);
kfree_skb_reason(skb, SKB_DROP_FUNC, drop_line);
return 0;

discard_and_relse:
Expand Down
4 changes: 2 additions & 2 deletions net/ipv4/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2477,7 +2477,7 @@ int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
* Hmm. We got an UDP packet to a port to which we
* don't wanna listen. Ignore it.
*/
kfree_skb_reason(skb, drop_line);
kfree_skb_reason(skb, SKB_DROP_FUNC, drop_line);
return 0;

short_packet:
Expand All @@ -2502,7 +2502,7 @@ int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
__UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
drop:
__UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
kfree_skb_reason(skb, drop_line);
kfree_skb_reason(skb, SKB_DROP_FUNC, drop_line);
return 0;
}

Expand Down

0 comments on commit f23dbdf

Please sign in to comment.