Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f0b07bb1 authored by Kirill Tkhai's avatar Kirill Tkhai Committed by David S. Miller
Browse files

net: Introduce net_rwsem to protect net_namespace_list



rtnl_lock() is used everywhere, and contention is very high.
When someone wants to iterate over alive net namespaces,
he/she has no a possibility to do that without exclusive lock.
But the exclusive rtnl_lock() in such places is overkill,
and it just increases the contention. Yes, there is already
for_each_net_rcu() in kernel, but it requires rcu_read_lock(),
and this can't be sleepable. Also, sometimes it may be need
really prevent net_namespace_list growth, so for_each_net_rcu()
is not fit there.

This patch introduces new rw_semaphore, which will be used
instead of rtnl_mutex to protect net_namespace_list. It is
sleepable and allows not-exclusive iterations over net
namespaces list. It allows to stop using rtnl_lock()
in several places (what is made in next patches) and makes
less the time, we keep rtnl_mutex. Here we just add new lock,
while the explanation of we can remove rtnl_lock() there are
in next patches.

Fine grained locks generally are better, then one big lock,
so let's do that with net_namespace_list, while the situation
allows that.

Signed-off-by: default avatarKirill Tkhai <ktkhai@virtuozzo.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 906edee9
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -403,10 +403,12 @@ static void enum_all_gids_of_dev_cb(struct ib_device *ib_dev,
	 * our feet
	 */
	rtnl_lock();
	down_read(&net_rwsem);
	for_each_net(net)
		for_each_netdev(net, ndev)
			if (is_eth_port_of_netdev(ib_dev, port, rdma_ndev, ndev))
				add_netdev_ips(ib_dev, port, rdma_ndev, ndev);
	up_read(&net_rwsem);
	rtnl_unlock();
}

+1 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ extern int rtnl_lock_killable(void);

extern wait_queue_head_t netdev_unregistering_wq;
extern struct rw_semaphore pernet_ops_rwsem;
extern struct rw_semaphore net_rwsem;

#ifdef CONFIG_PROVE_LOCKING
extern bool lockdep_rtnl_is_held(void);
+1 −0
Original line number Diff line number Diff line
@@ -291,6 +291,7 @@ static inline struct net *read_pnet(const possible_net_t *pnet)
#endif
}

/* Protected by net_rwsem */
#define for_each_net(VAR)				\
	list_for_each_entry(VAR, &net_namespace_list, list)

+5 −0
Original line number Diff line number Diff line
@@ -1629,6 +1629,7 @@ int register_netdevice_notifier(struct notifier_block *nb)
		goto unlock;
	if (dev_boot_phase)
		goto unlock;
	down_read(&net_rwsem);
	for_each_net(net) {
		for_each_netdev(net, dev) {
			err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
@@ -1642,6 +1643,7 @@ int register_netdevice_notifier(struct notifier_block *nb)
			call_netdevice_notifier(nb, NETDEV_UP, dev);
		}
	}
	up_read(&net_rwsem);

unlock:
	rtnl_unlock();
@@ -1664,6 +1666,7 @@ int register_netdevice_notifier(struct notifier_block *nb)
	}

outroll:
	up_read(&net_rwsem);
	raw_notifier_chain_unregister(&netdev_chain, nb);
	goto unlock;
}
@@ -1694,6 +1697,7 @@ int unregister_netdevice_notifier(struct notifier_block *nb)
	if (err)
		goto unlock;

	down_read(&net_rwsem);
	for_each_net(net) {
		for_each_netdev(net, dev) {
			if (dev->flags & IFF_UP) {
@@ -1704,6 +1708,7 @@ int unregister_netdevice_notifier(struct notifier_block *nb)
			call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
		}
	}
	up_read(&net_rwsem);
unlock:
	rtnl_unlock();
	return err;
+2 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ static unsigned int fib_seq_sum(void)
	struct net *net;

	rtnl_lock();
	down_read(&net_rwsem);
	for_each_net(net) {
		rcu_read_lock();
		list_for_each_entry_rcu(ops, &net->fib_notifier_ops, list) {
@@ -43,6 +44,7 @@ static unsigned int fib_seq_sum(void)
		}
		rcu_read_unlock();
	}
	up_read(&net_rwsem);
	rtnl_unlock();

	return fib_seq;
Loading