Commit c1038510 authored by Ahmed S. Darwish's avatar Ahmed S. Darwish Committed by Kelsey Skunberg

net: core: device_rename: Use rwsem instead of a seqcount

BugLink: https://bugs.launchpad.net/bugs/1885932

[ Upstream commit 11d6011c ]

Sequence counters write paths are critical sections that must never be
preempted, and blocking, even for CONFIG_PREEMPTION=n, is not allowed.

Commit 5dbe7c17 ("net: fix kernel deadlock with interface rename and
netdev name retrieval.") handled a deadlock, observed with
CONFIG_PREEMPTION=n, where the devnet_rename seqcount read side was
infinitely spinning: it got scheduled after the seqcount write side
blocked inside its own critical section.

To fix that deadlock, among other issues, the commit added a
cond_resched() inside the read side section. While this will get the
non-preemptible kernel eventually unstuck, the seqcount reader is fully
exhausting its slice just spinning -- until TIF_NEED_RESCHED is set.

The fix is also still broken: if the seqcount reader belongs to a
real-time scheduling policy, it can spin forever and the kernel will
livelock.

Disabling preemption over the seqcount write side critical section will
not work: inside it are a number of GFP_KERNEL allocations and mutex
locking through the drivers/base/ :: device_rename() call chain.

>From all the above, replace the seqcount with a rwsem.

Fixes: 5dbe7c17 (net: fix kernel deadlock with interface rename and netdev name retrieval.)
Fixes: 30e6c9fa (net: devnet_rename_seq should be a seqcount)
Fixes: c91f6df2 (sockopt: Change getsockopt() of SO_BINDTODEVICE to return an interface name)
Cc: <stable@vger.kernel.org>
Reported-by: kbuild test robot <lkp@intel.com> [ v1 missing up_read() on error exit ]
Reported-by: Dan Carpenter <dan.carpenter@oracle.com> [ v1 missing up_read() on error exit ]
Signed-off-by: default avatarAhmed S. Darwish <a.darwish@linutronix.de>
Reviewed-by: default avatarSebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
Signed-off-by: default avatarKamal Mostafa <kamal@canonical.com>
Signed-off-by: default avatarKelsey Skunberg <kelsey.skunberg@canonical.com>
parent c2f0078e
...@@ -82,6 +82,7 @@ ...@@ -82,6 +82,7 @@
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/rwsem.h>
#include <linux/string.h> #include <linux/string.h>
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/socket.h> #include <linux/socket.h>
...@@ -185,7 +186,7 @@ static DEFINE_SPINLOCK(napi_hash_lock); ...@@ -185,7 +186,7 @@ static DEFINE_SPINLOCK(napi_hash_lock);
static unsigned int napi_gen_id = NR_CPUS; static unsigned int napi_gen_id = NR_CPUS;
static DEFINE_HASHTABLE(napi_hash, 8); static DEFINE_HASHTABLE(napi_hash, 8);
static seqcount_t devnet_rename_seq; static DECLARE_RWSEM(devnet_rename_sem);
static inline void dev_base_seq_inc(struct net *net) static inline void dev_base_seq_inc(struct net *net)
{ {
...@@ -862,33 +863,28 @@ EXPORT_SYMBOL(dev_get_by_index); ...@@ -862,33 +863,28 @@ EXPORT_SYMBOL(dev_get_by_index);
* @net: network namespace * @net: network namespace
* @name: a pointer to the buffer where the name will be stored. * @name: a pointer to the buffer where the name will be stored.
* @ifindex: the ifindex of the interface to get the name from. * @ifindex: the ifindex of the interface to get the name from.
*
* The use of raw_seqcount_begin() and cond_resched() before
* retrying is required as we want to give the writers a chance
* to complete when CONFIG_PREEMPTION is not set.
*/ */
int netdev_get_name(struct net *net, char *name, int ifindex) int netdev_get_name(struct net *net, char *name, int ifindex)
{ {
struct net_device *dev; struct net_device *dev;
unsigned int seq; int ret;
retry: down_read(&devnet_rename_sem);
seq = raw_seqcount_begin(&devnet_rename_seq);
rcu_read_lock(); rcu_read_lock();
dev = dev_get_by_index_rcu(net, ifindex); dev = dev_get_by_index_rcu(net, ifindex);
if (!dev) { if (!dev) {
rcu_read_unlock(); ret = -ENODEV;
return -ENODEV; goto out;
} }
strcpy(name, dev->name); strcpy(name, dev->name);
rcu_read_unlock();
if (read_seqcount_retry(&devnet_rename_seq, seq)) {
cond_resched();
goto retry;
}
return 0; ret = 0;
out:
rcu_read_unlock();
up_read(&devnet_rename_sem);
return ret;
} }
/** /**
...@@ -1153,10 +1149,10 @@ int dev_change_name(struct net_device *dev, const char *newname) ...@@ -1153,10 +1149,10 @@ int dev_change_name(struct net_device *dev, const char *newname)
if (dev->flags & IFF_UP) if (dev->flags & IFF_UP)
return -EBUSY; return -EBUSY;
write_seqcount_begin(&devnet_rename_seq); down_write(&devnet_rename_sem);
if (strncmp(newname, dev->name, IFNAMSIZ) == 0) { if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
write_seqcount_end(&devnet_rename_seq); up_write(&devnet_rename_sem);
return 0; return 0;
} }
...@@ -1164,7 +1160,7 @@ int dev_change_name(struct net_device *dev, const char *newname) ...@@ -1164,7 +1160,7 @@ int dev_change_name(struct net_device *dev, const char *newname)
err = dev_get_valid_name(net, dev, newname); err = dev_get_valid_name(net, dev, newname);
if (err < 0) { if (err < 0) {
write_seqcount_end(&devnet_rename_seq); up_write(&devnet_rename_sem);
return err; return err;
} }
...@@ -1179,11 +1175,11 @@ int dev_change_name(struct net_device *dev, const char *newname) ...@@ -1179,11 +1175,11 @@ int dev_change_name(struct net_device *dev, const char *newname)
if (ret) { if (ret) {
memcpy(dev->name, oldname, IFNAMSIZ); memcpy(dev->name, oldname, IFNAMSIZ);
dev->name_assign_type = old_assign_type; dev->name_assign_type = old_assign_type;
write_seqcount_end(&devnet_rename_seq); up_write(&devnet_rename_sem);
return ret; return ret;
} }
write_seqcount_end(&devnet_rename_seq); up_write(&devnet_rename_sem);
netdev_adjacent_rename_links(dev, oldname); netdev_adjacent_rename_links(dev, oldname);
...@@ -1204,7 +1200,7 @@ int dev_change_name(struct net_device *dev, const char *newname) ...@@ -1204,7 +1200,7 @@ int dev_change_name(struct net_device *dev, const char *newname)
/* err >= 0 after dev_alloc_name() or stores the first errno */ /* err >= 0 after dev_alloc_name() or stores the first errno */
if (err >= 0) { if (err >= 0) {
err = ret; err = ret;
write_seqcount_begin(&devnet_rename_seq); down_write(&devnet_rename_sem);
memcpy(dev->name, oldname, IFNAMSIZ); memcpy(dev->name, oldname, IFNAMSIZ);
memcpy(oldname, newname, IFNAMSIZ); memcpy(oldname, newname, IFNAMSIZ);
dev->name_assign_type = old_assign_type; dev->name_assign_type = old_assign_type;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment