Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a device that needs a block of memory that is reserved solely for it, without the OS intervening. Is there any way to tell BIOS or the OS that a block of memory is reserved, and it must not use it?

I am using this device on an openSUSE machine.

share|improve this question

3 Answers 3

up vote 16 down vote accepted

What you're asking for is called DMA. You need to write a driver to reserve this memory.

Yes, I realize you said you didn't want the OS to intervene, and a driver becomes part of the OS, but in absence of a driver's reservation, the kernel believes all memory belongs to it. (Unless you tell the kernel to ignore the memory block, per Aaron's answer, that is.)

Chapter 15 (PDF) of "Linux Device Drivers, 3/e" by Rubini, Corbet and Kroah-Hartmann covers DMA and related topics.

If you want an HTML version of this, I found the second-edition version of the chapter elsewhere online. Beware that the 2nd edition is over a decade old now, having come out when kernel 2.4 was new. There's been a lot of work on the memory management subsystem of the kernel since those days, so it may not apply very well any more.

share|improve this answer
    
With DMA can I choose what physical addresses to use? Will the kernel give me a contiguous block of memory? Is it guaranteed to be always available? –  Nathan Fellman May 2 '12 at 4:15
1  
Your questions are answered starting on page 442 of the PDF I pointed you to. –  Warren Young May 2 '12 at 4:34

First enter this command, to check your current setting:

sysctl vm.min_free_kbytes

To change the set value, edit /etc/sysctl.conf. Look for the line:

vm.min_free_kbytes=12888

If it doesn't exist, create it (along with your desired value). The following values are acceptable:

8192
12288
16384
20480

8M is extremely conservative; it can sit at 16M comfortably. Once you change the value, run this and no reboot is needed:

sudo sysctl -p
share|improve this answer
2  
Please don't blank your posts; either delete it or flag it for a moderator to remove for you. –  jasonwryan Jan 24 '13 at 8:29

If you want the OS to totally ignore it, you need to make a memory hole using "memmap." See this reference. For example, if you want 512M at the 2GB barrier, you can put "memmap=512M$2G" on your kernel command line.

You will need to check your dmesg to find a contiguous hole to steal so you don't stomp on any devices; that is specific to your motherboard+cards.

This is not the recommended way to do things - see Warren Young's answer for how to properly do it (kernel drivers + DMA). I'm answering the exact question you asked. If you plan on making this for end users, they will hate you if you do this to them... trust me, that's the only reason I knew this answer.

share|improve this answer
    
Having created such a memory hole, can you then access it with mmap(MAP_ANONYMOUS)? If not, how? I ask here instead of posting a separate question because I assume it's a necessary part of what the OP is asking. –  Warren Young May 2 '12 at 0:12
    
This is not recommended for end users. This is for a probe I'm building for a research project for my MSc. –  Nathan Fellman May 2 '12 at 4:14
1  
While your answer does answer my question directly, Warren's answer seems to be the better way to go about it. :-) –  Nathan Fellman May 2 '12 at 6:32
2  
Just for grins, I looked a little deeper into this, and it appears you need MMAP_FIXED | MMAP_ANON. Without a custom DMA device here to play with, I can't say if it actually does what the OP wanted, but my CentOS box happily gave me an 8 MB block at 512 MB when I said memmap=8M$512M in GRUB. It doesn't even require root access, as I had feared it might. But even if this does do the right thing, I still think you'll probably need a driver to handle interrupts and such. –  Warren Young May 2 '12 at 13:05
2  
@AaronD.Marasco: No, mmap()'d pages are zeroed before userland code gets to see them. It's done for security, so that data doesn't leak from one process to the next. Another reason to use a driver, since you might need to preserve the contents of the DMA buffer at driver load time. Oh and by the way, the arbitrarily-located mmap() call succeeds even without the memmap kernel boot option, at least as long as no one is already using memory located where you asked. The boot option doubtless increases the chances of success, but it's not strictly necessary. –  Warren Young May 3 '12 at 17:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.