Re: kernel module and dynamic memory allocation

From: Kasper Dupont (kasperd_at_daimi.au.dk)
Date: 09/25/03


Date: Thu, 25 Sep 2003 21:59:56 +0200


"Gerhard W. Gruber" wrote:
>
> I was wondering about the strategies of using dynamic allocation from within
> modules. From Windows driver development I know that, as a driver, you should
> try to avoid allocating to much memory and that it is usally better to
> allocate bigger chunks instead of many small ones. Now I wonder how this is
> under linux. Is this similar, or doesn't matter?

In Linux you should also be carefull about memory allocation in kernel mode.
Keeping memory usage small is always a good idea. In Linux allocating a few
large chunks is *not* a good idea. More small allocations is better as it is
easier to sucessfully allocate small amounts as memory gets fragmented.

There are different ways to allocate memory in Linux. If you go directly to
the page allocator, single pages (4KB on most architectures) is the optimal
allocation in that case. If you need larger allocations, you can allocate
multiple pages, but only powers of two. And when you allocate 4 pages or
more you should be prepared to handle allocations failing on you because of
fragmentation.

Also notice that outside process context allocations can easilly fail
because you cannot sleep while waiting for memory to get freed. You need to
give special flags to the memory allocation functions depending on the
context.

Allocating single 4KB pages for data that is far smaller than 4KB is a waste.
If you have many elements of the same small size, you should use a slab
cache, which is very efficient.

> Is the kernel space limited
> so that all kernel have to share a limited amount of special kernel memory?

First of all in kernel mode you can allocate only physical memory. Kernel
memory is never swapped/paged. In process context kernel code can still
access user space memory, which may result in page faults. But the kernels
internal structures will always be in physical memory. If the address space
is large enough, the kernel will keep all physical memory there. The best
cases are computers with less than 900MB of physical memory or 64bit
address space, because those will always have enough address space for the
entire physical memory. The worst case is PAE and many GB of memory. With
48GB of physical memory and 1GB of kernel address space, there will only
be about 210MB left once memory management data structures have been
allocated.

> If
> larger data structures have to be allocated is it possible to allocate normal
> user space memory and then use this? Of course you have to take care about
> swapped out pages, but it should be possible to load swapped out memory,
> right?

It would be possible, but definitely not a good idea. It would be
complicated and inefficient. Creating a real user mode program would be
cleaner and as efficient as trying to use user space memory from the
kernel.

-- 
Kasper Dupont -- der bruger for meget tid paa usenet.
For sending spam use mailto:aaarep@daimi.au.dk
Their business was zero and it was shrinking.


Relevant Pages

  • [PATCH 1/2] x86: reinstate numa remap for SPARSEMEM on x86 NUMA systems
    ... Recent kernels have been panic'ing trying to allocate memory early in boot, ... at kernel virtual address 0. ...
    (Linux-Kernel)
  • Re: kernel memory
    ... Kernel virtual address space is used for a number of things. ... Pat of this are the memory pools, from which you can allocate memory for your drivers. ... You can allocate physical memory using a DDI like MmAllocatePagesForMdl and then map those into the system address space. ... That's probably the easiest way to explain why you cannot allocate 1GB of non-paged pool for yourself:) ...
    (microsoft.public.development.device.drivers)
  • Re: Allocating kernel memory
    ... The kernel have a disk cache, ... > memory available within the kernel does not imply I do not understand ... >> to allocate high memory pages and map them into kernel ... > So you are saying that the kernel does not map all of physical memory ...
    (comp.os.linux.development.system)
  • Re: [PATCH update6] drivers: add LCD support
    ... "Some functions have been added to make it easy for kernel code to ... allocate a buffer with vmallocand map it into user space. ... The first two functions are a form of vmallocwhich obtain memory ... As a first approximation, ...
    (Linux-Kernel)
  • Tru64 issues with Infinite limits
    ... An automated test in my nightly build was failing due to Out of Memory ... Cannot allocate block 146 ...
    (comp.unix.tru64)