linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] Input: xpad - return proper error in error path
@ 2010-11-12  2:48 Axel Lin
  2010-11-12  2:49 ` [PATCH 2/3] Input: xpad - fix a memory leak Axel Lin
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Axel Lin @ 2010-11-12  2:48 UTC (permalink / raw)
  To: linux-kernel
  Cc: Dmitry Torokhov, Marko Friedemann, Christoph Fritz, linux-input

In current implementation, xpad_probe return 0 when
usb_alloc_urb failed for xpad->bulk_out and kzalloc failed for xpad->bdata.

This patch removes the initialization for error variable,
assign the error code at the place the error happens instead.

Signed-off-by: Axel Lin <axel.lin@gmail.com>
---
 drivers/input/joystick/xpad.c |   33 +++++++++++++++++++++++----------
 1 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/drivers/input/joystick/xpad.c b/drivers/input/joystick/xpad.c
index f9fb7fa..39c0265 100644
--- a/drivers/input/joystick/xpad.c
+++ b/drivers/input/joystick/xpad.c
@@ -543,21 +543,25 @@ exit:
 static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
 {
 	struct usb_endpoint_descriptor *ep_irq_out;
-	int error = -ENOMEM;
+	int error;
 
 	if (xpad->xtype != XTYPE_XBOX360 && xpad->xtype != XTYPE_XBOX)
 		return 0;
 
 	xpad->odata = usb_alloc_coherent(xpad->udev, XPAD_PKT_LEN,
 					 GFP_KERNEL, &xpad->odata_dma);
-	if (!xpad->odata)
+	if (!xpad->odata) {
+		error = -ENOMEM;
 		goto fail1;
+	}
 
 	mutex_init(&xpad->odata_mutex);
 
 	xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
-	if (!xpad->irq_out)
+	if (!xpad->irq_out) {
+		error = -ENOMEM;
 		goto fail2;
+	}
 
 	ep_irq_out = &intf->cur_altsetting->endpoint[1].desc;
 	usb_fill_int_urb(xpad->irq_out, xpad->udev,
@@ -789,8 +793,7 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
 	struct usb_xpad *xpad;
 	struct input_dev *input_dev;
 	struct usb_endpoint_descriptor *ep_irq_in;
-	int i;
-	int error = -ENOMEM;
+	int i, error;
 
 	for (i = 0; xpad_device[i].idVendor; i++) {
 		if ((le16_to_cpu(udev->descriptor.idVendor) == xpad_device[i].idVendor) &&
@@ -800,17 +803,23 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
 
 	xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL);
 	input_dev = input_allocate_device();
-	if (!xpad || !input_dev)
+	if (!xpad || !input_dev) {
+		error = -ENOMEM;
 		goto fail1;
+	}
 
 	xpad->idata = usb_alloc_coherent(udev, XPAD_PKT_LEN,
 					 GFP_KERNEL, &xpad->idata_dma);
-	if (!xpad->idata)
+	if (!xpad->idata) {
+		error = -ENOMEM;
 		goto fail1;
+	}
 
 	xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
-	if (!xpad->irq_in)
+	if (!xpad->irq_in) {
+		error = -ENOMEM;
 		goto fail2;
+	}
 
 	xpad->udev = udev;
 	xpad->mapping = xpad_device[i].mapping;
@@ -929,12 +938,16 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
 		 * controller when it shows up
 		 */
 		xpad->bulk_out = usb_alloc_urb(0, GFP_KERNEL);
-		if(!xpad->bulk_out)
+		if (!xpad->bulk_out) {
+			error = -ENOMEM;
 			goto fail5;
+		}
 
 		xpad->bdata = kzalloc(XPAD_PKT_LEN, GFP_KERNEL);
-		if(!xpad->bdata)
+		if (!xpad->bdata) {
+			error = -ENOMEM;
 			goto fail6;
+		}
 
 		xpad->bdata[2] = 0x08;
 		switch (intf->cur_altsetting->desc.bInterfaceNumber) {
-- 
1.7.2




^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/3] Input: xpad - fix a memory leak
  2010-11-12  2:48 [PATCH 1/3] Input: xpad - return proper error in error path Axel Lin
@ 2010-11-12  2:49 ` Axel Lin
  2010-11-12  2:51 ` [PATCH 3/3] Input: xpad - fix resource reclaim in xpad_probe error path Axel Lin
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Axel Lin @ 2010-11-12  2:49 UTC (permalink / raw)
  To: linux-kernel
  Cc: Dmitry Torokhov, Marko Friedemann, Christoph Fritz, linux-input

In xpad_led_disconnect(), what we really want is to kfree(xpad_led).
In xpad_disconnect(), add a missing kfree(xpad->bdata) to fix the memory leak.

Signed-off-by: Axel Lin <axel.lin@gmail.com>
---
 drivers/input/joystick/xpad.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/drivers/input/joystick/xpad.c b/drivers/input/joystick/xpad.c
index 39c0265..26b2f2b 100644
--- a/drivers/input/joystick/xpad.c
+++ b/drivers/input/joystick/xpad.c
@@ -732,7 +732,7 @@ static void xpad_led_disconnect(struct usb_xpad *xpad)
 
 	if (xpad_led) {
 		led_classdev_unregister(&xpad_led->led_cdev);
-		kfree(xpad_led->name);
+		kfree(xpad_led);
 	}
 }
 #else
@@ -989,6 +989,7 @@ static void xpad_disconnect(struct usb_interface *intf)
 
 	usb_set_intfdata(intf, NULL);
 	if (xpad) {
+		kfree(xpad->bdata);
 		xpad_led_disconnect(xpad);
 		input_unregister_device(xpad->dev);
 		xpad_deinit_output(xpad);
-- 
1.7.2




^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/3] Input: xpad - fix resource reclaim in xpad_probe error path
  2010-11-12  2:48 [PATCH 1/3] Input: xpad - return proper error in error path Axel Lin
  2010-11-12  2:49 ` [PATCH 2/3] Input: xpad - fix a memory leak Axel Lin
