linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next v3] net: skb: prevent the split of kfree_skb_reason() by gcc
@ 2022-08-15  6:27 menglong8.dong
  2022-08-15 11:52 ` Miguel Ojeda
  0 siblings, 1 reply; 3+ messages in thread
From: menglong8.dong @ 2022-08-15  6:27 UTC (permalink / raw)
  To: kuba, miguel.ojeda.sandonis
  Cc: ojeda, ndesaulniers, davem, edumazet, pabeni, asml.silence,
	imagedong, luiz.von.dentz, vasily.averin, jk, linux-kernel,
	netdev, kernel test robot

From: Menglong Dong <imagedong@tencent.com>

Sometimes, gcc will optimize the function by spliting it to two or
more functions. In this case, kfree_skb_reason() is splited to
kfree_skb_reason and kfree_skb_reason.part.0. However, the
function/tracepoint trace_kfree_skb() in it needs the return address
of kfree_skb_reason().

This split makes the call chains becomes:
  kfree_skb_reason() -> kfree_skb_reason.part.0 -> trace_kfree_skb()

which makes the return address that passed to trace_kfree_skb() be
kfree_skb().

Therefore, prevent this kind of optimization to kfree_skb_reason() by
making the optimize level to "O1". I think these should be better
method instead of this "O1", but I can't figure it out......

This optimization CAN happen, which depend on the behavior of gcc.
I'm not able to reproduce it in the latest kernel code, but it happens
in my kernel of version 5.4.119. Maybe the latest code already do someting
that prevent this happen?

Signed-off-by: Menglong Dong <imagedong@tencent.com>
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
---
v3:
- define __nofnsplit only for GCC
- add some document

v2:
- replace 'optimize' with '__optimize__' in __nofnsplit, as Miguel Ojeda
  advised.
---
 include/linux/compiler-gcc.h   | 12 ++++++++++++
 include/linux/compiler_types.h |  4 ++++
 net/core/skbuff.c              |  3 ++-
 3 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h
index a0c55eeaeaf1..8d6d4d7b21a4 100644
--- a/include/linux/compiler-gcc.h
+++ b/include/linux/compiler-gcc.h
@@ -157,3 +157,15 @@
 #if GCC_VERSION < 90100
 #undef __alloc_size__
 #endif
+
+/*
+ * Prevent function from being splited to multiple part. As what the
+ * document says in gcc/ipa-split.cc, single function will be splited
+ * when necessary:
+ *
+ *   https://github.com/gcc-mirror/gcc/blob/master/gcc/ipa-split.cc
+ *
+ * This optimization seems only take effect on O2 and O3 optimize level.
+ * Therefore, make the optimize level to O1 to prevent this optimization.
+ */
+#define __nofnsplit		__attribute__((__optimize__("O1")))
diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index 4f2a819fd60a..e76cfff36491 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -380,4 +380,8 @@ struct ftrace_likely_data {
 #define __diag_ignore_all(option, comment)
 #endif
 
+#ifndef __nofnsplit
+#define __nofnsplit
+#endif
+
 #endif /* __LINUX_COMPILER_TYPES_H */
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 974bbbbe7138..ff9ccbc032b9 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -777,7 +777,8 @@ EXPORT_SYMBOL(__kfree_skb);
  *	hit zero. Meanwhile, pass the drop reason to 'kfree_skb'
  *	tracepoint.
  */
-void kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason)
+void __nofnsplit
+kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason)
 {
 	if (!skb_unref(skb))
 		return;
-- 
2.36.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net-next v3] net: skb: prevent the split of kfree_skb_reason() by gcc
  2022-08-15  6:27 [PATCH net-next v3] net: skb: prevent the split of kfree_skb_reason() by gcc menglong8.dong
@ 2022-08-15 11:52 ` Miguel Ojeda
  2022-08-16  3:30   ` Menglong Dong
  0 siblings, 1 reply; 3+ messages in thread
From: Miguel Ojeda @ 2022-08-15 11:52 UTC (permalink / raw)
  To: menglong8.dong
  Cc: kuba, ojeda, ndesaulniers, davem, edumazet, pabeni, asml.silence,
	imagedong, luiz.von.dentz, vasily.averin, jk, linux-kernel,
	netdev, kernel test robot

On Mon, Aug 15, 2022 at 8:27 AM <menglong8.dong@gmail.com> wrote:
>
>  include/linux/compiler-gcc.h   | 12 ++++++++++++
>  include/linux/compiler_types.h |  4 ++++

No, this should be in `compiler_attributes.h` like you had it before.

To be clear, what you did here would be fine, but it is the "old way"
(we added `compiler_attributes.h` to reduce the complexity of
`compiler-*` and `compiler_types.h` and make it a bit more
normalized).

Please take a moment and read how other attributes do it in
`compiler_attributes.h` with `__has_attribute`. Check, for instance,
`__copy`, which is very similar to your case (not supported by Clang
and ICC, except in your case GCC always supports at least since 5.1).

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net-next v3] net: skb: prevent the split of kfree_skb_reason() by gcc
  2022-08-15 11:52 ` Miguel Ojeda
@ 2022-08-16  3:30   ` Menglong Dong
  0 siblings, 0 replies; 3+ messages in thread
From: Menglong Dong @ 2022-08-16  3:30 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: kuba, ojeda, ndesaulniers, davem, edumazet, pabeni, asml.silence,
	imagedong, luiz.von.dentz, vasily.averin, jk, linux-kernel,
	netdev, kernel test robot

On Mon, Aug 15, 2022 at 7:52 PM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> On Mon, Aug 15, 2022 at 8:27 AM <menglong8.dong@gmail.com> wrote:
> >
> >  include/linux/compiler-gcc.h   | 12 ++++++++++++
> >  include/linux/compiler_types.h |  4 ++++
>
> No, this should be in `compiler_attributes.h` like you had it before.
>
> To be clear, what you did here would be fine, but it is the "old way"
> (we added `compiler_attributes.h` to reduce the complexity of
> `compiler-*` and `compiler_types.h` and make it a bit more
> normalized).
>
> Please take a moment and read how other attributes do it in
> `compiler_attributes.h` with `__has_attribute`. Check, for instance,
> `__copy`, which is very similar to your case (not supported by Clang
> and ICC, except in your case GCC always supports at least since 5.1).
>

Okay, I think I'm getting it now! Thank you, and I'm sending the V4.

Menglong Dong

> Cheers,
> Miguel

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-08-16  7:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-15  6:27 [PATCH net-next v3] net: skb: prevent the split of kfree_skb_reason() by gcc menglong8.dong
2022-08-15 11:52 ` Miguel Ojeda
2022-08-16  3:30   ` Menglong Dong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).