LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 08-12-2004, 06:02 AM   #1
appas
Member
 
Registered: Jul 2004
Posts: 72

Rep: Reputation: 15
Allocating memory for user program variable from kernel module


Hi,

Is it possible to allocate to allocate memory for char * variable defined in user program from
kernel module.

I would put things in detail with the below code.
The below code is part of character driver . "xxx_read" function is called by a user program passing
a char * pointer (buf in below code).

Code:
ssize_t xxx_read (struct file *filp, char *buf, size_t count, loff_t *pos)
{
  printk("PROCESS %i (%s) GOING TO SLEEP\n",current->pid, current->comm);
  interruptible_sleep_on(waitQueueRef);
  // initialize 'modifiedFile"
  // can i use:  buf = (char *) kmalloc(sizeof(modifiedFile), GFP_KERNEL);
  copy_to_user(buf,modifiedFile,strlen(modifiedFile));
  return 0; 
}
What i need is whether i can use kmalloc for the variable "buf" inside the "xxx_read" and free the
memory using "free" in my user program which calls the kernel read function.
 
Old 08-12-2004, 12:41 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
no, you cannot do that. a little hack that you can do to allocate space in a user program is this: adjust the top of the user heap manually, and then stick the data there. however, you CANNNOT call free on that pointer, as malloc has no idea that it even exists. if you wanted to do that:

user_ptr = current->mm->brk;
current->mm->brk += NUM_BYTES;
/* now user_ptr can be copied to */

however that is not something that 'proper' people will want you to do, it's rather hackish. why not just have the user pass a buffer instead?
 
Old 08-12-2004, 10:52 PM   #3
appas
Member
 
Registered: Jul 2004
Posts: 72

Original Poster
Rep: Reputation: 15
Thank you for your response,
I can pass the buffer from user program but i cannot determine the size of buffer at user level which will contain the data read.
I want to fetch all the data with single read without repeating the read function multiple times till end of file is reached.
Any way, I made an workaround, once again thanks for your support.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tool to identify memory leak in loadable kernel module appas Programming 3 06-05-2012 09:56 AM
Allocating Memory in the kernel arunachalam Linux - Software 4 10-12-2005 08:51 AM
seg. fault when allocating memory via a pointer inside a struct elmafiacs Programming 4 02-20-2005 07:26 AM
dynamic memory allocation in kernel module appas Programming 4 09-21-2004 06:36 AM
allocating memory eshwar_ind Programming 6 02-26-2004 07:06 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration