mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Matthew Wilcox <willy@infradead.org>
To: "Aiqun Yu (Maria)" <quic_aiquny@quicinc.com>
Cc: Hillf Danton <hdanton@sina.com>,
	"Eric W. Biederman" <ebiederm@xmission.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] kernel: Introduce a write lock/unlock wrapper for tasklist_lock
Date: Wed, 27 Dec 2023 10:14:58 +0000	[thread overview]
Message-ID: <ZYv5IhyCVeYhbj8b@casper.infradead.org> (raw)
In-Reply-To: <6e762e8e-b031-4e37-97c1-56390c9b8076@quicinc.com>

On Wed, Dec 27, 2023 at 09:41:29AM +0800, Aiqun Yu (Maria) wrote:
> On 12/26/2023 6:46 PM, Hillf Danton wrote:
> > On Wed, 13 Dec 2023 12:27:05 -0600 Eric W. Biederman <ebiederm@xmission.com>
> > > Matthew Wilcox <willy@infradead.org> writes:
> > > > On Wed, Dec 13, 2023 at 06:17:45PM +0800, Maria Yu wrote:
> > > > > +static inline void write_lock_tasklist_lock(void)
> > > > > +{
> > > > > +	while (1) {
> > > > > +		local_irq_disable();
> > > > > +		if (write_trylock(&tasklist_lock))
> > > > > +			break;
> > > > > +		local_irq_enable();
> > > > > +		cpu_relax();
> > > > 
> > > > This is a bad implementation though.  You don't set the _QW_WAITING flag
> > > > so readers don't know that there's a pending writer.  Also, I've seen
> > > > cpu_relax() pessimise CPU behaviour; putting it into a low-power mode
> > > > that takes a while to wake up from.
> > > > 
> > > > I think the right way to fix this is to pass a boolean flag to
> > > > queued_write_lock_slowpath() to let it know whether it can re-enable
> > > > interrupts while checking whether _QW_WAITING is set.
> > 
> > 	lock(&lock->wait_lock)
> > 	enable irq
> > 	int
> > 	lock(&lock->wait_lock)
> > 
> > You are adding chance for recursive locking.
> 
> Thx for the comments for discuss of the deadlock possibility. While I think
> deadlock can be differentiate with below 2 scenarios:
> 1. queued_write_lock_slowpath being triggered in interrupt context.
>   tasklist_lock don't have write_lock_irq(save) in interrupt context.
>   while for common rw lock, maybe write_lock_irq(save) usage in interrupt
> context is a possible.
>   so may introduce a state when lock->wait_lock is released and left the
> _QW_WAITING flag.
> Welcome others to suggest on designs and comments.

Hm?  I am confused.  You're talking about the scenario where:

 - CPU B holds the lock for read
 - CPU A attempts to get the lock for write in user context, fails, sets
   the _QW_WAITING flag
 - CPU A re-enables interrupts
 - CPU A executes an interrupt handler which calls queued_write_lock()
 - If CPU B has dropped the read lock in the meantime,
   atomic_try_cmpxchg_acquire(&lock->cnts, &cnts, _QW_LOCKED) succeeds
 - CPU A calls queued_write_unlock() which stores 0 to the lock and we
   _lose_ the _QW_WAITING flag for the userspace waiter.

How do we end up with CPU A leaving the _QW_WAITING flag set?


  reply	other threads:[~2023-12-27 10:15 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-13 10:17 Maria Yu
2023-12-13 16:22 ` Matthew Wilcox
2023-12-13 18:27   ` Eric W. Biederman
2023-12-15  5:52     ` Aiqun Yu (Maria)
2023-12-26 10:46     ` Hillf Danton
2023-12-26 20:49       ` Matthew Wilcox
2023-12-27 11:07         ` Hillf Danton
2023-12-28  9:07           ` Matthew Wilcox
2023-12-27  1:41       ` Aiqun Yu (Maria)
2023-12-27 10:14         ` Matthew Wilcox [this message]
2023-12-28 22:20     ` Matthew Wilcox
2023-12-29 11:35       ` kernel test robot
2024-01-02  2:19       ` Aiqun Yu (Maria)
2024-01-02  9:14         ` Matthew Wilcox
2024-01-03  2:58           ` Aiqun Yu (Maria)
2024-01-03 18:18             ` Matthew Wilcox
2024-01-04  0:46               ` Aiqun Yu (Maria)
2024-01-03  6:03       ` kernel test robot
2023-12-25  8:19 Maria Yu
2023-12-25  8:26 ` Aiqun Yu (Maria)
2024-01-03 14:04 ` Jarkko Sakkinen

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=ZYv5IhyCVeYhbj8b@casper.infradead.org \
    --to=willy@infradead.org \
    --cc=ebiederm@xmission.com \
    --cc=hdanton@sina.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=quic_aiquny@quicinc.com \
    /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

all inboxes | Powered by JetHome®