Skip to content

Commit

Permalink
netlink: make nlmsg_end() and genlmsg_end() void
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
jmberg-intel authored and davem330 committed Jan 18, 2015
1 parent ede58ef commit 053c095
Show file tree
Hide file tree
Showing 51 changed files with 203 additions and 158 deletions.
7 changes: 1 addition & 6 deletions drivers/acpi/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ int acpi_bus_generate_netlink_event(const char *device_class,
struct acpi_genl_event *event;
void *msg_header;
int size;
int result;

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

/* send multicast genetlink message */
result = genlmsg_end(skb, msg_header);
if (result < 0) {
nlmsg_free(skb);
return result;
}
genlmsg_end(skb, msg_header);

genlmsg_multicast(&acpi_event_genl_family, skb, 0, 0, GFP_ATOMIC);
return 0;
Expand Down
3 changes: 2 additions & 1 deletion drivers/net/ethernet/rocker/rocker.c
Original file line number Diff line number Diff line change
Expand Up @@ -3674,7 +3674,8 @@ static int rocker_fdb_fill_info(struct sk_buff *skb,
if (vid && nla_put_u16(skb, NDA_VLAN, vid))
goto nla_put_failure;

return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;

nla_put_failure:
nlmsg_cancel(skb, nlh);
Expand Down
3 changes: 2 additions & 1 deletion drivers/net/vxlan.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
goto nla_put_failure;

return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;

nla_put_failure:
nlmsg_cancel(skb, nlh);
Expand Down
3 changes: 2 additions & 1 deletion drivers/net/wireless/mac80211_hwsim.c
Original file line number Diff line number Diff line change
Expand Up @@ -2557,7 +2557,8 @@ static int mac80211_hwsim_get_radio(struct sk_buff *skb,
if (res < 0)
goto out_err;

return genlmsg_end(skb, hdr);
genlmsg_end(skb, hdr);
return 0;

out_err:
genlmsg_cancel(skb, hdr);
Expand Down
8 changes: 1 addition & 7 deletions drivers/scsi/pmcraid.c
Original file line number Diff line number Diff line change
Expand Up @@ -1473,13 +1473,7 @@ static int pmcraid_notify_aen(
}

/* send genetlink multicast message to notify appplications */
result = genlmsg_end(skb, msg_header);

if (result < 0) {
pmcraid_err("genlmsg_end failed\n");
nlmsg_free(skb);
return result;
}
genlmsg_end(skb, msg_header);

result = genlmsg_multicast(&pmcraid_event_family, skb,
0, 0, GFP_ATOMIC);
Expand Down
4 changes: 1 addition & 3 deletions drivers/target/target_core_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,7 @@ static int tcmu_netlink_event(enum tcmu_genl_cmd cmd, const char *name, int mino
if (ret < 0)
goto free_skb;

ret = genlmsg_end(skb, msg_header);
if (ret < 0)
goto free_skb;
genlmsg_end(skb, msg_header);

ret = genlmsg_multicast(&tcmu_genl_family, skb, 0,
TCMU_MCGRP_CONFIG, GFP_KERNEL);
Expand Down
6 changes: 1 addition & 5 deletions drivers/thermal/thermal_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1759,11 +1759,7 @@ int thermal_generate_netlink_event(struct thermal_zone_device *tz,
thermal_event->event = event;

/* send multicast genetlink message */
result = genlmsg_end(skb, msg_header);
if (result < 0) {
nlmsg_free(skb);
return result;
}
genlmsg_end(skb, msg_header);

result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
0, GFP_ATOMIC);
Expand Down
7 changes: 1 addition & 6 deletions fs/dlm/netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,8 @@ static int send_data(struct sk_buff *skb)
{
struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
void *data = genlmsg_data(genlhdr);
int rv;

rv = genlmsg_end(skb, data);
if (rv < 0) {
nlmsg_free(skb);
return rv;
}
genlmsg_end(skb, data);

return genlmsg_unicast(&init_net, skb, listener_nlportid);
}
Expand Down
4 changes: 2 additions & 2 deletions include/net/genetlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ static inline void *genlmsg_put_reply(struct sk_buff *skb,
* @skb: socket buffer the message is stored in
* @hdr: user specific header
*/
static inline int genlmsg_end(struct sk_buff *skb, void *hdr)
static inline void genlmsg_end(struct sk_buff *skb, void *hdr)
{
return nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
nlmsg_end(skb, hdr - GENL_HDRLEN - NLMSG_HDRLEN);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions include/net/netlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,14 +490,10 @@ static inline struct sk_buff *nlmsg_new(size_t payload, gfp_t flags)
* Corrects the netlink message header to include the appeneded
* attributes. Only necessary if attributes have been added to
* the message.
*
* Returns the total data length of the skb.
*/
static inline int nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
static inline void nlmsg_end(struct sk_buff *skb, struct nlmsghdr *nlh)
{
nlh->nlmsg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;

return skb->len;
}

/**
Expand Down
13 changes: 2 additions & 11 deletions kernel/taskstats.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,8 @@ static int send_reply(struct sk_buff *skb, struct genl_info *info)
{
struct genlmsghdr *genlhdr = nlmsg_data(nlmsg_hdr(skb));
void *reply = genlmsg_data(genlhdr);
int rc;

rc = genlmsg_end(skb, reply);
if (rc < 0) {
nlmsg_free(skb);
return rc;
}
genlmsg_end(skb, reply);

return genlmsg_reply(skb, info);
}
Expand All @@ -134,11 +129,7 @@ static void send_cpu_listeners(struct sk_buff *skb,
void *reply = genlmsg_data(genlhdr);
int rc, delcount = 0;

rc = genlmsg_end(skb, reply);
if (rc < 0) {
nlmsg_free(skb);
return;
}
genlmsg_end(skb, reply);

rc = 0;
down_read(&listeners->sem);
Expand Down
3 changes: 2 additions & 1 deletion net/bridge/br_fdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,8 @@ static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
if (fdb->vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16), &fdb->vlan_id))
goto nla_put_failure;

return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;

nla_put_failure:
nlmsg_cancel(skb, nlh);
Expand Down
3 changes: 2 additions & 1 deletion net/bridge/br_mdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ static int nlmsg_populate_mdb_fill(struct sk_buff *skb,

nla_nest_end(skb, nest2);
nla_nest_end(skb, nest);
return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;

end:
nla_nest_end(skb, nest);
Expand Down
3 changes: 2 additions & 1 deletion net/bridge/br_netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ static int br_fill_ifinfo(struct sk_buff *skb,
}

done:
return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;

nla_put_failure:
nlmsg_cancel(skb, nlh);
Expand Down
3 changes: 2 additions & 1 deletion net/can/gw.c
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,8 @@ static int cgw_put_job(struct sk_buff *skb, struct cgw_job *gwj, int type,
goto cancel;
}

return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;

cancel:
nlmsg_cancel(skb, nlh);
Expand Down
3 changes: 2 additions & 1 deletion net/core/fib_rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,8 @@ static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
if (ops->fill(rule, skb, frh) < 0)
goto nla_put_failure;

return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;

nla_put_failure:
nlmsg_cancel(skb, nlh);
Expand Down
12 changes: 8 additions & 4 deletions net/core/neighbour.c
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,8 @@ static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
goto nla_put_failure;

read_unlock_bh(&tbl->lock);
return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;

nla_put_failure:
read_unlock_bh(&tbl->lock);
Expand Down Expand Up @@ -1917,7 +1918,8 @@ static int neightbl_fill_param_info(struct sk_buff *skb,
goto errout;

read_unlock_bh(&tbl->lock);
return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;
errout:
read_unlock_bh(&tbl->lock);
nlmsg_cancel(skb, nlh);
Expand Down Expand Up @@ -2202,7 +2204,8 @@ static int neigh_fill_info(struct sk_buff *skb, struct neighbour *neigh,
nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
goto nla_put_failure;

return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;

nla_put_failure:
nlmsg_cancel(skb, nlh);
Expand Down Expand Up @@ -2232,7 +2235,8 @@ static int pneigh_fill_info(struct sk_buff *skb, struct pneigh_entry *pn,
if (nla_put(skb, NDA_DST, tbl->key_len, pn->key))
goto nla_put_failure;

return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;

nla_put_failure:
nlmsg_cancel(skb, nlh);
Expand Down
9 changes: 6 additions & 3 deletions net/core/rtnetlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,8 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,

nla_nest_end(skb, af_spec);

return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;

nla_put_failure:
nlmsg_cancel(skb, nlh);
Expand Down Expand Up @@ -2326,7 +2327,8 @@ static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
goto nla_put_failure;

return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;

nla_put_failure:
nlmsg_cancel(skb, nlh);
Expand Down Expand Up @@ -2809,7 +2811,8 @@ int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,

nla_nest_end(skb, protinfo);

return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;
nla_put_failure:
nlmsg_cancel(skb, nlh);
return -EMSGSIZE;
Expand Down
3 changes: 2 additions & 1 deletion net/decnet/dn_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,8 @@ static int dn_nl_fill_ifaddr(struct sk_buff *skb, struct dn_ifaddr *ifa,
nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) ||
nla_put_u32(skb, IFA_FLAGS, ifa_flags))
goto nla_put_failure;
return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;

nla_put_failure:
nlmsg_cancel(skb, nlh);
Expand Down
3 changes: 2 additions & 1 deletion net/decnet/dn_route.c
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,8 @@ static int dn_rt_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
nla_put_u32(skb, RTA_IIF, rt->fld.flowidn_iif) < 0)
goto errout;

return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;

errout:
nlmsg_cancel(skb, nlh);
Expand Down
3 changes: 2 additions & 1 deletion net/decnet/dn_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ static int dn_fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
nla_nest_end(skb, mp_head);
}

return nlmsg_end(skb, nlh);
nlmsg_end(skb, nlh);
return 0;

errout:
nlmsg_cancel(skb, nlh);
Expand Down
12 changes: 2 additions & 10 deletions net/ieee802154/netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,9 @@ int ieee802154_nl_mcast(struct sk_buff *msg, unsigned int group)
struct nlmsghdr *nlh = nlmsg_hdr(msg);
void *hdr = genlmsg_data(nlmsg_data(nlh));

if (genlmsg_end(msg, hdr) < 0)
goto out;
genlmsg_end(msg, hdr);

return genlmsg_multicast(&nl802154_family, msg, 0, group, GFP_ATOMIC);
out:
nlmsg_free(msg);
return -ENOBUFS;
}

struct sk_buff *ieee802154_nl_new_reply(struct genl_info *info,
Expand All @@ -96,13 +92,9 @@ int ieee802154_nl_reply(struct sk_buff *msg, struct genl_info *info)
struct nlmsghdr *nlh = nlmsg_hdr(msg);
void *hdr = genlmsg_data(nlmsg_data(nlh));

if (genlmsg_end(msg, hdr) < 0)
goto out;
genlmsg_end(msg, hdr);

return genlmsg_reply(msg, info);
out:
nlmsg_free(msg);
return -ENOBUFS;
}

static const struct genl_ops ieee8021154_ops[] = {
Expand Down
3 changes: 2 additions & 1 deletion net/ieee802154/nl-mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 portid,
}

wpan_phy_put(phy);
return genlmsg_end(msg, hdr);
genlmsg_end(msg, hdr);
return 0;

nla_put_failure:
wpan_phy_put(phy);
Expand Down
3 changes: 2 additions & 1 deletion net/ieee802154/nl-phy.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid,
goto nla_put_failure;
mutex_unlock(&phy->pib_lock);
kfree(buf);
return genlmsg_end(msg, hdr);
genlmsg_end(msg, hdr);
return 0;

nla_put_failure:
mutex_unlock(&phy->pib_lock);
Expand Down
6 changes: 4 additions & 2 deletions net/ieee802154/nl802154.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ static int nl802154_send_wpan_phy(struct cfg802154_registered_device *rdev,
goto nla_put_failure;

finish:
return genlmsg_end(msg, hdr);
genlmsg_end(msg, hdr);
return 0;

nla_put_failure:
genlmsg_cancel(msg, hdr);
Expand Down Expand Up @@ -489,7 +490,8 @@ nl802154_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
if (nla_put_u8(msg, NL802154_ATTR_LBT_MODE, wpan_dev->lbt))
goto nla_put_failure;

return genlmsg_end(msg, hdr);
genlmsg_end(msg, hdr);
return 0;

nla_put_failure:
genlmsg_cancel(msg, hdr);
Expand Down
Loading

0 comments on commit 053c095

Please sign in to comment.