Skip to contentSkip to conversation

eal: Pointer alignment check improvements

PATCH Superseded Morten Brørup 3 participants 11 messages Sep 21, 2022 – Sep 30, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal: Pointer alignment check improvements

Checking a const pointer for alignment would emit a warning about the const qualifier being discarded.

No need to calculate the aligned pointer; just check the last bits of the pointer.

---
 lib/eal/include/rte_common.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
lib/eal/include/rte_common.h +3 -3
index 2e22c1b955..5c20d3a81a 100644
@@ -403,10 +403,10 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  * @return
  *   True(1) where the pointer is correctly aligned, false(0) otherwise
  */
-static inline int
-rte_is_aligned(void *ptr, unsigned align)
+static __rte_always_inline __attribute__ ((const)) int
+rte_is_aligned(const void * const __rte_restrict ptr, unsigned int align)
 {
-	return RTE_PTR_ALIGN(ptr, align) == ptr;
+	return ((uintptr_t)ptr & (align - 1)) == 0;
 }
 
 /*********** Macros for compile type checks ********/
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/1 eal: Pointer alignment check improvements

Checking a const pointer for alignment would emit a warning about the const qualifier being discarded.

No need to calculate the aligned pointer; just check the last bits of the pointer.

v2:
- Remove compiler attribute ((const)) from function; it was a coding style issue.

---
 lib/eal/include/rte_common.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/eal/include/rte_common.h +2 -2
index 2e22c1b955..ed81e0db0a 100644
@@ -404,9 +404,9 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  *   True(1) where the pointer is correctly aligned, false(0) otherwise
  */
 static inline int
-rte_is_aligned(void *ptr, unsigned align)
+rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
 {
-	return RTE_PTR_ALIGN(ptr, align) == ptr;
+	return ((uintptr_t)ptr & (align - 1)) == 0;
 }
 
 /*********** Macros for compile type checks ********/
Signature · 1 line
2.17.1
#2 lore raw
Quoted 20 lines · Morten Brørup

Checking a const pointer for alignment would emit a warning about the const qualifier being discarded.

No need to calculate the aligned pointer; just check the last bits of the pointer.

v2:
- Remove compiler attribute ((const)) from function; it was a coding style issue.

---
 lib/eal/include/rte_common.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/eal/include/rte_common.h +0 -0
index 2e22c1b955..ed81e0db0a 100644

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

Quoted 8 lines
lib/eal/include/rte_common.h +2 -2
@@ -404,9 +404,9 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  *   True(1) where the pointer is correctly aligned, false(0) otherwise
  */
 static inline int
-rte_is_aligned(void *ptr, unsigned align)
+rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
 {
-	return RTE_PTR_ALIGN(ptr, align) == ptr;
+	return ((uintptr_t)ptr & (align - 1)) == 0;

Are we confident that in future, or using come compiler settings, we won't get an error due to using "uintptr_t" rather than "const uintptr_t" in the cast? I would put a const in there myself, just to be safe.

A further point, only-semi-related to this patch, which is fine as-is:
looking at the code for the various macros in rte_common.h:
* The various macros for working on pointers can can probably be converted to functions, since they don't need to work with variable-sized types.
* We can then see about properly ensuring those inline functions are const-correct.

/Bruce

#3 lore raw
Quoted 34 lines · Bruce Richardson
Quoted 20 lines · Morten Brørup

Checking a const pointer for alignment would emit a warning about the const qualifier being discarded.

No need to calculate the aligned pointer; just check the last bits of the pointer.

v2:
- Remove compiler attribute ((const)) from function; it was a coding style issue.

---
 lib/eal/include/rte_common.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/eal/include/rte_common.h +0 -0
index 2e22c1b955..ed81e0db0a 100644

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

Quoted 8 lines
lib/eal/include/rte_common.h +2 -2
@@ -404,9 +404,9 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  *   True(1) where the pointer is correctly aligned, false(0) otherwise
  */
 static inline int
-rte_is_aligned(void *ptr, unsigned align)
+rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
 {
-	return RTE_PTR_ALIGN(ptr, align) == ptr;
+	return ((uintptr_t)ptr & (align - 1)) == 0;

Are we confident that in future, or using come compiler settings, we won't get an error due to using "uintptr_t" rather than "const uintptr_t" in the cast? I would put a const in there myself, just to be safe.

Quoted 7 lines

A further point, only-semi-related to this patch, which is fine as-is:
looking at the code for the various macros in rte_common.h:
* The various macros for working on pointers can can probably be converted to functions, since they don't need to work with variable-sized types.
* We can then see about properly ensuring those inline functions are const-correct.

Actually, on further investigation in trying this, it appears that the macros are used in a number of places with integer data too, despite the "PTR" in the name, so things are best alone for now, I think.

/Bruce

#4 lore raw
Quoted 36 lines

From: Bruce Richardson [mailto:[email protected]]
Sent: Thursday, 22 September 2022 13.59

Quoted 2 lines · Bruce Richardson
Quoted 1 line · Morten Brørup

Checking a const pointer for alignment would emit a warning about

the

Quoted 3 lines
Quoted 3 lines

const qualifier being discarded.

No need to calculate the aligned pointer; just check the last bits

of the

Quoted 6 lines
Quoted 6 lines

pointer.

v2:
- Remove compiler attribute ((const)) from function; it was a coding style issue.

Quoted 6 lines

Review

Quoted 6 lines
---
 lib/eal/include/rte_common.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/lib/eal/include/rte_common.h

Quoted 4 lines

lib/eal/include/rte_common.h static void

Quoted 4 lines

index 2e22c1b955..ed81e0db0a 100644

lib/eal/include/rte_common.h +0 -0
@@ -404,9 +404,9 @@ static void

__attribute__((destructor(RTE_PRIO(prio)), used)) func(void)

Quoted 1 line

lib/eal/include/rte_common.h static void · line 404

Quoted 1 line
lib/eal/include/rte_common.h +0 -0
  *   True(1) where the pointer is correctly aligned, false(0)

otherwise

Quoted 4 lines

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

Quoted 4 lines
lib/eal/include/rte_common.h +1 -1
  */
 static inline int
-rte_is_aligned(void *ptr, unsigned align)
+rte_is_aligned(const void * const __rte_restrict ptr, const

unsigned int align)

Quoted 9 lines
Quoted 5 lines

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

Quoted 3 lines
lib/eal/include/rte_common.h +1 -1
 {
-	return RTE_PTR_ALIGN(ptr, align) == ptr;
+	return ((uintptr_t)ptr & (align - 1)) == 0;

Are we confident that in future, or using come compiler settings, we

won't

Quoted 1 line

get an error due to using "uintptr_t" rather than "const uintptr_t"

in the

Quoted 1 line

cast? I would put a const in there myself, just to be safe.

Good idea.

Quoted 10 lines
Quoted 2 lines

A further point, only-semi-related to this patch, which is fine as-

is:

Quoted 2 lines

looking at the code for the various macros in rte_common.h:
* The various macros for working on pointers can can probably be

converted

Quoted 1 line

to functions, since they don't need to work with variable-sized

types.

Quoted 2 lines

* We can then see about properly ensuring those inline functions are const-correct.

The problem with const in a function parameter is the ripple effect: all the underlying functions must also use const.

I generally prefer using const where possible, but the ripple effect often makes it difficult.

Quoted 5 lines
Quoted 1 line

Actually, on further investigation in trying this, it appears that the macros are used in a number of places with integer data too, despite the
"PTR" in the name, so things are best alone for now, I think.

Even the macros that also exist without "PTR" in the name? (Example, please.)

Instead of providing multiple macros for essentially doing the same thing to different types, we could use __builtin_choose_expr [1] to support a variety of types in the macros. This built-in can be used as a workaround for not being able to use C++, where the same function name can be used by multiple functions with different parameter types.

[1]: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

I was also wondering why the rte_is_aligned function doesn't have "ptr" in its name, because it cannot be used for integer types. Changing rte_is_aligned to a macro using __builtin_choose_expr could solve this. But I don't think such a patch will be popular, so I chose to stick with the simple fix.

-Morten

#5 lore raw

PATCH v3 1/1 eal: Pointer alignment check improvements

Checking a const pointer for alignment would emit a warning about the const qualifier being discarded.

No need to calculate the aligned pointer; just check the last bits of the pointer.

v3:
- Make the uintptr_t const to avoid potential future warnings. (Bruce) v2:
- Remove compiler attribute ((const)) from function;
it was a coding style issue.

---
 lib/eal/include/rte_common.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/eal/include/rte_common.h +2 -2
index 2e22c1b955..ed81e0db0a 100644
@@ -404,9 +404,9 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  *   True(1) where the pointer is correctly aligned, false(0) otherwise
  */
 static inline int
-rte_is_aligned(void *ptr, unsigned align)
+rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
 {
-	return RTE_PTR_ALIGN(ptr, align) == ptr;
+	return ((const uintptr_t)ptr & (align - 1)) == 0;
 }
 
 /*********** Macros for compile type checks ********/
Signature · 1 line
2.17.1
Acked-by on v3 1/1
#6 lore raw
Quoted 6 lines · Morten Brørup

Checking a const pointer for alignment would emit a warning about the const qualifier being discarded.

No need to calculate the aligned pointer; just check the last bits of the pointer.

Quoted 7 lines

v3:
- Make the uintptr_t const to avoid potential future warnings. (Bruce) v2:
- Remove compiler attribute ((const)) from function;
it was a coding style issue.

Or perhaps it should be "Const-acked-by: ... " :-)

#7 lore raw
Quoted 18 lines

From: Bruce Richardson [mailto:[email protected]]
Sent: Thursday, 22 September 2022 15.39

Quoted 4 lines · Morten Brørup

Checking a const pointer for alignment would emit a warning about the const qualifier being discarded.

No need to calculate the aligned pointer; just check the last bits of

the

Quoted 4 lines

pointer.

v3:
- Make the uintptr_t const to avoid potential future warnings.

(Bruce)

Quoted 4 lines

v2:
- Remove compiler attribute ((const)) from function; it was a coding style issue.

Quoted 5 lines
Quoted 1 line

Or perhaps it should be "Const-acked-by: ... " :-)

Unfortunately not as const as expected by both of us...

The v3 build fails at github [1] with:

../lib/eal/include/rte_common.h: In function 'int rte_is_aligned(const void*, unsigned int)':
../lib/eal/include/rte_common.h:409:27: error: type qualifiers ignored on cast result type [-Werror=ignored-qualifiers]
409 | return ((const uintptr_t)ptr & (align - 1)) == 0;
| ^~~

[1] http://mails.dpdk.org/archives/test-report/2022-September/308604.html

I don't understand what the problem is, so my solution is omitting the const, i.e. rolling back to v2, which doesn't fail building. Unless you can suggest a better solution, Bruce?

I have changed v2 status in Patchwork back to New and v3 to Superseded.

-Morten

#8 lore raw
Quoted 33 lines · Morten Brørup
Quoted 18 lines

From: Bruce Richardson [mailto:[email protected]]
Sent: Thursday, 22 September 2022 15.39

Quoted 4 lines · Morten Brørup

Checking a const pointer for alignment would emit a warning about the const qualifier being discarded.

No need to calculate the aligned pointer; just check the last bits of

the

Quoted 4 lines

pointer.

v3:
- Make the uintptr_t const to avoid potential future warnings.

(Bruce)

Quoted 4 lines

v2:
- Remove compiler attribute ((const)) from function; it was a coding style issue.

Quoted 5 lines
Quoted 1 line

Or perhaps it should be "Const-acked-by: ... " :-)

