Skip to contentSkip to conversation

Tree sweep for spin_is_locked misuses and start warning about it

PATCH In review Andi Kleen 7 participants 20 messages Mar 16, 2012 – Mar 21, 2012

Subsystems (14)
lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

I did a tree sweep for spin_is_locked() users and many of them were wrong, got UP incorrect, were obviously racy and had other problems. I replaced a lot of users with calls to the correct lock debugging functions, like lockdep_assert_held()

Generally I think spin_is_locked() should be deprecated because it is rarely used correctly. There are a few legitimate users so we cannot outright remove it unfortunately.

But most users can be removed or changed.

After this start warning in checkpatch.pl for spin_is_locked() because it's rarely wrong. Also error out there for the broken (WARN|BUG)_ON(!spin_is_locked()) which does not work on UP builds.

-Andi

author
#1 lore raw

PATCH 1/11 block: use lockdep_assert_held for queue locking

From: Andi Kleen <[email protected]>

Instead of an ugly open coded variant.

---
 block/blk-throttle.c   |    2 +-
 include/linux/blkdev.h |   18 +++++++-----------
 2 files changed, 8 insertions(+), 12 deletions(-)
block/blk-throttle.c +1 -1
index 5eed6a7..f2ddb94 100644
@@ -1218,7 +1218,7 @@ void blk_throtl_drain(struct request_queue *q)
 	struct bio_list bl;
 	struct bio *bio;
 
-	WARN_ON_ONCE(!queue_is_locked(q));
+	queue_lockdep_assert_held(q);
 
 	bio_list_init(&bl);
 
include/linux/blkdev.h +7 -11
index 606cf33..2aa2466 100644
@@ -426,14 +426,10 @@ struct request_queue {
 				 (1 << QUEUE_FLAG_SAME_COMP)	|	\
 				 (1 << QUEUE_FLAG_ADD_RANDOM))
 
-static inline int queue_is_locked(struct request_queue *q)
+static inline void queue_lockdep_assert_held(struct request_queue *q)
 {
-#ifdef CONFIG_SMP
-	spinlock_t *lock = q->queue_lock;
-	return lock && spin_is_locked(lock);
-#else
-	return 1;
-#endif
+	if (q->queue_lock)
+		lockdep_assert_held(q->queue_lock);
 }
 
 static inline void queue_flag_set_unlocked(unsigned int flag,
@@ -445,7 +441,7 @@ static inline void queue_flag_set_unlocked(unsigned int flag,
 static inline int queue_flag_test_and_clear(unsigned int flag,
 					    struct request_queue *q)
 {
-	WARN_ON_ONCE(!queue_is_locked(q));
+	queue_lockdep_assert_held(q);
 
 	if (test_bit(flag, &q->queue_flags)) {
 		__clear_bit(flag, &q->queue_flags);
@@ -458,7 +454,7 @@ static inline int queue_flag_test_and_clear(unsigned int flag,
 static inline int queue_flag_test_and_set(unsigned int flag,
 					  struct request_queue *q)
 {
-	WARN_ON_ONCE(!queue_is_locked(q));
+	queue_lockdep_assert_held(q);
 
 	if (!test_bit(flag, &q->queue_flags)) {
 		__set_bit(flag, &q->queue_flags);
@@ -470,7 +466,7 @@ static inline int queue_flag_test_and_set(unsigned int flag,
 
 static inline void queue_flag_set(unsigned int flag, struct request_queue *q)
 {
-	WARN_ON_ONCE(!queue_is_locked(q));
+	queue_lockdep_assert_held(q);
 	__set_bit(flag, &q->queue_flags);
 }
 
@@ -487,7 +483,7 @@ static inline int queue_in_flight(struct request_queue *q)
 
 static inline void queue_flag_clear(unsigned int flag, struct request_queue *q)
 {
-	WARN_ON_ONCE(!queue_is_locked(q));
+	queue_lockdep_assert_held(q);
 	__clear_bit(flag, &q->queue_flags);
 }
 
Signature · 1 line
1.7.7.6
author
#2 lore raw

PATCH 2/11 sgi-xp: Use lockdep_assert_held

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

!spin_is_locked() is always true on a up build, so cannot be used for assert. Replace with lockdep_assert_held.

I realize UP builds are not very likely for this driver, but it's still better to fix it.

---
 drivers/misc/sgi-xp/xpc_channel.c |    6 +++---
 drivers/misc/sgi-xp/xpc_sn2.c     |    2 +-
 drivers/misc/sgi-xp/xpc_uv.c      |    2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)
drivers/misc/sgi-xp/xpc_channel.c +3 -3
index 652593f..1655c21 100644
@@ -28,7 +28,7 @@ xpc_process_connect(struct xpc_channel *ch, unsigned long *irq_flags)
 {
 	enum xp_retval ret;
 
-	DBUG_ON(!spin_is_locked(&ch->lock));
+	lockdep_assert_held(&ch->lock);
 
 	if (!(ch->flags & XPC_C_OPENREQUEST) ||
 	    !(ch->flags & XPC_C_ROPENREQUEST)) {
@@ -82,7 +82,7 @@ xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
 	struct xpc_partition *part = &xpc_partitions[ch->partid];
 	u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
 
-	DBUG_ON(!spin_is_locked(&ch->lock));
+	lockdep_assert_held(&ch->lock);
 
 	if (!(ch->flags & XPC_C_DISCONNECTING))
 		return;
@@ -758,7 +758,7 @@ xpc_disconnect_channel(const int line, struct xpc_channel *ch,
 {
 	u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
 
-	DBUG_ON(!spin_is_locked(&ch->lock));
+	lockdep_assert_held(&ch->lock);
 
 	if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
 		return;
drivers/misc/sgi-xp/xpc_sn2.c +1 -1
index 7d71c04..9d3b113 100644
@@ -1674,7 +1674,7 @@ xpc_teardown_msg_structures_sn2(struct xpc_channel *ch)
 {
 	struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
 
-	DBUG_ON(!spin_is_locked(&ch->lock));
+	lockdep_assert_held(&ch->lock);
 
 	ch_sn2->remote_msgqueue_pa = 0;
 
drivers/misc/sgi-xp/xpc_uv.c +1 -1
index 17bbacb..666b081 100644
@@ -1181,7 +1181,7 @@ xpc_teardown_msg_structures_uv(struct xpc_channel *ch)
 {
 	struct xpc_channel_uv *ch_uv = &ch->sn.uv;
 
-	DBUG_ON(!spin_is_locked(&ch->lock));
+	lockdep_assert_held(&ch->lock);
 
 	kfree(ch_uv->cached_notify_gru_mq_desc);
 	ch_uv->cached_notify_gru_mq_desc = NULL;
Signature · 1 line
1.7.7.6
Acked-by on v1 2/11
#3 lore raw

Re: [PATCH 02/11] sgi-xp: Use lockdep_assert_held

Quoted 7 lines · Andi Kleen

From: Andi Kleen <[email protected]>

!spin_is_locked() is always true on a up build, so cannot be used for assert. Replace with lockdep_assert_held.

I realize UP builds are not very likely for this driver, but it's still better to fix it.

author
#4 lore raw

PATCH 3/11 ada152x: Remove broken usage of spin_is_locked

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

Remove racy usage of spin_is_locked. The author seems to have been unclear on the concept of locking.

This is debug code normally not enabled, but I caught it on a tree sweep.

---
 drivers/scsi/aha152x.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
drivers/scsi/aha152x.c +0 -3
index f17c92c..c56b617 100644
@@ -288,9 +288,6 @@ static LIST_HEAD(aha152x_host_list);
 
 #define DO_LOCK(flags)	\
 	do { \
-		if(spin_is_locked(&QLOCK)) { \
-			DPRINTK(debug_intr, DEBUG_LEAD "(%s:%d) already locked at %s:%d\n", CMDINFO(CURRENT_SC), __func__, __LINE__, QLOCKER, QLOCKERL); \
-		} \
 		DPRINTK(debug_locking, DEBUG_LEAD "(%s:%d) locking\n", CMDINFO(CURRENT_SC), __func__, __LINE__); \
 		spin_lock_irqsave(&QLOCK,flags); \
 		DPRINTK(debug_locking, DEBUG_LEAD "(%s:%d) locked\n", CMDINFO(CURRENT_SC), __func__, __LINE__); \
Signature · 1 line
1.7.7.6
author
#5 lore raw

PATCH 4/11 staging/zmem: Use lockdep_assert_held instead of spin_is_locked

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

WARN_ON(!spin_is_locked()) will always trigger on UP. Use lockdep_assert_held instead.

---
 drivers/staging/zcache/tmem.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/staging/zcache/tmem.h +1 -1
index ed147c4..0d4aa82 100644
@@ -47,7 +47,7 @@
 #define ASSERT_INVERTED_SENTINEL(_x, _y) do { } while (0)
 #endif
 
-#define ASSERT_SPINLOCK(_l)	WARN_ON(!spin_is_locked(_l))
+#define ASSERT_SPINLOCK(_l)	lockdep_assert_held(_l)
 
 /*
  * A pool is the highest-level data structure managed by tmem and
Signature · 1 line
1.7.7.6
author
#6 lore raw

PATCH 5/11 XFS: Fix lock ASSERT on UP

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

ASSERT(!spin_is_locked()) doesn't work on UP builds. Replace with a standard lockdep_assert_held()

---
 fs/xfs/xfs_iget.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
fs/xfs/xfs_iget.c +2 -2
index 8c3e463..8654d78 100644
@@ -76,7 +76,7 @@ xfs_inode_alloc(
 	}
 
 	ASSERT(atomic_read(&ip->i_pincount) == 0);
-	ASSERT(!spin_is_locked(&ip->i_flags_lock));
+	lockdep_assert_held(&ip->i_flags_lock);
 	ASSERT(!xfs_isiflocked(ip));
 	ASSERT(ip->i_ino == 0);
 
@@ -147,7 +147,7 @@ xfs_inode_free(
 
 	/* asserts to verify all state is correct here */
 	ASSERT(atomic_read(&ip->i_pincount) == 0);
-	ASSERT(!spin_is_locked(&ip->i_flags_lock));
+	lockdep_assert_held(&ip->i_flags_lock);
 	ASSERT(!xfs_isiflocked(ip));
 
 	/*
Signature · 1 line
1.7.7.6
#7 lore raw

Re: [PATCH 05/11] XFS: Fix lock ASSERT on UP

Quoted 25 lines · Andi Kleen

From: Andi Kleen <[email protected]>

ASSERT(!spin_is_locked()) doesn't work on UP builds. Replace with a standard lockdep_assert_held()

---
 fs/xfs/xfs_iget.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
fs/xfs/xfs_iget.c +1 -1
index 8c3e463..8654d78 100644
@@ -76,7 +76,7 @@ xfs_inode_alloc(
 	}
 
 	ASSERT(atomic_read(&ip->i_pincount) == 0);
-	ASSERT(!spin_is_locked(&ip->i_flags_lock));
+	lockdep_assert_held(&ip->i_flags_lock);
 	ASSERT(!xfs_isiflocked(ip));
 	ASSERT(ip->i_ino == 0);
 

fs/xfs/xfs_iget.c xfs_inode_free( · line 151

Quoted 6 lines
fs/xfs/xfs_iget.c +1 -1
@@ -147,7 +147,7 @@ xfs_inode_free(
 
 	/* asserts to verify all state is correct here */
 	ASSERT(atomic_read(&ip->i_pincount) == 0);
-	ASSERT(!spin_is_locked(&ip->i_flags_lock));
+	lockdep_assert_held(&ip->i_flags_lock);
 	ASSERT(!xfs_isiflocked(ip));

So this means we only ever check that the spinlock is held when lockdep is turned on instead of whenever CONFIG_XFS_DEBUG is set? That means it will rarely get checked during development instead of all the time. That's not an improvement IMO....

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
#8 lore raw

Re: [PATCH 05/11] XFS: Fix lock ASSERT on UP

Quoted 2 lines

So this means we only ever check that the spinlock is held when lockdep is turned on instead of whenever CONFIG_XFS_DEBUG is set?

You should regularly test with lockdep anyways. If you don't you clearly have a testing gap. lockdep is likely to find many more locking bugs than any of your very sparse manual annotations.

Quoted 2 lines

That means it will rarely get checked during development instead of all the time. That's not an improvement IMO....

It's an improvement that an !CONFIG_SMP && CONFIG_XFS_DEBUG kernel will not blow up anymore.

-Andi

#9 lore raw

Re: [PATCH 05/11] XFS: Fix lock ASSERT on UP

Quoted 4 lines · Andi Kleen
Quoted 2 lines

So this means we only ever check that the spinlock is held when lockdep is turned on instead of whenever CONFIG_XFS_DEBUG is set?

You should regularly test with lockdep anyways.

I do. Once or twice a release cycle I'll do a test cycle with lockdep enabled. i don't use it on day-to-day tst cycles because of it's overhead, and well, I understand the lock ordering of most of the code I'm modifying....

Quoted 3 lines

If you don't you clearly
have a testing gap. lockdep is likely to find many more locking bugs than any of your very sparse manual annotations.

That's not true. Lockdep validates lock order but it doesn't find race conditions. Nor does lockdep tell us that the correct lock is held when entering the function. Nor does it tell us that the state of a RCU freed object is correct when it is reallocated out of the kmem_cache it belongs to. These things need manual annotations, and they find more problems during development than lockdep does.

Not to mention that we have to annotate our read/write locks with extra state when CONFIG_XFS_DEBUG is enabled to ensure that the lock mode is correct because rwsems can't tell us the difference between a read hold and a write hold.

Hell, we only check the inode lock state in about 40 different locations via xfs_isilocked(), but many of these locations have multiple callers. e.g. there's a check in xfs_trans_ijoin(), and it has about 60 callers. So the "very sparse manual annotations" for locking behaviour in XFS are far more comprehensive than you claim and catch many more problems early in development under CONFIG_XFS_DEBUG than lockdep is even aware exists....

Quoted 5 lines
Quoted 2 lines

That means it will rarely get checked during development instead of all the time. That's not an improvement IMO....

It's an improvement that an !CONFIG_SMP && CONFIG_XFS_DEBUG kernel will not blow up anymore.

<shrug>

Whatever, it's not worth arguing over. There's only two places that XFS uses spin_is_locked(), and there are other checks around it that will catch the same race conditions with CONFIG_XFS_DEBUG enabled, so go ahead and use lockdep for them.

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
author
#10 lore raw

PATCH 6/11 huge-memory: Use lockdep_assert_held

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

Use lockdep_assert_held to check for locks instead of an opencoded variant.

---
 mm/huge_memory.c |    4 ++--
 mm/swap.c        |    2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
mm/huge_memory.c +2 -2
index 91d3efb..28669c6 100644
@@ -2083,7 +2083,7 @@ static void collect_mm_slot(struct mm_slot *mm_slot)
 {
 	struct mm_struct *mm = mm_slot->mm;
 
-	VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
+	lockdep_assert_held(&khugepaged_mm_lock);
 
 	if (khugepaged_test_exit(mm)) {
 		/* free mm_slot */
@@ -2113,7 +2113,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
 	int progress = 0;
 
 	VM_BUG_ON(!pages);
-	VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
+	lockdep_assert_held(&khugepaged_mm_lock);
 
 	if (khugepaged_scan.mm_slot)
 		mm_slot = khugepaged_scan.mm_slot;
mm/swap.c +1 -1
index fff1ff7..811e615 100644
@@ -659,7 +659,7 @@ void lru_add_page_tail(struct zone* zone,
 	VM_BUG_ON(!PageHead(page));
 	VM_BUG_ON(PageCompound(page_tail));
 	VM_BUG_ON(PageLRU(page_tail));
-	VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&zone->lru_lock));
+	lockdep_assert_held(&zone->lru_lock);
 
 	SetPageLRU(page_tail);
 
Signature · 1 line
1.7.7.6
#11 lore raw

Re: [PATCH 06/11] huge-memory: Use lockdep_assert_held

Hi Andi,

Quoted 17 lines · Andi Kleen

From: Andi Kleen <[email protected]>

Use lockdep_assert_held to check for locks instead of an opencoded variant.

---
 mm/huge_memory.c |    4 ++--
 mm/swap.c        |    2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
mm/huge_memory.c +0 -0
index 91d3efb..28669c6 100644

mm/huge_memory.c static void collect_mm_slot(struct mm_slot *mm_slot) · line 2086

Quoted 5 lines
mm/huge_memory.c +1 -1
@@ -2083,7 +2083,7 @@ static void collect_mm_slot(struct mm_slot *mm_slot)
 {
 	struct mm_struct *mm = mm_slot->mm;
 
-	VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
+	lockdep_assert_held(&khugepaged_mm_lock);

This check was intended to be a VM debug check, so getting enabled by DEBUG_VM, not through lockdep. I mean I always have DEBUG_VM enabled in all my kernels, but lockdep only enabled on my test system. So it's not an opencoded variant strictly speaking.

My estimate is that what gets more tested is BUG_ON, second VM_BUG_ON, third LOCKDEP. But hey, this code has been tested for a while so I'm neutral and if you prefer the lockdep version it's up to you.

Thanks,
Andrea

Acked-by
#12 lore raw

Re: [PATCH 06/11] huge-memory: Use lockdep_assert_held

Quoted 29 lines · Andrea Arcangeli
Quoted 17 lines · Andi Kleen

From: Andi Kleen <[email protected]>

Use lockdep_assert_held to check for locks instead of an opencoded variant.

---
 mm/huge_memory.c |    4 ++--
 mm/swap.c        |    2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
mm/huge_memory.c +0 -0
index 91d3efb..28669c6 100644

mm/huge_memory.c static void collect_mm_slot(struct mm_slot *mm_slot) · line 2086

Quoted 5 lines
mm/huge_memory.c +1 -1
@@ -2083,7 +2083,7 @@ static void collect_mm_slot(struct mm_slot *mm_slot)
 {
 	struct mm_struct *mm = mm_slot->mm;
 
-	VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
+	lockdep_assert_held(&khugepaged_mm_lock);

This check was intended to be a VM debug check, so getting enabled by DEBUG_VM, not through lockdep. I mean I always have DEBUG_VM enabled in all my kernels, but lockdep only enabled on my test system. So it's not an opencoded variant strictly speaking.

Quoted 2 lines

My estimate is that what gets more tested is BUG_ON, second VM_BUG_ON, third LOCKDEP.

My estimate too.

Quoted 2 lines

But hey, this code has been tested for a while so I'm neutral and if you prefer the lockdep version it's up to you.

Yes, it's not as if these are public interfaces used by drivers and
filesystems: they have very few callsites, and by now those lines are mostly useful as documentation.

Andrew did mention assert_spin_locked() when I added those NR_CPUS conditions, but we preferred VM_BUG_ON to BUG_ON, and I'm content with Andi's further restriction to lockdep.

Hugh

author
#13 lore raw

PATCH 7/11 futex: Use lockdep_assert_held() for lock checking

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

Use lockdep_assert_held() for lock checking instead of a strange homegrown variant. This removes the return for this case, but that is unlikely to be useful anyways.

---
 kernel/futex.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
kernel/futex.c +3 -2
index 1614be2..a77dda7 100644
@@ -823,8 +823,9 @@ static void __unqueue_futex(struct futex_q *q)
 {
 	struct futex_hash_bucket *hb;
 
-	if (WARN_ON_SMP(!q->lock_ptr || !spin_is_locked(q->lock_ptr))
-	    || WARN_ON(plist_node_empty(&q->list)))
+	if (q->lock_ptr)
+		lockdep_assert_held(q->lock_ptr);
+	if (WARN_ON(plist_node_empty(&q->list)))
 		return;
 
 	hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
Signature · 1 line
1.7.7.6
author
#14 lore raw

PATCH 8/11 irda: remove spin_is_locked

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

It's hard to imagine how this spin_is_locked debugging check is not totally racy. Remove it.

---
 drivers/net/irda/sir_dev.c |    5 -----
 1 files changed, 0 insertions(+), 5 deletions(-)
drivers/net/irda/sir_dev.c +0 -5
index 5039f08..90a1b3e 100644
@@ -632,11 +632,6 @@ static netdev_tx_t sirdev_hard_xmit(struct sk_buff *skb,
 	/* Init tx buffer*/
 	dev->tx_buff.data = dev->tx_buff.head;
 
-	/* Check problems */
-	if(spin_is_locked(&dev->tx_lock)) {
-		IRDA_DEBUG(3, "%s(), write not completed\n", __func__);
-	}
-
 	/* serialize with write completion */
 	spin_lock_irqsave(&dev->tx_lock, flags);
 
Signature · 1 line
1.7.7.6
author
#15 lore raw

PATCH 9/11 usb: gadget: f_fs: Remove lock is held before freeing checks

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

lock debugging already supports this, no need to do it explicitely.

---
 drivers/usb/gadget/f_fs.c |    4 +---
 1 files changed, 1 insertions(+), 3 deletions(-)
drivers/usb/gadget/f_fs.c +1 -3
index f63dc6c..c5e8994 100644
@@ -1258,9 +1258,7 @@ static void ffs_data_put(struct ffs_data *ffs)
 	if (unlikely(atomic_dec_and_test(&ffs->ref))) {
 		pr_info("%s(): freeing\n", __func__);
 		ffs_data_clear(ffs);
-		BUG_ON(mutex_is_locked(&ffs->mutex) ||
-		       spin_is_locked(&ffs->ev.waitq.lock) ||
-		       waitqueue_active(&ffs->ev.waitq) ||
+		BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
 		       waitqueue_active(&ffs->ep0req_completion.wait));
 		kfree(ffs);
 	}
Signature · 1 line
1.7.7.6
author
#16 lore raw

PATCH 10/11 smsc911x: Use lockdep_assert_held instead of home grown buggy construct

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

---
 drivers/net/ethernet/smsc/smsc911x.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/net/ethernet/smsc/smsc911x.h +1 -1
index 9ad5e5d..4fbb697 100644
@@ -52,7 +52,7 @@
 
 #ifdef CONFIG_DEBUG_SPINLOCK
 #define SMSC_ASSERT_MAC_LOCK(pdata) \
-		WARN_ON(!spin_is_locked(&pdata->mac_lock))
+		lockdep_assert_held(&(pdata)->mac_lock)
 #else
 #define SMSC_ASSERT_MAC_LOCK(pdata) do {} while (0)
 #endif				/* CONFIG_DEBUG_SPINLOCK */
Signature · 1 line
1.7.7.6
author
#17 lore raw

PATCH 11/11 checkpatch: Check for spin_is_locked

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

spin_is_locked is usually misued. In checkpatch.pl

- warn when it is used at all
- error out when it is asserted on free, because that's usually broken (e.g. doesn't work on on uni processor builds). Recommend lockdep_assert_held() instead.

Cc Andy Whitcroft <[email protected]>
---
 scripts/checkpatch.pl |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)
scripts/checkpatch.pl +14 -0
index a3b9782..9904547 100755
@@ -3330,6 +3330,20 @@ sub process {
 			}
 		}
 
+# spin_is_locked is usually misused. warn about it.
+		if ($line =~ /\bspin_is_locked\s*\(/) {
+			# BUG_ON/WARN_ON(!spin_is_locked() is generally a bug
+			if ($line =~ /(BUG_ON|WARN_ON|ASSERT)\s*\(!spin_is_locked/) {
+				ERROR("ASSERT_SPIN_IS_LOCKED",
+				     "Use lockdep_assert_held() instead of asserts on !spin_is_locked\n"
+				      . $herecurr);
+			} else {
+				WARN("SPIN_IS_LOCKED",
+				     "spin_is_locked is very rarely correctly used. Please reconsider\n"
+					. $herecurr)
+			}
+		}
+
 # check for lockdep_set_novalidate_class
 		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
 		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
Signature · 1 line
1.7.7.6
maintainer
#18 lore raw

Re: [PATCH 11/11] checkpatch: Check for spin_is_locked

Quoted 4 lines · Andi Kleen

From: Andi Kleen <[email protected]>
spin_is_locked is usually misused. In checkpatch.pl
- warn when it is used at all
- error out when it is asserted on free, because that's usually broken

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 4 lines
scripts/checkpatch.pl +4 -0
+# spin_is_locked is usually misused. warn about it.
+		if ($line =~ /\bspin_is_locked\s*\(/) {
+			# BUG_ON/WARN_ON(!spin_is_locked() is generally a bug
+			if ($line =~ /(BUG_ON|WARN_ON|ASSERT)\s*\(!spin_is_locked/) {

scripts/checkpatch.pl sub process { · line 3346

Quoted 10 lines
scripts/checkpatch.pl +10 -0
+				ERROR("ASSERT_SPIN_IS_LOCKED",
+				     "Use lockdep_assert_held() instead of asserts on !spin_is_locked\n"
+				      . $herecurr);
+			} else {
+				WARN("SPIN_IS_LOCKED",
+				     "spin_is_locked is very rarely correctly used. Please reconsider\n"
+					. $herecurr)
+			}
+		}
+

I suggest you use a single --ignore string of
"SPIN_IS_LOCKED" instead of different ones.

Grammar might be improved in the WARN. Maybe:
"Using spin_is_locked() is generally wrong. See [foo documentation]\n"

Also, like what was done for yield(), perhaps
some kernel-doc content would be useful.

See -next commit 8e3fabfde445a872c8aec2296846badf24d7c8b4

author
#19 lore raw

Re: [PATCH 11/11] checkpatch: Check for spin_is_locked

Quoted 17 lines · Joe Perches
Quoted 4 lines · Andi Kleen

From: Andi Kleen <[email protected]>
spin_is_locked is usually misused. In checkpatch.pl
- warn when it is used at all
- error out when it is asserted on free, because that's usually broken

[]

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

Quoted 9 lines
scripts/checkpatch.pl +9 -0
+# spin_is_locked is usually misused. warn about it.
+		if ($line =~ /\bspin_is_locked\s*\(/) {
+			# BUG_ON/WARN_ON(!spin_is_locked() is generally a bug
+			if ($line =~ /(BUG_ON|WARN_ON|ASSERT)\s*\(!spin_is_locked/) {
+				ERROR("ASSERT_SPIN_IS_LOCKED",
+				     "Use lockdep_assert_held() instead of asserts on !spin_is_locked\n"
+				      . $herecurr);
+			} else {
+				WARN("SPIN_IS_LOCKED",
Quoted 8 lines

scripts/checkpatch.pl sub process { · line 3346

Quoted 5 lines
scripts/checkpatch.pl +5 -0
+				     "spin_is_locked is very rarely correctly used. Please reconsider\n"
+					. $herecurr)
+			}
+		}
+

I suggest you use a single --ignore string of
"SPIN_IS_LOCKED" instead of different ones.

Done.

Quoted 3 lines

Grammar might be improved in the WARN. Maybe:
"Using spin_is_locked() is generally wrong. See [foo documentation]\n"

Done.

Quoted 3 lines

Also, like what was done for yield(), perhaps
some kernel-doc content would be useful.

Added to spinlocks.txt and spinlock.h

+/**
+ * spin_is_locked() - Check if a spinlock is being held.
+ * @lock: Lock to check.
+ *
+ * This function should normally not be used. Especially using it in
+ * WARN and BUG_ONs is usually incorrect or redundant.
+ * If you want to check if a lock is held in a function
+ * use lockdep_assert_held(). A lot of other usages are racy.
+ */

+spin_is_locked is a bad idea
+
+spin_is_locked checks if a lock is currently hold. On uniprocessor kernels +it always returns 0. In general this function should be avoided because most +uses of it are either redundant or broken.
+
+People often use spin_is_locked() to check if a particular lock is hold when a function +is called to enforce a locking discipline, like
+
+ WARN_ON(!spin_is_locked(!my_lock))
+
+or
+
+ BUG_ON(!spin_is_locked(!my_lock))
+
+or some variant of those.
+
+This does not work on uniprocessor kernels because they will always fail. +While there are ways around that they are ugly and not recommended. +Better use lockdep_assert_held(). This also only checks on a lock debugging +kernel (which you should occasionally run on your code anyways because +it catches many more problems).
+
+In generally this would be better done with static annotation anyways +(there's some support for it in sparse)
+
+ BUG_ON(spin_is_locked(obj->lock));
+ kfree(obj);
+
+Another usage is checking whether a lock is not hold when freeing an object. +However this is redundant because lock debugging supports this anyways +without explicit code. Just delete the BUG_ON.
+
+A third usage is to check in a console function if a lock is hold, to get +a panic crash dump out even when some other thread died in it.
+This is better implemented with spin_try_lock() et.al. and a timeout.
+
+Other usages are usually simply races.
+
+In summary just don't use it.

Signature · 1 line
[email protected] -- Speaking for myself only.