]> git.proxmox.com Git - mirror_ubuntu-kernels.git/commitdiff
net: skb: prevent the split of kfree_skb_reason() by gcc
authorMenglong Dong <imagedong@tencent.com>
Sun, 21 Aug 2022 05:18:58 +0000 (13:18 +0800)
committerDavid S. Miller <davem@davemloft.net>
Wed, 24 Aug 2022 08:47:51 +0000 (09:47 +0100)
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, introduce '__fix_address', which is the combination of
'__noclone' and 'noinline', and apply it to kfree_skb_reason() to
prevent to from being splited or made inline.

(Is it better to simply apply '__noclone oninline' to kfree_skb_reason?
I'm thinking maybe other functions have the same problems)

Meanwhile, wrap 'skb_unref()' with 'unlikely()', as the compiler thinks
it is likely return true and splits kfree_skb_reason().

Signed-off-by: Menglong Dong <imagedong@tencent.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/compiler_attributes.h
include/linux/skbuff.h
net/core/skbuff.c

index 445e80517cab689c7ff178f4fa22844c5f14739d..fc93c9488c764944c641f107ee1ebd420922d6e0 100644 (file)
  */
 #define __weak                          __attribute__((__weak__))
 
+/*
+ * Used by functions that use '__builtin_return_address'. These function
+ * don't want to be splited or made inline, which can make
+ * the '__builtin_return_address' get unexpected address.
+ */
+#define __fix_address noinline __noclone
+
 #endif /* __LINUX_COMPILER_ATTRIBUTES_H */
index ca8afa382bf2936f12d34dce1aa1329a640802c5..b51d07a727c9f23f783f1512657a2aa7684e6179 100644 (file)
@@ -1195,7 +1195,8 @@ static inline bool skb_unref(struct sk_buff *skb)
        return true;
 }
 
-void kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason);
+void __fix_address
+kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason);
 
 /**
  *     kfree_skb - free an sk_buff with 'NOT_SPECIFIED' reason
index 974bbbbe7138a447e04d3a773d8c875574413516..35d9d5958dc684c67bacbd2af07fa764f7a3d8d8 100644 (file)
@@ -777,9 +777,10 @@ 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 __fix_address
+kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason)
 {
-       if (!skb_unref(skb))
+       if (unlikely(!skb_unref(skb)))
                return;
 
        DEBUG_NET_WARN_ON_ONCE(reason <= 0 || reason >= SKB_DROP_REASON_MAX);