ILD

rtnl lock
作者:Yuan Jianpeng 邮箱:yuanjp89@163.com
发布时间:2019-8-20 站点:Inside Linux Development

内核的接口很多都显式的要求在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


Yes, I understood this part, but I am wondering what is the purpose of
this lock.   I am guessing it's to protect all network related
operations from critical events, for e.g.: protecting a packet
transmit during device removal, protecting routing table entry during
route lookup, etc., but I can't find its precise documentation

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


Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.