From nobody Fri Oct 31 16:16:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=actia.se Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1746186087366412.53083826504576; Fri, 2 May 2025 04:41:27 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.974588.1362404 (Exim 4.92) (envelope-from ) id 1uAolH-0004Gu-L3; Fri, 02 May 2025 11:40:59 +0000 Received: by outflank-mailman (output) from mailman id 974588.1362404; Fri, 02 May 2025 11:40:59 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uAolH-0004G9-Fd; Fri, 02 May 2025 11:40:59 +0000 Received: by outflank-mailman (input) for mailman id 974588; Fri, 02 May 2025 11:40:58 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uAolG-0004Db-K0 for xen-devel@lists.xenproject.org; Fri, 02 May 2025 11:40:58 +0000 Received: from mail.actia.se (mail.actia.se [212.181.117.226]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 4ff52dca-274a-11f0-9eb4-5ba50f476ded; Fri, 02 May 2025 13:40:57 +0200 (CEST) Received: from S036ANL.actianordic.se (10.12.31.117) by S035ANL.actianordic.se (10.12.31.116) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Fri, 2 May 2025 13:40:56 +0200 Received: from S036ANL.actianordic.se ([fe80::e13e:1feb:4ea6:ec69]) by S036ANL.actianordic.se ([fe80::e13e:1feb:4ea6:ec69%3]) with mapi id 15.01.2507.039; Fri, 2 May 2025 13:40:56 +0200 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 4ff52dca-274a-11f0-9eb4-5ba50f476ded From: John Ernberg To: Juergen Gross , Stefano Stabellini CC: Oleksandr Tyshchenko , Catalin Marinas , Andrew Morton , "xen-devel@lists.xenproject.org" , "iommu@lists.linux.dev" , "linux-kernel@vger.kernel.org" , "imx@lists.linux.dev" , John Ernberg , "stable@kernel.org" Subject: [PATCH 1/2] xen: swiotlb: Use swiotlb bouncing if kmalloc allocation demands it Thread-Topic: [PATCH 1/2] xen: swiotlb: Use swiotlb bouncing if kmalloc allocation demands it Thread-Index: AQHbu1cQNcuPrbmRHEK42QDjG7hf6w== Date: Fri, 2 May 2025 11:40:55 +0000 Message-ID: <20250502114043.1968976-2-john.ernberg@actia.se> References: <20250502114043.1968976-1-john.ernberg@actia.se> In-Reply-To: <20250502114043.1968976-1-john.ernberg@actia.se> Accept-Language: en-US, sv-SE Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: git-send-email 2.49.0 x-originating-ip: [10.12.12.35] x-esetresult: clean, is OK x-esetid: 37303A2956B14453667460 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-ZM-MESSAGEID: 1746186088423019000 Content-Type: text/plain; charset="utf-8" Xen swiotlb support was missed when the patch set starting with 4ab5f8ec7d71 ("mm/slab: decouple ARCH_KMALLOC_MINALIGN from ARCH_DMA_MINALIGN") was merged. When running Xen on iMX8QXP, a SoC without IOMMU, the effect was that USB transfers ended up corrupted when there was more than one URB inflight at the same time. Add a call to dma_kmalloc_needs_bounce() to make sure that allocations too small for DMA get bounced via swiotlb. Closes: https://lore.kernel.org/linux-usb/ab2776f0-b838-4cf6-a12a-c208eb6aa= d59@actia.se/ Fixes: 4ab5f8ec7d71 ("mm/slab: decouple ARCH_KMALLOC_MINALIGN from ARCH_DMA= _MINALIGN") Cc: stable@kernel.org # v6.5+ Signed-off-by: John Ernberg Reviewed-by: Catalin Marinas Reviewed-by: Stefano Stabellini --- It's impossible to pick an exact fixes tag since this driver was missed in the flagged patch set. I picked one I felt gave a decent enough picture for someone coming across this later. --- drivers/xen/swiotlb-xen.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c index 1f65795cf5d7..ef56a2500ed6 100644 --- a/drivers/xen/swiotlb-xen.c +++ b/drivers/xen/swiotlb-xen.c @@ -217,6 +217,7 @@ static dma_addr_t xen_swiotlb_map_page(struct device *d= ev, struct page *page, * buffering it. */ if (dma_capable(dev, dev_addr, size, true) && + !dma_kmalloc_needs_bounce(dev, size, dir) && !range_straddles_page_boundary(phys, size) && !xen_arch_need_swiotlb(dev, phys, dev_addr) && !is_swiotlb_force_bounce(dev)) --=20 2.49.0 From nobody Fri Oct 31 16:16:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=actia.se Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1746186088580242.10990922002293; Fri, 2 May 2025 04:41:28 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.974589.1362419 (Exim 4.92) (envelope-from ) id 1uAolK-0004gC-QR; Fri, 02 May 2025 11:41:02 +0000 Received: by outflank-mailman (output) from mailman id 974589.1362419; Fri, 02 May 2025 11:41:02 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uAolK-0004g5-N2; Fri, 02 May 2025 11:41:02 +0000 Received: by outflank-mailman (input) for mailman id 974589; Fri, 02 May 2025 11:41:01 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1uAolJ-0004fV-OM for xen-devel@lists.xenproject.org; Fri, 02 May 2025 11:41:01 +0000 Received: from mail.actia.se (mail.actia.se [212.181.117.226]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 4fb32e48-274a-11f0-9ffb-bf95429c2676; Fri, 02 May 2025 13:40:56 +0200 (CEST) Received: from S036ANL.actianordic.se (10.12.31.117) by S036ANL.actianordic.se (10.12.31.117) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Fri, 2 May 2025 13:40:56 +0200 Received: from S036ANL.actianordic.se ([fe80::e13e:1feb:4ea6:ec69]) by S036ANL.actianordic.se ([fe80::e13e:1feb:4ea6:ec69%3]) with mapi id 15.01.2507.039; Fri, 2 May 2025 13:40:56 +0200 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 4fb32e48-274a-11f0-9ffb-bf95429c2676 From: John Ernberg To: Juergen Gross , Stefano Stabellini CC: Oleksandr Tyshchenko , Catalin Marinas , Andrew Morton , "xen-devel@lists.xenproject.org" , "iommu@lists.linux.dev" , "linux-kernel@vger.kernel.org" , "imx@lists.linux.dev" , John Ernberg Subject: [PATCH 2/2] xen: swiotlb: Implement map_resource callback Thread-Topic: [PATCH 2/2] xen: swiotlb: Implement map_resource callback Thread-Index: AQHbu1cR+sKkXAZoXEOAUfrcrER15w== Date: Fri, 2 May 2025 11:40:56 +0000 Message-ID: <20250502114043.1968976-3-john.ernberg@actia.se> References: <20250502114043.1968976-1-john.ernberg@actia.se> In-Reply-To: <20250502114043.1968976-1-john.ernberg@actia.se> Accept-Language: en-US, sv-SE Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: git-send-email 2.49.0 x-originating-ip: [10.12.12.35] x-esetresult: clean, is OK x-esetid: 37303A2955B14453667460 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-ZM-MESSAGEID: 1746186091876019100 Content-Type: text/plain; charset="utf-8" Needed by the eDMA v3 DMA engine found in iommu-less SoCs such as iMX8QXP to be able to perform DMA operations as a Xen Hardware Domain, which needs to be able to do DMA in MMIO space. The callback implementation is basically the same as the one for direct mapping of resources, except this also takes into account Xen page mappings. There is nothing to do for unmap, just like with direct, so the unmap callback is not needed. Signed-off-by: John Ernberg --- I originally exported dma_direct_map_resource() and used that which appeared to work, but it felt like not checking Xen page mappings wasn't fully correct and I went with this. But if dma_direct_map_resource() is the correct approach here then I can submit that isntead. --- drivers/xen/swiotlb-xen.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c index ef56a2500ed6..0f02fdd9128d 100644 --- a/drivers/xen/swiotlb-xen.c +++ b/drivers/xen/swiotlb-xen.c @@ -285,6 +285,20 @@ static void xen_swiotlb_unmap_page(struct device *hwde= v, dma_addr_t dev_addr, attrs, pool); } =20 +static dma_addr_t xen_swiotlb_map_resource(struct device *dev, phys_addr_t= phys, + size_t size, enum dma_data_direction dir, + unsigned long attrs) +{ + dma_addr_t dev_addr =3D xen_phys_to_dma(dev, phys); + + BUG_ON(dir =3D=3D DMA_NONE); + + if (!dma_capable(dev, dev_addr, size, false)) + return DMA_MAPPING_ERROR; + + return dev_addr; +} + static void xen_swiotlb_sync_single_for_cpu(struct device *dev, dma_addr_t dma_addr, size_t size, enum dma_data_direction dir) @@ -426,4 +440,5 @@ const struct dma_map_ops xen_swiotlb_dma_ops =3D { .alloc_pages_op =3D dma_common_alloc_pages, .free_pages =3D dma_common_free_pages, .max_mapping_size =3D swiotlb_max_mapping_size, + .map_resource =3D xen_swiotlb_map_resource, }; --=20 2.49.0