Thread View: linux.kernel.netdev
18 messages
18 total messages
Started by Linus Walleij
Thu, 12 Nov 2026 13:30
[PATCH net-next v8 00/19] net: ethernet: cortina: Fix Gemini RX/TX buffer management
Author: Linus Walleij
Date: Thu, 12 Nov 2026 13:30
Date: Thu, 12 Nov 2026 13:30
170 lines
17002 bytes
17002 bytes
The Gemini ethernet uses a custom software free queue shared by both
ports. Its page metadata, DMA mappings and fragment references are not
managed consistently as pages move through the free queue and RX queues.
Correct the DMA address bookkeeping and synchronization.
Track exact fragment mappings with an XArray and keep
in-flight page metadata in bitmap-allocated slots until all hardware
references have been claimed.
Serialize queue resize against the refill interrupt, harden fragment
validation and teardown, then scale the default queue sizes for systems
with 32 or 64 MiB of RAM.
This series wasn't so complex to begin with but Sashiko and the other
LLMs just find more and more corner cases. It's for the better I
guess.
Before these patches the network driver would crash under strong load
but it does not happen any more after. Tested on the D-Link DIR-685
playing back media and transfering new media using ksmbd while
issuing ping storms.
Target the series for net-next, Sashiko keeps insisting to make some
patches Fixes: tagged, and yeah maybe it's true but OpenWrt is the
only user and I will surely backport all patches to OpenWrt anyway.
Signed-off-by: Linus Walleij <linusw@kernel.org>
---
Changes in v8:
- Protect TX fragment statistics updates during process-context teardown.
- Rebuild idle shared free queues on reopen to reclaim unclaimed fragments.
- Separate RX buffer capacity from retained-pointer descriptor ring sizing.
- Preserve PHY probe deferral and clean up fixed links on probe failure or removal.
- Link to v7: https://patch.msgid.link/20261110-gemini-ethernet-fixes-3-v7-0-9c32a1a46aec@kernel.org
Changes in v7:
- Bound free queue refill work and release surplus metadata on downsizing.
- Serialize TX completion statistics with the IRQ-side writers.
- Fix ethtool statistics retry bounds and counter synchronization.
- Reject pfmemalloc pages when recycling RX buffers.
- Document XArray dependency for lifetime and recycling backports.
- Reject TX rings smaller than retained hardware pointers, so now the
series has also started to fix things on the TX path.
- Document the retained SWFQ pointer shrink and allocation limits.
- Keep ports IRQ-only after PHY setup or netdev registration failure.
- Keep parent-held port clocks enabled through IRQ draining and queue teardown.
- Link to v6: https://patch.msgid.link/20261103-gemini-ethernet-fixes-3-v6-0-c4b5ff8a65d8@kernel.org
Changes in v6:
- Initialize statistics synchronization objects for lockdep.
- Post free queue pages from the exact retained descriptor pointer.
- Document replacement-page lookup failures and tag the DMA mapping fix.
- Initialize and drain the RX coalescing timer around its IRQ lifetime.
- Rebased on v7.4-rc1
- Link to v5: https://patch.msgid.link/20261015-gemini-ethernet-fixes-3-v5-0-b5c9e2f7b8a3@kernel.org
Changes in v5:
- Preserve complete frames terminated by an empty EOF descriptor.
- Recheck concurrently retired pages before consuming fresh metadata slots.
- Fold RX ring metadata sizing and resize rollback into the page lifetime
fix.
- Match free queue teardown unmaps to the saved full-page DMA mapping.
- Preserve the hardware-owned free queue read pointer across rebuilds and
keep replacement rings large enough for its retained position.
- Keep shared initialization parent-owned, quiesce inherited IRQ state
before child handlers register and force first queue setup from ring
state.
- Link to v4: https://patch.msgid.link/20261011-gemini-ethernet-fixes-3-v4-0-4b1631b08f5d@kernel.org
Changes in v4:
- Protect stop-path RX statistics updates against preemption on 32-bit systems.
- Validate final RX lengths against the Ethernet minimum and descriptor
transfer size so malformed chains cannot pass short skbs to GRO.
- Charge the full posted RX fragment size in skb truesize accounting.
- Reject free queue geometries that cannot post one complete page and
document the changed refill and recycling behavior.
- Reset the free queue allocation cursor with its bitmap during teardown.
- Bound recycler IRQ latency and release retained mappings for stack-owned
pages before allocating replacements.
- Rebuild oversized free queues while both ports are down so RX ring
downsizing releases the surplus pages.
- Scope RX statistics writer sections to counter updates for PREEMPT_RT.
- Restrict shared free queue handling to port 1 so IRQ draining covers
every registered handler path.
- Reset and mask free queue pointers when rebuilding the queue.
- Reorder the shared queue ownership fix before the port lifetime fix.
- Publish ports only after setup, serialize their lifetime with RTNL and
suppress independent child unbind while port 1 owns the shared IRQ.
- Move child-unbind suppression and PHY teardown ordering into the
parent-ownership patch.
- Link to v3: https://patch.msgid.link/20261002-gemini-ethernet-fixes-3-v3-0-3e1f25890ae8@kernel.org
Changes in v3:
- Add Fixes tags throughout the series, using the original driver commit
where no more specific offending commit exists, and replace Resolves with
Closes on the lifetime fix. I did not retarget the series to net
because there are dependencies in net-next and we are getting closer
to the merge window.
- Keep PHY-less port 1 bound as the shared free queue IRQ owner and clear
stale parent port pointers.
- Disable the port 1 Linux IRQ throughout free queue resizing so no newly
woken threaded handler can race queue teardown or setup.
- Keep failed queue rebuilds quiescent in the IRQ-drain patch.
- Serialize linear DMA lookups before converting them to an XArray.
- Keep all XArray mutation under the IRQ-safe free queue lock.
- Defer page unmapping until fragment lifetime is tracked, and fold DMA
synchronization into that fix.
- Check free queue space before allocating and mapping a candidate page.
- Rebuild free queue metadata when RX rings grow and roll back rejected
ring changes.
- Recycle released pages with explicit DMA synchronization using the
existing free queue allocator.
- Account inconsistent RX descriptor lengths as drops rather than wire
length errors.
- Count partial RX frames discarded during stop as dropped.
- Drop the cleanup guard conversion because it is not recommended for
network drivers.
- Link to v2: https://patch.msgid.link/20260928-gemini-ethernet-fixes-3-v2-0-758a795d7a78@kernel.org
Changes in v2:
- Replace the page_pool conversion with several smaller fixes using the
existing free queue page allocator.
- Split DMA bookkeeping, XArray lookup, bitmap allocation, cursor rotation,
synchronization, validation and teardown into individual patches.
- Drain the threaded free queue interrupt before resizing the shared queue.
- Allocate and DMA-map refill pages outside the IRQ-disabled free queue
lock.
- Reject and unwind partial initial free queue fills, reset their hardware
pointers and leave the refill interrupt masked so setup can be retried.
- Avoid full-page CPU synchronization after fragments enter the network
stack.
- Bound RX lengths to the posted fragment and account rejected lengths as
receive errors.
- Keep the fixes, and keep the add-on RAM sizing patch.
- Link to v1: https://patch.msgid.link/20260920-gemini-ethernet-fixes-3-v1-0-3a2a50a83d89@kernel.org
---
Linus Walleij (19):
net: ethernet: cortina: Keep shared free queue parent-owned
net: ethernet: cortina: Keep ports bound for shared IRQ
net: ethernet: cortina: Drain free queue IRQ before resize
net: ethernet: cortina: Normalize free queue pointers on rebuild
net: ethernet: cortina: Correct free queue DMA mappings
net: ethernet: cortina: Index free queue fragments with XArray
net: ethernet: cortina: Preserve in-flight free queue pages
net: ethernet: cortina: Rotate free queue page allocation
net: ethernet: cortina: Recycle claimed free queue pages
net: ethernet: cortina: Separate RX buffer capacity from free queue size
net: ethernet: cortina: Validate RX fragment lengths
net: ethernet: cortina: Account RX buffer size in truesize
net: ethernet: cortina: Release partial RX frames on stop
net: ethernet: cortina: Scale Gemini RX queues to system memory
net: ethernet: cortina: Serialize TX completion statistics
net: ethernet: cortina: Scope RX statistics updates
net: ethernet: cortina: Drain RX coalescing timer on stop
net: ethernet: cortina: Reject TX rings smaller than retained pointers
net: ethernet: cortina: Fix ethtool statistics reads
drivers/net/ethernet/cortina/gemini.c | 1009 +++++++++++++++++++++++++--------
drivers/net/ethernet/cortina/gemini.h | 2 +-
2 files changed, 766 insertions(+), 245 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20260919-gemini-ethernet-fixes-3-f0403653f23a
Best regards,
--
Linus Walleij <linusw@kernel.org>
[PATCH net-next v8 05/19] net: ethernet: cortina: Correct free queue DMA mappings
Author: Linus Walleij
Date: Thu, 12 Nov 2026 13:30
Date: Thu, 12 Nov 2026 13:30
287 lines
18077 bytes
18077 bytes
The free queue maps complete pages and splits each mapping between its
fragment descriptors. The mapping variable is advanced while filling the
descriptors and that advanced address is then saved as the page mapping.
Replacement also reads the old address after overwriting the descriptor
with the new mapping, so it unmaps the new buffer while leaving the old
mapping live.
The same reassignment leaves replacement metadata holding the new page
base while gmac_rx() still adds PAGE_SIZE before lookup. Once a page is
replaced under RX load, lookup therefore normally fails with "could not
find mapping" and drops the frame. If the resulting key matches another
page, RX can instead attach that page to the skb and drop the wrong page
reference.
Keep the page DMA base separate from the fragment iterator and remove the
invalid replacement unmap. The replaced mapping cannot be released until
its in-flight fragments have been claimed, which is handled with their
lifetime later in the series. Use the saved DMA base and PAGE_SIZE when
unwinding setup and tearing down the current queue.
Reject mappings that cannot fit in the 32-bit hardware descriptors and
derive fragment offsets from the saved DMA base instead of assuming
page-aligned DMA addresses. Snapshot the page, DMA base and offset under
the free queue lock so refill cannot replace them between lookup and use.
The locked linear scan is temporary: the following "Index free queue
fragments with XArray" patch removes it. Backport this change together
with that patch, "Preserve in-flight free queue pages" and "Recycle
claimed free queue pages", which complete the mapping lifetime and DMA
synchronization fixes.
Fixes: 4d5ae32f5e1e ("net: ethernet: Add a driver for Gemini gigabit ethernet")
Signed-off-by: Linus Walleij <linusw@kernel.org>
---
drivers/net/ethernet/cortina/gemini.c | 112 ++++++++++++++++++----------------
1 file changed, 58 insertions(+), 54 deletions(-)
diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
index 31dd4ddad9ea..b96325f652bb 100644
--- a/drivers/net/ethernet/cortina/gemini.c
+++ b/drivers/net/ethernet/cortina/gemini.c
@@ -759,32 +759,43 @@ static int gmac_setup_rxq(struct net_device *netdev)
return 0;
}
-static struct gmac_queue_page *
-gmac_get_queue_page(struct gemini_ethernet *geth,
- struct gemini_ethernet_port *port,
- dma_addr_t addr)
+static struct page *geth_freeq_lookup(struct gemini_ethernet *geth,
+ dma_addr_t mapping,
+ unsigned int *page_offs)
{
+ unsigned int frag_len = 1 << geth->freeq_frag_order;
struct gmac_queue_page *gpage;
- dma_addr_t mapping;
+ unsigned long flags;
+ dma_addr_t page_mapping;
+ struct page *page = NULL;
int i;
- /* Only look for even pages */
- mapping = addr & PAGE_MASK;
-
+ spin_lock_irqsave(&geth->freeq_lock, flags);
if (!geth->freeq_pages) {
dev_err_ratelimited(geth->dev,
"try to get page with no page list\n");
- return NULL;
+ goto unlock;
}
/* Look up a ring buffer page from virtual mapping */
for (i = 0; i < geth->num_freeq_pages; i++) {
gpage = &geth->freeq_pages[i];
- if (gpage->mapping == mapping)
- return gpage;
+ if (!gpage->page || mapping < gpage->mapping)
+ continue;
+
+ page_mapping = gpage->mapping;
+ if (mapping - page_mapping > PAGE_SIZE - frag_len ||
+ ((mapping - page_mapping) & (frag_len - 1)))
+ continue;
+
+ page = gpage->page;
+ *page_offs = mapping - page_mapping;
+ break;
}
- return NULL;
+unlock:
+ spin_unlock_irqrestore(&geth->freeq_lock, flags);
+ return page;
}
static void gmac_cleanup_rxq(struct net_device *netdev)
@@ -792,11 +803,12 @@ static void gmac_cleanup_rxq(struct net_device *netdev)
struct gemini_ethernet_port *port = netdev_priv(netdev);
struct gemini_ethernet *geth = port->geth;
struct gmac_rxdesc *rxd = port->rxq_ring;
- static struct gmac_queue_page *gpage;
struct nontoe_qhdr __iomem *qhdr;
void __iomem *dma_reg;
void __iomem *ptr_reg;
+ unsigned int page_offs;
dma_addr_t mapping;
+ struct page *page;
union dma_rwptr rw;
unsigned int r, w;
@@ -823,14 +835,13 @@ static void gmac_cleanup_rxq(struct net_device *netdev)
if (!mapping)
continue;
- /* Freeq pointers are one page off */
- gpage = gmac_get_queue_page(geth, port, mapping + PAGE_SIZE);
- if (!gpage) {
+ page = geth_freeq_lookup(geth, mapping, &page_offs);
+ if (!page) {
dev_err(geth->dev, "could not find page\n");
continue;
}
/* Release the RX queue reference to the page */
- put_page(gpage->page);
+ put_page(page);
}
dma_free_coherent(geth->dev, sizeof(*port->rxq_ring) << port->rxq_order,
@@ -844,6 +855,7 @@ static struct page *geth_freeq_alloc_map_page(struct gemini_ethernet *geth,
struct gmac_queue_page *gpage;
unsigned int fpp_order;
unsigned int frag_len;
+ dma_addr_t page_mapping;
dma_addr_t mapping;
struct page *page;
int i;
@@ -853,9 +865,17 @@ static struct page *geth_freeq_alloc_map_page(struct gemini_ethernet *geth,
if (!page)
return NULL;
- mapping = dma_map_single(geth->dev, page_address(page),
- PAGE_SIZE, DMA_FROM_DEVICE);
- if (dma_mapping_error(geth->dev, mapping)) {
+ page_mapping = dma_map_single(geth->dev, page_address(page),
+ PAGE_SIZE, DMA_FROM_DEVICE);
+ if (dma_mapping_error(geth->dev, page_mapping)) {
+ put_page(page);
+ return NULL;
+ }
+ if (page_mapping > U32_MAX - (PAGE_SIZE - 1)) {
+ dev_err_ratelimited(geth->dev,
+ "freeq DMA mapping exceeds 32 bits\n");
+ dma_unmap_single(geth->dev, page_mapping, PAGE_SIZE,
+ DMA_FROM_DEVICE);
put_page(page);
return NULL;
}
@@ -868,31 +888,25 @@ static struct page *geth_freeq_alloc_map_page(struct gemini_ethernet *geth,
*/
frag_len = 1 << geth->freeq_frag_order; /* Usually 2048 */
fpp_order = PAGE_SHIFT - geth->freeq_frag_order;
+ gpage = &geth->freeq_pages[pn];
+ if (gpage->page)
+ put_page(gpage->page);
+
+ /* Then put our new mapping into the page table */
+ gpage->mapping = page_mapping;
+ gpage->page = page;
+
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",
- pn, frag_len, (1 << fpp_order), freeq_entry);
+ pn, frag_len, (1 << fpp_order), freeq_entry);
+ mapping = page_mapping;
for (i = (1 << fpp_order); i > 0; i--) {
freeq_entry->word2.buf_adr = mapping;
freeq_entry++;
mapping += frag_len;
}
-
- /* If the freeq entry already has a page mapped, then unmap it. */
- gpage = &geth->freeq_pages[pn];
- if (gpage->page) {
- mapping = geth->freeq_ring[pn << fpp_order].word2.buf_adr;
- dma_unmap_single(geth->dev, mapping, frag_len, DMA_FROM_DEVICE);
- /* This should be the last reference to the page so it gets
- * released
- */
- put_page(gpage->page);
- }
-
- /* Then put our new mapping into the page table */
- dev_dbg(geth->dev, "page %d, DMA addr: %08x, page %p\n",
- pn, (unsigned int)mapping, page);
- gpage->mapping = mapping;
- gpage->page = page;
+ dev_dbg(geth->dev, "page %d, DMA addr: %pad, page %p\n",
+ pn, &page_mapping, page);
return page;
}
@@ -963,7 +977,6 @@ static unsigned int geth_fill_freeq(struct gemini_ethernet *geth, bool refill)
static int geth_setup_freeq(struct gemini_ethernet *geth)
{
unsigned int fpp_order = PAGE_SHIFT - geth->freeq_frag_order;
- unsigned int frag_len = 1 << geth->freeq_frag_order;
unsigned int len = 1 << geth->freeq_order;
unsigned int pages = len >> fpp_order;
union queue_threshold qt;
@@ -1015,12 +1028,11 @@ static int geth_setup_freeq(struct gemini_ethernet *geth)
err_freeq_alloc:
while (pn > 0) {
struct gmac_queue_page *gpage;
- dma_addr_t mapping;
--pn;
- mapping = geth->freeq_ring[pn << fpp_order].word2.buf_adr;
- dma_unmap_single(geth->dev, mapping, frag_len, DMA_FROM_DEVICE);
gpage = &geth->freeq_pages[pn];
+ dma_unmap_single(geth->dev, gpage->mapping, PAGE_SIZE,
+ DMA_FROM_DEVICE);
put_page(gpage->page);
}
@@ -1042,7 +1054,6 @@ static int geth_setup_freeq(struct gemini_ethernet *geth)
static void geth_cleanup_freeq(struct gemini_ethernet *geth)
{
unsigned int fpp_order = PAGE_SHIFT - geth->freeq_frag_order;
- unsigned int frag_len = 1 << geth->freeq_frag_order;
unsigned int len = 1 << geth->freeq_order;
unsigned int pages = len >> fpp_order;
unsigned int pn;
@@ -1056,12 +1067,10 @@ static void geth_cleanup_freeq(struct gemini_ethernet *geth)
for (pn = 0; pn < pages; pn++) {
struct gmac_queue_page *gpage;
- dma_addr_t mapping;
-
- mapping = geth->freeq_ring[pn << fpp_order].word2.buf_adr;
- dma_unmap_single(geth->dev, mapping, frag_len, DMA_FROM_DEVICE);
gpage = &geth->freeq_pages[pn];
+ dma_unmap_single(geth->dev, gpage->mapping, PAGE_SIZE,
+ DMA_FROM_DEVICE);
while (page_ref_count(gpage->page) > 0)
put_page(gpage->page);
}
@@ -1519,7 +1528,6 @@ static unsigned int gmac_rx(struct net_device *netdev, unsigned int budget,
unsigned int consumed = 0;
unsigned int frame_len, frag_len;
struct gmac_rxdesc *rx = NULL;
- struct gmac_queue_page *gpage;
unsigned int received = 0;
bool dropping = port->rx_dropping;
union gmac_rxdesc_0 word0;
@@ -1556,8 +1564,6 @@ static unsigned int gmac_rx(struct net_device *netdev, unsigned int budget,
frag_len = word0.bits.buffer_size;
frame_len = word1.bits.byte_count;
- page_offs = mapping & ~PAGE_MASK;
-
if (word3.bits32 & SOF_BIT) {
if (skb) {
napi_free_frags(&port->napi);
@@ -1576,14 +1582,12 @@ static unsigned int gmac_rx(struct net_device *netdev, unsigned int budget,
goto err_drop;
}
- /* Freeq pointers are one page off */
- gpage = gmac_get_queue_page(geth, port, mapping + PAGE_SIZE);
- if (!gpage) {
+ page = geth_freeq_lookup(geth, mapping, &page_offs);
+ if (!page) {
dev_err_ratelimited(geth->dev,
"could not find mapping\n");
goto err_drop;
}
- page = gpage->page;
if (word3.bits32 & SOF_BIT) {
skb = gmac_skb_if_good_frame(port, word0, frame_len);
--
2.56.0
[PATCH net-next v8 08/19] net: ethernet: cortina: Rotate free queue page allocation
Author: Linus Walleij
Date: Thu, 12 Nov 2026 13:30
Date: Thu, 12 Nov 2026 13:30
79 lines
10461 bytes
10461 bytes
The free queue metadata bitmap currently searches for a free slot from
zero for every page allocation. Under sustained traffic this repeatedly
scans occupied low-numbered slots.
Remember the position after the most recently allocated slot and wrap the
bitmap search at its end. Reset the cursor when the bitmap is released so
the two remain consistent across failed setup and teardown. This keeps
allocation cost distributed across the metadata table.
Signed-off-by: Linus Walleij <linusw@kernel.org>
---
drivers/net/ethernet/cortina/gemini.c | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
index 4b0c10d732ab..64c8c7e463f2 100644
--- a/drivers/net/ethernet/cortina/gemini.c
+++ b/drivers/net/ethernet/cortina/gemini.c
@@ -175,6 +175,7 @@ struct gemini_ethernet {
struct xarray freeq_mappings;
unsigned int num_freeq_pages;
unsigned long *freeq_page_bitmap;
+ unsigned int freeq_page_cursor;
spinlock_t freeq_lock; /* Locks queue from reentrance */
};
@@ -779,12 +780,20 @@ static int geth_freeq_alloc_slot(struct gemini_ethernet *geth)
lockdep_assert_held(&geth->freeq_lock);
- slot = find_first_zero_bit(geth->freeq_page_bitmap,
- geth->num_freeq_pages);
- if (slot == geth->num_freeq_pages)
- return -ENOSPC;
+ slot = find_next_zero_bit(geth->freeq_page_bitmap,
+ geth->num_freeq_pages,
+ geth->freeq_page_cursor);
+ if (slot == geth->num_freeq_pages) {
+ slot = find_first_zero_bit(geth->freeq_page_bitmap,
+ geth->freeq_page_cursor);
+ if (slot == geth->freeq_page_cursor)
+ return -ENOSPC;
+ }
__set_bit(slot, geth->freeq_page_bitmap);
+ geth->freeq_page_cursor = slot + 1;
+ if (geth->freeq_page_cursor == geth->num_freeq_pages)
+ geth->freeq_page_cursor = 0;
return slot;
}
@@ -1147,6 +1156,7 @@ static int geth_setup_freeq(struct gemini_ethernet *geth)
if (!geth->freeq_page_bitmap)
goto err_freeq_pages;
geth->num_freeq_pages = page_slots;
+ geth->freeq_page_cursor = 0;
/* Match the software pointer to the retained hardware pointer. */
rptr = GET_RPTR(geth->base + GLOBAL_SWFQ_RWPTR_REG);
@@ -1178,6 +1188,7 @@ static int geth_setup_freeq(struct gemini_ethernet *geth)
kfree(geth->freeq_pages);
geth->freeq_pages = NULL;
geth->num_freeq_pages = 0;
+ geth->freeq_page_cursor = 0;
err_freeq:
dma_free_coherent(geth->dev,
sizeof(*geth->freeq_ring) << geth->freeq_order,
@@ -1206,6 +1217,7 @@ static void geth_cleanup_freeq(struct gemini_ethernet *geth)
kfree(geth->freeq_pages);
geth->freeq_pages = NULL;
geth->num_freeq_pages = 0;
+ geth->freeq_page_cursor = 0;
dma_free_coherent(geth->dev,
sizeof(*geth->freeq_ring) << geth->freeq_order,
--
2.56.0
[PATCH net-next v8 09/19] net: ethernet: cortina: Recycle claimed free queue pages
Author: Linus Walleij
Date: Thu, 12 Nov 2026 13:30
Date: Thu, 12 Nov 2026 13:30
315 lines
18164 bytes
18164 bytes
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.
Reject pfmemalloc pages when recycling so emergency-reserve pages return
to the allocator instead of remaining pinned in the free queue.
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.
Fixes: 4d5ae32f5e1e ("net: ethernet: Add a driver for Gemini gigabit ethernet")
Signed-off-by: Linus Walleij <linusw@kernel.org>
---
drivers/net/ethernet/cortina/gemini.c | 153 +++++++++++++++++++++++++++++-----
1 file changed, 131 insertions(+), 22 deletions(-)
diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
index 64c8c7e463f2..aedd8eca3f15 100644
--- a/drivers/net/ethernet/cortina/gemini.c
+++ b/drivers/net/ethernet/cortina/gemini.c
@@ -175,7 +175,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 */
};
@@ -798,6 +800,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)
@@ -807,8 +837,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);
@@ -832,13 +862,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;
@@ -932,26 +957,23 @@ static int geth_freeq_map_page(struct gemini_ethernet *geth,
return 0;
}
-static int geth_freeq_add_page(struct gemini_ethernet *geth, unsigned int wptr,
- struct page *page, dma_addr_t page_mapping)
+static int geth_freeq_post_page(struct gemini_ethernet *geth,
+ unsigned int wptr,
+ struct gmac_queue_page *gpage, bool recycle)
{
struct gmac_rxdesc *freeq_entry;
- struct gmac_queue_page *gpage;
unsigned int ring_mask;
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
@@ -963,9 +985,6 @@ static int geth_freeq_add_page(struct gemini_ethernet *geth, unsigned int wptr,
fragments = 1 << fpp_order;
ring_mask = (1 << geth->freeq_order) - 1;
- 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,
@@ -974,6 +993,9 @@ static int geth_freeq_add_page(struct gemini_ethernet *geth, unsigned int wptr,
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);
@@ -997,9 +1019,70 @@ static int geth_freeq_add_page(struct gemini_ethernet *geth, unsigned int wptr,
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 wptr,
+ 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, wptr, 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 wptr)
+{
+ 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 ||
+ !dev_page_is_reusable(gpage->page)) {
+ 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, wptr, gpage, true);
+ if (ret) {
+ __set_bit(slot, geth->freeq_recycle_bitmap);
+ geth->freeq_recycle_pending++;
+ }
+
return ret;
}
@@ -1034,9 +1117,22 @@ static unsigned int geth_fill_freeq(struct gemini_ethernet *geth)
rptr = rw.bits.rptr & ring_mask;
wptr = rw.bits.wptr & ring_mask;
space = CIRC_SPACE(wptr, rptr, len) >= fragments;
+ ret = -ENOSPC;
+ if (space && geth->freeq_recycle_pending) {
+ ret = geth_freeq_recycle_page(geth, wptr);
+ if (!ret) {
+ count += fragments;
+ wptr = (wptr + fragments) & ring_mask;
+ SET_WPTR(geth->base + GLOBAL_SWFQ_RWPTR_REG, wptr);
+ }
+ }
spin_unlock_irqrestore(&geth->freeq_lock, flags);
if (!space)
break;
+ if (!ret || ret == -EAGAIN)
+ continue;
+ if (ret != -ENOSPC)
+ break;
ret = geth_freeq_map_page(geth, &page, &page_mapping);
if (ret)
@@ -1049,6 +1145,8 @@ static unsigned int geth_fill_freeq(struct gemini_ethernet *geth)
wptr = rw.bits.wptr & ring_mask;
if (CIRC_SPACE(wptr, rptr, len) < fragments) {
ret = -ENOSPC;
+ } else if (geth->freeq_recycle_pending) {
+ ret = -EAGAIN;
} else {
ret = geth_freeq_add_page(geth, wptr, page,
page_mapping);
@@ -1065,6 +1163,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;
}
}
@@ -1109,6 +1209,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
@@ -1155,6 +1256,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;
@@ -1182,6 +1286,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:
@@ -1212,6 +1319,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
[PATCH net-next v8 11/19] net: ethernet: cortina: Validate RX fragment lengths
Author: Linus Walleij
Date: Thu, 12 Nov 2026 13:30
Date: Thu, 12 Nov 2026 13:30
132 lines
12513 bytes
12513 bytes
RX descriptor lengths are used to adjust the page offset and populate skb
fragments without checking that the resulting range remains inside the
posted free queue fragment. A zero-length descriptor is logged but is still
appended.
The backing page is larger than the default 2 KiB DMA fragment, so checking
only the page boundary would allow a malformed descriptor for the first
half of a page to consume data from the sibling fragment. That fragment may
still be owned by the device.
Reject a short initial fragment before applying NET_IP_ALIGN. Validate the
final fragment length derived from the total frame byte count, rejecting
underflow and ranges extending beyond either the DMA fragment or the page.
Drop zero-length non-final descriptors. If an EOF descriptor carries no new
data after earlier descriptors completed the frame, release its unused page
reference and pass the assembled skb to GRO. Check this case before the
fragment limit so an empty EOF can terminate a full fragment array.
Correct the copied buffer_size comment to match the Gemini manual, which
defines it as the transfer buffer size associated with the descriptor.
Require the final frame length to cover an Ethernet frame so a malformed
chain cannot pass a short skb to GRO. Account descriptor inconsistencies as
drops rather than wire length errors.
The issue was found by inspecting the descriptor arithmetic and validated
against the Gemini receive descriptor definitions. The series was tested on
a D-Link DIR-685 with media playback and ksmbd transfers under ping load.
Fixes: 4d5ae32f5e1e ("net: ethernet: Add a driver for Gemini gigabit ethernet")
Signed-off-by: Linus Walleij <linusw@kernel.org>
---
drivers/net/ethernet/cortina/gemini.c | 42 +++++++++++++++++++++++++++--------
drivers/net/ethernet/cortina/gemini.h | 2 +-
2 files changed, 34 insertions(+), 10 deletions(-)
diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
index 72d2c002f8fc..364504c9d441 100644
--- a/drivers/net/ethernet/cortina/gemini.c
+++ b/drivers/net/ethernet/cortina/gemini.c
@@ -1790,6 +1790,7 @@ static unsigned int gmac_rx(struct net_device *netdev, unsigned int budget,
struct gemini_ethernet_port *port = netdev_priv(netdev);
unsigned short m = (1 << port->rxq_order) - 1;
struct gemini_ethernet *geth = port->geth;
+ unsigned int freeq_frag_len = 1 << geth->freeq_frag_order;
void __iomem *ptr_reg = port->rxq_rwptr;
unsigned int frag_nr = port->rx_frag_nr;
struct sk_buff *skb = port->rx_skb;
@@ -1864,6 +1865,9 @@ static unsigned int gmac_rx(struct net_device *netdev, unsigned int budget,
if (!skb)
goto err_drop;
+ if (frag_len < NET_IP_ALIGN)
+ goto err_drop;
+
page_offs += NET_IP_ALIGN;
frag_len -= NET_IP_ALIGN;
frag_nr = 0;
@@ -1872,15 +1876,33 @@ static unsigned int gmac_rx(struct net_device *netdev, unsigned int budget,
goto err_drop;
}
- if (word3.bits32 & EOF_BIT)
+ if (word3.bits32 & EOF_BIT) {
+ if (frame_len < ETH_ZLEN ||
+ frame_len < skb->len)
+ goto err_drop;
frag_len = frame_len - skb->len;
+ }
+
+ if (!frag_len) {
+ if (word3.bits32 & EOF_BIT) {
+ put_page(page);
+ page = NULL;
+ goto complete_frame;
+ }
+
+ if (net_ratelimit())
+ netdev_err(netdev,
+ "Received fragment with len = 0\n");
+ goto err_drop;
+ }
/* append page frag to skb */
if (frag_nr == MAX_SKB_FRAGS)
goto err_drop;
-
- if (frag_len == 0 && net_ratelimit())
- netdev_err(netdev, "Received fragment with len = 0\n");
+ if (frag_len > freeq_frag_len -
+ (page_offs & (freeq_frag_len - 1)) ||
+ frag_len > PAGE_SIZE - page_offs)
+ goto err_drop;
skb_fill_page_desc(skb, frag_nr, page, page_offs, frag_len);
skb->len += frag_len;
@@ -1888,11 +1910,13 @@ static unsigned int gmac_rx(struct net_device *netdev, unsigned int budget,
skb->truesize += frag_len;
frag_nr++;
- if (word3.bits32 & EOF_BIT) {
- napi_gro_frags(&port->napi);
- skb = NULL;
- frag_nr = 0;
- }
+ if (!(word3.bits32 & EOF_BIT))
+ goto next_desc;
+
+complete_frame:
+ napi_gro_frags(&port->napi);
+ skb = NULL;
+ frag_nr = 0;
goto next_desc;
err_drop:
diff --git a/drivers/net/ethernet/cortina/gemini.h b/drivers/net/ethernet/cortina/gemini.h
index d359c633c5de..43006fe44c9f 100644
--- a/drivers/net/ethernet/cortina/gemini.h
+++ b/drivers/net/ethernet/cortina/gemini.h
@@ -519,7 +519,7 @@ struct gmac_txdesc {
union gmac_rxdesc_0 {
unsigned int bits32;
struct bit_8060 {
- /* bit 15:0 number of descriptors used for the current frame */
+ /* bit 15:0 transfer buffer size for this descriptor */
unsigned int buffer_size:16;
/* bit 21:16 number of descriptors used for the current frame */
unsigned int desc_count:6;
--
2.56.0
[PATCH net-next v8 12/19] net: ethernet: cortina: Account RX buffer size in truesize
Author: Linus Walleij
Date: Thu, 12 Nov 2026 13:30
Date: Thu, 12 Nov 2026 13:30
34 lines
8927 bytes
8927 bytes
gmac_rx() charges only the received payload length for each page fragment
attached to an skb. Each fragment pins a complete free queue DMA buffer, so
short packets understate their memory use to GRO and socket accounting.
Charge the full posted fragment size for every attached fragment. The skb
already accounts for its own head separately.
The issue was found by reviewing the RX fragment accounting. The series was
tested on a D-Link DIR-685 with media playback and ksmbd transfers under
ping load.
Fixes: 4d5ae32f5e1e ("net: ethernet: Add a driver for Gemini gigabit ethernet")
Signed-off-by: Linus Walleij <linusw@kernel.org>
---
drivers/net/ethernet/cortina/gemini.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
index 364504c9d441..f31fd0de0059 100644
--- a/drivers/net/ethernet/cortina/gemini.c
+++ b/drivers/net/ethernet/cortina/gemini.c
@@ -1907,7 +1907,7 @@ static unsigned int gmac_rx(struct net_device *netdev, unsigned int budget,
skb_fill_page_desc(skb, frag_nr, page, page_offs, frag_len);
skb->len += frag_len;
skb->data_len += frag_len;
- skb->truesize += frag_len;
+ skb->truesize += freeq_frag_len;
frag_nr++;
if (!(word3.bits32 & EOF_BIT))
--
2.56.0
[PATCH net-next v8 14/19] net: ethernet: cortina: Scale Gemini RX queues to system memory
Author: Linus Walleij
Date: Thu, 12 Nov 2026 13:30
Date: Thu, 12 Nov 2026 13:30
69 lines
9997 bytes
9997 bytes
The default RX queues contain 512 descriptors per port. The shared
software free queue is sized at twice their combined capacity, requiring
4 MiB of page fragments even on Gemini systems with only 32 or 64 MiB of
RAM.
Choose smaller default RX queues on memory-constrained systems. Use 256
descriptors per port and a 2 MiB free queue with at most 64 MiB of RAM,
and 128 descriptors per port with a 1 MiB free queue with at most 32 MiB.
Keep the existing defaults on systems with more memory.
This preserves the existing relationship between RX descriptor capacity
and the free queue. Users can still override the defaults with ethtool.
Signed-off-by: Linus Walleij <linusw@kernel.org>
---
drivers/net/ethernet/cortina/gemini.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
index 0588e573f8da..5a87d88607cd 100644
--- a/drivers/net/ethernet/cortina/gemini.c
+++ b/drivers/net/ethernet/cortina/gemini.c
@@ -16,6 +16,7 @@
#include <linux/kernel.h>
#include <linux/bitmap.h>
#include <linux/init.h>
+#include <linux/mm.h>
#include <linux/module.h>
#include <linux/net.h>
#include <linux/platform_device.h>
@@ -37,6 +38,7 @@
#include <linux/skbuff.h>
#include <linux/phy.h>
#include <linux/crc32.h>
+#include <linux/sizes.h>
#include <linux/ethtool.h>
#include <linux/tcp.h>
#include <linux/u64_stats_sync.h>
@@ -509,6 +511,17 @@ static int gmac_pick_rx_max_len(unsigned int max_l3_len)
return -1;
}
+static unsigned int gmac_pick_rxq_order(void)
+{
+ if (totalram_pages() <= (SZ_32M >> PAGE_SHIFT))
+ return DEFAULT_GMAC_RXQ_ORDER - 2;
+
+ if (totalram_pages() <= (SZ_64M >> PAGE_SHIFT))
+ return DEFAULT_GMAC_RXQ_ORDER - 1;
+
+ return DEFAULT_GMAC_RXQ_ORDER;
+}
+
static int gmac_init(struct net_device *netdev)
{
struct gemini_ethernet_port *port = netdev_priv(netdev);
@@ -577,7 +590,7 @@ static int gmac_init(struct net_device *netdev)
writel(sw_weigh.bits32,
port->dma_base + GMAC_TX_WEIGHTING_CTRL_1_REG);
- port->rxq_order = DEFAULT_GMAC_RXQ_ORDER;
+ port->rxq_order = gmac_pick_rxq_order();
port->txq_order = DEFAULT_GMAC_TXQ_ORDER;
port->rx_coalesce_nsecs = DEFAULT_RX_COALESCE_NSECS;
--
2.56.0
[PATCH net-next v8 16/19] net: ethernet: cortina: Scope RX statistics updates
Author: Linus Walleij
Date: Thu, 12 Nov 2026 13:30
Date: Thu, 12 Nov 2026 13:30
117 lines
11977 bytes
11977 bytes
On 32-bit PREEMPT_RT systems u64_stats_update_begin() disables
preemption. The RX poll currently holds that writer section across
gmac_rx(), which takes spinlock_t locks and calls into GRO and fragment
handling. spinlock_t can sleep on PREEMPT_RT and must not be acquired
with preemption disabled.
Keep the writer sections around the counter updates only. NAPI ownership
continues to serialize normal RX writers, while the stop path updates its
counter after napi_disable(). This preserves the existing accounting
without covering potentially sleeping operations.
Initialize the dynamically allocated statistics synchronization objects
so their seqcount lockdep keys are valid on 32-bit debug kernels.
The issue was found by inspecting the u64_stats_sync writer scope. The
series was tested on a D-Link DIR-685 with media playback and ksmbd
transfers under ping load. PREEMPT_RT was not runtime-tested.
Fixes: 4d5ae32f5e1e ("net: ethernet: Add a driver for Gemini gigabit ethernet")
Signed-off-by: Linus Walleij <linusw@kernel.org>
---
drivers/net/ethernet/cortina/gemini.c | 26 +++++++++++++++++++-------
1 file changed, 19 insertions(+), 7 deletions(-)
diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
index cc26ff16a0a3..d0d161654f49 100644
--- a/drivers/net/ethernet/cortina/gemini.c
+++ b/drivers/net/ethernet/cortina/gemini.c
@@ -1769,12 +1769,12 @@ static struct sk_buff *gmac_skb_if_good_frame(struct gemini_ethernet_port *port,
unsigned int rx_status = word0.bits.status;
struct sk_buff *skb = NULL;
- port->rx_stats[rx_status]++;
- port->rx_csum_stats[rx_csum]++;
-
if (word0.bits.derr || word0.bits.perr ||
rx_status || frame_len < ETH_ZLEN ||
rx_csum >= RX_CHKSUM_IP_ERR_UNKNOWN) {
+ u64_stats_update_begin(&port->rx_stats_syncp);
+ port->rx_stats[rx_status]++;
+ port->rx_csum_stats[rx_csum]++;
port->stats.rx_errors++;
if (frame_len < ETH_ZLEN || RX_ERROR_LENGTH(rx_status))
@@ -1785,6 +1785,7 @@ static struct sk_buff *gmac_skb_if_good_frame(struct gemini_ethernet_port *port,
port->stats.rx_crc_errors++;
if (RX_ERROR_FRAME(rx_status))
port->stats.rx_frame_errors++;
+ u64_stats_update_end(&port->rx_stats_syncp);
return NULL;
}
@@ -1796,8 +1797,12 @@ static struct sk_buff *gmac_skb_if_good_frame(struct gemini_ethernet_port *port,
skb->ip_summed = CHECKSUM_UNNECESSARY;
update_exit:
+ u64_stats_update_begin(&port->rx_stats_syncp);
+ port->rx_stats[rx_status]++;
+ port->rx_csum_stats[rx_csum]++;
port->stats.rx_bytes += frame_len;
port->stats.rx_packets++;
+ u64_stats_update_end(&port->rx_stats_syncp);
return skb;
}
@@ -1855,7 +1860,9 @@ static unsigned int gmac_rx(struct net_device *netdev, unsigned int budget,
if (word3.bits32 & SOF_BIT) {
if (skb) {
napi_free_frags(&port->napi);
+ u64_stats_update_begin(&port->rx_stats_syncp);
port->stats.rx_dropped++;
+ u64_stats_update_end(&port->rx_stats_syncp);
skb = NULL;
frag_nr = 0;
}
@@ -1947,7 +1954,9 @@ static unsigned int gmac_rx(struct net_device *netdev, unsigned int budget,
put_page(page);
if (!dropping) {
+ u64_stats_update_begin(&port->rx_stats_syncp);
port->stats.rx_dropped++;
+ u64_stats_update_end(&port->rx_stats_syncp);
dropping = true;
}
@@ -1979,13 +1988,13 @@ static int gmac_napi_poll(struct napi_struct *napi, int budget)
bool reschedule;
freeq_threshold = 1 << (geth->freeq_buf_order - 1);
- u64_stats_update_begin(&port->rx_stats_syncp);
received = gmac_rx(napi->dev, budget, &freeq_consumed, &reschedule);
- if (!reschedule && received < budget)
+ if (!reschedule && received < budget) {
+ u64_stats_update_begin(&port->rx_stats_syncp);
++port->rx_napi_exits;
-
- u64_stats_update_end(&port->rx_stats_syncp);
+ u64_stats_update_end(&port->rx_stats_syncp);
+ }
port->freeq_refill += freeq_consumed;
if (port->freeq_refill > freeq_threshold) {
@@ -2912,6 +2921,9 @@ static int gemini_ethernet_port_probe(struct platform_device *pdev)
}
port = netdev_priv(netdev);
+ u64_stats_init(&port->tx_stats_syncp);
+ u64_stats_init(&port->rx_stats_syncp);
+ u64_stats_init(&port->ir_stats_syncp);
SET_NETDEV_DEV(netdev, dev);
port->netdev = netdev;
port->id = id;
--
2.56.0
[PATCH net-next v8 18/19] net: ethernet: cortina: Reject TX rings smaller than retained pointers
Author: Linus Walleij
Date: Thu, 12 Nov 2026 13:30
Date: Thu, 12 Nov 2026 13:30
101 lines
11297 bytes
11297 bytes
The hardware-owned TX read pointers can retain their positions when the
queues are rebuilt. After shrinking the TX ring with ethtool -G while
the interface is down, gmac_setup_txqs() copies those pointers into the
write and clean pointers without checking the new ring size. A retained
pointer outside the smaller ring can then index past both the skb table
and the coherent descriptor allocation.
Reject a TX ring size that cannot contain every retained read pointer,
before applying any RX ring changes in the same ethtool request. Check
again before allocating and programming the TX rings on open, including
for pointer state inherited from firmware. DMA is stopped at both checks.
Do not just mask the software pointers, which would leave the hardware
read pointer outside the new ring.
Found during Sashiko review and confirmed with a pointer arithmetic
model: a retained pointer of 200 with 16 entries reports 15 free entries
and permits the first TX write at index 200. Checked all 16-bit pointer
values against queue orders 0 through 15 and cross-built the driver for
ARM.
Tested on the D-Link DIR-685 with intense transfers and ping storms.
Fixes: 4d5ae32f5e1e ("net: ethernet: Add a driver for Gemini gigabit ethernet")
Signed-off-by: Linus Walleij <linusw@kernel.org>
---
drivers/net/ethernet/cortina/gemini.c | 32 +++++++++++++++++++++++++++++++-
1 file changed, 31 insertions(+), 1 deletion(-)
diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
index 24a66fc34738..899d96420b1b 100644
--- a/drivers/net/ethernet/cortina/gemini.c
+++ b/drivers/net/ethernet/cortina/gemini.c
@@ -602,6 +602,20 @@ static int gmac_init(struct net_device *netdev)
return 0;
}
+static bool gmac_txq_size_valid(struct net_device *netdev, unsigned int entries)
+{
+ struct gemini_ethernet_port *port = netdev_priv(netdev);
+ unsigned int i;
+
+ /* DMA is stopped, so the hardware read pointers cannot advance. */
+ for (i = 0; i < netdev->num_tx_queues; i++)
+ if (GET_RPTR(port->dma_base + GMAC_SW_TX_QUEUE_PTR_REG(i)) >=
+ entries)
+ return false;
+
+ return true;
+}
+
static int gmac_setup_txqs(struct net_device *netdev)
{
struct gemini_ethernet_port *port = netdev_priv(netdev);
@@ -616,6 +630,12 @@ static int gmac_setup_txqs(struct net_device *netdev)
unsigned int r;
int i;
+ if (!gmac_txq_size_valid(netdev, entries)) {
+ netdev_err(netdev,
+ "TX ring is too small for the retained read pointer\n");
+ return -EINVAL;
+ }
+
rwptr_reg = port->dma_base + GMAC_SW_TX_QUEUE0_PTR_REG;
skb_tab = kzalloc_objs(*skb_tab, len);
@@ -2623,11 +2643,21 @@ static int gmac_set_ringparam(struct net_device *netdev,
{
struct gemini_ethernet_port *port = netdev_priv(netdev);
unsigned int old_rxq_order;
+ unsigned int txq_order = port->txq_order;
int err = 0;
if (netif_running(netdev))
return -EBUSY;
+ if (rp->tx_pending) {
+ txq_order = min(15, ilog2(rp->tx_pending - 1) + 1);
+ if (!gmac_txq_size_valid(netdev, BIT(txq_order))) {
+ NL_SET_ERR_MSG(extack,
+ "TX ring is too small for the retained read pointer");
+ return -EINVAL;
+ }
+ }
+
if (rp->rx_pending) {
old_rxq_order = port->rxq_order;
port->rxq_order = min(15, ilog2(rp->rx_pending - 1) + 1);
@@ -2638,7 +2668,7 @@ static int gmac_set_ringparam(struct net_device *netdev,
}
}
if (rp->tx_pending) {
- port->txq_order = min(15, ilog2(rp->tx_pending - 1) + 1);
+ port->txq_order = txq_order;
port->irq_every_tx_packets = 1 << (port->txq_order - 2);
}
--
2.56.0
Re: [PATCH net-next v8 11/19] net: ethernet: cortina: Validate RX fragment lengths
Author: netdev-bot+sashiko
Date: Mon, 16 Nov 2026 14:45
Date: Mon, 16 Nov 2026 14:45
93 lines
10833 bytes
10833 bytes
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] In drivers/net/ethernet/cortina/gemini.c, gmac_rx() calls
gmac_skb_if_good_frame() on the SOF descriptor.
--
net: ethernet: cortina: Validate RX fragment lengths
This patch adds checks in gmac_rx() so that an RX fragment cannot run
past its posted free queue fragment or past the page. Zero-length
non-final descriptors are now dropped, and an empty EOF descriptor
completes the frame that is already assembled.
> diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
> index 72d2c002f8fc2..364504c9d441a 100644
> --- a/drivers/net/ethernet/cortina/gemini.c
> +++ b/drivers/net/ethernet/cortina/gemini.c
[ ... ]
> @@ -1864,6 +1865,9 @@ static unsigned int gmac_rx(struct net_device *netdev, unsigned int budget,
> if (!skb)
> goto err_drop;
>
> + if (frag_len < NET_IP_ALIGN)
> + goto err_drop;
> +
> page_offs += NET_IP_ALIGN;
> frag_len -= NET_IP_ALIGN;
> frag_nr = 0;
[Severity: Low]
Is this frame now counted as both received and dropped?
By the time this check runs, gmac_skb_if_good_frame() has already counted
the frame as received:
drivers/net/ethernet/cortina/gemini.c:gmac_skb_if_good_frame() {
...
update_exit:
port->stats.rx_bytes += frame_len;
port->stats.rx_packets++;
return skb;
}
err_drop then frees the skb and increments rx_dropped. The dropping flag
was reset to false on SOF, so that increment always happens:
err_drop:
if (skb) {
napi_free_frags(&port->napi);
...
if (!dropping) {
port->stats.rx_dropped++;
dropping = true;
}
Nothing takes back the earlier rx_packets and rx_bytes increments. The
rx_bytes value also comes from the hardware byte_count, which this patch
otherwise treats as untrusted.
The other new paths into err_drop that this patch adds later in gmac_rx()
do the same thing:
- the frame_len < ETH_ZLEN || frame_len < skb->len check on EOF
- the zero-length non-final descriptor
- the freeq_frag_len / PAGE_SIZE bounds check
The commit message says:
Account descriptor inconsistencies as drops rather than wire length
errors.
Is that accurate, given that these frames also show up in rx_packets and
rx_bytes?
The napi_get_frags() failure path and failures on later descriptors
already double count like this. This patch adds more ways to reach it. The
problem is also still there at the end of the series: "net: ethernet:
cortina: Scope RX statistics updates" only wraps the same update_exit
increments in u64_stats_update_begin()/u64_stats_update_end().
Could the rx_packets/rx_bytes update move to the napi_gro_frags()
completion point instead?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112-gemini-ethernet-fixes-3-v8-0-f55bd43e02d3%40kernel.org
Re: [PATCH net-next v8 11/19] net: ethernet: cortina: Validate RX fragment lengths
Author: Linus Walleij
Date: Tue, 17 Nov 2026 08:07
Date: Tue, 17 Nov 2026 08:07
30 lines
9838 bytes
9838 bytes
On Mon, Nov 16, 2026 at 3:45 PM <netdev-bot+sashiko@kernel.org> wrote: > > @@ -1864,6 +1865,9 @@ static unsigned int gmac_rx(struct net_device *netdev, unsigned int budget, > > if (!skb) > > goto err_drop; > > > > + if (frag_len < NET_IP_ALIGN) > > + goto err_drop; > > + > > page_offs += NET_IP_ALIGN; > > frag_len -= NET_IP_ALIGN; > > frag_nr = 0; > > [Severity: Low] > Is this frame now counted as both received and dropped? > > By the time this check runs, gmac_skb_if_good_frame() has already counted > the frame as received: (...) > Nothing takes back the earlier rx_packets and rx_bytes increments. The > rx_bytes value also comes from the hardware byte_count, which this patch > otherwise treats as untrusted. This can be addressed with follow-up patches if desired. Yours, Linus Walleij
Re: [PATCH net-next v8 00/19] net: ethernet: cortina: Fix Gemini RX/TX buffer management
Author: Linus Walleij
Date: Tue, 17 Nov 2026 08:12
Date: Tue, 17 Nov 2026 08:12
10 lines
8691 bytes
8691 bytes
I think the Sashiko noise on this patch series is now down to unexpected hardware failure, memory exhaustion and low-prio noise. I think it could be applied and then maybe I can follow up some of the comments later once this is landed and the ethernet is working properly. Yours, Linus Walleij
Re: [PATCH net-next v8 00/19] net: ethernet: cortina: Fix Gemini RX/TX buffer management
Author: Jakub Kicinski
Date: Fri, 20 Nov 2026 00:52
Date: Fri, 20 Nov 2026 00:52
20 lines
8698 bytes
8698 bytes
On Tue, 17 Nov 2026 09:12:02 +0100 Linus Walleij wrote: > I think the Sashiko noise on this patch series is now down to > unexpected hardware failure, memory exhaustion and low-prio noise. > > I think it could be applied and then maybe I can follow up some > of the comments later once this is landed and the ethernet is > working properly. Ugh, not only is this pile over 15 patches it also fell victim to the mass rename of kmalloc to kmalloc_obj that landed in Linus's tree :/ So I can't apply this even if I'd rather put it behind us :/ Please: - rebase - drop all the Fixes tags here, net-next == no Fixes tags - chop off the stats patches (14 patches should be left for v9, reset after the previous stuff is applied) Consider the 15 patch rule a hard one.
Re: [PATCH net-next v8 00/19] net: ethernet: cortina: Fix Gemini RX/TX buffer management
Author: Linus Walleij
Date: Fri, 20 Nov 2026 10:26
Date: Fri, 20 Nov 2026 10:26
41 lines
10336 bytes
10336 bytes
On Fri, Nov 20, 2026 at 1:52 AM Jakub Kicinski <kuba@kernel.org> wrote: > > I think it could be applied and then maybe I can follow up some > > of the comments later once this is landed and the ethernet is > > working properly. > > Ugh, not only is this pile over 15 patches I blame that on Sashiko's constant "oh by the way fix this too" and ability to find otherworldly error paths... > it also fell victim to the > mass rename of kmalloc to kmalloc_obj that landed in Linus's tree :/ Oh Christ :D > Please: > - rebase > - drop all the Fixes tags here, net-next == no Fixes tags NB: the Sashiko used by netdev insists on us adding these so we are giving incoherent messages here. Clearly Sashiko can see the prefix on each series and know whether Fixes: are applicable or not. We need to fix the Sashiko prompts. (I can perhaps do it if I know how, Roman?) Sometimes Fixes: are used for "nonurgent fixes" in other kernel trees but maybe it just doesn't work with netdev. > - chop off the stats patches (14 patches should be left for v9, reset > after the previous stuff is applied) > > Consider the 15 patch rule a hard one. OK I fix the fixes! I'll also squash the patch that was considered for squashing since it makes Sashiko happier. Yours, Linus Walleij
Re: [PATCH net-next v8 00/19] net: ethernet: cortina: Fix Gemini RX/TX buffer management
Author: Jakub Kicinski
Date: Fri, 20 Nov 2026 18:21
Date: Fri, 20 Nov 2026 18:21
14 lines
8514 bytes
8514 bytes
On Fri, 20 Nov 2026 11:26:15 +0100 Linus Walleij wrote: > > Ugh, not only is this pile over 15 patches > > I blame that on Sashiko's constant "oh by the way fix this too" > and ability to find otherworldly error paths... > > > it also fell victim to the > > mass rename of kmalloc to kmalloc_obj that landed in Linus's tree :/ > > Oh Christ :D None of this is funny to maintainers on the receiving end of the slop cannons. Please pay attention to what you're doing. No excuses.
Re: [PATCH net-next v8 00/19] net: ethernet: cortina: Fix Gemini RX/TX buffer management
Author: Roman Gushchin
Date: Fri, 20 Nov 2026 18:39
Date: Fri, 20 Nov 2026 18:39
36 lines
9475 bytes
9475 bytes
Linus Walleij <linusw@kernel.org> writes: > On Fri, Nov 20, 2026 at 1:52 AM Jakub Kicinski <kuba@kernel.org> wrote: > >> > I think it could be applied and then maybe I can follow up some >> > of the comments later once this is landed and the ethernet is >> > working properly. >> >> Ugh, not only is this pile over 15 patches > > I blame that on Sashiko's constant "oh by the way fix this too" > and ability to find otherworldly error paths... > >> it also fell victim to the >> mass rename of kmalloc to kmalloc_obj that landed in Linus's tree :/ > > Oh Christ :D > >> Please: >> - rebase >> - drop all the Fixes tags here, net-next == no Fixes tags > > NB: the Sashiko used by netdev insists on us adding these so we > are giving incoherent messages here. Clearly Sashiko can see > the prefix on each series and know whether Fixes: are applicable > or not. We need to fix the Sashiko > prompts. (I can perhaps do it if I know how, Roman?) > > Sometimes Fixes: are used for "nonurgent fixes" in other kernel > trees but maybe it just doesn't work with netdev. A clearly written text with rules in English would work, I can do the rest. But it's for "upstream" Sashiko, netdev-specific Sashiko is supported by Jakub and has quite significantly diverged.
Re: [PATCH net-next v8 00/19] net: ethernet: cortina: Fix Gemini RX/TX buffer management
Author: Jakub Kicinski
Date: Fri, 20 Nov 2026 22:34
Date: Fri, 20 Nov 2026 22:34
26 lines
9255 bytes
9255 bytes
On Fri, 20 Nov 2026 10:39:00 -0800 Roman Gushchin wrote: > >> Please: > >> - rebase > >> - drop all the Fixes tags here, net-next == no Fixes tags > > > > NB: the Sashiko used by netdev insists on us adding these so we > > are giving incoherent messages here. Clearly Sashiko can see > > the prefix on each series and know whether Fixes: are applicable > > or not. We need to fix the Sashiko > > prompts. (I can perhaps do it if I know how, Roman?) > > > > Sometimes Fixes: are used for "nonurgent fixes" in other kernel > > trees but maybe it just doesn't work with netdev. > > A clearly written text with rules in English would work, I can do the > rest. But it's for "upstream" Sashiko, netdev-specific Sashiko is > supported by Jakub and has quite significantly diverged. I don't think networking maintainers fully agree between each other on exactly what's considered a fix and what's not. So it's hard to write up, in English or thru any other medium :S So, sadly, humans have to still do the basic thinking and not slap unnecessary tags on -next patches, just because some bot suggests with a low confidence that a tag may be appropriate.
Re: [PATCH net-next v8 00/19] net: ethernet: cortina: Fix Gemini RX/TX buffer management
Author: Linus Walleij
Date: Fri, 20 Nov 2026 23:40
Date: Fri, 20 Nov 2026 23:40
21 lines
9845 bytes
9845 bytes
On Fri, Nov 20, 2026 at 7:21 PM Jakub Kicinski <kuba@kernel.org> wrote: > On Fri, 20 Nov 2026 11:26:15 +0100 Linus Walleij wrote: > > > Ugh, not only is this pile over 15 patches > > > > I blame that on Sashiko's constant "oh by the way fix this too" > > and ability to find otherworldly error paths... > > > > > it also fell victim to the > > > mass rename of kmalloc to kmalloc_obj that landed in Linus's tree :/ > > > > Oh Christ :D > > None of this is funny to maintainers on the receiving end of the slop > cannons. Please pay attention to what you're doing. No excuses. I'm sorry, I just try to see some light in the slop, sometimes that is indeed hard. Linus Walleij
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads