Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp4175408pxb; Mon, 8 Feb 2021 09:38:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJyqYZYEwPYGAT8x8lCPrbsZRRRmcuOG26Fh+N9+7CDiHFnpuK3oLxCCpldnKcDOIvsFTCf3 X-Received: by 2002:aa7:dd49:: with SMTP id o9mr18062776edw.14.1612805912636; Mon, 08 Feb 2021 09:38:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612805912; cv=none; d=google.com; s=arc-20160816; b=ba0YBzqV52TZrfn4eQBP+hE7c82xL38S4qnZ9bS64xUXKWGSl5ZmFamUeSq2/YX4hB zpsbV/6LgzpcubGtjvz72ULeglVNtM4CL4lmUzCnUWjVrVk6awmaxqM93pBY/AgMiWW7 6iZBbDBm1epk/5FH4LbVeoZJ42sKZ1d66saO6dXjQEMh8oi9ezkwd2MPHdZSrnUKeqOw ICE7UN7h766xvGmO9hp9IUWOtckqPgAfr/I61GIdUSkck6q7vIwc3nV4XFoB6czeKaD4 QvRaYn573v1dSWYMsWgSZ+WGvcJie5vI/BQEWP6ad7ctCR9Lu2Y2b/8BDv+OhHQ9ZMeb S1BQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=7ERDXzsHXikMpKGzNT7VlXH88pRI+r1o+z7hc4thwrk=; b=05XRKQUizr2od/ZN6JCgrPkwZXuq811Mb4WkeNb6Nsx/CEp4RhKGzafumyS4pvcjE1 +IDK0UvkY3ePG/qablhfWJTbJcTt/j9ikygCK6NEx5/yiV9j9dxkfOJCAZ4KJiYCbqjq L9A8w2GPbOAUvx9OsGrpLZYlu9Y0GRoUy4ddMl0DWh61U3rNSBGQpRFJIWprOXwNrOOu DjySg8Bl30OqlNG8LNoUuzbgKPETnrt76SxhQT1Vr9KyY0MpEKd0YcUZQ+JbUBIXI/za va0FJLAF6GtbnzAsRHZMeTq0Rw0olPxOq0kan3ipKr9wMM3XUzKSh/X1raDmGxsUgUwl x1xQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=oJ3CEF5o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jz24si11792340ejc.430.2021.02.08.09.38.08; Mon, 08 Feb 2021 09:38:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=oJ3CEF5o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232797AbhBHRdM (ORCPT + 99 others); Mon, 8 Feb 2021 12:33:12 -0500 Received: from mail.kernel.org ([198.145.29.99]:36416 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233470AbhBHP3e (ORCPT ); Mon, 8 Feb 2021 10:29:34 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 65A1964E50; Mon, 8 Feb 2021 15:16:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1612797414; bh=oKL58fKocLIGBEmXqpkzY7Q7KGHXNyGg5yjf+FxwM8k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oJ3CEF5ovOmK0okbFYf9g3g5BiqED2I1m4UcsPwrTABQe9PvAcCCpXIqpLXt8wYec zgddKRnKI4bO4KmvJm3wHyktajqrjJt6phmhPjEMTHFirWLqr5Xo5bnbPwcQ65u0hA yNKuz3pqTmwcmJak0ceat/o/F3PLd1VAgLodErEg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andreas Hartmann , Mathias Nyman Subject: [PATCH 5.10 069/120] xhci: fix bounce buffer usage for non-sg list case Date: Mon, 8 Feb 2021 16:00:56 +0100 Message-Id: <20210208145821.163155500@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210208145818.395353822@linuxfoundation.org> References: <20210208145818.395353822@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mathias Nyman commit d4a610635400ccc382792f6be69427078541c678 upstream. xhci driver may in some special cases need to copy small amounts of payload data to a bounce buffer in order to meet the boundary and alignment restrictions set by the xHCI specification. In the majority of these cases the data is in a sg list, and driver incorrectly assumed data is always in urb->sg when using the bounce buffer. If data instead is contiguous, and in urb->transfer_buffer, we may still need to bounce buffer a small part if data starts very close (less than packet size) to a 64k boundary. Check if sg list is used before copying data to/from it. Fixes: f9c589e142d0 ("xhci: TD-fragment, align the unsplittable case with a bounce buffer") Cc: stable@vger.kernel.org Reported-by: Andreas Hartmann Tested-by: Andreas Hartmann Signed-off-by: Mathias Nyman Link: https://lore.kernel.org/r/20210203113702.436762-2-mathias.nyman@linux.intel.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Greg Kroah-Hartman --- drivers/usb/host/xhci-ring.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) --- a/drivers/usb/host/xhci-ring.c +++ b/drivers/usb/host/xhci-ring.c @@ -699,11 +699,16 @@ static void xhci_unmap_td_bounce_buffer( dma_unmap_single(dev, seg->bounce_dma, ring->bounce_buf_len, DMA_FROM_DEVICE); /* for in tranfers we need to copy the data from bounce to sg */ - len = sg_pcopy_from_buffer(urb->sg, urb->num_sgs, seg->bounce_buf, - seg->bounce_len, seg->bounce_offs); - if (len != seg->bounce_len) - xhci_warn(xhci, "WARN Wrong bounce buffer read length: %zu != %d\n", - len, seg->bounce_len); + if (urb->num_sgs) { + len = sg_pcopy_from_buffer(urb->sg, urb->num_sgs, seg->bounce_buf, + seg->bounce_len, seg->bounce_offs); + if (len != seg->bounce_len) + xhci_warn(xhci, "WARN Wrong bounce buffer read length: %zu != %d\n", + len, seg->bounce_len); + } else { + memcpy(urb->transfer_buffer + seg->bounce_offs, seg->bounce_buf, + seg->bounce_len); + } seg->bounce_len = 0; seg->bounce_offs = 0; } @@ -3275,12 +3280,16 @@ static int xhci_align_td(struct xhci_hcd /* create a max max_pkt sized bounce buffer pointed to by last trb */ if (usb_urb_dir_out(urb)) { - len = sg_pcopy_to_buffer(urb->sg, urb->num_sgs, - seg->bounce_buf, new_buff_len, enqd_len); - if (len != new_buff_len) - xhci_warn(xhci, - "WARN Wrong bounce buffer write length: %zu != %d\n", - len, new_buff_len); + if (urb->num_sgs) { + len = sg_pcopy_to_buffer(urb->sg, urb->num_sgs, + seg->bounce_buf, new_buff_len, enqd_len); + if (len != new_buff_len) + xhci_warn(xhci, "WARN Wrong bounce buffer write length: %zu != %d\n", + len, new_buff_len); + } else { + memcpy(seg->bounce_buf, urb->transfer_buffer + enqd_len, new_buff_len); + } + seg->bounce_dma = dma_map_single(dev, seg->bounce_buf, max_pkt, DMA_TO_DEVICE); } else {