[PATCH net-next v3 0/2] Add an assert in napi_consume_skb()

STALE2087d LANDED

Revision v3 of 2 in this series; landed in mainline as 0e1f1cc89a77 on 2020-11-25.

4 messages, 2 authors, 2020-11-25 · open the first message on its own page

[PATCH net-next v3 0/2] Add an assert in napi_consume_skb()

From: Yunsheng Lin <hidden>
Date: 2020-11-24 10:53:36

This patch introduces a lockdep_assert_in_softirq() interface and
uses it to assert the case when napi_consume_skb() is not called in
the softirq context.

Changelog:
V3: add comment to emphasize the ambiguous semantics
V2: Use lockdep instead of one-off Kconfig knob

Yunsheng Lin (2):
  lockdep: Introduce in_softirq lockdep assert
  net: Use lockdep_assert_in_softirq() in napi_consume_skb()

 include/linux/lockdep.h | 8 ++++++++
 net/core/skbuff.c       | 2 ++
 2 files changed, 10 insertions(+)

-- 
2.8.1

[PATCH net-next v3 1/2] lockdep: Introduce in_softirq lockdep assert

From: Yunsheng Lin <hidden>
Date: 2020-11-24 10:53:36

The current semantic for napi_consume_skb() is that caller need
to provide non-zero budget when calling from NAPI context, and
breaking this semantic will cause hard to debug problem, because
_kfree_skb_defer() need to run in atomic context in order to push
the skb to the particular cpu' napi_alloc_cache atomically.

So add the lockdep_assert_in_softirq() to assert when the running
context is not in_softirq, in_softirq means softirq is serving or
BH is disabled, which has a ambiguous semantics due to the BH
disabled confusion, so add a comment to emphasize that.

And the softirq context can be interrupted by hard IRQ or NMI
context, lockdep_assert_in_softirq() need to assert about hard
IRQ or NMI context too.

Suggested-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Yunsheng Lin <redacted>
---
V3: add comment to emphasize the ambiguous semantics.
---
 include/linux/lockdep.h | 8 ++++++++
 1 file changed, 8 insertions(+)
diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
index f559487..8d60f46 100644
--- a/include/linux/lockdep.h
+++ b/include/linux/lockdep.h
@@ -594,6 +594,13 @@ do {									\
 		      this_cpu_read(hardirqs_enabled)));		\
 } while (0)
 
+/* Much like in_softirq() - semantics are ambiguous, use carefully. */
+#define lockdep_assert_in_softirq()					\
+do {									\
+	WARN_ON_ONCE(__lockdep_enabled			&&		\
+		     (!in_softirq() || in_irq() || in_nmi()));		\
+} while (0)
+
 #else
 # define might_lock(lock) do { } while (0)
 # define might_lock_read(lock) do { } while (0)
@@ -605,6 +612,7 @@ do {									\
 
 # define lockdep_assert_preemption_enabled() do { } while (0)
 # define lockdep_assert_preemption_disabled() do { } while (0)
+# define lockdep_assert_in_softirq() do { } while (0)
 #endif
 
 #ifdef CONFIG_PROVE_RAW_LOCK_NESTING
-- 
2.8.1

[PATCH net-next v3 2/2] net: Use lockdep_assert_in_softirq() in napi_consume_skb()

From: Yunsheng Lin <hidden>
Date: 2020-11-24 10:53:36

Use napi_consume_skb() to assert the case when it is not called
in a atomic softirq context.

Signed-off-by: Yunsheng Lin <redacted>
---
 net/core/skbuff.c | 2 ++
 1 file changed, 2 insertions(+)
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index ffe3dcc..effa19d 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -902,6 +902,8 @@ void napi_consume_skb(struct sk_buff *skb, int budget)
 		return;
 	}
 
+	lockdep_assert_in_softirq();
+
 	if (!skb_unref(skb))
 		return;
 
-- 
2.8.1

Re: [PATCH net-next v3 1/2] lockdep: Introduce in_softirq lockdep assert

From: Jakub Kicinski <kuba@kernel.org>
Date: 2020-11-25 23:12:18

On Tue, 24 Nov 2020 18:49:28 +0800 Yunsheng Lin wrote:
quoted hunk
The current semantic for napi_consume_skb() is that caller need
to provide non-zero budget when calling from NAPI context, and
breaking this semantic will cause hard to debug problem, because
_kfree_skb_defer() need to run in atomic context in order to push
the skb to the particular cpu' napi_alloc_cache atomically.

So add the lockdep_assert_in_softirq() to assert when the running
context is not in_softirq, in_softirq means softirq is serving or
BH is disabled, which has a ambiguous semantics due to the BH
disabled confusion, so add a comment to emphasize that.

And the softirq context can be interrupted by hard IRQ or NMI
context, lockdep_assert_in_softirq() need to assert about hard
IRQ or NMI context too.

Suggested-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Yunsheng Lin <redacted>
---
V3: add comment to emphasize the ambiguous semantics.
---
 include/linux/lockdep.h | 8 ++++++++
 1 file changed, 8 insertions(+)
diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
index f559487..8d60f46 100644
--- a/include/linux/lockdep.h
+++ b/include/linux/lockdep.h
@@ -594,6 +594,13 @@ do {									\
 		      this_cpu_read(hardirqs_enabled)));		\
 } while (0)
 
+/* Much like in_softirq() - semantics are ambiguous, use carefully. */
I've added both of the comments I suggested in the reply to Peter here
and applied to net-next.

Thanks for working on this.
+#define lockdep_assert_in_softirq()					\
+do {									\
+	WARN_ON_ONCE(__lockdep_enabled			&&		\
+		     (!in_softirq() || in_irq() || in_nmi()));		\
+} while (0)

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help