Skip to content

Commit ceb645c

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 ceb645c

File tree

6 files changed

+139
-3
lines changed

6 files changed

+139
-3
lines changed

include/zephyr/kernel.h

+40
Original file line numberDiff line numberDiff line change
@@ -3663,6 +3663,27 @@ int k_work_queue_unplug(struct k_work_q *queue);
36633663
*/
36643664
int k_work_queue_stop(struct k_work_q *queue, k_timeout_t timeout);
36653665

3666+
#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT)
3667+
/** @brief Check if a work queue is blocked.
3668+
*
3669+
* Checks if a work queue is blocked by a work item. The work queue is considered
3670+
* blocked if a work item takes longer to execute than the work_timeout provided
3671+
* to k_work_queue_start in the k_work_queue_config structure.
3672+
*
3673+
* @param queue Pointer to the queue structure.
3674+
*
3675+
* @retval true if the work queue is blocked
3676+
* @retval false if the work queue is not blocked
3677+
*/
3678+
bool k_work_queue_is_blocked(struct k_work_q *queue);
3679+
#else
3680+
static inline bool k_work_queue_is_blocked(struct k_work_q *queue)
3681+
{
3682+
ARG_UNUSED(queue);
3683+
return false;
3684+
}
3685+
#endif
3686+
36663687
/** @brief Initialize a delayable work structure.
36673688
*
36683689
* This must be invoked before scheduling a delayable work structure for the
@@ -3974,6 +3995,8 @@ enum {
39743995
K_WORK_QUEUE_PLUGGED = BIT(K_WORK_QUEUE_PLUGGED_BIT),
39753996
K_WORK_QUEUE_STOP_BIT = 4,
39763997
K_WORK_QUEUE_STOP = BIT(K_WORK_QUEUE_STOP_BIT),
3998+
K_WORK_QUEUE_BLOCKED_BIT = 5,
3999+
K_WORK_QUEUE_BLOCKED = BIT(K_WORK_QUEUE_BLOCKED_BIT),
39774000

39784001
/* Static work queue flags */
39794002
K_WORK_QUEUE_NO_YIELD_BIT = 8,
@@ -4168,6 +4191,14 @@ struct k_work_queue_config {
41684191
* essential thread.
41694192
*/
41704193
bool essential;
4194+
4195+
/** Controls whether work queue monitors work timeouts.
4196+
*
4197+
* If set to a positive value, the work queue will monitor the
4198+
* duration of each work item, and warn the user if the work
4199+
* item handler takes longer than work_timeout to execute.
4200+
*/
4201+
uint32_t work_timeout_ms;
41714202
};
41724203

41734204
/** @brief A structure used to hold work until it can be processed. */
@@ -4190,6 +4221,15 @@ struct k_work_q {
41904221

41914222
/* Flags describing queue state. */
41924223
uint32_t flags;
4224+
4225+
/* Timeout which invokes sentinal */
4226+
struct _timeout workto;
4227+
4228+
/* Work monitored by sentinal */
4229+
struct k_work *work;
4230+
4231+
/* Maximum work duration */
4232+
k_timeout_t work_timeout;
41934233
};
41944234

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

kernel/Kconfig

+10
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@ endmenu
574574

575575
rsource "Kconfig.obj_core"
576576

577+
config WORKQUEUE_WORK_TIMEOUT
578+
bool "Enable workqueue timeout monitoring"
579+
577580
menu "System Work Queue Options"
578581
config SYSTEM_WORKQUEUE_STACK_SIZE
579582
int "System workqueue stack size"
@@ -600,6 +603,13 @@ config SYSTEM_WORKQUEUE_NO_YIELD
600603
cooperative and a sequence of work items is expected to complete
601604
without yielding.
602605

606+
config SYSTEM_WORKQUEUE_WORK_TIMEOUT_MS
607+
int "Select system work queue work timeout in milliseconds"
608+
default 10000 if DEBUG
609+
default 0
610+
help
611+
Set to 0 to disable work timeout for system workqueue.
612+
603613
endmenu
604614

605615
menu "Barrier Operations"

kernel/system_work_q.c

+1
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

