Re: [PATCH net-next v2 2/2] page_pool: optimize the cpu sync operation when DMA mapping

From: Yunsheng Lin
Date: Sun Aug 22 2021 - 23:56:55 EST


On 2021/8/20 17:39, Ilias Apalodimas wrote:
> On Fri, Aug 20, 2021 at 02:56:51PM +0800, Yunsheng Lin wrote:
>> If the DMA_ATTR_SKIP_CPU_SYNC is not set, cpu syncing is
>> also done in dma_map_page_attrs(), so set the attrs according
>> to pool->p.flags to avoid calling cpu sync function again.
>
> Isn't DMA_ATTR_SKIP_CPU_SYNC checked within dma_map_page_attrs() anyway?

Yes, the checking in dma_map_page_attrs() should save us from
calling dma_sync_single_for_device() again if we set the attrs
according to "pool->p.flags & PP_FLAG_DMA_SYNC_DEV".

As dma_sync_single_for_device() is EXPORT_SYMBOL()'ed, and
should be a no-op for dma coherent device, so there may be a
function calling overhead for dma coherent device, letting
dma_map_page_attrs() handling the sync seems to avoid the stack
pushing/poping overhead:

https://elixir.bootlin.com/linux/latest/source/kernel/dma/direct.h#L104

The one thing I am not sure about is that the pool->p.offset
and pool->p.max_len are used to decide the sync range before this
patch, while the sync range is the same as the map range when doing
the sync in dma_map_page_attrs().

I assumed the above is not a issue? only sync more than we need?
and it won't hurt the performance?

>
> Regards
> /Ilias
>>
>> Signed-off-by: Yunsheng Lin <linyunsheng@xxxxxxxxxx>
>> ---
>> net/core/page_pool.c | 9 +++++----
>> 1 file changed, 5 insertions(+), 4 deletions(-)
>>
>> diff --git a/net/core/page_pool.c b/net/core/page_pool.c
>> index 1a69784..3df5554 100644
>> --- a/net/core/page_pool.c
>> +++ b/net/core/page_pool.c
>> @@ -191,8 +191,12 @@ static void page_pool_dma_sync_for_device(struct page_pool *pool,
>>
>> static bool page_pool_dma_map(struct page_pool *pool, struct page *page)
>> {
>> + unsigned long attrs = DMA_ATTR_SKIP_CPU_SYNC;
>> dma_addr_t dma;
>>
>> + if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
>> + attrs = 0;
>> +
>> /* Setup DMA mapping: use 'struct page' area for storing DMA-addr
>> * since dma_addr_t can be either 32 or 64 bits and does not always fit
>> * into page private data (i.e 32bit cpu with 64bit DMA caps)
>> @@ -200,15 +204,12 @@ static bool page_pool_dma_map(struct page_pool *pool, struct page *page)
>> */
>> dma = dma_map_page_attrs(pool->p.dev, page, 0,
>> (PAGE_SIZE << pool->p.order),
>> - pool->p.dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
>> + pool->p.dma_dir, attrs);
>> if (dma_mapping_error(pool->p.dev, dma))
>> return false;
>>
>> page_pool_set_dma_addr(page, dma);
>>
>> - if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
>> - page_pool_dma_sync_for_device(pool, page, pool->p.max_len);
>> -
>> return true;
>> }
>>
>> --
>> 2.7.4
>>
> .
>