Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 17: Data Synchronization


17.4 Thread Initialization


pdflushis used as a work procedure for kernel threads. Once generated,pdflushthreads go to sleep and
wait until other parts of the kernel assign them tasks that are described inpdflush_work.Consequently,
the number ofpdflushthreads need not match the number of tasks to be performed. The generated
threads are on call and simply wait until the kernel decides to give them work to do.

The code flow diagram in Figure 17-3 shows howpdflushworks.

Add to pdflush_list list

Create/destroy thread

my_work->fn

pdflush

__pdflush

set when_i_went_to_sleep

schedule

Figure 17-3: Code flow diagram forpdflush.

The start routine for generating a newpdflushthread ispdflush, but control flow is passed immediately
to__pdflush.^1

In__pdflush, the worker function of thepdflush_workinstance is set toNULLbecause the thread has not
been given a particular job to do. The global counter (nr_pdflush_threads) must also be incremented by
1 because a newpdflushthread has now been added to the system.

The thread then goes into an endless loop inwhich the following actions are performed:

❑ Thepdflush_workinstance of the thread is added to the global listpdflush_list(reminder: the
kernel is able to identify the thread by means of thewhoelement).
❑ when_i_went_to_sleepis set to the current system time in jiffies to remember when the thread
started sleeping.
❑ scheduleis invoked — this is the most important action. Because the status of the thread was
previously set toTASK_INTERRUPTIBLE, the thread now goes to sleep until woken by an external
event.
If the kernel requires a worker thread, it sets the worker function of apdflush_workinstance
in the global list and wakes the correspondingthread, which resumes work immediately after
schedule— but now with thefnworker function.

(^1) All that happens inpdflushis that an instance ofpdflush_workis generated; a pointer to it is passed to__pdflush_work
as a parameter. This is to stop the compiler from performing unfortunate optimizations on this variable. Additionally, the process
priority is set to 0, and the allowed CPUs are limited to the ones granted for the parentkthreadd.

Free download pdf