--- 25-akpm/drivers/char/tty_io.c | 50 +++++++++++++++++++++++++++++++++--------- 1 files changed, 40 insertions(+), 10 deletions(-) diff -puN drivers/char/tty_io.c~pty-allocation-first-fit drivers/char/tty_io.c --- 25/drivers/char/tty_io.c~pty-allocation-first-fit 2004-04-11 21:11:05.728839296 -0700 +++ 25-akpm/drivers/char/tty_io.c 2004-04-11 21:11:05.733838536 -0700 @@ -91,6 +91,7 @@ #include #include #include +#include #include #include @@ -128,6 +129,8 @@ DECLARE_MUTEX(tty_sem); #ifdef CONFIG_UNIX98_PTYS extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */ extern int pty_limit; /* Config limit on Unix98 ptys */ +static struct idr allocated_ptys; +static DECLARE_MUTEX(allocated_ptys_lock); #endif extern void disable_early_printk(void); @@ -1295,6 +1298,14 @@ static void release_dev(struct file * fi */ flush_scheduled_work(); +#ifdef CONFIG_UNIX98_PTYS + if (filp->f_dentry->d_inode->i_rdev == MKDEV(TTYAUX_MAJOR,2)) { + down(&allocated_ptys_lock); + idr_remove(&allocated_ptys, idx); + up(&allocated_ptys_lock); + } +#endif + /* * The release_mem function takes care of the details of clearing * the slots and preserving the termios structure. @@ -1319,7 +1330,7 @@ static int tty_open(struct inode * inode struct tty_struct *tty; int noctty, retval; struct tty_driver *driver; - int index; + int index = -1; dev_t device = inode->i_rdev; unsigned short saved_flags = filp->f_flags; retry_open: @@ -1362,22 +1373,32 @@ retry_open: #ifdef CONFIG_UNIX98_PTYS if (device == MKDEV(TTYAUX_MAJOR,2)) { /* find a device that is not in use. */ - static int next_ptmx_dev = 0; - retval = -1; + down(&allocated_ptys_lock); + if (!idr_pre_get(&allocated_ptys, GFP_KERNEL)) { + up(&allocated_ptys_lock); + return -ENOMEM; + } + index = idr_get_new(&allocated_ptys, NULL); + if (index >= pty_limit) { + idr_remove(&allocated_ptys, index); + up(&allocated_ptys_lock); + return -EIO; + } driver = ptm_driver; - while (driver->refcount < pty_limit) { - index = next_ptmx_dev; - next_ptmx_dev = (next_ptmx_dev+1) % driver->num; - if (!init_dev(driver, index, &tty)) - goto ptmx_found; /* ok! */ + retval = init_dev(driver, index, &tty); + if (retval) { + idr_remove(&allocated_ptys, index); + up(&allocated_ptys_lock); + return retval; } - return -EIO; /* no free ptys */ - ptmx_found: set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */ if (devpts_pty_new(tty->link)) { /* BADNESS - need to destroy both ptm and pts! */ + idr_remove(&allocated_ptys, index); + up(&allocated_ptys_lock); return -ENOMEM; } + up(&allocated_ptys_lock); noctty = 1; } else #endif @@ -1415,6 +1436,14 @@ got_driver: tty->name); #endif +#ifdef CONFIG_UNIX98_PTYS + if (index != -1) { + down(&allocated_ptys_lock); + idr_remove(&allocated_ptys, index); + up(&allocated_ptys_lock); + } +#endif + release_dev(filp); if (retval != -ERESTARTSYS) return retval; @@ -2400,6 +2429,7 @@ static struct cdev vc0_cdev; */ static int __init tty_init(void) { + idr_init(&allocated_ptys); cdev_init(&tty_cdev, &tty_fops); if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) _