Skip to content

Commit 07cc7b0

Browse files
q2venPaolo Abeni
authored andcommitted
rtnetlink: Add bulk registration helpers for rtnetlink message handlers.
Before commit addf9b9 ("net: rtnetlink: use rcu to free rtnl message handlers"), once rtnl_msg_handlers[protocol] was allocated, the following rtnl_register_module() for the same protocol never failed. However, after the commit, rtnl_msg_handler[protocol][msgtype] needs to be allocated in each rtnl_register_module(), so each call could fail. Many callers of rtnl_register_module() do not handle the returned error, and we need to add many error handlings. To handle that easily, let's add wrapper functions for bulk registration of rtnetlink message handlers. Signed-off-by: Kuniyuki Iwashima <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent 9a3cd87 commit 07cc7b0

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

include/net/rtnetlink.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,30 @@ static inline enum rtnl_kinds rtnl_msgtype_kind(int msgtype)
2929
return msgtype & RTNL_KIND_MASK;
3030
}
3131

32+
struct rtnl_msg_handler {
33+
struct module *owner;
34+
int protocol;
35+
int msgtype;
36+
rtnl_doit_func doit;
37+
rtnl_dumpit_func dumpit;
38+
int flags;
39+
};
40+
3241
void rtnl_register(int protocol, int msgtype,
3342
rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
3443
int rtnl_register_module(struct module *owner, int protocol, int msgtype,
3544
rtnl_doit_func, rtnl_dumpit_func, unsigned int flags);
3645
int rtnl_unregister(int protocol, int msgtype);
3746
void rtnl_unregister_all(int protocol);
3847

48+
int __rtnl_register_many(const struct rtnl_msg_handler *handlers, int n);
49+
void __rtnl_unregister_many(const struct rtnl_msg_handler *handlers, int n);
50+
51+
#define rtnl_register_many(handlers) \
52+
__rtnl_register_many(handlers, ARRAY_SIZE(handlers))
53+
#define rtnl_unregister_many(handlers) \
54+
__rtnl_unregister_many(handlers, ARRAY_SIZE(handlers))
55+
3956
static inline int rtnl_msg_family(const struct nlmsghdr *nlh)
4057
{
4158
if (nlmsg_len(nlh) >= sizeof(struct rtgenmsg))

net/core/rtnetlink.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,35 @@ void rtnl_unregister_all(int protocol)
384384
}
385385
EXPORT_SYMBOL_GPL(rtnl_unregister_all);
386386

387+
int __rtnl_register_many(const struct rtnl_msg_handler *handlers, int n)
388+
{
389+
const struct rtnl_msg_handler *handler;
390+
int i, err;
391+
392+
for (i = 0, handler = handlers; i < n; i++, handler++) {
393+
err = rtnl_register_internal(handler->owner, handler->protocol,
394+
handler->msgtype, handler->doit,
395+
handler->dumpit, handler->flags);
396+
if (err) {
397+
__rtnl_unregister_many(handlers, i);
398+
break;
399+
}
400+
}
401+
402+
return err;
403+
}
404+
EXPORT_SYMBOL_GPL(__rtnl_register_many);
405+
406+
void __rtnl_unregister_many(const struct rtnl_msg_handler *handlers, int n)
407+
{
408+
const struct rtnl_msg_handler *handler;
409+
int i;
410+
411+
for (i = n - 1, handler = handlers + n - 1; i >= 0; i--, handler--)
412+
rtnl_unregister(handler->protocol, handler->msgtype);
413+
}
414+
EXPORT_SYMBOL_GPL(__rtnl_unregister_many);
415+
387416
static LIST_HEAD(link_ops);
388417

389418
static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)

0 commit comments

Comments
 (0)