From 3a7583863c725c64b389813f28522213b0827fd9 Mon Sep 17 00:00:00 2001 From: Dj <43033058+DjDeveloperr@users.noreply.github.com> Date: Thu, 26 Nov 2026 03:10:44 -0500 Subject: [PATCH 1/4] wifi: brcmfmac: msgbuf: send from pages that hold only transmit buffers A frame to send was mapped where the stack left it, in an skb head that shares its page with other kernel memory. The IOMMU maps the whole page, 16 KiB with 16K pages, so the device could read whatever else was in it. Copy each frame, after the Ethernet header that goes in the descriptor, into a buffer from a pool of pages that hold only transmit buffers and stay mapped to the device for reading. There is one buffer per transmit packet id, so as many frames can be in flight as before. One copy now replaces the map and unmap of every sent frame. A frame too long for a buffer, which the MTU does not allow, is dropped and counted. The receive pool code is now shared by both directions. The pools also track in host memory which buffers are posted, so a completion that names a buffer already returned cannot return it twice. Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com> --- .../broadcom/brcm80211/brcmfmac/msgbuf.c | 252 ++++++++++-------- 1 file changed, 140 insertions(+), 112 deletions(-) diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c index d04e805db817d..75f14c313715e 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c @@ -269,13 +269,14 @@ struct brcmf_msgbuf_work_item { }; /* - * Receive buffers of one size, in pages that hold nothing else and stay - * mapped while the rings exist. An IOMMU maps whole pages, so a device that - * writes past the length it was given still reaches only other receive - * buffers, never an skb or other host memory. A completed buffer is copied - * into a new skb and goes back to the pool. + * DMA buffers of one size, in pages that hold nothing else and stay mapped + * while the rings exist. An IOMMU maps whole pages, so a device that reads or + * writes past the length it was given still reaches only other buffers of the + * pool, never an skb or other host memory. A frame is copied into a buffer to + * be sent, and out of one when it was received; the buffer then goes back to + * the pool. */ -struct brcmf_msgbuf_rxpool { +struct brcmf_msgbuf_pool { struct device *dev; enum dma_data_direction direction; u32 buf_size; @@ -284,9 +285,10 @@ struct brcmf_msgbuf_rxpool { u32 nr_chunks; void **chunk_cpu; dma_addr_t *chunk_dma; - spinlock_t lock; /* protects free and nr_free */ + spinlock_t lock; /* protects free, nr_free and busy */ u32 *free; u32 nr_free; + unsigned long *busy; /* posted, until its completion */ }; struct brcmf_msgbuf { @@ -327,8 +329,9 @@ struct brcmf_msgbuf { struct brcmf_msgbuf_pktids *tx_pktids; struct brcmf_msgbuf_pktids *rx_pktids; - struct brcmf_msgbuf_rxpool rx_data_pool; - struct brcmf_msgbuf_rxpool rx_ctl_pool; + struct brcmf_msgbuf_pool tx_pool; + struct brcmf_msgbuf_pool rx_data_pool; + struct brcmf_msgbuf_pool rx_ctl_pool; struct brcmf_flowring *flow; struct workqueue_struct *txflow_wq; @@ -347,9 +350,9 @@ struct brcmf_msgbuf_pktid { u16 data_offset; struct sk_buff *skb; dma_addr_t physaddr; - /* receive: the posted buffer, physaddr being its DMA address */ - struct brcmf_msgbuf_rxpool *rxpool; - u32 rxbuf; + /* the posted buffer, physaddr being its DMA address */ + struct brcmf_msgbuf_pool *pool; + u32 buf; }; struct brcmf_msgbuf_pktids { @@ -442,39 +445,7 @@ brcmf_msgbuf_claim_pktid(struct brcmf_msgbuf_pktids *pktids, u32 *idx) } -static int -brcmf_msgbuf_alloc_pktid(struct device *dev, - struct brcmf_msgbuf_pktids *pktids, - struct sk_buff *skb, u16 data_offset, - dma_addr_t *physaddr, u32 *idx) -{ - struct brcmf_msgbuf_pktid *array; - - array = pktids->array; - - *physaddr = dma_map_single(dev, skb->data + data_offset, - skb->len - data_offset, pktids->direction); - - if (dma_mapping_error(dev, *physaddr)) { - brcmf_err("dma_map_single failed !!\n"); - return -ENOMEM; - } - - if (brcmf_msgbuf_claim_pktid(pktids, idx)) { - dma_unmap_single(dev, *physaddr, skb->len - data_offset, - pktids->direction); - return -ENOMEM; - } - - array[*idx].data_offset = data_offset; - array[*idx].physaddr = *physaddr; - array[*idx].skb = skb; - - return 0; -} - - -static void brcmf_msgbuf_rxpool_free(struct brcmf_msgbuf_rxpool *pool) +static void brcmf_msgbuf_pool_free(struct brcmf_msgbuf_pool *pool) { u32 i; @@ -487,17 +458,19 @@ static void brcmf_msgbuf_rxpool_free(struct brcmf_msgbuf_rxpool *pool) kfree(pool->chunk_cpu); kfree(pool->chunk_dma); kfree(pool->free); + bitmap_free(pool->busy); pool->chunk_cpu = NULL; pool->chunk_dma = NULL; pool->free = NULL; + pool->busy = NULL; pool->nr_free = 0; } -static int brcmf_msgbuf_rxpool_init(struct device *dev, - struct brcmf_msgbuf_rxpool *pool, - u32 nr_bufs, u32 buf_size, - enum dma_data_direction direction) +static int brcmf_msgbuf_pool_init(struct device *dev, + struct brcmf_msgbuf_pool *pool, + u32 nr_bufs, u32 buf_size, + enum dma_data_direction direction) { u32 i; @@ -514,7 +487,8 @@ static int brcmf_msgbuf_rxpool_init(struct device *dev, pool->chunk_dma = kcalloc(pool->nr_chunks, sizeof(*pool->chunk_dma), GFP_KERNEL); pool->free = kcalloc(nr_bufs, sizeof(*pool->free), GFP_KERNEL); - if (!pool->chunk_cpu || !pool->chunk_dma || !pool->free) + pool->busy = bitmap_zalloc(nr_bufs, GFP_KERNEL); + if (!pool->chunk_cpu || !pool->chunk_dma || !pool->free || !pool->busy) goto fail; for (i = 0; i < pool->nr_chunks; i++) { @@ -537,32 +511,54 @@ static int brcmf_msgbuf_rxpool_init(struct device *dev, return 0; fail: - brcmf_msgbuf_rxpool_free(pool); + brcmf_msgbuf_pool_free(pool); return -ENOMEM; } -static dma_addr_t brcmf_msgbuf_rxbuf_dma(struct brcmf_msgbuf_rxpool *pool, - u32 buf) +static dma_addr_t brcmf_msgbuf_buf_dma(struct brcmf_msgbuf_pool *pool, + u32 buf) { return pool->chunk_dma[buf / pool->bufs_per_chunk] + (buf % pool->bufs_per_chunk) * pool->buf_size; } -static void *brcmf_msgbuf_rxbuf_cpu(struct brcmf_msgbuf_rxpool *pool, u32 buf) +static void *brcmf_msgbuf_buf_cpu(struct brcmf_msgbuf_pool *pool, u32 buf) { return pool->chunk_cpu[buf / pool->bufs_per_chunk] + (buf % pool->bufs_per_chunk) * pool->buf_size; } -static void brcmf_msgbuf_rxbuf_put(struct brcmf_msgbuf_rxpool *pool, u32 buf) +static int brcmf_msgbuf_buf_get(struct brcmf_msgbuf_pool *pool, u32 *buf) +{ + unsigned long flags; + + spin_lock_irqsave(&pool->lock, flags); + if (!pool->nr_free) { + spin_unlock_irqrestore(&pool->lock, flags); + return -ENOMEM; + } + *buf = pool->free[--pool->nr_free]; + __set_bit(*buf, pool->busy); + spin_unlock_irqrestore(&pool->lock, flags); + + return 0; +} + + +/* + * A completion the firmware sends for a packet id being posted can name a + * buffer already back in the pool: it is not returned twice. + */ +static void brcmf_msgbuf_buf_put(struct brcmf_msgbuf_pool *pool, u32 buf) { unsigned long flags; spin_lock_irqsave(&pool->lock, flags); - pool->free[pool->nr_free++] = buf; + if (__test_and_clear_bit(buf, pool->busy)) + pool->free[pool->nr_free++] = buf; spin_unlock_irqrestore(&pool->lock, flags); } @@ -570,27 +566,20 @@ static void brcmf_msgbuf_rxbuf_put(struct brcmf_msgbuf_rxpool *pool, u32 buf) /* Post a receive buffer whose data starts data_offset bytes into it. */ static int brcmf_msgbuf_alloc_rx_pktid(struct brcmf_msgbuf_pktids *pktids, - struct brcmf_msgbuf_rxpool *pool, u16 data_offset, + struct brcmf_msgbuf_pool *pool, u16 data_offset, dma_addr_t *physaddr, u32 *idx) { struct brcmf_msgbuf_pktid *pktid; - unsigned long flags; u32 buf; - spin_lock_irqsave(&pool->lock, flags); - if (!pool->nr_free) { - spin_unlock_irqrestore(&pool->lock, flags); + if (brcmf_msgbuf_buf_get(pool, &buf)) return -ENOMEM; - } - buf = pool->free[--pool->nr_free]; - spin_unlock_irqrestore(&pool->lock, flags); - if (brcmf_msgbuf_claim_pktid(pktids, idx)) { - brcmf_msgbuf_rxbuf_put(pool, buf); + brcmf_msgbuf_buf_put(pool, buf); return -ENOMEM; } - *physaddr = brcmf_msgbuf_rxbuf_dma(pool, buf); + *physaddr = brcmf_msgbuf_buf_dma(pool, buf); dma_sync_single_for_device(pool->dev, *physaddr, pool->buf_size, pool->direction); @@ -598,8 +587,46 @@ brcmf_msgbuf_alloc_rx_pktid(struct brcmf_msgbuf_pktids *pktids, pktid->data_offset = data_offset; pktid->physaddr = *physaddr; pktid->skb = NULL; - pktid->rxpool = pool; - pktid->rxbuf = buf; + pktid->pool = pool; + pktid->buf = buf; + + return 0; +} + + +/* + * Post a frame to be sent: copy it, from data_offset on, into a buffer of the + * pool. -EMSGSIZE if it does not fit one. + */ +static int +brcmf_msgbuf_alloc_tx_pktid(struct brcmf_msgbuf_pktids *pktids, + struct brcmf_msgbuf_pool *pool, + struct sk_buff *skb, u16 data_offset, + dma_addr_t *physaddr, u32 *idx) +{ + struct brcmf_msgbuf_pktid *pktid; + u32 len = skb->len - data_offset; + u32 buf; + + if (len > pool->buf_size) + return -EMSGSIZE; + if (brcmf_msgbuf_buf_get(pool, &buf)) + return -ENOMEM; + if (brcmf_msgbuf_claim_pktid(pktids, idx)) { + brcmf_msgbuf_buf_put(pool, buf); + return -ENOMEM; + } + + *physaddr = brcmf_msgbuf_buf_dma(pool, buf); + memcpy(brcmf_msgbuf_buf_cpu(pool, buf), skb->data + data_offset, len); + dma_sync_single_for_device(pool->dev, *physaddr, len, pool->direction); + + pktid = &pktids->array[*idx]; + pktid->data_offset = data_offset; + pktid->physaddr = *physaddr; + pktid->skb = skb; + pktid->pool = pool; + pktid->buf = buf; return 0; } @@ -616,7 +643,7 @@ brcmf_msgbuf_get_rx_pktid(struct brcmf_msgbuf_pktids *pktids, u32 idx, u32 offset, u32 len) { struct brcmf_msgbuf_pktid *pktid; - struct brcmf_msgbuf_rxpool *pool; + struct brcmf_msgbuf_pool *pool; struct sk_buff *skb = NULL; u32 size; @@ -631,7 +658,7 @@ brcmf_msgbuf_get_rx_pktid(struct brcmf_msgbuf_pktids *pktids, u32 idx, return NULL; } - pool = pktid->rxpool; + pool = pktid->pool; size = pool->buf_size - pktid->data_offset; if (offset > size) { brcmf_err("packet id %d: data offset %u past its %u bytes\n", @@ -646,20 +673,19 @@ brcmf_msgbuf_get_rx_pktid(struct brcmf_msgbuf_pktids *pktids, u32 idx, skb = brcmu_pkt_buf_get_skb(len); if (skb) memcpy(skb->data, - brcmf_msgbuf_rxbuf_cpu(pool, pktid->rxbuf) + offset, len); + brcmf_msgbuf_buf_cpu(pool, pktid->buf) + offset, len); else brcmf_err("Failed to alloc SKB\n"); release: - brcmf_msgbuf_rxbuf_put(pool, pktid->rxbuf); + brcmf_msgbuf_buf_put(pool, pktid->buf); atomic_set(&pktid->allocated, 0); return skb; } static struct sk_buff * -brcmf_msgbuf_get_pktid(struct device *dev, struct brcmf_msgbuf_pktids *pktids, - u32 idx) +brcmf_msgbuf_get_pktid(struct brcmf_msgbuf_pktids *pktids, u32 idx) { struct brcmf_msgbuf_pktid *pktid; struct sk_buff *skb; @@ -671,9 +697,7 @@ brcmf_msgbuf_get_pktid(struct device *dev, struct brcmf_msgbuf_pktids *pktids, } if (pktids->array[idx].allocated.counter) { pktid = &pktids->array[idx]; - dma_unmap_single(dev, pktid->physaddr, - pktid->skb->len - pktid->data_offset, - pktids->direction); + brcmf_msgbuf_buf_put(pktid->pool, pktid->buf); skb = pktid->skb; pktid->allocated.counter = 0; return skb; @@ -686,24 +710,17 @@ brcmf_msgbuf_get_pktid(struct device *dev, struct brcmf_msgbuf_pktids *pktids, static void -brcmf_msgbuf_release_array(struct device *dev, - struct brcmf_msgbuf_pktids *pktids) +brcmf_msgbuf_release_array(struct brcmf_msgbuf_pktids *pktids) { struct brcmf_msgbuf_pktid *array; - struct brcmf_msgbuf_pktid *pktid; u32 count; array = pktids->array; count = 0; do { - /* A receive buffer has no skb: its pool goes as a whole. */ - if (array[count].allocated.counter && array[count].skb) { - pktid = &array[count]; - dma_unmap_single(dev, pktid->physaddr, - pktid->skb->len - pktid->data_offset, - pktids->direction); - brcmu_pkt_buf_free_skb(pktid->skb); - } + /* The buffers go with their pools; a frame not sent is freed. */ + if (array[count].allocated.counter) + brcmu_pkt_buf_free_skb(array[count].skb); count++; } while (count < pktids->array_size); @@ -715,15 +732,14 @@ brcmf_msgbuf_release_array(struct device *dev, static void brcmf_msgbuf_release_pktids(struct brcmf_msgbuf *msgbuf) { if (msgbuf->rx_pktids) - brcmf_msgbuf_release_array(msgbuf->drvr->bus_if->dev, - msgbuf->rx_pktids); - brcmf_msgbuf_rxpool_free(&msgbuf->rx_data_pool); - brcmf_msgbuf_rxpool_free(&msgbuf->rx_ctl_pool); + brcmf_msgbuf_release_array(msgbuf->rx_pktids); + brcmf_msgbuf_pool_free(&msgbuf->rx_data_pool); + brcmf_msgbuf_pool_free(&msgbuf->rx_ctl_pool); brcmu_pkt_buf_free_skb(msgbuf->ioctl_resp); msgbuf->ioctl_resp = NULL; if (msgbuf->tx_pktids) - brcmf_msgbuf_release_array(msgbuf->drvr->bus_if->dev, - msgbuf->tx_pktids); + brcmf_msgbuf_release_array(msgbuf->tx_pktids); + brcmf_msgbuf_pool_free(&msgbuf->tx_pool); } @@ -1096,6 +1112,8 @@ static void brcmf_msgbuf_txflow(struct brcmf_msgbuf *msgbuf, u16 flowid) u32 pktid; struct msgbuf_tx_msghdr *tx_msghdr; u64 address; + u8 ifidx; + int ret; commonring = msgbuf->flowrings[flowid]; if (!brcmf_commonring_write_available(commonring)) @@ -1112,17 +1130,24 @@ static void brcmf_msgbuf_txflow(struct brcmf_msgbuf *msgbuf, u16 flowid) break; } skb_orphan(skb); - if (brcmf_msgbuf_alloc_pktid(msgbuf->drvr->bus_if->dev, - msgbuf->tx_pktids, skb, ETH_HLEN, - &physaddr, &pktid)) { + ret = brcmf_msgbuf_alloc_tx_pktid(msgbuf->tx_pktids, + &msgbuf->tx_pool, skb, + ETH_HLEN, &physaddr, &pktid); + if (ret == -EMSGSIZE) { + bphy_err(drvr, "Frame of %u bytes too long to send\n", + skb->len); + ifidx = brcmf_flowring_ifidx_get(flow, flowid); + brcmf_txfinalize(brcmf_get_ifp(drvr, ifidx), skb, false); + continue; + } + if (ret) { brcmf_flowring_reinsert(flow, flowid, skb); bphy_err(drvr, "No PKTID available !!\n"); break; } ret_ptr = brcmf_commonring_reserve_for_write(commonring); if (!ret_ptr) { - brcmf_msgbuf_get_pktid(msgbuf->drvr->bus_if->dev, - msgbuf->tx_pktids, pktid); + brcmf_msgbuf_get_pktid(msgbuf->tx_pktids, pktid); brcmf_flowring_reinsert(flow, flowid, skb); break; } @@ -1295,8 +1320,7 @@ brcmf_msgbuf_process_txstatus(struct brcmf_msgbuf *msgbuf, void *buf) tx_status = (struct msgbuf_tx_status *)buf; idx = le32_to_cpu(tx_status->msg.request_id) - 1; flow_ring_id = le16_to_cpu(tx_status->compl_hdr.flow_ring_id); - skb = brcmf_msgbuf_get_pktid(msgbuf->drvr->bus_if->dev, - msgbuf->tx_pktids, idx); + skb = brcmf_msgbuf_get_pktid(msgbuf->tx_pktids, idx); if (!skb) return; @@ -2120,16 +2144,20 @@ int brcmf_proto_msgbuf_attach(struct brcmf_pub *drvr) brcmf_msgbuf_rx_dma_direction(drvr->bus_if->dev)); if (!msgbuf->rx_pktids) goto fail; - if (brcmf_msgbuf_rxpool_init(drvr->bus_if->dev, &msgbuf->rx_data_pool, - min_t(u32, msgbuf->max_rxbufpost, - NR_RX_PKTIDS), - BRCMF_MSGBUF_MAX_PKT_SIZE, - msgbuf->rx_pktids->direction) || - brcmf_msgbuf_rxpool_init(drvr->bus_if->dev, &msgbuf->rx_ctl_pool, - msgbuf->max_ioctlrespbuf + - msgbuf->max_eventbuf, - BRCMF_MSGBUF_MAX_CTL_PKT_SIZE, - msgbuf->rx_pktids->direction)) + /* Every packet id can hold a frame being sent, as before. */ + if (brcmf_msgbuf_pool_init(drvr->bus_if->dev, &msgbuf->tx_pool, + NR_TX_PKTIDS, BRCMF_MSGBUF_MAX_PKT_SIZE, + msgbuf->tx_pktids->direction) || + brcmf_msgbuf_pool_init(drvr->bus_if->dev, &msgbuf->rx_data_pool, + min_t(u32, msgbuf->max_rxbufpost, + NR_RX_PKTIDS), + BRCMF_MSGBUF_MAX_PKT_SIZE, + msgbuf->rx_pktids->direction) || + brcmf_msgbuf_pool_init(drvr->bus_if->dev, &msgbuf->rx_ctl_pool, + msgbuf->max_ioctlrespbuf + + msgbuf->max_eventbuf, + BRCMF_MSGBUF_MAX_CTL_PKT_SIZE, + msgbuf->rx_pktids->direction)) goto fail; msgbuf->flow = brcmf_flowring_attach(drvr->bus_if->dev, From 69bfe62319cccd34bd7bb6e00c5a864a5d238b89 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Sun, 29 Nov 2026 14:34:24 +1100 Subject: [PATCH 2/4] wifi: brcmfmac: msgbuf: read the tx slot before releasing it brcmf_msgbuf_get_pktid() returned the buffer and dropped the slot with a plain store before it had read pktid->skb. On arm64 the store can become visible first: the TX worker claims the slot again and the completion returns the new frame's skb, which is then freed twice. Read skb, pool and buf first, clear pktid->skb and release the slot with a release store. brcmf_msgbuf_get_rx_pktid() gets the same release. alloc_tx_pktid() claims a slot before it fills it. With skb cleared on release, a completion that arrives in that window finds no skb: warn and leave the slot alone instead of freeing the previous frame again. Signed-off-by: Ryan Murray --- .../broadcom/brcm80211/brcmfmac/msgbuf.c | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c index 75f14c313715e..fcea6665ed0d8 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c @@ -679,7 +679,7 @@ brcmf_msgbuf_get_rx_pktid(struct brcmf_msgbuf_pktids *pktids, u32 idx, release: brcmf_msgbuf_buf_put(pool, pktid->buf); - atomic_set(&pktid->allocated, 0); + atomic_set_release(&pktid->allocated, 0); return skb; } @@ -688,24 +688,40 @@ static struct sk_buff * brcmf_msgbuf_get_pktid(struct brcmf_msgbuf_pktids *pktids, u32 idx) { struct brcmf_msgbuf_pktid *pktid; + struct brcmf_msgbuf_pool *pool; struct sk_buff *skb; + u32 buf; if (idx >= pktids->array_size) { brcmf_err("Invalid packet id %d (max %d)\n", idx, pktids->array_size); return NULL; } - if (pktids->array[idx].allocated.counter) { - pktid = &pktids->array[idx]; - brcmf_msgbuf_buf_put(pktid->pool, pktid->buf); - skb = pktid->skb; - pktid->allocated.counter = 0; - return skb; - } else { + pktid = &pktids->array[idx]; + if (!atomic_read(&pktid->allocated)) { brcmf_err("Invalid packet id %d (not in use)\n", idx); + return NULL; } - return NULL; + /* + * Read everything out of the slot before it is released: once + * allocated drops to 0 the TX worker may claim the slot again and + * overwrite it. A claimed slot with no skb is still being filled by + * brcmf_msgbuf_alloc_tx_pktid(); a completion for it is stray, so + * leave the slot alone rather than free the previous skb again. + */ + skb = pktid->skb; + if (WARN_ON_ONCE(!skb)) { + brcmf_err("packet id %d: completion before the slot was filled\n", + idx); + return NULL; + } + pool = pktid->pool; + buf = pktid->buf; + pktid->skb = NULL; + brcmf_msgbuf_buf_put(pool, buf); + atomic_set_release(&pktid->allocated, 0); + return skb; } From d532011d2d3bfca2fb6ca898aa8b763c54794321 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Sun, 29 Nov 2026 18:43:50 +1100 Subject: [PATCH 3/4] wifi: brcmfmac: msgbuf: publish a packet id only once it is filled A completion took a non-NULL pktid->skb as proof that a TX slot was ready. brcmf_msgbuf_alloc_tx_pktid() stores skb before pool and buf and the completion read them with plain loads, so on arm64 a completion that names a slot still being filled could see the new skb with the old pool or buffer, or the old fields with a claim already set. Give the slot three states. Claiming moves it FREE -> RESERVED; the allocators move it to READY with a release store after every field is written; completions acquire the state and read the fields only on READY. A completion for a RESERVED slot is stray and leaves the slot, its buffer and its skb to the TX worker. Teardown frees only the frames of READY slots. RX slots follow the same states. Add brcmf-msgbuf-pktid-tests.py: it runs the production claim, fill, completion and teardown code with a completion delivered in the middle of the fill, and five mutations of the ordering. Signed-off-by: Ryan Murray --- .../broadcom/brcm80211/brcmfmac/msgbuf.c | 66 +++- tools/asahi/brcmf-msgbuf-pktid-tests.py | 325 ++++++++++++++++++ tools/asahi/run-host-tests.py | 3 + 3 files changed, 377 insertions(+), 17 deletions(-) create mode 100755 tools/asahi/brcmf-msgbuf-pktid-tests.py diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c index fcea6665ed0d8..fceaa3a6bf86b 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c @@ -345,6 +345,17 @@ struct brcmf_msgbuf { struct list_head work_queue; }; +/* + * pktid->allocated: a slot is claimed (FREE -> RESERVED) before its fields + * are filled and only published (RESERVED -> READY, release) once they are. + * A completion reads the fields only after it sees READY (acquire), and a + * completion for a RESERVED slot is stray: it names a packet id the device + * was not given yet, so the slot is left to its owner. + */ +#define BRCMF_MSGBUF_PKTID_FREE 0 +#define BRCMF_MSGBUF_PKTID_RESERVED 1 +#define BRCMF_MSGBUF_PKTID_READY 2 + struct brcmf_msgbuf_pktid { atomic_t allocated; u16 data_offset; @@ -430,9 +441,13 @@ brcmf_msgbuf_claim_pktid(struct brcmf_msgbuf_pktids *pktids, u32 *idx) (*idx)++; if (*idx == pktids->array_size) *idx = 0; - if (array[*idx].allocated.counter == 0) - if (atomic_cmpxchg(&array[*idx].allocated, 0, 1) == 0) - break; + if (atomic_read(&array[*idx].allocated) == + BRCMF_MSGBUF_PKTID_FREE && + atomic_cmpxchg(&array[*idx].allocated, + BRCMF_MSGBUF_PKTID_FREE, + BRCMF_MSGBUF_PKTID_RESERVED) == + BRCMF_MSGBUF_PKTID_FREE) + break; count++; } while (count < pktids->array_size); @@ -589,6 +604,7 @@ brcmf_msgbuf_alloc_rx_pktid(struct brcmf_msgbuf_pktids *pktids, pktid->skb = NULL; pktid->pool = pool; pktid->buf = buf; + atomic_set_release(&pktid->allocated, BRCMF_MSGBUF_PKTID_READY); return 0; } @@ -627,6 +643,8 @@ brcmf_msgbuf_alloc_tx_pktid(struct brcmf_msgbuf_pktids *pktids, pktid->skb = skb; pktid->pool = pool; pktid->buf = buf; + /* Publish the filled slot: get_pktid() reads it after an acquire. */ + atomic_set_release(&pktid->allocated, BRCMF_MSGBUF_PKTID_READY); return 0; } @@ -653,7 +671,14 @@ brcmf_msgbuf_get_rx_pktid(struct brcmf_msgbuf_pktids *pktids, u32 idx, return NULL; } pktid = &pktids->array[idx]; - if (!pktid->allocated.counter) { + switch (atomic_read_acquire(&pktid->allocated)) { + case BRCMF_MSGBUF_PKTID_READY: + break; + case BRCMF_MSGBUF_PKTID_RESERVED: + brcmf_err("packet id %d: completion before the slot was posted\n", + idx); + return NULL; + default: brcmf_err("Invalid packet id %d (not in use)\n", idx); return NULL; } @@ -679,7 +704,7 @@ brcmf_msgbuf_get_rx_pktid(struct brcmf_msgbuf_pktids *pktids, u32 idx, release: brcmf_msgbuf_buf_put(pool, pktid->buf); - atomic_set_release(&pktid->allocated, 0); + atomic_set_release(&pktid->allocated, BRCMF_MSGBUF_PKTID_FREE); return skb; } @@ -698,29 +723,35 @@ brcmf_msgbuf_get_pktid(struct brcmf_msgbuf_pktids *pktids, u32 idx) return NULL; } pktid = &pktids->array[idx]; - if (!atomic_read(&pktid->allocated)) { + switch (atomic_read_acquire(&pktid->allocated)) { + case BRCMF_MSGBUF_PKTID_READY: + break; + case BRCMF_MSGBUF_PKTID_RESERVED: + /* + * Still being filled by brcmf_msgbuf_alloc_tx_pktid(): the + * completion is stray. Leave the slot alone rather than free + * the previous skb again. + */ + WARN_ON_ONCE(1); + brcmf_err("packet id %d: completion before the slot was posted\n", + idx); + return NULL; + default: brcmf_err("Invalid packet id %d (not in use)\n", idx); return NULL; } /* * Read everything out of the slot before it is released: once - * allocated drops to 0 the TX worker may claim the slot again and - * overwrite it. A claimed slot with no skb is still being filled by - * brcmf_msgbuf_alloc_tx_pktid(); a completion for it is stray, so - * leave the slot alone rather than free the previous skb again. + * allocated drops to FREE the TX worker may claim the slot again and + * overwrite it. */ skb = pktid->skb; - if (WARN_ON_ONCE(!skb)) { - brcmf_err("packet id %d: completion before the slot was filled\n", - idx); - return NULL; - } pool = pktid->pool; buf = pktid->buf; pktid->skb = NULL; brcmf_msgbuf_buf_put(pool, buf); - atomic_set_release(&pktid->allocated, 0); + atomic_set_release(&pktid->allocated, BRCMF_MSGBUF_PKTID_FREE); return skb; } @@ -735,7 +766,8 @@ brcmf_msgbuf_release_array(struct brcmf_msgbuf_pktids *pktids) count = 0; do { /* The buffers go with their pools; a frame not sent is freed. */ - if (array[count].allocated.counter) + if (atomic_read(&array[count].allocated) == + BRCMF_MSGBUF_PKTID_READY) brcmu_pkt_buf_free_skb(array[count].skb); count++; } while (count < pktids->array_size); diff --git a/tools/asahi/brcmf-msgbuf-pktid-tests.py b/tools/asahi/brcmf-msgbuf-pktid-tests.py new file mode 100755 index 0000000000000..0b843b529fab1 --- /dev/null +++ b/tools/asahi/brcmf-msgbuf-pktid-tests.py @@ -0,0 +1,325 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Run the brcmfmac msgbuf packet-id slot code against observing stubs. + +The production claim, fill, publish, completion and teardown functions are +compiled with stubbed DMA, atomics and skbs. The cases check that a TX or RX +slot is published READY only after all of its fields are written, with a +release store; that a completion reads the slot only after an acquire load +of READY; and that a completion arriving while the slot is still RESERVED +(between claim and publish) neither returns a frame nor frees the previous +one, and leaves the slot and its buffer with their owner. +""" +import argparse +import json +from pathlib import Path +import re +import subprocess +import tempfile + +ROOT = Path(__file__).resolve().parents[2] +DRIVER = ROOT / 'drivers/net/wireless/broadcom/brcm80211/brcmfmac' + + +def block_end(source, opening): + depth = 1 + end = opening + 1 + while depth: + depth += (source[end] == '{') - (source[end] == '}') + end += 1 + return end + + +def function(source, name): + match = re.search(r'^static [^;{]*?\b' + name + r'\([^;]*?\)\s*\{', source, re.M) + if not match: + raise ValueError('Missing function ' + name) + end = block_end(source, source.index('{', match.start())) + return source[match.start():end] + '\n' + + +def struct(source, name): + match = re.search(r'^struct ' + name + r' \{', source, re.M) + if not match: + raise ValueError('Missing struct ' + name) + end = block_end(source, source.index('{', match.start())) + return source[match.start():end] + ';\n' + + +PREFIX = r''' +#include +#include +#include +#include +#include +#include +#include +typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t dma_addr_t; +#define CHECK(c) do { if (!(c)) { fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #c); abort(); } } while (0) +#define container_of(p,t,m) ((t *)((char *)(p)-offsetof(t,m))) +#define min(a,b) ((a) < (b) ? (a) : (b)) +#define brcmf_err(...) (errors++) +#define WARN_ON_ONCE(c) ((c) ? (warns++, 1) : 0) +#define BITS_PER_LONG 64 +static int errors, warns; +enum dma_data_direction { DMA_BIDIRECTIONAL, DMA_TO_DEVICE, DMA_FROM_DEVICE }; +struct device { int unused; }; +typedef struct { int unused; } spinlock_t; +#define spin_lock_irqsave(l,f) ((void)(l),(f)=0) +#define spin_unlock_irqrestore(l,f) ((void)(l),(void)(f)) +static void __set_bit(u32 b, unsigned long *p) { p[b / BITS_PER_LONG] |= 1UL << (b % BITS_PER_LONG); } +static bool __test_and_clear_bit(u32 b, unsigned long *p) +{ bool was = p[b / BITS_PER_LONG] >> (b % BITS_PER_LONG) & 1; p[b / BITS_PER_LONG] &= ~(1UL << (b % BITS_PER_LONG)); return was; } +static bool test_bit(u32 b, unsigned long *p) { return p[b / BITS_PER_LONG] >> (b % BITS_PER_LONG) & 1; } + +struct sk_buff { u32 len; u8 *data; int freed; }; +static int skb_frees; +static void brcmu_pkt_buf_free_skb(struct sk_buff *s) { if (s) { CHECK(!s->freed); s->freed = 1; skb_frees++; } } +static struct sk_buff rx_skbs[8]; static u8 rx_mem[8][256]; static int rx_next; +static struct sk_buff *brcmu_pkt_buf_get_skb(u32 len) +{ struct sk_buff *s = &rx_skbs[rx_next]; s->data = rx_mem[rx_next++]; s->len = len; return s; } + +/* Atomics: every access is observed. */ +typedef struct { int counter; } atomic_t; +static bool in_completion; +static int releases, acquires; +static void on_publish(atomic_t *a); +static int atomic_read(const atomic_t *a) { CHECK(!in_completion); return a->counter; } +static int atomic_read_acquire(const atomic_t *a) { acquires++; return a->counter; } +static void atomic_set(atomic_t *a, int v) { (void)a; (void)v; CHECK(!"plain store to a slot state"); } +static void atomic_set_release(atomic_t *a, int v) { a->counter = v; releases++; if (v == 2) on_publish(a); } +static int atomic_cmpxchg(atomic_t *a, int o, int n) { int c = a->counter; if (c == o) a->counter = n; return c; } + +/* DMA: the hook runs between claim and publish, as an IRQ could. */ +static void (*mid_fill)(void); +static void dma_sync_single_for_device(struct device *d, dma_addr_t a, u32 l, int dir) +{ (void)d; (void)a; (void)l; (void)dir; if (mid_fill) { void (*f)(void) = mid_fill; mid_fill = NULL; f(); } } +static void dma_sync_single_for_cpu(struct device *d, dma_addr_t a, u32 l, int dir) +{ (void)d; (void)a; (void)l; (void)dir; } +''' + +MAIN = r''' +#define NBUF 4 +static struct device dev; +static u8 chunk[NBUF][64]; +static void *chunk_cpu[NBUF]; static dma_addr_t chunk_dma[NBUF]; +static u32 freelist[NBUF]; static unsigned long busy[1]; +static struct brcmf_msgbuf_pool pool; +static struct brcmf_msgbuf_pktid slots[4]; +static struct brcmf_msgbuf_pktids ids; + +static void setup(void) +{ + memset(&pool, 0, sizeof(pool)); memset(slots, 0, sizeof(slots)); busy[0] = 0; + pool.dev = &dev; pool.buf_size = 64; pool.chunk_size = 64; pool.bufs_per_chunk = 1; + pool.nr_chunks = NBUF; pool.chunk_cpu = chunk_cpu; pool.chunk_dma = chunk_dma; + pool.free = freelist; pool.busy = busy; + for (int i = 0; i < NBUF; i++) { + chunk_cpu[i] = chunk[i]; chunk_dma[i] = 0x1000 * (i + 1); + freelist[i] = NBUF - 1 - i; + } + pool.nr_free = NBUF; + ids = (struct brcmf_msgbuf_pktids){ 4, 3, DMA_TO_DEVICE, slots }; +} + +/* A slot is published READY only when every field the completion reads is in. */ +static struct sk_buff *expect_skb; +static void on_publish(atomic_t *a) +{ + struct brcmf_msgbuf_pktid *p = container_of(a, struct brcmf_msgbuf_pktid, allocated); + CHECK(p->pool == &pool); + CHECK(p->physaddr == brcmf_msgbuf_buf_dma(&pool, p->buf)); + CHECK(test_bit(p->buf, pool.busy)); + CHECK(p->skb == expect_skb); +} + +static struct sk_buff *complete_tx(u32 idx) +{ + struct sk_buff *s; + int acq = acquires; + in_completion = true; + s = brcmf_msgbuf_get_pktid(&ids, idx); + in_completion = false; + CHECK(acquires == acq + 1); + return s; +} + +static struct sk_buff *complete_rx(u32 idx) +{ + struct sk_buff *s; + int acq = acquires; + in_completion = true; + s = brcmf_msgbuf_get_rx_pktid(&ids, idx, 0, 8); + in_completion = false; + CHECK(acquires == acq + 1); + return s; +} + +static u32 filling; +static struct sk_buff *stray_result; +static int stray_calls; +static void stray_completion(void) +{ + /* The firmware names the packet id the TX worker is filling. */ + struct brcmf_msgbuf_pktid *p = &slots[filling]; + u32 nr_free = pool.nr_free; + stray_calls++; + CHECK(p->allocated.counter == BRCMF_MSGBUF_PKTID_RESERVED); + stray_result = complete_tx(filling); + CHECK(stray_result == NULL); + CHECK(p->allocated.counter == BRCMF_MSGBUF_PKTID_RESERVED); + CHECK(pool.nr_free == nr_free); +} + +static void stray_rx_completion(void) +{ + struct brcmf_msgbuf_pktid *p = &slots[filling]; + u32 nr_free = pool.nr_free; + stray_calls++; + CHECK(p->allocated.counter == BRCMF_MSGBUF_PKTID_RESERVED); + CHECK(complete_rx(filling) == NULL); + CHECK(p->allocated.counter == BRCMF_MSGBUF_PKTID_RESERVED); + CHECK(pool.nr_free == nr_free); +} + +int main(void) +{ + u8 payload[16] = "0123456789abcde"; + struct sk_buff a = { 16, payload, 0 }, b = { 16, payload, 0 }; + dma_addr_t phys; + u32 idx, idx2; + + /* Plain post and completion. */ + setup(); + expect_skb = &a; + CHECK(brcmf_msgbuf_alloc_tx_pktid(&ids, &pool, &a, 4, &phys, &idx) == 0); + CHECK(slots[idx].allocated.counter == BRCMF_MSGBUF_PKTID_READY); + CHECK(!memcmp(chunk[slots[idx].buf], payload + 4, 12)); + CHECK(complete_tx(idx) == &a); + CHECK(slots[idx].allocated.counter == BRCMF_MSGBUF_PKTID_FREE); + CHECK(slots[idx].skb == NULL && pool.nr_free == NBUF); + /* A second completion for the same id is refused. */ + errors = 0; + CHECK(complete_tx(idx) == NULL && errors == 1 && pool.nr_free == NBUF); + + /* Completion during reservation: the same slot is reused for b, and the + * firmware completes its id while b is being copied in. Neither a nor b + * may come back, and the slot and buffer stay with the TX worker. */ + ids.last_allocated_idx = idx ? idx - 1 : 3; + filling = idx; + mid_fill = stray_completion; + expect_skb = &b; + warns = 0; + CHECK(brcmf_msgbuf_alloc_tx_pktid(&ids, &pool, &b, 0, &phys, &idx2) == 0); + CHECK(idx2 == idx && stray_calls == 1 && warns == 1); + CHECK(slots[idx].allocated.counter == BRCMF_MSGBUF_PKTID_READY); + /* The real completion then returns b exactly once. */ + CHECK(complete_tx(idx) == &b); + CHECK(pool.nr_free == NBUF && slots[idx].allocated.counter == BRCMF_MSGBUF_PKTID_FREE); + + /* Teardown frees only published frames, never a reserved slot's. */ + setup(); + expect_skb = &a; a.freed = 0; + CHECK(brcmf_msgbuf_alloc_tx_pktid(&ids, &pool, &a, 0, &phys, &idx) == 0); + CHECK(brcmf_msgbuf_claim_pktid(&ids, &idx2) == 0 && idx2 != idx); + slots[idx2].skb = &b; b.freed = 0; /* stale pointer in a reserved slot */ + skb_frees = 0; + { + struct brcmf_msgbuf_pktid *arr = malloc(sizeof(slots)); + struct brcmf_msgbuf_pktids *p = malloc(sizeof(*p)); + memcpy(arr, slots, sizeof(slots)); + *p = ids; p->array = arr; + brcmf_msgbuf_release_array(p); + } + CHECK(skb_frees == 1 && a.freed && !b.freed); + + /* RX slots follow the same states. */ + setup(); + ids.direction = DMA_FROM_DEVICE; + expect_skb = NULL; + CHECK(brcmf_msgbuf_alloc_rx_pktid(&ids, &pool, 2, &phys, &idx) == 0); + CHECK(slots[idx].allocated.counter == BRCMF_MSGBUF_PKTID_READY); + CHECK(complete_rx(idx) != NULL); + CHECK(slots[idx].allocated.counter == BRCMF_MSGBUF_PKTID_FREE && pool.nr_free == NBUF); + ids.last_allocated_idx = idx ? idx - 1 : 3; + filling = idx; + mid_fill = stray_rx_completion; + CHECK(brcmf_msgbuf_alloc_rx_pktid(&ids, &pool, 2, &phys, &idx2) == 0); + CHECK(idx2 == idx && stray_calls == 2); + CHECK(complete_rx(idx) != NULL && pool.nr_free == NBUF); + + printf("msgbuf pktid: all cases passed (%d release, %d acquire)\n", releases, acquires); + return 0; +} +''' + +NAMES = ['brcmf_msgbuf_claim_pktid', 'brcmf_msgbuf_buf_dma', 'brcmf_msgbuf_buf_cpu', + 'brcmf_msgbuf_buf_get', 'brcmf_msgbuf_buf_put', 'brcmf_msgbuf_alloc_rx_pktid', + 'brcmf_msgbuf_alloc_tx_pktid', 'brcmf_msgbuf_get_rx_pktid', 'brcmf_msgbuf_get_pktid'] + +MUTATIONS = { + # Publish at claim time, before the fields are written. + 'publish-at-claim': ('BRCMF_MSGBUF_PKTID_RESERVED) ==', 'BRCMF_MSGBUF_PKTID_READY) =='), + # Treat a reserved slot as ready in the TX completion. + 'reserved-is-ready': ('\tcase BRCMF_MSGBUF_PKTID_READY:\n\t\tbreak;\n\tcase BRCMF_MSGBUF_PKTID_RESERVED:\n\t\t/*', + '\tcase BRCMF_MSGBUF_PKTID_READY:\n\tcase BRCMF_MSGBUF_PKTID_RESERVED:\n\t\tbreak;\n\t\t/*'), + # Plain load of the slot state in the TX completion. + 'plain-read': ('\tskb = pktid->skb;\n\tpool = pktid->pool;', + '\tskb = pktid->skb;\n\t(void)atomic_read(&pktid->allocated);\n\tpool = pktid->pool;'), + # Publish the TX slot before its skb is stored. + 'publish-before-skb': ('\tpktid->skb = skb;\n\tpktid->pool = pool;\n\tpktid->buf = buf;\n\t/* Publish', + '\tatomic_set_release(&pktid->allocated, BRCMF_MSGBUF_PKTID_READY);\n\tpktid->skb = skb;\n\tpktid->pool = pool;\n\tpktid->buf = buf;\n\t/* Publish'), + # Teardown frees whatever a claimed slot points at. + 'release-reserved': ('BRCMF_MSGBUF_PKTID_READY)\n\t\t\tbrcmu_pkt_buf_free_skb', + 'BRCMF_MSGBUF_PKTID_READY || 1)\n\t\t\tbrcmu_pkt_buf_free_skb'), +} + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--out', type=Path) + parser.add_argument('--mutation', choices=sorted(MUTATIONS)) + args = parser.parse_args() + + source = (DRIVER / 'msgbuf.c').read_text() + states = ''.join(re.findall(r'^#define BRCMF_MSGBUF_PKTID_\w+\s+\d+\n', source, re.M)) + if len(states.splitlines()) != 3: + raise SystemExit('missing BRCMF_MSGBUF_PKTID_* states') + structs = ''.join(struct(source, n) for n in + ('brcmf_msgbuf_pool', 'brcmf_msgbuf_pktid', 'brcmf_msgbuf_pktids')) + body = ''.join(function(source, n) for n in NAMES) + release = function(source, 'brcmf_msgbuf_release_array').replace('kfree(', 'free(') + body += release + if args.mutation: + old, new = MUTATIONS[args.mutation] + if body.count(old) != 1: + raise SystemExit(f'mutation {args.mutation} does not apply') + body = body.replace(old, new) + # The test reads atomic state directly; the code under test may not. + code = (PREFIX + states + structs.replace('atomic_t allocated', 'atomic_t allocated') + + body + MAIN) + + def run(out): + out.mkdir(parents=True, exist_ok=True) + src = out / 'pktid.c' + src.write_text(code) + exe = out / 'pktid' + subprocess.run(['clang', '-std=gnu11', '-O1', '-g', '-Wall', '-Wno-unused-function', + '-fsanitize=address,undefined', '-fno-sanitize-recover=all', + str(src), '-o', str(exe)], check=True) + result = subprocess.run([str(exe)], capture_output=True, text=True) + receipt = {'exit': result.returncode, 'stdout': result.stdout, 'stderr': result.stderr, + 'limits': 'Observing host stubs, single-threaded; the IRQ is modelled at the ' + 'DMA sync inside the fill, not as a concurrent CPU.'} + (out / 'results.json').write_text(json.dumps(receipt, indent=2) + '\n') + print(json.dumps(receipt)) + return result.returncode + + if args.out: + raise SystemExit(run(args.out)) + with tempfile.TemporaryDirectory(prefix='brcmf-msgbuf-pktid-') as temp: + raise SystemExit(run(Path(temp))) + + +if __name__ == '__main__': + main() diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index a3c33382e0fb1..42793cb2d9788 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -179,6 +179,9 @@ def mutations(flag, names, base, expect=KILL): + mutations('--mutation', ('stale-target', 'zero-named', 'no-call', 'lost-bss-success'), out()), 'brcmf-join-scan-tests.py': [(out(), PASS)] + [(out() + ['--ref', '33bec4016c466985bfbb5ff4c2089fcab8242dcf'], KILL)], + 'brcmf-msgbuf-pktid-tests.py': [(out(), PASS)] + + mutations('--mutation', ('publish-at-claim', 'reserved-is-ready', 'plain-read', + 'publish-before-skb', 'release-reserved'), out()), 'brcmf-pcie-dma-tests.py': [(out('--output'), PASS)] + mutations('--mutation', ('narrow-stream', 'ignore-aperture', 'revision-wide'), out('--output')), 'brcmf-recovery-tests.py': [(out(), PASS)] From 69f3a4afb64e489eeb5090a1beeec2912267988d Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Mon, 30 Nov 2026 00:15:56 +1100 Subject: [PATCH 4/4] wifi: brcmfmac: msgbuf: claim a tx packet id once, post it after the ring slot brcmf_msgbuf_get_pktid() only checked READY, so the TX worker's ring-full rollback and a completion naming the same packet id could both take the skb, and the slot was READY before brcmf_msgbuf_txflow() had a ring slot for it, so a stray completion in that window freed an skb the worker then reinserted. Keep the TX slot RESERVED until its message is in the ring and publish it there; on a full ring give the slot back directly, without going through the completion path. A completion now takes a READY slot with a cmpxchg to CLAIMED, TX and RX, so only one consumer ever gets its skb and buffer. The host test covers a stray completion before and inside the rollback, two interleaved completions for one id, and a threaded rollback/post stress, with two new mutations. Signed-off-by: Ryan Murray --- .../broadcom/brcm80211/brcmfmac/msgbuf.c | 69 ++++- tools/asahi/brcmf-msgbuf-pktid-tests.py | 262 ++++++++++++++++-- tools/asahi/run-host-tests.py | 3 +- 3 files changed, 302 insertions(+), 32 deletions(-) diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c index fceaa3a6bf86b..3f40748d6024f 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/msgbuf.c @@ -350,11 +350,19 @@ struct brcmf_msgbuf { * are filled and only published (RESERVED -> READY, release) once they are. * A completion reads the fields only after it sees READY (acquire), and a * completion for a RESERVED slot is stray: it names a packet id the device - * was not given yet, so the slot is left to its owner. + * was not given yet, so the slot is left to its owner. A TX slot stays + * RESERVED until its message has a place in the ring, so the TX worker can + * still take it back when the ring is full. + * + * A completion takes a READY slot over with READY -> CLAIMED (cmpxchg, + * acquire) before it reads the fields, so only one consumer ever gets the + * slot's skb and buffer; a second completion for the same id finds CLAIMED + * or FREE and does nothing. */ #define BRCMF_MSGBUF_PKTID_FREE 0 #define BRCMF_MSGBUF_PKTID_RESERVED 1 #define BRCMF_MSGBUF_PKTID_READY 2 +#define BRCMF_MSGBUF_PKTID_CLAIMED 3 struct brcmf_msgbuf_pktid { atomic_t allocated; @@ -611,8 +619,11 @@ brcmf_msgbuf_alloc_rx_pktid(struct brcmf_msgbuf_pktids *pktids, /* - * Post a frame to be sent: copy it, from data_offset on, into a buffer of the - * pool. -EMSGSIZE if it does not fit one. + * Fill a slot for a frame to be sent: copy it, from data_offset on, into a + * buffer of the pool. -EMSGSIZE if it does not fit one. The slot is left + * RESERVED: the caller publishes it with brcmf_msgbuf_post_tx_pktid() once + * the message is in the ring, or gives it back with + * brcmf_msgbuf_unclaim_tx_pktid(). */ static int brcmf_msgbuf_alloc_tx_pktid(struct brcmf_msgbuf_pktids *pktids, @@ -643,13 +654,38 @@ brcmf_msgbuf_alloc_tx_pktid(struct brcmf_msgbuf_pktids *pktids, pktid->skb = skb; pktid->pool = pool; pktid->buf = buf; - /* Publish the filled slot: get_pktid() reads it after an acquire. */ - atomic_set_release(&pktid->allocated, BRCMF_MSGBUF_PKTID_READY); return 0; } +/* + * Publish a filled TX slot: from here on its skb belongs to the completion, + * which reads the slot after an acquire. + */ +static void +brcmf_msgbuf_post_tx_pktid(struct brcmf_msgbuf_pktids *pktids, u32 idx) +{ + atomic_set_release(&pktids->array[idx].allocated, + BRCMF_MSGBUF_PKTID_READY); +} + + +/* + * Give back a TX slot that was never posted. It is still RESERVED, so no + * completion has touched it, and the caller keeps the skb. + */ +static void +brcmf_msgbuf_unclaim_tx_pktid(struct brcmf_msgbuf_pktids *pktids, u32 idx) +{ + struct brcmf_msgbuf_pktid *pktid = &pktids->array[idx]; + + pktid->skb = NULL; + brcmf_msgbuf_buf_put(pktid->pool, pktid->buf); + atomic_set_release(&pktid->allocated, BRCMF_MSGBUF_PKTID_FREE); +} + + /* * Take a completed receive buffer back from the device. Copy len bytes from * offset into the data as posted to a new skb, and return the buffer to its @@ -671,13 +707,18 @@ brcmf_msgbuf_get_rx_pktid(struct brcmf_msgbuf_pktids *pktids, u32 idx, return NULL; } pktid = &pktids->array[idx]; - switch (atomic_read_acquire(&pktid->allocated)) { + switch (atomic_cmpxchg_acquire(&pktid->allocated, + BRCMF_MSGBUF_PKTID_READY, + BRCMF_MSGBUF_PKTID_CLAIMED)) { case BRCMF_MSGBUF_PKTID_READY: break; case BRCMF_MSGBUF_PKTID_RESERVED: brcmf_err("packet id %d: completion before the slot was posted\n", idx); return NULL; + case BRCMF_MSGBUF_PKTID_CLAIMED: + brcmf_err("packet id %d: already being completed\n", idx); + return NULL; default: brcmf_err("Invalid packet id %d (not in use)\n", idx); return NULL; @@ -723,7 +764,9 @@ brcmf_msgbuf_get_pktid(struct brcmf_msgbuf_pktids *pktids, u32 idx) return NULL; } pktid = &pktids->array[idx]; - switch (atomic_read_acquire(&pktid->allocated)) { + switch (atomic_cmpxchg_acquire(&pktid->allocated, + BRCMF_MSGBUF_PKTID_READY, + BRCMF_MSGBUF_PKTID_CLAIMED)) { case BRCMF_MSGBUF_PKTID_READY: break; case BRCMF_MSGBUF_PKTID_RESERVED: @@ -736,14 +779,17 @@ brcmf_msgbuf_get_pktid(struct brcmf_msgbuf_pktids *pktids, u32 idx) brcmf_err("packet id %d: completion before the slot was posted\n", idx); return NULL; + case BRCMF_MSGBUF_PKTID_CLAIMED: + brcmf_err("packet id %d: already being completed\n", idx); + return NULL; default: brcmf_err("Invalid packet id %d (not in use)\n", idx); return NULL; } /* - * Read everything out of the slot before it is released: once - * allocated drops to FREE the TX worker may claim the slot again and + * The slot is ours now (CLAIMED). Read everything out of it before it + * is released: once allocated drops to FREE the TX worker may claim the slot again and * overwrite it. */ skb = pktid->skb; @@ -1195,7 +1241,8 @@ static void brcmf_msgbuf_txflow(struct brcmf_msgbuf *msgbuf, u16 flowid) } ret_ptr = brcmf_commonring_reserve_for_write(commonring); if (!ret_ptr) { - brcmf_msgbuf_get_pktid(msgbuf->tx_pktids, pktid); + /* Not posted: the slot is still RESERVED, skb is ours. */ + brcmf_msgbuf_unclaim_tx_pktid(msgbuf->tx_pktids, pktid); brcmf_flowring_reinsert(flow, flowid, skb); break; } @@ -1220,6 +1267,8 @@ static void brcmf_msgbuf_txflow(struct brcmf_msgbuf *msgbuf, u16 flowid) tx_msghdr->metadata_buf_addr.high_addr = 0; tx_msghdr->metadata_buf_addr.low_addr = 0; atomic_inc(&commonring->outstanding_tx); + /* skb is not touched after this: a completion may free it. */ + brcmf_msgbuf_post_tx_pktid(msgbuf->tx_pktids, pktid); if (count >= BRCMF_MSGBUF_TX_FLUSH_CNT2) { brcmf_commonring_write_complete(commonring); count = 0; diff --git a/tools/asahi/brcmf-msgbuf-pktid-tests.py b/tools/asahi/brcmf-msgbuf-pktid-tests.py index 0b843b529fab1..50e7201a60a51 100755 --- a/tools/asahi/brcmf-msgbuf-pktid-tests.py +++ b/tools/asahi/brcmf-msgbuf-pktid-tests.py @@ -9,6 +9,16 @@ of READY; and that a completion arriving while the slot is still RESERVED (between claim and publish) neither returns a frame nor frees the previous one, and leaves the slot and its buffer with their owner. + +A TX slot stays RESERVED until the TX worker has a ring slot for it. The +ring-full rollback gives the slot back without a completion, so a stray +completion during that window (before or inside the rollback) gets nothing +and the worker keeps its skb. A completion claims a READY slot with a +cmpxchg to CLAIMED, so two completions for one id, interleaved at the +buffer release, hand the skb out once. The TX worker's post/rollback step is +modelled in the harness; the slot functions are the production ones. A +threaded stress then runs the rollback and the post against completions on +other CPUs with real atomics. """ import argparse import json @@ -54,19 +64,26 @@ def struct(source, name): #include #include #include +#include typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t dma_addr_t; #define CHECK(c) do { if (!(c)) { fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #c); abort(); } } while (0) #define container_of(p,t,m) ((t *)((char *)(p)-offsetof(t,m))) #define min(a,b) ((a) < (b) ? (a) : (b)) -#define brcmf_err(...) (errors++) -#define WARN_ON_ONCE(c) ((c) ? (warns++, 1) : 0) +#define brcmf_err(...) (__atomic_fetch_add(&errors, 1, __ATOMIC_RELAXED)) +#define WARN_ON_ONCE(c) ((c) ? (__atomic_fetch_add(&warns, 1, __ATOMIC_RELAXED), 1) : 0) #define BITS_PER_LONG 64 static int errors, warns; enum dma_data_direction { DMA_BIDIRECTIONAL, DMA_TO_DEVICE, DMA_FROM_DEVICE }; struct device { int unused; }; +/* One real lock for the pools; the hook runs as another CPU could, just + * before a buffer goes back (inside a completion or the rollback). */ typedef struct { int unused; } spinlock_t; -#define spin_lock_irqsave(l,f) ((void)(l),(f)=0) -#define spin_unlock_irqrestore(l,f) ((void)(l),(void)(f)) +static pthread_mutex_t pool_mutex = PTHREAD_MUTEX_INITIALIZER; +static void (*at_lock)(void); +static void lock_pool(void) +{ if (at_lock) { void (*f)(void) = at_lock; at_lock = NULL; f(); } pthread_mutex_lock(&pool_mutex); } +#define spin_lock_irqsave(l,f) ((void)(l),(f)=0,lock_pool()) +#define spin_unlock_irqrestore(l,f) ((void)(l),(void)(f),pthread_mutex_unlock(&pool_mutex)) static void __set_bit(u32 b, unsigned long *p) { p[b / BITS_PER_LONG] |= 1UL << (b % BITS_PER_LONG); } static bool __test_and_clear_bit(u32 b, unsigned long *p) { bool was = p[b / BITS_PER_LONG] >> (b % BITS_PER_LONG) & 1; p[b / BITS_PER_LONG] &= ~(1UL << (b % BITS_PER_LONG)); return was; } @@ -79,16 +96,24 @@ def struct(source, name): static struct sk_buff *brcmu_pkt_buf_get_skb(u32 len) { struct sk_buff *s = &rx_skbs[rx_next]; s->data = rx_mem[rx_next++]; s->len = len; return s; } -/* Atomics: every access is observed. */ +/* Atomics: every access is observed (and real, for the threaded stress). */ typedef struct { int counter; } atomic_t; -static bool in_completion; -static int releases, acquires; +static _Thread_local bool in_completion; +static _Thread_local int acquires; +static int releases; static void on_publish(atomic_t *a); -static int atomic_read(const atomic_t *a) { CHECK(!in_completion); return a->counter; } -static int atomic_read_acquire(const atomic_t *a) { acquires++; return a->counter; } +static int atomic_read(const atomic_t *a) +{ CHECK(!in_completion); return __atomic_load_n(&a->counter, __ATOMIC_RELAXED); } +static int atomic_read_acquire(const atomic_t *a) +{ acquires++; return __atomic_load_n(&a->counter, __ATOMIC_ACQUIRE); } static void atomic_set(atomic_t *a, int v) { (void)a; (void)v; CHECK(!"plain store to a slot state"); } -static void atomic_set_release(atomic_t *a, int v) { a->counter = v; releases++; if (v == 2) on_publish(a); } -static int atomic_cmpxchg(atomic_t *a, int o, int n) { int c = a->counter; if (c == o) a->counter = n; return c; } +static void atomic_set_release(atomic_t *a, int v) +{ if (v == 2) on_publish(a); __atomic_store_n(&a->counter, v, __ATOMIC_RELEASE); + __atomic_fetch_add(&releases, 1, __ATOMIC_RELAXED); } +static int atomic_cmpxchg(atomic_t *a, int o, int n) +{ __atomic_compare_exchange_n(&a->counter, &o, n, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); return o; } +static int atomic_cmpxchg_acquire(atomic_t *a, int o, int n) +{ acquires++; __atomic_compare_exchange_n(&a->counter, &o, n, false, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE); return o; } /* DMA: the hook runs between claim and publish, as an IRQ could. */ static void (*mid_fill)(void); @@ -124,9 +149,13 @@ def struct(source, name): /* A slot is published READY only when every field the completion reads is in. */ static struct sk_buff *expect_skb; +static bool threaded; static void on_publish(atomic_t *a) { struct brcmf_msgbuf_pktid *p = container_of(a, struct brcmf_msgbuf_pktid, allocated); + if (threaded) + return; + CHECK(p->allocated.counter == BRCMF_MSGBUF_PKTID_RESERVED); CHECK(p->pool == &pool); CHECK(p->physaddr == brcmf_msgbuf_buf_dma(&pool, p->buf)); CHECK(test_bit(p->buf, pool.busy)); @@ -137,9 +166,10 @@ def struct(source, name): { struct sk_buff *s; int acq = acquires; + bool was = in_completion; /* a nested one models another CPU */ in_completion = true; s = brcmf_msgbuf_get_pktid(&ids, idx); - in_completion = false; + in_completion = was; CHECK(acquires == acq + 1); return s; } @@ -148,9 +178,10 @@ def struct(source, name): { struct sk_buff *s; int acq = acquires; + bool was = in_completion; /* a nested one models another CPU */ in_completion = true; s = brcmf_msgbuf_get_rx_pktid(&ids, idx, 0, 8); - in_completion = false; + in_completion = was; CHECK(acquires == acq + 1); return s; } @@ -171,6 +202,113 @@ def struct(source, name): CHECK(pool.nr_free == nr_free); } +/* + * The TX worker's step in brcmf_msgbuf_txflow(): fill a slot, then take a + * ring slot. at_reserve runs where the ring reservation would; on a full + * ring the slot goes back and the worker keeps (reinserts) the skb. + */ +static void (*at_reserve)(void); +static int tx_step(struct sk_buff *skb, u16 off, bool ring_full, u32 *idx) +{ + dma_addr_t phys; + int ret = brcmf_msgbuf_alloc_tx_pktid(&ids, &pool, skb, off, &phys, idx); + + if (ret) + return ret; + if (at_reserve) { void (*f)(void) = at_reserve; at_reserve = NULL; f(); } + if (ring_full) { + brcmf_msgbuf_unclaim_tx_pktid(&ids, *idx); + CHECK(!skb->freed); /* reinserted: still the worker's */ + return -ENOSPC; + } + brcmf_msgbuf_post_tx_pktid(&ids, *idx); + return 0; +} + +/* A completion for the slot just filled, before the ring answers. */ +static void stray_at_reserve(void) +{ + u32 nr_free = pool.nr_free; + int acq = acquires; + stray_calls++; + CHECK(slots[filling].allocated.counter == BRCMF_MSGBUF_PKTID_RESERVED); + CHECK(complete_tx(filling) == NULL); + acquires = acq; + CHECK(slots[filling].allocated.counter == BRCMF_MSGBUF_PKTID_RESERVED); + CHECK(pool.nr_free == nr_free); +} + +/* Another completion for the id a completion is already releasing. */ +static void second_completion(void) +{ + int acq = acquires; + stray_calls++; + stray_result = complete_tx(filling); + acquires = acq; /* counted against the other CPU */ +} + +/* Threaded stress: a TX worker against two completion CPUs. */ +#define ROUNDS 20000 +static struct sk_buff stress_skbs[ROUNDS]; +static u8 stress_mem[16]; +static int stress_round, stress_done, stress_got[ROUNDS]; +static int stress_full; + +static void *stress_completion(void *arg) +{ + (void)arg; + while (!__atomic_load_n(&stress_done, __ATOMIC_ACQUIRE)) { + for (u32 i = 0; i < 4; i++) { + struct sk_buff *s; + in_completion = true; + s = brcmf_msgbuf_get_pktid(&ids, i); + in_completion = false; + if (s) { + CHECK(!stress_full); + CHECK(!s->freed); + s->freed = 1; + __atomic_fetch_add(&stress_got[s - stress_skbs], 1, __ATOMIC_RELAXED); + } + } + } + return NULL; +} + +static void stress(bool ring_full) +{ + pthread_t t[2]; + u32 idx; + + setup(); + threaded = true; + stress_full = ring_full; + stress_done = 0; + memset(stress_skbs, 0, sizeof(stress_skbs)); + memset(stress_got, 0, sizeof(stress_got)); + for (int i = 0; i < 2; i++) + CHECK(pthread_create(&t[i], NULL, stress_completion, NULL) == 0); + for (stress_round = 0; stress_round < ROUNDS; stress_round++) { + struct sk_buff *s = &stress_skbs[stress_round]; + s->len = 16; s->data = stress_mem; + while (tx_step(s, 0, ring_full, &idx) == -ENOMEM) + ; /* all slots in flight: wait for completions */ + } + if (!ring_full) /* let the last ones complete */ + while (__atomic_load_n(&pool.nr_free, __ATOMIC_ACQUIRE) != NBUF) + ; + __atomic_store_n(&stress_done, 1, __ATOMIC_RELEASE); + for (int i = 0; i < 2; i++) + pthread_join(t[i], NULL); + threaded = false; + for (int i = 0; i < ROUNDS; i++) { + CHECK(stress_got[i] == !ring_full); + CHECK(stress_skbs[i].freed == !ring_full); + } + CHECK(pool.nr_free == NBUF); + for (int i = 0; i < 4; i++) + CHECK(slots[i].allocated.counter == BRCMF_MSGBUF_PKTID_FREE); +} + static void stray_rx_completion(void) { struct brcmf_msgbuf_pktid *p = &slots[filling]; @@ -193,6 +331,9 @@ def struct(source, name): setup(); expect_skb = &a; CHECK(brcmf_msgbuf_alloc_tx_pktid(&ids, &pool, &a, 4, &phys, &idx) == 0); + /* Not published until the worker has its ring slot. */ + CHECK(slots[idx].allocated.counter == BRCMF_MSGBUF_PKTID_RESERVED); + brcmf_msgbuf_post_tx_pktid(&ids, idx); CHECK(slots[idx].allocated.counter == BRCMF_MSGBUF_PKTID_READY); CHECK(!memcmp(chunk[slots[idx].buf], payload + 4, 12)); CHECK(complete_tx(idx) == &a); @@ -212,15 +353,80 @@ def struct(source, name): warns = 0; CHECK(brcmf_msgbuf_alloc_tx_pktid(&ids, &pool, &b, 0, &phys, &idx2) == 0); CHECK(idx2 == idx && stray_calls == 1 && warns == 1); + CHECK(slots[idx].allocated.counter == BRCMF_MSGBUF_PKTID_RESERVED); + brcmf_msgbuf_post_tx_pktid(&ids, idx); CHECK(slots[idx].allocated.counter == BRCMF_MSGBUF_PKTID_READY); /* The real completion then returns b exactly once. */ CHECK(complete_tx(idx) == &b); CHECK(pool.nr_free == NBUF && slots[idx].allocated.counter == BRCMF_MSGBUF_PKTID_FREE); + /* Ring full, stray completion before the rollback: the slot is still + * RESERVED, the completion gets nothing, the worker keeps a. */ + setup(); + a.freed = 0; expect_skb = &a; stray_calls = 0; warns = 0; + filling = (ids.last_allocated_idx + 1) % 4; + at_reserve = stray_at_reserve; + CHECK(tx_step(&a, 0, true, &idx) == -ENOSPC); + CHECK(idx == filling && stray_calls == 1 && warns == 1 && !a.freed); + CHECK(slots[idx].allocated.counter == BRCMF_MSGBUF_PKTID_FREE); + CHECK(slots[idx].skb == NULL && pool.nr_free == NBUF); + + /* Ring full, stray completion inside the rollback, as its buffer goes + * back: still nothing for the completion. */ + stray_calls = 0; warns = 0; + ids.last_allocated_idx = idx ? idx - 1 : 3; + at_lock = NULL; + { + dma_addr_t p2; + CHECK(brcmf_msgbuf_alloc_tx_pktid(&ids, &pool, &a, 0, &p2, &idx2) == 0); + CHECK(idx2 == idx); + at_lock = stray_at_reserve; + brcmf_msgbuf_unclaim_tx_pktid(&ids, idx); + } + CHECK(stray_calls == 1 && warns == 1 && !a.freed); + CHECK(slots[idx].allocated.counter == BRCMF_MSGBUF_PKTID_FREE && pool.nr_free == NBUF); + /* The skb is posted again later and completes exactly once. */ + CHECK(tx_step(&a, 0, false, &idx) == 0); + CHECK(complete_tx(idx) == &a && pool.nr_free == NBUF); + + /* Two completions for one posted id, the second arriving while the + * first releases the buffer: only one gets the skb. */ + setup(); + b.freed = 0; expect_skb = &b; stray_calls = 0; errors = 0; + CHECK(tx_step(&b, 0, false, &idx) == 0); + filling = idx; + stray_result = &a; + at_lock = second_completion; + CHECK(complete_tx(idx) == &b); + CHECK(stray_calls == 1 && stray_result == NULL && errors == 1); + CHECK(pool.nr_free == NBUF && slots[idx].allocated.counter == BRCMF_MSGBUF_PKTID_FREE); + /* The same for an RX slot. */ + ids.direction = DMA_FROM_DEVICE; + expect_skb = NULL; + CHECK(brcmf_msgbuf_alloc_rx_pktid(&ids, &pool, 0, &phys, &idx) == 0); + { + int e = errors; + stray_calls = 0; + at_lock = NULL; + /* Hold the slot CLAIMED as a first completion would. */ + CHECK(atomic_cmpxchg(&slots[idx].allocated, BRCMF_MSGBUF_PKTID_READY, + BRCMF_MSGBUF_PKTID_CLAIMED) == BRCMF_MSGBUF_PKTID_READY); + CHECK(complete_rx(idx) == NULL && errors == e + 1); + slots[idx].allocated.counter = BRCMF_MSGBUF_PKTID_READY; + CHECK(complete_rx(idx) != NULL && pool.nr_free == NBUF); + } + ids.direction = DMA_TO_DEVICE; + + /* Real CPUs: rollbacks against completions, then posts against two + * completion CPUs. */ + stress(true); + stress(false); + /* Teardown frees only published frames, never a reserved slot's. */ setup(); expect_skb = &a; a.freed = 0; CHECK(brcmf_msgbuf_alloc_tx_pktid(&ids, &pool, &a, 0, &phys, &idx) == 0); + brcmf_msgbuf_post_tx_pktid(&ids, idx); CHECK(brcmf_msgbuf_claim_pktid(&ids, &idx2) == 0 && idx2 != idx); slots[idx2].skb = &b; b.freed = 0; /* stale pointer in a reserved slot */ skb_frees = 0; @@ -243,9 +449,10 @@ def struct(source, name): CHECK(slots[idx].allocated.counter == BRCMF_MSGBUF_PKTID_FREE && pool.nr_free == NBUF); ids.last_allocated_idx = idx ? idx - 1 : 3; filling = idx; + stray_calls = 0; mid_fill = stray_rx_completion; CHECK(brcmf_msgbuf_alloc_rx_pktid(&ids, &pool, 2, &phys, &idx2) == 0); - CHECK(idx2 == idx && stray_calls == 2); + CHECK(idx2 == idx && stray_calls == 1); CHECK(complete_rx(idx) != NULL && pool.nr_free == NBUF); printf("msgbuf pktid: all cases passed (%d release, %d acquire)\n", releases, acquires); @@ -255,7 +462,8 @@ def struct(source, name): NAMES = ['brcmf_msgbuf_claim_pktid', 'brcmf_msgbuf_buf_dma', 'brcmf_msgbuf_buf_cpu', 'brcmf_msgbuf_buf_get', 'brcmf_msgbuf_buf_put', 'brcmf_msgbuf_alloc_rx_pktid', - 'brcmf_msgbuf_alloc_tx_pktid', 'brcmf_msgbuf_get_rx_pktid', 'brcmf_msgbuf_get_pktid'] + 'brcmf_msgbuf_alloc_tx_pktid', 'brcmf_msgbuf_post_tx_pktid', + 'brcmf_msgbuf_unclaim_tx_pktid', 'brcmf_msgbuf_get_rx_pktid', 'brcmf_msgbuf_get_pktid'] MUTATIONS = { # Publish at claim time, before the fields are written. @@ -267,8 +475,18 @@ def struct(source, name): 'plain-read': ('\tskb = pktid->skb;\n\tpool = pktid->pool;', '\tskb = pktid->skb;\n\t(void)atomic_read(&pktid->allocated);\n\tpool = pktid->pool;'), # Publish the TX slot before its skb is stored. - 'publish-before-skb': ('\tpktid->skb = skb;\n\tpktid->pool = pool;\n\tpktid->buf = buf;\n\t/* Publish', - '\tatomic_set_release(&pktid->allocated, BRCMF_MSGBUF_PKTID_READY);\n\tpktid->skb = skb;\n\tpktid->pool = pool;\n\tpktid->buf = buf;\n\t/* Publish'), + 'publish-before-skb': ('\tpktid->skb = skb;\n\tpktid->pool = pool;\n\tpktid->buf = buf;\n\n\treturn 0;', + '\tatomic_set_release(&pktid->allocated, BRCMF_MSGBUF_PKTID_READY);\n\tpktid->skb = skb;\n\tpktid->pool = pool;\n\tpktid->buf = buf;\n\n\treturn 0;'), + # Publish the TX slot when it is filled, before the ring slot is taken + # (the ring-full rollback then races a completion for the skb). + 'publish-before-reserve': ('\tpktid->buf = buf;\n\n\treturn 0;', + '\tpktid->buf = buf;\n\tatomic_set_release(&pktid->allocated, BRCMF_MSGBUF_PKTID_READY);\n\n\treturn 0;'), + # Check READY without claiming it: two completions both take the slot. + 'no-claim': ('atomic_cmpxchg_acquire(&pktid->allocated,\n\t\t\t\t BRCMF_MSGBUF_PKTID_READY,\n' + '\t\t\t\t BRCMF_MSGBUF_PKTID_CLAIMED)) {\n\tcase BRCMF_MSGBUF_PKTID_READY:\n\t\tbreak;\n' + '\tcase BRCMF_MSGBUF_PKTID_RESERVED:\n\t\t/*', + 'atomic_read_acquire(&pktid->allocated)) {\n\tcase BRCMF_MSGBUF_PKTID_READY:\n\t\tbreak;\n' + '\tcase BRCMF_MSGBUF_PKTID_RESERVED:\n\t\t/*'), # Teardown frees whatever a claimed slot points at. 'release-reserved': ('BRCMF_MSGBUF_PKTID_READY)\n\t\t\tbrcmu_pkt_buf_free_skb', 'BRCMF_MSGBUF_PKTID_READY || 1)\n\t\t\tbrcmu_pkt_buf_free_skb'), @@ -283,7 +501,7 @@ def main(): source = (DRIVER / 'msgbuf.c').read_text() states = ''.join(re.findall(r'^#define BRCMF_MSGBUF_PKTID_\w+\s+\d+\n', source, re.M)) - if len(states.splitlines()) != 3: + if len(states.splitlines()) != 4: raise SystemExit('missing BRCMF_MSGBUF_PKTID_* states') structs = ''.join(struct(source, n) for n in ('brcmf_msgbuf_pool', 'brcmf_msgbuf_pktid', 'brcmf_msgbuf_pktids')) @@ -304,13 +522,15 @@ def run(out): src = out / 'pktid.c' src.write_text(code) exe = out / 'pktid' - subprocess.run(['clang', '-std=gnu11', '-O1', '-g', '-Wall', '-Wno-unused-function', + subprocess.run(['clang', '-std=gnu11', '-O1', '-g', '-Wall', '-Wno-unused-function', '-pthread', '-fsanitize=address,undefined', '-fno-sanitize-recover=all', str(src), '-o', str(exe)], check=True) result = subprocess.run([str(exe)], capture_output=True, text=True) receipt = {'exit': result.returncode, 'stdout': result.stdout, 'stderr': result.stderr, - 'limits': 'Observing host stubs, single-threaded; the IRQ is modelled at the ' - 'DMA sync inside the fill, not as a concurrent CPU.'} + 'limits': 'Observing host stubs. The ordered cases model the IRQ at the DMA ' + 'sync inside the fill, at the ring reservation and at the buffer ' + 'release; the stress runs real threads on the host CPU, and the TX ' + 'worker step is a model of brcmf_msgbuf_txflow().'} (out / 'results.json').write_text(json.dumps(receipt, indent=2) + '\n') print(json.dumps(receipt)) return result.returncode diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index 42793cb2d9788..ed7ce9aa8e228 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -181,7 +181,8 @@ def mutations(flag, names, base, expect=KILL): + [(out() + ['--ref', '33bec4016c466985bfbb5ff4c2089fcab8242dcf'], KILL)], 'brcmf-msgbuf-pktid-tests.py': [(out(), PASS)] + mutations('--mutation', ('publish-at-claim', 'reserved-is-ready', 'plain-read', - 'publish-before-skb', 'release-reserved'), out()), + 'publish-before-skb', 'release-reserved', + 'publish-before-reserve', 'no-claim'), out()), 'brcmf-pcie-dma-tests.py': [(out('--output'), PASS)] + mutations('--mutation', ('narrow-stream', 'ignore-aperture', 'revision-wide'), out('--output')), 'brcmf-recovery-tests.py': [(out(), PASS)]