Skip to contentSkip to conversation

eal/x86: fix build on systems with WAITPKG support

PATCH Superseded Bruce Richardson 6 participants 12 messages Aug 25, 2023 – Aug 28, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal/x86: fix build on systems with WAITPKG support

When doing a build for a system with WAITPKG support and a modern compiler, we get build errors for the "_umonitor" intrinsic, due to the casting away of the "volatile" on the parameter.

../lib/eal/x86/rte_power_intrinsics.c: In function 'rte_power_monitor':
../lib/eal/x86/rte_power_intrinsics.c:113:22: error: passing argument 1 of '_umonitor' discards 'volatile' qualifier from pointer target type [-Werror=discarded-qualifiers]

  113 |         _umonitor(pmc->addr);
        |                   ~~~^~~~~~

We can avoid this issue by using RTE_PTR_ADD(..., 0) to cast the pointer through "uintptr_t" and thereby remove the volatile without warning. We also ensure comments are correct for each leg of the ifdef..else..endif block.

Fixes 60943c04f3bc ("eal/x86: use intrinsics for power management")
---
 lib/eal/x86/rte_power_intrinsics.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
lib/eal/x86/rte_power_intrinsics.c +6 -6
index 4066d1392e..4f0404bfb8 100644
@@ -103,15 +103,15 @@ rte_power_monitor(const struct rte_power_monitor_cond *pmc,
 	rte_spinlock_lock(&s->lock);
 	s->monitor_addr = pmc->addr;
 
-	/*
-	 * we're using raw byte codes for now as only the newest compiler
-	 * versions support this instruction natively.
-	 */
-
 	/* set address for UMONITOR */
 #if defined(RTE_TOOLCHAIN_MSVC) || defined(__WAITPKG__)
-	_umonitor(pmc->addr);
+	/* use RTE_PTR_ADD to cast away "volatile" when using the intrinsic */
+	_umonitor(RTE_PTR_ADD(pmc->addr, 0));
 #else
+	/*
+	 * we're using raw byte codes for compiler versions which
+	 * don't support this instruction natively.
+	 */
 	asm volatile(".byte 0xf3, 0x0f, 0xae, 0xf7;"
 			:
 			: "D"(pmc->addr));
Signature · 1 line
2.39.2
Acked-by on v1 1/1
#1 lore raw
Quoted 24 lines

From: Bruce Richardson [mailto:[email protected]]
Sent: Friday, 25 August 2023 17.29

When doing a build for a system with WAITPKG support and a modern compiler, we get build errors for the "_umonitor" intrinsic, due to the casting away of the "volatile" on the parameter.

../lib/eal/x86/rte_power_intrinsics.c: In function 'rte_power_monitor':
../lib/eal/x86/rte_power_intrinsics.c:113:22: error: passing argument 1 of '_umonitor' discards 'volatile' qualifier from pointer target type [-Werror=discarded-qualifiers]

  113 |         _umonitor(pmc->addr);
        |                   ~~~^~~~~~

We can avoid this issue by using RTE_PTR_ADD(..., 0) to cast the pointer through "uintptr_t" and thereby remove the volatile without warning. We also ensure comments are correct for each leg of the ifdef..else..endif block.

Fixes 60943c04f3bc ("eal/x86: use intrinsics for power management")

---

[...]

Quoted 3 lines
(quoted diff) +1 -1
-	_umonitor(pmc->addr);
+	/* use RTE_PTR_ADD to cast away "volatile" when using the

intrinsic */

Yes. Having a comment here is good, so people don't wonder why the magic has been added.

lib/eal/x86/rte_power_intrinsics.c rte_power_monitor(const struct rte_power_monitor_cond *pmc, · line 109

Quoted 1 line
lib/eal/x86/rte_power_intrinsics.c +1 -0
+	_umonitor(RTE_PTR_ADD(pmc->addr, 0));

I think that (void *)(uintptr_t)p is more readable than RTE_PTR_ADD(p, 0), but it's a matter of taste.

Regardless,

#2 lore raw
In reply to Bruce Richardson

Hello Bruce,

Quoted 17 lines · Bruce Richardson

When doing a build for a system with WAITPKG support and a modern compiler, we get build errors for the "_umonitor" intrinsic, due to the casting away of the "volatile" on the parameter.

../lib/eal/x86/rte_power_intrinsics.c: In function 'rte_power_monitor':
../lib/eal/x86/rte_power_intrinsics.c:113:22: error: passing argument 1 of '_umonitor' discards 'volatile' qualifier from pointer target type [-Werror=discarded-qualifiers]

  113 |         _umonitor(pmc->addr);
        |                   ~~~^~~~~~

We can avoid this issue by using RTE_PTR_ADD(..., 0) to cast the pointer through "uintptr_t" and thereby remove the volatile without warning. We also ensure comments are correct for each leg of the ifdef..else..endif block.

Quoted 4 lines
Fixes 60943c04f3bc ("eal/x86: use intrinsics for power management")

I'm looking for a system with WAITPKG in the RH lab.. so far, no luck. Do you have a way to force-reproduce this issue? Like some compiler options forcing support?

Signature · 1 line
David Marchand
#3 lore raw
Quoted 22 lines · David Marchand

Hello Bruce,

Quoted 17 lines · Bruce Richardson

When doing a build for a system with WAITPKG support and a modern compiler, we get build errors for the "_umonitor" intrinsic, due to the casting away of the "volatile" on the parameter.

../lib/eal/x86/rte_power_intrinsics.c: In function 'rte_power_monitor':
../lib/eal/x86/rte_power_intrinsics.c:113:22: error: passing argument 1 of '_umonitor' discards 'volatile' qualifier from pointer target type [-Werror=discarded-qualifiers]

  113 |         _umonitor(pmc->addr);
        |                   ~~~^~~~~~

We can avoid this issue by using RTE_PTR_ADD(..., 0) to cast the pointer through "uintptr_t" and thereby remove the volatile without warning. We also ensure comments are correct for each leg of the ifdef..else..endif block.

Quoted 8 lines
Quoted 4 lines
Fixes 60943c04f3bc ("eal/x86: use intrinsics for power management")

I'm looking for a system with WAITPKG in the RH lab.. so far, no luck. Do you have a way to force-reproduce this issue? Like some compiler options forcing support?

Ah.. reproduced with -Dcpu_instruction_set=sapphirerapids

[52/241] Compiling C object lib/librte_eal.a.p/eal_x86_rte_power_intrinsics.c.o ../lib/eal/x86/rte_power_intrinsics.c: In function ‘rte_power_monitor’:
../lib/eal/x86/rte_power_intrinsics.c:113:22: warning: passing argument 1 of ‘_umonitor’ discards ‘volatile’ qualifier from pointer target type [-Wdiscarded-qualifiers]

  113 |         _umonitor(pmc->addr);
      |                   ~~~^~~~~~

In file included from /usr/lib/gcc/x86_64-redhat-linux/11/include/x86gprintrin.h:89,
from
/usr/lib/gcc/x86_64-redhat-linux/11/include/immintrin.h:27,

                 from ../lib/eal/x86/include/rte_rtm.h:8,
                 from ../lib/eal/x86/rte_power_intrinsics.c:7:
/usr/lib/gcc/x86_64-redhat-linux/11/include/waitpkgintrin.h:39:18:
note: expected ‘void *’ but argument is of type ‘volatile void *’
   39 | _umonitor (void *__A)
      |            ~~~~~~^~~

[241/241] Linking target lib/librte_ethdev.so.24.0

Signature · 1 line
David Marchand
Tested-by on v1 1/1
#4 lore raw
In reply to Bruce Richardson
Quoted 9 lines · Bruce Richardson

When doing a build for a system with WAITPKG support and a modern compiler, we get build errors for the "_umonitor" intrinsic, due to the casting away of the "volatile" on the parameter.

../lib/eal/x86/rte_power_intrinsics.c: In function 'rte_power_monitor':
../lib/eal/x86/rte_power_intrinsics.c:113:22: error: passing argument 1 of '_umonitor' discards 'volatile' qualifier from pointer target type [-Werror=discarded-qualifiers]

Quoted 5 lines
  113 |         _umonitor(pmc->addr);
        |                   ~~~^~~~~~

We can avoid this issue by using RTE_PTR_ADD(..., 0) to cast the pointer through "uintptr_t" and thereby remove the volatile without warning.

As Morten, I prefer an explicit cast (keeping your comments) as it seems we are exploiting an implementation detail of RTE_PTR_ADD.

Quoted 2 lines

We also ensure comments are correct for each leg of the ifdef..else..endif block.

Thanks.. I had fixed other places but I have missed this one.

Quoted 25 lines
Fixes 60943c04f3bc ("eal/x86: use intrinsics for power management")
---
 lib/eal/x86/rte_power_intrinsics.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
lib/eal/x86/rte_power_intrinsics.c +0 -6
index 4066d1392e..4f0404bfb8 100644
@@ -103,15 +103,15 @@ rte_power_monitor(const struct rte_power_monitor_cond *pmc,
        rte_spinlock_lock(&s->lock);
        s->monitor_addr = pmc->addr;
 
-       /*
-        * we're using raw byte codes for now as only the newest compiler
-        * versions support this instruction natively.
-        */
-
        /* set address for UMONITOR */
 #if defined(RTE_TOOLCHAIN_MSVC) || defined(__WAITPKG__)
-       _umonitor(pmc->addr);

lib/eal/x86/rte_power_intrinsics.c rte_power_monitor(const struct rte_power_monitor_cond *pmc, · line 117

Quoted 10 lines
lib/eal/x86/rte_power_intrinsics.c +6 -0
+       /* use RTE_PTR_ADD to cast away "volatile" when using the intrinsic */
+       _umonitor(RTE_PTR_ADD(pmc->addr, 0));
 #else
+       /*
+        * we're using raw byte codes for compiler versions which
+        * don't support this instruction natively.
+        */
        asm volatile(".byte 0xf3, 0x0f, 0xae, 0xf7;"
                        :
                        : "D"(pmc->addr));

An additional question, would Intel CI catch such issue?
Or was it caught only because you are blessed with bleeding edge hw? :-)

Signature · 1 line
David Marchand
#5 lore raw
Quoted 14 lines · David Marchand
Quoted 12 lines · Bruce Richardson

When doing a build for a system with WAITPKG support and a modern compiler, we get build errors for the "_umonitor" intrinsic, due to the casting away of the "volatile" on the parameter.

../lib/eal/x86/rte_power_intrinsics.c: In function 'rte_power_monitor':
../lib/eal/x86/rte_power_intrinsics.c:113:22: error: passing argument 1 of '_umonitor' discards 'volatile' qualifier from pointer target type [-Werror=discarded-qualifiers]

  113 |         _umonitor(pmc->addr);
        |                   ~~~^~~~~~
Quoted 6 lines
Quoted 2 lines

We can avoid this issue by using RTE_PTR_ADD(..., 0) to cast the pointer through "uintptr_t" and thereby remove the volatile without warning.

As Morten, I prefer an explicit cast (keeping your comments) as it seems we are exploiting an implementation detail of RTE_PTR_ADD.

Ok, I'll do a respin with explicit cast.

Quoted 45 lines
Quoted 3 lines

We also ensure comments are correct for each leg of the ifdef..else..endif block.

Thanks.. I had fixed other places but I have missed this one.

Quoted 25 lines
Fixes 60943c04f3bc ("eal/x86: use intrinsics for power management")
---
 lib/eal/x86/rte_power_intrinsics.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
lib/eal/x86/rte_power_intrinsics.c +0 -6
index 4066d1392e..4f0404bfb8 100644
@@ -103,15 +103,15 @@ rte_power_monitor(const struct rte_power_monitor_cond *pmc,
        rte_spinlock_lock(&s->lock);
        s->monitor_addr = pmc->addr;
 
-       /*
-        * we're using raw byte codes for now as only the newest compiler
-        * versions support this instruction natively.
-        */
-
        /* set address for UMONITOR */
 #if defined(RTE_TOOLCHAIN_MSVC) || defined(__WAITPKG__)
-       _umonitor(pmc->addr);

lib/eal/x86/rte_power_intrinsics.c rte_power_monitor(const struct rte_power_monitor_cond *pmc, · line 117

Quoted 10 lines
lib/eal/x86/rte_power_intrinsics.c +6 -0
+       /* use RTE_PTR_ADD to cast away "volatile" when using the intrinsic */
+       _umonitor(RTE_PTR_ADD(pmc->addr, 0));
 #else
+       /*
+        * we're using raw byte codes for compiler versions which
+        * don't support this instruction natively.
+        */
        asm volatile(".byte 0xf3, 0x0f, 0xae, 0xf7;"
                        :
                        : "D"(pmc->addr));
Quoted 3 lines

An additional question, would Intel CI catch such issue?
Or was it caught only because you are blessed with bleeding edge hw? :-)

Not sure. I would hope so, though.

/Bruce

#6 lore raw

For humor
#define RTE_CASTAWAY(x) ((void *)(uinptr_t)(x))

Quoted 81 lines · Bruce Richardson
Quoted 7 lines · David Marchand
Quoted 5 lines · Bruce Richardson

When doing a build for a system with WAITPKG support and a modern compiler, we get build errors for the "_umonitor" intrinsic, due to the casting away of the "volatile" on the parameter.

Quoted 8 lines
Quoted 8 lines

../lib/eal/x86/rte_power_intrinsics.c: In function 'rte_power_monitor':
../lib/eal/x86/rte_power_intrinsics.c:113:22: error: passing argument 1 of '_umonitor' discards 'volatile' qualifier from pointer target type [-Werror=discarded-qualifiers]

  113 |         _umonitor(pmc->addr);
        |                   ~~~^~~~~~

We can avoid this issue by using RTE_PTR_ADD(..., 0) to cast the

pointer

Quoted 5 lines
Quoted 1 line

through "uintptr_t" and thereby remove the volatile without warning.

As Morten, I prefer an explicit cast (keeping your comments) as it seems we are exploiting an implementation detail of RTE_PTR_ADD.

Ok, I'll do a respin with explicit cast.

Quoted 11 lines
Quoted 3 lines

We also ensure comments are correct for each leg of the ifdef..else..endif block.

Thanks.. I had fixed other places but I have missed this one.

Quoted 4 lines
Fixes 60943c04f3bc ("eal/x86: use intrinsics for power management")
Quoted 6 lines

Review

Quoted 6 lines
---
 lib/eal/x86/rte_power_intrinsics.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/lib/eal/x86/rte_power_intrinsics.c

Quoted 4 lines

lib/eal/x86/rte_power_intrinsics.c rte_power_monitor(const struct

Quoted 4 lines

index 4066d1392e..4f0404bfb8 100644

lib/eal/x86/rte_power_intrinsics.c +0 -0
@@ -103,15 +103,15 @@ rte_power_monitor(const struct

rte_power_monitor_cond *pmc,

Quoted 5 lines

lib/eal/x86/rte_power_intrinsics.c rte_power_monitor(const struct rte_power_monitor_cond *pmc,

Quoted 5 lines
lib/eal/x86/rte_power_intrinsics.c +0 -2
        rte_spinlock_lock(&s->lock);
        s->monitor_addr = pmc->addr;
 
-       /*
-        * we're using raw byte codes for now as only the newest

compiler

Quoted 7 lines

lib/eal/x86/rte_power_intrinsics.c rte_power_monitor(const struct rte_power_monitor_cond *pmc,

Quoted 7 lines
lib/eal/x86/rte_power_intrinsics.c +1 -4
-        * versions support this instruction natively.
-        */
-
        /* set address for UMONITOR */
 #if defined(RTE_TOOLCHAIN_MSVC) || defined(__WAITPKG__)
-       _umonitor(pmc->addr);
+       /* use RTE_PTR_ADD to cast away "volatile" when using the

intrinsic */

Quoted 10 lines

lib/eal/x86/rte_power_intrinsics.c rte_power_monitor(const struct rte_power_monitor_cond *pmc, · line 117

Quoted 9 lines
lib/eal/x86/rte_power_intrinsics.c +5 -0
+       _umonitor(RTE_PTR_ADD(pmc->addr, 0));
 #else
+       /*
+        * we're using raw byte codes for compiler versions which
+        * don't support this instruction natively.
+        */
        asm volatile(".byte 0xf3, 0x0f, 0xae, 0xf7;"
                        :
                        : "D"(pmc->addr));
Quoted 5 lines

An additional question, would Intel CI catch such issue?
Or was it caught only because you are blessed with bleeding edge hw? :-)

Not sure. I would hope so, though.

/Bruce

#7 lore raw
Quoted 2 lines · Stephen Hemminger
   For humor
   #define RTE_CASTAWAY(x) ((void *)(uinptr_t)(x))

Yes, actually thought about that. Was also wondering about making it an inline function rather than macro, to ensure its only used on pointers, and to make clear what is being cast away:

static inline void *
rte_cast_no_volatile(volatile void *x)

{
	return (void *)(uintptr_t)(x);
}

and similarly we could do a rte_cast_no_const(const void *x).

WDYT?

/Bruce

#8 lore raw
Quoted 8 lines · Bruce Richardson
Quoted 2 lines · Stephen Hemminger
   For humor
   #define RTE_CASTAWAY(x) ((void *)(uinptr_t)(x))

Yes, actually thought about that. Was also wondering about making it an inline function rather than macro, to ensure its only used on pointers, and to make clear what is being cast away:

Quoted 6 lines

static inline void *
rte_cast_no_volatile(volatile void *x)

{
	return (void *)(uintptr_t)(x);
}

Not as good, without 'castaway' in the API/macro name :)

Quoted 5 lines

and similarly we could do a rte_cast_no_const(const void *x).

WDYT?

/Bruce

#9 lore raw
In reply to Bruce Richardson
Quoted 10 lines · Bruce Richardson
Quoted 2 lines · Stephen Hemminger
   For humor
   #define RTE_CASTAWAY(x) ((void *)(uinptr_t)(x))

Yes, actually thought about that. Was also wondering about making it an inline function rather than macro, to ensure its only used on pointers, and to make clear what is being cast away:

static inline void *
rte_cast_no_volatile(volatile void *x)

Quoted 7 lines
{
	return (void *)(uintptr_t)(x);
}

and similarly we could do a rte_cast_no_const(const void *x).

WDYT?

since we're introducing humor! now announcing dpdk requires C23 comliant compiler for typeof_unqual! https://en.cppreference.com/w/c/language/typeof

i like the idea, i like it being inline function you could use the name 'unqual' if you wanted to mimic a name similar to standard C typeof.

it could be 1 function that strips all qualifications or N that strip specific qualifications, const, volatile and _Atomic not sure which is best.

ty

Quoted 2 lines

/Bruce

#10 lore raw

PATCH v2 1/1 eal/x86: fix build on systems with WAITPKG support

In reply to Bruce Richardson

When doing a build for a system with WAITPKG support and a modern compiler, we get build errors for the "_umonitor" intrinsic, due to the casting away of the "volatile" on the parameter.

../lib/eal/x86/rte_power_intrinsics.c: In function 'rte_power_monitor':
../lib/eal/x86/rte_power_intrinsics.c:113:22: error: passing argument 1 of '_umonitor' discards 'volatile' qualifier from pointer target type [-Werror=discarded-qualifiers]

  113 |         _umonitor(pmc->addr);
        |                   ~~~^~~~~~

We can avoid this issue by casting through "uintptr_t" and thereby remove the volatile without warning. We also ensure comments are correct for each leg of the ifdef..else..endif block.

Fixes 60943c04f3bc ("eal/x86: use intrinsics for power management")
---
 lib/eal/x86/rte_power_intrinsics.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
lib/eal/x86/rte_power_intrinsics.c +6 -6
index 4066d1392e..97202e42fc 100644
@@ -103,15 +103,15 @@ rte_power_monitor(const struct rte_power_monitor_cond *pmc,
 	rte_spinlock_lock(&s->lock);
 	s->monitor_addr = pmc->addr;
 
-	/*
-	 * we're using raw byte codes for now as only the newest compiler
-	 * versions support this instruction natively.
-	 */
-
 	/* set address for UMONITOR */
 #if defined(RTE_TOOLCHAIN_MSVC) || defined(__WAITPKG__)
-	_umonitor(pmc->addr);
+	/* cast away "volatile" when using the intrinsic */
+	_umonitor((void *)(uintptr_t)pmc->addr);
 #else
+	/*
+	 * we're using raw byte codes for compiler versions which
+	 * don't support this instruction natively.
+	 */
 	asm volatile(".byte 0xf3, 0x0f, 0xae, 0xf7;"
 			:
 			: "D"(pmc->addr));
Signature · 1 line
2.39.2
#11 lore raw
Quoted 16 lines · Bruce Richardson

When doing a build for a system with WAITPKG support and a modern compiler, we get build errors for the "_umonitor" intrinsic, due to the casting away of the "volatile" on the parameter.

../lib/eal/x86/rte_power_intrinsics.c: In function 'rte_power_monitor':
../lib/eal/x86/rte_power_intrinsics.c:113:22: error: passing argument 1 of '_umonitor' discards 'volatile' qualifier from pointer target type [-Werror=discarded-qualifiers]

  113 |         _umonitor(pmc->addr);
        |                   ~~~^~~~~~

We can avoid this issue by casting through "uintptr_t" and thereby remove the volatile without warning. We also ensure comments are correct for each leg of the ifdef..else..endif block.

Quoted 6 lines
Fixes 60943c04f3bc ("eal/x86: use intrinsics for power management")

Applied to fix build on the main branch, thanks.

We can look at the casting helper as a followup.

Signature · 1 line
David Marchand