Return-Path: Received: from mail-sn1nam01on0111.outbound.protection.outlook.com ([104.47.32.111]:34496 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1755059AbeARVC5 (ORCPT ); Thu, 18 Jan 2018 16:02:57 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: Mateusz Berezecki , Greg Kroah-Hartman , Sasha Levin Subject: [added to the 4.1 stable tree] USB: Increase usbfs transfer limit Date: Thu, 18 Jan 2018 21:00:37 +0000 Message-ID: <20180118205908.3220-137-alexander.levin@microsoft.com> References: <20180118205908.3220-1-alexander.levin@microsoft.com> In-Reply-To: <20180118205908.3220-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Mateusz Berezecki This patch has been added to the stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ Upstream commit 1129d270cbfbb7e2b1ec3dede4a13930bdd10e41 ] Promote a variable keeping track of USB transfer memory usage to a wider data type and allow for higher bandwidth transfers from a large number of USB devices connected to a single host. Signed-off-by: Mateusz Berezecki Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/usb/core/devio.c | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/drivers/usb/core/devio.c b/drivers/usb/core/devio.c index 94a15883f8cc..de9fa65bb03b 100644 --- a/drivers/usb/core/devio.c +++ b/drivers/usb/core/devio.c @@ -113,42 +113,35 @@ enum snoop_when { #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0) =20 /* Limit on the total amount of memory we can allocate for transfers */ -static unsigned usbfs_memory_mb =3D 16; +static u32 usbfs_memory_mb =3D 16; module_param(usbfs_memory_mb, uint, 0644); MODULE_PARM_DESC(usbfs_memory_mb, "maximum MB allowed for usbfs buffers (0 =3D no limit)"); =20 -/* Hard limit, necessary to avoid arithmetic overflow */ -#define USBFS_XFER_MAX (UINT_MAX / 2 - 1000000) - -static atomic_t usbfs_memory_usage; /* Total memory currently allocated */ +static atomic64_t usbfs_memory_usage; /* Total memory currently allocated = */ =20 /* Check whether it's okay to allocate more memory for a transfer */ -static int usbfs_increase_memory_usage(unsigned amount) +static int usbfs_increase_memory_usage(u64 amount) { - unsigned lim; + u64 lim; =20 - /* - * Convert usbfs_memory_mb to bytes, avoiding overflows. - * 0 means use the hard limit (effectively unlimited). - */ lim =3D ACCESS_ONCE(usbfs_memory_mb); - if (lim =3D=3D 0 || lim > (USBFS_XFER_MAX >> 20)) - lim =3D USBFS_XFER_MAX; - else - lim <<=3D 20; + lim <<=3D 20; =20 - atomic_add(amount, &usbfs_memory_usage); - if (atomic_read(&usbfs_memory_usage) <=3D lim) - return 0; - atomic_sub(amount, &usbfs_memory_usage); - return -ENOMEM; + atomic64_add(amount, &usbfs_memory_usage); + + if (lim > 0 && atomic64_read(&usbfs_memory_usage) > lim) { + atomic64_sub(amount, &usbfs_memory_usage); + return -ENOMEM; + } + + return 0; } =20 /* Memory for a transfer is being deallocated */ -static void usbfs_decrease_memory_usage(unsigned amount) +static void usbfs_decrease_memory_usage(u64 amount) { - atomic_sub(amount, &usbfs_memory_usage); + atomic64_sub(amount, &usbfs_memory_usage); } =20 static int connected(struct usb_dev_state *ps) @@ -1077,7 +1070,7 @@ static int proc_bulk(struct usb_dev_state *ps, void _= _user *arg) if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN))) return -EINVAL; len1 =3D bulk.len; - if (len1 >=3D USBFS_XFER_MAX) + if (len1 >=3D (INT_MAX - sizeof(struct urb))) return -EINVAL; ret =3D usbfs_increase_memory_usage(len1 + sizeof(struct urb)); if (ret) @@ -1420,10 +1413,6 @@ static int proc_do_submiturb(struct usb_dev_state *p= s, struct usbdevfs_urb *uurb return -EINVAL; } =20 - if (uurb->buffer_length >=3D USBFS_XFER_MAX) { - ret =3D -EINVAL; - goto error; - } if (uurb->buffer_length > 0 && !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ, uurb->buffer, uurb->buffer_length)) { --=20 2.11.0