Skip to contentSkip to conversation

tty/n_gsm.c: fix a memory leak when gsmtty is removed

PATCH Superseded Pan Xinhui 3 participants 5 messages Mar 23, 2015 – Mar 27, 2015 TTY layer and serial drivers

lore
Share & tools
lore

Pan Xinhui

author
#0 lore raw

PATCH 1/1 tty/n_gsm.c: fix a memory leak when gsmtty is removed

In gsmtty_remove, we will put dlci. when dlci's ref-count is zero, tty_port_destructor will be called, and it will check if port->itty is NULL. However port->itty will be set to NULL in release_tty after gsmtty_remove. that may cause memory leak. so we use queue_work to put the dlci later.

---
 drivers/tty/n_gsm.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/tty/n_gsm.c +14 -2
index c434376..50f4660 100644
@@ -135,6 +135,7 @@ struct gsm_dlci {
 #define DLCI_OPEN		2	/* SABM/UA complete */
 #define DLCI_CLOSING		3	/* Sending DISC not seen UA/DM */
 	struct mutex mutex;
+	struct work_struct putself_work;
 
 	/* Link layer */
 	spinlock_t lock;	/* Protects the internal state */
@@ -3170,14 +3171,25 @@ 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 put_gsm_dlci(struct work_struct *work)
 {
-	struct gsm_dlci *dlci = tty->driver_data;
+	struct gsm_dlci *dlci =
+		container_of(work, struct gsm_dlci, putself_work);
 	struct gsm_mux *gsm = dlci->gsm;
 
+	mutex_lock(&gsm->mutex);
 	dlci_put(dlci);
 	dlci_put(gsm->dlci[0]);
+	mutex_unlock(&gsm->mutex);
 	mux_put(gsm);
+}
+
+static void gsmtty_remove(struct tty_driver *driver, struct tty_struct *tty)
+{
+	struct gsm_dlci *dlci = tty->driver_data;
+
+	INIT_WORK(&dlci->putself_work, put_gsm_dlci);
+	schedule_work(&dlci->putself_work);
 	driver->ttys[tty->index] = NULL;
 }
 
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v2 1/1 tty/n_gsm.c: fix a memory leak when gsmtty is removed

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. bellow 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 (now we do the cleanup work here.)

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

---
 drivers/tty/n_gsm.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/tty/n_gsm.c +2 -3
index c434376..bce16e4 100644
@@ -3170,7 +3170,7 @@ 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;
@@ -3178,7 +3178,6 @@ static void gsmtty_remove(struct tty_driver *driver, struct tty_struct *tty)
 	dlci_put(dlci);
 	dlci_put(gsm->dlci[0]);
 	mux_put(gsm);
-	driver->ttys[tty->index] = NULL;
 }
 
 /* Virtual ttys for the demux */
@@ -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,
 };
 
 
Signature · 1 line
1.9.1
maintainer
#2 lore raw
Quoted 9 lines · Pan Xinhui

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.

Quoted 7 lines

In call chain 2. we hit the memory leak. bellow 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 (now we do the cleanup work here.)

That doesn't line up at all :(

Quoted 3 lines

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

I need a real name here, "xinhui.pan" is not your real name, according to your "From:" line, right?

Please fix up and resend.

And also include what changed from the previous version, this is much different.

thanks,

greg k-h

author
#3 lore raw

PATCH RESEND v2 1/1 tty/n_gsm.c: fix a memory leak when gsmtty is removed

In reply to Pan Xinhui

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.

---
Changes in v2:
Don't use schedule_work to put dlci. Replace .remove with .cleanup callback.

 drivers/tty/n_gsm.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/tty/n_gsm.c +2 -3
index c434376..bce16e4 100644
@@ -3170,7 +3170,7 @@ 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;
@@ -3178,7 +3178,6 @@ static void gsmtty_remove(struct tty_driver *driver, struct tty_struct *tty)
 	dlci_put(dlci);
 	dlci_put(gsm->dlci[0]);
 	mux_put(gsm);
-	driver->ttys[tty->index] = NULL;
 }
 
 /* Virtual ttys for the demux */
@@ -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,
 };
 
 
Signature · 1 line
1.9.1
Acked-by on v2 1/1
#4 lore raw
Quoted 17 lines · Pan Xinhui

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
Quoted 6 lines

(added by our patch)

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

Fixes dfabf7ffa30585
Quoted 41 lines

---
Changes in v2:
Don't use schedule_work to put dlci. Replace .remove with .cleanup
callback.

 drivers/tty/n_gsm.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/tty/n_gsm.c +1 -1
index c434376..bce16e4 100644
@@ -3170,7 +3170,7 @@ 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;
drivers/tty/n_gsm.c +0 -1
@@ -3178,7 +3178,6 @@ static void gsmtty_remove(struct tty_driver
 *driver, struct tty_struct *tty)
     dlci_put(dlci);
     dlci_put(gsm->dlci[0]);
     mux_put(gsm);
-    driver->ttys[tty->index] = NULL;
 }
 

/* Virtual ttys for the demux */

drivers/tty/n_gsm.c +1 -1
@@ -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,
 };
 
 
Signature · 2 lines
js
suse labs