public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Christoph Hellwig <hch@infradead.org>
Cc: Matthias Kaehlcke <matthias.kaehlcke@gmail.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] use mutex instead of semaphore in tty_io.c
Date: Wed, 25 Apr 2007 19:20:20 -0700	[thread overview]
Message-ID: <20070425192020.aa14b08f.akpm@linux-foundation.org> (raw)
In-Reply-To: <20070425191359.GA13241@infradead.org>

On Wed, 25 Apr 2007 20:13:59 +0100 Christoph Hellwig <hch@infradead.org> wrote:

> On Wed, Apr 25, 2007 at 05:49:34PM +0200, Matthias Kaehlcke wrote:
> > drivers/char/tty_io.c uses a semaphore as mutex. use the mutex API
> > instead of the (binary) semaphore
> 
> This looks like it should be a spinlock:
> 
> > -		down(&allocated_ptys_lock);
> > +		mutex_lock(&allocated_ptys_lock);
> >  		idr_remove(&allocated_ptys, idx);
> > -		up(&allocated_ptys_lock);
> > +		mutex_unlock(&allocated_ptys_lock);
> 
> idr_remove is a quick operation that doesn't sleep.
> 
> > @@ -2639,24 +2639,24 @@ static int ptmx_open(struct inode * inode, struct file * filp)
> >  	nonseekable_open(inode, filp);
> >  
> >  	/* find a device that is not in use. */
> > -	down(&allocated_ptys_lock);
> > +	mutex_lock(&allocated_ptys_lock);
> >  	if (!idr_pre_get(&allocated_ptys, GFP_KERNEL)) {
> > -		up(&allocated_ptys_lock);
> 
> The idr_pre_get should be moved out of the lock, that's the whole
> point for it's existance..
> 

I think having it inside the lock makes sense:

	mutex_lock()
	idr_pre_get()
	idr_get_new()
	mutex_unlock()

here, if idr_pre_get() succeeded, we know that idr_get_new() will succeed.

otoh:

try_again:
	idr_pre_get()
	mutex_lock()
	if (idr_get_new() == failed) {
		mutex_unlock()
		goto try_again;
	}
	mutex_unlock()

is not nice.


the IDR api is awful.  A little project is to rip out all its internal
locking and to implement caller-provided locking.

Unfortunately the fact that the library allocates memory means that we
might need to do awkward things like radix_tree_preload() to make it
reliable for callers who use spinlocking.


      parent reply	other threads:[~2007-04-26  2:20 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-25 15:49 [PATCH] use mutex instead of semaphore in tty_io.c Matthias Kaehlcke
2007-04-25 19:13 ` Christoph Hellwig
2007-04-25 19:46   ` Matthias Kaehlcke
2007-04-25 19:45     ` Christoph Hellwig
2007-05-31 13:42       ` Matthias Kaehlcke
2007-05-31 13:59         ` Christoph Hellwig
2007-05-31 22:37         ` Andrew Morton
2007-06-01  7:48           ` Matthias Kaehlcke
2007-04-26  2:20   ` Andrew Morton [this message]

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=20070425192020.aa14b08f.akpm@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=hch@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matthias.kaehlcke@gmail.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