内核的接口很多都显式的要求在rtnl lock的保护下,它是一个mutex锁,用来保护netdevice等相关的数据,在内核,用户配置间的一致性。
定义在:net/core/rtnetlink.c
1 2 3 4 5 | void rtnl_lock( void ) { mutex_lock(&rtnl_mutex); } EXPORT_SYMBOL(rtnl_lock); |
Rohan Puri
This lock is used to serialize changes to net_device instances from runtime
events, conf changes
anywhere. Thanks,
What is rtnl mutex used for?
1 serializes all rtnetlink requests
2 serializes with other userspace apis (sysfs, ioctl, ...) to network configuration
3 protects list of net namespaces
As a consequence:
one request at a time, e.g. adding ip address must wait for user listing interface properties
dump requests (fib, tc classifier list, interfaces)... are also serialized
rtnl_mutex can be held for very long times:
schedule() (incl. GFP_KERNEL allocations) s
ynchronize_rcu(_net) rtnetlink: caveats
callbacks rely on rtnl mutex being held
rtnl_lock guarantees consistency during a dump
can’t blindly avoid rtnl mutex
allow to annotate handler: RTNL_DOIT_UNLOCKED
then start to push rtnl_lock down
https://lists.openfabrics.org/pipermail/general/2008-July/052458.html
https://lists.kernelnewbies.org/pipermail/kernelnewbies/2011-November/003979.html
https://www.netdevconf.org/2.2/slides/westphal-rtnlmutex-talk.pdf