2

What is a parked thread in the context of Linux kernel? I mean a thread that is in TASK_PARKED state?

How this state differs from TASK_INTERRUPTIBLE and TASK_UNINTERRUPTIBLE?

From which state a thread can be woken faster? Generally, and in particular case if used for waiting: kthread_parkme / kthread_unpark instead of [s]wait_event_... / [s]wake_up_...]?

I know that waitqueues support multiple waiters, but I am interested only in a single sleeper/waker pair.

1

1 Answer 1

6

CPU parking is a power saving feature that allows taking an idle cpu core offline so it stops consuming power. When there is demand for the cpu again, the cpu is unparked.

Before a cpu can be parked, all threads on the cpu must either be unbound from the cpu or parked. Presumably if the cpu is to be parked, other cpus are available and idle, so running threads can be unbound from the cpu core and moved to other cores. But if a thread is also idle (possibly waiting for I/O to complete or a timeout or a kernel thread just waiting for work to do), rather than do the expensive operation of unbinding a thread and moving it to another core, it can just be parked too.

When the kernel parks a thread, it wakes up the thread and tells it to park itself. A kernel thread gets a chance to do any clean up it wants including releasing resources that might otherwise deadlock, and then calls parkme which handles some race conditions and then marks the thread as parked. Once all the threads on a core are migrated or parked, the kernel can then park the cpu.

Presumably, unparking a thread (and the cpu it is on) would be faster than waiting to be scheduled on a busy cpu, or binding it to another already idle cpu (which might have been parked itself anyway).

Note that a few special kernel threads are dedicated to the core they are bound to, so would always be parked rather than unbound.

8
  • I am not sure this answer is on spot. I asked about parked threads, not parked CPUs. Or you imply that putting a thread into TASK_PARKED state would park the whole CPU (core)? Commented Apr 22 at 11:28
  • Parked threads exist so that you can park the cpu without migrating the thread. The cpu can't park until its threads are migrated or parked. Parking a thread is a hint that maybe the cpu could be parked too. The two concepts are tied. Commented Apr 22 at 21:56
  • It's not clear to me if it even makes sense to park a thread except when you are about to park the cpu it is on. However, there are race conditions there, so you could park a thread, and then have it woken up before the cpu parking occurs, and this shouldn't break anything. Commented Apr 22 at 21:58
  • 2
    It isn't possible to explain parked threads without explaining parked cpus first. Your question only asked about the differences from parking and if it was faster, not how the kernel handles a parked thread. I've added a paragraph about that. Parking is really separate from interruptable / uninterruptable, they are only related in the sense they are also thread states and there isn't really a good comparison. It's like asking why bananas are yellow because other fruit like apples are red. They're just different. Commented Apr 23 at 11:22
  • 1
    I think if your kernel thread is idle a lot and can contribute to power saving, then parking is appropriate when it is idle. Otherwise, probably not. Commented Apr 23 at 21:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.