Skip to content

Commit 053c095

Browse files
jmberg-inteldavem330
authored andcommitted
netlink: make nlmsg_end() and genlmsg_end() void
Contrary to common expectations for an "int" return, these functions return only a positive value -- if used correctly they cannot even return 0 because the message header will necessarily be in the skb. This makes the very common pattern of if (genlmsg_end(...) < 0) { ... } be a whole bunch of dead code. Many places also simply do return nlmsg_end(...); and the caller is expected to deal with it. This also commonly (at least for me) causes errors, because it is very common to write if (my_function(...)) /* error condition */ and if my_function() does "return nlmsg_end()" this is of course wrong. Additionally, there's not a single place in the kernel that actually needs the message length returned, and if anyone needs it later then it'll be very easy to just use skb->len there. Remove this, and make the functions void. This removes a bunch of dead code as described above. The patch adds lines because I did - return nlmsg_end(...); + nlmsg_end(...); + return 0; I could have preserved all the function's return values by returning skb->len, but instead I've audited all the places calling the affected functions and found that none cared. A few places actually compared the return value with <= 0 in dump functionality, but that could just be changed to < 0 with no change in behaviour, so I opted for the more efficient version. One instance of the error I've made numerous times now is also present in net/phonet/pn_netlink.c in the route_dumpit() function - it didn't check for <0 or <=0 and thus broke out of the loop every single time. I've preserved this since it will (I think) have caused the messages to userspace to be formatted differently with just a single message for every SKB returned to userspace. It's possible that this isn't needed for the tools that actually use this, but I don't even know what they are so couldn't test that changing this behaviour would be acceptable. Signed-off-by: Johannes Berg <johannes.berg@intel.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ede58ef commit 053c095

51 files changed

Lines changed: 203 additions & 158 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎drivers/acpi/event.c‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ int acpi_bus_generate_netlink_event(const char *device_class,
100100
struct acpi_genl_event *event;
101101
void *msg_header;
102102
int size;
103-
int result;
104103

105104
/* allocate memory */
106105
size = nla_total_size(sizeof(struct acpi_genl_event)) +
@@ -137,11 +136,7 @@ int acpi_bus_generate_netlink_event(const char *device_class,
137136
event->data = data;
138137

139138
/* send multicast genetlink message */
140-
result = genlmsg_end(skb, msg_header);
141-
if (result < 0) {
142-
nlmsg_free(skb);
143-
return result;
144-
}
139+
genlmsg_end(skb, msg_header);
145140

146141
genlmsg_multicast(&acpi_event_genl_family, skb, 0, 0, GFP_ATOMIC);
147142
return 0;

‎drivers/net/ethernet/rocker/rocker.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3674,7 +3674,8 @@ static int rocker_fdb_fill_info(struct sk_buff *skb,
36743674
if (vid && nla_put_u16(skb, NDA_VLAN, vid))
36753675
goto nla_put_failure;
36763676

3677-
return nlmsg_end(skb, nlh);
3677+
nlmsg_end(skb, nlh);
3678+
return 0;
36783679

36793680
nla_put_failure:
36803681
nlmsg_cancel(skb, nlh);

‎drivers/net/vxlan.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
363363
if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
364364
goto nla_put_failure;
365365

366-
return nlmsg_end(skb, nlh);
366+
nlmsg_end(skb, nlh);
367+
return 0;
367368

368369
nla_put_failure:
369370
nlmsg_cancel(skb, nlh);

‎drivers/net/wireless/mac80211_hwsim.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2557,7 +2557,8 @@ static int mac80211_hwsim_get_radio(struct sk_buff *skb,
25572557
if (res < 0)
25582558
goto out_err;
25592559

2560-
return genlmsg_end(skb, hdr);
2560+
genlmsg_end(skb, hdr);
2561+
return 0;
25612562

25622563
out_err:
25632564
genlmsg_cancel(skb, hdr);

‎drivers/scsi/pmcraid.c‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,13 +1473,7 @@ static int pmcraid_notify_aen(
14731473
}
14741474

14751475
/* send genetlink multicast message to notify appplications */
1476-
result = genlmsg_end(skb, msg_header);
1477-
1478-
if (result < 0) {
1479-
pmcraid_err("genlmsg_end failed\n");
1480-
nlmsg_free(skb);
1481-
return result;
1482-
}
1476+
genlmsg_end(skb, msg_header);
14831477

14841478
result = genlmsg_multicast(&pmcraid_event_family, skb,
14851479
0, 0, GFP_ATOMIC);

‎drivers/target/target_core_user.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -784,9 +784,7 @@ static int tcmu_netlink_event(enum tcmu_genl_cmd cmd, const char *name, int mino
784784
if (ret < 0)
785785
goto free_skb;
786786

787-
ret = genlmsg_end(skb, msg_header);
788-
if (ret < 0)
789-
goto free_skb;
787+
genlmsg_end(skb, msg_header);
790788

791789
ret = genlmsg_multicast(&tcmu_genl_family, skb, 0,
792790
TCMU_MCGRP_CONFIG, GFP_KERNEL);

‎drivers/thermal/thermal_core.c‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,11 +1759,7 @@ int thermal_generate_netlink_event(struct thermal_zone_device *tz,
17591759
thermal_event->event = event;
17601760

17611761
/* send multicast genetlink message */
1762-
result = genlmsg_end(skb, msg_header);
1763-
if (result < 0) {
1764-
nlmsg_free(skb);
1765-
return result;
1766-
}
1762+
genlmsg_end(skb, msg_header);
17671763

17681764
result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
17691765
0, GFP_ATOMIC);

‎fs/dlm/netlink.c‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,8 @@ static int send_data(struct sk_buff *skb)
5656
{
5757
struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
5858
void *data = genlmsg_data(genlhdr);
59-
int rv;
6059

61-
rv = genlmsg_end(skb, data);
62-
if (rv < 0) {
63-
nlmsg_free(skb);
64-
return rv;
65-
}
60+
genlmsg_end(skb, data);
6661

6762
return genlmsg_unicast(&init_net, skb, listener_nlportid);
6863
}

‎include/net/genetlink.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ static inline void *genlmsg_put_reply(struct sk_buff *skb,
245245
* @skb: socket buffer the message is stored in
246246
* @hdr: user specific header
247247
*/
248-
static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
248+
static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
249249
{
250-
return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
250+
nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
251251
}
252252

253253
/**

‎include/net/netlink.h‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,10 @@ static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
490490
* Corrects the netlink message header to include the appeneded
491491
* attributes. Only necessary if attributes have been added to
492492
* the message.
493-
*
494-
* Returns the total data length of the skb.
495493
*/
496-
static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
494+
static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
497495
{
498496
nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
499-
500-
return skb->len;
501497
}
502498

503499
/**

0 commit comments

Comments
 (0)