2007-05-31 13:40:51

by Matthias Kaehlcke

[permalink] [raw]
Subject: Re: [PATCH] use mutex instead of semaphore in tty_io.c

drivers/char/tty_io.c: Use spinlock instead of a (binary) semaphore

Signed-off-by: Matthias Kaehlcke <[email protected]>

--

diff --git a/drivers/char/tty_io.c b/drivers/char/tty_io.c
index 7a32df5..ff27587 100644
--- a/drivers/char/tty_io.c
+++ b/drivers/char/tty_io.c
@@ -137,7 +137,7 @@ EXPORT_SYMBOL(tty_mutex);
extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
extern int pty_limit; /* Config limit on Unix98 ptys */
static DEFINE_IDR(allocated_ptys);
-static DECLARE_MUTEX(allocated_ptys_lock);
+static DEFINE_SPINLOCK(allocated_ptys_lock);
static int ptmx_open(struct inode *, struct file *);
#endif

@@ -2471,9 +2471,9 @@ static void release_dev(struct file * filp)
#ifdef CONFIG_UNIX98_PTYS
/* Make this pty number available for reallocation */
if (devpts) {
- down(&allocated_ptys_lock);
+ spin_lock(&allocated_ptys_lock);
idr_remove(&allocated_ptys, idx);
- up(&allocated_ptys_lock);
+ spin_unlock(&allocated_ptys_lock);
}
#endif

@@ -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);
- if (!idr_pre_get(&allocated_ptys, GFP_KERNEL)) {
- up(&allocated_ptys_lock);
+ if (!idr_pre_get(&allocated_ptys, GFP_KERNEL))
return -ENOMEM;
- }
+
+ spin_lock(&allocated_ptys_lock);
+
idr_ret = idr_get_new(&allocated_ptys, NULL, &index);
if (idr_ret < 0) {
- up(&allocated_ptys_lock);
+ spin_unlock(&allocated_ptys_lock);
if (idr_ret == -EAGAIN)
return -ENOMEM;
return -EIO;
}
if (index >= pty_limit) {
idr_remove(&allocated_ptys, index);
- up(&allocated_ptys_lock);
+ spin_unlock(&allocated_ptys_lock);
return -EIO;
}
- up(&allocated_ptys_lock);
+ spin_unlock(&allocated_ptys_lock);

mutex_lock(&tty_mutex);
retval = init_dev(ptm_driver, index, &tty);
@@ -2681,9 +2681,9 @@ out1:
release_dev(filp);
return retval;
out:
- down(&allocated_ptys_lock);
+ spin_lock(&allocated_ptys_lock);
idr_remove(&allocated_ptys, index);
- up(&allocated_ptys_lock);
+ spin_unlock(&allocated_ptys_lock);
return retval;
}
#endif

--
Matthias Kaehlcke
Linux Application Developer
Barcelona

The assumption that what currently exists must necessarily
exist is the acid that corrodes all visionary thinking
.''`.
using free software / Debian GNU/Linux | http://debian.org : :' :
`. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4 `-


2007-05-31 13:59:37

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH] use mutex instead of semaphore in tty_io.c

On Thu, May 31, 2007 at 03:42:26PM +0200, Matthias Kaehlcke wrote:
> drivers/char/tty_io.c: Use spinlock instead of a (binary) semaphore

Looks good.

2007-05-31 22:37:56

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH] use mutex instead of semaphore in tty_io.c

On Thu, 31 May 2007 15:42:26 +0200
Matthias Kaehlcke <[email protected]> wrote:

> drivers/char/tty_io.c: Use spinlock instead of a (binary) semaphore
>

hm.

>
> --
>
> diff --git a/drivers/char/tty_io.c b/drivers/char/tty_io.c
> index 7a32df5..ff27587 100644
> --- a/drivers/char/tty_io.c
> +++ b/drivers/char/tty_io.c

We end up with this:

/* find a device that is not in use. */
if (!idr_pre_get(&allocated_ptys, GFP_KERNEL))
return -ENOMEM;

spin_lock(&allocated_ptys_lock);

idr_ret = idr_get_new(&allocated_ptys, NULL, &index);
if (idr_ret < 0) {
spin_unlock(&allocated_ptys_lock);
if (idr_ret == -EAGAIN)
return -ENOMEM;
return -EIO;
}
if (index >= pty_limit) {
idr_remove(&allocated_ptys, index);
spin_unlock(&allocated_ptys_lock);
return -EIO;
}
spin_unlock(&allocated_ptys_lock);

this leaves a small window in which another thread can come in and steal
away the idr tree's reserves, causing the idr_get_new() to fail. It's
highly improbable, but it's real.

Hence I think a straight semaphore->mutex conversion would be better.

The IDR API absolutely blows chunks: it should require caller-provided
locking, like radix-tree. But then it'd need gunk like radix_tree_preload
to be reliable. Fact is, storage librares which need to allocate memory at
insert-time are always going to be problematic in-kernel.


2007-06-01 07:46:52

by Matthias Kaehlcke

[permalink] [raw]
Subject: Re: [PATCH] use mutex instead of semaphore in tty_io.c

El Thu, May 31, 2007 at 03:37:12PM -0700 Andrew Morton ha dit:

> On Thu, 31 May 2007 15:42:26 +0200
> Matthias Kaehlcke <[email protected]> wrote:
>
> > drivers/char/tty_io.c: Use spinlock instead of a (binary) semaphore
> >
>
> hm.
>
> >
>
> We end up with this:
>
> /* find a device that is not in use. */
> if (!idr_pre_get(&allocated_ptys, GFP_KERNEL))
> return -ENOMEM;
>
> spin_lock(&allocated_ptys_lock);
>
> idr_ret = idr_get_new(&allocated_ptys, NULL, &index);
> if (idr_ret < 0) {
> spin_unlock(&allocated_ptys_lock);
> if (idr_ret == -EAGAIN)
> return -ENOMEM;
> return -EIO;
> }
> if (index >= pty_limit) {
> idr_remove(&allocated_ptys, index);
> spin_unlock(&allocated_ptys_lock);
> return -EIO;
> }
> spin_unlock(&allocated_ptys_lock);
>
> this leaves a small window in which another thread can come in and steal
> away the idr tree's reserves, causing the idr_get_new() to fail. It's
> highly improbable, but it's real.

i agree, thanks for pointing it out

> Hence I think a straight semaphore->mutex conversion would be better.

that leads us back to the initial patch. christoph: is that ok for
you or do you have another proposal?

--
Matthias Kaehlcke
Linux Application Developer
Barcelona

The assumption that what currently exists must necessarily
exist is the acid that corrodes all visionary thinking
.''`.
using free software / Debian GNU/Linux | http://debian.org : :' :
`. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4 `-