[PATCH RFC 1/3] locking/rwsem: make owner helpers globally available

Lance Yang posted 3 patches 6 months ago
There is a newer version of this series
[PATCH RFC 1/3] locking/rwsem: make owner helpers globally available
Posted by Lance Yang 6 months ago
From: Lance Yang <lance.yang@linux.dev>

In preparation for extending blocker tracking to support rwsems, make the
rwsem_owner() and is_rwsem_reader_owned() helpers globally available for
determining if the blocker is a writer or one of the readers.

Signed-off-by: Lance Yang <lance.yang@linux.dev>
---
 include/linux/rwsem.h  | 12 ++++++++++++
 kernel/locking/rwsem.c |  8 +++++---
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h
index c8b543d428b0..544853bed5b9 100644
--- a/include/linux/rwsem.h
+++ b/include/linux/rwsem.h
@@ -132,6 +132,18 @@ static inline int rwsem_is_contended(struct rw_semaphore *sem)
 	return !list_empty(&sem->wait_list);
 }
 
+#if defined(CONFIG_DEBUG_RWSEMS) || defined(CONFIG_DETECT_HUNG_TASK_BLOCKER)
+/*
+ * Return just the real task structure pointer of the owner
+ */
+extern struct task_struct *rwsem_owner(struct rw_semaphore *sem);
+
+/*
+ * Return true if the rwsem is owned by a reader.
+ */
+extern bool is_rwsem_reader_owned(struct rw_semaphore *sem);
+#endif
+
 #else /* !CONFIG_PREEMPT_RT */
 
 #include <linux/rwbase_rt.h>
diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
index 2ddb827e3bea..6cb29442d4fc 100644
--- a/kernel/locking/rwsem.c
+++ b/kernel/locking/rwsem.c
@@ -181,11 +181,11 @@ static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
 	__rwsem_set_reader_owned(sem, current);
 }
 
-#ifdef CONFIG_DEBUG_RWSEMS
+#if defined(CONFIG_DEBUG_RWSEMS) || defined(CONFIG_DETECT_HUNG_TASK_BLOCKER)
 /*
  * Return just the real task structure pointer of the owner
  */
