Skip to contentSkip to conversation

net/dev.c : Remove redundant state settings after waking up

PATCH Superseded 李哲 5 participants 8 messages Jan 10, 2023 – Mar 1, 2023 1 message not in the archive

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

李哲

author
#0 lore raw

PATCH 1/1 net/dev.c : Remove redundant state settings after waking up

the task status has been set to TASK_RUNNING in shcedule(), no need to set again here

---
 net/core/dev.c | 1 -
 1 file changed, 1 deletion(-)
net/core/dev.c +0 -1
index b76fb37b381e..4bd2d4b954c9 100644
@@ -6580,7 +6580,6 @@ static int napi_thread_wait(struct napi_struct *napi)
 		schedule();
 		/* woken being true indicates this thread owns this napi. */
 		woken = true;
-		set_current_state(TASK_INTERRUPTIBLE);
 	}
 	__set_current_state(TASK_RUNNING);
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 3 lines · 李哲

the task status has been set to TASK_RUNNING in shcedule(), no need to set again here

Changelog is rather confusing, this does not match the patch, which removes one set_current_state(TASK_INTERRUPTIBLE);

TASK_INTERRUPTIBLE != TASK_RUNNING

Patch itself looks okay (but has nothing to do with thread state after schedule()),
you should have CC Wei Wang because she
authored commit cb038357937e net: fix race between napi kthread mode and busy poll

Quoted 21 lines
---
 net/core/dev.c | 1 -
 1 file changed, 1 deletion(-)
net/core/dev.c +0 -2
index b76fb37b381e..4bd2d4b954c9 100644
@@ -6580,7 +6580,6 @@ static int napi_thread_wait(struct napi_struct *napi)
                schedule();
                /* woken being true indicates this thread owns this napi. */
                woken = true;
-               set_current_state(TASK_INTERRUPTIBLE);
        }
        __set_current_state(TASK_RUNNING);
 
--

2.17.1

maintainer
#2 lore raw
Quoted 6 lines · On Tue, 10 Jan 2023 10:29:20 +0100 Eric Dumazet wrote:
Quoted 2 lines

the task status has been set to TASK_RUNNING in shcedule(), no need to set again here

Changelog is rather confusing, this does not match the patch, which removes one set_current_state(TASK_INTERRUPTIBLE);

Quoted 7 lines

TASK_INTERRUPTIBLE != TASK_RUNNING

Patch itself looks okay (but has nothing to do with thread state after schedule()),
you should have CC Wei Wang because she
authored commit cb038357937e net: fix race between napi kthread mode and busy poll

AFAIU this is the semi-idiomatic way of handling wait loops. It's not schedule() that may set the task state to TASK_RUNNING, it's whoever wakes the process and makes the "wait condition" true. In this case - test_bit(NAPI_STATE_SCHED, &napi->state)

I vote to not futz with this logic.

#3 lore raw

I was not able to see the entire changelog, but I don't think

Quoted 1 line

- set_current_state(TASK_INTERRUPTIBLE);

is redundant.

It makes sure that if the previous if statement:

    if (test_bit(NAPI_STATE_SCHED_THREADED, &napi->state) || woken)

is false, this napi thread yields the CPU to other threads waiting to be run by calling schedule().

And the napi thread gets into running state again after the next wake_up_process() is called from ____napi_schedule().

Quoted 22 lines · Jakub Kicinski
Quoted 6 lines · On Tue, 10 Jan 2023 10:29:20 +0100 Eric Dumazet wrote:
Quoted 2 lines

the task status has been set to TASK_RUNNING in shcedule(), no need to set again here

Changelog is rather confusing, this does not match the patch, which removes one set_current_state(TASK_INTERRUPTIBLE);

Quoted 7 lines

TASK_INTERRUPTIBLE != TASK_RUNNING

Patch itself looks okay (but has nothing to do with thread state after schedule()),
you should have CC Wei Wang because she
authored commit cb038357937e net: fix race between napi kthread mode and busy poll

AFAIU this is the semi-idiomatic way of handling wait loops. It's not schedule() that may set the task state to TASK_RUNNING, it's whoever wakes the process and makes the "wait condition" true. In this case - test_bit(NAPI_STATE_SCHED, &napi->state)

I vote to not futz with this logic.

#4 lore raw
Quoted 8 lines · Wei Wang

I was not able to see the entire changelog, but I don't think

Quoted 1 line

- set_current_state(TASK_INTERRUPTIBLE);

is redundant.

It makes sure that if the previous if statement:

    if (test_bit(NAPI_STATE_SCHED_THREADED, &napi->state) || woken)

is false, this napi thread yields the CPU to other threads waiting to be run by calling schedule().

It made sense in the beginning but now the suggested patch is a clean up. First the `woken' parameter was added in commit
cb038357937ee ("net: fix race between napi kthread mode and busy poll")

and then the `napi_disable_pending' check was removed in commit
27f0ad71699de ("net: fix hangup on napi_disable for threaded napi")

which renders the code to:

|         while (!kthread_should_stop()) {
|                 if (test_bit(NAPI_STATE_SCHED_THREADED, &napi->state) || woken) {
|                         WARN_ON(!list_empty(&napi->poll_list));
|                         __set_current_state(TASK_RUNNING);
|                         return 0;
|                 }
| 
|                 schedule();
|                 /* woken being true indicates this thread owns this napi. */
|                 woken = true;
|                 set_current_state(TASK_INTERRUPTIBLE);
|         }
|         __set_current_state(TASK_RUNNING);

so when you get out of schedule() woken is set and even if NAPI_STATE_SCHED_THREADED is not set, the while() loop is left due to `woken = true'. So changing state to TASK_INTERRUPTIBLE makes no sense since it will be set back to TASK_RUNNING cycles later.

Sebastian

maintainer
#5 lore raw
Quoted 22 lines · On Wed, 11 Jan 2023 08:32:42 +0100 Sebastian Andrzej Siewior wrote:

It made sense in the beginning but now the suggested patch is a clean up. First the `woken' parameter was added in commit
cb038357937ee ("net: fix race between napi kthread mode and busy poll")

and then the `napi_disable_pending' check was removed in commit
27f0ad71699de ("net: fix hangup on napi_disable for threaded napi")

which renders the code to:

|         while (!kthread_should_stop()) {
|                 if (test_bit(NAPI_STATE_SCHED_THREADED, &napi->state) || woken) {
|                         WARN_ON(!list_empty(&napi->poll_list));
|                         __set_current_state(TASK_RUNNING);
|                         return 0;
|                 }
| 
|                 schedule();
|                 /* woken being true indicates this thread owns this napi. */
|                 woken = true;
|                 set_current_state(TASK_INTERRUPTIBLE);
|         }
|         __set_current_state(TASK_RUNNING);
Quoted 4 lines

so when you get out of schedule() woken is set and even if NAPI_STATE_SCHED_THREADED is not set, the while() loop is left due to `woken = true'. So changing state to TASK_INTERRUPTIBLE makes no sense since it will be set back to TASK_RUNNING cycles later.

Ah, fair point, forgot about the woken optimization.

#6 lore raw
Quoted 24 lines · Jakub Kicinski
Quoted 22 lines · On Wed, 11 Jan 2023 08:32:42 +0100 Sebastian Andrzej Siewior wrote:

It made sense in the beginning but now the suggested patch is a clean up. First the `woken' parameter was added in commit
cb038357937ee ("net: fix race between napi kthread mode and busy poll")

and then the `napi_disable_pending' check was removed in commit
27f0ad71699de ("net: fix hangup on napi_disable for threaded napi")

which renders the code to:

|         while (!kthread_should_stop()) {
|                 if (test_bit(NAPI_STATE_SCHED_THREADED, &napi->state) || woken) {
|                         WARN_ON(!list_empty(&napi->poll_list));
|                         __set_current_state(TASK_RUNNING);
|                         return 0;
|                 }
|
|                 schedule();
|                 /* woken being true indicates this thread owns this napi. */
|                 woken = true;
|                 set_current_state(TASK_INTERRUPTIBLE);
|         }
|         __set_current_state(TASK_RUNNING);
Quoted 6 lines
Quoted 4 lines

so when you get out of schedule() woken is set and even if NAPI_STATE_SCHED_THREADED is not set, the while() loop is left due to `woken = true'. So changing state to TASK_INTERRUPTIBLE makes no sense since it will be set back to TASK_RUNNING cycles later.

Ah, fair point, forgot about the woken optimization.

Agree. I think it is OK to remove this, since woken is set, and this function will set TASK_RUNNING and return 0. And the next time, it will enter with TASK_INTERRUPTIBLE and woken = false.

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#8 lore raw
Quoted 2 lines · On Wed, 1 Mar 2023 23:32:50 +0800 (CST) lizhe wrote:

HI :
if want to merge this patch into the main line, what should i do ?

Stop sending HTML emails please, the list only accepts plain text.

Read this:

https://www.kernel.org/doc/html/latest/process/maintainer-netdev.html

Improve the commit message and the subject line (look at other related commits to find examples). Repost when appropriate (net-next is closed).