Skip to contentSkip to conversation

locking/rwsem: Fix DEBUG_RWSEM warning from thaw_sup

RFC PATCH Superseded Waiman Long 6 participants 18 messages May 14, 2018 – May 15, 2018

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Waiman Long

author reviewer
#0 lore raw

v2 cover locking/rwsem: Fix DEBUG_RWSEM warning from thaw_sup

My original patch (https://lkml.org/lkml/2018/4/4/447) to fix this isse probably won't work. This is my second attempt to fix it.

I don't have the setup to reproduce the problem. Could someone try it to see if it can eliminate the warning?

Waiman Long (2):
locking/rwsem: Add a new RWSEM_WRITER_OWNED_NOSPIN flag locking/percpu-rwsem: Mark rwsem as non-spinnable in
percpu_rwsem_release()

 include/linux/percpu-rwsem.h |  6 +++---
 include/linux/rwsem.h        | 10 ++++++++++
 kernel/locking/rwsem-xadd.c  | 17 ++++++++---------
 kernel/locking/rwsem.c       | 16 +++++++++++++++-
 kernel/locking/rwsem.h       | 37 ++++++++++++++++++++++++++++++-------
 5 files changed, 66 insertions(+), 20 deletions(-)
Signature · 1 line
1.8.3.1
author reviewer
#1 lore raw

RFC PATCH v2 1/2 locking/rwsem: Add a new RWSEM_WRITER_OWNED_NOSPIN flag

There are use cases where a rwsem can be acquired by one task, but released by another task. In thess cases, it may not be appropriate for the lock waiters to spin on the task that acquires the lock. One example will be the filesystem freeze/thaw code.

To handle such use cases, a new RWSEM_WRITER_OWNED_NOSPIN flag can now be set in the owner field of the rwsem by the new rwsem_set_writer_owned_nospin() function to indicate that the rwsem is writer owned, but optimistic spinning on the rwsem should be disabled.

Later on, the new rwsem_set_writer_owned() function can be called to set the new owner, if it is known. This function should not be called without a prior rwsem_set_writer_owned_nospin() call.

---
 include/linux/rwsem.h       | 10 ++++++++++
 kernel/locking/rwsem-xadd.c | 17 ++++++++---------
 kernel/locking/rwsem.c      | 16 +++++++++++++++-
 kernel/locking/rwsem.h      | 37 ++++++++++++++++++++++++++++++-------
 4 files changed, 63 insertions(+), 17 deletions(-)
include/linux/rwsem.h +10 -0
index 56707d5..1ddf24b 100644
@@ -145,6 +145,16 @@ static inline int rwsem_is_contended(struct rw_semaphore *sem)
  */
 extern void downgrade_write(struct rw_semaphore *sem);
 
+#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
+extern void rwsem_set_writer_owned_nospin(struct rw_semaphore *sem);
+extern void rwsem_set_writer_owned(struct rw_semaphore *sem,
+				   struct task_struct *task);
+#else
+static inline void rwsem_set_writer_owned_nospin(struct rw_semaphore *sem) { }
+extern inline void rwsem_set_writer_owned(struct rw_semaphore *sem,
+					  struct task_struct *task) { }
+#endif
+
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 /*
  * nested locking. NOTE: rwsems are not allowed to recurse
kernel/locking/rwsem-xadd.c +8 -9
index e795908..a27dbb4 100644
@@ -357,11 +357,8 @@ static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem)
 
 	rcu_read_lock();
 	owner = READ_ONCE(sem->owner);
-	if (!rwsem_owner_is_writer(owner)) {
-		/*
-		 * Don't spin if the rwsem is readers owned.
-		 */
-		ret = !rwsem_owner_is_reader(owner);
+	if (!owner || !is_rwsem_owner_spinnable(owner)) {
+		ret = !owner;	/* !owner is spinnable */
 		goto done;
 	}
 
@@ -382,8 +379,10 @@ static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem)
 {
 	struct task_struct *owner = READ_ONCE(sem->owner);
 
-	if (!rwsem_owner_is_writer(owner))
-		goto out;
+	if (!owner)
+		return true;
+	else if (!is_rwsem_owner_spinnable(owner))
+		return false;
 
 	rcu_read_lock();
 	while (sem->owner == owner) {
@@ -408,12 +407,12 @@ static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem)
 		cpu_relax();
 	}
 	rcu_read_unlock();
-out:
+
 	/*
 	 * If there is a new owner or the owner is not set, we continue
 	 * spinning.
 	 */
-	return !rwsem_owner_is_reader(READ_ONCE(sem->owner));
+	return is_rwsem_owner_spinnable(READ_ONCE(sem->owner));
 }
 
 static bool rwsem_optimistic_spin(struct rw_semaphore *sem)
kernel/locking/rwsem.c +15 -1
index 30465a2..90e89ee 100644
@@ -130,7 +130,8 @@ void up_read(struct rw_semaphore *sem)
 void up_write(struct rw_semaphore *sem)
 {
 	rwsem_release(&sem->dep_map, 1, _RET_IP_);
-	DEBUG_RWSEMS_WARN_ON(sem->owner != current);
+	DEBUG_RWSEMS_WARN_ON((sem->owner != current) &&
+			     (sem->owner != RWSEM_WRITER_OWNED_NOSPIN));
 
 	rwsem_clear_owner(sem);
 	__up_write(sem);
@@ -222,4 +223,17 @@ void up_read_non_owner(struct rw_semaphore *sem)
 
 #endif
 
+#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
+void rwsem_set_writer_owned_nospin(struct rw_semaphore *sem)
+{
+	__rwsem_set_writer_owned_nospin(sem);
+}
+EXPORT_SYMBOL(rwsem_set_writer_owned_nospin);
 
+void rwsem_set_writer_owned(struct rw_semaphore *sem, struct task_struct *task)
+{
+	DEBUG_RWSEMS_WARN_ON(sem->owner != RWSEM_WRITER_OWNED_NOSPIN);
+	__rwsem_set_writer_owned(sem, task);
+}
+EXPORT_SYMBOL(rwsem_set_writer_owned);
+#endif
kernel/locking/rwsem.h +30 -7
index a17cba8..bbbd5a3 100644
@@ -11,10 +11,15 @@
  *  2) RWSEM_READER_OWNED
  *     - lock is currently or previously owned by readers (lock is free
  *       or not set by owner yet)
- *  3) Other non-zero value
- *     - a writer owns the lock
+ *  3) RWSEM_WRITER_OWNED_NOSPIN
+ *     - lock is owned by a writer whose lock ownership may be transfered to
+ *	 another task and so spinning on the lock owner should be disabled.
+ *  4) Other non-zero value
+ *     - a writer owns the lock and other writers can spin on the lock owner.
  */
-#define RWSEM_READER_OWNED	((struct task_struct *)1UL)
+#define RWSEM_READER_OWNED		((struct task_struct *)1UL)
+#define RWSEM_WRITER_OWNED_NOSPIN	((struct task_struct *)2UL)
+#define RWSEM_NOSPIN_MASK		3UL
 
 #ifdef CONFIG_DEBUG_RWSEMS
 # define DEBUG_RWSEMS_WARN_ON(c)	DEBUG_LOCKS_WARN_ON(c)
@@ -51,14 +56,32 @@ static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
 		WRITE_ONCE(sem->owner, RWSEM_READER_OWNED);
 }
 
-static inline bool rwsem_owner_is_writer(struct task_struct *owner)
+/*
+ * Mark the rwsem as writer owned, but optimistic spinning should be
+ * disabled.
+ *
+ * The caller must make sure that the rwsem is really writer owned
+ * and the lock won't be freed concurrently with this call.
+ */
+static inline void __rwsem_set_writer_owned_nospin(struct rw_semaphore *sem)
+{
+	WRITE_ONCE(sem->owner, RWSEM_WRITER_OWNED_NOSPIN);
+}
+
+static inline void __rwsem_set_writer_owned(struct rw_semaphore *sem,
+					    struct task_struct *task)
 {
-	return owner && owner != RWSEM_READER_OWNED;
+	WRITE_ONCE(sem->owner, task);
 }
 
