Skip to contentSkip to conversation

Introduce ENA local page cache

RFC PATCH New Shay Agroskin 1 participant 3 messages Mar 9, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover Introduce ENA local page cache

High incoming pps rate leads to frequent memory allocations by the napi routine to refill the pages of the incoming packets.

On several new instances in AWS fleet, with high pps rates, these frequent allocations create a contention between the different napi routines.
The contention happens because the routines end up accessing the buddy allocator which is a shared resource and requires lock-based synchronization (also, when freeing a page the same lock is held). In our tests we observed that that this contention causes the CPUs that serve the RX queues to reach 100% and damage network performance. While this contention can be relieved by making sure that pages are allocated and freed on the same core, which would allow the driver to take advantage of PCP, this solution is not always available or easy to maintain.

This patchset implements a page cache local to each RX queue. When the napi routine allocates a page, it first checks whether the cache has a previously allocated page that isn't used. If so, this page is fetched instead of allocating a new one. Otherwise, if the cache is out of free pages, a page is allocated using normal allocation path (PCP or buddy allocator) and returned to the caller.
A page that is allocated outside the cache, is afterwards cached, up to cache's maximum size (set to 2048 pages in this patchset).

The pages' availability is tracked by checking their refcount. A cached page has a refcount of 2 when it is passed to the napi routine as an RX buffer. When a refcount of a page reaches 1, the cache assumes that it is free to be re-used.

To avoid traversing all pages in the cache when looking for an available page, we only check the availability of the first page fetched for the RX queue that wasn't returned to the cache yet (i.e. has a refcount of more than 1).

For example, for a cache of size 8 from which pages at indices 0-7 were fetched (and placed in the RX SQ), the next time napi would try to fetch a page from the cache, the cache would check the availability of the page at index 0, and if it is available, this page would be fetched for the napi. The next time napi would try to fetch a page, the cache entry at index 1 would be checked, and so on.

Memory consumption:

In its maximum occupancy the cache would hold 2048 pages per each queue. Providing an interface with 32 queues, 32 * 2048 * 4K = 64MB are being used by the driver for its RX queues.

To avoid choking the system, this feature is only enabled for instances with more than 16 queues which in AWS come with several tens Gigs of RAM. Moreover, the feature can be turned off completely using ethtool.

Having said that, the memory cost of having RX queues with 2K entries would be the same as with 1K entries queue + LPC in worst case, while the latter allocates the memory only in case the traffic rate is higher than the rate of the pages being freed.

Performance results:

4 c5n.18xlarge instances sending iperf TCP traffic to a p4d.24xlarge instance. Packet size: 1500 bytes

c5n.18xlarge specs:
Intel(R) Xeon(R) Platinum 8124M CPU @ 3.00GHz
with 72 cores. 32 queue pairs.

p4d.24xlarge specs:
Intel(R) Xeon(R) Platinum 8275CL CPU @ 3.00GHz with 96 cores. 4 * 32 = 128 (4 interfaces) queue pairs.

|                     | before | after |
|                     +        +       |
| bandwidth (Gbps)    | 260    | 330   |
| CPU utilization (%) | 100    | 56    |

Shay Agroskin (3):
net: ena: implement local page cache (LPC) system
net: ena: update README file with a description about LPC
net: ena: support ethtool priv-flags and LPC state change

 .../device_drivers/ethernet/amazon/ena.rst    |  28 ++
 drivers/net/ethernet/amazon/ena/ena_ethtool.c |  56 ++-
 drivers/net/ethernet/amazon/ena/ena_netdev.c  | 369 +++++++++++++++++-
 drivers/net/ethernet/amazon/ena/ena_netdev.h  |  32 ++
 4 files changed, 458 insertions(+), 27 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

RFC PATCH 1/3 net: ena: implement local page cache (LPC) system

The page cache holds pages we allocated in the past during napi cycle, and tracks their availability status using page ref count.

The cache can hold up to 2048 pages. Upon allocating a page, we check whether the next entry in the cache contains an unused page, and if so fetch it. If the next page is already used by another entity or if it belongs to a different NUMA core than the napi routine, we allocate a page in the regular way (page from a different NUMA core is replaced by the newly allocated page).

This system can help us reduce the contention between different cores when allocating page since every cache is unique to a queue.

This patch adds the following ethtool counters:

- lpc_warm_up: If the next page in the cache isn't free, and the cache wasn't allocated its maximum possible pages, allocate a new page and store it in the cache. This counter increases everytime it happens. Its maximum value can be N * 'current queue size'.

- lpc_full: The next entry in the cache contains a page that is still used. In such case a page is allocated in the regular way (i.e. dev_alloc())

- lpc_wrong_numa: The next entry in the cache contains a page in a different NUMA node than the napi routine which allocates the page. In this case increase the counter and replace current entry with a page from the same NUMA node.

Note that in all three cases a page should be returned to the caller of the page cache function, and the page would be either from the cache, or from the Linux memory system.
In case the system is out-of-memory the cache returns NULL. This scenario doesn't break the cache's correctness.

The page cache is disabled when having less than 16 queues or when XDP is used.

