Skip to content

[Backport v4.3-branch] Bluetooth: Host: Introduce a general purpose Bluetooth workqueue - #116535

Open
d3zd3z wants to merge 4 commits into
v4.3-branchfrom
backport-93033-to-v4.3-branch
Open

[Backport v4.3-branch] Bluetooth: Host: Introduce a general purpose Bluetooth workqueue#116535
d3zd3z wants to merge 4 commits into
v4.3-branchfrom
backport-93033-to-v4.3-branch

Conversation

@d3zd3z

@d3zd3z d3zd3z commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Backport of the fix from #93033 to v4.3-branch.

Fixes: #116523

Remove the BT_RECV_CONTEXT Kconfig choice and its BT_RECV_WORKQ_SYS
and BT_RECV_WORKQ_BT options. Low-priority HCI packets now always run
on the dedicated Bluetooth RX workqueue.

BT_RECV_WORKQ_SYS existed to save the RX thread stack on
memory-constrained targets, primarily nRF51. Removing the alternate path
simplifies the host threading model and removes a configuration-dependent
RX context and its associated test matrix. The following commit retunes
the affected in-tree nRF51 sample configurations.

This breaking Kconfig change is tracked by RFC #113007. Update the public
GATT callback context documentation, release notes and migration guide,
and remove tests specific to the system-workqueue RX path.

[davidb: dropped the 4.5 release notes and migration guide entries, which
this branch does not carry, and applied the twister removals to the
testcase.yaml files, which is what these test lists are named here]

Assisted-by: Claude:claude-fable-5
Assisted-By: Claude:opus-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
(cherry picked from commit 3b379ec)
Signed-off-by: David Brown <david.brown@linaro.org>
With the removal of the BT_RECV_CONTEXT choice, nRF51 no longer
processes low-priority HCI packets on the system workqueue; a dedicated
Bluetooth RX thread is now always created. Adapt the bbc_microbit
configurations built in CI to fit the 16 KB SRAM:

- Size the RX thread through CONFIG_BT_RX_STACK_SIZE instead of relying
  on the system workqueue stack.
- Shrink CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE, since low-priority RX no
  longer runs on it.
- Preserve the controller ISR stack requirements and trim other options
  based on measured need.

[davidb: applied the observer twister change to sample.yaml, which is
what that test list is named on this branch]

Assisted-by: Claude:claude-fable-5
Assisted-By: Claude:opus-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
(cherry picked from commit 73c16a3)
Signed-off-by: David Brown <david.brown@linaro.org>
Add bt_work_submit(), bt_work_schedule() and bt_work_reschedule()
helpers that submit internal host work to the dedicated Bluetooth RX
workqueue instead of the shared system workqueue.

Start the workqueue on the first bt_enable() call and keep it alive for
the lifetime of the system. Delayable host work can outlive an individual
Bluetooth enable/disable cycle, so aborting and reinitializing the queue
could strand work item state.

The RX thread abort that bt_disable() used to perform is replaced with a
graceful equivalent: before resetting and closing the transport,
bt_disable() now stops queuing new low-priority HCI packets, discards
the ones already queued, and cancels the RX work synchronously. An
in-flight RX work item is thereby waited for while the transport is
still able to serve any HCI commands it may issue, and a packet that
slips into the queue during the teardown is dropped by the handler
instead of being dispatched after the transport is gone. Unlike the
abort, this cannot terminate the RX handler mid-execution with a buffer
still referenced.

The RX workqueue and dedicated TX processor thread are intentionally kept
separate. Merging them would allow a blocking application callback in the
RX context to stall command processing and reintroduce the deadlock the
TX processor thread was created to avoid.

Subsequent commits migrate selected delayed and immediate host work items
to these helpers.

Assisted-by: Claude:claude-fable-5
Assisted-By: Claude:opus-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
(cherry picked from commit 9ef7acd)
Signed-off-by: David Brown <david.brown@linaro.org>
Migrate the LE host's internal work items from the system workqueue to the
dedicated Bluetooth workqueue by using the bt_work_schedule(),
bt_work_reschedule() and bt_work_submit() helpers.

