Skip to content

Commit 55c2d46

Browse files
Deomid rojer Ryabkovsjanc
authored andcommitted
nimble/host: Fix race in HCI ACL TX outstanding packets counter
The outstanding packets counter (bhc_outstanding_pkts / avail_pkts) was decremented after ble_hs_tx_data() returned. However, by that point the controller may have already processed the packet and sent a Number of Completed Packets event, which increments avail_pkts. This race can cause avail_pkts to momentarily exceed the controller maximum, leading to buffer overflows. Move the counter update before ble_hs_tx_data() and roll back on error. Forward-port of espressif@f5136d2b3
1 parent e49531b commit 55c2d46

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

nimble/host/src/ble_hs_hci.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,17 @@ ble_hs_hci_acl_tx_now(struct ble_hs_conn *conn, struct os_mbuf **om)
560560
BLE_HS_LOG(DEBUG, "\n");
561561
#endif
562562

563+
/* Account for the controller buf that will hold the txed fragment.
564+
* Do this before ble_hs_tx_data() to avoid a race with the Number of
565+
* Completed Packets event that may arrive before tx returns.
566+
*/
567+
conn->bhc_outstanding_pkts++;
568+
ble_hs_hci_avail_pkts--;
569+
563570
rc = ble_hs_tx_data(frag);
564571
if (rc != 0) {
572+
conn->bhc_outstanding_pkts--;
573+
ble_hs_hci_avail_pkts++;
565574
goto err;
566575
}
567576

@@ -570,10 +579,6 @@ ble_hs_hci_acl_tx_now(struct ble_hs_conn *conn, struct os_mbuf **om)
570579
*/
571580
conn->bhc_flags |= BLE_HS_CONN_F_TX_FRAG;
572581
pb = BLE_HCI_PB_MIDDLE;
573-
574-
/* Account for the controller buf that will hold the txed fragment. */
575-
conn->bhc_outstanding_pkts++;
576-
ble_hs_hci_avail_pkts--;
577582
}
578583

579584
if (txom != NULL) {

0 commit comments

Comments
 (0)