[PATCH] net: ena: Do not waste napi skb cache

Hyeonggon Yoo posted 1 patch 4 years, 5 months ago
drivers/net/ethernet/amazon/ena/ena_netdev.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] net: ena: Do not waste napi skb cache
Posted by Hyeonggon Yoo 4 years, 5 months ago
By profiling, discovered that ena device driver allocates skb by
build_skb() and frees by napi_skb_cache_put(). Because the driver
does not use napi skb cache in allocation path, napi skb cache is
periodically filled and flushed. This is waste of napi skb cache.

As ena_alloc_skb() is called only in napi, Use napi_build_skb()
instead of build_skb() to when allocating skb.

This patch was tested on aws a1.metal instance.

Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>
---
 drivers/net/ethernet/amazon/ena/ena_netdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/amazon/ena/ena_netdev.c b/drivers/net/ethernet/amazon/ena/ena_netdev.c
index c72f0c7ff4aa..2c67fb1703c5 100644
--- a/drivers/net/ethernet/amazon/ena/ena_netdev.c
+++ b/drivers/net/ethernet/amazon/ena/ena_netdev.c
@@ -1407,7 +1407,7 @@ static struct sk_buff *ena_alloc_skb(struct ena_ring *rx_ring, void *first_frag)
 		skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
 						rx_ring->rx_copybreak);
 	else
