Skip to contentSkip to conversation

fixes for f_mass_storage

PATCH In review Roger Quadros 2 participants 14 messages Apr 5, 2011 USB

lore
Share & tools
lore
#0 lore raw

cover fixes for f_mass_storage

Hi,

These patches contain some fixes and improvements for f_mass_storage.c

cheers,
-roger

---
Roger Quadros (5):
usb: gadget: f_mass_storage: Fix Bulk-only RESET handling
usb: gadget: f_mass_storage: If 'ro'/'cdrom' specified, open file as
read-only
usb: gadget: f_mass_storage: Prevent NULL pointer dereference
usb: gadget: f_mass_storage: Fix potential memory leak
usb: gadget: f_mass_storage: remove unnecessary initialization

 drivers/usb/gadget/f_mass_storage.c |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
#1 lore raw

PATCH 1/5 usb: gadget: f_mass_storage: Fix Bulk-only RESET handling

The ep0 request tag was not recorded thus resulting in phase problems while sending status/response in handle_execption() handler. This was resulting in MSC compliance test failures with USBCV tool.

With this patch, the Bulk-Only Mass storage RESET request is handled correctly and the MSC compliance tests pass.

---
 drivers/usb/gadget/f_mass_storage.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
drivers/usb/gadget/f_mass_storage.c +5 -0
index ef6c65a..90472af 100644
@@ -613,6 +613,11 @@ static int fsg_setup(struct usb_function *f,
 	if (!fsg_is_set(fsg->common))
 		return -EOPNOTSUPP;
 
+	++fsg->common->ep0_req_tag;	/* Record arrival of a new request */
+	req->context = NULL;
+	req->length = 0;
+	dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
+
 	switch (ctrl->bRequest) {
 
 	case USB_BULK_RESET_REQUEST:
Signature · 1 line
1.6.0.4
Acked-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] usb: gadget: f_mass_storage: Fix Bulk-only RESET handling

Quoted 8 lines · Roger Quadros

The ep0 request tag was not recorded thus resulting in phase problems while sending status/response in handle_execption() handler. This was resulting in MSC compliance test failures with USBCV tool.

With this patch, the Bulk-Only Mass storage RESET request is handled correctly and the MSC compliance tests pass.

Quoted 12 lines
drivers/usb/gadget/f_mass_storage.c +5 -0
@@ -613,6 +613,11 @@ static int fsg_setup(struct usb_function *f,
 	if (!fsg_is_set(fsg->common))
 		return -EOPNOTSUPP;
+	++fsg->common->ep0_req_tag;	/* Record arrival of a new request */
+	req->context = NULL;
+	req->length = 0;
+	dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
+
 	switch (ctrl->bRequest) {
 	case USB_BULK_RESET_REQUEST:
Signature · 4 lines
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
ooo +-----<email/xmpp: [email protected]>-----ooO--(_)--Ooo--
#3 lore raw

PATCH 2/5 usb: gadget: f_mass_storage: If 'ro'/'cdrom' specified, open file as read-only

In reply to Roger Quadros

If we don't need Write access then attempt to open backing file in Read Only mode instead of bailing out too soon.

---
 drivers/usb/gadget/f_mass_storage.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
drivers/usb/gadget/f_mass_storage.c +1 -0
index 90472af..5d7de93 100644
@@ -2810,6 +2810,7 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
 	for (i = 0, lcfg = cfg->luns; i < nluns; ++i, ++curlun, ++lcfg) {
 		curlun->cdrom = !!lcfg->cdrom;
 		curlun->ro = lcfg->cdrom || lcfg->ro;
+		curlun->initially_ro = curlun->ro;
 		curlun->removable = lcfg->removable;
 		curlun->dev.release = fsg_lun_release;
 		curlun->dev.parent = &gadget->dev;
Signature · 1 line
1.6.0.4
Acked-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] usb: gadget: f_mass_storage: If 'ro'/'cdrom' specified, open file as read-only

Quoted 3 lines · Roger Quadros

If we don't need Write access then attempt to open backing file in Read Only mode instead of bailing out too soon.

Quoted 11 lines
drivers/usb/gadget/f_mass_storage.c +1 -0
@@ -2810,6 +2810,7 @@ static struct fsg_common *fsg_common_init(struct  
 fsg_common *common,
 	for (i = 0, lcfg = cfg->luns; i < nluns; ++i, ++curlun, ++lcfg) {
 		curlun->cdrom = !!lcfg->cdrom;
 		curlun->ro = lcfg->cdrom || lcfg->ro;
+		curlun->initially_ro = curlun->ro;
 		curlun->removable = lcfg->removable;
 		curlun->dev.release = fsg_lun_release;

curlun->dev.parent = &gadget->dev;

Signature · 4 lines
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
ooo +-----<email/xmpp: [email protected]>-----ooO--(_)--Ooo--
#5 lore raw

PATCH 3/5 usb: gadget: f_mass_storage: Prevent NULL pointer dereference

In reply to Roger Quadros

Prevent a NULL pointer dereference in fsg_config_from_params() if 'file' parameter is not specified.

---
 drivers/usb/gadget/f_mass_storage.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/usb/gadget/f_mass_storage.c +1 -1
index 5d7de93..f6bd001 100644
@@ -3177,7 +3177,7 @@ fsg_config_from_params(struct fsg_config *cfg,
 		lun->removable = /* Removable by default */
 			params->removable_count <= i || params->removable[i];
 		lun->filename =
-			params->file_count > i && params->file[i][0]
+			params->file_count > i && params->file[i]
 			? params->file[i]
 			: 0;
 	}
Signature · 1 line
1.6.0.4
#6 lore raw

Re: [PATCH 3/5] usb: gadget: f_mass_storage: Prevent NULL pointer dereference

Quoted 2 lines · Roger Quadros

Prevent a NULL pointer dereference in fsg_config_from_params() if 'file' parameter is not specified.

Have you observed this behaviour? I don't see how it could happen with module parameters and if it appears in some gadget it's a bug in the gadget. Not that I'm saying checking for null pointer is a bad idea.

Quoted 10 lines
---
 drivers/usb/gadget/f_mass_storage.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/usb/gadget/f_mass_storage.c +0 -0
index 5d7de93..f6bd001 100644

drivers/usb/gadget/f_mass_storage.c fsg_config_from_params(struct fsg_config *cfg, · line 3180

Quoted 5 lines
drivers/usb/gadget/f_mass_storage.c +1 -1
@@ -3177,7 +3177,7 @@ fsg_config_from_params(struct fsg_config *cfg,
 		lun->removable = /* Removable by default */
 			params->removable_count <= i || params->removable[i];
 		lun->filename =
-			params->file_count > i && params->file[i][0]
+			params->file_count > i && params->file[i]

You're removing the check if an empty file name has been specified. It should read:

+ params->file_count > i && params->file[i] && params->file[i][0]

And since the line is getting pretty long, maybe convert it to a proper “if”. I'm sure Greg will like that. ;)

Quoted 3 lines
drivers/usb/gadget/f_mass_storage.c +0 -0
 			? params->file[i]
 			: 0;
 	}
Signature · 4 lines
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
ooo +-----<email/xmpp: [email protected]>-----ooO--(_)--Ooo--
#7 lore raw

Re: [PATCH 3/5] usb: gadget: f_mass_storage: Prevent NULL pointer dereference

Quoted 7 lines · ext Michal Nazarewicz
Quoted 2 lines · Roger Quadros

Prevent a NULL pointer dereference in fsg_config_from_params() if 'file' parameter is not specified.

Have you observed this behaviour? I don't see how it could happen with module parameters and if it appears in some gadget it's a bug in the

It can happen if the gadget that uses f_mass_storage specifies file_count=1 and doesn't specify a file name.

Quoted 1 line

gadget. Not that I'm saying checking for null pointer is a bad idea.

OK. let's do that then.

Quoted 21 lines
Quoted 12 lines
---
 drivers/usb/gadget/f_mass_storage.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/usb/gadget/f_mass_storage.c +0 -0
index 5d7de93..f6bd001 100644

drivers/usb/gadget/f_mass_storage.c fsg_config_from_params(struct fsg_config *cfg, · line 3180

Quoted 5 lines
drivers/usb/gadget/f_mass_storage.c +1 -1
@@ -3177,7 +3177,7 @@ fsg_config_from_params(struct fsg_config *cfg,
         lun->removable = /* Removable by default */
             params->removable_count <= i || params->removable[i];
         lun->filename =
-            params->file_count > i && params->file[i][0]
+            params->file_count > i && params->file[i]

You're removing the check if an empty file name has been specified. It should read:

Quoted 2 lines

+ params->file_count > i && params->file[i] &&
params->file[i][0]

Right.

Quoted 8 lines

And since the line is getting pretty long, maybe convert it to a proper “if”. I'm sure Greg will like that. ;)

Quoted 3 lines
drivers/usb/gadget/f_mass_storage.c +0 -0
             ? params->file[i]
             : 0;
     }

ok.

Signature · 2 lines
regards,
-roger
#8 lore raw

PATCH 4/5 usb: gadget: f_mass_storage: Fix potential memory leak

In reply to Roger Quadros

If allocation of multiple buffers would fail then we would leak memory. Fix that.

---
 drivers/usb/gadget/f_mass_storage.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
drivers/usb/gadget/f_mass_storage.c +6 -0
index f6bd001..60b4df9 100644
@@ -2863,6 +2863,12 @@ buffhds_first_it:
 		bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
 		if (unlikely(!bh->buf)) {
 			rc = -ENOMEM;
+			/* clean up */
+			while (i < FSG_NUM_BUFFERS) {
+				bh--;
+				kfree(bh->buf);
+				i++;
+			}
 			goto error_release;
 		}
 	} while (--i);
Signature · 1 line
1.6.0.4
#9 lore raw

Re: [PATCH 4/5] usb: gadget: f_mass_storage: Fix potential memory leak

Quoted 15 lines · Roger Quadros

If allocation of multiple buffers would fail then we would leak memory. Fix that.

---
 drivers/usb/gadget/f_mass_storage.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
drivers/usb/gadget/f_mass_storage.c +0 -0
index f6bd001..60b4df9 100644
@@ -2863,6 +2863,12 @@ buffhds_first_it:
 		bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
 		if (unlikely(!bh->buf)) {

drivers/usb/gadget/f_mass_storage.c buffhds_first_it: · line 2874

Quoted 10 lines
drivers/usb/gadget/f_mass_storage.c +6 -0
 			rc = -ENOMEM;
+			/* clean up */
+			while (i < FSG_NUM_BUFFERS) {
+				bh--;
+				kfree(bh->buf);
+				i++;
+			}
 			goto error_release;
 		}
 	} while (--i);

This is handled in fsg_common_release(), isn't it? Feel free to send a patch with a comment explaining that.

Signature · 4 lines
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
ooo +-----<email/xmpp: [email protected]>-----ooO--(_)--Ooo--
#10 lore raw

Re: [PATCH 4/5] usb: gadget: f_mass_storage: Fix potential memory leak

Quoted 24 lines · ext Michal Nazarewicz
Quoted 21 lines · Roger Quadros

If allocation of multiple buffers would fail then we would leak memory. Fix that.

---
 drivers/usb/gadget/f_mass_storage.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
drivers/usb/gadget/f_mass_storage.c +4 -0
index f6bd001..60b4df9 100644
@@ -2863,6 +2863,12 @@ buffhds_first_it:
         bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
         if (unlikely(!bh->buf)) {
             rc = -ENOMEM;
+            /* clean up */
+            while (i < FSG_NUM_BUFFERS) {
+                bh--;
+                kfree(bh->buf);
Quoted 9 lines

drivers/usb/gadget/f_mass_storage.c buffhds_first_it: · line 2874

Quoted 5 lines
drivers/usb/gadget/f_mass_storage.c +2 -0
+                i++;
+            }
             goto error_release;
         }
     } while (--i);

This is handled in fsg_common_release(), isn't it? Feel free to send a patch with a comment explaining that.

Yes it is. This patch is not required.

Signature · 2 lines
regards,
-roger
#11 lore raw

PATCH 5/5 usb: gadget: f_mass_storage: remove unnecessary initialization

In reply to Roger Quadros

memset sets the entire data structure to zero, so no need to zero initialize its field again.

---
 drivers/usb/gadget/f_mass_storage.c |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)
drivers/usb/gadget/f_mass_storage.c +0 -1
index 60b4df9..d3c00e6 100644
@@ -2775,7 +2775,6 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
 		common->free_storage_on_release = 1;
 	} else {
 		memset(common, 0, sizeof *common);
-		common->free_storage_on_release = 0;
 	}
 
 	common->ops = cfg->ops;
Signature · 1 line
1.6.0.4
#12 lore raw

Re: [PATCH 5/5] usb: gadget: f_mass_storage: remove unnecessary initialization

Quoted 13 lines · Roger Quadros

memset sets the entire data structure to zero, so no need to zero initialize its field again.

---
 drivers/usb/gadget/f_mass_storage.c |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)
drivers/usb/gadget/f_mass_storage.c +0 -0
index 60b4df9..d3c00e6 100644

drivers/usb/gadget/f_mass_storage.c static struct fsg_common *fsg_common_init(struct · line 2780

Quoted 7 lines
drivers/usb/gadget/f_mass_storage.c +0 -1
@@ -2775,7 +2775,6 @@ static struct fsg_common *fsg_common_init(struct  
 fsg_common *common,
 		common->free_storage_on_release = 1;
 	} else {
 		memset(common, 0, sizeof *common);
-		common->free_storage_on_release = 0;
 	}
 	common->ops = cfg->ops;

Yes, that's correct, but *I* would still leave that for the sake of symmetry.

Signature · 4 lines
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
ooo +-----<email/xmpp: [email protected]>-----ooO--(_)--Ooo--
#13 lore raw
In reply to Roger Quadros
Quoted 1 line · Roger Quadros

These patches contain some fixes and improvements for f_mass_storage.c

Thanks for the fixes! Comments follow each patch.

Signature · 4 lines
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
ooo +-----<email/xmpp: [email protected]>-----ooO--(_)--Ooo--