mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3] locking/rwsem: add DEBUG_RWSEMS_WARN_ON to warn invalid rwsem
@ 2025-09-29 14:48 buckzhang1212
  2025-09-30  3:09 ` Waiman Long
  0 siblings, 1 reply; 3+ messages in thread
From: buckzhang1212 @ 2025-09-29 14:48 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Will Deacon, Boqun Feng,
	Waiman Long, linux-kernel
  Cc: buckzhang1212

From: "buck.zhang" <buckzhang1212@yeah.net>

Add the lock->magic check to warn invalid rwsem without initialization

Signed-off-by: buck.zhang <buckzhang1212@yeah.net>

---
Changes in v3:
  - Use the lock->magic check in __down_read_common/__down_write_common
  - (Suggested by Waiman Long)
---
 kernel/locking/rwsem.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
index 24df4d98f..38d884643 100644
--- a/kernel/locking/rwsem.c
+++ b/kernel/locking/rwsem.c
@@ -1256,6 +1256,9 @@ static __always_inline int __down_read_common(struct rw_semaphore *sem, int stat
 	int ret = 0;
 	long count;
 
+	/* add the lock->magic check to warn the invalid rwsem without initialization */
+	DEBUG_RWSEMS_WARN_ON(sem->magic != sem, sem);
+
 	preempt_disable();
 	if (!rwsem_read_trylock(sem, &count)) {
 		if (IS_ERR(rwsem_down_read_slowpath(sem, count, state))) {
@@ -1312,6 +1315,9 @@ static __always_inline int __down_write_common(struct rw_semaphore *sem, int sta
 {
 	int ret = 0;
 
+	/* add the lock->magic check to warn the invalid rwsem without initialization */
+	DEBUG_RWSEMS_WARN_ON(sem->magic != sem, sem);
+
 	preempt_disable();
 	if (unlikely(!rwsem_write_trylock(sem))) {
 		if (IS_ERR(rwsem_down_write_slowpath(sem, state)))
-- 
2.17.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] locking/rwsem: add DEBUG_RWSEMS_WARN_ON to warn invalid rwsem
  2025-09-29 14:48 [PATCH v3] locking/rwsem: add DEBUG_RWSEMS_WARN_ON to warn invalid rwsem buckzhang1212
@ 2025-09-30  3:09 ` Waiman Long
       [not found]   ` <78363f7b.34166.19998b29e71.Coremail.buckzhang1212@yeah.net>
  0 siblings, 1 reply; 3+ messages in thread
From: Waiman Long @ 2025-09-30  3:09 UTC (permalink / raw)
  To: buckzhang1212, Peter Zijlstra, Ingo Molnar, Will Deacon,
	Boqun Feng, linux-kernel

On 9/29/25 10:48 AM, buckzhang1212@yeah.net wrote:
> From: "buck.zhang" <buckzhang1212@yeah.net>
>
> Add the lock->magic check to warn invalid rwsem without initialization
>
> Signed-off-by: buck.zhang <buckzhang1212@yeah.net>
>
> ---
> Changes in v3:
>    - Use the lock->magic check in __down_read_common/__down_write_common
>    - (Suggested by Waiman Long)
> ---
>   kernel/locking/rwsem.c | 6 ++++++
>   1 file changed, 6 insertions(+)
>
> diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
> index 24df4d98f..38d884643 100644
> --- a/kernel/locking/rwsem.c
> +++ b/kernel/locking/rwsem.c
> @@ -1256,6 +1256,9 @@ static __always_inline int __down_read_common(struct rw_semaphore *sem, int stat
>   	int ret = 0;
>   	long count;
>   
> +	/* add the lock->magic check to warn the invalid rwsem without initialization */
> +	DEBUG_RWSEMS_WARN_ON(sem->magic != sem, sem);
> +
>   	preempt_disable();
>   	if (!rwsem_read_trylock(sem, &count)) {
>   		if (IS_ERR(rwsem_down_read_slowpath(sem, count, state))) {
> @@ -1312,6 +1315,9 @@ static __always_inline int __down_write_common(struct rw_semaphore *sem, int sta
>   {
>   	int ret = 0;
>   
> +	/* add the lock->magic check to warn the invalid rwsem without initialization */
> +	DEBUG_RWSEMS_WARN_ON(sem->magic != sem, sem);
> +
>   	preempt_disable();
>   	if (unlikely(!rwsem_write_trylock(sem))) {
>   		if (IS_ERR(rwsem_down_write_slowpath(sem, state)))

Just to confirm that this patch will generate the needed warning for 
your test case. Right?

Acked-by: Waiman Long <longman@redhat.com>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v3] locking/rwsem: add DEBUG_RWSEMS_WARN_ON to warn invalid rwsem
       [not found]   ` <78363f7b.34166.19998b29e71.Coremail.buckzhang1212@yeah.net>
@ 2025-09-30 14:36     ` Waiman Long
  0 siblings, 0 replies; 3+ messages in thread
From: Waiman Long @ 2025-09-30 14:36 UTC (permalink / raw)
  To: buckzhang1212, llong; +Cc: peterz, mingo, will, boqun.feng, linux-kernel

On 9/29/25 11:37 PM, buckzhang1212@yeah.net wrote:
> Hey Long
> Yes. I tested it yesterday. patch can provide  the  needed warning 
>  for my test case when enable CONFIG_DEBUG_RESEMES
>
>
Thanks for the confirmation.

Cheers,
Longman


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-09-30 14:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-29 14:48 [PATCH v3] locking/rwsem: add DEBUG_RWSEMS_WARN_ON to warn invalid rwsem buckzhang1212
2025-09-30  3:09 ` Waiman Long
     [not found]   ` <78363f7b.34166.19998b29e71.Coremail.buckzhang1212@yeah.net>
2025-09-30 14:36     ` Waiman Long

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®