Skip to contentSkip to conversation

Add noinline_for_tracing and apply it to tcp_drop_reason

PATCH Applied Yafang Shao 5 participants 11 messages Oct 24, 2024 – Nov 3, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Add noinline_for_tracing and apply it to tcp_drop_reason

This patchset introduces a new compiler annotation, noinline_for_tracing, designed to prevent specific functions from being inlined to facilitate tracing. In Patch #2, this annotation is applied to the tcp_drop_reason().

Yafang Shao (2):
compiler_types: Add noinline_for_tracing annotation
net: tcp: Add noinline_for_tracing annotation for tcp_drop_reason()

 include/linux/compiler_types.h | 6 ++++++
 net/ipv4/tcp_input.c           | 4 ++--
 2 files changed, 8 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.5
#1 lore raw

PATCH 1/2 compiler_types: Add noinline_for_tracing annotation

Kernel functions that are not inlined can be easily hooked with BPF for tracing. However, functions intended for tracing may still be inlined unexpectedly. For example, in our case, after upgrading the compiler from GCC 9 to GCC 11, the tcp_drop_reason() function was inlined, which broke our monitoring tools. To prevent this, we need to ensure that the function remains non-inlined.

The noinline_for_tracing annotation is introduced as a general solution for preventing inlining of kernel functions that need to be traced. This approach avoids the need for adding individual noinline comments to each function and provides a more consistent way to maintain traceability.

---
 include/linux/compiler_types.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/compiler_types.h +6 -0
index 1a957ea2f4fe..0c8b9601e603 100644
@@ -265,6 +265,12 @@ struct ftrace_likely_data {
  */
 #define noinline_for_stack noinline
 
+/*
+ * Use noinline_for_tracing for functions that should not be inlined.
+ * For tracing reasons.
+ */
+#define noinline_for_tracing noinline
+
 /*
  * Sanitizer helper attributes: Because using __always_inline and
  * __no_sanitize_* conflict, provide helper attributes that will either expand
Signature · 1 line
2.43.5
#2 lore raw

PATCH 2/2 net: tcp: Add noinline_for_tracing annotation for tcp_drop_reason()

In reply to Yafang Shao

We previously hooked the tcp_drop_reason() function using BPF to monitor TCP drop reasons. However, after upgrading our compiler from GCC 9 to GCC 11, tcp_drop_reason() is now inlined, preventing us from hooking into it. To address this, it would be beneficial to make noinline explicitly for tracing.

---
 net/ipv4/tcp_input.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp_input.c +2 -2
index cc05ec1faac8..cf1e2e3a7407 100644
@@ -4894,8 +4894,8 @@ static bool tcp_ooo_try_coalesce(struct sock *sk,
 	return res;
 }
 
-static void tcp_drop_reason(struct sock *sk, struct sk_buff *skb,
-			    enum skb_drop_reason reason)
+noinline_for_tracing static void
+tcp_drop_reason(struct sock *sk, struct sk_buff *skb, enum skb_drop_reason reason)
 {
 	sk_drops_add(sk, skb);
 	sk_skb_reason_drop(sk, skb, reason);
Signature · 1 line
2.43.5
#3 lore raw

Re: [PATCH 2/2] net: tcp: Add noinline_for_tracing annotation for tcp_drop_reason()

From: Yafang Shao <[email protected]>
Date: Thu, 24 Oct 2024 17:37:42 +0800

Quoted 10 lines

We previously hooked the tcp_drop_reason() function using BPF to monitor TCP drop reasons. However, after upgrading our compiler from GCC 9 to GCC 11, tcp_drop_reason() is now inlined, preventing us from hooking into it. To address this, it would be beneficial to make noinline explicitly for tracing.

I saw a somewhat related post yesterday.
https://x.com/__dxu/status/1849271647989068107

Daniel, could we apply your approach to this issue in the near future ?

Thread link: https://lore.kernel.org/netdev/[email protected]/

Quoted 22 lines
---
 net/ipv4/tcp_input.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp_input.c +2 -2
index cc05ec1faac8..cf1e2e3a7407 100644
@@ -4894,8 +4894,8 @@ static bool tcp_ooo_try_coalesce(struct sock *sk,
 	return res;
 }
 
-static void tcp_drop_reason(struct sock *sk, struct sk_buff *skb,
-			    enum skb_drop_reason reason)
+noinline_for_tracing static void
+tcp_drop_reason(struct sock *sk, struct sk_buff *skb, enum skb_drop_reason reason)
 {
 	sk_drops_add(sk, skb);
 	sk_skb_reason_drop(sk, skb, reason);
Signature · 1 line
2.43.5
#4 lore raw

Re: [PATCH 2/2] net: tcp: Add noinline_for_tracing annotation for tcp_drop_reason()

Hi Kuniyuki,

Quoted 8 lines · Kuniyuki Iwashima

From: Yafang Shao <[email protected]>
Date: Thu, 24 Oct 2024 17:37:42 +0800

Quoted 6 lines

We previously hooked the tcp_drop_reason() function using BPF to monitor TCP drop reasons. However, after upgrading our compiler from GCC 9 to GCC 11, tcp_drop_reason() is now inlined, preventing us from hooking into it. To address this, it would be beneficial to make noinline explicitly for tracing.

Quoted 7 lines
Quoted 4 lines

I saw a somewhat related post yesterday.
https://x.com/__dxu/status/1849271647989068107

Glad to hear you're interested!

Quoted 3 lines

Daniel, could we apply your approach to this issue in the near future ?

I suppose that depends on how you define "near". I'm being vague in that thread b/c we're still experimenting and have a couple things to look at still. But eventually, hopefully yes. Perhaps some time within a year if you had to press me.

Quoted 26 lines

Thread link:
https://lore.kernel.org/netdev/[email protected]/

Quoted 22 lines
---
 net/ipv4/tcp_input.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/tcp_input.c +2 -2
index cc05ec1faac8..cf1e2e3a7407 100644
@@ -4894,8 +4894,8 @@ static bool tcp_ooo_try_coalesce(struct sock *sk,
 	return res;
 }
 
-static void tcp_drop_reason(struct sock *sk, struct sk_buff *skb,
-			    enum skb_drop_reason reason)
+noinline_for_tracing static void
+tcp_drop_reason(struct sock *sk, struct sk_buff *skb, enum skb_drop_reason reason)
 {
 	sk_drops_add(sk, skb);
 	sk_skb_reason_drop(sk, skb, reason);
Signature · 1 line
2.43.5
#5 lore raw

Re: [PATCH 2/2] net: tcp: Add noinline_for_tracing annotation for tcp_drop_reason()

From: "Daniel Xu" <[email protected]>
Date: Thu, 24 Oct 2024 13:52:06 -0700

Quoted 22 lines

Hi Kuniyuki,

Quoted 8 lines · Kuniyuki Iwashima

From: Yafang Shao <[email protected]>
Date: Thu, 24 Oct 2024 17:37:42 +0800

Quoted 6 lines

We previously hooked the tcp_drop_reason() function using BPF to monitor TCP drop reasons. However, after upgrading our compiler from GCC 9 to GCC 11, tcp_drop_reason() is now inlined, preventing us from hooking into it. To address this, it would be beneficial to make noinline explicitly for tracing.

Quoted 7 lines
Quoted 4 lines

I saw a somewhat related post yesterday.
https://x.com/__dxu/status/1849271647989068107

Glad to hear you're interested!

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

Daniel, could we apply your approach to this issue in the near future ?

I suppose that depends on how you define "near". I'm being vague in that thread b/c we're still experimenting and have a couple things to look at still. But eventually, hopefully yes. Perhaps some time within a year if you had to press me.

Sorry, I didn't intend to rush you. I was just curious if we would need to revert the patch soon after being merged, so please take your time :)

Thanks!

#6 lore raw

Re: [PATCH 2/2] net: tcp: Add noinline_for_tracing annotation for tcp_drop_reason()

In reply to Yafang Shao

Hi Yafang,

Quoted 5 lines · Yafang Shao

We previously hooked the tcp_drop_reason() function using BPF to monitor TCP drop reasons. However, after upgrading our compiler from GCC 9 to GCC 11, tcp_drop_reason() is now inlined, preventing us from hooking into it. To address this, it would be beneficial to make noinline explicitly for tracing.

It looks like kfree_skb() tracepoint has rx_sk field now. Added in c53795d48ee8 ("net: add rx_sk to trace_kfree_skb").

Between sk and skb, is there enough information to monitor TCP drops? Or do you need something particular about tcp_drop_reason()?

Thanks,
Daniel

#7 lore raw

Re: [PATCH 2/2] net: tcp: Add noinline_for_tracing annotation for tcp_drop_reason()

Quoted 4 lines · Daniel Xu

Hi Yafang,

On Thu, Oct 24, 2024, at 2:37 AM, Yafang Shao wrote:

Quoted 8 lines
Quoted 5 lines

We previously hooked the tcp_drop_reason() function using BPF to monitor TCP drop reasons. However, after upgrading our compiler from GCC 9 to GCC 11, tcp_drop_reason() is now inlined, preventing us from hooking into it. To address this, it would be beneficial to make noinline explicitly for tracing.

It looks like kfree_skb() tracepoint has rx_sk field now. Added in c53795d48ee8 ("net: add rx_sk to trace_kfree_skb").

This commit is helpful. Thank you for providing the information. I plan to backport it to our local kernel.

Quoted 3 lines

Between sk and skb, is there enough information to monitor TCP drops? Or do you need something particular about tcp_drop_reason()?

There's nothing else specific to mention. The @rx_sk introduced in the commit you referred to will be beneficial to us.

BTW, it would be fantastic if we could trace inline functions. Additionally, can your feature[0] also allow for live patching of inline functions?

[0] https://x.com/__dxu/status/1849271647989068107

Signature · 2 lines
Regards
Yafang
maintainer
#8 lore raw

Re: [PATCH 2/2] net: tcp: Add noinline_for_tracing annotation for tcp_drop_reason()

Hi,

Quoted 15 lines · Yafang Shao
Quoted 9 lines · Daniel Xu
Quoted 5 lines · Yafang Shao

We previously hooked the tcp_drop_reason() function using BPF to monitor TCP drop reasons. However, after upgrading our compiler from GCC 9 to GCC 11, tcp_drop_reason() is now inlined, preventing us from hooking into it. To address this, it would be beneficial to make noinline explicitly for tracing.

It looks like kfree_skb() tracepoint has rx_sk field now. Added in c53795d48ee8 ("net: add rx_sk to trace_kfree_skb").

This commit is helpful. Thank you for providing the information. I plan to backport it to our local kernel.

Quoted 1 line
Quoted 5 lines
Quoted 2 lines

Between sk and skb, is there enough information to monitor TCP drops? Or do you need something particular about tcp_drop_reason()?

There's nothing else specific to mention. The @rx_sk introduced in the commit you referred to will be beneficial to us.

The implications of the above statement are not clear to me. Do you mean this patchset is not needed anymore?

Thanks!

Paolo

#9 lore raw

Re: [PATCH 2/2] net: tcp: Add noinline_for_tracing annotation for tcp_drop_reason()

Quoted 19 lines · Paolo Abeni

Hi,

Quoted 15 lines · Yafang Shao
Quoted 9 lines · Daniel Xu
Quoted 5 lines · Yafang Shao

We previously hooked the tcp_drop_reason() function using BPF to monitor TCP drop reasons. However, after upgrading our compiler from GCC 9 to GCC 11, tcp_drop_reason() is now inlined, preventing us from hooking into it. To address this, it would be beneficial to make noinline explicitly for tracing.

It looks like kfree_skb() tracepoint has rx_sk field now. Added in c53795d48ee8 ("net: add rx_sk to trace_kfree_skb").

This commit is helpful. Thank you for providing the information. I plan to backport it to our local kernel.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Between sk and skb, is there enough information to monitor TCP drops? Or do you need something particular about tcp_drop_reason()?

There's nothing else specific to mention. The @rx_sk introduced in the commit you referred to will be beneficial to us.

The implications of the above statement are not clear to me. Do you mean this patchset is not needed anymore?

The introduction of a dedicated tcp_drop_reason() function is intended to enhance the traceability of TCP drops for the user, providing more convenient and detailed insights. Additionally, since tcp_drop_reason() does not impact the critical path, marking it as noinline is acceptable. For these reasons, I believe this patchset remains necessary.

Signature · 2 lines
Regards
Yafang
#10 lore raw
In reply to Yafang Shao
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Thu, 24 Oct 2024 17:37:40 +0800 you wrote:

This patchset introduces a new compiler annotation, noinline_for_tracing, designed to prevent specific functions from being inlined to facilitate tracing. In Patch #2, this annotation is applied to the tcp_drop_reason().

Yafang Shao (2):
compiler_types: Add noinline_for_tracing annotation
net: tcp: Add noinline_for_tracing annotation for tcp_drop_reason()

[...]

Here is the summary with links:
- [1/2] compiler_types: Add noinline_for_tracing annotation

    https://git.kernel.org/netdev/net-next/c/a8f80673ca0d
  - [2/2] net: tcp: Add noinline_for_tracing annotation for tcp_drop_reason()
    https://git.kernel.org/netdev/net-next/c/dbd5e2e79ed8

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html