Skip to contentSkip to conversation

bpf: Annotate rqspinlock lock acquiring functions with __must_check

PATCH Superseded Amery Hung 5 participants 9 messages Nov 17, 2025 – Nov 26, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Amery Hung

author
#0 lore raw

PATCH 1/1 bpf: Annotate rqspinlock lock acquiring functions with __must_check

Locking a resilient queued spinlock can fail when deadlock or timeout happen. Mark the lock acquring functions with __must_check to make sure callers always handle the returned error.

---
 include/asm-generic/rqspinlock.h | 47 +++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 19 deletions(-)
include/asm-generic/rqspinlock.h +28 -19
index 6d4244d643df..855c09435506 100644
@@ -171,7 +171,7 @@ static __always_inline void release_held_lock_entry(void)
  * * -EDEADLK	- Lock acquisition failed because of AA/ABBA deadlock.
  * * -ETIMEDOUT - Lock acquisition failed because of timeout.
  */
-static __always_inline int res_spin_lock(rqspinlock_t *lock)
+static __always_inline __must_check int res_spin_lock(rqspinlock_t *lock)
 {
 	int val = 0;
 
@@ -223,27 +223,36 @@ static __always_inline void res_spin_unlock(rqspinlock_t *lock)
 #define raw_res_spin_lock_init(lock) ({ *(lock) = (rqspinlock_t){0}; })
 #endif
 
-#define raw_res_spin_lock(lock)                    \
-	({                                         \
-		int __ret;                         \
-		preempt_disable();                 \
-		__ret = res_spin_lock(lock);	   \
-		if (__ret)                         \
-			preempt_enable();          \
-		__ret;                             \
-	})
+static __always_inline __must_check int raw_res_spin_lock(rqspinlock_t *lock)
+{
+	int ret;
+
+	preempt_disable();
+	ret = res_spin_lock(lock);
+	if (ret)
+		preempt_enable();
+
+	return ret;
+}
 
 #define raw_res_spin_unlock(lock) ({ res_spin_unlock(lock); preempt_enable(); })
 
-#define raw_res_spin_lock_irqsave(lock, flags)    \
-	({                                        \
-		int __ret;                        \
-		local_irq_save(flags);            \
-		__ret = raw_res_spin_lock(lock);  \
-		if (__ret)                        \
-			local_irq_restore(flags); \
-		__ret;                            \
-	})
+static __always_inline __must_check int
+__raw_res_spin_lock_irqsave(rqspinlock_t *lock, unsigned long *flags)
+{
+	unsigned long __flags;
+	int ret;
+
+	local_irq_save(__flags);
+	ret = raw_res_spin_lock(lock);
+	if (ret)
+		local_irq_restore(__flags);
+
+	*flags = __flags;
+	return ret;
+}
+
+#define raw_res_spin_lock_irqsave(lock, flags) __raw_res_spin_lock_irqsave(lock, &flags)
 
 #define raw_res_spin_unlock_irqrestore(lock, flags) ({ raw_res_spin_unlock(lock); local_irq_restore(flags); })
 
Signature · 1 line
2.47.3
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Amery,

kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Amery-Hung/bpf-Annotate-rqspinlock-lock-acquiring-functions-with-__must_check/20251118-031838
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20251117191515.2934026-1-ameryhung%40gmail.com
patch subject: [PATCH bpf-next v1 1/1] bpf: Annotate rqspinlock lock acquiring functions with __must_check
config: arm-randconfig-002-20251118 (https://download.01.org/0day-ci/archive/20251118/[email protected]/config)
compiler: arm-linux-gnueabi-gcc (GCC) 10.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251118/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from ./arch/arm/include/generated/asm/rqspinlock.h:1,
from kernel/locking/locktorture.c:367:
kernel/locking/locktorture.c: In function 'torture_raw_res_spin_write_lock_irq':

>> include/asm-generic/rqspinlock.h:255:48: warning: ignoring return value of '__raw_res_spin_lock_irqsave' declared with attribute 'warn_unused_result' [-Wunused-result]
     255 | #define raw_res_spin_lock_irqsave(lock, flags) __raw_res_spin_lock_irqsave(lock, &flags)
         |                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   kernel/locking/locktorture.c:396:2: note: in expansion of macro 'raw_res_spin_lock_irqsave'
     396 |  raw_res_spin_lock_irqsave(&rqspinlock, flags);
         |  ^~~~~~~~~~~~~~~~~~~~~~~~~
   kernel/locking/locktorture.c: In function 'torture_raw_res_spin_write_lock':
>> kernel/locking/locktorture.c:372:2: warning: ignoring return value of 'raw_res_spin_lock' declared with attribute 'warn_unused_result' [-Wunused-result]
     372 |  raw_res_spin_lock(&rqspinlock);
         |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
   In file included from arch/arm/include/generated/asm/rqspinlock.h:1,
                    from locktorture.c:367:
   locktorture.c: In function 'torture_raw_res_spin_write_lock_irq':
>> include/asm-generic/rqspinlock.h:255:48: warning: ignoring return value of '__raw_res_spin_lock_irqsave' declared with attribute 'warn_unused_result' [-Wunused-result]
     255 | #define raw_res_spin_lock_irqsave(lock, flags) __raw_res_spin_lock_irqsave(lock, &flags)
         |                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   locktorture.c:396:2: note: in expansion of macro 'raw_res_spin_lock_irqsave'
     396 |  raw_res_spin_lock_irqsave(&rqspinlock, flags);
         |  ^~~~~~~~~~~~~~~~~~~~~~~~~
   locktorture.c: In function 'torture_raw_res_spin_write_lock':
   locktorture.c:372:2: warning: ignoring return value of 'raw_res_spin_lock' declared with attribute 'warn_unused_result' [-Wunused-result]
     372 |  raw_res_spin_lock(&rqspinlock);
         |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +255 include/asm-generic/rqspinlock.h

254

 > 255	#define raw_res_spin_lock_irqsave(lock, flags) __raw_res_spin_lock_irqsave(lock, &flags)

256

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to Amery Hung
kernel test robot noticed the following build warnings:

Hi Amery,

kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Amery-Hung/bpf-Annotate-rqspinlock-lock-acquiring-functions-with-__must_check/20251118-031838
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20251117191515.2934026-1-ameryhung%40gmail.com
patch subject: [PATCH bpf-next v1 1/1] bpf: Annotate rqspinlock lock acquiring functions with __must_check
config: i386-randconfig-002-20251118 (https://download.01.org/0day-ci/archive/20251118/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251118/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> kernel/locking/locktorture.c:372:2: warning: ignoring return value of function declared with 'warn_unused_result' attribute [-Wunused-result]
     372 |         raw_res_spin_lock(&rqspinlock);
         |         ^~~~~~~~~~~~~~~~~ ~~~~~~~~~~~
   kernel/locking/locktorture.c:396:2: warning: ignoring return value of function declared with 'warn_unused_result' attribute [-Wunused-result]
     396 |         raw_res_spin_lock_irqsave(&rqspinlock, flags);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

include/asm-generic/rqspinlock.h:255:48: note: expanded from macro 'raw_res_spin_lock_irqsave'
255 | #define raw_res_spin_lock_irqsave(lock, flags) __raw_res_spin_lock_irqsave(lock, &flags)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~
2 warnings generated.

vim +/warn_unused_result +372 kernel/locking/locktorture.c

a6884f6f1dd565 Kumar Kartikeya Dwivedi 2025-03-15  369  
a6884f6f1dd565 Kumar Kartikeya Dwivedi 2025-03-15  370  static int torture_raw_res_spin_write_lock(int tid __maybe_unused)
a6884f6f1dd565 Kumar Kartikeya Dwivedi 2025-03-15  371  {
a6884f6f1dd565 Kumar Kartikeya Dwivedi 2025-03-15 @372  	raw_res_spin_lock(&rqspinlock);
a6884f6f1dd565 Kumar Kartikeya Dwivedi 2025-03-15  373  	return 0;
a6884f6f1dd565 Kumar Kartikeya Dwivedi 2025-03-15  374  }

a6884f6f1dd565 Kumar Kartikeya Dwivedi 2025-03-15 375

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer Acked-by on v1 1/1
#3 lore raw
In reply to Amery Hung
Quoted 8 lines · Amery Hung

Locking a resilient queued spinlock can fail when deadlock or timeout happen. Mark the lock acquring functions with __must_check to make sure callers always handle the returned error.

---

Looks like it's working :)
I would just explicitly ignore with (void) cast the locktorture case. After that is fixed, you can add:

Thanks!

Quoted 74 lines
 include/asm-generic/rqspinlock.h | 47 +++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 19 deletions(-)
include/asm-generic/rqspinlock.h +28 -20
index 6d4244d643df..855c09435506 100644
@@ -171,7 +171,7 @@ static __always_inline void release_held_lock_entry(void)
  * * -EDEADLK  - Lock acquisition failed because of AA/ABBA deadlock.
  * * -ETIMEDOUT - Lock acquisition failed because of timeout.
  */
-static __always_inline int res_spin_lock(rqspinlock_t *lock)
+static __always_inline __must_check int res_spin_lock(rqspinlock_t *lock)
 {
        int val = 0;
 
@@ -223,27 +223,36 @@ static __always_inline void res_spin_unlock(rqspinlock_t *lock)
 #define raw_res_spin_lock_init(lock) ({ *(lock) = (rqspinlock_t){0}; })
 #endif
 
-#define raw_res_spin_lock(lock)                    \
-       ({                                         \
-               int __ret;                         \
-               preempt_disable();                 \
-               __ret = res_spin_lock(lock);       \
-               if (__ret)                         \
-                       preempt_enable();          \
-               __ret;                             \
-       })
+static __always_inline __must_check int raw_res_spin_lock(rqspinlock_t *lock)
+{
+       int ret;
+
+       preempt_disable();
+       ret = res_spin_lock(lock);
+       if (ret)
+               preempt_enable();
+
+       return ret;
+}
 
 #define raw_res_spin_unlock(lock) ({ res_spin_unlock(lock); preempt_enable(); })
 
-#define raw_res_spin_lock_irqsave(lock, flags)    \
-       ({                                        \
-               int __ret;                        \
-               local_irq_save(flags);            \
-               __ret = raw_res_spin_lock(lock);  \
-               if (__ret)                        \
-                       local_irq_restore(flags); \
-               __ret;                            \
-       })
+static __always_inline __must_check int
+__raw_res_spin_lock_irqsave(rqspinlock_t *lock, unsigned long *flags)
+{
+       unsigned long __flags;
+       int ret;
+
+       local_irq_save(__flags);
+       ret = raw_res_spin_lock(lock);
+       if (ret)
+               local_irq_restore(__flags);
+
+       *flags = __flags;
+       return ret;
+}
+
+#define raw_res_spin_lock_irqsave(lock, flags) __raw_res_spin_lock_irqsave(lock, &flags)
 
 #define raw_res_spin_unlock_irqrestore(lock, flags) ({ raw_res_spin_unlock(lock); local_irq_restore(flags); })
 
--

2.47.3

#4 lore raw
Quoted 6 lines · On Tue, 18 Nov 2025 05:16:50 -0500 Kumar Kartikeya Dwivedi <[email protected]>…
Quoted 5 lines · Amery Hung

Locking a resilient queued spinlock can fail when deadlock or timeout happen. Mark the lock acquring functions with __must_check to make sure callers always handle the returned error.

Quoted 6 lines
Quoted 3 lines

---

Looks like it's working :)
I would just explicitly ignore with (void) cast the locktorture case.

I'm not sure that works - I usually have to try a lot harder to ignore a '__must_check' result.

David

author
#5 lore raw
Quoted 14 lines · David Laight
Quoted 10 lines · On Tue, 18 Nov 2025 05:16:50 -0500 Kumar Kartikeya Dwivedi <[email protected]>…
Quoted 8 lines · Amery Hung

Locking a resilient queued spinlock can fail when deadlock or timeout happen. Mark the lock acquring functions with __must_check to make sure callers always handle the returned error.

---

Quoted 5 lines
Quoted 2 lines

Looks like it's working :)
I would just explicitly ignore with (void) cast the locktorture case.

I'm not sure that works - I usually have to try a lot harder to ignore a '__must_check' result.

Thanks for the heads up.

Indeed, gcc still complains about it even casting the return to (void) while clang does not.

I have to silence the warning by:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"

       raw_res_spin_lock(&rqspinlock);

#pragma GCC diagnostic pop

Thanks!
Amery

Quoted 2 lines

David

#6 lore raw
Quoted 27 lines · On Thu, 20 Nov 2025 12:12:12 -0800 Amery Hung <[email protected]> wrote:
Quoted 14 lines · David Laight
Quoted 10 lines · On Tue, 18 Nov 2025 05:16:50 -0500 Kumar Kartikeya Dwivedi <[email protected]>…
Quoted 8 lines · Amery Hung

Locking a resilient queued spinlock can fail when deadlock or timeout happen. Mark the lock acquring functions with __must_check to make sure callers always handle the returned error.

---

Quoted 5 lines
Quoted 2 lines

Looks like it's working :)
I would just explicitly ignore with (void) cast the locktorture case.

I'm not sure that works - I usually have to try a lot harder to ignore a '__must_check' result.

Thanks for the heads up.

Indeed, gcc still complains about it even casting the return to (void) while clang does not.

Quoted 6 lines

I have to silence the warning by:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"

       raw_res_spin_lock(&rqspinlock);

#pragma GCC diagnostic pop

I think the simpler:

	if (raw_res_spin_lock(&rqspinlock)) {};
also works.
But I'm sure I've resorted to crap like:
	x += foo() ? 0 : 0;
and/or:
	x += foo() == IMPOSSIBLE_VALUE;

and/or wrapping the call in a static inline function.

It is all a right PITA when you are doing read/write on a pipe that is being used for events.

At least no one has put a 'must_check' on fprintf() (yet). Code that looks at the return value is usually broken! (hint: you need to call fflush() and then check ferror().)

David

Quoted 6 lines

Thanks!
Amery

Quoted 2 lines

David

#7 lore raw
In reply to Amery Hung
Quoted 28 lines · Amery Hung
Quoted 14 lines · David Laight
Quoted 10 lines · On Tue, 18 Nov 2025 05:16:50 -0500 Kumar Kartikeya Dwivedi <[email protected]>…
Quoted 8 lines · Amery Hung

Locking a resilient queued spinlock can fail when deadlock or timeout happen. Mark the lock acquring functions with __must_check to make sure callers always handle the returned error.

---

Quoted 5 lines
Quoted 2 lines

Looks like it's working :)
I would just explicitly ignore with (void) cast the locktorture case.

I'm not sure that works - I usually have to try a lot harder to ignore a '__must_check' result.

Thanks for the heads up.

Indeed, gcc still complains about it even casting the return to (void) while clang does not.

Quoted 7 lines

I have to silence the warning by:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"

       raw_res_spin_lock(&rqspinlock);

#pragma GCC diagnostic pop

For BPF selftests we have

#define __sink(expr) asm volatile("" : "+g"(expr))

Try if that works here?

Quoted 5 lines

Thanks!
Amery

Quoted 2 lines

David

author
#8 lore raw
Quoted 38 lines · Andrii Nakryiko
Quoted 28 lines · Amery Hung
Quoted 14 lines · David Laight
Quoted 10 lines · On Tue, 18 Nov 2025 05:16:50 -0500 Kumar Kartikeya Dwivedi <[email protected]>…
Quoted 8 lines · Amery Hung

Locking a resilient queued spinlock can fail when deadlock or timeout happen. Mark the lock acquring functions with __must_check to make sure callers always handle the returned error.

---

Quoted 5 lines
Quoted 2 lines

Looks like it's working :)
I would just explicitly ignore with (void) cast the locktorture case.

I'm not sure that works - I usually have to try a lot harder to ignore a '__must_check' result.

Thanks for the heads up.

Indeed, gcc still complains about it even casting the return to (void) while clang does not.

Quoted 7 lines

I have to silence the warning by:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"

       raw_res_spin_lock(&rqspinlock);

#pragma GCC diagnostic pop

Quoted 5 lines

For BPF selftests we have

#define __sink(expr) asm volatile("" : "+g"(expr))

Try if that works here?

Thanks for the tip.

In v2, I decided to return the error to the caller to align with another test case, where the lock (ww_mutex) can also fail and has __must_check annotation.

Quoted 6 lines
Quoted 6 lines

Thanks!
Amery

Quoted 2 lines

David