---
 drivers/net/ethernet/amazon/ena/ena_ethtool.c |   3 +
 drivers/net/ethernet/amazon/ena/ena_netdev.c  | 337 ++++++++++++++++--
 drivers/net/ethernet/amazon/ena/ena_netdev.h  |  30 ++
 3 files changed, 350 insertions(+), 20 deletions(-)
drivers/net/ethernet/amazon/ena/ena_ethtool.c +3 -0
index d6cc7aa612b7..fe16b3d5bd73 100644
@@ -96,6 +96,9 @@ static const struct ena_stats ena_stats_rx_strings[] = {
 	ENA_STAT_RX_ENTRY(xdp_tx),
 	ENA_STAT_RX_ENTRY(xdp_invalid),
 	ENA_STAT_RX_ENTRY(xdp_redirect),
+	ENA_STAT_RX_ENTRY(lpc_warm_up),
+	ENA_STAT_RX_ENTRY(lpc_full),
+	ENA_STAT_RX_ENTRY(lpc_wrong_numa),
 };
 
 static const struct ena_stats ena_stats_ena_com_strings[] = {
drivers/net/ethernet/amazon/ena/ena_netdev.c +317 -20

432 lines not on this page · raw message

drivers/net/ethernet/amazon/ena/ena_netdev.h +30 -0
index 74af15d62ee1..242c9ce4a782 100644
@@ -194,6 +194,7 @@ struct ena_rx_buffer {
 	struct page *page;
 	u32 page_offset;
 	struct ena_com_buf ena_buf;
+	bool is_lpc_page;
 } ____cacheline_aligned;
 
 struct ena_stats_tx {
@@ -234,8 +235,33 @@ struct ena_stats_rx {
 	u64 xdp_tx;
 	u64 xdp_invalid;
 	u64 xdp_redirect;
+	u64 lpc_warm_up;
+	u64 lpc_full;
+	u64 lpc_wrong_numa;
 };
 
+/* LPC definitions */
+#define ENA_LPC_DEFAULT_MULTIPLIER 2
+#define ENA_LPC_MULTIPLIER_UNIT 1024
+#define ENA_LPC_MIN_NUM_OF_CHANNELS 16
+
+/* Store DMA address along with the page */
+struct ena_page {
+	struct page *page;
+	dma_addr_t dma_addr;
+};
+
+struct ena_page_cache {
+	/* How many pages are produced */
+	u32 head;
+	/* How many of the entries were initialized */
+	u32 current_size;
+	/* Maximum number of pages the cache can hold */
+	u32 max_size;
+
+	struct ena_page cache[0];
+} ____cacheline_aligned;
+
 struct ena_ring {
 	/* Holds the empty requests for TX/RX
 	 * out of order completions
@@ -252,6 +278,7 @@ struct ena_ring {
 	struct pci_dev *pdev;
 	struct napi_struct *napi;
 	struct net_device *netdev;
+	struct ena_page_cache *page_cache;
 	struct ena_com_dev *ena_dev;
 	struct ena_adapter *adapter;
 	struct ena_com_io_cq *ena_com_io_cq;
@@ -333,6 +360,9 @@ struct ena_adapter {
 	u32 num_io_queues;
 	u32 max_num_io_queues;
 
+	/* Local page cache size */
+	u32 lpc_size;
+
 	int msix_vecs;
 
 	u32 missing_tx_completion_threshold;
Signature · 1 line
2.25.1
#2 lore raw

RFC PATCH 2/3 net: ena: update README file with a description about LPC

In reply to Shay Agroskin

Add a description for local page cache system to the ENA driver readme file.

---
 .../device_drivers/ethernet/amazon/ena.rst    | 25 +++++++++++++++++++
 1 file changed, 25 insertions(+)
Documentation/networking/device_drivers/ethernet/amazon/ena.rst +25 -0
index 3561a8a29fd2..d3423a2f472c 100644
@@ -222,6 +222,31 @@ If the frame length is larger than rx_copybreak, napi_get_frags()
 is used, otherwise netdev_alloc_skb_ip_align() is used, the buffer
 content is copied (by CPU) to the SKB, and the buffer is recycled.
 
+Local Page Cache (LPC)
+======================
+ENA Linux driver allows to reduce lock contention and improve CPU usage by
+allocating RX buffers from a page cache rather than from Linux memory system
+(PCP or buddy allocator). The cache is created and binded per RX queue, and
+pages allocated for the queue are stored in the cache (up to cache maximum
+size).
+
+When enabled, LPC cache size is ENA_LPC_DEFAULT_MULTIPLIER * 1024 (2048 by
+default) pages.
+
+The cache usage for each queue can be monitored using ``ethtool -S`` counters. Where:
+
+- *rx_queue#_lpc_warm_up* - number of pages that were allocated and stored in
+  the cache
+- *rx_queue#_lpc_full* - number of pages that were allocated without using the
+  cache because it didn't have free pages
+- *rx_queue#_lpc_wrong_numa* -  number of pages from the cache that belong to a
+  different NUMA node than the CPU which runs the NAPI routine. In this case,
+  the driver would try to allocate a new page from the same NUMA node instead
+
+LPC is disabled when using XDP or when using less than 16 queue pairs. Note that
+cache usage might increase the memory footprint of the driver (depending on the
+traffic).
+
 Statistics
 ==========
 The user can obtain ENA device and driver statistics using ethtool.
Signature · 1 line
2.25.1