Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp467976yba; Wed, 15 May 2019 04:38:57 -0700 (PDT) X-Google-Smtp-Source: APXvYqySK/XnB8Uor2QVUvykA1lhb7Ovz/SA1u1TJZDOIBbVvO98O6GSLX3+btyNuCzInb0OtwGS X-Received: by 2002:a65:42c3:: with SMTP id l3mr19495012pgp.372.1557920337463; Wed, 15 May 2019 04:38:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557920337; cv=none; d=google.com; s=arc-20160816; b=rt5sdYuglXSX45SEVw3O74CdCjFMn6jueZiQjlodVtOs4GEo2pvR9WitlsAs3KSIF9 KED882WVeZ5neWYpEsqqbWHU+81hCwl9NdfmyWdV6TbK2AXwe9eJMC5gHrbnQpIm7oUa YNytraZlBagOkULKjyAyVaQ2tcfugMacDd74aMqFoehwo858cOpmjzXKcx8PGWdrNpM4 dcUYRuRbOjSe3w1Rww9HI3ocy/Vpp7yOViuhDkreinmmvPN4KT4B9qzpgQpZlIKijgG4 rQY5Qj0GksUNHipZLpSVhh9LDKQTD1Yd8eeQNfvSKP3IFy65feEbTxwAg9mPgGgCwKCc xcjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=uAh77bHRTU60edg7mOpz8gRUVWdCvIS0Di1ba6msz+s=; b=1HGxo7UsxBpCi29hNPhtmkntvodsq5+I87WXjjC3A1g/5XyPxnNtbosg7CmOoKhe15 MRwVidT91qrgpKkmxbYn16rLXCJe5U+WnqL5Gem2+PrHJbEIjvkKqw5IK45pxSSPrnD7 vuZTIrEuOpjpcEHDyClqZhkoehcxNpu2Uprrf6/AHp0th28Nab8wBo5DCtWG99fdh2fl +ceAUPfuj7NrVA1NQ/eu+2PajUA9wgiTL78o4fsU0KrsmZRtT+WxiQsZvFjKh7QGtxEO iGHMWCMfdJVN5oJpTR381Kamaf76HslLv4Emiy9C6z1R52AkLgRUxsk2BkW0Khodw3Lc sJMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=f1vVnTq7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p16si1596296pgg.59.2019.05.15.04.38.43; Wed, 15 May 2019 04:38:57 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=f1vVnTq7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732684AbfEOLcO (ORCPT + 99 others); Wed, 15 May 2019 07:32:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:43978 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732968AbfEOLcL (ORCPT ); Wed, 15 May 2019 07:32:11 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8A35620843; Wed, 15 May 2019 11:32:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1557919929; bh=U+cZlY8muUchaepQ07mFSd7U+l0+CHSxVPLR5nRyDus=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f1vVnTq77qVHq+Kg2gUsoy+4Fbm6PG2CrHTNQ+ri/XHZoSNL/+uYgqMUC0ChyZtmI RD+G/2YoysSH2muyHYy12ZLp/ObY9hin+Xvyr0iWXLRtM8YFXbtDf7ylTKQedQnr2B pUDOXNOmD/sIpNuOnUZvD7BHd8brltz+n0XKSz14= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Johan Hovold Subject: [PATCH 5.1 10/46] USB: serial: fix unthrottle races Date: Wed, 15 May 2019 12:56:34 +0200 Message-Id: <20190515090621.586245951@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190515090616.670410738@linuxfoundation.org> References: <20190515090616.670410738@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Johan Hovold commit 3f5edd58d040bfa4b74fb89bc02f0bc6b9cd06ab upstream. Fix two long-standing bugs which could potentially lead to memory corruption or leave the port throttled until it is reopened (on weakly ordered systems), respectively, when read-URB completion races with unthrottle(). First, the URB must not be marked as free before processing is complete to prevent it from being submitted by unthrottle() on another CPU. CPU 1 CPU 2 ================ ================ complete() unthrottle() process_urb(); smp_mb__before_atomic(); set_bit(i, free); if (test_and_clear_bit(i, free)) submit_urb(); Second, the URB must be marked as free before checking the throttled flag to prevent unthrottle() on another CPU from failing to observe that the URB needs to be submitted if complete() sees that the throttled flag is set. CPU 1 CPU 2 ================ ================ complete() unthrottle() set_bit(i, free); throttled = 0; smp_mb__after_atomic(); smp_mb(); if (throttled) if (test_and_clear_bit(i, free)) return; submit_urb(); Note that test_and_clear_bit() only implies barriers when the test is successful. To handle the case where the URB is still in use an explicit barrier needs to be added to unthrottle() for the second race condition. Fixes: d83b405383c9 ("USB: serial: add support for multiple read urbs") Signed-off-by: Johan Hovold Signed-off-by: Greg Kroah-Hartman --- drivers/usb/serial/generic.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) --- a/drivers/usb/serial/generic.c +++ b/drivers/usb/serial/generic.c @@ -376,6 +376,7 @@ void usb_serial_generic_read_bulk_callba struct usb_serial_port *port = urb->context; unsigned char *data = urb->transfer_buffer; unsigned long flags; + bool stopped = false; int status = urb->status; int i; @@ -383,33 +384,51 @@ void usb_serial_generic_read_bulk_callba if (urb == port->read_urbs[i]) break; } - set_bit(i, &port->read_urbs_free); dev_dbg(&port->dev, "%s - urb %d, len %d\n", __func__, i, urb->actual_length); switch (status) { case 0: + usb_serial_debug_data(&port->dev, __func__, urb->actual_length, + data); + port->serial->type->process_read_urb(urb); break; case -ENOENT: case -ECONNRESET: case -ESHUTDOWN: dev_dbg(&port->dev, "%s - urb stopped: %d\n", __func__, status); - return; + stopped = true; + break; case -EPIPE: dev_err(&port->dev, "%s - urb stopped: %d\n", __func__, status); - return; + stopped = true; + break; default: dev_dbg(&port->dev, "%s - nonzero urb status: %d\n", __func__, status); - goto resubmit; + break; } - usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data); - port->serial->type->process_read_urb(urb); + /* + * Make sure URB processing is done before marking as free to avoid + * racing with unthrottle() on another CPU. Matches the barriers + * implied by the test_and_clear_bit() in + * usb_serial_generic_submit_read_urb(). + */ + smp_mb__before_atomic(); + set_bit(i, &port->read_urbs_free); + /* + * Make sure URB is marked as free before checking the throttled flag + * to avoid racing with unthrottle() on another CPU. Matches the + * smp_mb() in unthrottle(). + */ + smp_mb__after_atomic(); + + if (stopped) + return; -resubmit: /* Throttle the device if requested by tty */ spin_lock_irqsave(&port->lock, flags); port->throttled = port->throttle_req; @@ -484,6 +503,12 @@ void usb_serial_generic_unthrottle(struc port->throttled = port->throttle_req = 0; spin_unlock_irq(&port->lock); + /* + * Matches the smp_mb__after_atomic() in + * usb_serial_generic_read_bulk_callback(). + */ + smp_mb(); + if (was_throttled) usb_serial_generic_submit_read_urbs(port, GFP_KERNEL); }