[PATCH v2 2/2] xen/swiotlb: fix allocated size

Juergen Gross posted 2 patches 2 months, 2 weeks ago
[PATCH v2 2/2] xen/swiotlb: fix allocated size
Posted by Juergen Gross 2 months, 2 weeks ago
The allocated size in xen_swiotlb_alloc_coherent() and
xen_swiotlb_free_coherent() is calculated wrong for the case of
XEN_PAGE_SIZE not matching PAGE_SIZE. Fix that.

Fixes: 7250f422da04 ("xen-swiotlb: use actually allocated size on check physical continuous")
Reported-by: Jan Beulich <jbeulich@suse.com>
Signed-off-by: Juergen Gross <jgross@suse.com>
---
V2:
- new patch
---
 drivers/xen/swiotlb-xen.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/xen/swiotlb-xen.c b/drivers/xen/swiotlb-xen.c
index ddf5b1df632e..faa2fb7c74ae 100644
--- a/drivers/xen/swiotlb-xen.c
+++ b/drivers/xen/swiotlb-xen.c
@@ -147,7 +147,7 @@ xen_swiotlb_alloc_coherent(struct device *dev, size_t size,
 	void *ret;
 
 	/* Align the allocation to the Xen page size */
-	size = 1UL << (order + XEN_PAGE_SHIFT);
+	size = ALIGN(size, XEN_PAGE_SIZE);
 
 	ret = (void *)__get_free_pages(flags, get_order(size));
 	if (!ret)
@@ -179,7 +179,7 @@ xen_swiotlb_free_coherent(struct device *dev, size_t size, void *vaddr,
 	int order = get_order(size);
 
 	/* Convert the size to actually allocated. */
-	size = 1UL << (order + XEN_PAGE_SHIFT);
+	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)))
-- 
2.43.0
Re: [PATCH v2 2/2] xen/swiotlb: fix allocated size
Posted by Jan Beulich 2 months, 2 weeks ago
On 16.09.2024 08:47, Juergen Gross wrote:
> The allocated size in xen_swiotlb_alloc_coherent() and
> xen_swiotlb_free_coherent() is calculated wrong for the case of
> XEN_PAGE_SIZE not matching PAGE_SIZE. Fix that.
> 
> Fixes: 7250f422da04 ("xen-swiotlb: use actually allocated size on check physical continuous")
> Reported-by: Jan Beulich <jbeulich@suse.com>
> Signed-off-by: Juergen Gross <jgross@suse.com>

Reviewed-by: Jan Beulich <jbeulich@suse.com>

> --- a/drivers/xen/swiotlb-xen.c
> +++ b/drivers/xen/swiotlb-xen.c
> @@ -147,7 +147,7 @@ xen_swiotlb_alloc_coherent(struct device *dev, size_t size,
>  	void *ret;
>  
>  	/* Align the allocation to the Xen page size */
> -	size = 1UL << (order + XEN_PAGE_SHIFT);
> +	size = ALIGN(size, XEN_PAGE_SIZE);

The way you're doing it has further positive effects, as the size
is now also no longer needlessly over-aligned. May want mentioning
in the description. Hope of course is that no-one came to rely on
the up-to-next-power-of-2 allocation anywhere (which of course
would be a bug there, yet might end in a perceived regression).

Jan
Re: [PATCH v2 2/2] xen/swiotlb: fix allocated size
Posted by Stefano Stabellini 2 months, 1 week ago
On Mon, 16 Sep 2024, Jan Beulich wrote:
> On 16.09.2024 08:47, Juergen Gross wrote:
> > The allocated size in xen_swiotlb_alloc_coherent() and
> > xen_swiotlb_free_coherent() is calculated wrong for the case of
> > XEN_PAGE_SIZE not matching PAGE_SIZE. Fix that.
> > 
> > Fixes: 7250f422da04 ("xen-swiotlb: use actually allocated size on check physical continuous")
> > Reported-by: Jan Beulich <jbeulich@suse.com>
> > Signed-off-by: Juergen Gross <jgross@suse.com>
> 
> Reviewed-by: Jan Beulich <jbeulich@suse.com>

Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>
Re: [PATCH v2 2/2] xen/swiotlb: fix allocated size
Posted by Jürgen Groß 2 months, 2 weeks ago
On 16.09.24 09:59, Jan Beulich wrote:
> On 16.09.2024 08:47, Juergen Gross wrote:
>> The allocated size in xen_swiotlb_alloc_coherent() and
>> xen_swiotlb_free_coherent() is calculated wrong for the case of
>> XEN_PAGE_SIZE not matching PAGE_SIZE. Fix that.
>>
>> Fixes: 7250f422da04 ("xen-swiotlb: use actually allocated size on check physical continuous")
>> Reported-by: Jan Beulich <jbeulich@suse.com>
>> Signed-off-by: Juergen Gross <jgross@suse.com>
> 
> Reviewed-by: Jan Beulich <jbeulich@suse.com>
> 
>> --- a/drivers/xen/swiotlb-xen.c
>> +++ b/drivers/xen/swiotlb-xen.c
>> @@ -147,7 +147,7 @@ xen_swiotlb_alloc_coherent(struct device *dev, size_t size,
>>   	void *ret;
>>   
>>   	/* Align the allocation to the Xen page size */
>> -	size = 1UL << (order + XEN_PAGE_SHIFT);
>> +	size = ALIGN(size, XEN_PAGE_SIZE);
> 
> The way you're doing it has further positive effects, as the size
> is now also no longer needlessly over-aligned. May want mentioning
> in the description. Hope of course is that no-one came to rely on
> the up-to-next-power-of-2 allocation anywhere (which of course
> would be a bug there, yet might end in a perceived regression).

Quite unlikely IMHO, as this is a Xen-only behavior. I'm not aware of
any hardware used with Xen only. So for a regression to happen the driver
allocating DMA memory would need to have a Xen-specific handling relying
on the higher alignment.


Juergen