Skip to contentSkip to conversation

PATCH In review Siddharth C 5 participants 8 messages Jun 20 – Jun 25

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index aa742f413c35..90318b2b76dc 100644
@@ -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
index 5e59ab896f05..8f2d7013620f 100644
@@ -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
index dc7b859e8bbf..f419fa0e53e5 100644
@@ -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
index 9890a30584ba..9691d8dfadf3 100644
@@ -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
#2 lore raw

Re: [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
index 5e59ab896f058..8f2d7013620f7 100644
@@ -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 -0
 		xdp_convert_frame_to_buff(xdpf, &xdp);
 
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#3 lore raw

Re: [PATCH 1/2] bpf: preserve rx_queue_index across XDP redirects

In reply to Siddharth C

kernel/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
index 5e59ab896f05..8f2d7013620f 100644
@@ -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 -0
 		xdp_convert_frame_to_buff(xdpf, &xdp);
 
kernel/bpf/devmap.c +1 -1
index dc7b859e8bbf..f419fa0e53e5 100644
@@ -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;
#4 lore raw

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
index 5e59ab896f05..8f2d7013620f 100644
@@ -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 -1
 		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?

It's a 3rd if not 4th attempt from various "people" to address this TODO. We should just remove this line instead.

#5 lore raw

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
index 5e59ab896f05..8f2d7013620f 100644
@@ -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 -1
  		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?

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

#6 lore raw

PATCH 2/2 selftests/bpf: validate rx_queue_index in xdp_metadata

In reply to Siddharth C

From: 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
index 5c31054ad4a4..f8cabbbe7bb7 100644
@@ -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
index 09bb8a038d52..62ae83860d7f 100644
@@ -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
index 87318ad1117a..1f0ae4c00091 100644
@@ -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
#7 lore raw

Re: [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