Skip to content

Commit 11d6011

Browse files
a-darwishdavem330
authored andcommitted
net: core: device_rename: Use rwsem instead of a seqcount
Sequence counters write paths are critical sections that must never be preempted, and blocking, even for CONFIG_PREEMPTION=n, is not allowed. Commit 5dbe7c1 ("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: 5dbe7c1 (net: fix kernel deadlock with interface rename and netdev name retrieval.) Fixes: 30e6c9f (net: devnet_rename_seq should be a seqcount) Fixes: c91f6df (sockopt: Change getsockopt() of SO_BINDTODEVICE to return an interface name) Cc: <[email protected]> Reported-by: kbuild test robot <[email protected]> [ v1 missing up_read() on error exit ] Reported-by: Dan Carpenter <[email protected]> [ v1 missing up_read() on error exit ] Signed-off-by: Ahmed S. Darwish <[email protected]> Reviewed-by: Sebastian Andrzej Siewior <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 67122a7 commit 11d6011

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

net/core/dev.c

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#include <linux/sched.h>
8080
#include <linux/sched/mm.h>
8181
#include <linux/mutex.h>
82+
#include <linux/rwsem.h>
8283
#include <linux/string.h>
8384
#include <linux/mm.h>
8485
#include <linux/socket.h>
@@ -194,7 +195,7 @@ static DEFINE_SPINLOCK(napi_hash_lock);
194195
static unsigned int napi_gen_id = NR_CPUS;
195196
static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
196197

197-
static seqcount_t devnet_rename_seq;
198+
static DECLARE_RWSEM(devnet_rename_sem);
198199

199200
static inline void dev_base_seq_inc(struct net *net)
200201
{
@@ -998,33 +999,28 @@ EXPORT_SYMBOL(dev_get_by_napi_id);
998999
* @net: network namespace
9991000
* @name: a pointer to the buffer where the name will be stored.
10001001
* @ifindex: the ifindex of the interface to get the name from.
1001-
*
1002-
* The use of raw_seqcount_begin() and cond_resched() before
1003-
* retrying is required as we want to give the writers a chance
1004-
* to complete when CONFIG_PREEMPTION is not set.
10051002
*/
10061003
int netdev_get_name(struct net *net, char *name, int ifindex)
10071004
{
10081005
struct net_device *dev;
1009-
unsigned int seq;
1006+
int ret;
10101007

1011-
retry:
1012-
seq = raw_seqcount_begin(&devnet_rename_seq);
1008+
down_read(&devnet_rename_sem);
10131009
rcu_read_lock();
1010+
10141011
dev = dev_get_by_index_rcu(net, ifindex);
10151012
if (!dev) {
1016-
rcu_read_unlock();
1017-
return -ENODEV;
1013+
ret = -ENODEV;
1014+
goto out;
10181015
}
10191016

10201017
strcpy(name, dev->name);
1021-
rcu_read_unlock();
1022-
if (read_seqcount_retry(&devnet_rename_seq, seq)) {
1023-
cond_resched();
1024-
goto retry;
1025-
}
10261018

1027-
return 0;
1019+
ret = 0;
1020+
out:
1021+
rcu_read_unlock();
1022+
up_read(&devnet_rename_sem);
1023+
return ret;
10281024
}
10291025

10301026
/**
@@ -1296,18 +1292,18 @@ int dev_change_name(struct net_device *dev, const char *newname)
12961292
likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK)))
12971293
return -EBUSY;
12981294

1299-
write_seqcount_begin(&devnet_rename_seq);
1295+
down_write(&devnet_rename_sem);
13001296

13011297
if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
1302-
write_seqcount_end(&devnet_rename_seq);
1298+
up_write(&devnet_rename_sem);
13031299
return 0;
13041300
}
13051301

13061302
memcpy(oldname, dev->name, IFNAMSIZ);
13071303

13081304
err = dev_get_valid_name(net, dev, newname);
13091305
if (err < 0) {
1310-
write_seqcount_end(&devnet_rename_seq);
1306+
up_write(&devnet_rename_sem);
13111307
return err;
13121308
}
13131309

@@ -1322,11 +1318,11 @@ int dev_change_name(struct net_device *dev, const char *newname)
13221318
if (ret) {
13231319
memcpy(dev->name, oldname, IFNAMSIZ);
13241320
dev->name_assign_type = old_assign_type;
1325-
write_seqcount_end(&devnet_rename_seq);
1321+
up_write(&devnet_rename_sem);
13261322
return ret;
13271323
}
13281324

1329-
write_seqcount_end(&devnet_rename_seq);
1325+
up_write(&devnet_rename_sem);
13301326

13311327
netdev_adjacent_rename_links(dev, oldname);
13321328

@@ -1347,7 +1343,7 @@ int dev_change_name(struct net_device *dev, const char *newname)
13471343
/* err >= 0 after dev_alloc_name() or stores the first errno */
13481344
if (err >= 0) {
13491345
err = ret;
1350-
write_seqcount_begin(&devnet_rename_seq);
1346+
down_write(&devnet_rename_sem);
13511347
memcpy(dev->name, oldname, IFNAMSIZ);
13521348
memcpy(oldname, newname, IFNAMSIZ);
13531349
dev->name_assign_type = old_assign_type;

0 commit comments

Comments
 (0)