@ 2010-11-12  2:51 ` Axel Lin
  2010-11-12  6:06 ` [PATCH 1/3] Input: xpad - return proper error in " Dmitry Torokhov
  2010-11-12 13:41 ` Oliver Neukum
  3 siblings, 0 replies; 6+ messages in thread
From: Axel Lin @ 2010-11-12  2:51 UTC (permalink / raw)
  To: linux-kernel
  Cc: Dmitry Torokhov, Marko Friedemann, Christoph Fritz, linux-input

Properly free the resources in error path by the reverse order of resource
allocation.

Signed-off-by: Axel Lin <axel.lin@gmail.com>
---
 drivers/input/joystick/xpad.c |   27 ++++++++++++++++-----------
 1 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/drivers/input/joystick/xpad.c b/drivers/input/joystick/xpad.c
index 26b2f2b..f581301 100644
--- a/drivers/input/joystick/xpad.c
+++ b/drivers/input/joystick/xpad.c
@@ -896,15 +896,15 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
 
 	error = xpad_init_output(intf, xpad);
 	if (error)
-		goto fail2;
+		goto fail3;
 
 	error = xpad_init_ff(xpad);
 	if (error)
-		goto fail3;
+		goto fail4;
 
 	error = xpad_led_probe(xpad);
 	if (error)
-		goto fail3;
+		goto fail5;
 
 	ep_irq_in = &intf->cur_altsetting->endpoint[0].desc;
 	usb_fill_int_urb(xpad->irq_in, udev,
@@ -916,7 +916,7 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
 
 	error = input_register_device(xpad->dev);
 	if (error)
-		goto fail4;
+		goto fail6;
 
 	usb_set_intfdata(intf, xpad);
 
@@ -931,7 +931,7 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
 		xpad->irq_in->dev = xpad->udev;
 		error = usb_submit_urb(xpad->irq_in, GFP_KERNEL);
 		if (error)
-			goto fail4;
+			goto fail7;
 
 		/*
 		 * Setup the message to set the LEDs on the
@@ -940,13 +940,13 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
 		xpad->bulk_out = usb_alloc_urb(0, GFP_KERNEL);
 		if (!xpad->bulk_out) {
 			error = -ENOMEM;
-			goto fail5;
+			goto fail8;
 		}
 
 		xpad->bdata = kzalloc(XPAD_PKT_LEN, GFP_KERNEL);
 		if (!xpad->bdata) {
 			error = -ENOMEM;
-			goto fail6;
+			goto fail9;
 		}
 
 		xpad->bdata[2] = 0x08;
@@ -972,10 +972,15 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
 
 	return 0;
 
- fail6:	usb_free_urb(xpad->bulk_out);
- fail5:	usb_kill_urb(xpad->irq_in);
- fail4:	usb_free_urb(xpad->irq_in);
- fail3:	xpad_deinit_output(xpad);
+ fail9:	usb_free_urb(xpad->bulk_out);
+ fail8:	usb_kill_urb(xpad->irq_in);
+ fail7:	input_unregister_device(input_dev);
+	input_dev = NULL;
+ fail6:	xpad_led_disconnect(xpad);
+ fail5:	if (input_dev)
+		input_ff_destroy(input_dev);
+ fail4:	xpad_deinit_output(xpad);
+ fail3:	usb_free_urb(xpad->irq_in);
  fail2:	usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
  fail1:	input_free_device(input_dev);
 	kfree(xpad);
-- 
1.7.2




^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/3] Input: xpad - return proper error in error path
  2010-11-12  2:48 [PATCH 1/3] Input: xpad - return proper error in error path Axel Lin
  2010-11-12  2:49 ` [PATCH 2/3] Input: xpad - fix a memory leak Axel Lin
  2010-11-12  2:51 ` [PATCH 3/3] Input: xpad - fix resource reclaim in xpad_probe error path Axel Lin
@ 2010-11-12  6:06 ` Dmitry Torokhov
  2010-11-12 13:41 ` Oliver Neukum
  3 siblings, 0 replies; 6+ messages in thread
From: Dmitry Torokhov @ 2010-11-12  6:06 UTC (permalink / raw)
  To: Axel Lin; +Cc: linux-kernel, Marko Friedemann, Christoph Fritz, linux-input

On Fri, Nov 12, 2010 at 10:48:36AM +0800, Axel Lin wrote:
> In current implementation, xpad_probe return 0 when
> usb_alloc_urb failed for xpad->bulk_out and kzalloc failed for xpad->bdata.
> 
> This patch removes the initialization for error variable,
> assign the error code at the place the error happens instead.
> 
> Signed-off-by: Axel Lin <axel.lin@gmail.com>

Applied all 3, thanks Axel.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/3] Input: xpad - return proper error in error path
  2010-11-12  2:48 [PATCH 1/3] Input: xpad - return proper error in error path Axel Lin
                   ` (2 preceding siblings ...)
  2010-11-12  6:06 ` [PATCH 1/3] Input: xpad - return proper error in " Dmitry Torokhov
@ 2010-11-12 13:41 ` Oliver Neukum
  2010-11-12 17:33   ` Dmitry Torokhov
  3 siblings, 1 reply; 6+ messages in thread
From: Oliver Neukum @ 2010-11-12 13:41 UTC (permalink / raw)
  To: Axel Lin
  Cc: linux-kernel, Dmitry Torokhov, Marko Friedemann, Christoph Fritz,
	linux-input

Am Freitag, 12. November 2010, 03:48:36 schrieb Axel Lin:
> In current implementation, xpad_probe return 0 when
> usb_alloc_urb failed for xpad->bulk_out and kzalloc failed for xpad->bdata.
> 
> This patch removes the initialization for error variable,
> assign the error code at the place the error happens instead.

I am afraid you cannot let stand the order of allocations here anyway,
as xpad->irq_in is currently submitted before xpad->bulk_out is allocated.
That however is a race, because the callback for irq_in can call
xpad360w_process_packet(), which will in turn submit the bulk URB.

I am afraid your patch is pointless unless the logic is also fixed.

	Regards
		Oliver

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/3] Input: xpad - return proper error in error path
  2010-11-12 13:41 ` Oliver Neukum
