Skip to content

kthread

Purpose

Kernel threads provide long-lived execution contexts inside the Linux kernel.

Unlike workqueues, which execute individual deferred jobs and return, kernel threads continuously run until explicitly stopped.

Kernel threads are commonly used for:

  • Protocol state machines
  • Connection managers
  • Background services
  • Event-driven processing
  • Long-lived driver tasks

#include <linux/kthread.h>

Key APIs

kthread_create()

Create a kernel thread without starting execution.

Prototype:

struct task_struct *kthread_create(
        int (*threadfn)(void *data),
        void *data,
        const char namefmt[],
        ...);

Behavior:

  • Allocate and initialize a kernel thread
  • Thread is created in a stopped state
  • Caller must invoke wake_up_process() to start execution

Example:

task = kthread_create(
        demo_thread_fn,
        mydev,
        "demo_thread");

wake_up_process(task);

wake_up_process()

Wake and schedule a sleeping task.

Prototype:

int wake_up_process(
        struct task_struct *task);

Typical usage:

task = kthread_create(...);

wake_up_process(task);

kthread_run()

Create and start a kernel thread.

Prototype:

struct task_struct *kthread_run(
        int (*threadfn)(void *data),
        void *data,
        const char namefmt[],
        ...);

Parameters:

Parameter Description
threadfn Thread entry function
data Private context pointer
namefmt Thread name

Return:

Value Meaning
Valid pointer Thread created successfully
ERR_PTR() Failure

Example:

mydev->thread =
    kthread_run(
        demo_thread_fn,
        mydev,
        "demo_thread");

kthread_should_stop()

Check whether thread termination has been requested.

Prototype:

bool kthread_should_stop(void);

Typical usage:

while (!kthread_should_stop()) {

    ...
}

kthread_stop()

Request thread termination and wait for thread exit.

Prototype:

int kthread_stop(
        struct task_struct *thread);

Example:

if (mydev->thread)
    kthread_stop(mydev->thread);

Behavior:

  1. Set stop flag
  2. Wake sleeping thread
  3. Wait for thread exit

Thread Lifecycle

kthread_create()
CREATED
wake_up_process()
RUNNING
kthread_stop()
STOP_REQUESTED
thread exits
EXITED

kthread_stop() does not forcibly terminate a thread.

The thread must periodically check:

kthread_should_stop()

and exit cooperatively.


Typical Thread Structure

static int demo_thread_fn(void *arg)
{
    while (!kthread_should_stop()) {

        wait_event(...);

        process_event();
    }

    return 0;
}

Event-Driven Pattern

Kernel threads are commonly combined with wait queues.

Consumer:

wait_event(
    mydev->wq,
    condition);

Producer:

wake_up(
    &mydev->wq);

Typical architecture:

Producer

wake_up()

Kernel Thread

process_event()

wait_event()

Common Pitfalls

Forgetting kthread_should_stop()

Incorrect:

while (1) {

    ...
}

The thread can never terminate cleanly.


Polling Instead of Waiting

Incorrect:

while (!kthread_should_stop()) {

    process();

    msleep(100);
}

Prefer:

wait_event(...);

to avoid unnecessary wakeups.


Blocking for Long Periods

Long blocking operations can delay processing of new events.

Consider:

  • wait queues
  • event queues
  • timeout-driven state machines

instead of long sleeps.


  • wait_event()
  • wait_event_timeout()
  • wake_up()
  • workqueue
  • devm_request_threaded_irq()

  • Kernel Thread Fundamentals
  • Workqueue and Deferred Execution
  • Completion Synchronization
  • Wait Queue Internals