-static inline bool rwsem_owner_is_reader(struct task_struct *owner)
+/*
+ * Return true if the a rwsem waiter can spin on the rwsem's owner
+ * and steal the lock.
+ * N.B. !owner is considered spinnable.
+ */
+static inline bool is_rwsem_owner_spinnable(struct task_struct *owner)
 {
-	return owner == RWSEM_READER_OWNED;
+	return !((unsigned long)owner & RWSEM_NOSPIN_MASK);
 }
 #else
 static inline void rwsem_set_owner(struct rw_semaphore *sem)
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [RFC PATCH v2 1/2] locking/rwsem: Add a new RWSEM_WRITER_OWNED_NOSPIN flag

Quoted 10 lines · Waiman Long

There are use cases where a rwsem can be acquired by one task, but released by another task. In thess cases, it may not be appropriate for the lock waiters to spin on the task that acquires the lock. One example will be the filesystem freeze/thaw code.

To handle such use cases, a new RWSEM_WRITER_OWNED_NOSPIN flag can now be set in the owner field of the rwsem by the new rwsem_set_writer_owned_nospin() function to indicate that the rwsem is writer owned, but optimistic spinning on the rwsem should be disabled.

Quoted 5 lines

Later on, the new rwsem_set_writer_owned() function can be called to set the new owner, if it is known. This function should not be called without a prior rwsem_set_writer_owned_nospin() call.

Makes sense to me. one nit.

kernel/locking/rwsem.h static inline void rwsem_set_reader_owned(struct rw_semaphore *sem) · line 72

Quoted 3 lines
kernel/locking/rwsem.h +2 -0
+static inline void __rwsem_set_writer_owned(struct rw_semaphore *sem,
+                                           struct task_struct *task)

rwsem_set_owner() doesn't pass in task argument and IMO __rwsem_set_writer_owned() shouldn't either.

Thanks,
Amir.

maintainer
#3 lore raw

Re: [RFC PATCH v2 1/2] locking/rwsem: Add a new RWSEM_WRITER_OWNED_NOSPIN flag

In reply to Waiman Long
Quoted 5 lines · Waiman Long

There are use cases where a rwsem can be acquired by one task, but released by another task. In thess cases, it may not be appropriate for the lock waiters to spin on the task that acquires the lock. One example will be the filesystem freeze/thaw code.

Quoted 8 lines

To handle such use cases, a new RWSEM_WRITER_OWNED_NOSPIN flag can now be set in the owner field of the rwsem by the new rwsem_set_writer_owned_nospin() function to indicate that the rwsem is writer owned, but optimistic spinning on the rwsem should be disabled.

Later on, the new rwsem_set_writer_owned() function can be called to set the new owner, if it is known. This function should not be called without a prior rwsem_set_writer_owned_nospin() call.

Urgh.. no please don't do this. Aside from the horrible naming, do not expose 'set-owner' semantics. Can't we just stick to the existing _non_owner() interface without further polluting the API?

author reviewer
#4 lore raw

RFC PATCH v2 2/2 locking/percpu-rwsem: Mark rwsem as non-spinnable in percpu_rwsem_release()

In reply to Waiman Long

The percpu_rwsem_release() is called when the ownership of the embedded rwsem is to be transferred to another task. The new owner, however, may take a while to get the ownership of the lock via percpu_rwsem_acquire(). During that period, the rwsem is now marked as writer-owned with no optimistic spinning.

---
 include/linux/percpu-rwsem.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/percpu-rwsem.h +3 -3
