Skip to content

Commit 2588783

Browse files
committed
Add error handling for workqueue allocation
Previously, sched_init() did not check whether alloc_workqueue() succeeded. This could lead to a NULL pointer dereference and kernel crash if memory allocation failed. This commit adds a check for a NULL return and logs an error message if allocation fails. By returning -ENOMEM early, we ensure the module will not be loaded in an invalid state and avoid undefined behavior or system instability.
1 parent f105003 commit 2588783

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

examples/sched.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ static void work_handler(struct work_struct *data)
1616
static int __init sched_init(void)
1717
{
1818
queue = alloc_workqueue("HELLOWORLD", WQ_UNBOUND, 1);
19+
if (!queue) {
20+
pr_err("Failed to allocate workqueue\n");
21+
return -ENOMEM;
22+
}
1923
INIT_WORK(&work, work_handler);
2024
queue_work(queue, &work);
2125
return 0;

0 commit comments

Comments
 (0)