From: Linus Walleij <linusw@kernel.org>
To: "Hans Ulli Kroll" <ulli.kroll@googlemail.com>,
"Andrew Lunn" <andrew+netdev@lunn.ch>,
"David S. Miller" <davem@davemloft.net>,
"Jakub Kicinski" <kuba@kernel.org>,
"Paolo Abeni" <pabeni@redhat.com>,
"Michał Mirosław" <mirq-linux@rere.qmqm.pl>,
"Myeonghun Pak" <mhun512@gmail.com>,
"Eric Dumazet" <edumazet@kernel.org>
Cc: netdev@vger.kernel.org, Linus Walleij <linusw@kernel.org>
Subject: [PATCH net-next v5 09/14] net: ethernet: cortina: Recycle claimed free queue pages
Date: Thu, 15 Oct 2026 17:24:09 +0200 [thread overview]
Message-ID: <20261015-gemini-ethernet-fixes-3-v5-9-b5c9e2f7b8a3@kernel.org> (raw)
In-Reply-To: <20261015-gemini-ethernet-fixes-3-v5-0-b5c9e2f7b8a3@kernel.org>
The preceding "Preserve in-flight free queue pages" fix releases each
mapping after its last descriptor is claimed. Refilling therefore
allocates and maps a fresh page at line rate even after the network stack
has released all references to an old page.
Keep the free queue base reference and streaming mapping after the
fragments are claimed. Track fully claimed slots in a separate bitmap and
inspect one at a time during refill. Recycle a page once its reference
count returns to one. If the stack still owns fragments, unmap the page
and relinquish the base reference before allocating its replacement.
This prevents retained pages from exhausting the finite metadata table
and bounds the IRQ-disabled search to a bitmap lookup and one reference
count check. It retains the existing free queue allocator without
introducing a page pool.
Recheck the recycle bitmap on each refill iteration and again after mapping
a candidate page. If another RX path retires a page concurrently, release
the candidate and return to the recycler before consuming another metadata
slot. Retry recycling immediately when a retained page is discarded so
other pending entries are examined before allocating a replacement. This
avoids stopping refill while recyclable slots are pending.
Assisted-by: LLM
Signed-off-by: Linus Walleij <linusw@kernel.org>
---
drivers/net/ethernet/cortina/gemini.c | 154 +++++++++++++++++++++++++++++-----
1 file changed, 131 insertions(+), 23 deletions(-)
diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
index 1d71c5066457..e42eeefd4d19 100644
--- a/drivers/net/ethernet/cortina/gemini.c
+++ b/drivers/net/ethernet/cortina/gemini.c
@@ -174,7 +174,9 @@ struct gemini_ethernet {
struct xarray freeq_mappings;
unsigned int num_freeq_pages;
unsigned long *freeq_page_bitmap;
+ unsigned long *freeq_recycle_bitmap;
unsigned int freeq_page_cursor;
+ unsigned int freeq_recycle_pending;
spinlock_t freeq_lock; /* Locks queue from reentrance */
};
@@ -764,6 +766,34 @@ static int geth_freeq_alloc_slot(struct gemini_ethernet *geth)
return slot;
}
+static int geth_freeq_recycle_slot(struct gemini_ethernet *geth)
+{
+ unsigned int slot = geth->freeq_page_cursor;
+
+ lockdep_assert_held(&geth->freeq_lock);
+
+ if (!geth->freeq_recycle_pending)
+ return -ENOSPC;
+
+ slot = find_next_bit(geth->freeq_recycle_bitmap,
+ geth->num_freeq_pages,
+ geth->freeq_page_cursor);
+ if (slot == geth->num_freeq_pages) {
+ slot = find_first_bit(geth->freeq_recycle_bitmap,
+ geth->freeq_page_cursor);
+ if (slot == geth->freeq_page_cursor)
+ return -ENOSPC;
+ }
+
+ __clear_bit(slot, geth->freeq_recycle_bitmap);
+ geth->freeq_page_cursor = slot + 1;
+ if (geth->freeq_page_cursor == geth->num_freeq_pages)
+ geth->freeq_page_cursor = 0;
+ geth->freeq_recycle_pending--;
+
+ return slot;
+}
+
static struct page *geth_freeq_claim(struct gemini_ethernet *geth,
dma_addr_t mapping,
unsigned int *page_offs)
@@ -773,8 +803,8 @@ static struct page *geth_freeq_claim(struct gemini_ethernet *geth,
unsigned long index;
unsigned long flags;
dma_addr_t page_mapping;
- unsigned int slot;
struct page *page;
+ unsigned int slot;
bool valid;
index = geth_freeq_mapping_index(geth, mapping);
@@ -798,13 +828,8 @@ static struct page *geth_freeq_claim(struct gemini_ethernet *geth,
xa_erase(&geth->freeq_mappings, index);
if (!--gpage->fragments) {
slot = gpage - geth->freeq_pages;
- dma_unmap_single_attrs(geth->dev, page_mapping, PAGE_SIZE,
- DMA_FROM_DEVICE,
- DMA_ATTR_SKIP_CPU_SYNC);
- gpage->page = NULL;
- gpage->mapping = 0;
- __clear_bit(slot, geth->freeq_page_bitmap);
- put_page(page);
+ __set_bit(slot, geth->freeq_recycle_bitmap);
+ geth->freeq_recycle_pending++;
}
*page_offs = mapping - page_mapping;
@@ -898,25 +923,21 @@ static int geth_freeq_map_page(struct gemini_ethernet *geth,
return 0;
}
-static int geth_freeq_add_page(struct gemini_ethernet *geth, unsigned int pn,
- struct page *page, dma_addr_t page_mapping)
+static int geth_freeq_post_page(struct gemini_ethernet *geth, unsigned int pn,
+ struct gmac_queue_page *gpage, bool recycle)
{
struct gmac_rxdesc *freeq_entry;
- struct gmac_queue_page *gpage;
unsigned int fpp_order;
unsigned int fragments;
unsigned int frag_len;
+ dma_addr_t page_mapping = gpage->mapping;
dma_addr_t mapping;
+ struct page *page = gpage->page;
int ret;
- int slot;
int i;
lockdep_assert_held(&geth->freeq_lock);
- slot = geth_freeq_alloc_slot(geth);
- if (slot < 0)
- return slot;
-
/* The assign the page mapping (physical address) to the buffer address
* in the hardware queue. PAGE_SHIFT on ARM is 12 (1 page is 4096 bytes,
* 4k), and the default RX frag order is 11 (fragments are up 20 2048
@@ -927,9 +948,6 @@ static int geth_freeq_add_page(struct gemini_ethernet *geth, unsigned int pn,
fpp_order = PAGE_SHIFT - geth->freeq_frag_order;
fragments = 1 << fpp_order;
- gpage = &geth->freeq_pages[slot];
- gpage->page = page;
- gpage->mapping = page_mapping;
for (i = 0; i < fragments; i++) {
mapping = page_mapping + i * frag_len;
ret = xa_insert(&geth->freeq_mappings,
@@ -938,11 +956,14 @@ static int geth_freeq_add_page(struct gemini_ethernet *geth, unsigned int pn,
if (ret)
goto err_mappings;
}
+ if (recycle)
+ dma_sync_single_for_device(geth->dev, page_mapping, PAGE_SIZE,
+ DMA_FROM_DEVICE);
gpage->fragments = fragments;
page_ref_add(page, fragments);
freeq_entry = geth->freeq_ring + (pn << fpp_order);
- dev_dbg(geth->dev, "allocate page %d fragment length %d fragments per page %d, freeq entry %p\n",
+ dev_dbg(geth->dev, "post page %d fragment length %d fragments per page %d, freeq entry %p\n",
pn, frag_len, (1 << fpp_order), freeq_entry);
mapping = page_mapping;
for (i = (1 << fpp_order); i > 0; i--) {
@@ -961,9 +982,68 @@ static int geth_freeq_add_page(struct gemini_ethernet *geth, unsigned int pn,
xa_erase(&geth->freeq_mappings,
geth_freeq_mapping_index(geth, mapping));
}
- gpage->page = NULL;
- gpage->mapping = 0;
- __clear_bit(slot, geth->freeq_page_bitmap);
+ return ret;
+}
+
+static int geth_freeq_add_page(struct gemini_ethernet *geth, unsigned int pn,
+ struct page *page, dma_addr_t page_mapping)
+{
+ struct gmac_queue_page *gpage;
+ int ret;
+ int slot;
+
+ lockdep_assert_held(&geth->freeq_lock);
+
+ slot = geth_freeq_alloc_slot(geth);
+ if (slot < 0)
+ return slot;
+
+ gpage = &geth->freeq_pages[slot];
+ gpage->page = page;
+ gpage->mapping = page_mapping;
+ ret = geth_freeq_post_page(geth, pn, gpage, false);
+ if (ret) {
+ gpage->page = NULL;
+ gpage->mapping = 0;
+ __clear_bit(slot, geth->freeq_page_bitmap);
+ }
+
+ return ret;
+}
+
+static int geth_freeq_recycle_page(struct gemini_ethernet *geth,
+ unsigned int pn)
+{
+ struct gmac_queue_page *gpage;
+ struct page *page;
+ int ret;
+ int slot;
+
+ lockdep_assert_held(&geth->freeq_lock);
+
+ slot = geth_freeq_recycle_slot(geth);
+ if (slot < 0)
+ return slot;
+
+ gpage = &geth->freeq_pages[slot];
+ if (page_ref_count(gpage->page) > 1) {
+ page = gpage->page;
+ dma_unmap_single_attrs(geth->dev, gpage->mapping, PAGE_SIZE,
+ DMA_FROM_DEVICE,
+ DMA_ATTR_SKIP_CPU_SYNC);
+ gpage->page = NULL;
+ gpage->mapping = 0;
+ __clear_bit(slot, geth->freeq_page_bitmap);
+ put_page(page);
+ return -EAGAIN;
+ }
+
+ ret = geth_freeq_post_page(geth, pn, gpage, true);
+ if (ret) {
+ __set_bit(slot, geth->freeq_recycle_bitmap);
+ geth->freeq_recycle_pending++;
+ }
+
return ret;
}
@@ -997,9 +1077,24 @@ static unsigned int geth_fill_freeq(struct gemini_ethernet *geth)
pn = (rw.bits.wptr >> fpp_order) & m_pn;
epn = (rw.bits.rptr >> fpp_order) - 1;
epn &= m_pn;
+ ret = -ENOSPC;
+ if (pn != epn && geth->freeq_recycle_pending) {
+ ret = geth_freeq_recycle_page(geth, pn);
+ if (!ret) {
+ count += 1 << fpp_order;
+ pn++;
+ pn &= m_pn;
+ writew(pn << fpp_order,
+ geth->base + GLOBAL_SWFQ_RWPTR_REG + 2);
+ }
+ }
spin_unlock_irqrestore(&geth->freeq_lock, flags);
if (pn == epn)
break;
+ if (!ret || ret == -EAGAIN)
+ continue;
+ if (ret != -ENOSPC)
+ break;
ret = geth_freeq_map_page(geth, &page, &page_mapping);
if (ret)
@@ -1013,6 +1108,8 @@ static unsigned int geth_fill_freeq(struct gemini_ethernet *geth)
epn &= m_pn;
if (pn == epn) {
ret = -ENOSPC;
+ } else if (geth->freeq_recycle_pending) {
+ ret = -EAGAIN;
} else {
ret = geth_freeq_add_page(geth, pn, page,
page_mapping);
@@ -1031,6 +1128,8 @@ static unsigned int geth_fill_freeq(struct gemini_ethernet *geth)
dma_unmap_single(geth->dev, page_mapping, PAGE_SIZE,
DMA_FROM_DEVICE);
put_page(page);
+ if (ret == -EAGAIN)
+ continue;
break;
}
}
@@ -1075,6 +1174,7 @@ static void geth_freeq_release_pages(struct gemini_ethernet *geth)
put_page(page);
}
xa_destroy(&geth->freeq_mappings);
+ geth->freeq_recycle_pending = 0;
}
static unsigned int
@@ -1121,6 +1221,9 @@ static int geth_setup_freeq(struct gemini_ethernet *geth)
geth->freeq_page_bitmap = bitmap_zalloc(page_slots, GFP_KERNEL);
if (!geth->freeq_page_bitmap)
goto err_freeq_pages;
+ geth->freeq_recycle_bitmap = bitmap_zalloc(page_slots, GFP_KERNEL);
+ if (!geth->freeq_recycle_bitmap)
+ goto err_freeq_page_bitmap;
geth->num_freeq_pages = page_slots;
geth->freeq_page_cursor = 0;
@@ -1148,6 +1251,9 @@ static int geth_setup_freeq(struct gemini_ethernet *geth)
writew(readw(geth->base + GLOBAL_SWFQ_RWPTR_REG),
geth->base + GLOBAL_SWFQ_RWPTR_REG + 2);
geth_freeq_release_pages(geth);
+ bitmap_free(geth->freeq_recycle_bitmap);
+ geth->freeq_recycle_bitmap = NULL;
+err_freeq_page_bitmap:
bitmap_free(geth->freeq_page_bitmap);
geth->freeq_page_bitmap = NULL;
err_freeq_pages:
@@ -1178,6 +1284,8 @@ static void geth_cleanup_freeq(struct gemini_ethernet *geth)
geth_freeq_release_pages(geth);
+ bitmap_free(geth->freeq_recycle_bitmap);
+ geth->freeq_recycle_bitmap = NULL;
bitmap_free(geth->freeq_page_bitmap);
geth->freeq_page_bitmap = NULL;
kfree(geth->freeq_pages);
--
2.56.0
next prev parent reply other threads:[~2026-10-15 15:24 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-15 15:24 [PATCH net-next v5 00/14] net: ethernet: cortina: Fix Gemini RX buffer management Linus Walleij
2026-10-15 15:24 ` [PATCH net-next v5 01/14] net: ethernet: cortina: Keep shared free queue parent-owned Linus Walleij
2026-10-19 17:28 ` netdev-bot+sashiko
2026-10-15 15:24 ` [PATCH net-next v5 02/14] net: ethernet: cortina: Keep PHY-less port bound for shared IRQ Linus Walleij
2026-10-15 15:24 ` [PATCH net-next v5 03/14] net: ethernet: cortina: Drain free queue IRQ before resize Linus Walleij
2026-10-15 15:24 ` [PATCH net-next v5 04/14] net: ethernet: cortina: Normalize free queue pointers on rebuild Linus Walleij
2026-10-19 17:28 ` netdev-bot+sashiko
2026-10-15 15:24 ` [PATCH net-next v5 05/14] net: ethernet: cortina: Correct free queue DMA mappings Linus Walleij
2026-10-19 17:28 ` netdev-bot+sashiko
2026-10-15 15:24 ` [PATCH net-next v5 06/14] net: ethernet: cortina: Index free queue fragments with XArray Linus Walleij
2026-10-15 15:24 ` [PATCH net-next v5 07/14] net: ethernet: cortina: Preserve in-flight free queue pages Linus Walleij
2026-10-19 17:28 ` netdev-bot+sashiko
2026-10-15 15:24 ` [PATCH net-next v5 08/14] net: ethernet: cortina: Rotate free queue page allocation Linus Walleij
2026-10-15 15:24 ` Linus Walleij [this message]
2026-10-19 17:28 ` [PATCH net-next v5 09/14] net: ethernet: cortina: Recycle claimed free queue pages netdev-bot+sashiko
2026-10-15 15:24 ` [PATCH net-next v5 10/14] net: ethernet: cortina: Validate RX fragment lengths Linus Walleij
2026-10-19 17:28 ` netdev-bot+sashiko
2026-10-15 15:24 ` [PATCH net-next v5 11/14] net: ethernet: cortina: Account RX buffer size in truesize Linus Walleij
2026-10-19 17:28 ` netdev-bot+sashiko
2026-10-15 15:24 ` [PATCH net-next v5 12/14] net: ethernet: cortina: Release partial RX frames on stop Linus Walleij
2026-10-19 17:28 ` netdev-bot+sashiko
2026-10-15 15:24 ` [PATCH net-next v5 13/14] net: ethernet: cortina: Scale Gemini RX queues to system memory Linus Walleij
2026-10-15 15:24 ` [PATCH net-next v5 14/14] net: ethernet: cortina: Scope RX statistics updates Linus Walleij
2026-10-19 17:28 ` netdev-bot+sashiko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261015-gemini-ethernet-fixes-3-v5-9-b5c9e2f7b8a3@kernel.org \
--to=linusw@kernel.org \
--cc=andrew+netdev@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=kuba@kernel.org \
--cc=mhun512@gmail.com \
--cc=mirq-linux@rere.qmqm.pl \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=ulli.kroll@googlemail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox