Skip to content

Commit d5a127a

Browse files
alwa-nordicMaureenHelm
authored andcommitted
Bluetooth: host: sched-lock bt_recv()
`bt_recv` is invoked from the BT long work queue, which is preemptible. The host uses cooperative scheduling to ensure thread safety. Signed-off-by: Aleksander Wasaznik <[email protected]>
1 parent c684dab commit d5a127a

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

drivers/bluetooth/hci/ipc.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,7 @@ static void bt_ipc_rx(const uint8_t *data, size_t len)
229229

230230
if (buf) {
231231
LOG_DBG("Calling bt_recv(%p)", buf);
232-
233-
/* The IPC service does not guarantee that the handler thread
234-
* is cooperative. In particular, the OpenAMP implementation is
235-
* preemtible by default. OTOH, the HCI driver interface requires
236-
* that the bt_recv() function is called from a cooperative
237-
* thread.
238-
*
239-
* Calling `k_sched lock()` has the effect of making the current
240-
* thread cooperative.
241-
*/
242-
k_sched_lock();
243232
bt_recv(buf);
244-
k_sched_unlock();
245233

246234
LOG_HEXDUMP_DBG(buf->data, buf->len, "RX buf payload:");
247235
}

include/zephyr/drivers/bluetooth/hci_driver.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ static inline uint8_t bt_hci_evt_get_flags(uint8_t evt)
8989
* for so-called high priority HCI events, which should instead be delivered to
9090
* the host stack through bt_recv_prio().
9191
*
92-
* @note This function must only be called from a cooperative thread.
93-
*
9492
* @param buf Network buffer containing data from the controller.
9593
*
9694
* @return 0 on success or negative error number on failure.

subsys/bluetooth/host/hci_core.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3854,7 +3854,7 @@ static void rx_queue_put(struct net_buf *buf)
38543854
}
38553855
#endif /* !CONFIG_BT_RECV_BLOCKING */
38563856

3857-
int bt_recv(struct net_buf *buf)
3857+
static int bt_recv_unsafe(struct net_buf *buf)
38583858
{
38593859
bt_monitor_send(bt_monitor_opcode(buf), buf->data, buf->len);
38603860

@@ -3905,6 +3905,17 @@ int bt_recv(struct net_buf *buf)
39053905
}
39063906
}
39073907

3908+
int bt_recv(struct net_buf *buf)
3909+
{
3910+
int err;
3911+
3912+
k_sched_lock();
3913+
err = bt_recv_unsafe(buf);
3914+
k_sched_unlock();
3915+
3916+
return err;
3917+
}
3918+
39083919
int bt_recv_prio(struct net_buf *buf)
39093920
{
39103921
bt_monitor_send(bt_monitor_opcode(buf), buf->data, buf->len);

0 commit comments

Comments
 (0)