From nobody Mon Nov 25 15:30:00 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 3FA8D1632CE; Sun, 27 Oct 2024 14:21:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730038908; cv=none; b=jgtaLtl0V4fjqjYZxLevvOG63i6SZUv1gRecTpvH2ZRiJjG5OddI4bbQtzuYCpxqIG9tW/BP+YEAr/svItn9n5hPyADf+RQNuXSNd/mdire5sSc543UUT84uKqDwjQ4LicdRUEURF7ntunEb6gZ3TYKAMGqOTmvAWmJrIWIp6G8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730038908; c=relaxed/simple; bh=RhcK3jpx80P74UKqRSLHZKtWTOJGp14TVgQFOHpK5uo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mxb2Azq9BhJS7eN5wb1WmkYq11/Uxm7OLuZW3u4+mU76RZ8pFxDUiT8g4syY43gXC3j6h8uwrABFwvv0D5wvkRHlwxZCvjPRhf323vcROSTWCDJeKi9wLfQsHHe8eQKBlJELgJYbdYunpoReRLM7+RKrvmUeN0mToNtmIqc+EL8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nXd1InWC; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nXd1InWC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 53221C4CEE8; Sun, 27 Oct 2024 14:21:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730038908; bh=RhcK3jpx80P74UKqRSLHZKtWTOJGp14TVgQFOHpK5uo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nXd1InWCDEf9wFXEnvYnNA199PsDEzlkERQQaYZC8V+zLcE33wRMLyfgbSH/SMU98 H8d+1ptvk6eIe6/6IES17WIXLlv7+BF2han6/V0cGsFPRXP8l1QOAztijX2Y3GaG2q wnm5X1/7IT2NFH57Zi6tSrsT9ewxKmSpbfJPLHqRaP5V67ASKO9DHXsrl04GbkZFmk FT+T/qAyFvHJUWTerjbYT7JeNh7vI3p8lKZqtn9C9pOWLNQm6u9bVpPJEsmidekpnc 8RVUPCJjT5FAT0cc1xKwpYaiwdDQ/cTA2KzTkUsb9kGeBaFOqkDLV2x2pXhgjZRSLR 6D6mn65kvJ4Qw== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Christoph Hellwig , Sagi Grimberg Cc: Keith Busch , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , Marek Szyprowski , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Andrew Morton , Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-rdma@vger.kernel.org, iommu@lists.linux.dev, linux-nvme@lists.infradead.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 03/18] iommu: generalize the batched sync after map interface Date: Sun, 27 Oct 2024 16:21:03 +0200 Message-ID: <6bcf8efc0e817be5c19c263b6bc43994b411b0c5.1730037276.git.leon@kernel.org> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Christoph Hellwig For the upcoming IOVA-based DMA API we want to use the interface batch the sync after mapping multiple entries from dma-iommu without having a scatterlist. For that move more sanity checks from the callers into __iommu_map and make that function available outside of iommu.c as iommu_map_nosync. Add a wrapper for the map_sync as iommu_sync_map so that callers don't need to poke into the methods directly. Signed-off-by: Christoph Hellwig Signed-off-by: Leon Romanovsky --- drivers/iommu/iommu.c | 65 +++++++++++++++++++------------------------ include/linux/iommu.h | 4 +++ 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 83c8e617a2c5..6b0943397e1e 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2439,8 +2439,8 @@ static size_t iommu_pgsize(struct iommu_domain *domai= n, unsigned long iova, return pgsize; } =20 -static int __iommu_map(struct iommu_domain *domain, unsigned long iova, - phys_addr_t paddr, size_t size, int prot, gfp_t gfp) +int iommu_map_nosync(struct iommu_domain *domain, unsigned long iova, + phys_addr_t paddr, size_t size, int prot, gfp_t gfp) { const struct iommu_domain_ops *ops =3D domain->ops; unsigned long orig_iova =3D iova; @@ -2449,12 +2449,19 @@ static int __iommu_map(struct iommu_domain *domain,= unsigned long iova, phys_addr_t orig_paddr =3D paddr; int ret =3D 0; =20 + might_sleep_if(gfpflags_allow_blocking(gfp)); + if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) return -EINVAL; =20 if (WARN_ON(!ops->map_pages || domain->pgsize_bitmap =3D=3D 0UL)) return -ENODEV; =20 + /* Discourage passing strange GFP flags */ + if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 | + __GFP_HIGHMEM))) + return -EINVAL; + /* find out the minimum page size supported */ min_pagesz =3D 1 << __ffs(domain->pgsize_bitmap); =20 @@ -2502,31 +2509,27 @@ static int __iommu_map(struct iommu_domain *domain,= unsigned long iova, return ret; } =20 -int iommu_map(struct iommu_domain *domain, unsigned long iova, - phys_addr_t paddr, size_t size, int prot, gfp_t gfp) +int iommu_sync_map(struct iommu_domain *domain, unsigned long iova, size_t= size) { const struct iommu_domain_ops *ops =3D domain->ops; - int ret; - - might_sleep_if(gfpflags_allow_blocking(gfp)); =20 - /* Discourage passing strange GFP flags */ - if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 | - __GFP_HIGHMEM))) - return -EINVAL; + if (!ops->iotlb_sync_map) + return 0; + return ops->iotlb_sync_map(domain, iova, size); +} =20 - ret =3D __iommu_map(domain, iova, paddr, size, prot, gfp); - if (ret =3D=3D 0 && ops->iotlb_sync_map) { - ret =3D ops->iotlb_sync_map(domain, iova, size); - if (ret) - goto out_err; - } +int iommu_map(struct iommu_domain *domain, unsigned long iova, + phys_addr_t paddr, size_t size, int prot, gfp_t gfp) +{ + int ret; =20 - return ret; + ret =3D iommu_map_nosync(domain, iova, paddr, size, prot, gfp); + if (ret) + return ret; =20 -out_err: - /* undo mappings already done */ - iommu_unmap(domain, iova, size); + ret =3D iommu_sync_map(domain, iova, size); + if (ret) + iommu_unmap(domain, iova, size); =20 return ret; } @@ -2612,26 +2615,17 @@ ssize_t iommu_map_sg(struct iommu_domain *domain, u= nsigned long iova, struct scatterlist *sg, unsigned int nents, int prot, gfp_t gfp) { - const struct iommu_domain_ops *ops =3D domain->ops; size_t len =3D 0, mapped =3D 0; phys_addr_t start; unsigned int i =3D 0; int ret; =20 - might_sleep_if(gfpflags_allow_blocking(gfp)); - - /* Discourage passing strange GFP flags */ - if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 | - __GFP_HIGHMEM))) - return -EINVAL; - while (i <=3D nents) { phys_addr_t s_phys =3D sg_phys(sg); =20 if (len && s_phys !=3D start + len) { - ret =3D __iommu_map(domain, iova + mapped, start, + ret =3D iommu_map_nosync(domain, iova + mapped, start, len, prot, gfp); - if (ret) goto out_err; =20 @@ -2654,11 +2648,10 @@ ssize_t iommu_map_sg(struct iommu_domain *domain, u= nsigned long iova, sg =3D sg_next(sg); } =20 - if (ops->iotlb_sync_map) { - ret =3D ops->iotlb_sync_map(domain, iova, mapped); - if (ret) - goto out_err; - } + ret =3D iommu_sync_map(domain, iova, mapped); + if (ret) + goto out_err; + return mapped; =20 out_err: diff --git a/include/linux/iommu.h b/include/linux/iommu.h index bd722f473635..8927e5f996c2 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -799,6 +799,10 @@ extern struct iommu_domain *iommu_get_domain_for_dev(s= truct device *dev); extern struct iommu_domain *iommu_get_dma_domain(struct device *dev); extern int iommu_map(struct iommu_domain *domain, unsigned long iova, phys_addr_t paddr, size_t size, int prot, gfp_t gfp); +int iommu_map_nosync(struct iommu_domain *domain, unsigned long iova, + phys_addr_t paddr, size_t size, int prot, gfp_t gfp); +int iommu_sync_map(struct iommu_domain *domain, unsigned long iova, + size_t size); extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size); extern size_t iommu_unmap_fast(struct iommu_domain *domain, --=20 2.46.2