* [PATCH] locking/rwsem: Remove unnessary check in rwsem_down_read_slowpath()
@ 2023-11-08 10:56 Haifeng Xu
2023-11-08 14:04 ` Waiman Long
0 siblings, 1 reply; 7+ messages in thread
From: Haifeng Xu @ 2023-11-08 10:56 UTC (permalink / raw)
To: longman; +Cc: peterz, mingo, will, boqun.feng, linux-kernel, Haifeng Xu
When the owner of rw_semaphore is reader, the count can't be
RWSEM_WRITER_LOCKED, so there is no need to check it.
Signed-off-by: Haifeng Xu <haifeng.xu@shopee.com>
---
kernel/locking/rwsem.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
index 2340b6d90ec6..7a4d8a9ebd9c 100644
--- a/kernel/locking/rwsem.c
+++ b/kernel/locking/rwsem.c
@@ -1005,8 +1005,7 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat
* waiter, don't attempt optimistic lock stealing if the lock is
* currently owned by readers.
*/
- if ((atomic_long_read(&sem->owner) & RWSEM_READER_OWNED) &&
- (rcnt > 1) && !(count & RWSEM_WRITER_LOCKED))
+ if ((atomic_long_read(&sem->owner) & RWSEM_READER_OWNED) && (rcnt > 1))
goto queue;
/*
--
2.25.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] locking/rwsem: Remove unnessary check in rwsem_down_read_slowpath()
2023-11-08 10:56 [PATCH] locking/rwsem: Remove unnessary check in rwsem_down_read_slowpath() Haifeng Xu
@ 2023-11-08 14:04 ` Waiman Long
2023-11-09 3:17 ` Haifeng Xu
0 siblings, 1 reply; 7+ messages in thread
From: Waiman Long @ 2023-11-08 14:04 UTC (permalink / raw)
To: Haifeng Xu; +Cc: peterz, mingo, will, boqun.feng, linux-kernel
On 11/8/23 05:56, Haifeng Xu wrote:
> When the owner of rw_semaphore is reader, the count can't be
> RWSEM_WRITER_LOCKED, so there is no need to check it.
>
> Signed-off-by: Haifeng Xu <haifeng.xu@shopee.com>
> ---
> kernel/locking/rwsem.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
> index 2340b6d90ec6..7a4d8a9ebd9c 100644
> --- a/kernel/locking/rwsem.c
> +++ b/kernel/locking/rwsem.c
> @@ -1005,8 +1005,7 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat
> * waiter, don't attempt optimistic lock stealing if the lock is
> * currently owned by readers.
> */
> - if ((atomic_long_read(&sem->owner) & RWSEM_READER_OWNED) &&
> - (rcnt > 1) && !(count & RWSEM_WRITER_LOCKED))
> + if ((atomic_long_read(&sem->owner) & RWSEM_READER_OWNED) && (rcnt > 1))
> goto queue;
>
> /*
Unlike RWSEM_WRITER_LOCKED bit in count, the RWSEM_READER_OWNED bit in
owner is just a hint, not an authoritative state of the rwsem. So it is
possible that both the RWSEM_READER_OWNED bit can be set in owner and
RWSEM_WRITER_LOCKED bit set in count in a transition period right after
RWSEM_WRITER_LOCKED bit is set. So the RWSEM_WRITER_LOCKED check can
still provide some value. We should probably update the comment to
reflect that.
Cheers,
Longman
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] locking/rwsem: Remove unnessary check in rwsem_down_read_slowpath()
2023-11-08 14:04 ` Waiman Long
@ 2023-11-09 3:17 ` Haifeng Xu
2023-11-10 6:54 ` Tang Yizhou
0 siblings, 1 reply; 7+ messages in thread
From: Haifeng Xu @ 2023-11-09 3:17 UTC (permalink / raw)
To: Waiman Long; +Cc: peterz, mingo, will, boqun.feng, linux-kernel
On 2023/11/8 22:04, Waiman Long wrote:
> On 11/8/23 05:56, Haifeng Xu wrote:
>> When the owner of rw_semaphore is reader, the count can't be
>> RWSEM_WRITER_LOCKED, so there is no need to check it.
>>
>> Signed-off-by: Haifeng Xu <haifeng.xu@shopee.com>
>> ---
>> kernel/locking/rwsem.c | 3 +--
>> 1 file changed, 1 insertion(+), 2 deletions(-)
>>
>> diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c
>> index 2340b6d90ec6..7a4d8a9ebd9c 100644
>> --- a/kernel/locking/rwsem.c
>> +++ b/kernel/locking/rwsem.c
>> @@ -1005,8 +1005,7 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat
>> * waiter, don't attempt optimistic lock stealing if the lock is
>> * currently owned by readers.
>> */
>> - if ((atomic_long_read(&sem->owner) & RWSEM_READER_OWNED) &&
>> - (rcnt > 1) && !(count & RWSEM_WRITER_LOCKED))
>> + if ((atomic_long_read(&sem->owner) & RWSEM_READER_OWNED) && (rcnt > 1))
>> goto queue;
>> /*
>
> Unlike RWSEM_WRITER_LOCKED bit in count, the RWSEM_READER_OWNED bit in owner is just a hint, not an authoritative state of the rwsem. So it is possible that both the RWSEM_READER_OWNED bit can be set in owner and RWSEM_WRITER_LOCKED bit set in count in a transition period right after RWSEM_WRITER_LOCKED bit is set.
reader writer reader
acquire
release
rwsem_write_trylock
set RWSEM_WRITER_LOCKED
rwsem_down_read_slowpath
set owner
If prev lock holder is a reader, when it releases the lock, the owner isn't cleared(CONFIG_DEBUG_RWSEMS isn't enabled).
A writer comes and can set the RWSEM_WRITER_LOCKED bit succsessfully, then a new reader run into slow path, before
the writer set the owner, the new reader will see that both the RWSEM_READER_OWNED bit and RWSEM_WRITER_LOCKED bit are
set.
So the above sequence could be the case, right?
So the RWSEM_WRITER_LOCKED check can still provide some value. We should probably update the comment to reflect that.
>
> Cheers,
> Longman
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] locking/rwsem: Remove unnessary check in rwsem_down_read_slowpath()
2023-11-09 3:17 ` Haifeng Xu
@ 2023-11-10 6:54 ` Tang Yizhou
2023-11-10 10:29 ` Haifeng Xu
2023-11-10 11:00 ` Haifeng Xu
0 siblings, 2 replies; 7+ messages in thread
From: Tang Yizhou @ 2023-11-10 6:54 UTC (permalink / raw)
To: Haifeng Xu; +Cc: Waiman Long, peterz, mingo, will, boqun.feng, linux-kernel
On Thu, Nov 9, 2023 at 11:17 AM Haifeng Xu <haifeng.xu@shopee.com> wrote:
>
> reader writer reader
>
> acquire
> release
> rwsem_write_trylock
> set RWSEM_WRITER_LOCKED
> rwsem_down_read_slowpath
> set owner
>
> If prev lock holder is a reader, when it releases the lock, the owner isn't cleared(CONFIG_DEBUG_RWSEMS isn't enabled).
> A writer comes and can set the RWSEM_WRITER_LOCKED bit succsessfully, then a new reader run into slow path, before
> the writer set the owner, the new reader will see that both the RWSEM_READER_OWNED bit and RWSEM_WRITER_LOCKED bit are
> set.
>
For the above example, it won't cause a problem. When the writer
successfully sets RWSEM_WRITER_LOCKED, the reader, when reading rcnt
through rwsem_down_read_slowpath(), will see that rcnt is 0 and will
jump to the queue label.
Thanks,
Tang
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] locking/rwsem: Remove unnessary check in rwsem_down_read_slowpath()
2023-11-10 6:54 ` Tang Yizhou
@ 2023-11-10 10:29 ` Haifeng Xu
2023-11-10 13:38 ` Waiman Long
2023-11-10 11:00 ` Haifeng Xu
1 sibling, 1 reply; 7+ messages in thread
From: Haifeng Xu @ 2023-11-10 10:29 UTC (permalink / raw)
To: Tang Yizhou; +Cc: Waiman Long, peterz, mingo, will, boqun.feng, linux-kernel
On 2023/11/10 14:54, Tang Yizhou wrote:
> On Thu, Nov 9, 2023 at 11:17 AM Haifeng Xu <haifeng.xu@shopee.com> wrote:
>>
>> reader writer reader
>>
>> acquire
>> release
>> rwsem_write_trylock
>> set RWSEM_WRITER_LOCKED
>> rwsem_down_read_slowpath
>> set owner
>>
>> If prev lock holder is a reader, when it releases the lock, the owner isn't cleared(CONFIG_DEBUG_RWSEMS isn't enabled).
>> A writer comes and can set the RWSEM_WRITER_LOCKED bit succsessfully, then a new reader run into slow path, before
>> the writer set the owner, the new reader will see that both the RWSEM_READER_OWNED bit and RWSEM_WRITER_LOCKED bit are
>> set.
>>
>
> For the above example, it won't cause a problem. When the writer
> successfully sets RWSEM_WRITER_LOCKED, the reader, when reading rcnt
> through rwsem_down_read_slowpath(), will see that rcnt is 0 and will
> jump to the queue label.
>
> Thanks,
> Tang
Yes, so if rcnt > 1, the RWSEM_WRITER_LOCKED bit couldn't be set?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] locking/rwsem: Remove unnessary check in rwsem_down_read_slowpath()
2023-11-10 10:29 ` Haifeng Xu
@ 2023-11-10 13:38 ` Waiman Long
0 siblings, 0 replies; 7+ messages in thread
From: Waiman Long @ 2023-11-10 13:38 UTC (permalink / raw)
To: Haifeng Xu, Tang Yizhou; +Cc: peterz, mingo, will, boqun.feng, linux-kernel
On 11/10/23 05:29, Haifeng Xu wrote:
>
> On 2023/11/10 14:54, Tang Yizhou wrote:
>> On Thu, Nov 9, 2023 at 11:17 AM Haifeng Xu <haifeng.xu@shopee.com> wrote:
>>> reader writer reader
>>>
>>> acquire
>>> release
>>> rwsem_write_trylock
>>> set RWSEM_WRITER_LOCKED
>>> rwsem_down_read_slowpath
>>> set owner
>>>
>>> If prev lock holder is a reader, when it releases the lock, the owner isn't cleared(CONFIG_DEBUG_RWSEMS isn't enabled).
>>> A writer comes and can set the RWSEM_WRITER_LOCKED bit succsessfully, then a new reader run into slow path, before
>>> the writer set the owner, the new reader will see that both the RWSEM_READER_OWNED bit and RWSEM_WRITER_LOCKED bit are
>>> set.
>>>
>> For the above example, it won't cause a problem. When the writer
>> successfully sets RWSEM_WRITER_LOCKED, the reader, when reading rcnt
>> through rwsem_down_read_slowpath(), will see that rcnt is 0 and will
>> jump to the queue label.
>>
>> Thanks,
>> Tang
> Yes, so if rcnt > 1, the RWSEM_WRITER_LOCKED bit couldn't be set?
No. The way readers acquire the lock is via
atomic_long_add_return_acquire() without looking at current state of the
rwsem (write-locked or not). So rcnt can be greater than 0 and the rwsem
is still writer-owned.
Because of that atomic_long_add_return_acquire() primitive, rcnt
includes its reader count. The lock may be read-locked if only if there
is at least one other reader present. So rcnt must be bigger than 1.
Cheers,
Longman
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] locking/rwsem: Remove unnessary check in rwsem_down_read_slowpath()
2023-11-10 6:54 ` Tang Yizhou
2023-11-10 10:29 ` Haifeng Xu
@ 2023-11-10 11:00 ` Haifeng Xu
1 sibling, 0 replies; 7+ messages in thread
From: Haifeng Xu @ 2023-11-10 11:00 UTC (permalink / raw)
To: Tang Yizhou; +Cc: Waiman Long, peterz, mingo, will, boqun.feng, linux-kernel
On 2023/11/10 14:54, Tang Yizhou wrote:
> On Thu, Nov 9, 2023 at 11:17 AM Haifeng Xu <haifeng.xu@shopee.com> wrote:
>>
>> reader writer reader
>>
>> acquire
>> release
>> rwsem_write_trylock
>> set RWSEM_WRITER_LOCKED
>> rwsem_down_read_slowpath
>> set owner
>>
>> If prev lock holder is a reader, when it releases the lock, the owner isn't cleared(CONFIG_DEBUG_RWSEMS isn't enabled).
>> A writer comes and can set the RWSEM_WRITER_LOCKED bit succsessfully, then a new reader run into slow path, before
>> the writer set the owner, the new reader will see that both the RWSEM_READER_OWNED bit and RWSEM_WRITER_LOCKED bit are
>> set.
>>
>
> For the above example, it won't cause a problem. When the writer
> successfully sets RWSEM_WRITER_LOCKED, the reader, when reading rcnt
> through rwsem_down_read_slowpath(), will see that rcnt is 0 and will
> jump to the queue label.
>
> Thanks,
> Tang
In this case, rcnt is not 0, it's 1, because rwsem_read_trylock() has add RWSEM_READER_BIAS, so if more than one new reader comes,
it could be the case.
reader writer reader reader
acquire
release
rwsem_write_trylock
set RWSEM_WRITER_LOCKED
rwsem_down_read_slowpath rwsem_down_read_slowpath
... check RWSEM_WRITER_LOCKED bit(rcnt=2)
count = atomic_long_add_return(adjustment, &sem->count);
set owner
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2023-11-10 18:21 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-08 10:56 [PATCH] locking/rwsem: Remove unnessary check in rwsem_down_read_slowpath() Haifeng Xu
2023-11-08 14:04 ` Waiman Long
2023-11-09 3:17 ` Haifeng Xu
2023-11-10 6:54 ` Tang Yizhou
2023-11-10 10:29 ` Haifeng Xu
2023-11-10 13:38 ` Waiman Long
2023-11-10 11:00 ` Haifeng Xu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®