Skip to contentSkip to conversation

locking/rwsem: Make rwsem_is_contended() track status of OSQ

PATCH In review Waiman Long 2 participants 3 messages Mar 7, 2017 Locking primitives

lore
Share & tools
lore

Waiman Long

author reviewer
#0 lore raw

PATCH 1/1 locking/rwsem: Make rwsem_is_contended() track status of OSQ

It was found that the current rwsem_is_contended() function did not look at the status of the OSQ and hence would miss waiters on OSQ. So that function is now modified to look at the OSQ as well.

---
 include/linux/rwsem.h | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
include/linux/rwsem.h +11 -1
index dd1d142..9cb64e3 100644
@@ -71,8 +71,18 @@ static inline int rwsem_is_locked(struct rw_semaphore *sem)
 
 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
 #define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL
+
+static inline bool rwsem_osq_is_locked(struct rw_semaphore *sem)
+{
+	return osq_is_locked(&sem->osq);
+}
 #else
 #define __RWSEM_OPT_INIT(lockname)
+
+static inline bool rwsem_osq_is_locked(struct rw_semaphore *sem)
+{
+	return false;
+}
 #endif
 
 #define __RWSEM_INITIALIZER(name)				\
@@ -103,7 +113,7 @@ extern void __init_rwsem(struct rw_semaphore *sem, const char *name,
  */
 static inline int rwsem_is_contended(struct rw_semaphore *sem)
 {
-	return !list_empty(&sem->wait_list);
+	return !list_empty(&sem->wait_list) || rwsem_osq_is_locked(sem);
 }
 
 /*
Signature · 1 line
1.8.3.1
maintainer
#1 lore raw
Quoted 3 lines · Waiman Long

It was found that the current rwsem_is_contended() function did not look at the status of the OSQ and hence would miss waiters on OSQ. So that function is now modified to look at the OSQ as well.

Ideally I'd kill the entire function.

	if (need_resched() ||
	    rwsem_is_contended(&fs_info->commit_root_sem)) {
		if (wakeup)
			caching_ctl->progress = last;
		btrfs_release_path(path);
		up_read(&fs_info->commit_root_sem);
		mutex_unlock(&caching_ctl->mutex);
		cond_resched();
		mutex_lock(&caching_ctl->mutex);
		down_read(&fs_info->commit_root_sem);
		goto next;
	}

is the only user of it in the entire tree and it makes no bloody sense what so ever. rwsem is a preemptible lock after all.

author reviewer
#2 lore raw
Quoted 19 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long

It was found that the current rwsem_is_contended() function did not look at the status of the OSQ and hence would miss waiters on OSQ. So that function is now modified to look at the OSQ as well.

Ideally I'd kill the entire function.

	if (need_resched() ||
	    rwsem_is_contended(&fs_info->commit_root_sem)) {
		if (wakeup)
			caching_ctl->progress = last;
		btrfs_release_path(path);
		up_read(&fs_info->commit_root_sem);
		mutex_unlock(&caching_ctl->mutex);
		cond_resched();
		mutex_lock(&caching_ctl->mutex);
		down_read(&fs_info->commit_root_sem);
		goto next;
	}
Quoted 2 lines

is the only user of it in the entire tree and it makes no bloody sense what so ever. rwsem is a preemptible lock after all.

That works for me too. I do realize that there is only one user in the kernel, but I am hesitant to change it as I am not familiar with that piece of code.

Cheers,
Longman