Skip to content

Commit a4c23a1

Browse files
kernel: workq: introduce work timeout:
Introduce work timeout, which is an optional workqueue configuration which enables monitoring for work items which take longer than expected. This could be due to long running or deadlocked handlers. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
1 parent 6061deb commit a4c23a1

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

include/zephyr/kernel.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4168,6 +4168,16 @@ struct k_work_queue_config {
41684168
* essential thread.
41694169
*/
41704170
bool essential;
4171+
4172+
/** Controls whether work queue monitors work timeouts.
4173+
*
4174+
* If non-zero, and CONFIG_WORKQUEUE_WORK_TIMEOUT is enabled,
4175+
* the work queue will monitor the duration of each work item.
4176+
* If the work item handler takes longer than the specified
4177+
* time to execute, the work queue thread will be aborted, and
4178+
* an error will be logged if CONFIG_LOG is enabled.
4179+
*/
4180+
uint32_t work_timeout_ms;
41714181
};
41724182

41734183
/** @brief A structure used to hold work until it can be processed. */
@@ -4190,6 +4200,12 @@ struct k_work_q {
41904200

41914201
/* Flags describing queue state. */
41924202
uint32_t flags;
4203+
4204+
#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT)
4205+
struct _timeout workto;
4206+
struct k_work *work;
4207+
k_timeout_t work_timeout;
4208+
#endif /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */
41934209
};
41944210

41954211
/* Provide the implementation for inline functions declared above */

kernel/Kconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,14 @@ endmenu
574574

575575
rsource "Kconfig.obj_core"
576576

577+
config WORKQUEUE_WORK_TIMEOUT
578+
bool "Support workqueue work timeout monitoring"
579+
help
580+
If enabled, the workqueue will monitor the duration of each work item.
581+
If the work item handler takes longer than the specified time to
582+
execute, the work queue thread will be aborted, and an error will be
583+
logged.
584+
577585
menu "System Work Queue Options"
578586
config SYSTEM_WORKQUEUE_STACK_SIZE
579587
int "System workqueue stack size"
@@ -600,6 +608,14 @@ config SYSTEM_WORKQUEUE_NO_YIELD
600608
cooperative and a sequence of work items is expected to complete
601609
without yielding.
602610

611+
config SYSTEM_WORKQUEUE_WORK_TIMEOUT_MS
612+
int "Select system work queue work timeout in milliseconds"
613+
default 5000 if ASSERT
614+
default 0
615+
help
616+
Set to 0 to disable work timeout for system workqueue. Option
617+
has no effect if WORKQUEUE_WORK_TIMEOUT is not enabled.
618+
603619
endmenu
604620

605621
menu "Barrier Operations"

kernel/system_work_q.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static int k_sys_work_q_init(void)
2525
.name = "sysworkq",
2626
.no_yield = IS_ENABLED(CONFIG_SYSTEM_WORKQUEUE_NO_YIELD),
2727
.essential = true,
28+
.work_timeout_ms = CONFIG_SYSTEM_WORKQUEUE_WORK_TIMEOUT_MS,
2829
};
2930

3031
k_work_queue_start(&k_sys_work_q,

kernel/work.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include <errno.h>
1818
#include <ksched.h>
1919
#include <zephyr/sys/printk.h>
20+
#include <zephyr/logging/log.h>
21+
22+
LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL);
2023

2124
static inline void flag_clear(uint32_t *flagp,
2225
uint32_t bit)
@@ -599,6 +602,50 @@ bool k_work_cancel_sync(struct k_work *work,
599602
return pending;
600603
}
601604

605+
#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT)
606+
static void workto_handler(struct _timeout *to)
607+
{
608+
struct k_work_q *queue = CONTAINER_OF(to, struct k_work_q, workto);
609+
const char *name;
610+
struct k_work *work;
611+
k_work_handler_t handler;
612+
613+
name = k_thread_name_get(&queue->thread);
614+
615+
K_SPINLOCK(&lock) {
616+
work = queue->work;
617+
handler = work->handler;
618+
}
619+
620+
if (name != NULL) {
621+
LOG_ERR("queue %s blocked by work %p with handler %p", name, work, handler);
622+
} else {
623+
LOG_ERR("queue %p blocked by work %p with handler %p", queue, work, handler);
624+
}
625+
626+
k_thread_abort(&queue->thread);
627+
}
628+
629+
static void work_timeout_start_locked(struct k_work_q *queue, struct k_work *work)
630+
{
631+
if (K_TIMEOUT_EQ(queue->work_timeout, K_FOREVER)) {
632+
return;
633+
}
634+
635+
queue->work = work;
636+
z_add_timeout(&queue->workto, workto_handler, queue->work_timeout);
637+
}
638+
639+
static void work_timeout_stop_locked(struct k_work_q *queue)
640+
{
641+
if (K_TIMEOUT_EQ(queue->work_timeout, K_FOREVER)) {
642+
return;
643+
}
644+
645+
z_abort_timeout(&queue->workto);
646+
}
647+
#endif /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */
648+
602649
/* Loop executed by a work queue thread.
603650
*
604651
* @param workq_ptr pointer to the work queue structure
@@ -678,6 +725,10 @@ static void work_queue_main(void *workq_ptr, void *p2, void *p3)
678725
continue;
679726
}
680727

728+
#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT)
729+
work_timeout_start_locked(queue, work);
730+
#endif /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */
731+
681732
k_spin_unlock(&lock, key);
682733

683734
__ASSERT_NO_MSG(handler != NULL);
@@ -690,6 +741,10 @@ static void work_queue_main(void *workq_ptr, void *p2, void *p3)
690741
*/
691742
key = k_spin_lock(&lock);
692743

744+
#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT)
745+
work_timeout_stop_locked(queue);
746+
#endif /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */
747+
693748
flag_clear(&work->flags, K_WORK_RUNNING_BIT);
694749
if (flag_test(&work->flags, K_WORK_FLUSHING_BIT)) {
695750
finalize_flush_locked(work);
@@ -761,6 +816,14 @@ void k_work_queue_start(struct k_work_q *queue,
761816
queue->thread.base.user_options |= K_ESSENTIAL;
762817
}
763818

819+
#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT)
820+
if ((cfg != NULL) && (cfg->work_timeout_ms)) {
821+
queue->work_timeout = K_MSEC(cfg->work_timeout_ms);
822+
} else {
823+
queue->work_timeout = K_FOREVER;
824+
}
825+
#endif /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */
826+
764827
k_thread_start(&queue->thread);
765828

766829
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work_queue, start, queue);

submanifests/optional.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ manifest:
6060
groups:
6161
- optional
6262
- name: zephyr-lang-rust
63-
revision: d4f9036a88e53080bf2b186afa5289f9c77a0f73
63+
revision: pull/86/head
6464
path: modules/lang/rust
6565
remote: upstream
6666
groups:

0 commit comments

Comments
 (0)