LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-11-2008, 08:43 AM   #1
shreyasw
LQ Newbie
 
Registered: Sep 2008
Posts: 7

Rep: Reputation: 0
how to mmap file in kernel module


Hi,
I want to mmap a file from kernel module. I tried using do_mmap it works but only when i do mmap and access memory in same module.
I want to map a memroy in one module and then will call a function( which access this pointer where memory is mapped) from another module.

Now i have 2 modules first say mmap_mod.c and second say tempmod.c

in mmap_mod.c i call do_mmap in init module and take address in a pointer say mem_ptr. Now same module has another function say get_param, this function just access mem_ptr and and print its content. I have exported this function so i can access it from other module.

Now in tempmod.c i just call this get_param function (exported from mmap_mod.c) and expect data in mmaped region to get printed.

it this case it prints kernel stack and gives error "Unable to handle kernel paging request at virtual memory" It fails where i try to access mem_ptr.

same thing if i do mmap in get_param then everything works fine.

I dont know why this is happening.

am i doing something wrong? or does any one have some other solution if yes please help me out.

Thanks
Shreyas
 
Old 09-11-2008, 09:16 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,148
Blog Entries: 4

Rep: Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716Reputation: 2716
Are the addresses "real" ones? (Not virtual...)

Are the page-frames in question locked in memory so they can't be invalidated or paged-out?
 
Old 09-12-2008, 03:55 AM   #3
shreyasw
LQ Newbie
 
Registered: Sep 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Are the addresses "real" ones? (Not virtual...)

I think they are virtual. ( again not sure i just print address of pointer in both cases)

Are the page-frames in question locked in memory so they can't be invalidated or paged-out?

I am not doing any locking also dont know whether do_mmap does it or not. did not see any locks in trace also.
 
Old 09-25-2008, 01:04 AM   #4
shreyasw
LQ Newbie
 
Registered: Sep 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Smile

Well i went to see how do_mmap allocates memory and came to know following things.
1)do_mmap is used by mmap system call and it maps file/any other thing to process address space.

2) when i called do_mmap directly inside kernel module, there was no process address space so kernel uses previous process's address space ( which can be of insmod process) and does mmap in that process's address space.
Therefore when i was trying to access that mapped memory inside same module , i got access to it was in same address space , but same thing when i tried to access same mapped memory from other module, that second module was not having access to mapped area in this address space and thus it segfaulted.

conclusion: do_mmap can not be used to mmap a file inside kernel module. (if u want to access that mapped area from same module then it might work)

Note: I just got this information by going through source code and discussing with some people. I might be wrong in whatever i mentioned above, if so please correct me.

finally i have decide to mmap file in userspace and pass values to kernel module.

Thanks to all who entertained my question.
shreyas
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
mmap for device file problem? advanxiang Linux - Software 0 04-16-2008 03:21 AM
want to read a file from kernel module ksrinivas Linux - Kernel 3 09-11-2006 05:58 AM
Building kernel module from multiple source file in 2.6 kernel yogeshwar_s Programming 1 12-20-2004 09:31 AM
add new kernel module/file system blackzone Linux - General 1 11-07-2004 11:14 PM
kernel BUG at mmap.c:1159! housemusic42 Linux - General 3 03-22-2004 03:49 PM


All times are GMT -5. The time now is 09:45 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration