tty-ldisc: make refcount be atomic_t 'users' count
[safe/jmp/linux-2.6] / drivers / char / tty_ldisc.c
index 39c8f86..fd175e6 100644 (file)
@@ -21,7 +21,6 @@
 #include <linux/proc_fs.h>
 #include <linux/init.h>
 #include <linux/module.h>
-#include <linux/smp_lock.h>
 #include <linux/device.h>
 #include <linux/wait.h>
 #include <linux/bitops.h>
@@ -143,13 +142,15 @@ static struct tty_ldisc *tty_ldisc_try_get(int disc)
                        /* lock it */
                        ldops->refcount++;
                        ld->ops = ldops;
-                       ld->refcount = 0;
+                       atomic_set(&ld->users, 0);
                        err = 0;
                }
        }
        spin_unlock_irqrestore(&tty_ldisc_lock, flags);
-       if (err)
+       if (err) {
+               kfree(ld);
                return ERR_PTR(err);
+       }
        return ld;
 }
 
@@ -205,6 +206,7 @@ static void tty_ldisc_put(struct tty_ldisc *ld)
        ldo->refcount--;
        module_put(ldo->owner);
        spin_unlock_irqrestore(&tty_ldisc_lock, flags);
+       WARN_ON(atomic_read(&ld->users));
        kfree(ld);
 }
 
@@ -262,7 +264,7 @@ const struct file_operations tty_ldiscs_proc_fops = {
  *     @ld: line discipline
  *
  *     Install an instance of a line discipline into a tty structure. The
- *     ldisc must have a reference count above zero to ensure it remains/
+ *     ldisc must have a reference count above zero to ensure it remains.
  *     The tty instance refcount starts at zero.
  *
  *     Locking:
@@ -295,7 +297,7 @@ static int tty_ldisc_try(struct tty_struct *tty)
        spin_lock_irqsave(&tty_ldisc_lock, flags);
        ld = tty->ldisc;
        if (test_bit(TTY_LDISC, &tty->flags)) {
-               ld->refcount++;
+               atomic_inc(&ld->users);
                ret = 1;
        }
        spin_unlock_irqrestore(&tty_ldisc_lock, flags);
@@ -322,7 +324,7 @@ struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
 {
        /* wait_event is a macro */
        wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
-       WARN_ON(tty->ldisc->refcount == 0);
+       WARN_ON(atomic_read(&tty->ldisc->users) == 0);
        return tty->ldisc;
 }
 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
@@ -363,11 +365,9 @@ void tty_ldisc_deref(struct tty_ldisc *ld)
        BUG_ON(ld == NULL);
 
        spin_lock_irqsave(&tty_ldisc_lock, flags);
-       if (ld->refcount == 0)
+       if (atomic_read(&ld->users) == 0)
                printk(KERN_ERR "tty_ldisc_deref: no references.\n");
-       else
-               ld->refcount--;
-       if (ld->refcount == 0)
+       else if (atomic_dec_and_test(&ld->users))
                wake_up(&tty_ldisc_wait);
        spin_unlock_irqrestore(&tty_ldisc_lock, flags);
 }
@@ -534,10 +534,10 @@ static int tty_ldisc_wait_idle(struct tty_struct *tty)
 {
        unsigned long flags;
        spin_lock_irqsave(&tty_ldisc_lock, flags);
-       while (tty->ldisc->refcount) {
+       while (atomic_read(&tty->ldisc->users)) {
                spin_unlock_irqrestore(&tty_ldisc_lock, flags);
                if (wait_event_timeout(tty_ldisc_wait,
-                               tty->ldisc->refcount == 0, 5 * HZ) == 0)
+                               atomic_read(&tty->ldisc->users) == 0, 5 * HZ) == 0)
                        return -EBUSY;
                spin_lock_irqsave(&tty_ldisc_lock, flags);
        }
@@ -788,15 +788,20 @@ void tty_ldisc_hangup(struct tty_struct *tty)
         * N_TTY.
         */
        if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
-               /* Avoid racing set_ldisc */
+               /* Avoid racing set_ldisc or tty_ldisc_release */
                mutex_lock(&tty->ldisc_mutex);
-               /* Switch back to N_TTY */
-               tty_ldisc_reinit(tty);
-               /* At this point we have a closed ldisc and we want to
-                  reopen it. We could defer this to the next open but
-                  it means auditing a lot of other paths so this is a FIXME */
-               WARN_ON(tty_ldisc_open(tty, tty->ldisc));
-               tty_ldisc_enable(tty);
+               if (tty->ldisc) {       /* Not yet closed */
+                       /* Switch back to N_TTY */
+                       tty_ldisc_halt(tty);
+                       tty_ldisc_wait_idle(tty);
+                       tty_ldisc_reinit(tty);
+                       /* At this point we have a closed ldisc and we want to
+                          reopen it. We could defer this to the next open but
+                          it means auditing a lot of other paths so this is
+                          a FIXME */
+                       WARN_ON(tty_ldisc_open(tty, tty->ldisc));
+                       tty_ldisc_enable(tty);
+               }
                mutex_unlock(&tty->ldisc_mutex);
                tty_reset_termios(tty);
        }
@@ -861,16 +866,25 @@ void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
 
        tty_ldisc_wait_idle(tty);
 
+       mutex_lock(&tty->ldisc_mutex);
        /*
-        * Shutdown the current line discipline, and reset it to N_TTY.
-        *
-        * FIXME: this MUST get fixed for the new reflocking
+        * Now kill off the ldisc
         */
+       tty_ldisc_close(tty, tty->ldisc);
+       tty_ldisc_put(tty->ldisc);
+       /* Force an oops if we mess this up */
+       tty->ldisc = NULL;
+
+       /* Ensure the next open requests the N_TTY ldisc */
+       tty_set_termios_ldisc(tty, N_TTY);
+       mutex_unlock(&tty->ldisc_mutex);
 
-       tty_ldisc_reinit(tty);
        /* This will need doing differently if we need to lock */
        if (o_tty)
                tty_ldisc_release(o_tty, NULL);
+
+       /* And the memory resources remaining (buffers, termios) will be
+          disposed of when the kref hits zero */
 }
 
 /**