@ 2010-11-12 17:33   ` Dmitry Torokhov
  0 siblings, 0 replies; 6+ messages in thread
From: Dmitry Torokhov @ 2010-11-12 17:33 UTC (permalink / raw)
  To: Oliver Neukum
  Cc: Axel Lin, linux-kernel, Marko Friedemann, Christoph Fritz,
	linux-input

On Fri, Nov 12, 2010 at 02:41:08PM +0100, Oliver Neukum wrote:
> Am Freitag, 12. November 2010, 03:48:36 schrieb Axel Lin:
> > In current implementation, xpad_probe return 0 when
> > usb_alloc_urb failed for xpad->bulk_out and kzalloc failed for xpad->bdata.
> > 
> > This patch removes the initialization for error variable,
> > assign the error code at the place the error happens instead.
> 
> I am afraid you cannot let stand the order of allocations here anyway,
> as xpad->irq_in is currently submitted before xpad->bulk_out is allocated.
> That however is a race, because the callback for irq_in can call
> xpad360w_process_packet(), which will in turn submit the bulk URB.
> 
> I am afraid your patch is pointless unless the logic is also fixed.

I do not think it is fair to call the patch pointless. It does fix the
resource leak and can be a basis for further rework.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-11-12 17:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-11-12  2:48 [PATCH 1/3] Input: xpad - return proper error in error path Axel Lin
2010-11-12  2:49 ` [PATCH 2/3] Input: xpad - fix a memory leak Axel Lin
2010-11-12  2:51 ` [PATCH 3/3] Input: xpad - fix resource reclaim in xpad_probe error path Axel Lin
2010-11-12  6:06 ` [PATCH 1/3] Input: xpad - return proper error in " Dmitry Torokhov
2010-11-12 13:41 ` Oliver Neukum
2010-11-12 17:33   ` Dmitry Torokhov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).