index b1f37a8..dd37102 100644
@@ -131,16 +131,16 @@ static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
 					bool read, unsigned long ip)
 {
 	lock_release(&sem->rw_sem.dep_map, 1, ip);
-#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
 	if (!read)
-		sem->rw_sem.owner = NULL;
-#endif
+		rwsem_set_writer_owned_nospin(&sem->rw_sem);
 }
 
 static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem,
 					bool read, unsigned long ip)
 {
 	lock_acquire(&sem->rw_sem.dep_map, 0, 1, read, 1, NULL, ip);
+	if (!read)
+		rwsem_set_writer_owned(&sem->rw_sem, current);
 }
 
 #endif
Signature · 1 line
1.8.3.1
#5 lore raw

Re: [RFC PATCH v2 2/2] locking/percpu-rwsem: Mark rwsem as non-spinnable in percpu_rwsem_release()

Quoted 6 lines · Waiman Long

The percpu_rwsem_release() is called when the ownership of the embedded rwsem is to be transferred to another task. The new owner, however, may take a while to get the ownership of the lock via percpu_rwsem_acquire(). During that period, the rwsem is now marked as writer-owned with no optimistic spinning.

Waiman,

Thanks for the fix. I will test it soon.

For this commit message I suggest that you add parts of the reproducer found here:
https://marc.info/?l=linux-fsdevel&m=152622016219975&w=2

Thanks,
Amir.

Quoted 32 lines
---
 include/linux/percpu-rwsem.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/percpu-rwsem.h +3 -4
index b1f37a8..dd37102 100644
@@ -131,16 +131,16 @@ static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
                                        bool read, unsigned long ip)
 {
        lock_release(&sem->rw_sem.dep_map, 1, ip);
-#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
        if (!read)
-               sem->rw_sem.owner = NULL;
-#endif
+               rwsem_set_writer_owned_nospin(&sem->rw_sem);
 }
 
 static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem,
                                        bool read, unsigned long ip)
 {
        lock_acquire(&sem->rw_sem.dep_map, 0, 1, read, 1, NULL, ip);
+       if (!read)
+               rwsem_set_writer_owned(&sem->rw_sem, current);
 }
 
 #endif
--

1.8.3.1

Tested-by
#6 lore raw

Re: [RFC PATCH v2 2/2] locking/percpu-rwsem: Mark rwsem as non-spinnable in percpu_rwsem_release()

Quoted 8 lines · Amir Goldstein
Quoted 6 lines · Waiman Long

The percpu_rwsem_release() is called when the ownership of the embedded rwsem is to be transferred to another task. The new owner, however, may take a while to get the ownership of the lock via percpu_rwsem_acquire(). During that period, the rwsem is now marked as writer-owned with no optimistic spinning.

Quoted 8 lines

Waiman,

Thanks for the fix. I will test it soon.

For this commit message I suggest that you add parts of the reproducer found here:
https://marc.info/?l=linux-fsdevel&m=152622016219975&w=2

fsfreeze is happy with these changes.

You may add:

Thanks,
Amir.

author reviewer
#7 lore raw

Re: [RFC PATCH v2 2/2] locking/percpu-rwsem: Mark rwsem as non-spinnable in percpu_rwsem_release()

In reply to Amir Goldstein
Quoted 9 lines · Amir Goldstein
Quoted 6 lines · Waiman Long

The percpu_rwsem_release() is called when the ownership of the embedded rwsem is to be transferred to another task. The new owner, however, may take a while to get the ownership of the lock via percpu_rwsem_acquire(). During that period, the rwsem is now marked as writer-owned with no optimistic spinning.

Waiman,

Quoted 8 lines

Thanks for the fix. I will test it soon.

For this commit message I suggest that you add parts of the reproducer found here:
https://marc.info/?l=linux-fsdevel&m=152622016219975&w=2

Thanks,
Amir.

Sure. I will add that to the commit log.

Cheers,
Longman

maintainer
#8 lore raw

Re: [RFC PATCH v2 2/2] locking/percpu-rwsem: Mark rwsem as non-spinnable in percpu_rwsem_release()

In reply to Waiman Long
Quoted 5 lines · Waiman Long

