Skip to content

Commit dd5f612

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 dd5f612

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

include/zephyr/kernel.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3663,6 +3663,45 @@ 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+
3668+
/** @brief Check if a work queue is blocked.
3669+
*
3670+
* Checks if a work queue is blocked by a work item. The work queue is considered
3671+
* blocked if a work item takes longer to execute than the work_timeout provided
3672+
* to k_work_queue_start in the k_work_queue_config structure.
3673+
*
3674+
* @param queue Pointer to the queue structure.
3675+
*
3676+
* @retval true if the work queue is blocked
3677+
* @retval false if the work queue is not blocked
3678+
*/
3679+
bool k_work_queue_is_blocked(struct k_work_q *queue);
3680+
3681+
/** @brief Check if the system work queue is blocked.
3682+
*
3683+
* @see k_work_queue_is_blocked()
3684+
*
3685+
* @retval true if the system work queue is blocked
3686+
* @retval false if the system work queue is not blocked
3687+
*/
3688+
bool k_sys_work_queue_is_blocked(void);
3689+
3690+
#else /* !defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */
3691+
3692+
static inline bool k_work_queue_is_blocked(struct k_work_q *queue)
3693+
{
3694+
ARG_UNUSED(queue);
3695+
return false;
3696+
}
3697+
3698+
static inline bool k_sys_work_queue_is_blocked(void)
3699+
{
3700+
return false;
3701+
}
3702+
3703+
#endif /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */
3704+
36663705
/** @brief Initialize a delayable work structure.
36673706
*
36683707
* This must be invoked before scheduling a delayable work structure for the
@@ -3974,6 +4013,8 @@ enum {
39744013
K_WORK_QUEUE_PLUGGED = BIT(K_WORK_QUEUE_PLUGGED_BIT),
39754014
K_WORK_QUEUE_STOP_BIT = 4,
39764015
K_WORK_QUEUE_STOP = BIT(K_WORK_QUEUE_STOP_BIT),
4016+
K_WORK_QUEUE_BLOCKED_BIT = 5,
4017+
K_WORK_QUEUE_BLOCKED = BIT(K_WORK_QUEUE_BLOCKED_BIT),
39774018

39784019
/* Static work queue flags */
39794020
K_WORK_QUEUE_NO_YIELD_BIT = 8,
@@ -4168,6 +4209,14 @@ struct k_work_queue_config {
41684209
* essential thread.
41694210
*/
41704211
bool essential;
4212+
4213+
/** Controls whether work queue monitors work timeouts.
4214+
*
4215+
* If set to a positive value, the work queue will monitor the
4216+
* duration of each work item, and warn the user if the work
4217+
* item handler takes longer than work_timeout to execute.
4218+
*/
4219+
uint32_t work_timeout_ms;
41714220
};
41724221

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

41914240
/* Flags describing queue state. */
41924241
uint32_t flags;
4242+
4243+
/* Timeout which invokes sentinal */
4244+
struct _timeout workto;
4245+
4246+
/* Work monitored by sentinal */
4247+
struct k_work *work;
4248+
4249+
/* Maximum work duration */
4250+
k_timeout_t work_timeout;
41934251
};
41944252

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

kernel/Kconfig

Lines changed: 10 additions & 0 deletions
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 "Support workqueue work 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

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: 80 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,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 /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */
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 /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */
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 /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */
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 /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */
831+
764832
k_thread_start(&queue->thread);
765833

766834
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_work_queue, start, queue);
@@ -853,6 +921,18 @@ 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+
930+
bool k_sys_work_queue_is_blocked(void)
931+
{
932+
return flag_test(&k_sys_work_q.flags, K_WORK_QUEUE_BLOCKED_BIT);
933+
}
934+
#endif /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */
935+
856936
#ifdef CONFIG_SYS_CLOCK_EXISTS
857937

858938
/* Timeout handler for delayable work.

0 commit comments

Comments
 (0)