Subsystems (6)
Subject: [PATCH 0/2] bpf: preserve rx_queue_index across XDP redirects
XDP programs executed after redirect through cpumap and devmap currently lose ingress RX queue information because rx_queue_index is not preserved across xdp_buff to xdp_frame conversion.
Preserve rx_queue_index in struct xdp_frame and restore it when rebuilding xdp_rxq_info for redirected execution paths.
Add a selftest validating that ctx->rx_queue_index remains available through redirected execution.
Testing:
* Built modified kernel objects
* Ran tools/testing/selftests/bpf/test_progs -t xdp_metadata -v
* Verified xdp_metadata passes
* Added explicit rx_queue_index assertion
Siddharth C (1):
bpf: preserve rx_queue_index across XDP redirects
Siddharth_Cibi (1):
selftests/bpf: validate rx_queue_index in xdp_metadata
include/net/xdp.h | 2 ++ kernel/bpf/cpumap.c | 2 +- kernel/bpf/devmap.c | 5 ++++- net/core/xdp.c | 1 + tools/testing/selftests/bpf/prog_tests/xdp_metadata.c | 3 ++- tools/testing/selftests/bpf/progs/xdp_metadata.c | 2 +- tools/testing/selftests/bpf/xdp_metadata.h | 1 + 7 files changed, 12 insertions(+), 4 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/2 bpf: preserve rx_queue_index across XDP redirects
Store rx_queue_index in struct xdp_frame during xdp_buff to xdp_frame conversion and restore it when rebuilding xdp_rxq_info for cpumap and devmap execution paths.
This preserves ingress RX queue information for XDP programs executed after redirect, allowing access to the original rx_queue_index instead of losing queue context.
Also propagate rx_queue_index for zero-copy XDP frame conversion.
--- include/net/xdp.h | 2 ++ kernel/bpf/cpumap.c | 2 +- kernel/bpf/devmap.c | 5 ++++- net/core/xdp.c | 1 + 4 files changed, 8 insertions(+), 2 deletions(-)
include/net/xdp.h +2 -0
@@ -301,6 +301,7 @@ struct xdp_frame {*/ enum xdp_mem_type mem_type:32;struct net_device *dev_rx;/* used by cpumap */ +u32 rx_queue_index;u32 frame_sz;u32 flags;/* supported values defined in xdp_buff_flags */ };
@@ -441,6 +442,7 @@ struct xdp_frame *xdp_convert_buff_to_frame(struct xdp_buff *xdp)/* rxq only valid until napi_schedule ends, convert to xdp_mem_type */ xdp_frame->mem_type = xdp->rxq->mem.type; + xdp_frame->rx_queue_index = xdp->rxq->queue_index;return xdp_frame; }
kernel/bpf/cpumap.c +1 -1
@@ -197,7 +197,7 @@ static int cpu_map_bpf_prog_run_xdp(struct bpf_cpu_map_entry *rcpu, rxq.dev = xdpf->dev_rx; rxq.mem.type = xdpf->mem_type; -/* TODO: report queue_index to xdp_rxq_info */ + rxq.queue_index = xdpf->rx_queue_index;xdp_convert_frame_to_buff (xdpf, &xdp);
kernel/bpf/devmap.c +4 -1
@@ -339,7 +339,7 @@ static int dev_map_bpf_prog_run(struct bpf_prog *xdp_prog, struct net_device *rx_dev) {struct xdp_txq_info txq = { .dev = tx_dev }; -struct xdp_rxq_info rxq = { .dev = rx_dev }; +struct xdp_rxq_info rxq = { };struct xdp_buff xdp;int i, nframes =0 ;
@@ -349,6 +349,9 @@ static int dev_map_bpf_prog_run(struct bpf_prog *xdp_prog,int err;xdp_convert_frame_to_buff (xdpf, &xdp); + rxq.dev = rx_dev; + rxq.mem.type = xdpf->mem_type; + rxq.queue_index = xdpf->rx_queue_index; xdp.txq = &txq; xdp.rxq = &rxq;
net/core/xdp.c +1 -0
@@ -606,6 +606,7 @@ struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp) xdpf->metasize = metasize; xdpf->frame_sz =PAGE_SIZE ; xdpf->mem_type =MEM_TYPE_PAGE_ORDER0 ; + xdpf->rx_queue_index = xdp->rxq->queue_index;xsk_buff_free (xdp);return xdpf;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 1/2] bpf: preserve rx_queue_index across XDP redirects
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Uninitialized `rx_queue_index` in `struct xdp_frame` constructed by the BPF testing framework…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Uninitialized rx_queue_index in struct xdp_frame constructed by the BPF testing framework causes a kernel memory information leak. --
commit f1285cd92bd58ea552eb0b340894db477870968e
Author: Siddharth C <[email protected]> Date: Sat Jun 20 12:13:13 2026 +0000
bpf: preserve rx_queue_index across XDP redirects
Store rx_queue_index in struct xdp_frame during xdp_buff to xdp_frame conversion and restore it when rebuilding xdp_rxq_info for cpumap and devmap execution paths.
This preserves ingress RX queue information for XDP programs executed after redirect, allowing access to the original rx_queue_index instead of losing queue context.
Also propagate rx_queue_index for zero-copy XDP frame conversion.
kernel/bpf/cpumap.c static int cpu_map_bpf_prog_run_xdp(struct bpf_cpu_map_entry *rcpu, · line 200
Quoted 10 lines
kernel/bpf/cpumap.c +1
-1@@ -197,7 +197,7 @@ static int cpu_map_bpf_prog_run_xdp(struct bpf_cpu_map_entry *rcpu, rxq.dev = xdpf->dev_rx; rxq.mem.type = xdpf->mem_type; -/* TODO: report queue_index to xdp_rxq_info */ + rxq.queue_index = xdpf->rx_queue_index;
[Severity: High]
Can this leak uninitialized kernel page memory via ctx->rx_queue_index when using BPF_PROG_TEST_RUN?
The BPF testing framework uses xdp_update_frame_from_buff() to initialize mock frames in net/bpf/test_run.c. Because xdp_update_frame_from_buff() only copies sizes and pointers, test_run manually copies mem_type but was not updated by this patch to initialize rx_queue_index:
net/bpf/test_run.c:xdp_test_run_init_page() {
...
xdp_update_frame_from_buff(new_ctx, frm);
frm->mem_type = new_ctx->rxq->mem.type;
/* Missing: frm->rx_queue_index = new_ctx->rxq->queue_index; */
...
}It is also missed in the context reset path:
net/bpf/test_run.c:reset_ctx() {
...
xdp_update_frame_from_buff(&head->ctx, head->frame);
head->frame->mem_type = head->orig_ctx.rxq->mem.type;
/* Missing: head->frame->rx_queue_index = head->orig_ctx.rxq->queue_index; */
}If a tested BPF program returns XDP_REDIRECT to a cpumap or devmap, the map execution reconstructs the xdp_rxq_info using the uninitialized xdpf->rx_queue_index field. A chained BPF program executing on the map could then read ctx->rx_queue_index, exposing 4 bytes of uninitialized kernel page memory.
Should net/bpf/test_run.c be updated to initialize rx_queue_index?
Quoted 3 lines
kernel/bpf/cpumap.c +0
-0xdp_convert_frame_to_buff (xdpf, &xdp);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Jakub Kicinski
maintainerRe: [PATCH 1/2] bpf: preserve rx_queue_index across XDP redirects
In reply to Siddharth Ckernel/bpf/cpumap.c static int cpu_map_bpf_prog_run_xdp(struct bpf_cpu_map_entry *rcpu, · line 200
Quoted 10 lines · On Sat, 20 Jun 2026 12:13:13 +0000 Siddharth C wrote:
kernel/bpf/cpumap.c +1
-1@@ -197,7 +197,7 @@ static int cpu_map_bpf_prog_run_xdp(struct bpf_cpu_map_entry *rcpu, rxq.dev = xdpf->dev_rx; rxq.mem.type = xdpf->mem_type; -/* TODO: report queue_index to xdp_rxq_info */ + rxq.queue_index = xdpf->rx_queue_index;
Do you actually need this or you're just trying to address the TODO? You can always store the info you need in the metadata prepend.
Quoted 16 lines
kernel/bpf/cpumap.c +0
-0xdp_convert_frame_to_buff (xdpf, &xdp);kernel/bpf/devmap.c +1
-1@@ -339,7 +339,7 @@ static int dev_map_bpf_prog_run(struct bpf_prog *xdp_prog, struct net_device *rx_dev) {struct xdp_txq_info txq = { .dev = tx_dev }; -struct xdp_rxq_info rxq = { .dev = rx_dev }; +struct xdp_rxq_info rxq = { };struct xdp_buff xdp;int i, nframes =0 ;
kernel/bpf/devmap.c static int dev_map_bpf_prog_run(struct bpf_prog *xdp_prog, · line 353
Quoted 5 lines
kernel/bpf/devmap.c +2
-0@@ -349,6 +349,9 @@ static int dev_map_bpf_prog_run(struct bpf_prog *xdp_prog,int err;xdp_convert_frame_to_buff (xdpf, &xdp); + rxq.dev = rx_dev; + rxq.mem.type = xdpf->mem_type;
Why are you setting mem_type?
Quoted 3 lines
kernel/bpf/devmap.c +1
-0+ rxq.queue_index = xdpf->rx_queue_index; xdp.txq = &txq; xdp.rxq = &rxq;
Re: [PATCH 1/2] bpf: preserve rx_queue_index across XDP redirects
Quoted 7 lines · Jakub Kicinski
Quoted 6 lines · On Sat, 20 Jun 2026 12:13:13 +0000 Siddharth C wrote:
kernel/bpf/cpumap.c +0
-0@@ -197,7 +197,7 @@ static int cpu_map_bpf_prog_run_xdp(struct bpf_cpu_map_entry *rcpu,
Quoted 6 lines
kernel/bpf/cpumap.c static int cpu_map_bpf_prog_run_xdp(struct bpf_cpu_map_entry *rcpu, · line 200
Quoted 4 lines
kernel/bpf/cpumap.c +1
-1rxq.dev = xdpf->dev_rx; rxq.mem.type = xdpf->mem_type; -/* TODO: report queue_index to xdp_rxq_info */ + rxq.queue_index = xdpf->rx_queue_index;Do you actually need this or you're just trying to address the TODO?
It's a 3rd if not 4th attempt from various "people" to address this TODO. We should just remove this line instead.
Re: [PATCH 1/2] bpf: preserve rx_queue_index across XDP redirects
Quoted 15 lines · Alexei Starovoitov
Quoted 7 lines · Jakub Kicinski
Quoted 6 lines · On Sat, 20 Jun 2026 12:13:13 +0000 Siddharth C wrote:
kernel/bpf/cpumap.c +0
-0@@ -197,7 +197,7 @@ static int cpu_map_bpf_prog_run_xdp(struct bpf_cpu_map_entry *rcpu,Quoted 6 lines
kernel/bpf/cpumap.c static int cpu_map_bpf_prog_run_xdp(struct bpf_cpu_map_entry *rcpu, · line 200
Quoted 4 lines
kernel/bpf/cpumap.c +1
-1rxq.dev = xdpf->dev_rx; rxq.mem.type = xdpf->mem_type; -/* TODO: report queue_index to xdp_rxq_info */ + rxq.queue_index = xdpf->rx_queue_index;Do you actually need this or you're just trying to address the TODO?
Quoted 3 lines
It's a 3rd if not 4th attempt from various "people" to address this TODO. We should just remove this line instead.
I was one of the people that tried this. And also I have already sent
a patch to address the misleading comment to prevent future wasted effort. My patch is still on hold (LINK: https://lore.kernel.org/all/[email protected]/).
Best Regards,
Mehdi Ben Hadj Khelifa
PATCH 2/2 selftests/bpf: validate rx_queue_index in xdp_metadata
In reply to Siddharth CFrom: Siddharth_Cibi <[email protected]>
Extend xdp_metadata selftest coverage to validate that ctx->rx_queue_index is preserved and observable after XDP redirect execution.
Capture rx_queue_index in metadata and assert that it matches the expected queue during packet verification.
--- tools/testing/selftests/bpf/prog_tests/xdp_metadata.c | 3 ++- tools/testing/selftests/bpf/progs/xdp_metadata.c | 2 +- tools/testing/selftests/bpf/xdp_metadata.h | 1 + 3 files changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/xdp_metadata.c +2 -1
@@ -309,7 +309,8 @@ static int verify_xsk_metadata(struct xsk *xsk, bool sent_from_af_xdp)if (!ASSERT_NEQ (meta->rx_hash,0 ,"rx_hash" ))return -1 ; - +if (!ASSERT_EQ (meta->rx_queue_index,QUEUE_ID ,"rx_queue_index" )) +return -1 ;if (!sent_from_af_xdp) {if (!ASSERT_NEQ (meta->rx_hash_type &XDP_RSS_TYPE_L4 ,0 ,"rx_hash_type" ))return -1 ;
tools/testing/selftests/bpf/progs/xdp_metadata.c +1 -1
@@ -98,7 +98,7 @@ int rx(struct xdp_md *ctx)bpf_xdp_metadata_rx_hash (ctx, &meta->rx_hash, &meta->rx_hash_type);bpf_xdp_metadata_rx_vlan_tag (ctx, &meta->rx_vlan_proto, &meta->rx_vlan_tci); - + meta->rx_queue_index = ctx->rx_queue_index;return bpf_redirect_map (&xsk, ctx->rx_queue_index,XDP_PASS ); }
tools/testing/selftests/bpf/xdp_metadata.h +1 -0
@@ -49,4 +49,5 @@ struct xdp_meta {__s32 rx_vlan_tag_err; };enum xdp_meta_field hint_valid; +__u32 rx_queue_index; };
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 2/2] selftests/bpf: validate rx_queue_index in xdp_metadata
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Adding `rx_queue_index` changes `struct xdp_meta` size to 36 bytes, causing implicit padding to…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Adding rx_queue_index changes struct xdp_meta size to 36 bytes, causing implicit padding to 40 bytes on 64-bit systems but not on 32-bit systems. This creates a BPF-to-userspace metadata offset mismatch on 32-bit hosts.
- [Low] The newly added assertion returns directly with incorrect space-based indentation.
--
commit 5d50c9ea90d4f7a83d1f9562df723456d7ed8e43
Author: Siddharth_Cibi <[email protected]>
selftests/bpf: validate rx_queue_index in xdp_metadata
This commit extends the xdp_metadata selftest to capture and validate the rx_queue_index field. It adds the index to the xdp_meta structure and asserts its value matches the expected queue during packet verification.
tools/testing/selftests/bpf/prog_tests/xdp_metadata.c static int verify_xsk_metadata(struct xsk *xsk, bool sent_from_af_xdp) · line 313
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_metadata.c +2
-1@@ -309,7 +309,8 @@ static int verify_xsk_metadata(struct xsk *xsk, bool sent_from_af_xdp)if (!ASSERT_NEQ (meta->rx_hash,0 ,"rx_hash" ))return -1 ; - +if (!ASSERT_EQ (meta->rx_queue_index,QUEUE_ID ,"rx_queue_index" )) +return -1 ;
[Severity: Low]
This isn't a bug, but does this return statement use spaces for indentation instead of tabs?
[ ... ]
tools/testing/selftests/bpf/xdp_metadata.h struct xdp_meta { · line 53
Quoted 9 lines
tools/testing/selftests/bpf/xdp_metadata.h +1
-0@@ -49,4 +49,5 @@ struct xdp_meta {__s32 rx_vlan_tag_err; };enum xdp_meta_field hint_valid; +__u32 rx_queue_index; };
[Severity: Medium]
Does adding rx_queue_index cause a structure size mismatch between the BPF target and 32-bit hosts?
The xdp_meta struct contains __u64 fields, so on the BPF target and 64-bit hosts, the 36-byte structure will be padded to 40 bytes to maintain 8-byte alignment. In the BPF program, bpf_xdp_adjust_meta() will then allocate 40 bytes.
However, on 32-bit hosts where __u64 only requires 4-byte alignment, the compiler will evaluate the structure size as 36 bytes. The userspace test then reads the metadata pointer exactly 4 bytes after the start of the BPF-populated metadata, leading to misaligned reads for all fields.
Should an explicit 4-byte padding field be added at the end of the struct, or an explicit alignment attribute be used?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2