This covers advertising (limited advertising timeout), ATT (channel
timeout, EATT connection work), connections (deferred work and
auto-initiated procedures), GATT (service changed, delayed store,
database hash, multiple notifications), identity (RPA update), L2CAP (RTX
and RX work), settings (ID and IRK store), and SMP (timeout).

Migrating the per-connection deferred work is what keeps the existing
teardown code correct: that work item performs the L2CAP channel and
profile teardown (including the ATT and SMP disconnected callbacks), and
the non-blocking k_work_cancel*() calls in those paths only reliably stop
a concurrently running work item when both run on the same workqueue.
Keeping teardown on the system workqueue while the canceled work items
run on the Bluetooth workqueue would turn those cancellations into races.
For the same reason the SMP timeout moves along with it.

Additionally, cancel a dynamic L2CAP channel's RX work in
l2cap_chan_destroy() so that a queued work item cannot be left behind
with a reference to a channel object that the application may free or
re-use after teardown.

Update the id and conn host unit test mocks accordingly: id.c now uses
bt_work_schedule() and conn.c uses bt_work_submit(), bt_work_schedule()
and bt_work_reschedule(), so add fakes for these to the respective
hci_core.c mocks (and drop the now-unused k_work_schedule() and
k_work_reschedule() fakes from the kernel mocks).

Assisted-by: Claude:claude-fable-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
(cherry picked from commit 22896cb)
Signed-off-by: David Brown <david.brown@linaro.org>
Copilot AI lite review requested due to automatic review settings August 17, 2026 20:36
@d3zd3z d3zd3z added the Backport Backport PR and backport failure issues label Aug 17, 2026
@github-project-automation github-project-automation Bot moved this to To do in Backports Aug 17, 2026
@d3zd3z
d3zd3z requested review from Thalley, alxelax and jhedberg August 17, 2026 20:36
@zephyrbot zephyrbot added area: Bluetooth area: Bluetooth Mesh area: Bluetooth Host Bluetooth Host (excluding BR/EDR) area: Samples Samples area: Tests Issues related to a particular existing or missing test platform: nRF BSIM Nordic Semiconductors, nRF BabbleSim labels Aug 17, 2026
@zephyrbot

Copy link
Copy Markdown

@ludvigsj

You have been identified as a likely reviewer for the code this pull request changes, but could not be added to its review request automatically. Please review it if you are able to.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Backport that introduces a dedicated, general-purpose Bluetooth Host workqueue (Bluetooth RX thread context) and migrates selected internal Host work items off the shared system workqueue to reduce interference between Bluetooth processing and unrelated system work.

Changes:

  • Introduce bt_work_submit/schedule/reschedule() helpers and route low-priority RX + selected Host work to the Bluetooth workqueue.
  • Remove CONFIG_BT_RECV_CONTEXT / CONFIG_BT_RECV_WORKQ_{SYS,BT} configuration options and update code/docs accordingly.
  • Update tests/samples to reflect the new always-on Bluetooth RX workqueue behavior and adjust memory/stack sizing where needed.

Reviewed changes

Copilot reviewed 28 out of 28 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/bsim/bluetooth/host/misc/conn_stress/central/prj.conf Removes now-obsolete BT RX workqueue selection config/comments.
tests/bsim/bluetooth/host/l2cap/stress/tests_scripts/l2cap_syswq.sh Removes syswq-specific stress runner script (deleted).
tests/bsim/bluetooth/host/l2cap/stress/testcase.yaml Drops syswq-specific stress testcase entry.
tests/bsim/bluetooth/host/l2cap/stress/overlay-syswq.conf Removes syswq overlay config (deleted).
tests/bluetooth/init/testcase.yaml Removes init coverage for CONFIG_BT_RECV_WORKQ_BT since option is removed.
tests/bluetooth/host/id/mocks/kernel.h Updates kernel fakes to match new work submission path.
tests/bluetooth/host/id/mocks/kernel.c Updates kernel fake definitions accordingly.
tests/bluetooth/host/id/mocks/hci_core.h Adds fakes for bt_work_schedule.
tests/bluetooth/host/id/mocks/hci_core.c Adds fake definition for bt_work_schedule.
tests/bluetooth/host/conn/mocks/kernel.h Removes fakes for k_work_{schedule,reschedule} no longer used by tested code.
tests/bluetooth/host/conn/mocks/kernel.c Removes fake definitions for k_work_{schedule,reschedule}.
tests/bluetooth/host/conn/mocks/hci_core.h Adds fakes for bt_work_{submit,schedule,reschedule}.
tests/bluetooth/host/conn/mocks/hci_core.c Adds fake definitions for bt_work_{submit,schedule,reschedule}.
subsys/bluetooth/host/smp.c Moves SMP timeout rescheduling onto Bluetooth workqueue helpers.
subsys/bluetooth/host/settings.c Routes selected settings store work to Bluetooth workqueue helpers.
subsys/bluetooth/host/l2cap.c Cancels/detours L2CAP work and routes RX work submission via bt_work_submit.
subsys/bluetooth/host/Kconfig Removes RX context selection Kconfig choice/options.
subsys/bluetooth/host/id.c Schedules RPA update using bt_work_schedule.
subsys/bluetooth/host/hci_core.h Declares the new bt_work_* helper APIs.
subsys/bluetooth/host/hci_core.c Implements Bluetooth workqueue + helpers; updates RX enqueue/teardown behavior.
subsys/bluetooth/host/gatt.c Migrates selected GATT delayed work onto bt_work_* helpers.
subsys/bluetooth/host/conn.c Routes connection deferred work and auto procedures onto Bluetooth workqueue helpers.
subsys/bluetooth/host/att.c Migrates ATT timeouts and EATT connect scheduling onto bt_work_reschedule.
subsys/bluetooth/host/adv.c Moves limited advertising timeout reschedule onto Bluetooth workqueue helpers.
samples/bluetooth/observer/sample.yaml Adjusts test configuration for bbc_microbit to fit thread analyzer + BT RX thread.
samples/bluetooth/observer/overlay_bbc_microbit-bt_ll_sw_split.conf Rebalances stack sizes for new RX thread presence and ISR usage.
samples/bluetooth/mesh_demo/boards/bbc_microbit.conf Adjusts stack sizes/buffer counts to accommodate new RX thread memory needs.
include/zephyr/bluetooth/gatt.h Updates API docs to reference Bluetooth RX thread instead of removed config context.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +1377 to +1381
/* Make sure the RX work is not left queued with a reference to a
* channel object that may be freed or re-used after this call.
*/
(void)k_work_cancel(&le_chan->rx_work);

Comment on lines 448 to 452
int bt_settings_store_id(void)
{
k_work_submit(&store_id_work);
bt_work_submit(&store_id_work);

return 0;
Comment on lines 473 to 477
int bt_settings_store_irk(void)
{
#if defined(CONFIG_BT_PRIVACY)
k_work_submit(&store_irk_work);
bt_work_submit(&store_irk_work);
#endif /* defined(CONFIG_BT_PRIVACY) */
Comment on lines +4725 to +4733
/* Stop low-priority RX processing before resetting and closing the
* transport: new packets are no longer queued (see
* rx_teardown_active()), already-queued ones are discarded here, and
* an in-flight RX work item is waited for while the transport is
* still able to serve any HCI commands it may issue. High-priority
* (RECV_PRIO) events are unaffected, as the HCI Reset below relies on
* them. The workqueue itself is kept running, since delayable host
* work may remain scheduled across an enable/disable cycle.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Bluetooth Host Bluetooth Host (excluding BR/EDR) area: Bluetooth Mesh area: Bluetooth area: Samples Samples area: Tests Issues related to a particular existing or missing test Backport Backport PR and backport failure issues platform: nRF BSIM Nordic Semiconductors, nRF BabbleSim

Projects

Status: To do

Development

Successfully merging this pull request may close these issues.

4 participants