drivers/xen/swiotlb-xen.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
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@citrix.com>
---
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)
@@ -185,7 +191,7 @@ xen_swiotlb_free_coherent(struct device *dev, size_t size, void *vaddr,
size = ALIGN(size, XEN_PAGE_SIZE);
if (WARN_ON_ONCE(dma_handle + size - 1 > dev->coherent_dma_mask) ||
- WARN_ON_ONCE(range_straddles_page_boundary(phys, size) ||
+ WARN_ON_ONCE(range_straddles_page_boundary(dev, phys, size) ||
range_requires_alignment(phys, size)))
return;
@@ -233,7 +239,7 @@ static dma_addr_t xen_swiotlb_map_phys(struct device *dev, phys_addr_t phys,
*/
if (dma_capable(dev, dev_addr, size, true, attrs) &&
!dma_kmalloc_needs_bounce(dev, size, dir) &&
- !range_straddles_page_boundary(phys, size) &&
+ !range_straddles_page_boundary(dev, phys, size) &&
!xen_arch_need_swiotlb(dev, phys, dev_addr) &&
!is_swiotlb_force_bounce(dev))
goto done;
base-commit: 7e1392a42ea288d70a266f6553fe06f57f8647ea
--
2.52.0
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@citrix.com>
> ---
> 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
On 18.11.26 12:25, Ross Lagerwall wrote:
> 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@citrix.com>
>> ---
>> 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.
Hmm, agreed.
Juergen
On 18/11/2026 12:24, Jürgen Groß wrote:
> On 18.11.26 12:25, Ross Lagerwall wrote:
>> 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.
>
> Hmm, agreed.
Thanks both. Agreed, the check is unnecessary for the coherent paths.
I had noted this in the commit message ("coherent allocations are already
naturally aligned via range_requires_alignment()"), but the code still
changed them.
For v2 I plan to leave range_straddles_page_boundary() and the coherent
paths untouched, and check only streaming mappings, with a small helper
next to range_straddles_page_boundary():
/* Bus-contiguous, but may still cross the device's segment boundary */
static inline bool range_crosses_seg_boundary(struct device *dev,
dma_addr_t dma, size_t size)
{
unsigned long mask = dma_get_seg_boundary(dev);
return size && ((dma ^ (dma + size - 1)) & ~mask);
}
used in xen_swiotlb_map_phys():
if (dma_capable(dev, dev_addr, size, true, attrs) &&
!dma_kmalloc_needs_bounce(dev, size, dir) &&
!range_straddles_page_boundary(phys, size) &&
!range_crosses_seg_boundary(dev, dev_addr, size) &&
!xen_arch_need_swiotlb(dev, phys, dev_addr) &&
!is_swiotlb_force_bounce(dev))
goto done;
A mapping larger than the boundary window also takes the bounce path:
swiotlb cannot find slots that do not span the boundary, so the mapping
fails instead of the device being handed a crossing range.
This is the shape of the version Jürgen first looked at, before the check
was moved into the helper.
I'll send v2 if you are both happy with that.
Lin
On 19.11.26 05:35, Lin Liu wrote:
> On 18/11/2026 12:24, Jürgen Groß wrote:
>> On 18.11.26 12:25, Ross Lagerwall wrote:
>>> 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.
>>
>> Hmm, agreed.
>
> Thanks both. Agreed, the check is unnecessary for the coherent paths.
> I had noted this in the commit message ("coherent allocations are already
> naturally aligned via range_requires_alignment()"), but the code still
> changed them.
>
> For v2 I plan to leave range_straddles_page_boundary() and the coherent
> paths untouched, and check only streaming mappings, with a small helper
> next to range_straddles_page_boundary():
>
> /* Bus-contiguous, but may still cross the device's segment boundary */
> static inline bool range_crosses_seg_boundary(struct device *dev,
> dma_addr_t dma, size_t size)
> {
> unsigned long mask = dma_get_seg_boundary(dev);
>
> return size && ((dma ^ (dma + size - 1)) & ~mask);
> }
>
> used in xen_swiotlb_map_phys():
>
> if (dma_capable(dev, dev_addr, size, true, attrs) &&
> !dma_kmalloc_needs_bounce(dev, size, dir) &&
> !range_straddles_page_boundary(phys, size) &&
> !range_crosses_seg_boundary(dev, dev_addr, size) &&
> !xen_arch_need_swiotlb(dev, phys, dev_addr) &&
> !is_swiotlb_force_bounce(dev))
> goto done;
>
> A mapping larger than the boundary window also takes the bounce path:
> swiotlb cannot find slots that do not span the boundary, so the mapping
> fails instead of the device being handed a crossing range.
>
> This is the shape of the version Jürgen first looked at, before the check
> was moved into the helper.
>
> I'll send v2 if you are both happy with that.
Could you please add a comment to the alloc_coherent path that the check
isn't needed there due to the alignment guaranteed by __get_free_pages()?
Juergen
On 19/11/2026 05:59, Jürgen Groß wrote: > Could you please add a comment to the alloc_coherent path that the check > isn't needed there due to the alignment guaranteed by __get_free_pages()? Sure, will do in v2. Lin
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 segment 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.
Bounce such streaming mappings. Coherent allocations need no check:
they are naturally aligned in bus space (range_requires_alignment()), so
they cannot cross a boundary they fit in.
Signed-off-by: Lin Liu <lin.liu01@citrix.com>
---
v2:
- Only check streaming mappings; coherent allocations are naturally
aligned in bus space and cannot cross the boundary (Ross, Jürgen).
- Leave range_straddles_page_boundary() unchanged.
- Comment why alloc_coherent needs no boundary check (Jürgen).
drivers/xen/swiotlb-xen.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c
index e2538824e..5041be7d3 100644
--- a/drivers/xen/swiotlb-xen.c
+++ b/drivers/xen/swiotlb-xen.c
@@ -96,6 +96,15 @@ static inline int range_straddles_page_boundary(phys_addr_t p, size_t size)
return 0;
}
+/* Bus-contiguous, but may still cross the device's segment boundary */
+static inline bool range_crosses_seg_boundary(struct device *dev,
+ dma_addr_t dma, size_t size)
+{
+ unsigned long mask = dma_get_seg_boundary(dev);
+
+ return size && ((dma ^ (dma + size - 1)) & ~mask);
+}
+
static struct io_tlb_pool *xen_swiotlb_find_pool(struct device *dev,
dma_addr_t dma_addr)
{
@@ -157,6 +166,7 @@ xen_swiotlb_alloc_coherent(struct device *dev, size_t size,
phys = virt_to_phys(ret);
*dma_handle = xen_phys_to_dma(dev, phys);
+ /* No seg boundary check: __get_free_pages() is naturally aligned */
if (*dma_handle + size - 1 > dma_mask ||
range_straddles_page_boundary(phys, size) ||
range_requires_alignment(phys, size)) {
@@ -234,6 +244,7 @@ static dma_addr_t xen_swiotlb_map_phys(struct device *dev, phys_addr_t phys,
if (dma_capable(dev, dev_addr, size, true, attrs) &&
!dma_kmalloc_needs_bounce(dev, size, dir) &&
!range_straddles_page_boundary(phys, size) &&
+ !range_crosses_seg_boundary(dev, dev_addr, size) &&
!xen_arch_need_swiotlb(dev, phys, dev_addr) &&
!is_swiotlb_force_bounce(dev))
goto done;
base-commit: 7e1392a42ea288d70a266f6553fe06f57f8647ea
--
2.52.0
On 19.11.26 07:38, 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 segment 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. > > Bounce such streaming mappings. Coherent allocations need no check: > they are naturally aligned in bus space (range_requires_alignment()), so > they cannot cross a boundary they fit in. > > Signed-off-by: Lin Liu <lin.liu01@citrix.com> Reviewed-by: Juergen Gross <jgross@suse.com> Juergen
© 2016 - 2026 Red Hat, Inc.