Re: kernel module and dynamic memory allocation
From: Kasper Dupont (kasperd_at_daimi.au.dk)
Date: 09/25/03
- Next message: Tom: "Non-Blocking Socket"
- Previous message: Gerhard W. Gruber: "kernel module and dynamic memory allocation"
- In reply to: Gerhard W. Gruber: "kernel module and dynamic memory allocation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Tom: "Non-Blocking Socket"
- Previous message: Gerhard W. Gruber: "kernel module and dynamic memory allocation"
- In reply to: Gerhard W. Gruber: "kernel module and dynamic memory allocation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|