Skip to content

Commit 2ae0f17

Browse files
jmberg-inteldavem330
authored andcommitted
genetlink: use idr to track families
Since generic netlink family IDs are small integers, allocated densely, IDR is an ideal match for lookups. Replace the existing hand-written hash-table with IDR for allocation and lookup. This lets the families only be written to once, during register, since the list_head can be removed and removal of a family won't cause any writes. It also slightly reduces the code size (by about 1.3k on x86-64). Signed-off-by: Johannes Berg <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 489111e commit 2ae0f17

File tree

3 files changed

+123
-181
lines changed

3 files changed

+123
-181
lines changed

include/net/genetlink.h

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ struct genl_info;
4040
* generic netlink family is removed while there are still open
4141
* sockets.
4242
* @attrbuf: buffer to store parsed attributes (private)
43-
* @family_list: family list (private)
4443
* @mcgrps: multicast groups used by this family
4544
* @n_mcgrps: number of multicast groups
4645
* @mcgrp_offset: starting number of multicast group IDs in this family
@@ -70,11 +69,10 @@ struct genl_family {
7069
unsigned int n_ops;
7170
unsigned int n_mcgrps;
7271
unsigned int mcgrp_offset; /* private */
73-
struct list_head family_list; /* private */
7472
struct module *module;
7573
};
7674

77-
struct nlattr **genl_family_attrbuf(struct genl_family *family);
75+
struct nlattr **genl_family_attrbuf(const struct genl_family *family);
7876

7977
/**
8078
* struct genl_info - receiving information
@@ -134,12 +132,12 @@ struct genl_ops {
134132
};
135133

136134
int genl_register_family(struct genl_family *family);
137-
int genl_unregister_family(struct genl_family *family);
138-
void genl_notify(struct genl_family *family, struct sk_buff *skb,
135+
int genl_unregister_family(const struct genl_family *family);
136+
void genl_notify(const struct genl_family *family, struct sk_buff *skb,
139137
struct genl_info *info, u32 group, gfp_t flags);
140138

141139
void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
142-
struct genl_family *family, int flags, u8 cmd);
140+
const struct genl_family *family, int flags, u8 cmd);
143141

144142
/**
145143
* genlmsg_nlhdr - Obtain netlink header from user specified header
@@ -148,8 +146,8 @@ void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
148146
*
149147
* Returns pointer to netlink header.
150148
*/
151-
static inline struct nlmsghdr *genlmsg_nlhdr(void *user_hdr,
152-
struct genl_family *family)
149+
static inline struct nlmsghdr *
150+
genlmsg_nlhdr(void *user_hdr, const struct genl_family *family)
153151
{
154152
return (struct nlmsghdr *)((char *)user_hdr -
155153
family->hdrsize -
@@ -185,7 +183,7 @@ static inline int genlmsg_parse(const struct nlmsghdr *nlh,
185183
*/
186184
static inline void genl_dump_check_consistent(struct netlink_callback *cb,
187185
void *user_hdr,
188-
struct genl_family *family)
186+
const struct genl_family *family)
189187
{
190188
nl_dump_check_consistent(cb, genlmsg_nlhdr(user_hdr, family));
191189
}
@@ -202,7 +200,7 @@ static inline void genl_dump_check_consistent(struct netlink_callback *cb,
202200
*/
203201
static inline void *genlmsg_put_reply(struct sk_buff *skb,
204202
struct genl_info *info,
205-
struct genl_family *family,
203+
const struct genl_family *family,
206204
int flags, u8 cmd)
207205
{
208206
return genlmsg_put(skb, info->snd_portid, info->snd_seq, family,
@@ -239,7 +237,7 @@ static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)
239237
* @group: offset of multicast group in groups array
240238
* @flags: allocation flags
241239
*/
242-
static inline int genlmsg_multicast_netns(struct genl_family *family,
240+
static inline int genlmsg_multicast_netns(const struct genl_family *family,
243241
struct net *net, struct sk_buff *skb,
244242
u32 portid, unsigned int group, gfp_t flags)
245243
{
@@ -257,7 +255,7 @@ static inline int genlmsg_multicast_netns(struct genl_family *family,
257255
* @group: offset of multicast group in groups array
258256
* @flags: allocation flags
259257
*/
260-
static inline int genlmsg_multicast(struct genl_family *family,
258+
static inline int genlmsg_multicast(const struct genl_family *family,
261259
struct sk_buff *skb, u32 portid,
262260
unsigned int group, gfp_t flags)
263261
{
@@ -275,7 +273,7 @@ static inline int genlmsg_multicast(struct genl_family *family,
275273
*
276274
* This function must hold the RTNL or rcu_read_lock().
277275
*/
278-
int genlmsg_multicast_allns(struct genl_family *family,
276+
int genlmsg_multicast_allns(const struct genl_family *family,
279277
struct sk_buff *skb, u32 portid,
280278
unsigned int group, gfp_t flags);
281279

@@ -359,16 +357,17 @@ static inline struct sk_buff *genlmsg_new(size_t payload, gfp_t flags)
359357
* This function returns the number of broadcast listeners that have set the
360358
* NETLINK_RECV_NO_ENOBUFS socket option.
361359
*/
362-
static inline int genl_set_err(struct genl_family *family, struct net *net,
363-
u32 portid, u32 group, int code)
360+
static inline int genl_set_err(const struct genl_family *family,
361+
struct net *net, u32 portid,
362+
u32 group, int code)
364363
{
365364
if (WARN_ON_ONCE(group >= family->n_mcgrps))
366365
return -EINVAL;
367366
group = family->mcgrp_offset + group;
368367
return netlink_set_err(net->genl_sock, portid, group, code);
369368
}
370369

371-
static inline int genl_has_listeners(struct genl_family *family,
370+
static inline int genl_has_listeners(const struct genl_family *family,
372371
struct net *net, unsigned int group)
373372
{
374373
if (WARN_ON_ONCE(group >= family->n_mcgrps))

include/uapi/linux/genetlink.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ struct genlmsghdr {
2929
#define GENL_ID_CTRL NLMSG_MIN_TYPE
3030
#define GENL_ID_VFS_DQUOT (NLMSG_MIN_TYPE + 1)
3131
#define GENL_ID_PMCRAID (NLMSG_MIN_TYPE + 2)
32+
/* must be last reserved + 1 */
33+
#define GENL_START_ALLOC (NLMSG_MIN_TYPE + 3)
3234

3335
/**************************************************************************
3436
* Controller

0 commit comments

Comments
 (0)