Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allocating more than 4 MB of pinned contiguous memory in the Linux Kernel

Tags:

For some interaction with a PCI device that is being built, we'd like to create large contiguous pieces of memory that the board can access. As it stands now, the largest piece of memory that I've been able to allocate is 4 megabytes in size. I'm wondering if there are any methods to create larger regions.

I do know that I can use the boot option mem= to do this, but for numa reasons, I'd rather not go this route. If, on the other hand, someone knew a way to do this, but distribute it over numa nodes, that would be fine.

As I said initially, I'm limited to 4 Megabytes currently. Allocations are currently done by __alloc_pages, which is limited by MAX_ORDER. MAX_ORDER is a compile-time constant, and I'm also concerned that editing it could have affects elsewhere.

Thanks.

like image 886
Bill Lynch Avatar asked May 09 '11 17:05

Bill Lynch


People also ask

How do I allocate more memory to linux?

The most straightforward way to allocate memory is to use a function from the kmalloc() family. And, to be on the safe side it's best to use routines that set memory to zero, like kzalloc() . If you need to allocate memory for an array, there are kmalloc_array() and kcalloc() helpers.

How does the kernel allocate memory?

The kernel should allocate memory dynamically to other kernel subsystems and to user processes. The KMA (kernel memory allocation) API usually consists of two functions: void* malloc(size_t nbytes); void free(void* ptr); There are various issues associated with allocation of memory.

Why can't we use malloc in kernel code?

This means that ANY function you're calling in the kernel needs to be defined in the kernel. Linux does not define a malloc, hence you can't use it. There is a memory allocator and a family of memory allocation functions. Read the kernel docs on the memory allocator for more information.

What is the difference between Kmalloc and Vmalloc?

The kmalloc() function guarantees that the pages are physically contiguous (and virtually contiguous). The vmalloc() function works in a similar fashion to kmalloc() , except it allocates memory that is only virtually contiguous and not necessarily physically contiguous.


1 Answers

If you can compile your PCI device driver into the kernel (that is, not linked as a module), you could try allocating the memory at boot time. That should let you bypass the upper bounds on dynamic allocations. Refer to Linux Device Drivers, ed. 3 ch. 8 for details.

like image 65
Karmastan Avatar answered Nov 04 '22 02:11

Karmastan