From bb1e9f97e62047d631ee9b2cd581c8d954a49ba9 Mon Sep 17 00:00:00 2001 From: Bjarki Arge Andreasen Date: Sun, 23 Mar 2025 13:40:22 +0100 Subject: [PATCH 1/4] doc: kernel: serivces: threads: warn unsafe sys work queue use Add warning to workqueue docs, explaining that using the system work queue for blocking work can not be done safely. Signed-off-by: Bjarki Arge Andreasen --- doc/kernel/services/threads/workqueue.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/kernel/services/threads/workqueue.rst b/doc/kernel/services/threads/workqueue.rst index 18c1cf0e31a6..0af0288464f3 100644 --- a/doc/kernel/services/threads/workqueue.rst +++ b/doc/kernel/services/threads/workqueue.rst @@ -103,6 +103,12 @@ operations that are potentially blocking (e.g. taking a semaphore) must be used with care, since the workqueue cannot process subsequent work items in its queue until the handler function finishes executing. +.. warning:: + + The system workqueue can not safely be used to perform operations which are + potentially blocking, as there is no guarantee that work items submitted to + it do not depend on subsequent work items in the queue to unblock them. + The single argument that is passed to a handler function can be ignored if it is not required. If the handler function requires additional information about the work it is to perform, the work item can be embedded in a larger data From eb67bb0cfcb6281af07aa84d5d386ef8d6c6bbf0 Mon Sep 17 00:00:00 2001 From: Bjarki Arge Andreasen Date: Sun, 23 Mar 2025 20:11:53 +0100 Subject: [PATCH 2/4] tests: kernel: workq: work_queue: use k_busy_wait to sim work System workqueue items must not use blocking APIs, like k_msleep(). Replace k_msleep() with k_busy_wait() to both adhere to this rule, and to better emulate more realistic work being done by system workqueue. Signed-off-by: Bjarki Arge Andreasen --- tests/kernel/workq/work_queue/src/main.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/kernel/workq/work_queue/src/main.c b/tests/kernel/workq/work_queue/src/main.c index b14d28787171..671c74110261 100644 --- a/tests/kernel/workq/work_queue/src/main.c +++ b/tests/kernel/workq/work_queue/src/main.c @@ -32,6 +32,13 @@ LOG_MODULE_REGISTER(test); #define WORK_ITEM_WAIT_ALIGNED \ k_ticks_to_ms_floor64(k_ms_to_ticks_ceil32(WORK_ITEM_WAIT) + _TICK_ALIGN) +/* + * System work queue is not allowed to unready threads. k_busy_wait() is + * used to simulate work. It is higly inprecise, use way lower wait to + * account for this. + */ +#define WORK_ITEM_BUSY_WAIT ((WORK_ITEM_WAIT * USEC_PER_MSEC) / 4) + /* * Wait 50ms between work submissions, to ensure co-op and prempt * preempt thread submit alternatively. @@ -97,7 +104,7 @@ static void work_handler(struct k_work *work) CONTAINER_OF(dwork, struct delayed_test_item, work); LOG_DBG(" - Running test item %d", ti->key); - k_msleep(WORK_ITEM_WAIT); + k_busy_wait(WORK_ITEM_BUSY_WAIT); results[num_results++] = ti->key; } @@ -211,7 +218,7 @@ static void resubmit_work_handler(struct k_work *work) struct delayed_test_item *ti = CONTAINER_OF(dwork, struct delayed_test_item, work); - k_msleep(WORK_ITEM_WAIT); + k_busy_wait(WORK_ITEM_BUSY_WAIT); results[num_results++] = ti->key; From 949b6897e42a4b9ba94175c66f541a8c10404aa4 Mon Sep 17 00:00:00 2001 From: Bjarki Arge Andreasen Date: Sun, 23 Mar 2025 14:43:44 +0100 Subject: [PATCH 3/4] kernel: sys_workq: add k_is_in_sys_work API Add k_is_in_sys_work() API which returns true if the thread context is the system workqueue thread and the thread is currently servicing a work item. Useful for checking if blocking is safe or required in the case of calling an API which uses the system work queue internally. Signed-off-by: Bjarki Arge Andreasen --- include/zephyr/kernel.h | 13 +++++++++++++ kernel/work.c | 6 ++++++ subsys/bluetooth/host/att.c | 2 +- subsys/bluetooth/host/classic/hfp_ag.c | 2 +- subsys/bluetooth/host/conn.c | 3 +-- subsys/bluetooth/host/hci_core.c | 2 +- subsys/bluetooth/host/l2cap.c | 3 +-- subsys/input/input.c | 3 +-- tests/bluetooth/host/conn/mocks/kernel.c | 1 + tests/bluetooth/host/conn/mocks/kernel.h | 2 ++ 10 files changed, 28 insertions(+), 9 deletions(-) diff --git a/include/zephyr/kernel.h b/include/zephyr/kernel.h index b75813384b97..89667bede579 100644 --- a/include/zephyr/kernel.h +++ b/include/zephyr/kernel.h @@ -1181,6 +1181,19 @@ void k_thread_time_slice_set(struct k_thread *th, int32_t slice_ticks, */ bool k_is_in_isr(void); +/** + * @brief Determine if code is running from system work item + * + * This routine allows the caller to customize its actions, depending on + * whether it is running from a system workqueue item. + * + * @funcprops \isr_ok + * + * @return false if not invoked from a system workqueue item. + * @return true if invoked from a system workqueue item. + */ +bool k_is_in_sys_work(void); + /** * @brief Determine if code is running in a preemptible thread. * diff --git a/kernel/work.c b/kernel/work.c index 24691bd31096..b8e840b6ebfa 100644 --- a/kernel/work.c +++ b/kernel/work.c @@ -1175,3 +1175,9 @@ bool k_work_flush_delayable(struct k_work_delayable *dwork, } #endif /* CONFIG_SYS_CLOCK_EXISTS */ + +bool k_is_in_sys_work(void) +{ + return k_current_get() == k_work_queue_thread_get(&k_sys_work_q) && + flag_test(&k_sys_work_q.flags, K_WORK_QUEUE_BUSY_BIT); +} diff --git a/subsys/bluetooth/host/att.c b/subsys/bluetooth/host/att.c index b10a598fed44..aac3330e1a55 100644 --- a/subsys/bluetooth/host/att.c +++ b/subsys/bluetooth/host/att.c @@ -732,7 +732,7 @@ static struct net_buf *bt_att_chan_create_pdu(struct bt_att_chan *chan, uint8_t default: { k_tid_t current_thread = k_current_get(); - if (current_thread == k_work_queue_thread_get(&k_sys_work_q)) { + if (k_is_in_sys_work()) { /* No blocking in the sysqueue. */ timeout = K_NO_WAIT; } else if (current_thread == att_handle_rsp_thread) { diff --git a/subsys/bluetooth/host/classic/hfp_ag.c b/subsys/bluetooth/host/classic/hfp_ag.c index c9f92f1618cf..9d4d876f4664 100644 --- a/subsys/bluetooth/host/classic/hfp_ag.c +++ b/subsys/bluetooth/host/classic/hfp_ag.c @@ -215,7 +215,7 @@ static struct bt_ag_tx *bt_ag_tx_alloc(void) * so if we're in the same workqueue but there are no immediate * contexts available, there's no chance we'll get one by waiting. */ - if (k_current_get() == &k_sys_work_q.thread) { + if (k_is_in_sys_work()) { return k_fifo_get(&ag_tx_free, K_NO_WAIT); } diff --git a/subsys/bluetooth/host/conn.c b/subsys/bluetooth/host/conn.c index 0c6d8b70153f..acf5ef5ccb37 100644 --- a/subsys/bluetooth/host/conn.c +++ b/subsys/bluetooth/host/conn.c @@ -1606,8 +1606,7 @@ struct net_buf *bt_conn_create_pdu_timeout(struct net_buf_pool *pool, */ __ASSERT_NO_MSG(!k_is_in_isr()); - if (!K_TIMEOUT_EQ(timeout, K_NO_WAIT) && - k_current_get() == k_work_queue_thread_get(&k_sys_work_q)) { + if (!K_TIMEOUT_EQ(timeout, K_NO_WAIT) && k_is_in_sys_work()) { LOG_WRN("Timeout discarded. No blocking in syswq."); timeout = K_NO_WAIT; } diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index 919f65617be5..b41ec17f37e4 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -410,7 +410,7 @@ int bt_hci_cmd_send_sync(uint16_t opcode, struct net_buf *buf, /* Since the commands are now processed in the syswq, we cannot suspend * and wait. We have to send the command from the current context. */ - if (k_current_get() == &k_sys_work_q.thread) { + if (k_is_in_sys_work()) { /* drain the command queue until we get to send the command of interest. */ struct net_buf *cmd = NULL; diff --git a/subsys/bluetooth/host/l2cap.c b/subsys/bluetooth/host/l2cap.c index 34a6b556470e..6a98288f05ff 100644 --- a/subsys/bluetooth/host/l2cap.c +++ b/subsys/bluetooth/host/l2cap.c @@ -674,8 +674,7 @@ struct net_buf *bt_l2cap_create_pdu_timeout(struct net_buf_pool *pool, size_t reserve, k_timeout_t timeout) { - if (!K_TIMEOUT_EQ(timeout, K_NO_WAIT) && - k_current_get() == k_work_queue_thread_get(&k_sys_work_q)) { + if (!K_TIMEOUT_EQ(timeout, K_NO_WAIT) && k_is_in_sys_work()) { timeout = K_NO_WAIT; } diff --git a/subsys/input/input.c b/subsys/input/input.c index 55ff1afc5d30..78c73f4a7d93 100644 --- a/subsys/input/input.c +++ b/subsys/input/input.c @@ -52,8 +52,7 @@ int input_report(const struct device *dev, #ifdef CONFIG_INPUT_MODE_THREAD int ret; - if (!K_TIMEOUT_EQ(timeout, K_NO_WAIT) && - k_current_get() == k_work_queue_thread_get(&k_sys_work_q)) { + if (!K_TIMEOUT_EQ(timeout, K_NO_WAIT) && k_is_in_sys_work()) { LOG_DBG("Timeout discarded. No blocking in syswq."); timeout = K_NO_WAIT; } diff --git a/tests/bluetooth/host/conn/mocks/kernel.c b/tests/bluetooth/host/conn/mocks/kernel.c index 5f12f5b246ce..f940f82e2e45 100644 --- a/tests/bluetooth/host/conn/mocks/kernel.c +++ b/tests/bluetooth/host/conn/mocks/kernel.c @@ -31,5 +31,6 @@ DEFINE_FAKE_VALUE_FUNC(void *, k_heap_alloc, struct k_heap *, size_t, k_timeout_ DEFINE_FAKE_VOID_FUNC(k_heap_free, struct k_heap *, void *); DEFINE_FAKE_VOID_FUNC(k_sched_lock); DEFINE_FAKE_VOID_FUNC(k_sched_unlock); +DEFINE_FAKE_VALUE_FUNC(bool, k_is_in_sys_work); struct k_work_q k_sys_work_q; diff --git a/tests/bluetooth/host/conn/mocks/kernel.h b/tests/bluetooth/host/conn/mocks/kernel.h index ee6edca355d1..f1936307275c 100644 --- a/tests/bluetooth/host/conn/mocks/kernel.h +++ b/tests/bluetooth/host/conn/mocks/kernel.h @@ -32,6 +32,7 @@ FAKE(k_heap_free) \ FAKE(k_sched_lock) \ FAKE(k_sched_unlock) \ + FAKE(k_is_in_sys_work) \ DECLARE_FAKE_VALUE_FUNC(bool, k_is_in_isr); DECLARE_FAKE_VALUE_FUNC(int, k_poll_signal_raise, struct k_poll_signal *, int); @@ -56,3 +57,4 @@ DECLARE_FAKE_VALUE_FUNC(void *, k_heap_alloc, struct k_heap *, size_t, k_timeout DECLARE_FAKE_VOID_FUNC(k_heap_free, struct k_heap *, void *); DECLARE_FAKE_VOID_FUNC(k_sched_lock); DECLARE_FAKE_VOID_FUNC(k_sched_unlock); +DECLARE_FAKE_VALUE_FUNC(bool, k_is_in_sys_work); From f0d9a3bb250076a5f418d454a2261e95c1bb9c1b Mon Sep 17 00:00:00 2001 From: Bjarki Arge Andreasen Date: Sun, 23 Mar 2025 14:46:20 +0100 Subject: [PATCH 4/4] kernel: sched: impl sys workq item no block enforcement The system workqueue should never be unreadied from a work item handler (while busy). This commit implements an optional check which will invoke a kernel oops is a blocking operation is attempted from a work item passed to the system workqueue. Signed-off-by: Bjarki Arge Andreasen --- doc/kernel/services/threads/workqueue.rst | 3 +++ kernel/Kconfig | 8 ++++++++ kernel/sched.c | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/doc/kernel/services/threads/workqueue.rst b/doc/kernel/services/threads/workqueue.rst index 0af0288464f3..845aabed6fd8 100644 --- a/doc/kernel/services/threads/workqueue.rst +++ b/doc/kernel/services/threads/workqueue.rst @@ -109,6 +109,9 @@ its queue until the handler function finishes executing. potentially blocking, as there is no guarantee that work items submitted to it do not depend on subsequent work items in the queue to unblock them. + :kconfig:option:`CONFIG_SYSTEM_WORKQUEUE_NO_BLOCK` enforces that no work + items submitted to the system workqueue perform any blocking operations. + The single argument that is passed to a handler function can be ignored if it is not required. If the handler function requires additional information about the work it is to perform, the work item can be embedded in a larger data diff --git a/kernel/Kconfig b/kernel/Kconfig index fbc0750845c7..83ab871843cf 100644 --- a/kernel/Kconfig +++ b/kernel/Kconfig @@ -600,6 +600,14 @@ config SYSTEM_WORKQUEUE_NO_YIELD cooperative and a sequence of work items is expected to complete without yielding. +config SYSTEM_WORKQUEUE_NO_BLOCK + bool "Select whether system work queue enforces non-blocking work items" + help + By default, the system work queue does not enforce work items + passed to it to not perform blocking operations. Selecting this + enforces that blocking operations are not performed by invoking + a kernel oops if such operations are attempted. + endmenu menu "Barrier Operations" diff --git a/kernel/sched.c b/kernel/sched.c index d517a01787b9..524f9f40c1d2 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -523,6 +523,10 @@ static inline void z_vrfy_k_thread_resume(k_tid_t thread) static void unready_thread(struct k_thread *thread) { + if (IS_ENABLED(CONFIG_SYSTEM_WORKQUEUE_NO_BLOCK) && k_is_in_sys_work()) { + k_oops(); + } + if (z_is_thread_queued(thread)) { dequeue_thread(thread); }