net: Fix data-races around weight_p and dev_weight_[rt]x_bias.
authorKuniyuki Iwashima <kuniyu@amazon.com>
Tue, 23 Aug 2022 17:46:45 +0000 (10:46 -0700)
committerDavid S. Miller <davem@davemloft.net>
Wed, 24 Aug 2022 12:46:57 +0000 (13:46 +0100)
While reading weight_p, it can be changed concurrently.  Thus, we need
to add READ_ONCE() to its reader.

Also, dev_[rt]x_weight can be read/written at the same time.  So, we
need to use READ_ONCE() and WRITE_ONCE() for its access.  Moreover, to
use the same weight_p while changing dev_[rt]x_weight, we add a mutex
in proc_do_dev_weight().

Fixes: 3d48b53fb2ae ("net: dev_weight: TX/RX orthogonality")
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
net/core/dev.c
net/core/sysctl_net_core.c
net/sched/sch_generic.c

index 716df64..b5b92dc 100644 (file)
@@ -5918,7 +5918,7 @@ static int process_backlog(struct napi_struct *napi, int quota)
                net_rps_action_and_irq_enable(sd);
        }
 
-       napi->weight = dev_rx_weight;
+       napi->weight = READ_ONCE(dev_rx_weight);
        while (again) {
                struct sk_buff *skb;
 
index 71a1359..7258915 100644 (file)
@@ -234,14 +234,17 @@ static int set_default_qdisc(struct ctl_table *table, int write,
 static int proc_do_dev_weight(struct ctl_table *table, int write,
                           void *buffer, size_t *lenp, loff_t *ppos)
 {
-       int ret;
+       static DEFINE_MUTEX(dev_weight_mutex);
+       int ret, weight;
 
+       mutex_lock(&dev_weight_mutex);
        ret = proc_dointvec(table, write, buffer, lenp, ppos);
-       if (ret != 0)
-               return ret;
-
-       dev_rx_weight = weight_p * dev_weight_rx_bias;
-       dev_tx_weight = weight_p * dev_weight_tx_bias;
+       if (!ret && write) {
+               weight = READ_ONCE(weight_p);
+               WRITE_ONCE(dev_rx_weight, weight * dev_weight_rx_bias);
+               WRITE_ONCE(dev_tx_weight, weight * dev_weight_tx_bias);
+       }
+       mutex_unlock(&dev_weight_mutex);
 
        return ret;
 }
index d47b968..99b697a 100644 (file)
@@ -409,7 +409,7 @@ static inline bool qdisc_restart(struct Qdisc *q, int *packets)
 
 void __qdisc_run(struct Qdisc *q)
 {
-       int quota = dev_tx_weight;
+       int quota = READ_ONCE(dev_tx_weight);
        int packets;
 
        while (qdisc_restart(q, &packets)) {