The percpu_rwsem_release() is called when the ownership of the embedded rwsem is to be transferred to another task. The new owner, however, may take a while to get the ownership of the lock via percpu_rwsem_acquire(). During that period, the rwsem is now marked as writer-owned with no optimistic spinning.

This does not explain the problem sufficiently to even begin considering if the proposed solution is sensible.

reviewer
#9 lore raw

Re: [RFC PATCH v2 2/2] locking/percpu-rwsem: Mark rwsem as non-spinnable in percpu_rwsem_release()

Quoted 9 lines · Peter Zijlstra
Quoted 5 lines · Waiman Long

The percpu_rwsem_release() is called when the ownership of the embedded rwsem is to be transferred to another task. The new owner, however, may take a while to get the ownership of the lock via percpu_rwsem_acquire(). During that period, the rwsem is now marked as writer-owned with no optimistic spinning.

This does not explain the problem sufficiently to even begin considering if the proposed solution is sensible.

So the original problem is following: There is percpu_rw_semaphore in super_block which is used to implement filesystem freezing (actually three of them but that's not really substantial here). This semaphore is acquired for writing when a fs is frozen (i.e., in response to a syscall) and we return to userspace with this semaphore held. Later someone else calls another syscall to unfreeze the filesystem which drops the semaphore.

Now this behavior upsets lockdep and that's why we fool it by telling the semaphore got released before returning to userspace (through percpu_rwsem_release() helper) and similarly we tell lockdep we've got the semaphore when an unfreeze syscall is called by percpu_rwsem_acquire(). Now Amir has discovered that also rwsem debugging code gets confused by this behavior and previously also someone noticed that rwsem spinning does not make sense and can be broken by this behavior. So these patches from Waiman try to fix up all these problems...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#10 lore raw

Re: [RFC PATCH v2 2/2] locking/percpu-rwsem: Mark rwsem as non-spinnable in percpu_rwsem_release()

Quoted 7 lines · Jan Kara

Now this behavior upsets lockdep and that's why we fool it by telling the semaphore got released before returning to userspace (through percpu_rwsem_release() helper) and similarly we tell lockdep we've got the semaphore when an unfreeze syscall is called by percpu_rwsem_acquire(). Now Amir has discovered that also rwsem debugging code gets confused by this behavior

Yes, plus someone else has already reported the problem a month ago,

Quoted 2 lines

and previously also someone noticed that rwsem spinning does not make sense and can be broken by this behavior.

Well, this doesn't really matter but again, freeze_super() checks frozen == SB_UNFROZEN under sb->s_umount and only then does sb_wait_write(), when the previous writer has already realeased this lock. So the new writer will never spin after lockdep_sb_freeze_release() clears ->owner.

Oleg.

maintainer
#11 lore raw

Re: [RFC PATCH v2 2/2] locking/percpu-rwsem: Mark rwsem as non-spinnable in percpu_rwsem_release()

In reply to Waiman Long
Quoted 23 lines · Waiman Long

The percpu_rwsem_release() is called when the ownership of the embedded rwsem is to be transferred to another task. The new owner, however, may take a while to get the ownership of the lock via percpu_rwsem_acquire(). During that period, the rwsem is now marked as writer-owned with no optimistic spinning.

---
 include/linux/percpu-rwsem.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/percpu-rwsem.h +0 -3
index b1f37a8..dd37102 100644
@@ -131,16 +131,16 @@ static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
 					bool read, unsigned long ip)
 {
 	lock_release(&sem->rw_sem.dep_map, 1, ip);
-#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
 	if (!read)
-		sem->rw_sem.owner = NULL;
-#endif

include/linux/percpu-rwsem.h static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem, · line 144

Quoted 10 lines
include/linux/percpu-rwsem.h +3 -0
+		rwsem_set_writer_owned_nospin(&sem->rw_sem);
 }
 
 static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem,
 					bool read, unsigned long ip)
 {
 	lock_acquire(&sem->rw_sem.dep_map, 0, 1, read, 1, NULL, ip);
+	if (!read)
+		rwsem_set_writer_owned(&sem->rw_sem, current);
 }

