Commit b8f0e820 authored by Johan Hovold's avatar Johan Hovold Committed by Greg Kroah-Hartman

USB: usb-wwan: fix multiple memory leaks in error paths

Fix port-data memory leak in usb-serial probe error path by moving port
data allocation to port_probe.

Since commit a1028f0a ("usb: usb_wwan: replace release and disconnect
with a port_remove hook") port data is deallocated in port_remove. This
leaves a possibility for memory leaks if usb-serial probe fails after
attach but before the port in question has been successfully registered.

Note that this patch also fixes two additional memory leaks in the error
path of attach should port initialisation fail for any port as the urbs
were never freed and neither was the data of any of the successfully
initialised ports.

Compile-only tested.
Signed-off-by: default avatarJohan Hovold <jhovold@gmail.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent f79b2d0f
...@@ -304,8 +304,8 @@ static struct usb_serial_driver ipw_device = { ...@@ -304,8 +304,8 @@ static struct usb_serial_driver ipw_device = {
.open = ipw_open, .open = ipw_open,
.close = ipw_close, .close = ipw_close,
.probe = ipw_probe, .probe = ipw_probe,
.attach = usb_wwan_startup,
.release = ipw_release, .release = ipw_release,
.port_probe = usb_wwan_port_probe,
.port_remove = usb_wwan_port_remove, .port_remove = usb_wwan_port_remove,
.dtr_rts = ipw_dtr_rts, .dtr_rts = ipw_dtr_rts,
.write = usb_wwan_write, .write = usb_wwan_write,
......
...@@ -1288,8 +1288,8 @@ static struct usb_serial_driver option_1port_device = { ...@@ -1288,8 +1288,8 @@ static struct usb_serial_driver option_1port_device = {
.tiocmget = usb_wwan_tiocmget, .tiocmget = usb_wwan_tiocmget,
.tiocmset = usb_wwan_tiocmset, .tiocmset = usb_wwan_tiocmset,
.ioctl = usb_wwan_ioctl, .ioctl = usb_wwan_ioctl,
.attach = usb_wwan_startup,
.release = option_release, .release = option_release,
.port_probe = usb_wwan_port_probe,
.port_remove = usb_wwan_port_remove, .port_remove = usb_wwan_port_remove,
.read_int_callback = option_instat_callback, .read_int_callback = option_instat_callback,
#ifdef CONFIG_PM #ifdef CONFIG_PM
......
...@@ -285,8 +285,8 @@ static struct usb_serial_driver qcdevice = { ...@@ -285,8 +285,8 @@ static struct usb_serial_driver qcdevice = {
.write = usb_wwan_write, .write = usb_wwan_write,
.write_room = usb_wwan_write_room, .write_room = usb_wwan_write_room,
.chars_in_buffer = usb_wwan_chars_in_buffer, .chars_in_buffer = usb_wwan_chars_in_buffer,
.attach = usb_wwan_startup,
.release = qc_release, .release = qc_release,
.port_probe = usb_wwan_port_probe,
.port_remove = usb_wwan_port_remove, .port_remove = usb_wwan_port_remove,
#ifdef CONFIG_PM #ifdef CONFIG_PM
.suspend = usb_wwan_suspend, .suspend = usb_wwan_suspend,
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
extern void usb_wwan_dtr_rts(struct usb_serial_port *port, int on); extern void usb_wwan_dtr_rts(struct usb_serial_port *port, int on);
extern int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port); extern int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port);
extern void usb_wwan_close(struct usb_serial_port *port); extern void usb_wwan_close(struct usb_serial_port *port);
extern int usb_wwan_startup(struct usb_serial *serial); extern int usb_wwan_port_probe(struct usb_serial_port *port);
extern int usb_wwan_port_remove(struct usb_serial_port *port); extern int usb_wwan_port_remove(struct usb_serial_port *port);
extern int usb_wwan_write_room(struct tty_struct *tty); extern int usb_wwan_write_room(struct tty_struct *tty);
extern void usb_wwan_set_termios(struct tty_struct *tty, extern void usb_wwan_set_termios(struct tty_struct *tty,
......
...@@ -447,10 +447,12 @@ void usb_wwan_close(struct usb_serial_port *port) ...@@ -447,10 +447,12 @@ void usb_wwan_close(struct usb_serial_port *port)
EXPORT_SYMBOL(usb_wwan_close); EXPORT_SYMBOL(usb_wwan_close);
/* Helper functions used by usb_wwan_setup_urbs */ /* Helper functions used by usb_wwan_setup_urbs */
static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint, static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
int endpoint,
int dir, void *ctx, char *buf, int len, int dir, void *ctx, char *buf, int len,
void (*callback) (struct urb *)) void (*callback) (struct urb *))
{ {
struct usb_serial *serial = port->serial;
struct urb *urb; struct urb *urb;
if (endpoint == -1) if (endpoint == -1)
...@@ -472,101 +474,75 @@ static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint, ...@@ -472,101 +474,75 @@ static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint,
return urb; return urb;
} }
/* Setup urbs */ int usb_wwan_port_probe(struct usb_serial_port *port)
static void usb_wwan_setup_urbs(struct usb_serial *serial)
{ {
int i, j;
struct usb_serial_port *port;
struct usb_wwan_port_private *portdata; struct usb_wwan_port_private *portdata;
struct urb *urb;
u8 *buffer;
int err;
int i;
for (i = 0; i < serial->num_ports; i++) { portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
port = serial->port[i]; if (!portdata)
portdata = usb_get_serial_port_data(port); return -ENOMEM;
/* Do indat endpoints first */ init_usb_anchor(&portdata->delayed);
for (j = 0; j < N_IN_URB; ++j) {
portdata->in_urbs[j] = usb_wwan_setup_urb(serial,
port->
bulk_in_endpointAddress,
USB_DIR_IN,
port,
portdata->
in_buffer[j],
IN_BUFLEN,
usb_wwan_indat_callback);
}
/* outdat endpoints */ for (i = 0; i < N_IN_URB; i++) {
for (j = 0; j < N_OUT_URB; ++j) { buffer = (u8 *)__get_free_page(GFP_KERNEL);
portdata->out_urbs[j] = usb_wwan_setup_urb(serial, if (!buffer)
port-> goto bail_out_error;
bulk_out_endpointAddress, portdata->in_buffer[i] = buffer;
USB_DIR_OUT,
port, urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
portdata-> USB_DIR_IN, port,
out_buffer buffer, IN_BUFLEN,
[j], usb_wwan_indat_callback);
OUT_BUFLEN, portdata->in_urbs[i] = urb;
usb_wwan_outdat_callback);
}
} }
}
int usb_wwan_startup(struct usb_serial *serial)
{
int i, j, err;
struct usb_serial_port *port;
struct usb_wwan_port_private *portdata;
u8 *buffer;
/* Now setup per port private data */ for (i = 0; i < N_OUT_URB; i++) {
for (i = 0; i < serial->num_ports; i++) { if (port->bulk_out_endpointAddress == -1)
port = serial->port[i]; continue;
portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
if (!portdata) {
dev_dbg(&port->dev, "%s: kmalloc for usb_wwan_port_private (%d) failed!.\n",
__func__, i);
return 1;
}
init_usb_anchor(&portdata->delayed);
for (j = 0; j < N_IN_URB; j++) { buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
buffer = (u8 *) __get_free_page(GFP_KERNEL); if (!buffer)
if (!buffer) goto bail_out_error2;
goto bail_out_error; portdata->out_buffer[i] = buffer;
portdata->in_buffer[j] = buffer;
}
for (j = 0; j < N_OUT_URB; j++) { urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL); USB_DIR_OUT, port,
if (!buffer) buffer, OUT_BUFLEN,
goto bail_out_error2; usb_wwan_outdat_callback);
portdata->out_buffer[j] = buffer; portdata->out_urbs[i] = urb;
} }
usb_set_serial_port_data(port, portdata); usb_set_serial_port_data(port, portdata);
if (!port->interrupt_in_urb) if (port->interrupt_in_urb) {
continue;
err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
if (err) if (err)
dev_dbg(&port->dev, "%s: submit irq_in urb failed %d\n", dev_dbg(&port->dev, "%s: submit irq_in urb failed %d\n",
__func__, err); __func__, err);
} }
usb_wwan_setup_urbs(serial);
return 0; return 0;
bail_out_error2: bail_out_error2:
for (j = 0; j < N_OUT_URB; j++) for (i = 0; i < N_OUT_URB; i++) {
kfree(portdata->out_buffer[j]); usb_free_urb(portdata->out_urbs[i]);
kfree(portdata->out_buffer[i]);
}
bail_out_error: bail_out_error:
for (j = 0; j < N_IN_URB; j++) for (i = 0; i < N_IN_URB; i++) {
if (portdata->in_buffer[j]) usb_free_urb(portdata->in_urbs[i]);
free_page((unsigned long)portdata->in_buffer[j]); free_page((unsigned long)portdata->in_buffer[i]);
}
kfree(portdata); kfree(portdata);
return 1;
return -ENOMEM;
} }
EXPORT_SYMBOL(usb_wwan_startup); EXPORT_SYMBOL_GPL(usb_wwan_port_probe);
int usb_wwan_port_remove(struct usb_serial_port *port) int usb_wwan_port_remove(struct usb_serial_port *port)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment