From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6E336CA0FFE for ; Tue, 2 Sep 2025 12:29:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To: Content-Transfer-Encoding:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=RUP9TtL1xAhoFb9CaSEK22nm8Sa8vb9894tliSqEP4U=; b=K0Q12wuZQ81qhsNTRzkGOEZG9b LJKGF/oQxYM9wFtzaFhc90yDxs0mOncdokFGQPszGQwdBelrgF1YS4wXcgOoN4ydGGshLjyI1+rZr B+/9qGNx4fBzF+L8gqZ8i+fEWmY/cllgfcSgA/WxoAYZiTLhtVI+1vwhGEmpyRaXEpdgCS6j76CWY TKqKp3+hH/QcbxSGBXKGnpdptHGVR2alNa3HVtWe/g6rB/DzbUOtd0MLUHeJu7O0aeer8ZMacXx65 JIgaW7L2vBqpHU7VdZC/C1YTB4j62AWNN2Wanvpj470+e51VrCedDzN+GSmSMvEtcR+n1CTlQQOUF +NqNHpoQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1utQ8f-0000000HGWt-2gjy; Tue, 02 Sep 2025 12:29:29 +0000 Received: from sea.source.kernel.org ([2600:3c0a:e001:78e:0:1991:8:25]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1utNKS-0000000GNkc-0VSq for linux-nvme@lists.infradead.org; Tue, 02 Sep 2025 09:29:29 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sea.source.kernel.org (Postfix) with ESMTP id 6678F43E7E; Tue, 2 Sep 2025 09:29:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 793FEC4CEED; Tue, 2 Sep 2025 09:29:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1756805367; bh=DnDVES5Jg8WrIGoMPMfvxV9Wox20mcRcpnotwmcTmTg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=u+YP0LKc7Ca9KSEGyhHI9LWKXOpYxDkmlx+xi/d94i28PXwnbkADYdG/kMj0eiK5B zd/GooYAccyXKqdo2LtN0swNMmhiizGQIgjHrkSZ2IAXK/1jBPSRk3StbcZycbdrVP g5quZtmGQMVEWVOV9clnOLPmMIcFNqak1dVeFhnzWaY1VIAyIlM9TCnw1bUSOb10fT eYPBFQEGyOkOZzS7K5YPtS3dmkawe2LLvJU33SWPAwVjDdMQDDLsS8LD6Qfe4rZdHI wki/IP/V+av0oWZTHcazqMPHDSPlXAIhw9qhWxKa10uhggFEbKZZEF+GYaHO+zcGYb 634HFrNZvr1dg== Date: Tue, 2 Sep 2025 12:29:20 +0300 From: Leon Romanovsky To: Jason Gunthorpe , Marek Szyprowski Cc: Abdiel Janulgue , Alexander Potapenko , Alex Gaynor , Andrew Morton , Christoph Hellwig , Danilo Krummrich , iommu@lists.linux.dev, Jason Wang , Jens Axboe , Joerg Roedel , Jonathan Corbet , Juergen Gross , kasan-dev@googlegroups.com, Keith Busch , linux-block@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-nvme@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, linux-trace-kernel@vger.kernel.org, Madhavan Srinivasan , Masami Hiramatsu , Michael Ellerman , "Michael S. Tsirkin" , Miguel Ojeda , Robin Murphy , rust-for-linux@vger.kernel.org, Sagi Grimberg , Stefano Stabellini , Steven Rostedt , virtualization@lists.linux.dev, Will Deacon , xen-devel@lists.xenproject.org Subject: Re: [PATCH v4 00/16] dma-mapping: migrate to physical address-based API Message-ID: <20250902092920.GE10073@unreal> References: <20250828115729.GA10073@unreal> <26bd901a-0812-492d-9736-4a7bb2e6d6b4@samsung.com> <20250901222302.GA186519@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20250901222302.GA186519@nvidia.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250902_022928_200035_6F7380EA X-CRM114-Status: GOOD ( 22.47 ) X-Mailman-Approved-At: Tue, 02 Sep 2025 05:11:04 -0700 X-BeenThere: linux-nvme@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-nvme" Errors-To: linux-nvme-bounces+linux-nvme=archiver.kernel.org@lists.infradead.org On Mon, Sep 01, 2025 at 07:23:02PM -0300, Jason Gunthorpe wrote: > On Mon, Sep 01, 2025 at 11:47:59PM +0200, Marek Szyprowski wrote: > > I would like to give those patches a try in linux-next, but in meantime > > I tested it on my test farm and found a regression in dma_map_resource() > > handling. Namely the dma_map_resource() is no longer possible with size > > not aligned to kmalloc()'ed buffer, as dma_direct_map_phys() calls > > dma_kmalloc_needs_bounce(), > > Hmm, it's this bit: > > capable = dma_capable(dev, dma_addr, size, !(attrs & DMA_ATTR_MMIO)); > if (unlikely(!capable) || dma_kmalloc_needs_bounce(dev, size, dir)) { > if (is_swiotlb_active(dev) && !(attrs & DMA_ATTR_MMIO)) > return swiotlb_map(dev, phys, size, dir, attrs); > > goto err_overflow; > } > > We shouldn't be checking dma_kmalloc_needs_bounce() on mmio as there > is no cache flushing so the "dma safe alignment" for non-coherent DMA > does not apply. > > Like you say looks good to me, and more of the surrouding code can be > pulled in too, no sense in repeating the boolean logic: > > if (attrs & DMA_ATTR_MMIO) { > dma_addr = phys; > if (unlikely(!dma_capable(dev, dma_addr, size, false))) > goto err_overflow; > } else { > dma_addr = phys_to_dma(dev, phys); > if (unlikely(!dma_capable(dev, dma_addr, size, true)) || I tried to reuse same code as much as possible :( > dma_kmalloc_needs_bounce(dev, size, dir)) { > if (is_swiotlb_active(dev)) > return swiotlb_map(dev, phys, size, dir, attrs); > > goto err_overflow; > } > if (!dev_is_dma_coherent(dev) && > !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) > arch_sync_dma_for_device(phys, size, dir); > } Like Jason wrote, but in diff format: diff --git a/kernel/dma/direct.h b/kernel/dma/direct.h index 92dbadcd3b2f..3f4792910604 100644 --- a/kernel/dma/direct.h +++ b/kernel/dma/direct.h @@ -85,7 +85,6 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev, unsigned long attrs) { dma_addr_t dma_addr; - bool capable; if (is_swiotlb_force_bounce(dev)) { if (attrs & DMA_ATTR_MMIO) @@ -94,17 +93,19 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev, return swiotlb_map(dev, phys, size, dir, attrs); } - if (attrs & DMA_ATTR_MMIO) + if (attrs & DMA_ATTR_MMIO) { dma_addr = phys; - else + if (unlikely(dma_capable(dev, dma_addr, size, false))) + goto err_overflow; + } else { dma_addr = phys_to_dma(dev, phys); + if (unlikely(!dma_capable(dev, dma_addr, size, true)) || + dma_kmalloc_needs_bounce(dev, size, dir)) { + if (is_swiotlb_active(dev)) + return swiotlb_map(dev, phys, size, dir, attrs); - capable = dma_capable(dev, dma_addr, size, !(attrs & DMA_ATTR_MMIO)); - if (unlikely(!capable) || dma_kmalloc_needs_bounce(dev, size, dir)) { - if (is_swiotlb_active(dev) && !(attrs & DMA_ATTR_MMIO)) - return swiotlb_map(dev, phys, size, dir, attrs); - - goto err_overflow; + goto err_overflow; + } } if (!dev_is_dma_coherent(dev) && I created new tag with fixed code. https://git.kernel.org/pub/scm/linux/kernel/git/leon/linux-rdma.git/tag/?h=dma-phys-Sep-2 Thanks > > Jason