views:

4530

answers:

3

I've googled around and found most people advocating the use of kmalloc, as you're guaranteed to get contiguous physical blocks of memory. However, it also seems as though kmalloc can fail if a contiguous physical block that you want can't be found.
What are the advantages of having a contiguous block of memory? Specifically, why would I need to have a contiguous physical block of memory in a system call? Is there any reason I couldn't just use vmalloc?
Finally, if I did use kmalloc, since this is for a system call, should I specify GFP_ATOMIC since, technically, a system call is called as part of an interrupt handler, and I read the following:

GFP_ATOMIC
The allocation is high-priority and does not sleep. This is the flag to use in interrupt handlers, bottom halves and other situations where you cannot sleep.

+1  A: 

What are the advantages of having a contiguous block of memory? Specifically, why would I need to have a contiguous physical block of memory in a system call? Is there any reason I couldn't just use vmalloc?

From Google's "I'm Feeling Lucky" on vmalloc:

kmalloc is the preferred way, as long as you don't need very big areas. The trouble is, if you want to do DMA from/to some hardware device, you'll need to use kmalloc, and you'll probably need bigger chunk. The solution is to allocate memory as soon as possible, before memory gets fragmented.

Dark Shikari
See, I read that, and it doesn't make sense to me. I understand using kmalloc for __big__ areas; but for small allocations, why not use vmalloc to avoid fragmenting the physical memory?
FreeMemory
Because you should trust the kernel to do what's best; if it thinks allocating a single chunk is better, it'll do so. vmalloc is only for when you absolutely *must* have a contiguous chunk.
Dark Shikari
I guess that makes sense, but it seems counterintuitive. kmalloc sounds like it ought to be used when performance is of utmost concern (i.e. I can not be plagued by disk IO). Also, what about GFP_ATOMIC?
FreeMemory
+4  A: 

Short answer: download Linux Device Drivers and read the chapter on memory management.

Seriously, there are a lot of subtle issues related to kernel memory management that you need to understand - I spend a lot of my time debugging problems with it.

vmalloc() is very rarely used, because the kernel rarely uses virtual memory. kmalloc() is what is typically used, but you have to know what the consequences of the different flags are and you need a strategy for dealing with what happens when it fails - particularly if you're in an interrupt handler, like you suggested.

Mike Heinz
+7  A: 

You only need to worry about using physically contiguous memory if the buffer will be accessed by a DMA device on a physically addressed bus (like PCI). The trouble is that many system calls have no way to know whether their buffer will eventually be passed to a DMA device: once you pass the buffer to another kernel subsystem, you really cannot know where it is going to go. Even if the kernel does not use the buffer for DMA today, a future development might do so.

vmalloc is often slower than kmalloc, because it may have to remap the buffer space into a virtually contiguous range. kmalloc never remaps, though if not called with GFP_ATOMIC kmalloc can block.

kmalloc is limited in the size of buffer it can provide: 128 KBytes. If you need a really big buffer, you have to use vmalloc or some other mechanism like reserving high memory at boot.

For a system call you don't need to pass GFP_ATOMIC to kmalloc(), you can use GFP_KERNEL. You're not an interrupt handler: the application code enters the kernel context by means of a trap, it is not an interrupt.

DGentry
I thought system calls were entered by triggering int $0x80? (i.e. an interrupt)?
FreeMemory
int $0x80 is a software interrupt, also called a trap. What is meant by interrupt handlers is a hardware interrupt, such as when the user presses a key or moves the moves.
Branan
System calls are for userspace to kernel space transitions...kmalloc is used in the kernel context only??
AIB