net: ibm: emac: use page_pool for RX buffers
Subsystems (2)
PATCH 1/1 net: ibm: emac: use page_pool for RX buffers
The RX ring held skbs that were allocated and DMA mapped when a slot was refilled. By the time a frame arrived in that slot the skb was cold, and every refill paid for a full skb allocation and a dma_map_single().
Take the RX buffers from a page_pool instead and build the skb with napi_build_skb() once a frame has arrived. Frames spanning several BDs get the following buffers attached as page fragments instead of being copied into one large skb. The BD size is capped so that a buffer always fits into a page. The driver now syncs the received data for the CPU before reading it.
The pool recycles directly into the MAL NAPI instance, so it is destroyed while that instance is disabled.
Routing a single TCP stream through the qca8k switch of a Meraki MX60 (iperf3 median of 3):
LAN->WAN WAN->LAN (Mbit/s) before 588 508 after 614 528
--- drivers/net/ethernet/ibm/emac/Kconfig | 1 + drivers/net/ethernet/ibm/emac/core.c | 234 ++++++++++++++++---------- drivers/net/ethernet/ibm/emac/core.h | 39 +++-- drivers/net/ethernet/ibm/emac/mal.c | 18 ++ drivers/net/ethernet/ibm/emac/mal.h | 2 + 5 files changed, 191 insertions(+), 103 deletions(-)
drivers/net/ethernet/ibm/emac/Kconfig +1 -0
@@ -3,6 +3,7 @@ config IBM_EMAC tristate "IBM EMAC Ethernet support" depends on PPC_DCR select CRC32 + select PAGE_POOL select PHYLIB help This driver supports the IBM EMAC family of Ethernet controllers
drivers/net/ethernet/ibm/emac/core.c +146 -88
378 lines not on this page · raw message
drivers/net/ethernet/ibm/emac/core.h +24 -15
@@ -53,24 +53,31 @@/* Maximum L2 header length (VLAN tagged, no FCS) */ #define EMAC_MTU_OVERHEAD (6 * 2 + 2 + 4) -/* RX BD size for the given MTU */ -static inline int emac_rx_size (int mtu) -{ -return mal_rx_size (max (mtu,ETH_DATA_LEN ) +EMAC_MTU_OVERHEAD ); -} +/* Headroom in front of the received frame */ +#define EMAC_RX_HEADROOM (NET_SKB_PAD + NET_IP_ALIGN) + +/* Largest RX BD size whose buffer still fits into one page */ +#define EMAC_RX_MAX_SIZE \ + ALIGN_DOWN(SKB_WITH_OVERHEAD(PAGE_SIZE) - EMAC_RX_HEADROOM, 16) -/* Size of RX skb for the given MTU */ -static inline int emac_rx_skb_size (int mtu) +/* RX BD size for the given MTU, larger frames span several BDs */ +static inline int emac_rx_size (int mtu) { -int size =max (mtu +EMAC_MTU_OVERHEAD ,emac_rx_size (mtu)); +int size =mal_rx_size (max (mtu,ETH_DATA_LEN ) +EMAC_MTU_OVERHEAD ); -return SKB_DATA_ALIGN (size +NET_IP_ALIGN ) +NET_SKB_PAD ; +return min_t (int, size,EMAC_RX_MAX_SIZE ); } -/* RX DMA sync size */ -static inline int emac_rx_sync_size (int mtu) +/* Size of an RX buffer for the given MTU, including the headroom and +* the skb_shared_info. Buffers above half a page take a whole page. +*/ +static inline int emac_rx_buf_size (int mtu) { -return SKB_DATA_ALIGN (emac_rx_size (mtu) +NET_IP_ALIGN ); +int size =SKB_HEAD_ALIGN (NET_SKB_PAD + +SKB_DATA_ALIGN (emac_rx_size (mtu) + +NET_IP_ALIGN )); + +return size >PAGE_SIZE /2 ?PAGE_SIZE : size; }/* Driver statistcs is split into two parts to make it more cache friendly:
@@ -238,11 +245,13 @@ struct emac_instance {struct mal_descriptor *rx_desc;int rx_slot;struct sk_buff *rx_sg_skb;/* 1 */ -int rx_skb_size; -int rx_sync_size; +int rx_buf_size; +struct page_pool *rx_pool;struct sk_buff *tx_skb[NUM_TX_BUFF ]; -struct sk_buff *rx_skb[NUM_RX_BUFF ]; +void *rx_buf[NUM_RX_BUFF ]; +/* DMA address of rx_buf + NET_SKB_PAD, cache line aligned */ +dma_addr_t rx_dma[NUM_RX_BUFF ];/* Stats */
drivers/net/ethernet/ibm/emac/mal.c +18 -0
@@ -25,6 +25,7 @@#include <linux/of.h> #include <linux/of_irq.h> #include <linux/platform_device.h> +#include <net/page_pool/types.h> #include "core.h" #include <asm/dcr-regs.h>
@@ -86,6 +87,23 @@ void mal_unregister_commac(struct mal_instance *mal,netdev_unlock (mal->napi.dev); } +/* A page pool that recycles directly into the MAL NAPI instance can only be +* destroyed while that instance is disabled. +*/ +void mal_destroy_page_pool (struct mal_instance *mal,struct page_pool *pool) +{ +bool enabled; + +netdev_lock (mal->napi.dev); + enabled = !list_empty (&mal->list); +if (enabled) +napi_disable_locked (&mal->napi); +page_pool_destroy (pool); +if (enabled) +napi_enable_locked (&mal->napi); +netdev_unlock (mal->napi.dev); +} +int mal_set_rcbs (struct mal_instance *mal,int channel,unsigned long size) { BUG_ON(channel < 0 || channel >= mal->num_rx_chans ||
drivers/net/ethernet/ibm/emac/mal.h +2 -0
@@ -276,6 +276,8 @@ void mal_disable_rx_channel(struct mal_instance *mal, int channel);void mal_poll_disable (struct mal_instance *mal,struct mal_commac *commac);void mal_poll_enable (struct mal_instance *mal,struct mal_commac *commac); +void mal_destroy_page_pool (struct mal_instance *mal,struct page_pool *pool); +/* Add/remove EMAC to/from MAL polling list */ void mal_poll_add (struct mal_instance *mal,struct mal_commac *commac);void mal_poll_del (struct mal_instance *mal,struct mal_commac *commac);
Signature · 1 line
2.56.0
Simon Horman
reviewerQuoted 22 lines · Rosen Penev
The RX ring held skbs that were allocated and DMA mapped when a slot was refilled. By the time a frame arrived in that slot the skb was cold, and every refill paid for a full skb allocation and a
dma_map_single().Take the RX buffers from a
page_poolinstead and build the skb withnapi_build_skb()once a frame has arrived. Frames spanning several BDs get the following buffers attached as page fragments instead of being copied into one large skb. The BD size is capped so that a buffer always fits into a page. The driver now syncs the received data for the CPU before reading it.The pool recycles directly into the MAL NAPI instance, so it is destroyed while that instance is disabled.
Routing a single TCP stream through the qca8k switch of a Meraki MX60 (iperf3 median of 3):
LAN->WAN WAN->LAN (Mbit/s) before 588 508 after 614 528Signed-off-by Rosen Penev <[email protected]>
...
drivers/net/ethernet/ibm/emac/core.c static void emac_poll_tx(void *param) · line 1677
Quoted 12 lines
drivers/net/ethernet/ibm/emac/core.c +8
-0@@ -1661,17 +1667,23 @@ static void emac_poll_tx(void *param)netif_tx_unlock_bh (dev->ndev); } +static inline void emac_rx_sync_for_cpu (struct emac_instance *dev,int slot, +int len) +{ +dma_sync_single_for_cpu (&dev->ofdev->dev, dev->rx_dma[slot], +SKB_DATA_ALIGN (len +NET_IP_ALIGN ), +DMA_FROM_DEVICE ); +} +
Hi Rosen,
I realise this is pattern already pervasive in this file. But please do not add new instances of the inline keyword in C files unless there is a demonstrable reason to do so - usually performance.
Instead, please let the compiler do it's thing.
Otherwise this patch looks good to me.
Signature · 3 lines
pw-bot: changes-requested ...