linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Waiman Long <longman@redhat.com>
To: 马振华 <mazhenhua@xiaomi.com>, peterz <peterz@infradead.org>,
	mingo <mingo@redhat.com>, will <will@kernel.org>,
	"boqun.feng" <boqun.feng@gmail.com>,
	linux-kernel <linux-kernel@vger.kernel.org>
Subject: Re: [BUG]locking/rwsem: only clean RWSEM_FLAG_HANDOFF when already set
Date: Sat, 6 Nov 2021 23:25:38 -0400	[thread overview]
Message-ID: <55bfbf1c-5906-9cc9-ee68-a1c69651bbc3@redhat.com> (raw)
In-Reply-To: <4fafad133b074f279dbab1aa3642e23f@xiaomi.com>

On 11/6/21 08:39, 马振华 wrote:
> Dear longman,
>
> recently , i find a issue which rwsem count is negative value, it 
> happened always when a task try to get the lock 
> with __down_write_killable , then it is killed
>
> this issue happened like this
>
>             CPU2         CPU4
>     task A[reader]     task B[writer]
>     down_read_killable[locked]
>     sem->count=0x100
>             down_write_killable
>             sem->count=0x102[wlist not empty]
>     up_read
>     count=0x2
>             sig kill received
>     down_read_killable
>     sem->count=0x102[wlist not empty]
>             goto branch out_nolock:
> list_del(&waiter.list);
> wait list is empty
> sem->count-RWSEM_FLAG_HANDOFF
> sem->count=0xFE
>     list_empty(&sem->wait_list) is TRUE
>      sem->count andnot RWSEM_FLAG_WAITERS
>       sem->count=0xFC
>     up_read
>     sem->count -= 0x100
>     sem->count=0xFFFFFFFFFFFFFFFC
>     DEBUG_RWSEMS_WARN_ON(tmp < 0, sem);
>
> so sem->count will be negative after writer is killed
> i think if flag RWSEM_FLAG_HANDOFF is not set, we shouldn't clean it

Thanks for reporting this possible race condition.

However, I am still trying to figure how it is possible to set the 
wstate to WRITER_HANDOFF without actually setting the handoff bit as 
well. The statement sequence should be as follows:

wstate = WRITER_HANDOFF;
raw_spin_lock_irq(&sem->wait_lock);
if (rwsem_try_write_lock(sem, wstate))
raw_spin_unlock_irq(&sem->wait_lock);
   :
if (signal_pending_state(state, current))
     goto out_nolock

The rwsem_try_write_lock() function will make sure that we either 
acquire the lock and clear handoff or set the handoff bit. This should 
be done before we actually check for signal. I do think that it is 
probably safer to use atomic_long_andnot to clear the handoff bit 
instead of using atomic_long_add().

Cheers,
Longman


       reply	other threads:[~2021-11-07  3:26 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <4fafad133b074f279dbab1aa3642e23f@xiaomi.com>
2021-11-07  3:25 ` Waiman Long [this message]
2021-11-07  3:28   ` [BUG]locking/rwsem: only clean RWSEM_FLAG_HANDOFF when already set Waiman Long
     [not found] ` <20211107090131.1535-1-hdanton@sina.com>
2021-11-07 15:24   ` Waiman Long
2021-11-07 19:52     ` Waiman Long
2021-11-10 21:38       ` Peter Zijlstra
2021-11-11  2:42         ` Maria Yu
2021-11-11 15:08         ` Peter Zijlstra
2021-11-11 19:14           ` Waiman Long
2021-11-11 19:20             ` Peter Zijlstra
2021-11-11 19:36               ` Waiman Long
2021-11-11 19:52                 ` Waiman Long
2021-11-11 20:26                 ` Peter Zijlstra
2021-11-11 21:01                   ` Waiman Long
2021-11-11 21:25                     ` Waiman Long
2021-11-11 21:53                       ` Peter Zijlstra
2021-11-11 21:55                         ` Waiman Long
2021-11-11 22:00                           ` Waiman Long
2021-11-11 21:38                     ` Peter Zijlstra
2021-11-11 21:46                       ` Waiman Long
2021-11-11 20:35                 ` Peter Zijlstra
2021-11-11 20:39                   ` Peter Zijlstra
2021-11-11 20:45                   ` Waiman Long
2021-11-11 21:27                     ` Peter Zijlstra
2021-11-11 21:54                       ` Waiman Long
2021-11-11 20:50                 ` Peter Zijlstra
2021-11-11 21:09                   ` Waiman Long

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=55bfbf1c-5906-9cc9-ee68-a1c69651bbc3@redhat.com \
    --to=longman@redhat.com \
    --cc=boqun.feng@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mazhenhua@xiaomi.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).