[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <5e221be7-b97c-41a4-e118-128a570befdc@redhat.com>
Date: Fri, 10 Nov 2023 08:38:25 -0500
From: Waiman Long <longman@...hat.com>
To: Haifeng Xu <haifeng.xu@...pee.com>,
Tang Yizhou <yizhou.tang@...pee.com>
Cc: peterz@...radead.org, mingo@...hat.com, will@...nel.org,
boqun.feng@...il.com, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] locking/rwsem: Remove unnessary check in
rwsem_down_read_slowpath()
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@...pee.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
Powered by blists - more mailing lists