net: fix race between napi kthread mode and busy poll
Subsystems (2)
PATCH v3 1/1 net: fix race between napi kthread mode and busy poll
Currently, napi_thread_wait() checks for NAPI_STATE_SCHED bit to determine if the kthread owns this napi and could call napi->poll() on it. However, if socket busy poll is enabled, it is possible that the busy poll thread grabs this SCHED bit (after the previous napi->poll() invokes napi_complete_done() and clears SCHED bit) and tries to poll on the same napi. napi_disable() could grab the SCHED bit as well. This patch tries to fix this race by adding a new bit NAPI_STATE_SCHED_THREADED in napi->state. This bit gets set in ____napi_schedule() if the threaded mode is enabled, and gets cleared in napi_complete_done(), and we only poll the napi in kthread if this bit is set. This helps distinguish the ownership of the napi between kthread and other scenarios and fixes the race issue.
--- include/linux/netdevice.h | 2 ++ net/core/dev.c | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-)
include/linux/netdevice.h +2 -0
@@ -360,6 +360,7 @@ enum {NAPI_STATE_IN_BUSY_POLL ,/* sk_busy_loop() owns this NAPI */ NAPI_STATE_PREFER_BUSY_POLL ,/* prefer busy-polling over softirq processing*/ NAPI_STATE_THREADED ,/* The poll is performed inside its own thread*/ + NAPI_STATE_SCHED_THREADED, /* Napi is currently scheduled in threaded mode */ }; enum {
@@ -372,6 +373,7 @@ enum {NAPIF_STATE_IN_BUSY_POLL =BIT (NAPI_STATE_IN_BUSY_POLL ),NAPIF_STATE_PREFER_BUSY_POLL =BIT (NAPI_STATE_PREFER_BUSY_POLL ),NAPIF_STATE_THREADED =BIT (NAPI_STATE_THREADED ), + NAPIF_STATE_SCHED_THREADED = BIT(NAPI_STATE_SCHED_THREADED), }; enum gro_result {
net/core/dev.c +13 -1
@@ -4294,6 +4294,8 @@ static inline void ____napi_schedule(struct softnet_data *sd,*/ thread =READ_ONCE (napi->thread);if (thread) { +if (thread->state !=TASK_INTERRUPTIBLE ) +set_bit (NAPI_STATE_SCHED_THREADED , &napi->state);wake_up_process (thread);return ; }
@@ -6486,6 +6488,7 @@ bool napi_complete_done(struct napi_struct *n, int work_done)WARN_ON_ONCE (!(val &NAPIF_STATE_SCHED )); new = val & ~(NAPIF_STATE_MISSED |NAPIF_STATE_SCHED | +NAPIF_STATE_SCHED_THREADED |NAPIF_STATE_PREFER_BUSY_POLL );/* If STATE_MISSED was set, leave STATE_SCHED set,
@@ -6968,16 +6971,25 @@ static int napi_poll(struct napi_struct *n, struct list_head *repoll)static int napi_thread_wait (struct napi_struct *napi) { +bool woken = false; +set_current_state (TASK_INTERRUPTIBLE );while (!kthread_should_stop () && !napi_disable_pending (napi)) { -if (test_bit (NAPI_STATE_SCHED , &napi->state)) { +/* Testing SCHED_THREADED bit here to make sure the current +* kthread owns this napi and could poll on this napi. +* Testing SCHED bit is not enough because SCHED bit might be +* set by some other busy poll thread or by napi_disable(). +*/ +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 );
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
Jakub Kicinski
maintainerQuoted 13 lines · On Mon, 1 Mar 2021 17:21:13 -0800 Wei Wang wrote:
Currently,
napi_thread_wait()checks forNAPI_STATE_SCHEDbit to determine if the kthread owns this napi and could callnapi->poll()on it. However, if socket busy poll is enabled, it is possible that the busy poll thread grabs this SCHED bit (after the previousnapi->poll()invokesnapi_complete_done()and clears SCHED bit) and tries to poll on the same napi.napi_disable()could grab the SCHED bit as well. This patch tries to fix this race by adding a new bitNAPI_STATE_SCHED_THREADEDinnapi->state. This bit gets set in____napi_schedule()if the threaded mode is enabled, and gets cleared innapi_complete_done(), and we only poll the napi in kthread if this bit is set. This helps distinguish the ownership of the napi between kthread and other scenarios and fixes the race issue.
Quoted 8 lines
Fixes 29863d41bb6e ("net: implement threaded-able napi poll loop support")Reported-by Martin Zaharinov <[email protected]>Suggested-by Jakub Kicinski <[email protected]>Signed-off-by Wei Wang <[email protected]>Cc Alexander Duyck <[email protected]>Cc Eric Dumazet <[email protected]>Cc Paolo Abeni <[email protected]>Cc Hannes Frederic Sowa <[email protected]>
Thanks!
Quoted 52 lines · Wei Wang
Currently,
napi_thread_wait()checks forNAPI_STATE_SCHEDbit to determine if the kthread owns this napi and could callnapi->poll()on it. However, if socket busy poll is enabled, it is possible that the busy poll thread grabs this SCHED bit (after the previousnapi->poll()invokesnapi_complete_done()and clears SCHED bit) and tries to poll on the same napi.napi_disable()could grab the SCHED bit as well. This patch tries to fix this race by adding a new bitNAPI_STATE_SCHED_THREADEDinnapi->state. This bit gets set in____napi_schedule()if the threaded mode is enabled, and gets cleared innapi_complete_done(), and we only poll the napi in kthread if this bit is set. This helps distinguish the ownership of the napi between kthread and other scenarios and fixes the race issue.Fixes 29863d41bb6e ("net: implement threaded-able napi poll loop support")Reported-by Martin Zaharinov <[email protected]>Suggested-by Jakub Kicinski <[email protected]>Signed-off-by Wei Wang <[email protected]>Cc Alexander Duyck <[email protected]>Cc Eric Dumazet <[email protected]>Cc Paolo Abeni <[email protected]>Cc Hannes Frederic Sowa <[email protected]>--- include/linux/netdevice.h | 2 ++ net/core/dev.c | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-)include/linux/netdevice.h +2
-0@@ -360,6 +360,7 @@ enum {NAPI_STATE_IN_BUSY_POLL ,/* sk_busy_loop() owns this NAPI */ NAPI_STATE_PREFER_BUSY_POLL ,/* prefer busy-polling over softirq processing*/ NAPI_STATE_THREADED ,/* The poll is performed inside its own thread*/ + NAPI_STATE_SCHED_THREADED, /* Napi is currently scheduled in threaded mode */ }; enum {@@ -372,6 +373,7 @@ enum {NAPIF_STATE_IN_BUSY_POLL =BIT (NAPI_STATE_IN_BUSY_POLL ),NAPIF_STATE_PREFER_BUSY_POLL =BIT (NAPI_STATE_PREFER_BUSY_POLL ),NAPIF_STATE_THREADED =BIT (NAPI_STATE_THREADED ), + NAPIF_STATE_SCHED_THREADED = BIT(NAPI_STATE_SCHED_THREADED), }; enum gro_result {net/core/dev.c +0
-0
net/core/dev.c static inline void ____napi_schedule(struct softnet_data *sd, · line 4297
Quoted 4 lines
net/core/dev.c +1
-0@@ -4294,6 +4294,8 @@ static inline void ____napi_schedule(struct softnet_data *sd,*/ thread =READ_ONCE (napi->thread);if (thread) { +if (thread->state !=TASK_INTERRUPTIBLE )
How safe is this read ?
Presumably KMSAN will detect that another cpu/thread is able to change thread->state under us,
so a READ_ONCE() (or data_race()) would be needed.
Nowhere else in the kernel can we find a similar construct, I find unfortunate to bury
in net/core/dev.c something that might be incorrect in the future.
Quoted 42 lines
net/core/dev.c +12
-2+set_bit (NAPI_STATE_SCHED_THREADED , &napi->state);wake_up_process (thread);return ; }@@ -6486,6 +6488,7 @@ bool napi_complete_done(struct napi_struct *n, int work_done)WARN_ON_ONCE (!(val &NAPIF_STATE_SCHED )); new = val & ~(NAPIF_STATE_MISSED |NAPIF_STATE_SCHED | +NAPIF_STATE_SCHED_THREADED |NAPIF_STATE_PREFER_BUSY_POLL );/* If STATE_MISSED was set, leave STATE_SCHED set, @@ -6968,16 +6971,25 @@ static int napi_poll(struct napi_struct *n, struct list_head *repoll)static int napi_thread_wait (struct napi_struct *napi) { +bool woken = false; +set_current_state (TASK_INTERRUPTIBLE );while (!kthread_should_stop () && !napi_disable_pending (napi)) { -if (test_bit (NAPI_STATE_SCHED , &napi->state)) { +/* Testing SCHED_THREADED bit here to make sure the current +* kthread owns this napi and could poll on this napi. +* Testing SCHED bit is not enough because SCHED bit might be +* set by some other busy poll thread or by napi_disable(). +*/ +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 ); --2.30.1.766.gb4fecdf3b7-goog
Quoted 61 lines · Eric Dumazet
Quoted 52 lines · Wei Wang
Currently,
napi_thread_wait()checks forNAPI_STATE_SCHEDbit to determine if the kthread owns this napi and could callnapi->poll()on it. However, if socket busy poll is enabled, it is possible that the busy poll thread grabs this SCHED bit (after the previousnapi->poll()invokesnapi_complete_done()and clears SCHED bit) and tries to poll on the same napi.napi_disable()could grab the SCHED bit as well. This patch tries to fix this race by adding a new bitNAPI_STATE_SCHED_THREADEDinnapi->state. This bit gets set in____napi_schedule()if the threaded mode is enabled, and gets cleared innapi_complete_done(), and we only poll the napi in kthread if this bit is set. This helps distinguish the ownership of the napi between kthread and other scenarios and fixes the race issue.Fixes 29863d41bb6e ("net: implement threaded-able napi poll loop support")Reported-by Martin Zaharinov <[email protected]>Suggested-by Jakub Kicinski <[email protected]>Signed-off-by Wei Wang <[email protected]>Cc Alexander Duyck <[email protected]>Cc Eric Dumazet <[email protected]>Cc Paolo Abeni <[email protected]>Cc Hannes Frederic Sowa <[email protected]>--- include/linux/netdevice.h | 2 ++ net/core/dev.c | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-)include/linux/netdevice.h +2
-0@@ -360,6 +360,7 @@ enum {NAPI_STATE_IN_BUSY_POLL ,/* sk_busy_loop() owns this NAPI */ NAPI_STATE_PREFER_BUSY_POLL ,/* prefer busy-polling over softirq processing*/ NAPI_STATE_THREADED ,/* The poll is performed inside its own thread*/ + NAPI_STATE_SCHED_THREADED, /* Napi is currently scheduled in threaded mode */ }; enum {@@ -372,6 +373,7 @@ enum {NAPIF_STATE_IN_BUSY_POLL =BIT (NAPI_STATE_IN_BUSY_POLL ),NAPIF_STATE_PREFER_BUSY_POLL =BIT (NAPI_STATE_PREFER_BUSY_POLL ),NAPIF_STATE_THREADED =BIT (NAPI_STATE_THREADED ), + NAPIF_STATE_SCHED_THREADED = BIT(NAPI_STATE_SCHED_THREADED), }; enum gro_result {net/core/dev.c +0
-0net/core/dev.c static inline void ____napi_schedule(struct softnet_data *sd, · line 4297
Quoted 4 lines
net/core/dev.c +1
-0@@ -4294,6 +4294,8 @@ static inline void ____napi_schedule(struct softnet_data *sd,*/ thread =READ_ONCE (napi->thread);if (thread) { +if (thread->state !=TASK_INTERRUPTIBLE )How safe is this read ?
Quoted 4 lines
Presumably KMSAN will detect that another cpu/thread is able to change
thread->stateunder us,
so aREAD_ONCE()(ordata_race()) would be needed.
Of course I meant KCSAN here.
Quoted 46 lines
Nowhere else in the kernel can we find a similar construct, I find unfortunate to bury
innet/core/dev.csomething that might be incorrect in the future.Quoted 42 lines
net/core/dev.c +12
-2+set_bit (NAPI_STATE_SCHED_THREADED , &napi->state);wake_up_process (thread);return ; }@@ -6486,6 +6488,7 @@ bool napi_complete_done(struct napi_struct *n, int work_done)WARN_ON_ONCE (!(val &NAPIF_STATE_SCHED )); new = val & ~(NAPIF_STATE_MISSED |NAPIF_STATE_SCHED | +NAPIF_STATE_SCHED_THREADED |NAPIF_STATE_PREFER_BUSY_POLL );/* If STATE_MISSED was set, leave STATE_SCHED set, @@ -6968,16 +6971,25 @@ static int napi_poll(struct napi_struct *n, struct list_head *repoll)static int napi_thread_wait (struct napi_struct *napi) { +bool woken = false; +set_current_state (TASK_INTERRUPTIBLE );while (!kthread_should_stop () && !napi_disable_pending (napi)) { -if (test_bit (NAPI_STATE_SCHED , &napi->state)) { +/* Testing SCHED_THREADED bit here to make sure the current +* kthread owns this napi and could poll on this napi. +* Testing SCHED bit is not enough because SCHED bit might be +* set by some other busy poll thread or by napi_disable(). +*/ +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 ); --2.30.1.766.gb4fecdf3b7-goog
Quoted 63 lines · Eric Dumazet
Quoted 61 lines · Eric Dumazet
Quoted 52 lines · Wei Wang
Currently,
napi_thread_wait()checks forNAPI_STATE_SCHEDbit to determine if the kthread owns this napi and could callnapi->poll()on it. However, if socket busy poll is enabled, it is possible that the busy poll thread grabs this SCHED bit (after the previousnapi->poll()invokesnapi_complete_done()and clears SCHED bit) and tries to poll on the same napi.napi_disable()could grab the SCHED bit as well. This patch tries to fix this race by adding a new bitNAPI_STATE_SCHED_THREADEDinnapi->state. This bit gets set in____napi_schedule()if the threaded mode is enabled, and gets cleared innapi_complete_done(), and we only poll the napi in kthread if this bit is set. This helps distinguish the ownership of the napi between kthread and other scenarios and fixes the race issue.Fixes 29863d41bb6e ("net: implement threaded-able napi poll loop support")Reported-by Martin Zaharinov <[email protected]>Suggested-by Jakub Kicinski <[email protected]>Signed-off-by Wei Wang <[email protected]>Cc Alexander Duyck <[email protected]>Cc Eric Dumazet <[email protected]>Cc Paolo Abeni <[email protected]>Cc Hannes Frederic Sowa <[email protected]>--- include/linux/netdevice.h | 2 ++ net/core/dev.c | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-)include/linux/netdevice.h +2
-0@@ -360,6 +360,7 @@ enum {NAPI_STATE_IN_BUSY_POLL ,/* sk_busy_loop() owns this NAPI */ NAPI_STATE_PREFER_BUSY_POLL ,/* prefer busy-polling over softirq processing*/ NAPI_STATE_THREADED ,/* The poll is performed inside its own thread*/ + NAPI_STATE_SCHED_THREADED, /* Napi is currently scheduled in threaded mode */ }; enum {@@ -372,6 +373,7 @@ enum {NAPIF_STATE_IN_BUSY_POLL =BIT (NAPI_STATE_IN_BUSY_POLL ),NAPIF_STATE_PREFER_BUSY_POLL =BIT (NAPI_STATE_PREFER_BUSY_POLL ),NAPIF_STATE_THREADED =BIT (NAPI_STATE_THREADED ), + NAPIF_STATE_SCHED_THREADED = BIT(NAPI_STATE_SCHED_THREADED), }; enum gro_result {net/core/dev.c +0
-0net/core/dev.c static inline void ____napi_schedule(struct softnet_data *sd, · line 4297
Quoted 4 lines
net/core/dev.c +1
-0@@ -4294,6 +4294,8 @@ static inline void ____napi_schedule(struct softnet_data *sd,*/ thread =READ_ONCE (napi->thread);if (thread) { +if (thread->state !=TASK_INTERRUPTIBLE )How safe is this read ?
Quoted 11 lines
Quoted 4 lines
Presumably KMSAN will detect that another cpu/thread is able to change
thread->stateunder us,
so aREAD_ONCE()(ordata_race()) would be needed.Of course I meant KCSAN here.
Quoted 4 lines
Nowhere else in the kernel can we find a similar construct, I find unfortunate to bury
innet/core/dev.csomething that might be incorrect in the future.
Indeed. It seems not much code is reading and checking the thread state. Not sure if there is any risk involved in doing this. The reason to check for the state and then set the bit is to try to avoid some atomic operations here. And the test I ran did show it is working properly. But the workload I tested does not represent all the scenarios.
Not sure what to do here. Should we remove the if () check and unconditionally set SCHED_THREADED bit?
Quoted 42 lines
Quoted 42 lines
Quoted 42 lines
net/core/dev.c +12
-2+set_bit (NAPI_STATE_SCHED_THREADED , &napi->state);wake_up_process (thread);return ; }@@ -6486,6 +6488,7 @@ bool napi_complete_done(struct napi_struct *n, int work_done)WARN_ON_ONCE (!(val &NAPIF_STATE_SCHED )); new = val & ~(NAPIF_STATE_MISSED |NAPIF_STATE_SCHED | +NAPIF_STATE_SCHED_THREADED |NAPIF_STATE_PREFER_BUSY_POLL );/* If STATE_MISSED was set, leave STATE_SCHED set, @@ -6968,16 +6971,25 @@ static int napi_poll(struct napi_struct *n, struct list_head *repoll)static int napi_thread_wait (struct napi_struct *napi) { +bool woken = false; +set_current_state (TASK_INTERRUPTIBLE );while (!kthread_should_stop () && !napi_disable_pending (napi)) { -if (test_bit (NAPI_STATE_SCHED , &napi->state)) { +/* Testing SCHED_THREADED bit here to make sure the current +* kthread owns this napi and could poll on this napi. +* Testing SCHED bit is not enough because SCHED bit might be +* set by some other busy poll thread or by napi_disable(). +*/ +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 ); --2.30.1.766.gb4fecdf3b7-goog