ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to use kmalloc() to allocate contiguous memory on ram. But I can not seem to find the required header file(s) like linux/slab.h. I suppose I do not have the required library and I certainly do not know what and where to look. I would really appreciate some help.
I want to use kmalloc() to allocate contiguous memory on ram... I suppose I do not have the required library
kmalloc() is a function used internally by the Linux kernel when parts of it need memory. It is not part of any library - you would only need it if you were modifying kernel sources and/or writing a device driver.
I suspect you're not doing that - you just want to use the ordinary malloc() from the C standard library.
malloc does not serve the purpose of allocating contiguous memory on ram which is why i need to kmalloc. I would really appreciate if you can elaborate what do i need to do use kmalloc.
malloc does not serve the purpose of allocating contiguous memory on ram which is why i need to kmalloc.
Well, it depends on your point of view! Maybe you've heard of the term 'virtual memory' which abstracts the physical memory so that each process gets a 'contiguous' memory area for its use. If you say malloc(whatever_size), you'll get a contiguous memory area in VIRTUAL MEMORY, but - that's presumably your point - not necessarily in PHYSICAL RAM. But, that's exactly the sense of virtual memory.
But maybe, you could share with us why you definitely need the contiguous are in physical ram?
Quote:
Originally Posted by yousafsajjad
I would really appreciate if you can elaborate what do i need to do use kmalloc.
I am working on a utility library which would serve as an interface to many drivers and other low level operations like SMI operations which require having physically contiguous memory. Allocating virtually contiguous memory does not serve that purpose, I have already tried that.
A picture of the whole idea is to allocate physically contiguous memory, write to it, convert the virtual address to physical address and read the content using memory mapped I/O
Last edited by yousafsajjad; 06-16-2010 at 04:56 PM.
malloc does not serve the purpose of allocating contiguous memory on ram which is why i need to kmalloc. I would really appreciate if you can elaborate what do i need to do use kmalloc.
I cannot think why you would care if your memory is physically contiguous. Are you aware of the difference between physical and virtual memory, and that a user-space application doesn't know the difference?
There's no standard way to do it from user-space, because there's no point. If you ever need access to a specific area of physical memory, the kernel/a device driver will map it into your virtual address space for you.
I am working on a utility library which would serve as an interface to many drivers and other low level operations like SMI operations which require having physically contiguous memory. Allocating virtually contiguous memory does not serve that purpose, I have already tried that.
A picture of the whole idea is to allocate physically contiguous memory, write to it, convert the virtual address to physical address and read the content using memory mapped I/O
Then you want to write a device driver - you could implement the user-space interface as a library, but you'd ultimately end-up in kernel-space at some point.
There's no easy road to this though. Look up "linux device drivers, 3rd edition", which is a good book.
I cannot think why you would care if your memory is physically contiguous. Are you aware of the difference between physical and virtual memory, and that a user-space application doesn't know the difference?
Yeah John, I am aware of the difference between physical and virtual memory and I agree it does not matter for user-space application. But the whole idea is after allocation that physically contiguous space I would call an operation and pass the starting physical address to it and ask it to read 'x' bytes from the memory.
Quote:
Originally Posted by JohnGraham
There's no standard way to do it from user-space, because there's no point. If you ever need access to a specific area of physical memory, the kernel/a device driver will map it into your virtual address space for you.
hmm .. you are right that there is no standard way to it .. so can you tell me how can i do it ... a small example would be really helpful ..
hmm .. you are right that there is no standard way to it .. so can you tell me how can i do it ... a small example would be really helpful ..
I'm not sure there would be such a thing as a "small" example. The best bet would be to have a look at some driver code that has to provide DMA - e.g. ALSA when you use snd_pcm_mmap_begin to access the virtual address of physically mapped memory.
Anyway, learning to write a device driver is the first hurdle, by then you should have a much better idea of what to do.
I have found something that might help me but I need your opinion on it ..
"mlock() locks pages in the address range starting at addr and continuing for len bytes. All pages that contain a part of the specified address range are guaranteed to be resident in RAM when the call returns successfully; the pages are guaranteed to stay in RAM until later unlocked" (http://linux.die.net/man/2/mlockall)
So this would pretty much do what I need but I am not sure how to get the starting address of that physical memory
I have found something that might help me but I need your opinion on it ..
"mlock() locks pages in the address range starting at addr and continuing for len bytes. All pages that contain a part of the specified address range are guaranteed to be resident in RAM when the call returns successfully; the pages are guaranteed to stay in RAM until later unlocked" (http://linux.die.net/man/2/mlockall)
So this would pretty much do what I need but I am not sure how to get the starting address of that physical memory
Hey,
I think this is NOT what you're searching for. The mlock(2) guarantees that your virtual address space range starting from the addr argument is resident in physical memory, but the physical pages belonging to this virtual address might not be contiguous.
Andi
Last edited by ForzaItalia2006; 06-17-2010 at 11:31 AM.
In fact, mlock() can be useful only in case you want to have 4K of contiguous ram space. On a 32-bit system this is the page size you get ...
anyhow just for argument sake .. how can i know the physical address after doing mlock() .. ??
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.