Skip to content

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

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

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

Conversation

@d3zd3z

@d3zd3z d3zd3z commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Backport of the fix from #93033 to v4.4-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>
@d3zd3z d3zd3z added the Backport Backport PR and backport failure issues label Aug 17, 2026
Copilot AI lite review requested due to automatic review settings August 17, 2026 20:35
@d3zd3z d3zd3z added the Backport Backport PR and backport failure issues label Aug 17, 2026
@d3zd3z
d3zd3z requested review from Thalley, alxelax and jhedberg August 17, 2026 20:35
@github-project-automation github-project-automation Bot moved this to To do in Backports Aug 17, 2026
@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

This backport aligns the v4.4 Bluetooth Host with upstream behavior by introducing an unconditional, general-purpose Bluetooth RX workqueue and routing selected internal Host work items onto it to reduce interference with the shared system workqueue and to improve teardown/cancellation correctness.

Changes:

  • Introduces bt_work_submit/schedule/reschedule() helpers and a dedicated Bluetooth workqueue that is started once and kept alive across enable/disable cycles.
  • Migrates multiple Host internal work items (immediate and delayable) from the system workqueue to the Bluetooth workqueue and updates teardown behavior in bt_disable().
  • Removes CONFIG_BT_RECV_CONTEXT / CONFIG_BT_RECV_WORKQ_{SYS,BT} configuration paths and updates tests/samples/docs accordingly.

Reviewed changes

Copilot reviewed 28 out of 28 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/bsim/bluetooth/host/misc/conn_stress/central/prj.conf Removes obsolete BT RX workqueue selection config/comments.
tests/bsim/bluetooth/host/l2cap/stress/tests_scripts/l2cap_syswq.sh Deletes syswq-specific stress runner script now that selection is removed.
tests/bsim/bluetooth/host/l2cap/stress/testcase.yaml Removes syswq-specific stress testcase entry.
tests/bsim/bluetooth/host/l2cap/stress/overlay-syswq.conf Removes syswq overlay config that no longer applies.
tests/bluetooth/init/testcase.yaml Removes a test case that toggled the removed BT RX context option.
tests/bluetooth/host/id/mocks/kernel.h Updates unit-test fakes to match new work submission/scheduling usage.
tests/bluetooth/host/id/mocks/kernel.c Updates unit-test fake definitions to match header changes.
tests/bluetooth/host/id/mocks/hci_core.h Adds fake for bt_work_schedule() used by the Host after migration.
tests/bluetooth/host/id/mocks/hci_core.c Adds fake implementation for bt_work_schedule().
tests/bluetooth/host/conn/mocks/kernel.h Drops fakes for k_work_{schedule,reschedule} that are no longer used by the migrated codepaths.
tests/bluetooth/host/conn/mocks/kernel.c Drops matching fake definitions for removed kernel work scheduling fakes.
tests/bluetooth/host/conn/mocks/hci_core.h Adds fakes for bt_work_{submit,schedule,reschedule} for migrated Host paths.
tests/bluetooth/host/conn/mocks/hci_core.c Adds fake implementations for bt_work_{submit,schedule,reschedule}.
subsys/bluetooth/host/smp.c Moves SMP timeout rescheduling onto the Bluetooth workqueue helper.
subsys/bluetooth/host/settings.c Moves settings store work items onto the Bluetooth workqueue helper.
subsys/bluetooth/host/l2cap.c Routes RX/RTX-related work onto Bluetooth workqueue; adds RX work cancellation in channel destroy.
subsys/bluetooth/host/Kconfig Removes CONFIG_BT_RECV_CONTEXT choice/options.
subsys/bluetooth/host/id.c Migrates RPA update scheduling to bt_work_schedule().
subsys/bluetooth/host/hci_core.h Declares bt_work_* helper APIs for internal Host use.
subsys/bluetooth/host/hci_core.c Implements Bluetooth workqueue and helpers; updates RX queue handling and bt_disable() teardown.
subsys/bluetooth/host/gatt.c Migrates several GATT delayed works to bt_work_{schedule,reschedule}.
subsys/bluetooth/host/conn.c Moves connection deferred work submission/scheduling to Bluetooth workqueue helpers.
subsys/bluetooth/host/att.c Migrates ATT/EATT delayable rescheduling to Bluetooth workqueue helpers.
subsys/bluetooth/host/adv.c Migrates limited advertising timeout rescheduling to Bluetooth workqueue helpers.
samples/bluetooth/observer/sample.yaml Adjusts test config to accommodate the always-on dedicated Bluetooth RX thread on micro:bit.
samples/bluetooth/observer/overlay_bbc_microbit-bt_ll_sw_split.conf Rebalances stack sizes (ISR/syswq/BT RX) for micro:bit RAM constraints.
samples/bluetooth/mesh_demo/boards/bbc_microbit.conf Adjusts stack sizes and buffer counts to fit with dedicated BT RX thread.
include/zephyr/bluetooth/gatt.h Updates documentation to reflect callbacks now running in Bluetooth RX thread context.

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

Comment on lines +1376 to +1380
/* 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);

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