Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-12-2004, 06:02 AM   #1
Registered: Jul 2004
Posts: 72

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


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

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);
  // initialize 'modifiedFile"
  // can i use:  buf = (char *) kmalloc(sizeof(modifiedFile), GFP_KERNEL);
  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
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
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.


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
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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:36 AM.

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