DPDK patches and discussions
 help / color / mirror / Atom feed
From: Isaac Boukris <iboukris@gmail.com>
To: Honnappa Nagarahalli <honnappa.nagarahalli@arm.com>,
	Konstantin Ananyev <konstantin.ananyev@huawei.com>
Cc: dev@dpdk.org, "Isaac Boukris" <iboukris@gmail.com>,
	"Wathsala Vithanage" <wathsala.vithanage@arm.com>,
	"Morten Brørup" <mb@smartsharesystems.com>
Subject: [PATCH v3] lib/ring: do not allow zero size ring
Date: Tue, 11 Nov 2025 08:22:52 +0200	[thread overview]
Message-ID: <20251111062253.578121-1-iboukris@gmail.com> (raw)
In-Reply-To: <20251110222827.575488-1-iboukris@gmail.com>

this might happen when for instance the ring size is read from
config, and would cause runtime crash. Do so by using the
RTE_IS_POWER_OF_2 macro.

Signed-off-by: Isaac Boukris <iboukris@gmail.com>
Acked-by: Wathsala Vithanage <wathsala.vithanage@arm.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
---

v3:
* changed to use the RTE_IS_POWER_OF_2 macro.

v2:
* move the fix to the POWEROF2 macro itself.
* fixed style in the test code.

 app/test/test_ring.c | 7 +++++++
 lib/ring/rte_ring.c  | 8 +++-----
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/app/test/test_ring.c b/app/test/test_ring.c
index ba1fec1de3..3078348b2f 100644
--- a/app/test/test_ring.c
+++ b/app/test/test_ring.c
@@ -505,6 +505,13 @@ test_ring_negative_tests(void)
 	struct rte_ring *rt = NULL;
 	unsigned int i;
 
+	/* Test zero size ring */
+	rp = test_ring_create("test_zero_size_ring", -1, 0, SOCKET_ID_ANY, 0);
+	if (rp != NULL) {
+		printf("Test failed to detect zero size ring\n");
+		goto test_fail;
+	}
+
 	/* Test with esize not a multiple of 4 */
 	rp = test_ring_create("test_bad_element_size", 23,
 				RING_SIZE + 1, SOCKET_ID_ANY, 0);
diff --git a/lib/ring/rte_ring.c b/lib/ring/rte_ring.c
index edd63aa535..f10050a1c4 100644
--- a/lib/ring/rte_ring.c
+++ b/lib/ring/rte_ring.c
@@ -25,6 +25,7 @@
 #include <rte_string_fns.h>
 #include <rte_tailq.h>
 #include <rte_telemetry.h>
+#include <rte_bitops.h>
 
 #include "rte_ring.h"
 #include "rte_ring_elem.h"
@@ -46,9 +47,6 @@ EAL_REGISTER_TAILQ(rte_ring_tailq)
 		     RING_F_MP_RTS_ENQ | RING_F_MC_RTS_DEQ |	       \
 		     RING_F_MP_HTS_ENQ | RING_F_MC_HTS_DEQ)
 
-/* true if x is a power of 2 */
-#define POWEROF2(x) ((((x)-1) & (x)) == 0)
-
 /* by default set head/tail distance as 1/8 of ring capacity */
 #define HTD_MAX_DEF	8
 
@@ -67,7 +65,7 @@ rte_ring_get_memsize_elem(unsigned int esize, unsigned int count)
 	}
 
 	/* count must be a power of 2 */
-	if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
+	if ((!RTE_IS_POWER_OF_2(count)) || (count > RTE_RING_SZ_MASK)) {
 		RING_LOG(ERR,
 			"Requested number of elements is invalid, must be power of 2, and not exceed %u",
 			RTE_RING_SZ_MASK);
@@ -227,7 +225,7 @@ rte_ring_init(struct rte_ring *r, const char *name, unsigned int count,
 		r->mask = r->size - 1;
 		r->capacity = count;
 	} else {
-		if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK)) {
+		if ((!RTE_IS_POWER_OF_2(count)) || (count > RTE_RING_SZ_MASK)) {
 			RING_LOG(ERR,
 				"Requested size is invalid, must be power of 2, and not exceed the size limit %u",
 				RTE_RING_SZ_MASK);
-- 
2.49.0


  parent reply	other threads:[~2025-11-11  6:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-10 22:28 [PATCH] " Isaac Boukris
2025-11-10 23:15 ` [PATCH v2] " Isaac Boukris
2025-11-10 23:26   ` Wathsala Vithanage
2025-11-11  0:40   ` Morten Brørup
2025-11-11  6:22 ` Isaac Boukris [this message]
2025-11-11  8:24   ` [PATCH v3] " Konstantin Ananyev
2025-11-11 12:25     ` Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251111062253.578121-1-iboukris@gmail.com \
    --to=iboukris@gmail.com \
    --cc=dev@dpdk.org \
    --cc=honnappa.nagarahalli@arm.com \
    --cc=konstantin.ananyev@huawei.com \
    --cc=mb@smartsharesystems.com \
    --cc=wathsala.vithanage@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).