提交 b9e00088 编写于 作者: M Michal Nazarewicz 提交者: Greg Kroah-Hartman

USB: gadget: f_mass_storage: fix in error recovery

In to places in fsg_common_init() an unconditional call to kfree()
on common was performed in error recovery which is not a valid
behaviour since fsg_common structure is not always allocated by
fsg_common_init().

To fix, the calls has been replaced with a goto to a proper error
recovery which does the correct thing.

Also, refactored fsg_common_release() function.
Signed-off-by: NMichal Nazarewicz <mina86@mina86.com>
Reviewed-by: NViral Mehta <viral.mehta@lntinfotech.com>
Signed-off-by: NGreg Kroah-Hartman <gregkh@suse.de>
上级 f537da68
...@@ -2742,10 +2742,8 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common, ...@@ -2742,10 +2742,8 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
/* Maybe allocate device-global string IDs, and patch descriptors */ /* Maybe allocate device-global string IDs, and patch descriptors */
if (fsg_strings[FSG_STRING_INTERFACE].id == 0) { if (fsg_strings[FSG_STRING_INTERFACE].id == 0) {
rc = usb_string_id(cdev); rc = usb_string_id(cdev);
if (rc < 0) { if (unlikely(rc < 0))
kfree(common); goto error_release;
return ERR_PTR(rc);
}
fsg_strings[FSG_STRING_INTERFACE].id = rc; fsg_strings[FSG_STRING_INTERFACE].id = rc;
fsg_intf_desc.iInterface = rc; fsg_intf_desc.iInterface = rc;
} }
...@@ -2753,9 +2751,9 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common, ...@@ -2753,9 +2751,9 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
/* Create the LUNs, open their backing files, and register the /* Create the LUNs, open their backing files, and register the
* LUN devices in sysfs. */ * LUN devices in sysfs. */
curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL); curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL);
if (!curlun) { if (unlikely(!curlun)) {
kfree(common); rc = -ENOMEM;
return ERR_PTR(-ENOMEM); goto error_release;
} }
common->luns = curlun; common->luns = curlun;
...@@ -2914,11 +2912,7 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common, ...@@ -2914,11 +2912,7 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
static void fsg_common_release(struct kref *ref) static void fsg_common_release(struct kref *ref)
{ {
struct fsg_common *common = struct fsg_common *common = container_of(ref, struct fsg_common, ref);
container_of(ref, struct fsg_common, ref);
unsigned i = common->nluns;
struct fsg_lun *lun = common->luns;
struct fsg_buffhd *bh;
/* If the thread isn't already dead, tell it to exit now */ /* If the thread isn't already dead, tell it to exit now */
if (common->state != FSG_STATE_TERMINATED) { if (common->state != FSG_STATE_TERMINATED) {
...@@ -2929,23 +2923,28 @@ static void fsg_common_release(struct kref *ref) ...@@ -2929,23 +2923,28 @@ static void fsg_common_release(struct kref *ref)
complete(&common->thread_notifier); complete(&common->thread_notifier);
} }
/* Beware tempting for -> do-while optimization: when in error if (likely(common->luns)) {
* recovery nluns may be zero. */ struct fsg_lun *lun = common->luns;
unsigned i = common->nluns;
for (; i; --i, ++lun) { /* In error recovery common->nluns may be zero. */
device_remove_file(&lun->dev, &dev_attr_ro); for (; i; --i, ++lun) {
device_remove_file(&lun->dev, &dev_attr_file); device_remove_file(&lun->dev, &dev_attr_ro);
fsg_lun_close(lun); device_remove_file(&lun->dev, &dev_attr_file);
device_unregister(&lun->dev); fsg_lun_close(lun);
} device_unregister(&lun->dev);
}
kfree(common->luns); kfree(common->luns);
}
i = FSG_NUM_BUFFERS; {
bh = common->buffhds; struct fsg_buffhd *bh = common->buffhds;
do { unsigned i = FSG_NUM_BUFFERS;
kfree(bh->buf); do {
} while (++bh, --i); kfree(bh->buf);
} while (++bh, --i);
}
if (common->free_storage_on_release) if (common->free_storage_on_release)
kfree(common); kfree(common);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册