LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2010, 04:07 PM   #1
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Rep: Reputation: 15
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
 
Old 06-16-2010, 04:24 PM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by yousafsajjad View Post
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.
 
Old 06-16-2010, 04:43 PM   #3
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
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.
 
Old 06-16-2010, 04:47 PM   #4
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by yousafsajjad View Post
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 View Post
I would really appreciate if you can elaborate what do i need to do use kmalloc.
This is not possible with virtual memory!
 
Old 06-16-2010, 04:54 PM   #5
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
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.
 
Old 06-16-2010, 04:58 PM   #6
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by yousafsajjad View Post
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.
 
Old 06-16-2010, 05:01 PM   #7
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by yousafsajjad View Post
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.
 
Old 06-16-2010, 05:07 PM   #8
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by JohnGraham View Post
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 View Post
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 ..
 
Old 06-16-2010, 05:15 PM   #9
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
Thanks John. I really appreciate it.
 
Old 06-16-2010, 05:15 PM   #10
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by yousafsajjad View Post
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.
 
Old 06-16-2010, 05:38 PM   #11
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by JohnGraham View Post
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.
 
Old 06-17-2010, 11:07 AM   #12
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
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
 
Old 06-17-2010, 11:29 AM   #13
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by yousafsajjad View Post
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.
 
Old 06-17-2010, 02:40 PM   #14
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ForzaItalia2006 View Post
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 ....
 
Old 06-17-2010, 03:47 PM   #15
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
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() .. ??
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to find kernel headers such as kmalloc.h manish.ym Linux - Kernel 2 06-16-2010 11:00 AM
[SOLVED] Find Physical address of memory provided by kmalloc raghu2383 Programming 3 10-23-2009 02:25 PM
automake: find header files Ephracis Programming 1 10-17-2008 04:03 PM
how to find the header files in c compiler ss100 Linux - Software 2 12-12-2004 11:04 PM
xv can't find X11 header files tunedLow Linux - Software 9 07-02-2002 01:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration