Skip to contentSkip to conversation

eal: RTE_PTR_ADD/SUB char* for compiler optimizations

PATCH Superseded [email protected] 5 participants 63 messages Jan 14 – Sep 4 Page 1 of 2

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

PATCH v6 1/1 eal: RTE_PTR_ADD/SUB char* for compiler optimizations

From: Scott Mitchell <[email protected]>

Modify RTE_PTR_ADD and RTE_PTR_SUB to use char* pointer arithmetic on Clang instead of uintptr_t casts. Benefits of this approach:
- API compatibility: works for both integer and pointer inputs
- Retains simple macros: no pragmas, no _Generic
- Enables Clang optimizations: Clang can now unroll and vectorize pointer loops. GCC uses uintptr_t to avoid false positive warnings.

Example use case which benefits is __rte_raw_cksum. Performance results from cksum_perf_autotest on Intel Xeon (Cascade Lake, AVX-512) built with Clang 18.1 (TSC cycles/byte):
Block size Before After Improvement

         100      0.40     0.24        ~40%
        1500      0.50     0.06        ~8x
        9000      0.49     0.06        ~8x

---
v6:
- Make char* optimization Clang-only to avoid GCC false positive warnings
- Improve tests: use named constants instead of magic numbers
- Improve tests: use void* casts on expected values instead of uintptr_t on results

v5:
- Initial implementation with char* arithmetic for all compilers

v4:
- Used _Generic for type-based dispatch with char* casts
- Had warnings on both Clang and GCC due to _Generic type-checking all branches

---
 app/test/meson.build         |   1 +
 app/test/test_ptr_add_sub.c  | 207 +++++++++++++++++++++++++++++++++++
 lib/eal/include/rte_common.h |  14 ++-
 3 files changed, 218 insertions(+), 4 deletions(-)
 create mode 100644 app/test/test_ptr_add_sub.c
app/test/meson.build +1 -0
index efec42a6bf..80a19d65ba 100644
@@ -152,6 +152,7 @@ source_file_deps = {
     'test_power_intel_uncore.c': ['power', 'power_intel_uncore'],
     'test_power_kvm_vm.c': ['power', 'power_kvm_vm'],
     'test_prefetch.c': [],
+    'test_ptr_add_sub.c': [],
     'test_ptr_compress.c': ['ptr_compress'],
     'test_rand_perf.c': [],
     'test_rawdev.c': ['rawdev', 'bus_vdev', 'raw_skeleton'],
app/test/test_ptr_add_sub.c new file +207 -0

207 lines not on this page · raw message

lib/eal/include/rte_common.h +10 -4
index 9e7d84f929..b525110b06 100644
@@ -549,14 +549,20 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
 /*********** Macros for pointer arithmetic ********/
 
 /**
- * add a byte-value offset to a pointer
+ * add and subtract a byte-value offset from a pointer
  */
-#define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
-
+#ifdef RTE_CC_CLANG
 /**
- * subtract a byte-value offset from a pointer
+ * Clang doesn't optimize through uintptr_t, (char*) enables
+ * optimizations and doesn't generate warnings. GCC does optimize
+ * through uintptr_t but throws warnings (e.g. array-bounds) when cast to char*.
  */
+#define RTE_PTR_ADD(ptr, x) ((void *)((char *)(uintptr_t)(ptr) + (x)))
+#define RTE_PTR_SUB(ptr, x) ((void *)((char *)(uintptr_t)(ptr) - (x)))
+#else
+#define RTE_PTR_ADD(ptr, x) ((void *)((uintptr_t)(ptr) + (x)))
 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
+#endif
 
 /**
  * get the difference between two pointer values, i.e. how far apart
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw

PATCH v7 1/1 eal: RTE_PTR_ADD/SUB char* for compiler optimizations

From: Scott Mitchell <[email protected]>

Modify RTE_PTR_ADD and RTE_PTR_SUB to use char* pointer arithmetic on Clang instead of uintptr_t casts. Benefits of this approach:
- API compatibility: works for both integer and pointer inputs
- Retains simple macros: no pragmas, no _Generic
- Enables Clang optimizations: Clang can now unroll and vectorize pointer loops. GCC uses uintptr_t to avoid false positive warnings.

Example use case which benefits is __rte_raw_cksum. Performance results from cksum_perf_autotest on Intel Xeon (Cascade Lake, AVX-512) built with Clang 18.1 (TSC cycles/byte):
Block size Before After Improvement

         100      0.40     0.24        ~40%
        1500      0.50     0.06        ~8x
        9000      0.49     0.06        ~8x

---
v7:
- Fix tests: use TEST_BUFFER_SIZE macro for buffer allocation
- Fix tests: ADD then SUB same amount to avoid out-of-bounds pointer arithmetic
- All RTE_PTR_SUB tests now verify round-trip (ADD+SUB returns to original)

v6:
- Make char* optimization Clang-only to avoid GCC false positive warnings
- Improve tests: use named constants instead of magic numbers
- Improve tests: use void* casts on expected values instead of uintptr_t on results

v5:
- Initial implementation with char* arithmetic for all compilers

v4:
- Used _Generic for type-based dispatch with char* casts
- Had warnings on both Clang and GCC due to _Generic type-checking all branches

---
 app/test/meson.build         |   1 +
 app/test/test_ptr_add_sub.c  | 197 +++++++++++++++++++++++++++++++++++
 lib/eal/include/rte_common.h |  14 ++-
 3 files changed, 208 insertions(+), 4 deletions(-)
 create mode 100644 app/test/test_ptr_add_sub.c
app/test/meson.build +1 -0
index efec42a6bf..80a19d65ba 100644
@@ -152,6 +152,7 @@ source_file_deps = {
     'test_power_intel_uncore.c': ['power', 'power_intel_uncore'],
     'test_power_kvm_vm.c': ['power', 'power_kvm_vm'],
     'test_prefetch.c': [],
+    'test_ptr_add_sub.c': [],
     'test_ptr_compress.c': ['ptr_compress'],
     'test_rand_perf.c': [],
     'test_rawdev.c': ['rawdev', 'bus_vdev', 'raw_skeleton'],
app/test/test_ptr_add_sub.c new file +197 -0

197 lines not on this page · raw message

lib/eal/include/rte_common.h +10 -4
index 9e7d84f929..b525110b06 100644
@@ -549,14 +549,20 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
 /*********** Macros for pointer arithmetic ********/
 
 /**
- * add a byte-value offset to a pointer
+ * add and subtract a byte-value offset from a pointer
  */
-#define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
-
+#ifdef RTE_CC_CLANG
 /**
- * subtract a byte-value offset from a pointer
+ * Clang doesn't optimize through uintptr_t, (char*) enables
+ * optimizations and doesn't generate warnings. GCC does optimize
+ * through uintptr_t but throws warnings (e.g. array-bounds) when cast to char*.
  */
+#define RTE_PTR_ADD(ptr, x) ((void *)((char *)(uintptr_t)(ptr) + (x)))
+#define RTE_PTR_SUB(ptr, x) ((void *)((char *)(uintptr_t)(ptr) - (x)))
+#else
+#define RTE_PTR_ADD(ptr, x) ((void *)((uintptr_t)(ptr) + (x)))
 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
+#endif
 
 /**
  * get the difference between two pointer values, i.e. how far apart
Signature · 1 line
2.39.5 (Apple Git-154)
#2 lore raw
Quoted 83 lines

From: Scott Mitchell <[email protected]>

Modify RTE_PTR_ADD and RTE_PTR_SUB to use char* pointer arithmetic on Clang instead of uintptr_t casts. Benefits of this approach:
- API compatibility: works for both integer and pointer inputs
- Retains simple macros: no pragmas, no _Generic
- Enables Clang optimizations: Clang can now unroll and vectorize pointer loops. GCC uses uintptr_t to avoid false positive warnings.

Example use case which benefits is __rte_raw_cksum. Performance results from cksum_perf_autotest on Intel Xeon (Cascade Lake, AVX-512) built with Clang 18.1 (TSC cycles/byte):
Block size Before After Improvement

         100      0.40     0.24        ~40%
        1500      0.50     0.06        ~8x
        9000      0.49     0.06        ~8x

---
v7:
- Fix tests: use TEST_BUFFER_SIZE macro for buffer allocation
- Fix tests: ADD then SUB same amount to avoid out-of-bounds pointer arithmetic
- All RTE_PTR_SUB tests now verify round-trip (ADD+SUB returns to original)

v6:
- Make char* optimization Clang-only to avoid GCC false positive warnings
- Improve tests: use named constants instead of magic numbers
- Improve tests: use void* casts on expected values instead of uintptr_t on results

v5:
- Initial implementation with char* arithmetic for all compilers

v4:
- Used _Generic for type-based dispatch with char* casts
- Had warnings on both Clang and GCC due to _Generic type-checking all branches

---
 app/test/meson.build         |   1 +
 app/test/test_ptr_add_sub.c  | 197 +++++++++++++++++++++++++++++++++++
 lib/eal/include/rte_common.h |  14 ++-
 3 files changed, 208 insertions(+), 4 deletions(-)
 create mode 100644 app/test/test_ptr_add_sub.c
app/test/meson.build +1 -0
index efec42a6bf..80a19d65ba 100644
@@ -152,6 +152,7 @@ source_file_deps = {
     'test_power_intel_uncore.c': ['power', 'power_intel_uncore'],
     'test_power_kvm_vm.c': ['power', 'power_kvm_vm'],
     'test_prefetch.c': [],
+    'test_ptr_add_sub.c': [],
     'test_ptr_compress.c': ['ptr_compress'],
     'test_rand_perf.c': [],
     'test_rawdev.c': ['rawdev', 'bus_vdev', 'raw_skeleton'],
app/test/test_ptr_add_sub.c new file +15 -0
new file mode 100644
index 0000000000..2956eabeb3
@@ -0,0 +1,197 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2026 Apple Inc.
+ */
+
+#include "test.h"
+#include <stdint.h>
+#include <stdbool.h>
+
+#include <rte_common.h>
+
+/* Test constants for large integer types (16-bit and wider) */
+#define TEST_INITVAL_LARGE 0x1000
+#define TEST_INCREMENT_LARGE 100
+#define TEST_RETVAL_LARGE ((void *)0x1064)
+#define TEST_SUBVAL_LARGE ((void *)(TEST_INITVAL_LARGE -

TEST_INCREMENT_LARGE))

app/test/test_ptr_add_sub.c new file +1 -0
+

app/test/test_ptr_add_sub.c line 26

Quoted 10 lines
app/test/test_ptr_add_sub.c new file +10 -0
+/* Test constants for 8-bit types */
+#define TEST_INITVAL_SMALL 100
+#define TEST_INCREMENT_SMALL 50
+#define TEST_RETVAL_SMALL ((void *)150)
+#define TEST_SUBVAL_SMALL ((void *)50)
+
+/* Test constants for bool (1-bit) */
+#define TEST_INITVAL_BOOL 1
+#define TEST_INCREMENT_BOOL 99
+#define TEST_RETVAL_BOOL ((void *)100)

TEST_RETVAL_BOOL is incorrect if typeof(ptr) is bool. (int)(bool)(true+99) == 1

Now that _Generic is not used anymore, I suggest you remove test cases for other types than pointers.
At your preference, you may also keep pointer compatible types, i.e. 32 bit integers when building for 32-bit architecture and 64 bit integers when building for 64-bit architecture.

Quoted 220 lines
app/test/test_ptr_add_sub.c new file +11 -0
+
+/* Buffer size for pointer tests */
+#define TEST_BUFFER_SIZE (TEST_INCREMENT_LARGE + TEST_INCREMENT_SMALL
+ 256)
+
+/* Test all C11 standard integer types */
+static int
+test_ptr_add_sub_integer_types(void)
+{
+	unsigned long long ull = TEST_INITVAL_LARGE;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(ull, TEST_INCREMENT_LARGE),

TEST_RETVAL_LARGE,

app/test/test_ptr_add_sub.c new file +2 -0
+		"RTE_PTR_ADD failed for unsigned long long");
+	TEST_ASSERT_EQUAL(RTE_PTR_SUB(ull, TEST_INCREMENT_LARGE),

TEST_SUBVAL_LARGE,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_SUB failed for unsigned long long");
+
+	long long ll = TEST_INITVAL_LARGE;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(ll, TEST_INCREMENT_LARGE),

TEST_RETVAL_LARGE,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for long long");
+
+	unsigned long ul = TEST_INITVAL_LARGE;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(ul, TEST_INCREMENT_LARGE),

TEST_RETVAL_LARGE,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for unsigned long");
+
+	long l = TEST_INITVAL_LARGE;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(l, TEST_INCREMENT_LARGE),

TEST_RETVAL_LARGE,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for long");
+
+	unsigned int ui = TEST_INITVAL_LARGE;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(ui, TEST_INCREMENT_LARGE),

TEST_RETVAL_LARGE,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for unsigned int");
+
+	int i = TEST_INITVAL_LARGE;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(i, TEST_INCREMENT_LARGE),

TEST_RETVAL_LARGE,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for int");
+
+	unsigned short us = TEST_INITVAL_LARGE;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(us, TEST_INCREMENT_LARGE),

TEST_RETVAL_LARGE,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for unsigned short");
+
+	short s = TEST_INITVAL_LARGE;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(s, TEST_INCREMENT_LARGE),

TEST_RETVAL_LARGE,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for short");
+
+	unsigned char uc = TEST_INITVAL_SMALL;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(uc, TEST_INCREMENT_SMALL),

TEST_RETVAL_SMALL,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for unsigned char");
+
+	signed char sc = TEST_INITVAL_SMALL;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(sc, TEST_INCREMENT_SMALL),

TEST_RETVAL_SMALL,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for signed char");
+
+	char c = TEST_INITVAL_SMALL;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(c, TEST_INCREMENT_SMALL),

TEST_RETVAL_SMALL,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for char");
+
+	_Bool b = TEST_INITVAL_BOOL;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(b, TEST_INCREMENT_BOOL),

TEST_RETVAL_BOOL,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for _Bool");
+
+	bool b2 = TEST_INITVAL_BOOL;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(b2, TEST_INCREMENT_BOOL),

TEST_RETVAL_BOOL,

app/test/test_ptr_add_sub.c new file +17 -0
+		"RTE_PTR_ADD failed for bool");
+
+	return 0;
+}
+
+/* Test pointer types including const correctness */
+static int
+test_ptr_add_sub_pointer_types(void)
+{
+	char buffer[TEST_BUFFER_SIZE];
+	void *result;
+
+	/* Test void* and const void* */
+	void *vp = buffer;
+	result = RTE_PTR_ADD(vp, TEST_INCREMENT_LARGE);
+	result = RTE_PTR_SUB(result, TEST_INCREMENT_LARGE);
+	TEST_ASSERT_EQUAL(result, vp, "RTE_PTR_ADD/SUB failed for

void*");

app/test/test_ptr_add_sub.c new file +12 -0
+
+	const void *cvp = buffer;
+	result = RTE_PTR_ADD(cvp, TEST_INCREMENT_SMALL);
+	result = RTE_PTR_SUB(result, TEST_INCREMENT_SMALL);
+	TEST_ASSERT_EQUAL(result, (const void *)cvp,
+		"RTE_PTR_ADD/SUB failed for const void*");
+
+	/* Test char* and const char* */
+	char *cp = buffer;
+	result = RTE_PTR_ADD(cp, TEST_INCREMENT_LARGE);
+	result = RTE_PTR_SUB(result, TEST_INCREMENT_LARGE);
+	TEST_ASSERT_EQUAL(result, (void *)cp, "RTE_PTR_ADD/SUB failed for

char*");

app/test/test_ptr_add_sub.c new file +21 -0
+
+	const char *ccp = buffer;
+	result = RTE_PTR_ADD(ccp, TEST_INCREMENT_SMALL);
+	result = RTE_PTR_SUB(result, TEST_INCREMENT_SMALL);
+	TEST_ASSERT_EQUAL(result, (const void *)ccp,
+		"RTE_PTR_ADD/SUB failed for const char*");
+
+	/* Test uint32_t* and const uint32_t* */
+	uint32_t *u32p = (uint32_t *)buffer;
+	result = RTE_PTR_ADD(u32p, TEST_INCREMENT_LARGE);
+	result = RTE_PTR_SUB(result, TEST_INCREMENT_LARGE);
+	TEST_ASSERT_EQUAL(result, (void *)u32p,
+		"RTE_PTR_ADD/SUB failed for uint32_t*");
+
+	const uint32_t *cu32p = (const uint32_t *)buffer;
+	result = RTE_PTR_ADD(cu32p, TEST_INCREMENT_SMALL);
+	result = RTE_PTR_SUB(result, TEST_INCREMENT_SMALL);
+	TEST_ASSERT_EQUAL(result, (const void *)cu32p,
+		"RTE_PTR_ADD/SUB failed for const uint32_t*");
+
+	/* Verify assigning to const pointer works (adding const is safe)

*/

app/test/test_ptr_add_sub.c new file +1 -0
+	const void *result_const = RTE_PTR_ADD(cvp,

TEST_INCREMENT_LARGE);

app/test/test_ptr_add_sub.c new file +13 -0
+	result_const = RTE_PTR_SUB(result_const, TEST_INCREMENT_LARGE);
+	TEST_ASSERT_EQUAL(result_const, cvp,
+		"RTE_PTR_ADD/SUB failed when assigning to const void*");
+
+	return 0;
+}
+
+/* Test that typedefs resolve to native types correctly */
+static int
+test_ptr_add_sub_typedefs(void)
+{
+	uint64_t u64 = TEST_INITVAL_LARGE;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(u64, TEST_INCREMENT_LARGE),

TEST_RETVAL_LARGE,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for uint64_t");
+
+	uint32_t u32 = TEST_INITVAL_LARGE;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(u32, TEST_INCREMENT_LARGE),

TEST_RETVAL_LARGE,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for uint32_t");
+
+	uint16_t u16 = TEST_INITVAL_LARGE;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(u16, TEST_INCREMENT_LARGE),

TEST_RETVAL_LARGE,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for uint16_t");
+
+	uint8_t u8 = TEST_INITVAL_SMALL;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(u8, TEST_INCREMENT_SMALL),

TEST_RETVAL_SMALL,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for uint8_t");
+
+	uintptr_t uptr = TEST_INITVAL_LARGE;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(uptr, TEST_INCREMENT_LARGE),

TEST_RETVAL_LARGE,

app/test/test_ptr_add_sub.c new file +4 -0
+		"RTE_PTR_ADD failed for uintptr_t");
+
+	size_t sz = TEST_INITVAL_LARGE;
+	TEST_ASSERT_EQUAL(RTE_PTR_ADD(sz, TEST_INCREMENT_LARGE),

TEST_RETVAL_LARGE,

app/test/test_ptr_add_sub.c new file +27 -0
+		"RTE_PTR_ADD failed for size_t");
+
+	return 0;
+}
+
+/* Main test function that runs all subtests */
+static int
+test_ptr_add_sub(void)
+{
+	int ret;
+
+	ret = test_ptr_add_sub_integer_types();
+	if (ret != 0)
+		return ret;
+
+	ret = test_ptr_add_sub_pointer_types();
+	if (ret != 0)
+		return ret;
+
+	ret = test_ptr_add_sub_typedefs();
+	if (ret != 0)
+		return ret;
+
+	return 0;
+}
+
+REGISTER_FAST_TEST(ptr_add_sub_autotest, true, true,

test_ptr_add_sub);

lib/eal/include/rte_common.h +5 -4
index 9e7d84f929..b525110b06 100644
@@ -549,14 +549,20 @@ static void
 __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
 /*********** Macros for pointer arithmetic ********/
 
 /**
- * add a byte-value offset to a pointer
+ * add and subtract a byte-value offset from a pointer
  */
-#define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
-
+#ifdef RTE_CC_CLANG
 /**
- * subtract a byte-value offset from a pointer
+ * Clang doesn't optimize through uintptr_t, (char*) enables
+ * optimizations and doesn't generate warnings. GCC does optimize
+ * through uintptr_t but throws warnings (e.g. array-bounds) when cast

to char*.
*/

lib/eal/include/rte_common.h static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void) · line 565

Quoted 6 lines
lib/eal/include/rte_common.h +5 -0
+#define RTE_PTR_ADD(ptr, x) ((void *)((char *)(uintptr_t)(ptr) + (x)))
+#define RTE_PTR_SUB(ptr, x) ((void *)((char *)(uintptr_t)(ptr) - (x)))
+#else
+#define RTE_PTR_ADD(ptr, x) ((void *)((uintptr_t)(ptr) + (x)))
 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
+#endif

Please keep descriptions for RTE_PTR_ADD and RTE_PTR_SUB separate, so they show up for both macros in the API documentation:
https://doc.dpdk.org/api/rte__common_8h.html#a96c6294ef6a307980f78899fe8a5813e

Quoted 5 lines

/**
* get the difference between two pointer values, i.e. how far apart
--
2.39.5 (Apple Git-154)

#4 lore raw

PATCH v8 1/1 eal: RTE_PTR_ADD/SUB char* for compiler optimizations

In reply to [email protected]

From: Scott Mitchell <[email protected]>

Modify RTE_PTR_ADD and RTE_PTR_SUB to use char* pointer arithmetic on Clang instead of uintptr_t casts. Benefits of this approach:
- API compatibility: works for both integer and pointer inputs
- Retains simple macros: no pragmas, no _Generic
- Enables Clang optimizations: Clang can now unroll and vectorize pointer loops. GCC uses uintptr_t to avoid false positive warnings.

Example use case which benefits is __rte_raw_cksum. Performance results from cksum_perf_autotest on Intel Xeon (Cascade Lake, AVX-512) built with Clang 18.1 (TSC cycles/byte):
Block size Before After Improvement

         100      0.40     0.24        ~40%
        1500      0.50     0.06        ~8x
        9000      0.49     0.06        ~8x

---
v8:
- Remove tests for types < 32-bit (bool, char, short, uint8_t, uint16_t)
- Merge test_ptr_add_sub_typedefs() into test_ptr_add_sub_integer_types()
- Separate RTE_PTR_ADD and RTE_PTR_SUB documentation
- Move Clang/GCC implementation note from Doxygen to regular comment
- Tests verify both intermediate ADD result and SUB round-trip
- Use uintptr_t cast consistently for all integer-to-pointer conversions
- Make TEST_RETVAL calculated from TEST_INITVAL + TEST_INCREMENT

v7:
- Fix tests: use TEST_BUFFER_SIZE macro for buffer allocation
- Fix tests: ADD then SUB same amount to avoid out-of-bounds pointer arithmetic
- All RTE_PTR_SUB tests now verify round-trip (ADD+SUB returns to original)

v6:
- Make char* optimization Clang-only to avoid GCC false positive warnings
- Improve tests: use named constants instead of magic numbers
- Improve tests: use void* casts on expected values instead of uintptr_t on results

v5:
- Initial implementation with char* arithmetic for all compilers

v4:
- Used _Generic for type-based dispatch with char* casts
- Had warnings on both Clang and GCC due to _Generic type-checking all branches

---
 app/test/meson.build         |   1 +
 app/test/test_ptr_add_sub.c  | 193 +++++++++++++++++++++++++++++++++++
 lib/eal/include/rte_common.h |  15 ++-
 3 files changed, 208 insertions(+), 1 deletion(-)
 create mode 100644 app/test/test_ptr_add_sub.c
app/test/meson.build +1 -0
index efec42a6bf..80a19d65ba 100644
@@ -152,6 +152,7 @@ source_file_deps = {
     'test_power_intel_uncore.c': ['power', 'power_intel_uncore'],
     'test_power_kvm_vm.c': ['power', 'power_kvm_vm'],
     'test_prefetch.c': [],
+    'test_ptr_add_sub.c': [],
     'test_ptr_compress.c': ['ptr_compress'],
     'test_rand_perf.c': [],
     'test_rawdev.c': ['rawdev', 'bus_vdev', 'raw_skeleton'],
app/test/test_ptr_add_sub.c new file +193 -0

193 lines not on this page · raw message

lib/eal/include/rte_common.h +14 -1
index 9e7d84f929..f2935929f7 100644
@@ -551,12 +551,25 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
 /**
  * add a byte-value offset to a pointer
  */
-#define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
+#ifdef RTE_CC_CLANG
+/*
+ * Clang doesn't optimize through uintptr_t, (char*) enables
+ * optimizations and doesn't generate warnings. GCC does optimize
+ * through uintptr_t but throws warnings (e.g. array-bounds) when cast to char*.
+ */
+#define RTE_PTR_ADD(ptr, x) ((void *)((char *)(uintptr_t)(ptr) + (x)))
+#else
+#define RTE_PTR_ADD(ptr, x) ((void *)((uintptr_t)(ptr) + (x)))
+#endif
 
 /**
  * subtract a byte-value offset from a pointer
  */
+#ifdef RTE_CC_CLANG
+#define RTE_PTR_SUB(ptr, x) ((void *)((char *)(uintptr_t)(ptr) - (x)))
+#else
 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
+#endif
 
 /**
  * get the difference between two pointer values, i.e. how far apart
Signature · 1 line
2.39.5 (Apple Git-154)
#5 lore raw
Quoted 13 lines · On Fri, 16 Jan 2026 15:19:07 -0800 [email protected] wrote:

From: Scott Mitchell <[email protected]>

Modify RTE_PTR_ADD and RTE_PTR_SUB to use char* pointer arithmetic on Clang instead of uintptr_t casts. Benefits of this approach:
- API compatibility: works for both integer and pointer inputs
- Retains simple macros: no pragmas, no _Generic
- Enables Clang optimizations: Clang can now unroll and vectorize pointer loops. GCC uses uintptr_t to avoid false positive warnings.

Example use case which benefits is __rte_raw_cksum. Performance results from cksum_perf_autotest on Intel Xeon (Cascade Lake, AVX-512) built with Clang 18.1 (TSC cycles/byte):
Block size Before After Improvement

Quoted 7 lines
         100      0.40     0.24        ~40%
        1500      0.50     0.06        ~8x
        9000      0.49     0.06        ~8x

---

Looks good to me and not too complex.

AI code review had some additional comments.

## Patch Review: eal: RTE_PTR_ADD/SUB char* for compiler optimizations (v8)

### ✅ PASS Items

**Commit Message - Subject Line:**
- Length: ~52 characters (✓ under 60 limit)
- Prefix: eal: is correct for EAL changes
- No trailing period
- Imperative mood

**Commit Message - Body:**
- Well-documented rationale with clear performance data
- Does NOT start with "It" ✓
- Performance table shows concrete improvements (~40% to ~8x)
- Line wrapping appears within 75-character limit

**Tags:**
- Signed-off-by: present with real name and valid email ✓
- Tag order is correct

**SPDX/License (new file test_ptr_add_sub.c):**
- SPDX identifier on first line ✓
- Copyright follows SPDX ✓
- Blank line after copyright ✓
- BSD-3-Clause appropriate for app/test ✓

**Tests:**
- Comprehensive test coverage for integer and pointer types ✓
- Uses TEST_ASSERT_EQUAL macros ✓
- Uses REGISTER_FAST_TEST infrastructure ✓
- Tests both ADD and SUB with round-trip verification ✓
- Named constants (TEST_INITVAL, TEST_INCREMENT) instead of magic numbers ✓

**Code Quality:**
- Macro implementation is well-documented with explanatory comment
- Conditional compilation (#ifdef RTE_CC_CLANG) is clean
- No forbidden terminology

---

### ⚠️ WARNING Items

**1. Include Order (test_ptr_add_sub.c lines 161-164):**

```c
#include "test.h"
#include <stdint.h>

#include <rte_common.h>
```
Per AGENTS.md, order should be: (1) system includes, (2) EAL includes, (3) misc DPDK includes, (4) application-specific. Should be:
```c
#include <stdint.h>

#include <rte_common.h>

#include "test.h"
```

**2. Subject Line Contains Underscores:**
The subject eal: RTE_PTR_ADD/SUB char* for compiler optimizations includes underscores (code identifiers after the colon). Per AGENTS.md: "Underscores after the colon (indicates code in subject)" is flagged. Consider:

```
eal: use char pointer arithmetic for Clang optimizations
```

**3. Missing Release Notes:**
This is a notable performance optimization (~8x improvement in some cases). Per AGENTS.md, "significant changes" should update release notes in doc/guides/rel_notes/. Consider adding a note about the checksum performance improvement.

**4. Doxygen Documentation:**
The existing Doxygen comments for RTE_PTR_ADD and RTE_PTR_SUB remain minimal:

```c
/**
 * add a byte-value offset to a pointer
 */
```

Consider adding @param and @return documentation, especially noting that ptr can be either a pointer or integer type.

---

### ℹ️ INFO Items (Minor Suggestions)

**1. Variable Declaration Style:**
In test_ptr_add_sub_integer_types(), variables are declared at point-of-use (C99 style), which is acceptable per AGENTS.md. However, void *result is declared at function start while type-specific variables are inline—this mixing is fine but slightly inconsistent.

**2. Test Structure:**
The tests don't use unit_test_suite_runner infrastructure (they use direct function calls in test_ptr_add_sub()). While functional, AGENTS.md suggests using unit_test_suite_runner for "proper integration with the DPDK test framework." This is optional since REGISTER_FAST_TEST is used correctly.

**3. Comment Placement:**
The implementation comment explaining Clang vs GCC behavior is placed between the Doxygen comment and the macro. This works, but having implementation notes inside a /* ... */ block separate from Doxygen is good practice (which this patch does correctly).

---

### Summary

| Category | Status |
|----------|--------|
| Commit message format | ✅ Pass (minor subject line concern) |
| SPDX/License | ✅ Pass |
| Code style | ⚠️ Include order should be fixed |
| Tests | ✅ Pass - comprehensive |
| Documentation | ⚠️ Consider release notes |
| Compilation | Should verify independently |

**Overall Assessment:** This is a well-crafted v8 patch with solid test coverage and clear performance benefits. The main actionable items are fixing the include order and considering release notes for this optimization. The subject line concern about underscores is minor since RTE_PTR_ADD/SUB are canonical macro names in DPDK.

#6 lore raw
Quoted 3 lines

Looks good to me and not too complex.

AI code review had some additional comments.

I fixed the import order, the build failure (due to recent changes in REGISTER_FAST_TEST), and submitted v9. Please let me know if you feel any of the other items are blocking and must be resolved. I held off on release notes for now as I would also like to get
https://patches.dpdk.org/project/dpdk/patch/[email protected]/
merged which makes unaligned typedefs safe for general use and direct pointer arithmetic (less risk of PTR ADD/SUB macros confusing compilers as Morten also found).

#7 lore raw

PATCH v9 1/1 eal: RTE_PTR_ADD/SUB char* for compiler optimizations

In reply to [email protected]

From: Scott Mitchell <[email protected]>

Modify RTE_PTR_ADD and RTE_PTR_SUB to use char* pointer arithmetic on Clang instead of uintptr_t casts. Benefits of this approach:
- API compatibility: works for both integer and pointer inputs
- Retains simple macros: no pragmas, no _Generic
- Enables Clang optimizations: Clang can now unroll and vectorize pointer loops. GCC uses uintptr_t to avoid false positive warnings.

Example use case which benefits is __rte_raw_cksum. Performance results from cksum_perf_autotest on Intel Xeon (Cascade Lake, AVX-512) built with Clang 18.1 (TSC cycles/byte):
Block size Before After Improvement

         100      0.40     0.24        ~40%
        1500      0.50     0.06        ~8x
        9000      0.49     0.06        ~8x

---
v9:
- Fix include order: system includes, then DPDK includes, then application includes
- Use NOHUGE_OK and ASAN_OK constants in REGISTER_FAST_TEST (instead of true, true)

v8:
- Remove tests for types < 32-bit (bool, char, short, uint8_t, uint16_t)
- Merge test_ptr_add_sub_typedefs() into test_ptr_add_sub_integer_types()
- Separate RTE_PTR_ADD and RTE_PTR_SUB documentation
- Move Clang/GCC implementation note from Doxygen to regular comment
- Tests verify both intermediate ADD result and SUB round-trip
- Use uintptr_t cast consistently for all integer-to-pointer conversions
- Make TEST_RETVAL calculated from TEST_INITVAL + TEST_INCREMENT

v7:
- Fix tests: use TEST_BUFFER_SIZE macro for buffer allocation
- Fix tests: ADD then SUB same amount to avoid out-of-bounds pointer arithmetic
- All RTE_PTR_SUB tests now verify round-trip (ADD+SUB returns to original)

v6:
- Make char* optimization Clang-only to avoid GCC false positive warnings
- Improve tests: use named constants instead of magic numbers
- Improve tests: use void* casts on expected values instead of uintptr_t on results

v5:
- Initial implementation with char* arithmetic for all compilers

v4:
- Used _Generic for type-based dispatch with char* casts
- Had warnings on both Clang and GCC due to _Generic type-checking all branches

---
 app/test/meson.build         |   1 +
 app/test/test_ptr_add_sub.c  | 194 +++++++++++++++++++++++++++++++++++
 lib/eal/include/rte_common.h |  15 ++-
 3 files changed, 209 insertions(+), 1 deletion(-)
 create mode 100644 app/test/test_ptr_add_sub.c
app/test/meson.build +1 -0
index efec42a6bf..80a19d65ba 100644
@@ -152,6 +152,7 @@ source_file_deps = {
     'test_power_intel_uncore.c': ['power', 'power_intel_uncore'],
     'test_power_kvm_vm.c': ['power', 'power_kvm_vm'],
     'test_prefetch.c': [],
+    'test_ptr_add_sub.c': [],
     'test_ptr_compress.c': ['ptr_compress'],
     'test_rand_perf.c': [],
     'test_rawdev.c': ['rawdev', 'bus_vdev', 'raw_skeleton'],
app/test/test_ptr_add_sub.c new file +194 -0

194 lines not on this page · raw message

lib/eal/include/rte_common.h +14 -1
index 9e7d84f929..f2935929f7 100644
@@ -551,12 +551,25 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
 /**
  * add a byte-value offset to a pointer
  */
-#define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
+#ifdef RTE_CC_CLANG
+/*
+ * Clang doesn't optimize through uintptr_t, (char*) enables
+ * optimizations and doesn't generate warnings. GCC does optimize
+ * through uintptr_t but throws warnings (e.g. array-bounds) when cast to char*.
+ */
+#define RTE_PTR_ADD(ptr, x) ((void *)((char *)(uintptr_t)(ptr) + (x)))
+#else
+#define RTE_PTR_ADD(ptr, x) ((void *)((uintptr_t)(ptr) + (x)))
+#endif
 
 /**
  * subtract a byte-value offset from a pointer
  */
+#ifdef RTE_CC_CLANG
+#define RTE_PTR_SUB(ptr, x) ((void *)((char *)(uintptr_t)(ptr) - (x)))
+#else
 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
+#endif
 
 /**
  * get the difference between two pointer values, i.e. how far apart
Signature · 1 line
2.39.5 (Apple Git-154)
#8 lore raw
Quoted 7 lines · On Sat, 17 Jan 2026 13:03:22 -0800 [email protected] wrote:
app/test/test_ptr_add_sub.c new file +7 -0
+/* Main test function that runs all subtests */
+static int
+test_ptr_add_sub(void)
+{
+	int ret;
+
+	ret = test_ptr_add_sub_integer_types();

app/test/test_ptr_add_sub.c line 193

Quoted 10 lines
app/test/test_ptr_add_sub.c new file +10 -0
+	if (ret != 0)
+		return ret;
+
+	ret = test_ptr_add_sub_pointer_types();
+	if (ret != 0)
+		return ret;
+
+	return 0;
+}
+

Ok, but if you add more tests consider using the unit_test_runner() in test.h. Mainly because it makes it easier to pinpoint which subtest is failing in a big test.

#9 lore raw
Quoted 3 lines

Ok, but if you add more tests consider using the unit_test_runner() in test.h. Mainly because it makes it easier to pinpoint which subtest is failing in a big test.

Sounds good, v10 coming soon.

#11 lore raw

PATCH v10 1/1 eal: RTE_PTR_ADD/SUB char* for compiler optimizations

In reply to [email protected]

From: Scott Mitchell <[email protected]>

Modify RTE_PTR_ADD and RTE_PTR_SUB to use char* pointer arithmetic on Clang instead of uintptr_t casts. Benefits of this approach:
- API compatibility: works for both integer and pointer inputs
- Retains simple macros: no pragmas, no _Generic
- Enables Clang optimizations: Clang can now unroll and vectorize pointer loops. GCC uses uintptr_t to avoid false positive warnings.

Example use case which benefits is __rte_raw_cksum. Performance results from cksum_perf_autotest on Intel Xeon (Cascade Lake, AVX-512) built with Clang 18.1 (TSC cycles/byte):
Block size Before After Improvement

         100      0.40     0.24        ~40%
        1500      0.50     0.06        ~8x
        9000      0.49     0.06        ~8x

---
v10:
- Use unit_test_suite_runner for easier subtest failure identification

v9:
- Fix include order: system includes, then DPDK includes, then application includes
- Use NOHUGE_OK and ASAN_OK constants in REGISTER_FAST_TEST (instead of true, true)

v8:
- Remove tests for types < 32-bit (bool, char, short, uint8_t, uint16_t)
- Merge test_ptr_add_sub_typedefs() into test_ptr_add_sub_integer_types()
- Separate RTE_PTR_ADD and RTE_PTR_SUB documentation
- Move Clang/GCC implementation note from Doxygen to regular comment
- Tests verify both intermediate ADD result and SUB round-trip
- Use uintptr_t cast consistently for all integer-to-pointer conversions
- Make TEST_RETVAL calculated from TEST_INITVAL + TEST_INCREMENT

v7:
- Fix tests: use TEST_BUFFER_SIZE macro for buffer allocation
- Fix tests: ADD then SUB same amount to avoid out-of-bounds pointer arithmetic
- All RTE_PTR_SUB tests now verify round-trip (ADD+SUB returns to original)

v6:
- Make char* optimization Clang-only to avoid GCC false positive warnings
- Improve tests: use named constants instead of magic numbers
- Improve tests: use void* casts on expected values instead of uintptr_t on results

v5:
- Initial implementation with char* arithmetic for all compilers

v4:
- Used _Generic for type-based dispatch with char* casts
- Had warnings on both Clang and GCC due to _Generic type-checking all branches

---
 app/test/meson.build         |   1 +
 app/test/test_ptr_add_sub.c  | 195 +++++++++++++++++++++++++++++++++++
 lib/eal/include/rte_common.h |  15 ++-
 3 files changed, 210 insertions(+), 1 deletion(-)
 create mode 100644 app/test/test_ptr_add_sub.c
app/test/meson.build +1 -0
index efec42a6bf..80a19d65ba 100644
@@ -152,6 +152,7 @@ source_file_deps = {
     'test_power_intel_uncore.c': ['power', 'power_intel_uncore'],
     'test_power_kvm_vm.c': ['power', 'power_kvm_vm'],
     'test_prefetch.c': [],
+    'test_ptr_add_sub.c': [],
     'test_ptr_compress.c': ['ptr_compress'],
     'test_rand_perf.c': [],
     'test_rawdev.c': ['rawdev', 'bus_vdev', 'raw_skeleton'],
app/test/test_ptr_add_sub.c new file +195 -0

195 lines not on this page · raw message

lib/eal/include/rte_common.h +14 -1
index 9e7d84f929..f2935929f7 100644
@@ -551,12 +551,25 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
 /**
  * add a byte-value offset to a pointer
  */
-#define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
+#ifdef RTE_CC_CLANG
+/*
+ * Clang doesn't optimize through uintptr_t, (char*) enables
+ * optimizations and doesn't generate warnings. GCC does optimize
+ * through uintptr_t but throws warnings (e.g. array-bounds) when cast to char*.
+ */
+#define RTE_PTR_ADD(ptr, x) ((void *)((char *)(uintptr_t)(ptr) + (x)))
+#else
+#define RTE_PTR_ADD(ptr, x) ((void *)((uintptr_t)(ptr) + (x)))
+#endif
 
 /**
  * subtract a byte-value offset from a pointer
  */
+#ifdef RTE_CC_CLANG
+#define RTE_PTR_SUB(ptr, x) ((void *)((char *)(uintptr_t)(ptr) - (x)))
+#else
 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
+#endif
 
 /**
  * get the difference between two pointer values, i.e. how far apart
Signature · 1 line
2.39.5 (Apple Git-154)
Acked-by on v10 1/1
#12 lore raw
Quoted 13 lines

From: Scott Mitchell <[email protected]>

Modify RTE_PTR_ADD and RTE_PTR_SUB to use char* pointer arithmetic on Clang instead of uintptr_t casts. Benefits of this approach:
- API compatibility: works for both integer and pointer inputs
- Retains simple macros: no pragmas, no _Generic
- Enables Clang optimizations: Clang can now unroll and vectorize pointer loops. GCC uses uintptr_t to avoid false positive warnings.

Example use case which benefits is __rte_raw_cksum. Performance results from cksum_perf_autotest on Intel Xeon (Cascade Lake, AVX-512) built with Clang 18.1 (TSC cycles/byte):
Block size Before After Improvement

Quoted 5 lines
         100      0.40     0.24        ~40%
        1500      0.50     0.06        ~8x
        9000      0.49     0.06        ~8x
#13 lore raw
In reply to [email protected]

UBSan flagged a legitimate warning [1] with this approach. There are existing use cases in the codebase that pass ptr as NULL.

Before this change: NULL (0) is cast to uintptr_t. The addition happens in the integer domain, which is valid defined behavior. After this change: NULL is cast to char*. Performing arithmetic on a NULL pointer ((char*)0 + x) is Undefined Behavior in C, even if the offset is 0.

An example of this occurring is shown in [2].

We have a few options to resolve this:

1. Add runtime NULL checks into RTE_PTR_ADD -> Undesirable due to runtime overhead in a hot-path macro.
2. Clarify that the caller is responsible for NULL checks -> Require audit & update call sites in DPDK (and potentially breaking external applications) to ensure NULL is never passed.
3. Revisit the API -> Existing API is convenient but limits type safety, discards qualifiers (in caller code), and obscures pointer provenance from the compiler (preventing optimizations). Here are options to split the API:

Option 3.1: Distinct APIs per use case (Split Const/Non-Const) This splits the macro into three explicit variants.

/* Add a byte-value offset to an integer address */
#define RTE_INT_PTR_ADD(intptr, x) ((typeof(intptr))((uintptr_t)(intptr) + (x)))

/* Add a byte-value offset to a non-const pointer */ #define RTE_PTR_ADD(ptr, x) ((void *)((char *)(ptr) + (x)))

/* Add a byte-value offset to a const pointer */
#define RTE_CONST_PTR_ADD(ptr, x) ((const void *)((const char *)(ptr) + (x)))

Pros:
Simpler Implementation: No suppressions or casting under the hood.

Cons:
Pointer API fragmentation: Callers must manually select the right macro. Dependency Cascade: Dependent macros like RTE_PTR_ALIGN_CEIL rely on RTE_PTR_ADD. Splitting RTE_PTR_ADD would also split RTE_PTR_ALIGN_CEIL (into const and non-const variants). If we change CEIL then RTE_PTR_ALIGN_FLOOR should be considered for consistency too.

Option 3.2: Pointer API and Integer-Address API (Preferred) This separates integer use cases but keeps a single unified macro for pointers that preserves const correctness automatically.

/* Add a byte-value offset to an integer address */
#define RTE_INT_PTR_ADD(intptr, x) ((typeof(intptr))((uintptr_t)(intptr) + (x)))

/* Add a byte-value offset to a pointer. Returns the same type as the input (preserving const/volatile). */
#define RTE_PTR_ADD(ptr, x) \

  (__extension__ ({ \
    __rte_diagnostic_push \
    __rte_diagnostic_ignored_wcast_qual \
    typeof(ptr) __rte_ptr_add_result = \
          (typeof(ptr))((char *)(ptr) + (x)); \
    __rte_diagnostic_pop \
    __rte_ptr_add_result; \
}))

Pros:
Preserves Optimization: Uses char* arithmetic, allowing the compiler to track pointer provenance.
Better Ergonomics: Callers don't need to distinguish between const and non-const.
Existing Precedent: Following the existing pattern of RTE_PTR_ALIGN which returns typeof(ptr).

Cons:
Requires warning suppressions: Suppressions are localized within the macro and don't affect user code, and the typeof() return ensures type safety at the call site.

I will submit a draft for v11 that implements option 3.2. If people like this approach we can discuss if/how breaking into multiple patches makes sense.

[1] ../lib/eal/common/eal_common_memory.c:335:9: runtime error:
applying zero offset to null pointer

[2] github.com/DPDK/dpdk/blob/main/lib/eal/common/eal_common_memory.c#L335

start = msl->base_va; // void* (can be NULL)
end = RTE_PTR_ADD(start, msl->len); // UB if start is NULL if (addr >= start && addr < end)

    break;
#14 lore raw
Quoted 3 lines

I will submit a draft for v11 that implements option 3.2. If people like this approach we can discuss if/how breaking into multiple patches makes sense.

Compilers assume memory is aligned when casting to a specific type which isn't guaranteed when using these APIs. Returning void* prevents compilers from making these optimizations and is a safer choice. I will submit a v12 with APIs returning void* (I think I can still preserve qualifiers).

#15 lore raw

PATCH v11 1/1 eal: RTE_PTR_ADD/SUB API improvements

In reply to [email protected]

From: Scott Mitchell <[email protected]>

RTE_PTR_ADD and RTE_PTR_SUB APIs have a few limitations:
1. ptr cast to uintptr_t drops pointer provenance and prevents compiler optimizations
2. return cast to void* removes type safety and discards qualifiers (const, volatile)
3. Accepts both "pointers" and "integers as pointers" which overloads the use case and constrains the implementation to address other challenges.

This patch splits the API on two dimensions:
1. pointer types
2. integer types that represent pointers

This split allows addressing each of the challenges above and provides distinct APIs for the distinct use cases. Examples:
1. Clang is able to optimize and improve __rte_raw_cksum (which uses RTE_PTR_ADD) by ~40% (100 bytes) to ~8x (1.5k bytes) TSC cycles/byte.
2. Refactoring discovered cases that dropped qualifiers (volatile) which are likely bugs and warrant more explicit cast/handling.

---
v11:
- Split API into PTR and INT_PTR variants, update all usage of PTR API for new APIs.

v10:
- Use unit_test_suite_runner for easier subtest failure identification

v9:
- Fix include order: system includes, then DPDK includes, then application includes
- Use NOHUGE_OK and ASAN_OK constants in REGISTER_FAST_TEST (instead of true, true)

v8:
- Remove tests for types < 32-bit (bool, char, short, uint8_t, uint16_t)
- Merge test_ptr_add_sub_typedefs() into test_ptr_add_sub_integer_types()
- Separate RTE_PTR_ADD and RTE_PTR_SUB documentation
- Move Clang/GCC implementation note from Doxygen to regular comment
- Tests verify both intermediate ADD result and SUB round-trip
- Use uintptr_t cast consistently for all integer-to-pointer conversions
- Make TEST_RETVAL calculated from TEST_INITVAL + TEST_INCREMENT

v7:
- Fix tests: use TEST_BUFFER_SIZE macro for buffer allocation
- Fix tests: ADD then SUB same amount to avoid out-of-bounds pointer arithmetic
- All RTE_PTR_SUB tests now verify round-trip (ADD+SUB returns to original)

v6:
- Make char* optimization Clang-only to avoid GCC false positive warnings
- Improve tests: use named constants instead of magic numbers
- Improve tests: use void* casts on expected values instead of uintptr_t on results

v5:
- Initial implementation with char* arithmetic for all compilers

v4:
- Used _Generic for type-based dispatch with char* casts
- Had warnings on both Clang and GCC due to _Generic type-checking all branches

---
 app/test-eventdev/test_perf_common.h        |   7 +-
 app/test-pmd/cmdline_flow.c                 |   6 +-
 app/test-pmd/csumonly.c                     |   2 +-
 app/test/meson.build                        |   1 +
 app/test/test_common.c                      |  20 +-
 app/test/test_ptr_add_sub.c                 | 197 ++++++++++++++++++++
 drivers/bus/pci/linux/pci_vfio.c            |   2 +-
 drivers/common/cnxk/cnxk_security.c         |   2 +-
 drivers/common/cnxk/roc_cpt_debug.c         |   4 +-
 drivers/common/cnxk/roc_nix_bpf.c           |   2 +-
 drivers/common/cnxk/roc_nix_inl.h           |   4 +-
 drivers/common/cnxk/roc_nix_inl_dp.h        |   8 +-
 drivers/common/cnxk/roc_platform.h          |   6 +-
 drivers/common/cnxk/roc_se.c                |   2 +-
 drivers/common/cpt/cpt_ucode_asym.h         |  12 +-
 drivers/common/mlx5/linux/mlx5_nl.c         |  14 +-
 drivers/compress/mlx5/mlx5_compress.c       |   2 +-
 drivers/crypto/cnxk/cn9k_cryptodev_ops.c    |   2 +-
 drivers/crypto/cnxk/cnxk_cryptodev_ops.c    |   2 +-
 drivers/crypto/mlx5/mlx5_crypto_gcm.c       |   6 +-
 drivers/crypto/mlx5/mlx5_crypto_xts.c       |   2 +-
 drivers/crypto/octeontx/otx_cryptodev_ops.c |   2 +-
 drivers/dma/idxd/idxd_internal.h            |   1 +
 drivers/dma/idxd/idxd_pci.c                 |  11 +-
 drivers/dma/odm/odm_dmadev.c                |   4 +-
 drivers/mempool/bucket/rte_mempool_bucket.c |   7 +-
 drivers/ml/cnxk/cn10k_ml_model.c            |   2 +-
 drivers/ml/cnxk/cn10k_ml_ops.c              |   2 +-
 drivers/ml/cnxk/cnxk_ml_ops.c               |   2 +-
 drivers/net/ark/ark_ethdev.c                |   4 +-
 drivers/net/cxgbe/cxgbe_ethdev.c            |   6 +-
 drivers/net/gve/base/gve_adminq.c           |  12 +-
 drivers/net/intel/ice/ice_tm.c              |   4 +-
 drivers/net/intel/ixgbe/ixgbe_rxtx.c        |   2 +-
 drivers/net/mlx5/mlx5_flow.c                |   4 +-
 drivers/net/mlx5/mlx5_flow_hw.c             |   4 +-
 drivers/net/mlx5/mlx5_tx.h                  |   4 +-
 drivers/net/mlx5/mlx5_txpp.c                |   4 +-
 drivers/net/sfc/sfc_mae.c                   |  10 +-
 drivers/net/virtio/virtio_rxtx.c            |   2 +-
 lib/eal/common/eal_common_memory.c          |  19 +-
 lib/eal/common/eal_common_options.c         |   2 +-
 lib/eal/common/malloc_elem.h                |   4 +-
 lib/eal/common/malloc_heap.c                |   6 +-
 lib/eal/include/rte_common.h                | 174 ++++++++++++++++-
 lib/ethdev/rte_ethdev.c                     |   6 +-
 lib/graph/graph_populate.c                  |   4 +-
 lib/graph/graph_stats.c                     |   2 +-
 lib/graph/rte_graph.h                       |   4 +-
 lib/graph/rte_graph_model_mcore_dispatch.c  |   2 +-
 lib/latencystats/rte_latencystats.c         |   2 +-
 lib/mbuf/rte_mbuf.c                         |   2 +-
 lib/member/rte_xxh64_avx512.h               |   6 +-
 lib/pdcp/pdcp_entity.h                      |   8 +-
 lib/reorder/rte_reorder.c                   |   2 +-
 55 files changed, 502 insertions(+), 131 deletions(-)
 create mode 100644 app/test/test_ptr_add_sub.c
app/test-eventdev/test_perf_common.h +4 -3
index 365464f269..2b0434b844 100644
@@ -141,7 +141,7 @@ perf_mark_fwd_latency(enum evt_prod_type prod_type, struct rte_event *const ev)
 
 			pe = rte_pktmbuf_mtod(m, struct perf_elt *);
 		} else {
-			pe = RTE_PTR_ADD(op->asym->modex.result.data,
+			pe = RTE_PTR_ADD((void *)op->asym->modex.result.data,
 					 op->asym->modex.result.length);
 		}
 		pe->timestamp = rte_get_timer_cycles();
@@ -179,7 +179,8 @@ perf_elt_from_vec_get(struct rte_event_vector *vec)
 		m = cop->sym->m_dst == NULL ? cop->sym->m_src : cop->sym->m_dst;
 		return rte_pktmbuf_mtod(m, struct perf_elt *);
 	} else {
-		return RTE_PTR_ADD(cop->asym->modex.result.data, cop->asym->modex.result.length);
+		return RTE_PTR_ADD((void *)cop->asym->modex.result.data,
+				   cop->asym->modex.result.length);
 	}
 }
 
@@ -297,7 +298,7 @@ perf_process_last_stage_latency(struct rte_mempool *const pool, enum evt_prod_ty
 			to_free_in_bulk = m;
 			pe = rte_pktmbuf_mtod(m, struct perf_elt *);
 		} else {
-			pe = RTE_PTR_ADD(op->asym->modex.result.data,
+			pe = RTE_PTR_ADD((void *)op->asym->modex.result.data,
 					 op->asym->modex.result.length);
 			to_free_in_bulk = op->asym->modex.result.data;
 		}
app/test-pmd/cmdline_flow.c +3 -3
index ebc036b14b..01a45fb807 100644
@@ -12468,7 +12468,7 @@ parse_meter_color(struct context *ctx, const struct token *token,
 
 		if (!arg)
 			return -1;
-		*(int *)RTE_PTR_ADD(action->conf, arg->offset) = i;
+		*(int *)RTE_PTR_ADD((void *)action->conf, arg->offset) = i;
 	} else {
 		((struct rte_flow_item_meter_color *)
 			ctx->object)->color = (enum rte_color)i;
@@ -13343,7 +13343,7 @@ indirect_action_flow_conf_create(const struct buffer *in)
 		goto end;
 	indlst_conf->id = in->args.vc.attr.group;
 	indlst_conf->conf_num = in->args.vc.actions_n - 1;
-	indlst_conf->actions = RTE_PTR_ADD(indlst_conf, base);
+	indlst_conf->actions = RTE_PTR_ADD((void *)indlst_conf, base);
 	ret = rte_flow_conv(RTE_FLOW_CONV_OP_ACTIONS, indlst_conf->actions,
 			    len, src, NULL);
 	if (ret <= 0) {
@@ -13351,7 +13351,7 @@ indirect_action_flow_conf_create(const struct buffer *in)
 		indlst_conf = NULL;
 		goto end;
 	}
-	indlst_conf->conf = RTE_PTR_ADD(indlst_conf, base + len);
+	indlst_conf->conf = (const void **)RTE_PTR_ADD((void *)indlst_conf, base + len);
 	for (i = 0; i < indlst_conf->conf_num; i++)
 		indlst_conf->conf[i] = indlst_conf->actions[i].conf;
 	SLIST_INSERT_HEAD(&indlst_conf_head, indlst_conf, next);
app/test-pmd/csumonly.c +1 -1
index c841651756..24866e563f 100644
@@ -488,7 +488,7 @@ get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype)
 		return _htons(RTE_ETHER_TYPE_IPV6);
 	default:
 		ethertype = eth_hdr->ether_type;
-		vlan_hdr = RTE_PTR_ADD(eth_hdr, offsetof(struct rte_ether_hdr, ether_type));
+		vlan_hdr = RTE_PTR_ADD((void *)eth_hdr, offsetof(struct rte_ether_hdr, ether_type));
 		max_vlans = vlan_hdr + MAX_VLAN_HEADERS;
 		while ((ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
 				ethertype == _htons(RTE_ETHER_TYPE_QINQ)) &&
app/test/meson.build +1 -0
index f4d04a6e42..aa56fc4297 100644
@@ -152,6 +152,7 @@ source_file_deps = {
     'test_power_intel_uncore.c': ['power', 'power_intel_uncore'],
     'test_power_kvm_vm.c': ['power', 'power_kvm_vm'],
     'test_prefetch.c': [],
+    'test_ptr_add_sub.c': [],
     'test_ptr_compress.c': ['ptr_compress'],
     'test_rand_perf.c': [],
     'test_rawdev.c': ['rawdev', 'bus_vdev', 'raw_skeleton'],
app/test/test_common.c +10 -10
index 3e1c7df0c1..cc0ce50bbc 100644
@@ -37,10 +37,10 @@ test_macros(int __rte_unused unused_parm)
 	RTE_SWAP(smaller, bigger);
 	RTE_TEST_ASSERT(smaller == BIGGER && bigger == SMALLER,
 		"RTE_SWAP");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF), BIGGER,
-		"RTE_PTR_ADD");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF), SMALLER,
-		"RTE_PTR_SUB");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR_ADD(SMALLER, PTR_DIFF), BIGGER,
+		"RTE_INT_PTR_ADD");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR_SUB(BIGGER, PTR_DIFF), SMALLER,
+		"RTE_INT_PTR_SUB");
 	RTE_TEST_ASSERT_EQUAL(RTE_PTR_DIFF(BIGGER, SMALLER), PTR_DIFF,
 		"RTE_PTR_DIFF");
 	RTE_TEST_ASSERT_EQUAL(RTE_MAX(SMALLER, BIGGER), BIGGER,
@@ -188,18 +188,18 @@ test_align(void)
 			if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
-			val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
+			val = RTE_INT_PTR_ALIGN_FLOOR((uintptr_t) i, p);
 			if (ERROR_FLOOR(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_FLOOR", i, p);
 
 			val = RTE_ALIGN_FLOOR(i, p);
 			if (ERROR_FLOOR(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
 			/* align ceiling */
-			val = RTE_PTR_ALIGN((uintptr_t) i, p);
+			val = RTE_INT_PTR_ALIGN((uintptr_t) i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN", i, p);
 
 			val = RTE_ALIGN(i, p);
 			if (ERROR_CEIL(val, i, p))
@@ -209,9 +209,9 @@ test_align(void)
 			if (ERROR_CEIL(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
 
-			val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
+			val = RTE_INT_PTR_ALIGN_CEIL((uintptr_t)i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_CEIL", i, p);
 
 			/* by this point we know that val is aligned to p */
 			if (!rte_is_aligned((void*)(uintptr_t) val, p))
app/test/test_ptr_add_sub.c new file +197 -0

197 lines not on this page · raw message

drivers/bus/pci/linux/pci_vfio.c +1 -1
index 242f567ed7..004cb3cf91 100644
@@ -672,7 +672,7 @@ pci_vfio_info_cap(struct vfio_region_info *info, int cap)
 
 	offset = info->cap_offset;
 	while (offset != 0) {
-		h = RTE_PTR_ADD(info, offset);
+		h = RTE_PTR_ADD((void *)info, offset);
 		if (h->id == cap)
 			return h;
 		offset = h->next;
drivers/common/cnxk/cnxk_security.c +1 -1
index 600098ae1c..4e2105aaa6 100644
@@ -965,7 +965,7 @@ on_fill_ipsec_common_sa(struct rte_security_ipsec_xform *ipsec,
 		else if (crypto_xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM) {
 			ccm_flag = 0x07 & ~ROC_CPT_AES_CCM_CTR_LEN;
 			*common_sa->iv.gcm.nonce = ccm_flag;
-			memcpy(PLT_PTR_ADD(common_sa->iv.gcm.nonce, 1), &ipsec->salt, 3);
+			memcpy(PLT_PTR_ADD(&common_sa->iv.gcm.nonce[0], 1), &ipsec->salt, 3);
 		}
 		cipher_key = crypto_xform->aead.key.data;
 		cipher_key_len = crypto_xform->aead.key.length;
drivers/common/cnxk/roc_cpt_debug.c +2 -2
index 28aedf088e..ff27a2ede9 100644
@@ -56,7 +56,7 @@ cpt_cnxk_parse_hdr_dump(FILE *file, const struct cpt_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*32B */
 	offset = cpth->w2.ptr_offset;
 	offset = (((offset - 1) & 0x7) + 1) * 32;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD((void *)cpth, offset);
 
 	if (cpth->w0.num_frags > 0) {
 		cpt_dump(file, "CPT Fraginfo_0 \t%p:", frag_info);
@@ -162,7 +162,7 @@ cpt_cn10k_parse_hdr_dump(FILE *file, const struct cpt_cn10k_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*8B */
 	offset = cpth->w2.fi_offset;
 	offset = (((offset - 1) & 0x1f) + 1) * 8;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD((void *)cpth, offset);
 
 	cpt_dump(file, "CPT Fraginfo \t0x%p:", frag_info);
 
drivers/common/cnxk/roc_nix_bpf.c +1 -1
index 98c9855a5b..985b318fdd 100644
@@ -160,7 +160,7 @@ nix_precolor_conv_table_write(struct roc_nix *roc_nix, uint64_t val,
 	struct nix *nix = roc_nix_to_nix_priv(roc_nix);
 	int64_t *addr;
 
-	addr = PLT_PTR_ADD(nix->base, off);
+	addr = PLT_PTR_ADD((void *)nix->base, off);
 	plt_write64(val, addr);
 }
 
drivers/common/cnxk/roc_nix_inl.h +2 -2
index 7970ac2258..9fe1417756 100644
@@ -59,7 +59,7 @@ roc_nix_inl_on_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_PTR_ADD((void *)base, off);
 }
 
 static inline struct roc_ie_on_outb_sa *
@@ -67,7 +67,7 @@ roc_nix_inl_on_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_PTR_ADD((void *)base, off);
 }
 
 static inline void *
drivers/common/cnxk/roc_nix_inl_dp.h +4 -4
index eb101db179..d7737cb7ca 100644
@@ -49,7 +49,7 @@ roc_nix_inl_ot_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_PTR_ADD((void *)base, off);
 }
 
 static inline struct roc_ot_ipsec_outb_sa *
@@ -57,7 +57,7 @@ roc_nix_inl_ot_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_PTR_ADD((void *)base, off);
 }
 
 static inline void *
@@ -77,7 +77,7 @@ roc_nix_inl_ow_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_PTR_ADD((void *)base, off);
 }
 
 static inline struct roc_ow_ipsec_outb_sa *
@@ -85,7 +85,7 @@ roc_nix_inl_ow_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_PTR_ADD((void *)base, off);
 }
 
 static inline void *
drivers/common/cnxk/roc_platform.h +4 -2
index e22a50d47a..1bee272dde 100644
@@ -47,6 +47,8 @@
 #define PLT_PTR_ADD		 RTE_PTR_ADD
 #define PLT_PTR_SUB		 RTE_PTR_SUB
 #define PLT_PTR_DIFF		 RTE_PTR_DIFF
+#define PLT_INT_PTR_ADD		 RTE_INT_PTR_ADD
+#define PLT_INT_PTR_SUB		 RTE_INT_PTR_SUB
 #define PLT_MAX_RXTX_INTR_VEC_ID RTE_MAX_RXTX_INTR_VEC_ID
 #define PLT_INTR_VEC_RXTX_OFFSET RTE_INTR_VEC_RXTX_OFFSET
 #define PLT_MIN			 RTE_MIN
@@ -84,8 +86,8 @@
 #define PLT_U16_CAST(val) ((uint16_t)(val))
 
 /* Add / Sub pointer with scalar and cast to uint64_t */
-#define PLT_PTR_ADD_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_PTR_ADD(__ptr, __x))
-#define PLT_PTR_SUB_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_PTR_SUB(__ptr, __x))
+#define PLT_PTR_ADD_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_INT_PTR_ADD(__ptr, __x))
+#define PLT_PTR_SUB_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_INT_PTR_SUB(__ptr, __x))
 
 /** Divide ceil */
 #define PLT_DIV_CEIL(x, y)			\
drivers/common/cnxk/roc_se.c +1 -1
index f71832ff3a..445e2a66dc 100644
@@ -635,7 +635,7 @@ roc_se_ctx_init(struct roc_se_ctx *roc_se_ctx)
 	ctx_len = PLT_ALIGN_CEIL(ctx_len, 8);
 
 	/* Skip w0 for swap */
-	uc_ctx = PLT_PTR_ADD(ctx, sizeof(ctx->w0));
+	uc_ctx = PLT_PTR_ADD((void *)ctx, sizeof(ctx->w0));
 	for (i = 0; i < (ctx_len / 8); i++)
 		uc_ctx[i] = plt_cpu_to_be_64(((uint64_t *)uc_ctx)[i]);
 
drivers/common/cpt/cpt_ucode_asym.h +6 -6
index 5122378ec7..85654600c8 100644
@@ -251,7 +251,7 @@ cpt_modex_prep(struct asym_op_params *modex_params,
 	total_key_len = mod_len + exp_len;
 
 	/* Input buffer */
-	dptr = RTE_PTR_ADD(req, sizeof(struct cpt_request_info));
+	dptr = RTE_PTR_ADD((void *)req, sizeof(struct cpt_request_info));
 	memcpy(dptr, mod->modulus.data, total_key_len);
 	dptr += total_key_len;
 	memcpy(dptr, mod_op.base.data, base_len);
@@ -314,7 +314,7 @@ cpt_rsa_prep(struct asym_op_params *rsa_params,
 	total_key_len  = mod_len + exp_len;
 
 	/* Input buffer */
-	dptr = RTE_PTR_ADD(req, sizeof(struct cpt_request_info));
+	dptr = RTE_PTR_ADD((void *)req, sizeof(struct cpt_request_info));
 	memcpy(dptr, rsa->n.data, total_key_len);
 	dptr += total_key_len;
 
@@ -399,7 +399,7 @@ cpt_rsa_crt_prep(struct asym_op_params *rsa_params,
 	total_key_len = p_len + q_len + dP_len + dQ_len + qInv_len;
 
 	/* Input buffer */
-	dptr = RTE_PTR_ADD(req, sizeof(struct cpt_request_info));
+	dptr = RTE_PTR_ADD((void *)req, sizeof(struct cpt_request_info));
 	memcpy(dptr, rsa->qt.q.data, total_key_len);
 	dptr += total_key_len;
 
@@ -665,7 +665,7 @@ cpt_ecdsa_sign_prep(struct rte_crypto_ecdsa_op_param *ecdsa,
 	pk_offset = prime_len - pkey_len;
 
 	/* Input buffer */
-	dptr = RTE_PTR_ADD(req, sizeof(struct cpt_request_info));
+	dptr = RTE_PTR_ADD((void *)req, sizeof(struct cpt_request_info));
 
 	/*
 	 * Set dlen = sum(sizeof(fpm address), ROUNDUP8(scalar len, input len),
@@ -773,7 +773,7 @@ cpt_ecdsa_verify_prep(struct rte_crypto_ecdsa_op_param *ecdsa,
 	s_offset = prime_len - s_len;
 
 	/* Input buffer */
-	dptr = RTE_PTR_ADD(req, sizeof(struct cpt_request_info));
+	dptr = RTE_PTR_ADD((void *)req, sizeof(struct cpt_request_info));
 
 	/*
 	 * Set dlen = sum(sizeof(fpm address), ROUNDUP8(message len),
@@ -884,7 +884,7 @@ cpt_ecpm_prep(struct rte_crypto_ecpm_op_param *ecpm,
 	prime_len = ec_grp[curveid].prime.length;
 
 	/* Input buffer */
-	dptr = RTE_PTR_ADD(req, sizeof(struct cpt_request_info));
+	dptr = RTE_PTR_ADD((void *)req, sizeof(struct cpt_request_info));
 
 	p_align = RTE_ALIGN_CEIL(prime_len, 8);
 	scalar_align = RTE_ALIGN_CEIL(ecpm->scalar.length, 8);
drivers/common/mlx5/linux/mlx5_nl.c +7 -7
index d53543a113..65f553c20b 100644
@@ -1654,8 +1654,8 @@ static int
 mlx5_nl_family_id_cb(struct nlmsghdr *nh, void *arg)
 {
 
-	struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
-	struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
+	struct nlattr *tail = RTE_PTR_ADD((void *)nh, nh->nlmsg_len);
+	struct nlattr *nla = RTE_PTR_ADD((void *)nh, NLMSG_ALIGN(sizeof(*nh)) +
 					NLMSG_ALIGN(sizeof(struct genlmsghdr)));
 
 	for (; nla->nla_len && nla < tail;
@@ -1752,8 +1752,8 @@ mlx5_nl_roce_cb(struct nlmsghdr *nh, void *arg)
 
 	int ret = -EINVAL;
 	int *enable = arg;
-	struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
-	struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
+	struct nlattr *tail = RTE_PTR_ADD((void *)nh, nh->nlmsg_len);
+	struct nlattr *nla = RTE_PTR_ADD((void *)nh, NLMSG_ALIGN(sizeof(*nh)) +
 					NLMSG_ALIGN(sizeof(struct genlmsghdr)));
 
 	while (nla->nla_len && nla < tail) {
@@ -2049,8 +2049,8 @@ mlx5_nl_esw_multiport_cb(struct nlmsghdr *nh, void *arg)
 
 	int ret = -EINVAL;
 	int *enable = arg;
-	struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
-	struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
+	struct nlattr *tail = RTE_PTR_ADD((void *)nh, nh->nlmsg_len);
+	struct nlattr *nla = RTE_PTR_ADD((void *)nh, NLMSG_ALIGN(sizeof(*nh)) +
 					NLMSG_ALIGN(sizeof(struct genlmsghdr)));
 
 	while (nla->nla_len && nla < tail) {
@@ -2261,7 +2261,7 @@ mlx5_nl_get_mtu_bounds_cb(struct nlmsghdr *nh, void *arg)
 	struct mlx5_mtu *out = arg;
 
 	while (off < nh->nlmsg_len) {
-		struct rtattr *ra = RTE_PTR_ADD(nh, off);
+		struct rtattr *ra = RTE_PTR_ADD((void *)nh, off);
 		uint32_t *payload;
 
 		switch (ra->rta_type) {
drivers/compress/mlx5/mlx5_compress.c +1 -1
index e5325c6150..cfac5dc6c1 100644
@@ -626,7 +626,7 @@ mlx5_compress_cqe_err_handle(struct mlx5_compress_qp *qp,
 								    qp->qp.wqes;
 	volatile union mlx5_gga_compress_opaque *opaq = qp->opaque_mr.addr;
 
-	volatile uint32_t *synd_word = RTE_PTR_ADD(cqe, MLX5_ERROR_CQE_SYNDROME_OFFSET);
+	volatile uint32_t *synd_word = RTE_PTR_ADD((void *)cqe, MLX5_ERROR_CQE_SYNDROME_OFFSET);
 	switch (*synd_word) {
 	case MLX5_GGA_COMP_OUT_OF_SPACE_SYNDROME_BE:
 		op->status = RTE_COMP_OP_STATUS_OUT_OF_SPACE_TERMINATED;
drivers/crypto/cnxk/cn9k_cryptodev_ops.c +1 -1
index 5551e40cb0..d1c68436ac 100644
@@ -528,7 +528,7 @@ cn9k_cpt_sec_post_process(struct rte_crypto_op *cop,
 		hdr = rte_pktmbuf_mtod(m, struct roc_ie_on_inb_hdr *);
 
 		if (likely(m->next == NULL)) {
-			ip = PLT_PTR_ADD(hdr, ROC_IE_ON_INB_RPTR_HDR);
+			ip = PLT_PTR_ADD((void *)hdr, ROC_IE_ON_INB_RPTR_HDR);
 		} else {
 			ip = (struct rte_ipv4_hdr *)hdr;
 			hdr = infl_req->mdata;
drivers/crypto/cnxk/cnxk_cryptodev_ops.c +1 -1
index 370f311dd3..07abeb5a52 100644
@@ -523,7 +523,7 @@ cnxk_cpt_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
 			goto exit;
 		}
 
-		qp->pend_q.req_queue = PLT_PTR_ADD(
+		qp->pend_q.req_queue = PLT_PTR_ADD((void *)
 			qp->lf.cq_vaddr, ROC_CPT_CQ_ENTRY_SIZE_UNIT << qp->lf.cq_entry_size);
 	}
 
drivers/crypto/mlx5/mlx5_crypto_gcm.c +3 -3
index 89f32c7722..2c06183416 100644
@@ -370,7 +370,7 @@ mlx5_crypto_gcm_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
 		goto err;
 	}
 	qp->opaque_addr = qp->mr.addr;
-	qp->klm_array = RTE_PTR_ADD(qp->opaque_addr, opaq_size);
+	qp->klm_array = RTE_PTR_ADD((void *)qp->opaque_addr, opaq_size);
 	/*
 	 * Triple the CQ size as UMR QP which contains UMR and SEND_EN WQE
 	 * will share this CQ .
@@ -733,8 +733,8 @@ static __rte_always_inline void
 mlx5_crypto_gcm_build_send_en(struct mlx5_crypto_qp *qp)
 {
 	uint32_t wqe_offset = (qp->umr_pi & (qp->umr_wqbbs - 1)) * MLX5_SEND_WQE_BB;
-	struct mlx5_wqe_cseg *cs = RTE_PTR_ADD(qp->umr_qp_obj.wqes, wqe_offset);
-	struct mlx5_wqe_qseg *qs = RTE_PTR_ADD(cs, sizeof(struct mlx5_wqe_cseg));
+	struct mlx5_wqe_cseg *cs = RTE_PTR_ADD((void *)qp->umr_qp_obj.wqes, wqe_offset);
+	struct mlx5_wqe_qseg *qs = RTE_PTR_ADD((void *)cs, sizeof(struct mlx5_wqe_cseg));
 
 	cs->opcode = rte_cpu_to_be_32(MLX5_OPCODE_SEND_EN | ((uint32_t)qp->umr_pi << 8));
 	cs->sq_ds = rte_cpu_to_be_32((qp->umr_qp_obj.qp->id << 8) | 2);
drivers/crypto/mlx5/mlx5_crypto_xts.c +1 -1
index 1c914caa85..a9f8ab2e4d 100644
@@ -291,7 +291,7 @@ mlx5_crypto_xts_wqe_set(struct mlx5_crypto_priv *priv,
 	qp->db_pi += priv->umr_wqe_stride;
 	/* Set RDMA_WRITE WQE. */
 	cseg = RTE_PTR_ADD(cseg, priv->umr_wqe_size);
-	klms = RTE_PTR_ADD(cseg, sizeof(struct mlx5_rdma_write_wqe));
+	klms = RTE_PTR_ADD((void *)cseg, sizeof(struct mlx5_rdma_write_wqe));
 	if (!ipl) {
 		klm_n = mlx5_crypto_xts_klms_set(qp, op, op->sym->m_src, klms);
 		if (unlikely(klm_n == 0))
drivers/crypto/octeontx/otx_cryptodev_ops.c +1 -1
index 88657f49cc..dde216787f 100644
@@ -446,7 +446,7 @@ otx_cpt_enq_single_asym(struct cpt_instance *instance,
 	cop[1] = (uintptr_t)op;
 	cop[2] = cop[3] = 0ULL;
 
-	params.req = RTE_PTR_ADD(cop, 4 * sizeof(uintptr_t));
+	params.req = RTE_PTR_ADD((void *)cop, 4 * sizeof(uintptr_t));
 	params.req->op = cop;
 
 	/* Adjust meta_buf by crypto_op data  and request_info struct */
drivers/dma/idxd/idxd_internal.h +1 -0
index b80a113455..064aeb45f3 100644
@@ -56,6 +56,7 @@ struct idxd_dmadev {
 	unsigned short batch_start; /* start+size == write pointer for hdls/desc */
 	unsigned short batch_size;
 
+	/* FIXME: cast drops volatile propagation that should happen to idxd_dmadev.portal */
 	void *portal; /* address to write the batch descriptor */
 
 	struct idxd_completion *batch_comp_ring;
drivers/dma/idxd/idxd_pci.c +6 -5
index 214f6f22d5..8109ac14b2 100644
@@ -59,7 +59,7 @@ idxd_pci_dev_command(struct idxd_dmadev *idxd, enum rte_idxd_cmds command)
 	return err_code;
 }
 
-static uint32_t *
+static volatile uint32_t *
 idxd_get_wq_cfg(struct idxd_pci_common *pci, uint8_t wq_idx)
 {
 	return RTE_PTR_ADD(pci->wq_regs_base,
@@ -69,7 +69,7 @@ idxd_get_wq_cfg(struct idxd_pci_common *pci, uint8_t wq_idx)
 static int
 idxd_is_wq_enabled(struct idxd_dmadev *idxd)
 {
-	uint32_t state = idxd_get_wq_cfg(idxd->u.pci, idxd->qid)[wq_state_idx];
+	volatile uint32_t state = idxd_get_wq_cfg(idxd->u.pci, idxd->qid)[wq_state_idx];
 	return ((state >> WQ_STATE_SHIFT) & WQ_STATE_MASK) == 0x1;
 }
 
@@ -205,9 +205,9 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,
 	pci->regs = dev->mem_resource[0].addr;
 	version = pci->regs->version;
 	grp_offset = (uint16_t)pci->regs->offsets[0];
-	pci->grp_regs = RTE_PTR_ADD(pci->regs, grp_offset * 0x100);
+	pci->grp_regs = RTE_PTR_ADD((volatile void *)pci->regs, grp_offset * 0x100);
 	wq_offset = (uint16_t)(pci->regs->offsets[0] >> 16);
-	pci->wq_regs_base = RTE_PTR_ADD(pci->regs, wq_offset * 0x100);
+	pci->wq_regs_base = RTE_PTR_ADD((volatile void *)pci->regs, wq_offset * 0x100);
 	pci->portals = dev->mem_resource[2].addr;
 	pci->wq_cfg_sz = (pci->regs->wqcap >> 24) & 0x0F;
 
@@ -395,7 +395,8 @@ idxd_dmadev_probe_pci(struct rte_pci_driver *drv, struct rte_pci_device *dev)
 		/* add the queue number to each device name */
 		snprintf(qname, sizeof(qname), "%s-q%d", name, qid);
 		idxd.qid = qid;
-		idxd.portal = RTE_PTR_ADD(idxd.u.pci->portals,
+		/* FIXME: cast drops volatile propagation to idxd_dmadev.portal */
+		idxd.portal = RTE_PTR_ADD((void *)idxd.u.pci->portals,
 				qid * IDXD_PORTAL_SIZE);
 		if (idxd_is_wq_enabled(&idxd))
 			IDXD_PMD_ERR("Error, WQ %u seems enabled", qid);
drivers/dma/odm/odm_dmadev.c +2 -2
index a2f4ed9a8e..6e64998230 100644
@@ -434,7 +434,7 @@ odm_dmadev_completed(void *dev_private, uint16_t vchan, const uint16_t nb_cpls,
 	}
 
 	for (cnt = 0; cnt < nb_cpls; cnt++) {
-		cmpl_ptr = RTE_PTR_ADD(base_addr, cring_head * sizeof(cmpl));
+		cmpl_ptr = RTE_PTR_ADD((void *)base_addr, cring_head * sizeof(cmpl));
 		cmpl.u = rte_atomic_load_explicit((RTE_ATOMIC(uint32_t) *)cmpl_ptr,
 						  rte_memory_order_relaxed);
 		if (!cmpl.s.valid)
@@ -501,7 +501,7 @@ odm_dmadev_completed_status(void *dev_private, uint16_t vchan, const uint16_t nb
 #endif
 
 	for (cnt = 0; cnt < nb_cpls; cnt++) {
-		cmpl_ptr = RTE_PTR_ADD(base_addr, cring_head * sizeof(cmpl));
+		cmpl_ptr = RTE_PTR_ADD((void *)base_addr, cring_head * sizeof(cmpl));
 		cmpl.u = rte_atomic_load_explicit((RTE_ATOMIC(uint32_t) *)cmpl_ptr,
 						  rte_memory_order_relaxed);
 		if (!cmpl.s.valid)
drivers/mempool/bucket/rte_mempool_bucket.c +3 -4
index c0b480bfc7..6fee10176b 100644
@@ -376,8 +376,8 @@ count_underfilled_buckets(struct rte_mempool *mp,
 	uintptr_t align;
 	uint8_t *iter;
 
-	align = (uintptr_t)RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz) -
-		(uintptr_t)memhdr->addr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz),
+			     memhdr->addr);
 
 	for (iter = (uint8_t *)memhdr->addr + align;
 	     iter < (uint8_t *)memhdr->addr + memhdr->len;
@@ -602,8 +602,7 @@ bucket_populate(struct rte_mempool *mp, unsigned int max_objs,
 		return -EINVAL;
 
 	bucket_page_sz = rte_align32pow2(bd->bucket_mem_size);
-	align = RTE_PTR_ALIGN_CEIL((uintptr_t)vaddr, bucket_page_sz) -
-		(uintptr_t)vaddr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, bucket_page_sz), vaddr);
 
 	bucket_header_sz = bd->header_size - mp->header_size;
 	if (iova != RTE_BAD_IOVA)
drivers/ml/cnxk/cn10k_ml_model.c +1 -1
index 12a2dda800..cb47027ee6 100644
@@ -555,7 +555,7 @@ cn10k_ml_model_info_set(struct cnxk_ml_dev *cnxk_mldev, struct cnxk_ml_model *mo
 
 	metadata = &model->glow.metadata;
 	info = PLT_PTR_CAST(model->info);
-	input = PLT_PTR_ADD(info, sizeof(struct rte_ml_model_info));
+	input = PLT_PTR_ADD((void *)info, sizeof(struct rte_ml_model_info));
 	output = PLT_PTR_ADD(input, ML_CNXK_MODEL_MAX_INPUT_OUTPUT * sizeof(struct rte_ml_io_info));
 
 	/* Set model info */
drivers/ml/cnxk/cn10k_ml_ops.c +1 -1
index b30af7c7a4..8a3fcf78eb 100644
@@ -702,7 +702,7 @@ cn10k_ml_layer_load(void *device, uint16_t model_id, const char *layer_name, uin
 	layer->glow.req = PLT_PTR_ADD(mz->addr, layer_object_size + layer_scratch_size);
 
 	/* Reset burst and sync stats */
-	layer->glow.burst_xstats = PLT_PTR_ADD(
+	layer->glow.burst_xstats = PLT_PTR_ADD((void *)
 		layer->glow.req, PLT_ALIGN_CEIL(sizeof(struct cnxk_ml_req), ML_CN10K_ALIGN_SIZE));
 	for (qp_id = 0; qp_id < cnxk_mldev->mldev->data->nb_queue_pairs + 1; qp_id++) {
 		layer->glow.burst_xstats[qp_id].hw_latency_tot = 0;
drivers/ml/cnxk/cnxk_ml_ops.c +1 -1
index 9958945670..ba2c4909f9 100644
@@ -1211,7 +1211,7 @@ cnxk_ml_model_load(struct rte_ml_dev *dev, struct rte_ml_model_params *params, u
 	model->cnxk_mldev = cnxk_mldev;
 	model->type = type;
 	model->model_id = lcl_model_id;
-	model->info = PLT_PTR_ADD(
+	model->info = PLT_PTR_ADD((void *)
 		model, PLT_ALIGN_CEIL(sizeof(struct cnxk_ml_model), dev_info.align_size));
 	dev->data->models[lcl_model_id] = model;
 
drivers/net/ark/ark_ethdev.c +2 -2
index 8b25ed948f..9c34597c81 100644
@@ -713,8 +713,8 @@ ark_dev_info_get(struct rte_eth_dev *dev,
 		     struct rte_eth_dev_info *dev_info)
 {
 	struct ark_adapter *ark = dev->data->dev_private;
-	struct ark_mpu_t *tx_mpu = RTE_PTR_ADD(ark->bar0, ARK_MPU_TX_BASE);
-	struct ark_mpu_t *rx_mpu = RTE_PTR_ADD(ark->bar0, ARK_MPU_RX_BASE);
+	struct ark_mpu_t *tx_mpu = RTE_PTR_ADD((void *)ark->bar0, ARK_MPU_TX_BASE);
+	struct ark_mpu_t *rx_mpu = RTE_PTR_ADD((void *)ark->bar0, ARK_MPU_RX_BASE);
 	uint16_t ports = ark->num_ports;
 
 	dev_info->max_rx_pktlen = ARK_RX_MAX_PKT_LEN;
drivers/net/cxgbe/cxgbe_ethdev.c +3 -3
index 0c337a6cc8..f405d60449 100644
@@ -940,7 +940,7 @@ static int cxgbe_dev_xstats(struct rte_eth_dev *dev,
 				 sizeof(xstats_names[count].name),
 				 "%s", xstats_str[i].name);
 		if (xstats != NULL) {
-			stats_ptr = RTE_PTR_ADD(&ps,
+			stats_ptr = RTE_PTR_ADD((void *)&ps,
 						xstats_str[i].offset);
 			xstats[count].value = *stats_ptr;
 			xstats[count].id = count;
@@ -959,7 +959,7 @@ static int cxgbe_dev_xstats(struct rte_eth_dev *dev,
 					 "tx_q%u_%s",
 					 qid, xstats_str[i].name);
 			if (xstats != NULL) {
-				stats_ptr = RTE_PTR_ADD(&txq->stats,
+				stats_ptr = RTE_PTR_ADD((void *)&txq->stats,
 							xstats_str[i].offset);
 				xstats[count].value = *stats_ptr;
 				xstats[count].id = count;
@@ -979,7 +979,7 @@ static int cxgbe_dev_xstats(struct rte_eth_dev *dev,
 					 "rx_q%u_%s",
 					 qid, xstats_str[i].name);
 			if (xstats != NULL) {
-				stats_ptr = RTE_PTR_ADD(&rxq->stats,
+				stats_ptr = RTE_PTR_ADD((void *)&rxq->stats,
 							xstats_str[i].offset);
 				xstats[count].value = *stats_ptr;
 				xstats[count].id = count;
drivers/net/gve/base/gve_adminq.c +6 -6
index bc1759f006..0180baefc2 100644
@@ -75,7 +75,7 @@ void gve_parse_device_option(struct gve_priv *priv,
 			PMD_DRV_LOG(WARNING,
 				    GVE_DEVICE_OPTION_TOO_BIG_FMT, "GQI RDA");
 		}
-		*dev_op_gqi_rda = RTE_PTR_ADD(option, sizeof(*option));
+		*dev_op_gqi_rda = RTE_PTR_ADD((void *)option, sizeof(*option));
 		break;
 	case GVE_DEV_OPT_ID_GQI_QPL:
 		if (option_length < sizeof(**dev_op_gqi_qpl) ||
@@ -91,7 +91,7 @@ void gve_parse_device_option(struct gve_priv *priv,
 			PMD_DRV_LOG(WARNING,
 				    GVE_DEVICE_OPTION_TOO_BIG_FMT, "GQI QPL");
 		}
-		*dev_op_gqi_qpl = RTE_PTR_ADD(option, sizeof(*option));
+		*dev_op_gqi_qpl = RTE_PTR_ADD((void *)option, sizeof(*option));
 		break;
 	case GVE_DEV_OPT_ID_DQO_RDA:
 		if (option_length < sizeof(**dev_op_dqo_rda) ||
@@ -107,7 +107,7 @@ void gve_parse_device_option(struct gve_priv *priv,
 			PMD_DRV_LOG(WARNING,
 				    GVE_DEVICE_OPTION_TOO_BIG_FMT, "DQO RDA");
 		}
-		*dev_op_dqo_rda = RTE_PTR_ADD(option, sizeof(*option));
+		*dev_op_dqo_rda = RTE_PTR_ADD((void *)option, sizeof(*option));
 		break;
 	case GVE_DEV_OPT_ID_MODIFY_RING:
 		/* Min ring size bound is optional. */
@@ -127,7 +127,7 @@ void gve_parse_device_option(struct gve_priv *priv,
 				    GVE_DEVICE_OPTION_TOO_BIG_FMT,
 				    "Modify Ring");
 		}
-		*dev_op_modify_ring = RTE_PTR_ADD(option, sizeof(*option));
+		*dev_op_modify_ring = RTE_PTR_ADD((void *)option, sizeof(*option));
 
 		/* Min ring size included; set the minimum ring size. */
 		if (option_length == sizeof(**dev_op_modify_ring))
@@ -149,7 +149,7 @@ void gve_parse_device_option(struct gve_priv *priv,
 				    GVE_DEVICE_OPTION_TOO_BIG_FMT,
 				    "Jumbo Frames");
 		}
-		*dev_op_jumbo_frames = RTE_PTR_ADD(option, sizeof(*option));
+		*dev_op_jumbo_frames = RTE_PTR_ADD((void *)option, sizeof(*option));
 		break;
 	default:
 		/* If we don't recognize the option just continue
@@ -175,7 +175,7 @@ gve_process_device_options(struct gve_priv *priv,
 	int i;
 
 	/* The options struct directly follows the device descriptor. */
-	dev_opt = RTE_PTR_ADD(descriptor, sizeof(*descriptor));
+	dev_opt = RTE_PTR_ADD((void *)descriptor, sizeof(*descriptor));
 	for (i = 0; i < num_options; i++) {
 		struct gve_device_option *next_opt;
 
drivers/net/intel/ice/ice_tm.c +2 -2
index f2d8e12181..f9d86ab800 100644
@@ -456,7 +456,7 @@ ice_tm_node_add(struct rte_eth_dev *dev, uint32_t node_id,
 		tm_node->parent = NULL;
 		tm_node->reference_count = 0;
 		tm_node->shaper_profile = shaper_profile;
-		tm_node->children = RTE_PTR_ADD(tm_node, sizeof(struct ice_tm_node));
+		tm_node->children = RTE_PTR_ADD((void *)tm_node, sizeof(struct ice_tm_node));
 		tm_node->params = *params;
 		pf->tm_conf.root = tm_node;
 		return 0;
@@ -518,7 +518,7 @@ ice_tm_node_add(struct rte_eth_dev *dev, uint32_t node_id,
 	tm_node->parent = parent_node;
 	tm_node->level = level_id;
 	tm_node->shaper_profile = shaper_profile;
-	tm_node->children = RTE_PTR_ADD(tm_node, sizeof(struct ice_tm_node));
+	tm_node->children = RTE_PTR_ADD((void *)tm_node, sizeof(struct ice_tm_node));
 	tm_node->parent->children[tm_node->parent->reference_count++] = tm_node;
 	tm_node->params = *params;
 
drivers/net/intel/ixgbe/ixgbe_rxtx.c +1 -1
index a7583c178a..92fa9ddd96 100644
@@ -2852,7 +2852,7 @@ ixgbe_dev_tx_queue_setup(struct rte_eth_dev *dev,
 				 RTE_CACHE_LINE_SIZE, socket_id);
 	if (txq == NULL)
 		return -ENOMEM;
-	txq->ctx_cache = RTE_PTR_ADD(txq, sizeof(struct ci_tx_queue));
+	txq->ctx_cache = RTE_PTR_ADD((void *)txq, sizeof(struct ci_tx_queue));
 
 	/*
 	 * Allocate TX ring hardware descriptors. A memzone large enough to
drivers/net/mlx5/mlx5_flow.c +2 -2
index 2c48f1b01b..a40f5ef47a 100644
@@ -8326,9 +8326,9 @@ flow_alloc_thread_workspace(void)
 		DRV_LOG(ERR, "Failed to allocate flow workspace memory.");
 		return NULL;
 	}
-	data->rss_desc.queue = RTE_PTR_ADD(data, data_size);
+	data->rss_desc.queue = RTE_PTR_ADD((void *)data, data_size);
 #ifdef HAVE_MLX5_HWS_SUPPORT
-	data->table = RTE_PTR_ADD(data->rss_desc.queue, rss_queue_array_size);
+	data->table = RTE_PTR_ADD((void *)data->rss_desc.queue, rss_queue_array_size);
 #endif
 	return data;
 }
drivers/net/mlx5/mlx5_flow_hw.c +2 -2
index c41b99746f..15599f47e3 100644
@@ -126,7 +126,7 @@ mlx5_flow_hw_aux(uint16_t port_id, struct rte_flow_hw *flow)
 		if (rte_flow_template_table_resizable(port_id, &table->cfg.attr)) {
 			size_t offset = sizeof(struct rte_flow_hw) + mlx5dr_rule_get_handle_size();
 
-			return RTE_PTR_ADD(flow, offset);
+			return RTE_PTR_ADD((void *)flow, offset);
 		} else {
 			return &table->flow_aux[flow->idx - 1];
 		}
@@ -8123,7 +8123,7 @@ __flow_hw_actions_template_create(struct rte_eth_dev *dev,
 	at->src_off = RTE_PTR_ADD(at->dr_off,
 				  RTE_ALIGN(act_num * sizeof(*at->dr_off), 16));
 	memcpy(at->src_off, src_off, act_num * sizeof(at->src_off[0]));
-	at->orig_actions = RTE_PTR_ADD(at->src_off,
+	at->orig_actions = RTE_PTR_ADD((void *)at->src_off,
 				       RTE_ALIGN(act_num * sizeof(*at->src_off), 16));
 	orig_act_len = rte_flow_conv(RTE_FLOW_CONV_OP_ACTIONS, at->orig_actions, orig_act_len,
 				     actions, error);
drivers/net/mlx5/mlx5_tx.h +2 -2
index 16307206e2..3f7bc3c607 100644
@@ -897,7 +897,7 @@ mlx5_tx_qseg_init(struct mlx5_txq_data *restrict txq,
 {
 	struct mlx5_wqe_qseg *qs;
 
-	qs = RTE_PTR_ADD(wqe, MLX5_WSEG_SIZE);
+	qs = RTE_PTR_ADD((void *)wqe, MLX5_WSEG_SIZE);
 	qs->max_index = rte_cpu_to_be_32(wci);
 	qs->qpn_cqn = rte_cpu_to_be_32(txq->sh->txpp.clock_queue.cq_obj.cq->id);
 	qs->reserved0 = RTE_BE32(0);
@@ -928,7 +928,7 @@ mlx5_tx_wseg_init(struct mlx5_txq_data *restrict txq,
 {
 	struct mlx5_wqe_wseg *ws;
 
-	ws = RTE_PTR_ADD(wqe, MLX5_WSEG_SIZE);
+	ws = RTE_PTR_ADD((void *)wqe, MLX5_WSEG_SIZE);
 	ws->operation = rte_cpu_to_be_32(MLX5_WAIT_COND_CYCLIC_SMALLER);
 	ws->lkey = RTE_BE32(0);
 	ws->va_high = RTE_BE32(0);
drivers/net/mlx5/mlx5_txpp.c +2 -2
index 0e99b58bde..ac1a17476c 100644
@@ -194,7 +194,7 @@ mlx5_txpp_fill_wqe_rearm_queue(struct mlx5_dev_ctx_shared *sh)
 		cs->flags = RTE_BE32(MLX5_COMP_ALWAYS <<
 				     MLX5_COMP_MODE_OFFSET);
 		cs->misc = RTE_BE32(0);
-		qs = RTE_PTR_ADD(cs, sizeof(struct mlx5_wqe_cseg));
+		qs = RTE_PTR_ADD((void *)cs, sizeof(struct mlx5_wqe_cseg));
 		index = (i * MLX5_TXPP_REARM / 2 + MLX5_TXPP_REARM) &
 			((1 << MLX5_WQ_INDEX_WIDTH) - 1);
 		qs->max_index = rte_cpu_to_be_32(index);
@@ -207,7 +207,7 @@ mlx5_txpp_fill_wqe_rearm_queue(struct mlx5_dev_ctx_shared *sh)
 		cs->flags = RTE_BE32(MLX5_COMP_ONLY_ERR <<
 				     MLX5_COMP_MODE_OFFSET);
 		cs->misc = RTE_BE32(0);
-		qs = RTE_PTR_ADD(cs, sizeof(struct mlx5_wqe_cseg));
+		qs = RTE_PTR_ADD((void *)cs, sizeof(struct mlx5_wqe_cseg));
 		index = (i * MLX5_TXPP_REARM / 2 + MLX5_TXPP_REARM / 2) &
 			((1 << MLX5_CQ_INDEX_WIDTH) - 1);
 		qs->max_index = rte_cpu_to_be_32(index);
drivers/net/sfc/sfc_mae.c +5 -5
index b0e8b02b41..f31e406fb2 100644
@@ -4097,7 +4097,7 @@ sfc_mae_header_force_item_masks(uint8_t *header_buf,
 
 		for (ofst = 0; ofst < proto_header_size;
 		     ofst += sizeof(rte_be16_t)) {
-			rte_be16_t *wp = RTE_PTR_ADD(header_buf, ofst);
+			rte_be16_t *wp = RTE_PTR_ADD((void *)header_buf, ofst);
 			const rte_be16_t *w_maskp;
 			const rte_be16_t *w_specp;
 
@@ -4231,10 +4231,10 @@ sfc_mae_rule_parse_action_vxlan_encap(
 
 			proto_header_size = sizeof(struct rte_vlan_hdr);
 
-			ethertypep = RTE_PTR_ADD(buf, eth_ethertype_ofst);
+			ethertypep = RTE_PTR_ADD((void *)buf, eth_ethertype_ofst);
 			*ethertypep = RTE_BE16(RTE_ETHER_TYPE_QINQ);
 
-			ethertypep = RTE_PTR_ADD(buf, ethertype_ofst);
+			ethertypep = RTE_PTR_ADD((void *)buf, ethertype_ofst);
 			*ethertypep = RTE_BE16(RTE_ETHER_TYPE_VLAN);
 
 			ethertype_ofst =
@@ -4255,7 +4255,7 @@ sfc_mae_rule_parse_action_vxlan_encap(
 
 			proto_header_size = sizeof(struct rte_ipv4_hdr);
 
-			ethertypep = RTE_PTR_ADD(buf, ethertype_ofst);
+			ethertypep = RTE_PTR_ADD((void *)buf, ethertype_ofst);
 			*ethertypep = RTE_BE16(RTE_ETHER_TYPE_IPV4);
 
 			next_proto_ofst =
@@ -4274,7 +4274,7 @@ sfc_mae_rule_parse_action_vxlan_encap(
 
 			proto_header_size = sizeof(struct rte_ipv6_hdr);
 
-			ethertypep = RTE_PTR_ADD(buf, ethertype_ofst);
+			ethertypep = RTE_PTR_ADD((void *)buf, ethertype_ofst);
 			*ethertypep = RTE_BE16(RTE_ETHER_TYPE_IPV6);
 
 			next_proto_ofst = bounce_eh->size +
drivers/net/virtio/virtio_rxtx.c +1 -1
index edecd2011f..089811160b 100644
@@ -414,7 +414,7 @@ virtio_tso_fix_cksum(struct rte_mbuf *m)
 
 		iph = rte_pktmbuf_mtod_offset(m,
 					struct rte_ipv4_hdr *, m->l2_len);
-		th = RTE_PTR_ADD(iph, m->l3_len);
+		th = RTE_PTR_ADD((void *)iph, m->l3_len);
 
 		/*
 		 * Calculate IPv4 header checksum with current total length value
lib/eal/common/eal_common_memory.c +9 -10
index c62edf5e55..a1414e790c 100644
@@ -332,6 +332,8 @@ virt2memseg_list(const void *addr)
 		msl = &mcfg->memsegs[msl_idx];
 
 		start = msl->base_va;
+		if (start == NULL)
+			continue;
 		end = RTE_PTR_ADD(start, msl->len);
 		if (addr >= start && addr < end)
 			break;
@@ -680,10 +682,9 @@ RTE_EXPORT_SYMBOL(rte_mem_lock_page)
 int
 rte_mem_lock_page(const void *virt)
 {
-	uintptr_t virtual = (uintptr_t)virt;
 	size_t page_size = rte_mem_page_size();
-	uintptr_t aligned = RTE_PTR_ALIGN_FLOOR(virtual, page_size);
-	return rte_mem_lock((void *)aligned, page_size);
+	const void *aligned = RTE_PTR_ALIGN_FLOOR(virt, page_size);
+	return rte_mem_lock(aligned, page_size);
 }
 
 RTE_EXPORT_SYMBOL(rte_memseg_contig_walk_thread_unsafe)
@@ -1447,7 +1448,7 @@ handle_eal_memseg_info_request(const char *cmd __rte_unused,
 
 	ms_iova = ms->iova;
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = RTE_INT_PTR_ADD(ms_start_addr, ms->len);
 	ms_size = ms->len;
 	hugepage_size = ms->hugepage_sz;
 	ms_socket_id = ms->socket_id;
@@ -1519,7 +1520,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = RTE_INT_PTR_ADD(ms_start_addr, ms->len);
 	rte_mcfg_mem_read_unlock();
 
 	rte_tel_data_start_dict(d);
@@ -1530,8 +1531,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = RTE_INT_PTR_ADD(elem_start_addr, elem->size);
 
 		if ((uint64_t)elem_start_addr >= ms_start_addr &&
 		    (uint64_t)elem_end_addr <= ms_end_addr)
@@ -1597,7 +1597,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = RTE_INT_PTR_ADD(ms_start_addr, ms->len);
 
 	rte_mcfg_mem_read_unlock();
 
@@ -1609,8 +1609,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = RTE_INT_PTR_ADD(elem_start_addr, elem->size);
 
 		if (elem_start_addr < ms_start_addr ||
 				elem_end_addr > ms_end_addr) {
lib/eal/common/eal_common_options.c +1 -1
index 485655865d..d62011b037 100644
@@ -1656,7 +1656,7 @@ eal_parse_base_virtaddr(const char *arg)
 	 * on x86 and other architectures.
 	 */
 	internal_conf->base_virtaddr =
-		RTE_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
+		RTE_INT_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
 
 	return 0;
 }
lib/eal/common/malloc_elem.h +2 -2
index c7ff6718f8..22bfe96883 100644
@@ -309,10 +309,10 @@ malloc_elem_from_data(const void *data)
 	if (data == NULL)
 		return NULL;
 
-	struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
+	struct malloc_elem *elem = RTE_PTR_SUB((void *)data, MALLOC_ELEM_HEADER_LEN);
 	if (!malloc_elem_cookies_ok(elem))
 		return NULL;
-	return elem->state != ELEM_PAD ? elem:  RTE_PTR_SUB(elem, elem->pad);
+	return elem->state != ELEM_PAD ? elem : RTE_PTR_SUB((void *)elem, elem->pad);
 }
 
 /*
lib/eal/common/malloc_heap.c +5 -1
index 39240c261c..bae6de58c4 100644
@@ -1390,7 +1390,11 @@ malloc_heap_destroy(struct malloc_heap *heap)
 
 	/* Reset all of the heap but the (hold) lock so caller can release it. */
 	RTE_BUILD_BUG_ON(offsetof(struct malloc_heap, lock) != 0);
-	memset(RTE_PTR_ADD(heap, sizeof(heap->lock)), 0,
+	/* Cast to void* to avoid compiler alignment assumptions from typed pointer.
+	 * RTE_PTR_ADD preserves type, but heap+sizeof(lock) is misaligned for
+	 * struct malloc_heap, which can cause crashes with vectorized memset.
+	 */
+	memset((void *)RTE_PTR_ADD(heap, sizeof(heap->lock)), 0,
 		sizeof(*heap) - sizeof(heap->lock));
 
 	return 0;
lib/eal/include/rte_common.h +169 -5

205 lines not on this page · raw message

lib/ethdev/rte_ethdev.c +3 -3
index 2659e8d9eb..05d5b52faa 100644
@@ -3711,7 +3711,7 @@ eth_basic_stats_get(uint16_t port_id, struct rte_eth_xstat *xstats)
 
 	/* global stats */
 	for (i = 0; i < RTE_NB_STATS; i++) {
-		stats_ptr = RTE_PTR_ADD(&eth_stats,
+		stats_ptr = RTE_PTR_ADD((void *)&eth_stats,
 					eth_dev_stats_strings[i].offset);
 		val = *stats_ptr;
 		xstats[count++].value = val;
@@ -3723,7 +3723,7 @@ eth_basic_stats_get(uint16_t port_id, struct rte_eth_xstat *xstats)
 	/* per-rxq stats */
 	for (q = 0; q < nb_rxqs; q++) {
 		for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
-			stats_ptr = RTE_PTR_ADD(&queue_stats,
+			stats_ptr = RTE_PTR_ADD((void *)&queue_stats,
 					eth_dev_rxq_stats_strings[i].offset +
 					q * sizeof(uint64_t));
 			val = *stats_ptr;
@@ -3734,7 +3734,7 @@ eth_basic_stats_get(uint16_t port_id, struct rte_eth_xstat *xstats)
 	/* per-txq stats */
 	for (q = 0; q < nb_txqs; q++) {
 		for (i = 0; i < RTE_NB_TXQ_STATS; i++) {
-			stats_ptr = RTE_PTR_ADD(&queue_stats,
+			stats_ptr = RTE_PTR_ADD((void *)&queue_stats,
 					eth_dev_txq_stats_strings[i].offset +
 					q * sizeof(uint64_t));
 			val = *stats_ptr;
lib/graph/graph_populate.c +2 -2
index 026daecb21..4ec20c26b1 100644
@@ -62,7 +62,7 @@ graph_header_popluate(struct graph *_graph)
 	graph->head = (int32_t)-_graph->src_node_count;
 	graph->cir_mask = _graph->cir_mask;
 	graph->nb_nodes = _graph->node_count;
-	graph->cir_start = RTE_PTR_ADD(graph, _graph->cir_start);
+	graph->cir_start = RTE_PTR_ADD((void *)graph, _graph->cir_start);
 	graph->nodes_start = _graph->nodes_start;
 	graph->socket = _graph->socket;
 	graph->id = _graph->id;
@@ -81,7 +81,7 @@ graph_nodes_populate(struct graph *_graph)
 	rte_node_t pid;
 
 	STAILQ_FOREACH(graph_node, &_graph->node_list, next) {
-		struct rte_node *node = RTE_PTR_ADD(graph, off);
+		struct rte_node *node = RTE_PTR_ADD((void *)graph, off);
 		memset(node, 0, sizeof(*node));
 		node->fence = RTE_GRAPH_FENCE;
 		node->off = off;
lib/graph/graph_stats.c +1 -1
index b87b5707f7..e7b56966eb 100644
@@ -501,7 +501,7 @@ cluster_node_arregate_stats(struct cluster_node *cluster, bool dispatch)
 
 		if (node->xstat_off == 0)
 			continue;
-		xstat = RTE_PTR_ADD(node, node->xstat_off);
+		xstat = RTE_PTR_ADD((void *)node, node->xstat_off);
 		for (i = 0; i < stat->xstat_cntrs; i++)
 			stat->xstat_count[i] += xstat[i];
 	}
lib/graph/rte_graph.h +2 -2
index 7e433f4661..7eb7495eae 100644
@@ -407,9 +407,9 @@ void rte_graph_obj_dump(FILE *f, struct rte_graph *graph, bool all);
 /** Macro to browse rte_node object after the graph creation */
 #define rte_graph_foreach_node(count, off, graph, node)                        \
 	for (count = 0, off = graph->nodes_start,                              \
-	     node = RTE_PTR_ADD(graph, off);                                   \
+	     node = RTE_PTR_ADD((void *)graph, off);                           \
 	     count < graph->nb_nodes;                                          \
-	     off = node->next, node = RTE_PTR_ADD(graph, off), count++)
+	     off = node->next, node = RTE_PTR_ADD((void *)graph, off), count++)
 
 /**
  * Get node object with in graph from id.
lib/graph/rte_graph_model_mcore_dispatch.c +1 -1
index 706b5469f0..cd3fc130d3 100644
@@ -154,7 +154,7 @@ __rte_graph_mcore_dispatch_sched_wq_process(struct rte_graph *graph)
 
 	for (i = 0; i < n; i++) {
 		wq_node = wq_nodes[i];
-		node = RTE_PTR_ADD(graph, wq_node->node_off);
+		node = RTE_PTR_ADD((void *)graph, wq_node->node_off);
 		RTE_ASSERT(node->fence == RTE_GRAPH_FENCE);
 		idx = node->idx;
 		free_space = node->size - idx;
lib/latencystats/rte_latencystats.c +1 -1
index f61d5a273f..cc27205500 100644
@@ -105,7 +105,7 @@ latencystats_collect(uint64_t values[])
 	const uint64_t *stats;
 
 	for (i = 0; i < NUM_LATENCY_STATS; i++) {
-		stats = RTE_PTR_ADD(glob_stats, lat_stats_strings[i].offset);
+		stats = RTE_PTR_ADD((void *)glob_stats, lat_stats_strings[i].offset);
 		scale = lat_stats_strings[i].scale;
 
 		/* used to mark samples which are not a time interval */
lib/mbuf/rte_mbuf.c +1 -1
index 0d931c7a15..61d174cba3 100644
@@ -215,7 +215,7 @@ __rte_pktmbuf_init_extmem(struct rte_mempool *mp,
 	m->next = NULL;
 
 	/* init external buffer shared info items */
-	shinfo = RTE_PTR_ADD(m, mbuf_size);
+	shinfo = RTE_PTR_ADD((void *)m, mbuf_size);
 	m->shinfo = shinfo;
 	shinfo->free_cb = rte_pktmbuf_free_pinned_extmem;
 	shinfo->fcb_opaque = m;
lib/member/rte_xxh64_avx512.h +3 -3
index 58f896ebb8..774b26d8df 100644
@@ -58,7 +58,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 			 _mm512_set1_epi64(key_len));
 
 	while (remaining >= 8) {
-		input = _mm512_set1_epi64(*(uint64_t *)RTE_PTR_ADD(key, offset));
+		input = _mm512_set1_epi64(*(const uint64_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 				xxh64_round_avx512(_mm512_setzero_si512(), input));
 		v_hash = _mm512_madd52lo_epu64(_mm512_set1_epi64(PRIME64_4),
@@ -71,7 +71,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	if (remaining >= 4) {
 		input = _mm512_set1_epi64
-			(*(uint32_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint32_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_1)));
@@ -86,7 +86,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	while (remaining != 0) {
 		input = _mm512_set1_epi64
-			(*(uint8_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint8_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_5)));
lib/pdcp/pdcp_entity.h +5 -3
index f854192e98..e8251cbdbc 100644
@@ -198,17 +198,19 @@ struct entity_priv_ul_part {
 
 static inline struct entity_priv *
 entity_priv_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity));
+	return RTE_PTR_ADD((void *)entity, sizeof(struct rte_pdcp_entity));
 }
 
 static inline struct entity_priv_dl_part *
 entity_dl_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD((void *)entity,
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline struct entity_priv_ul_part *
 entity_ul_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD((void *)entity,
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline int
lib/reorder/rte_reorder.c +1 -1
index be06530860..5caebf20d4 100644
@@ -120,7 +120,7 @@ rte_reorder_init(struct rte_reorder_buffer *b, unsigned int bufsize,
 	b->order_buf.size = b->ready_buf.size = size;
 	b->order_buf.mask = b->ready_buf.mask = size - 1;
 	b->ready_buf.entries = (void *)&b[1];
-	b->order_buf.entries = RTE_PTR_ADD(&b[1],
+	b->order_buf.entries = RTE_PTR_ADD((void *)&b[1],
 			size * sizeof(b->ready_buf.entries[0]));
 
 	return b;
Signature · 1 line
2.39.5 (Apple Git-154)
#16 lore raw

PATCH v11 1/1 eal: RTE_PTR_ADD/SUB API improvements

From: Scott Mitchell <[email protected]>

RTE_PTR_ADD and RTE_PTR_SUB APIs have a few limitations:
1. ptr cast to uintptr_t drops pointer provenance and prevents compiler optimizations
2. return cast to void* removes type safety and discards qualifiers (const, volatile)
3. Accepts both "pointers" and "integers as pointers" which overloads the use case and constrains the implementation to address other challenges.

This patch splits the API on two dimensions:
1. pointer types
2. integer types that represent pointers

This split allows addressing each of the challenges above and provides distinct APIs for the distinct use cases. Examples:
1. Clang is able to optimize and improve __rte_raw_cksum (which uses RTE_PTR_ADD) by ~40% (100 bytes) to ~8x (1.5k bytes) TSC cycles/byte.
2. Refactoring discovered cases that dropped qualifiers (volatile) which are likely bugs and warrant more explicit cast/handling.

---
v11:
- Split API into PTR and INT_PTR variants, update all usage of PTR API for new APIs.

v10:
- Use unit_test_suite_runner for easier subtest failure identification

v9:
- Fix include order: system includes, then DPDK includes, then application includes
- Use NOHUGE_OK and ASAN_OK constants in REGISTER_FAST_TEST (instead of true, true)

v8:
- Remove tests for types < 32-bit (bool, char, short, uint8_t, uint16_t)
- Merge test_ptr_add_sub_typedefs() into test_ptr_add_sub_integer_types()
- Separate RTE_PTR_ADD and RTE_PTR_SUB documentation
- Move Clang/GCC implementation note from Doxygen to regular comment
- Tests verify both intermediate ADD result and SUB round-trip
- Use uintptr_t cast consistently for all integer-to-pointer conversions
- Make TEST_RETVAL calculated from TEST_INITVAL + TEST_INCREMENT

v7:
- Fix tests: use TEST_BUFFER_SIZE macro for buffer allocation
- Fix tests: ADD then SUB same amount to avoid out-of-bounds pointer arithmetic
- All RTE_PTR_SUB tests now verify round-trip (ADD+SUB returns to original)

v6:
- Make char* optimization Clang-only to avoid GCC false positive warnings
- Improve tests: use named constants instead of magic numbers
- Improve tests: use void* casts on expected values instead of uintptr_t on results

v5:
- Initial implementation with char* arithmetic for all compilers

v4:
- Used _Generic for type-based dispatch with char* casts
- Had warnings on both Clang and GCC due to _Generic type-checking all branches

---
 app/test-eventdev/test_perf_common.h        |   7 +-
 app/test-pmd/cmdline_flow.c                 |   6 +-
 app/test-pmd/csumonly.c                     |   2 +-
 app/test/meson.build                        |   1 +
 app/test/test_common.c                      |  20 +-
 app/test/test_ptr_add_sub.c                 | 197 ++++++++++++++++++++
 drivers/bus/pci/linux/pci_vfio.c            |   2 +-
 drivers/common/cnxk/cnxk_security.c         |   2 +-
 drivers/common/cnxk/roc_cpt_debug.c         |   4 +-
 drivers/common/cnxk/roc_nix_bpf.c           |   2 +-
 drivers/common/cnxk/roc_nix_inl.h           |   4 +-
 drivers/common/cnxk/roc_nix_inl_dp.h        |   8 +-
 drivers/common/cnxk/roc_platform.h          |   6 +-
 drivers/common/cnxk/roc_se.c                |   2 +-
 drivers/common/cpt/cpt_ucode_asym.h         |  12 +-
 drivers/common/mlx5/linux/mlx5_nl.c         |  14 +-
 drivers/compress/mlx5/mlx5_compress.c       |   2 +-
 drivers/crypto/cnxk/cn9k_cryptodev_ops.c    |   2 +-
 drivers/crypto/cnxk/cnxk_cryptodev_ops.c    |   2 +-
 drivers/crypto/mlx5/mlx5_crypto_gcm.c       |   6 +-
 drivers/crypto/mlx5/mlx5_crypto_xts.c       |   2 +-
 drivers/crypto/octeontx/otx_cryptodev_ops.c |   2 +-
 drivers/dma/idxd/idxd_internal.h            |   1 +
 drivers/dma/idxd/idxd_pci.c                 |  11 +-
 drivers/dma/odm/odm_dmadev.c                |   4 +-
 drivers/mempool/bucket/rte_mempool_bucket.c |   7 +-
 drivers/ml/cnxk/cn10k_ml_model.c            |   2 +-
 drivers/ml/cnxk/cn10k_ml_ops.c              |   2 +-
 drivers/ml/cnxk/cnxk_ml_ops.c               |   2 +-
 drivers/net/ark/ark_ethdev.c                |   4 +-
 drivers/net/cxgbe/cxgbe_ethdev.c            |   6 +-
 drivers/net/gve/base/gve_adminq.c           |  12 +-
 drivers/net/intel/ice/ice_tm.c              |   4 +-
 drivers/net/intel/ixgbe/ixgbe_rxtx.c        |   2 +-
 drivers/net/mlx5/mlx5_flow.c                |   4 +-
 drivers/net/mlx5/mlx5_flow_hw.c             |   4 +-
 drivers/net/mlx5/mlx5_tx.h                  |   4 +-
 drivers/net/mlx5/mlx5_txpp.c                |   4 +-
 drivers/net/sfc/sfc_mae.c                   |  10 +-
 drivers/net/virtio/virtio_rxtx.c            |   2 +-
 lib/eal/common/eal_common_memory.c          |  19 +-
 lib/eal/common/eal_common_options.c         |   2 +-
 lib/eal/common/malloc_elem.h                |   4 +-
 lib/eal/common/malloc_heap.c                |   6 +-
 lib/eal/include/rte_common.h                | 174 ++++++++++++++++-
 lib/ethdev/rte_ethdev.c                     |   6 +-
 lib/graph/graph_populate.c                  |   4 +-
 lib/graph/graph_stats.c                     |   2 +-
 lib/graph/rte_graph.h                       |   4 +-
 lib/graph/rte_graph_model_mcore_dispatch.c  |   2 +-
 lib/latencystats/rte_latencystats.c         |   2 +-
 lib/mbuf/rte_mbuf.c                         |   2 +-
 lib/member/rte_xxh64_avx512.h               |   6 +-
 lib/pdcp/pdcp_entity.h                      |   8 +-
 lib/reorder/rte_reorder.c                   |   2 +-
 55 files changed, 502 insertions(+), 131 deletions(-)
 create mode 100644 app/test/test_ptr_add_sub.c
app/test-eventdev/test_perf_common.h +4 -3
index 365464f269..2b0434b844 100644
@@ -141,7 +141,7 @@ perf_mark_fwd_latency(enum evt_prod_type prod_type, struct rte_event *const ev)
 
 			pe = rte_pktmbuf_mtod(m, struct perf_elt *);
 		} else {
-			pe = RTE_PTR_ADD(op->asym->modex.result.data,
+			pe = RTE_PTR_ADD((void *)op->asym->modex.result.data,
 					 op->asym->modex.result.length);
 		}
 		pe->timestamp = rte_get_timer_cycles();
@@ -179,7 +179,8 @@ perf_elt_from_vec_get(struct rte_event_vector *vec)
 		m = cop->sym->m_dst == NULL ? cop->sym->m_src : cop->sym->m_dst;
 		return rte_pktmbuf_mtod(m, struct perf_elt *);
 	} else {
-		return RTE_PTR_ADD(cop->asym->modex.result.data, cop->asym->modex.result.length);
+		return RTE_PTR_ADD((void *)cop->asym->modex.result.data,
+				   cop->asym->modex.result.length);
 	}
 }
 
@@ -297,7 +298,7 @@ perf_process_last_stage_latency(struct rte_mempool *const pool, enum evt_prod_ty
 			to_free_in_bulk = m;
 			pe = rte_pktmbuf_mtod(m, struct perf_elt *);
 		} else {
-			pe = RTE_PTR_ADD(op->asym->modex.result.data,
+			pe = RTE_PTR_ADD((void *)op->asym->modex.result.data,
 					 op->asym->modex.result.length);
 			to_free_in_bulk = op->asym->modex.result.data;
 		}
app/test-pmd/cmdline_flow.c +3 -3
index ebc036b14b..01a45fb807 100644
@@ -12468,7 +12468,7 @@ parse_meter_color(struct context *ctx, const struct token *token,
 
 		if (!arg)
 			return -1;
-		*(int *)RTE_PTR_ADD(action->conf, arg->offset) = i;
+		*(int *)RTE_PTR_ADD((void *)action->conf, arg->offset) = i;
 	} else {
 		((struct rte_flow_item_meter_color *)
 			ctx->object)->color = (enum rte_color)i;
@@ -13343,7 +13343,7 @@ indirect_action_flow_conf_create(const struct buffer *in)
 		goto end;
 	indlst_conf->id = in->args.vc.attr.group;
 	indlst_conf->conf_num = in->args.vc.actions_n - 1;
-	indlst_conf->actions = RTE_PTR_ADD(indlst_conf, base);
+	indlst_conf->actions = RTE_PTR_ADD((void *)indlst_conf, base);
 	ret = rte_flow_conv(RTE_FLOW_CONV_OP_ACTIONS, indlst_conf->actions,
 			    len, src, NULL);
 	if (ret <= 0) {
@@ -13351,7 +13351,7 @@ indirect_action_flow_conf_create(const struct buffer *in)
 		indlst_conf = NULL;
 		goto end;
 	}
-	indlst_conf->conf = RTE_PTR_ADD(indlst_conf, base + len);
+	indlst_conf->conf = (const void **)RTE_PTR_ADD((void *)indlst_conf, base + len);
 	for (i = 0; i < indlst_conf->conf_num; i++)
 		indlst_conf->conf[i] = indlst_conf->actions[i].conf;
 	SLIST_INSERT_HEAD(&indlst_conf_head, indlst_conf, next);
app/test-pmd/csumonly.c +1 -1
index c841651756..24866e563f 100644
@@ -488,7 +488,7 @@ get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype)
 		return _htons(RTE_ETHER_TYPE_IPV6);
 	default:
 		ethertype = eth_hdr->ether_type;
-		vlan_hdr = RTE_PTR_ADD(eth_hdr, offsetof(struct rte_ether_hdr, ether_type));
+		vlan_hdr = RTE_PTR_ADD((void *)eth_hdr, offsetof(struct rte_ether_hdr, ether_type));
 		max_vlans = vlan_hdr + MAX_VLAN_HEADERS;
 		while ((ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
 				ethertype == _htons(RTE_ETHER_TYPE_QINQ)) &&
app/test/meson.build +1 -0
index f4d04a6e42..aa56fc4297 100644
@@ -152,6 +152,7 @@ source_file_deps = {
     'test_power_intel_uncore.c': ['power', 'power_intel_uncore'],
     'test_power_kvm_vm.c': ['power', 'power_kvm_vm'],
     'test_prefetch.c': [],
+    'test_ptr_add_sub.c': [],
     'test_ptr_compress.c': ['ptr_compress'],
     'test_rand_perf.c': [],
     'test_rawdev.c': ['rawdev', 'bus_vdev', 'raw_skeleton'],
app/test/test_common.c +10 -10
index 3e1c7df0c1..cc0ce50bbc 100644
@@ -37,10 +37,10 @@ test_macros(int __rte_unused unused_parm)
 	RTE_SWAP(smaller, bigger);
 	RTE_TEST_ASSERT(smaller == BIGGER && bigger == SMALLER,
 		"RTE_SWAP");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF), BIGGER,
-		"RTE_PTR_ADD");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF), SMALLER,
-		"RTE_PTR_SUB");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR_ADD(SMALLER, PTR_DIFF), BIGGER,
+		"RTE_INT_PTR_ADD");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR_SUB(BIGGER, PTR_DIFF), SMALLER,
+		"RTE_INT_PTR_SUB");
 	RTE_TEST_ASSERT_EQUAL(RTE_PTR_DIFF(BIGGER, SMALLER), PTR_DIFF,
 		"RTE_PTR_DIFF");
 	RTE_TEST_ASSERT_EQUAL(RTE_MAX(SMALLER, BIGGER), BIGGER,
@@ -188,18 +188,18 @@ test_align(void)
 			if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
-			val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
+			val = RTE_INT_PTR_ALIGN_FLOOR((uintptr_t) i, p);
 			if (ERROR_FLOOR(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_FLOOR", i, p);
 
 			val = RTE_ALIGN_FLOOR(i, p);
 			if (ERROR_FLOOR(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
 			/* align ceiling */
-			val = RTE_PTR_ALIGN((uintptr_t) i, p);
+			val = RTE_INT_PTR_ALIGN((uintptr_t) i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN", i, p);
 
 			val = RTE_ALIGN(i, p);
 			if (ERROR_CEIL(val, i, p))
@@ -209,9 +209,9 @@ test_align(void)
 			if (ERROR_CEIL(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
 
-			val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
+			val = RTE_INT_PTR_ALIGN_CEIL((uintptr_t)i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_CEIL", i, p);
 
 			/* by this point we know that val is aligned to p */
 			if (!rte_is_aligned((void*)(uintptr_t) val, p))
app/test/test_ptr_add_sub.c new file +197 -0

197 lines not on this page · raw message

drivers/bus/pci/linux/pci_vfio.c +1 -1
index 242f567ed7..004cb3cf91 100644
@@ -672,7 +672,7 @@ pci_vfio_info_cap(struct vfio_region_info *info, int cap)
 
 	offset = info->cap_offset;
 	while (offset != 0) {
-		h = RTE_PTR_ADD(info, offset);
+		h = RTE_PTR_ADD((void *)info, offset);
 		if (h->id == cap)
 			return h;
 		offset = h->next;
drivers/common/cnxk/cnxk_security.c +1 -1
index 600098ae1c..4e2105aaa6 100644
@@ -965,7 +965,7 @@ on_fill_ipsec_common_sa(struct rte_security_ipsec_xform *ipsec,
 		else if (crypto_xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM) {
 			ccm_flag = 0x07 & ~ROC_CPT_AES_CCM_CTR_LEN;
 			*common_sa->iv.gcm.nonce = ccm_flag;
-			memcpy(PLT_PTR_ADD(common_sa->iv.gcm.nonce, 1), &ipsec->salt, 3);
+			memcpy(PLT_PTR_ADD(&common_sa->iv.gcm.nonce[0], 1), &ipsec->salt, 3);
 		}
 		cipher_key = crypto_xform->aead.key.data;
 		cipher_key_len = crypto_xform->aead.key.length;
drivers/common/cnxk/roc_cpt_debug.c +2 -2
index 28aedf088e..ff27a2ede9 100644
@@ -56,7 +56,7 @@ cpt_cnxk_parse_hdr_dump(FILE *file, const struct cpt_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*32B */
 	offset = cpth->w2.ptr_offset;
 	offset = (((offset - 1) & 0x7) + 1) * 32;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD((void *)cpth, offset);
 
 	if (cpth->w0.num_frags > 0) {
 		cpt_dump(file, "CPT Fraginfo_0 \t%p:", frag_info);
@@ -162,7 +162,7 @@ cpt_cn10k_parse_hdr_dump(FILE *file, const struct cpt_cn10k_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*8B */
 	offset = cpth->w2.fi_offset;
 	offset = (((offset - 1) & 0x1f) + 1) * 8;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD((void *)cpth, offset);
 
 	cpt_dump(file, "CPT Fraginfo \t0x%p:", frag_info);
 
drivers/common/cnxk/roc_nix_bpf.c +1 -1
index 98c9855a5b..985b318fdd 100644
@@ -160,7 +160,7 @@ nix_precolor_conv_table_write(struct roc_nix *roc_nix, uint64_t val,
 	struct nix *nix = roc_nix_to_nix_priv(roc_nix);
 	int64_t *addr;
 
-	addr = PLT_PTR_ADD(nix->base, off);
+	addr = PLT_PTR_ADD((void *)nix->base, off);
 	plt_write64(val, addr);
 }
 
drivers/common/cnxk/roc_nix_inl.h +2 -2
index 7970ac2258..9fe1417756 100644
@@ -59,7 +59,7 @@ roc_nix_inl_on_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_PTR_ADD((void *)base, off);
 }
 
 static inline struct roc_ie_on_outb_sa *
@@ -67,7 +67,7 @@ roc_nix_inl_on_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_PTR_ADD((void *)base, off);
 }
 
 static inline void *
drivers/common/cnxk/roc_nix_inl_dp.h +4 -4
index eb101db179..d7737cb7ca 100644
@@ -49,7 +49,7 @@ roc_nix_inl_ot_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_PTR_ADD((void *)base, off);
 }
 
 static inline struct roc_ot_ipsec_outb_sa *
@@ -57,7 +57,7 @@ roc_nix_inl_ot_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_PTR_ADD((void *)base, off);
 }
 
 static inline void *
@@ -77,7 +77,7 @@ roc_nix_inl_ow_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_PTR_ADD((void *)base, off);
 }
 
 static inline struct roc_ow_ipsec_outb_sa *
@@ -85,7 +85,7 @@ roc_nix_inl_ow_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_PTR_ADD((void *)base, off);
 }
 
 static inline void *
drivers/common/cnxk/roc_platform.h +4 -2
index e22a50d47a..1bee272dde 100644
@@ -47,6 +47,8 @@
 #define PLT_PTR_ADD		 RTE_PTR_ADD
 #define PLT_PTR_SUB		 RTE_PTR_SUB
 #define PLT_PTR_DIFF		 RTE_PTR_DIFF
+#define PLT_INT_PTR_ADD		 RTE_INT_PTR_ADD
+#define PLT_INT_PTR_SUB		 RTE_INT_PTR_SUB
 #define PLT_MAX_RXTX_INTR_VEC_ID RTE_MAX_RXTX_INTR_VEC_ID
 #define PLT_INTR_VEC_RXTX_OFFSET RTE_INTR_VEC_RXTX_OFFSET
 #define PLT_MIN			 RTE_MIN
@@ -84,8 +86,8 @@
 #define PLT_U16_CAST(val) ((uint16_t)(val))
 
 /* Add / Sub pointer with scalar and cast to uint64_t */
-#define PLT_PTR_ADD_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_PTR_ADD(__ptr, __x))
-#define PLT_PTR_SUB_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_PTR_SUB(__ptr, __x))
+#define PLT_PTR_ADD_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_INT_PTR_ADD(__ptr, __x))
+#define PLT_PTR_SUB_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_INT_PTR_SUB(__ptr, __x))
 
 /** Divide ceil */
 #define PLT_DIV_CEIL(x, y)			\
drivers/common/cnxk/roc_se.c +1 -1
index f71832ff3a..445e2a66dc 100644
@@ -635,7 +635,7 @@ roc_se_ctx_init(struct roc_se_ctx *roc_se_ctx)
 	ctx_len = PLT_ALIGN_CEIL(ctx_len, 8);
 
 	/* Skip w0 for swap */
-	uc_ctx = PLT_PTR_ADD(ctx, sizeof(ctx->w0));
+	uc_ctx = PLT_PTR_ADD((void *)ctx, sizeof(ctx->w0));
 	for (i = 0; i < (ctx_len / 8); i++)
 		uc_ctx[i] = plt_cpu_to_be_64(((uint64_t *)uc_ctx)[i]);
 
drivers/common/cpt/cpt_ucode_asym.h +6 -6
index 5122378ec7..85654600c8 100644
@@ -251,7 +251,7 @@ cpt_modex_prep(struct asym_op_params *modex_params,
 	total_key_len = mod_len + exp_len;
 
 	/* Input buffer */
-	dptr = RTE_PTR_ADD(req, sizeof(struct cpt_request_info));
+	dptr = RTE_PTR_ADD((void *)req, sizeof(struct cpt_request_info));
 	memcpy(dptr, mod->modulus.data, total_key_len);
 	dptr += total_key_len;
 	memcpy(dptr, mod_op.base.data, base_len);
@@ -314,7 +314,7 @@ cpt_rsa_prep(struct asym_op_params *rsa_params,
 	total_key_len  = mod_len + exp_len;
 
 	/* Input buffer */
-	dptr = RTE_PTR_ADD(req, sizeof(struct cpt_request_info));
+	dptr = RTE_PTR_ADD((void *)req, sizeof(struct cpt_request_info));
 	memcpy(dptr, rsa->n.data, total_key_len);
 	dptr += total_key_len;
 
@@ -399,7 +399,7 @@ cpt_rsa_crt_prep(struct asym_op_params *rsa_params,
 	total_key_len = p_len + q_len + dP_len + dQ_len + qInv_len;
 
 	/* Input buffer */
-	dptr = RTE_PTR_ADD(req, sizeof(struct cpt_request_info));
+	dptr = RTE_PTR_ADD((void *)req, sizeof(struct cpt_request_info));
 	memcpy(dptr, rsa->qt.q.data, total_key_len);
 	dptr += total_key_len;
 
@@ -665,7 +665,7 @@ cpt_ecdsa_sign_prep(struct rte_crypto_ecdsa_op_param *ecdsa,
 	pk_offset = prime_len - pkey_len;
 
 	/* Input buffer */
-	dptr = RTE_PTR_ADD(req, sizeof(struct cpt_request_info));
+	dptr = RTE_PTR_ADD((void *)req, sizeof(struct cpt_request_info));
 
 	/*
 	 * Set dlen = sum(sizeof(fpm address), ROUNDUP8(scalar len, input len),
@@ -773,7 +773,7 @@ cpt_ecdsa_verify_prep(struct rte_crypto_ecdsa_op_param *ecdsa,
 	s_offset = prime_len - s_len;
 
 	/* Input buffer */
-	dptr = RTE_PTR_ADD(req, sizeof(struct cpt_request_info));
+	dptr = RTE_PTR_ADD((void *)req, sizeof(struct cpt_request_info));
 
 	/*
 	 * Set dlen = sum(sizeof(fpm address), ROUNDUP8(message len),
@@ -884,7 +884,7 @@ cpt_ecpm_prep(struct rte_crypto_ecpm_op_param *ecpm,
 	prime_len = ec_grp[curveid].prime.length;
 
 	/* Input buffer */
-	dptr = RTE_PTR_ADD(req, sizeof(struct cpt_request_info));
+	dptr = RTE_PTR_ADD((void *)req, sizeof(struct cpt_request_info));
 
 	p_align = RTE_ALIGN_CEIL(prime_len, 8);
 	scalar_align = RTE_ALIGN_CEIL(ecpm->scalar.length, 8);
drivers/common/mlx5/linux/mlx5_nl.c +7 -7
index d53543a113..65f553c20b 100644
@@ -1654,8 +1654,8 @@ static int
 mlx5_nl_family_id_cb(struct nlmsghdr *nh, void *arg)
 {
 
-	struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
-	struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
+	struct nlattr *tail = RTE_PTR_ADD((void *)nh, nh->nlmsg_len);
+	struct nlattr *nla = RTE_PTR_ADD((void *)nh, NLMSG_ALIGN(sizeof(*nh)) +
 					NLMSG_ALIGN(sizeof(struct genlmsghdr)));
 
 	for (; nla->nla_len && nla < tail;
@@ -1752,8 +1752,8 @@ mlx5_nl_roce_cb(struct nlmsghdr *nh, void *arg)
 
 	int ret = -EINVAL;
 	int *enable = arg;
-	struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
-	struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
+	struct nlattr *tail = RTE_PTR_ADD((void *)nh, nh->nlmsg_len);
+	struct nlattr *nla = RTE_PTR_ADD((void *)nh, NLMSG_ALIGN(sizeof(*nh)) +
 					NLMSG_ALIGN(sizeof(struct genlmsghdr)));
 
 	while (nla->nla_len && nla < tail) {
@@ -2049,8 +2049,8 @@ mlx5_nl_esw_multiport_cb(struct nlmsghdr *nh, void *arg)
 
 	int ret = -EINVAL;
 	int *enable = arg;
-	struct nlattr *tail = RTE_PTR_ADD(nh, nh->nlmsg_len);
-	struct nlattr *nla = RTE_PTR_ADD(nh, NLMSG_ALIGN(sizeof(*nh)) +
+	struct nlattr *tail = RTE_PTR_ADD((void *)nh, nh->nlmsg_len);
+	struct nlattr *nla = RTE_PTR_ADD((void *)nh, NLMSG_ALIGN(sizeof(*nh)) +
 					NLMSG_ALIGN(sizeof(struct genlmsghdr)));
 
 	while (nla->nla_len && nla < tail) {
@@ -2261,7 +2261,7 @@ mlx5_nl_get_mtu_bounds_cb(struct nlmsghdr *nh, void *arg)
 	struct mlx5_mtu *out = arg;
 
 	while (off < nh->nlmsg_len) {
-		struct rtattr *ra = RTE_PTR_ADD(nh, off);
+		struct rtattr *ra = RTE_PTR_ADD((void *)nh, off);
 		uint32_t *payload;
 
 		switch (ra->rta_type) {
drivers/compress/mlx5/mlx5_compress.c +1 -1
index e5325c6150..cfac5dc6c1 100644
@@ -626,7 +626,7 @@ mlx5_compress_cqe_err_handle(struct mlx5_compress_qp *qp,
 								    qp->qp.wqes;
 	volatile union mlx5_gga_compress_opaque *opaq = qp->opaque_mr.addr;
 
-	volatile uint32_t *synd_word = RTE_PTR_ADD(cqe, MLX5_ERROR_CQE_SYNDROME_OFFSET);
+	volatile uint32_t *synd_word = RTE_PTR_ADD((void *)cqe, MLX5_ERROR_CQE_SYNDROME_OFFSET);
 	switch (*synd_word) {
 	case MLX5_GGA_COMP_OUT_OF_SPACE_SYNDROME_BE:
 		op->status = RTE_COMP_OP_STATUS_OUT_OF_SPACE_TERMINATED;
drivers/crypto/cnxk/cn9k_cryptodev_ops.c +1 -1
index 5551e40cb0..d1c68436ac 100644
@@ -528,7 +528,7 @@ cn9k_cpt_sec_post_process(struct rte_crypto_op *cop,
 		hdr = rte_pktmbuf_mtod(m, struct roc_ie_on_inb_hdr *);
 
 		if (likely(m->next == NULL)) {
-			ip = PLT_PTR_ADD(hdr, ROC_IE_ON_INB_RPTR_HDR);
+			ip = PLT_PTR_ADD((void *)hdr, ROC_IE_ON_INB_RPTR_HDR);
 		} else {
 			ip = (struct rte_ipv4_hdr *)hdr;
 			hdr = infl_req->mdata;
drivers/crypto/cnxk/cnxk_cryptodev_ops.c +1 -1
index 370f311dd3..07abeb5a52 100644
@@ -523,7 +523,7 @@ cnxk_cpt_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
 			goto exit;
 		}
 
-		qp->pend_q.req_queue = PLT_PTR_ADD(
+		qp->pend_q.req_queue = PLT_PTR_ADD((void *)
 			qp->lf.cq_vaddr, ROC_CPT_CQ_ENTRY_SIZE_UNIT << qp->lf.cq_entry_size);
 	}
 
drivers/crypto/mlx5/mlx5_crypto_gcm.c +3 -3
index 89f32c7722..2c06183416 100644
@@ -370,7 +370,7 @@ mlx5_crypto_gcm_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
 		goto err;
 	}
 	qp->opaque_addr = qp->mr.addr;
-	qp->klm_array = RTE_PTR_ADD(qp->opaque_addr, opaq_size);
+	qp->klm_array = RTE_PTR_ADD((void *)qp->opaque_addr, opaq_size);
 	/*
 	 * Triple the CQ size as UMR QP which contains UMR and SEND_EN WQE
 	 * will share this CQ .
@@ -733,8 +733,8 @@ static __rte_always_inline void
 mlx5_crypto_gcm_build_send_en(struct mlx5_crypto_qp *qp)
 {
 	uint32_t wqe_offset = (qp->umr_pi & (qp->umr_wqbbs - 1)) * MLX5_SEND_WQE_BB;
-	struct mlx5_wqe_cseg *cs = RTE_PTR_ADD(qp->umr_qp_obj.wqes, wqe_offset);
-	struct mlx5_wqe_qseg *qs = RTE_PTR_ADD(cs, sizeof(struct mlx5_wqe_cseg));
+	struct mlx5_wqe_cseg *cs = RTE_PTR_ADD((void *)qp->umr_qp_obj.wqes, wqe_offset);
+	struct mlx5_wqe_qseg *qs = RTE_PTR_ADD((void *)cs, sizeof(struct mlx5_wqe_cseg));
 
 	cs->opcode = rte_cpu_to_be_32(MLX5_OPCODE_SEND_EN | ((uint32_t)qp->umr_pi << 8));
 	cs->sq_ds = rte_cpu_to_be_32((qp->umr_qp_obj.qp->id << 8) | 2);
drivers/crypto/mlx5/mlx5_crypto_xts.c +1 -1
index 1c914caa85..a9f8ab2e4d 100644
@@ -291,7 +291,7 @@ mlx5_crypto_xts_wqe_set(struct mlx5_crypto_priv *priv,
 	qp->db_pi += priv->umr_wqe_stride;
 	/* Set RDMA_WRITE WQE. */
 	cseg = RTE_PTR_ADD(cseg, priv->umr_wqe_size);
-	klms = RTE_PTR_ADD(cseg, sizeof(struct mlx5_rdma_write_wqe));
+	klms = RTE_PTR_ADD((void *)cseg, sizeof(struct mlx5_rdma_write_wqe));
 	if (!ipl) {
 		klm_n = mlx5_crypto_xts_klms_set(qp, op, op->sym->m_src, klms);
 		if (unlikely(klm_n == 0))
drivers/crypto/octeontx/otx_cryptodev_ops.c +1 -1
index 88657f49cc..dde216787f 100644
@@ -446,7 +446,7 @@ otx_cpt_enq_single_asym(struct cpt_instance *instance,
 	cop[1] = (uintptr_t)op;
 	cop[2] = cop[3] = 0ULL;
 
-	params.req = RTE_PTR_ADD(cop, 4 * sizeof(uintptr_t));
+	params.req = RTE_PTR_ADD((void *)cop, 4 * sizeof(uintptr_t));
 	params.req->op = cop;
 
 	/* Adjust meta_buf by crypto_op data  and request_info struct */
drivers/dma/idxd/idxd_internal.h +1 -0
index b80a113455..064aeb45f3 100644
@@ -56,6 +56,7 @@ struct idxd_dmadev {
 	unsigned short batch_start; /* start+size == write pointer for hdls/desc */
 	unsigned short batch_size;
 
+	/* FIXME: cast drops volatile propagation that should happen to idxd_dmadev.portal */
 	void *portal; /* address to write the batch descriptor */
 
 	struct idxd_completion *batch_comp_ring;
drivers/dma/idxd/idxd_pci.c +6 -5
index 214f6f22d5..8109ac14b2 100644
@@ -59,7 +59,7 @@ idxd_pci_dev_command(struct idxd_dmadev *idxd, enum rte_idxd_cmds command)
 	return err_code;
 }
 
-static uint32_t *
+static volatile uint32_t *
 idxd_get_wq_cfg(struct idxd_pci_common *pci, uint8_t wq_idx)
 {
 	return RTE_PTR_ADD(pci->wq_regs_base,
@@ -69,7 +69,7 @@ idxd_get_wq_cfg(struct idxd_pci_common *pci, uint8_t wq_idx)
 static int
 idxd_is_wq_enabled(struct idxd_dmadev *idxd)
 {
-	uint32_t state = idxd_get_wq_cfg(idxd->u.pci, idxd->qid)[wq_state_idx];
+	volatile uint32_t state = idxd_get_wq_cfg(idxd->u.pci, idxd->qid)[wq_state_idx];
 	return ((state >> WQ_STATE_SHIFT) & WQ_STATE_MASK) == 0x1;
 }
 
@@ -205,9 +205,9 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,
 	pci->regs = dev->mem_resource[0].addr;
 	version = pci->regs->version;
 	grp_offset = (uint16_t)pci->regs->offsets[0];
-	pci->grp_regs = RTE_PTR_ADD(pci->regs, grp_offset * 0x100);
+	pci->grp_regs = RTE_PTR_ADD((volatile void *)pci->regs, grp_offset * 0x100);
 	wq_offset = (uint16_t)(pci->regs->offsets[0] >> 16);
-	pci->wq_regs_base = RTE_PTR_ADD(pci->regs, wq_offset * 0x100);
+	pci->wq_regs_base = RTE_PTR_ADD((volatile void *)pci->regs, wq_offset * 0x100);
 	pci->portals = dev->mem_resource[2].addr;
 	pci->wq_cfg_sz = (pci->regs->wqcap >> 24) & 0x0F;
 
@@ -395,7 +395,8 @@ idxd_dmadev_probe_pci(struct rte_pci_driver *drv, struct rte_pci_device *dev)
 		/* add the queue number to each device name */
 		snprintf(qname, sizeof(qname), "%s-q%d", name, qid);
 		idxd.qid = qid;
-		idxd.portal = RTE_PTR_ADD(idxd.u.pci->portals,
+		/* FIXME: cast drops volatile propagation to idxd_dmadev.portal */
+		idxd.portal = RTE_PTR_ADD((void *)idxd.u.pci->portals,
 				qid * IDXD_PORTAL_SIZE);
 		if (idxd_is_wq_enabled(&idxd))
 			IDXD_PMD_ERR("Error, WQ %u seems enabled", qid);
drivers/dma/odm/odm_dmadev.c +2 -2
index a2f4ed9a8e..6e64998230 100644
@@ -434,7 +434,7 @@ odm_dmadev_completed(void *dev_private, uint16_t vchan, const uint16_t nb_cpls,
 	}
 
 	for (cnt = 0; cnt < nb_cpls; cnt++) {
-		cmpl_ptr = RTE_PTR_ADD(base_addr, cring_head * sizeof(cmpl));
+		cmpl_ptr = RTE_PTR_ADD((void *)base_addr, cring_head * sizeof(cmpl));
 		cmpl.u = rte_atomic_load_explicit((RTE_ATOMIC(uint32_t) *)cmpl_ptr,
 						  rte_memory_order_relaxed);
 		if (!cmpl.s.valid)
@@ -501,7 +501,7 @@ odm_dmadev_completed_status(void *dev_private, uint16_t vchan, const uint16_t nb
 #endif
 
 	for (cnt = 0; cnt < nb_cpls; cnt++) {
-		cmpl_ptr = RTE_PTR_ADD(base_addr, cring_head * sizeof(cmpl));
+		cmpl_ptr = RTE_PTR_ADD((void *)base_addr, cring_head * sizeof(cmpl));
 		cmpl.u = rte_atomic_load_explicit((RTE_ATOMIC(uint32_t) *)cmpl_ptr,
 						  rte_memory_order_relaxed);
 		if (!cmpl.s.valid)
drivers/mempool/bucket/rte_mempool_bucket.c +3 -4
index c0b480bfc7..6fee10176b 100644
@@ -376,8 +376,8 @@ count_underfilled_buckets(struct rte_mempool *mp,
 	uintptr_t align;
 	uint8_t *iter;
 
-	align = (uintptr_t)RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz) -
-		(uintptr_t)memhdr->addr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz),
+			     memhdr->addr);
 
 	for (iter = (uint8_t *)memhdr->addr + align;
 	     iter < (uint8_t *)memhdr->addr + memhdr->len;
@@ -602,8 +602,7 @@ bucket_populate(struct rte_mempool *mp, unsigned int max_objs,
 		return -EINVAL;
 
 	bucket_page_sz = rte_align32pow2(bd->bucket_mem_size);
-	align = RTE_PTR_ALIGN_CEIL((uintptr_t)vaddr, bucket_page_sz) -
-		(uintptr_t)vaddr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, bucket_page_sz), vaddr);
 
 	bucket_header_sz = bd->header_size - mp->header_size;
 	if (iova != RTE_BAD_IOVA)
drivers/ml/cnxk/cn10k_ml_model.c +1 -1
index 12a2dda800..cb47027ee6 100644
@@ -555,7 +555,7 @@ cn10k_ml_model_info_set(struct cnxk_ml_dev *cnxk_mldev, struct cnxk_ml_model *mo
 
 	metadata = &model->glow.metadata;
 	info = PLT_PTR_CAST(model->info);
-	input = PLT_PTR_ADD(info, sizeof(struct rte_ml_model_info));
+	input = PLT_PTR_ADD((void *)info, sizeof(struct rte_ml_model_info));
 	output = PLT_PTR_ADD(input, ML_CNXK_MODEL_MAX_INPUT_OUTPUT * sizeof(struct rte_ml_io_info));
 
 	/* Set model info */
drivers/ml/cnxk/cn10k_ml_ops.c +1 -1
index b30af7c7a4..8a3fcf78eb 100644
@@ -702,7 +702,7 @@ cn10k_ml_layer_load(void *device, uint16_t model_id, const char *layer_name, uin
 	layer->glow.req = PLT_PTR_ADD(mz->addr, layer_object_size + layer_scratch_size);
 
 	/* Reset burst and sync stats */
-	layer->glow.burst_xstats = PLT_PTR_ADD(
+	layer->glow.burst_xstats = PLT_PTR_ADD((void *)
 		layer->glow.req, PLT_ALIGN_CEIL(sizeof(struct cnxk_ml_req), ML_CN10K_ALIGN_SIZE));
 	for (qp_id = 0; qp_id < cnxk_mldev->mldev->data->nb_queue_pairs + 1; qp_id++) {
 		layer->glow.burst_xstats[qp_id].hw_latency_tot = 0;
drivers/ml/cnxk/cnxk_ml_ops.c +1 -1
index 9958945670..ba2c4909f9 100644
@@ -1211,7 +1211,7 @@ cnxk_ml_model_load(struct rte_ml_dev *dev, struct rte_ml_model_params *params, u
 	model->cnxk_mldev = cnxk_mldev;
 	model->type = type;
 	model->model_id = lcl_model_id;
-	model->info = PLT_PTR_ADD(
+	model->info = PLT_PTR_ADD((void *)
 		model, PLT_ALIGN_CEIL(sizeof(struct cnxk_ml_model), dev_info.align_size));
 	dev->data->models[lcl_model_id] = model;
 
drivers/net/ark/ark_ethdev.c +2 -2
index 8b25ed948f..9c34597c81 100644
@@ -713,8 +713,8 @@ ark_dev_info_get(struct rte_eth_dev *dev,
 		     struct rte_eth_dev_info *dev_info)
 {
 	struct ark_adapter *ark = dev->data->dev_private;
-	struct ark_mpu_t *tx_mpu = RTE_PTR_ADD(ark->bar0, ARK_MPU_TX_BASE);
-	struct ark_mpu_t *rx_mpu = RTE_PTR_ADD(ark->bar0, ARK_MPU_RX_BASE);
+	struct ark_mpu_t *tx_mpu = RTE_PTR_ADD((void *)ark->bar0, ARK_MPU_TX_BASE);
+	struct ark_mpu_t *rx_mpu = RTE_PTR_ADD((void *)ark->bar0, ARK_MPU_RX_BASE);
 	uint16_t ports = ark->num_ports;
 
 	dev_info->max_rx_pktlen = ARK_RX_MAX_PKT_LEN;
drivers/net/cxgbe/cxgbe_ethdev.c +3 -3
index 0c337a6cc8..f405d60449 100644
@@ -940,7 +940,7 @@ static int cxgbe_dev_xstats(struct rte_eth_dev *dev,
 				 sizeof(xstats_names[count].name),
 				 "%s", xstats_str[i].name);
 		if (xstats != NULL) {
-			stats_ptr = RTE_PTR_ADD(&ps,
+			stats_ptr = RTE_PTR_ADD((void *)&ps,
 						xstats_str[i].offset);
 			xstats[count].value = *stats_ptr;
 			xstats[count].id = count;
@@ -959,7 +959,7 @@ static int cxgbe_dev_xstats(struct rte_eth_dev *dev,
 					 "tx_q%u_%s",
 					 qid, xstats_str[i].name);
 			if (xstats != NULL) {
-				stats_ptr = RTE_PTR_ADD(&txq->stats,
+				stats_ptr = RTE_PTR_ADD((void *)&txq->stats,
 							xstats_str[i].offset);
 				xstats[count].value = *stats_ptr;
 				xstats[count].id = count;
@@ -979,7 +979,7 @@ static int cxgbe_dev_xstats(struct rte_eth_dev *dev,
 					 "rx_q%u_%s",
 					 qid, xstats_str[i].name);
 			if (xstats != NULL) {
-				stats_ptr = RTE_PTR_ADD(&rxq->stats,
+				stats_ptr = RTE_PTR_ADD((void *)&rxq->stats,
 							xstats_str[i].offset);
 				xstats[count].value = *stats_ptr;
 				xstats[count].id = count;
drivers/net/gve/base/gve_adminq.c +6 -6
index bc1759f006..0180baefc2 100644
@@ -75,7 +75,7 @@ void gve_parse_device_option(struct gve_priv *priv,
 			PMD_DRV_LOG(WARNING,
 				    GVE_DEVICE_OPTION_TOO_BIG_FMT, "GQI RDA");
 		}
-		*dev_op_gqi_rda = RTE_PTR_ADD(option, sizeof(*option));
+		*dev_op_gqi_rda = RTE_PTR_ADD((void *)option, sizeof(*option));
 		break;
 	case GVE_DEV_OPT_ID_GQI_QPL:
 		if (option_length < sizeof(**dev_op_gqi_qpl) ||
@@ -91,7 +91,7 @@ void gve_parse_device_option(struct gve_priv *priv,
 			PMD_DRV_LOG(WARNING,
 				    GVE_DEVICE_OPTION_TOO_BIG_FMT, "GQI QPL");
 		}
-		*dev_op_gqi_qpl = RTE_PTR_ADD(option, sizeof(*option));
+		*dev_op_gqi_qpl = RTE_PTR_ADD((void *)option, sizeof(*option));
 		break;
 	case GVE_DEV_OPT_ID_DQO_RDA:
 		if (option_length < sizeof(**dev_op_dqo_rda) ||
@@ -107,7 +107,7 @@ void gve_parse_device_option(struct gve_priv *priv,
 			PMD_DRV_LOG(WARNING,
 				    GVE_DEVICE_OPTION_TOO_BIG_FMT, "DQO RDA");
 		}
-		*dev_op_dqo_rda = RTE_PTR_ADD(option, sizeof(*option));
+		*dev_op_dqo_rda = RTE_PTR_ADD((void *)option, sizeof(*option));
 		break;
 	case GVE_DEV_OPT_ID_MODIFY_RING:
 		/* Min ring size bound is optional. */
@@ -127,7 +127,7 @@ void gve_parse_device_option(struct gve_priv *priv,
 				    GVE_DEVICE_OPTION_TOO_BIG_FMT,
 				    "Modify Ring");
 		}
-		*dev_op_modify_ring = RTE_PTR_ADD(option, sizeof(*option));
+		*dev_op_modify_ring = RTE_PTR_ADD((void *)option, sizeof(*option));
 
 		/* Min ring size included; set the minimum ring size. */
 		if (option_length == sizeof(**dev_op_modify_ring))
@@ -149,7 +149,7 @@ void gve_parse_device_option(struct gve_priv *priv,
 				    GVE_DEVICE_OPTION_TOO_BIG_FMT,
 				    "Jumbo Frames");
 		}
-		*dev_op_jumbo_frames = RTE_PTR_ADD(option, sizeof(*option));
+		*dev_op_jumbo_frames = RTE_PTR_ADD((void *)option, sizeof(*option));
 		break;
 	default:
 		/* If we don't recognize the option just continue
@@ -175,7 +175,7 @@ gve_process_device_options(struct gve_priv *priv,
 	int i;
 
 	/* The options struct directly follows the device descriptor. */
-	dev_opt = RTE_PTR_ADD(descriptor, sizeof(*descriptor));
+	dev_opt = RTE_PTR_ADD((void *)descriptor, sizeof(*descriptor));
 	for (i = 0; i < num_options; i++) {
 		struct gve_device_option *next_opt;
 
drivers/net/intel/ice/ice_tm.c +2 -2
index f2d8e12181..f9d86ab800 100644
@@ -456,7 +456,7 @@ ice_tm_node_add(struct rte_eth_dev *dev, uint32_t node_id,
 		tm_node->parent = NULL;
 		tm_node->reference_count = 0;
 		tm_node->shaper_profile = shaper_profile;
-		tm_node->children = RTE_PTR_ADD(tm_node, sizeof(struct ice_tm_node));
+		tm_node->children = RTE_PTR_ADD((void *)tm_node, sizeof(struct ice_tm_node));
 		tm_node->params = *params;
 		pf->tm_conf.root = tm_node;
 		return 0;
@@ -518,7 +518,7 @@ ice_tm_node_add(struct rte_eth_dev *dev, uint32_t node_id,
 	tm_node->parent = parent_node;
 	tm_node->level = level_id;
 	tm_node->shaper_profile = shaper_profile;
-	tm_node->children = RTE_PTR_ADD(tm_node, sizeof(struct ice_tm_node));
+	tm_node->children = RTE_PTR_ADD((void *)tm_node, sizeof(struct ice_tm_node));
 	tm_node->parent->children[tm_node->parent->reference_count++] = tm_node;
 	tm_node->params = *params;
 
drivers/net/intel/ixgbe/ixgbe_rxtx.c +1 -1
index a7583c178a..92fa9ddd96 100644
@@ -2852,7 +2852,7 @@ ixgbe_dev_tx_queue_setup(struct rte_eth_dev *dev,
 				 RTE_CACHE_LINE_SIZE, socket_id);
 	if (txq == NULL)
 		return -ENOMEM;
-	txq->ctx_cache = RTE_PTR_ADD(txq, sizeof(struct ci_tx_queue));
+	txq->ctx_cache = RTE_PTR_ADD((void *)txq, sizeof(struct ci_tx_queue));
 
 	/*
 	 * Allocate TX ring hardware descriptors. A memzone large enough to
drivers/net/mlx5/mlx5_flow.c +2 -2
index 2c48f1b01b..a40f5ef47a 100644
@@ -8326,9 +8326,9 @@ flow_alloc_thread_workspace(void)
 		DRV_LOG(ERR, "Failed to allocate flow workspace memory.");
 		return NULL;
 	}
-	data->rss_desc.queue = RTE_PTR_ADD(data, data_size);
+	data->rss_desc.queue = RTE_PTR_ADD((void *)data, data_size);
 #ifdef HAVE_MLX5_HWS_SUPPORT
-	data->table = RTE_PTR_ADD(data->rss_desc.queue, rss_queue_array_size);
+	data->table = RTE_PTR_ADD((void *)data->rss_desc.queue, rss_queue_array_size);
 #endif
 	return data;
 }
drivers/net/mlx5/mlx5_flow_hw.c +2 -2
index c41b99746f..15599f47e3 100644
@@ -126,7 +126,7 @@ mlx5_flow_hw_aux(uint16_t port_id, struct rte_flow_hw *flow)
 		if (rte_flow_template_table_resizable(port_id, &table->cfg.attr)) {
 			size_t offset = sizeof(struct rte_flow_hw) + mlx5dr_rule_get_handle_size();
 
-			return RTE_PTR_ADD(flow, offset);
+			return RTE_PTR_ADD((void *)flow, offset);
 		} else {
 			return &table->flow_aux[flow->idx - 1];
 		}
@@ -8123,7 +8123,7 @@ __flow_hw_actions_template_create(struct rte_eth_dev *dev,
 	at->src_off = RTE_PTR_ADD(at->dr_off,
 				  RTE_ALIGN(act_num * sizeof(*at->dr_off), 16));
 	memcpy(at->src_off, src_off, act_num * sizeof(at->src_off[0]));
-	at->orig_actions = RTE_PTR_ADD(at->src_off,
+	at->orig_actions = RTE_PTR_ADD((void *)at->src_off,
 				       RTE_ALIGN(act_num * sizeof(*at->src_off), 16));
 	orig_act_len = rte_flow_conv(RTE_FLOW_CONV_OP_ACTIONS, at->orig_actions, orig_act_len,
 				     actions, error);
drivers/net/mlx5/mlx5_tx.h +2 -2
index 16307206e2..3f7bc3c607 100644
@@ -897,7 +897,7 @@ mlx5_tx_qseg_init(struct mlx5_txq_data *restrict txq,
 {
 	struct mlx5_wqe_qseg *qs;
 
-	qs = RTE_PTR_ADD(wqe, MLX5_WSEG_SIZE);
+	qs = RTE_PTR_ADD((void *)wqe, MLX5_WSEG_SIZE);
 	qs->max_index = rte_cpu_to_be_32(wci);
 	qs->qpn_cqn = rte_cpu_to_be_32(txq->sh->txpp.clock_queue.cq_obj.cq->id);
 	qs->reserved0 = RTE_BE32(0);
@@ -928,7 +928,7 @@ mlx5_tx_wseg_init(struct mlx5_txq_data *restrict txq,
 {
 	struct mlx5_wqe_wseg *ws;
 
-	ws = RTE_PTR_ADD(wqe, MLX5_WSEG_SIZE);
+	ws = RTE_PTR_ADD((void *)wqe, MLX5_WSEG_SIZE);
 	ws->operation = rte_cpu_to_be_32(MLX5_WAIT_COND_CYCLIC_SMALLER);
 	ws->lkey = RTE_BE32(0);
 	ws->va_high = RTE_BE32(0);
drivers/net/mlx5/mlx5_txpp.c +2 -2
index 0e99b58bde..ac1a17476c 100644
@@ -194,7 +194,7 @@ mlx5_txpp_fill_wqe_rearm_queue(struct mlx5_dev_ctx_shared *sh)
 		cs->flags = RTE_BE32(MLX5_COMP_ALWAYS <<
 				     MLX5_COMP_MODE_OFFSET);
 		cs->misc = RTE_BE32(0);
-		qs = RTE_PTR_ADD(cs, sizeof(struct mlx5_wqe_cseg));
+		qs = RTE_PTR_ADD((void *)cs, sizeof(struct mlx5_wqe_cseg));
 		index = (i * MLX5_TXPP_REARM / 2 + MLX5_TXPP_REARM) &
 			((1 << MLX5_WQ_INDEX_WIDTH) - 1);
 		qs->max_index = rte_cpu_to_be_32(index);
@@ -207,7 +207,7 @@ mlx5_txpp_fill_wqe_rearm_queue(struct mlx5_dev_ctx_shared *sh)
 		cs->flags = RTE_BE32(MLX5_COMP_ONLY_ERR <<
 				     MLX5_COMP_MODE_OFFSET);
 		cs->misc = RTE_BE32(0);
-		qs = RTE_PTR_ADD(cs, sizeof(struct mlx5_wqe_cseg));
+		qs = RTE_PTR_ADD((void *)cs, sizeof(struct mlx5_wqe_cseg));
 		index = (i * MLX5_TXPP_REARM / 2 + MLX5_TXPP_REARM / 2) &
 			((1 << MLX5_CQ_INDEX_WIDTH) - 1);
 		qs->max_index = rte_cpu_to_be_32(index);
drivers/net/sfc/sfc_mae.c +5 -5
index b0e8b02b41..f31e406fb2 100644
@@ -4097,7 +4097,7 @@ sfc_mae_header_force_item_masks(uint8_t *header_buf,
 
 		for (ofst = 0; ofst < proto_header_size;
 		     ofst += sizeof(rte_be16_t)) {
-			rte_be16_t *wp = RTE_PTR_ADD(header_buf, ofst);
+			rte_be16_t *wp = RTE_PTR_ADD((void *)header_buf, ofst);
 			const rte_be16_t *w_maskp;
 			const rte_be16_t *w_specp;
 
@@ -4231,10 +4231,10 @@ sfc_mae_rule_parse_action_vxlan_encap(
 
 			proto_header_size = sizeof(struct rte_vlan_hdr);
 
-			ethertypep = RTE_PTR_ADD(buf, eth_ethertype_ofst);
+			ethertypep = RTE_PTR_ADD((void *)buf, eth_ethertype_ofst);
 			*ethertypep = RTE_BE16(RTE_ETHER_TYPE_QINQ);
 
-			ethertypep = RTE_PTR_ADD(buf, ethertype_ofst);
+			ethertypep = RTE_PTR_ADD((void *)buf, ethertype_ofst);
 			*ethertypep = RTE_BE16(RTE_ETHER_TYPE_VLAN);
 
 			ethertype_ofst =
@@ -4255,7 +4255,7 @@ sfc_mae_rule_parse_action_vxlan_encap(
 
 			proto_header_size = sizeof(struct rte_ipv4_hdr);
 
-			ethertypep = RTE_PTR_ADD(buf, ethertype_ofst);
+			ethertypep = RTE_PTR_ADD((void *)buf, ethertype_ofst);
 			*ethertypep = RTE_BE16(RTE_ETHER_TYPE_IPV4);
 
 			next_proto_ofst =
@@ -4274,7 +4274,7 @@ sfc_mae_rule_parse_action_vxlan_encap(
 
 			proto_header_size = sizeof(struct rte_ipv6_hdr);
 
-			ethertypep = RTE_PTR_ADD(buf, ethertype_ofst);
+			ethertypep = RTE_PTR_ADD((void *)buf, ethertype_ofst);
 			*ethertypep = RTE_BE16(RTE_ETHER_TYPE_IPV6);
 
 			next_proto_ofst = bounce_eh->size +
drivers/net/virtio/virtio_rxtx.c +1 -1
index edecd2011f..089811160b 100644
@@ -414,7 +414,7 @@ virtio_tso_fix_cksum(struct rte_mbuf *m)
 
 		iph = rte_pktmbuf_mtod_offset(m,
 					struct rte_ipv4_hdr *, m->l2_len);
-		th = RTE_PTR_ADD(iph, m->l3_len);
+		th = RTE_PTR_ADD((void *)iph, m->l3_len);
 
 		/*
 		 * Calculate IPv4 header checksum with current total length value
lib/eal/common/eal_common_memory.c +9 -10
index c62edf5e55..a1414e790c 100644
@@ -332,6 +332,8 @@ virt2memseg_list(const void *addr)
 		msl = &mcfg->memsegs[msl_idx];
 
 		start = msl->base_va;
+		if (start == NULL)
+			continue;
 		end = RTE_PTR_ADD(start, msl->len);
 		if (addr >= start && addr < end)
 			break;
@@ -680,10 +682,9 @@ RTE_EXPORT_SYMBOL(rte_mem_lock_page)
 int
 rte_mem_lock_page(const void *virt)
 {
-	uintptr_t virtual = (uintptr_t)virt;
 	size_t page_size = rte_mem_page_size();
-	uintptr_t aligned = RTE_PTR_ALIGN_FLOOR(virtual, page_size);
-	return rte_mem_lock((void *)aligned, page_size);
+	const void *aligned = RTE_PTR_ALIGN_FLOOR(virt, page_size);
+	return rte_mem_lock(aligned, page_size);
 }
 
 RTE_EXPORT_SYMBOL(rte_memseg_contig_walk_thread_unsafe)
@@ -1447,7 +1448,7 @@ handle_eal_memseg_info_request(const char *cmd __rte_unused,
 
 	ms_iova = ms->iova;
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = RTE_INT_PTR_ADD(ms_start_addr, ms->len);
 	ms_size = ms->len;
 	hugepage_size = ms->hugepage_sz;
 	ms_socket_id = ms->socket_id;
@@ -1519,7 +1520,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = RTE_INT_PTR_ADD(ms_start_addr, ms->len);
 	rte_mcfg_mem_read_unlock();
 
 	rte_tel_data_start_dict(d);
@@ -1530,8 +1531,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = RTE_INT_PTR_ADD(elem_start_addr, elem->size);
 
 		if ((uint64_t)elem_start_addr >= ms_start_addr &&
 		    (uint64_t)elem_end_addr <= ms_end_addr)
@@ -1597,7 +1597,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = RTE_INT_PTR_ADD(ms_start_addr, ms->len);
 
 	rte_mcfg_mem_read_unlock();
 
@@ -1609,8 +1609,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = RTE_INT_PTR_ADD(elem_start_addr, elem->size);
 
 		if (elem_start_addr < ms_start_addr ||
 				elem_end_addr > ms_end_addr) {
lib/eal/common/eal_common_options.c +1 -1
index 485655865d..d62011b037 100644
@@ -1656,7 +1656,7 @@ eal_parse_base_virtaddr(const char *arg)
 	 * on x86 and other architectures.
 	 */
 	internal_conf->base_virtaddr =
-		RTE_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
+		RTE_INT_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
 
 	return 0;
 }
lib/eal/common/malloc_elem.h +2 -2
index c7ff6718f8..22bfe96883 100644
@@ -309,10 +309,10 @@ malloc_elem_from_data(const void *data)
 	if (data == NULL)
 		return NULL;
 
-	struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
+	struct malloc_elem *elem = RTE_PTR_SUB((void *)data, MALLOC_ELEM_HEADER_LEN);
 	if (!malloc_elem_cookies_ok(elem))
 		return NULL;
-	return elem->state != ELEM_PAD ? elem:  RTE_PTR_SUB(elem, elem->pad);
+	return elem->state != ELEM_PAD ? elem : RTE_PTR_SUB((void *)elem, elem->pad);
 }
 
 /*
lib/eal/common/malloc_heap.c +5 -1
index 39240c261c..bae6de58c4 100644
@@ -1390,7 +1390,11 @@ malloc_heap_destroy(struct malloc_heap *heap)
 
 	/* Reset all of the heap but the (hold) lock so caller can release it. */
 	RTE_BUILD_BUG_ON(offsetof(struct malloc_heap, lock) != 0);
-	memset(RTE_PTR_ADD(heap, sizeof(heap->lock)), 0,
+	/* Cast to void* to avoid compiler alignment assumptions from typed pointer.
+	 * RTE_PTR_ADD preserves type, but heap+sizeof(lock) is misaligned for
+	 * struct malloc_heap, which can cause crashes with vectorized memset.
+	 */
+	memset((void *)RTE_PTR_ADD(heap, sizeof(heap->lock)), 0,
 		sizeof(*heap) - sizeof(heap->lock));
 
 	return 0;
lib/eal/include/rte_common.h +169 -5

205 lines not on this page · raw message

lib/ethdev/rte_ethdev.c +3 -3
index 2659e8d9eb..05d5b52faa 100644
@@ -3711,7 +3711,7 @@ eth_basic_stats_get(uint16_t port_id, struct rte_eth_xstat *xstats)
 
 	/* global stats */
 	for (i = 0; i < RTE_NB_STATS; i++) {
-		stats_ptr = RTE_PTR_ADD(&eth_stats,
+		stats_ptr = RTE_PTR_ADD((void *)&eth_stats,
 					eth_dev_stats_strings[i].offset);
 		val = *stats_ptr;
 		xstats[count++].value = val;
@@ -3723,7 +3723,7 @@ eth_basic_stats_get(uint16_t port_id, struct rte_eth_xstat *xstats)
 	/* per-rxq stats */
 	for (q = 0; q < nb_rxqs; q++) {
 		for (i = 0; i < RTE_NB_RXQ_STATS; i++) {
-			stats_ptr = RTE_PTR_ADD(&queue_stats,
+			stats_ptr = RTE_PTR_ADD((void *)&queue_stats,
 					eth_dev_rxq_stats_strings[i].offset +
 					q * sizeof(uint64_t));
 			val = *stats_ptr;
@@ -3734,7 +3734,7 @@ eth_basic_stats_get(uint16_t port_id, struct rte_eth_xstat *xstats)
 	/* per-txq stats */
 	for (q = 0; q < nb_txqs; q++) {
 		for (i = 0; i < RTE_NB_TXQ_STATS; i++) {
-			stats_ptr = RTE_PTR_ADD(&queue_stats,
+			stats_ptr = RTE_PTR_ADD((void *)&queue_stats,
 					eth_dev_txq_stats_strings[i].offset +
 					q * sizeof(uint64_t));
 			val = *stats_ptr;
lib/graph/graph_populate.c +2 -2
index 026daecb21..4ec20c26b1 100644
@@ -62,7 +62,7 @@ graph_header_popluate(struct graph *_graph)
 	graph->head = (int32_t)-_graph->src_node_count;
 	graph->cir_mask = _graph->cir_mask;
 	graph->nb_nodes = _graph->node_count;
-	graph->cir_start = RTE_PTR_ADD(graph, _graph->cir_start);
+	graph->cir_start = RTE_PTR_ADD((void *)graph, _graph->cir_start);
 	graph->nodes_start = _graph->nodes_start;
 	graph->socket = _graph->socket;
 	graph->id = _graph->id;
@@ -81,7 +81,7 @@ graph_nodes_populate(struct graph *_graph)
 	rte_node_t pid;
 
 	STAILQ_FOREACH(graph_node, &_graph->node_list, next) {
-		struct rte_node *node = RTE_PTR_ADD(graph, off);
+		struct rte_node *node = RTE_PTR_ADD((void *)graph, off);
 		memset(node, 0, sizeof(*node));
 		node->fence = RTE_GRAPH_FENCE;
 		node->off = off;
lib/graph/graph_stats.c +1 -1
index b87b5707f7..e7b56966eb 100644
@@ -501,7 +501,7 @@ cluster_node_arregate_stats(struct cluster_node *cluster, bool dispatch)
 
 		if (node->xstat_off == 0)
 			continue;
-		xstat = RTE_PTR_ADD(node, node->xstat_off);
+		xstat = RTE_PTR_ADD((void *)node, node->xstat_off);
 		for (i = 0; i < stat->xstat_cntrs; i++)
 			stat->xstat_count[i] += xstat[i];
 	}
lib/graph/rte_graph.h +2 -2
index 7e433f4661..7eb7495eae 100644
@@ -407,9 +407,9 @@ void rte_graph_obj_dump(FILE *f, struct rte_graph *graph, bool all);
 /** Macro to browse rte_node object after the graph creation */
 #define rte_graph_foreach_node(count, off, graph, node)                        \
 	for (count = 0, off = graph->nodes_start,                              \
-	     node = RTE_PTR_ADD(graph, off);                                   \
+	     node = RTE_PTR_ADD((void *)graph, off);                           \
 	     count < graph->nb_nodes;                                          \
-	     off = node->next, node = RTE_PTR_ADD(graph, off), count++)
+	     off = node->next, node = RTE_PTR_ADD((void *)graph, off), count++)
 
 /**
  * Get node object with in graph from id.
lib/graph/rte_graph_model_mcore_dispatch.c +1 -1
index 706b5469f0..cd3fc130d3 100644
@@ -154,7 +154,7 @@ __rte_graph_mcore_dispatch_sched_wq_process(struct rte_graph *graph)
 
 	for (i = 0; i < n; i++) {
 		wq_node = wq_nodes[i];
-		node = RTE_PTR_ADD(graph, wq_node->node_off);
+		node = RTE_PTR_ADD((void *)graph, wq_node->node_off);
 		RTE_ASSERT(node->fence == RTE_GRAPH_FENCE);
 		idx = node->idx;
 		free_space = node->size - idx;
lib/latencystats/rte_latencystats.c +1 -1
index f61d5a273f..cc27205500 100644
@@ -105,7 +105,7 @@ latencystats_collect(uint64_t values[])
 	const uint64_t *stats;
 
 	for (i = 0; i < NUM_LATENCY_STATS; i++) {
-		stats = RTE_PTR_ADD(glob_stats, lat_stats_strings[i].offset);
+		stats = RTE_PTR_ADD((void *)glob_stats, lat_stats_strings[i].offset);
 		scale = lat_stats_strings[i].scale;
 
 		/* used to mark samples which are not a time interval */
lib/mbuf/rte_mbuf.c +1 -1
index 0d931c7a15..61d174cba3 100644
@@ -215,7 +215,7 @@ __rte_pktmbuf_init_extmem(struct rte_mempool *mp,
 	m->next = NULL;
 
 	/* init external buffer shared info items */
-	shinfo = RTE_PTR_ADD(m, mbuf_size);
+	shinfo = RTE_PTR_ADD((void *)m, mbuf_size);
 	m->shinfo = shinfo;
 	shinfo->free_cb = rte_pktmbuf_free_pinned_extmem;
 	shinfo->fcb_opaque = m;
lib/member/rte_xxh64_avx512.h +3 -3
index 58f896ebb8..774b26d8df 100644
@@ -58,7 +58,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 			 _mm512_set1_epi64(key_len));
 
 	while (remaining >= 8) {
-		input = _mm512_set1_epi64(*(uint64_t *)RTE_PTR_ADD(key, offset));
+		input = _mm512_set1_epi64(*(const uint64_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 				xxh64_round_avx512(_mm512_setzero_si512(), input));
 		v_hash = _mm512_madd52lo_epu64(_mm512_set1_epi64(PRIME64_4),
@@ -71,7 +71,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	if (remaining >= 4) {
 		input = _mm512_set1_epi64
-			(*(uint32_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint32_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_1)));
@@ -86,7 +86,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	while (remaining != 0) {
 		input = _mm512_set1_epi64
-			(*(uint8_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint8_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_5)));
lib/pdcp/pdcp_entity.h +5 -3
index f854192e98..e8251cbdbc 100644
@@ -198,17 +198,19 @@ struct entity_priv_ul_part {
 
 static inline struct entity_priv *
 entity_priv_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity));
+	return RTE_PTR_ADD((void *)entity, sizeof(struct rte_pdcp_entity));
 }
 
 static inline struct entity_priv_dl_part *
 entity_dl_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD((void *)entity,
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline struct entity_priv_ul_part *
 entity_ul_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD((void *)entity,
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline int
lib/reorder/rte_reorder.c +1 -1
index be06530860..5caebf20d4 100644
@@ -120,7 +120,7 @@ rte_reorder_init(struct rte_reorder_buffer *b, unsigned int bufsize,
 	b->order_buf.size = b->ready_buf.size = size;
 	b->order_buf.mask = b->ready_buf.mask = size - 1;
 	b->ready_buf.entries = (void *)&b[1];
-	b->order_buf.entries = RTE_PTR_ADD(&b[1],
+	b->order_buf.entries = RTE_PTR_ADD((void *)&b[1],
 			size * sizeof(b->ready_buf.entries[0]));
 
 	return b;
Signature · 1 line
2.39.5 (Apple Git-154)
#17 lore raw

PATCH v12 1/1 eal: RTE_PTR_ADD/SUB API improvements

In reply to [email protected]

From: Scott Mitchell <[email protected]>

RTE_PTR_ADD and RTE_PTR_SUB APIs have a few limitations:
1. ptr cast to uintptr_t drops pointer provenance and prevents compiler optimizations
2. return cast discards qualifiers (const, volatile) which may hide correctness/concurrency issues.
3. Accepts both "pointers" and "integers as pointers" which overloads the use case and constrains the implementation to address other challenges.

This patch splits the API on two dimensions:
1. pointer types
2. integer types that represent pointers

This split allows addressing each of the challenges above and provides distinct APIs for the distinct use cases. Examples:
1. Clang is able to optimize and improve __rte_raw_cksum (which uses RTE_PTR_ADD) by ~40% (100 bytes) to ~8x (1.5k bytes) TSC cycles/byte.
2. Refactoring discovered cases that dropped qualifiers (volatile) which that the new API exposes.

---
v12:
- void* return type to avoid optimizations assuming aligned access which isn't generally safe/true.

v11:
- Split API into PTR and INT_PTR variants, update all usage of PTR API for new APIs.

v10:
- Use unit_test_suite_runner for easier subtest failure identification

v9:
- Fix include order: system includes, then DPDK includes, then application includes
- Use NOHUGE_OK and ASAN_OK constants in REGISTER_FAST_TEST (instead of true, true)

v8:
- Remove tests for types < 32-bit (bool, char, short, uint8_t, uint16_t)
- Merge test_ptr_add_sub_typedefs() into test_ptr_add_sub_integer_types()
- Separate RTE_PTR_ADD and RTE_PTR_SUB documentation
- Move Clang/GCC implementation note from Doxygen to regular comment
- Tests verify both intermediate ADD result and SUB round-trip
- Use uintptr_t cast consistently for all integer-to-pointer conversions
- Make TEST_RETVAL calculated from TEST_INITVAL + TEST_INCREMENT

v7:
- Fix tests: use TEST_BUFFER_SIZE macro for buffer allocation
- Fix tests: ADD then SUB same amount to avoid out-of-bounds pointer arithmetic
- All RTE_PTR_SUB tests now verify round-trip (ADD+SUB returns to original)

v6:
- Make char* optimization Clang-only to avoid GCC false positive warnings
- Improve tests: use named constants instead of magic numbers
- Improve tests: use void* casts on expected values instead of uintptr_t on results

v5:
- Initial implementation with char* arithmetic for all compilers

v4:
- Used _Generic for type-based dispatch with char* casts
- Had warnings on both Clang and GCC due to _Generic type-checking all branches

---
 app/test-pmd/cmdline_flow.c                 |   4 +-
 app/test/meson.build                        |   1 +
 app/test/test_common.c                      |  20 +-
 app/test/test_ptr_add_sub.c                 | 197 +++++++++++++++++++
 drivers/bus/cdx/cdx_vfio.c                  |   6 +-
 drivers/bus/pci/linux/pci.c                 |   6 +-
 drivers/bus/vmbus/linux/vmbus_uio.c         |   6 +-
 drivers/common/cnxk/roc_cpt_debug.c         |   4 +-
 drivers/common/cnxk/roc_nix_bpf.c           |   2 +-
 drivers/common/cnxk/roc_nix_inl.h           |   4 +-
 drivers/common/cnxk/roc_nix_inl_dp.h        |   8 +-
 drivers/common/cnxk/roc_platform.h          |   7 +-
 drivers/common/mlx5/mlx5_common_mr.c        |   2 +-
 drivers/dma/idxd/idxd_pci.c                 |   9 +-
 drivers/dma/odm/odm_dmadev.c                |   4 +-
 drivers/event/cnxk/cn10k_worker.c           |   2 +-
 drivers/event/cnxk/cn20k_worker.c           |   2 +-
 drivers/mempool/bucket/rte_mempool_bucket.c |   7 +-
 drivers/mempool/dpaa/dpaa_mempool.c         |   2 +-
 drivers/net/cxgbe/sge.c                     |   4 +-
 drivers/net/ena/ena_ethdev.c                |   9 +-
 drivers/net/intel/fm10k/fm10k.h             |   6 +-
 drivers/net/intel/fm10k/fm10k_rxtx_vec.c    |  12 +-
 drivers/net/mlx4/mlx4_txq.c                 |   3 +-
 lib/eal/common/eal_common_fbarray.c         |   2 +
 lib/eal/common/eal_common_memory.c          |  31 +--
 lib/eal/common/eal_common_options.c         |   2 +-
 lib/eal/common/malloc_elem.h                |   8 +-
 lib/eal/freebsd/eal_memory.c                |   4 +
 lib/eal/include/rte_common.h                | 206 ++++++++++++++++++--
 lib/eal/linux/eal_memalloc.c                |   6 +
 lib/eal/linux/eal_memory.c                  |   7 +
 lib/eal/windows/eal_memalloc.c              |   6 +
 lib/graph/rte_graph.h                       |   4 +-
 lib/latencystats/rte_latencystats.c         |   3 +
 lib/mbuf/rte_mbuf.c                         |   1 +
 lib/mbuf/rte_mbuf.h                         |   2 +
 lib/member/rte_xxh64_avx512.h               |   6 +-
 lib/mempool/rte_mempool.c                   |   8 +-
 lib/mempool/rte_mempool.h                   |   3 +
 lib/mempool/rte_mempool_ops_default.c       |   2 +-
 lib/pdcp/pdcp_entity.h                      |   8 +-
 lib/vhost/vhost_user.c                      |  13 +-
 43 files changed, 550 insertions(+), 99 deletions(-)
 create mode 100644 app/test/test_ptr_add_sub.c
app/test-pmd/cmdline_flow.c +2 -2
index ebc036b14b..8c2fc6c7b6 100644
@@ -12468,7 +12468,7 @@ parse_meter_color(struct context *ctx, const struct token *token,
 
 		if (!arg)
 			return -1;
-		*(int *)RTE_PTR_ADD(action->conf, arg->offset) = i;
+		*(int *)RTE_PTR_ADD(RTE_PTR_UNQUAL(action->conf), arg->offset) = i;
 	} else {
 		((struct rte_flow_item_meter_color *)
 			ctx->object)->color = (enum rte_color)i;
@@ -13351,7 +13351,7 @@ indirect_action_flow_conf_create(const struct buffer *in)
 		indlst_conf = NULL;
 		goto end;
 	}
-	indlst_conf->conf = RTE_PTR_ADD(indlst_conf, base + len);
+	indlst_conf->conf = (const void **)RTE_PTR_ADD(indlst_conf, base + len);
 	for (i = 0; i < indlst_conf->conf_num; i++)
 		indlst_conf->conf[i] = indlst_conf->actions[i].conf;
 	SLIST_INSERT_HEAD(&indlst_conf_head, indlst_conf, next);
app/test/meson.build +1 -0
index f4d04a6e42..aa56fc4297 100644
@@ -152,6 +152,7 @@ source_file_deps = {
     'test_power_intel_uncore.c': ['power', 'power_intel_uncore'],
     'test_power_kvm_vm.c': ['power', 'power_kvm_vm'],
     'test_prefetch.c': [],
+    'test_ptr_add_sub.c': [],
     'test_ptr_compress.c': ['ptr_compress'],
     'test_rand_perf.c': [],
     'test_rawdev.c': ['rawdev', 'bus_vdev', 'raw_skeleton'],
app/test/test_common.c +10 -10
index 3e1c7df0c1..83a689155f 100644
@@ -37,10 +37,10 @@ test_macros(int __rte_unused unused_parm)
 	RTE_SWAP(smaller, bigger);
 	RTE_TEST_ASSERT(smaller == BIGGER && bigger == SMALLER,
 		"RTE_SWAP");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF), BIGGER,
-		"RTE_PTR_ADD");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF), SMALLER,
-		"RTE_PTR_SUB");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR_ADD(SMALLER, PTR_DIFF), (void *) BIGGER,
+		"RTE_INT_PTR_ADD");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR_SUB(BIGGER, PTR_DIFF), (void *) SMALLER,
+		"RTE_INT_PTR_SUB");
 	RTE_TEST_ASSERT_EQUAL(RTE_PTR_DIFF(BIGGER, SMALLER), PTR_DIFF,
 		"RTE_PTR_DIFF");
 	RTE_TEST_ASSERT_EQUAL(RTE_MAX(SMALLER, BIGGER), BIGGER,
@@ -188,18 +188,18 @@ test_align(void)
 			if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
-			val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN_FLOOR((uintptr_t) i, p);
 			if (ERROR_FLOOR(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_FLOOR", i, p);
 
 			val = RTE_ALIGN_FLOOR(i, p);
 			if (ERROR_FLOOR(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
 			/* align ceiling */
-			val = RTE_PTR_ALIGN((uintptr_t) i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN((uintptr_t) i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN", i, p);
 
 			val = RTE_ALIGN(i, p);
 			if (ERROR_CEIL(val, i, p))
@@ -209,9 +209,9 @@ test_align(void)
 			if (ERROR_CEIL(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
 
-			val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN_CEIL((uintptr_t)i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_CEIL", i, p);
 
 			/* by this point we know that val is aligned to p */
 			if (!rte_is_aligned((void*)(uintptr_t) val, p))
app/test/test_ptr_add_sub.c new file +197 -0

197 lines not on this page · raw message

drivers/bus/cdx/cdx_vfio.c +5 -1
index 11fe3265d2..5db9de3aaa 100644
@@ -368,9 +368,13 @@ static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
 	size_t sz = msl->len;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
+	void *end_va;
 	void **max_va = arg;
 
+	if (msl->base_va == NULL)
+		return 0;
+
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
 	if (*max_va < end_va)
 		*max_va = end_va;
 	return 0;
drivers/bus/pci/linux/pci.c +5 -1
index 2ffac82e94..455db2cdec 100644
@@ -109,9 +109,13 @@ static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
 	size_t sz = msl->len;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
+	void *end_va;
 	void **max_va = arg;
 
+	if (msl->base_va == NULL)
+		return 0;
+
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
 	if (*max_va < end_va)
 		*max_va = end_va;
 	return 0;
drivers/bus/vmbus/linux/vmbus_uio.c +5 -1
index fbafc5027d..0ef05c0096 100644
@@ -122,9 +122,13 @@ static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
 	size_t sz = msl->memseg_arr.len * msl->page_sz;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
+	void *end_va;
 	void **max_va = arg;
 
+	if (msl->base_va == NULL)
+		return 0;
+
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
 	if (*max_va < end_va)
 		*max_va = end_va;
 	return 0;
drivers/common/cnxk/roc_cpt_debug.c +2 -2
index 28aedf088e..252658a83f 100644
@@ -56,7 +56,7 @@ cpt_cnxk_parse_hdr_dump(FILE *file, const struct cpt_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*32B */
 	offset = cpth->w2.ptr_offset;
 	offset = (((offset - 1) & 0x7) + 1) * 32;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD(PLT_PTR_UNQUAL(cpth), offset);
 
 	if (cpth->w0.num_frags > 0) {
 		cpt_dump(file, "CPT Fraginfo_0 \t%p:", frag_info);
@@ -162,7 +162,7 @@ cpt_cn10k_parse_hdr_dump(FILE *file, const struct cpt_cn10k_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*8B */
 	offset = cpth->w2.fi_offset;
 	offset = (((offset - 1) & 0x1f) + 1) * 8;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD(PLT_PTR_UNQUAL(cpth), offset);
 
 	cpt_dump(file, "CPT Fraginfo \t0x%p:", frag_info);
 
drivers/common/cnxk/roc_nix_bpf.c +1 -1
index 98c9855a5b..05fad44581 100644
@@ -160,7 +160,7 @@ nix_precolor_conv_table_write(struct roc_nix *roc_nix, uint64_t val,
 	struct nix *nix = roc_nix_to_nix_priv(roc_nix);
 	int64_t *addr;
 
-	addr = PLT_PTR_ADD(nix->base, off);
+	addr = PLT_INT_PTR_ADD(nix->base, off);
 	plt_write64(val, addr);
 }
 
drivers/common/cnxk/roc_nix_inl.h +2 -2
index 7970ac2258..90d6aa5025 100644
@@ -59,7 +59,7 @@ roc_nix_inl_on_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline struct roc_ie_on_outb_sa *
@@ -67,7 +67,7 @@ roc_nix_inl_on_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline void *
drivers/common/cnxk/roc_nix_inl_dp.h +4 -4
index eb101db179..7061cd39ac 100644
@@ -49,7 +49,7 @@ roc_nix_inl_ot_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline struct roc_ot_ipsec_outb_sa *
@@ -57,7 +57,7 @@ roc_nix_inl_ot_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline void *
@@ -77,7 +77,7 @@ roc_nix_inl_ow_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline struct roc_ow_ipsec_outb_sa *
@@ -85,7 +85,7 @@ roc_nix_inl_ow_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline void *
drivers/common/cnxk/roc_platform.h +5 -2
index e22a50d47a..9261d26175 100644
@@ -47,6 +47,9 @@
 #define PLT_PTR_ADD		 RTE_PTR_ADD
 #define PLT_PTR_SUB		 RTE_PTR_SUB
 #define PLT_PTR_DIFF		 RTE_PTR_DIFF
+#define PLT_PTR_UNQUAL		 RTE_PTR_UNQUAL
+#define PLT_INT_PTR_ADD		 RTE_INT_PTR_ADD
+#define PLT_INT_PTR_SUB		 RTE_INT_PTR_SUB
 #define PLT_MAX_RXTX_INTR_VEC_ID RTE_MAX_RXTX_INTR_VEC_ID
 #define PLT_INTR_VEC_RXTX_OFFSET RTE_INTR_VEC_RXTX_OFFSET
 #define PLT_MIN			 RTE_MIN
@@ -84,8 +87,8 @@
 #define PLT_U16_CAST(val) ((uint16_t)(val))
 
 /* Add / Sub pointer with scalar and cast to uint64_t */
-#define PLT_PTR_ADD_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_PTR_ADD(__ptr, __x))
-#define PLT_PTR_SUB_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_PTR_SUB(__ptr, __x))
+#define PLT_PTR_ADD_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_INT_PTR_ADD(__ptr, __x))
+#define PLT_PTR_SUB_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_INT_PTR_SUB(__ptr, __x))
 
 /** Divide ceil */
 #define PLT_DIV_CEIL(x, y)			\
drivers/common/mlx5/mlx5_common_mr.c +1 -1
index 8ed988dec9..aae39fdb6e 100644
@@ -1447,7 +1447,7 @@ mlx5_mempool_get_extmem_cb(struct rte_mempool *mp, void *opaque,
 	seg = &heap[data->heap_size - 1];
 	msl = rte_mem_virt2memseg_list((void *)addr);
 	page_size = msl != NULL ? msl->page_sz : rte_mem_page_size();
-	page_start = RTE_PTR_ALIGN_FLOOR(addr, page_size);
+	page_start = RTE_ALIGN_FLOOR(addr, page_size);
 	seg->start = page_start;
 	seg->end = page_start + page_size;
 	/* Maintain the heap order. */
drivers/dma/idxd/idxd_pci.c +5 -4
index 214f6f22d5..0a35a687e8 100644
@@ -59,7 +59,7 @@ idxd_pci_dev_command(struct idxd_dmadev *idxd, enum rte_idxd_cmds command)
 	return err_code;
 }
 
-static uint32_t *
+static volatile uint32_t *
 idxd_get_wq_cfg(struct idxd_pci_common *pci, uint8_t wq_idx)
 {
 	return RTE_PTR_ADD(pci->wq_regs_base,
@@ -205,9 +205,9 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,
 	pci->regs = dev->mem_resource[0].addr;
 	version = pci->regs->version;
 	grp_offset = (uint16_t)pci->regs->offsets[0];
-	pci->grp_regs = RTE_PTR_ADD(pci->regs, grp_offset * 0x100);
+	pci->grp_regs = RTE_PTR_ADD((volatile void *)pci->regs, grp_offset * 0x100);
 	wq_offset = (uint16_t)(pci->regs->offsets[0] >> 16);
-	pci->wq_regs_base = RTE_PTR_ADD(pci->regs, wq_offset * 0x100);
+	pci->wq_regs_base = RTE_PTR_ADD((volatile void *)pci->regs, wq_offset * 0x100);
 	pci->portals = dev->mem_resource[2].addr;
 	pci->wq_cfg_sz = (pci->regs->wqcap >> 24) & 0x0F;
 
@@ -395,7 +395,8 @@ idxd_dmadev_probe_pci(struct rte_pci_driver *drv, struct rte_pci_device *dev)
 		/* add the queue number to each device name */
 		snprintf(qname, sizeof(qname), "%s-q%d", name, qid);
 		idxd.qid = qid;
-		idxd.portal = RTE_PTR_ADD(idxd.u.pci->portals,
+		/* FIXME: cast drops volatile propagation to idxd_dmadev.portal */
+		idxd.portal = RTE_PTR_ADD(RTE_PTR_UNQUAL(idxd.u.pci->portals),
 				qid * IDXD_PORTAL_SIZE);
 		if (idxd_is_wq_enabled(&idxd))
 			IDXD_PMD_ERR("Error, WQ %u seems enabled", qid);
drivers/dma/odm/odm_dmadev.c +2 -2
index a2f4ed9a8e..3b4b058427 100644
@@ -422,7 +422,7 @@ odm_dmadev_completed(void *dev_private, uint16_t vchan, const uint16_t nb_cpls,
 	int cnt;
 
 	vq = &odm->vq[vchan];
-	const uint32_t *base_addr = vq->cring_mz->addr;
+	uint32_t *base_addr = vq->cring_mz->addr;
 	const uint16_t cring_max_entry = vq->cring_max_entry;
 
 	cring_head = vq->cring_head;
@@ -482,7 +482,7 @@ odm_dmadev_completed_status(void *dev_private, uint16_t vchan, const uint16_t nb
 	int cnt;
 
 	vq = &odm->vq[vchan];
-	const uint32_t *base_addr = vq->cring_mz->addr;
+	uint32_t *base_addr = vq->cring_mz->addr;
 	const uint16_t cring_max_entry = vq->cring_max_entry;
 
 	cring_head = vq->cring_head;
drivers/event/cnxk/cn10k_worker.c +1 -1
index 80077ec8a1..b596cb8702 100644
@@ -338,7 +338,7 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 		aw0 |= ev[0].event & (BIT_ULL(32) - 1);
 		aw0 |= (uint64_t)ev[0].sched_type << 32;
 		*((__uint128_t *)lmt_addr) = aw0;
-		lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+		lmt_addr += 16;
 	}
 #endif
 
drivers/event/cnxk/cn20k_worker.c +1 -1
index 53daf3b4b0..e8e60d727c 100644
@@ -296,7 +296,7 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 		aw0 |= ev[0].event & (BIT_ULL(32) - 1);
 		aw0 |= (uint64_t)ev[0].sched_type << 32;
 		*((__uint128_t *)lmt_addr) = aw0;
-		lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+		lmt_addr += 16;
 	}
 #endif
 
drivers/mempool/bucket/rte_mempool_bucket.c +3 -4
index c0b480bfc7..6fee10176b 100644
@@ -376,8 +376,8 @@ count_underfilled_buckets(struct rte_mempool *mp,
 	uintptr_t align;
 	uint8_t *iter;
 
-	align = (uintptr_t)RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz) -
-		(uintptr_t)memhdr->addr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz),
+			     memhdr->addr);
 
 	for (iter = (uint8_t *)memhdr->addr + align;
 	     iter < (uint8_t *)memhdr->addr + memhdr->len;
@@ -602,8 +602,7 @@ bucket_populate(struct rte_mempool *mp, unsigned int max_objs,
 		return -EINVAL;
 
 	bucket_page_sz = rte_align32pow2(bd->bucket_mem_size);
-	align = RTE_PTR_ALIGN_CEIL((uintptr_t)vaddr, bucket_page_sz) -
-		(uintptr_t)vaddr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, bucket_page_sz), vaddr);
 
 	bucket_header_sz = bd->header_size - mp->header_size;
 	if (iova != RTE_BAD_IOVA)
drivers/mempool/dpaa/dpaa_mempool.c +1 -1
index 2f9395b3f4..85e1c01017 100644
@@ -321,7 +321,7 @@ dpaa_adjust_obj_bounds(char *va, size_t *offset,
 	size_t off = *offset;
 
 	if (dpaa_check_obj_bounds(va + off, pg_sz, total) == false) {
-		off += RTE_PTR_ALIGN_CEIL(va + off, pg_sz) - (va + off);
+		off += RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(va + off, pg_sz), va + off);
 		if (flags & RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ)
 			off += total - ((((size_t)va + off - 1) % total) + 1);
 	}
drivers/net/cxgbe/sge.c +2 -2
index e9d45f24c4..a5d112d52d 100644
@@ -591,7 +591,7 @@ static void write_sgl(struct rte_mbuf *mbuf, struct sge_txq *q,
 			memcpy(sgl->sge, buf, part0);
 		part1 = RTE_PTR_DIFF((u8 *)end, (u8 *)q->stat);
 		rte_memcpy(q->desc, RTE_PTR_ADD((u8 *)buf, part0), part1);
-		end = RTE_PTR_ADD((void *)q->desc, part1);
+		end = RTE_PTR_ADD(q->desc, part1);
 	}
 	if ((uintptr_t)end & 8)           /* 0-pad to multiple of 16 */
 		*(u64 *)end = 0;
@@ -1297,7 +1297,7 @@ static void inline_tx_mbuf(const struct sge_txq *q, caddr_t from, caddr_t *to,
 		from = RTE_PTR_ADD(from, left);
 		left = len - left;
 		rte_memcpy((void *)q->desc, from, left);
-		*to = RTE_PTR_ADD((void *)q->desc, left);
+		*to = RTE_PTR_ADD(q->desc, left);
 	}
 }
 
drivers/net/ena/ena_ethdev.c +8 -1
index ea4afbc75d..da23b271d5 100644
@@ -2379,7 +2379,14 @@ static void *pci_bar_addr(struct rte_pci_device *dev, uint32_t bar)
 {
 	const struct rte_mem_resource *res = &dev->mem_resource[bar];
 	size_t offset = res->phys_addr % rte_mem_page_size();
-	void *vaddr = RTE_PTR_ADD(res->addr, offset);
+	void *vaddr;
+
+	if (res->addr == NULL) {
+		PMD_INIT_LOG_LINE(ERR, "PCI BAR [%u] address is NULL", bar);
+		return NULL;
+	}
+
+	vaddr = RTE_PTR_ADD(res->addr, offset);
 
 	PMD_INIT_LOG_LINE(INFO, "PCI BAR [%u]: phys_addr=0x%" PRIx64 ", addr=%p, offset=0x%zx, adjusted_addr=%p",
 		bar, res->phys_addr, res->addr, offset, vaddr);
drivers/net/intel/fm10k/fm10k.h +3 -3
index 0eb32ac0d0..4e3081785f 100644
@@ -264,9 +264,9 @@ fm10k_pktmbuf_reset(struct rte_mbuf *mb, uint16_t in_port)
 	mb->nb_segs = 1;
 
 	/* enforce 512B alignment on default Rx virtual addresses */
-	mb->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb->buf_addr +
-			RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-			- (char *)mb->buf_addr);
+	mb->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb->buf_addr +
+			RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+			(char *)mb->buf_addr);
 	mb->port = in_port;
 }
 
drivers/net/intel/fm10k/fm10k_rxtx_vec.c +6 -6
index 0eada7275e..a08af75bc7 100644
@@ -315,12 +315,12 @@ fm10k_rxq_rearm(struct fm10k_rx_queue *rxq)
 		_mm_store_si128(RTE_CAST_PTR(__m128i *, &rxdp++->q), dma_addr1);
 
 		/* enforce 512B alignment on default Rx virtual addresses */
-		mb0->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb0->buf_addr
-				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-				- (char *)mb0->buf_addr);
-		mb1->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb1->buf_addr
-				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-				- (char *)mb1->buf_addr);
+		mb0->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb0->buf_addr
+				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+				(char *)mb0->buf_addr);
+		mb1->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb1->buf_addr
+				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+				(char *)mb1->buf_addr);
 	}
 
 	rxq->rxrearm_start += RTE_FM10K_RXQ_REARM_THRESH;
drivers/net/mlx4/mlx4_txq.c +2 -1
index 0db2e55bef..348040c1b9 100644
@@ -114,7 +114,8 @@ txq_uar_uninit_secondary(struct txq *txq)
 	void *addr;
 
 	addr = ppriv->uar_table[txq->stats.idx];
-	munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
+	if (addr)
+		munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
 }
 
 /**
lib/eal/common/eal_common_fbarray.c +2 -0
index 8bdcefb717..c4e03f45f7 100644
@@ -10,6 +10,7 @@
 #include <unistd.h>
 
 #include <rte_common.h>
+#include <rte_debug.h>
 #include <rte_eal_paging.h>
 #include <rte_errno.h>
 #include <rte_log.h>
@@ -1058,6 +1059,7 @@ rte_fbarray_get(const struct rte_fbarray *arr, unsigned int idx)
 		return NULL;
 	}
 
+	RTE_ASSERT(arr->data);
 	ret = RTE_PTR_ADD(arr->data, idx * arr->elt_sz);
 
 	return ret;
lib/eal/common/eal_common_memory.c +20 -11
index c62edf5e55..ee7054ec14 100644
@@ -309,6 +309,9 @@ virt2memseg(const void *addr, const struct rte_memseg_list *msl)
 
 	/* a memseg list was specified, check if it's the right one */
 	start = msl->base_va;
+	if (start == NULL)
+		return NULL;
+
 	end = RTE_PTR_ADD(start, msl->len);
 
 	if (addr < start || addr >= end)
@@ -332,6 +335,8 @@ virt2memseg_list(const void *addr)
 		msl = &mcfg->memsegs[msl_idx];
 
 		start = msl->base_va;
+		if (start == NULL)
+			continue;
 		end = RTE_PTR_ADD(start, msl->len);
 		if (addr >= start && addr < end)
 			break;
@@ -680,10 +685,16 @@ RTE_EXPORT_SYMBOL(rte_mem_lock_page)
 int
 rte_mem_lock_page(const void *virt)
 {
-	uintptr_t virtual = (uintptr_t)virt;
 	size_t page_size = rte_mem_page_size();
-	uintptr_t aligned = RTE_PTR_ALIGN_FLOOR(virtual, page_size);
-	return rte_mem_lock((void *)aligned, page_size);
+	const void *aligned;
+
+	if (virt == NULL) {
+		rte_errno = EINVAL;
+		return -1;
+	}
+
+	aligned = RTE_PTR_ALIGN_FLOOR(virt, page_size);
+	return rte_mem_lock(aligned, page_size);
 }
 
 RTE_EXPORT_SYMBOL(rte_memseg_contig_walk_thread_unsafe)
@@ -1447,7 +1458,7 @@ handle_eal_memseg_info_request(const char *cmd __rte_unused,
 
 	ms_iova = ms->iova;
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 	ms_size = ms->len;
 	hugepage_size = ms->hugepage_sz;
 	ms_socket_id = ms->socket_id;
@@ -1519,7 +1530,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 	rte_mcfg_mem_read_unlock();
 
 	rte_tel_data_start_dict(d);
@@ -1530,8 +1541,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = elem_start_addr + elem->size;
 
 		if ((uint64_t)elem_start_addr >= ms_start_addr &&
 		    (uint64_t)elem_end_addr <= ms_end_addr)
@@ -1553,7 +1563,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	struct rte_mem_config *mcfg;
 	struct rte_memseg_list *msl;
 	const struct rte_memseg *ms;
-	struct malloc_elem *elem;
+	struct malloc_elem *volatile elem;
 	struct malloc_heap *heap;
 	struct rte_tel_data *c;
 	uint64_t ms_start_addr, ms_end_addr;
@@ -1597,7 +1607,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 
 	rte_mcfg_mem_read_unlock();
 
@@ -1609,8 +1619,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = elem_start_addr + elem->size;
 
 		if (elem_start_addr < ms_start_addr ||
 				elem_end_addr > ms_end_addr) {
lib/eal/common/eal_common_options.c +1 -1
index 485655865d..0e05683a67 100644
@@ -1656,7 +1656,7 @@ eal_parse_base_virtaddr(const char *arg)
 	 * on x86 and other architectures.
 	 */
 	internal_conf->base_virtaddr =
-		RTE_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
+		(uintptr_t) RTE_INT_PTR_ALIGN_CEIL(addr, (size_t)RTE_PGSIZE_16M);
 
 	return 0;
 }
lib/eal/common/malloc_elem.h +4 -4
index c7ff6718f8..304a1fdce6 100644
@@ -79,8 +79,8 @@ static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
 #define MALLOC_TRAILER_COOKIE  0xadd2e55badbadbadULL /**< Trailer cookie.*/
 
 /* define macros to make referencing the header and trailer cookies easier */
-#define MALLOC_ELEM_TRAILER(elem) (*((uint64_t*)RTE_PTR_ADD(elem, \
-		elem->size - MALLOC_ELEM_TRAILER_LEN)))
+#define MALLOC_ELEM_TRAILER(elem) \
+	(*((const uint64_t *)RTE_PTR_ADD(elem, elem->size - MALLOC_ELEM_TRAILER_LEN)))
 #define MALLOC_ELEM_HEADER(elem) (elem->header_cookie)
 
 static inline void
@@ -309,10 +309,10 @@ malloc_elem_from_data(const void *data)
 	if (data == NULL)
 		return NULL;
 
-	struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
+	struct malloc_elem *elem = RTE_PTR_SUB(RTE_PTR_UNQUAL(data), MALLOC_ELEM_HEADER_LEN);
 	if (!malloc_elem_cookies_ok(elem))
 		return NULL;
-	return elem->state != ELEM_PAD ? elem:  RTE_PTR_SUB(elem, elem->pad);
+	return elem->state != ELEM_PAD ? elem : RTE_PTR_SUB(elem, elem->pad);
 }
 
 /*
lib/eal/freebsd/eal_memory.c +4 -0
index 6d3d46a390..49a89fe2fb 100644
@@ -178,6 +178,10 @@ rte_eal_hugepage_init(void)
 					"RTE_MAX_MEMSEG_PER_TYPE and/or RTE_MAX_MEM_MB_PER_TYPE in configuration.");
 				return -1;
 			}
+			if (msl->base_va == NULL) {
+				EAL_LOG(ERR, "Base VA is NULL for memseg list %d", msl_idx);
+				return -1;
+			}
 			arr = &msl->memseg_arr;
 			seg = rte_fbarray_get(arr, ms_idx);
 
lib/eal/include/rte_common.h +192 -14

244 lines not on this page · raw message

lib/eal/linux/eal_memalloc.c +6 -0
index 1e60e21620..f770826a43 100644
@@ -835,6 +835,12 @@ alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
 		void *map_addr;
 
 		cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
+
+		if (cur_msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			goto out;
+		}
+
 		map_addr = RTE_PTR_ADD(cur_msl->base_va,
 				cur_idx * page_sz);
 
lib/eal/linux/eal_memory.c +7 -0
index 8e1763e890..3830bd5d7f 100644
@@ -759,6 +759,13 @@ remap_segment(struct hugepage_file *hugepages, int seg_start, int seg_end)
 			return -1;
 		}
 		memseg_len = (size_t)page_sz;
+
+		if (msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			close(fd);
+			return -1;
+		}
+
 		addr = RTE_PTR_ADD(msl->base_va, ms_idx * memseg_len);
 
 		/* we know this address is already mmapped by memseg list, so
lib/eal/windows/eal_memalloc.c +6 -0
index 5db5a474cc..6432caccd3 100644
@@ -230,6 +230,12 @@ alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
 		void *map_addr;
 
 		cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
+
+		if (cur_msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			goto out;
+		}
+
 		map_addr = RTE_PTR_ADD(cur_msl->base_va, cur_idx * page_sz);
 
 		if (alloc_seg(cur, map_addr, wa->socket, wa->hi)) {
lib/graph/rte_graph.h +2 -2
index 7e433f4661..d8ac0011e4 100644
@@ -407,9 +407,9 @@ void rte_graph_obj_dump(FILE *f, struct rte_graph *graph, bool all);
 /** Macro to browse rte_node object after the graph creation */
 #define rte_graph_foreach_node(count, off, graph, node)                        \
 	for (count = 0, off = graph->nodes_start,                              \
-	     node = RTE_PTR_ADD(graph, off);                                   \
+	     node = RTE_PTR_ADD(RTE_PTR_UNQUAL(graph), off);                           \
 	     count < graph->nb_nodes;                                          \
-	     off = node->next, node = RTE_PTR_ADD(graph, off), count++)
+	     off = node->next, node = RTE_PTR_ADD(RTE_PTR_UNQUAL(graph), off), count++)
 
 /**
  * Get node object with in graph from id.
lib/latencystats/rte_latencystats.c +3 -0
index f61d5a273f..f2cd0db4b7 100644
@@ -104,6 +104,9 @@ latencystats_collect(uint64_t values[])
 	unsigned int i, scale;
 	const uint64_t *stats;
 
+	if (glob_stats == NULL)
+		return;
+
 	for (i = 0; i < NUM_LATENCY_STATS; i++) {
 		stats = RTE_PTR_ADD(glob_stats, lat_stats_strings[i].offset);
 		scale = lat_stats_strings[i].scale;
lib/mbuf/rte_mbuf.c +1 -0
index 0d931c7a15..557954d45e 100644
@@ -193,6 +193,7 @@ __rte_pktmbuf_init_extmem(struct rte_mempool *mp,
 
 	RTE_ASSERT(ctx->ext < ctx->ext_num);
 	RTE_ASSERT(ctx->off + ext_mem->elt_size <= ext_mem->buf_len);
+	RTE_ASSERT(ext_mem->buf_ptr);
 
 	m->buf_addr = RTE_PTR_ADD(ext_mem->buf_ptr, ctx->off);
 	rte_mbuf_iova_set(m, ext_mem->buf_iova == RTE_BAD_IOVA ? RTE_BAD_IOVA :
lib/mbuf/rte_mbuf.h +2 -0
index 2004391f57..3100feb740 100644
@@ -217,6 +217,7 @@ rte_mbuf_data_iova_default(const struct rte_mbuf *mb)
 static inline struct rte_mbuf *
 rte_mbuf_from_indirect(struct rte_mbuf *mi)
 {
+	RTE_ASSERT(mi);
 	return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
 }
 
@@ -289,6 +290,7 @@ rte_mbuf_to_baddr(struct rte_mbuf *md)
 static inline void *
 rte_mbuf_to_priv(struct rte_mbuf *m)
 {
+	RTE_ASSERT(m);
 	return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
 }
 
lib/member/rte_xxh64_avx512.h +3 -3
index 58f896ebb8..774b26d8df 100644
@@ -58,7 +58,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 			 _mm512_set1_epi64(key_len));
 
 	while (remaining >= 8) {
-		input = _mm512_set1_epi64(*(uint64_t *)RTE_PTR_ADD(key, offset));
+		input = _mm512_set1_epi64(*(const uint64_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 				xxh64_round_avx512(_mm512_setzero_si512(), input));
 		v_hash = _mm512_madd52lo_epu64(_mm512_set1_epi64(PRIME64_4),
@@ -71,7 +71,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	if (remaining >= 4) {
 		input = _mm512_set1_epi64
-			(*(uint32_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint32_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_1)));
@@ -86,7 +86,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	while (remaining != 0) {
 		input = _mm512_set1_epi64
-			(*(uint8_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint8_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_5)));
lib/mempool/rte_mempool.c +4 -4
index 3042d94c14..f1ff668205 100644
@@ -349,9 +349,9 @@ rte_mempool_populate_iova(struct rte_mempool *mp, char *vaddr,
 	memhdr->opaque = opaque;
 
 	if (mp->flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
-		off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
+		off = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, 8), vaddr);
 	else
-		off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN) - vaddr;
+		off = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN), vaddr);
 
 	if (off > len) {
 		ret = 0;
@@ -425,8 +425,8 @@ rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
 
 		/* populate with the largest group of contiguous pages */
 		for (phys_len = RTE_MIN(
-			(size_t)(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz) -
-				(addr + off)),
+			(size_t)RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz),
+				addr + off),
 			len - off);
 		     off + phys_len < len;
 		     phys_len = RTE_MIN(phys_len + pg_sz, len - off)) {
lib/mempool/rte_mempool.h +3 -0
index aedc100964..091976574a 100644
@@ -376,6 +376,7 @@ struct __rte_cache_aligned rte_mempool {
 static inline struct rte_mempool_objhdr *
 rte_mempool_get_header(void *obj)
 {
+	RTE_ASSERT(obj);
 	return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
 		sizeof(struct rte_mempool_objhdr));
 }
@@ -399,6 +400,7 @@ static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
 static inline struct rte_mempool_objtlr *rte_mempool_get_trailer(void *obj)
 {
 	struct rte_mempool *mp = rte_mempool_from_obj(obj);
+	RTE_ASSERT(mp);
 	return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
 }
 
@@ -1844,6 +1846,7 @@ rte_mempool_empty(const struct rte_mempool *mp)
 static inline rte_iova_t
 rte_mempool_virt2iova(const void *elt)
 {
+	RTE_ASSERT(elt);
 	const struct rte_mempool_objhdr *hdr;
 	hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
 		sizeof(*hdr));
lib/mempool/rte_mempool_ops_default.c +1 -1
index d27d6fc473..e28a288b91 100644
@@ -117,7 +117,7 @@ rte_mempool_op_populate_helper(struct rte_mempool *mp, unsigned int flags,
 	for (i = 0; i < max_objs; i++) {
 		/* avoid objects to cross page boundaries */
 		if (check_obj_bounds(va + off, pg_sz, total_elt_sz) < 0) {
-			off += RTE_PTR_ALIGN_CEIL(va + off, pg_sz) - (va + off);
+			off += RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(va + off, pg_sz), va + off);
 			if (flags & RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ)
 				off += total_elt_sz -
 					(((uintptr_t)(va + off - 1) %
lib/pdcp/pdcp_entity.h +5 -3
index f854192e98..7a2c41dd56 100644
@@ -198,17 +198,19 @@ struct entity_priv_ul_part {
 
 static inline struct entity_priv *
 entity_priv_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity), sizeof(struct rte_pdcp_entity));
 }
 
 static inline struct entity_priv_dl_part *
 entity_dl_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity),
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline struct entity_priv_ul_part *
 entity_ul_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity),
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline int
lib/vhost/vhost_user.c +10 -3
index 4bfb13fb98..a9ab6487f2 100644
@@ -824,9 +824,16 @@ void
 mem_set_dump(struct virtio_net *dev, void *ptr, size_t size, bool enable, uint64_t pagesz)
 {
 #ifdef MADV_DONTDUMP
-	void *start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz);
-	uintptr_t end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz);
-	size_t len = end - (uintptr_t)start;
+	void *start;
+	uintptr_t end;
+	size_t len;
+
+	if (ptr == NULL)
+		return;
+
+	start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz);
+	end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz);
+	len = end - (uintptr_t)start;
 
 	if (madvise(start, len, enable ? MADV_DODUMP : MADV_DONTDUMP) == -1) {
 		VHOST_CONFIG_LOG(dev->ifname, INFO,
Signature · 1 line
2.39.5 (Apple Git-154)
#18 lore raw

[REVIEW] eal: RTE_PTR_ADD/SUB API improvements

AI-generated review of ptr-add.mbo
Reviewed using Claude (claude-opus-4-5-20251101)

This is an automated review. Please verify all suggestions.

---

# DPDK Patch Review: eal: RTE_PTR_ADD/SUB API improvements

## Summary
This patch introduces improvements to the RTE_PTR_ADD/SUB macros by splitting the API into pointer-based and integer-based variants to preserve pointer provenance and type qualifiers.

---

## Errors (Must Fix)

### 1. Subject Line Exceeds 60 Characters
**Location:** Commit subject
**Issue:** Subject line "eal: RTE_PTR_ADD/SUB API improvements" is 39 characters, which is acceptable. However, the format should use lowercase after the colon. **Severity:** The subject looks acceptable.

### 2. Missing Blank Line Between Tag Groups
**Location:** Commit message tags
**Issue:** The Signed-off-by tag appears without proper separation from the body text. According to guidelines, there should be a blank line before Reported-by, Suggested-by, Signed-off-by group. **Current:**

```
...the new API exposes.

Signed-off-by: Scott Mitchell <[email protected]>
```

**Expected:** Blank line separation is present, so this is acceptable.

### 3. Unnecessary Cast of void* in Test File **Location:** app/test/test_ptr_add_sub.c, line 145

```c
uint32_t *u32p = (uint32_t *)buffer;
```

**Issue:** While this cast is necessary for the type conversion, consider using a union or aligned buffer to avoid potential alignment issues.

---

## Warnings (Should Fix)

### 1. Documentation Does Not Match Code for RTE_PTR_UNQUAL
**Location:** lib/eal/include/rte_common.h
**Issue:** The patch adds RTE_PTR_UNQUAL macro usage throughout the codebase but the macro definition is not shown in the patch. If this is a new macro, it needs documentation.

### 2. Use of __auto_type Without Fallback for All Compilers **Location:** lib/eal/include/rte_common.h, lines 106-111

```c
#ifdef __cplusplus
#define __rte_auto_type auto
#elif defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
#define __rte_auto_type __auto_type
#endif
```

**Issue:** No fallback defined for compilers other than GCC/Clang in C mode. This could cause compilation failures with other compilers.

### 3. Implicit Pointer Comparison **Location:** drivers/bus/cdx/cdx_vfio.c, line 374

```c
if (msl->base_va == NULL)
```

**Status:** This is correct per guidelines (explicit NULL comparison).

### 4. Missing Cc: [email protected] Tag
**Location:** Commit message
**Issue:** This patch fixes issues that could affect existing code (compiler optimization issues, qualifier dropping). If these are considered bug fixes for stable releases, the Cc: [email protected] tag should be added.

### 5. Variable Declaration Style Inconsistency **Location:** drivers/bus/cdx/cdx_vfio.c, lines 371-372

```c
size_t sz = msl->len;
void *end_va;
```

**Issue:** Mixed declaration styles - sz is initialized at declaration while end_va is not. Consider consistency within the function.

### 6. FIXME Comment Left in Code **Location:** drivers/dma/idxd/idxd_pci.c, line 398

```c
/* FIXME: cast drops volatile propagation to idxd_dmadev.portal */
```

**Issue:** FIXME comments should be resolved before merging, or tracked in a bug tracker.

### 7. Missing Test Registration Constant Documentation **Location:** app/test/test_ptr_add_sub.c, line 197

```c
REGISTER_FAST_TEST(ptr_add_sub_autotest, NOHUGE_OK, ASAN_OK, test_ptr_add_sub_suite);
```

**Status:** Good - uses named constants NOHUGE_OK and ASAN_OK as required.

### 8. Inconsistent Error Handling Pattern
**Location:** Multiple files
**Issue:** Some NULL checks return early, others use goto. For example:
- drivers/bus/cdx/cdx_vfio.c: returns 0
- lib/eal/linux/eal_memalloc.c: uses goto

While both may be correct for their contexts, ensure consistency within each file.

---

## Info (Consider)

### 1. Good Practice: Using TEST_ASSERT Macros
**Location:** app/test/test_ptr_add_sub.c
**Status:** The test file correctly uses TEST_ASSERT_EQUAL and follows the unit_test_suite_runner pattern as required by guidelines.

### 2. Good Practice: Proper SPDX and Copyright **Location:** app/test/test_ptr_add_sub.c, lines 1-3

```c
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2026 Apple Inc.
 */
```

**Status:** Correct format with SPDX on first line, copyright following, blank line before includes.

### 3. Good Practice: Include Order **Location:** app/test/test_ptr_add_sub.c, lines 5-9

```c
#include <stdint.h>

#include <rte_common.h>

#include "test.h"
```

**Status:** Correct order - system includes, DPDK includes, application includes, with blank lines separating groups.

### 4. Complex Macro Implementation
**Location:** lib/eal/include/rte_common.h, RTE_PTR_ADD/SUB macros
**Observation:** The new implementation using __extension__, __rte_auto_type, and diagnostic pragmas is complex. Consider adding implementation notes as regular comments (which was done in v8 per changelog).

### 5. Potential ABI Impact
**Location:** lib/eal/include/rte_common.h
**Observation:** While these are macro changes (not ABI), the behavioral change could affect compiled code. The release notes should document this change clearly.

### 6. Line Length Compliance
**Location:** Various files
**Status:** Lines appear to be within the 100-character limit for source code.

### 7. Good Practice: Defensive NULL Checks Added
**Location:** Multiple files (vhost_user.c, ena_ethdev.c, rte_mbuf.h, etc.) **Status:** The patch adds appropriate NULL checks before pointer operations, improving robustness.

### 8. Cast Style in Tests
**Location:** app/test/test_common.c, line 40

```c
RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR_ADD(SMALLER, PTR_DIFF), (void *) BIGGER,
```

**Note:** Space before BIGGER in cast (void *) BIGGER - minor style inconsistency but not an error.

---

## Release Notes Requirement

This patch introduces API changes (new macros, modified behavior of existing macros) and should include updates to doc/guides/rel_notes/ for the current release noting:
- New RTE_INT_PTR_ADD, RTE_INT_PTR_SUB macros
- New RTE_INT_PTR_ALIGN_* macros
- New RTE_PTR_UNQUAL macro (if defined in this patch)
- Modified behavior of RTE_PTR_ADD/RTE_PTR_SUB to preserve qualifiers

---

## Overall Assessment

The patch is well-structured and addresses real issues with pointer provenance and qualifier preservation. The test coverage is good, following DPDK test framework conventions. Main concerns are:

1. Missing definition/documentation for RTE_PTR_UNQUAL macro
2. FIXME comment should be resolved
3. Release notes should be updated for API changes
4. Consider adding fallback for __rte_auto_type for non-GCC/Clang compilers

#19 lore raw

Re: [REVIEW] eal: RTE_PTR_ADD/SUB API improvements

v13 coming soon addressing the relevant comments and some compilation warnings on newer compilers.

#20 lore raw

RE: [REVIEW] eal: RTE_PTR_ADD/SUB API improvements

In reply to Stephen Hemminger

Stephen,

The email subject should reflect which version of the patch the review covers.

PS: Thanks for all the work you are putting into AI review and automating it, Stephen. It's great to follow the progress from experimentation to production. :-)

-Morten

#21 lore raw

PATCH v13 1/1 eal: RTE_PTR_ADD/SUB API improvements

In reply to [email protected]

From: Scott Mitchell <[email protected]>

RTE_PTR_ADD and RTE_PTR_SUB APIs have a few limitations:
1. ptr cast to uintptr_t drops pointer provenance and prevents compiler optimizations
2. return cast discards qualifiers (const, volatile) which may hide correctness/concurrency issues.
3. Accepts both "pointers" and "integers as pointers" which overloads the use case and constrains the implementation to address other challenges.

This patch splits the API on two dimensions:
1. pointer types
2. integer types that represent pointers

This split allows addressing each of the challenges above and provides distinct APIs for the distinct use cases. Examples:
1. Clang is able to optimize and improve __rte_raw_cksum (which uses RTE_PTR_ADD) by ~40% (100 bytes) to ~8x (1.5k bytes) TSC cycles/byte.
2. Refactoring discovered cases that dropped qualifiers (volatile) that the new API exposes.

---
v13:
- Added release notes documenting API changes
- Fixed alignment in test file: use alignas(uint32_t) for buffer
- Fixed NULL pointer handling in cdx_vfio.c: check base_va before RTE_PTR_ADD
- Added GCC array-bounds diagnostic suppression in malloc_elem_from_data()
- Added bug tracker reference for volatile cast issue in idxd_pci.c
- Improved __rte_auto_type documentation: added C++11 and C23 support
- Moved doxygen rationale for void* return type to @return blocks
- Fixed MALLOC_ELEM_TRAILER to use RTE_PTR_UNQUAL for write operations

v12:
- void* return type to avoid optimizations assuming aligned access which isn't generally safe/true.

v11:
- Split API into PTR and INT_PTR variants, update all usage of PTR API for new APIs.

v10:
- Use unit_test_suite_runner for easier subtest failure identification

v9:
- Fix include order: system includes, then DPDK includes, then application includes
- Use NOHUGE_OK and ASAN_OK constants in REGISTER_FAST_TEST (instead of true, true)

v8:
- Remove tests for types < 32-bit (bool, char, short, uint8_t, uint16_t)
- Merge test_ptr_add_sub_typedefs() into test_ptr_add_sub_integer_types()
- Separate RTE_PTR_ADD and RTE_PTR_SUB documentation
- Move Clang/GCC implementation note from Doxygen to regular comment
- Tests verify both intermediate ADD result and SUB round-trip
- Use uintptr_t cast consistently for all integer-to-pointer conversions
- Make TEST_RETVAL calculated from TEST_INITVAL + TEST_INCREMENT

v7:
- Fix tests: use TEST_BUFFER_SIZE macro for buffer allocation
- Fix tests: ADD then SUB same amount to avoid out-of-bounds pointer arithmetic
- All RTE_PTR_SUB tests now verify round-trip (ADD+SUB returns to original)

v6:
- Make char* optimization Clang-only to avoid GCC false positive warnings
- Improve tests: use named constants instead of magic numbers
- Improve tests: use void* casts on expected values instead of uintptr_t on results

v5:
- Initial implementation with char* arithmetic for all compilers

v4:
- Used _Generic for type-based dispatch with char* casts
- Had warnings on both Clang and GCC due to _Generic type-checking all branches

___

 app/test-pmd/cmdline_flow.c                 |   4 +-
 app/test/meson.build                        |   1 +
 app/test/test_common.c                      |  20 +-
 app/test/test_ptr_add_sub.c                 | 199 ++++++++++++++++++
 doc/guides/rel_notes/release_26_03.rst      |  11 +
 drivers/bus/cdx/cdx_vfio.c                  |  13 +-
 drivers/bus/pci/linux/pci.c                 |   6 +-
 drivers/bus/vmbus/linux/vmbus_uio.c         |   6 +-
 drivers/common/cnxk/roc_cpt_debug.c         |   4 +-
 drivers/common/cnxk/roc_nix_bpf.c           |   2 +-
 drivers/common/cnxk/roc_nix_inl.h           |   4 +-
 drivers/common/cnxk/roc_nix_inl_dp.h        |   8 +-
 drivers/common/cnxk/roc_platform.h          |   7 +-
 drivers/common/mlx5/mlx5_common_mr.c        |   2 +-
 drivers/dma/idxd/idxd_pci.c                 |  11 +-
 drivers/dma/odm/odm_dmadev.c                |   4 +-
 drivers/event/cnxk/cn10k_worker.c           |   2 +-
 drivers/event/cnxk/cn20k_worker.c           |   2 +-
 drivers/mempool/bucket/rte_mempool_bucket.c |   7 +-
 drivers/mempool/dpaa/dpaa_mempool.c         |   2 +-
 drivers/net/cxgbe/sge.c                     |   4 +-
 drivers/net/ena/ena_ethdev.c                |   9 +-
 drivers/net/intel/fm10k/fm10k.h             |   6 +-
 drivers/net/intel/fm10k/fm10k_rxtx_vec.c    |  12 +-
 drivers/net/mlx4/mlx4_txq.c                 |   3 +-
 lib/eal/common/eal_common_fbarray.c         |   2 +
 lib/eal/common/eal_common_memory.c          |  31 ++-
 lib/eal/common/eal_common_options.c         |   2 +-
 lib/eal/common/malloc_elem.h                |  20 +-
 lib/eal/freebsd/eal_memory.c                |   4 +
 lib/eal/include/rte_common.h                | 219 ++++++++++++++++++--
 lib/eal/linux/eal_memalloc.c                |   6 +
 lib/eal/linux/eal_memory.c                  |   7 +
 lib/eal/windows/eal_memalloc.c              |   6 +
 lib/graph/rte_graph.h                       |   4 +-
 lib/latencystats/rte_latencystats.c         |   3 +
 lib/mbuf/rte_mbuf.c                         |   1 +
 lib/mbuf/rte_mbuf.h                         |   2 +
 lib/member/rte_xxh64_avx512.h               |   6 +-
 lib/mempool/rte_mempool.c                   |   8 +-
 lib/mempool/rte_mempool.h                   |   3 +
 lib/mempool/rte_mempool_ops_default.c       |   2 +-
 lib/pdcp/pdcp_entity.h                      |   8 +-
 lib/vhost/vhost_user.c                      |  13 +-
 44 files changed, 594 insertions(+), 102 deletions(-)
 create mode 100644 app/test/test_ptr_add_sub.c
app/test-pmd/cmdline_flow.c +2 -2
index ebc036b14b..8c2fc6c7b6 100644
@@ -12468,7 +12468,7 @@ parse_meter_color(struct context *ctx, const struct token *token,
 
 		if (!arg)
 			return -1;
-		*(int *)RTE_PTR_ADD(action->conf, arg->offset) = i;
+		*(int *)RTE_PTR_ADD(RTE_PTR_UNQUAL(action->conf), arg->offset) = i;
 	} else {
 		((struct rte_flow_item_meter_color *)
 			ctx->object)->color = (enum rte_color)i;
@@ -13351,7 +13351,7 @@ indirect_action_flow_conf_create(const struct buffer *in)
 		indlst_conf = NULL;
 		goto end;
 	}
-	indlst_conf->conf = RTE_PTR_ADD(indlst_conf, base + len);
+	indlst_conf->conf = (const void **)RTE_PTR_ADD(indlst_conf, base + len);
 	for (i = 0; i < indlst_conf->conf_num; i++)
 		indlst_conf->conf[i] = indlst_conf->actions[i].conf;
 	SLIST_INSERT_HEAD(&indlst_conf_head, indlst_conf, next);
app/test/meson.build +1 -0
index f4d04a6e42..aa56fc4297 100644
@@ -152,6 +152,7 @@ source_file_deps = {
     'test_power_intel_uncore.c': ['power', 'power_intel_uncore'],
     'test_power_kvm_vm.c': ['power', 'power_kvm_vm'],
     'test_prefetch.c': [],
+    'test_ptr_add_sub.c': [],
     'test_ptr_compress.c': ['ptr_compress'],
     'test_rand_perf.c': [],
     'test_rawdev.c': ['rawdev', 'bus_vdev', 'raw_skeleton'],
app/test/test_common.c +10 -10
index 3e1c7df0c1..299700f84b 100644
@@ -37,10 +37,10 @@ test_macros(int __rte_unused unused_parm)
 	RTE_SWAP(smaller, bigger);
 	RTE_TEST_ASSERT(smaller == BIGGER && bigger == SMALLER,
 		"RTE_SWAP");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF), BIGGER,
-		"RTE_PTR_ADD");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF), SMALLER,
-		"RTE_PTR_SUB");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR_ADD(SMALLER, PTR_DIFF), (void *)BIGGER,
+		"RTE_INT_PTR_ADD");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR_SUB(BIGGER, PTR_DIFF), (void *)SMALLER,
+		"RTE_INT_PTR_SUB");
 	RTE_TEST_ASSERT_EQUAL(RTE_PTR_DIFF(BIGGER, SMALLER), PTR_DIFF,
 		"RTE_PTR_DIFF");
 	RTE_TEST_ASSERT_EQUAL(RTE_MAX(SMALLER, BIGGER), BIGGER,
@@ -188,18 +188,18 @@ test_align(void)
 			if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
-			val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN_FLOOR((uintptr_t) i, p);
 			if (ERROR_FLOOR(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_FLOOR", i, p);
 
 			val = RTE_ALIGN_FLOOR(i, p);
 			if (ERROR_FLOOR(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
 			/* align ceiling */
-			val = RTE_PTR_ALIGN((uintptr_t) i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN((uintptr_t) i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN", i, p);
 
 			val = RTE_ALIGN(i, p);
 			if (ERROR_CEIL(val, i, p))
@@ -209,9 +209,9 @@ test_align(void)
 			if (ERROR_CEIL(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
 
-			val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN_CEIL((uintptr_t)i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_CEIL", i, p);
 
 			/* by this point we know that val is aligned to p */
 			if (!rte_is_aligned((void*)(uintptr_t) val, p))
app/test/test_ptr_add_sub.c new file +199 -0

199 lines not on this page · raw message

doc/guides/rel_notes/release_26_03.rst +11 -0
index 15dabee7a1..1a793204d0 100644
@@ -84,6 +84,17 @@ API Changes
    Also, make sure to start the actual text at the margin.
    =======================================================
 
+* eal: Improved pointer arithmetic macros to preserve pointer provenance and type qualifiers.
+
+  * ``RTE_PTR_ADD`` and ``RTE_PTR_SUB`` now preserve const/volatile qualifiers
+    and use pointer arithmetic instead of integer casts to enable compiler optimizations.
+  * Passing NULL to ``RTE_PTR_ADD`` or ``RTE_PTR_SUB`` is now undefined behavior.
+  * Added ``RTE_INT_PTR_ADD`` and ``RTE_INT_PTR_SUB`` for integer-as-pointer arithmetic.
+  * Added ``RTE_INT_PTR_ALIGN``, ``RTE_INT_PTR_ALIGN_FLOOR``, and ``RTE_INT_PTR_ALIGN_CEIL``
+    for aligning integer addresses.
+  * Existing code using ``RTE_PTR_ADD``/``RTE_PTR_SUB`` with integer types should migrate
+    to ``RTE_INT_PTR_*`` variants for clarity and correctness.
+
 
 ABI Changes
 -----------
drivers/bus/cdx/cdx_vfio.c +10 -3
index 11fe3265d2..a1009bc0ca 100644
@@ -367,9 +367,16 @@ cdx_vfio_get_region_info(int vfio_dev_fd, struct vfio_region_info **info,
 static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
-	size_t sz = msl->len;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
-	void **max_va = arg;
+	size_t sz;
+	void *end_va;
+	void **max_va;
+
+	if (msl->base_va == NULL)
+		return 0;
+
+	sz = msl->len;
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
+	max_va = arg;
 
 	if (*max_va < end_va)
 		*max_va = end_va;
drivers/bus/pci/linux/pci.c +5 -1
index 2ffac82e94..455db2cdec 100644
@@ -109,9 +109,13 @@ static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
 	size_t sz = msl->len;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
+	void *end_va;
 	void **max_va = arg;
 
+	if (msl->base_va == NULL)
+		return 0;
+
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
 	if (*max_va < end_va)
 		*max_va = end_va;
 	return 0;
drivers/bus/vmbus/linux/vmbus_uio.c +5 -1
index fbafc5027d..0ef05c0096 100644
@@ -122,9 +122,13 @@ static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
 	size_t sz = msl->memseg_arr.len * msl->page_sz;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
+	void *end_va;
 	void **max_va = arg;
 
+	if (msl->base_va == NULL)
+		return 0;
+
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
 	if (*max_va < end_va)
 		*max_va = end_va;
 	return 0;
drivers/common/cnxk/roc_cpt_debug.c +2 -2
index 28aedf088e..252658a83f 100644
@@ -56,7 +56,7 @@ cpt_cnxk_parse_hdr_dump(FILE *file, const struct cpt_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*32B */
 	offset = cpth->w2.ptr_offset;
 	offset = (((offset - 1) & 0x7) + 1) * 32;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD(PLT_PTR_UNQUAL(cpth), offset);
 
 	if (cpth->w0.num_frags > 0) {
 		cpt_dump(file, "CPT Fraginfo_0 \t%p:", frag_info);
@@ -162,7 +162,7 @@ cpt_cn10k_parse_hdr_dump(FILE *file, const struct cpt_cn10k_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*8B */
 	offset = cpth->w2.fi_offset;
 	offset = (((offset - 1) & 0x1f) + 1) * 8;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD(PLT_PTR_UNQUAL(cpth), offset);
 
 	cpt_dump(file, "CPT Fraginfo \t0x%p:", frag_info);
 
drivers/common/cnxk/roc_nix_bpf.c +1 -1
index 98c9855a5b..05fad44581 100644
@@ -160,7 +160,7 @@ nix_precolor_conv_table_write(struct roc_nix *roc_nix, uint64_t val,
 	struct nix *nix = roc_nix_to_nix_priv(roc_nix);
 	int64_t *addr;
 
-	addr = PLT_PTR_ADD(nix->base, off);
+	addr = PLT_INT_PTR_ADD(nix->base, off);
 	plt_write64(val, addr);
 }
 
drivers/common/cnxk/roc_nix_inl.h +2 -2
index 7970ac2258..90d6aa5025 100644
@@ -59,7 +59,7 @@ roc_nix_inl_on_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline struct roc_ie_on_outb_sa *
@@ -67,7 +67,7 @@ roc_nix_inl_on_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline void *
drivers/common/cnxk/roc_nix_inl_dp.h +4 -4
index eb101db179..7061cd39ac 100644
@@ -49,7 +49,7 @@ roc_nix_inl_ot_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline struct roc_ot_ipsec_outb_sa *
@@ -57,7 +57,7 @@ roc_nix_inl_ot_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline void *
@@ -77,7 +77,7 @@ roc_nix_inl_ow_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline struct roc_ow_ipsec_outb_sa *
@@ -85,7 +85,7 @@ roc_nix_inl_ow_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline void *
drivers/common/cnxk/roc_platform.h +5 -2
index e22a50d47a..9261d26175 100644
@@ -47,6 +47,9 @@
 #define PLT_PTR_ADD		 RTE_PTR_ADD
 #define PLT_PTR_SUB		 RTE_PTR_SUB
 #define PLT_PTR_DIFF		 RTE_PTR_DIFF
+#define PLT_PTR_UNQUAL		 RTE_PTR_UNQUAL
+#define PLT_INT_PTR_ADD		 RTE_INT_PTR_ADD
+#define PLT_INT_PTR_SUB		 RTE_INT_PTR_SUB
 #define PLT_MAX_RXTX_INTR_VEC_ID RTE_MAX_RXTX_INTR_VEC_ID
 #define PLT_INTR_VEC_RXTX_OFFSET RTE_INTR_VEC_RXTX_OFFSET
 #define PLT_MIN			 RTE_MIN
@@ -84,8 +87,8 @@
 #define PLT_U16_CAST(val) ((uint16_t)(val))
 
 /* Add / Sub pointer with scalar and cast to uint64_t */
-#define PLT_PTR_ADD_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_PTR_ADD(__ptr, __x))
-#define PLT_PTR_SUB_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_PTR_SUB(__ptr, __x))
+#define PLT_PTR_ADD_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_INT_PTR_ADD(__ptr, __x))
+#define PLT_PTR_SUB_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_INT_PTR_SUB(__ptr, __x))
 
 /** Divide ceil */
 #define PLT_DIV_CEIL(x, y)			\
drivers/common/mlx5/mlx5_common_mr.c +1 -1
index 8ed988dec9..aae39fdb6e 100644
@@ -1447,7 +1447,7 @@ mlx5_mempool_get_extmem_cb(struct rte_mempool *mp, void *opaque,
 	seg = &heap[data->heap_size - 1];
 	msl = rte_mem_virt2memseg_list((void *)addr);
 	page_size = msl != NULL ? msl->page_sz : rte_mem_page_size();
-	page_start = RTE_PTR_ALIGN_FLOOR(addr, page_size);
+	page_start = RTE_ALIGN_FLOOR(addr, page_size);
 	seg->start = page_start;
 	seg->end = page_start + page_size;
 	/* Maintain the heap order. */
drivers/dma/idxd/idxd_pci.c +7 -4
index 214f6f22d5..fb76a050b1 100644
@@ -59,7 +59,7 @@ idxd_pci_dev_command(struct idxd_dmadev *idxd, enum rte_idxd_cmds command)
 	return err_code;
 }
 
-static uint32_t *
+static volatile uint32_t *
 idxd_get_wq_cfg(struct idxd_pci_common *pci, uint8_t wq_idx)
 {
 	return RTE_PTR_ADD(pci->wq_regs_base,
@@ -205,9 +205,9 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,
 	pci->regs = dev->mem_resource[0].addr;
 	version = pci->regs->version;
 	grp_offset = (uint16_t)pci->regs->offsets[0];
-	pci->grp_regs = RTE_PTR_ADD(pci->regs, grp_offset * 0x100);
+	pci->grp_regs = RTE_PTR_ADD((volatile void *)pci->regs, grp_offset * 0x100);
 	wq_offset = (uint16_t)(pci->regs->offsets[0] >> 16);
-	pci->wq_regs_base = RTE_PTR_ADD(pci->regs, wq_offset * 0x100);
+	pci->wq_regs_base = RTE_PTR_ADD((volatile void *)pci->regs, wq_offset * 0x100);
 	pci->portals = dev->mem_resource[2].addr;
 	pci->wq_cfg_sz = (pci->regs->wqcap >> 24) & 0x0F;
 
@@ -395,7 +395,10 @@ idxd_dmadev_probe_pci(struct rte_pci_driver *drv, struct rte_pci_device *dev)
 		/* add the queue number to each device name */
 		snprintf(qname, sizeof(qname), "%s-q%d", name, qid);
 		idxd.qid = qid;
-		idxd.portal = RTE_PTR_ADD(idxd.u.pci->portals,
+		/* FIXME: cast drops volatile propagation to idxd_dmadev.portal
+		 * See: https://bugs.dpdk.org/show_bug.cgi?id=1871
+		 */
+		idxd.portal = RTE_PTR_ADD(RTE_PTR_UNQUAL(idxd.u.pci->portals),
 				qid * IDXD_PORTAL_SIZE);
 		if (idxd_is_wq_enabled(&idxd))
 			IDXD_PMD_ERR("Error, WQ %u seems enabled", qid);
drivers/dma/odm/odm_dmadev.c +2 -2
index a2f4ed9a8e..3b4b058427 100644
@@ -422,7 +422,7 @@ odm_dmadev_completed(void *dev_private, uint16_t vchan, const uint16_t nb_cpls,
 	int cnt;
 
 	vq = &odm->vq[vchan];
-	const uint32_t *base_addr = vq->cring_mz->addr;
+	uint32_t *base_addr = vq->cring_mz->addr;
 	const uint16_t cring_max_entry = vq->cring_max_entry;
 
 	cring_head = vq->cring_head;
@@ -482,7 +482,7 @@ odm_dmadev_completed_status(void *dev_private, uint16_t vchan, const uint16_t nb
 	int cnt;
 
 	vq = &odm->vq[vchan];
-	const uint32_t *base_addr = vq->cring_mz->addr;
+	uint32_t *base_addr = vq->cring_mz->addr;
 	const uint16_t cring_max_entry = vq->cring_max_entry;
 
 	cring_head = vq->cring_head;
drivers/event/cnxk/cn10k_worker.c +1 -1
index 80077ec8a1..b596cb8702 100644
@@ -338,7 +338,7 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 		aw0 |= ev[0].event & (BIT_ULL(32) - 1);
 		aw0 |= (uint64_t)ev[0].sched_type << 32;
 		*((__uint128_t *)lmt_addr) = aw0;
-		lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+		lmt_addr += 16;
 	}
 #endif
 
drivers/event/cnxk/cn20k_worker.c +1 -1
index 53daf3b4b0..e8e60d727c 100644
@@ -296,7 +296,7 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 		aw0 |= ev[0].event & (BIT_ULL(32) - 1);
 		aw0 |= (uint64_t)ev[0].sched_type << 32;
 		*((__uint128_t *)lmt_addr) = aw0;
-		lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+		lmt_addr += 16;
 	}
 #endif
 
drivers/mempool/bucket/rte_mempool_bucket.c +3 -4
index c0b480bfc7..6fee10176b 100644
@@ -376,8 +376,8 @@ count_underfilled_buckets(struct rte_mempool *mp,
 	uintptr_t align;
 	uint8_t *iter;
 
-	align = (uintptr_t)RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz) -
-		(uintptr_t)memhdr->addr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz),
+			     memhdr->addr);
 
 	for (iter = (uint8_t *)memhdr->addr + align;
 	     iter < (uint8_t *)memhdr->addr + memhdr->len;
@@ -602,8 +602,7 @@ bucket_populate(struct rte_mempool *mp, unsigned int max_objs,
 		return -EINVAL;
 
 	bucket_page_sz = rte_align32pow2(bd->bucket_mem_size);
-	align = RTE_PTR_ALIGN_CEIL((uintptr_t)vaddr, bucket_page_sz) -
-		(uintptr_t)vaddr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, bucket_page_sz), vaddr);
 
 	bucket_header_sz = bd->header_size - mp->header_size;
 	if (iova != RTE_BAD_IOVA)
drivers/mempool/dpaa/dpaa_mempool.c +1 -1
index 2f9395b3f4..85e1c01017 100644
@@ -321,7 +321,7 @@ dpaa_adjust_obj_bounds(char *va, size_t *offset,
 	size_t off = *offset;
 
 	if (dpaa_check_obj_bounds(va + off, pg_sz, total) == false) {
-		off += RTE_PTR_ALIGN_CEIL(va + off, pg_sz) - (va + off);
+		off += RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(va + off, pg_sz), va + off);
 		if (flags & RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ)
 			off += total - ((((size_t)va + off - 1) % total) + 1);
 	}
drivers/net/cxgbe/sge.c +2 -2
index e9d45f24c4..a5d112d52d 100644
@@ -591,7 +591,7 @@ static void write_sgl(struct rte_mbuf *mbuf, struct sge_txq *q,
 			memcpy(sgl->sge, buf, part0);
 		part1 = RTE_PTR_DIFF((u8 *)end, (u8 *)q->stat);
 		rte_memcpy(q->desc, RTE_PTR_ADD((u8 *)buf, part0), part1);
-		end = RTE_PTR_ADD((void *)q->desc, part1);
+		end = RTE_PTR_ADD(q->desc, part1);
 	}
 	if ((uintptr_t)end & 8)           /* 0-pad to multiple of 16 */
 		*(u64 *)end = 0;
@@ -1297,7 +1297,7 @@ static void inline_tx_mbuf(const struct sge_txq *q, caddr_t from, caddr_t *to,
 		from = RTE_PTR_ADD(from, left);
 		left = len - left;
 		rte_memcpy((void *)q->desc, from, left);
-		*to = RTE_PTR_ADD((void *)q->desc, left);
+		*to = RTE_PTR_ADD(q->desc, left);
 	}
 }
 
drivers/net/ena/ena_ethdev.c +8 -1
index ea4afbc75d..da23b271d5 100644
@@ -2379,7 +2379,14 @@ static void *pci_bar_addr(struct rte_pci_device *dev, uint32_t bar)
 {
 	const struct rte_mem_resource *res = &dev->mem_resource[bar];
 	size_t offset = res->phys_addr % rte_mem_page_size();
-	void *vaddr = RTE_PTR_ADD(res->addr, offset);
+	void *vaddr;
+
+	if (res->addr == NULL) {
+		PMD_INIT_LOG_LINE(ERR, "PCI BAR [%u] address is NULL", bar);
+		return NULL;
+	}
+
+	vaddr = RTE_PTR_ADD(res->addr, offset);
 
 	PMD_INIT_LOG_LINE(INFO, "PCI BAR [%u]: phys_addr=0x%" PRIx64 ", addr=%p, offset=0x%zx, adjusted_addr=%p",
 		bar, res->phys_addr, res->addr, offset, vaddr);
drivers/net/intel/fm10k/fm10k.h +3 -3
index 0eb32ac0d0..4e3081785f 100644
@@ -264,9 +264,9 @@ fm10k_pktmbuf_reset(struct rte_mbuf *mb, uint16_t in_port)
 	mb->nb_segs = 1;
 
 	/* enforce 512B alignment on default Rx virtual addresses */
-	mb->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb->buf_addr +
-			RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-			- (char *)mb->buf_addr);
+	mb->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb->buf_addr +
+			RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+			(char *)mb->buf_addr);
 	mb->port = in_port;
 }
 
drivers/net/intel/fm10k/fm10k_rxtx_vec.c +6 -6
index 0eada7275e..a08af75bc7 100644
@@ -315,12 +315,12 @@ fm10k_rxq_rearm(struct fm10k_rx_queue *rxq)
 		_mm_store_si128(RTE_CAST_PTR(__m128i *, &rxdp++->q), dma_addr1);
 
 		/* enforce 512B alignment on default Rx virtual addresses */
-		mb0->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb0->buf_addr
-				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-				- (char *)mb0->buf_addr);
-		mb1->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb1->buf_addr
-				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-				- (char *)mb1->buf_addr);
+		mb0->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb0->buf_addr
+				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+				(char *)mb0->buf_addr);
+		mb1->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb1->buf_addr
+				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+				(char *)mb1->buf_addr);
 	}
 
 	rxq->rxrearm_start += RTE_FM10K_RXQ_REARM_THRESH;
drivers/net/mlx4/mlx4_txq.c +2 -1
index 0db2e55bef..348040c1b9 100644
@@ -114,7 +114,8 @@ txq_uar_uninit_secondary(struct txq *txq)
 	void *addr;
 
 	addr = ppriv->uar_table[txq->stats.idx];
-	munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
+	if (addr)
+		munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
 }
 
 /**
lib/eal/common/eal_common_fbarray.c +2 -0
index 8bdcefb717..c4e03f45f7 100644
@@ -10,6 +10,7 @@
 #include <unistd.h>
 
 #include <rte_common.h>
+#include <rte_debug.h>
 #include <rte_eal_paging.h>
 #include <rte_errno.h>
 #include <rte_log.h>
@@ -1058,6 +1059,7 @@ rte_fbarray_get(const struct rte_fbarray *arr, unsigned int idx)
 		return NULL;
 	}
 
+	RTE_ASSERT(arr->data);
 	ret = RTE_PTR_ADD(arr->data, idx * arr->elt_sz);
 
 	return ret;
lib/eal/common/eal_common_memory.c +20 -11
index c62edf5e55..ee7054ec14 100644
@@ -309,6 +309,9 @@ virt2memseg(const void *addr, const struct rte_memseg_list *msl)
 
 	/* a memseg list was specified, check if it's the right one */
 	start = msl->base_va;
+	if (start == NULL)
+		return NULL;
+
 	end = RTE_PTR_ADD(start, msl->len);
 
 	if (addr < start || addr >= end)
@@ -332,6 +335,8 @@ virt2memseg_list(const void *addr)
 		msl = &mcfg->memsegs[msl_idx];
 
 		start = msl->base_va;
+		if (start == NULL)
+			continue;
 		end = RTE_PTR_ADD(start, msl->len);
 		if (addr >= start && addr < end)
 			break;
@@ -680,10 +685,16 @@ RTE_EXPORT_SYMBOL(rte_mem_lock_page)
 int
 rte_mem_lock_page(const void *virt)
 {
-	uintptr_t virtual = (uintptr_t)virt;
 	size_t page_size = rte_mem_page_size();
-	uintptr_t aligned = RTE_PTR_ALIGN_FLOOR(virtual, page_size);
-	return rte_mem_lock((void *)aligned, page_size);
+	const void *aligned;
+
+	if (virt == NULL) {
+		rte_errno = EINVAL;
+		return -1;
+	}
+
+	aligned = RTE_PTR_ALIGN_FLOOR(virt, page_size);
+	return rte_mem_lock(aligned, page_size);
 }
 
 RTE_EXPORT_SYMBOL(rte_memseg_contig_walk_thread_unsafe)
@@ -1447,7 +1458,7 @@ handle_eal_memseg_info_request(const char *cmd __rte_unused,
 
 	ms_iova = ms->iova;
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 	ms_size = ms->len;
 	hugepage_size = ms->hugepage_sz;
 	ms_socket_id = ms->socket_id;
@@ -1519,7 +1530,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 	rte_mcfg_mem_read_unlock();
 
 	rte_tel_data_start_dict(d);
@@ -1530,8 +1541,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = elem_start_addr + elem->size;
 
 		if ((uint64_t)elem_start_addr >= ms_start_addr &&
 		    (uint64_t)elem_end_addr <= ms_end_addr)
@@ -1553,7 +1563,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	struct rte_mem_config *mcfg;
 	struct rte_memseg_list *msl;
 	const struct rte_memseg *ms;
-	struct malloc_elem *elem;
+	struct malloc_elem *volatile elem;
 	struct malloc_heap *heap;
 	struct rte_tel_data *c;
 	uint64_t ms_start_addr, ms_end_addr;
@@ -1597,7 +1607,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 
 	rte_mcfg_mem_read_unlock();
 
@@ -1609,8 +1619,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = elem_start_addr + elem->size;
 
 		if (elem_start_addr < ms_start_addr ||
 				elem_end_addr > ms_end_addr) {
lib/eal/common/eal_common_options.c +1 -1
index 485655865d..0e05683a67 100644
@@ -1656,7 +1656,7 @@ eal_parse_base_virtaddr(const char *arg)
 	 * on x86 and other architectures.
 	 */
 	internal_conf->base_virtaddr =
-		RTE_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
+		(uintptr_t) RTE_INT_PTR_ALIGN_CEIL(addr, (size_t)RTE_PGSIZE_16M);
 
 	return 0;
 }
lib/eal/common/malloc_elem.h +15 -5
index c7ff6718f8..cba2c4467b 100644
@@ -79,8 +79,8 @@ static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
 #define MALLOC_TRAILER_COOKIE  0xadd2e55badbadbadULL /**< Trailer cookie.*/
 
 /* define macros to make referencing the header and trailer cookies easier */
-#define MALLOC_ELEM_TRAILER(elem) (*((uint64_t*)RTE_PTR_ADD(elem, \
-		elem->size - MALLOC_ELEM_TRAILER_LEN)))
+#define MALLOC_ELEM_TRAILER(elem) \
+	(*((uint64_t *)RTE_PTR_ADD(elem, elem->size - MALLOC_ELEM_TRAILER_LEN)))
 #define MALLOC_ELEM_HEADER(elem) (elem->header_cookie)
 
 static inline void
@@ -103,7 +103,7 @@ malloc_elem_cookies_ok(const struct malloc_elem *elem)
 {
 	return elem != NULL &&
 			MALLOC_ELEM_HEADER(elem) == MALLOC_HEADER_COOKIE &&
-			MALLOC_ELEM_TRAILER(elem) == MALLOC_TRAILER_COOKIE;
+			MALLOC_ELEM_TRAILER(RTE_PTR_UNQUAL(elem)) == MALLOC_TRAILER_COOKIE;
 }
 
 #endif
@@ -309,10 +309,20 @@ malloc_elem_from_data(const void *data)
 	if (data == NULL)
 		return NULL;
 
-	struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
+	/* rte_malloc_socket pool layout: [pad if ELEM_PAD][header][user data][trailer].
+	 * The pointer 'data' points to user data, but the compiler can't trace this
+	 * through the allocation function. GCC's interprocedural analysis issues a
+	 * false positive warning when accessing the header via backwards pointer arithmetic.
+	 */
+	__rte_diagnostic_push
+	__rte_diagnostic_ignored_array_bounds
+	struct malloc_elem *elem = RTE_PTR_SUB(RTE_PTR_UNQUAL(data), MALLOC_ELEM_HEADER_LEN);
+
 	if (!malloc_elem_cookies_ok(elem))
 		return NULL;
-	return elem->state != ELEM_PAD ? elem:  RTE_PTR_SUB(elem, elem->pad);
+
+	return elem->state != ELEM_PAD ? elem : RTE_PTR_SUB(elem, elem->pad);
+	__rte_diagnostic_pop
 }
 
 /*
lib/eal/freebsd/eal_memory.c +4 -0
index 6d3d46a390..49a89fe2fb 100644
@@ -178,6 +178,10 @@ rte_eal_hugepage_init(void)
 					"RTE_MAX_MEMSEG_PER_TYPE and/or RTE_MAX_MEM_MB_PER_TYPE in configuration.");
 				return -1;
 			}
+			if (msl->base_va == NULL) {
+				EAL_LOG(ERR, "Base VA is NULL for memseg list %d", msl_idx);
+				return -1;
+			}
 			arr = &msl->memseg_arr;
 			seg = rte_fbarray_get(arr, ms_idx);
 
lib/eal/include/rte_common.h +205 -14

263 lines not on this page · raw message

lib/eal/linux/eal_memalloc.c +6 -0
index 1e60e21620..f770826a43 100644
@@ -835,6 +835,12 @@ alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
 		void *map_addr;
 
 		cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
+
+		if (cur_msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			goto out;
+		}
+
 		map_addr = RTE_PTR_ADD(cur_msl->base_va,
 				cur_idx * page_sz);
 
lib/eal/linux/eal_memory.c +7 -0
index 8e1763e890..3830bd5d7f 100644
@@ -759,6 +759,13 @@ remap_segment(struct hugepage_file *hugepages, int seg_start, int seg_end)
 			return -1;
 		}
 		memseg_len = (size_t)page_sz;
+
+		if (msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			close(fd);
+			return -1;
+		}
+
 		addr = RTE_PTR_ADD(msl->base_va, ms_idx * memseg_len);
 
 		/* we know this address is already mmapped by memseg list, so
lib/eal/windows/eal_memalloc.c +6 -0
index 5db5a474cc..6432caccd3 100644
@@ -230,6 +230,12 @@ alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
 		void *map_addr;
 
 		cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
+
+		if (cur_msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			goto out;
+		}
+
 		map_addr = RTE_PTR_ADD(cur_msl->base_va, cur_idx * page_sz);
 
 		if (alloc_seg(cur, map_addr, wa->socket, wa->hi)) {
lib/graph/rte_graph.h +2 -2
index 7e433f4661..d8ac0011e4 100644
@@ -407,9 +407,9 @@ void rte_graph_obj_dump(FILE *f, struct rte_graph *graph, bool all);
 /** Macro to browse rte_node object after the graph creation */
 #define rte_graph_foreach_node(count, off, graph, node)                        \
 	for (count = 0, off = graph->nodes_start,                              \
-	     node = RTE_PTR_ADD(graph, off);                                   \
+	     node = RTE_PTR_ADD(RTE_PTR_UNQUAL(graph), off);                           \
 	     count < graph->nb_nodes;                                          \
-	     off = node->next, node = RTE_PTR_ADD(graph, off), count++)
+	     off = node->next, node = RTE_PTR_ADD(RTE_PTR_UNQUAL(graph), off), count++)
 
 /**
  * Get node object with in graph from id.
lib/latencystats/rte_latencystats.c +3 -0
index f61d5a273f..f2cd0db4b7 100644
@@ -104,6 +104,9 @@ latencystats_collect(uint64_t values[])
 	unsigned int i, scale;
 	const uint64_t *stats;
 
+	if (glob_stats == NULL)
+		return;
+
 	for (i = 0; i < NUM_LATENCY_STATS; i++) {
 		stats = RTE_PTR_ADD(glob_stats, lat_stats_strings[i].offset);
 		scale = lat_stats_strings[i].scale;
lib/mbuf/rte_mbuf.c +1 -0
index 0d931c7a15..557954d45e 100644
@@ -193,6 +193,7 @@ __rte_pktmbuf_init_extmem(struct rte_mempool *mp,
 
 	RTE_ASSERT(ctx->ext < ctx->ext_num);
 	RTE_ASSERT(ctx->off + ext_mem->elt_size <= ext_mem->buf_len);
+	RTE_ASSERT(ext_mem->buf_ptr);
 
 	m->buf_addr = RTE_PTR_ADD(ext_mem->buf_ptr, ctx->off);
 	rte_mbuf_iova_set(m, ext_mem->buf_iova == RTE_BAD_IOVA ? RTE_BAD_IOVA :
lib/mbuf/rte_mbuf.h +2 -0
index 2004391f57..3100feb740 100644
@@ -217,6 +217,7 @@ rte_mbuf_data_iova_default(const struct rte_mbuf *mb)
 static inline struct rte_mbuf *
 rte_mbuf_from_indirect(struct rte_mbuf *mi)
 {
+	RTE_ASSERT(mi);
 	return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
 }
 
@@ -289,6 +290,7 @@ rte_mbuf_to_baddr(struct rte_mbuf *md)
 static inline void *
 rte_mbuf_to_priv(struct rte_mbuf *m)
 {
+	RTE_ASSERT(m);
 	return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
 }
 
lib/member/rte_xxh64_avx512.h +3 -3
index 58f896ebb8..774b26d8df 100644
@@ -58,7 +58,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 			 _mm512_set1_epi64(key_len));
 
 	while (remaining >= 8) {
-		input = _mm512_set1_epi64(*(uint64_t *)RTE_PTR_ADD(key, offset));
+		input = _mm512_set1_epi64(*(const uint64_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 				xxh64_round_avx512(_mm512_setzero_si512(), input));
 		v_hash = _mm512_madd52lo_epu64(_mm512_set1_epi64(PRIME64_4),
@@ -71,7 +71,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	if (remaining >= 4) {
 		input = _mm512_set1_epi64
-			(*(uint32_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint32_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_1)));
@@ -86,7 +86,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	while (remaining != 0) {
 		input = _mm512_set1_epi64
-			(*(uint8_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint8_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_5)));
lib/mempool/rte_mempool.c +4 -4
index 3042d94c14..f1ff668205 100644
@@ -349,9 +349,9 @@ rte_mempool_populate_iova(struct rte_mempool *mp, char *vaddr,
 	memhdr->opaque = opaque;
 
 	if (mp->flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
-		off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
+		off = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, 8), vaddr);
 	else
-		off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN) - vaddr;
+		off = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN), vaddr);
 
 	if (off > len) {
 		ret = 0;
@@ -425,8 +425,8 @@ rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
 
 		/* populate with the largest group of contiguous pages */
 		for (phys_len = RTE_MIN(
-			(size_t)(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz) -
-				(addr + off)),
+			(size_t)RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz),
+				addr + off),
 			len - off);
 		     off + phys_len < len;
 		     phys_len = RTE_MIN(phys_len + pg_sz, len - off)) {
lib/mempool/rte_mempool.h +3 -0
index aedc100964..091976574a 100644
@@ -376,6 +376,7 @@ struct __rte_cache_aligned rte_mempool {
 static inline struct rte_mempool_objhdr *
 rte_mempool_get_header(void *obj)
 {
+	RTE_ASSERT(obj);
 	return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
 		sizeof(struct rte_mempool_objhdr));
 }
@@ -399,6 +400,7 @@ static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
 static inline struct rte_mempool_objtlr *rte_mempool_get_trailer(void *obj)
 {
 	struct rte_mempool *mp = rte_mempool_from_obj(obj);
+	RTE_ASSERT(mp);
 	return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
 }
 
@@ -1844,6 +1846,7 @@ rte_mempool_empty(const struct rte_mempool *mp)
 static inline rte_iova_t
 rte_mempool_virt2iova(const void *elt)
 {
+	RTE_ASSERT(elt);
 	const struct rte_mempool_objhdr *hdr;
 	hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
 		sizeof(*hdr));
lib/mempool/rte_mempool_ops_default.c +1 -1
index d27d6fc473..e28a288b91 100644
@@ -117,7 +117,7 @@ rte_mempool_op_populate_helper(struct rte_mempool *mp, unsigned int flags,
 	for (i = 0; i < max_objs; i++) {
 		/* avoid objects to cross page boundaries */
 		if (check_obj_bounds(va + off, pg_sz, total_elt_sz) < 0) {
-			off += RTE_PTR_ALIGN_CEIL(va + off, pg_sz) - (va + off);
+			off += RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(va + off, pg_sz), va + off);
 			if (flags & RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ)
 				off += total_elt_sz -
 					(((uintptr_t)(va + off - 1) %
lib/pdcp/pdcp_entity.h +5 -3
index f854192e98..7a2c41dd56 100644
@@ -198,17 +198,19 @@ struct entity_priv_ul_part {
 
 static inline struct entity_priv *
 entity_priv_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity), sizeof(struct rte_pdcp_entity));
 }
 
 static inline struct entity_priv_dl_part *
 entity_dl_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity),
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline struct entity_priv_ul_part *
 entity_ul_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity),
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline int
lib/vhost/vhost_user.c +10 -3
index 4bfb13fb98..a9ab6487f2 100644
@@ -824,9 +824,16 @@ void
 mem_set_dump(struct virtio_net *dev, void *ptr, size_t size, bool enable, uint64_t pagesz)
 {
 #ifdef MADV_DONTDUMP
-	void *start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz);
-	uintptr_t end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz);
-	size_t len = end - (uintptr_t)start;
+	void *start;
+	uintptr_t end;
+	size_t len;
+
+	if (ptr == NULL)
+		return;
+
+	start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz);
+	end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz);
+	len = end - (uintptr_t)start;
 
 	if (madvise(start, len, enable ? MADV_DODUMP : MADV_DONTDUMP) == -1) {
 		VHOST_CONFIG_LOG(dev->ifname, INFO,
Signature · 1 line
2.39.5 (Apple Git-154)
#22 lore raw

PATCH v14 1/1 eal: RTE_PTR_ADD/SUB API improvements

From: Scott Mitchell <[email protected]>

RTE_PTR_ADD and RTE_PTR_SUB APIs have a few limitations:
1. ptr cast to uintptr_t drops pointer provenance and prevents compiler optimizations
2. return cast discards qualifiers (const, volatile) which may hide correctness/concurrency issues.
3. Accepts both "pointers" and "integers as pointers" which overloads the use case and constrains the implementation to address other challenges.

This patch splits the API on two dimensions:
1. pointer types
2. integer types that represent pointers

This split allows addressing each of the challenges above and provides distinct APIs for the distinct use cases. Examples:
1. Clang is able to optimize and improve __rte_raw_cksum (which uses RTE_PTR_ADD) by ~40% (100 bytes) to ~8x (1.5k bytes) TSC cycles/byte.
2. Refactoring discovered cases that dropped qualifiers (volatile) that the new API exposes.

---
v14:
- fixed cpp compiler error, avoiding array pointer decay which is implicitly done in cpp (but not c)
- fixed MALLOC_ELEM_TRAILER const preservation compile error

v13:
- Added release notes documenting API changes
- Fixed alignment in test file: use alignas(uint32_t) for buffer
- Fixed NULL pointer handling in cdx_vfio.c: check base_va before RTE_PTR_ADD
- Added GCC array-bounds diagnostic suppression in malloc_elem_from_data()
- Added bug tracker reference for volatile cast issue in idxd_pci.c
- Improved __rte_auto_type documentation: added C++11 and C23 support
- Moved doxygen rationale for void* return type to @return blocks
- Fixed MALLOC_ELEM_TRAILER to use RTE_PTR_UNQUAL for write operations

v12:
- void* return type to avoid optimizations assuming aligned access which isn't generally safe/true.

v11:
- Split API into PTR and INT_PTR variants, update all usage of PTR API for new APIs.

v10:
- Use unit_test_suite_runner for easier subtest failure identification

v9:
- Fix include order: system includes, then DPDK includes, then application includes
- Use NOHUGE_OK and ASAN_OK constants in REGISTER_FAST_TEST (instead of true, true)

v8:
- Remove tests for types < 32-bit (bool, char, short, uint8_t, uint16_t)
- Merge test_ptr_add_sub_typedefs() into test_ptr_add_sub_integer_types()
- Separate RTE_PTR_ADD and RTE_PTR_SUB documentation
- Move Clang/GCC implementation note from Doxygen to regular comment
- Tests verify both intermediate ADD result and SUB round-trip
- Use uintptr_t cast consistently for all integer-to-pointer conversions
- Make TEST_RETVAL calculated from TEST_INITVAL + TEST_INCREMENT

v7:
- Fix tests: use TEST_BUFFER_SIZE macro for buffer allocation
- Fix tests: ADD then SUB same amount to avoid out-of-bounds pointer arithmetic
- All RTE_PTR_SUB tests now verify round-trip (ADD+SUB returns to original)

v6:
- Make char* optimization Clang-only to avoid GCC false positive warnings
- Improve tests: use named constants instead of magic numbers
- Improve tests: use void* casts on expected values instead of uintptr_t on results

v5:
- Initial implementation with char* arithmetic for all compilers

v4:
- Used _Generic for type-based dispatch with char* casts
- Had warnings on both Clang and GCC due to _Generic type-checking all branches

---
 app/test-pmd/cmdline_flow.c                 |   4 +-
 app/test/meson.build                        |   1 +
 app/test/test_common.c                      |  20 +-
 app/test/test_ptr_add_sub.c                 | 199 ++++++++++++++++
 doc/guides/rel_notes/release_26_03.rst      |  11 +
 drivers/bus/cdx/cdx_vfio.c                  |  13 +-
 drivers/bus/pci/linux/pci.c                 |   6 +-
 drivers/bus/vmbus/linux/vmbus_uio.c         |   6 +-
 drivers/common/cnxk/roc_cpt_debug.c         |   4 +-
 drivers/common/cnxk/roc_nix_bpf.c           |   2 +-
 drivers/common/cnxk/roc_nix_inl.h           |   4 +-
 drivers/common/cnxk/roc_nix_inl_dp.h        |   8 +-
 drivers/common/cnxk/roc_platform.h          |   7 +-
 drivers/common/mlx5/mlx5_common_mr.c        |   2 +-
 drivers/dma/idxd/idxd_pci.c                 |  11 +-
 drivers/dma/odm/odm_dmadev.c                |   4 +-
 drivers/event/cnxk/cn10k_worker.c           |  32 +--
 drivers/event/cnxk/cn20k_worker.c           |  32 +--
 drivers/mempool/bucket/rte_mempool_bucket.c |   7 +-
 drivers/mempool/dpaa/dpaa_mempool.c         |   2 +-
 drivers/net/cxgbe/sge.c                     |   4 +-
 drivers/net/ena/ena_ethdev.c                |   9 +-
 drivers/net/intel/fm10k/fm10k.h             |   6 +-
 drivers/net/intel/fm10k/fm10k_rxtx_vec.c    |  12 +-
 drivers/net/mlx4/mlx4_txq.c                 |   3 +-
 lib/eal/common/eal_common_fbarray.c         |   2 +
 lib/eal/common/eal_common_memory.c          |  31 ++-
 lib/eal/common/eal_common_options.c         |   2 +-
 lib/eal/common/malloc_elem.h                |  34 ++-
 lib/eal/freebsd/eal_memory.c                |   4 +
 lib/eal/include/rte_common.h                | 242 ++++++++++++++++++--
 lib/eal/linux/eal_memalloc.c                |   6 +
 lib/eal/linux/eal_memory.c                  |   7 +
 lib/eal/windows/eal_memalloc.c              |   6 +
 lib/graph/rte_graph.h                       |   4 +-
 lib/latencystats/rte_latencystats.c         |   3 +
 lib/mbuf/rte_mbuf.c                         |   1 +
 lib/mbuf/rte_mbuf.h                         |   2 +
 lib/member/rte_xxh64_avx512.h               |   6 +-
 lib/mempool/rte_mempool.c                   |   8 +-
 lib/mempool/rte_mempool.h                   |   3 +
 lib/mempool/rte_mempool_ops_default.c       |   2 +-
 lib/pdcp/pdcp_entity.h                      |   8 +-
 lib/vhost/vhost_user.c                      |  13 +-
 44 files changed, 659 insertions(+), 134 deletions(-)
 create mode 100644 app/test/test_ptr_add_sub.c
app/test-pmd/cmdline_flow.c +2 -2
index ebc036b14b..8c2fc6c7b6 100644
@@ -12468,7 +12468,7 @@ parse_meter_color(struct context *ctx, const struct token *token,
 
 		if (!arg)
 			return -1;
-		*(int *)RTE_PTR_ADD(action->conf, arg->offset) = i;
+		*(int *)RTE_PTR_ADD(RTE_PTR_UNQUAL(action->conf), arg->offset) = i;
 	} else {
 		((struct rte_flow_item_meter_color *)
 			ctx->object)->color = (enum rte_color)i;
@@ -13351,7 +13351,7 @@ indirect_action_flow_conf_create(const struct buffer *in)
 		indlst_conf = NULL;
 		goto end;
 	}
-	indlst_conf->conf = RTE_PTR_ADD(indlst_conf, base + len);
+	indlst_conf->conf = (const void **)RTE_PTR_ADD(indlst_conf, base + len);
 	for (i = 0; i < indlst_conf->conf_num; i++)
 		indlst_conf->conf[i] = indlst_conf->actions[i].conf;
 	SLIST_INSERT_HEAD(&indlst_conf_head, indlst_conf, next);
app/test/meson.build +1 -0
index f4d04a6e42..aa56fc4297 100644
@@ -152,6 +152,7 @@ source_file_deps = {
     'test_power_intel_uncore.c': ['power', 'power_intel_uncore'],
     'test_power_kvm_vm.c': ['power', 'power_kvm_vm'],
     'test_prefetch.c': [],
+    'test_ptr_add_sub.c': [],
     'test_ptr_compress.c': ['ptr_compress'],
     'test_rand_perf.c': [],
     'test_rawdev.c': ['rawdev', 'bus_vdev', 'raw_skeleton'],
app/test/test_common.c +10 -10
index 3e1c7df0c1..299700f84b 100644
@@ -37,10 +37,10 @@ test_macros(int __rte_unused unused_parm)
 	RTE_SWAP(smaller, bigger);
 	RTE_TEST_ASSERT(smaller == BIGGER && bigger == SMALLER,
 		"RTE_SWAP");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF), BIGGER,
-		"RTE_PTR_ADD");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF), SMALLER,
-		"RTE_PTR_SUB");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR_ADD(SMALLER, PTR_DIFF), (void *)BIGGER,
+		"RTE_INT_PTR_ADD");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR_SUB(BIGGER, PTR_DIFF), (void *)SMALLER,
+		"RTE_INT_PTR_SUB");
 	RTE_TEST_ASSERT_EQUAL(RTE_PTR_DIFF(BIGGER, SMALLER), PTR_DIFF,
 		"RTE_PTR_DIFF");
 	RTE_TEST_ASSERT_EQUAL(RTE_MAX(SMALLER, BIGGER), BIGGER,
@@ -188,18 +188,18 @@ test_align(void)
 			if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
-			val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN_FLOOR((uintptr_t) i, p);
 			if (ERROR_FLOOR(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_FLOOR", i, p);
 
 			val = RTE_ALIGN_FLOOR(i, p);
 			if (ERROR_FLOOR(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
 			/* align ceiling */
-			val = RTE_PTR_ALIGN((uintptr_t) i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN((uintptr_t) i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN", i, p);
 
 			val = RTE_ALIGN(i, p);
 			if (ERROR_CEIL(val, i, p))
@@ -209,9 +209,9 @@ test_align(void)
 			if (ERROR_CEIL(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
 
-			val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN_CEIL((uintptr_t)i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_CEIL", i, p);
 
 			/* by this point we know that val is aligned to p */
 			if (!rte_is_aligned((void*)(uintptr_t) val, p))
app/test/test_ptr_add_sub.c new file +199 -0

199 lines not on this page · raw message

doc/guides/rel_notes/release_26_03.rst +11 -0
index 15dabee7a1..1a793204d0 100644
@@ -84,6 +84,17 @@ API Changes
    Also, make sure to start the actual text at the margin.
    =======================================================
 
+* eal: Improved pointer arithmetic macros to preserve pointer provenance and type qualifiers.
+
+  * ``RTE_PTR_ADD`` and ``RTE_PTR_SUB`` now preserve const/volatile qualifiers
+    and use pointer arithmetic instead of integer casts to enable compiler optimizations.
+  * Passing NULL to ``RTE_PTR_ADD`` or ``RTE_PTR_SUB`` is now undefined behavior.
+  * Added ``RTE_INT_PTR_ADD`` and ``RTE_INT_PTR_SUB`` for integer-as-pointer arithmetic.
+  * Added ``RTE_INT_PTR_ALIGN``, ``RTE_INT_PTR_ALIGN_FLOOR``, and ``RTE_INT_PTR_ALIGN_CEIL``
+    for aligning integer addresses.
+  * Existing code using ``RTE_PTR_ADD``/``RTE_PTR_SUB`` with integer types should migrate
+    to ``RTE_INT_PTR_*`` variants for clarity and correctness.
+
 
 ABI Changes
 -----------
drivers/bus/cdx/cdx_vfio.c +10 -3
index 11fe3265d2..a1009bc0ca 100644
@@ -367,9 +367,16 @@ cdx_vfio_get_region_info(int vfio_dev_fd, struct vfio_region_info **info,
 static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
-	size_t sz = msl->len;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
-	void **max_va = arg;
+	size_t sz;
+	void *end_va;
+	void **max_va;
+
+	if (msl->base_va == NULL)
+		return 0;
+
+	sz = msl->len;
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
+	max_va = arg;
 
 	if (*max_va < end_va)
 		*max_va = end_va;
drivers/bus/pci/linux/pci.c +5 -1
index 2ffac82e94..455db2cdec 100644
@@ -109,9 +109,13 @@ static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
 	size_t sz = msl->len;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
+	void *end_va;
 	void **max_va = arg;
 
+	if (msl->base_va == NULL)
+		return 0;
+
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
 	if (*max_va < end_va)
 		*max_va = end_va;
 	return 0;
drivers/bus/vmbus/linux/vmbus_uio.c +5 -1
index fbafc5027d..0ef05c0096 100644
@@ -122,9 +122,13 @@ static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
 	size_t sz = msl->memseg_arr.len * msl->page_sz;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
+	void *end_va;
 	void **max_va = arg;
 
+	if (msl->base_va == NULL)
+		return 0;
+
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
 	if (*max_va < end_va)
 		*max_va = end_va;
 	return 0;
drivers/common/cnxk/roc_cpt_debug.c +2 -2
index 28aedf088e..252658a83f 100644
@@ -56,7 +56,7 @@ cpt_cnxk_parse_hdr_dump(FILE *file, const struct cpt_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*32B */
 	offset = cpth->w2.ptr_offset;
 	offset = (((offset - 1) & 0x7) + 1) * 32;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD(PLT_PTR_UNQUAL(cpth), offset);
 
 	if (cpth->w0.num_frags > 0) {
 		cpt_dump(file, "CPT Fraginfo_0 \t%p:", frag_info);
@@ -162,7 +162,7 @@ cpt_cn10k_parse_hdr_dump(FILE *file, const struct cpt_cn10k_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*8B */
 	offset = cpth->w2.fi_offset;
 	offset = (((offset - 1) & 0x1f) + 1) * 8;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD(PLT_PTR_UNQUAL(cpth), offset);
 
 	cpt_dump(file, "CPT Fraginfo \t0x%p:", frag_info);
 
drivers/common/cnxk/roc_nix_bpf.c +1 -1
index 98c9855a5b..05fad44581 100644
@@ -160,7 +160,7 @@ nix_precolor_conv_table_write(struct roc_nix *roc_nix, uint64_t val,
 	struct nix *nix = roc_nix_to_nix_priv(roc_nix);
 	int64_t *addr;
 
-	addr = PLT_PTR_ADD(nix->base, off);
+	addr = PLT_INT_PTR_ADD(nix->base, off);
 	plt_write64(val, addr);
 }
 
drivers/common/cnxk/roc_nix_inl.h +2 -2
index 7970ac2258..90d6aa5025 100644
@@ -59,7 +59,7 @@ roc_nix_inl_on_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline struct roc_ie_on_outb_sa *
@@ -67,7 +67,7 @@ roc_nix_inl_on_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline void *
drivers/common/cnxk/roc_nix_inl_dp.h +4 -4
index eb101db179..7061cd39ac 100644
@@ -49,7 +49,7 @@ roc_nix_inl_ot_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline struct roc_ot_ipsec_outb_sa *
@@ -57,7 +57,7 @@ roc_nix_inl_ot_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline void *
@@ -77,7 +77,7 @@ roc_nix_inl_ow_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline struct roc_ow_ipsec_outb_sa *
@@ -85,7 +85,7 @@ roc_nix_inl_ow_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR_ADD(base, off);
 }
 
 static inline void *
drivers/common/cnxk/roc_platform.h +5 -2
index e22a50d47a..9261d26175 100644
@@ -47,6 +47,9 @@
 #define PLT_PTR_ADD		 RTE_PTR_ADD
 #define PLT_PTR_SUB		 RTE_PTR_SUB
 #define PLT_PTR_DIFF		 RTE_PTR_DIFF
+#define PLT_PTR_UNQUAL		 RTE_PTR_UNQUAL
+#define PLT_INT_PTR_ADD		 RTE_INT_PTR_ADD
+#define PLT_INT_PTR_SUB		 RTE_INT_PTR_SUB
 #define PLT_MAX_RXTX_INTR_VEC_ID RTE_MAX_RXTX_INTR_VEC_ID
 #define PLT_INTR_VEC_RXTX_OFFSET RTE_INTR_VEC_RXTX_OFFSET
 #define PLT_MIN			 RTE_MIN
@@ -84,8 +87,8 @@
 #define PLT_U16_CAST(val) ((uint16_t)(val))
 
 /* Add / Sub pointer with scalar and cast to uint64_t */
-#define PLT_PTR_ADD_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_PTR_ADD(__ptr, __x))
-#define PLT_PTR_SUB_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_PTR_SUB(__ptr, __x))
+#define PLT_PTR_ADD_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_INT_PTR_ADD(__ptr, __x))
+#define PLT_PTR_SUB_U64_CAST(__ptr, __x) PLT_U64_CAST(PLT_INT_PTR_SUB(__ptr, __x))
 
 /** Divide ceil */
 #define PLT_DIV_CEIL(x, y)			\
drivers/common/mlx5/mlx5_common_mr.c +1 -1
index 8ed988dec9..aae39fdb6e 100644
@@ -1447,7 +1447,7 @@ mlx5_mempool_get_extmem_cb(struct rte_mempool *mp, void *opaque,
 	seg = &heap[data->heap_size - 1];
 	msl = rte_mem_virt2memseg_list((void *)addr);
 	page_size = msl != NULL ? msl->page_sz : rte_mem_page_size();
-	page_start = RTE_PTR_ALIGN_FLOOR(addr, page_size);
+	page_start = RTE_ALIGN_FLOOR(addr, page_size);
 	seg->start = page_start;
 	seg->end = page_start + page_size;
 	/* Maintain the heap order. */
drivers/dma/idxd/idxd_pci.c +7 -4
index 214f6f22d5..fb76a050b1 100644
@@ -59,7 +59,7 @@ idxd_pci_dev_command(struct idxd_dmadev *idxd, enum rte_idxd_cmds command)
 	return err_code;
 }
 
-static uint32_t *
+static volatile uint32_t *
 idxd_get_wq_cfg(struct idxd_pci_common *pci, uint8_t wq_idx)
 {
 	return RTE_PTR_ADD(pci->wq_regs_base,
@@ -205,9 +205,9 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,
 	pci->regs = dev->mem_resource[0].addr;
 	version = pci->regs->version;
 	grp_offset = (uint16_t)pci->regs->offsets[0];
-	pci->grp_regs = RTE_PTR_ADD(pci->regs, grp_offset * 0x100);
+	pci->grp_regs = RTE_PTR_ADD((volatile void *)pci->regs, grp_offset * 0x100);
 	wq_offset = (uint16_t)(pci->regs->offsets[0] >> 16);
-	pci->wq_regs_base = RTE_PTR_ADD(pci->regs, wq_offset * 0x100);
+	pci->wq_regs_base = RTE_PTR_ADD((volatile void *)pci->regs, wq_offset * 0x100);
 	pci->portals = dev->mem_resource[2].addr;
 	pci->wq_cfg_sz = (pci->regs->wqcap >> 24) & 0x0F;
 
@@ -395,7 +395,10 @@ idxd_dmadev_probe_pci(struct rte_pci_driver *drv, struct rte_pci_device *dev)
 		/* add the queue number to each device name */
 		snprintf(qname, sizeof(qname), "%s-q%d", name, qid);
 		idxd.qid = qid;
-		idxd.portal = RTE_PTR_ADD(idxd.u.pci->portals,
+		/* FIXME: cast drops volatile propagation to idxd_dmadev.portal
+		 * See: https://bugs.dpdk.org/show_bug.cgi?id=1871
+		 */
+		idxd.portal = RTE_PTR_ADD(RTE_PTR_UNQUAL(idxd.u.pci->portals),
 				qid * IDXD_PORTAL_SIZE);
 		if (idxd_is_wq_enabled(&idxd))
 			IDXD_PMD_ERR("Error, WQ %u seems enabled", qid);
drivers/dma/odm/odm_dmadev.c +2 -2
index a2f4ed9a8e..3b4b058427 100644
@@ -422,7 +422,7 @@ odm_dmadev_completed(void *dev_private, uint16_t vchan, const uint16_t nb_cpls,
 	int cnt;
 
 	vq = &odm->vq[vchan];
-	const uint32_t *base_addr = vq->cring_mz->addr;
+	uint32_t *base_addr = vq->cring_mz->addr;
 	const uint16_t cring_max_entry = vq->cring_max_entry;
 
 	cring_head = vq->cring_head;
@@ -482,7 +482,7 @@ odm_dmadev_completed_status(void *dev_private, uint16_t vchan, const uint16_t nb
 	int cnt;
 
 	vq = &odm->vq[vchan];
-	const uint32_t *base_addr = vq->cring_mz->addr;
+	uint32_t *base_addr = vq->cring_mz->addr;
 	const uint16_t cring_max_entry = vq->cring_max_entry;
 
 	cring_head = vq->cring_head;
drivers/event/cnxk/cn10k_worker.c +16 -16
index 80077ec8a1..af0b50753d 100644
@@ -261,14 +261,14 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 					aw7);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 64), aw4);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 80), aw5);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 96), aw6);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 112), aw7);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 128);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 16), aw1);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 32), aw2);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 48), aw3);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 64), aw4);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 80), aw5);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 96), aw6);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 112), aw7);
+			lmt_addr += 128;
 		} break;
 		case 4: {
 			uint64x2_t aw0, aw1, aw2, aw3;
@@ -291,10 +291,10 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 					aw3);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 64);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 16), aw1);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 32), aw2);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 48), aw3);
+			lmt_addr += 64;
 		} break;
 		case 2: {
 			uint64x2_t aw0, aw1;
@@ -310,8 +310,8 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 					aw1);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 32);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 16), aw1);
+			lmt_addr += 32;
 		} break;
 		case 1: {
 			__uint128_t aw0;
@@ -322,7 +322,7 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 			aw0 |= (uint64_t)ev[0].sched_type << 32;
 
 			*((__uint128_t *)lmt_addr) = aw0;
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+			lmt_addr += 16;
 		} break;
 		}
 		ev += parts;
@@ -338,7 +338,7 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 		aw0 |= ev[0].event & (BIT_ULL(32) - 1);
 		aw0 |= (uint64_t)ev[0].sched_type << 32;
 		*((__uint128_t *)lmt_addr) = aw0;
-		lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+		lmt_addr += 16;
 	}
 #endif
 
drivers/event/cnxk/cn20k_worker.c +16 -16
index 53daf3b4b0..ad369af7f3 100644
@@ -231,14 +231,14 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw7 = vorrq_u64(vandq_u64(vshrq_n_u64(aw7, 6), tt_mask), aw7);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 64), aw4);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 80), aw5);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 96), aw6);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 112), aw7);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 128);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 16), aw1);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 32), aw2);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 48), aw3);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 64), aw4);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 80), aw5);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 96), aw6);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 112), aw7);
+			lmt_addr += 128;
 		} break;
 		case 4: {
 			uint64x2_t aw0, aw1, aw2, aw3;
@@ -253,10 +253,10 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw3 = vorrq_u64(vandq_u64(vshrq_n_u64(aw3, 6), tt_mask), aw3);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 64);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 16), aw1);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 32), aw2);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 48), aw3);
+			lmt_addr += 64;
 		} break;
 		case 2: {
 			uint64x2_t aw0, aw1;
@@ -268,8 +268,8 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw1 = vorrq_u64(vandq_u64(vshrq_n_u64(aw1, 6), tt_mask), aw1);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 32);
+			vst1q_u64(PLT_INT_PTR_ADD(lmt_addr, 16), aw1);
+			lmt_addr += 32;
 		} break;
 		case 1: {
 			__uint128_t aw0;
@@ -280,7 +280,7 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw0 |= (uint64_t)ev[0].sched_type << 32;
 
 			*((__uint128_t *)lmt_addr) = aw0;
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+			lmt_addr += 16;
 		} break;
 		}
 		ev += parts;
@@ -296,7 +296,7 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 		aw0 |= ev[0].event & (BIT_ULL(32) - 1);
 		aw0 |= (uint64_t)ev[0].sched_type << 32;
 		*((__uint128_t *)lmt_addr) = aw0;
-		lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+		lmt_addr += 16;
 	}
 #endif
 
drivers/mempool/bucket/rte_mempool_bucket.c +3 -4
index c0b480bfc7..6fee10176b 100644
@@ -376,8 +376,8 @@ count_underfilled_buckets(struct rte_mempool *mp,
 	uintptr_t align;
 	uint8_t *iter;
 
-	align = (uintptr_t)RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz) -
-		(uintptr_t)memhdr->addr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz),
+			     memhdr->addr);
 
 	for (iter = (uint8_t *)memhdr->addr + align;
 	     iter < (uint8_t *)memhdr->addr + memhdr->len;
@@ -602,8 +602,7 @@ bucket_populate(struct rte_mempool *mp, unsigned int max_objs,
 		return -EINVAL;
 
 	bucket_page_sz = rte_align32pow2(bd->bucket_mem_size);
-	align = RTE_PTR_ALIGN_CEIL((uintptr_t)vaddr, bucket_page_sz) -
-		(uintptr_t)vaddr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, bucket_page_sz), vaddr);
 
 	bucket_header_sz = bd->header_size - mp->header_size;
 	if (iova != RTE_BAD_IOVA)
drivers/mempool/dpaa/dpaa_mempool.c +1 -1
index 2f9395b3f4..85e1c01017 100644
@@ -321,7 +321,7 @@ dpaa_adjust_obj_bounds(char *va, size_t *offset,
 	size_t off = *offset;
 
 	if (dpaa_check_obj_bounds(va + off, pg_sz, total) == false) {
-		off += RTE_PTR_ALIGN_CEIL(va + off, pg_sz) - (va + off);
+		off += RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(va + off, pg_sz), va + off);
 		if (flags & RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ)
 			off += total - ((((size_t)va + off - 1) % total) + 1);
 	}
drivers/net/cxgbe/sge.c +2 -2
index e9d45f24c4..a5d112d52d 100644
@@ -591,7 +591,7 @@ static void write_sgl(struct rte_mbuf *mbuf, struct sge_txq *q,
 			memcpy(sgl->sge, buf, part0);
 		part1 = RTE_PTR_DIFF((u8 *)end, (u8 *)q->stat);
 		rte_memcpy(q->desc, RTE_PTR_ADD((u8 *)buf, part0), part1);
-		end = RTE_PTR_ADD((void *)q->desc, part1);
+		end = RTE_PTR_ADD(q->desc, part1);
 	}
 	if ((uintptr_t)end & 8)           /* 0-pad to multiple of 16 */
 		*(u64 *)end = 0;
@@ -1297,7 +1297,7 @@ static void inline_tx_mbuf(const struct sge_txq *q, caddr_t from, caddr_t *to,
 		from = RTE_PTR_ADD(from, left);
 		left = len - left;
 		rte_memcpy((void *)q->desc, from, left);
-		*to = RTE_PTR_ADD((void *)q->desc, left);
+		*to = RTE_PTR_ADD(q->desc, left);
 	}
 }
 
drivers/net/ena/ena_ethdev.c +8 -1
index ea4afbc75d..da23b271d5 100644
@@ -2379,7 +2379,14 @@ static void *pci_bar_addr(struct rte_pci_device *dev, uint32_t bar)
 {
 	const struct rte_mem_resource *res = &dev->mem_resource[bar];
 	size_t offset = res->phys_addr % rte_mem_page_size();
-	void *vaddr = RTE_PTR_ADD(res->addr, offset);
+	void *vaddr;
+
+	if (res->addr == NULL) {
+		PMD_INIT_LOG_LINE(ERR, "PCI BAR [%u] address is NULL", bar);
+		return NULL;
+	}
+
+	vaddr = RTE_PTR_ADD(res->addr, offset);
 
 	PMD_INIT_LOG_LINE(INFO, "PCI BAR [%u]: phys_addr=0x%" PRIx64 ", addr=%p, offset=0x%zx, adjusted_addr=%p",
 		bar, res->phys_addr, res->addr, offset, vaddr);
drivers/net/intel/fm10k/fm10k.h +3 -3
index 0eb32ac0d0..4e3081785f 100644
@@ -264,9 +264,9 @@ fm10k_pktmbuf_reset(struct rte_mbuf *mb, uint16_t in_port)
 	mb->nb_segs = 1;
 
 	/* enforce 512B alignment on default Rx virtual addresses */
-	mb->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb->buf_addr +
-			RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-			- (char *)mb->buf_addr);
+	mb->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb->buf_addr +
+			RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+			(char *)mb->buf_addr);
 	mb->port = in_port;
 }
 
drivers/net/intel/fm10k/fm10k_rxtx_vec.c +6 -6
index 0eada7275e..a08af75bc7 100644
@@ -315,12 +315,12 @@ fm10k_rxq_rearm(struct fm10k_rx_queue *rxq)
 		_mm_store_si128(RTE_CAST_PTR(__m128i *, &rxdp++->q), dma_addr1);
 
 		/* enforce 512B alignment on default Rx virtual addresses */
-		mb0->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb0->buf_addr
-				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-				- (char *)mb0->buf_addr);
-		mb1->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb1->buf_addr
-				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-				- (char *)mb1->buf_addr);
+		mb0->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb0->buf_addr
+				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+				(char *)mb0->buf_addr);
+		mb1->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb1->buf_addr
+				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+				(char *)mb1->buf_addr);
 	}
 
 	rxq->rxrearm_start += RTE_FM10K_RXQ_REARM_THRESH;
drivers/net/mlx4/mlx4_txq.c +2 -1
index 0db2e55bef..348040c1b9 100644
@@ -114,7 +114,8 @@ txq_uar_uninit_secondary(struct txq *txq)
 	void *addr;
 
 	addr = ppriv->uar_table[txq->stats.idx];
-	munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
+	if (addr)
+		munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
 }
 
 /**
lib/eal/common/eal_common_fbarray.c +2 -0
index 8bdcefb717..c4e03f45f7 100644
@@ -10,6 +10,7 @@
 #include <unistd.h>
 
 #include <rte_common.h>
+#include <rte_debug.h>
 #include <rte_eal_paging.h>
 #include <rte_errno.h>
 #include <rte_log.h>
@@ -1058,6 +1059,7 @@ rte_fbarray_get(const struct rte_fbarray *arr, unsigned int idx)
 		return NULL;
 	}
 
+	RTE_ASSERT(arr->data);
 	ret = RTE_PTR_ADD(arr->data, idx * arr->elt_sz);
 
 	return ret;
lib/eal/common/eal_common_memory.c +20 -11
index c62edf5e55..ee7054ec14 100644
@@ -309,6 +309,9 @@ virt2memseg(const void *addr, const struct rte_memseg_list *msl)
 
 	/* a memseg list was specified, check if it's the right one */
 	start = msl->base_va;
+	if (start == NULL)
+		return NULL;
+
 	end = RTE_PTR_ADD(start, msl->len);
 
 	if (addr < start || addr >= end)
@@ -332,6 +335,8 @@ virt2memseg_list(const void *addr)
 		msl = &mcfg->memsegs[msl_idx];
 
 		start = msl->base_va;
+		if (start == NULL)
+			continue;
 		end = RTE_PTR_ADD(start, msl->len);
 		if (addr >= start && addr < end)
 			break;
@@ -680,10 +685,16 @@ RTE_EXPORT_SYMBOL(rte_mem_lock_page)
 int
 rte_mem_lock_page(const void *virt)
 {
-	uintptr_t virtual = (uintptr_t)virt;
 	size_t page_size = rte_mem_page_size();
-	uintptr_t aligned = RTE_PTR_ALIGN_FLOOR(virtual, page_size);
-	return rte_mem_lock((void *)aligned, page_size);
+	const void *aligned;
+
+	if (virt == NULL) {
+		rte_errno = EINVAL;
+		return -1;
+	}
+
+	aligned = RTE_PTR_ALIGN_FLOOR(virt, page_size);
+	return rte_mem_lock(aligned, page_size);
 }
 
 RTE_EXPORT_SYMBOL(rte_memseg_contig_walk_thread_unsafe)
@@ -1447,7 +1458,7 @@ handle_eal_memseg_info_request(const char *cmd __rte_unused,
 
 	ms_iova = ms->iova;
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 	ms_size = ms->len;
 	hugepage_size = ms->hugepage_sz;
 	ms_socket_id = ms->socket_id;
@@ -1519,7 +1530,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 	rte_mcfg_mem_read_unlock();
 
 	rte_tel_data_start_dict(d);
@@ -1530,8 +1541,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = elem_start_addr + elem->size;
 
 		if ((uint64_t)elem_start_addr >= ms_start_addr &&
 		    (uint64_t)elem_end_addr <= ms_end_addr)
@@ -1553,7 +1563,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	struct rte_mem_config *mcfg;
 	struct rte_memseg_list *msl;
 	const struct rte_memseg *ms;
-	struct malloc_elem *elem;
+	struct malloc_elem *volatile elem;
 	struct malloc_heap *heap;
 	struct rte_tel_data *c;
 	uint64_t ms_start_addr, ms_end_addr;
@@ -1597,7 +1607,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 
 	rte_mcfg_mem_read_unlock();
 
@@ -1609,8 +1619,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = elem_start_addr + elem->size;
 
 		if (elem_start_addr < ms_start_addr ||
 				elem_end_addr > ms_end_addr) {
lib/eal/common/eal_common_options.c +1 -1
index 485655865d..0e05683a67 100644
@@ -1656,7 +1656,7 @@ eal_parse_base_virtaddr(const char *arg)
 	 * on x86 and other architectures.
 	 */
 	internal_conf->base_virtaddr =
-		RTE_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
+		(uintptr_t) RTE_INT_PTR_ALIGN_CEIL(addr, (size_t)RTE_PGSIZE_16M);
 
 	return 0;
 }
lib/eal/common/malloc_elem.h +27 -7
index c7ff6718f8..babaead7de 100644
@@ -79,9 +79,11 @@ static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
 #define MALLOC_TRAILER_COOKIE  0xadd2e55badbadbadULL /**< Trailer cookie.*/
 
 /* define macros to make referencing the header and trailer cookies easier */
-#define MALLOC_ELEM_TRAILER(elem) (*((uint64_t*)RTE_PTR_ADD(elem, \
-		elem->size - MALLOC_ELEM_TRAILER_LEN)))
-#define MALLOC_ELEM_HEADER(elem) (elem->header_cookie)
+#define MALLOC_ELEM_TRAILER(elem) \
+	/* typeof preserves qualifiers (const/volatile) of elem */ \
+	(*(typeof((elem)->header_cookie) *)RTE_PTR_ADD(elem, \
+		(elem)->size - MALLOC_ELEM_TRAILER_LEN))
+#define MALLOC_ELEM_HEADER(elem) ((elem)->header_cookie)
 
 static inline void
 set_header(struct malloc_elem *elem)
@@ -306,13 +308,31 @@ old_malloc_size(struct malloc_elem *elem)
 static inline struct malloc_elem *
 malloc_elem_from_data(const void *data)
 {
+	struct malloc_elem *result;
+
 	if (data == NULL)
 		return NULL;
 
-	struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
-	if (!malloc_elem_cookies_ok(elem))
-		return NULL;
-	return elem->state != ELEM_PAD ? elem:  RTE_PTR_SUB(elem, elem->pad);
+	/* The allocator returns a pointer in the middle of an allocation pool.
+	 * GCC's interprocedural analysis can't trace this and warns about
+	 * out-of-bounds access when we do backwards pointer arithmetic to
+	 * find the malloc_elem header.
+	 */
+	__rte_diagnostic_push
+	__rte_diagnostic_ignored_array_bounds
+	{
+		struct malloc_elem *elem =
+			RTE_PTR_SUB(RTE_PTR_UNQUAL(data), MALLOC_ELEM_HEADER_LEN);
+
+		if (!malloc_elem_cookies_ok(elem))
+			result = NULL;
+		else
+			result = elem->state != ELEM_PAD ? elem :
+				RTE_PTR_SUB(elem, elem->pad);
+	}
+	__rte_diagnostic_pop
+
+	return result;
 }
 
 /*
lib/eal/freebsd/eal_memory.c +4 -0
index 6d3d46a390..49a89fe2fb 100644
@@ -178,6 +178,10 @@ rte_eal_hugepage_init(void)
 					"RTE_MAX_MEMSEG_PER_TYPE and/or RTE_MAX_MEM_MB_PER_TYPE in configuration.");
 				return -1;
 			}
+			if (msl->base_va == NULL) {
+				EAL_LOG(ERR, "Base VA is NULL for memseg list %d", msl_idx);
+				return -1;
+			}
 			arr = &msl->memseg_arr;
 			seg = rte_fbarray_get(arr, ms_idx);
 
lib/eal/include/rte_common.h +228 -14

286 lines not on this page · raw message

lib/eal/linux/eal_memalloc.c +6 -0
index 1e60e21620..f770826a43 100644
@@ -835,6 +835,12 @@ alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
 		void *map_addr;
 
 		cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
+
+		if (cur_msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			goto out;
+		}
+
 		map_addr = RTE_PTR_ADD(cur_msl->base_va,
 				cur_idx * page_sz);
 
lib/eal/linux/eal_memory.c +7 -0
index 8e1763e890..3830bd5d7f 100644
@@ -759,6 +759,13 @@ remap_segment(struct hugepage_file *hugepages, int seg_start, int seg_end)
 			return -1;
 		}
 		memseg_len = (size_t)page_sz;
+
+		if (msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			close(fd);
+			return -1;
+		}
+
 		addr = RTE_PTR_ADD(msl->base_va, ms_idx * memseg_len);
 
 		/* we know this address is already mmapped by memseg list, so
lib/eal/windows/eal_memalloc.c +6 -0
index 5db5a474cc..6432caccd3 100644
@@ -230,6 +230,12 @@ alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
 		void *map_addr;
 
 		cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
+
+		if (cur_msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			goto out;
+		}
+
 		map_addr = RTE_PTR_ADD(cur_msl->base_va, cur_idx * page_sz);
 
 		if (alloc_seg(cur, map_addr, wa->socket, wa->hi)) {
lib/graph/rte_graph.h +2 -2
index 7e433f4661..d8ac0011e4 100644
@@ -407,9 +407,9 @@ void rte_graph_obj_dump(FILE *f, struct rte_graph *graph, bool all);
 /** Macro to browse rte_node object after the graph creation */
 #define rte_graph_foreach_node(count, off, graph, node)                        \
 	for (count = 0, off = graph->nodes_start,                              \
-	     node = RTE_PTR_ADD(graph, off);                                   \
+	     node = RTE_PTR_ADD(RTE_PTR_UNQUAL(graph), off);                           \
 	     count < graph->nb_nodes;                                          \
-	     off = node->next, node = RTE_PTR_ADD(graph, off), count++)
+	     off = node->next, node = RTE_PTR_ADD(RTE_PTR_UNQUAL(graph), off), count++)
 
 /**
  * Get node object with in graph from id.
lib/latencystats/rte_latencystats.c +3 -0
index f61d5a273f..f2cd0db4b7 100644
@@ -104,6 +104,9 @@ latencystats_collect(uint64_t values[])
 	unsigned int i, scale;
 	const uint64_t *stats;
 
+	if (glob_stats == NULL)
+		return;
+
 	for (i = 0; i < NUM_LATENCY_STATS; i++) {
 		stats = RTE_PTR_ADD(glob_stats, lat_stats_strings[i].offset);
 		scale = lat_stats_strings[i].scale;
lib/mbuf/rte_mbuf.c +1 -0
index 0d931c7a15..557954d45e 100644
@@ -193,6 +193,7 @@ __rte_pktmbuf_init_extmem(struct rte_mempool *mp,
 
 	RTE_ASSERT(ctx->ext < ctx->ext_num);
 	RTE_ASSERT(ctx->off + ext_mem->elt_size <= ext_mem->buf_len);
+	RTE_ASSERT(ext_mem->buf_ptr);
 
 	m->buf_addr = RTE_PTR_ADD(ext_mem->buf_ptr, ctx->off);
 	rte_mbuf_iova_set(m, ext_mem->buf_iova == RTE_BAD_IOVA ? RTE_BAD_IOVA :
lib/mbuf/rte_mbuf.h +2 -0
index 2004391f57..3100feb740 100644
@@ -217,6 +217,7 @@ rte_mbuf_data_iova_default(const struct rte_mbuf *mb)
 static inline struct rte_mbuf *
 rte_mbuf_from_indirect(struct rte_mbuf *mi)
 {
+	RTE_ASSERT(mi);
 	return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
 }
 
@@ -289,6 +290,7 @@ rte_mbuf_to_baddr(struct rte_mbuf *md)
 static inline void *
 rte_mbuf_to_priv(struct rte_mbuf *m)
 {
+	RTE_ASSERT(m);
 	return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
 }
 
lib/member/rte_xxh64_avx512.h +3 -3
index 58f896ebb8..774b26d8df 100644
@@ -58,7 +58,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 			 _mm512_set1_epi64(key_len));
 
 	while (remaining >= 8) {
-		input = _mm512_set1_epi64(*(uint64_t *)RTE_PTR_ADD(key, offset));
+		input = _mm512_set1_epi64(*(const uint64_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 				xxh64_round_avx512(_mm512_setzero_si512(), input));
 		v_hash = _mm512_madd52lo_epu64(_mm512_set1_epi64(PRIME64_4),
@@ -71,7 +71,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	if (remaining >= 4) {
 		input = _mm512_set1_epi64
-			(*(uint32_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint32_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_1)));
@@ -86,7 +86,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	while (remaining != 0) {
 		input = _mm512_set1_epi64
-			(*(uint8_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint8_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_5)));
lib/mempool/rte_mempool.c +4 -4
index 3042d94c14..f1ff668205 100644
@@ -349,9 +349,9 @@ rte_mempool_populate_iova(struct rte_mempool *mp, char *vaddr,
 	memhdr->opaque = opaque;
 
 	if (mp->flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
-		off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
+		off = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, 8), vaddr);
 	else
-		off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN) - vaddr;
+		off = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN), vaddr);
 
 	if (off > len) {
 		ret = 0;
@@ -425,8 +425,8 @@ rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
 
 		/* populate with the largest group of contiguous pages */
 		for (phys_len = RTE_MIN(
-			(size_t)(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz) -
-				(addr + off)),
+			(size_t)RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz),
+				addr + off),
 			len - off);
 		     off + phys_len < len;
 		     phys_len = RTE_MIN(phys_len + pg_sz, len - off)) {
lib/mempool/rte_mempool.h +3 -0
index aedc100964..091976574a 100644
@@ -376,6 +376,7 @@ struct __rte_cache_aligned rte_mempool {
 static inline struct rte_mempool_objhdr *
 rte_mempool_get_header(void *obj)
 {
+	RTE_ASSERT(obj);
 	return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
 		sizeof(struct rte_mempool_objhdr));
 }
@@ -399,6 +400,7 @@ static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
 static inline struct rte_mempool_objtlr *rte_mempool_get_trailer(void *obj)
 {
 	struct rte_mempool *mp = rte_mempool_from_obj(obj);
+	RTE_ASSERT(mp);
 	return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
 }
 
@@ -1844,6 +1846,7 @@ rte_mempool_empty(const struct rte_mempool *mp)
 static inline rte_iova_t
 rte_mempool_virt2iova(const void *elt)
 {
+	RTE_ASSERT(elt);
 	const struct rte_mempool_objhdr *hdr;
 	hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
 		sizeof(*hdr));
lib/mempool/rte_mempool_ops_default.c +1 -1
index d27d6fc473..e28a288b91 100644
@@ -117,7 +117,7 @@ rte_mempool_op_populate_helper(struct rte_mempool *mp, unsigned int flags,
 	for (i = 0; i < max_objs; i++) {
 		/* avoid objects to cross page boundaries */
 		if (check_obj_bounds(va + off, pg_sz, total_elt_sz) < 0) {
-			off += RTE_PTR_ALIGN_CEIL(va + off, pg_sz) - (va + off);
+			off += RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(va + off, pg_sz), va + off);
 			if (flags & RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ)
 				off += total_elt_sz -
 					(((uintptr_t)(va + off - 1) %
lib/pdcp/pdcp_entity.h +5 -3
index f854192e98..7a2c41dd56 100644
@@ -198,17 +198,19 @@ struct entity_priv_ul_part {
 
 static inline struct entity_priv *
 entity_priv_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity), sizeof(struct rte_pdcp_entity));
 }
 
 static inline struct entity_priv_dl_part *
 entity_dl_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity),
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline struct entity_priv_ul_part *
 entity_ul_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity),
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline int
lib/vhost/vhost_user.c +10 -3
index 4bfb13fb98..a9ab6487f2 100644
@@ -824,9 +824,16 @@ void
 mem_set_dump(struct virtio_net *dev, void *ptr, size_t size, bool enable, uint64_t pagesz)
 {
 #ifdef MADV_DONTDUMP
-	void *start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz);
-	uintptr_t end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz);
-	size_t len = end - (uintptr_t)start;
+	void *start;
+	uintptr_t end;
+	size_t len;
+
+	if (ptr == NULL)
+		return;
+
+	start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz);
+	end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz);
+	len = end - (uintptr_t)start;
 
 	if (madvise(start, len, enable ? MADV_DODUMP : MADV_DONTDUMP) == -1) {
 		VHOST_CONFIG_LOG(dev->ifname, INFO,
Signature · 1 line
2.39.5 (Apple Git-154)
#23 lore raw

RE: [PATCH v14] eal: RTE_PTR_ADD/SUB API improvements

Quoted 11 lines

From: Scott Mitchell <[email protected]>

RTE_PTR_ADD and RTE_PTR_SUB APIs have a few limitations:
1. ptr cast to uintptr_t drops pointer provenance and prevents compiler optimizations
2. return cast discards qualifiers (const, volatile) which may hide correctness/concurrency issues.
3. Accepts both "pointers" and "integers as pointers" which overloads the use case and constrains the implementation to address other challenges.

Quoted 6 lines

This patch splits the API on two dimensions:
1. pointer types
2. integer types that represent pointers

This split allows addressing each of the challenges above and provides distinct APIs for the distinct use cases.

First of all, I think it's a huge improvement of the RTE_PTR_ADD/SUB macros to not discard qualifiers anymore. Silently discarding qualifiers will cause bugs (discarding volatile) and potentially degrade performance (discarding const).

And I agree with your approach of generally fixing instances where this qualifier-discarding property is abused across existing code. Qualifiers should only be explicitly discarded.

Unfortunately, fixing the macros changes their behavior, which may be considered API breakage. Personally, I consider the qualifier-discarding property of the current macros a bug, which makes your patch a bug fix. I would like the opinion of other community members on this!
It seems a patch can be both a bug fix and an API break. ;-)

Second, I think RTE_INT_PTR_ADD/SUB macros are superfluous.
Just use standard + and - operations when operating on integers, including "integers as pointers".

I guess existing instances of RTE_PTR_ADD/SUB operating on "integers as pointers" can be fixed across existing code to either use standard +/- operators or - probably more correct - use actual pointer types instead of integer types.

<feature creep>
If we decide to proceed with the RTE_PTR_ADD/SUB macros not discarding qualifiers, similar RTE_PTR_ macros should be updated similarly. But let's cross that bridge if we go there.
</feature creep>

-Morten

#24 lore raw

Re: [PATCH v14] eal: RTE_PTR_ADD/SUB API improvements

Quoted 2 lines

First of all, I think it's a huge improvement of the RTE_PTR_ADD/SUB macros to not discard qualifiers anymore. Silently discarding qualifiers will cause bugs (discarding volatile) and potentially degrade performance (discarding const).

Yay! It's been an iterative process but I agree it is converging to a better result.

Quoted 7 lines

And I agree with your approach of generally fixing instances where this qualifier-discarding property is abused across existing code. Qualifiers should only be explicitly discarded.

Unfortunately, fixing the macros changes their behavior, which may be considered API breakage. Personally, I consider the qualifier-discarding property of the current macros a bug, which makes your patch a bug fix. I would like the opinion of other community members on this!
It seems a patch can be both a bug fix and an API break. ;-)

I agree this is a breaking change for these APIs. Just to clarify that is OK for inclusion in main (for next LTS) and just debating how/if we backport, correct? If so, another option is to deprecate the existing APIs, stop using them in DPDK, and introduce implementations in this patch as a new API (e.g. RTE_PTR_ADD_TYPED) that is used instead.

Quoted 4 lines

Second, I think RTE_INT_PTR_ADD/SUB macros are superfluous.
Just use standard + and - operations when operating on integers, including "integers as pointers".

I guess existing instances of RTE_PTR_ADD/SUB operating on "integers as pointers" can be fixed across existing code to either use standard +/- operators or - probably more correct - use actual pointer types instead of integer types.

Good topic. The current RTE_INT_PTR_ADD macros are doing 2 things:
1. Offsetting by an amount
2. Safely converting to void* to prevent bugs where compilers assume aligned memory, which isn't guaranteed generally, and may crash at runtime (which I did in my first attempt which preserved pointer return type).

For (1), agreed for strict integer operation: use native operators (e.g. + or -). I tried to take that approach in this PR for clear-cut-cases.

For (2) I think there is still merit in having a macro to convert between domains safley (e.g. int -> pointer). This macro implementation would be the same as RTE_PTR_UNQUAL, but I think it's worth adding an explicit macro bcz the semantics/motivation may be different (and maybe we find a better way to do the conversion in the future). I can add RTE_PTR_FROM_INT(..) or similar to see what people think.

#25 lore raw

Re: [PATCH v14] eal: RTE_PTR_ADD/SUB API improvements

In reply to Morten Brørup
Quoted 4 lines

<feature creep>
If we decide to proceed with the RTE_PTR_ADD/SUB macros not discarding qualifiers, similar RTE_PTR_ macros should be updated similarly. But let's cross that bridge if we go there.
</feature creep>

Is RTE_PTR_DIFF the only remaining? There is no difference in assembly by casting to uintptr_t https://godbolt.org/z/z77nEr6Ts (I assume bcz integer return means pointer provenance isn't broken). Native pointer types will throw an error if the pointers are not of the same type which can strengthen type checking, but similar call-site analysis will be needed as there is mixed usage (different ptr types, int + ptr types).

#26 lore raw

v15 cover eal: RTE_PTR_ADD/SUB API improvements

In reply to [email protected]

From: Scott <[email protected]>

This series addresses two issues:

1. Removes the semantically incorrect __rte_alloc_size attribute from rte_lcore_var_alloc which was causing FORTIFY_SOURCE failures on Ubuntu 24.04 CI. The attribute was incorrect because the function returns a handle to per-lcore variables, not a direct pointer to 'size' bytes.

2. Improves RTE_PTR_ADD/SUB macros to preserve pointer type qualifiers (const/volatile) and use pointer arithmetic instead of integer casts to enable compiler optimizations and maintain pointer provenance.

The bugfix (patch 1) is placed first so it can be backported independently to stable branches.

---
v15:
- Fixed __rte_alloc_size, spilt into 2 patch series
- Replaced RTE_INT_PTR_ADD/SUB with simpler RTE_INT_PTR(val) macro users do int arithmetic directly: RTE_INT_PTR(addr + offset)

v14:
- fixed cpp compiler error, avoiding array pointer decay which is implicitly done in cpp (but not c)
- fixed MALLOC_ELEM_TRAILER const preservation compile error

v13:
- Added release notes documenting API changes
- Fixed alignment in test file: use alignas(uint32_t) for buffer
- Fixed NULL pointer handling in cdx_vfio.c: check base_va before RTE_PTR_ADD
- Added GCC array-bounds diagnostic suppression in malloc_elem_from_data()
- Added bug tracker reference for volatile cast issue in idxd_pci.c
- Improved __rte_auto_type documentation: added C++11 and C23 support
- Moved doxygen rationale for void* return type to @return blocks
- Fixed MALLOC_ELEM_TRAILER to use RTE_PTR_UNQUAL for write operations

v12:
- void* return type to avoid optimizations assuming aligned access which isn't generally safe/true.

v11:
- Split API into PTR and INT_PTR variants, update all usage of PTR API for new APIs.

v10:
- Use unit_test_suite_runner for easier subtest failure identification

v9:
- Fix include order: system includes, then DPDK includes, then application includes
- Use NOHUGE_OK and ASAN_OK constants in REGISTER_FAST_TEST (instead of true, true)

v8:
- Remove tests for types < 32-bit (bool, char, short, uint8_t, uint16_t)
- Merge test_ptr_add_sub_typedefs() into test_ptr_add_sub_integer_types()
- Separate RTE_PTR_ADD and RTE_PTR_SUB documentation
- Move Clang/GCC implementation note from Doxygen to regular comment
- Tests verify both intermediate ADD result and SUB round-trip
- Use uintptr_t cast consistently for all integer-to-pointer conversions
- Make TEST_RETVAL calculated from TEST_INITVAL + TEST_INCREMENT

v7:
- Fix tests: use TEST_BUFFER_SIZE macro for buffer allocation
- Fix tests: ADD then SUB same amount to avoid out-of-bounds pointer arithmetic
- All RTE_PTR_SUB tests now verify round-trip (ADD+SUB returns to original)

v6:
- Make char* optimization Clang-only to avoid GCC false positive warnings
- Improve tests: use named constants instead of magic numbers
- Improve tests: use void* casts on expected values instead of uintptr_t on results

v5:
- Initial implementation with char* arithmetic for all compilers

v4:
- Used _Generic for type-based dispatch with char* casts
- Had warnings on both Clang and GCC due to _Generic type-checking all branches

Scott Mitchell (2):
eal: remove alloc_size from rte_lcore_var_alloc
eal: RTE_PTR_ADD/SUB API improvements

 app/test-pmd/cmdline_flow.c                 |   4 +-
 app/test/meson.build                        |   1 +
 app/test/test_common.c                      |  20 +-
 app/test/test_ptr_add_sub.c                 | 199 +++++++++++++++++
 doc/guides/rel_notes/release_26_03.rst      |  11 +
 drivers/bus/cdx/cdx_vfio.c                  |  13 +-
 drivers/bus/pci/linux/pci.c                 |   6 +-
 drivers/bus/vmbus/linux/vmbus_uio.c         |   6 +-
 drivers/common/cnxk/roc_cpt_debug.c         |   4 +-
 drivers/common/cnxk/roc_ml.c                |   4 +-
 drivers/common/cnxk/roc_nix_bpf.c           |   2 +-
 drivers/common/cnxk/roc_nix_inl.h           |   4 +-
 drivers/common/cnxk/roc_nix_inl_dp.h        |   8 +-
 drivers/common/cnxk/roc_platform.h          |   2 +
 drivers/common/mlx5/mlx5_common_mr.c        |   2 +-
 drivers/dma/idxd/idxd_pci.c                 |  11 +-
 drivers/dma/odm/odm_dmadev.c                |   4 +-
 drivers/event/cnxk/cn10k_worker.c           |  32 +--
 drivers/event/cnxk/cn20k_worker.c           |  32 +--
 drivers/mempool/bucket/rte_mempool_bucket.c |   7 +-
 drivers/mempool/dpaa/dpaa_mempool.c         |   2 +-
 drivers/net/cxgbe/sge.c                     |   4 +-
 drivers/net/ena/ena_ethdev.c                |   9 +-
 drivers/net/intel/fm10k/fm10k.h             |   6 +-
 drivers/net/intel/fm10k/fm10k_rxtx_vec.c    |  12 +-
 drivers/net/mlx4/mlx4_txq.c                 |   3 +-
 lib/eal/common/eal_common_fbarray.c         |   2 +
 lib/eal/common/eal_common_memory.c          |  31 ++-
 lib/eal/common/eal_common_options.c         |   2 +-
 lib/eal/common/malloc_elem.h                |  34 ++-
 lib/eal/freebsd/eal_memory.c                |   4 +
 lib/eal/include/rte_common.h                | 231 ++++++++++++++++++--
 lib/eal/include/rte_lcore_var.h             |   2 +-
 lib/eal/linux/eal_memalloc.c                |   6 +
 lib/eal/linux/eal_memory.c                  |   7 +
 lib/eal/windows/eal_memalloc.c              |   6 +
 lib/graph/rte_graph.h                       |   4 +-
 lib/latencystats/rte_latencystats.c         |   3 +
 lib/mbuf/rte_mbuf.c                         |   1 +
 lib/mbuf/rte_mbuf.h                         |   2 +
 lib/member/rte_xxh64_avx512.h               |   6 +-
 lib/mempool/rte_mempool.c                   |   8 +-
 lib/mempool/rte_mempool.h                   |   3 +
 lib/mempool/rte_mempool_ops_default.c       |   2 +-
 lib/pdcp/pdcp_entity.h                      |   8 +-
 lib/vhost/vhost_user.c                      |  13 +-
 46 files changed, 648 insertions(+), 135 deletions(-)
 create mode 100644 app/test/test_ptr_add_sub.c
Signature · 1 line
2.39.5 (Apple Git-154)
#27 lore raw

PATCH v15 1/2 eal: remove alloc_size from rte_lcore_var_alloc

From: Scott Mitchell <[email protected]>

The __rte_alloc_size(1) attribute on rte_lcore_var_alloc() is semantically incorrect and causes false positives with FORTIFY_SOURCE runtime checks.

The attribute tells the compiler that the function returns a pointer to 'size' bytes of usable memory. However, rte_lcore_var_alloc() actually returns a handle to a per-lcore variable scheme. The allocator internally allocates 'size' bytes per lcore (size * RTE_MAX_LCORE total), partitioned into per-lcore sections. The handle points to lcore 0's copy, and accessed via RTE_LCORE_VAR_LCORE(lcore_id, handle) which computes:
handle + (lcore_id * RTE_MAX_LCORE_VAR). Access is expected beyond 'size' bytes beyond the returned pointer when 'lcore_id != 0' but FORTIFY_SOURCE may terminate the program due to out of bounds access.

This can be observed on CI with gcc 13.3.0 in lcore_var_autotest with '*** buffer overflow detected ***: terminated' if pointer provenance is preserved (e.g. if offsets avoid casting to uintptr_t).

Fixes 5bce9bed67ad ("eal: add static per-lcore memory allocation facility")
---
 lib/eal/include/rte_lcore_var.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/eal/include/rte_lcore_var.h +1 -1
index ca31dff6fd..48b022e84e 100644
@@ -202,7 +202,7 @@ rte_lcore_var_lcore(unsigned int lcore_id, void *handle)
 __rte_experimental
 void *
 rte_lcore_var_alloc(size_t size, size_t align)
-	__rte_alloc_size(1) __rte_alloc_align(2);
+	__rte_alloc_align(2);
 
 #ifdef __cplusplus
 }
Signature · 1 line
2.39.5 (Apple Git-154)
#28 lore raw

PATCH v15 2/2 eal: RTE_PTR_ADD/SUB API improvements

In reply to [email protected]

From: Scott Mitchell <[email protected]>

RTE_PTR_ADD and RTE_PTR_SUB APIs have a few limitations:
1. ptr cast to uintptr_t drops pointer provenance and prevents compiler optimizations
2. return cast discards qualifiers (const, volatile) which may hide correctness/concurrency issues.
3. Accepts both "pointers" and "integers as pointers" which overloads the use case and constrains the implementation to address other challenges.

This patch splits the API on two dimensions:
1. pointer types
2. integer types that represent pointers

This split allows addressing each of the challenges above and provides distinct APIs for the distinct use cases. Examples:
1. Clang is able to optimize and improve __rte_raw_cksum (which uses RTE_PTR_ADD) by ~40% (100 bytes) to ~8x (1.5k bytes) TSC cycles/byte.
2. Refactoring discovered cases that dropped qualifiers (volatile) that the new API exposes.

---
 app/test-pmd/cmdline_flow.c                 |   4 +-
 app/test/meson.build                        |   1 +
 app/test/test_common.c                      |  20 +-
 app/test/test_ptr_add_sub.c                 | 199 +++++++++++++++++
 doc/guides/rel_notes/release_26_03.rst      |  11 +
 drivers/bus/cdx/cdx_vfio.c                  |  13 +-
 drivers/bus/pci/linux/pci.c                 |   6 +-
 drivers/bus/vmbus/linux/vmbus_uio.c         |   6 +-
 drivers/common/cnxk/roc_cpt_debug.c         |   4 +-
 drivers/common/cnxk/roc_ml.c                |   4 +-
 drivers/common/cnxk/roc_nix_bpf.c           |   2 +-
 drivers/common/cnxk/roc_nix_inl.h           |   4 +-
 drivers/common/cnxk/roc_nix_inl_dp.h        |   8 +-
 drivers/common/cnxk/roc_platform.h          |   2 +
 drivers/common/mlx5/mlx5_common_mr.c        |   2 +-
 drivers/dma/idxd/idxd_pci.c                 |  11 +-
 drivers/dma/odm/odm_dmadev.c                |   4 +-
 drivers/event/cnxk/cn10k_worker.c           |  32 +--
 drivers/event/cnxk/cn20k_worker.c           |  32 +--
 drivers/mempool/bucket/rte_mempool_bucket.c |   7 +-
 drivers/mempool/dpaa/dpaa_mempool.c         |   2 +-
 drivers/net/cxgbe/sge.c                     |   4 +-
 drivers/net/ena/ena_ethdev.c                |   9 +-
 drivers/net/intel/fm10k/fm10k.h             |   6 +-
 drivers/net/intel/fm10k/fm10k_rxtx_vec.c    |  12 +-
 drivers/net/mlx4/mlx4_txq.c                 |   3 +-
 lib/eal/common/eal_common_fbarray.c         |   2 +
 lib/eal/common/eal_common_memory.c          |  31 ++-
 lib/eal/common/eal_common_options.c         |   2 +-
 lib/eal/common/malloc_elem.h                |  34 ++-
 lib/eal/freebsd/eal_memory.c                |   4 +
 lib/eal/include/rte_common.h                | 231 ++++++++++++++++++--
 lib/eal/linux/eal_memalloc.c                |   6 +
 lib/eal/linux/eal_memory.c                  |   7 +
 lib/eal/windows/eal_memalloc.c              |   6 +
 lib/graph/rte_graph.h                       |   4 +-
 lib/latencystats/rte_latencystats.c         |   3 +
 lib/mbuf/rte_mbuf.c                         |   1 +
 lib/mbuf/rte_mbuf.h                         |   2 +
 lib/member/rte_xxh64_avx512.h               |   6 +-
 lib/mempool/rte_mempool.c                   |   8 +-
 lib/mempool/rte_mempool.h                   |   3 +
 lib/mempool/rte_mempool_ops_default.c       |   2 +-
 lib/pdcp/pdcp_entity.h                      |   8 +-
 lib/vhost/vhost_user.c                      |  13 +-
 45 files changed, 647 insertions(+), 134 deletions(-)
 create mode 100644 app/test/test_ptr_add_sub.c
app/test-pmd/cmdline_flow.c +2 -2
index ebc036b14b..8c2fc6c7b6 100644
@@ -12468,7 +12468,7 @@ parse_meter_color(struct context *ctx, const struct token *token,
 
 		if (!arg)
 			return -1;
-		*(int *)RTE_PTR_ADD(action->conf, arg->offset) = i;
+		*(int *)RTE_PTR_ADD(RTE_PTR_UNQUAL(action->conf), arg->offset) = i;
 	} else {
 		((struct rte_flow_item_meter_color *)
 			ctx->object)->color = (enum rte_color)i;
@@ -13351,7 +13351,7 @@ indirect_action_flow_conf_create(const struct buffer *in)
 		indlst_conf = NULL;
 		goto end;
 	}
-	indlst_conf->conf = RTE_PTR_ADD(indlst_conf, base + len);
+	indlst_conf->conf = (const void **)RTE_PTR_ADD(indlst_conf, base + len);
 	for (i = 0; i < indlst_conf->conf_num; i++)
 		indlst_conf->conf[i] = indlst_conf->actions[i].conf;
 	SLIST_INSERT_HEAD(&indlst_conf_head, indlst_conf, next);
app/test/meson.build +1 -0
index f4d04a6e42..aa56fc4297 100644
@@ -152,6 +152,7 @@ source_file_deps = {
     'test_power_intel_uncore.c': ['power', 'power_intel_uncore'],
     'test_power_kvm_vm.c': ['power', 'power_kvm_vm'],
     'test_prefetch.c': [],
+    'test_ptr_add_sub.c': [],
     'test_ptr_compress.c': ['ptr_compress'],
     'test_rand_perf.c': [],
     'test_rawdev.c': ['rawdev', 'bus_vdev', 'raw_skeleton'],
app/test/test_common.c +10 -10
index 3e1c7df0c1..7332ff53f4 100644
@@ -37,10 +37,10 @@ test_macros(int __rte_unused unused_parm)
 	RTE_SWAP(smaller, bigger);
 	RTE_TEST_ASSERT(smaller == BIGGER && bigger == SMALLER,
 		"RTE_SWAP");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF), BIGGER,
-		"RTE_PTR_ADD");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF), SMALLER,
-		"RTE_PTR_SUB");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR(SMALLER + PTR_DIFF), (void *)BIGGER,
+		"RTE_INT_PTR");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR(BIGGER - PTR_DIFF), (void *)SMALLER,
+		"RTE_INT_PTR");
 	RTE_TEST_ASSERT_EQUAL(RTE_PTR_DIFF(BIGGER, SMALLER), PTR_DIFF,
 		"RTE_PTR_DIFF");
 	RTE_TEST_ASSERT_EQUAL(RTE_MAX(SMALLER, BIGGER), BIGGER,
@@ -188,18 +188,18 @@ test_align(void)
 			if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
-			val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN_FLOOR((uintptr_t) i, p);
 			if (ERROR_FLOOR(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_FLOOR", i, p);
 
 			val = RTE_ALIGN_FLOOR(i, p);
 			if (ERROR_FLOOR(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
 			/* align ceiling */
-			val = RTE_PTR_ALIGN((uintptr_t) i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN((uintptr_t) i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN", i, p);
 
 			val = RTE_ALIGN(i, p);
 			if (ERROR_CEIL(val, i, p))
@@ -209,9 +209,9 @@ test_align(void)
 			if (ERROR_CEIL(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
 
-			val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN_CEIL((uintptr_t)i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_CEIL", i, p);
 
 			/* by this point we know that val is aligned to p */
 			if (!rte_is_aligned((void*)(uintptr_t) val, p))
app/test/test_ptr_add_sub.c new file +199 -0

199 lines not on this page · raw message

doc/guides/rel_notes/release_26_03.rst +11 -0
index 15dabee7a1..cabec95b37 100644
@@ -84,6 +84,17 @@ API Changes
    Also, make sure to start the actual text at the margin.
    =======================================================
 
+* eal: Improved pointer arithmetic macros to preserve pointer provenance and type qualifiers.
+
+  * ``RTE_PTR_ADD`` and ``RTE_PTR_SUB`` now preserve const/volatile qualifiers
+    and use pointer arithmetic instead of integer casts to enable compiler optimizations.
+  * Passing NULL to ``RTE_PTR_ADD`` or ``RTE_PTR_SUB`` is now undefined behavior.
+  * Added ``RTE_INT_PTR`` for converting integer addresses to pointers.
+  * Added ``RTE_INT_PTR_ALIGN``, ``RTE_INT_PTR_ALIGN_FLOOR``, and ``RTE_INT_PTR_ALIGN_CEIL``
+    for aligning integer addresses.
+  * Existing code using ``RTE_PTR_ADD``/``RTE_PTR_SUB`` with integer types should migrate
+    to ``RTE_INT_PTR_*`` variants for clarity and correctness.
+
 
 ABI Changes
 -----------
drivers/bus/cdx/cdx_vfio.c +10 -3
index 11fe3265d2..a1009bc0ca 100644
@@ -367,9 +367,16 @@ cdx_vfio_get_region_info(int vfio_dev_fd, struct vfio_region_info **info,
 static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
-	size_t sz = msl->len;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
-	void **max_va = arg;
+	size_t sz;
+	void *end_va;
+	void **max_va;
+
+	if (msl->base_va == NULL)
+		return 0;
+
+	sz = msl->len;
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
+	max_va = arg;
 
 	if (*max_va < end_va)
 		*max_va = end_va;
drivers/bus/pci/linux/pci.c +5 -1
index 2ffac82e94..455db2cdec 100644
@@ -109,9 +109,13 @@ static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
 	size_t sz = msl->len;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
+	void *end_va;
 	void **max_va = arg;
 
+	if (msl->base_va == NULL)
+		return 0;
+
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
 	if (*max_va < end_va)
 		*max_va = end_va;
 	return 0;
drivers/bus/vmbus/linux/vmbus_uio.c +5 -1
index fbafc5027d..0ef05c0096 100644
@@ -122,9 +122,13 @@ static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
 	size_t sz = msl->memseg_arr.len * msl->page_sz;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
+	void *end_va;
 	void **max_va = arg;
 
+	if (msl->base_va == NULL)
+		return 0;
+
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
 	if (*max_va < end_va)
 		*max_va = end_va;
 	return 0;
drivers/common/cnxk/roc_cpt_debug.c +2 -2
index 28aedf088e..252658a83f 100644
@@ -56,7 +56,7 @@ cpt_cnxk_parse_hdr_dump(FILE *file, const struct cpt_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*32B */
 	offset = cpth->w2.ptr_offset;
 	offset = (((offset - 1) & 0x7) + 1) * 32;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD(PLT_PTR_UNQUAL(cpth), offset);
 
 	if (cpth->w0.num_frags > 0) {
 		cpt_dump(file, "CPT Fraginfo_0 \t%p:", frag_info);
@@ -162,7 +162,7 @@ cpt_cn10k_parse_hdr_dump(FILE *file, const struct cpt_cn10k_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*8B */
 	offset = cpth->w2.fi_offset;
 	offset = (((offset - 1) & 0x1f) + 1) * 8;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD(PLT_PTR_UNQUAL(cpth), offset);
 
 	cpt_dump(file, "CPT Fraginfo \t0x%p:", frag_info);
 
drivers/common/cnxk/roc_ml.c +3 -1
index 7390697b1d..aa24dbb82f 100644
@@ -589,7 +589,9 @@ roc_ml_blk_init(struct roc_bphy *roc_bphy, struct roc_ml *roc_ml)
 
 	plt_ml_dbg(
 		"MLAB: Physical Address : 0x%016lx",
-		PLT_PTR_ADD_U64_CAST(ml->pci_dev->mem_resource[0].phys_addr, ML_MLAB_BLK_OFFSET));
+		   PLT_PTR_ADD_U64_CAST(
+				   PLT_INT_PTR(ml->pci_dev->mem_resource[0].phys_addr),
+				   ML_MLAB_BLK_OFFSET));
 	plt_ml_dbg("MLAB: Virtual Address : 0x%016lx",
 		   PLT_PTR_ADD_U64_CAST(ml->pci_dev->mem_resource[0].addr, ML_MLAB_BLK_OFFSET));
 
drivers/common/cnxk/roc_nix_bpf.c +1 -1
index 98c9855a5b..c42fb7e004 100644
@@ -160,7 +160,7 @@ nix_precolor_conv_table_write(struct roc_nix *roc_nix, uint64_t val,
 	struct nix *nix = roc_nix_to_nix_priv(roc_nix);
 	int64_t *addr;
 
-	addr = PLT_PTR_ADD(nix->base, off);
+	addr = PLT_INT_PTR(nix->base + off);
 	plt_write64(val, addr);
 }
 
drivers/common/cnxk/roc_nix_inl.h +2 -2
index 7970ac2258..3526ec9268 100644
@@ -59,7 +59,7 @@ roc_nix_inl_on_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline struct roc_ie_on_outb_sa *
@@ -67,7 +67,7 @@ roc_nix_inl_on_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline void *
drivers/common/cnxk/roc_nix_inl_dp.h +4 -4
index eb101db179..ae161e02ed 100644
@@ -49,7 +49,7 @@ roc_nix_inl_ot_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline struct roc_ot_ipsec_outb_sa *
@@ -57,7 +57,7 @@ roc_nix_inl_ot_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline void *
@@ -77,7 +77,7 @@ roc_nix_inl_ow_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline struct roc_ow_ipsec_outb_sa *
@@ -85,7 +85,7 @@ roc_nix_inl_ow_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline void *
drivers/common/cnxk/roc_platform.h +2 -0
index e22a50d47a..4a5baa9a78 100644
@@ -47,6 +47,8 @@
 #define PLT_PTR_ADD		 RTE_PTR_ADD
 #define PLT_PTR_SUB		 RTE_PTR_SUB
 #define PLT_PTR_DIFF		 RTE_PTR_DIFF
+#define PLT_PTR_UNQUAL		 RTE_PTR_UNQUAL
+#define PLT_INT_PTR		 RTE_INT_PTR
 #define PLT_MAX_RXTX_INTR_VEC_ID RTE_MAX_RXTX_INTR_VEC_ID
 #define PLT_INTR_VEC_RXTX_OFFSET RTE_INTR_VEC_RXTX_OFFSET
 #define PLT_MIN			 RTE_MIN
drivers/common/mlx5/mlx5_common_mr.c +1 -1
index 8ed988dec9..aae39fdb6e 100644
@@ -1447,7 +1447,7 @@ mlx5_mempool_get_extmem_cb(struct rte_mempool *mp, void *opaque,
 	seg = &heap[data->heap_size - 1];
 	msl = rte_mem_virt2memseg_list((void *)addr);
 	page_size = msl != NULL ? msl->page_sz : rte_mem_page_size();
-	page_start = RTE_PTR_ALIGN_FLOOR(addr, page_size);
+	page_start = RTE_ALIGN_FLOOR(addr, page_size);
 	seg->start = page_start;
 	seg->end = page_start + page_size;
 	/* Maintain the heap order. */
drivers/dma/idxd/idxd_pci.c +7 -4
index 214f6f22d5..fb76a050b1 100644
@@ -59,7 +59,7 @@ idxd_pci_dev_command(struct idxd_dmadev *idxd, enum rte_idxd_cmds command)
 	return err_code;
 }
 
-static uint32_t *
+static volatile uint32_t *
 idxd_get_wq_cfg(struct idxd_pci_common *pci, uint8_t wq_idx)
 {
 	return RTE_PTR_ADD(pci->wq_regs_base,
@@ -205,9 +205,9 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,
 	pci->regs = dev->mem_resource[0].addr;
 	version = pci->regs->version;
 	grp_offset = (uint16_t)pci->regs->offsets[0];
-	pci->grp_regs = RTE_PTR_ADD(pci->regs, grp_offset * 0x100);
+	pci->grp_regs = RTE_PTR_ADD((volatile void *)pci->regs, grp_offset * 0x100);
 	wq_offset = (uint16_t)(pci->regs->offsets[0] >> 16);
-	pci->wq_regs_base = RTE_PTR_ADD(pci->regs, wq_offset * 0x100);
+	pci->wq_regs_base = RTE_PTR_ADD((volatile void *)pci->regs, wq_offset * 0x100);
 	pci->portals = dev->mem_resource[2].addr;
 	pci->wq_cfg_sz = (pci->regs->wqcap >> 24) & 0x0F;
 
@@ -395,7 +395,10 @@ idxd_dmadev_probe_pci(struct rte_pci_driver *drv, struct rte_pci_device *dev)
 		/* add the queue number to each device name */
 		snprintf(qname, sizeof(qname), "%s-q%d", name, qid);
 		idxd.qid = qid;
-		idxd.portal = RTE_PTR_ADD(idxd.u.pci->portals,
+		/* FIXME: cast drops volatile propagation to idxd_dmadev.portal
+		 * See: https://bugs.dpdk.org/show_bug.cgi?id=1871
+		 */
+		idxd.portal = RTE_PTR_ADD(RTE_PTR_UNQUAL(idxd.u.pci->portals),
 				qid * IDXD_PORTAL_SIZE);
 		if (idxd_is_wq_enabled(&idxd))
 			IDXD_PMD_ERR("Error, WQ %u seems enabled", qid);
drivers/dma/odm/odm_dmadev.c +2 -2
index a2f4ed9a8e..3b4b058427 100644
@@ -422,7 +422,7 @@ odm_dmadev_completed(void *dev_private, uint16_t vchan, const uint16_t nb_cpls,
 	int cnt;
 
 	vq = &odm->vq[vchan];
-	const uint32_t *base_addr = vq->cring_mz->addr;
+	uint32_t *base_addr = vq->cring_mz->addr;
 	const uint16_t cring_max_entry = vq->cring_max_entry;
 
 	cring_head = vq->cring_head;
@@ -482,7 +482,7 @@ odm_dmadev_completed_status(void *dev_private, uint16_t vchan, const uint16_t nb
 	int cnt;
 
 	vq = &odm->vq[vchan];
-	const uint32_t *base_addr = vq->cring_mz->addr;
+	uint32_t *base_addr = vq->cring_mz->addr;
 	const uint16_t cring_max_entry = vq->cring_max_entry;
 
 	cring_head = vq->cring_head;
drivers/event/cnxk/cn10k_worker.c +16 -16
index 80077ec8a1..cd35cc3e55 100644
@@ -261,14 +261,14 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 					aw7);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 64), aw4);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 80), aw5);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 96), aw6);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 112), aw7);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 128);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 32), aw2);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 48), aw3);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 64), aw4);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 80), aw5);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 96), aw6);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 112), aw7);
+			lmt_addr += 128;
 		} break;
 		case 4: {
 			uint64x2_t aw0, aw1, aw2, aw3;
@@ -291,10 +291,10 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 					aw3);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 64);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 32), aw2);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 48), aw3);
+			lmt_addr += 64;
 		} break;
 		case 2: {
 			uint64x2_t aw0, aw1;
@@ -310,8 +310,8 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 					aw1);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 32);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			lmt_addr += 32;
 		} break;
 		case 1: {
 			__uint128_t aw0;
@@ -322,7 +322,7 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 			aw0 |= (uint64_t)ev[0].sched_type << 32;
 
 			*((__uint128_t *)lmt_addr) = aw0;
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+			lmt_addr += 16;
 		} break;
 		}
 		ev += parts;
@@ -338,7 +338,7 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 		aw0 |= ev[0].event & (BIT_ULL(32) - 1);
 		aw0 |= (uint64_t)ev[0].sched_type << 32;
 		*((__uint128_t *)lmt_addr) = aw0;
-		lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+		lmt_addr += 16;
 	}
 #endif
 
drivers/event/cnxk/cn20k_worker.c +16 -16
index 53daf3b4b0..2113a2ea78 100644
@@ -231,14 +231,14 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw7 = vorrq_u64(vandq_u64(vshrq_n_u64(aw7, 6), tt_mask), aw7);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 64), aw4);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 80), aw5);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 96), aw6);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 112), aw7);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 128);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 32), aw2);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 48), aw3);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 64), aw4);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 80), aw5);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 96), aw6);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 112), aw7);
+			lmt_addr += 128;
 		} break;
 		case 4: {
 			uint64x2_t aw0, aw1, aw2, aw3;
@@ -253,10 +253,10 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw3 = vorrq_u64(vandq_u64(vshrq_n_u64(aw3, 6), tt_mask), aw3);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 64);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 32), aw2);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 48), aw3);
+			lmt_addr += 64;
 		} break;
 		case 2: {
 			uint64x2_t aw0, aw1;
@@ -268,8 +268,8 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw1 = vorrq_u64(vandq_u64(vshrq_n_u64(aw1, 6), tt_mask), aw1);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 32);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			lmt_addr += 32;
 		} break;
 		case 1: {
 			__uint128_t aw0;
@@ -280,7 +280,7 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw0 |= (uint64_t)ev[0].sched_type << 32;
 
 			*((__uint128_t *)lmt_addr) = aw0;
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+			lmt_addr += 16;
 		} break;
 		}
 		ev += parts;
@@ -296,7 +296,7 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 		aw0 |= ev[0].event & (BIT_ULL(32) - 1);
 		aw0 |= (uint64_t)ev[0].sched_type << 32;
 		*((__uint128_t *)lmt_addr) = aw0;
-		lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+		lmt_addr += 16;
 	}
 #endif
 
drivers/mempool/bucket/rte_mempool_bucket.c +3 -4
index c0b480bfc7..6fee10176b 100644
@@ -376,8 +376,8 @@ count_underfilled_buckets(struct rte_mempool *mp,
 	uintptr_t align;
 	uint8_t *iter;
 
-	align = (uintptr_t)RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz) -
-		(uintptr_t)memhdr->addr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz),
+			     memhdr->addr);
 
 	for (iter = (uint8_t *)memhdr->addr + align;
 	     iter < (uint8_t *)memhdr->addr + memhdr->len;
@@ -602,8 +602,7 @@ bucket_populate(struct rte_mempool *mp, unsigned int max_objs,
 		return -EINVAL;
 
 	bucket_page_sz = rte_align32pow2(bd->bucket_mem_size);
-	align = RTE_PTR_ALIGN_CEIL((uintptr_t)vaddr, bucket_page_sz) -
-		(uintptr_t)vaddr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, bucket_page_sz), vaddr);
 
 	bucket_header_sz = bd->header_size - mp->header_size;
 	if (iova != RTE_BAD_IOVA)
drivers/mempool/dpaa/dpaa_mempool.c +1 -1
index 2f9395b3f4..85e1c01017 100644
@@ -321,7 +321,7 @@ dpaa_adjust_obj_bounds(char *va, size_t *offset,
 	size_t off = *offset;
 
 	if (dpaa_check_obj_bounds(va + off, pg_sz, total) == false) {
-		off += RTE_PTR_ALIGN_CEIL(va + off, pg_sz) - (va + off);
+		off += RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(va + off, pg_sz), va + off);
 		if (flags & RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ)
 			off += total - ((((size_t)va + off - 1) % total) + 1);
 	}
drivers/net/cxgbe/sge.c +2 -2
index e9d45f24c4..a5d112d52d 100644
@@ -591,7 +591,7 @@ static void write_sgl(struct rte_mbuf *mbuf, struct sge_txq *q,
 			memcpy(sgl->sge, buf, part0);
 		part1 = RTE_PTR_DIFF((u8 *)end, (u8 *)q->stat);
 		rte_memcpy(q->desc, RTE_PTR_ADD((u8 *)buf, part0), part1);
-		end = RTE_PTR_ADD((void *)q->desc, part1);
+		end = RTE_PTR_ADD(q->desc, part1);
 	}
 	if ((uintptr_t)end & 8)           /* 0-pad to multiple of 16 */
 		*(u64 *)end = 0;
@@ -1297,7 +1297,7 @@ static void inline_tx_mbuf(const struct sge_txq *q, caddr_t from, caddr_t *to,
 		from = RTE_PTR_ADD(from, left);
 		left = len - left;
 		rte_memcpy((void *)q->desc, from, left);
-		*to = RTE_PTR_ADD((void *)q->desc, left);
+		*to = RTE_PTR_ADD(q->desc, left);
 	}
 }
 
drivers/net/ena/ena_ethdev.c +8 -1
index ea4afbc75d..da23b271d5 100644
@@ -2379,7 +2379,14 @@ static void *pci_bar_addr(struct rte_pci_device *dev, uint32_t bar)
 {
 	const struct rte_mem_resource *res = &dev->mem_resource[bar];
 	size_t offset = res->phys_addr % rte_mem_page_size();
-	void *vaddr = RTE_PTR_ADD(res->addr, offset);
+	void *vaddr;
+
+	if (res->addr == NULL) {
+		PMD_INIT_LOG_LINE(ERR, "PCI BAR [%u] address is NULL", bar);
+		return NULL;
+	}
+
+	vaddr = RTE_PTR_ADD(res->addr, offset);
 
 	PMD_INIT_LOG_LINE(INFO, "PCI BAR [%u]: phys_addr=0x%" PRIx64 ", addr=%p, offset=0x%zx, adjusted_addr=%p",
 		bar, res->phys_addr, res->addr, offset, vaddr);
drivers/net/intel/fm10k/fm10k.h +3 -3
index 0eb32ac0d0..4e3081785f 100644
@@ -264,9 +264,9 @@ fm10k_pktmbuf_reset(struct rte_mbuf *mb, uint16_t in_port)
 	mb->nb_segs = 1;
 
 	/* enforce 512B alignment on default Rx virtual addresses */
-	mb->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb->buf_addr +
-			RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-			- (char *)mb->buf_addr);
+	mb->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb->buf_addr +
+			RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+			(char *)mb->buf_addr);
 	mb->port = in_port;
 }
 
drivers/net/intel/fm10k/fm10k_rxtx_vec.c +6 -6
index 0eada7275e..a08af75bc7 100644
@@ -315,12 +315,12 @@ fm10k_rxq_rearm(struct fm10k_rx_queue *rxq)
 		_mm_store_si128(RTE_CAST_PTR(__m128i *, &rxdp++->q), dma_addr1);
 
 		/* enforce 512B alignment on default Rx virtual addresses */
-		mb0->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb0->buf_addr
-				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-				- (char *)mb0->buf_addr);
-		mb1->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb1->buf_addr
-				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-				- (char *)mb1->buf_addr);
+		mb0->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb0->buf_addr
+				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+				(char *)mb0->buf_addr);
+		mb1->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb1->buf_addr
+				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+				(char *)mb1->buf_addr);
 	}
 
 	rxq->rxrearm_start += RTE_FM10K_RXQ_REARM_THRESH;
drivers/net/mlx4/mlx4_txq.c +2 -1
index 0db2e55bef..348040c1b9 100644
@@ -114,7 +114,8 @@ txq_uar_uninit_secondary(struct txq *txq)
 	void *addr;
 
 	addr = ppriv->uar_table[txq->stats.idx];
-	munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
+	if (addr)
+		munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
 }
 
 /**
lib/eal/common/eal_common_fbarray.c +2 -0
index 8bdcefb717..c4e03f45f7 100644
@@ -10,6 +10,7 @@
 #include <unistd.h>
 
 #include <rte_common.h>
+#include <rte_debug.h>
 #include <rte_eal_paging.h>
 #include <rte_errno.h>
 #include <rte_log.h>
@@ -1058,6 +1059,7 @@ rte_fbarray_get(const struct rte_fbarray *arr, unsigned int idx)
 		return NULL;
 	}
 
+	RTE_ASSERT(arr->data);
 	ret = RTE_PTR_ADD(arr->data, idx * arr->elt_sz);
 
 	return ret;
lib/eal/common/eal_common_memory.c +20 -11
index c62edf5e55..ee7054ec14 100644
@@ -309,6 +309,9 @@ virt2memseg(const void *addr, const struct rte_memseg_list *msl)
 
 	/* a memseg list was specified, check if it's the right one */
 	start = msl->base_va;
+	if (start == NULL)
+		return NULL;
+
 	end = RTE_PTR_ADD(start, msl->len);
 
 	if (addr < start || addr >= end)
@@ -332,6 +335,8 @@ virt2memseg_list(const void *addr)
 		msl = &mcfg->memsegs[msl_idx];
 
 		start = msl->base_va;
+		if (start == NULL)
+			continue;
 		end = RTE_PTR_ADD(start, msl->len);
 		if (addr >= start && addr < end)
 			break;
@@ -680,10 +685,16 @@ RTE_EXPORT_SYMBOL(rte_mem_lock_page)
 int
 rte_mem_lock_page(const void *virt)
 {
-	uintptr_t virtual = (uintptr_t)virt;
 	size_t page_size = rte_mem_page_size();
-	uintptr_t aligned = RTE_PTR_ALIGN_FLOOR(virtual, page_size);
-	return rte_mem_lock((void *)aligned, page_size);
+	const void *aligned;
+
+	if (virt == NULL) {
+		rte_errno = EINVAL;
+		return -1;
+	}
+
+	aligned = RTE_PTR_ALIGN_FLOOR(virt, page_size);
+	return rte_mem_lock(aligned, page_size);
 }
 
 RTE_EXPORT_SYMBOL(rte_memseg_contig_walk_thread_unsafe)
@@ -1447,7 +1458,7 @@ handle_eal_memseg_info_request(const char *cmd __rte_unused,
 
 	ms_iova = ms->iova;
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 	ms_size = ms->len;
 	hugepage_size = ms->hugepage_sz;
 	ms_socket_id = ms->socket_id;
@@ -1519,7 +1530,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 	rte_mcfg_mem_read_unlock();
 
 	rte_tel_data_start_dict(d);
@@ -1530,8 +1541,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = elem_start_addr + elem->size;
 
 		if ((uint64_t)elem_start_addr >= ms_start_addr &&
 		    (uint64_t)elem_end_addr <= ms_end_addr)
@@ -1553,7 +1563,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	struct rte_mem_config *mcfg;
 	struct rte_memseg_list *msl;
 	const struct rte_memseg *ms;
-	struct malloc_elem *elem;
+	struct malloc_elem *volatile elem;
 	struct malloc_heap *heap;
 	struct rte_tel_data *c;
 	uint64_t ms_start_addr, ms_end_addr;
@@ -1597,7 +1607,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 
 	rte_mcfg_mem_read_unlock();
 
@@ -1609,8 +1619,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = elem_start_addr + elem->size;
 
 		if (elem_start_addr < ms_start_addr ||
 				elem_end_addr > ms_end_addr) {
lib/eal/common/eal_common_options.c +1 -1
index 485655865d..0e05683a67 100644
@@ -1656,7 +1656,7 @@ eal_parse_base_virtaddr(const char *arg)
 	 * on x86 and other architectures.
 	 */
 	internal_conf->base_virtaddr =
-		RTE_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
+		(uintptr_t) RTE_INT_PTR_ALIGN_CEIL(addr, (size_t)RTE_PGSIZE_16M);
 
 	return 0;
 }
lib/eal/common/malloc_elem.h +27 -7
index c7ff6718f8..babaead7de 100644
@@ -79,9 +79,11 @@ static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
 #define MALLOC_TRAILER_COOKIE  0xadd2e55badbadbadULL /**< Trailer cookie.*/
 
 /* define macros to make referencing the header and trailer cookies easier */
-#define MALLOC_ELEM_TRAILER(elem) (*((uint64_t*)RTE_PTR_ADD(elem, \
-		elem->size - MALLOC_ELEM_TRAILER_LEN)))
-#define MALLOC_ELEM_HEADER(elem) (elem->header_cookie)
+#define MALLOC_ELEM_TRAILER(elem) \
+	/* typeof preserves qualifiers (const/volatile) of elem */ \
+	(*(typeof((elem)->header_cookie) *)RTE_PTR_ADD(elem, \
+		(elem)->size - MALLOC_ELEM_TRAILER_LEN))
+#define MALLOC_ELEM_HEADER(elem) ((elem)->header_cookie)
 
 static inline void
 set_header(struct malloc_elem *elem)
@@ -306,13 +308,31 @@ old_malloc_size(struct malloc_elem *elem)
 static inline struct malloc_elem *
 malloc_elem_from_data(const void *data)
 {
+	struct malloc_elem *result;
+
 	if (data == NULL)
 		return NULL;
 
-	struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
-	if (!malloc_elem_cookies_ok(elem))
-		return NULL;
-	return elem->state != ELEM_PAD ? elem:  RTE_PTR_SUB(elem, elem->pad);
+	/* The allocator returns a pointer in the middle of an allocation pool.
+	 * GCC's interprocedural analysis can't trace this and warns about
+	 * out-of-bounds access when we do backwards pointer arithmetic to
+	 * find the malloc_elem header.
+	 */
+	__rte_diagnostic_push
+	__rte_diagnostic_ignored_array_bounds
+	{
+		struct malloc_elem *elem =
+			RTE_PTR_SUB(RTE_PTR_UNQUAL(data), MALLOC_ELEM_HEADER_LEN);
+
+		if (!malloc_elem_cookies_ok(elem))
+			result = NULL;
+		else
+			result = elem->state != ELEM_PAD ? elem :
+				RTE_PTR_SUB(elem, elem->pad);
+	}
+	__rte_diagnostic_pop
+
+	return result;
 }
 
 /*
lib/eal/freebsd/eal_memory.c +4 -0
index 6d3d46a390..49a89fe2fb 100644
@@ -178,6 +178,10 @@ rte_eal_hugepage_init(void)
 					"RTE_MAX_MEMSEG_PER_TYPE and/or RTE_MAX_MEM_MB_PER_TYPE in configuration.");
 				return -1;
 			}
+			if (msl->base_va == NULL) {
+				EAL_LOG(ERR, "Base VA is NULL for memseg list %d", msl_idx);
+				return -1;
+			}
 			arr = &msl->memseg_arr;
 			seg = rte_fbarray_get(arr, ms_idx);
 
lib/eal/include/rte_common.h +216 -15

285 lines not on this page · raw message

lib/eal/linux/eal_memalloc.c +6 -0
index 1e60e21620..f770826a43 100644
@@ -835,6 +835,12 @@ alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
 		void *map_addr;
 
 		cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
+
+		if (cur_msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			goto out;
+		}
+
 		map_addr = RTE_PTR_ADD(cur_msl->base_va,
 				cur_idx * page_sz);
 
lib/eal/linux/eal_memory.c +7 -0
index 8e1763e890..3830bd5d7f 100644
@@ -759,6 +759,13 @@ remap_segment(struct hugepage_file *hugepages, int seg_start, int seg_end)
 			return -1;
 		}
 		memseg_len = (size_t)page_sz;
+
+		if (msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			close(fd);
+			return -1;
+		}
+
 		addr = RTE_PTR_ADD(msl->base_va, ms_idx * memseg_len);
 
 		/* we know this address is already mmapped by memseg list, so
lib/eal/windows/eal_memalloc.c +6 -0
index 5db5a474cc..6432caccd3 100644
@@ -230,6 +230,12 @@ alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
 		void *map_addr;
 
 		cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
+
+		if (cur_msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			goto out;
+		}
+
 		map_addr = RTE_PTR_ADD(cur_msl->base_va, cur_idx * page_sz);
 
 		if (alloc_seg(cur, map_addr, wa->socket, wa->hi)) {
lib/graph/rte_graph.h +2 -2
index 7e433f4661..d8ac0011e4 100644
@@ -407,9 +407,9 @@ void rte_graph_obj_dump(FILE *f, struct rte_graph *graph, bool all);
 /** Macro to browse rte_node object after the graph creation */
 #define rte_graph_foreach_node(count, off, graph, node)                        \
 	for (count = 0, off = graph->nodes_start,                              \
-	     node = RTE_PTR_ADD(graph, off);                                   \
+	     node = RTE_PTR_ADD(RTE_PTR_UNQUAL(graph), off);                           \
 	     count < graph->nb_nodes;                                          \
-	     off = node->next, node = RTE_PTR_ADD(graph, off), count++)
+	     off = node->next, node = RTE_PTR_ADD(RTE_PTR_UNQUAL(graph), off), count++)
 
 /**
  * Get node object with in graph from id.
lib/latencystats/rte_latencystats.c +3 -0
index f61d5a273f..f2cd0db4b7 100644
@@ -104,6 +104,9 @@ latencystats_collect(uint64_t values[])
 	unsigned int i, scale;
 	const uint64_t *stats;
 
+	if (glob_stats == NULL)
+		return;
+
 	for (i = 0; i < NUM_LATENCY_STATS; i++) {
 		stats = RTE_PTR_ADD(glob_stats, lat_stats_strings[i].offset);
 		scale = lat_stats_strings[i].scale;
lib/mbuf/rte_mbuf.c +1 -0
index 0d931c7a15..557954d45e 100644
@@ -193,6 +193,7 @@ __rte_pktmbuf_init_extmem(struct rte_mempool *mp,
 
 	RTE_ASSERT(ctx->ext < ctx->ext_num);
 	RTE_ASSERT(ctx->off + ext_mem->elt_size <= ext_mem->buf_len);
+	RTE_ASSERT(ext_mem->buf_ptr);
 
 	m->buf_addr = RTE_PTR_ADD(ext_mem->buf_ptr, ctx->off);
 	rte_mbuf_iova_set(m, ext_mem->buf_iova == RTE_BAD_IOVA ? RTE_BAD_IOVA :
lib/mbuf/rte_mbuf.h +2 -0
index 2004391f57..3100feb740 100644
@@ -217,6 +217,7 @@ rte_mbuf_data_iova_default(const struct rte_mbuf *mb)
 static inline struct rte_mbuf *
 rte_mbuf_from_indirect(struct rte_mbuf *mi)
 {
+	RTE_ASSERT(mi);
 	return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
 }
 
@@ -289,6 +290,7 @@ rte_mbuf_to_baddr(struct rte_mbuf *md)
 static inline void *
 rte_mbuf_to_priv(struct rte_mbuf *m)
 {
+	RTE_ASSERT(m);
 	return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
 }
 
lib/member/rte_xxh64_avx512.h +3 -3
index 58f896ebb8..774b26d8df 100644
@@ -58,7 +58,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 			 _mm512_set1_epi64(key_len));
 
 	while (remaining >= 8) {
-		input = _mm512_set1_epi64(*(uint64_t *)RTE_PTR_ADD(key, offset));
+		input = _mm512_set1_epi64(*(const uint64_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 				xxh64_round_avx512(_mm512_setzero_si512(), input));
 		v_hash = _mm512_madd52lo_epu64(_mm512_set1_epi64(PRIME64_4),
@@ -71,7 +71,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	if (remaining >= 4) {
 		input = _mm512_set1_epi64
-			(*(uint32_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint32_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_1)));
@@ -86,7 +86,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	while (remaining != 0) {
 		input = _mm512_set1_epi64
-			(*(uint8_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint8_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_5)));
lib/mempool/rte_mempool.c +4 -4
index 3042d94c14..f1ff668205 100644
@@ -349,9 +349,9 @@ rte_mempool_populate_iova(struct rte_mempool *mp, char *vaddr,
 	memhdr->opaque = opaque;
 
 	if (mp->flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
-		off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
+		off = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, 8), vaddr);
 	else
-		off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN) - vaddr;
+		off = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN), vaddr);
 
 	if (off > len) {
 		ret = 0;
@@ -425,8 +425,8 @@ rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
 
 		/* populate with the largest group of contiguous pages */
 		for (phys_len = RTE_MIN(
-			(size_t)(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz) -
-				(addr + off)),
+			(size_t)RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz),
+				addr + off),
 			len - off);
 		     off + phys_len < len;
 		     phys_len = RTE_MIN(phys_len + pg_sz, len - off)) {
lib/mempool/rte_mempool.h +3 -0
index aedc100964..091976574a 100644
@@ -376,6 +376,7 @@ struct __rte_cache_aligned rte_mempool {
 static inline struct rte_mempool_objhdr *
 rte_mempool_get_header(void *obj)
 {
+	RTE_ASSERT(obj);
 	return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
 		sizeof(struct rte_mempool_objhdr));
 }
@@ -399,6 +400,7 @@ static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
 static inline struct rte_mempool_objtlr *rte_mempool_get_trailer(void *obj)
 {
 	struct rte_mempool *mp = rte_mempool_from_obj(obj);
+	RTE_ASSERT(mp);
 	return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
 }
 
@@ -1844,6 +1846,7 @@ rte_mempool_empty(const struct rte_mempool *mp)
 static inline rte_iova_t
 rte_mempool_virt2iova(const void *elt)
 {
+	RTE_ASSERT(elt);
 	const struct rte_mempool_objhdr *hdr;
 	hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
 		sizeof(*hdr));
lib/mempool/rte_mempool_ops_default.c +1 -1
index d27d6fc473..e28a288b91 100644
@@ -117,7 +117,7 @@ rte_mempool_op_populate_helper(struct rte_mempool *mp, unsigned int flags,
 	for (i = 0; i < max_objs; i++) {
 		/* avoid objects to cross page boundaries */
 		if (check_obj_bounds(va + off, pg_sz, total_elt_sz) < 0) {
-			off += RTE_PTR_ALIGN_CEIL(va + off, pg_sz) - (va + off);
+			off += RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(va + off, pg_sz), va + off);
 			if (flags & RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ)
 				off += total_elt_sz -
 					(((uintptr_t)(va + off - 1) %
lib/pdcp/pdcp_entity.h +5 -3
index f854192e98..7a2c41dd56 100644
@@ -198,17 +198,19 @@ struct entity_priv_ul_part {
 
 static inline struct entity_priv *
 entity_priv_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity), sizeof(struct rte_pdcp_entity));
 }
 
 static inline struct entity_priv_dl_part *
 entity_dl_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity),
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline struct entity_priv_ul_part *
 entity_ul_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity),
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline int
lib/vhost/vhost_user.c +10 -3
index 4bfb13fb98..a9ab6487f2 100644
@@ -824,9 +824,16 @@ void
 mem_set_dump(struct virtio_net *dev, void *ptr, size_t size, bool enable, uint64_t pagesz)
 {
 #ifdef MADV_DONTDUMP
-	void *start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz);
-	uintptr_t end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz);
-	size_t len = end - (uintptr_t)start;
+	void *start;
+	uintptr_t end;
+	size_t len;
+
+	if (ptr == NULL)
+		return;
+
+	start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz);
+	end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz);
+	len = end - (uintptr_t)start;
 
 	if (madvise(start, len, enable ? MADV_DODUMP : MADV_DONTDUMP) == -1) {
 		VHOST_CONFIG_LOG(dev->ifname, INFO,
Signature · 1 line
2.39.5 (Apple Git-154)
#29 lore raw

v16 cover eal: RTE_PTR_ADD/SUB API improvements

In reply to [email protected]

From: Scott <[email protected]>

This series addresses two issues:

1. Removes the semantically incorrect __rte_alloc_size attribute from rte_lcore_var_alloc which was causing FORTIFY_SOURCE failures on Ubuntu 24.04 CI. The attribute was incorrect because the function returns a handle to per-lcore variables, not a direct pointer to 'size' bytes.

2. Improves RTE_PTR_ADD/SUB macros to preserve pointer type qualifiers (const/volatile) and use pointer arithmetic instead of integer casts to enable compiler optimizations and maintain pointer provenance.

The bugfix (patch 1) is placed first so it can be backported independently to stable branches.

---
v16:
- Fixed test_common.c: parenthesize PTR_DIFF in RTE_INT_PTR tests

v15:
- Fixed __rte_alloc_size, spilt into 2 patch series
- Replaced RTE_INT_PTR_ADD/SUB with simpler RTE_INT_PTR(val) macro users do int arithmetic directly: RTE_INT_PTR(addr + offset)

v14:
- fixed cpp compiler error, avoiding array pointer decay which is implicitly done in cpp (but not c)
- fixed MALLOC_ELEM_TRAILER const preservation compile error

v13:
- Added release notes documenting API changes
- Fixed alignment in test file: use alignas(uint32_t) for buffer
- Fixed NULL pointer handling in cdx_vfio.c: check base_va before RTE_PTR_ADD
- Added GCC array-bounds diagnostic suppression in malloc_elem_from_data()
- Added bug tracker reference for volatile cast issue in idxd_pci.c
- Improved __rte_auto_type documentation: added C++11 and C23 support
- Moved doxygen rationale for void* return type to @return blocks
- Fixed MALLOC_ELEM_TRAILER to use RTE_PTR_UNQUAL for write operations

v12:
- void* return type to avoid optimizations assuming aligned access which isn't generally safe/true.

v11:
- Split API into PTR and INT_PTR variants, update all usage of PTR API for new APIs.

v10:
- Use unit_test_suite_runner for easier subtest failure identification

v9:
- Fix include order: system includes, then DPDK includes, then application includes
- Use NOHUGE_OK and ASAN_OK constants in REGISTER_FAST_TEST (instead of true, true)

v8:
- Remove tests for types < 32-bit (bool, char, short, uint8_t, uint16_t)
- Merge test_ptr_add_sub_typedefs() into test_ptr_add_sub_integer_types()
- Separate RTE_PTR_ADD and RTE_PTR_SUB documentation
- Move Clang/GCC implementation note from Doxygen to regular comment
- Tests verify both intermediate ADD result and SUB round-trip
- Use uintptr_t cast consistently for all integer-to-pointer conversions
- Make TEST_RETVAL calculated from TEST_INITVAL + TEST_INCREMENT

v7:
- Fix tests: use TEST_BUFFER_SIZE macro for buffer allocation
- Fix tests: ADD then SUB same amount to avoid out-of-bounds pointer arithmetic
- All RTE_PTR_SUB tests now verify round-trip (ADD+SUB returns to original)

v6:
- Make char* optimization Clang-only to avoid GCC false positive warnings
- Improve tests: use named constants instead of magic numbers
- Improve tests: use void* casts on expected values instead of uintptr_t on results

v5:
- Initial implementation with char* arithmetic for all compilers

v4:
- Used _Generic for type-based dispatch with char* casts
- Had warnings on both Clang and GCC due to _Generic type-checking all branches

Scott Mitchell (2):
eal: remove alloc_size from rte_lcore_var_alloc
eal: RTE_PTR_ADD/SUB API improvements

 app/test-pmd/cmdline_flow.c                 |   4 +-
 app/test/meson.build                        |   1 +
 app/test/test_common.c                      |  20 +-
 app/test/test_ptr_add_sub.c                 | 199 +++++++++++++++++
 doc/guides/rel_notes/release_26_03.rst      |  11 +
 drivers/bus/cdx/cdx_vfio.c                  |  13 +-
 drivers/bus/pci/linux/pci.c                 |   6 +-
 drivers/bus/vmbus/linux/vmbus_uio.c         |   6 +-
 drivers/common/cnxk/roc_cpt_debug.c         |   4 +-
 drivers/common/cnxk/roc_ml.c                |   4 +-
 drivers/common/cnxk/roc_nix_bpf.c           |   2 +-
 drivers/common/cnxk/roc_nix_inl.h           |   4 +-
 drivers/common/cnxk/roc_nix_inl_dp.h        |   8 +-
 drivers/common/cnxk/roc_platform.h          |   2 +
 drivers/common/mlx5/mlx5_common_mr.c        |   2 +-
 drivers/dma/idxd/idxd_pci.c                 |  11 +-
 drivers/dma/odm/odm_dmadev.c                |   4 +-
 drivers/event/cnxk/cn10k_worker.c           |  32 +--
 drivers/event/cnxk/cn20k_worker.c           |  32 +--
 drivers/mempool/bucket/rte_mempool_bucket.c |   7 +-
 drivers/mempool/dpaa/dpaa_mempool.c         |   2 +-
 drivers/net/cxgbe/sge.c                     |   4 +-
 drivers/net/ena/ena_ethdev.c                |   9 +-
 drivers/net/intel/fm10k/fm10k.h             |   6 +-
 drivers/net/intel/fm10k/fm10k_rxtx_vec.c    |  12 +-
 drivers/net/mlx4/mlx4_txq.c                 |   3 +-
 lib/eal/common/eal_common_fbarray.c         |   2 +
 lib/eal/common/eal_common_memory.c          |  31 ++-
 lib/eal/common/eal_common_options.c         |   2 +-
 lib/eal/common/malloc_elem.h                |  34 ++-
 lib/eal/freebsd/eal_memory.c                |   4 +
 lib/eal/include/rte_common.h                | 231 ++++++++++++++++++--
 lib/eal/include/rte_lcore_var.h             |   2 +-
 lib/eal/linux/eal_memalloc.c                |   6 +
 lib/eal/linux/eal_memory.c                  |   7 +
 lib/eal/windows/eal_memalloc.c              |   6 +
 lib/graph/rte_graph.h                       |   4 +-
 lib/latencystats/rte_latencystats.c         |   3 +
 lib/mbuf/rte_mbuf.c                         |   1 +
 lib/mbuf/rte_mbuf.h                         |   2 +
 lib/member/rte_xxh64_avx512.h               |   6 +-
 lib/mempool/rte_mempool.c                   |   8 +-
 lib/mempool/rte_mempool.h                   |   3 +
 lib/mempool/rte_mempool_ops_default.c       |   2 +-
 lib/pdcp/pdcp_entity.h                      |   8 +-
 lib/vhost/vhost_user.c                      |  13 +-
 46 files changed, 648 insertions(+), 135 deletions(-)
 create mode 100644 app/test/test_ptr_add_sub.c
Signature · 1 line
2.39.5 (Apple Git-154)
#30 lore raw

PATCH v16 1/2 eal: remove alloc_size from rte_lcore_var_alloc

From: Scott Mitchell <[email protected]>

The __rte_alloc_size(1) attribute on rte_lcore_var_alloc() is semantically incorrect and causes false positives with FORTIFY_SOURCE runtime checks.

The attribute tells the compiler that the function returns a pointer to 'size' bytes of usable memory. However, rte_lcore_var_alloc() actually returns a handle to a per-lcore variable scheme. The allocator internally allocates 'size' bytes per lcore (size * RTE_MAX_LCORE total), partitioned into per-lcore sections. The handle points to lcore 0's copy, and accessed via RTE_LCORE_VAR_LCORE(lcore_id, handle) which computes:
handle + (lcore_id * RTE_MAX_LCORE_VAR). Access is expected beyond 'size' bytes beyond the returned pointer when 'lcore_id != 0' but FORTIFY_SOURCE may terminate the program due to out of bounds access.

This can be observed on CI with gcc 13.3.0 in lcore_var_autotest with '*** buffer overflow detected ***: terminated' if pointer provenance is preserved (e.g. if offsets avoid casting to uintptr_t).

Fixes 5bce9bed67ad ("eal: add static per-lcore memory allocation facility")
---
 lib/eal/include/rte_lcore_var.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/eal/include/rte_lcore_var.h +1 -1
index ca31dff6fd..48b022e84e 100644
@@ -202,7 +202,7 @@ rte_lcore_var_lcore(unsigned int lcore_id, void *handle)
 __rte_experimental
 void *
 rte_lcore_var_alloc(size_t size, size_t align)
-	__rte_alloc_size(1) __rte_alloc_align(2);
+	__rte_alloc_align(2);
 
 #ifdef __cplusplus
 }
Signature · 1 line
2.39.5 (Apple Git-154)
Acked-by on v16 1/2
#31 lore raw

Re: [PATCH v16 1/2] eal: remove alloc_size from rte_lcore_var_alloc

Quoted 22 lines · On 1/27/26 06:28, [email protected] wrote:

From: Scott Mitchell <[email protected]>

The __rte_alloc_size(1) attribute on rte_lcore_var_alloc() is semantically incorrect and causes false positives with FORTIFY_SOURCE runtime checks.

The attribute tells the compiler that the function returns a pointer to 'size' bytes of usable memory. However, rte_lcore_var_alloc() actually returns a handle to a per-lcore variable scheme. The allocator internally allocates 'size' bytes per lcore (size * RTE_MAX_LCORE total), partitioned into per-lcore sections. The handle points to lcore 0's copy, and accessed via RTE_LCORE_VAR_LCORE(lcore_id, handle) which computes:
handle + (lcore_id * RTE_MAX_LCORE_VAR). Access is expected beyond 'size' bytes beyond the returned pointer when 'lcore_id != 0' but FORTIFY_SOURCE may terminate the program due to out of bounds access.

This can be observed on CI with gcc 13.3.0 in lcore_var_autotest with '*** buffer overflow detected ***: terminated' if pointer provenance is preserved (e.g. if offsets avoid casting to uintptr_t).

Quoted 6 lines
Fixes 5bce9bed67ad ("eal: add static per-lcore memory allocation facility")

---

Thanks!

Quoted 16 lines
  lib/eal/include/rte_lcore_var.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
lib/eal/include/rte_lcore_var.h +1 -1
index ca31dff6fd..48b022e84e 100644
@@ -202,7 +202,7 @@ rte_lcore_var_lcore(unsigned int lcore_id, void *handle)
  __rte_experimental
  void *
  rte_lcore_var_alloc(size_t size, size_t align)
-	__rte_alloc_size(1) __rte_alloc_align(2);
+	__rte_alloc_align(2);
 
  #ifdef __cplusplus
  }
Acked-by on v16 1/2
#32 lore raw

Re: [PATCH v16 1/2] eal: remove alloc_size from rte_lcore_var_alloc

In reply to [email protected]
Quoted 22 lines · On Mon, 26 Jan 2026 21:28:59 -0800 [email protected] wrote:

From: Scott Mitchell <[email protected]>

The __rte_alloc_size(1) attribute on rte_lcore_var_alloc() is semantically incorrect and causes false positives with FORTIFY_SOURCE runtime checks.

The attribute tells the compiler that the function returns a pointer to 'size' bytes of usable memory. However, rte_lcore_var_alloc() actually returns a handle to a per-lcore variable scheme. The allocator internally allocates 'size' bytes per lcore (size * RTE_MAX_LCORE total), partitioned into per-lcore sections. The handle points to lcore 0's copy, and accessed via RTE_LCORE_VAR_LCORE(lcore_id, handle) which computes:
handle + (lcore_id * RTE_MAX_LCORE_VAR). Access is expected beyond 'size' bytes beyond the returned pointer when 'lcore_id != 0' but FORTIFY_SOURCE may terminate the program due to out of bounds access.

This can be observed on CI with gcc 13.3.0 in lcore_var_autotest with '*** buffer overflow detected ***: terminated' if pointer provenance is preserved (e.g. if offsets avoid casting to uintptr_t).

Quoted 5 lines
Fixes 5bce9bed67ad ("eal: add static per-lcore memory allocation facility")
#33 lore raw

PATCH v16 2/2 eal: RTE_PTR_ADD/SUB API improvements

In reply to [email protected]

From: Scott Mitchell <[email protected]>

RTE_PTR_ADD and RTE_PTR_SUB APIs have a few limitations:
1. ptr cast to uintptr_t drops pointer provenance and prevents compiler optimizations
2. return cast discards qualifiers (const, volatile) which may hide correctness/concurrency issues.
3. Accepts both "pointers" and "integers as pointers" which overloads the use case and constrains the implementation to address other challenges.

This patch splits the API on two dimensions:
1. pointer types
2. integer types that represent pointers

This split allows addressing each of the challenges above and provides distinct APIs for the distinct use cases. Examples:
1. Clang is able to optimize and improve __rte_raw_cksum (which uses RTE_PTR_ADD) by ~40% (100 bytes) to ~8x (1.5k bytes) TSC cycles/byte.
2. Refactoring discovered cases that dropped qualifiers (volatile) that the new API exposes.

---
 app/test-pmd/cmdline_flow.c                 |   4 +-
 app/test/meson.build                        |   1 +
 app/test/test_common.c                      |  20 +-
 app/test/test_ptr_add_sub.c                 | 199 +++++++++++++++++
 doc/guides/rel_notes/release_26_03.rst      |  11 +
 drivers/bus/cdx/cdx_vfio.c                  |  13 +-
 drivers/bus/pci/linux/pci.c                 |   6 +-
 drivers/bus/vmbus/linux/vmbus_uio.c         |   6 +-
 drivers/common/cnxk/roc_cpt_debug.c         |   4 +-
 drivers/common/cnxk/roc_ml.c                |   4 +-
 drivers/common/cnxk/roc_nix_bpf.c           |   2 +-
 drivers/common/cnxk/roc_nix_inl.h           |   4 +-
 drivers/common/cnxk/roc_nix_inl_dp.h        |   8 +-
 drivers/common/cnxk/roc_platform.h          |   2 +
 drivers/common/mlx5/mlx5_common_mr.c        |   2 +-
 drivers/dma/idxd/idxd_pci.c                 |  11 +-
 drivers/dma/odm/odm_dmadev.c                |   4 +-
 drivers/event/cnxk/cn10k_worker.c           |  32 +--
 drivers/event/cnxk/cn20k_worker.c           |  32 +--
 drivers/mempool/bucket/rte_mempool_bucket.c |   7 +-
 drivers/mempool/dpaa/dpaa_mempool.c         |   2 +-
 drivers/net/cxgbe/sge.c                     |   4 +-
 drivers/net/ena/ena_ethdev.c                |   9 +-
 drivers/net/intel/fm10k/fm10k.h             |   6 +-
 drivers/net/intel/fm10k/fm10k_rxtx_vec.c    |  12 +-
 drivers/net/mlx4/mlx4_txq.c                 |   3 +-
 lib/eal/common/eal_common_fbarray.c         |   2 +
 lib/eal/common/eal_common_memory.c          |  31 ++-
 lib/eal/common/eal_common_options.c         |   2 +-
 lib/eal/common/malloc_elem.h                |  34 ++-
 lib/eal/freebsd/eal_memory.c                |   4 +
 lib/eal/include/rte_common.h                | 231 ++++++++++++++++++--
 lib/eal/linux/eal_memalloc.c                |   6 +
 lib/eal/linux/eal_memory.c                  |   7 +
 lib/eal/windows/eal_memalloc.c              |   6 +
 lib/graph/rte_graph.h                       |   4 +-
 lib/latencystats/rte_latencystats.c         |   3 +
 lib/mbuf/rte_mbuf.c                         |   1 +
 lib/mbuf/rte_mbuf.h                         |   2 +
 lib/member/rte_xxh64_avx512.h               |   6 +-
 lib/mempool/rte_mempool.c                   |   8 +-
 lib/mempool/rte_mempool.h                   |   3 +
 lib/mempool/rte_mempool_ops_default.c       |   2 +-
 lib/pdcp/pdcp_entity.h                      |   8 +-
 lib/vhost/vhost_user.c                      |  13 +-
 45 files changed, 647 insertions(+), 134 deletions(-)
 create mode 100644 app/test/test_ptr_add_sub.c
app/test-pmd/cmdline_flow.c +2 -2
index ebc036b14b..8c2fc6c7b6 100644
@@ -12468,7 +12468,7 @@ parse_meter_color(struct context *ctx, const struct token *token,
 
 		if (!arg)
 			return -1;
-		*(int *)RTE_PTR_ADD(action->conf, arg->offset) = i;
+		*(int *)RTE_PTR_ADD(RTE_PTR_UNQUAL(action->conf), arg->offset) = i;
 	} else {
 		((struct rte_flow_item_meter_color *)
 			ctx->object)->color = (enum rte_color)i;
@@ -13351,7 +13351,7 @@ indirect_action_flow_conf_create(const struct buffer *in)
 		indlst_conf = NULL;
 		goto end;
 	}
-	indlst_conf->conf = RTE_PTR_ADD(indlst_conf, base + len);
+	indlst_conf->conf = (const void **)RTE_PTR_ADD(indlst_conf, base + len);
 	for (i = 0; i < indlst_conf->conf_num; i++)
 		indlst_conf->conf[i] = indlst_conf->actions[i].conf;
 	SLIST_INSERT_HEAD(&indlst_conf_head, indlst_conf, next);
app/test/meson.build +1 -0
index f4d04a6e42..aa56fc4297 100644
@@ -152,6 +152,7 @@ source_file_deps = {
     'test_power_intel_uncore.c': ['power', 'power_intel_uncore'],
     'test_power_kvm_vm.c': ['power', 'power_kvm_vm'],
     'test_prefetch.c': [],
+    'test_ptr_add_sub.c': [],
     'test_ptr_compress.c': ['ptr_compress'],
     'test_rand_perf.c': [],
     'test_rawdev.c': ['rawdev', 'bus_vdev', 'raw_skeleton'],
app/test/test_common.c +10 -10
index 3e1c7df0c1..a2507d0f51 100644
@@ -37,10 +37,10 @@ test_macros(int __rte_unused unused_parm)
 	RTE_SWAP(smaller, bigger);
 	RTE_TEST_ASSERT(smaller == BIGGER && bigger == SMALLER,
 		"RTE_SWAP");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF), BIGGER,
-		"RTE_PTR_ADD");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF), SMALLER,
-		"RTE_PTR_SUB");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR(SMALLER + (PTR_DIFF)), (void *)BIGGER,
+		"RTE_INT_PTR ADD");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR(BIGGER - (PTR_DIFF)), (void *)SMALLER,
+		"RTE_INT_PTR SUB");
 	RTE_TEST_ASSERT_EQUAL(RTE_PTR_DIFF(BIGGER, SMALLER), PTR_DIFF,
 		"RTE_PTR_DIFF");
 	RTE_TEST_ASSERT_EQUAL(RTE_MAX(SMALLER, BIGGER), BIGGER,
@@ -188,18 +188,18 @@ test_align(void)
 			if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
-			val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN_FLOOR((uintptr_t) i, p);
 			if (ERROR_FLOOR(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_FLOOR", i, p);
 
 			val = RTE_ALIGN_FLOOR(i, p);
 			if (ERROR_FLOOR(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
 			/* align ceiling */
-			val = RTE_PTR_ALIGN((uintptr_t) i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN((uintptr_t) i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN", i, p);
 
 			val = RTE_ALIGN(i, p);
 			if (ERROR_CEIL(val, i, p))
@@ -209,9 +209,9 @@ test_align(void)
 			if (ERROR_CEIL(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
 
-			val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN_CEIL((uintptr_t)i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_CEIL", i, p);
 
 			/* by this point we know that val is aligned to p */
 			if (!rte_is_aligned((void*)(uintptr_t) val, p))
app/test/test_ptr_add_sub.c new file +199 -0

199 lines not on this page · raw message

doc/guides/rel_notes/release_26_03.rst +11 -0
index 15dabee7a1..cabec95b37 100644
@@ -84,6 +84,17 @@ API Changes
    Also, make sure to start the actual text at the margin.
    =======================================================
 
+* eal: Improved pointer arithmetic macros to preserve pointer provenance and type qualifiers.
+
+  * ``RTE_PTR_ADD`` and ``RTE_PTR_SUB`` now preserve const/volatile qualifiers
+    and use pointer arithmetic instead of integer casts to enable compiler optimizations.
+  * Passing NULL to ``RTE_PTR_ADD`` or ``RTE_PTR_SUB`` is now undefined behavior.
+  * Added ``RTE_INT_PTR`` for converting integer addresses to pointers.
+  * Added ``RTE_INT_PTR_ALIGN``, ``RTE_INT_PTR_ALIGN_FLOOR``, and ``RTE_INT_PTR_ALIGN_CEIL``
+    for aligning integer addresses.
+  * Existing code using ``RTE_PTR_ADD``/``RTE_PTR_SUB`` with integer types should migrate
+    to ``RTE_INT_PTR_*`` variants for clarity and correctness.
+
 
 ABI Changes
 -----------
drivers/bus/cdx/cdx_vfio.c +10 -3
index 11fe3265d2..a1009bc0ca 100644
@@ -367,9 +367,16 @@ cdx_vfio_get_region_info(int vfio_dev_fd, struct vfio_region_info **info,
 static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
-	size_t sz = msl->len;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
-	void **max_va = arg;
+	size_t sz;
+	void *end_va;
+	void **max_va;
+
+	if (msl->base_va == NULL)
+		return 0;
+
+	sz = msl->len;
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
+	max_va = arg;
 
 	if (*max_va < end_va)
 		*max_va = end_va;
drivers/bus/pci/linux/pci.c +5 -1
index 2ffac82e94..455db2cdec 100644
@@ -109,9 +109,13 @@ static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
 	size_t sz = msl->len;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
+	void *end_va;
 	void **max_va = arg;
 
+	if (msl->base_va == NULL)
+		return 0;
+
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
 	if (*max_va < end_va)
 		*max_va = end_va;
 	return 0;
drivers/bus/vmbus/linux/vmbus_uio.c +5 -1
index fbafc5027d..0ef05c0096 100644
@@ -122,9 +122,13 @@ static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
 	size_t sz = msl->memseg_arr.len * msl->page_sz;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
+	void *end_va;
 	void **max_va = arg;
 
+	if (msl->base_va == NULL)
+		return 0;
+
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
 	if (*max_va < end_va)
 		*max_va = end_va;
 	return 0;
drivers/common/cnxk/roc_cpt_debug.c +2 -2
index 28aedf088e..252658a83f 100644
@@ -56,7 +56,7 @@ cpt_cnxk_parse_hdr_dump(FILE *file, const struct cpt_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*32B */
 	offset = cpth->w2.ptr_offset;
 	offset = (((offset - 1) & 0x7) + 1) * 32;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD(PLT_PTR_UNQUAL(cpth), offset);
 
 	if (cpth->w0.num_frags > 0) {
 		cpt_dump(file, "CPT Fraginfo_0 \t%p:", frag_info);
@@ -162,7 +162,7 @@ cpt_cn10k_parse_hdr_dump(FILE *file, const struct cpt_cn10k_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*8B */
 	offset = cpth->w2.fi_offset;
 	offset = (((offset - 1) & 0x1f) + 1) * 8;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD(PLT_PTR_UNQUAL(cpth), offset);
 
 	cpt_dump(file, "CPT Fraginfo \t0x%p:", frag_info);
 
drivers/common/cnxk/roc_ml.c +3 -1
index 7390697b1d..aa24dbb82f 100644
@@ -589,7 +589,9 @@ roc_ml_blk_init(struct roc_bphy *roc_bphy, struct roc_ml *roc_ml)
 
 	plt_ml_dbg(
 		"MLAB: Physical Address : 0x%016lx",
-		PLT_PTR_ADD_U64_CAST(ml->pci_dev->mem_resource[0].phys_addr, ML_MLAB_BLK_OFFSET));
+		   PLT_PTR_ADD_U64_CAST(
+				   PLT_INT_PTR(ml->pci_dev->mem_resource[0].phys_addr),
+				   ML_MLAB_BLK_OFFSET));
 	plt_ml_dbg("MLAB: Virtual Address : 0x%016lx",
 		   PLT_PTR_ADD_U64_CAST(ml->pci_dev->mem_resource[0].addr, ML_MLAB_BLK_OFFSET));
 
drivers/common/cnxk/roc_nix_bpf.c +1 -1
index 98c9855a5b..c42fb7e004 100644
@@ -160,7 +160,7 @@ nix_precolor_conv_table_write(struct roc_nix *roc_nix, uint64_t val,
 	struct nix *nix = roc_nix_to_nix_priv(roc_nix);
 	int64_t *addr;
 
-	addr = PLT_PTR_ADD(nix->base, off);
+	addr = PLT_INT_PTR(nix->base + off);
 	plt_write64(val, addr);
 }
 
drivers/common/cnxk/roc_nix_inl.h +2 -2
index 7970ac2258..3526ec9268 100644
@@ -59,7 +59,7 @@ roc_nix_inl_on_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline struct roc_ie_on_outb_sa *
@@ -67,7 +67,7 @@ roc_nix_inl_on_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline void *
drivers/common/cnxk/roc_nix_inl_dp.h +4 -4
index eb101db179..ae161e02ed 100644
@@ -49,7 +49,7 @@ roc_nix_inl_ot_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline struct roc_ot_ipsec_outb_sa *
@@ -57,7 +57,7 @@ roc_nix_inl_ot_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline void *
@@ -77,7 +77,7 @@ roc_nix_inl_ow_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline struct roc_ow_ipsec_outb_sa *
@@ -85,7 +85,7 @@ roc_nix_inl_ow_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline void *
drivers/common/cnxk/roc_platform.h +2 -0
index e22a50d47a..4a5baa9a78 100644
@@ -47,6 +47,8 @@
 #define PLT_PTR_ADD		 RTE_PTR_ADD
 #define PLT_PTR_SUB		 RTE_PTR_SUB
 #define PLT_PTR_DIFF		 RTE_PTR_DIFF
+#define PLT_PTR_UNQUAL		 RTE_PTR_UNQUAL
+#define PLT_INT_PTR		 RTE_INT_PTR
 #define PLT_MAX_RXTX_INTR_VEC_ID RTE_MAX_RXTX_INTR_VEC_ID
 #define PLT_INTR_VEC_RXTX_OFFSET RTE_INTR_VEC_RXTX_OFFSET
 #define PLT_MIN			 RTE_MIN
drivers/common/mlx5/mlx5_common_mr.c +1 -1
index 8ed988dec9..aae39fdb6e 100644
@@ -1447,7 +1447,7 @@ mlx5_mempool_get_extmem_cb(struct rte_mempool *mp, void *opaque,
 	seg = &heap[data->heap_size - 1];
 	msl = rte_mem_virt2memseg_list((void *)addr);
 	page_size = msl != NULL ? msl->page_sz : rte_mem_page_size();
-	page_start = RTE_PTR_ALIGN_FLOOR(addr, page_size);
+	page_start = RTE_ALIGN_FLOOR(addr, page_size);
 	seg->start = page_start;
 	seg->end = page_start + page_size;
 	/* Maintain the heap order. */
drivers/dma/idxd/idxd_pci.c +7 -4
index 214f6f22d5..fb76a050b1 100644
@@ -59,7 +59,7 @@ idxd_pci_dev_command(struct idxd_dmadev *idxd, enum rte_idxd_cmds command)
 	return err_code;
 }
 
-static uint32_t *
+static volatile uint32_t *
 idxd_get_wq_cfg(struct idxd_pci_common *pci, uint8_t wq_idx)
 {
 	return RTE_PTR_ADD(pci->wq_regs_base,
@@ -205,9 +205,9 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,
 	pci->regs = dev->mem_resource[0].addr;
 	version = pci->regs->version;
 	grp_offset = (uint16_t)pci->regs->offsets[0];
-	pci->grp_regs = RTE_PTR_ADD(pci->regs, grp_offset * 0x100);
+	pci->grp_regs = RTE_PTR_ADD((volatile void *)pci->regs, grp_offset * 0x100);
 	wq_offset = (uint16_t)(pci->regs->offsets[0] >> 16);
-	pci->wq_regs_base = RTE_PTR_ADD(pci->regs, wq_offset * 0x100);
+	pci->wq_regs_base = RTE_PTR_ADD((volatile void *)pci->regs, wq_offset * 0x100);
 	pci->portals = dev->mem_resource[2].addr;
 	pci->wq_cfg_sz = (pci->regs->wqcap >> 24) & 0x0F;
 
@@ -395,7 +395,10 @@ idxd_dmadev_probe_pci(struct rte_pci_driver *drv, struct rte_pci_device *dev)
 		/* add the queue number to each device name */
 		snprintf(qname, sizeof(qname), "%s-q%d", name, qid);
 		idxd.qid = qid;
-		idxd.portal = RTE_PTR_ADD(idxd.u.pci->portals,
+		/* FIXME: cast drops volatile propagation to idxd_dmadev.portal
+		 * See: https://bugs.dpdk.org/show_bug.cgi?id=1871
+		 */
+		idxd.portal = RTE_PTR_ADD(RTE_PTR_UNQUAL(idxd.u.pci->portals),
 				qid * IDXD_PORTAL_SIZE);
 		if (idxd_is_wq_enabled(&idxd))
 			IDXD_PMD_ERR("Error, WQ %u seems enabled", qid);
drivers/dma/odm/odm_dmadev.c +2 -2
index a2f4ed9a8e..3b4b058427 100644
@@ -422,7 +422,7 @@ odm_dmadev_completed(void *dev_private, uint16_t vchan, const uint16_t nb_cpls,
 	int cnt;
 
 	vq = &odm->vq[vchan];
-	const uint32_t *base_addr = vq->cring_mz->addr;
+	uint32_t *base_addr = vq->cring_mz->addr;
 	const uint16_t cring_max_entry = vq->cring_max_entry;
 
 	cring_head = vq->cring_head;
@@ -482,7 +482,7 @@ odm_dmadev_completed_status(void *dev_private, uint16_t vchan, const uint16_t nb
 	int cnt;
 
 	vq = &odm->vq[vchan];
-	const uint32_t *base_addr = vq->cring_mz->addr;
+	uint32_t *base_addr = vq->cring_mz->addr;
 	const uint16_t cring_max_entry = vq->cring_max_entry;
 
 	cring_head = vq->cring_head;
drivers/event/cnxk/cn10k_worker.c +16 -16
index 80077ec8a1..cd35cc3e55 100644
@@ -261,14 +261,14 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 					aw7);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 64), aw4);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 80), aw5);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 96), aw6);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 112), aw7);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 128);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 32), aw2);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 48), aw3);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 64), aw4);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 80), aw5);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 96), aw6);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 112), aw7);
+			lmt_addr += 128;
 		} break;
 		case 4: {
 			uint64x2_t aw0, aw1, aw2, aw3;
@@ -291,10 +291,10 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 					aw3);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 64);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 32), aw2);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 48), aw3);
+			lmt_addr += 64;
 		} break;
 		case 2: {
 			uint64x2_t aw0, aw1;
@@ -310,8 +310,8 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 					aw1);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 32);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			lmt_addr += 32;
 		} break;
 		case 1: {
 			__uint128_t aw0;
@@ -322,7 +322,7 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 			aw0 |= (uint64_t)ev[0].sched_type << 32;
 
 			*((__uint128_t *)lmt_addr) = aw0;
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+			lmt_addr += 16;
 		} break;
 		}
 		ev += parts;
@@ -338,7 +338,7 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 		aw0 |= ev[0].event & (BIT_ULL(32) - 1);
 		aw0 |= (uint64_t)ev[0].sched_type << 32;
 		*((__uint128_t *)lmt_addr) = aw0;
-		lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+		lmt_addr += 16;
 	}
 #endif
 
drivers/event/cnxk/cn20k_worker.c +16 -16
index 53daf3b4b0..2113a2ea78 100644
@@ -231,14 +231,14 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw7 = vorrq_u64(vandq_u64(vshrq_n_u64(aw7, 6), tt_mask), aw7);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 64), aw4);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 80), aw5);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 96), aw6);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 112), aw7);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 128);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 32), aw2);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 48), aw3);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 64), aw4);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 80), aw5);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 96), aw6);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 112), aw7);
+			lmt_addr += 128;
 		} break;
 		case 4: {
 			uint64x2_t aw0, aw1, aw2, aw3;
@@ -253,10 +253,10 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw3 = vorrq_u64(vandq_u64(vshrq_n_u64(aw3, 6), tt_mask), aw3);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 64);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 32), aw2);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 48), aw3);
+			lmt_addr += 64;
 		} break;
 		case 2: {
 			uint64x2_t aw0, aw1;
@@ -268,8 +268,8 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw1 = vorrq_u64(vandq_u64(vshrq_n_u64(aw1, 6), tt_mask), aw1);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 32);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			lmt_addr += 32;
 		} break;
 		case 1: {
 			__uint128_t aw0;
@@ -280,7 +280,7 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw0 |= (uint64_t)ev[0].sched_type << 32;
 
 			*((__uint128_t *)lmt_addr) = aw0;
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+			lmt_addr += 16;
 		} break;
 		}
 		ev += parts;
@@ -296,7 +296,7 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 		aw0 |= ev[0].event & (BIT_ULL(32) - 1);
 		aw0 |= (uint64_t)ev[0].sched_type << 32;
 		*((__uint128_t *)lmt_addr) = aw0;
-		lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+		lmt_addr += 16;
 	}
 #endif
 
drivers/mempool/bucket/rte_mempool_bucket.c +3 -4
index c0b480bfc7..6fee10176b 100644
@@ -376,8 +376,8 @@ count_underfilled_buckets(struct rte_mempool *mp,
 	uintptr_t align;
 	uint8_t *iter;
 
-	align = (uintptr_t)RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz) -
-		(uintptr_t)memhdr->addr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz),
+			     memhdr->addr);
 
 	for (iter = (uint8_t *)memhdr->addr + align;
 	     iter < (uint8_t *)memhdr->addr + memhdr->len;
@@ -602,8 +602,7 @@ bucket_populate(struct rte_mempool *mp, unsigned int max_objs,
 		return -EINVAL;
 
 	bucket_page_sz = rte_align32pow2(bd->bucket_mem_size);
-	align = RTE_PTR_ALIGN_CEIL((uintptr_t)vaddr, bucket_page_sz) -
-		(uintptr_t)vaddr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, bucket_page_sz), vaddr);
 
 	bucket_header_sz = bd->header_size - mp->header_size;
 	if (iova != RTE_BAD_IOVA)
drivers/mempool/dpaa/dpaa_mempool.c +1 -1
index 2f9395b3f4..85e1c01017 100644
@@ -321,7 +321,7 @@ dpaa_adjust_obj_bounds(char *va, size_t *offset,
 	size_t off = *offset;
 
 	if (dpaa_check_obj_bounds(va + off, pg_sz, total) == false) {
-		off += RTE_PTR_ALIGN_CEIL(va + off, pg_sz) - (va + off);
+		off += RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(va + off, pg_sz), va + off);
 		if (flags & RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ)
 			off += total - ((((size_t)va + off - 1) % total) + 1);
 	}
drivers/net/cxgbe/sge.c +2 -2
index e9d45f24c4..a5d112d52d 100644
@@ -591,7 +591,7 @@ static void write_sgl(struct rte_mbuf *mbuf, struct sge_txq *q,
 			memcpy(sgl->sge, buf, part0);
 		part1 = RTE_PTR_DIFF((u8 *)end, (u8 *)q->stat);
 		rte_memcpy(q->desc, RTE_PTR_ADD((u8 *)buf, part0), part1);
-		end = RTE_PTR_ADD((void *)q->desc, part1);
+		end = RTE_PTR_ADD(q->desc, part1);
 	}
 	if ((uintptr_t)end & 8)           /* 0-pad to multiple of 16 */
 		*(u64 *)end = 0;
@@ -1297,7 +1297,7 @@ static void inline_tx_mbuf(const struct sge_txq *q, caddr_t from, caddr_t *to,
 		from = RTE_PTR_ADD(from, left);
 		left = len - left;
 		rte_memcpy((void *)q->desc, from, left);
-		*to = RTE_PTR_ADD((void *)q->desc, left);
+		*to = RTE_PTR_ADD(q->desc, left);
 	}
 }
 
drivers/net/ena/ena_ethdev.c +8 -1
index ea4afbc75d..da23b271d5 100644
@@ -2379,7 +2379,14 @@ static void *pci_bar_addr(struct rte_pci_device *dev, uint32_t bar)
 {
 	const struct rte_mem_resource *res = &dev->mem_resource[bar];
 	size_t offset = res->phys_addr % rte_mem_page_size();
-	void *vaddr = RTE_PTR_ADD(res->addr, offset);
+	void *vaddr;
+
+	if (res->addr == NULL) {
+		PMD_INIT_LOG_LINE(ERR, "PCI BAR [%u] address is NULL", bar);
+		return NULL;
+	}
+
+	vaddr = RTE_PTR_ADD(res->addr, offset);
 
 	PMD_INIT_LOG_LINE(INFO, "PCI BAR [%u]: phys_addr=0x%" PRIx64 ", addr=%p, offset=0x%zx, adjusted_addr=%p",
 		bar, res->phys_addr, res->addr, offset, vaddr);
drivers/net/intel/fm10k/fm10k.h +3 -3
index 0eb32ac0d0..4e3081785f 100644
@@ -264,9 +264,9 @@ fm10k_pktmbuf_reset(struct rte_mbuf *mb, uint16_t in_port)
 	mb->nb_segs = 1;
 
 	/* enforce 512B alignment on default Rx virtual addresses */
-	mb->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb->buf_addr +
-			RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-			- (char *)mb->buf_addr);
+	mb->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb->buf_addr +
+			RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+			(char *)mb->buf_addr);
 	mb->port = in_port;
 }
 
drivers/net/intel/fm10k/fm10k_rxtx_vec.c +6 -6
index 0eada7275e..a08af75bc7 100644
@@ -315,12 +315,12 @@ fm10k_rxq_rearm(struct fm10k_rx_queue *rxq)
 		_mm_store_si128(RTE_CAST_PTR(__m128i *, &rxdp++->q), dma_addr1);
 
 		/* enforce 512B alignment on default Rx virtual addresses */
-		mb0->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb0->buf_addr
-				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-				- (char *)mb0->buf_addr);
-		mb1->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb1->buf_addr
-				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-				- (char *)mb1->buf_addr);
+		mb0->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb0->buf_addr
+				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+				(char *)mb0->buf_addr);
+		mb1->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb1->buf_addr
+				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+				(char *)mb1->buf_addr);
 	}
 
 	rxq->rxrearm_start += RTE_FM10K_RXQ_REARM_THRESH;
drivers/net/mlx4/mlx4_txq.c +2 -1
index 0db2e55bef..348040c1b9 100644
@@ -114,7 +114,8 @@ txq_uar_uninit_secondary(struct txq *txq)
 	void *addr;
 
 	addr = ppriv->uar_table[txq->stats.idx];
-	munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
+	if (addr)
+		munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
 }
 
 /**
lib/eal/common/eal_common_fbarray.c +2 -0
index 8bdcefb717..c4e03f45f7 100644
@@ -10,6 +10,7 @@
 #include <unistd.h>
 
 #include <rte_common.h>
+#include <rte_debug.h>
 #include <rte_eal_paging.h>
 #include <rte_errno.h>
 #include <rte_log.h>
@@ -1058,6 +1059,7 @@ rte_fbarray_get(const struct rte_fbarray *arr, unsigned int idx)
 		return NULL;
 	}
 
+	RTE_ASSERT(arr->data);
 	ret = RTE_PTR_ADD(arr->data, idx * arr->elt_sz);
 
 	return ret;
lib/eal/common/eal_common_memory.c +20 -11
index c62edf5e55..ee7054ec14 100644
@@ -309,6 +309,9 @@ virt2memseg(const void *addr, const struct rte_memseg_list *msl)
 
 	/* a memseg list was specified, check if it's the right one */
 	start = msl->base_va;
+	if (start == NULL)
+		return NULL;
+
 	end = RTE_PTR_ADD(start, msl->len);
 
 	if (addr < start || addr >= end)
@@ -332,6 +335,8 @@ virt2memseg_list(const void *addr)
 		msl = &mcfg->memsegs[msl_idx];
 
 		start = msl->base_va;
+		if (start == NULL)
+			continue;
 		end = RTE_PTR_ADD(start, msl->len);
 		if (addr >= start && addr < end)
 			break;
@@ -680,10 +685,16 @@ RTE_EXPORT_SYMBOL(rte_mem_lock_page)
 int
 rte_mem_lock_page(const void *virt)
 {
-	uintptr_t virtual = (uintptr_t)virt;
 	size_t page_size = rte_mem_page_size();
-	uintptr_t aligned = RTE_PTR_ALIGN_FLOOR(virtual, page_size);
-	return rte_mem_lock((void *)aligned, page_size);
+	const void *aligned;
+
+	if (virt == NULL) {
+		rte_errno = EINVAL;
+		return -1;
+	}
+
+	aligned = RTE_PTR_ALIGN_FLOOR(virt, page_size);
+	return rte_mem_lock(aligned, page_size);
 }
 
 RTE_EXPORT_SYMBOL(rte_memseg_contig_walk_thread_unsafe)
@@ -1447,7 +1458,7 @@ handle_eal_memseg_info_request(const char *cmd __rte_unused,
 
 	ms_iova = ms->iova;
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 	ms_size = ms->len;
 	hugepage_size = ms->hugepage_sz;
 	ms_socket_id = ms->socket_id;
@@ -1519,7 +1530,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 	rte_mcfg_mem_read_unlock();
 
 	rte_tel_data_start_dict(d);
@@ -1530,8 +1541,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = elem_start_addr + elem->size;
 
 		if ((uint64_t)elem_start_addr >= ms_start_addr &&
 		    (uint64_t)elem_end_addr <= ms_end_addr)
@@ -1553,7 +1563,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	struct rte_mem_config *mcfg;
 	struct rte_memseg_list *msl;
 	const struct rte_memseg *ms;
-	struct malloc_elem *elem;
+	struct malloc_elem *volatile elem;
 	struct malloc_heap *heap;
 	struct rte_tel_data *c;
 	uint64_t ms_start_addr, ms_end_addr;
@@ -1597,7 +1607,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 
 	rte_mcfg_mem_read_unlock();
 
@@ -1609,8 +1619,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = elem_start_addr + elem->size;
 
 		if (elem_start_addr < ms_start_addr ||
 				elem_end_addr > ms_end_addr) {
lib/eal/common/eal_common_options.c +1 -1
index 485655865d..0e05683a67 100644
@@ -1656,7 +1656,7 @@ eal_parse_base_virtaddr(const char *arg)
 	 * on x86 and other architectures.
 	 */
 	internal_conf->base_virtaddr =
-		RTE_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
+		(uintptr_t) RTE_INT_PTR_ALIGN_CEIL(addr, (size_t)RTE_PGSIZE_16M);
 
 	return 0;
 }
lib/eal/common/malloc_elem.h +27 -7
index c7ff6718f8..babaead7de 100644
@@ -79,9 +79,11 @@ static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
 #define MALLOC_TRAILER_COOKIE  0xadd2e55badbadbadULL /**< Trailer cookie.*/
 
 /* define macros to make referencing the header and trailer cookies easier */
-#define MALLOC_ELEM_TRAILER(elem) (*((uint64_t*)RTE_PTR_ADD(elem, \
-		elem->size - MALLOC_ELEM_TRAILER_LEN)))
-#define MALLOC_ELEM_HEADER(elem) (elem->header_cookie)
+#define MALLOC_ELEM_TRAILER(elem) \
+	/* typeof preserves qualifiers (const/volatile) of elem */ \
+	(*(typeof((elem)->header_cookie) *)RTE_PTR_ADD(elem, \
+		(elem)->size - MALLOC_ELEM_TRAILER_LEN))
+#define MALLOC_ELEM_HEADER(elem) ((elem)->header_cookie)
 
 static inline void
 set_header(struct malloc_elem *elem)
@@ -306,13 +308,31 @@ old_malloc_size(struct malloc_elem *elem)
 static inline struct malloc_elem *
 malloc_elem_from_data(const void *data)
 {
+	struct malloc_elem *result;
+
 	if (data == NULL)
 		return NULL;
 
-	struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
-	if (!malloc_elem_cookies_ok(elem))
-		return NULL;
-	return elem->state != ELEM_PAD ? elem:  RTE_PTR_SUB(elem, elem->pad);
+	/* The allocator returns a pointer in the middle of an allocation pool.
+	 * GCC's interprocedural analysis can't trace this and warns about
+	 * out-of-bounds access when we do backwards pointer arithmetic to
+	 * find the malloc_elem header.
+	 */
+	__rte_diagnostic_push
+	__rte_diagnostic_ignored_array_bounds
+	{
+		struct malloc_elem *elem =
+			RTE_PTR_SUB(RTE_PTR_UNQUAL(data), MALLOC_ELEM_HEADER_LEN);
+
+		if (!malloc_elem_cookies_ok(elem))
+			result = NULL;
+		else
+			result = elem->state != ELEM_PAD ? elem :
+				RTE_PTR_SUB(elem, elem->pad);
+	}
+	__rte_diagnostic_pop
+
+	return result;
 }
 
 /*
lib/eal/freebsd/eal_memory.c +4 -0
index 6d3d46a390..49a89fe2fb 100644
@@ -178,6 +178,10 @@ rte_eal_hugepage_init(void)
 					"RTE_MAX_MEMSEG_PER_TYPE and/or RTE_MAX_MEM_MB_PER_TYPE in configuration.");
 				return -1;
 			}
+			if (msl->base_va == NULL) {
+				EAL_LOG(ERR, "Base VA is NULL for memseg list %d", msl_idx);
+				return -1;
+			}
 			arr = &msl->memseg_arr;
 			seg = rte_fbarray_get(arr, ms_idx);
 
lib/eal/include/rte_common.h +216 -15

285 lines not on this page · raw message

lib/eal/linux/eal_memalloc.c +6 -0
index 1e60e21620..f770826a43 100644
@@ -835,6 +835,12 @@ alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
 		void *map_addr;
 
 		cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
+
+		if (cur_msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			goto out;
+		}
+
 		map_addr = RTE_PTR_ADD(cur_msl->base_va,
 				cur_idx * page_sz);
 
lib/eal/linux/eal_memory.c +7 -0
index 8e1763e890..3830bd5d7f 100644
@@ -759,6 +759,13 @@ remap_segment(struct hugepage_file *hugepages, int seg_start, int seg_end)
 			return -1;
 		}
 		memseg_len = (size_t)page_sz;
+
+		if (msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			close(fd);
+			return -1;
+		}
+
 		addr = RTE_PTR_ADD(msl->base_va, ms_idx * memseg_len);
 
 		/* we know this address is already mmapped by memseg list, so
lib/eal/windows/eal_memalloc.c +6 -0
index 5db5a474cc..6432caccd3 100644
@@ -230,6 +230,12 @@ alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
 		void *map_addr;
 
 		cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
+
+		if (cur_msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			goto out;
+		}
+
 		map_addr = RTE_PTR_ADD(cur_msl->base_va, cur_idx * page_sz);
 
 		if (alloc_seg(cur, map_addr, wa->socket, wa->hi)) {
lib/graph/rte_graph.h +2 -2
index 7e433f4661..d8ac0011e4 100644
@@ -407,9 +407,9 @@ void rte_graph_obj_dump(FILE *f, struct rte_graph *graph, bool all);
 /** Macro to browse rte_node object after the graph creation */
 #define rte_graph_foreach_node(count, off, graph, node)                        \
 	for (count = 0, off = graph->nodes_start,                              \
-	     node = RTE_PTR_ADD(graph, off);                                   \
+	     node = RTE_PTR_ADD(RTE_PTR_UNQUAL(graph), off);                           \
 	     count < graph->nb_nodes;                                          \
-	     off = node->next, node = RTE_PTR_ADD(graph, off), count++)
+	     off = node->next, node = RTE_PTR_ADD(RTE_PTR_UNQUAL(graph), off), count++)
 
 /**
  * Get node object with in graph from id.
lib/latencystats/rte_latencystats.c +3 -0
index f61d5a273f..f2cd0db4b7 100644
@@ -104,6 +104,9 @@ latencystats_collect(uint64_t values[])
 	unsigned int i, scale;
 	const uint64_t *stats;
 
+	if (glob_stats == NULL)
+		return;
+
 	for (i = 0; i < NUM_LATENCY_STATS; i++) {
 		stats = RTE_PTR_ADD(glob_stats, lat_stats_strings[i].offset);
 		scale = lat_stats_strings[i].scale;
lib/mbuf/rte_mbuf.c +1 -0
index 0d931c7a15..557954d45e 100644
@@ -193,6 +193,7 @@ __rte_pktmbuf_init_extmem(struct rte_mempool *mp,
 
 	RTE_ASSERT(ctx->ext < ctx->ext_num);
 	RTE_ASSERT(ctx->off + ext_mem->elt_size <= ext_mem->buf_len);
+	RTE_ASSERT(ext_mem->buf_ptr);
 
 	m->buf_addr = RTE_PTR_ADD(ext_mem->buf_ptr, ctx->off);
 	rte_mbuf_iova_set(m, ext_mem->buf_iova == RTE_BAD_IOVA ? RTE_BAD_IOVA :
lib/mbuf/rte_mbuf.h +2 -0
index 2004391f57..3100feb740 100644
@@ -217,6 +217,7 @@ rte_mbuf_data_iova_default(const struct rte_mbuf *mb)
 static inline struct rte_mbuf *
 rte_mbuf_from_indirect(struct rte_mbuf *mi)
 {
+	RTE_ASSERT(mi);
 	return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
 }
 
@@ -289,6 +290,7 @@ rte_mbuf_to_baddr(struct rte_mbuf *md)
 static inline void *
 rte_mbuf_to_priv(struct rte_mbuf *m)
 {
+	RTE_ASSERT(m);
 	return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
 }
 
lib/member/rte_xxh64_avx512.h +3 -3
index 58f896ebb8..774b26d8df 100644
@@ -58,7 +58,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 			 _mm512_set1_epi64(key_len));
 
 	while (remaining >= 8) {
-		input = _mm512_set1_epi64(*(uint64_t *)RTE_PTR_ADD(key, offset));
+		input = _mm512_set1_epi64(*(const uint64_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 				xxh64_round_avx512(_mm512_setzero_si512(), input));
 		v_hash = _mm512_madd52lo_epu64(_mm512_set1_epi64(PRIME64_4),
@@ -71,7 +71,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	if (remaining >= 4) {
 		input = _mm512_set1_epi64
-			(*(uint32_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint32_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_1)));
@@ -86,7 +86,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	while (remaining != 0) {
 		input = _mm512_set1_epi64
-			(*(uint8_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint8_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_5)));
lib/mempool/rte_mempool.c +4 -4
index 3042d94c14..f1ff668205 100644
@@ -349,9 +349,9 @@ rte_mempool_populate_iova(struct rte_mempool *mp, char *vaddr,
 	memhdr->opaque = opaque;
 
 	if (mp->flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
-		off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
+		off = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, 8), vaddr);
 	else
-		off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN) - vaddr;
+		off = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN), vaddr);
 
 	if (off > len) {
 		ret = 0;
@@ -425,8 +425,8 @@ rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
 
 		/* populate with the largest group of contiguous pages */
 		for (phys_len = RTE_MIN(
-			(size_t)(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz) -
-				(addr + off)),
+			(size_t)RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz),
+				addr + off),
 			len - off);
 		     off + phys_len < len;
 		     phys_len = RTE_MIN(phys_len + pg_sz, len - off)) {
lib/mempool/rte_mempool.h +3 -0
index aedc100964..091976574a 100644
@@ -376,6 +376,7 @@ struct __rte_cache_aligned rte_mempool {
 static inline struct rte_mempool_objhdr *
 rte_mempool_get_header(void *obj)
 {
+	RTE_ASSERT(obj);
 	return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
 		sizeof(struct rte_mempool_objhdr));
 }
@@ -399,6 +400,7 @@ static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
 static inline struct rte_mempool_objtlr *rte_mempool_get_trailer(void *obj)
 {
 	struct rte_mempool *mp = rte_mempool_from_obj(obj);
+	RTE_ASSERT(mp);
 	return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
 }
 
@@ -1844,6 +1846,7 @@ rte_mempool_empty(const struct rte_mempool *mp)
 static inline rte_iova_t
 rte_mempool_virt2iova(const void *elt)
 {
+	RTE_ASSERT(elt);
 	const struct rte_mempool_objhdr *hdr;
 	hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
 		sizeof(*hdr));
lib/mempool/rte_mempool_ops_default.c +1 -1
index d27d6fc473..e28a288b91 100644
@@ -117,7 +117,7 @@ rte_mempool_op_populate_helper(struct rte_mempool *mp, unsigned int flags,
 	for (i = 0; i < max_objs; i++) {
 		/* avoid objects to cross page boundaries */
 		if (check_obj_bounds(va + off, pg_sz, total_elt_sz) < 0) {
-			off += RTE_PTR_ALIGN_CEIL(va + off, pg_sz) - (va + off);
+			off += RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(va + off, pg_sz), va + off);
 			if (flags & RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ)
 				off += total_elt_sz -
 					(((uintptr_t)(va + off - 1) %
lib/pdcp/pdcp_entity.h +5 -3
index f854192e98..7a2c41dd56 100644
@@ -198,17 +198,19 @@ struct entity_priv_ul_part {
 
 static inline struct entity_priv *
 entity_priv_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity), sizeof(struct rte_pdcp_entity));
 }
 
 static inline struct entity_priv_dl_part *
 entity_dl_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity),
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline struct entity_priv_ul_part *
 entity_ul_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity),
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline int
lib/vhost/vhost_user.c +10 -3
index 4bfb13fb98..a9ab6487f2 100644
@@ -824,9 +824,16 @@ void
 mem_set_dump(struct virtio_net *dev, void *ptr, size_t size, bool enable, uint64_t pagesz)
 {
 #ifdef MADV_DONTDUMP
-	void *start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz);
-	uintptr_t end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz);
-	size_t len = end - (uintptr_t)start;
+	void *start;
+	uintptr_t end;
+	size_t len;
+
+	if (ptr == NULL)
+		return;
+
+	start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz);
+	end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz);
+	len = end - (uintptr_t)start;
 
 	if (madvise(start, len, enable ? MADV_DODUMP : MADV_DONTDUMP) == -1) {
 		VHOST_CONFIG_LOG(dev->ifname, INFO,
Signature · 1 line
2.39.5 (Apple Git-154)
#34 lore raw

v17 cover eal: RTE_PTR_ADD/SUB API improvements

In reply to [email protected]

From: Scott <[email protected]>

This series addresses two issues:

1. Removes the semantically incorrect __rte_alloc_size attribute from rte_lcore_var_alloc which was causing FORTIFY_SOURCE failures on Ubuntu 24.04 CI. The attribute was incorrect because the function returns a handle to per-lcore variables, not a direct pointer to 'size' bytes.

2. Improves RTE_PTR_ADD/SUB macros to preserve pointer type qualifiers (const/volatile) and use pointer arithmetic instead of integer casts to enable compiler optimizations and maintain pointer provenance.

The bugfix (patch 1) is placed first so it can be backported independently to stable branches.

---
v17:
- Improved release notes to explicitly list macro names for search/indexing
- eal_common_fbarray.c RTE_ASSERT to runtime NULL check

v16:
- Fixed test_common.c: parenthesize PTR_DIFF in RTE_INT_PTR tests

v15:
- Fixed __rte_alloc_size, spilt into 2 patch series
- Replaced RTE_INT_PTR_ADD/SUB with simpler RTE_INT_PTR(val) macro users do int arithmetic directly: RTE_INT_PTR(addr + offset)

v14:
- fixed cpp compiler error, avoiding array pointer decay which is implicitly done in cpp (but not c)
- fixed MALLOC_ELEM_TRAILER const preservation compile error

v13:
- Added release notes documenting API changes
- Fixed alignment in test file: use alignas(uint32_t) for buffer
- Fixed NULL pointer handling in cdx_vfio.c: check base_va before RTE_PTR_ADD
- Added GCC array-bounds diagnostic suppression in malloc_elem_from_data()
- Added bug tracker reference for volatile cast issue in idxd_pci.c
- Improved __rte_auto_type documentation: added C++11 and C23 support
- Moved doxygen rationale for void* return type to @return blocks
- Fixed MALLOC_ELEM_TRAILER to use RTE_PTR_UNQUAL for write operations

v12:
- void* return type to avoid optimizations assuming aligned access which isn't generally safe/true.

v11:
- Split API into PTR and INT_PTR variants, update all usage of PTR API for new APIs.

v10:
- Use unit_test_suite_runner for easier subtest failure identification

v9:
- Fix include order: system includes, then DPDK includes, then application includes
- Use NOHUGE_OK and ASAN_OK constants in REGISTER_FAST_TEST (instead of true, true)

v8:
- Remove tests for types < 32-bit (bool, char, short, uint8_t, uint16_t)
- Merge test_ptr_add_sub_typedefs() into test_ptr_add_sub_integer_types()
- Separate RTE_PTR_ADD and RTE_PTR_SUB documentation
- Move Clang/GCC implementation note from Doxygen to regular comment
- Tests verify both intermediate ADD result and SUB round-trip
- Use uintptr_t cast consistently for all integer-to-pointer conversions
- Make TEST_RETVAL calculated from TEST_INITVAL + TEST_INCREMENT

v7:
- Fix tests: use TEST_BUFFER_SIZE macro for buffer allocation
- Fix tests: ADD then SUB same amount to avoid out-of-bounds pointer arithmetic
- All RTE_PTR_SUB tests now verify round-trip (ADD+SUB returns to original)

v6:
- Make char* optimization Clang-only to avoid GCC false positive warnings
- Improve tests: use named constants instead of magic numbers
- Improve tests: use void* casts on expected values instead of uintptr_t on results

v5:
- Initial implementation with char* arithmetic for all compilers

v4:
- Used _Generic for type-based dispatch with char* casts
- Had warnings on both Clang and GCC due to _Generic type-checking all branches

Scott Mitchell (2):
eal: remove alloc_size from rte_lcore_var_alloc
eal: RTE_PTR_ADD/SUB API improvements

 app/test-pmd/cmdline_flow.c                 |   4 +-
 app/test/meson.build                        |   1 +
 app/test/test_common.c                      |  20 +-
 app/test/test_ptr_add_sub.c                 | 199 +++++++++++++++++
 doc/guides/rel_notes/release_26_03.rst      |  14 ++
 drivers/bus/cdx/cdx_vfio.c                  |  13 +-
 drivers/bus/pci/linux/pci.c                 |   6 +-
 drivers/bus/vmbus/linux/vmbus_uio.c         |   6 +-
 drivers/common/cnxk/roc_cpt_debug.c         |   4 +-
 drivers/common/cnxk/roc_ml.c                |   4 +-
 drivers/common/cnxk/roc_nix_bpf.c           |   2 +-
 drivers/common/cnxk/roc_nix_inl.h           |   4 +-
 drivers/common/cnxk/roc_nix_inl_dp.h        |   8 +-
 drivers/common/cnxk/roc_platform.h          |   2 +
 drivers/common/mlx5/mlx5_common_mr.c        |   2 +-
 drivers/dma/idxd/idxd_pci.c                 |  11 +-
 drivers/dma/odm/odm_dmadev.c                |   4 +-
 drivers/event/cnxk/cn10k_worker.c           |  32 +--
 drivers/event/cnxk/cn20k_worker.c           |  32 +--
 drivers/mempool/bucket/rte_mempool_bucket.c |   7 +-
 drivers/mempool/dpaa/dpaa_mempool.c         |   2 +-
 drivers/net/cxgbe/sge.c                     |   4 +-
 drivers/net/ena/ena_ethdev.c                |   9 +-
 drivers/net/intel/fm10k/fm10k.h             |   6 +-
 drivers/net/intel/fm10k/fm10k_rxtx_vec.c    |  12 +-
 drivers/net/mlx4/mlx4_txq.c                 |   3 +-
 lib/eal/common/eal_common_fbarray.c         |   3 +-
 lib/eal/common/eal_common_memory.c          |  32 ++-
 lib/eal/common/eal_common_options.c         |   2 +-
 lib/eal/common/malloc_elem.h                |  34 ++-
 lib/eal/freebsd/eal_memory.c                |   4 +
 lib/eal/include/rte_common.h                | 231 ++++++++++++++++++--
 lib/eal/include/rte_lcore_var.h             |   2 +-
 lib/eal/linux/eal_memalloc.c                |   6 +
 lib/eal/linux/eal_memory.c                  |   7 +
 lib/eal/windows/eal_memalloc.c              |   6 +
 lib/graph/rte_graph.h                       |   4 +-
 lib/latencystats/rte_latencystats.c         |   3 +
 lib/mbuf/rte_mbuf.c                         |   1 +
 lib/mbuf/rte_mbuf.h                         |   2 +
 lib/member/rte_xxh64_avx512.h               |   6 +-
 lib/mempool/rte_mempool.c                   |   8 +-
 lib/mempool/rte_mempool.h                   |   3 +
 lib/mempool/rte_mempool_ops_default.c       |   2 +-
 lib/pdcp/pdcp_entity.h                      |   8 +-
 lib/vhost/vhost_user.c                      |  13 +-
 46 files changed, 652 insertions(+), 136 deletions(-)
 create mode 100644 app/test/test_ptr_add_sub.c
Signature · 1 line
2.39.5 (Apple Git-154)
#35 lore raw

PATCH v17 1/2 eal: remove alloc_size from rte_lcore_var_alloc

From: Scott Mitchell <[email protected]>

The __rte_alloc_size(1) attribute on rte_lcore_var_alloc() is semantically incorrect and causes false positives with FORTIFY_SOURCE runtime checks.

The attribute tells the compiler that the function returns a pointer to 'size' bytes of usable memory. However, rte_lcore_var_alloc() actually returns a handle to a per-lcore variable scheme. The allocator internally allocates 'size' bytes per lcore (size * RTE_MAX_LCORE total), partitioned into per-lcore sections. The handle points to lcore 0's copy, and accessed via RTE_LCORE_VAR_LCORE(lcore_id, handle) which computes:
handle + (lcore_id * RTE_MAX_LCORE_VAR). Access is expected beyond 'size' bytes beyond the returned pointer when 'lcore_id != 0' but FORTIFY_SOURCE may terminate the program due to out of bounds access.

This can be observed on CI with gcc 13.3.0 in lcore_var_autotest with '*** buffer overflow detected ***: terminated' if pointer provenance is preserved (e.g. if offsets avoid casting to uintptr_t).

Fixes 5bce9bed67ad ("eal: add static per-lcore memory allocation facility")
---
 lib/eal/include/rte_lcore_var.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/eal/include/rte_lcore_var.h +1 -1
index ca31dff6fd..48b022e84e 100644
@@ -202,7 +202,7 @@ rte_lcore_var_lcore(unsigned int lcore_id, void *handle)
 __rte_experimental
 void *
 rte_lcore_var_alloc(size_t size, size_t align)
-	__rte_alloc_size(1) __rte_alloc_align(2);
+	__rte_alloc_align(2);
 
 #ifdef __cplusplus
 }
Signature · 1 line
2.39.5 (Apple Git-154)
Acked-by on v17 1/2
#36 lore raw

RE: [PATCH v17 1/2] eal: remove alloc_size from rte_lcore_var_alloc

Quoted 46 lines

From: Scott Mitchell <[email protected]>

The __rte_alloc_size(1) attribute on rte_lcore_var_alloc() is semantically incorrect and causes false positives with FORTIFY_SOURCE runtime checks.

The attribute tells the compiler that the function returns a pointer to 'size' bytes of usable memory. However, rte_lcore_var_alloc() actually returns a handle to a per-lcore variable scheme. The allocator internally allocates 'size' bytes per lcore (size * RTE_MAX_LCORE total), partitioned into per-lcore sections. The handle points to lcore 0's copy, and accessed via RTE_LCORE_VAR_LCORE(lcore_id, handle) which computes:
handle + (lcore_id * RTE_MAX_LCORE_VAR). Access is expected beyond 'size' bytes beyond the returned pointer when 'lcore_id != 0' but FORTIFY_SOURCE may terminate the program due to out of bounds access.

This can be observed on CI with gcc 13.3.0 in lcore_var_autotest with '*** buffer overflow detected ***: terminated' if pointer provenance is preserved (e.g. if offsets avoid casting to uintptr_t).

Fixes 5bce9bed67ad ("eal: add static per-lcore memory allocation

facility")
Cc: [email protected]
Cc: [email protected]

---
 lib/eal/include/rte_lcore_var.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/eal/include/rte_lcore_var.h +1 -1
index ca31dff6fd..48b022e84e 100644
@@ -202,7 +202,7 @@ rte_lcore_var_lcore(unsigned int lcore_id, void
 *handle)
 __rte_experimental
 void *
 rte_lcore_var_alloc(size_t size, size_t align)
-	__rte_alloc_size(1) __rte_alloc_align(2);
+	__rte_alloc_align(2);
 
 #ifdef __cplusplus
Quoted 3 lines

}
--
2.39.5 (Apple Git-154)

This patch could be separated from the RTE_PTR_ADD/SUB series.

NB: Please keep this ACK for any new versions of this patch if the essence of the patch doesn't change.

#37 lore raw

Re: [PATCH v17 1/2] eal: remove alloc_size from rte_lcore_var_alloc

Quoted 23 lines · Morten Brørup
Quoted 23 lines

From: Scott Mitchell <[email protected]>

The __rte_alloc_size(1) attribute on rte_lcore_var_alloc() is semantically incorrect and causes false positives with FORTIFY_SOURCE runtime checks.

The attribute tells the compiler that the function returns a pointer to 'size' bytes of usable memory. However, rte_lcore_var_alloc() actually returns a handle to a per-lcore variable scheme. The allocator internally allocates 'size' bytes per lcore (size * RTE_MAX_LCORE total), partitioned into per-lcore sections. The handle points to lcore 0's copy, and accessed via RTE_LCORE_VAR_LCORE(lcore_id, handle) which computes:
handle + (lcore_id * RTE_MAX_LCORE_VAR). Access is expected beyond 'size' bytes beyond the returned pointer when 'lcore_id != 0' but FORTIFY_SOURCE may terminate the program due to out of bounds access.

This can be observed on CI with gcc 13.3.0 in lcore_var_autotest with '*** buffer overflow detected ***: terminated' if pointer provenance is preserved (e.g. if offsets avoid casting to uintptr_t).

Quoted 8 lines
Quoted 6 lines
Fixes 5bce9bed67ad ("eal: add static per-lcore memory allocation

facility")
Cc: [email protected]
Cc: [email protected]

This patch could be separated from the RTE_PTR_ADD/SUB series.

I agree.
There were comments on patch 2, so I took this patch directly.

Quoted 3 lines

NB: Please keep this ACK for any new versions of this patch if the essence of the patch doesn't change.

Yes please, I restored acks from Mattias and Stephen.

Applied, thanks.

Signature · 1 line
David Marchand
#38 lore raw

PATCH v17 2/2 eal: RTE_PTR_ADD/SUB API improvements

In reply to [email protected]

From: Scott Mitchell <[email protected]>

RTE_PTR_ADD and RTE_PTR_SUB APIs have a few limitations:
1. ptr cast to uintptr_t drops pointer provenance and prevents compiler optimizations
2. return cast discards qualifiers (const, volatile) which may hide correctness/concurrency issues.
3. Accepts both "pointers" and "integers as pointers" which overloads the use case and constrains the implementation to address other challenges.

This patch splits the API on two dimensions:
1. pointer types
2. integer types that represent pointers

This split allows addressing each of the challenges above and provides distinct APIs for the distinct use cases. Examples:
1. Clang is able to optimize and improve __rte_raw_cksum (which uses RTE_PTR_ADD) by ~40% (100 bytes) to ~8x (1.5k bytes) TSC cycles/byte.
2. Refactoring discovered cases that dropped qualifiers (volatile) that the new API exposes.

---
 app/test-pmd/cmdline_flow.c                 |   4 +-
 app/test/meson.build                        |   1 +
 app/test/test_common.c                      |  20 +-
 app/test/test_ptr_add_sub.c                 | 199 +++++++++++++++++
 doc/guides/rel_notes/release_26_03.rst      |  14 ++
 drivers/bus/cdx/cdx_vfio.c                  |  13 +-
 drivers/bus/pci/linux/pci.c                 |   6 +-
 drivers/bus/vmbus/linux/vmbus_uio.c         |   6 +-
 drivers/common/cnxk/roc_cpt_debug.c         |   4 +-
 drivers/common/cnxk/roc_ml.c                |   4 +-
 drivers/common/cnxk/roc_nix_bpf.c           |   2 +-
 drivers/common/cnxk/roc_nix_inl.h           |   4 +-
 drivers/common/cnxk/roc_nix_inl_dp.h        |   8 +-
 drivers/common/cnxk/roc_platform.h          |   2 +
 drivers/common/mlx5/mlx5_common_mr.c        |   2 +-
 drivers/dma/idxd/idxd_pci.c                 |  11 +-
 drivers/dma/odm/odm_dmadev.c                |   4 +-
 drivers/event/cnxk/cn10k_worker.c           |  32 +--
 drivers/event/cnxk/cn20k_worker.c           |  32 +--
 drivers/mempool/bucket/rte_mempool_bucket.c |   7 +-
 drivers/mempool/dpaa/dpaa_mempool.c         |   2 +-
 drivers/net/cxgbe/sge.c                     |   4 +-
 drivers/net/ena/ena_ethdev.c                |   9 +-
 drivers/net/intel/fm10k/fm10k.h             |   6 +-
 drivers/net/intel/fm10k/fm10k_rxtx_vec.c    |  12 +-
 drivers/net/mlx4/mlx4_txq.c                 |   3 +-
 lib/eal/common/eal_common_fbarray.c         |   3 +-
 lib/eal/common/eal_common_memory.c          |  32 ++-
 lib/eal/common/eal_common_options.c         |   2 +-
 lib/eal/common/malloc_elem.h                |  34 ++-
 lib/eal/freebsd/eal_memory.c                |   4 +
 lib/eal/include/rte_common.h                | 231 ++++++++++++++++++--
 lib/eal/linux/eal_memalloc.c                |   6 +
 lib/eal/linux/eal_memory.c                  |   7 +
 lib/eal/windows/eal_memalloc.c              |   6 +
 lib/graph/rte_graph.h                       |   4 +-
 lib/latencystats/rte_latencystats.c         |   3 +
 lib/mbuf/rte_mbuf.c                         |   1 +
 lib/mbuf/rte_mbuf.h                         |   2 +
 lib/member/rte_xxh64_avx512.h               |   6 +-
 lib/mempool/rte_mempool.c                   |   8 +-
 lib/mempool/rte_mempool.h                   |   3 +
 lib/mempool/rte_mempool_ops_default.c       |   2 +-
 lib/pdcp/pdcp_entity.h                      |   8 +-
 lib/vhost/vhost_user.c                      |  13 +-
 45 files changed, 651 insertions(+), 135 deletions(-)
 create mode 100644 app/test/test_ptr_add_sub.c
app/test-pmd/cmdline_flow.c +2 -2
index ebc036b14b..8c2fc6c7b6 100644
@@ -12468,7 +12468,7 @@ parse_meter_color(struct context *ctx, const struct token *token,
 
 		if (!arg)
 			return -1;
-		*(int *)RTE_PTR_ADD(action->conf, arg->offset) = i;
+		*(int *)RTE_PTR_ADD(RTE_PTR_UNQUAL(action->conf), arg->offset) = i;
 	} else {
 		((struct rte_flow_item_meter_color *)
 			ctx->object)->color = (enum rte_color)i;
@@ -13351,7 +13351,7 @@ indirect_action_flow_conf_create(const struct buffer *in)
 		indlst_conf = NULL;
 		goto end;
 	}
-	indlst_conf->conf = RTE_PTR_ADD(indlst_conf, base + len);
+	indlst_conf->conf = (const void **)RTE_PTR_ADD(indlst_conf, base + len);
 	for (i = 0; i < indlst_conf->conf_num; i++)
 		indlst_conf->conf[i] = indlst_conf->actions[i].conf;
 	SLIST_INSERT_HEAD(&indlst_conf_head, indlst_conf, next);
app/test/meson.build +1 -0
index f4d04a6e42..aa56fc4297 100644
@@ -152,6 +152,7 @@ source_file_deps = {
     'test_power_intel_uncore.c': ['power', 'power_intel_uncore'],
     'test_power_kvm_vm.c': ['power', 'power_kvm_vm'],
     'test_prefetch.c': [],
+    'test_ptr_add_sub.c': [],
     'test_ptr_compress.c': ['ptr_compress'],
     'test_rand_perf.c': [],
     'test_rawdev.c': ['rawdev', 'bus_vdev', 'raw_skeleton'],
app/test/test_common.c +10 -10
index 3e1c7df0c1..a2507d0f51 100644
@@ -37,10 +37,10 @@ test_macros(int __rte_unused unused_parm)
 	RTE_SWAP(smaller, bigger);
 	RTE_TEST_ASSERT(smaller == BIGGER && bigger == SMALLER,
 		"RTE_SWAP");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF), BIGGER,
-		"RTE_PTR_ADD");
-	RTE_TEST_ASSERT_EQUAL((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF), SMALLER,
-		"RTE_PTR_SUB");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR(SMALLER + (PTR_DIFF)), (void *)BIGGER,
+		"RTE_INT_PTR ADD");
+	RTE_TEST_ASSERT_EQUAL(RTE_INT_PTR(BIGGER - (PTR_DIFF)), (void *)SMALLER,
+		"RTE_INT_PTR SUB");
 	RTE_TEST_ASSERT_EQUAL(RTE_PTR_DIFF(BIGGER, SMALLER), PTR_DIFF,
 		"RTE_PTR_DIFF");
 	RTE_TEST_ASSERT_EQUAL(RTE_MAX(SMALLER, BIGGER), BIGGER,
@@ -188,18 +188,18 @@ test_align(void)
 			if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
-			val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN_FLOOR((uintptr_t) i, p);
 			if (ERROR_FLOOR(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_FLOOR", i, p);
 
 			val = RTE_ALIGN_FLOOR(i, p);
 			if (ERROR_FLOOR(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
 
 			/* align ceiling */
-			val = RTE_PTR_ALIGN((uintptr_t) i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN((uintptr_t) i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN", i, p);
 
 			val = RTE_ALIGN(i, p);
 			if (ERROR_CEIL(val, i, p))
@@ -209,9 +209,9 @@ test_align(void)
 			if (ERROR_CEIL(val, i, p))
 				FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
 
-			val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
+			val = (uint32_t)(uintptr_t)RTE_INT_PTR_ALIGN_CEIL((uintptr_t)i, p);
 			if (ERROR_CEIL(val, i, p))
-				FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
+				FAIL_ALIGN("RTE_INT_PTR_ALIGN_CEIL", i, p);
 
 			/* by this point we know that val is aligned to p */
 			if (!rte_is_aligned((void*)(uintptr_t) val, p))
app/test/test_ptr_add_sub.c new file +199 -0

199 lines not on this page · raw message

doc/guides/rel_notes/release_26_03.rst +14 -0
index 15dabee7a1..d2157e8b42 100644
@@ -84,6 +84,20 @@ API Changes
    Also, make sure to start the actual text at the margin.
    =======================================================
 
+* eal: Improved pointer arithmetic macros to preserve pointer provenance and type qualifiers.
+
+  * ``RTE_PTR_ADD``, ``RTE_PTR_SUB``, ``RTE_PTR_ALIGN_CEIL``, and ``RTE_PTR_ALIGN_FLOOR``
+    now preserve const/volatile qualifiers and use pointer arithmetic instead of integer
+    casts to enable compiler optimizations.
+  * Passing NULL to ``RTE_PTR_ADD``, ``RTE_PTR_SUB``, ``RTE_PTR_ALIGN_CEIL``, or
+    ``RTE_PTR_ALIGN_FLOOR`` clarified as undefined behavior.
+  * Added ``RTE_INT_PTR`` for converting integer addresses to pointers.
+  * Added ``RTE_INT_PTR_ALIGN``, ``RTE_INT_PTR_ALIGN_FLOOR``, and ``RTE_INT_PTR_ALIGN_CEIL``
+    for aligning integer addresses.
+  * Existing code using ``RTE_PTR_ADD``, ``RTE_PTR_SUB``, ``RTE_PTR_ALIGN_CEIL``, or
+    ``RTE_PTR_ALIGN_FLOOR`` with integer types should migrate to ``RTE_INT_PTR*``
+    variants for clarity and correctness.
+
 
 ABI Changes
 -----------
drivers/bus/cdx/cdx_vfio.c +10 -3
index 11fe3265d2..a1009bc0ca 100644
@@ -367,9 +367,16 @@ cdx_vfio_get_region_info(int vfio_dev_fd, struct vfio_region_info **info,
 static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
-	size_t sz = msl->len;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
-	void **max_va = arg;
+	size_t sz;
+	void *end_va;
+	void **max_va;
+
+	if (msl->base_va == NULL)
+		return 0;
+
+	sz = msl->len;
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
+	max_va = arg;
 
 	if (*max_va < end_va)
 		*max_va = end_va;
drivers/bus/pci/linux/pci.c +5 -1
index 2ffac82e94..455db2cdec 100644
@@ -109,9 +109,13 @@ static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
 	size_t sz = msl->len;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
+	void *end_va;
 	void **max_va = arg;
 
+	if (msl->base_va == NULL)
+		return 0;
+
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
 	if (*max_va < end_va)
 		*max_va = end_va;
 	return 0;
drivers/bus/vmbus/linux/vmbus_uio.c +5 -1
index fbafc5027d..0ef05c0096 100644
@@ -122,9 +122,13 @@ static int
 find_max_end_va(const struct rte_memseg_list *msl, void *arg)
 {
 	size_t sz = msl->memseg_arr.len * msl->page_sz;
-	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
+	void *end_va;
 	void **max_va = arg;
 
+	if (msl->base_va == NULL)
+		return 0;
+
+	end_va = RTE_PTR_ADD(msl->base_va, sz);
 	if (*max_va < end_va)
 		*max_va = end_va;
 	return 0;
drivers/common/cnxk/roc_cpt_debug.c +2 -2
index 28aedf088e..252658a83f 100644
@@ -56,7 +56,7 @@ cpt_cnxk_parse_hdr_dump(FILE *file, const struct cpt_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*32B */
 	offset = cpth->w2.ptr_offset;
 	offset = (((offset - 1) & 0x7) + 1) * 32;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD(PLT_PTR_UNQUAL(cpth), offset);
 
 	if (cpth->w0.num_frags > 0) {
 		cpt_dump(file, "CPT Fraginfo_0 \t%p:", frag_info);
@@ -162,7 +162,7 @@ cpt_cn10k_parse_hdr_dump(FILE *file, const struct cpt_cn10k_parse_hdr_s *cpth)
 	/* offset of 0 implies 256B, otherwise it implies offset*8B */
 	offset = cpth->w2.fi_offset;
 	offset = (((offset - 1) & 0x1f) + 1) * 8;
-	frag_info = PLT_PTR_ADD(cpth, offset);
+	frag_info = PLT_PTR_ADD(PLT_PTR_UNQUAL(cpth), offset);
 
 	cpt_dump(file, "CPT Fraginfo \t0x%p:", frag_info);
 
drivers/common/cnxk/roc_ml.c +3 -1
index 7390697b1d..aa24dbb82f 100644
@@ -589,7 +589,9 @@ roc_ml_blk_init(struct roc_bphy *roc_bphy, struct roc_ml *roc_ml)
 
 	plt_ml_dbg(
 		"MLAB: Physical Address : 0x%016lx",
-		PLT_PTR_ADD_U64_CAST(ml->pci_dev->mem_resource[0].phys_addr, ML_MLAB_BLK_OFFSET));
+		   PLT_PTR_ADD_U64_CAST(
+				   PLT_INT_PTR(ml->pci_dev->mem_resource[0].phys_addr),
+				   ML_MLAB_BLK_OFFSET));
 	plt_ml_dbg("MLAB: Virtual Address : 0x%016lx",
 		   PLT_PTR_ADD_U64_CAST(ml->pci_dev->mem_resource[0].addr, ML_MLAB_BLK_OFFSET));
 
drivers/common/cnxk/roc_nix_bpf.c +1 -1
index 98c9855a5b..c42fb7e004 100644
@@ -160,7 +160,7 @@ nix_precolor_conv_table_write(struct roc_nix *roc_nix, uint64_t val,
 	struct nix *nix = roc_nix_to_nix_priv(roc_nix);
 	int64_t *addr;
 
-	addr = PLT_PTR_ADD(nix->base, off);
+	addr = PLT_INT_PTR(nix->base + off);
 	plt_write64(val, addr);
 }
 
drivers/common/cnxk/roc_nix_inl.h +2 -2
index 7970ac2258..3526ec9268 100644
@@ -59,7 +59,7 @@ roc_nix_inl_on_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline struct roc_ie_on_outb_sa *
@@ -67,7 +67,7 @@ roc_nix_inl_on_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_ON_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline void *
drivers/common/cnxk/roc_nix_inl_dp.h +4 -4
index eb101db179..ae161e02ed 100644
@@ -49,7 +49,7 @@ roc_nix_inl_ot_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline struct roc_ot_ipsec_outb_sa *
@@ -57,7 +57,7 @@ roc_nix_inl_ot_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OT_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline void *
@@ -77,7 +77,7 @@ roc_nix_inl_ow_ipsec_inb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_INB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline struct roc_ow_ipsec_outb_sa *
@@ -85,7 +85,7 @@ roc_nix_inl_ow_ipsec_outb_sa(uintptr_t base, uint64_t idx)
 {
 	uint64_t off = idx << ROC_NIX_INL_OW_IPSEC_OUTB_SA_SZ_LOG2;
 
-	return PLT_PTR_ADD(base, off);
+	return PLT_INT_PTR(base + off);
 }
 
 static inline void *
drivers/common/cnxk/roc_platform.h +2 -0
index e22a50d47a..4a5baa9a78 100644
@@ -47,6 +47,8 @@
 #define PLT_PTR_ADD		 RTE_PTR_ADD
 #define PLT_PTR_SUB		 RTE_PTR_SUB
 #define PLT_PTR_DIFF		 RTE_PTR_DIFF
+#define PLT_PTR_UNQUAL		 RTE_PTR_UNQUAL
+#define PLT_INT_PTR		 RTE_INT_PTR
 #define PLT_MAX_RXTX_INTR_VEC_ID RTE_MAX_RXTX_INTR_VEC_ID
 #define PLT_INTR_VEC_RXTX_OFFSET RTE_INTR_VEC_RXTX_OFFSET
 #define PLT_MIN			 RTE_MIN
drivers/common/mlx5/mlx5_common_mr.c +1 -1
index 8ed988dec9..aae39fdb6e 100644
@@ -1447,7 +1447,7 @@ mlx5_mempool_get_extmem_cb(struct rte_mempool *mp, void *opaque,
 	seg = &heap[data->heap_size - 1];
 	msl = rte_mem_virt2memseg_list((void *)addr);
 	page_size = msl != NULL ? msl->page_sz : rte_mem_page_size();
-	page_start = RTE_PTR_ALIGN_FLOOR(addr, page_size);
+	page_start = RTE_ALIGN_FLOOR(addr, page_size);
 	seg->start = page_start;
 	seg->end = page_start + page_size;
 	/* Maintain the heap order. */
drivers/dma/idxd/idxd_pci.c +7 -4
index 214f6f22d5..fb76a050b1 100644
@@ -59,7 +59,7 @@ idxd_pci_dev_command(struct idxd_dmadev *idxd, enum rte_idxd_cmds command)
 	return err_code;
 }
 
-static uint32_t *
+static volatile uint32_t *
 idxd_get_wq_cfg(struct idxd_pci_common *pci, uint8_t wq_idx)
 {
 	return RTE_PTR_ADD(pci->wq_regs_base,
@@ -205,9 +205,9 @@ init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,
 	pci->regs = dev->mem_resource[0].addr;
 	version = pci->regs->version;
 	grp_offset = (uint16_t)pci->regs->offsets[0];
-	pci->grp_regs = RTE_PTR_ADD(pci->regs, grp_offset * 0x100);
+	pci->grp_regs = RTE_PTR_ADD((volatile void *)pci->regs, grp_offset * 0x100);
 	wq_offset = (uint16_t)(pci->regs->offsets[0] >> 16);
-	pci->wq_regs_base = RTE_PTR_ADD(pci->regs, wq_offset * 0x100);
+	pci->wq_regs_base = RTE_PTR_ADD((volatile void *)pci->regs, wq_offset * 0x100);
 	pci->portals = dev->mem_resource[2].addr;
 	pci->wq_cfg_sz = (pci->regs->wqcap >> 24) & 0x0F;
 
@@ -395,7 +395,10 @@ idxd_dmadev_probe_pci(struct rte_pci_driver *drv, struct rte_pci_device *dev)
 		/* add the queue number to each device name */
 		snprintf(qname, sizeof(qname), "%s-q%d", name, qid);
 		idxd.qid = qid;
-		idxd.portal = RTE_PTR_ADD(idxd.u.pci->portals,
+		/* FIXME: cast drops volatile propagation to idxd_dmadev.portal
+		 * See: https://bugs.dpdk.org/show_bug.cgi?id=1871
+		 */
+		idxd.portal = RTE_PTR_ADD(RTE_PTR_UNQUAL(idxd.u.pci->portals),
 				qid * IDXD_PORTAL_SIZE);
 		if (idxd_is_wq_enabled(&idxd))
 			IDXD_PMD_ERR("Error, WQ %u seems enabled", qid);
drivers/dma/odm/odm_dmadev.c +2 -2
index a2f4ed9a8e..3b4b058427 100644
@@ -422,7 +422,7 @@ odm_dmadev_completed(void *dev_private, uint16_t vchan, const uint16_t nb_cpls,
 	int cnt;
 
 	vq = &odm->vq[vchan];
-	const uint32_t *base_addr = vq->cring_mz->addr;
+	uint32_t *base_addr = vq->cring_mz->addr;
 	const uint16_t cring_max_entry = vq->cring_max_entry;
 
 	cring_head = vq->cring_head;
@@ -482,7 +482,7 @@ odm_dmadev_completed_status(void *dev_private, uint16_t vchan, const uint16_t nb
 	int cnt;
 
 	vq = &odm->vq[vchan];
-	const uint32_t *base_addr = vq->cring_mz->addr;
+	uint32_t *base_addr = vq->cring_mz->addr;
 	const uint16_t cring_max_entry = vq->cring_max_entry;
 
 	cring_head = vq->cring_head;
drivers/event/cnxk/cn10k_worker.c +16 -16
index 80077ec8a1..cd35cc3e55 100644
@@ -261,14 +261,14 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 					aw7);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 64), aw4);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 80), aw5);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 96), aw6);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 112), aw7);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 128);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 32), aw2);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 48), aw3);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 64), aw4);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 80), aw5);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 96), aw6);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 112), aw7);
+			lmt_addr += 128;
 		} break;
 		case 4: {
 			uint64x2_t aw0, aw1, aw2, aw3;
@@ -291,10 +291,10 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 					aw3);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 64);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 32), aw2);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 48), aw3);
+			lmt_addr += 64;
 		} break;
 		case 2: {
 			uint64x2_t aw0, aw1;
@@ -310,8 +310,8 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 					aw1);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 32);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			lmt_addr += 32;
 		} break;
 		case 1: {
 			__uint128_t aw0;
@@ -322,7 +322,7 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 			aw0 |= (uint64_t)ev[0].sched_type << 32;
 
 			*((__uint128_t *)lmt_addr) = aw0;
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+			lmt_addr += 16;
 		} break;
 		}
 		ev += parts;
@@ -338,7 +338,7 @@ cn10k_sso_hws_new_event_lmtst(struct cn10k_sso_hws *ws, uint8_t queue_id,
 		aw0 |= ev[0].event & (BIT_ULL(32) - 1);
 		aw0 |= (uint64_t)ev[0].sched_type << 32;
 		*((__uint128_t *)lmt_addr) = aw0;
-		lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+		lmt_addr += 16;
 	}
 #endif
 
drivers/event/cnxk/cn20k_worker.c +16 -16
index 53daf3b4b0..2113a2ea78 100644
@@ -231,14 +231,14 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw7 = vorrq_u64(vandq_u64(vshrq_n_u64(aw7, 6), tt_mask), aw7);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 64), aw4);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 80), aw5);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 96), aw6);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 112), aw7);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 128);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 32), aw2);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 48), aw3);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 64), aw4);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 80), aw5);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 96), aw6);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 112), aw7);
+			lmt_addr += 128;
 		} break;
 		case 4: {
 			uint64x2_t aw0, aw1, aw2, aw3;
@@ -253,10 +253,10 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw3 = vorrq_u64(vandq_u64(vshrq_n_u64(aw3, 6), tt_mask), aw3);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 32), aw2);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 48), aw3);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 64);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 32), aw2);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 48), aw3);
+			lmt_addr += 64;
 		} break;
 		case 2: {
 			uint64x2_t aw0, aw1;
@@ -268,8 +268,8 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw1 = vorrq_u64(vandq_u64(vshrq_n_u64(aw1, 6), tt_mask), aw1);
 
 			vst1q_u64((void *)lmt_addr, aw0);
-			vst1q_u64((void *)PLT_PTR_ADD(lmt_addr, 16), aw1);
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 32);
+			vst1q_u64(PLT_INT_PTR(lmt_addr + 16), aw1);
+			lmt_addr += 32;
 		} break;
 		case 1: {
 			__uint128_t aw0;
@@ -280,7 +280,7 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 			aw0 |= (uint64_t)ev[0].sched_type << 32;
 
 			*((__uint128_t *)lmt_addr) = aw0;
-			lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+			lmt_addr += 16;
 		} break;
 		}
 		ev += parts;
@@ -296,7 +296,7 @@ cn20k_sso_hws_new_event_lmtst(struct cn20k_sso_hws *ws, uint8_t queue_id,
 		aw0 |= ev[0].event & (BIT_ULL(32) - 1);
 		aw0 |= (uint64_t)ev[0].sched_type << 32;
 		*((__uint128_t *)lmt_addr) = aw0;
-		lmt_addr = (uintptr_t)PLT_PTR_ADD(lmt_addr, 16);
+		lmt_addr += 16;
 	}
 #endif
 
drivers/mempool/bucket/rte_mempool_bucket.c +3 -4
index c0b480bfc7..6fee10176b 100644
@@ -376,8 +376,8 @@ count_underfilled_buckets(struct rte_mempool *mp,
 	uintptr_t align;
 	uint8_t *iter;
 
-	align = (uintptr_t)RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz) -
-		(uintptr_t)memhdr->addr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(memhdr->addr, bucket_page_sz),
+			     memhdr->addr);
 
 	for (iter = (uint8_t *)memhdr->addr + align;
 	     iter < (uint8_t *)memhdr->addr + memhdr->len;
@@ -602,8 +602,7 @@ bucket_populate(struct rte_mempool *mp, unsigned int max_objs,
 		return -EINVAL;
 
 	bucket_page_sz = rte_align32pow2(bd->bucket_mem_size);
-	align = RTE_PTR_ALIGN_CEIL((uintptr_t)vaddr, bucket_page_sz) -
-		(uintptr_t)vaddr;
+	align = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, bucket_page_sz), vaddr);
 
 	bucket_header_sz = bd->header_size - mp->header_size;
 	if (iova != RTE_BAD_IOVA)
drivers/mempool/dpaa/dpaa_mempool.c +1 -1
index 2f9395b3f4..85e1c01017 100644
@@ -321,7 +321,7 @@ dpaa_adjust_obj_bounds(char *va, size_t *offset,
 	size_t off = *offset;
 
 	if (dpaa_check_obj_bounds(va + off, pg_sz, total) == false) {
-		off += RTE_PTR_ALIGN_CEIL(va + off, pg_sz) - (va + off);
+		off += RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(va + off, pg_sz), va + off);
 		if (flags & RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ)
 			off += total - ((((size_t)va + off - 1) % total) + 1);
 	}
drivers/net/cxgbe/sge.c +2 -2
index e9d45f24c4..a5d112d52d 100644
@@ -591,7 +591,7 @@ static void write_sgl(struct rte_mbuf *mbuf, struct sge_txq *q,
 			memcpy(sgl->sge, buf, part0);
 		part1 = RTE_PTR_DIFF((u8 *)end, (u8 *)q->stat);
 		rte_memcpy(q->desc, RTE_PTR_ADD((u8 *)buf, part0), part1);
-		end = RTE_PTR_ADD((void *)q->desc, part1);
+		end = RTE_PTR_ADD(q->desc, part1);
 	}
 	if ((uintptr_t)end & 8)           /* 0-pad to multiple of 16 */
 		*(u64 *)end = 0;
@@ -1297,7 +1297,7 @@ static void inline_tx_mbuf(const struct sge_txq *q, caddr_t from, caddr_t *to,
 		from = RTE_PTR_ADD(from, left);
 		left = len - left;
 		rte_memcpy((void *)q->desc, from, left);
-		*to = RTE_PTR_ADD((void *)q->desc, left);
+		*to = RTE_PTR_ADD(q->desc, left);
 	}
 }
 
drivers/net/ena/ena_ethdev.c +8 -1
index ea4afbc75d..da23b271d5 100644
@@ -2379,7 +2379,14 @@ static void *pci_bar_addr(struct rte_pci_device *dev, uint32_t bar)
 {
 	const struct rte_mem_resource *res = &dev->mem_resource[bar];
 	size_t offset = res->phys_addr % rte_mem_page_size();
-	void *vaddr = RTE_PTR_ADD(res->addr, offset);
+	void *vaddr;
+
+	if (res->addr == NULL) {
+		PMD_INIT_LOG_LINE(ERR, "PCI BAR [%u] address is NULL", bar);
+		return NULL;
+	}
+
+	vaddr = RTE_PTR_ADD(res->addr, offset);
 
 	PMD_INIT_LOG_LINE(INFO, "PCI BAR [%u]: phys_addr=0x%" PRIx64 ", addr=%p, offset=0x%zx, adjusted_addr=%p",
 		bar, res->phys_addr, res->addr, offset, vaddr);
drivers/net/intel/fm10k/fm10k.h +3 -3
index 0eb32ac0d0..4e3081785f 100644
@@ -264,9 +264,9 @@ fm10k_pktmbuf_reset(struct rte_mbuf *mb, uint16_t in_port)
 	mb->nb_segs = 1;
 
 	/* enforce 512B alignment on default Rx virtual addresses */
-	mb->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb->buf_addr +
-			RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-			- (char *)mb->buf_addr);
+	mb->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb->buf_addr +
+			RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+			(char *)mb->buf_addr);
 	mb->port = in_port;
 }
 
drivers/net/intel/fm10k/fm10k_rxtx_vec.c +6 -6
index 0eada7275e..a08af75bc7 100644
@@ -315,12 +315,12 @@ fm10k_rxq_rearm(struct fm10k_rx_queue *rxq)
 		_mm_store_si128(RTE_CAST_PTR(__m128i *, &rxdp++->q), dma_addr1);
 
 		/* enforce 512B alignment on default Rx virtual addresses */
-		mb0->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb0->buf_addr
-				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-				- (char *)mb0->buf_addr);
-		mb1->data_off = (uint16_t)(RTE_PTR_ALIGN((char *)mb1->buf_addr
-				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN)
-				- (char *)mb1->buf_addr);
+		mb0->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb0->buf_addr
+				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+				(char *)mb0->buf_addr);
+		mb1->data_off = (uint16_t)RTE_PTR_DIFF(RTE_PTR_ALIGN((char *)mb1->buf_addr
+				+ RTE_PKTMBUF_HEADROOM, FM10K_RX_DATABUF_ALIGN),
+				(char *)mb1->buf_addr);
 	}
 
 	rxq->rxrearm_start += RTE_FM10K_RXQ_REARM_THRESH;
drivers/net/mlx4/mlx4_txq.c +2 -1
index 0db2e55bef..348040c1b9 100644
@@ -114,7 +114,8 @@ txq_uar_uninit_secondary(struct txq *txq)
 	void *addr;
 
 	addr = ppriv->uar_table[txq->stats.idx];
-	munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
+	if (addr)
+		munmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
 }
 
 /**
lib/eal/common/eal_common_fbarray.c +2 -1
index 8bdcefb717..526ba9f2eb 100644
@@ -10,6 +10,7 @@
 #include <unistd.h>
 
 #include <rte_common.h>
+#include <rte_debug.h>
 #include <rte_eal_paging.h>
 #include <rte_errno.h>
 #include <rte_log.h>
@@ -1048,7 +1049,7 @@ void *
 rte_fbarray_get(const struct rte_fbarray *arr, unsigned int idx)
 {
 	void *ret = NULL;
-	if (arr == NULL) {
+	if (arr == NULL || arr->data == NULL) {
 		rte_errno = EINVAL;
 		return NULL;
 	}
lib/eal/common/eal_common_memory.c +21 -11
index c62edf5e55..5f3168a286 100644
@@ -309,6 +309,9 @@ virt2memseg(const void *addr, const struct rte_memseg_list *msl)
 
 	/* a memseg list was specified, check if it's the right one */
 	start = msl->base_va;
+	if (start == NULL)
+		return NULL;
+
 	end = RTE_PTR_ADD(start, msl->len);
 
 	if (addr < start || addr >= end)
@@ -332,6 +335,8 @@ virt2memseg_list(const void *addr)
 		msl = &mcfg->memsegs[msl_idx];
 
 		start = msl->base_va;
+		if (start == NULL)
+			continue;
 		end = RTE_PTR_ADD(start, msl->len);
 		if (addr >= start && addr < end)
 			break;
@@ -680,10 +685,16 @@ RTE_EXPORT_SYMBOL(rte_mem_lock_page)
 int
 rte_mem_lock_page(const void *virt)
 {
-	uintptr_t virtual = (uintptr_t)virt;
 	size_t page_size = rte_mem_page_size();
-	uintptr_t aligned = RTE_PTR_ALIGN_FLOOR(virtual, page_size);
-	return rte_mem_lock((void *)aligned, page_size);
+	const void *aligned;
+
+	if (virt == NULL) {
+		rte_errno = EINVAL;
+		return -1;
+	}
+
+	aligned = RTE_PTR_ALIGN_FLOOR(virt, page_size);
+	return rte_mem_lock(aligned, page_size);
 }
 
 RTE_EXPORT_SYMBOL(rte_memseg_contig_walk_thread_unsafe)
@@ -1447,7 +1458,7 @@ handle_eal_memseg_info_request(const char *cmd __rte_unused,
 
 	ms_iova = ms->iova;
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 	ms_size = ms->len;
 	hugepage_size = ms->hugepage_sz;
 	ms_socket_id = ms->socket_id;
@@ -1519,7 +1530,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 	rte_mcfg_mem_read_unlock();
 
 	rte_tel_data_start_dict(d);
@@ -1530,8 +1541,7 @@ handle_eal_element_list_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = elem_start_addr + elem->size;
 
 		if ((uint64_t)elem_start_addr >= ms_start_addr &&
 		    (uint64_t)elem_end_addr <= ms_end_addr)
@@ -1553,7 +1563,8 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	struct rte_mem_config *mcfg;
 	struct rte_memseg_list *msl;
 	const struct rte_memseg *ms;
-	struct malloc_elem *elem;
+	/* volatile placement consistent with malloc_heap pointers */
+	struct malloc_elem *volatile elem;
 	struct malloc_heap *heap;
 	struct rte_tel_data *c;
 	uint64_t ms_start_addr, ms_end_addr;
@@ -1597,7 +1608,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	}
 
 	ms_start_addr = ms->addr_64;
-	ms_end_addr = (uint64_t)RTE_PTR_ADD(ms_start_addr, ms->len);
+	ms_end_addr = ms_start_addr + ms->len;
 
 	rte_mcfg_mem_read_unlock();
 
@@ -1609,8 +1620,7 @@ handle_eal_element_info_request(const char *cmd __rte_unused,
 	elem = heap->first;
 	while (elem) {
 		elem_start_addr = (uint64_t)elem;
-		elem_end_addr =
-			(uint64_t)RTE_PTR_ADD(elem_start_addr, elem->size);
+		elem_end_addr = elem_start_addr + elem->size;
 
 		if (elem_start_addr < ms_start_addr ||
 				elem_end_addr > ms_end_addr) {
lib/eal/common/eal_common_options.c +1 -1
index 485655865d..0e05683a67 100644
@@ -1656,7 +1656,7 @@ eal_parse_base_virtaddr(const char *arg)
 	 * on x86 and other architectures.
 	 */
 	internal_conf->base_virtaddr =
-		RTE_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
+		(uintptr_t) RTE_INT_PTR_ALIGN_CEIL(addr, (size_t)RTE_PGSIZE_16M);
 
 	return 0;
 }
lib/eal/common/malloc_elem.h +27 -7
index c7ff6718f8..babaead7de 100644
@@ -79,9 +79,11 @@ static const unsigned int MALLOC_ELEM_TRAILER_LEN = RTE_CACHE_LINE_SIZE;
 #define MALLOC_TRAILER_COOKIE  0xadd2e55badbadbadULL /**< Trailer cookie.*/
 
 /* define macros to make referencing the header and trailer cookies easier */
-#define MALLOC_ELEM_TRAILER(elem) (*((uint64_t*)RTE_PTR_ADD(elem, \
-		elem->size - MALLOC_ELEM_TRAILER_LEN)))
-#define MALLOC_ELEM_HEADER(elem) (elem->header_cookie)
+#define MALLOC_ELEM_TRAILER(elem) \
+	/* typeof preserves qualifiers (const/volatile) of elem */ \
+	(*(typeof((elem)->header_cookie) *)RTE_PTR_ADD(elem, \
+		(elem)->size - MALLOC_ELEM_TRAILER_LEN))
+#define MALLOC_ELEM_HEADER(elem) ((elem)->header_cookie)
 
 static inline void
 set_header(struct malloc_elem *elem)
@@ -306,13 +308,31 @@ old_malloc_size(struct malloc_elem *elem)
 static inline struct malloc_elem *
 malloc_elem_from_data(const void *data)
 {
+	struct malloc_elem *result;
+
 	if (data == NULL)
 		return NULL;
 
-	struct malloc_elem *elem = RTE_PTR_SUB(data, MALLOC_ELEM_HEADER_LEN);
-	if (!malloc_elem_cookies_ok(elem))
-		return NULL;
-	return elem->state != ELEM_PAD ? elem:  RTE_PTR_SUB(elem, elem->pad);
+	/* The allocator returns a pointer in the middle of an allocation pool.
+	 * GCC's interprocedural analysis can't trace this and warns about
+	 * out-of-bounds access when we do backwards pointer arithmetic to
+	 * find the malloc_elem header.
+	 */
+	__rte_diagnostic_push
+	__rte_diagnostic_ignored_array_bounds
+	{
+		struct malloc_elem *elem =
+			RTE_PTR_SUB(RTE_PTR_UNQUAL(data), MALLOC_ELEM_HEADER_LEN);
+
+		if (!malloc_elem_cookies_ok(elem))
+			result = NULL;
+		else
+			result = elem->state != ELEM_PAD ? elem :
+				RTE_PTR_SUB(elem, elem->pad);
+	}
+	__rte_diagnostic_pop
+
+	return result;
 }
 
 /*
lib/eal/freebsd/eal_memory.c +4 -0
index 6d3d46a390..49a89fe2fb 100644
@@ -178,6 +178,10 @@ rte_eal_hugepage_init(void)
 					"RTE_MAX_MEMSEG_PER_TYPE and/or RTE_MAX_MEM_MB_PER_TYPE in configuration.");
 				return -1;
 			}
+			if (msl->base_va == NULL) {
+				EAL_LOG(ERR, "Base VA is NULL for memseg list %d", msl_idx);
+				return -1;
+			}
 			arr = &msl->memseg_arr;
 			seg = rte_fbarray_get(arr, ms_idx);
 
lib/eal/include/rte_common.h +216 -15

285 lines not on this page · raw message

lib/eal/linux/eal_memalloc.c +6 -0
index 1e60e21620..f770826a43 100644
@@ -835,6 +835,12 @@ alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
 		void *map_addr;
 
 		cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
+
+		if (cur_msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			goto out;
+		}
+
 		map_addr = RTE_PTR_ADD(cur_msl->base_va,
 				cur_idx * page_sz);
 
lib/eal/linux/eal_memory.c +7 -0
index 8e1763e890..3830bd5d7f 100644
@@ -759,6 +759,13 @@ remap_segment(struct hugepage_file *hugepages, int seg_start, int seg_end)
 			return -1;
 		}
 		memseg_len = (size_t)page_sz;
+
+		if (msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			close(fd);
+			return -1;
+		}
+
 		addr = RTE_PTR_ADD(msl->base_va, ms_idx * memseg_len);
 
 		/* we know this address is already mmapped by memseg list, so
lib/eal/windows/eal_memalloc.c +6 -0
index 5db5a474cc..6432caccd3 100644
@@ -230,6 +230,12 @@ alloc_seg_walk(const struct rte_memseg_list *msl, void *arg)
 		void *map_addr;
 
 		cur = rte_fbarray_get(&cur_msl->memseg_arr, cur_idx);
+
+		if (cur_msl->base_va == NULL) {
+			EAL_LOG(ERR, "Base VA is NULL for memseg list");
+			goto out;
+		}
+
 		map_addr = RTE_PTR_ADD(cur_msl->base_va, cur_idx * page_sz);
 
 		if (alloc_seg(cur, map_addr, wa->socket, wa->hi)) {
lib/graph/rte_graph.h +2 -2
index 7e433f4661..d8ac0011e4 100644
@@ -407,9 +407,9 @@ void rte_graph_obj_dump(FILE *f, struct rte_graph *graph, bool all);
 /** Macro to browse rte_node object after the graph creation */
 #define rte_graph_foreach_node(count, off, graph, node)                        \
 	for (count = 0, off = graph->nodes_start,                              \
-	     node = RTE_PTR_ADD(graph, off);                                   \
+	     node = RTE_PTR_ADD(RTE_PTR_UNQUAL(graph), off);                           \
 	     count < graph->nb_nodes;                                          \
-	     off = node->next, node = RTE_PTR_ADD(graph, off), count++)
+	     off = node->next, node = RTE_PTR_ADD(RTE_PTR_UNQUAL(graph), off), count++)
 
 /**
  * Get node object with in graph from id.
lib/latencystats/rte_latencystats.c +3 -0
index f61d5a273f..f2cd0db4b7 100644
@@ -104,6 +104,9 @@ latencystats_collect(uint64_t values[])
 	unsigned int i, scale;
 	const uint64_t *stats;
 
+	if (glob_stats == NULL)
+		return;
+
 	for (i = 0; i < NUM_LATENCY_STATS; i++) {
 		stats = RTE_PTR_ADD(glob_stats, lat_stats_strings[i].offset);
 		scale = lat_stats_strings[i].scale;
lib/mbuf/rte_mbuf.c +1 -0
index 0d931c7a15..557954d45e 100644
@@ -193,6 +193,7 @@ __rte_pktmbuf_init_extmem(struct rte_mempool *mp,
 
 	RTE_ASSERT(ctx->ext < ctx->ext_num);
 	RTE_ASSERT(ctx->off + ext_mem->elt_size <= ext_mem->buf_len);
+	RTE_ASSERT(ext_mem->buf_ptr);
 
 	m->buf_addr = RTE_PTR_ADD(ext_mem->buf_ptr, ctx->off);
 	rte_mbuf_iova_set(m, ext_mem->buf_iova == RTE_BAD_IOVA ? RTE_BAD_IOVA :
lib/mbuf/rte_mbuf.h +2 -0
index 2004391f57..3100feb740 100644
@@ -217,6 +217,7 @@ rte_mbuf_data_iova_default(const struct rte_mbuf *mb)
 static inline struct rte_mbuf *
 rte_mbuf_from_indirect(struct rte_mbuf *mi)
 {
+	RTE_ASSERT(mi);
 	return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) + mi->priv_size);
 }
 
@@ -289,6 +290,7 @@ rte_mbuf_to_baddr(struct rte_mbuf *md)
 static inline void *
 rte_mbuf_to_priv(struct rte_mbuf *m)
 {
+	RTE_ASSERT(m);
 	return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
 }
 
lib/member/rte_xxh64_avx512.h +3 -3
index 58f896ebb8..774b26d8df 100644
@@ -58,7 +58,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 			 _mm512_set1_epi64(key_len));
 
 	while (remaining >= 8) {
-		input = _mm512_set1_epi64(*(uint64_t *)RTE_PTR_ADD(key, offset));
+		input = _mm512_set1_epi64(*(const uint64_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 				xxh64_round_avx512(_mm512_setzero_si512(), input));
 		v_hash = _mm512_madd52lo_epu64(_mm512_set1_epi64(PRIME64_4),
@@ -71,7 +71,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	if (remaining >= 4) {
 		input = _mm512_set1_epi64
-			(*(uint32_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint32_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_1)));
@@ -86,7 +86,7 @@ rte_xxh64_sketch_avx512(const void *key, uint32_t key_len,
 
 	while (remaining != 0) {
 		input = _mm512_set1_epi64
-			(*(uint8_t *)RTE_PTR_ADD(key, offset));
+			(*(const uint8_t *)RTE_PTR_ADD(key, offset));
 		v_hash = _mm512_xor_epi64(v_hash,
 			_mm512_mullo_epi64(input,
 				_mm512_set1_epi64(PRIME64_5)));
lib/mempool/rte_mempool.c +4 -4
index 3042d94c14..f1ff668205 100644
@@ -349,9 +349,9 @@ rte_mempool_populate_iova(struct rte_mempool *mp, char *vaddr,
 	memhdr->opaque = opaque;
 
 	if (mp->flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
-		off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
+		off = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, 8), vaddr);
 	else
-		off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN) - vaddr;
+		off = RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN), vaddr);
 
 	if (off > len) {
 		ret = 0;
@@ -425,8 +425,8 @@ rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
 
 		/* populate with the largest group of contiguous pages */
 		for (phys_len = RTE_MIN(
-			(size_t)(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz) -
-				(addr + off)),
+			(size_t)RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz),
+				addr + off),
 			len - off);
 		     off + phys_len < len;
 		     phys_len = RTE_MIN(phys_len + pg_sz, len - off)) {
lib/mempool/rte_mempool.h +3 -0
index aedc100964..091976574a 100644
@@ -376,6 +376,7 @@ struct __rte_cache_aligned rte_mempool {
 static inline struct rte_mempool_objhdr *
 rte_mempool_get_header(void *obj)
 {
+	RTE_ASSERT(obj);
 	return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
 		sizeof(struct rte_mempool_objhdr));
 }
@@ -399,6 +400,7 @@ static inline struct rte_mempool *rte_mempool_from_obj(void *obj)
 static inline struct rte_mempool_objtlr *rte_mempool_get_trailer(void *obj)
 {
 	struct rte_mempool *mp = rte_mempool_from_obj(obj);
+	RTE_ASSERT(mp);
 	return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp->elt_size);
 }
 
@@ -1844,6 +1846,7 @@ rte_mempool_empty(const struct rte_mempool *mp)
 static inline rte_iova_t
 rte_mempool_virt2iova(const void *elt)
 {
+	RTE_ASSERT(elt);
 	const struct rte_mempool_objhdr *hdr;
 	hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
 		sizeof(*hdr));
lib/mempool/rte_mempool_ops_default.c +1 -1
index d27d6fc473..e28a288b91 100644
@@ -117,7 +117,7 @@ rte_mempool_op_populate_helper(struct rte_mempool *mp, unsigned int flags,
 	for (i = 0; i < max_objs; i++) {
 		/* avoid objects to cross page boundaries */
 		if (check_obj_bounds(va + off, pg_sz, total_elt_sz) < 0) {
-			off += RTE_PTR_ALIGN_CEIL(va + off, pg_sz) - (va + off);
+			off += RTE_PTR_DIFF(RTE_PTR_ALIGN_CEIL(va + off, pg_sz), va + off);
 			if (flags & RTE_MEMPOOL_POPULATE_F_ALIGN_OBJ)
 				off += total_elt_sz -
 					(((uintptr_t)(va + off - 1) %
lib/pdcp/pdcp_entity.h +5 -3
index f854192e98..7a2c41dd56 100644
@@ -198,17 +198,19 @@ struct entity_priv_ul_part {
 
 static inline struct entity_priv *
 entity_priv_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity), sizeof(struct rte_pdcp_entity));
 }
 
 static inline struct entity_priv_dl_part *
 entity_dl_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity),
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline struct entity_priv_ul_part *
 entity_ul_part_get(const struct rte_pdcp_entity *entity) {
-	return RTE_PTR_ADD(entity, sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
+	return RTE_PTR_ADD(RTE_PTR_UNQUAL(entity),
+			   sizeof(struct rte_pdcp_entity) + sizeof(struct entity_priv));
 }
 
 static inline int
lib/vhost/vhost_user.c +10 -3
index 4bfb13fb98..a9ab6487f2 100644
@@ -824,9 +824,16 @@ void
 mem_set_dump(struct virtio_net *dev, void *ptr, size_t size, bool enable, uint64_t pagesz)
 {
 #ifdef MADV_DONTDUMP
-	void *start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz);
-	uintptr_t end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz);
-	size_t len = end - (uintptr_t)start;
+	void *start;
+	uintptr_t end;
+	size_t len;
+
+	if (ptr == NULL)
+		return;
+
+	start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz);
+	end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz);
+	len = end - (uintptr_t)start;
 
 	if (madvise(start, len, enable ? MADV_DODUMP : MADV_DONTDUMP) == -1) {
 		VHOST_CONFIG_LOG(dev->ifname, INFO,
Signature · 1 line
2.39.5 (Apple Git-154)
#39 lore raw

RE: [PATCH v17 2/2] eal: RTE_PTR_ADD/SUB API improvements

Quoted 11 lines

RTE_PTR_ADD and RTE_PTR_SUB APIs have a few limitations:
1. ptr cast to uintptr_t drops pointer provenance and prevents compiler optimizations
2. return cast discards qualifiers (const, volatile) which may hide correctness/concurrency issues.
3. Accepts both "pointers" and "integers as pointers" which overloads the use case and constrains the implementation to address other challenges.

This patch splits the API on two dimensions:
1. pointer types

Again, thank you for all the work you put into this, Scott!

Quoted 1 line

2. integer types that represent pointers

Please get rid of all the RTE_INT_PTR macros.
IMO, these macros look too much like plain wrappers around simple +/- operators.
It seems they are only needed for the cnxk drivers; those drivers can probably be fixed in some other way.

Quoted 10 lines

This split allows addressing each of the challenges above and provides distinct APIs for the distinct use cases. Examples:
1. Clang is able to optimize and improve __rte_raw_cksum (which uses RTE_PTR_ADD) by ~40% (100 bytes) to ~8x (1.5k bytes) TSC cycles/byte.
2. Refactoring discovered cases that dropped qualifiers (volatile) that the new API exposes.

Quoted 2 lines

---

A few minor details follow.

lib/mbuf/rte_mbuf.h rte_mbuf_data_iova_default(const struct rte_mbuf *mb) · line 220

Quoted 4 lines

static inline struct rte_mbuf *
rte_mbuf_from_indirect(struct rte_mbuf *mi)
{

lib/mbuf/rte_mbuf.h +1 -0
+	RTE_ASSERT(mi);

Preferred: RTE_ASSERT(mi != NULL);

lib/mbuf/rte_mbuf.h rte_mbuf_to_baddr(struct rte_mbuf *md) · line 293

Quoted 9 lines

return (struct rte_mbuf *)RTE_PTR_SUB(mi->buf_addr, sizeof(*mi) +
mi->priv_size);
}

lib/mbuf/rte_mbuf.h +1 -0
@@ -289,6 +290,7 @@ rte_mbuf_to_baddr(struct rte_mbuf *md)
 static inline void *
 rte_mbuf_to_priv(struct rte_mbuf *m)
 {
+	RTE_ASSERT(m);

Preferred: RTE_ASSERT(m != NULL);

lib/mempool/rte_mempool.h struct __rte_cache_aligned rte_mempool { · line 379

Quoted 12 lines
lib/mbuf/rte_mbuf.h +0 -0
 	return RTE_PTR_ADD(m, sizeof(struct rte_mbuf));
 }
 
lib/mempool/rte_mempool.h +1 -0
@@ -376,6 +376,7 @@ struct __rte_cache_aligned rte_mempool {
 static inline struct rte_mempool_objhdr *
 rte_mempool_get_header(void *obj)
 {
+	RTE_ASSERT(obj);

Preferred: RTE_ASSERT(obj != NULL);

lib/mempool/rte_mempool.h static inline struct rte_mempool · line 405

Quoted 10 lines
lib/mempool/rte_mempool.h +1 -0
 	return (struct rte_mempool_objhdr *)RTE_PTR_SUB(obj,
 		sizeof(struct rte_mempool_objhdr));
 }
@@ -399,6 +400,7 @@ static inline struct rte_mempool
 *rte_mempool_from_obj(void *obj)
 static inline struct rte_mempool_objtlr *rte_mempool_get_trailer(void
 *obj)
 {
 	struct rte_mempool *mp = rte_mempool_from_obj(obj);
+	RTE_ASSERT(mp);

Preferred: RTE_ASSERT(mp != NULL);

lib/mempool/rte_mempool.h rte_mempool_empty(const struct rte_mempool *mp) · line 1850

Quoted 10 lines
lib/mempool/rte_mempool.h +0 -0
 	return (struct rte_mempool_objtlr *)RTE_PTR_ADD(obj, mp-
Quoted 1 line

elt_size);

}

lib/mempool/rte_mempool.h +1 -0
@@ -1844,6 +1846,7 @@ rte_mempool_empty(const struct rte_mempool *mp)
 static inline rte_iova_t
 rte_mempool_virt2iova(const void *elt)
 {
+	RTE_ASSERT(elt);
 	const struct rte_mempool_objhdr *hdr;

Preferred: RTE_ASSERT(elt != NULL);
Also, it might be preferable adding the RTE_ASSERT() here instead of above.

Quoted 2 lines
lib/mempool/rte_mempool.h +0 -0
 	hdr = (const struct rte_mempool_objhdr *)RTE_PTR_SUB(elt,
 		sizeof(*hdr));
#40 lore raw

Re: [PATCH v17 2/2] eal: RTE_PTR_ADD/SUB API improvements

Quoted 1 line

Again, thank you for all the work you put into this, Scott!

Thanks for all the feedback :)

Quoted 6 lines
Quoted 1 line

2. integer types that represent pointers

Please get rid of all the RTE_INT_PTR macros.
IMO, these macros look too much like plain wrappers around simple +/- operators.
It seems they are only needed for the cnxk drivers; those drivers can probably be fixed in some other way.

Sounds good. Looking at the current state, cnxk doesn't even need them. Just 1 usage in eal_common_options.c I can cast and work around.

Quoted 3 lines
Quoted 1 line

+ RTE_ASSERT(mi);

Preferred: RTE_ASSERT(mi != NULL);

Asserts updated

#41 lore raw

Re: [PATCH v17 2/2] eal: RTE_PTR_ADD/SUB API improvements

In reply to [email protected]
Quoted 25 lines · On Mon, 2 Feb 2026 at 06:25, <[email protected]> wrote:

From: Scott Mitchell <[email protected]>

RTE_PTR_ADD and RTE_PTR_SUB APIs have a few limitations:
1. ptr cast to uintptr_t drops pointer provenance and prevents compiler optimizations
2. return cast discards qualifiers (const, volatile) which may hide correctness/concurrency issues.
3. Accepts both "pointers" and "integers as pointers" which overloads the use case and constrains the implementation to address other challenges.

This patch splits the API on two dimensions:
1. pointer types
2. integer types that represent pointers

This split allows addressing each of the challenges above and provides distinct APIs for the distinct use cases. Examples:
1. Clang is able to optimize and improve __rte_raw_cksum (which uses RTE_PTR_ADD) by ~40% (100 bytes) to ~8x (1.5k bytes) TSC cycles/byte.
2. Refactoring discovered cases that dropped qualifiers (volatile) that the new API exposes.

Quoted 6 lines
---
 app/test-pmd/cmdline_flow.c                 |   4 +-
 app/test/meson.build                        |   1 +
 app/test/test_common.c                      |  20 +-
 app/test/test_ptr_add_sub.c                 | 199 +++++++++++++++++

Do we *need* a new file for testing new macros?
So far, everything went to test_common.c.

Signature · 1 line
David Marchand
#42 lore raw

Re: [PATCH v17 2/2] eal: RTE_PTR_ADD/SUB API improvements

Quoted 2 lines

Do we *need* a new file for testing new macros?
So far, everything went to test_common.c.

I'll move into test_common.c. I'll keep independent tests that verify pointer types natively (existing tests use uintptr_t).

#43 lore raw

v18 cover eal: RTE_PTR_ADD/SUB API improvements

In reply to [email protected]

From: Scott <[email protected]>

This series addresses two issues:

1. Removes the semantically incorrect __rte_alloc_size attribute from rte_lcore_var_alloc which was causing FORTIFY_SOURCE failures on Ubuntu 24.04 CI. The attribute was incorrect because the function returns a handle to per-lcore variables, not a direct pointer to 'size' bytes.

2. Improves RTE_PTR_ADD/SUB macros to preserve pointer type qualifiers (const/volatile) and use pointer arithmetic instead of integer casts to enable compiler optimizations and maintain pointer provenance.

The bugfix (patch 1) is placed first so it can be backported independently to stable branches.

---
v18:
- Removed RTE_INT_PTR* macros
- Explicit NULL compare in asserts
- Consolidated test_ptr_add_sub.c into test_common.c

v17:
- Improved release notes to explicitly list macro names for search/indexing
- eal_common_fbarray.c RTE_ASSERT to runtime NULL check

v16:
- Fixed test_common.c: parenthesize PTR_DIFF in RTE_INT_PTR tests

v15:
- Fixed __rte_alloc_size, spilt into 2 patch series
- Replaced RTE_INT_PTR_ADD/SUB with simpler RTE_INT_PTR(val) macro users do int arithmetic directly: RTE_INT_PTR(addr + offset)

v14:
- fixed cpp compiler error, avoiding array pointer decay which is implicitly done in cpp (but not c)
- fixed MALLOC_ELEM_TRAILER const preservation compile error

v13:
- Added release notes documenting API changes
- Fixed alignment in test file: use alignas(uint32_t) for buffer
- Fixed NULL pointer handling in cdx_vfio.c: check base_va before RTE_PTR_ADD
- Added GCC array-bounds diagnostic suppression in malloc_elem_from_data()
- Added bug tracker reference for volatile cast issue in idxd_pci.c
- Improved __rte_auto_type documentation: added C++11 and C23 support
- Moved doxygen rationale for void* return type to @return blocks
- Fixed MALLOC_ELEM_TRAILER to use RTE_PTR_UNQUAL for write operations

v12:
- void* return type to avoid optimizations assuming aligned access which isn't generally safe/true.

v11:
- Split API into PTR and INT_PTR variants, update all usage of PTR API for new APIs.

v10:
- Use unit_test_suite_runner for easier subtest failure identification

v9:
- Fix include order: system includes, then DPDK includes, then application includes
- Use NOHUGE_OK and ASAN_OK constants in REGISTER_FAST_TEST (instead of true, true)

v8:
- Remove tests for types < 32-bit (bool, char, short, uint8_t, uint16_t)
- Merge test_ptr_add_sub_typedefs() into test_ptr_add_sub_integer_types()
- Separate RTE_PTR_ADD and RTE_PTR_SUB documentation
- Move Clang/GCC implementation note from Doxygen to regular comment
- Tests verify both intermediate ADD result and SUB round-trip
- Use uintptr_t cast consistently for all integer-to-pointer conversions
- Make TEST_RETVAL calculated from TEST_INITVAL + TEST_INCREMENT

v7:
- Fix tests: use TEST_BUFFER_SIZE macro for buffer allocation
- Fix tests: ADD then SUB same amount to avoid out-of-bounds pointer arithmetic
- All RTE_PTR_SUB tests now verify round-trip (ADD+SUB returns to original)

v6:
- Make char* optimization Clang-only to avoid GCC false positive warnings
- Improve tests: use named constants instead of magic numbers
- Improve tests: use void* casts on expected values instead of uintptr_t on results

v5:
- Initial implementation with char* arithmetic for all compilers

v4:
- Used _Generic for type-based dispatch with char* casts
- Had warnings on both Clang and GCC due to _Generic type-checking all branches

Scott Mitchell (2):
eal: remove alloc_size from rte_lcore_var_alloc
eal: RTE_PTR_ADD/SUB API improvements

 app/test-pmd/cmdline_flow.c                 |   4 +-
 app/test/test_common.c                      | 307 ++++++++++++++++++--
 doc/guides/rel_notes/release_26_03.rst      |  12 +
 drivers/bus/cdx/cdx_vfio.c                  |  13 +-
 drivers/bus/pci/linux/pci.c                 |   6 +-
 drivers/bus/vmbus/linux/vmbus_uio.c         |   6 +-
 drivers/common/cnxk/roc_cpt_debug.c         |   4 +-
 drivers/common/cnxk/roc_ml.c                |   4 +-
 drivers/common/cnxk/roc_nix_bpf.c           |   2 +-
 drivers/common/cnxk/roc_nix_inl.h           |   4 +-
 drivers/common/cnxk/roc_nix_inl_dp.h        |   8 +-
 drivers/common/cnxk/roc_platform.h          |   2 +
 drivers/common/mlx5/mlx5_common_mr.c        |   2 +-
 drivers/dma/idxd/idxd_pci.c                 |  11 +-
 drivers/dma/odm/odm_dmadev.c                |   4 +-
 drivers/event/cnxk/cn10k_worker.c           |  32 +-
 drivers/event/cnxk/cn20k_worker.c           |  32 +-
 drivers/mempool/bucket/rte_mempool_bucket.c |   7 +-
 drivers/mempool/dpaa/dpaa_mempool.c         |   2 +-
 drivers/net/cxgbe/sge.c                     |   4 +-
 drivers/net/ena/ena_ethdev.c                |   9 +-
 drivers/net/intel/fm10k/fm10k.h             |   6 +-
 drivers/net/intel/fm10k/fm10k_rxtx_vec.c    |  12 +-
 drivers/net/mlx4/mlx4_txq.c                 |   3 +-
 lib/eal/common/eal_common_fbarray.c         |   3 +-
 lib/eal/common/eal_common_memory.c          |  32 +-
 lib/eal/common/eal_common_options.c         |   3 +-
 lib/eal/common/malloc_elem.h                |  34 ++-
 lib/eal/freebsd/eal_memory.c                |   4 +
 lib/eal/include/rte_common.h                | 158 +++++++++-
 lib/eal/include/rte_lcore_var.h             |   2 +-
 lib/eal/linux/eal_memalloc.c                |   6 +
 lib/eal/linux/eal_memory.c                  |   7 +
 lib/eal/windows/eal_memalloc.c              |   6 +
 lib/graph/rte_graph.h                       |   4 +-
 lib/latencystats/rte_latencystats.c         |   3 +
 lib/mbuf/rte_mbuf.c                         |   1 +
 lib/mbuf/rte_mbuf.h                         |   2 +
 lib/member/rte_xxh64_avx512.h               |   6 +-
 lib/mempool/rte_mempool.c                   |   8 +-
 lib/mempool/rte_mempool.h                   |   3 +
 lib/mempool/rte_mempool_ops_default.c       |   2 +-
 lib/pdcp/pdcp_entity.h                      |   8 +-
 lib/vhost/vhost_user.c                      |  13 +-
 44 files changed, 649 insertions(+), 152 deletions(-)
Signature · 1 line
2.39.5 (Apple Git-154)
#44 lore raw

PATCH v18 1/2 eal: remove alloc_size from rte_lcore_var_alloc

From: Scott Mitchell <[email protected]>

The __rte_alloc_size(1) attribute on rte_lcore_var_alloc() is semantically incorrect and causes false positives with FORTIFY_SOURCE runtime checks.

The attribute tells the compiler that the function returns a pointer to 'size' bytes of usable memory. However, rte_lcore_var_alloc() actually returns a handle to a per-lcore variable scheme. The allocator internally allocates 'size' bytes per lcore (size * RTE_MAX_LCORE total), partitioned into per-lcore sections. The handle points to lcore 0's copy, and accessed via RTE_LCORE_VAR_LCORE(lcore_id, handle) which computes:
handle + (lcore_id * RTE_MAX_LCORE_VAR). Access is expected beyond 'size' bytes beyond the returned pointer when 'lcore_id != 0' but FORTIFY_SOURCE may terminate the program due to out of bounds access.

This can be observed on CI with gcc 13.3.0 in lcore_var_autotest with '*** buffer overflow detected ***: terminated' if pointer provenance is preserved (e.g. if offsets avoid casting to uintptr_t).

Fixes 5bce9bed67ad ("eal: add static per-lcore memory allocation facility")
---
 lib/eal/include/rte_lcore_var.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/eal/include/rte_lcore_var.h +1 -1
index ca31dff6fd..48b022e84e 100644
@@ -202,7 +202,7 @@ rte_lcore_var_lcore(unsigned int lcore_id, void *handle)
 __rte_experimental
 void *
 rte_lcore_var_alloc(size_t size, size_t align)
-	__rte_alloc_size(1) __rte_alloc_align(2);
+	__rte_alloc_align(2);
 
 #ifdef __cplusplus
 }
Signature · 1 line
2.39.5 (Apple Git-154)