Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp886519lqp; Thu, 23 May 2024 03:05:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXpZxcBIfvqD4TNSetoZsR3Mvb+zSQgro7dcixBBIbmuV3Y+V4UKLOYQfYrR5D4Ra0uYHjnke5BKI3XcBhZq2D/yfqNpgQRSLCL9WoP3Q== X-Google-Smtp-Source: AGHT+IEpMtQvW0ts+zQxb5Wgcs8SeHtxFkg+nS8xwVk55FYcBf2oTORQkaiZewxAfu7BaMwX0HZ2 X-Received: by 2002:a05:6214:3903:b0:6a0:56ad:7d08 with SMTP id 6a1803df08f44-6ab808c9792mr43138636d6.43.1716458739301; Thu, 23 May 2024 03:05:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716458739; cv=pass; d=google.com; s=arc-20160816; b=jWpIVEO7BYD//TuVsTBuaoEXua6jniEE5Xh/4TsChM8IYQbIZNs0+6OjrhF+0Cyfkn CVaMmH9I+0AV9cvZwdxDL05nylaZDJYI1YlrReGeI4EUgp3TkO3ymcRS7r6vqXzj5Hrz a9sOGD8oGQWFDW87EEoiXw6g9dHLECrAfLR7FWQ+PwfEJUEbSkQLA1RKF7N8z7sfcTVY 0K/H3DyXmvFZrzdmUAzkN7AzuygrjhGB3LZp7DrK5/bAwpK6gcO/cx+KG+fGjKr5J72v aZPjG38djk3GtiCJFl+cjnAzfD2aujxMdZDMn6D1h/f1AIfDNHNQ6FS7oUrFQvImGxjA Oh+Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=pAe3f0t7fGkXkM9DVD8JBYyOm30vGE+lYfRxxy+Wvn8=; fh=Jud7WmuGPhQzTH6dBvWIRVN5wYoI6s4RTKgFgQL532k=; b=rh41jhjOTtDQ3VqT+cOlwvNwON2PRV559irz19UQvBnDhBny+X9IsucFN6GEP50Ois p2p5h2tqLrvQxxbP94CgKjiIslKxSoHljTY1pYfE8QOKmGBII6medh9mcQ23c6rmXMlW C8cqsZm++lsrnhtCEhMrU7oXq0HuHhf34YYvkFROZh8HH3WTmTXRL0zMEKqdHzIOwy6x v1m29Q56g1H7uSXFbqPd90qBGObozyILPU4dh1ZaimsGfT8wpDU7v4vs0A873TuH7cH7 K8RxauKsJ9nx69T2lXOBU7PmJorRsB+D2qJ4VEP3Gd6bi+UgqAJDB613ndY2k2L65VoM gG3w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=siengine.com); spf=pass (google.com: domain of linux-kernel+bounces-187305-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-187305-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 6a1803df08f44-6ab75c526absi64773566d6.176.2024.05.23.03.05.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 03:05:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-187305-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=siengine.com); spf=pass (google.com: domain of linux-kernel+bounces-187305-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-187305-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id EED091C20854 for ; Thu, 23 May 2024 10:05:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C6E4B13DDD9; Thu, 23 May 2024 10:05:29 +0000 (UTC) Received: from mail03.siengine.com (mail03.siengine.com [43.240.192.165]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E8CA54FA9; Thu, 23 May 2024 10:05:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=43.240.192.165 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716458729; cv=none; b=HyaaV70CIqHtlqjc71L4YQgsetfytkEEk+h29XHieJXHMBTjU+4KpU6v5krJBAxTIkiZg/J9jljwa3Nxngn1K3ci25hqOmuAKtLqEkpYVGyrRedyDvl1ErB7CAzFoY5XryT/fXt0tZllRbQgXrpenvICf2oTp9ftgu0249zASgo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716458729; c=relaxed/simple; bh=TVULNsPOp0MUxGLrhYYex7VZrXDsn6a1Lrdw1F8dmQA=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=Ll3o/ynoTF8c7ZY7KxkhyAnFbjoIJOf+Yf2jAChHFz/16VuJvp8xWXaqsK9DlJfEaMQgOdZ+y3TtUuUIFhYpmVNRvIRgS9bQm7uCo+ugJcNxXRPblMgCTKm6WsdHVcE3Ix1mNTzKp7DlzSGRcaimbbRYPhzJKVPSraNOr7ev318= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=siengine.com; spf=pass smtp.mailfrom=siengine.com; arc=none smtp.client-ip=43.240.192.165 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=siengine.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=siengine.com Received: from mail03.siengine.com (localhost [127.0.0.2] (may be forged)) by mail03.siengine.com with ESMTP id 44NA3ffa073193; Thu, 23 May 2024 18:03:41 +0800 (+08) (envelope-from hongchi.peng@siengine.com) Received: from dsgsiengine01 ([10.8.1.61]) by mail03.siengine.com with ESMTPS id 44NA3HPr073142 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 23 May 2024 18:03:17 +0800 (+08) (envelope-from hongchi.peng@siengine.com) Received: from SEEXMB04-2019.siengine.com (SEEXMB04-2019.siengine.com [10.8.1.34]) by dsgsiengine01 (SkyGuard) with ESMTPS id 4VlNz801Cvz7ZMhX; Thu, 23 May 2024 18:03:15 +0800 (CST) Received: from SEEXMB03-2019.siengine.com (10.8.1.33) by SEEXMB04-2019.siengine.com (10.8.1.34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.2.1544.11; Thu, 23 May 2024 18:03:15 +0800 Received: from localhost (10.12.10.38) by SEEXMB03-2019.siengine.com (10.8.1.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA) id 15.2.1544.11 via Frontend Transport; Thu, 23 May 2024 18:03:15 +0800 From: Peng Hongchi To: CC: , , , Subject: [PATCH] usb: dwc2: gadget: Don't write invalid mapped sg entries into dma_desc with iommu enabled Date: Thu, 23 May 2024 18:03:15 +0800 Message-ID: <20240523100315.7226-1-hongchi.peng@siengine.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-DKIM-Results: [10.8.1.61]; dkim=none; X-DNSRBL: X-SPAM-SOURCE-CHECK: pass X-MAIL:mail03.siengine.com 44NA3ffa073193 When using dma_map_sg() to map the scatterlist with iommu enabled, the entries in the scatterlist can be mergerd into less but longer entries in the function __finalise_sg(). So that the number of valid mapped entries is actually smaller than ureq->num_reqs,and there are still some invalid entries in the scatterlist with dma_addr=0xffffffff and len=0. Writing these invalid sg entries into the dma_desc can cause a data transmission error. The function dma_map_sg() returns the number of valid map entries and the return value is assigned to usb_request::num_mapped_sgs in function usb_gadget_map_request_by_dev(). So that just write valid mapped entries into dma_desc according to the usb_request::num_mapped_sgs, and set the IOC bit if it's the last valid mapped entry. This patch poses no risk to no-iommu situation, cause ureq->num_mapped_sgs equals ureq->num_sgs while using dma_direct_map_sg() to map the scatterlist whith iommu disabled. Signed-off-by: Peng Hongchi --- drivers/usb/dwc2/gadget.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c index 74ac79abd8f3..e7bf9cc635be 100644 --- a/drivers/usb/dwc2/gadget.c +++ b/drivers/usb/dwc2/gadget.c @@ -885,10 +885,10 @@ static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep, } /* DMA sg buffer */ - for_each_sg(ureq->sg, sg, ureq->num_sgs, i) { + for_each_sg(ureq->sg, sg, ureq->num_mapped_sgs, i) { dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &desc, sg_dma_address(sg) + sg->offset, sg_dma_len(sg), - sg_is_last(sg)); + (i == (ureq->num_mapped_sgs - 1))); desc_count += hs_ep->desc_count; } -- 2.34.1