Skip to contentSkip to conversation

net: few debug refinements

PATCH Applied Eric Dumazet 2 participants 10 messages Jun 8, 2022 – Jun 10, 2022

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

v2 cover net: few debug refinements

From: Eric Dumazet <[email protected]>

Adopt DEBUG_NET_WARN_ON_ONCE() or WARN_ON_ONCE()
in some places where it makes sense.

Add checks in napi_consume_skb() and __napi_alloc_skb()

Make sure napi_get_frags() does not use page fragments for skb->head.

Eric Dumazet (8):
net: use DEBUG_NET_WARN_ON_ONCE() in __release_sock()
net: use DEBUG_NET_WARN_ON_ONCE() in dev_loopback_xmit()
net: use WARN_ON_ONCE() in inet_sock_destruct()
net: use WARN_ON_ONCE() in sk_stream_kill_queues()
af_unix: use DEBUG_NET_WARN_ON_ONCE()
net: use DEBUG_NET_WARN_ON_ONCE() in skb_release_head_state()
net: add debug checks in napi_consume_skb and __napi_alloc_skb()
net: add napi_get_frags_check() helper

 net/core/dev.c     | 20 +++++++++++++++++++-
 net/core/skbuff.c  |  5 +++--
 net/core/sock.c    |  2 +-
 net/core/stream.c  |  6 +++---
 net/ipv4/af_inet.c |  8 ++++----
 net/unix/af_unix.c |  8 ++++----
 6 files changed, 34 insertions(+), 15 deletions(-)
Signature · 1 line
2.36.1.255.ge46751e96f-goog
#1 lore raw

PATCH v2 1/8 net: use DEBUG_NET_WARN_ON_ONCE() in __release_sock()

From: Eric Dumazet <[email protected]>

Check against skb dst in socket backlog has never triggered in past years.

Keep the check omly for CONFIG_DEBUG_NET=y builds.

---
 net/core/sock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/sock.c +1 -1
index 2ff40dd0a7a652029cca1743109286b50c2a17f3..f5062d9e122275a511efbc4d30de2ee501182498 100644
@@ -2844,7 +2844,7 @@ void __release_sock(struct sock *sk)
 		do {
 			next = skb->next;
 			prefetch(next);
-			WARN_ON_ONCE(skb_dst_is_noref(skb));
+			DEBUG_NET_WARN_ON_ONCE(skb_dst_is_noref(skb));
 			skb_mark_not_on_list(skb);
 			sk_backlog_rcv(sk, skb);
 
Signature · 1 line
2.36.1.255.ge46751e96f-goog
#2 lore raw

PATCH v2 2/8 net: use DEBUG_NET_WARN_ON_ONCE() in dev_loopback_xmit()

In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>

One check in dev_loopback_xmit() has not caught issues in the past.

Keep it for CONFIG_DEBUG_NET=y builds only.

---
 net/core/dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/dev.c +1 -1
index 08ce317fcec89609f6f8e9335b3d9f57e813024d..27ad09ad80a4550097ce4d113719a558b5e2a811 100644
@@ -3925,7 +3925,7 @@ int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
 	skb->pkt_type = PACKET_LOOPBACK;
 	if (skb->ip_summed == CHECKSUM_NONE)
 		skb->ip_summed = CHECKSUM_UNNECESSARY;
-	WARN_ON(!skb_dst(skb));
+	DEBUG_NET_WARN_ON_ONCE(!skb_dst(skb));
 	skb_dst_force(skb);
 	netif_rx(skb);
 	return 0;
Signature · 1 line
2.36.1.255.ge46751e96f-goog
#3 lore raw

PATCH v2 3/8 net: use WARN_ON_ONCE() in inet_sock_destruct()

In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>

inet_sock_destruct() has four warnings which have been useful to point to kernel bugs in the past.

However they are potentially a problem because they could flood the syslog.

---
 net/ipv4/af_inet.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/ipv4/af_inet.c +4 -4
index 93da9f783bec52e4bda6213dc78ef3820e180cc4..30e0e8992085d5d4ac5941b5f3a101f798588be9 100644
@@ -148,10 +148,10 @@ void inet_sock_destruct(struct sock *sk)
 		return;
 	}
 
-	WARN_ON(atomic_read(&sk->sk_rmem_alloc));
-	WARN_ON(refcount_read(&sk->sk_wmem_alloc));
-	WARN_ON(sk->sk_wmem_queued);
-	WARN_ON(sk_forward_alloc_get(sk));
+	WARN_ON_ONCE(atomic_read(&sk->sk_rmem_alloc));
+	WARN_ON_ONCE(refcount_read(&sk->sk_wmem_alloc));
+	WARN_ON_ONCE(sk->sk_wmem_queued);
+	WARN_ON_ONCE(sk_forward_alloc_get(sk));
 
 	kfree(rcu_dereference_protected(inet->inet_opt, 1));
 	dst_release(rcu_dereference_protected(sk->sk_dst_cache, 1));
