[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] xen/swiotlb: Check the segment boundary in range_straddles_page_boundary()


  • To: Lin Liu <lin.liu01@xxxxxxxxxx>, jgross@xxxxxxxx, sstabellini@xxxxxxxxxx
  • From: Ross Lagerwall <ross.lagerwall@xxxxxxxxxx>
  • Date: Wed, 18 Nov 2026 11:25:04 +0000
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=zM3k/2WCvMrCdMm27K8Fi760CqD+YE5EovKRW6Oi8RA=; b=i6vWGs2LH90jfHBwDZLypd1ubIiFjGbKc3D5yDCot8iIpR5qgKa1N+C7mube300eoDkzE/iG7AU7etPtKxDPrQIUpp/EDizxhO+75iQJGx1dvucnMnmyGPwZ85YMP6Dx0LJkOqZaHaB85eEyJ1ytBRKy6mpuI9svRwCpCYH6+ss5hC1f69Eb1o4K7GiOaV2VhevJRPRfkcwijAXXAHodRFyCX++hvsYvCJ+XtfI6vBic8x7EXM0kLe8ZlpQR21+AvnCe/WZAlV+n67tYeFL+u6PkRg2cwaLykE94nZJnrz594q7KtUrCJla0W+ebF2sGVhro2CSjkIG0/Z8jer+31Q==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=vJgKLig6WnslawOZfsuMRvdo23gTnsuta62R4N3FJIVavCfLLIJeWvj/+hKy9nrFJBr5ykFCtnFxXMF5pNrjmgU3qvERASh4AD3o1HHNd+sA+xkZ1z0Dgrn39vXThfaoDHCURfEf+j/iAfMcoqIXzCrHjyGPAMyaPpjmOYvk7UBiOc9uucfL7yASfoagu88bj9e5mAJh5jPtWPb94bWc0l/PSZ8jP3lbubPsGt0cTfqLfxAAhG1745zaXbNqcXUiWYBwb5Zd6Mq9Q84ept2+TQFvgUlt5AcWtZuyi9mNXdZumPfMgN31Yi7x5Cwudv26u/QSzuXlyAiOpjMg8qDe5Q==
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=citrix.com header.i="@citrix.com" header.h="From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck"
  • Authentication-results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com;
  • Cc: oleksandr_tyshchenko@xxxxxxxx, andrew.cooper3@xxxxxxxxxx, xen-devel@xxxxxxxxxxxxxxxxxxxx, iommu@xxxxxxxxxxxxxxx, linux-kernel@xxxxxxxxxxxxxxx
  • Delivery-date: Wed, 18 Nov 2026 11:25:14 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 11/13/26 12:47 PM, Lin Liu wrote:
The block layer applies a device's DMA segment boundary to
pseudo-physical addresses, but in a Xen PV domain the bus address is the
machine address.  A range that is contiguous in both spaces can still
cross the boundary in machine space and reach the device.  AHCI declares
a 4 GiB boundary, and controllers that do not carry across it (e.g.
Marvell 88SE9230) then DMA the tail of the transfer 4 GiB too low.

Make range_straddles_page_boundary() also reject a contiguous range that
crosses the device's segment boundary in bus space, so all its callers
cover it.  Streaming mappings are bounced; coherent allocations are
already naturally aligned via range_requires_alignment().

Signed-off-by: Lin Liu <lin.liu01@xxxxxxxxxx>
---
  drivers/xen/swiotlb-xen.c | 16 +++++++++++-----
  1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c
index e2538824e..181f8cef2 100644
--- a/drivers/xen/swiotlb-xen.c
+++ b/drivers/xen/swiotlb-xen.c
@@ -82,10 +82,13 @@ static inline bool range_requires_alignment(phys_addr_t p, 
size_t size)
        return IS_ALIGNED(p, algn) && !IS_ALIGNED(bus_addr, algn);
  }
-static inline int range_straddles_page_boundary(phys_addr_t p, size_t size)
+static inline int range_straddles_page_boundary(struct device *dev,
+                                               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);
+       unsigned long mask;
+       dma_addr_t dma;
next_bfn = pfn_to_bfn(xen_pfn); @@ -93,7 +96,10 @@ static inline int range_straddles_page_boundary(phys_addr_t p, size_t size)
                if (pfn_to_bfn(++xen_pfn) != ++next_bfn)
                        return 1;
- return 0;
+       /* Contiguous, but may cross the segment boundary in bus space */
+       mask = dma_get_seg_boundary(dev);
+       dma = xen_phys_to_dma(dev, p);
+       return size - 1 <= mask && ((dma ^ (dma + size - 1)) & ~mask);
  }
static struct io_tlb_pool *xen_swiotlb_find_pool(struct device *dev,
@@ -158,7 +164,7 @@ xen_swiotlb_alloc_coherent(struct device *dev, size_t size,
*dma_handle = xen_phys_to_dma(dev, phys);
        if (*dma_handle + size - 1 > dma_mask ||
-           range_straddles_page_boundary(phys, size) ||
+           range_straddles_page_boundary(dev, phys, size) ||
            range_requires_alignment(phys, size)) {
                if (xen_create_contiguous_region(phys, order, fls64(dma_mask),
                                dma_handle) != 0)

Is the change actually needed for alloc_coherent / free_coherent?
The allocation from __get_free_pages() is naturally aligned and
range_requires_alignment() requires it has the same alignment so it is
naturally aligned in bus space. I don't think that it can cross a
segment boundary then.

Ross



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.