From 97a75e1afbf0679e80cf6a6c56ee0b62af5fd25c Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Fri, 7 Jun 2024 21:55:18 -0500 Subject: [PATCH] usb: Allow overriding bulk write size and increase default Since commit 760b3dffb03d ("qdl: Rework qdl_write to limit write sizes to out_maxpktsize") outgoing transfers has been chunked into blocks of wMaxPacketSize. As reported by Wiktor Drewniak, Maksim Paimushkin, and Luca Weiss in [1] there's huge benefits to be found in reverting this change, but out of caution the limit was untouched. With the transition to libusb, the throughput dropped another ~15% on my machine. The numbers for HighSpeed and SuperSpeed are also in the same ballpark. With SuperSpeed, benchmarking of different chunk sizes in the megabyte range shows improvement over these numbers in the range of 15-20x on the same machine/board combination. The bug report related to the reduction in size describes a host machine running out of swiotlb, from the fact that we requested 1MB physically contiguous memory. It's unclear if attempts was made to correct the kernel's behavior. libusb does inquiry the capabilities of the kernel and will split the buffer and submitting multiple URBs at once, as needed. While no definitive guidance has been found, multiple sources seems to recommend passing 1-2MB of buffer to libusb at a time. So, let's move the default chunk size back up to 1MB and hope that libusb resolves the reported problem. Additionally, introduce a new option --out-chunk-size, which allow the user to override the chunk size. This would allow any user to reduce the size if needed, but also allow the value to be increased as needed. [1] https://github.com/linux-msm/qdl/pull/39 Reported-by: Wiktor Drewniak Reported-by: Maksim Paimushkin Reported-by: Luca Weiss Signed-off-by: Bjorn Andersson --- qdl.c | 12 +++++++++++- qdl.h | 2 ++ usb.c | 23 ++++++++++++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/qdl.c b/qdl.c index e15dcbd..8cc4650 100644 --- a/qdl.c +++ b/qdl.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -101,6 +102,10 @@ static void print_usage(void) __progname); } +enum { + OPT_OUT_CHUNK_SIZE = 1000, +}; + int main(int argc, char **argv) { char *prog_mbn, *storage="ufs"; @@ -110,12 +115,13 @@ int main(int argc, char **argv) int ret; int opt; bool qdl_finalize_provisioning = false; - + long out_chunk_size; static struct option options[] = { {"debug", no_argument, 0, 'd'}, {"include", required_argument, 0, 'i'}, {"finalize-provisioning", no_argument, 0, 'l'}, + {"out-chunk-size", required_argument, 0, OPT_OUT_CHUNK_SIZE }, {"serial", required_argument, 0, 'S'}, {"storage", required_argument, 0, 's'}, {0, 0, 0, 0} @@ -132,6 +138,10 @@ int main(int argc, char **argv) case 'l': qdl_finalize_provisioning = true; break; + case OPT_OUT_CHUNK_SIZE: + out_chunk_size = strtol(optarg, NULL, 10); + qdl_set_out_chunk_size(&qdl, out_chunk_size); + break; case 's': storage = optarg; break; diff --git a/qdl.h b/qdl.h index 0bb310a..b0b9b7d 100644 --- a/qdl.h +++ b/qdl.h @@ -20,6 +20,7 @@ struct qdl_device { size_t in_maxpktsize; size_t out_maxpktsize; + size_t out_chunk_size; char *mappings[MAPPING_SZ]; // array index is the id from the device }; @@ -27,6 +28,7 @@ struct qdl_device { int qdl_open(struct qdl_device *qdl, const char *serial); int qdl_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout); int qdl_write(struct qdl_device *qdl, const void *buf, size_t len); +void qdl_set_out_chunk_size(struct qdl_device *qdl, long size); int firehose_run(struct qdl_device *qdl, const char *incdir, const char *storage); int sahara_run(struct qdl_device *qdl, char *img_arr[], bool single_image, diff --git a/usb.c b/usb.c index 381cfa3..cda5e74 100644 --- a/usb.c +++ b/usb.c @@ -10,6 +10,8 @@ #include "qdl.h" +#define DEFAULT_OUT_CHUNK_SIZE (1024 * 1024) + /* * libusb commit f0cce43f882d ("core: Fix definition and use of enum * libusb_transfer_type") split transfer type and endpoint transfer types. @@ -141,6 +143,20 @@ static int qdl_try_open(libusb_device *dev, struct qdl_device *qdl, const char * qdl->in_maxpktsize = in_size; qdl->out_maxpktsize = out_size; + if (qdl->out_chunk_size && qdl->out_chunk_size % out_size) { + fprintf(stderr, + "WARNING: requested out-chunk-size must be multiple of the device's wMaxPacketSize %ld, using %ld\n", + out_size, out_size); + qdl->out_chunk_size = out_size; + } else if (!qdl->out_chunk_size) { + qdl->out_chunk_size = DEFAULT_OUT_CHUNK_SIZE; + } + + if (qdl_debug) { + fprintf(stderr, "USB: using out-chunk-size of %ld\n", + qdl->out_chunk_size); + } + return 1; } @@ -215,7 +231,7 @@ int qdl_write(struct qdl_device *qdl, const void *buf, size_t len) int ret; while (len > 0) { - xfer = (len > qdl->out_maxpktsize) ? qdl->out_maxpktsize : len; + xfer = (len > qdl->out_chunk_size) ? qdl->out_chunk_size : len; ret = libusb_bulk_transfer(qdl->usb_handle, qdl->out_ep, data, xfer, &actual, 1000); @@ -239,3 +255,8 @@ int qdl_write(struct qdl_device *qdl, const void *buf, size_t len) return count; } + +void qdl_set_out_chunk_size(struct qdl_device *qdl, long size) +{ + qdl->out_chunk_size = size; +}