Signature · 1 line
2.36.1.255.ge46751e96f-goog
#4 lore raw

PATCH v2 4/8 net: use WARN_ON_ONCE() in sk_stream_kill_queues()

In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>

sk_stream_kill_queues() has three checks which have been useful to detect kernel bugs in the past.

However they are potentially a problem because they could flood the syslog.

---
 net/core/stream.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/core/stream.c +3 -3
index 06b36c730ce8a29bb2d8984495e780931907ca72..ccc083cdef23266dc100368a7183bdeb0e99cbb7 100644
@@ -196,13 +196,13 @@ void sk_stream_kill_queues(struct sock *sk)
 	__skb_queue_purge(&sk->sk_receive_queue);
 
 	/* Next, the write queue. */
-	WARN_ON(!skb_queue_empty(&sk->sk_write_queue));
+	WARN_ON_ONCE(!skb_queue_empty(&sk->sk_write_queue));
 
 	/* Account for returned memory. */
 	sk_mem_reclaim_final(sk);
 
-	WARN_ON(sk->sk_wmem_queued);
-	WARN_ON(sk->sk_forward_alloc);
+	WARN_ON_ONCE(sk->sk_wmem_queued);
+	WARN_ON_ONCE(sk->sk_forward_alloc);
 
 	/* It is _impossible_ for the backlog to contain anything
 	 * when we get here.  All user references to this socket
Signature · 1 line
2.36.1.255.ge46751e96f-goog
#5 lore raw

PATCH v2 5/8 af_unix: use DEBUG_NET_WARN_ON_ONCE()

In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>

Replace four WARN_ON() that have not triggered recently with DEBUG_NET_WARN_ON_ONCE().

---
 net/unix/af_unix.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/unix/af_unix.c +4 -4
index 654dcef7cfb3849463be9d905ae625319fbae406..ae254195aac87f196a93853443048b40e332cc60 100644
@@ -302,7 +302,7 @@ static void __unix_remove_socket(struct sock *sk)
 
 static void __unix_insert_socket(struct sock *sk)
 {
-	WARN_ON(!sk_unhashed(sk));
+	DEBUG_NET_WARN_ON_ONCE(!sk_unhashed(sk));
 	sk_add_node(sk, &unix_socket_table[sk->sk_hash]);
 }
 
@@ -554,9 +554,9 @@ static void unix_sock_destructor(struct sock *sk)
 		u->oob_skb = NULL;
 	}
 #endif
-	WARN_ON(refcount_read(&sk->sk_wmem_alloc));
-	WARN_ON(!sk_unhashed(sk));
-	WARN_ON(sk->sk_socket);
+	DEBUG_NET_WARN_ON_ONCE(refcount_read(&sk->sk_wmem_alloc));
+	DEBUG_NET_WARN_ON_ONCE(!sk_unhashed(sk));
+	DEBUG_NET_WARN_ON_ONCE(sk->sk_socket);
 	if (!sock_flag(sk, SOCK_DEAD)) {
 		pr_info("Attempt to release alive unix socket: %p\n", sk);
 		return;
Signature · 1 line
2.36.1.255.ge46751e96f-goog
#6 lore raw

PATCH v2 6/8 net: use DEBUG_NET_WARN_ON_ONCE() in skb_release_head_state()

In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>

Remove this check from fast path unless CONFIG_DEBUG_NET=y

---
 net/core/skbuff.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/skbuff.c +1 -1
index b661040c100efbb59aea58ed31247f820292bd64..cf83d9b8f41dc28ae11391f2651e5abee3dcde8f 100644
@@ -728,7 +728,7 @@ void skb_release_head_state(struct sk_buff *skb)
 {
 	skb_dst_drop(skb);
 	if (skb->destructor) {
-		WARN_ON(in_hardirq());
+		DEBUG_NET_WARN_ON_ONCE(in_hardirq());
 		skb->destructor(skb);
 	}
 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
Signature · 1 line
2.36.1.255.ge46751e96f-goog
#7 lore raw

PATCH v2 7/8 net: add debug checks in napi_consume_skb and __napi_alloc_skb()

In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>

Commit 6454eca81eae ("net: Use lockdep_assert_in_softirq() in napi_consume_skb()") added a check in napi_consume_skb() which is a bit weak.

napi_consume_skb() and __napi_alloc_skb() should only be used from BH context, not from hard irq or nmi context, otherwise we could have races.

---
 net/core/skbuff.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/core/skbuff.c +2 -1
index cf83d9b8f41dc28ae11391f2651e5abee3dcde8f..fec75f8bf1f416f05c14f76009a15412b2559b6e 100644
@@ -560,6 +560,7 @@ struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
 	struct sk_buff *skb;
 	void *data;
 
+	DEBUG_NET_WARN_ON_ONCE(!in_softirq());
 	len += NET_SKB_PAD + NET_IP_ALIGN;
 
 	/* If requested length is either too small or too big,
@@ -981,7 +982,7 @@ void napi_consume_skb(struct sk_buff *skb, int budget)
 		return;
 	}
 
-	lockdep_assert_in_softirq();
+	DEBUG_NET_WARN_ON_ONCE(!in_softirq());
 
 	if (!skb_unref(skb))
 		return;
Signature · 1 line
2.36.1.255.ge46751e96f-goog
#8 lore raw

PATCH v2 8/8 net: add napi_get_frags_check() helper

In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>

This is a follow up of commit 3226b158e67c
("net: avoid 32 x truesize under-estimation for tiny skbs")

When/if we increase MAX_SKB_FRAGS, we better make sure the old bug will not come back.

Adding a check in napi_get_frags() would be costly, even if using DEBUG_NET_WARN_ON_ONCE().

---
 net/core/dev.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
net/core/dev.c +18 -0
index 27ad09ad80a4550097ce4d113719a558b5e2a811..4ce9b2563a116066d85bae7a862e38fb160ef0e2 100644
@@ -6351,6 +6351,23 @@ int dev_set_threaded(struct net_device *dev, bool threaded)
 }
 EXPORT_SYMBOL(dev_set_threaded);
 
+/* Double check that napi_get_frags() allocates skbs with
+ * skb->head being backed by slab, not a page fragment.
+ * This is to make sure bug fixed in 3226b158e67c
+ * ("net: avoid 32 x truesize under-estimation for tiny skbs")
+ * does not accidentally come back.
+ */
+static void napi_get_frags_check(struct napi_struct *napi)
+{
+	struct sk_buff *skb;
+
+	local_bh_disable();
+	skb = napi_get_frags(napi);
+	WARN_ON_ONCE(skb && skb->head_frag);
+	napi_free_frags(napi);
+	local_bh_enable();
+}
+
 void netif_napi_add_weight(struct net_device *dev, struct napi_struct *napi,
 			   int (*poll)(struct napi_struct *, int), int weight)
 {
@@ -6378,6 +6395,7 @@ void netif_napi_add_weight(struct net_device *dev, struct napi_struct *napi,
 	set_bit(NAPI_STATE_NPSVC, &napi->state);
 	list_add_rcu(&napi->dev_list, &dev->napi_list);
 	napi_hash_add(napi);
+	napi_get_frags_check(napi);
 	/* Create kthread for this napi if dev->threaded is set.
 	 * Clear dev->threaded if kthread creation failed so that
 	 * threaded mode will not be enabled in napi_enable().
Signature · 1 line
2.36.1.255.ge46751e96f-goog
#9 lore raw
In reply to Eric Dumazet
This series was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Hello:

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

Quoted 8 lines · On Wed, 8 Jun 2022 09:04:30 -0700 you wrote:

From: Eric Dumazet <[email protected]>

Adopt DEBUG_NET_WARN_ON_ONCE() or WARN_ON_ONCE()
in some places where it makes sense.

Add checks in napi_consume_skb() and __napi_alloc_skb()

[...]

Here is the summary with links:
- [v2,net-next,1/8] net: use DEBUG_NET_WARN_ON_ONCE() in __release_sock()

    https://git.kernel.org/netdev/net-next/c/63fbdd3c77ec
  - [v2,net-next,2/8] net: use DEBUG_NET_WARN_ON_ONCE() in dev_loopback_xmit()
    https://git.kernel.org/netdev/net-next/c/76458faeb285
  - [v2,net-next,3/8] net: use WARN_ON_ONCE() in inet_sock_destruct()
    https://git.kernel.org/netdev/net-next/c/3e7f2b8d3088
  - [v2,net-next,4/8] net: use WARN_ON_ONCE() in sk_stream_kill_queues()
    https://git.kernel.org/netdev/net-next/c/c59f02f84867
  - [v2,net-next,5/8] af_unix: use DEBUG_NET_WARN_ON_ONCE()
    https://git.kernel.org/netdev/net-next/c/dd29c67dbbbf
  - [v2,net-next,6/8] net: use DEBUG_NET_WARN_ON_ONCE() in skb_release_head_state()
    https://git.kernel.org/netdev/net-next/c/7890e2f09d43
  - [v2,net-next,7/8] net: add debug checks in napi_consume_skb and __napi_alloc_skb()
    https://git.kernel.org/netdev/net-next/c/ee2640df2393
  - [v2,net-next,8/8] net: add napi_get_frags_check() helper
    https://git.kernel.org/netdev/net-next/c/fd9ea57f4e95

You are awesome, thank you!

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