-static inline struct task_struct *rwsem_owner(struct rw_semaphore *sem)
+struct task_struct *rwsem_owner(struct rw_semaphore *sem)
 {
 	return (struct task_struct *)
 		(atomic_long_read(&sem->owner) & ~RWSEM_OWNER_FLAGS_MASK);
@@ -194,7 +194,7 @@ static inline struct task_struct *rwsem_owner(struct rw_semaphore *sem)
 /*
  * Return true if the rwsem is owned by a reader.
  */
-static inline bool is_rwsem_reader_owned(struct rw_semaphore *sem)
+bool is_rwsem_reader_owned(struct rw_semaphore *sem)
 {
 	/*
 	 * Check the count to see if it is write-locked.
@@ -205,7 +205,9 @@ static inline bool is_rwsem_reader_owned(struct rw_semaphore *sem)
 		return false;
 	return rwsem_test_oflags(sem, RWSEM_READER_OWNED);
 }
+#endif
 
+#ifdef CONFIG_DEBUG_RWSEMS
 /*
  * With CONFIG_DEBUG_RWSEMS configured, it will make sure that if there
  * is a task pointer in owner of a reader-owned rwsem, it will be the
-- 
2.49.0
Re: [PATCH RFC 1/3] locking/rwsem: make owner helpers globally available
Posted by Masami Hiramatsu (Google) 5 months, 3 weeks ago
On Thu, 12 Jun 2025 12:19:24 +0800
Lance Yang <ioworker0@gmail.com> wrote:

> From: Lance Yang <lance.yang@linux.dev>
> 
> In preparation for extending blocker tracking to support rwsems, make the
> rwsem_owner() and is_rwsem_reader_owned() helpers globally available for
> determining if the blocker is a writer or one of the readers.
> 

Looks good to me.

Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Thanks!

> Signed-off-by: Lance Yang <lance.yang@linux.dev>
> ---
>  include/linux/rwsem.h  | 12 ++++++++++++
>  kernel/locking/rwsem.c |  8 +++++---
>  2 files changed, 17 insertions(+), 3 deletions(-)
> 
> diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h
> index c8b543d428b0..544853bed5b9 100644
> --- a/include/linux/rwsem.h
> +++ b/include/linux/rwsem.h
> @@ -132,6 +132,18 @@ static inline int rwsem_is_contended(struct rw_semaphore *sem)
>  	return !list_empty(&sem->wait_list);
>  }
>  
> +#if defined(CONFIG_DEBUG_RWSEMS) || defined(CONFIG_DETECT_HUNG_TASK_BLOCKER)
> +/*
> + * Return just the real task structure pointer of the owner
> + */
> +extern struct task_struct *rwsem_owner(struct rw_semaphore *sem);
> +
> +/*
> + * Return true if the rwsem is owned by a reader.
> + */
> +extern bool is_rwsem_reader_owned(struct rw_semaphore *sem);
> +#endif
> +
>  #else /* !CONFIG_PREEMPT_RT */
>  
>  #include <linux/rwbase_rt.h>
> diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
> index 2ddb827e3bea..6cb29442d4fc 100644
> --- a/kernel/locking/rwsem.c
> +++ b/kernel/locking/rwsem.c
> @@ -181,11 +181,11 @@ static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
>  	__rwsem_set_reader_owned(sem, current);
>  }
>  
> -#ifdef CONFIG_DEBUG_RWSEMS
> +#if defined(CONFIG_DEBUG_RWSEMS) || defined(CONFIG_DETECT_HUNG_TASK_BLOCKER)
>  /*
>   * Return just the real task structure pointer of the owner
>   */
> -static inline struct task_struct *rwsem_owner(struct rw_semaphore *sem)
> +struct task_struct *rwsem_owner(struct rw_semaphore *sem)
>  {
>  	return (struct task_struct *)
>  		(atomic_long_read(&sem->owner) & ~RWSEM_OWNER_FLAGS_MASK);
> @@ -194,7 +194,7 @@ static inline struct task_struct *rwsem_owner(struct rw_semaphore *sem)
>  /*
>   * Return true if the rwsem is owned by a reader.
>   */
> -static inline bool is_rwsem_reader_owned(struct rw_semaphore *sem)
> +bool is_rwsem_reader_owned(struct rw_semaphore *sem)
>  {
>  	/*
>  	 * Check the count to see if it is write-locked.
> @@ -205,7 +205,9 @@ static inline bool is_rwsem_reader_owned(struct rw_semaphore *sem)
>  		return false;
>  	return rwsem_test_oflags(sem, RWSEM_READER_OWNED);
>  }
> +#endif
>  
> +#ifdef CONFIG_DEBUG_RWSEMS
>  /*
>   * With CONFIG_DEBUG_RWSEMS configured, it will make sure that if there
>   * is a task pointer in owner of a reader-owned rwsem, it will be the
> -- 
> 2.49.0
> 


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>
Re: [PATCH RFC 1/3] locking/rwsem: make owner helpers globally available
Posted by Lance Yang 5 months, 3 weeks ago

On 2025/6/24 08:17, Masami Hiramatsu (Google) wrote:
> On Thu, 12 Jun 2025 12:19:24 +0800
> Lance Yang <ioworker0@gmail.com> wrote:
> 
>> From: Lance Yang <lance.yang@linux.dev>
>>
>> In preparation for extending blocker tracking to support rwsems, make the
>> rwsem_owner() and is_rwsem_reader_owned() helpers globally available for
>> determining if the blocker is a writer or one of the readers.
>>
> 
> Looks good to me.
> 
> Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> 
> Thanks!

Thanks for taking the time to review!
Lance

> 
>> Signed-off-by: Lance Yang <lance.yang@linux.dev>
>> ---
>>   include/linux/rwsem.h  | 12 ++++++++++++
>>   kernel/locking/rwsem.c |  8 +++++---
>>   2 files changed, 17 insertions(+), 3 deletions(-)
>>
>> diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h
>> index c8b543d428b0..544853bed5b9 100644
>> --- a/include/linux/rwsem.h
>> +++ b/include/linux/rwsem.h
>> @@ -132,6 +132,18 @@ static inline int rwsem_is_contended(struct rw_semaphore *sem)
>>   	return !list_empty(&sem->wait_list);
>>   }
>>   
>> +#if defined(CONFIG_DEBUG_RWSEMS) || defined(CONFIG_DETECT_HUNG_TASK_BLOCKER)
>> +/*
>> + * Return just the real task structure pointer of the owner
>> + */
>> +extern struct task_struct *rwsem_owner(struct rw_semaphore *sem);
>> +
>> +/*
>> + * Return true if the rwsem is owned by a reader.
>> + */
>> +extern bool is_rwsem_reader_owned(struct rw_semaphore *sem);
>> +#endif
>> +
>>   #else /* !CONFIG_PREEMPT_RT */
>>   
>>   #include <linux/rwbase_rt.h>
>> diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
>> index 2ddb827e3bea..6cb29442d4fc 100644
>> --- a/kernel/locking/rwsem.c
>> +++ b/kernel/locking/rwsem.c
>> @@ -181,11 +181,11 @@ static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)
>>   	__rwsem_set_reader_owned(sem, current);
>>   }
>>   
>> -#ifdef CONFIG_DEBUG_RWSEMS
>> +#if defined(CONFIG_DEBUG_RWSEMS) || defined(CONFIG_DETECT_HUNG_TASK_BLOCKER)
>>   /*
>>    * Return just the real task structure pointer of the owner
>>    */
>> -static inline struct task_struct *rwsem_owner(struct rw_semaphore *sem)
>> +struct task_struct *rwsem_owner(struct rw_semaphore *sem)
>>   {
>>   	return (struct task_struct *)
>>   		(atomic_long_read(&sem->owner) & ~RWSEM_OWNER_FLAGS_MASK);
>> @@ -194,7 +194,7 @@ static inline struct task_struct *rwsem_owner(struct rw_semaphore *sem)
>>   /*
>>    * Return true if the rwsem is owned by a reader.
>>    */
>> -static inline bool is_rwsem_reader_owned(struct rw_semaphore *sem)
>> +bool is_rwsem_reader_owned(struct rw_semaphore *sem)
>>   {
>>   	/*
>>   	 * Check the count to see if it is write-locked.
>> @@ -205,7 +205,9 @@ static inline bool is_rwsem_reader_owned(struct rw_semaphore *sem)
>>   		return false;
>>   	return rwsem_test_oflags(sem, RWSEM_READER_OWNED);
>>   }
>> +#endif
>>   
>> +#ifdef CONFIG_DEBUG_RWSEMS
>>   /*
>>    * With CONFIG_DEBUG_RWSEMS configured, it will make sure that if there
>>    * is a task pointer in owner of a reader-owned rwsem, it will be the
>> -- 
>> 2.49.0
>>
> 
>