spinlock: remove volatile qualifier
PATCH 1/1 spinlock: remove volatile qualifier
The user and count fields of rte_spinlock_recursive_t do not need the volatile qualifier
because they are only accessed by the thread holding the lock, which already provides the necessary memory ordering.
Removing volatile aligns with a C++20 deprecation for increment and decrement of volatile variables.
This issue was seen with GCC 16 which changes the default C++ version from -std=gnu++17 to -std=gnu++20.
--- lib/eal/include/generic/rte_spinlock.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/eal/include/generic/rte_spinlock.h +2 -2
@@ -197,8 +197,8 @@ rte_spinlock_trylock_tm(rte_spinlock_t *sl)*/ typedef struct {rte_spinlock_t sl;/**< the actual spinlock */ -volatile int user;/**< core id using lock, -1 for unused */ -volatile int count;/**< count of time this lock has been called */ +int user;/**< core id using lock, -1 for unused */ +int count;/**< count of time this lock has been called */ }rte_spinlock_recursive_t ;/**
Signature · 1 line
2.54.0
04/05/2026 10:37, Thomas Monjalon:
Quoted 8 lines
The user and count fields of
rte_spinlock_recursive_tdo not need the volatile qualifier
because they are only accessed by the thread holding the lock, which already provides the necessary memory ordering.Removing volatile aligns with a C++20 deprecation for increment and decrement of volatile variables.
Quoted 7 lines
This issue was seen with GCC 16 which changes the default C++ version from -std=gnu++17 to -std=gnu++20.
Fixes af75078fece3 ("first public release")Signed-off-by Thomas Monjalon <[email protected]>
I've just found this has been discussed 4 years ago:
https://inbox.dpdk.org/dev/[email protected]
Stephen had found some comments issues that I will fix in another patch.
PATCH 1/1 spinlock: fix API comments
This is not a read-write lock.
The user field stores the thread ID, not the core ID.
The implementation is architecture-specific in some cases only.
--- lib/eal/include/generic/rte_spinlock.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
lib/eal/include/generic/rte_spinlock.h +9 -5
@@ -7,12 +7,16 @@/** * @file +* DPDK spinlocks * -* RTE Spinlocks +* This is an API for spinlocks. +* This kind of lock simply waits in a loop +* repeatedly checking until the lock becomes available. * -* This file defines an API for read-write locks, which are implemented -* in an architecture-specific way. This kind of lock simply waits in -* a loop repeatedly checking until the lock becomes available. +* Some functions may have an architecture-specific implementation +* if RTE_FORCE_INTRINSICS is disabled. +* The hardware transactional memory (lock elision) functions have _tm suffix +* and are implemented in architecture-specific files. * * All locks must be initialised before use, and only initialised once. */
@@ -197,7 +201,7 @@ rte_spinlock_trylock_tm(rte_spinlock_t *sl)*/ typedef struct {rte_spinlock_t sl;/**< the actual spinlock */ -int user;/**< core id using lock, -1 for unused */ +int user;/**< thread id using lock, -1 for unused */ int count;/**< count of time this lock has been called */ }rte_spinlock_recursive_t ;
Signature · 1 line
2.54.0
Re: [PATCH] spinlock: fix API comments
04/05/2026 11:12, Thomas Monjalon:
Quoted 8 lines
This is not a read-write lock.
The user field stores the thread ID, not the core ID.
The implementation is architecture-specific in some cases only.
Reported-by Stephen Hemminger <[email protected]>Signed-off-by Thomas Monjalon <[email protected]>
04/05/2026 11:06, Thomas Monjalon:
Quoted 15 lines
04/05/2026 10:37, Thomas Monjalon:
Quoted 14 lines
The user and count fields of
rte_spinlock_recursive_tdo not need the volatile qualifier
because they are only accessed by the thread holding the lock, which already provides the necessary memory ordering.Removing volatile aligns with a C++20 deprecation for increment and decrement of volatile variables.
This issue was seen with GCC 16 which changes the default C++ version from -std=gnu++17 to -std=gnu++20.
Fixes af75078fece3 ("first public release")
Quoted 6 lines
Quoted 1 line
Signed-off-by Thomas Monjalon <[email protected]>I've just found this has been discussed 4 years ago:
https://inbox.dpdk.org/dev/[email protected]Stephen had found some comments issues that I will fix in another patch.
Adding the compiler messages:
rte_spinlock.h:241:14: error: '++' expression of 'volatile'-qualified type is deprecated [-Werror=volatile] rte_spinlock.h:252:21: error: '--' expression of 'volatile'-qualified type is deprecated [-Werror=volatile] rte_spinlock.h:278:14: error: '++' expression of 'volatile'-qualified type is deprecated [-Werror=volatile]
lib/eal/include/generic/rte_spinlock.h rte_spinlock_trylock_tm(rte_spinlock_t *sl) · line 201
Quoted 12 lines · On Mon, 4 May 2026 10:37:14 +0200 Thomas Monjalon <[email protected]> wrote:
lib/eal/include/generic/rte_spinlock.h +2
-2@@ -197,8 +197,8 @@ rte_spinlock_trylock_tm(rte_spinlock_t *sl)*/ typedef struct { rte_spinlock_t sl; /**< the actual spinlock */ - volatile int user; /**< core id using lock, -1 for unused */ - volatile int count; /**< count of time this lock has been called */ + int user; /**< core id using lock, -1 for unused */ + int count; /**< count of time this lock has been called */
It might make sense to use unsigned for count, and sized types. I.e do you really need 32 bit values?
Only in tree use of recursive spinlock in vdev code.
PS: I wonder if we really need to keep the transactional memory versions of stuff. I
04/05/2026 19:02, Stephen Hemminger:
Quoted 18 lines
lib/eal/include/generic/rte_spinlock.h rte_spinlock_trylock_tm(rte_spinlock_t *sl) · line 201
Quoted 12 lines · On Mon, 4 May 2026 10:37:14 +0200 Thomas Monjalon <[email protected]> wrote:
lib/eal/include/generic/rte_spinlock.h +2
-2@@ -197,8 +197,8 @@ rte_spinlock_trylock_tm(rte_spinlock_t *sl)*/ typedef struct { rte_spinlock_t sl; /**< the actual spinlock */ - volatile int user; /**< core id using lock, -1 for unused */ - volatile int count; /**< count of time this lock has been called */ + int user; /**< core id using lock, -1 for unused */ + int count; /**< count of time this lock has been called */It might make sense to use unsigned for count, and sized types. I.e do you really need 32 bit values?
Such a change would need to be evaluated in a performance benchmark.
Quoted 3 lines
Only in tree use of recursive spinlock in vdev code.
PS: I wonder if we really need to keep the transactional memory versions of stuff.
I don't know, it's part of the API and has no maintenance cost.
Quoted 3 lines · On Tue, 05 May 2026 09:01:43 +0200 Thomas Monjalon <[email protected]> wrote:
Quoted 1 line
PS: I wonder if we really need to keep the transactional memory versions of stuff.
I don't know, it's part of the API and has no maintenance cost.
It is not tested, only worked on x86 and most of the CPU's and tooling dropped support for transactional memory years ago. It was a failed idea.
PATCH v2 1/2 spinlock: remove volatile qualifier
In reply to Thomas MonjalonWhen compiling with C++20 standard requirement (default in GCC 16), the increment and decrement of volatile variables are rejected:
rte_spinlock.h:241:14: error:
'++' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:252:21: error:
'--' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:278:14: error:
'++' expression of 'volatile'-qualified type is deprecated
The count field of rte_spinlock_recursive_t
does not need the volatile qualifier
because it is only accessed by the thread holding the lock, which already provides the necessary memory ordering.
The user field can be accessed outside of the lock, so it must handled as a C11 atomic variable.
---
v1: drop volatile keyword
v2: make user an atomic variable
--- lib/eal/include/generic/rte_spinlock.h | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)
lib/eal/include/generic/rte_spinlock.h +8 -9
@@ -197,8 +197,8 @@ rte_spinlock_trylock_tm(rte_spinlock_t *sl)*/ typedef struct {rte_spinlock_t sl;/**< the actual spinlock */ -volatile int user;/**< core id using lock, -1 for unused */ -volatile int count;/**< count of time this lock has been called */ +RTE_ATOMIC (int ) user;/**< core id using lock, -1 for unused */ +int count;/**< count of time this lock has been called */ }rte_spinlock_recursive_t ;/**
@@ -215,7 +215,7 @@ typedef struct {static inline void rte_spinlock_recursive_init (rte_spinlock_recursive_t *slr) {rte_spinlock_init (&slr->sl); - slr->user =-1 ; +rte_atomic_store_explicit (&slr->user,-1 , rte_memory_order_relaxed); slr->count =0 ; }
@@ -230,9 +230,9 @@ static inline void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr) {int id =rte_gettid (); -if (slr->user != id) { +if (rte_atomic_load_explicit (&slr->user, rte_memory_order_relaxed) != id) {rte_spinlock_lock (&slr->sl); - slr->user = id; +rte_atomic_store_explicit (&slr->user, id, rte_memory_order_relaxed); } slr->count++; }
@@ -246,10 +246,9 @@ static inline void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr) __rte_no_thread_safety_analysis {if (--(slr->count) ==0 ) { - slr->user =-1 ; +rte_atomic_store_explicit (&slr->user,-1 , rte_memory_order_relaxed);rte_spinlock_unlock (&slr->sl); } - }/**
@@ -266,10 +265,10 @@ static inline int rte_spinlock_recursive_trylock(rte_spinlock_recursive_t *slr) {int id =rte_gettid (); -if (slr->user != id) { +if (rte_atomic_load_explicit (&slr->user, rte_memory_order_relaxed) != id) {if (rte_spinlock_trylock (&slr->sl) ==0 )return 0 ; - slr->user = id; +rte_atomic_store_explicit (&slr->user, id, rte_memory_order_relaxed); } slr->count++;return 1 ;
Signature · 1 line
2.54.0
PATCH v2 2/2 spinlock: fix API comments
This is not a read-write lock.
The user field stores the thread ID, not the core ID since a change in DPDK 2.0.0.
The implementation is architecture-specific in some cases only.
--- lib/eal/include/generic/rte_spinlock.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
lib/eal/include/generic/rte_spinlock.h +9 -5
@@ -7,12 +7,16 @@/** * @file +* DPDK spinlocks * -* RTE Spinlocks +* This is an API for spinlocks. +* This kind of lock simply waits in a loop +* repeatedly checking until the lock becomes available. * -* This file defines an API for read-write locks, which are implemented -* in an architecture-specific way. This kind of lock simply waits in -* a loop repeatedly checking until the lock becomes available. +* Some functions may have an architecture-specific implementation +* if RTE_FORCE_INTRINSICS is disabled. +* The hardware transactional memory (lock elision) functions have _tm suffix +* and are implemented in architecture-specific files. * * All locks must be initialised before use, and only initialised once. */
@@ -197,7 +201,7 @@ rte_spinlock_trylock_tm(rte_spinlock_t *sl)*/ typedef struct {rte_spinlock_t sl;/**< the actual spinlock */ -RTE_ATOMIC (int ) user;/**< core id using lock, -1 for unused */ +RTE_ATOMIC (int ) user;/**< thread id using lock, -1 for unused */ int count;/**< count of time this lock has been called */ }rte_spinlock_recursive_t ;
Signature · 1 line
2.54.0
Hey Thomas,
Thomas Monjalon, May 18, 2026 at 17:07:
Quoted 54 lines
When compiling with C++20 standard requirement (default in GCC 16), the increment and decrement of volatile variables are rejected:
rte_spinlock.h:241:14: error:
'++' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:252:21: error:
'--' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:278:14: error:
'++' expression of 'volatile'-qualified type is deprecatedThe count field of
rte_spinlock_recursive_t
does not need the volatile qualifier
because it is only accessed by the thread holding the lock, which already provides the necessary memory ordering.The user field can be accessed outside of the lock, so it must handled as a C11 atomic variable.
Fixes af75078fece3 ("first public release")Signed-off-by Thomas Monjalon <[email protected]>---
v1: drop volatile keyword
v2: make user an atomic variable--- lib/eal/include/generic/rte_spinlock.h | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)lib/eal/include/generic/rte_spinlock.h +3
-3@@ -197,8 +197,8 @@ rte_spinlock_trylock_tm(rte_spinlock_t *sl)*/ typedef struct {rte_spinlock_t sl;/**< the actual spinlock */ -volatile int user;/**< core id using lock, -1 for unused */ -volatile int count;/**< count of time this lock has been called */ +RTE_ATOMIC (int ) user;/**< core id using lock, -1 for unused */ +int count;/**< count of time this lock has been called */ }rte_spinlock_recursive_t ;/** @@ -215,7 +215,7 @@ typedef struct {static inline void rte_spinlock_recursive_init (rte_spinlock_recursive_t *slr) {rte_spinlock_init (&slr->sl); - slr->user =-1 ; +rte_atomic_store_explicit (&slr->user,-1 , rte_memory_order_relaxed); slr->count =0 ; }
lib/eal/include/generic/rte_spinlock.h static inline void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr) · line 233
Quoted 5 lines
lib/eal/include/generic/rte_spinlock.h +1
-1@@ -230,9 +230,9 @@ static inline void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr) {int id =rte_gettid (); -if (slr->user != id) { +if (rte_atomic_load_explicit (&slr->user, rte_memory_order_relaxed) != id) {
This needs to be rte_memory_order_acquire
lib/eal/include/generic/rte_spinlock.h static inline void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr) · line 235
Quoted 3 lines
lib/eal/include/generic/rte_spinlock.h +1
-1rte_spinlock_lock (&slr->sl); - slr->user = id; +rte_atomic_store_explicit (&slr->user, id, rte_memory_order_relaxed);
And rte_memory_order_release
lib/eal/include/generic/rte_spinlock.h static inline void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr) · line 248
Quoted 7 lines
lib/eal/include/generic/rte_spinlock.h +0
-0} slr->count++; }@@ -246,10 +246,9 @@ static inline void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr) __rte_no_thread_safety_analysis {if (--(slr->count) ==0 ) {
This code is completely broken. Any thread can unlock without any check.
I think it should be:
lib/eal/include/generic/rte_spinlock.h +10 -4
@@ -245,11 +247,17 @@ static inline void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr)static inline void rte_spinlock_recursive_unlock (rte_spinlock_recursive_t *slr) __rte_no_thread_safety_analysis { -if (--(slr->count) ==0 ) { - slr->user =-1 ; -rte_spinlock_unlock (&slr->sl); - } +int id =rte_gettid (); +if (rte_atomic_load_explicit (&slr->user, rte_memory_order_acquire) == id) { +RTE_ASSERT (slr->count >0 ); +if (--(slr->count) ==0 ) { +rte_atomic_store_explicit (&slr->user,-1 , rte_memory_order_release); +rte_spinlock_unlock (&slr->sl); + } + }else { +RTE_ASSERT (id !=-1 &&"unlocked from another thread" ); + } }
(not tested)
Quoted 9 lines
lib/eal/include/generic/rte_spinlock.h +1
-2- slr->user =-1 ; +rte_atomic_store_explicit (&slr->user,-1 , rte_memory_order_relaxed);rte_spinlock_unlock (&slr->sl); } - }/**
lib/eal/include/generic/rte_spinlock.h static inline int rte_spinlock_recursive_trylock(rte_spinlock_recursive_t *slr) · line 268
Quoted 5 lines
lib/eal/include/generic/rte_spinlock.h +1
-1@@ -266,10 +265,10 @@ static inline int rte_spinlock_recursive_trylock(rte_spinlock_recursive_t *slr) {int id =rte_gettid (); -if (slr->user != id) { +if (rte_atomic_load_explicit (&slr->user, rte_memory_order_relaxed) != id) {
rte_memory_order_acquire
lib/eal/include/generic/rte_spinlock.h static inline int rte_spinlock_recursive_trylock(rte_spinlock_recursive_t *slr) · line 271
Quoted 4 lines
lib/eal/include/generic/rte_spinlock.h +1
-1if (rte_spinlock_trylock (&slr->sl) ==0 )return 0 ; - slr->user = id; +rte_atomic_store_explicit (&slr->user, id, rte_memory_order_relaxed);
rte_memory_order_release
Quoted 3 lines
lib/eal/include/generic/rte_spinlock.h +0
-0} slr->count++;return 1 ;
Signature · 1 line
Robin
Quoted 1 line
Monitored by the American Human Association.
18/05/2026 17:14, Robin Jarry:
Quoted 36 lines
Hey Thomas,
Thomas Monjalon, May 18, 2026 at 17:07:
Quoted 33 lines
When compiling with C++20 standard requirement (default in GCC 16), the increment and decrement of volatile variables are rejected:
rte_spinlock.h:241:14: error:
'++' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:252:21: error:
'--' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:278:14: error:
'++' expression of 'volatile'-qualified type is deprecatedThe count field of
rte_spinlock_recursive_t
does not need the volatile qualifier
because it is only accessed by the thread holding the lock, which already provides the necessary memory ordering.The user field can be accessed outside of the lock, so it must handled as a C11 atomic variable.
Fixes af75078fece3 ("first public release")Signed-off-by Thomas Monjalon <[email protected]>---
v1: drop volatile keyword
v2: make user an atomic variable
---
typedef struct {(quoted diff) +2
-2rte_spinlock_t sl; /**< the actual spinlock */ - volatile int user; /**< core id using lock, -1 for unused */ - volatile int count; /**< count of time this lock has been called */ + RTE_ATOMIC(int) user; /**< core id using lock, -1 for unused */ + int count; /**< count of time this lock has been called */ } rte_spinlock_recursive_t;
[...]
Quoted 8 lines
Review static inline void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr) · line 233
Quoted 6 lines
(quoted diff) +1
-1@@ -230,9 +230,9 @@ static inline void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr) { int id = rte_gettid(); - if (slr->user != id) { + if (rte_atomic_load_explicit(&slr->user, rte_memory_order_relaxed) != id) {This needs to be
rte_memory_order_acquire
The memory ordering is managed with the sl variable.
The variable user has just to be atomic, so relaxed should be enough, am I wrong?
[...]
Quoted 6 lines
lib/eal/include/generic/rte_spinlock.h static inline void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr) · line 248
Quoted 4 lines
lib/eal/include/generic/rte_spinlock.h +0
-0@@ -246,10 +246,9 @@ static inline void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr) __rte_no_thread_safety_analysis {if (--(slr->count) ==0 ) {This code is completely broken. Any thread can unlock without any check.
Maybe, but I don't intend to fix recursive unlock in this patch. The subject is "remove volatile qualifier" (and unblock GCC 16).
Quoted 4 lines · Thomas Monjalon
18/05/2026 17:14, Robin Jarry:
Quoted 3 lines
Hey Thomas,
Thomas Monjalon, May 18, 2026 at 17:07:
<snip>
Quoted 9 lines
[...]
Quoted 6 lines
lib/eal/include/generic/rte_spinlock.h static inline void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr) · line 248
Quoted 4 lines
lib/eal/include/generic/rte_spinlock.h +0
-0@@ -246,10 +246,9 @@ static inline void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr) __rte_no_thread_safety_analysis {if (--(slr->count) ==0 ) {This code is completely broken. Any thread can unlock without any check.
Quoted 3 lines
Maybe, but I don't intend to fix recursive unlock in this patch. The subject is "remove volatile qualifier" (and unblock GCC 16).
As with regular mutexes, threads should not go around unlocking when not holding the lock. Therefore, I think this is fine. [With regular spinlocks we can't check since we don't track threads, therefore I think we just need to assume a well-behaved app.]
/Bruce
Bruce Richardson, May 18, 2026 at 17:28:
Quoted 17 lines · Thomas Monjalon
Quoted 4 lines
18/05/2026 17:14, Robin Jarry:
Quoted 3 lines
Hey Thomas,
Thomas Monjalon, May 18, 2026 at 17:07:
<snip>
Quoted 9 lines
[...]
Quoted 6 lines
lib/eal/include/generic/rte_spinlock.h static inline void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr) · line 248
Quoted 4 lines
lib/eal/include/generic/rte_spinlock.h +0
-0@@ -246,10 +246,9 @@ static inline void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr) __rte_no_thread_safety_analysis {if (--(slr->count) ==0 ) {This code is completely broken. Any thread can unlock without any check.
Quoted 7 lines
Quoted 3 lines
Maybe, but I don't intend to fix recursive unlock in this patch. The subject is "remove volatile qualifier" (and unblock GCC 16).
As with regular mutexes, threads should not go around unlocking when not holding the lock. Therefore, I think this is fine. [With regular spinlocks we can't check since we don't track threads, therefore I think we just need to assume a well-behaved app.]
Ok, fair enough :)
As Stephen suggested, maybe we could add some assertions?
static inline void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr)
__rte_no_thread_safety_analysis
{
+ RTE_ASSERT(rte_atomic_load_explicit(&slr->user, rte_memory_order_relaxed) == rte_gettid());
+ RTE_ASSERT(slr->count > 0);
+
19/05/2026 11:17, Robin Jarry:
Quoted 26 lines
Bruce Richardson, May 18, 2026 at 17:28:
Quoted 10 lines · Thomas Monjalon
Quoted 9 lines
18/05/2026 17:14, Robin Jarry:
Quoted 7 lines
Thomas Monjalon, May 18, 2026 at 17:07:
lib/eal/include/generic/rte_spinlock.h static inline void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr) · line 248
Quoted 4 lines
lib/eal/include/generic/rte_spinlock.h +0
-0@@ -246,10 +246,9 @@ static inline void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr) __rte_no_thread_safety_analysis {if (--(slr->count) ==0 ) {This code is completely broken. Any thread can unlock without any check.
Quoted 7 lines
Quoted 3 lines
Maybe, but I don't intend to fix recursive unlock in this patch. The subject is "remove volatile qualifier" (and unblock GCC 16).
As with regular mutexes, threads should not go around unlocking when not holding the lock. Therefore, I think this is fine. [With regular spinlocks we can't check since we don't track threads, therefore I think we just need to assume a well-behaved app.]
Ok, fair enough :)
As Stephen suggested, maybe we could add some assertions?
static inline void
rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr)
__rte_no_thread_safety_analysis
{
Review
Quoted 3 lines
(quoted diff) +3
-0+ RTE_ASSERT(rte_atomic_load_explicit(&slr->user, rte_memory_order_relaxed) == rte_gettid()); + RTE_ASSERT(slr->count > 0); +
OK I add it as a separate patch in v3.
Quoted 56 lines · On Mon, 18 May 2026 17:14:54 +0200 "Robin Jarry" <[email protected]> wrote:
Hey Thomas,
Thomas Monjalon, May 18, 2026 at 17:07:
Quoted 53 lines
When compiling with C++20 standard requirement (default in GCC 16), the increment and decrement of volatile variables are rejected:
rte_spinlock.h:241:14: error:
'++' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:252:21: error:
'--' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:278:14: error:
'++' expression of 'volatile'-qualified type is deprecatedThe count field of
rte_spinlock_recursive_t
does not need the volatile qualifier
because it is only accessed by the thread holding the lock, which already provides the necessary memory ordering.The user field can be accessed outside of the lock, so it must handled as a C11 atomic variable.
Fixes af75078fece3 ("first public release")Signed-off-by Thomas Monjalon <[email protected]>---
v1: drop volatile keyword
v2: make user an atomic variable--- lib/eal/include/generic/rte_spinlock.h | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)lib/eal/include/generic/rte_spinlock.h +3
-3@@ -197,8 +197,8 @@ rte_spinlock_trylock_tm(rte_spinlock_t *sl)*/ typedef struct {rte_spinlock_t sl;/**< the actual spinlock */ -volatile int user;/**< core id using lock, -1 for unused */ -volatile int count;/**< count of time this lock has been called */ +RTE_ATOMIC (int ) user;/**< core id using lock, -1 for unused */ +int count;/**< count of time this lock has been called */ }rte_spinlock_recursive_t ;/** @@ -215,7 +215,7 @@ typedef struct {static inline void rte_spinlock_recursive_init (rte_spinlock_recursive_t *slr) {rte_spinlock_init (&slr->sl); - slr->user =-1 ; +rte_atomic_store_explicit (&slr->user,-1 , rte_memory_order_relaxed); slr->count =0 ; }
Quoted 8 lines
lib/eal/include/generic/rte_spinlock.h static inline void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr) · line 233
Quoted 6 lines
lib/eal/include/generic/rte_spinlock.h +1
-1@@ -230,9 +230,9 @@ static inline void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr) {int id =rte_gettid (); -if (slr->user != id) { +if (rte_atomic_load_explicit (&slr->user, rte_memory_order_relaxed) != id) {This needs to be
rte_memory_order_acquire
No it does not. There is no dependency here.
The acquire is inside the spinlock.
Quoted 6 lines
lib/eal/include/generic/rte_spinlock.h static inline void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr) · line 235
Quoted 4 lines
lib/eal/include/generic/rte_spinlock.h +1
-1rte_spinlock_lock (&slr->sl); - slr->user = id; +rte_atomic_store_explicit (&slr->user, id, rte_memory_order_relaxed);And
rte_memory_order_release
Ditto.
Quoted 10 lines
lib/eal/include/generic/rte_spinlock.h static inline void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr) · line 248
Quoted 8 lines
lib/eal/include/generic/rte_spinlock.h +0
-0} slr->count++; }@@ -246,10 +246,9 @@ static inline void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr) __rte_no_thread_safety_analysis {if (--(slr->count) ==0 ) {This code is completely broken. Any thread can unlock without any check.
I would make an RTE_ASSERT() that id matched current thread id. Since caller holds lock, no atomic required for that.
Quoted 15 lines · On Mon, 18 May 2026 17:07:35 +0200 Thomas Monjalon <[email protected]> wrote:
When compiling with C++20 standard requirement (default in GCC 16), the increment and decrement of volatile variables are rejected:
rte_spinlock.h:241:14: error:
'++' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:252:21: error:
'--' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:278:14: error:
'++' expression of 'volatile'-qualified type is deprecatedThe count field of
rte_spinlock_recursive_t
does not need the volatile qualifier
because it is only accessed by the thread holding the lock, which already provides the necessary memory ordering.
Quoted 7 lines
The user field can be accessed outside of the lock, so it must handled as a C11 atomic variable.
Fixes af75078fece3 ("first public release")Signed-off-by Thomas Monjalon <[email protected]>
I would also suggest renaming 'user' to 'owner' to follow common practice in other places. It also provides an intentional API break if somebody is misguided enough to do direct access to the structure field.
PATCH v3 1/3 spinlock: remove volatile qualifier
In reply to Thomas MonjalonWhen compiling with C++20 standard requirement (default in GCC 16), the increment and decrement of volatile variables are rejected:
rte_spinlock.h:241:14: error:
'++' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:252:21: error:
'--' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:278:14: error:
'++' expression of 'volatile'-qualified type is deprecated
The count field of rte_spinlock_recursive_t
does not need the volatile qualifier
because it is only accessed by the thread holding the lock, which already provides the necessary memory ordering.
The user field can be accessed outside of the lock,
so it must handled as a C11 atomic variable.
The name is also changed from user to owner.
It will break if an application is accessing this field directly, which should never happen.
---
v1: drop volatile keyword
v2: make user an atomic variable
v3: rename user as owner
--- lib/eal/include/generic/rte_spinlock.h | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)
lib/eal/include/generic/rte_spinlock.h +8 -9
@@ -197,8 +197,8 @@ rte_spinlock_trylock_tm(rte_spinlock_t *sl)*/ typedef struct {rte_spinlock_t sl;/**< the actual spinlock */ -volatile int user;/**< core id using lock, -1 for unused */ -volatile int count;/**< count of time this lock has been called */ +RTE_ATOMIC (int ) owner;/**< core id using lock, -1 for unused */ +int count;/**< count of time this lock has been called */ }rte_spinlock_recursive_t ;/**
@@ -215,7 +215,7 @@ typedef struct {static inline void rte_spinlock_recursive_init (rte_spinlock_recursive_t *slr) {rte_spinlock_init (&slr->sl); - slr->user =-1 ; +rte_atomic_store_explicit (&slr->owner,-1 , rte_memory_order_relaxed); slr->count =0 ; }
@@ -230,9 +230,9 @@ static inline void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr) {int id =rte_gettid (); -if (slr->user != id) { +if (rte_atomic_load_explicit (&slr->owner, rte_memory_order_relaxed) != id) {rte_spinlock_lock (&slr->sl); - slr->user = id; +rte_atomic_store_explicit (&slr->owner, id, rte_memory_order_relaxed); } slr->count++; }
@@ -246,10 +246,9 @@ static inline void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr) __rte_no_thread_safety_analysis {if (--(slr->count) ==0 ) { - slr->user =-1 ; +rte_atomic_store_explicit (&slr->owner,-1 , rte_memory_order_relaxed);rte_spinlock_unlock (&slr->sl); } - }/**
@@ -266,10 +265,10 @@ static inline int rte_spinlock_recursive_trylock(rte_spinlock_recursive_t *slr) {int id =rte_gettid (); -if (slr->user != id) { +if (rte_atomic_load_explicit (&slr->owner, rte_memory_order_relaxed) != id) {if (rte_spinlock_trylock (&slr->sl) ==0 )return 0 ; - slr->user = id; +rte_atomic_store_explicit (&slr->owner, id, rte_memory_order_relaxed); } slr->count++;return 1 ;
Signature · 1 line
2.54.0
PATCH v3 2/3 spinlock: add debug checks in recursive unlock
The recursive unlock assumes that the caller owns the lock. This behavior will be checked when RTE_ENABLE_ASSERT is on. There is an additional check for the count which should be positive if no corruption happened.
---
v3: new patch in the series
--- lib/eal/include/generic/rte_spinlock.h | 3 +++ 1 file changed, 3 insertions(+)
lib/eal/include/generic/rte_spinlock.h +3 -0
@@ -21,6 +21,7 @@#ifdef RTE_FORCE_INTRINSICS #include <rte_common.h> #endif +#include <rte_debug.h> #include <rte_lock_annotations.h> #include <rte_pause.h> #include <rte_stdatomic.h>
@@ -245,6 +246,8 @@ static inline void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr)static inline void rte_spinlock_recursive_unlock (rte_spinlock_recursive_t *slr) __rte_no_thread_safety_analysis { +RTE_ASSERT (rte_atomic_load_explicit (&slr->owner, rte_memory_order_relaxed) ==rte_gettid ()); +RTE_ASSERT (slr->count >0 );if (--(slr->count) ==0 ) {rte_atomic_store_explicit (&slr->owner,-1 , rte_memory_order_relaxed);rte_spinlock_unlock (&slr->sl);
Signature · 1 line
2.54.0
Re: [PATCH v3 2/3] spinlock: add debug checks in recursive unlock
19/05/2026 12:34, Thomas Monjalon:
Quoted 8 lines
The recursive unlock assumes that the caller owns the lock. This behavior will be checked when
RTE_ENABLE_ASSERTis on. There is an additional check for the count which should be positive if no corruption happened.Signed-off-by Thomas Monjalon <[email protected]>---
v3: new patch in the series
Forgot:
Re: [PATCH v3 2/3] spinlock: add debug checks in recursive unlock
In reply to Thomas MonjalonThomas Monjalon, May 19, 2026 at 12:34:
Quoted 25 lines
The recursive unlock assumes that the caller owns the lock. This behavior will be checked when
RTE_ENABLE_ASSERTis on. There is an additional check for the count which should be positive if no corruption happened.Signed-off-by Thomas Monjalon <[email protected]>---
v3: new patch in the series--- lib/eal/include/generic/rte_spinlock.h | 3 +++ 1 file changed, 3 insertions(+)lib/eal/include/generic/rte_spinlock.h +1
-0@@ -21,6 +21,7 @@#ifdef RTE_FORCE_INTRINSICS #include <rte_common.h> #endif +#include <rte_debug.h> #include <rte_lock_annotations.h> #include <rte_pause.h> #include <rte_stdatomic.h>
lib/eal/include/generic/rte_spinlock.h static inline void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr) · line 253
Quoted 8 lines
lib/eal/include/generic/rte_spinlock.h +2
-0@@ -245,6 +246,8 @@ static inline void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr)static inline void rte_spinlock_recursive_unlock (rte_spinlock_recursive_t *slr) __rte_no_thread_safety_analysis { +RTE_ASSERT (rte_atomic_load_explicit (&slr->owner, rte_memory_order_relaxed) ==rte_gettid ()); +RTE_ASSERT (slr->count >0 );if (--(slr->count) ==0 ) {rte_atomic_store_explicit (&slr->owner,-1 , rte_memory_order_relaxed);rte_spinlock_unlock (&slr->sl);
PATCH v3 3/3 spinlock: fix API comments
In reply to Thomas MonjalonThis is not a read-write lock.
The user field stores the thread ID, not the core ID since a change in DPDK 2.0.0.
The implementation is architecture-specific in some cases only.
--- lib/eal/include/generic/rte_spinlock.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
lib/eal/include/generic/rte_spinlock.h +9 -5
@@ -7,12 +7,16 @@/** * @file +* DPDK spinlocks * -* RTE Spinlocks +* This is an API for spinlocks. +* This kind of lock simply waits in a loop +* repeatedly checking until the lock becomes available. * -* This file defines an API for read-write locks, which are implemented -* in an architecture-specific way. This kind of lock simply waits in -* a loop repeatedly checking until the lock becomes available. +* Some functions may have an architecture-specific implementation +* if RTE_FORCE_INTRINSICS is disabled. +* The hardware transactional memory (lock elision) functions have _tm suffix +* and are implemented in architecture-specific files. * * All locks must be initialised before use, and only initialised once. */
@@ -198,7 +202,7 @@ rte_spinlock_trylock_tm(rte_spinlock_t *sl)*/ typedef struct {rte_spinlock_t sl;/**< the actual spinlock */ -RTE_ATOMIC (int ) owner;/**< core id using lock, -1 for unused */ +RTE_ATOMIC (int ) owner;/**< thread id owning lock, -1 for unused */ int count;/**< count of time this lock has been called */ }rte_spinlock_recursive_t ;
Signature · 1 line
2.54.0
Quoted 24 lines · Thomas Monjalon
When compiling with C++20 standard requirement (default in GCC 16), the increment and decrement of volatile variables are rejected:
rte_spinlock.h:241:14: error:
'++' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:252:21: error:
'--' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:278:14: error:
'++' expression of 'volatile'-qualified type is deprecatedThe count field of
rte_spinlock_recursive_t
does not need the volatile qualifier
because it is only accessed by the thread holding the lock, which already provides the necessary memory ordering.The user field can be accessed outside of the lock,
so it must handled as a C11 atomic variable.
The name is also changed from user to owner.
It will break if an application is accessing this field directly, which should never happen.Fixes af75078fece3 ("first public release")
Quoted 9 lines
Signed-off-by Thomas Monjalon <[email protected]>---
v1: drop volatile keyword
v2: make user an atomic variable
v3: rename user as owner--- lib/eal/include/generic/rte_spinlock.h | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)
Quoted 21 lines · On Tue, 19 May 2026 12:34:14 +0200 Thomas Monjalon <[email protected]> wrote:
When compiling with C++20 standard requirement (default in GCC 16), the increment and decrement of volatile variables are rejected:
rte_spinlock.h:241:14: error:
'++' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:252:21: error:
'--' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:278:14: error:
'++' expression of 'volatile'-qualified type is deprecatedThe count field of
rte_spinlock_recursive_t
does not need the volatile qualifier
because it is only accessed by the thread holding the lock, which already provides the necessary memory ordering.The user field can be accessed outside of the lock,
so it must handled as a C11 atomic variable.
The name is also changed from user to owner.
It will break if an application is accessing this field directly, which should never happen.
Quoted 4 lines
Fixes af75078fece3 ("first public release")Signed
Quoted 22 lines · Thomas Monjalon
When compiling with C++20 standard requirement (default in GCC 16), the increment and decrement of volatile variables are rejected:
rte_spinlock.h:241:14: error:
'++' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:252:21: error:
'--' expression of 'volatile'-qualified type is deprecated
rte_spinlock.h:278:14: error:
'++' expression of 'volatile'-qualified type is deprecatedThe count field of
rte_spinlock_recursive_t
does not need the volatile qualifier
because it is only accessed by the thread holding the lock, which already provides the necessary memory ordering.The user field can be accessed outside of the lock,
so it must handled as a C11 atomic variable.
The name is also changed from user to owner.
It will break if an application is accessing this field directly, which should never happen.
Quoted 4 lines
Fixes af75078fece3 ("first public release")Signed-off-by Thomas Monjalon <[email protected]>
Series applied, thanks.
Signature · 1 line
David Marchand