So what's wrong with adding:

	if (!read)
		sem->rw_sem.owner = current;

?

Afaict the whole .owner=NULL thing in release already stops the spinners dead, and the above 'fixes' the debug splat. And this avoids exposing that horrible interface and keeps the mucking private to rwsem/percpu_rwsem.

#12 lore raw

Re: [RFC PATCH v2 2/2] locking/percpu-rwsem: Mark rwsem as non-spinnable in percpu_rwsem_release()

Quoted 5 lines · Peter Zijlstra

So what's wrong with adding:

	if (!read)
		sem->rw_sem.owner = current;

Agreed, I have already suggested this change twice. Except we obviously need to check CONFIG_RWSEM_SPIN_ON_OWNER (->owner doesn't exists otherwise) or even CONFIG_DEBUG_RWSEMS to make the purpose more clear.

Quoted 1 line

Afaict the whole .owner=NULL thing in release already stops the spinners

Not really, the new writer will spin in this case, afaics.

But this is another problem and probably we do not care. The new writer is almost impossible in this particular case, another freeze_super() should notice frozen != SB_UNFROZEN and return EBUSY.

Quoted 1 line

and the above 'fixes' the debug splat.

Yes.

Waiman, can't we trivially fix the problem first? Then we can add the helpers and think about other improvements.

Oleg.

maintainer
#13 lore raw

Re: [RFC PATCH v2 2/2] locking/percpu-rwsem: Mark rwsem as non-spinnable in percpu_rwsem_release()

Quoted 10 lines · Oleg Nesterov
Quoted 5 lines · Peter Zijlstra

So what's wrong with adding:

	if (!read)
		sem->rw_sem.owner = current;

Agreed, I have already suggested this change twice. Except we obviously need to check CONFIG_RWSEM_SPIN_ON_OWNER (->owner doesn't exists otherwise) or even CONFIG_DEBUG_RWSEMS to make the purpose more clear.

Right, details ;-)

Quoted 7 lines
Quoted 1 line

Afaict the whole .owner=NULL thing in release already stops the spinners

Not really, the new writer will spin in this case, afaics.

But this is another problem and probably we do not care. The new writer is almost impossible in this particular case, another freeze_super() should notice frozen != SB_UNFROZEN and return EBUSY.

rwsem_spin_on_owner() checks rwsem_owner_is_writer(), which does owner && owner != RWSEM_READER_OWNED, which will fail for !owner.

Or am I completely confused again?

Quoted 6 lines
Quoted 1 line

and the above 'fixes' the debug splat.

Yes.

Waiman, can't we trivially fix the problem first? Then we can add the helpers and think about other improvements.

It is really simple; we're not going to add public (and EXPORT'ed to boot) interfaces to rwsem for this.

#14 lore raw

Re: [RFC PATCH v2 2/2] locking/percpu-rwsem: Mark rwsem as non-spinnable in percpu_rwsem_release()

Quoted 11 lines · Peter Zijlstra
Quoted 8 lines
Quoted 2 lines

Afaict the whole .owner=NULL thing in release already stops the spinners

Not really, the new writer will spin in this case, afaics.

But this is another problem and probably we do not care. The new writer is almost impossible in this particular case, another freeze_super() should notice frozen != SB_UNFROZEN and return EBUSY.

rwsem_spin_on_owner() checks rwsem_owner_is_writer(), which does owner && owner != RWSEM_READER_OWNED, which will fail for !owner.

Yep. So rwsem_spin_on_owner() goes to "out:" and returns !rwsem_owner_is_reader() == T.

IOW, afaics owner == NULL means "spin unconditionally", I guess this is for the case when the new writer is going to do rwsem_set_owner() or up_write() has already called rwsem_clear_owner() but didn't do up_write() yet.

Probably makes sense, but the code is not very clean,

Quoted 1 line

Or am I completely confused again?

Or me, I am not sure.

Oleg.

maintainer
#15 lore raw

Re: [RFC PATCH v2 2/2] locking/percpu-rwsem: Mark rwsem as non-spinnable in percpu_rwsem_release()

