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

From: Christoph Hellwig
Date: Wed Apr 25 2007 - 15:14:29 EST


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..

> + mutex_unlock(&allocated_ptys_lock);
> return -ENOMEM;
> }
> idr_ret = idr_get_new(&allocated_ptys, NULL, &index);
> if (idr_ret < 0) {
> - up(&allocated_ptys_lock);
> + mutex_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);
> + mutex_unlock(&allocated_ptys_lock);
> return -EIO;
> }
> - up(&allocated_ptys_lock);
> + mutex_unlock(&allocated_ptys_lock);

And idr_get_new is another quick, non-blocking operation.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/