Commit aef29bc2603014cb28dfe39bab8d888546fe18e7

Authored by Alan Cox
Committed by Linus Torvalds
1 parent 5298976562

tty: Fix the leak in tty_ldisc_release

Currently we reinit the ldisc on final tty close which is what the old code
did to ensure that if the device retained its termios settings then it had the
right ldisc. tty_ldisc_reinit does that but also leaves us with the reset
ldisc reference which is then leaked.

At this point we know the port will be recycled so we can kill the ldisc
off completely rather than try and add another ldisc free up when the kref
count hits zero.

At this point it is safe to keep the ldisc closed as tty_ldisc waiting
methods are only used from the user side, and as the final close we are
the last such reference. Interrupt/driver side methods will always use the
non wait version and get back a NULL.

Found with kmemleak and investigated/identified by Catalin Marinas.

Signed-off-by: Alan Cox <alan@linux.intel.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 1 changed file with 11 additions and 4 deletions Side-by-side Diff

drivers/char/tty_ldisc.c
... ... @@ -867,15 +867,22 @@
867 867 tty_ldisc_wait_idle(tty);
868 868  
869 869 /*
870   - * Shutdown the current line discipline, and reset it to N_TTY.
871   - *
872   - * FIXME: this MUST get fixed for the new reflocking
  870 + * Now kill off the ldisc
873 871 */
  872 + tty_ldisc_close(tty, tty->ldisc);
  873 + tty_ldisc_put(tty->ldisc);
  874 + /* Force an oops if we mess this up */
  875 + tty->ldisc = NULL;
874 876  
875   - tty_ldisc_reinit(tty);
  877 + /* Ensure the next open requests the N_TTY ldisc */
  878 + tty_set_termios_ldisc(tty, N_TTY);
  879 +
876 880 /* This will need doing differently if we need to lock */
877 881 if (o_tty)
878 882 tty_ldisc_release(o_tty, NULL);
  883 +
  884 + /* And the memory resources remaining (buffers, termios) will be
  885 + disposed of when the kref hits zero */
879 886 }
880 887  
881 888 /**