Skip to contentSkip to conversation

locking/rwsem: add DEBUG_RWSEMS_WARN_ON to warn invalid rwsem

PATCH In review [email protected] 2 participants 3 messages Sep 29, 2025 – Sep 30, 2025 1 message not in the archive Locking primitives

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 locking/rwsem: add DEBUG_RWSEMS_WARN_ON to warn invalid rwsem

From: "buck.zhang" <[email protected]>

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

---
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(+)
kernel/locking/rwsem.c +6 -0
index 24df4d98f..38d884643 100644
@@ -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)))
Signature · 1 line
2.17.1
Acked-by on v3 1/1
#1 lore raw
Quoted 29 lines · On 9/29/25 10:48 AM, [email protected] wrote:

From: "buck.zhang" <[email protected]>

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

---
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(+)
kernel/locking/rwsem.c +3 -0
index 24df4d98f..38d884643 100644
@@ -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))) {

kernel/locking/rwsem.c static __always_inline int __down_write_common(struct rw_semaphore *sem, int sta · line 1323

Quoted 9 lines
kernel/locking/rwsem.c +3 -0
@@ -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?

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 4 lines · On 9/29/25 11:37 PM, [email protected] 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