fixes for f_mass_storage
PATCH 2/5 usb: gadget: f_mass_storage: If 'ro'/'cdrom' specified, open file as read-only
In reply to Roger QuadrosIf 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
@@ -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
PATCH 1/5 usb: gadget: f_mass_storage: Fix Bulk-only RESET handling
In reply to Roger QuadrosThe 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
@@ -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
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(-)
PATCH 4/5 usb: gadget: f_mass_storage: Fix potential memory leak
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
@@ -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
PATCH 5/5 usb: gadget: f_mass_storage: remove unnecessary initialization
In reply to Roger Quadrosmemset 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
@@ -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
PATCH 3/5 usb: gadget: f_mass_storage: Prevent NULL pointer dereference
In reply to Roger QuadrosPrevent 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
@@ -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
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
Signed-off-by Roger Quadros <[email protected]>--- drivers/usb/gadget/f_mass_storage.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)drivers/usb/gadget/f_mass_storage.c +0
-0
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--
Re: [PATCH 4/5] usb: gadget: f_mass_storage: Fix potential memory leak
In reply to Roger QuadrosQuoted 15 lines · Roger Quadros
If allocation of multiple buffers would fail then we would leak memory. Fix that.
Signed-off-by Roger Quadros <[email protected]>--- drivers/usb/gadget/f_mass_storage.c | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-)drivers/usb/gadget/f_mass_storage.c +0
-0@@ -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
-0rc = -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--
Re: [PATCH 5/5] usb: gadget: f_mass_storage: remove unnecessary initialization
In reply to Roger QuadrosQuoted 13 lines · Roger Quadros
memset sets the entire data structure to zero, so no need to zero initialize its field again.
Signed-off-by Roger Quadros <[email protected]>--- drivers/usb/gadget/f_mass_storage.c | 1 - 1 files changed, 0 insertions(+), 1 deletions(-)drivers/usb/gadget/f_mass_storage.c +0
-0
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--
Re: [PATCH 2/5] usb: gadget: f_mass_storage: If 'ro'/'cdrom' specified, open file as read-only
In reply to Roger QuadrosQuoted 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.
Signed-off-by Roger Quadros <[email protected]>
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--
Re: [PATCH 1/5] usb: gadget: f_mass_storage: Fix Bulk-only RESET handling
In reply to Roger QuadrosQuoted 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.
Signed-off-by Roger Quadros <[email protected]>
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--
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--
Re: [PATCH 3/5] usb: gadget: f_mass_storage: Prevent NULL pointer dereference
In reply to Michal NazarewiczQuoted 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
Signed-off-by Roger Quadros <[email protected]>--- drivers/usb/gadget/f_mass_storage.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)drivers/usb/gadget/f_mass_storage.c +0
-0drivers/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
Re: [PATCH 4/5] usb: gadget: f_mass_storage: Fix potential memory leak
In reply to Michal NazarewiczQuoted 24 lines · ext Michal Nazarewicz
Quoted 21 lines · Roger Quadros
If allocation of multiple buffers would fail then we would leak memory. Fix that.
Signed-off-by Roger Quadros <[email protected]>--- drivers/usb/gadget/f_mass_storage.c | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-)drivers/usb/gadget/f_mass_storage.c +4
-0@@ -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