Thread View: linux.kernel.netdev
2 messages
2 total messages
Started by Linus Walleij
Tue, 10 Nov 2026 22:13
[PATCH net-next v7 05/18] net: ethernet: cortina: Correct free queue DMA mappings
Author: Linus Walleij
Date: Tue, 10 Nov 2026 22:13
Date: Tue, 10 Nov 2026 22:13
281 lines
17782 bytes
17782 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.
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 fa51f2b54073..71d7e6189531 100644
--- a/drivers/net/ethernet/cortina/gemini.c
+++ b/drivers/net/ethernet/cortina/gemini.c
@@ -726,32 +726,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)
@@ -759,11 +770,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;
@@ -790,14 +802,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,
@@ -811,6 +822,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;
@@ -820,9 +832,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;
}
@@ -835,31 +855,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;
}
@@ -930,7 +944,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;
@@ -982,12 +995,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);
}
@@ -1009,7 +1021,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;
@@ -1023,12 +1034,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);
}
@@ -1486,7 +1495,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;
@@ -1523,8 +1531,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);
@@ -1543,14 +1549,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
Re: [PATCH net-next v7 05/18] net: ethernet: cortina: Correct free queue DMA mappings
Author: netdev-bot+sashiko
Date: Wed, 11 Nov 2026 23:10
Date: Wed, 11 Nov 2026 23:10
253 lines
17908 bytes
17908 bytes
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 (5) · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] geth_freeq_lookup() now holds the global geth->freeq_lock with
spin_lock_irqsave() while it scans all num_freeq_pages entries…
Pre-existing issues:
- [Medium] After this patch, geth_freeq_alloc_map_page() still overwrites
geth->freeq_pages[pn].page and .mapping (gpage->mapping =…
- [Medium] When a free-queue page is replaced,
geth_freeq_alloc_map_page() overwrites the old page's PAGE_SIZE…
- [Medium] In this patch, gmac_rx() takes frag_len from the 16-bit
hardware fields word0.bits.buffer_size and word1.bits.byte_count.
- [Medium] geth_cleanup_freeq() drains references with 'while
(page_ref_count(gpage->page) > 0) put_page(gpage->page);'.
- [Medium] Free-queue pages use long-lived DMA_FROM_DEVICE streaming
mappings.
--
net: ethernet: cortina: Correct free queue DMA mappings
This patch keeps the free queue page DMA base separate from the fragment
iterator, drops the replacement path's unmap of the wrong buffer, and uses
the saved base and PAGE_SIZE for setup unwind and teardown. It replaces
gmac_get_queue_page() with geth_freeq_lookup(), which derives the fragment
offset from the saved base and snapshots page, base and offset under
freeq_lock. It also rejects mappings that cannot fit in the 32-bit
hardware descriptors.
> diff --git a/drivers/net/ethernet/cortina/gemini.c b/drivers/net/ethernet/cortina/gemini.c
> index fa51f2b54073f..71d7e61895312 100644
> --- a/drivers/net/ethernet/cortina/gemini.c
> +++ b/drivers/net/ethernet/cortina/gemini.c
> @@ -726,32 +726,43 @@ static int gmac_setup_rxq(struct net_device *netdev)
[ ... ]
> + 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;
> }
[Severity: Medium]
geth_freeq_lookup() now takes the shared geth->freeq_lock with
spin_lock_irqsave() and holds it while it scans all num_freeq_pages
entries one by one. gmac_rx() calls it once per RX descriptor. With
default sizing that is about 1024 iterations per descriptor with IRQs
off, and it contends with refill and with the other port.
Is this extra IRQ-off latency on the RX hot path acceptable for this
intermediate state?
The next patch in the series, "net: ethernet: cortina: Index free queue
fragments with XArray", replaces the scan with a single xa_load() under
the lock. The cost only exists between these two patches.
[ ... ]
> @@ -835,31 +855,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);
[Severity: Medium]
This isn't a bug introduced by this patch, but when a slot is replaced
here, the old page's PAGE_SIZE streaming mapping in gpage->mapping is
overwritten without being unmapped. geth_cleanup_freeq() and the
geth_setup_freeq() unwind path only unmap the slot's current
gpage->mapping.
Does the replaced DMA mapping leak? Can the page also go back to the
allocator while it is still mapped?
The old code never unmapped the old mapping either. It unmapped the first
fragment of the new buffer instead. The commit message also defers this
to later in the series.
At the end of the series this is handled: geth_freeq_recycle_page() calls
dma_unmap_single_attrs() before it drops a page that is still referenced,
geth_freeq_release_pages() unmaps every live slot, and slots are no longer
overwritten while their fragments are in flight. These changes come from
"net: ethernet: cortina: Preserve in-flight free queue pages" and
"Recycle claimed free queue pages".
> +
> + /* Then put our new mapping into the page table */
> + gpage->mapping = page_mapping;
> + gpage->page = page;
[Severity: Medium]
This is a pre-existing issue, but the slot metadata is still overwritten
here when geth_fill_freeq() replaces a page whose page_ref_count() > 1.
RX descriptors can still hold fragment addresses from the replaced page,
and geth_freeq_lookup() only matches each slot's current base:
geth_fill_freeq()
geth_freeq_alloc_map_page()
gpage->mapping = page_mapping; <- old base is forgotten
gmac_rx()
geth_freeq_lookup() <- NULL for an old-page fragment
goto err_drop; <- page == NULL, no put_page()
Could this drop the frame and leak that fragment's page reference?
gmac_cleanup_rxq() would hit the same NULL return and skip put_page().
The baseline behaved the same way, and the commit message defers the
lifetime of in-flight fragments to later in the series.
At the end of the series, "net: ethernet: cortina: Preserve in-flight
free queue pages" fixes this. Metadata slots are allocated independently
of the free queue position through geth_freeq_alloc_slot(), and each
fragment address stays in geth->freeq_mappings until geth_freeq_claim()
claims it.
[ ... ]
> @@ -1023,12 +1034,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);
[Severity: Medium]
This isn't a bug introduced by this patch, but this while loop drops
references the driver doesn't own, including references held by skbs
that were already handed to the stack. It also re-reads the refcount
after the page may already have been freed.
Can this over-put a page, or read a freed one, at queue teardown (ndo_stop
or a ring resize)?
At the end of the series, geth_cleanup_freeq() calls
geth_freeq_release_pages() instead. That function puts exactly
gpage->fragments outstanding hardware references plus the single base
reference ("net: ethernet: cortina: Preserve in-flight free queue pages").
[ ... ]
> @@ -1523,8 +1531,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;
> -
[Severity: Medium]
This isn't a bug introduced by this patch, but frag_len and frame_len come
straight from the 16-bit hardware fields word0.bits.buffer_size and
word1.bits.byte_count. Later, frag_len gets unchecked unsigned
adjustments (frag_len -= NET_IP_ALIGN; frag_len = frame_len - skb->len)
and is then passed to skb_fill_page_desc(). geth_freeq_lookup() only
validates the offset.
Could a malformed descriptor attach a page fragment that runs past the
free queue fragment or past the end of the page?
This is fixed later in the series by "net: ethernet: cortina: Validate RX
fragment lengths". That patch rejects frag_len < NET_IP_ALIGN, frame_len
< ETH_ZLEN or frame_len < skb->len, zero-length descriptors, and frag_len
values larger than the space left in the fragment or the page.
> if (word3.bits32 & SOF_BIT) {
> if (skb) {
> napi_free_frags(&port->napi);
> @@ -1543,14 +1549,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;
[Severity: Medium]
This is a pre-existing issue, but the free queue pages use long-lived
DMA_FROM_DEVICE streaming mappings. gmac_rx() hands fragments written by
the device to the stack without any dma_sync_single_*_for_cpu() call.
geth_fill_freeq() also re-posts pages to the device without
dma_sync_single_for_device().
Doesn't this break the DMA API ownership rules for these buffers?
The CPU sync is a no-op on the Gemini FA526, so there should be no
visible effect on the target hardware. Other implementations could see
stale data.
At the end of the series this is handled:
- geth_freeq_claim() calls dma_sync_single_range_for_cpu() for each
claimed fragment.
- geth_freeq_post_page() calls dma_sync_single_for_device() when it
recycles a page.
- geth_freeq_release_pages() syncs fragments still owned by the
hardware before unmapping with DMA_ATTR_SKIP_CPU_SYNC.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261110-gemini-ethernet-fixes-3-v7-0-9c32a1a46aec%40kernel.org
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