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 04-03-2008, 04:45 AM   #1
linuxdoniv
Member
 
Registered: Mar 2008
Posts: 63

Rep: Reputation: 15
Unhappy using malloc in kernel module?


Hi,
I want to access task->mm->ANY_FIELD. where task is a pointer of type struct task_struct. But I am sure that without allocating the space to task, I can not access the fields in the structure pointed by mm.

How do we allocate memory in a kernel module.
I can't declare task = (struct task_struct *)malloc(sizeof(task_struct));
How do I go about it.

Thanks in advance.....
 
Old 04-03-2008, 06:28 AM   #2
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Rep: Reputation: 15
kmalloc perhaps?
http://www.win.tue.nl/~aeb/linux/lk/lk-9.html

I have *no* idea about this, just googled
 
Old 04-03-2008, 07:15 AM   #3
hiren_bhatt
Member
 
Registered: Oct 2005
Distribution: FC3,Debian
Posts: 127

Rep: Reputation: 15
You should use kmalloc within a kernel module. Kernel does not have access to library function such as malloc.

This is one thing. I am not clear about your objective but here are few guidelines.

You need not to allocate any memory for the tast_struct. Also I am not sure how you are accessing the tast_struct, but generally you can access using some macro such as current. These macros will return a tas_struct, so you just need a pointer to it. For example

struct task_struct *task = current; Will return tast_struct of the current process and then you can user the tast->"any thing you want". You need to study others macros too. You can also use the list_head structure to iterate over all the tasks.

I you are dealing with kernal 2.6 you may use struct threas_info->tast to access a task_struct.

I hope this helps.
 
Old 04-03-2008, 07:40 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
"Read the source, Luke!"

The full kernel source-code is right there in front of you and it's full of copious working examples, as well as substantial documentation files. You don't have to "figure out" anything you can discover.
 
Old 04-03-2008, 08:56 AM   #5
hiren_bhatt
Member
 
Registered: Oct 2005
Distribution: FC3,Debian
Posts: 127

Rep: Reputation: 15
Quote:
Originally Posted by sundialsvcs View Post
"Read the source, Luke!"
You don't have to "figure out" anything you can discover.
This is correct that you can discover. The kernel source is the best way place to learn itself.

But every one need pointers and correct directions at the starting. Some help will make them learn faster which is good. In my view one should pose as many question as possible but first they should work on it.

As far as kernel internals is concern I would suggest linuxdoniv to read few more things about kernel basics, if really interested in learning. Tldp would be a good source. If you basics are strong you will catch things faster, and do feel free to ask questions.
 
  


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
Can a kernel module call the function of another kernel module sceadu Programming 3 05-02-2011 02:22 AM
2in1 problem thread. (nvidia kernel module vs X module, and strange workbug phenom) htedrom Linux - Software 15 10-04-2007 10:55 PM
[Error] Kernel Module : No kernel module build environment saman007uk Debian 7 09-09-2006 06:34 PM
nvidia driver 8762 kernel module mismatch x module openfun Ubuntu 5 06-27-2006 10:02 PM
kernel module ignorant newbie looking for any one with kernel module knowledge cpoet Slackware 4 11-24-2003 09:37 PM

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

All times are GMT -5. The time now is 07:04 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