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

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>
---
 include/linux/compiler_attributes.h | 2 ++
 net/core/skbuff.c                   | 3 ++-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/include/linux/compiler_attributes.h b/include/linux/compiler_attributes.h
index 445e80517cab..51f7c13bca98 100644
--- a/include/linux/compiler_attributes.h
+++ b/include/linux/compiler_attributes.h
@@ -371,4 +371,6 @@
  */
 #define __weak                          __attribute__((__weak__))
 
+#define __nofnsplit                     __attribute__((optimize("O1")))
+
 #endif /* __LINUX_COMPILER_ATTRIBUTES_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] 5+ messages in thread

* Re: [PATCH net-next] net: skb: prevent the split of kfree_skb_reason() by gcc
  2022-08-11 12:07 [PATCH net-next] net: skb: prevent the split of kfree_skb_reason() by gcc menglong8.dong
@ 2022-08-11 14:34 ` Miguel Ojeda
  2022-08-11 14:35   ` Miguel Ojeda
  2022-08-11 18:27 ` kernel test robot
  1 sibling, 1 reply; 5+ messages in thread
From: Miguel Ojeda @ 2022-08-11 14:34 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

On Thu, Aug 11, 2022 at 2:07 PM <menglong8.dong@gmail.com> wrote:
>
> diff --git a/include/linux/compiler_attributes.h b/include/linux/compiler_attributes.h
> index 445e80517cab..51f7c13bca98 100644
> --- a/include/linux/compiler_attributes.h
> +++ b/include/linux/compiler_attributes.h
> @@ -371,4 +371,6 @@
>   */
>  #define __weak                          __attribute__((__weak__))
>
> +#define __nofnsplit                     __attribute__((optimize("O1")))
> +
>  #endif /* __LINUX_COMPILER_ATTRIBUTES_H */

Two notes on this: please use the double underscore form:
`__optimize__` and keep the file sorted (it should go after
`__overloadable__`, since we sort by the actual attribute name).

Thanks!

Cheers,
Miguel

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

* Re: [PATCH net-next] net: skb: prevent the split of kfree_skb_reason() by gcc
  2022-08-11 14:34 ` Miguel Ojeda
@ 2022-08-11 14:35   ` Miguel Ojeda
  2022-08-12  2:30     ` Menglong Dong
  0 siblings, 1 reply; 5+ messages in thread
From: Miguel Ojeda @ 2022-08-11 14:35 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

On Thu, Aug 11, 2022 at 4:34 PM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> Two notes on this: please use the double underscore form:
> `__optimize__` and keep the file sorted (it should go after
> `__overloadable__`, since we sort by the actual attribute name).

s/after/before

Cheers,
Miguel

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

* Re: [PATCH net-next] net: skb: prevent the split of kfree_skb_reason() by gcc
  2022-08-11 12:07 [PATCH net-next] net: skb: prevent the split of kfree_skb_reason() by gcc menglong8.dong
  2022-08-11 14:34 ` Miguel Ojeda
@ 2022-08-11 18:27 ` kernel test robot
  1 sibling, 0 replies; 5+ messages in thread
From: kernel test robot @ 2022-08-11 18:27 UTC (permalink / raw)
  To: menglong8.dong, kuba
  Cc: llvm, kbuild-all, ojeda, ndesaulniers, davem, edumazet, pabeni,
	asml.silence, imagedong, luiz.von.dentz, vasily.averin, jk,
	linux-kernel, netdev

Hi,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on net-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/menglong8-dong-gmail-com/net-skb-prevent-the-split-of-kfree_skb_reason-by-gcc/20220811-200919
base:   https://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git f86d1fbbe7858884d6754534a0afbb74fc30bc26
config: riscv-randconfig-r042-20220811 (https://download.01.org/0day-ci/archive/20220812/202208120238.cz3RvV74-lkp@intel.com/config)
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project 5f1c7e2cc5a3c07cbc2412e851a7283c1841f520)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install riscv cross compiling tool for clang build
        # apt-get install binutils-riscv64-linux-gnu
        # https://github.com/intel-lab-lkp/linux/commit/8e014b654d9e51abed132155237d855b25da06a0
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review menglong8-dong-gmail-com/net-skb-prevent-the-split-of-kfree_skb_reason-by-gcc/20220811-200919
        git checkout 8e014b654d9e51abed132155237d855b25da06a0
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=riscv SHELL=/bin/bash net/core/

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> net/core/skbuff.c:780:6: warning: unknown attribute 'optimize' ignored [-Wunknown-attributes]
   void __nofnsplit
        ^~~~~~~~~~~
   include/linux/compiler_attributes.h:374:56: note: expanded from macro '__nofnsplit'
   #define __nofnsplit                     __attribute__((optimize("O1")))
                                                          ^~~~~~~~~~~~~~
   1 warning generated.


vim +/optimize +780 net/core/skbuff.c

   770	
   771	/**
   772	 *	kfree_skb_reason - free an sk_buff with special reason
   773	 *	@skb: buffer to free
   774	 *	@reason: reason why this skb is dropped
   775	 *
   776	 *	Drop a reference to the buffer and free it if the usage count has
   777	 *	hit zero. Meanwhile, pass the drop reason to 'kfree_skb'
   778	 *	tracepoint.
   779	 */
 > 780	void __nofnsplit
   781	kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason)
   782	{
   783		if (!skb_unref(skb))
   784			return;
   785	
   786		DEBUG_NET_WARN_ON_ONCE(reason <= 0 || reason >= SKB_DROP_REASON_MAX);
   787	
   788		trace_kfree_skb(skb, __builtin_return_address(0), reason);
   789		__kfree_skb(skb);
   790	}
   791	EXPORT_SYMBOL(kfree_skb_reason);
   792	

-- 
0-DAY CI Kernel Test Service
https://01.org/lkp

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

* Re: [PATCH net-next] net: skb: prevent the split of kfree_skb_reason() by gcc
  2022-08-11 14:35   ` Miguel Ojeda
@ 2022-08-12  2:30     ` Menglong Dong
  0 siblings, 0 replies; 5+ messages in thread
From: Menglong Dong @ 2022-08-12  2: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

On Thu, Aug 11, 2022 at 10:35 PM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> On Thu, Aug 11, 2022 at 4:34 PM Miguel Ojeda
> <miguel.ojeda.sandonis@gmail.com> wrote:
> >
> > Two notes on this: please use the double underscore form:
> > `__optimize__` and keep the file sorted (it should go after
> > `__overloadable__`, since we sort by the actual attribute name).
>
> s/after/before
>

Okay......Thanks for your reminds :/

Menglong Dong

> Cheers,
> Miguel

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

end of thread, other threads:[~2022-08-12  2:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-11 12:07 [PATCH net-next] net: skb: prevent the split of kfree_skb_reason() by gcc menglong8.dong
2022-08-11 14:34 ` Miguel Ojeda
2022-08-11 14:35   ` Miguel Ojeda
2022-08-12  2:30     ` Menglong Dong
2022-08-11 18:27 ` kernel test robot

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).