* [PATCH net v9 1/6] xdp: run generic XDP on frags with a copy of the rxq info
2026-11-27 10:01 [PATCH net v9 0/6] xdp: fix page_pool leaks Slawomir Stepien
@ 2026-11-27 10:01 ` Slawomir Stepien
2026-11-27 10:01 ` [PATCH net v9 2/6] bpf: test_run: Set frag_size on " Slawomir Stepien
` (4 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Slawomir Stepien @ 2026-11-27 10:01 UTC (permalink / raw)
To: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Alexei Starovoitov, Daniel Borkmann, Jesper Dangaard Brouer,
John Fastabend, Stanislav Fomichev
Cc: bpf, netdev, Jiayuan Chen, Dudu Lu, Maciej Fijalkowski,
Tianyi Gao, Octavian Purdila, Toke Høiland-Jørgensen,
Alexander Lobakin, Lorenzo Bianconi, Lorenzo Bianconi,
Ihor Solodrai, Andrew Lunn, Willem de Bruijn, Simon Horman,
Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
Yonghong Song, Jiri Olsa, Kumar Kartikeya Dwivedi,
Emil Tsalapatis, Shuah Khan, linux-kselftest, Slawomir Stepien,
syzbot+ff145014d6b0ce64a173, syzbot+237bbeed8dfe0699b7f5, stable
Since commit e6d5dbdd20aa ("xdp: add multi-buff support for xdp running
in generic mode") generic XDP copies a non-linear skb into page_pool
memory with skb_pp_cow_data() and gives the program its frags. The
program still runs with the rxq info of the device queue,
dev->_rx[i].xdp_rxq, which never has a memory model, so its mem type is
MEM_TYPE_PAGE_SHARED. When the program shrinks a frag away with
bpf_xdp_adjust_tail(), bpf_xdp_shrink_data() frees the page_pool page
with page_frag_free():
BUG: Bad page state in process ping pfn:1f3ae
page dumped because: page_pool leak
and a later free of the same page can hit VM_BUG_ON_PAGE() in
page_frag_free().
The mem type cannot be set in dev->_rx[i].xdp_rxq: it is shared by all
CPUs that receive on that queue and by BPF_PROG_TEST_RUN, which borrows
the rxq info of lo's queue 0. For a non-linear skb, run the program with
a copy of the rxq info on the stack that says MEM_TYPE_PAGE_POOL. The
frags of such an skb always come from skb_pp_cow_data(): when the copy
is not possible, the skb is linearized instead. The program cannot free
the head, so its memory does not matter here. The copy goes out of scope
when the function returns, so point xdp->rxq back to the device rxq info
after the program ran.
Fixes: e6d5dbdd20aa ("xdp: add multi-buff support for xdp running in generic mode")
Reported-by: syzbot+ff145014d6b0ce64a173@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=ff145014d6b0ce64a173
Reported-by: syzbot+237bbeed8dfe0699b7f5@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=237bbeed8dfe0699b7f5
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/all/DN3QVHST3PUQ.3NINFUW0FL9IW@gmail.com/
Cc: <stable@vger.kernel.org>
Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
net/core/dev.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/net/core/dev.c b/net/core/dev.c
index cd0796a1dbfd..8f2e8c90951b 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -5686,6 +5686,7 @@ u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp,
void *orig_data, *orig_data_end, *hard_start;
struct netdev_rx_queue *rxqueue;
bool orig_bcast, orig_host;
+ struct xdp_rxq_info rxq;
u32 mac_len, frame_sz;
__be16 orig_eth_type;
struct ethhdr *eth;
@@ -5709,6 +5710,13 @@ u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp,
if (skb_is_nonlinear(skb)) {
skb_shinfo(skb)->xdp_frags_size = skb->data_len;
xdp_buff_set_frags_flag(xdp);
+ /*
+ * The frags came from skb_pp_cow_data(). xdp_rxq is shared
+ * with other CPUs and with BPF_PROG_TEST_RUN, so use a copy.
+ */
+ rxq = rxqueue->xdp_rxq;
+ rxq.mem.type = MEM_TYPE_PAGE_POOL;
+ xdp->rxq = &rxq;
} else {
xdp_buff_clear_frags_flag(xdp);
}
@@ -5721,6 +5729,7 @@ u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp,
orig_eth_type = eth->h_proto;
act = bpf_prog_run_xdp(xdp_prog, xdp);
+ xdp->rxq = &rxqueue->xdp_rxq;
/* check if bpf_xdp_adjust_head was used */
off = xdp->data - orig_data;
base-commit: e9e45fcb6d39bee9325e6a7dca72bd14e82760f3
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH net v9 2/6] bpf: test_run: Set frag_size on a copy of the rxq info
2026-11-27 10:01 [PATCH net v9 0/6] xdp: fix page_pool leaks Slawomir Stepien
2026-11-27 10:01 ` [PATCH net v9 1/6] xdp: run generic XDP on frags with a copy of the rxq info Slawomir Stepien
@ 2026-11-27 10:01 ` Slawomir Stepien
2026-11-27 10:01 ` [PATCH net v9 3/6] veth: use the frame mem type when running XDP on an xdp_frame Slawomir Stepien
` (3 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Slawomir Stepien @ 2026-11-27 10:01 UTC (permalink / raw)
To: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Alexei Starovoitov, Daniel Borkmann, Jesper Dangaard Brouer,
John Fastabend, Stanislav Fomichev
Cc: bpf, netdev, Jiayuan Chen, Dudu Lu, Maciej Fijalkowski,
Tianyi Gao, Octavian Purdila, Toke Høiland-Jørgensen,
Alexander Lobakin, Lorenzo Bianconi, Lorenzo Bianconi,
Ihor Solodrai, Andrew Lunn, Willem de Bruijn, Simon Horman,
Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
Yonghong Song, Jiri Olsa, Kumar Kartikeya Dwivedi,
Emil Tsalapatis, Shuah Khan, linux-kselftest, Slawomir Stepien,
stable
bpf_prog_test_run_xdp() runs the program with the rxq info of lo's
queue 0 and stores frag_size = PAGE_SIZE into it, because the frags it
allocates are full pages. That rxq info is shared with generic XDP on
lo, whose frags come from skb_pp_cow_data() and can be a part of a
page_pool page. After a test run, a generic XDP program on lo can grow
such a frag with bpf_xdp_adjust_tail() up to the end of the page, over
the data of the next frag in that page.
The store also comes too early when the test run picks the rxq info of
another device with ingress_ifindex: xdp_convert_md_to_buff() replaces
xdp->rxq after it, the frag_size of that rxq info is 0, and growing a
frag of the test run fails with -EOPNOTSUPP.
Keep lo's rxq info as it is. Once xdp_convert_md_to_buff() has chosen
the rxq info, run the program with a copy of it on the stack that has
frag_size = PAGE_SIZE.
Fixes: 1c1949982524 ("bpf: introduce frags support to bpf_prog_test_run_xdp()")
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/all/sashiko-outbox-223211@kernel.org/
Link: https://lore.kernel.org/all/DN3QVHST3PUQ.3NINFUW0FL9IW@gmail.com/
Cc: <stable@vger.kernel.org>
Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
net/bpf/test_run.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
index d6c9dc62182e..1d89b6f71a48 100644
--- a/net/bpf/test_run.c
+++ b/net/bpf/test_run.c
@@ -1349,6 +1349,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
struct netdev_rx_queue *rxqueue;
struct skb_shared_info *sinfo;
struct xdp_buff xdp = {};
+ struct xdp_rxq_info rxq;
int i, ret = -EINVAL;
struct xdp_md *ctx;
void *data;
@@ -1417,8 +1418,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
}
rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
- rxqueue->xdp_rxq.frag_size = PAGE_SIZE;
- xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq);
+ xdp_init_buff(&xdp, PAGE_SIZE, &rxqueue->xdp_rxq);
xdp_prepare_buff(&xdp, data, headroom, linear_sz, true);
sinfo = xdp_get_shared_info_from_buff(&xdp);
@@ -1426,6 +1426,11 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
if (ret)
goto free_data;
+ /* generic XDP uses the same rxq and its frags can be smaller */
+ rxq = *xdp.rxq;
+ rxq.frag_size = PAGE_SIZE;
+ xdp.rxq = &rxq;
+
size = linear_sz;
if (unlikely(kattr->test.data_size_in > size)) {
void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH net v9 3/6] veth: use the frame mem type when running XDP on an xdp_frame
2026-11-27 10:01 [PATCH net v9 0/6] xdp: fix page_pool leaks Slawomir Stepien
2026-11-27 10:01 ` [PATCH net v9 1/6] xdp: run generic XDP on frags with a copy of the rxq info Slawomir Stepien
2026-11-27 10:01 ` [PATCH net v9 2/6] bpf: test_run: Set frag_size on " Slawomir Stepien
@ 2026-11-27 10:01 ` Slawomir Stepien
2026-11-27 10:01 ` [PATCH net v9 4/6] veth: use the page_pool mem type when running XDP on an skb Slawomir Stepien
` (2 subsequent siblings)
5 siblings, 0 replies; 9+ messages in thread
From: Slawomir Stepien @ 2026-11-27 10:01 UTC (permalink / raw)
To: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Alexei Starovoitov, Daniel Borkmann, Jesper Dangaard Brouer,
John Fastabend, Stanislav Fomichev
Cc: bpf, netdev, Jiayuan Chen, Dudu Lu, Maciej Fijalkowski,
Tianyi Gao, Octavian Purdila, Toke Høiland-Jørgensen,
Alexander Lobakin, Lorenzo Bianconi, Lorenzo Bianconi,
Ihor Solodrai, Andrew Lunn, Willem de Bruijn, Simon Horman,
Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
Yonghong Song, Jiri Olsa, Kumar Kartikeya Dwivedi,
Emil Tsalapatis, Shuah Khan, linux-kselftest, Slawomir Stepien,
stable
veth_xdp_rcv_one() runs the program on an xdp_frame with rq->xdp_rxq,
whose mem type is MEM_TYPE_PAGE_SHARED, and copies the frame's mem type
into it only for XDP_TX and XDP_REDIRECT, after the program ran. That is
too late: the program itself can free frags, and bpf_xdp_shrink_data()
frees them according to xdp->rxq->mem.type. When the program shrinks a
frag of a page_pool frame away, bpf_xdp_shrink_data() frees the
page_pool page with page_frag_free(). The mem type stored for XDP_TX and
XDP_REDIRECT also stays in rq->xdp_rxq, so later packets on the queue,
skbs and frames, run with the mem type of that frame.
Do not store into rq->xdp_rxq. Run the program with an rxq info on the
stack of veth_xdp_rcv(), and set its mem type to the frame's one before
the program runs. The queue index is the one veth_enable_xdp_range()
registers rq->xdp_rxq with.
Fixes: 718a18a0c8a6 ("veth: Rework veth_xdp_rcv_skb in order to accept non-linear skb")
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/all/DN3QVHST3PUQ.3NINFUW0FL9IW@gmail.com/
Cc: <stable@vger.kernel.org>
Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
drivers/net/veth.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/drivers/net/veth.c b/drivers/net/veth.c
index faa837acea7b..cce253b58c2b 100644
--- a/drivers/net/veth.c
+++ b/drivers/net/veth.c
@@ -642,6 +642,7 @@ static int veth_xdp_tx(struct veth_rq *rq, struct xdp_buff *xdp,
}
static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
+ struct xdp_rxq_info *rxq,
struct xdp_frame *frame,
struct veth_xdp_tx_bq *bq,
struct veth_stats *stats)
@@ -657,7 +658,8 @@ static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
u32 act;
xdp_convert_frame_to_buff(frame, xdp);
- xdp->rxq = &rq->xdp_rxq;
+ rxq->mem.type = frame->mem_type;
+ xdp->rxq = rxq;
vxbuf.skb = NULL;
act = bpf_prog_run_xdp(xdp_prog, xdp);
@@ -669,7 +671,6 @@ static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
break;
case XDP_TX:
orig_frame = *frame;
- xdp->rxq->mem.type = frame->mem_type;
if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
trace_xdp_exception(rq->dev, xdp_prog, act);
frame = &orig_frame;
@@ -681,7 +682,6 @@ static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
goto xdp_xmit;
case XDP_REDIRECT:
orig_frame = *frame;
- xdp->rxq->mem.type = frame->mem_type;
if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
frame = &orig_frame;
stats->rx_drops++;
@@ -914,6 +914,11 @@ static int veth_xdp_rcv(struct veth_rq *rq, int budget,
struct veth_xdp_tx_bq *bq,
struct veth_stats *stats)
{
+ struct veth_priv *priv = netdev_priv(rq->dev);
+ struct xdp_rxq_info rxq = {
+ .dev = rq->dev,
+ .queue_index = rq - priv->rq,
+ };
int i, done = 0, n_xdpf = 0;
void *xdpf[VETH_XDP_BATCH];
@@ -928,7 +933,7 @@ static int veth_xdp_rcv(struct veth_rq *rq, int budget,
struct xdp_frame *frame = veth_ptr_to_xdp(ptr);
stats->xdp_bytes += xdp_get_frame_len(frame);
- frame = veth_xdp_rcv_one(rq, frame, bq, stats);
+ frame = veth_xdp_rcv_one(rq, &rxq, frame, bq, stats);
if (frame) {
/* XDP_PASS */
xdpf[n_xdpf++] = frame;
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* [PATCH net v9 4/6] veth: use the page_pool mem type when running XDP on an skb
2026-11-27 10:01 [PATCH net v9 0/6] xdp: fix page_pool leaks Slawomir Stepien
` (2 preceding siblings ...)
2026-11-27 10:01 ` [PATCH net v9 3/6] veth: use the frame mem type when running XDP on an xdp_frame Slawomir Stepien
@ 2026-11-27 10:01 ` Slawomir Stepien
2026-11-28 10:03 ` netdev-bot+sashiko
2026-11-27 10:01 ` [PATCH net v9 5/6] selftests/bpf: add xdp_shrink_frags Slawomir Stepien
2026-11-27 10:01 ` [PATCH net v9 6/6] selftests/bpf: Grow test_run and generic XDP frags after a test run Slawomir Stepien
5 siblings, 1 reply; 9+ messages in thread
From: Slawomir Stepien @ 2026-11-27 10:01 UTC (permalink / raw)
To: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Alexei Starovoitov, Daniel Borkmann, Jesper Dangaard Brouer,
John Fastabend, Stanislav Fomichev
Cc: bpf, netdev, Jiayuan Chen, Dudu Lu, Maciej Fijalkowski,
Tianyi Gao, Octavian Purdila, Toke Høiland-Jørgensen,
Alexander Lobakin, Lorenzo Bianconi, Lorenzo Bianconi,
Ihor Solodrai, Andrew Lunn, Willem de Bruijn, Simon Horman,
Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
Yonghong Song, Jiri Olsa, Kumar Kartikeya Dwivedi,
Emil Tsalapatis, Shuah Khan, linux-kselftest, Slawomir Stepien,
stable
Since commit 0ebab78cbcbf ("net: veth: add page_pool for page
recycling") veth_convert_skb_to_xdp_buff() copies the skb into pages
from rq->page_pool when the program cannot run on the skb as it is, for
example when the skb is non-linear or has too little headroom. The
program still runs with rq->xdp_rxq, whose mem type is
MEM_TYPE_PAGE_SHARED, so when it shrinks a frag away,
bpf_xdp_shrink_data() frees the page_pool page with page_frag_free():
BUG: Bad page state in process ping pfn:6aa02
page dumped because: page_pool leak
Run the program with the rxq info on the stack of veth_xdp_rcv() and
set its mem type to MEM_TYPE_PAGE_POOL. The program can free only frags
and they always come from skb_pp_cow_data(). On XDP_TX and
XDP_REDIRECT, veth_xdp_get() takes plain page references and the skb is
consumed, so the buff holds plain page references from then on: set the
mem type to MEM_TYPE_PAGE_SHARED there, which the frame keeps, as it
does today. rq->xdp_rxq is not changed any more, so rq->xdp_mem, which
restored it, can go.
Fixes: 0ebab78cbcbf ("net: veth: add page_pool for page recycling")
Reported-by: Alexei Starovoitov <ast@kernel.org>
Closes: https://lore.kernel.org/bpf/CAADnVQ+bBofJDfieyOYzSmSujSfJwDTQhiz3aJw7hE+4E2_iPA@mail.gmail.com/
Suggested-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/all/DN3QVHST3PUQ.3NINFUW0FL9IW@gmail.com/
Cc: <stable@vger.kernel.org>
Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
drivers/net/veth.c | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)
diff --git a/drivers/net/veth.c b/drivers/net/veth.c
index cce253b58c2b..8bb015c13376 100644
--- a/drivers/net/veth.c
+++ b/drivers/net/veth.c
@@ -63,7 +63,6 @@ struct veth_rq {
struct napi_struct __rcu *napi; /* points to xdp_napi when the latter is initialized */
struct net_device *dev;
struct bpf_prog __rcu *xdp_prog;
- struct xdp_mem_info xdp_mem;
struct veth_rq_stats stats;
bool rx_notify_masked;
struct ptr_ring xdp_ring;
@@ -746,6 +745,9 @@ static void veth_xdp_get(struct xdp_buff *xdp)
struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
int i;
+ /* the buff holds plain page references from here on */
+ xdp->rxq->mem.type = MEM_TYPE_PAGE_SHARED;
+
get_page(virt_to_page(xdp->data));
if (likely(!xdp_buff_has_frags(xdp)))
return;
@@ -755,6 +757,7 @@ static void veth_xdp_get(struct xdp_buff *xdp)
}
static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,
+ struct xdp_rxq_info *rxq,
struct xdp_buff *xdp,
struct sk_buff **pskb)
{
@@ -773,7 +776,9 @@ static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,
/* SKB "head" area always have tailroom for skb_shared_info */
frame_sz = skb_end_pointer(skb) - skb->head;
frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
- xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
+ /* the prog can free only frags and they came from skb_pp_cow_data() */
+ rxq->mem.type = MEM_TYPE_PAGE_POOL;
+ xdp_init_buff(xdp, frame_sz, rxq);
xdp_prepare_buff(xdp, skb->head, skb_headroom(skb),
skb_headlen(skb), true);
@@ -794,6 +799,7 @@ static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,
}
static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
+ struct xdp_rxq_info *rxq,
struct sk_buff *skb,
struct veth_xdp_tx_bq *bq,
struct veth_stats *stats)
@@ -815,7 +821,7 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
}
__skb_push(skb, skb->data - skb_mac_header(skb));
- if (veth_convert_skb_to_xdp_buff(rq, xdp, &skb))
+ if (veth_convert_skb_to_xdp_buff(rq, rxq, xdp, &skb))
goto drop;
vxbuf.skb = skb;
@@ -830,7 +836,6 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
case XDP_TX:
veth_xdp_get(xdp);
consume_skb(skb);
- xdp->rxq->mem = rq->xdp_mem;
if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
trace_xdp_exception(rq->dev, xdp_prog, act);
stats->rx_drops++;
@@ -842,7 +847,6 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
case XDP_REDIRECT:
veth_xdp_get(xdp);
consume_skb(skb);
- xdp->rxq->mem = rq->xdp_mem;
if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
stats->rx_drops++;
goto err_xdp;
@@ -948,7 +952,7 @@ static int veth_xdp_rcv(struct veth_rq *rq, int budget,
struct sk_buff *skb = ptr;
stats->xdp_bytes += skb->len;
- skb = veth_xdp_rcv_skb(rq, skb, bq, stats);
+ skb = veth_xdp_rcv_skb(rq, &rxq, skb, bq, stats);
if (skb) {
if (skb_shared(skb) || skb_unclone(skb, GFP_ATOMIC))
netif_receive_skb(skb);
@@ -1154,9 +1158,6 @@ static int veth_enable_xdp_range(struct net_device *dev, int start, int end,
NULL);
if (err < 0)
goto err_reg_mem;
-
- /* Save original mem info as it can be overwritten */
- rq->xdp_mem = rq->xdp_rxq.mem;
}
return 0;
@@ -1185,7 +1186,6 @@ static void veth_disable_xdp_range(struct net_device *dev, int start, int end,
for (i = start; i < end; i++) {
struct veth_rq *rq = &priv->rq[i];
- rq->xdp_rxq.mem = rq->xdp_mem;
xdp_rxq_info_unreg(&rq->xdp_rxq);
if (delete_napi)
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH net v9 4/6] veth: use the page_pool mem type when running XDP on an skb
2026-11-27 10:01 ` [PATCH net v9 4/6] veth: use the page_pool mem type when running XDP on an skb Slawomir Stepien
@ 2026-11-28 10:03 ` netdev-bot+sashiko
0 siblings, 0 replies; 9+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28 10:03 UTC (permalink / raw)
To: sst
Cc: davem, edumazet, kuba, pabeni, ast, daniel, hawk, john.fastabend,
sdf, bpf, netdev, jiayuan.chen, phx0fer, maciej.fijalkowski,
tianyi, tavip, toke, aleksander.lobakin, lorenzo,
lorenzo.bianconi, ihor.solodrai, andrew+netdev, willemb, horms,
andrii, martin.lau, eddyz87, song, yonghong.song, jolsa, memxor,
emil, shuah, linux-kselftest
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] In drivers/net/veth.c, veth_convert_skb_to_xdp_buff() now
always sets rxq->mem.type = MEM_TYPE_PAGE_POOL.
--
veth: use the page_pool mem type when running XDP on an skb
veth now runs the XDP program on an skb with an on-stack rxq info whose
mem type is MEM_TYPE_PAGE_POOL. Frags that the program shrinks away go
back to the page_pool they came from. On XDP_TX and XDP_REDIRECT,
veth_xdp_get() switches the mem type back to MEM_TYPE_PAGE_SHARED, and
the saved rq->xdp_mem is removed.
Link: https://lore.kernel.org/all/DN3QVHST3PUQ.3NINFUW0FL9IW@gmail.com/
> diff --git a/drivers/net/veth.c b/drivers/net/veth.c
> index cce253b58c2bc..8bb015c133760 100644
> --- a/drivers/net/veth.c
> +++ b/drivers/net/veth.c
[ ... ]
> @@ -773,7 +776,9 @@ static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,
> /* SKB "head" area always have tailroom for skb_shared_info */
> frame_sz = skb_end_pointer(skb) - skb->head;
> frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
> - xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
> + /* the prog can free only frags and they came from skb_pp_cow_data() */
> + rxq->mem.type = MEM_TYPE_PAGE_POOL;
> + xdp_init_buff(xdp, frame_sz, rxq);
[Severity: Medium]
Is the invariant in this comment guaranteed by the code?
Earlier in veth_convert_skb_to_xdp_buff(), the copy into page_pool
memory depends on skb_is_nonlinear(), which tests skb->data_len:
if (skb_shared(skb) || skb_head_is_locked(skb) ||
skb_is_nonlinear(skb) ||
skb_headroom(skb) < XDP_PACKET_HEADROOM) {
if (skb_pp_cow_data(rq->page_pool, pskb, XDP_PACKET_HEADROOM))
goto drop;
The frags flag set just below depends on nr_frags instead:
if (skb_shinfo(skb)->nr_frags) {
skb_shinfo(skb)->xdp_frags_size = skb->data_len;
xdp_buff_set_frags_flag(xdp);
Take an skb that is unshared and unlocked, has enough headroom, and has
nr_frags > 0 but data_len == 0 (all frags are zero-length). It skips
skb_pp_cow_data() and keeps its original frag pages, which are not
page_pool pages. It still gets XDP_FLAGS_HAS_FRAGS and runs with
MEM_TYPE_PAGE_POOL.
Suppose a frags-aware program then calls bpf_xdp_adjust_tail() with a
negative offset. For each zero-size frag, bpf_xdp_frags_shrink_tail()
computes shrink = 0, and bpf_xdp_shrink_data() treats the frag as fully
consumed:
bpf_xdp_adjust_tail()
bpf_xdp_frags_shrink_tail()
bpf_xdp_shrink_data() /* release = (size == shrink) */
__xdp_return(netmem, MEM_TYPE_PAGE_POOL, ...)
page_pool_put_full_netmem(netmem_get_pp(netmem), ...)
For MEM_TYPE_PAGE_POOL, __xdp_return() does not check netmem_is_pp().
Wouldn't netmem_get_pp() then read a pp pointer from a page that was
never part of a page_pool? Before this patch, the same frag went through
page_frag_free() under MEM_TYPE_PAGE_SHARED, which releases it correctly.
I could not find a concrete source of such an skb reaching veth_xmit(),
and the usual skb helpers avoid leaving zero-size frags behind. However,
nothing in veth rejects that state.
The sibling change "xdp: run generic XDP on frags with a copy of the rxq
info" uses skb_is_nonlinear() for the copy decision, the frags flag and
MEM_TYPE_PAGE_POOL. Could the frags flag here also be set from
skb_is_nonlinear()? Alternatively, MEM_TYPE_PAGE_POOL could be used only
when skb_pp_cow_data() actually ran, keeping MEM_TYPE_PAGE_SHARED
otherwise.
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127100132.1977890-1-sst%40poczta.fm
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH net v9 5/6] selftests/bpf: add xdp_shrink_frags
2026-11-27 10:01 [PATCH net v9 0/6] xdp: fix page_pool leaks Slawomir Stepien
` (3 preceding siblings ...)
2026-11-27 10:01 ` [PATCH net v9 4/6] veth: use the page_pool mem type when running XDP on an skb Slawomir Stepien
@ 2026-11-27 10:01 ` Slawomir Stepien
2026-11-28 10:04 ` netdev-bot+sashiko
2026-11-27 10:01 ` [PATCH net v9 6/6] selftests/bpf: Grow test_run and generic XDP frags after a test run Slawomir Stepien
5 siblings, 1 reply; 9+ messages in thread
From: Slawomir Stepien @ 2026-11-27 10:01 UTC (permalink / raw)
To: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Alexei Starovoitov, Daniel Borkmann, Jesper Dangaard Brouer,
John Fastabend, Stanislav Fomichev
Cc: bpf, netdev, Jiayuan Chen, Dudu Lu, Maciej Fijalkowski,
Tianyi Gao, Octavian Purdila, Toke Høiland-Jørgensen,
Alexander Lobakin, Lorenzo Bianconi, Lorenzo Bianconi,
Ihor Solodrai, Andrew Lunn, Willem de Bruijn, Simon Horman,
Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
Yonghong Song, Jiri Olsa, Kumar Kartikeya Dwivedi,
Emil Tsalapatis, Shuah Khan, linux-kselftest, Slawomir Stepien
From: Jiayuan Chen <jiayuan.chen@linux.dev>
Add a test that attaches an xdp.frags program which shrinks a whole frag
away, so bpf_xdp_shrink_data() frees a page_pool frag.
test_tun triggers the page-type mismatch on the generic XDP path.
test_veth triggers the same mismatch on the veth path.
test_veth_tx bounces the cow'd buff with XDP_TX so the peer runs a frags
program on the resulting frame, covering the buff -> frame -> buff path.
A kernel without the fix will report "Bad page state ... page_pool leak".
The packet sizes only leave a frag after the cow on 4K pages, so the
subtests skip elsewhere.
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
[sst: reworded two comments for the on-stack rxq info]
Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
.../bpf/prog_tests/xdp_shrink_frags.c | 288 ++++++++++++++++++
.../selftests/bpf/progs/xdp_shrink_frags.c | 34 +++
2 files changed, 322 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_shrink_frags.c
create mode 100644 tools/testing/selftests/bpf/progs/xdp_shrink_frags.c
diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_shrink_frags.c b/tools/testing/selftests/bpf/prog_tests/xdp_shrink_frags.c
new file mode 100644
index 000000000000..789dfdd51b57
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/xdp_shrink_frags.c
@@ -0,0 +1,288 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <network_helpers.h>
+#include <linux/if_tun.h>
+#include <linux/if_ether.h>
+#include <sys/uio.h>
+#include <net/if.h>
+#include <arpa/inet.h>
+#include "xdp_shrink_frags.skel.h"
+
+/*
+ * A generic-XDP program that shrinks into the frags frees a page_pool frag.
+ * skb-backed XDP first cow's the nonlinear skb into page_pool memory
+ * (skb_cow_data_for_xdp() for generic XDP, skb_pp_cow_data() for veth), but
+ * the shared rxq is registered as MEM_TYPE_PAGE_SHARED, so a buggy kernel
+ * frees the frag with page_frag_free() -> "Bad page state ... page_pool leak".
+ */
+
+#define TAP_NAME "xdp_shrink0"
+#define TAP_NETNS "xdp_shrink_tap"
+
+#define NS_NAME_MAX_LEN 32
+#define VETH_LOCAL "xdp_shrinkA"
+#define VETH_PEER "xdp_shrinkB"
+#define VETH_LOCAL_IP "10.9.9.1"
+#define VETH_PEER_IP "10.9.9.2"
+#define VETH_LOCAL_MAC "02:00:00:00:00:01"
+
+/*
+ * skb_pp_cow_data() keeps up to one page in the linear part, so the packet
+ * sizes below only leave a frag (smaller than the 3000-byte shrink, so it is
+ * released as a whole) on 4K pages. Skip elsewhere rather than run a test
+ * that cannot tell a fixed kernel from a buggy one.
+ */
+#define PAGE_SIZE_4K 4096
+
+/*
+ * Generous, so a loaded CI does not fail the assert prematurely; normally
+ * the first check already succeeds.
+ */
+#define WAIT_ITERS 10000
+#define WAIT_US 1000
+
+static void wait_for_prog(struct xdp_shrink_frags *skel)
+{
+ int i;
+
+ for (i = 0; i < WAIT_ITERS && !skel->bss->shrink_ran; i++)
+ usleep(WAIT_US);
+}
+
+static int create_tap_napi_frags(const char *ifname)
+{
+ struct ifreq ifr = {
+ .ifr_flags = IFF_TAP | IFF_NO_PI | IFF_NAPI | IFF_NAPI_FRAGS,
+ };
+ int fd, err;
+
+ strscpy(ifr.ifr_name, ifname);
+
+ fd = open("/dev/net/tun", O_RDWR);
+ if (fd < 0)
+ return -errno;
+
+ err = ioctl(fd, TUNSETIFF, &ifr);
+ if (err) {
+ err = -errno;
+ close(fd);
+ return err;
+ }
+
+ return fd;
+}
+
+/*
+ * Similar to flow_dissector.c: writev() an IFF_NAPI_FRAGS tap to build a
+ * nonlinear skb that tun runs through do_xdp_generic().
+ */
+static void test_tun(struct xdp_shrink_frags *skel)
+{
+ __u8 head[74], frag1[2048], frag2[2048];
+ struct ethhdr *eth = (void *)head;
+ int tap_fd = -1, ifindex, err;
+ struct netns_obj *ns = NULL;
+ struct iovec iov[3];
+ ssize_t n;
+
+ if (getpagesize() != PAGE_SIZE_4K) {
+ test__skip();
+ return;
+ }
+
+ ns = netns_new(TAP_NETNS, true);
+ if (!ASSERT_OK_PTR(ns, "netns_new"))
+ return;
+
+ tap_fd = create_tap_napi_frags(TAP_NAME);
+ if (!ASSERT_GE(tap_fd, 0, "create_tap"))
+ goto out;
+
+ SYS(out, "ip link set dev " TAP_NAME " up");
+
+ ifindex = if_nametoindex(TAP_NAME);
+ if (!ASSERT_GT(ifindex, 0, "if_nametoindex"))
+ goto out;
+
+ skel->bss->shrink_ran = 0;
+
+ err = bpf_xdp_attach(ifindex, bpf_program__fd(skel->progs.xdp_shrink),
+ 0, NULL);
+ if (!ASSERT_OK(err, "bpf_xdp_attach"))
+ goto out;
+
+ memset(head, 0, sizeof(head));
+ memset(frag1, 0x41, sizeof(frag1));
+ memset(frag2, 0x42, sizeof(frag2));
+ eth->h_proto = htons(ETH_P_IP);
+
+ iov[0].iov_base = head; iov[0].iov_len = sizeof(head);
+ iov[1].iov_base = frag1; iov[1].iov_len = sizeof(frag1);
+ iov[2].iov_base = frag2; iov[2].iov_len = sizeof(frag2);
+
+ n = writev(tap_fd, iov, ARRAY_SIZE(iov));
+ ASSERT_EQ(n, sizeof(head) + sizeof(frag1) + sizeof(frag2), "writev");
+
+ wait_for_prog(skel);
+ /* a buggy kernel only splats "page_pool leak", it does not fail here */
+ ASSERT_GT(skel->bss->shrink_ran, 0, "xdp_prog_ran");
+
+ bpf_xdp_detach(ifindex, 0, NULL);
+out:
+ if (tap_fd >= 0)
+ close(tap_fd);
+ netns_free(ns);
+}
+
+/*
+ * Both veth ends live in their own namespace, so the traffic really crosses
+ * the pair and nothing is created in the caller's namespace.
+ */
+static int veth_setup(char *ns0, char *ns1)
+{
+ if (!ASSERT_OK(append_tid(ns0, NS_NAME_MAX_LEN), "append_tid ns0"))
+ return -1;
+ if (!ASSERT_OK(append_tid(ns1, NS_NAME_MAX_LEN), "append_tid ns1"))
+ return -1;
+
+ SYS(fail, "ip netns add %s", ns0);
+ SYS(fail_ns0, "ip netns add %s", ns1);
+ SYS(fail_ns1, "ip -n %s link add %s mtu 8000 type veth peer name %s mtu 8000",
+ ns0, VETH_LOCAL, VETH_PEER);
+ SYS(fail_ns1, "ip -n %s link set %s netns %s", ns0, VETH_PEER, ns1);
+ SYS(fail_ns1, "ip -n %s link set %s address %s", ns0, VETH_LOCAL,
+ VETH_LOCAL_MAC);
+ SYS(fail_ns1, "ip -n %s addr add %s/24 dev %s", ns0, VETH_LOCAL_IP,
+ VETH_LOCAL);
+ SYS(fail_ns1, "ip -n %s link set %s up", ns0, VETH_LOCAL);
+ SYS(fail_ns1, "ip -n %s addr add %s/24 dev %s", ns1, VETH_PEER_IP,
+ VETH_PEER);
+ SYS(fail_ns1, "ip -n %s link set %s up", ns1, VETH_PEER);
+
+ return 0;
+
+fail_ns1:
+ SYS_NOFAIL("ip netns del %s", ns1);
+fail_ns0:
+ SYS_NOFAIL("ip netns del %s", ns0);
+fail:
+ return -1;
+}
+
+static void veth_cleanup(const char *ns0, const char *ns1)
+{
+ /* Dropping the namespaces takes the veth pair and its XDP programs. */
+ SYS_NOFAIL("ip netns del %s", ns1);
+ SYS_NOFAIL("ip netns del %s", ns0);
+}
+
+static int veth_attach(const char *ns, const char *dev, int prog_fd)
+{
+ struct nstoken *nstoken;
+ int ifindex, err;
+
+ nstoken = open_netns(ns);
+ if (!ASSERT_OK_PTR(nstoken, "open_netns"))
+ return -1;
+
+ ifindex = if_nametoindex(dev);
+ if (!ASSERT_GT(ifindex, 0, "if_nametoindex")) {
+ close_netns(nstoken);
+ return -1;
+ }
+
+ err = bpf_xdp_attach(ifindex, prog_fd, 0, NULL);
+ close_netns(nstoken);
+
+ return ASSERT_OK(err, "bpf_xdp_attach") ? 0 : -1;
+}
+
+/* A large ping builds a nonlinear skb that veth cow's into its page_pool. */
+static void test_veth(struct xdp_shrink_frags *skel)
+{
+ char ns0[NS_NAME_MAX_LEN] = "xdp_shrink_ns0-";
+ char ns1[NS_NAME_MAX_LEN] = "xdp_shrink_ns1-";
+
+ if (getpagesize() != PAGE_SIZE_4K) {
+ test__skip();
+ return;
+ }
+
+ if (veth_setup(ns0, ns1))
+ return;
+
+ skel->bss->shrink_ran = 0;
+
+ if (veth_attach(ns0, VETH_LOCAL, bpf_program__fd(skel->progs.xdp_shrink)))
+ goto out;
+
+ SYS_NOFAIL("ip netns exec %s ping -q -s 5000 -c 3 -W 1 %s",
+ ns1, VETH_LOCAL_IP);
+
+ wait_for_prog(skel);
+ /* a buggy kernel only splats "page_pool leak", it does not fail here */
+ ASSERT_GT(skel->bss->shrink_ran, 0, "xdp_prog_ran");
+out:
+ veth_cleanup(ns0, ns1);
+}
+
+/*
+ * LOCAL cow's the incoming skb and returns XDP_TX, so the buff is turned into
+ * an xdp_frame and bounced to PEER, which shrinks a frag. The page_pool mem
+ * type the buff ran with must not reach the frame, or PEER frees a plain
+ * page (whose pp was already cleared on the XDP_TX side) as page_pool memory.
+ */
+static void test_veth_tx(struct xdp_shrink_frags *skel)
+{
+ char ns0[NS_NAME_MAX_LEN] = "xdp_shrink_tx0-";
+ char ns1[NS_NAME_MAX_LEN] = "xdp_shrink_tx1-";
+
+ if (getpagesize() != PAGE_SIZE_4K) {
+ test__skip();
+ return;
+ }
+
+ if (veth_setup(ns0, ns1))
+ return;
+
+ /*
+ * LOCAL bounces everything (incl. ARP) with XDP_TX, so pin a static
+ * neighbour to let the ping's payload actually reach it.
+ */
+ SYS(out, "ip -n %s neigh add %s lladdr %s dev %s nud permanent",
+ ns1, VETH_LOCAL_IP, VETH_LOCAL_MAC, VETH_PEER);
+
+ skel->bss->shrink_ran = 0;
+
+ if (veth_attach(ns0, VETH_LOCAL, bpf_program__fd(skel->progs.xdp_tx)))
+ goto out;
+ if (veth_attach(ns1, VETH_PEER, bpf_program__fd(skel->progs.xdp_shrink)))
+ goto out;
+
+ SYS_NOFAIL("ip netns exec %s ping -q -s 5000 -c 3 -W 1 %s",
+ ns1, VETH_LOCAL_IP);
+
+ wait_for_prog(skel);
+ /* a buggy kernel only splats "page_pool leak", it does not fail here */
+ ASSERT_GT(skel->bss->shrink_ran, 0, "xdp_prog_ran");
+out:
+ veth_cleanup(ns0, ns1);
+}
+
+void test_xdp_shrink_frags(void)
+{
+ struct xdp_shrink_frags *skel;
+
+ skel = xdp_shrink_frags__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "skel_open_load"))
+ return;
+
+ if (test__start_subtest("tun"))
+ test_tun(skel);
+ if (test__start_subtest("veth"))
+ test_veth(skel);
+ if (test__start_subtest("veth_tx"))
+ test_veth_tx(skel);
+
+ xdp_shrink_frags__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/xdp_shrink_frags.c b/tools/testing/selftests/bpf/progs/xdp_shrink_frags.c
new file mode 100644
index 000000000000..5d8e26f1b3a6
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/xdp_shrink_frags.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+int shrink_ran;
+
+SEC("xdp.frags")
+int xdp_shrink(struct xdp_md *ctx)
+{
+ /*
+ * Runs on both skb-backed XDP paths (generic XDP via tun, and veth):
+ * the nonlinear skb is cow'd into page_pool memory before we run, so
+ * shrinking the tail releases a whole frag that has to go back to that
+ * pool. The counter only tells us the program ran and the helper
+ * succeeded -- a linear buff returns 0 as well -- it does not prove a
+ * frag was released.
+ */
+ if (bpf_xdp_adjust_tail(ctx, -3000) == 0)
+ __sync_fetch_and_add(&shrink_ran, 1);
+ return XDP_PASS;
+}
+
+SEC("xdp.frags")
+int xdp_tx(struct xdp_md *ctx)
+{
+ /*
+ * Bounce the frame back. On veth this turns the buff into an
+ * xdp_frame, which must not take over the page_pool mem type the
+ * buff ran with.
+ */
+ return XDP_TX;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH net v9 5/6] selftests/bpf: add xdp_shrink_frags
2026-11-27 10:01 ` [PATCH net v9 5/6] selftests/bpf: add xdp_shrink_frags Slawomir Stepien
@ 2026-11-28 10:04 ` netdev-bot+sashiko
0 siblings, 0 replies; 9+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28 10:04 UTC (permalink / raw)
To: sst
Cc: davem, edumazet, kuba, pabeni, ast, daniel, hawk, john.fastabend,
sdf, bpf, netdev, jiayuan.chen, phx0fer, maciej.fijalkowski,
tianyi, tavip, toke, aleksander.lobakin, lorenzo,
lorenzo.bianconi, ihor.solodrai, andrew+netdev, willemb, horms,
andrii, martin.lau, eddyz87, song, yonghong.song, jolsa, memxor,
emil, shuah, linux-kselftest
Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 3
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] None of the three subtests in
tools/testing/selftests/bpf/prog_tests/xdp_shrink_frags.c can fail on…
- [Low] The commit message lists the three subtests and then says 'A
kernel without the fix will report "Bad page state ... page_pool…
- [Low] The comment in xdp_shrink()
(tools/testing/selftests/bpf/progs/xdp_shrink_frags.c) says the…
- [Low] Two comments say the veth path starts from a nonlinear skb.
--
selftests/bpf: add xdp_shrink_frags
This adds a selftest with an xdp.frags program that shrinks the tail by
3000 bytes, so that bpf_xdp_shrink_data() releases a page_pool frag on the
skb-backed XDP paths. Three subtests cover generic XDP on a tap device,
the veth skb path, and a veth XDP_TX bounce to the peer, and all three
skip on systems without 4K pages.
> test_veth_tx bounces the cow'd buff with XDP_TX so the peer runs a frags
> program on the resulting frame, covering the buff -> frame -> buff path.
>
> A kernel without the fix will report "Bad page state ... page_pool leak".
[Severity: Low]
Is this true for test_veth_tx?
On the baseline kernel, the XDP_TX branch of veth_xdp_rcv_skb() already
restores the rxq mem type before the buff is converted to a frame:
drivers/net/veth.c:veth_xdp_rcv_skb() {
...
case XDP_TX:
veth_xdp_get(xdp);
consume_skb(skb);
xdp->rxq->mem = rq->xdp_mem;
if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
...
}
rq->xdp_mem comes from the MEM_TYPE_PAGE_SHARED registration, and the
xdp_tx program on LOCAL does not shrink anything.
So the frame that reaches PEER is PAGE_SHARED, and PEER's rxq is
PAGE_SHARED too. xdp_shrink on PEER then frees a plain page through
page_frag_free(), which is correct. An unfixed kernel would report nothing
for this subtest.
It looks like test_veth_tx only guards the reset that the veth skb-path fix
in this series adds to veth_xdp_get():
/* the buff holds plain page references from here on */
xdp->rxq->mem.type = MEM_TYPE_PAGE_SHARED;
If that reset went missing, the symptom would not be a "page_pool leak"
splat. A page whose pp was cleared would be sent down the page_pool return
path instead.
Every frame veth builds from the skb path is PAGE_SHARED. That means the
page_pool frame case handled by "veth: use the frame mem type when running
XDP on an xdp_frame" is not reached either, despite the "covering the
buff -> frame -> buff path" wording.
The series has four fixes, so "the fix" is also ambiguous. Could the commit
message say which fix each subtest covers and what failure it protects
against?
> diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_shrink_frags.c b/tools/testing/selftests/bpf/prog_tests/xdp_shrink_frags.c
> new file mode 100644
> index 0000000000000..789dfdd51b579
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/xdp_shrink_frags.c
> @@ -0,0 +1,288 @@
[ ... ]
> +static void test_tun(struct xdp_shrink_frags *skel)
> +{
[ ... ]
> + n = writev(tap_fd, iov, ARRAY_SIZE(iov));
> + ASSERT_EQ(n, sizeof(head) + sizeof(frag1) + sizeof(frag2), "writev");
> +
> + wait_for_prog(skel);
> + /* a buggy kernel only splats "page_pool leak", it does not fail here */
> + ASSERT_GT(skel->bss->shrink_ran, 0, "xdp_prog_ran");
[Severity: Medium]
Can any of the three subtests fail on a kernel without the fixes?
This ASSERT_GT() is the only check here, and test_veth() and test_veth_tx()
use the same one. xdp_shrink() increments shrink_ran whenever
bpf_xdp_adjust_tail(ctx, -3000) returns 0:
if (bpf_xdp_adjust_tail(ctx, -3000) == 0)
__sync_fetch_and_add(&shrink_ran, 1);
That happens on fixed and buggy kernels alike. It also happens on any
linear buff longer than about 3014 bytes, where there is no frag at all.
The regression only shows up as "BUG: Bad page state ... page_pool leak"
from bad_page(). Nothing in the test reads /proc/sys/kernel/tainted or
scans kmsg, so unless the harness scans dmesg, test_progs reports PASS on
a regressed kernel.
The subtests would also keep passing, without testing anything, if a later
change to skb_pp_cow_data() or veth stopped leaving a frag after the cow.
bad_page() calls add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE) even when
its printk is rate-limited. Would it work to compare the TAINT_BAD_PAGE bit
(bit 5) of /proc/sys/kernel/tainted before and after each subtest?
Another option is for xdp_shrink() to count only when the buff had frags
before the shrink and lost one. It could compare bpf_xdp_get_buff_len()
against data_end - data to tell.
> +
> + bpf_xdp_detach(ifindex, 0, NULL);
[ ... ]
> +/* A large ping builds a nonlinear skb that veth cow's into its page_pool. */
> +static void test_veth(struct xdp_shrink_frags *skel)
[Severity: Low]
Is the skb here actually nonlinear?
With MTU 8000 and a 5000-byte ping, __ip_append_data() puts the payload in
the linear area. paged is false because ping has no gso_size, zerocopy or
MSG_SPLICE_PAGES. Also fraglen + alloc_extra < SKB_MAX_ALLOC, so
alloclen = fraglen.
veth_convert_skb_to_xdp_buff() then cows the skb only because of the
headroom check, or because the skb is shared or its head is locked:
if (skb_shared(skb) || skb_head_is_locked(skb) ||
skb_is_nonlinear(skb) ||
skb_headroom(skb) < XDP_PACKET_HEADROOM) {
The skb only becomes nonlinear as a result of skb_pp_cow_data(). The file
header comment ("skb-backed XDP first cow's the nonlinear skb into
page_pool memory") has the same problem for the veth path.
If the sender ever left at least XDP_PACKET_HEADROOM of headroom, veth
would run the program on the linear skb without a cow. The 3000-byte
shrink would still succeed, and this subtest would pass without reaching
the page_pool frag path.
Could the comment describe the headroom dependency instead?
[ ... ]
> diff --git a/tools/testing/selftests/bpf/progs/xdp_shrink_frags.c b/tools/testing/selftests/bpf/progs/xdp_shrink_frags.c
> new file mode 100644
> index 0000000000000..5d8e26f1b3a63
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/xdp_shrink_frags.c
> @@ -0,0 +1,34 @@
[ ... ]
> +SEC("xdp.frags")
> +int xdp_shrink(struct xdp_md *ctx)
> +{
> + /*
> + * Runs on both skb-backed XDP paths (generic XDP via tun, and veth):
> + * the nonlinear skb is cow'd into page_pool memory before we run, so
> + * shrinking the tail releases a whole frag that has to go back to that
> + * pool. The counter only tells us the program ran and the helper
> + * succeeded -- a linear buff returns 0 as well -- it does not prove a
> + * frag was released.
> + */
[Severity: Low]
This comment only describes the skb-backed case, but test_veth_tx() also
attaches xdp_shrink to VETH_PEER:
if (veth_attach(ns1, VETH_PEER, bpf_program__fd(skel->progs.xdp_shrink)))
On PEER the program runs from veth_xdp_rcv_one() on the xdp_frame that
LOCAL bounced with XDP_TX, not on a cow'd skb.
By then veth_xdp_get() has switched those frags to plain page references
with MEM_TYPE_PAGE_SHARED. The released frag must go through
page_frag_free() rather than back to a pool. The comment above
test_veth_tx() itself says that freeing it "as page_pool memory" is the
bug.
Should this comment mention the frame case on PEER?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127100132.1977890-1-sst%40poczta.fm
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH net v9 6/6] selftests/bpf: Grow test_run and generic XDP frags after a test run
2026-11-27 10:01 [PATCH net v9 0/6] xdp: fix page_pool leaks Slawomir Stepien
` (4 preceding siblings ...)
2026-11-27 10:01 ` [PATCH net v9 5/6] selftests/bpf: add xdp_shrink_frags Slawomir Stepien
@ 2026-11-27 10:01 ` Slawomir Stepien
5 siblings, 0 replies; 9+ messages in thread
From: Slawomir Stepien @ 2026-11-27 10:01 UTC (permalink / raw)
To: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Alexei Starovoitov, Daniel Borkmann, Jesper Dangaard Brouer,
John Fastabend, Stanislav Fomichev
Cc: bpf, netdev, Jiayuan Chen, Dudu Lu, Maciej Fijalkowski,
Tianyi Gao, Octavian Purdila, Toke Høiland-Jørgensen,
Alexander Lobakin, Lorenzo Bianconi, Lorenzo Bianconi,
Ihor Solodrai, Andrew Lunn, Willem de Bruijn, Simon Horman,
Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
Yonghong Song, Jiri Olsa, Kumar Kartikeya Dwivedi,
Emil Tsalapatis, Shuah Khan, linux-kselftest, Slawomir Stepien
Add two xdp_adjust_tail subtests for frag_size of the rxq info a
program runs with:
- xdp_adjust_frags_tail_grow_ingress_ifindex grows the last frag of a
9000 byte test_run packet with ingress_ifindex set to a veth device.
test_run allocates PAGE_SIZE frags, so the grow must succeed.
- xdp_adjust_frags_tail_grow_lo_after_test_run does a test run, then
sends a UDP packet a bit larger than a page over lo with a generic XDP
frags program attached, or is skipped with 64K pages. Generic XDP
copies the packet into a page_pool head page and a frag, which can be
a part of a page, so growing the frag must fail with -EOPNOTSUPP. The
program asks for more than any tailroom, so nothing is written even
when the grow is allowed.
Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
.../bpf/prog_tests/xdp_adjust_tail.c | 137 ++++++++++++++++++
.../test_xdp_adjust_frags_tail_grow_lo.c | 25 ++++
2 files changed, 162 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_adjust_frags_tail_grow_lo.c
diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c b/tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c
index 43264347e7d7..c7c803fd9241 100644
--- a/tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c
+++ b/tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c
@@ -1,6 +1,10 @@
// SPDX-License-Identifier: GPL-2.0
#include <test_progs.h>
#include <network_helpers.h>
+#include <linux/if_link.h>
+#include <net/if.h>
+#include "test_xdp_adjust_frags_tail_grow_lo.skel.h"
+#include "test_xdp_adjust_tail_grow.skel.h"
static void test_xdp_adjust_tail_shrink(void)
{
@@ -355,6 +359,135 @@ static void test_xdp_adjust_frags_tail_grow_64k(void)
bpf_object__close(obj);
}
+/*
+ * test_run allocates PAGE_SIZE frags, so a program may grow the last one up
+ * to the end of its page, also when ingress_ifindex picks the rxq info of
+ * another device.
+ */
+static void test_xdp_adjust_frags_tail_grow_ingress_ifindex(void)
+{
+ struct xdp_md ctx = { .data_end = 9000 };
+ struct test_xdp_adjust_tail_grow *skel;
+ struct netns_obj *ns = NULL;
+ int err, ifindex, prog_fd;
+ __u8 *buf = NULL;
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .ctx_in = &ctx,
+ .ctx_size_in = sizeof(ctx),
+ );
+
+ if (getpagesize() != 4096) {
+ test__skip();
+ return;
+ }
+
+ skel = test_xdp_adjust_tail_grow__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "skel open_and_load"))
+ return;
+
+ ns = netns_new("xdp_adjust_tail_ns", true);
+ if (!ASSERT_OK_PTR(ns, "netns_new"))
+ goto out;
+
+ SYS(out, "ip link add veth_xat0 type veth peer name veth_xat1");
+ ifindex = if_nametoindex("veth_xat0");
+ if (!ASSERT_GT(ifindex, 0, "veth ifindex"))
+ goto out;
+ ctx.ingress_ifindex = ifindex;
+
+ buf = malloc(16384);
+ if (!ASSERT_OK_PTR(buf, "alloc buf 16Kb"))
+ goto out;
+ memset(buf, 1, 16384);
+
+ /* Test case add 10 bytes to last frag */
+ topts.data_in = buf;
+ topts.data_out = buf;
+ topts.data_size_in = 9000;
+ topts.data_size_out = 16384;
+ prog_fd = bpf_program__fd(skel->progs._xdp_adjust_tail_grow);
+ err = bpf_prog_test_run_opts(prog_fd, &topts);
+
+ ASSERT_OK(err, "9Kb+10b");
+ ASSERT_EQ(topts.retval, XDP_TX, "9Kb+10b retval");
+ ASSERT_EQ(topts.data_size_out, 9000 + 10, "9Kb+10b size");
+out:
+ free(buf);
+ netns_free(ns);
+ test_xdp_adjust_tail_grow__destroy(skel);
+}
+
+/*
+ * The frags generic XDP gets from skb_pp_cow_data() can be parts of a
+ * page_pool page, so its rxq info must not allow growing them. test_run
+ * must not change that for lo.
+ */
+static void test_xdp_adjust_frags_tail_grow_lo_after_test_run(void)
+{
+ struct sockaddr_in addr = {
+ .sin_family = AF_INET,
+ .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
+ .sin_port = htons(9),
+ };
+ struct test_xdp_adjust_frags_tail_grow_lo *skel;
+ struct netns_obj *ns = NULL;
+ static char payload[60000];
+ int fd = -1, lo, i, len, prog_fd;
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .data_in = &pkt_v4,
+ .data_size_in = sizeof(pkt_v4),
+ .repeat = 1,
+ );
+
+ /* larger than the head generic XDP copies the packet into */
+ len = getpagesize() + 500;
+ if (len > (int)sizeof(payload)) {
+ test__skip();
+ return;
+ }
+
+ skel = test_xdp_adjust_frags_tail_grow_lo__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "skel open_and_load"))
+ return;
+
+ ns = netns_new("xdp_adjust_tail_ns", true);
+ if (!ASSERT_OK_PTR(ns, "netns_new"))
+ goto out;
+
+ SYS(out, "ip link set dev lo up");
+ lo = if_nametoindex("lo");
+ if (!ASSERT_GT(lo, 0, "lo ifindex"))
+ goto out;
+
+ /* a test run in this netns, before generic XDP sees a packet on lo */
+ prog_fd = bpf_program__fd(skel->progs.xdp_grow_frags);
+ ASSERT_OK(bpf_prog_test_run_opts(prog_fd, &topts), "test_run");
+
+ if (!ASSERT_OK(bpf_xdp_attach(lo, prog_fd, XDP_FLAGS_SKB_MODE, NULL),
+ "attach generic XDP"))
+ goto out;
+
+ /*
+ * a linear skb with little headroom: generic XDP copies it into a
+ * page_pool head page and a frag
+ */
+ fd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (ASSERT_GE(fd, 0, "socket"))
+ ASSERT_EQ(sendto(fd, payload, len, 0, (struct sockaddr *)&addr,
+ sizeof(addr)), len, "sendto");
+ for (i = 0; i < 100 && skel->data->grow_ret == 1; i++)
+ usleep(10000);
+ bpf_xdp_detach(lo, XDP_FLAGS_SKB_MODE, NULL);
+
+ if (ASSERT_TRUE(skel->data->grow_ret != 1, "xdp prog saw frags"))
+ ASSERT_EQ(skel->data->grow_ret, -EOPNOTSUPP, "grow frag on lo");
+out:
+ if (fd >= 0)
+ close(fd);
+ netns_free(ns);
+ test_xdp_adjust_frags_tail_grow_lo__destroy(skel);
+}
+
void test_xdp_adjust_tail(void)
{
int page_size = getpagesize();
@@ -373,4 +506,8 @@ void test_xdp_adjust_tail(void)
else
test_xdp_adjust_frags_tail_grow_4k();
}
+ if (test__start_subtest("xdp_adjust_frags_tail_grow_ingress_ifindex"))
+ test_xdp_adjust_frags_tail_grow_ingress_ifindex();
+ if (test__start_subtest("xdp_adjust_frags_tail_grow_lo_after_test_run"))
+ test_xdp_adjust_frags_tail_grow_lo_after_test_run();
}
diff --git a/tools/testing/selftests/bpf/progs/test_xdp_adjust_frags_tail_grow_lo.c b/tools/testing/selftests/bpf/progs/test_xdp_adjust_frags_tail_grow_lo.c
new file mode 100644
index 000000000000..207346b14ad8
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_xdp_adjust_frags_tail_grow_lo.c
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+/* 1 until the program saw a packet with frags */
+int grow_ret = 1;
+
+SEC("xdp.frags")
+int xdp_grow_frags(struct xdp_md *xdp)
+{
+ void *data_end = (void *)(long)xdp->data_end;
+ void *data = (void *)(long)xdp->data;
+
+ if (grow_ret != 1 || bpf_xdp_get_buff_len(xdp) <= data_end - data)
+ return XDP_PASS;
+
+ /*
+ * More than the tailroom of any frag, so nothing is written: the
+ * result only tells if frag_size of the rxq allows a grow at all.
+ */
+ grow_ret = bpf_xdp_adjust_tail(xdp, 0x10000);
+ return XDP_PASS;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.56.0
^ permalink raw reply related [flat|nested] 9+ messages in thread