aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Keeping <john@metanate.com>2017-09-12 05:24:40 -0400
committerFelipe Balbi <felipe.balbi@linux.intel.com>2017-09-28 05:37:24 -0400
commitaddfc5823dbf3e6ed400e98e49c7e64b10e191d6 (patch)
tree14a9eb490836ad46c806a6dccd4c900b8c35e2de
parent0a2ce62b61f2c76d0213edf4e37aaf54a8ddf295 (diff)
usb: gadget: ffs: handle I/O completion in-order
By submitting completed transfers to the system workqueue there is no guarantee that completion events will be queued up in the correct order, as in multi-processor systems there is a thread running for each processor and the work items are not bound to a particular core. This means that several completions are in the queue at the same time, they may be processed in parallel and complete out of order, resulting in data appearing corrupt when read by userspace. Create a single-threaded workqueue for FunctionFS so that data completed requests is passed to userspace in the order in which they complete. Acked-by: Michal Nazarewicz <mina86@mina86.com> Signed-off-by: John Keeping <john@metanate.com> Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
-rw-r--r--drivers/usb/gadget/function/f_fs.c17
-rw-r--r--drivers/usb/gadget/function/u_fs.h1
2 files changed, 14 insertions, 4 deletions
diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index 9990944a7245..8b342587f8ad 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -46,7 +46,8 @@
46static void ffs_data_get(struct ffs_data *ffs); 46static void ffs_data_get(struct ffs_data *ffs);
47static void ffs_data_put(struct ffs_data *ffs); 47static void ffs_data_put(struct ffs_data *ffs);
48/* Creates new ffs_data object. */ 48/* Creates new ffs_data object. */
49static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc)); 49static struct ffs_data *__must_check ffs_data_new(const char *dev_name)
50 __attribute__((malloc));
50 51
51/* Opened counter handling. */ 52/* Opened counter handling. */
52static void ffs_data_opened(struct ffs_data *ffs); 53static void ffs_data_opened(struct ffs_data *ffs);
@@ -780,11 +781,12 @@ static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
780 struct usb_request *req) 781 struct usb_request *req)
781{ 782{
782 struct ffs_io_data *io_data = req->context; 783 struct ffs_io_data *io_data = req->context;
784 struct ffs_data *ffs = io_data->ffs;
783 785
784 ENTER(); 786 ENTER();
785 787
786 INIT_WORK(&io_data->work, ffs_user_copy_worker); 788 INIT_WORK(&io_data->work, ffs_user_copy_worker);
787 schedule_work(&io_data->work); 789 queue_work(ffs->io_completion_wq, &io_data->work);
788} 790}
789 791
790static void __ffs_epfile_read_buffer_free(struct ffs_epfile *epfile) 792static void __ffs_epfile_read_buffer_free(struct ffs_epfile *epfile)
@@ -1500,7 +1502,7 @@ ffs_fs_mount(struct file_system_type *t, int flags,
1500 if (unlikely(ret < 0)) 1502 if (unlikely(ret < 0))
1501 return ERR_PTR(ret); 1503 return ERR_PTR(ret);
1502 1504
1503 ffs = ffs_data_new(); 1505 ffs = ffs_data_new(dev_name);
1504 if (unlikely(!ffs)) 1506 if (unlikely(!ffs))
1505 return ERR_PTR(-ENOMEM); 1507 return ERR_PTR(-ENOMEM);
1506 ffs->file_perms = data.perms; 1508 ffs->file_perms = data.perms;
@@ -1610,6 +1612,7 @@ static void ffs_data_put(struct ffs_data *ffs)
1610 BUG_ON(waitqueue_active(&ffs->ev.waitq) || 1612 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1611 waitqueue_active(&ffs->ep0req_completion.wait) || 1613 waitqueue_active(&ffs->ep0req_completion.wait) ||
1612 waitqueue_active(&ffs->wait)); 1614 waitqueue_active(&ffs->wait));
1615 destroy_workqueue(ffs->io_completion_wq);
1613 kfree(ffs->dev_name); 1616 kfree(ffs->dev_name);
1614 kfree(ffs); 1617 kfree(ffs);
1615 } 1618 }
@@ -1642,7 +1645,7 @@ static void ffs_data_closed(struct ffs_data *ffs)
1642 ffs_data_put(ffs); 1645 ffs_data_put(ffs);
1643} 1646}
1644 1647
1645static struct ffs_data *ffs_data_new(void) 1648static struct ffs_data *ffs_data_new(const char *dev_name)
1646{ 1649{
1647 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL); 1650 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1648 if (unlikely(!ffs)) 1651 if (unlikely(!ffs))
@@ -1650,6 +1653,12 @@ static struct ffs_data *ffs_data_new(void)
1650 1653
1651 ENTER(); 1654 ENTER();
1652 1655
1656 ffs->io_completion_wq = alloc_ordered_workqueue("%s", 0, dev_name);
1657 if (!ffs->io_completion_wq) {
1658 kfree(ffs);
1659 return NULL;
1660 }
1661
1653 refcount_set(&ffs->ref, 1); 1662 refcount_set(&ffs->ref, 1);
1654 atomic_set(&ffs->opened, 0); 1663 atomic_set(&ffs->opened, 0);
1655 ffs->state = FFS_READ_DESCRIPTORS; 1664 ffs->state = FFS_READ_DESCRIPTORS;
diff --git a/drivers/usb/gadget/function/u_fs.h b/drivers/usb/gadget/function/u_fs.h
index 540f1c48c1a8..79f70ebf85dc 100644
--- a/drivers/usb/gadget/function/u_fs.h
+++ b/drivers/usb/gadget/function/u_fs.h
@@ -279,6 +279,7 @@ struct ffs_data {
279 } file_perms; 279 } file_perms;
280 280
281 struct eventfd_ctx *ffs_eventfd; 281 struct eventfd_ctx *ffs_eventfd;
282 struct workqueue_struct *io_completion_wq;
282 bool no_disconnect; 283 bool no_disconnect;
283 struct work_struct reset_work; 284 struct work_struct reset_work;
284 285