Skip to content

Commit 019a316

Browse files
Florian Westphaldavem330
authored andcommitted
rtnetlink: add reference counting to prevent module unload while dump is in progress
I don't see what prevents rmmod (unregister_all is called) while a dump is active. Even if we'd add rtnl lock/unlock pair to unregister_all (as done here), thats not enough either as rtnl_lock is released right before the dump process starts. So this adds a refcount: * acquire rtnl mutex * bump refcount * release mutex * start the dump ... and make unregister_all remove the callbacks (no new dumps possible) and then wait until refcount is 0. Signed-off-by: Florian Westphal <[email protected]> Reviewed-by: Hannes Frederic Sowa <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent b97bac6 commit 019a316

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

net/core/rtnetlink.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ EXPORT_SYMBOL(lockdep_rtnl_is_held);
127127
#endif /* #ifdef CONFIG_PROVE_LOCKING */
128128

129129
static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
130+
static refcount_t rtnl_msg_handlers_ref[RTNL_FAMILY_MAX + 1];
130131

131132
static inline int rtm_msgindex(int msgtype)
132133
{
@@ -272,10 +273,18 @@ EXPORT_SYMBOL_GPL(rtnl_unregister);
272273
*/
273274
void rtnl_unregister_all(int protocol)
274275
{
276+
struct rtnl_link *handlers;
277+
275278
BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
276279

277-
kfree(rtnl_msg_handlers[protocol]);
280+
rtnl_lock();
281+
handlers = rtnl_msg_handlers[protocol];
278282
rtnl_msg_handlers[protocol] = NULL;
283+
rtnl_unlock();
284+
285+
while (refcount_read(&rtnl_msg_handlers_ref[protocol]) > 0)
286+
schedule();
287+
kfree(handlers);
279288
}
280289
EXPORT_SYMBOL_GPL(rtnl_unregister_all);
281290

@@ -4173,6 +4182,8 @@ static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
41734182
if (dumpit == NULL)
41744183
return -EOPNOTSUPP;
41754184

4185+
refcount_inc(&rtnl_msg_handlers_ref[family]);
4186+
41764187
if (type == RTM_GETLINK)
41774188
min_dump_alloc = rtnl_calcit(skb, nlh);
41784189

@@ -4186,6 +4197,7 @@ static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
41864197
err = netlink_dump_start(rtnl, skb, nlh, &c);
41874198
}
41884199
rtnl_lock();
4200+
refcount_dec(&rtnl_msg_handlers_ref[family]);
41894201
return err;
41904202
}
41914203

0 commit comments

Comments
 (0)