-		skb = build_skb(first_frag, ENA_PAGE_SIZE);
+		skb = napi_build_skb(first_frag, ENA_PAGE_SIZE);
 
 	if (unlikely(!skb)) {
 		ena_increase_stat(&rx_ring->rx_stats.skb_alloc_fail, 1,
-- 
2.33.1

Re: [PATCH] net: ena: Do not waste napi skb cache
Posted by Julian Wiedmann 4 years, 5 months ago
On 23.01.22 13:56, Hyeonggon Yoo wrote:
> By profiling, discovered that ena device driver allocates skb by
> build_skb() and frees by napi_skb_cache_put(). Because the driver
> does not use napi skb cache in allocation path, napi skb cache is
> periodically filled and flushed. This is waste of napi skb cache.
> 
> As ena_alloc_skb() is called only in napi, Use napi_build_skb()
> instead of build_skb() to when allocating skb.
> 
> This patch was tested on aws a1.metal instance.
> 
> Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>
> ---
>  drivers/net/ethernet/amazon/ena/ena_netdev.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/net/ethernet/amazon/ena/ena_netdev.c b/drivers/net/ethernet/amazon/ena/ena_netdev.c
> index c72f0c7ff4aa..2c67fb1703c5 100644
> --- a/drivers/net/ethernet/amazon/ena/ena_netdev.c
> +++ b/drivers/net/ethernet/amazon/ena/ena_netdev.c
> @@ -1407,7 +1407,7 @@ static struct sk_buff *ena_alloc_skb(struct ena_ring *rx_ring, void *first_frag)
>  		skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
>  						rx_ring->rx_copybreak);

To keep things consistent, this should then also be napi_alloc_skb().

>  	else
> -		skb = build_skb(first_frag, ENA_PAGE_SIZE);
> +		skb = napi_build_skb(first_frag, ENA_PAGE_SIZE);
>  
>  	if (unlikely(!skb)) {
>  		ena_increase_stat(&rx_ring->rx_stats.skb_alloc_fail, 1,

Re: [PATCH] net: ena: Do not waste napi skb cache
Posted by Julian Wiedmann 4 years, 5 months ago
On 24.01.22 10:57, Julian Wiedmann wrote:
> On 23.01.22 13:56, Hyeonggon Yoo wrote:
>> By profiling, discovered that ena device driver allocates skb by
>> build_skb() and frees by napi_skb_cache_put(). Because the driver
>> does not use napi skb cache in allocation path, napi skb cache is
>> periodically filled and flushed. This is waste of napi skb cache.
>>
>> As ena_alloc_skb() is called only in napi, Use napi_build_skb()
>> instead of build_skb() to when allocating skb.
>>
>> This patch was tested on aws a1.metal instance.
>>
>> Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>
>> ---
>>  drivers/net/ethernet/amazon/ena/ena_netdev.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/net/ethernet/amazon/ena/ena_netdev.c b/drivers/net/ethernet/amazon/ena/ena_netdev.c
>> index c72f0c7ff4aa..2c67fb1703c5 100644
>> --- a/drivers/net/ethernet/amazon/ena/ena_netdev.c
>> +++ b/drivers/net/ethernet/amazon/ena/ena_netdev.c
>> @@ -1407,7 +1407,7 @@ static struct sk_buff *ena_alloc_skb(struct ena_ring *rx_ring, void *first_frag)
>>  		skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
>>  						rx_ring->rx_copybreak);
> 
> To keep things consistent, this should then also be napi_alloc_skb().
> 

And on closer look, this copybreak path also looks buggy. If rx_copybreak
gets reduced _while_ receiving a frame, the allocated skb can end up too
small to take all the data.

@ ena maintainers: can you please fix this?

>>  	else
>> -		skb = build_skb(first_frag, ENA_PAGE_SIZE);
>> +		skb = napi_build_skb(first_frag, ENA_PAGE_SIZE);
>>  
>>  	if (unlikely(!skb)) {
>>  		ena_increase_stat(&rx_ring->rx_stats.skb_alloc_fail, 1,
> 

Re: [PATCH] net: ena: Do not waste napi skb cache
Posted by Shay Agroskin 4 years, 5 months ago
Julian Wiedmann <jwiedmann.dev@gmail.com> writes:

> On 24.01.22 10:57, Julian Wiedmann wrote:
>> On 23.01.22 13:56, Hyeonggon Yoo wrote:
>>> By profiling, discovered that ena device driver allocates skb 
>>> by
>>> build_skb() and frees by napi_skb_cache_put(). Because the 
>>> driver
>>> does not use napi skb cache in allocation path, napi skb cache 
>>> is
>>> periodically filled and flushed. This is waste of napi skb 
>>> cache.
>>>
>>> As ena_alloc_skb() is called only in napi, Use 
>>> napi_build_skb()
>>> instead of build_skb() to when allocating skb.
>>>
>>> This patch was tested on aws a1.metal instance.
>>>
>>> Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>
>>> ---
>>>  drivers/net/ethernet/amazon/ena/ena_netdev.c | 2 +-
>>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/net/ethernet/amazon/ena/ena_netdev.c 
>>> b/drivers/net/ethernet/amazon/ena/ena_netdev.c
>>> index c72f0c7ff4aa..2c67fb1703c5 100644
>>> --- a/drivers/net/ethernet/amazon/ena/ena_netdev.c
>>> +++ b/drivers/net/ethernet/amazon/ena/ena_netdev.c
>>> @@ -1407,7 +1407,7 @@ static struct sk_buff 
>>> *ena_alloc_skb(struct ena_ring *rx_ring, void *first_frag)
>>>  		skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
>>>  						rx_ring->rx_copybreak);
>> 
>> To keep things consistent, this should then also be 
>> napi_alloc_skb().
>> 
>
> And on closer look, this copybreak path also looks buggy. If 
> rx_copybreak
> gets reduced _while_ receiving a frame, the allocated skb can 
> end up too
> small to take all the data.
>
> @ ena maintainers: can you please fix this?
>

Updating the copybreak value is done through ena_ethtool.c 
(ena_set_tunable()) which updates `adapter->rx_copybreak`.
The adapter->rx_copybreak value is "propagated back" to the ring 
local attributes (rx_ring->rx_copybreak) only after an interface 
toggle which stops the napi routine first.

Unless I'm missing something here I don't think the bug you're 
describing exists.

I agree that the netdev_alloc_skb_ip_align() can become 
napi_alloc_skb(). Hyeonggon Yoo, can you please apply this change 
as well to this patch?

Thanks,
Shay


>>>  	else
>>> -		skb = build_skb(first_frag, ENA_PAGE_SIZE);
>>> +		skb = napi_build_skb(first_frag, ENA_PAGE_SIZE);
>>>  
>>>  	if (unlikely(!skb)) {
>>>  		ena_increase_stat(&rx_ring->rx_stats.skb_alloc_fail, 
>>>  1,
>> 
Re: [PATCH] net: ena: Do not waste napi skb cache
Posted by Julian Wiedmann 4 years, 5 months ago
On 24.01.22 22:50, Shay Agroskin wrote:
> 
> Julian Wiedmann <jwiedmann.dev@gmail.com> writes:
> 
>> On 24.01.22 10:57, Julian Wiedmann wrote:
>>> On 23.01.22 13:56, Hyeonggon Yoo wrote:
>>>> By profiling, discovered that ena device driver allocates skb by
>>>> build_skb() and frees by napi_skb_cache_put(). Because the driver
>>>> does not use napi skb cache in allocation path, napi skb cache is
>>>> periodically filled and flushed. This is waste of napi skb cache.
>>>>
>>>> As ena_alloc_skb() is called only in napi, Use napi_build_skb()
>>>> instead of build_skb() to when allocating skb.
>>>>
>>>> This patch was tested on aws a1.metal instance.
>>>>
>>>> Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>
>>>> ---
>>>>  drivers/net/ethernet/amazon/ena/ena_netdev.c | 2 +-
>>>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>>>
>>>> diff --git a/drivers/net/ethernet/amazon/ena/ena_netdev.c b/drivers/net/ethernet/amazon/ena/ena_netdev.c
>>>> index c72f0c7ff4aa..2c67fb1703c5 100644
>>>> --- a/drivers/net/ethernet/amazon/ena/ena_netdev.c
>>>> +++ b/drivers/net/ethernet/amazon/ena/ena_netdev.c
>>>> @@ -1407,7 +1407,7 @@ static struct sk_buff *ena_alloc_skb(struct ena_ring *rx_ring, void *first_frag)
>>>>          skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
>>>>                          rx_ring->rx_copybreak);
>>>
>>> To keep things consistent, this should then also be napi_alloc_skb().
>>>
>>
>> And on closer look, this copybreak path also looks buggy. If rx_copybreak
>> gets reduced _while_ receiving a frame, the allocated skb can end up too
>> small to take all the data.
>>
>> @ ena maintainers: can you please fix this?
>>
> 
> Updating the copybreak value is done through ena_ethtool.c (ena_set_tunable()) which updates `adapter->rx_copybreak`.
> The adapter->rx_copybreak value is "propagated back" to the ring local attributes (rx_ring->rx_copybreak) only after an interface toggle which stops the napi routine first.
> 

That's unfortunate. ena_get_tunable() returns the updated adapter->rx_copybreak, how would a user know that their update isn't actually live yet?

> Unless I'm missing something here I don't think the bug you're describing exists.
> 

ack, thanks for double-checking!

> I agree that the netdev_alloc_skb_ip_align() can become napi_alloc_skb(). Hyeonggon Yoo, can you please apply this change as well to this patch?
> 
> Thanks,
> Shay
> 
> 
>>>>      else
>>>> -        skb = build_skb(first_frag, ENA_PAGE_SIZE);
>>>> +        skb = napi_build_skb(first_frag, ENA_PAGE_SIZE);
>>>>  
>>>>      if (unlikely(!skb)) {
>>>>          ena_increase_stat(&rx_ring->rx_stats.skb_alloc_fail,  1,
>>>

[PATCH v2] net: ena: Do not waste napi skb cache
Posted by Hyeonggon Yoo 4 years, 5 months ago
By profiling, discovered that ena device driver allocates skb by
build_skb() and frees by napi_skb_cache_put(). Because the driver
does not use napi skb cache in allocation path, napi skb cache is
periodically filled and flushed. This is waste of napi skb cache.

As ena_alloc_skb() is called only in napi, Use napi_build_skb()
and napi_alloc_skb() when allocating skb.

This patch was tested on aws a1.metal instance.

[ jwiedmann.dev@gmail.com: Use napi_alloc_skb() instead of
  netdev_alloc_skb_ip_align() to keep things consistent. ]

Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>
---
 drivers/net/ethernet/amazon/ena/ena_netdev.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/amazon/ena/ena_netdev.c b/drivers/net/ethernet/amazon/ena/ena_netdev.c
index 53080fd143dc..07444aead3fd 100644
--- a/drivers/net/ethernet/amazon/ena/ena_netdev.c
+++ b/drivers/net/ethernet/amazon/ena/ena_netdev.c
@@ -1400,10 +1400,9 @@ static struct sk_buff *ena_alloc_skb(struct ena_ring *rx_ring, void *first_frag)
 	struct sk_buff *skb;
 
 	if (!first_frag)
-		skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
-						rx_ring->rx_copybreak);
+		skb = napi_alloc_skb(rx_ring->napi, rx_ring->rx_copybreak);
 	else
-		skb = build_skb(first_frag, ENA_PAGE_SIZE);
+		skb = napi_build_skb(first_frag, ENA_PAGE_SIZE);
 
 	if (unlikely(!skb)) {
 		ena_increase_stat(&rx_ring->rx_stats.skb_alloc_fail, 1,
-- 
2.33.1

Re: [PATCH v2] net: ena: Do not waste napi skb cache
Posted by Shay Agroskin 4 years, 5 months ago
Hyeonggon Yoo <42.hyeyoo@gmail.com> writes:

> CAUTION: This email originated from outside of the 
> organization. Do not click links or open attachments unless you 
> can confirm the sender and know the content is safe.
>
>
>
> By profiling, discovered that ena device driver allocates skb by
> build_skb() and frees by napi_skb_cache_put(). Because the 
> driver
> does not use napi skb cache in allocation path, napi skb cache 
> is
> periodically filled and flushed. This is waste of napi skb 
> cache.
>
> As ena_alloc_skb() is called only in napi, Use napi_build_skb()
> and napi_alloc_skb() when allocating skb.
>
> This patch was tested on aws a1.metal instance.
>
> [ jwiedmann.dev@gmail.com: Use napi_alloc_skb() instead of
>   netdev_alloc_skb_ip_align() to keep things consistent. ]
>
> Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>
> ---
>  drivers/net/ethernet/amazon/ena/ena_netdev.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
>

Thank you for this work

Acked-by: Shay Agroskin <shayagr@amazon.com>
Re: [PATCH v2] net: ena: Do not waste napi skb cache
Posted by Jakub Kicinski 4 years, 5 months ago
On Wed, 26 Jan 2022 13:16:41 +0000 Hyeonggon Yoo wrote:
> By profiling, discovered that ena device driver allocates skb by
> build_skb() and frees by napi_skb_cache_put(). Because the driver
> does not use napi skb cache in allocation path, napi skb cache is
> periodically filled and flushed. This is waste of napi skb cache.
> 
> As ena_alloc_skb() is called only in napi, Use napi_build_skb()
> and napi_alloc_skb() when allocating skb.
> 
> This patch was tested on aws a1.metal instance.
> 
> [ jwiedmann.dev@gmail.com: Use napi_alloc_skb() instead of
>   netdev_alloc_skb_ip_align() to keep things consistent. ]
> 
> Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>

It seems you missed CCing netdev@, you'll need to repost.
You can keep the ack from Shay.