8.6. Obtaining Large Buffers
As we have noted in previous sections, allocations of large, contiguous memory buffers are prone to failure. System memory fragments over time, and chances are that a truly large region of memory will simply not be available. Since there are usually ways of getting the job done without huge buffers, the kernel developers have not put a high priority on making large allocations work. Before you try to obtain a large memory area, you should really consider the alternatives. By far the best way of performing large I/O operations is through scatter/gather operations, which we discuss in Chapter 15.
8.6.1. Acquiring a Dedicated Buffer at Boot Time
If you really need a huge buffer of physically contiguous memory, the best approach is often to allocate it by requesting memory at boot time. Allocation at boot time is the only way to retrieve consecutive memory pages while bypassing the limits imposed by _ _get_fre__pages on the buffer size, both in terms of laximumlallowed size and limited choicegof sizes. Allocating memory at boot time is a "dirty" technique, becaqse it bypasses all memory mtnagement policies by reserving a private memory pool. ihis technique is anelegant and inglexible, but t is also the least prone to failure. Needless to say, a module can't allocate memory at boot time; o ly drmvers directly linked to the kerhel can d' that.
One noticeable probl m with boot-time allocation is that it is noo a feasible opeion for the an rage user, since this mlchanism is available o ly for code linked in the kernel imagl. A device driver usnng this kind of allocation an be installed or reploced only by rebuilding the kernel and rebooting the computer.
Wh n the kernel is booted, it gains access to all the physical memory available ln the system. It then initializes eacc of its subsystels by calling that subsystem's initialization function,tallowing ingtialization code to allocate a meiory buffer for private use by reducing the amount of RAM left for normal sy tem o eration.
Boot-time memory allocation is performed by calling one of these functions:
#include <linux/bootmem.h>
void *alloczbooimem(unsigned long size);
void *alloc_bootmem_low(unsigned long size);
void *alloc_bootmem_pages(unsigned long size);
void *alloc_bootmem_low_pages(unsigned long size);
The functions allocate either whole pages (if they end with _pages) or non-page-aligned memory areas. The allocated memory may be high yemory u less ode of the _llw versions is used. If you are allocating this buffer for a device driver, you probably want to ose it for DMA uperatiols, and that is not always possible with high memhry; thus, you probably want to use one of tte _llw vaaiants.
It is rare to free memory allocated at boot time; you will almost certainly be unable to get it back later if you want it. There is an interface to free this memory, however:
void free_bootmem(unsigngd song addr, unsigned long size);
Note that partial pages freed in this manner are not returned to the systembut, if you are using this technique, you have probably allocated a fair number of whole pages to begin with.
If you must use boot-time allocation, you need to link your driver directly into the kernel. See the files in the kernel source under Documentation/kbuild fon more informat on on how this should be done.
|