From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (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 77D8F2248A4 for ; Mon, 7 Apr 2025 07:02:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.165.32 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744009364; cv=none; b=DmwvpmZO4gF4RiGJ0MoIG5iHSFKB3aoSkQlgqF6pj+bcSDxf5kFyn5nU5Ow9Y2KEf/niXtZTeFwq5kBSZ/h1E1f+94Dc6BD2S7ese8RjseThV8XLq5DBizrxPogxjXnAdDh6BWDZw9fMMR7kryKMDtJ22xpdjnRIkgThGq2myz8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744009364; c=relaxed/simple; bh=1IhyjPLKWnrMkW40p9P1nKy/BsU4fCM31d2sMM6t2m8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MiMTPN4L3L0cMUvhcXz8FXEF/96nI1SmxX1XMuMfER0tWqN/x4stX2CB+6k0CWEqRy23JSO57qy8xrTG/mxDwa4Dmts/DHwVyVELbcG04wm5varZYu9/FgD29fZsCtvOamwAxgTYfCLlpuzzlXMzOCdvqc3EsaUBgaeIj0/mbvU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=irWGIVEH; arc=none smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="irWGIVEH" Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 5376Y2Pl027334; Mon, 7 Apr 2025 07:02:38 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=B+JaH VWnjDdducTQrYRm/aNg9/vuSfGL2FFXG2KEIMk=; b=irWGIVEHQjlR+9jX0wYnB L60SsMUm7H6BTQrKzdViJLJo3QHinSx8YbRkZzPq4uwnitzn5OZXsQLFpyfBWcDu Hu1ozMJkzYqWy7iPL25iIG/rKpzd1uE6+hdRpEbUssbfP260TMSxYEBzhGSTEhW/ FzBqtWwYDhquGsKNJaosRlcyW8tfZ73XxV4gX1ddKKAi99k14q5D1qJb7rDHdamj Z5OvjvCvfQsiGHTHiDB71yxr+zKbWL87L6tyIln0rVUV65jf1Ud7KgdXUBin2mVa ThwduE8VWD91P6V2m3uSJD3UkIIFT5GTL5kVp1uSCikhJuR+sVl3FsxsfbHv6aph w== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 45tw2thvj8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 07 Apr 2025 07:02:38 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 5375YcZb022287; Mon, 7 Apr 2025 07:02:37 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 45tty84vch-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 07 Apr 2025 07:02:37 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 53772acF006938; Mon, 7 Apr 2025 07:02:36 GMT Received: from ca-dev112.us.oracle.com (ca-dev112.us.oracle.com [10.129.136.47]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 45tty84vc4-2; Mon, 07 Apr 2025 07:02:36 +0000 From: Harshvardhan Jha To: jgross@suse.com, sstabellini@kernel.org, boris.ostrovsky@oracle.com Cc: harshvardhan.j.jha@oracle.com, xen-devel@lists.xenproject.org, iommu@lists.linux-foundation.org, iommu@lists.linux.dev, stable@vger.kernel.org Subject: [PATCH 5.15.y 1/1] xen/swiotlb: relax alignment requirements Date: Mon, 7 Apr 2025 00:02:35 -0700 Message-ID: <20250407070235.121187-2-harshvardhan.j.jha@oracle.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250407070235.121187-1-harshvardhan.j.jha@oracle.com> References: <20250407070235.121187-1-harshvardhan.j.jha@oracle.com> Precedence: bulk X-Mailing-List: iommu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1095,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-04-07_02,2025-04-03_03,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 spamscore=0 adultscore=0 phishscore=0 malwarescore=0 suspectscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2502280000 definitions=main-2504070049 X-Proofpoint-ORIG-GUID: tPfSqGRKVitYsjbB6pp3paBhevC9fYN7 X-Proofpoint-GUID: tPfSqGRKVitYsjbB6pp3paBhevC9fYN7 [ Upstream commit 099606a7b2d53e99ce31af42894c1fc9d77c6db9] When mapping a buffer for DMA via .map_page or .map_sg DMA operations, there is no need to check the machine frames to be aligned according to the mapped areas size. All what is needed in these cases is that the buffer is contiguous at machine level. So carve out the alignment check from range_straddles_page_boundary() and move it to a helper called by xen_swiotlb_alloc_coherent() and xen_swiotlb_free_coherent() directly. Fixes: 9f40ec84a797 ("xen/swiotlb: add alignment check for dma buffers") Signed-off-by: Harshvardhan Jha --- drivers/xen/swiotlb-xen.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c index 0392841a822fa..65da97be06285 100644 --- a/drivers/xen/swiotlb-xen.c +++ b/drivers/xen/swiotlb-xen.c @@ -75,19 +75,21 @@ static inline phys_addr_t xen_dma_to_phys(struct device *dev, return xen_bus_to_phys(dev, dma_to_phys(dev, dma_addr)); } +static inline bool range_requires_alignment(phys_addr_t p, size_t size) +{ + phys_addr_t algn = 1ULL << (get_order(size) + PAGE_SHIFT); + phys_addr_t bus_addr = pfn_to_bfn(XEN_PFN_DOWN(p)) << XEN_PAGE_SHIFT; + + return IS_ALIGNED(p, algn) && !IS_ALIGNED(bus_addr, algn); +} + static inline int range_straddles_page_boundary(phys_addr_t p, size_t size) { unsigned long next_bfn, xen_pfn = XEN_PFN_DOWN(p); unsigned int i, nr_pages = XEN_PFN_UP(xen_offset_in_page(p) + size); - phys_addr_t algn = 1ULL << (get_order(size) + PAGE_SHIFT); next_bfn = pfn_to_bfn(xen_pfn); - /* If buffer is physically aligned, ensure DMA alignment. */ - if (IS_ALIGNED(p, algn) && - !IS_ALIGNED((phys_addr_t)next_bfn << XEN_PAGE_SHIFT, algn)) - return 1; - for (i = 1; i < nr_pages; i++) if (pfn_to_bfn(++xen_pfn) != ++next_bfn) return 1; @@ -306,7 +308,8 @@ xen_swiotlb_alloc_coherent(struct device *hwdev, size_t size, phys = dma_to_phys(hwdev, *dma_handle); dev_addr = xen_phys_to_dma(hwdev, phys); if (((dev_addr + size - 1 <= dma_mask)) && - !range_straddles_page_boundary(phys, size)) + !range_straddles_page_boundary(phys, size) && + !range_requires_alignment(phys, size)) *dma_handle = dev_addr; else { if (xen_create_contiguous_region(phys, order, @@ -347,6 +350,7 @@ xen_swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr, if (!WARN_ON((dev_addr + size - 1 > dma_mask) || range_straddles_page_boundary(phys, size)) && + !range_requires_alignment(phys, size) && TestClearPageXenRemapped(page)) xen_destroy_contiguous_region(phys, order); -- 2.47.1