[PATCH] sched: fix queue stats read for mixed subports

Alexander.Chesnokov at kaspersky.com Alexander.Chesnokov at kaspersky.com
Wed Nov 25 11:33:15 CET 2026


From: Alexander Chesnokov <Alexander.Chesnokov at kaspersky.com>

rte_sched_queue_read_stats() checks the queue ID against the total
number of queues of the port, summed over the subports. The queue
address is computed the other way: with a fixed subport capacity of
n_pipes_per_subport pipes. Subports of the same port may enable
different numbers of pipes, so a queue ID that passes the check can
decode to an index beyond the arrays of a smaller subport. The stats
copy and the clear then write outside the subport memory. The damage
surfaces later, at the first use of the overwritten data; ASAN does
not see it because the subport arrays share one allocation.

Validate the decoded queue index against the number of queues of the
subport that owns the queue ID, and reject queue IDs that decode
outside of it with -EINVAL. Queue IDs produced by
rte_sched_port_pkt_write() are valid before and after the change.
Numbering queues consecutively across subports matches this layout
only when all subports enable the same number of pipes. The stats
commands of the qos_sched example number queues this way, so on a
mixed subport layout they now get -EINVAL instead of corrupting the
memory of a smaller subport.

Add a sched_hetero_subports_autotest case with a port of two
subports: one pipe enabled on subport 0 and eight on subport 1.
Queue IDs inside the hole of the small subport and IDs beyond the
total queue count are rejected. Queue IDs of the first and the last
queue of both subports read cleanly. A packet enqueued through the
first queue of subport 1 shows up in the queue stats read for that
queue ID.

Fixes: 831104f0e84f ("sched: update queue stats read for config flexibility")
Cc: stable at dpdk.org

Signed-off-by: Alexander Chesnokov <Alexander.Chesnokov at kaspersky.com>
---

Cc: jasvinder.singh at intel.com

---
 app/test/test_sched.c | 108 ++++++++++++++++++++++++++++++++++++++++++
 lib/sched/rte_sched.c |   5 ++
 2 files changed, 113 insertions(+)

diff --git a/app/test/test_sched.c b/app/test/test_sched.c
index 96b48f1897..c73da2c7af 100644
--- a/app/test/test_sched.c
+++ b/app/test/test_sched.c
@@ -22,6 +22,13 @@ test_sched(void)
 	printf("sched not supported on Windows, skipping test\n");
 	return TEST_SKIPPED;
 }
+
+static int
+test_sched_hetero_subports(void)
+{
+	printf("sched not supported on Windows, skipping test\n");
+	return TEST_SKIPPED;
+}
 #else
 
 #include <rte_sched.h>
@@ -213,6 +220,107 @@ test_sched(void)
 	return 0;
 }
 
