Patch "napi: fix race inside napi_enable" has been added to the 5.14-stable tree

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is a note to let you know that I've just added the patch titled

    napi: fix race inside napi_enable

to the 5.14-stable tree which can be found at:
    http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
     napi-fix-race-inside-napi_enable.patch
and it can be found in the queue-5.14 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree,
please let <stable@xxxxxxxxxxxxxxx> know about it.



commit e66cad414e6f11dd5921096760711ca2a37ed8c3
Author: Xuan Zhuo <xuanzhuo@xxxxxxxxxxxxxxxxx>
Date:   Sat Sep 18 16:52:32 2021 +0800

    napi: fix race inside napi_enable
    
    [ Upstream commit 3765996e4f0b8a755cab215a08df744490c76052 ]
    
    The process will cause napi.state to contain NAPI_STATE_SCHED and
    not in the poll_list, which will cause napi_disable() to get stuck.
    
    The prefix "NAPI_STATE_" is removed in the figure below, and
    NAPI_STATE_HASHED is ignored in napi.state.
    
                          CPU0       |                   CPU1       | napi.state
    ===============================================================================
    napi_disable()                   |                              | SCHED | NPSVC
    napi_enable()                    |                              |
    {                                |                              |
        smp_mb__before_atomic();     |                              |
        clear_bit(SCHED, &n->state); |                              | NPSVC
                                     | napi_schedule_prep()         | SCHED | NPSVC
                                     | napi_poll()                  |
                                     |   napi_complete_done()       |
                                     |   {                          |
                                     |      if (n->state & (NPSVC | | (1)
                                     |               _BUSY_POLL)))  |
                                     |           return false;      |
                                     |     ................         |
                                     |   }                          | SCHED | NPSVC
                                     |                              |
        clear_bit(NPSVC, &n->state); |                              | SCHED
    }                                |                              |
                                     |                              |
    napi_schedule_prep()             |                              | SCHED | MISSED (2)
    
    (1) Here return direct. Because of NAPI_STATE_NPSVC exists.
    (2) NAPI_STATE_SCHED exists. So not add napi.poll_list to sd->poll_list
    
    Since NAPI_STATE_SCHED already exists and napi is not in the
    sd->poll_list queue, NAPI_STATE_SCHED cannot be cleared and will always
    exist.
    
    1. This will cause this queue to no longer receive packets.
    2. If you encounter napi_disable under the protection of rtnl_lock, it
       will cause the entire rtnl_lock to be locked, affecting the overall
       system.
    
    This patch uses cmpxchg to implement napi_enable(), which ensures that
    there will be no race due to the separation of clear two bits.
    
    Fixes: 2d8bff12699abc ("netpoll: Close race condition between poll_one_napi and napi_disable")
    Signed-off-by: Xuan Zhuo <xuanzhuo@xxxxxxxxxxxxxxxxx>
    Reviewed-by: Dust Li <dust.li@xxxxxxxxxxxxxxxxx>
    Signed-off-by: David S. Miller <davem@xxxxxxxxxxxxx>
    Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>

diff --git a/net/core/dev.c b/net/core/dev.c
index 8f1a47ad6781..693f15a05630 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -6988,12 +6988,16 @@ EXPORT_SYMBOL(napi_disable);
  */
 void napi_enable(struct napi_struct *n)
 {
-	BUG_ON(!test_bit(NAPI_STATE_SCHED, &n->state));
-	smp_mb__before_atomic();
-	clear_bit(NAPI_STATE_SCHED, &n->state);
-	clear_bit(NAPI_STATE_NPSVC, &n->state);
-	if (n->dev->threaded && n->thread)
-		set_bit(NAPI_STATE_THREADED, &n->state);
+	unsigned long val, new;
+
+	do {
+		val = READ_ONCE(n->state);
+		BUG_ON(!test_bit(NAPI_STATE_SCHED, &val));
+
+		new = val & ~(NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC);
+		if (n->dev->threaded && n->thread)
+			new |= NAPIF_STATE_THREADED;
+	} while (cmpxchg(&n->state, val, new) != val);
 }
 EXPORT_SYMBOL(napi_enable);
 



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux