netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: Samiullah Khawaja <skhawaja@google.com>
Cc: Wei Wang <weiwan@google.com>,
	"David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Paolo Abeni <pabeni@redhat.com>,
	almasrymina@google.com, willemb@google.com, jdamato@fastly.com,
	mkarsten@uwaterloo.ca, netdev@vger.kernel.org
Subject: Re: [PATCH net-next v2] net: stop napi kthreads when THREADED napi is disabled
Date: Wed, 28 May 2025 17:04:42 -0700	[thread overview]
Message-ID: <20250528170442.160f6f99@kernel.org> (raw)
In-Reply-To: <CAAywjhScfevLxYho-wxU6WNF+0VpwngW8MzZjpx1HQ83NTXUDw@mail.gmail.com>

On Wed, 21 May 2025 15:50:19 -0700 Samiullah Khawaja wrote:
> > Just to be clear - the stopping of the thread has to be after the
> > proposed loop, so kthread_should_stop() does not come into play.  
> Wait, the thread will unset STATE_THREADED if kthread_should_stop is
> true. right? Otherwise how would thread know that it has to unset the
> bit and stop?
> 
> As I understand, we should be doing something like following:
> 
> while (true) {
>    state = READ_ONCE()
>    can_stop = false;
> 
>    if (kthread_should_stop) {
>        if (SCHED_THREADED || !SCHED) {
>            state &= !THREADED
>        } else {
>            msleep(1);
>            continue;
>        }
> 
>         if (try_cmpxchg) {
>             can_stop = true;
>             if (!SCHED_THREADED)
>                 break;
>         }
>    }
> 
>    if (SCHED_THREADED)
>        poll()
> 
>    if (can_stop))
>        break;
> }

So moving the stopping logic into the polling thread? I don't think this
helps anything. 

Once we unset the THREADED bit we should wait for the thread to clear
SCHED_THREADED (before trying to stop it). SCHED_THREADED going away
is our signal that it's safe to reap the thread.

If you're afraid that the thread will not terminate (because packets
continue to flow in) - we probably want something like:

diff --git a/net/core/dev.c b/net/core/dev.c
index 2b514d95c528..33d4b726395b 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -7548,6 +7548,13 @@ static void napi_threaded_poll_loop(struct napi_struct *napi)
                if (!repoll)
                        break;
 
+               /* Thread is going away, give up the ownership */
+               if (!likely(READ_ONCE(napi->state) & NAPIF_STATE_THREADED)) {
+                       clear_bit(NAPI_STATE_SCHED_THREADED, &napi->state);
+                       __napi_schedule(napi);
+                       break;
+               }
+
                rcu_softirq_qs_periodic(last_qs);
                cond_resched();
        }

      reply	other threads:[~2025-05-29  0:04 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-19 22:43 [PATCH net-next v2] net: stop napi kthreads when THREADED napi is disabled Samiullah Khawaja
2025-05-20  0:34 ` Wei Wang
2025-05-21  2:09 ` Jakub Kicinski
2025-05-21 16:41   ` Wei Wang
2025-05-21 17:28     ` Samiullah Khawaja
2025-05-21 19:51       ` Samiullah Khawaja
2025-05-21 22:21         ` Jakub Kicinski
2025-05-21 22:50           ` Samiullah Khawaja
2025-05-29  0:04             ` Jakub Kicinski [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250528170442.160f6f99@kernel.org \
    --to=kuba@kernel.org \
    --cc=almasrymina@google.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=jdamato@fastly.com \
    --cc=mkarsten@uwaterloo.ca \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=skhawaja@google.com \
    --cc=weiwan@google.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).