Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 5: Locking and Interprocess Communication


The identifier visible to userland is given bys*SEQ_MULTIPLIER+i,wheresis the current
sequence number andiis the kernel-internal identifier. The sequence multiplier is set to
the upper limit for IPC objects. If an internal ID is reused, a different userspace identifier is
generated this way because the sequence numberis reused. This minimizes the risk of using a
wrong resource when a stale ID is passed from userland.
❑ rw_mutexis a kernel semaphore. It is used to implement semaphore operations and safeguards
against race conditions in userspace. The mutex appropriately protects the data structures that
contain, for instance, the semaphore value.

Each IPC object is represented by an instance ofkern_ipc_permtowhichwecomeinamoment.Each
object has a kernel-internal ID, andipcs_idris used to associate an ID with a pointer to the correspond-
ingkern_ipc_perminstance. Since the number of used IPC objects can grow and shrink dynamically,
a static array would not serve well to manage the information, but the kernel provides a radix-tree-like
(see Appendix C) standard data structure inlib/idr.cfor this purpose. How the entries are managed
in detail is not relevant for our purposes; it suffices to know that each internal ID can be associated with
the respectivekern_ipc_perminstance without problems.


The elements ofkern_ipc_permhold information on semaphore ‘‘owners‘‘ and on access permissions.


<ipc.h>
struct kern_ipc_perm
{
int id;
key_t key;
uid_t uid;
gid_t gid;
uid_t cuid;
gid_t cgid;
mode_t mode;
unsigned long seq;
};

The structure can be used not only for semaphores but also for other IPC mechanisms. You will come
across it frequently in this chapter.


❑ keyholds the magic number used by user programs to identify the semaphore, andidis the
kernel-internal identifier.
❑ uidandgidspecify the user and group ID of the owner.cuidandcgidhold the same data for
the process that generated the semaphore.
❑ seqis a sequence number that was used when the object was reserved.
❑ modeholds the bitmask, which specifies access permissions in accordance with the owner, group,
others scheme.

The above data structures are not sufficient to keep all information required for semaphores. A special
per-task element is required:


<sched.h>
struct task_struct {
...
#ifdef CONFIG_SYSVIPC
Free download pdf