Skip to contentSkip to conversation

net: make napi_disable() symmetric with enable

PATCH Superseded Jakub Kicinski 2 participants 3 messages Sep 24, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

PATCH 1/1 net: make napi_disable() symmetric with enable

Commit 3765996e4f0b ("napi: fix race inside napi_enable") fixed an ordering bug in napi_enable() and made the napi_enable() diverge from napi_disable(). The state transitions done on disable are not symmetric to enable.

There is no known bug in napi_disable() this is just refactoring.

---
Does this look like a reasonable cleanup?

TBH my preference would be to stick to the code we have in disable, and refactor enable back to single ops just in the right order. I find the series of atomic ops far easier to read and cmpxchg is not really required here.

---
 net/core/dev.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
net/core/dev.c +12 -6
index 62ddd7d6e00d..0d297423b304 100644
@@ -6900,19 +6900,25 @@ EXPORT_SYMBOL(netif_napi_add);
 
 void napi_disable(struct napi_struct *n)
 {
+	unsigned long val, new;
+
 	might_sleep();
 	set_bit(NAPI_STATE_DISABLE, &n->state);
 
-	while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
-		msleep(1);
-	while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
-		msleep(1);
+	do {
+		val = READ_ONCE(n->state);
+		if (val & (NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC)) {
+			msleep(1);
+			continue;
+		}
+
+		new = val | NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC;
+		new &= ~(NAPIF_STATE_THREADED | NAPIF_STATE_PREFER_BUSY_POLL);
+	} while (cmpxchg(&n->state, val, new) != val);
 
 	hrtimer_cancel(&n->timer);
 
-	clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &n->state);
 	clear_bit(NAPI_STATE_DISABLE, &n->state);
-	clear_bit(NAPI_STATE_THREADED, &n->state);
 }
 EXPORT_SYMBOL(napi_disable);
 
Signature · 1 line
2.31.1
#1 lore raw
Quoted 7 lines · Jakub Kicinski

Commit 3765996e4f0b ("napi: fix race inside napi_enable") fixed an ordering bug in napi_enable() and made the napi_enable() diverge from napi_disable(). The state transitions done on disable are not symmetric to enable.

There is no known bug in napi_disable() this is just refactoring.

Quoted 8 lines

---
Does this look like a reasonable cleanup?

TBH my preference would be to stick to the code we have in disable, and refactor enable back to single ops just in the right order. I find the series of atomic ops far easier to read and cmpxchg is not really required here.

I think RT crowd does not like the cmpxchg(), but I guess now we have them in fast path, we are a bit stuck.

Quoted 15 lines
---
 net/core/dev.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
net/core/dev.c +2 -0
index 62ddd7d6e00d..0d297423b304 100644
@@ -6900,19 +6900,25 @@ EXPORT_SYMBOL(netif_napi_add);
 
 void napi_disable(struct napi_struct *n)
 {
+	unsigned long val, new;
+
 	might_sleep();

net/core/dev.c EXPORT_SYMBOL(netif_napi_add); · line 6911

Quoted 10 lines
net/core/dev.c +4 -4
 	set_bit(NAPI_STATE_DISABLE, &n->state);
 
-	while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
-		msleep(1);
-	while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
-		msleep(1);
+	do {
+		val = READ_ONCE(n->state);
+		if (val & (NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC)) {
+			msleep(1);

Patch seems good to me.

We also could replace this pessimistic msleep(1) with more opportunistic usleep_range(20, 200)

Quoted 16 lines
net/core/dev.c +6 -2
+			continue;
+		}
+
+		new = val | NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC;
+		new &= ~(NAPIF_STATE_THREADED | NAPIF_STATE_PREFER_BUSY_POLL);
+	} while (cmpxchg(&n->state, val, new) != val);
 
 	hrtimer_cancel(&n->timer);
 
-	clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &n->state);
 	clear_bit(NAPI_STATE_DISABLE, &n->state);
-	clear_bit(NAPI_STATE_THREADED, &n->state);
 }
 EXPORT_SYMBOL(napi_disable);
 
author maintainer
#2 lore raw
Quoted 18 lines · On Fri, 24 Sep 2021 07:30:30 -0700 Eric Dumazet wrote:
Quoted 5 lines

void napi_disable(struct napi_struct *n)
{

net/core/dev.c +2 -0
+	unsigned long val, new;
+
 	might_sleep();

net/core/dev.c EXPORT_SYMBOL(netif_napi_add); · line 6911

Quoted 10 lines
net/core/dev.c +4 -4
 	set_bit(NAPI_STATE_DISABLE, &n->state);
 
-	while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
-		msleep(1);
-	while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
-		msleep(1);
+	do {
+		val = READ_ONCE(n->state);
+		if (val & (NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC)) {
+			msleep(1);  

Patch seems good to me.

Quoted 1 line

We also could replace this pessimistic msleep(1) with more opportunistic usleep_range(20, 200)

Will do, thanks!