Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: introduce skb_list_walk_safe for skb segment walking

As part of the continual effort to remove direct usage of skb->next and
skb->prev, this patch adds a helper for iterating through the
singly-linked variant of skb lists, which are used for lists of GSO
packet. The name "skb_list_..." has been chosen to match the existing
function, "kfree_skb_list, which also operates on these singly-linked
lists, and the "..._walk_safe" part is the same idiom as elsewhere in
the kernel.

This patch removes the helper from wireguard and puts it into
linux/skbuff.h, while making it a bit more robust for general usage. In
particular, parenthesis are added around the macro argument usage, and it
now accounts for trying to iterate through an already-null skb pointer,
which will simply run the iteration zero times. This latter enhancement
means it can be used to replace both do { ... } while and while (...)
open-coded idioms.

This should take care of these three possible usages, which match all
current methods of iterations.

skb_list_walk_safe(segs, skb, next) { ... }
skb_list_walk_safe(skb, skb, next) { ... }
skb_list_walk_safe(segs, skb, segs) { ... }

Gcc appears to generate efficient code for each of these.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Jason A. Donenfeld and committed by
David S. Miller
dcfea72e 542d3065

+5 -8
-8
drivers/net/wireguard/device.h
··· 62 62 int wg_device_init(void); 63 63 void wg_device_uninit(void); 64 64 65 - /* Later after the dust settles, this can be moved into include/linux/skbuff.h, 66 - * where virtually all code that deals with GSO segs can benefit, around ~30 67 - * drivers as of writing. 68 - */ 69 - #define skb_list_walk_safe(first, skb, next) \ 70 - for (skb = first, next = skb->next; skb; \ 71 - skb = next, next = skb ? skb->next : NULL) 72 - 73 65 #endif /* _WG_DEVICE_H */
+5
include/linux/skbuff.h
··· 1478 1478 skb->next = NULL; 1479 1479 } 1480 1480 1481 + /* Iterate through singly-linked GSO fragments of an skb. */ 1482 + #define skb_list_walk_safe(first, skb, next) \ 1483 + for ((skb) = (first), (next) = (skb) ? (skb)->next : NULL; (skb); \ 1484 + (skb) = (next), (next) = (skb) ? (skb)->next : NULL) 1485 + 1481 1486 static inline void skb_list_del_init(struct sk_buff *skb) 1482 1487 { 1483 1488 __list_del_entry(&skb->list);