get_free_page and Friends

If a module needs to allocate big chunks of memory, it is better to use a page-oriented technique. Requesting whole pages also has other advantages, which will be introduced later, in Section 13.2 in Chapter 13.

To allocate pages, the following functions are available:

  • get_free_page returns a pointer to a new page and zeros the page.

  • __get_free_page is like get_free_page, but doesn’t clear the page.

  • __get_free_pages returns a pointer to the first byte of a memory area that is several pages long, but doesn’t zero the area.

  • __get_dma_pages returns the pointer to the first byte of a memory area several pages long; the pages are consecutive in physical memory and suitable for DMA.

The prototypes for the functions, as defined in Linux 2.0, follow:

unsigned long get_free_page(int priority);
unsigned long __get_free_page(int priority);
unsigned long __get_dma_pages(int priority,
                              unsigned long order);
unsigned long __get_free_pages(int priority,
                               unsigned long order, int dma);

As a matter of fact, all the functions except __get_free_pages are either macros or inline functions that ultimately map to __get_free_pages.

When a program is done with the pages, it can call one of the following functions. The first function is a macro that falls back on the second:

void free_page(unsigned long addr);
void free_pages(unsigned long addr, unsigned long order);

If you’re writing code to work with both 1.2 and 2.0, it’s better not to use __get_free_pages directly because ...

Get Linux Device Drivers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.