[PATCH] swiotlb: check alloc_size before the allocation of a new memory pool

Peng Zhang posted 1 patch 1 year, 11 months ago
There is a newer version of this series
kernel/dma/swiotlb.c | 3 +++
1 file changed, 3 insertions(+)
[PATCH] swiotlb: check alloc_size before the allocation of a new memory pool
Posted by Peng Zhang 1 year, 11 months ago
From: ZhangPeng <zhangpeng362@huawei.com>

The allocation request for swiotlb contiguous memory greater than
128*2KB cannot be fulfilled because it exceeds the maximum contiguous
memory limit. If the swiotlb memory we allocate is larger than 128*2KB,
swiotlb_find_slots() will still schedule the allocation of a new memory
pool, which will increase memory overhead.

Fix it by adding a check with alloc_size no more than 128*2KB before
scheduling the allocation of a new memory pool in swiotlb_find_slots().

Signed-off-by: ZhangPeng <zhangpeng362@huawei.com>
---
 kernel/dma/swiotlb.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
index 33d942615be5..cc92cff02c60 100644
--- a/kernel/dma/swiotlb.c
+++ b/kernel/dma/swiotlb.c
@@ -1126,6 +1126,9 @@ static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr,
 	u64 phys_limit;
 	int index;
 
+	if (alloc_size > IO_TLB_SEGSIZE * IO_TLB_SIZE)
+		return -1;
+
 	rcu_read_lock();
 	list_for_each_entry_rcu(pool, &mem->pools, node) {
 		index = swiotlb_pool_find_slots(dev, pool, orig_addr,
-- 
2.25.1
Re: [PATCH] swiotlb: check alloc_size before the allocation of a new memory pool
Posted by Christoph Hellwig 1 year, 11 months ago
This looks good, but the surrounding code changed quite a bit in
the dma-mapping code.  Can you redo it against dma-mapping for-next
(which should get merged into Linus' tree ASAP as I already sent the
pull request)
Re: [PATCH] swiotlb: check alloc_size before the allocation of a new memory pool
Posted by zhangpeng (AS) 1 year, 11 months ago
On 2024/1/9 1:06, Christoph Hellwig wrote:

> This looks good, but the surrounding code changed quite a bit in
> the dma-mapping code.  Can you redo it against dma-mapping for-next
> (which should get merged into Linus' tree ASAP as I already sent the
> pull request)

Of course, I'll rebase to the for-next branch in the next version. Thanks!

-- 
Best Regards,
Peng
Re: [PATCH] swiotlb: check alloc_size before the allocation of a new memory pool
Posted by Petr Tesarik 1 year, 11 months ago
On 1/8/2024 3:00 PM, Peng Zhang wrote:
> From: ZhangPeng <zhangpeng362@huawei.com>
> 
> The allocation request for swiotlb contiguous memory greater than
> 128*2KB cannot be fulfilled because it exceeds the maximum contiguous
> memory limit. If the swiotlb memory we allocate is larger than 128*2KB,
> swiotlb_find_slots() will still schedule the allocation of a new memory
> pool, which will increase memory overhead.
> 
> Fix it by adding a check with alloc_size no more than 128*2KB before
> scheduling the allocation of a new memory pool in swiotlb_find_slots().
> 
> Signed-off-by: ZhangPeng <zhangpeng362@huawei.com>
> ---
>  kernel/dma/swiotlb.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
> index 33d942615be5..cc92cff02c60 100644
> --- a/kernel/dma/swiotlb.c
> +++ b/kernel/dma/swiotlb.c
> @@ -1126,6 +1126,9 @@ static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr,
>  	u64 phys_limit;
>  	int index;
>  
> +	if (alloc_size > IO_TLB_SEGSIZE * IO_TLB_SIZE)
> +		return -1;
> +
>  	rcu_read_lock();
>  	list_for_each_entry_rcu(pool, &mem->pools, node) {
>  		index = swiotlb_pool_find_slots(dev, pool, orig_addr,

IIUC this such big allocations are not normally required by drivers, but
I have already run into a similar issue with a Raspberry Pi 4 dma-buf
object, so they can be triggered at will by user space. I also believe
this sanity check is a good idea in general, not only when dynamic
SWIOTLB is enabled.

Reviewed-by: Petr Tesarik <petr.tesarik1@huawei-partners.com>

Petr T
Re: [PATCH] swiotlb: check alloc_size before the allocation of a new memory pool
Posted by zhangpeng (AS) 1 year, 11 months ago
On 2024/1/8 23:46, Petr Tesarik wrote:

> On 1/8/2024 3:00 PM, Peng Zhang wrote:
>> From: ZhangPeng <zhangpeng362@huawei.com>
>>
>> The allocation request for swiotlb contiguous memory greater than
>> 128*2KB cannot be fulfilled because it exceeds the maximum contiguous
>> memory limit. If the swiotlb memory we allocate is larger than 128*2KB,
>> swiotlb_find_slots() will still schedule the allocation of a new memory
>> pool, which will increase memory overhead.
>>
>> Fix it by adding a check with alloc_size no more than 128*2KB before
>> scheduling the allocation of a new memory pool in swiotlb_find_slots().
>>
>> Signed-off-by: ZhangPeng <zhangpeng362@huawei.com>
>> ---
>>   kernel/dma/swiotlb.c | 3 +++
>>   1 file changed, 3 insertions(+)
>>
>> diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
>> index 33d942615be5..cc92cff02c60 100644
>> --- a/kernel/dma/swiotlb.c
>> +++ b/kernel/dma/swiotlb.c
>> @@ -1126,6 +1126,9 @@ static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr,
>>   	u64 phys_limit;
>>   	int index;
>>   
>> +	if (alloc_size > IO_TLB_SEGSIZE * IO_TLB_SIZE)
>> +		return -1;
>> +
>>   	rcu_read_lock();
>>   	list_for_each_entry_rcu(pool, &mem->pools, node) {
>>   		index = swiotlb_pool_find_slots(dev, pool, orig_addr,
> IIUC this such big allocations are not normally required by drivers, but
> I have already run into a similar issue with a Raspberry Pi 4 dma-buf
> object, so they can be triggered at will by user space. I also believe
> this sanity check is a good idea in general, not only when dynamic
> SWIOTLB is enabled.
>
> Reviewed-by: Petr Tesarik <petr.tesarik1@huawei-partners.com>
>
> Petr T

Thanks for your review!

-- 
Best Regards,
Peng