Unfortunately not as const as expected by both of us...

The v3 build fails at github [1] with:

../lib/eal/include/rte_common.h: In function 'int rte_is_aligned(const void*, unsigned int)':
../lib/eal/include/rte_common.h:409:27: error: type qualifiers ignored on cast result type [-Werror=ignored-qualifiers]
409 | return ((const uintptr_t)ptr & (align - 1)) == 0;
| ^~~

Quoted 6 lines

[1] http://mails.dpdk.org/archives/test-report/2022-September/308604.html

I don't understand what the problem is, so my solution is omitting the const, i.e. rolling back to v2, which doesn't fail building. Unless you can suggest a better solution, Bruce?

I have changed v2 status in Patchwork back to New and v3 to Superseded.

That is fine. I'll ack v2 so.

Acked-by on v2 1/1
#9 lore raw
In reply to Morten Brørup
Quoted 20 lines · Morten Brørup

Checking a const pointer for alignment would emit a warning about the const qualifier being discarded.

No need to calculate the aligned pointer; just check the last bits of the pointer.

v2:
- Remove compiler attribute ((const)) from function; it was a coding style issue.

---
 lib/eal/include/rte_common.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/eal/include/rte_common.h +0 -0
index 2e22c1b955..ed81e0db0a 100644

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

Quoted 10 lines
lib/eal/include/rte_common.h +2 -2
@@ -404,9 +404,9 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
  *   True(1) where the pointer is correctly aligned, false(0) otherwise
  */
 static inline int
-rte_is_aligned(void *ptr, unsigned align)
+rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
 {
-	return RTE_PTR_ALIGN(ptr, align) == ptr;
+	return ((uintptr_t)ptr & (align - 1)) == 0;
 }