From: Johannes Berg johannes.berg@intel.com
There were many places that my previous spatch didn't find, as pointed out by yuan linyu in various patches.
The following spatch found many more and also removes the now unnecessary casts:
@@ identifier p, p2; expression len; expression skb; type t, t2; @@ ( -p = skb_put(skb, len); +p = skb_put_zero(skb, len); | -p = (t)skb_put(skb, len); +p = skb_put_zero(skb, len); ) ... when != p ( p2 = (t2)p; -memset(p2, 0, len); | -memset(p, 0, len); )
@@ type t, t2; identifier p, p2; expression skb; @@ t *p; ... ( -p = skb_put(skb, sizeof(t)); +p = skb_put_zero(skb, sizeof(t)); | -p = (t *)skb_put(skb, sizeof(t)); +p = skb_put_zero(skb, sizeof(t)); ) ... when != p ( p2 = (t2)p; -memset(p2, 0, sizeof(*p)); | -memset(p, 0, sizeof(*p)); )
@@ expression skb, len; @@ -memset(skb_put(skb, len), 0, len); +skb_put_zero(skb, len);
Apply it to the tree (with one manual fixup to keep the comment in vxlan.c, which spatch removed.)
Signed-off-by: Johannes Berg johannes.berg@intel.com Signed-off-by: David S. Miller davem@davemloft.net [sven@narfation.org: Add compat code] Signed-off-by: Sven Eckelmann sven@narfation.org --- This patch is already part of net-next. Only the compat code is new.
compat-include/linux/skbuff.h | 13 +++++++++++++ net/batman-adv/bat_v_elp.c | 3 +-- 2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/compat-include/linux/skbuff.h b/compat-include/linux/skbuff.h index 95f645ad..02bc11de 100644 --- a/compat-include/linux/skbuff.h +++ b/compat-include/linux/skbuff.h @@ -76,4 +76,17 @@ struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
#endif /* < KERNEL_VERSION(4, 2, 0) */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0) + +static inline void *skb_put_zero(struct sk_buff *skb, unsigned int len) +{ + void *tmp = skb_put(skb, len); + + memset(tmp, 0, len); + + return tmp; +} + +#endif /* < KERNEL_VERSION(4, 13, 0) */ + #endif /* _NET_BATMAN_ADV_COMPAT_LINUX_SKBUFF_H_ */ diff --git a/net/batman-adv/bat_v_elp.c b/net/batman-adv/bat_v_elp.c index b58007b7..bd1064d9 100644 --- a/net/batman-adv/bat_v_elp.c +++ b/net/batman-adv/bat_v_elp.c @@ -346,9 +346,8 @@ int batadv_v_elp_iface_enable(struct batadv_hard_iface *hard_iface) goto out;
skb_reserve(hard_iface->bat_v.elp_skb, ETH_HLEN + NET_IP_ALIGN); - elp_buff = skb_put(hard_iface->bat_v.elp_skb, BATADV_ELP_HLEN); + elp_buff = skb_put_zero(hard_iface->bat_v.elp_skb, BATADV_ELP_HLEN); elp_packet = (struct batadv_elp_packet *)elp_buff; - memset(elp_packet, 0, BATADV_ELP_HLEN);
elp_packet->packet_type = BATADV_ELP; elp_packet->version = BATADV_COMPAT_VERSION;
From: Johannes Berg johannes.berg@intel.com
A common pattern with skb_put() is to just want to memcpy() some data into the new space, introduce skb_put_data() for this.
An spatch similar to the one for skb_put_zero() converts many of the places using it:
@@ identifier p, p2; expression len, skb, data; type t, t2; @@ ( -p = skb_put(skb, len); +p = skb_put_data(skb, data, len); | -p = (t)skb_put(skb, len); +p = skb_put_data(skb, data, len); ) ( p2 = (t2)p; -memcpy(p2, data, len); | -memcpy(p, data, len); )
@@ type t, t2; identifier p, p2; expression skb, data; @@ t *p; ... ( -p = skb_put(skb, sizeof(t)); +p = skb_put_data(skb, data, sizeof(t)); | -p = (t *)skb_put(skb, sizeof(t)); +p = skb_put_data(skb, data, sizeof(t)); ) ( p2 = (t2)p; -memcpy(p2, data, sizeof(*p)); | -memcpy(p, data, sizeof(*p)); )
@@ expression skb, len, data; @@ -memcpy(skb_put(skb, len), data, len); +skb_put_data(skb, data, len);
(again, manually post-processed to retain some comments)
Reviewed-by: Stephen Hemminger stephen@networkplumber.org Signed-off-by: Johannes Berg johannes.berg@intel.com Signed-off-by: David S. Miller davem@davemloft.net [sven@narfation.org: Add compat code] Signed-off-by: Sven Eckelmann sven@narfation.org --- This patch is already part of net-next. Only the compat code is new.
compat-include/linux/skbuff.h | 10 ++++++++++ net/batman-adv/bat_iv_ogm.c | 4 ++-- net/batman-adv/bat_v_ogm.c | 6 ++---- net/batman-adv/fragmentation.c | 3 +-- 4 files changed, 15 insertions(+), 8 deletions(-)
diff --git a/compat-include/linux/skbuff.h b/compat-include/linux/skbuff.h index 02bc11de..23677e27 100644 --- a/compat-include/linux/skbuff.h +++ b/compat-include/linux/skbuff.h @@ -87,6 +87,16 @@ static inline void *skb_put_zero(struct sk_buff *skb, unsigned int len) return tmp; }
+static inline void *skb_put_data(struct sk_buff *skb, const void *data, + unsigned int len) +{ + void *tmp = skb_put(skb, len); + + memcpy(tmp, data, len); + + return tmp; +} + #endif /* < KERNEL_VERSION(4, 13, 0) */
#endif /* _NET_BATMAN_ADV_COMPAT_LINUX_SKBUFF_H_ */ diff --git a/net/batman-adv/bat_iv_ogm.c b/net/batman-adv/bat_iv_ogm.c index fa8d6b47..a3501173 100644 --- a/net/batman-adv/bat_iv_ogm.c +++ b/net/batman-adv/bat_iv_ogm.c @@ -732,8 +732,8 @@ static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr, unsigned char *skb_buff; unsigned long new_direct_link_flag;
- skb_buff = skb_put(forw_packet_aggr->skb, packet_len); - memcpy(skb_buff, packet_buff, packet_len); + skb_buff = skb_put_data(forw_packet_aggr->skb, packet_buff, + packet_len); forw_packet_aggr->packet_len += packet_len; forw_packet_aggr->num_packets++;
diff --git a/net/batman-adv/bat_v_ogm.c b/net/batman-adv/bat_v_ogm.c index 03a35c9f..1e3dc374 100644 --- a/net/batman-adv/bat_v_ogm.c +++ b/net/batman-adv/bat_v_ogm.c @@ -166,8 +166,7 @@ static void batadv_v_ogm_send(struct work_struct *work) goto reschedule;
skb_reserve(skb, ETH_HLEN); - pkt_buff = skb_put(skb, ogm_buff_len); - memcpy(pkt_buff, ogm_buff, ogm_buff_len); + pkt_buff = skb_put_data(skb, ogm_buff, ogm_buff_len);
ogm_packet = (struct batadv_ogm2_packet *)skb->data; ogm_packet->seqno = htonl(atomic_read(&bat_priv->bat_v.ogm_seqno)); @@ -382,8 +381,7 @@ static void batadv_v_ogm_forward(struct batadv_priv *bat_priv, goto out;
skb_reserve(skb, ETH_HLEN); - skb_buff = skb_put(skb, packet_len); - memcpy(skb_buff, ogm_received, packet_len); + skb_buff = skb_put_data(skb, ogm_received, packet_len);
/* apply forward penalty */ ogm_forward = (struct batadv_ogm2_packet *)skb_buff; diff --git a/net/batman-adv/fragmentation.c b/net/batman-adv/fragmentation.c index 8f964bea..a98cf110 100644 --- a/net/batman-adv/fragmentation.c +++ b/net/batman-adv/fragmentation.c @@ -296,8 +296,7 @@ batadv_frag_merge_packets(struct hlist_head *chain) /* Copy the payload of the each fragment into the last skb */ hlist_for_each_entry(entry, chain, list) { size = entry->skb->len - hdr_size; - memcpy(skb_put(skb_out, size), entry->skb->data + hdr_size, - size); + skb_put_data(skb_out, entry->skb->data + hdr_size, size); }
free:
From: Johannes Berg johannes.berg@intel.com
It seems like a historic accident that these return unsigned char *, and in many places that means casts are required, more often than not.
Make these functions (skb_put, __skb_put and pskb_put) return void * and remove all the casts across the tree, adding a (u8 *) cast only where the unsigned char pointer was used directly, all done with the following spatch:
@@ expression SKB, LEN; typedef u8; identifier fn = { skb_put, __skb_put }; @@ - *(fn(SKB, LEN)) + *(u8 *)fn(SKB, LEN)
@@ expression E, SKB, LEN; identifier fn = { skb_put, __skb_put }; type T; @@ - E = ((T *)(fn(SKB, LEN))) + E = fn(SKB, LEN)
which actually doesn't cover pskb_put since there are only three users overall.
A handful of stragglers were converted manually, notably a macro in drivers/isdn/i4l/isdn_bsdcomp.c and, oddly enough, one of the many instances in net/bluetooth/hci_sock.c. In the former file, I also had to fix one whitespace problem spatch introduced.
Signed-off-by: Johannes Berg johannes.berg@intel.com Signed-off-by: David S. Miller davem@davemloft.net [sven@narfation.org: Add compat code] Signed-off-by: Sven Eckelmann sven@narfation.org --- This patch is already part of net-next. Only the compat code is new.
compat-include/linux/skbuff.h | 6 ++++++ net/batman-adv/icmp_socket.c | 2 +- net/batman-adv/tp_meter.c | 6 +++--- 3 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/compat-include/linux/skbuff.h b/compat-include/linux/skbuff.h index 23677e27..03765c62 100644 --- a/compat-include/linux/skbuff.h +++ b/compat-include/linux/skbuff.h @@ -78,6 +78,12 @@ struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
+static inline void *batadv_skb_put(struct sk_buff *skb, unsigned int len) +{ + return (void *)skb_put(skb, len); +} +#define skb_put batadv_skb_put + static inline void *skb_put_zero(struct sk_buff *skb, unsigned int len) { void *tmp = skb_put(skb, len); diff --git a/net/batman-adv/icmp_socket.c b/net/batman-adv/icmp_socket.c index 6308c9f0..8ead2928 100644 --- a/net/batman-adv/icmp_socket.c +++ b/net/batman-adv/icmp_socket.c @@ -207,7 +207,7 @@ static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
skb->priority = TC_PRIO_CONTROL; skb_reserve(skb, ETH_HLEN); - icmp_header = (struct batadv_icmp_header *)skb_put(skb, packet_len); + icmp_header = skb_put(skb, packet_len);
if (copy_from_user(icmp_header, buff, packet_len)) { len = -EFAULT; diff --git a/net/batman-adv/tp_meter.c b/net/batman-adv/tp_meter.c index e3e2585d..bfe8effe 100644 --- a/net/batman-adv/tp_meter.c +++ b/net/batman-adv/tp_meter.c @@ -595,7 +595,7 @@ static int batadv_tp_send_msg(struct batadv_tp_vars *tp_vars, const u8 *src, return BATADV_TP_REASON_MEMORY_ERROR;
skb_reserve(skb, ETH_HLEN); - icmp = (struct batadv_icmp_tp_packet *)skb_put(skb, sizeof(*icmp)); + icmp = skb_put(skb, sizeof(*icmp));
/* fill the icmp header */ ether_addr_copy(icmp->dst, orig_node->orig); @@ -612,7 +612,7 @@ static int batadv_tp_send_msg(struct batadv_tp_vars *tp_vars, const u8 *src, icmp->timestamp = htonl(timestamp);
data_len = len - sizeof(*icmp); - data = (u8 *)skb_put(skb, data_len); + data = skb_put(skb, data_len); batadv_tp_fill_prerandom(tp_vars, data, data_len);
r = batadv_send_skb_to_orig(skb, orig_node, NULL); @@ -1190,7 +1190,7 @@ static int batadv_tp_send_ack(struct batadv_priv *bat_priv, const u8 *dst, }
skb_reserve(skb, ETH_HLEN); - icmp = (struct batadv_icmp_tp_packet *)skb_put(skb, sizeof(*icmp)); + icmp = skb_put(skb, sizeof(*icmp)); icmp->packet_type = BATADV_ICMP; icmp->version = BATADV_COMPAT_VERSION; icmp->ttl = BATADV_TTL;
On Sonntag, 18. Juni 2017 09:20:30 CEST Sven Eckelmann wrote:
From: Johannes Berg johannes.berg@intel.com
There were many places that my previous spatch didn't find, as pointed out by yuan linyu in various patches.
The following spatch found many more and also removes the now unnecessary casts:
[...]
Apply it to the tree (with one manual fixup to keep the comment in vxlan.c, which spatch removed.)
Signed-off-by: Johannes Berg johannes.berg@intel.com Signed-off-by: David S. Miller davem@davemloft.net [sven@narfation.org: Add compat code] Signed-off-by: Sven Eckelmann sven@narfation.org
This patch is already part of net-next. Only the compat code is new.
All patches were now applied because they will be submitted soon (by David Miller) to Linus for 4.13
* https://git.open-mesh.org/batman-adv.git/commit/3acf2107a025a333d4b28950bfbf... * https://git.open-mesh.org/batman-adv.git/commit/f05af5ac212f75bc24a419ae6446... * https://git.open-mesh.org/batman-adv.git/commit/e66e975fc252cbad49f4a65cb5cd...
Kind regards, Sven
b.a.t.m.a.n@lists.open-mesh.org