LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Can not find header files to use kmalloc() (https://www.linuxquestions.org/questions/programming-9/can-not-find-header-files-to-use-kmalloc-814585/)

yousafsajjad 06-16-2010 04:07 PM

Can not find header files to use kmalloc()
 
Hi,

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.

Thanks

JohnGraham 06-16-2010 04:24 PM

Quote:

Originally Posted by yousafsajjad (Post 4005860)
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.

yousafsajjad 06-16-2010 04:43 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.

ForzaItalia2006 06-16-2010 04:47 PM

Quote:

Originally Posted by yousafsajjad (Post 4005896)
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 (Post 4005896)
I would really appreciate if you can elaborate what do i need to do use kmalloc.

This is not possible with virtual memory!

yousafsajjad 06-16-2010 04:54 PM

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

JohnGraham 06-16-2010 04:58 PM

Quote:

Originally Posted by yousafsajjad (Post 4005896)
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.

JohnGraham 06-16-2010 05:01 PM

Quote:

Originally Posted by yousafsajjad (Post 4005908)
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.

yousafsajjad 06-16-2010 05:07 PM

Quote:

Originally Posted by JohnGraham (Post 4005913)
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 (Post 4005913)
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 ..

yousafsajjad 06-16-2010 05:15 PM

Thanks John. I really appreciate it.

JohnGraham 06-16-2010 05:15 PM

Quote:

Originally Posted by yousafsajjad (Post 4005921)
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.

yousafsajjad 06-16-2010 05:38 PM

Quote:

Originally Posted by JohnGraham (Post 4005927)
Learning to write a device driver is the first hurdle, by then you should have a much better idea of what to do.

Yeah, I hope so. Thanks for your help.

yousafsajjad 06-17-2010 11:07 AM

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

ForzaItalia2006 06-17-2010 11:29 AM

Quote:

Originally Posted by yousafsajjad (Post 4006677)
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

yousafsajjad 06-17-2010 02:40 PM

Quote:

Originally Posted by ForzaItalia2006 (Post 4006695)
the physical pages belonging to this virtual address might not be contiguous

Yeah, I guess you are right. This function does not promise contiguous physical memory ....

yousafsajjad 06-17-2010 03:47 PM

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() .. ??


All times are GMT -5. The time now is 11:12 AM.