Skip to content

Commit

Permalink
tty/n_gsm.c: fix a memory leak when gsmtty is removed
Browse files Browse the repository at this point in the history
when gsmtty_remove put dlci, it will cause memory leak if dlci->port's refcount is zero.
So we do the cleanup work in .cleanup callback instead.

dlci will be last put in two call chains.
1) gsmld_close -> gsm_cleanup_mux -> gsm_dlci_release -> dlci_put
2) gsmld_remove -> dlci_put
so there is a race. the memory leak depends on the race.

In call chain 2. we hit the memory leak. below comment tells.

release_tty -> tty_driver_remove_tty -> gsmtty_remove -> dlci_put -> tty_port_destructor (WARN_ON(port->itty) and return directly)
                         |
                tty->port->itty = NULL;
                         |
                tty_kref_put ---> release_one_tty -> gsmtty_cleanup (added by our patch)

So our patch fix the memory leak by doing the cleanup work after tty core did.

Signed-off-by: Pan Xinhui <xinhuix.pan@intel.com>
Fixes: dfabf7f
Cc: stable <stable@vger.kernel.org> # 3.14+
Acked-by: Jiri Slaby <jslaby@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Pan Xinhui authored and Greg Kroah-Hartman committed May 10, 2015
1 parent ec61847 commit 8f9cfee
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions drivers/tty/n_gsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -3170,15 +3170,14 @@ static int gsmtty_break_ctl(struct tty_struct *tty, int state)
return gsmtty_modem_update(dlci, encode);
}

static void gsmtty_remove(struct tty_driver *driver, struct tty_struct *tty)
static void gsmtty_cleanup(struct tty_struct *tty)
{
struct gsm_dlci *dlci = tty->driver_data;
struct gsm_mux *gsm = dlci->gsm;

dlci_put(dlci);
dlci_put(gsm->dlci[0]);
mux_put(gsm);
driver->ttys[tty->index] = NULL;
}

/* Virtual ttys for the demux */
Expand All @@ -3199,7 +3198,7 @@ static const struct tty_operations gsmtty_ops = {
.tiocmget = gsmtty_tiocmget,
.tiocmset = gsmtty_tiocmset,
.break_ctl = gsmtty_break_ctl,
.remove = gsmtty_remove,
.cleanup = gsmtty_cleanup,
};


Expand Down

0 comments on commit 8f9cfee

Please sign in to comment.