Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 5: Locking and Interprocess Communication


...
barrier(); \
preempt_check_resched(); \
} while (0)

This measure protects against the second erroneous reordering shown above.

All barrier commands discussed so far are made available by including<system.h>. You might have got-
ten the impression that memory barriers are notoriously complicated to use, and your perception serves
you well, indeed — getting memory and optimization barriers right can be a very tricky business. It
should therefore be noted that memory barriers are not particularly favored by some kernel maintainers,
and code using them will have a hard time finding its way into mainline. So it’s always worth a try to see
if things cannot be done differently without barriers. This is possible because locking instructions will on
many architectures also act as memory barriers. However, this needs to be checked for the specific cases
that require memory barriers, and general advice is hard to give.

5.2.6 Reader/Writer Locks


The mechanisms described above have one disadvantage. They do not differentiate between situations
in which data structures are simply read and those in which they are actively manipulated. Usually, any
number of processes are granted concurrent read access to data structures, whereas write access must be
restricted exclusively to a single task.

The kernel therefore provides additional semaphore and spinlock versions to cater for the above — these
are known accordingly asReader/Writersemaphores and Reader/Writer spinlocks.

Therwlock_tdata type is defined for Reader/Writer spinlocks. Locks must be acquired in different ways
in order to differentiate between read and write access.

❑ read_lockandread_unlockmust be executed before and after a critical region to which a pro-
cess requires read access. The kernel grants any number of read processes concurrent access to
the critical region.
❑ write_lockandwrite_unlockare used for write access. The kernel ensures that only one writer
(and no readers) is in the region.

An_irq _irqsavevariant is also available and functions in the same way as normal spinlocks. Variants
ending in_bhare also available. They disable software interrupts, but leave hardware interrupts still
enabled.

Read/write semaphores are used in a similar way. The equivalent data structure isstruct rw_semaphore,
anddown_readandup_readare used to obtain read access to the critical region. Write access is per-
formed with the help ofdown_writeandup_write.The_trylockvariants are also available for all
commands — they also function as described above.

5.2.7 The Big Kernel Lock


A relic of earlier days is the option of locking the entire kernel to ensure that no processors run in par-
allel in kernel mode. This lock is known as thebig kernel lockbut is most frequently referred to by its
abbreviation,BKL.

Thecompletekernelislockedusinglock_kernel; its unlocking counterpart isunlock_kernel.
Free download pdf