FAILED: patch "[PATCH] packet: use ubuf_info completion for TX_RING packets" failed to apply to 6.1-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.1-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-6.1.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 '2026092951-await-radiator-1fb3@gregkh' --subject-prefix 'PATCH 6.1.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 9518405613863d0bf0700927a21367f5942cf058 on 6.1:
Keep sk_refcnt_debug_release() in packet_release(), but move pending-count storage release to packet_sock_destruct() as upstream requires. Allocate the vector header with kzalloc(struct_size(...)) and omit __counted_by, which this tree does not provide.
The unmodified target also needs interfaces absent from 6.1. Adapt the ubuf_info operations-table conversion from 7ab4f16f9e244 ("net: extend ubuf_info callback to ops structure"), preserving all existing callback implementations and updating every producer and consumer. Change the existing skb_setup_tx_timestamp() helper and all callers to pass the sockcm_cookie, retaining the same timestamp flags and behavior. Supply the two-argument kzalloc_obj() macro used by both dependency and target in terms of the existing kzalloc(). No additional functions beyond those already introduced by this dependency are added.
These preparations preserve the deferred-ring lifetime fix and allow the unmodified target to merge without conflicts on the stable tree.
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 | 2 + drivers/net/xen-netback/interface.c | 2 +- drivers/net/xen-netback/netback.c | 6 +- drivers/vhost/net.c | 6 +- include/linux/skbuff.h | 18 +++-- include/linux/slab.h | 3 + include/net/sock.h | 5 +- io_uring/notif.c | 6 +- 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, 140 insertions(+), 30 deletions(-)
drivers/net/tap.c +1 -1
@@ -747,7 +747,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
@@ -1913,7 +1913,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 +2 -0
@@ -391,6 +391,8 @@ bool xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb);void xenvif_carrier_on (struct xenvif *vif);/* Callback from stack when TX packet can be released */ +extern const struct ubuf_info_ops xenvif_ubuf_ops; +void xenvif_zerocopy_callback (struct sk_buff *skb,struct ubuf_info *ubuf,bool zerocopy_success);
drivers/net/xen-netback/interface.c +1 -1
@@ -590,7 +590,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_msgzc ) - { { .callback = xenvif_zerocopy_callback }, + { { .ops = &xenvif_ubuf_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. */
@@ -1323,6 +1323,10 @@ void xenvif_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *ubuf_base,xenvif_skb_zerocopy_complete (queue); } +const struct ubuf_info_ops xenvif_ubuf_ops = { + .complete = xenvif_zerocopy_callback, +}; +static inline void xenvif_tx_dealloc_action (struct xenvif_queue *queue) {struct gnttab_unmap_grant_ref *gop;
drivers/vhost/net.c +5 -1
@@ -421,6 +421,10 @@ static void vhost_zerocopy_callback(struct sk_buff *skb,rcu_read_unlock_bh (); } +static const struct ubuf_info_ops vhost_ubuf_ops = { + .complete = vhost_zerocopy_callback, +}; +static inline unsigned long busy_clock (void ) {return local_clock () >>10 ;
@@ -922,7 +926,7 @@ static void handle_tx_zerocopy(struct vhost_net *net, struct socket *sock) vq->heads[nvq->upend_idx].len =VHOST_DMA_IN_PROGRESS ; ubuf->ctx = nvq->ubufs; ubuf->desc = nvq->upend_idx; - ubuf->ubuf.callback = vhost_zerocopy_callback; + ubuf->ubuf.ops = &vhost_ubuf_ops; ubuf->ubuf.flags =SKBFL_ZEROCOPY_FRAG ;refcount_set (&ubuf->ubuf.refcnt,1 ); msg.msg_control = &ctl;
include/linux/skbuff.h +13 -5
@@ -531,6 +531,13 @@ enum {#define SKBFL_ALL_ZEROCOPY (SKBFL_ZEROCOPY_FRAG | SKBFL_PURE_ZEROCOPY | \ SKBFL_DONT_ORPHAN | SKBFL_MANAGED_FRAG_REFS) +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 * lower device, the skb last reference should be 0 when calling this.
@@ -540,8 +547,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;refcount_t refcnt;u8 flags; };
@@ -1652,6 +1658,8 @@ static inline void skb_set_end_offset(struct sk_buff *skb, unsigned int offset) }#endif +extern const struct ubuf_info_ops msg_zerocopy_ubuf_ops; +struct ubuf_info *msg_zerocopy_realloc (struct sock *sk,size_t size,struct ubuf_info *uarg);
@@ -1747,13 +1755,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);
@@ -1767,7 +1775,7 @@ 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_ALL_ZEROCOPY ; }
include/linux/slab.h +3 -0
@@ -699,6 +699,9 @@ static inline __alloc_size(1) void *kzalloc(size_t size, gfp_t flags)return kmalloc (size, flags | __GFP_ZERO); } +/* Allocate a zeroed object using its variable or type. */ +#define kzalloc_obj (obj, flags) kzalloc(sizeof(obj), (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
@@ -2879,9 +2879,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); }
io_uring/notif.c +5 -1
@@ -45,6 +45,10 @@ static void io_uring_tx_zerocopy_callback(struct sk_buff *skb, } } +static const struct ubuf_info_ops io_ubuf_ops = { + .complete = io_uring_tx_zerocopy_callback, +}; +struct io_kiocb *io_alloc_notif (struct io_ring_ctx *ctx) __must_hold(&ctx->uring_lock) {
@@ -65,7 +69,7 @@ struct io_kiocb *io_alloc_notif(struct io_ring_ctx *ctx) nd =io_notif_to_data (notif); nd->account_pages =0 ; nd->uarg.flags =SKBFL_ZEROCOPY_FRAG |SKBFL_DONT_ORPHAN ; - nd->uarg.callback = io_uring_tx_zerocopy_callback; + nd->uarg.ops = &io_ubuf_ops; nd->zc_report = nd->zc_used = nd->zc_copied = false;refcount_set (&nd->uarg.refcnt,1 );return notif;
net/can/raw.c +1 -1
@@ -892,7 +892,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
@@ -1309,7 +1309,7 @@ static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size)return NULL ; } - uarg->ubuf.callback = msg_zerocopy_callback; + uarg->ubuf.ops = &msg_zerocopy_ubuf_ops; uarg->id = ((u32 )atomic_inc_return (&sk->sk_zckey)) -1 ; uarg->len =1 ; uarg->bytelen = size;
@@ -1335,7 +1335,7 @@ struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size,u32 bytelen, next;/* there might be non MSG_ZEROCOPY users */ -if (uarg->callback != msg_zerocopy_callback) +if (uarg->ops != &msg_zerocopy_ubuf_ops)return NULL ;/* realloc only when socket is locked (TCP, UDP cork),
@@ -1458,6 +1458,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_to_msgzc (uarg))->sk;
net/ipv4/raw.c +1 -1
@@ -361,7 +361,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
@@ -622,7 +622,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
@@ -1736,22 +1736,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)
@@ -1774,8 +1758,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