Skip to content

Commit

Permalink
net-sysfs: take the rtnl lock when accessing xps_cpus_map and num_tc
Browse files Browse the repository at this point in the history
Accesses to dev->xps_cpus_map (when using dev->num_tc) should be
protected by the rtnl lock, like we do for netif_set_xps_queue. I didn't
see an actual bug being triggered, but let's be safe here and take the
rtnl lock while accessing the map in sysfs.

Fixes: 184c449 ("net: Add support for XPS with QoS via traffic classes")
Signed-off-by: Antoine Tenart <atenart@kernel.org>
Reviewed-by: Alexander Duyck <alexanderduyck@fb.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
  • Loading branch information
atenart authored and kuba-moo committed Dec 28, 2020
1 parent 1ad5822 commit fb25038
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions net/core/net-sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1317,8 +1317,8 @@ static const struct attribute_group dql_group = {
static ssize_t xps_cpus_show(struct netdev_queue *queue,
char *buf)
{
int cpu, len, ret, num_tc = 1, tc = 0;
struct net_device *dev = queue->dev;
int cpu, len, num_tc = 1, tc = 0;
struct xps_dev_maps *dev_maps;
cpumask_var_t mask;
unsigned long index;
Expand All @@ -1328,22 +1328,31 @@ static ssize_t xps_cpus_show(struct netdev_queue *queue,

index = get_netdev_queue_index(queue);

if (!rtnl_trylock())
return restart_syscall();

if (dev->num_tc) {
/* Do not allow XPS on subordinate device directly */
num_tc = dev->num_tc;
if (num_tc < 0)
return -EINVAL;
if (num_tc < 0) {
ret = -EINVAL;
goto err_rtnl_unlock;
}

/* If queue belongs to subordinate dev use its map */
dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;

tc = netdev_txq_to_tc(dev, index);
if (tc < 0)
return -EINVAL;
if (tc < 0) {
ret = -EINVAL;
goto err_rtnl_unlock;
}
}

if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
return -ENOMEM;
if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
ret = -ENOMEM;
goto err_rtnl_unlock;
}

rcu_read_lock();
dev_maps = rcu_dereference(dev->xps_cpus_map);
Expand All @@ -1366,9 +1375,15 @@ static ssize_t xps_cpus_show(struct netdev_queue *queue,
}
rcu_read_unlock();

rtnl_unlock();

len = snprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask));
free_cpumask_var(mask);
return len < PAGE_SIZE ? len : -EINVAL;

err_rtnl_unlock:
rtnl_unlock();
return ret;
}

static ssize_t xps_cpus_store(struct netdev_queue *queue,
Expand Down

0 comments on commit fb25038

Please sign in to comment.