All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net-next] net: make napi_disable() symmetric with enable
@ 2021-09-24  4:02 Jakub Kicinski
  2021-09-24 14:30 ` Eric Dumazet
  0 siblings, 1 reply; 3+ messages in thread
From: Jakub Kicinski @ 2021-09-24  4:02 UTC (permalink / raw)
  To: davem; +Cc: netdev, eric.dumazet, weiwan, xuanzhuo, 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.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
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(-)

diff --git a/net/core/dev.c b/net/core/dev.c
index 62ddd7d6e00d..0d297423b304 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -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);
 
-- 
2.31.1


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

* Re: [PATCH net-next] net: make napi_disable() symmetric with enable
  2021-09-24  4:02 [PATCH net-next] net: make napi_disable() symmetric with enable Jakub Kicinski
@ 2021-09-24 14:30 ` Eric Dumazet
  2021-09-24 20:21   ` Jakub Kicinski
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Dumazet @ 2021-09-24 14:30 UTC (permalink / raw)
  To: Jakub Kicinski, davem; +Cc: netdev, eric.dumazet, weiwan, xuanzhuo



On 9/23/21 9:02 PM, Jakub Kicinski wrote:
> 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.
> 
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
> ---
> 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.

> ---
>  net/core/dev.c | 18 ++++++++++++------
>  1 file changed, 12 insertions(+), 6 deletions(-)
> 
> diff --git a/net/core/dev.c b/net/core/dev.c
> index 62ddd7d6e00d..0d297423b304 100644
> --- a/net/core/dev.c
> +++ b/net/core/dev.c
> @@ -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);

Patch seems good to me.

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

> +			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);
>  
> 

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

* Re: [PATCH net-next] net: make napi_disable() symmetric with enable
  2021-09-24 14:30 ` Eric Dumazet
@ 2021-09-24 20:21   ` Jakub Kicinski
  0 siblings, 0 replies; 3+ messages in thread
From: Jakub Kicinski @ 2021-09-24 20:21 UTC (permalink / raw)
  To: Eric Dumazet; +Cc: davem, netdev, weiwan, xuanzhuo

On Fri, 24 Sep 2021 07:30:30 -0700 Eric Dumazet wrote:
> >  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);  
> 
> Patch seems good to me.
> 
> We also could replace this pessimistic msleep(1) with more opportunistic usleep_range(20, 200)

Will do, thanks!

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

end of thread, other threads:[~2021-09-24 20:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-24  4:02 [PATCH net-next] net: make napi_disable() symmetric with enable Jakub Kicinski
2021-09-24 14:30 ` Eric Dumazet
2021-09-24 20:21   ` Jakub Kicinski

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.