public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Byungchul Park <byungchul.park@lge.com>
To: Ingo Molnar <mingo@kernel.org>
Cc: willy@linux.intel.com, akpm@linux-foundation.org,
	linux-kernel@vger.kernel.org, akinobu.mita@gmail.com,
	jack@suse.cz, sergey.senozhatsky.work@gmail.com,
	peter@hurleysoftware.com
Subject: Re: [PATCH v3] lock/semaphore: Avoid an unnecessary deadlock within up()
Date: Wed, 9 Mar 2016 11:00:37 +0900	[thread overview]
Message-ID: <20160309020037.GA2865@X58A-UD3R> (raw)
In-Reply-To: <20160217092828.GA19001@gmail.com>

On Wed, Feb 17, 2016 at 10:28:29AM +0100, Ingo Molnar wrote:
> 
> * Byungchul Park <byungchul.park@lge.com> wrote:
> 
> > diff --git a/kernel/locking/semaphore.c b/kernel/locking/semaphore.c
> > index b8120ab..6634b68 100644
> > --- a/kernel/locking/semaphore.c
> > +++ b/kernel/locking/semaphore.c
> > @@ -130,13 +130,14 @@ EXPORT_SYMBOL(down_killable);
> >  int down_trylock(struct semaphore *sem)
> >  {
> >  	unsigned long flags;
> > -	int count;
> > +	int count = -1;
> >  
> > -	raw_spin_lock_irqsave(&sem->lock, flags);
> > -	count = sem->count - 1;
> > -	if (likely(count >= 0))
> > -		sem->count = count;
> > -	raw_spin_unlock_irqrestore(&sem->lock, flags);
> > +	if (raw_spin_trylock_irqsave(&sem->lock, flags)) {
> > +		count = sem->count - 1;
> > +		if (likely(count >= 0))
> > +			sem->count = count;
> > +		raw_spin_unlock_irqrestore(&sem->lock, flags);
> > +	}
> 
> I still don't really like it: two parallel trylocks will cause one of them to fail 
> - while with the previous code they would both succeed.
> 
> None of these changes are necessary with all the printk robustification 
> changes/enhancements we talked about, right?

Not only printk() but any code using a semaphore, mutex and so on, can also
cause a deadlock if wake_up_process() eventually tries to acquire the lock.
There are several ways to solve this problem.

1. ensure wake_up_process() does not try to acquire the locks.
2. ensure wake_up_process() isn't protected by a spinlock of the locks.
3. ensure any kind of trylock stuff never cause waiting and deadlock.
4. and so on..

I am not sure which one is the best. But I think 3rd one is the one since
it can be done by a generic way, even though it might decrease the success
ratio as Ingo said, but IMHO it's not a big problem since a trylock user 
only uses the trylock when it doesn't need to be cared whether it succeed
or fail.

Which one among those do you think the best approach? Please let me know,
then I will try to solve this problem by the appoach.

> 
> Thanks,
> 
> 	Ingo

  parent reply	other threads:[~2016-03-09  2:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-17  9:11 [PATCH v3] lock/semaphore: Avoid an unnecessary deadlock within up() Byungchul Park
2016-02-17  9:28 ` Ingo Molnar
2016-02-18  8:00   ` Byungchul Park
2016-03-09  2:00   ` Byungchul Park [this message]
2016-03-09  6:07     ` Byungchul Park
2016-03-10  0:38       ` Byungchul Park
2016-03-10  1:12         ` Byungchul Park
2016-02-17 10:40 ` Peter Zijlstra
2016-02-18  8:13   ` Byungchul Park

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=20160309020037.GA2865@X58A-UD3R \
    --to=byungchul.park@lge.com \
    --cc=akinobu.mita@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=jack@suse.cz \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peter@hurleysoftware.com \
    --cc=sergey.senozhatsky.work@gmail.com \
    --cc=willy@linux.intel.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