FAILED: patch "[PATCH] packet: use ubuf_info completion for TX_RING packets" failed to apply to 5.15-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 5.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.15.y
git checkout FETCH_HEAD
git cherry-pick -x 9518405613863d0bf0700927a21367f5942cf058
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026092952-contents-unworn-2736@gregkh' --subject-prefix 'PATCH 5.15.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 9518405613863d0bf0700927a21367f5942cf058 Mon Sep 17 00:00:00 2001
From: Willem de Bruijn <[email protected]>
Date: Fri, 18 Sep 2026 20:47:29 -0400
Subject: [PATCH] packet: use ubuf_info completion for TX_RING packets
tpacket_snd sends skbs with frags pointing into its ring slots. Slots are released when skb->destructor is called.
A call to skb_orphan calls skb->destructor before the skb is freed. This can cause the slot to be reused while still linked into the skb.
Switch to standard zerocopy completion (ubuf_info) so the slot is only released once all references to the payload are freed or copied. Restore skb->destructor to standard sock_wfree.
The ubuf_info completion callback can be called with a NULL skb, but only from net_zcopy_put and related API, used by zerocopy implementations that hold their own reference on the uarg, such as MSG_ZEROCOPY. This uarg is only ever completed from skb_zcopy_clear, so skb is always set.
To prevent userspace from aliasing in-flight state on shared ring slots, allocate tpacket_uarg per packet, rather than per slot. This adds a small allocation to the transmit path. Use standard kmalloc to allow backporting to stable kernels.
The uarg holds an sk_wmem_alloc reference, rather than an sk_refcnt reference. packet_free_tx_ring waits on sk_wmem_alloc before freeing the ring pages. Always allocate vec->deferred for tx_ring so page-backed rings also wait on sk_wmem_alloc when skb_copy_ubufs drops page refs before calling tpacket_ubuf_complete.
Drop the tx_ring.pg_vec test that tpacket_destruct_skb performed before accessing the slot. The sk_wmem_alloc reference now guarantees that the slot is valid. The test is also not sufficient by itself, as it reads pg_vec without pg_vec_lock, so it can race with packet_set_ring.
As a result a slot is released when its payload is copied, which can be before transmission (e.g., in skb_orphan_frags_rx). If copied before skb_tx_timestamp() is called, no slot timestamp is recorded, similar to when skb_orphan() was called early in the datapath before this patch.
Revert the now unused previous skb_zcopy_.._nouarg infra.
Depends on commit 992cc9f94ca9 ("net/packet: defer vmalloc TX_RING free until skbs finish").
include/linux/skbuff.h +1 -18
@@ -1834,22 +1834,6 @@ static inline void skb_zcopy_set(struct sk_buff *skb, struct ubuf_info *uarg, } } -static inline void skb_zcopy_set_nouarg (struct sk_buff *skb,void *val) -{ -skb_shinfo (skb)->destructor_arg = (void *)((uintptr_t ) val |0x1UL ); -skb_shinfo (skb)->flags |=SKBFL_ZEROCOPY_FRAG ; -} - -static inline bool skb_zcopy_is_nouarg (struct sk_buff *skb) -{ -return (uintptr_t )skb_shinfo (skb)->destructor_arg &0x1UL ; -} - -static inline void *skb_zcopy_get_nouarg (struct sk_buff *skb) -{ -return (void *)((uintptr_t )skb_shinfo (skb)->destructor_arg & ~0x1UL ); -} -static inline void net_zcopy_put (struct ubuf_info *uarg) {if (uarg)
@@ -1872,8 +1856,7 @@ static inline void skb_zcopy_clear(struct sk_buff *skb, bool zerocopy_success)struct ubuf_info *uarg =skb_zcopy (skb);if (uarg) { -if (!skb_zcopy_is_nouarg (skb)) - uarg->ops->complete (skb, uarg, zerocopy_success); + uarg->ops->complete (skb, uarg, zerocopy_success);skb_shinfo (skb)->flags &= ~SKBFL_ALL_ZEROCOPY ; }
net/packet/af_packet.c +64 -38
158 lines not on this page · raw message
Sasha Levin
maintainerPATCH 1/2 net/packet: defer vmalloc TX_RING free until skbs finish
From: Kyle Zeng <[email protected]>
[ Upstream commit 992cc9f94ca924089a506ba9b327caa9af797529 ]
AF_PACKET TX_RING skbs keep a raw pointer to their ring frame. The skb page references preserve page-backed ring blocks after pg_vec is freed, but they do not preserve a vmalloc mapping.
tpacket_destruct_skb() currently drops the pending reference before writing the timestamp and TP_STATUS_AVAILABLE to the frame. Move the decrement after those stores. The smp_wmb() in __packet_set_status() orders the frame stores before the decrement.
Also recheck pending TX frames under pg_vec_lock before non-closing ring replacement, so a racing send cannot add a pending skb between the initial check and the ring swap.
Ring allocation can produce a mixture of page-backed and vmalloc-backed blocks. Allocate deferred-work storage during TX ring setup when the first vmalloc-backed block is encountered, and keep its pointer in the pg_vec allocation header. If allocation fails, return -ENOMEM from ring setup. On socket close, a non-NULL pointer identifies a vmalloc-backed vector without a scan. If TX skbs remain, defer the whole vector to system_long_wq.
After pg_vec is detached, a late destructor can skip the pending decrement. Use socket write-memory accounting as the deferred lifetime gate instead: an skb remains charged through its final sock_wfree(), after all ring-frame accesses. The delayed work retains a socket reference and reschedules itself until no TX skbs remain.
Move pending_refcnt release to packet_sock_destruct() so late skb destructors and deferred cleanup can safely use it after packet_release(). Page-backed teardown remains synchronous, and no lock is added to the TX completion hot path.
Backport preparation for 9518405613863 on Linux 5.15:
Keep sk_refcnt_debug_release() in packet_release(), while moving only packet_free_pending() to the final socket destructor. Use struct_size() with kzalloc(), plain container_of(), and an unannotated flexible array for interfaces that this branch provides. Retain the upstream deferred TX-ring cleanup functions without adding any further functions.
Prepare the target's zerocopy completion interface by replacing the ubuf_info callback pointer with an ops pointer and updating every existing producer and consumer, keeping their callback implementations unchanged. Use SKBFL_DONT_ORPHAN for MSG_ZEROCOPY so skb_orphan_frags() no longer depends on the nouarg helper that the target removes; include the flag in SKBFL_ALL_ZEROCOPY when clearing zerocopy state.
Pass the existing socket cookie to skb_setup_tx_timestamp() and update all its callers, preserving timestamp semantics while avoiding a merge conflict adjacent to the target's removal of skb_zcopy_set_nouarg(). Provide the explicit-flags kzalloc_obj() macro and the upstream debug assertion macro used by the target, without importing unrelated newer allocation or socket timestamp machinery.
Stable-dep-of: 951840561386 ("packet: use ubuf_info completion for TX_RING packets")
--- drivers/net/tap.c | 2 +- drivers/net/tun.c | 2 +- drivers/net/xen-netback/common.h | 1 + drivers/net/xen-netback/interface.c | 2 +- drivers/net/xen-netback/netback.c | 6 +- drivers/vhost/net.c | 6 +- include/linux/net.h | 6 ++ include/linux/skbuff.h | 25 ++++--- include/linux/slab.h | 3 + include/net/sock.h | 5 +- net/can/raw.c | 2 +- net/core/skbuff.c | 9 ++- net/ipv4/raw.c | 2 +- net/ipv6/raw.c | 2 +- net/packet/af_packet.c | 103 ++++++++++++++++++++++++---- 15 files changed, 144 insertions(+), 32 deletions(-)
drivers/net/tap.c +1 -1
@@ -733,7 +733,7 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,skb_zcopy_init (skb, msg_control); }else if (msg_control) {struct ubuf_info *uarg = msg_control; - uarg->callback (NULL , uarg, false); + uarg->ops->complete (NULL , uarg, false); }dev_queue_xmit (skb);
drivers/net/tun.c +1 -1
@@ -1909,7 +1909,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,skb_zcopy_init (skb, msg_control); }else if (msg_control) {struct ubuf_info *uarg = msg_control; - uarg->callback (NULL , uarg, false); + uarg->ops->complete (NULL , uarg, false); }skb_reset_network_header (skb);
drivers/net/xen-netback/common.h +1 -0
@@ -393,6 +393,7 @@ void xenvif_carrier_on(struct xenvif *vif);/* Callback from stack when TX packet can be released */ void xenvif_zerocopy_callback (struct sk_buff *skb,struct ubuf_info *ubuf,bool zerocopy_success); +extern const struct ubuf_info_ops xenvif_zerocopy_ops;static inline pending_ring_idx_t nr_pending_reqs (struct xenvif_queue *queue) {
drivers/net/xen-netback/interface.c +1 -1
@@ -588,7 +588,7 @@ int xenvif_init_queue(struct xenvif_queue *queue)for (i =0 ; i <MAX_PENDING_REQS ; i++) { queue->pending_tx_info[i].callback_struct = (struct ubuf_info ) - { .callback = xenvif_zerocopy_callback, + { .ops = &xenvif_zerocopy_ops, { { .ctx =NULL , .desc = i } } }; queue->grant_tx_handle[i] =NETBACK_INVALID_HANDLE ;
drivers/net/xen-netback/netback.c +5 -1
@@ -1167,7 +1167,7 @@ static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *s uarg =skb_shinfo (skb)->destructor_arg;/* increase inflight counter to offset decrement in callback */ atomic_inc (&queue->inflight_packets); - uarg->callback (NULL , uarg, true); + uarg->ops->complete (NULL , uarg, true);skb_shinfo (skb)->destructor_arg =NULL ;/* Fill the skb with the new (local) frags. */
@@ -1291,6 +1291,10 @@ static int xenvif_tx_submit(struct xenvif_queue *queue)return work_done; } +const struct ubuf_info_ops xenvif_zerocopy_ops = { + .complete = xenvif_zerocopy_callback, +}; +void xenvif_zerocopy_callback (struct sk_buff *skb,struct ubuf_info *ubuf,bool zerocopy_success) {
drivers/vhost/net.c +5 -1
@@ -413,6 +413,10 @@ static void vhost_zerocopy_callback(struct sk_buff *skb,rcu_read_unlock_bh (); } +static const struct ubuf_info_ops vhost_zerocopy_ops = { + .complete = vhost_zerocopy_callback, +}; +static inline unsigned long busy_clock (void ) {return local_clock () >>10 ;
@@ -912,7 +916,7 @@ static void handle_tx_zerocopy(struct vhost_net *net, struct socket *sock) ubuf = nvq->ubuf_info + nvq->upend_idx; vq->heads[nvq->upend_idx].id =cpu_to_vhost32 (vq, head); vq->heads[nvq->upend_idx].len =VHOST_DMA_IN_PROGRESS ; - ubuf->callback = vhost_zerocopy_callback; + ubuf->ops = &vhost_zerocopy_ops; ubuf->ctx = nvq->ubufs; ubuf->desc = nvq->upend_idx; ubuf->flags =SKBFL_ZEROCOPY_FRAG ;
include/linux/net.h +6 -0
@@ -242,6 +242,12 @@ struct socket *sock_from_file(struct file *file);#define sockfd_put (sock) fput(sock->file)int net_ratelimit (void ); +#if defined(CONFIG_DEBUG_NET ) +#define DEBUG_NET_WARN_ON_ONCE (cond) ((void)WARN_ON_ONCE(cond)) +#else +#define DEBUG_NET_WARN_ON_ONCE (cond) BUILD_BUG_ON_INVALID(cond) +#endif +#define net_ratelimited_function (function, ...) \ do { \ if (net_ratelimit()) \
include/linux/skbuff.h +17 -8
@@ -493,9 +493,19 @@ enum {* all frags to avoid possible bad checksum */ SKBFL_SHARED_FRAG =BIT (1 ), + + SKBFL_DONT_ORPHAN = BIT(3), };#define SKBFL_ZEROCOPY_FRAG (SKBFL_ZEROCOPY_ENABLE | SKBFL_SHARED_FRAG) +#define SKBFL_ALL_ZEROCOPY (SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN) + +struct ubuf_info ; + +struct ubuf_info_ops { +void (*complete)(struct sk_buff *,struct ubuf_info *, +bool zerocopy_success); +};/* * The callback notifies userspace to release buffers when skb DMA is done in
@@ -506,8 +516,7 @@ enum {* The desc field is used to track userspace buffer index. */ struct ubuf_info { - void (*callback)(struct sk_buff *, struct ubuf_info *, - bool zerocopy_success); + const struct ubuf_info_ops *ops; union { struct { unsigned long desc;
@@ -542,6 +551,7 @@ void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref);void msg_zerocopy_callback (struct sk_buff *skb,struct ubuf_info *uarg,bool success); +extern const struct ubuf_info_ops msg_zerocopy_ubuf_ops;int skb_zerocopy_iter_dgram (struct sk_buff *skb,struct msghdr *msg,int len); int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
@@ -1558,13 +1568,13 @@ static inline void *skb_zcopy_get_nouarg(struct sk_buff *skb)static inline void net_zcopy_put (struct ubuf_info *uarg) {if (uarg) - uarg->callback (NULL , uarg, true); + uarg->ops->complete (NULL , uarg, true); }static inline void net_zcopy_put_abort (struct ubuf_info *uarg,bool have_uref) {if (uarg) { -if (uarg->callback == msg_zerocopy_callback) +if (uarg->ops == &msg_zerocopy_ubuf_ops)msg_zerocopy_put_abort (uarg, have_uref);else if (have_uref)net_zcopy_put (uarg);
@@ -1578,9 +1588,9 @@ static inline void skb_zcopy_clear(struct sk_buff *skb, bool zerocopy_success)if (uarg) {if (!skb_zcopy_is_nouarg (skb)) - uarg->callback (skb, uarg, zerocopy_success); + uarg->ops->complete (skb, uarg, zerocopy_success); -skb_shinfo (skb)->flags &= ~SKBFL_ZEROCOPY_FRAG ; +skb_shinfo (skb)->flags &= ~SKBFL_ALL_ZEROCOPY ; } }
@@ -2949,8 +2959,7 @@ static inline int skb_orphan_frags(struct sk_buff *skb, gfp_t gfp_mask) {if (likely (!skb_zcopy (skb)))return 0 ; -if (!skb_zcopy_is_nouarg (skb) && -skb_uarg (skb)->callback == msg_zerocopy_callback) +if (skb_shinfo (skb)->flags &SKBFL_DONT_ORPHAN )return 0 ;return skb_copy_ubufs (skb, gfp_mask); }
include/linux/slab.h +3 -0
@@ -738,6 +738,9 @@ static inline void *kzalloc(size_t size, gfp_t flags)return kmalloc (size, flags | __GFP_ZERO); } +/* Allocate one zeroed object with explicit GFP flags. */ +#define kzalloc_obj (P , flags) kzalloc(sizeof(P), flags) +/** * kzalloc_node - allocate zeroed memory from a particular memory node. * @size: how many bytes of memory are required.
include/net/sock.h +3 -2
@@ -2766,9 +2766,10 @@ static inline void sock_tx_timestamp(struct sock *sk, __u16 tsflags,_sock_tx_timestamp (sk, tsflags, tx_flags,NULL ); } -static inline void skb_setup_tx_timestamp (struct sk_buff *skb,__u16 tsflags) +static inline void skb_setup_tx_timestamp (struct sk_buff *skb, +const struct sockcm_cookie *sockc) { -_sock_tx_timestamp (skb->sk, tsflags, &skb_shinfo (skb)->tx_flags, +_sock_tx_timestamp (skb->sk, sockc->tsflags, &skb_shinfo (skb)->tx_flags, &skb_shinfo (skb)->tskey); }
net/can/raw.c +1 -1
@@ -847,7 +847,7 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) skb->mark = sk->sk_mark; skb->tstamp = sockc.transmit_time; -skb_setup_tx_timestamp (skb, sockc.tsflags); +skb_setup_tx_timestamp (skb, &sockc); err =can_send (skb, ro->loopback);
net/core/skbuff.c +7 -2
@@ -1192,12 +1192,12 @@ struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size)return NULL ; } - uarg->callback = msg_zerocopy_callback; + uarg->ops = &msg_zerocopy_ubuf_ops; uarg->id = ((u32 )atomic_inc_return (&sk->sk_zckey)) -1 ; uarg->len =1 ; uarg->bytelen = size; uarg->zerocopy =1 ; - uarg->flags =SKBFL_ZEROCOPY_FRAG ; + uarg->flags =SKBFL_ZEROCOPY_FRAG |SKBFL_DONT_ORPHAN ;refcount_set (&uarg->refcnt,1 );sock_hold (sk);
@@ -1334,6 +1334,11 @@ void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, }EXPORT_SYMBOL_GPL (msg_zerocopy_callback); +const struct ubuf_info_ops msg_zerocopy_ubuf_ops = { + .complete = msg_zerocopy_callback, +}; +EXPORT_SYMBOL_GPL (msg_zerocopy_ubuf_ops); +void msg_zerocopy_put_abort (struct ubuf_info *uarg,bool have_uref) {struct sock *sk =skb_from_uarg (uarg)->sk;
net/ipv4/raw.c +1 -1
@@ -360,7 +360,7 @@ static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4, skb->ip_summed =CHECKSUM_NONE ; -skb_setup_tx_timestamp (skb, sockc->tsflags); +skb_setup_tx_timestamp (skb, sockc);if (flags &MSG_CONFIRM )skb_set_dst_pending_confirm (skb,1 );
net/ipv6/raw.c +1 -1
@@ -627,7 +627,7 @@ static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length, skb->ip_summed =CHECKSUM_NONE ; -skb_setup_tx_timestamp (skb, sockc->tsflags); +skb_setup_tx_timestamp (skb, sockc);if (flags &MSG_CONFIRM )skb_set_dst_pending_confirm (skb,1 );
net/packet/af_packet.c +91 -12
206 lines not on this page · raw message
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/2 packet: use ubuf_info completion for TX_RING packets
From: Willem de Bruijn <[email protected]>
[ Upstream commit 9518405613863d0bf0700927a21367f5942cf058 ]
tpacket_snd sends skbs with frags pointing into its ring slots. Slots are released when skb->destructor is called.
A call to skb_orphan calls skb->destructor before the skb is freed. This can cause the slot to be reused while still linked into the skb.
Switch to standard zerocopy completion (ubuf_info) so the slot is only released once all references to the payload are freed or copied. Restore skb->destructor to standard sock_wfree.
The ubuf_info completion callback can be called with a NULL skb, but only from net_zcopy_put and related API, used by zerocopy implementations that hold their own reference on the uarg, such as MSG_ZEROCOPY. This uarg is only ever completed from skb_zcopy_clear, so skb is always set.
To prevent userspace from aliasing in-flight state on shared ring slots, allocate tpacket_uarg per packet, rather than per slot. This adds a small allocation to the transmit path. Use standard kmalloc to allow backporting to stable kernels.
The uarg holds an sk_wmem_alloc reference, rather than an sk_refcnt reference. packet_free_tx_ring waits on sk_wmem_alloc before freeing the ring pages. Always allocate vec->deferred for tx_ring so page-backed rings also wait on sk_wmem_alloc when skb_copy_ubufs drops page refs before calling tpacket_ubuf_complete.
Drop the tx_ring.pg_vec test that tpacket_destruct_skb performed before accessing the slot. The sk_wmem_alloc reference now guarantees that the slot is valid. The test is also not sufficient by itself, as it reads pg_vec without pg_vec_lock, so it can race with packet_set_ring.
As a result a slot is released when its payload is copied, which can be before transmission (e.g., in skb_orphan_frags_rx). If copied before skb_tx_timestamp() is called, no slot timestamp is recorded, similar to when skb_orphan() was called early in the datapath before this patch.
Revert the now unused previous skb_zcopy_.._nouarg infra.
Depends on commit 992cc9f94ca9 ("net/packet: defer vmalloc TX_RING free until skbs finish").
--- include/linux/skbuff.h | 19 +------- net/packet/af_packet.c | 102 ++++++++++++++++++++++++++--------------- 2 files changed, 65 insertions(+), 56 deletions(-)
include/linux/skbuff.h +1 -18
@@ -1549,22 +1549,6 @@ static inline void skb_zcopy_set(struct sk_buff *skb, struct ubuf_info *uarg, } } -static inline void skb_zcopy_set_nouarg (struct sk_buff *skb,void *val) -{ -skb_shinfo (skb)->destructor_arg = (void *)((uintptr_t ) val |0x1UL ); -skb_shinfo (skb)->flags |=SKBFL_ZEROCOPY_FRAG ; -} - -static inline bool skb_zcopy_is_nouarg (struct sk_buff *skb) -{ -return (uintptr_t )skb_shinfo (skb)->destructor_arg &0x1UL ; -} - -static inline void *skb_zcopy_get_nouarg (struct sk_buff *skb) -{ -return (void *)((uintptr_t )skb_shinfo (skb)->destructor_arg & ~0x1UL ); -} -static inline void net_zcopy_put (struct ubuf_info *uarg) {if (uarg)
@@ -1587,8 +1571,7 @@ static inline void skb_zcopy_clear(struct sk_buff *skb, bool zerocopy_success)struct ubuf_info *uarg =skb_zcopy (skb);if (uarg) { -if (!skb_zcopy_is_nouarg (skb)) - uarg->ops->complete (skb, uarg, zerocopy_success); + uarg->ops->complete (skb, uarg, zerocopy_success);skb_shinfo (skb)->flags &= ~SKBFL_ALL_ZEROCOPY ; }
net/packet/af_packet.c +64 -38
158 lines not on this page · raw message
Signature · 1 line
2.53.0