Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


ranges from 100 to 149 and from 150 to 199 are assigned. The originally large resource area is repeatedly
subdivided into smaller sections, each of which represents a layer of an abstraction model. Consequently,
child elements can be used to partition the area into ever smaller and ever more specific sections.

Requestingand ReleasingResources


To ensure that resources — regardless of kind — aredeployed reliably, the kernel must feature a mech-
anism to allocate and subsequently release them. Once a resource has been allocated, it may not be used
by any other driver.

Requesting and releasing a resource equates to nothing more than adding and removing entries from a
resource tree.^16

Requesting Resources


The kernel provides the__request_resourcefunction to request a resource area.^17 The function expects
a series of parameters including a pointer to the parent element, the start and end address of the resource
area, and a string to hold the name of the area.

kernel/resource.c
static struct resource * request_resource(struct resource *root,
struct resource *new);

The purpose of the function is to allocate arequestinstance and fill it with the data passed. Checks are
also performed to detect obvious errors (start address bigger than the end address, for example) that
would render the request useless and would abort the action.request_resourceis responsible only for
the requisite locking. The heavy work is delegated to__request_resource. It scans the existing resources
consecutively to add the new resource at the correctposition or to reveal conflicts with areas already
allocated. It does this by running through the list of siblings. The newresourceinstance is inserted if the
required resource area is free, thus reserving the resource. Reservation fails if the area is not free.

The children of a specific parent are scanned on one sibling level only. The kernel does not scan the list
of children downward.

If a resource cannot be reserved, the driver automatically knows that it is already in use and is therefore
not available at the moment.

Releasing Resources


Therelease_resourcefunction is invoked to release a resource that is in use.

kernel/resource.c
void release_resource(struct resource *old)

(^16) It is important to note that many system resourcescouldbe addressed without the need to reserve them. With few exceptions, pro-
cessors have no way of enforcing resource reservation. The functions described below should therefore be employed in the interests
of a clean programming style, although it would be possible to dispense with reservations in most cases.
(^17) The kernel sources include other functions for allocating resources for reasons of compatibility, but they should no longer be used
in new code. There are also functions thatsearchfor resources of a certain size so that areas still free are filled automatically. I won’t
discuss these extended options as they are used only at a few places in the kernel.

Free download pdf