+75
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,55 @@ 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+
k_spinlock_key_t key;
610+
const char *name;
611+
struct k_work *work;
612+
k_work_handler_t handler;
613+
614+
key = k_spin_lock(&lock);
615+
616+
flag_set(&queue->flags, K_WORK_QUEUE_BLOCKED_BIT);
617+
618+
name = k_thread_name_get(&queue->thread);
619+
work = queue->work;
620+
handler = work->handler;
621+
622+
if (name != NULL) {
623+
LOG_WRN("queue %s blocked by work %p with handler %p", name, work, handler);
624+
} else {
625+
LOG_WRN("queue %p blocked by work %p with handler %p", queue, work, handler);
626+
}
627+
628+
k_spin_unlock(&lock, key);
629+
}
630+
631+
static void work_timeout_start_locked(struct k_work_q *queue, struct k_work *work)
632+
{
633+
if (K_TIMEOUT_EQ(queue->work_timeout, K_FOREVER)) {
634+
return;
635+
}
636+
637+
queue->work = work;
638+
z_add_timeout(&queue->workto, workto_handler, queue->work_timeout);
639+
}
640+
641+
static void work_timeout_stop_locked(struct k_work_q *queue)
642+
{
643+
if (K_TIMEOUT_EQ(queue->work_timeout, K_FOREVER)) {
644+
return;
645+
}
646+
647+
z_abort_timeout(&queue->workto);
648+
if (flag_test_and_clear(&queue->flags, K_WORK_QUEUE_BLOCKED_BIT)) {
649+
LOG_INF("queue %p unblocked", queue);
650+
}
651+
}
652+
#endif
653+
602654
/* Loop executed by a work queue thread.
603655
*
604656
* @param workq_ptr pointer to the work queue structure
@@ -678,6 +730,10 @@ static void work_queue_main(void *workq_ptr, void *p2, void *p3)
678730
continue;
679731
}
680732

733+
#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT)
734+
work_timeout_start_locked(queue, work);
735+
#endif
736+
681737
k_spin_unlock(&lock, key);
682738

683739
__ASSERT_NO_MSG(handler != NULL);
@@ -690,6 +746,10 @@ static void work_queue_main(void *workq_ptr, void *p2, void *p3)
690746
*/
691747
key = k_spin_lock(&lock);
692748

749+
#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT)
750+
work_timeout_stop_locked(queue);
751+
#endif
752+
693753
flag_clear(&work->flags, K_WORK_RUNNING_BIT);
694754
if (flag_test(&work->flags, K_WORK_FLUSHING_BIT)) {
695755
finalize_flush_locked(work);
@@ -761,6 +821,14 @@ void k_work_queue_start(struct k_work_q *queue,
761821
queue->thread.base.user_options |= K_ESSENTIAL;
762822
}
763823

824+
#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT)
825+
if ((cfg != NULL) && (cfg->work_timeout_ms)) {
826+
queue->work_timeout = K_MSEC(cfg->work_timeout_ms);
827+
} else {
828+
queue->work_timeout = K_FOREVER;
829+
}
830+
#endif
831+
764832
k_thread_start(&queue->thread);
765833

766834
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work_queue, start, queue);
@@ -853,6 +921,13 @@ int k_work_queue_stop(struct k_work_q *queue, k_timeout_t timeout)
853921
return 0;
854922
}
855923

924+
#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT)
925+
bool k_work_queue_is_blocked(struct k_work_q *queue)
926+
{
927+
return flag_test(&queue->flags, K_WORK_QUEUE_BLOCKED_BIT);
928+
}
929+
#endif
930+
856931
#ifdef CONFIG_SYS_CLOCK_EXISTS
857932

858933
/* Timeout handler for delayable work.

samples/hello_world/prj.conf

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
# nothing here
2+
CONFIG_LOG=y
3+
CONFIG_THREAD_NAME=y
4+
CONFIG_WORKQUEUE_WORK_TIMEOUT=y
5+
CONFIG_SYSTEM_WORKQUEUE_WORK_TIMEOUT_MS=1000

samples/hello_world/src/main.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#include <stdio.h>
7+
#include <zephyr/kernel.h>
88

9-
int main(void)
9+
void my_handler(struct k_work *work)
1010
{
11-
printf("Hello World! %s\n", CONFIG_BOARD_TARGET);
11+
k_sleep(K_SECONDS(2));
12+
}
13+
14+
K_WORK_DEFINE(my_work, my_handler);
1215

16+
int main(void)
17+
{
18+
k_work_submit(&my_work);
1319
return 0;
1420
}

0 commit comments

Comments
 (0)