+/*
+ * Port with mixed subports: subport 0 enables 1 pipe, subport 1 enables
+ * 8. Queue IDs valid for the queue stats API follow the subport shift
+ * layout of rte_sched_port_pkt_write().
+ */
+static int
+test_sched_hetero_subports(void)
+{
+	struct rte_sched_port_params port_params = port_param;
+	struct rte_sched_subport_params sp[2];
+	struct rte_sched_queue_stats queue_stats;
+	struct rte_mbuf *mbufs[1];
+	struct rte_mempool *mp;
+	struct rte_sched_port *port;
+	uint16_t qlen;
+	uint32_t i;
+	int err;
+
+	port_params.rate = (uint64_t) 10000 * 1000 * 1000 / 8;
+	port_params.n_subports_per_port = 2;
+	port_params.n_pipes_per_subport = 8;
+
+	sp[0] = subport_param[0];
+	sp[0].n_pipes_per_subport_enabled = 1;
+	sp[1] = sp[0];
+	sp[1].n_pipes_per_subport_enabled = 8;
+
+	mp = create_mempool();
+	TEST_ASSERT_NOT_NULL(mp, "Error creating mempool\n");
+
+	port = rte_sched_port_config(&port_params);
+	TEST_ASSERT_NOT_NULL(port, "Error config sched port\n");
+
+	for (i = 0; i < 2; i++) {
+		err = rte_sched_subport_config(port, i, &sp[i], 0);
+		TEST_ASSERT_SUCCESS(err,
+			"Error config sched subport %u, err=%d\n", i, err);
+	}
+
+	for (i = 0; i < sp[0].n_pipes_per_subport_enabled; i++) {
+		err = rte_sched_pipe_config(port, 0, i, 0);
+		TEST_ASSERT_SUCCESS(err,
+			"Error config sched subport 0 pipe %u, err=%d\n",
+			i, err);
+	}
+
+	for (i = 0; i < sp[1].n_pipes_per_subport_enabled; i++) {
+		err = rte_sched_pipe_config(port, 1, i, 0);
+		TEST_ASSERT_SUCCESS(err,
+			"Error config sched subport 1 pipe %u, err=%d\n",
+			i, err);
+	}
+
+	/* Queue ID in the hole of the small subport 0 is rejected */
+	err = rte_sched_queue_read_stats(port, 92, &queue_stats, &qlen);
+	TEST_ASSERT_FAIL(err, "Queue ID 92 accepted, err=%d\n", err);
+
+	/* Queue ID at or above the total queue count is rejected */
+	err = rte_sched_queue_read_stats(port, 144, &queue_stats, &qlen);
+	TEST_ASSERT_FAIL(err, "Queue ID 144 accepted, err=%d\n", err);
+
+	/* First and last queue of the small subport 0 read cleanly */
+	err = rte_sched_queue_read_stats(port, 0, &queue_stats, &qlen);
+	TEST_ASSERT_SUCCESS(err, "Queue ID 0 rejected, err=%d\n", err);
+	err = rte_sched_queue_read_stats(port, 15, &queue_stats, &qlen);
+	TEST_ASSERT_SUCCESS(err, "Queue ID 15 rejected, err=%d\n", err);
+
+	/* First and last queue of the large subport 1 read cleanly */
+	err = rte_sched_queue_read_stats(port, 128, &queue_stats, &qlen);
+	TEST_ASSERT_SUCCESS(err, "Queue ID 128 rejected, err=%d\n", err);
+	err = rte_sched_queue_read_stats(port, 143, &queue_stats, &qlen);
+	TEST_ASSERT_SUCCESS(err, "Queue ID 143 rejected, err=%d\n", err);
+
+	/*
+	 * A packet through the first queue of subport 1 (queue ID 128)
+	 * shows up in the queue that the stats API reads for that ID
+	 */
+	mbufs[0] = rte_pktmbuf_alloc(mp);
+	TEST_ASSERT_NOT_NULL(mbufs[0], "Packet allocation failed\n");
+	mbufs[0]->pkt_len = 60;
+	mbufs[0]->data_len = 60;
+	rte_sched_port_pkt_write(port, mbufs[0], 1, 0, 0, 0, RTE_COLOR_GREEN);
+
+	err = rte_sched_port_enqueue(port, mbufs, 1);
+	TEST_ASSERT_EQUAL(err, 1, "Wrong enqueue, err=%d\n", err);
+
+	err = rte_sched_port_dequeue(port, mbufs, 1);
+	TEST_ASSERT_EQUAL(err, 1, "Wrong dequeue, err=%d\n", err);
+
+	err = rte_sched_queue_read_stats(port, 128, &queue_stats, &qlen);
+	TEST_ASSERT_SUCCESS(err, "Queue ID 128 rejected, err=%d\n", err);
+	TEST_ASSERT_EQUAL(queue_stats.n_pkts, 1, "Wrong queue stats\n");
+	TEST_ASSERT_EQUAL(qlen, 0, "Wrong queue length\n");
+
+	rte_sched_port_free(port);
+
+	return 0;
+}
+
 #endif /* !RTE_EXEC_ENV_WINDOWS */
 
 REGISTER_FAST_TEST(sched_autotest, NOHUGE_OK, ASAN_OK, test_sched);
+REGISTER_FAST_TEST(sched_hetero_subports_autotest, NOHUGE_OK, ASAN_OK,
+	test_sched_hetero_subports);
diff --git a/lib/sched/rte_sched.c b/lib/sched/rte_sched.c
index daa31e8b99..7b6a26879e 100644
--- a/lib/sched/rte_sched.c
+++ b/lib/sched/rte_sched.c
@@ -1780,6 +1780,11 @@ rte_sched_queue_read_stats(struct rte_sched_port *port,
 
 	s = port->subports[subport_id];
 	subport_qindex = ((1 << subport_qmask) - 1) & queue_id;
+	if (subport_qindex >= rte_sched_subport_pipe_queues(s)) {
+		SCHED_LOG(ERR,
+			"%s: Incorrect value for queue id", __func__);
+		return -EINVAL;
+	}
 	q = s->queue + subport_qindex;
 	qe = s->queue_extra + subport_qindex;
 
-- 
2.43.0



More information about the stable mailing list