From 00bbd46fb0479838c402605ebac00cecf12ed55c Mon Sep 17 00:00:00 2001 From: Morten Priess Date: Mon, 28 Dec 2020 13:29:35 +0100 Subject: [PATCH] Bluetooth: controller: Prevent ticker_job re-entrance It ticker operation is initiated from e.g. hci tx thread, there is a risk that the ticker job is run (without chaining) while it is already running, triggered via mayfly. To prevent this, the ticker instance job_guard is checked to prevent re-entrance. Clearing of the job_guard is moved down to complete all data manipulating operations before allowing job to run again. Signed-off-by: Morten Priess --- subsys/bluetooth/controller/ticker/ticker.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/subsys/bluetooth/controller/ticker/ticker.c b/subsys/bluetooth/controller/ticker/ticker.c index a7b62d00b71c0..ef48d5182925d 100644 --- a/subsys/bluetooth/controller/ticker/ticker.c +++ b/subsys/bluetooth/controller/ticker/ticker.c @@ -2276,11 +2276,19 @@ void ticker_job(void *param) DEBUG_TICKER_JOB(1); - /* Defer worker, as job is now running */ + /* Prevent running job if worker is still running */ if (instance->worker_trigger) { DEBUG_TICKER_JOB(0); return; } + + /* Prevent running job if already running (re-entrance) */ + if (instance->job_guard) { + DEBUG_TICKER_JOB(0); + return; + } + + /* Defer worker, as job is now running */ instance->job_guard = 1U; /* Back up the previous known tick */ @@ -2357,14 +2365,14 @@ void ticker_job(void *param) ticker_job_list_inquire(instance); } - /* Permit worker job to run */ - instance->job_guard = 0U; - /* update compare if head changed */ if (flag_compare_update) { ticker_job_compare_update(instance, ticker_id_old_head); } + /* Permit worker job to run */ + instance->job_guard = 0U; + /* trigger worker if deferred */ if (instance->worker_trigger) { instance->sched_cb(TICKER_CALL_ID_JOB, TICKER_CALL_ID_WORKER, 1,