Quoted 13 lines · Oleg Nesterov
Quoted 11 lines · Peter Zijlstra
Quoted 8 lines
Quoted 2 lines

Afaict the whole .owner=NULL thing in release already stops the spinners

Not really, the new writer will spin in this case, afaics.

But this is another problem and probably we do not care. The new writer is almost impossible in this particular case, another freeze_super() should notice frozen != SB_UNFROZEN and return EBUSY.

rwsem_spin_on_owner() checks rwsem_owner_is_writer(), which does owner && owner != RWSEM_READER_OWNED, which will fail for !owner.

Quoted 8 lines

Yep. So rwsem_spin_on_owner() goes to "out:" and returns !rwsem_owner_is_reader() == T.

IOW, afaics owner == NULL means "spin unconditionally", I guess this is for the case when the new writer is going to do rwsem_set_owner() or up_write() has already called rwsem_clear_owner() but didn't do up_write() yet.

Probably makes sense, but the code is not very clean,

Arrgh, you're right... I hate this rwsem code.

Some day I'll finish the atomic_long_t version, which similar to mutex, merges the owner and 'count' fields.

author reviewer
#16 lore raw

Re: [RFC PATCH v2 2/2] locking/percpu-rwsem: Mark rwsem as non-spinnable in percpu_rwsem_release()

In reply to Peter Zijlstra
Quoted 36 lines · Peter Zijlstra
Quoted 23 lines · Waiman Long

The percpu_rwsem_release() is called when the ownership of the embedded rwsem is to be transferred to another task. The new owner, however, may take a while to get the ownership of the lock via percpu_rwsem_acquire(). During that period, the rwsem is now marked as writer-owned with no optimistic spinning.

---
 include/linux/percpu-rwsem.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/percpu-rwsem.h +0 -3
index b1f37a8..dd37102 100644
@@ -131,16 +131,16 @@ static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
 					bool read, unsigned long ip)
 {
 	lock_release(&sem->rw_sem.dep_map, 1, ip);
-#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
 	if (!read)
-		sem->rw_sem.owner = NULL;
-#endif

include/linux/percpu-rwsem.h static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem, · line 144

Quoted 10 lines
include/linux/percpu-rwsem.h +3 -0
+		rwsem_set_writer_owned_nospin(&sem->rw_sem);
 }
 
 static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem,
 					bool read, unsigned long ip)
 {
 	lock_acquire(&sem->rw_sem.dep_map, 0, 1, read, 1, NULL, ip);
+	if (!read)
+		rwsem_set_writer_owned(&sem->rw_sem, current);
 }

So what's wrong with adding:

Quoted 4 lines
	if (!read)
		sem->rw_sem.owner = current;

?

Yes, we can certainly do that within a "#ifdef" block.

Quoted 5 lines

Afaict the whole .owner=NULL thing in release already stops the spinners dead, and the above 'fixes' the debug splat. And this avoids exposing that horrible interface and keeps the mucking private to rwsem/percpu_rwsem.

Actually setting owner to NULL does not stop spinning. The code just assume that the lock is going to be freed and spin in the outer loop. We need some special value to indicate that spinning should be stopped. How about just exposing a special value for that in linux/rwsem.h? Any suggestion for a good name?

Cheers,
Longman

#17 lore raw

Re: [RFC PATCH v2 2/2] locking/percpu-rwsem: Mark rwsem as non-spinnable in percpu_rwsem_release()

Quoted 10 lines · Waiman Long
Quoted 4 lines

Afaict the whole .owner=NULL thing in release already stops the spinners dead, and the above 'fixes' the debug splat. And this avoids exposing that horrible interface and keeps the mucking private to rwsem/percpu_rwsem.

Actually setting owner to NULL does not stop spinning. The code just assume that the lock is going to be freed and spin in the outer loop. We need some special value to indicate that spinning should be stopped. How about just exposing a special value for that in linux/rwsem.h? Any suggestion for a good name?

RWSEM_NO_OWNER