From 9a9bac14a395ff6fb418a579f085e9181088cd4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Rymanowski?= Date: Tue, 19 Feb 2019 08:29:33 +0100 Subject: [PATCH] nimble/ll: Fix race condition This patch fixes race condition when g_ble_ll_data.ll_rfclk_state is out of sync with nrf52_clock_hfxo_refcnt in nrf52_clock.c It could happen when HCI Reset has been done while scanning/advertising etc. This patch also adds OS_ASSERT_CRITICAL() in places where ll_rfclk_state is changed. --- nimble/controller/src/ble_ll.c | 2 ++ nimble/controller/src/ble_ll_xcvr.c | 3 +++ 2 files changed, 5 insertions(+) diff --git a/nimble/controller/src/ble_ll.c b/nimble/controller/src/ble_ll.c index 041416788..aaa2b7c61 100644 --- a/nimble/controller/src/ble_ll.c +++ b/nimble/controller/src/ble_ll.c @@ -1303,8 +1303,10 @@ ble_ll_reset(void) ble_ll_state_set(BLE_LL_STATE_STANDBY); #ifdef BLE_XCVR_RFCLK + OS_ENTER_CRITICAL(sr); /* Stops rf clock and rfclock timer */ ble_ll_xcvr_rfclk_stop(); + OS_EXIT_CRITICAL(sr); #endif /* Reset our random address */ diff --git a/nimble/controller/src/ble_ll_xcvr.c b/nimble/controller/src/ble_ll_xcvr.c index 66353cf7c..4446748a3 100644 --- a/nimble/controller/src/ble_ll_xcvr.c +++ b/nimble/controller/src/ble_ll_xcvr.c @@ -33,6 +33,7 @@ ble_ll_xcvr_rfclk_state(void) { uint32_t expiry; + OS_ASSERT_CRITICAL(); if (g_ble_ll_data.ll_rfclk_state == BLE_RFCLK_STATE_ON) { expiry = g_ble_ll_data.ll_rfclk_start_time; if ((int32_t)(os_cputime_get32() - expiry) > @@ -46,6 +47,7 @@ ble_ll_xcvr_rfclk_state(void) void ble_ll_xcvr_rfclk_enable(void) { + OS_ASSERT_CRITICAL(); if (g_ble_ll_data.ll_rfclk_state == BLE_RFCLK_STATE_OFF) { g_ble_ll_data.ll_rfclk_state = BLE_RFCLK_STATE_ON; ble_phy_rfclk_enable(); @@ -55,6 +57,7 @@ ble_ll_xcvr_rfclk_enable(void) void ble_ll_xcvr_rfclk_disable(void) { + OS_ASSERT_CRITICAL(); if (g_ble_ll_data.ll_rfclk_state != BLE_RFCLK_STATE_OFF) { ble_phy_rfclk_disable(); g_ble_ll_data.ll_rfclk_state = BLE_RFCLK_STATE_OFF;