how to use the memory allocated in kernel?

卜弋天 buyit at live.cn
Wed Apr 18 07:44:21 EDT 2012


Hi :    you can find out the meaning in vmalloc.c, function vmalloc().    vmalloc accept only one parameter, the size of memory that you want to allocate.   this function will return the virtual address which you can use to read/write data inside it.   vmalloc hides some implementation details about real memory allocation, for example: the virtual address thatreturned from vmalloc() function is continuous, but physical memory can not continuous.      vmalloc() will first call function alloc_vmap_area(), which you mentioned before, this function will occupy a continuousvirtuall address space in VMALLOC region, note that it only occupies virtual address space, no physical memory are mapped tothese virtual address at this moment. so if you operate at this virtual address as you did before, it is a bug!     vmalloc() then call function __vmalloc_area_node(), which will find out enough non-continuous physical memory, then modifyPTEs of current task, map the virtual address to real physical memory. after this, you can operate at the address returned byalloc_vmap_area(). the example code you did lacks this action, you can try to add this and test again.     i suggest that you use kmalloc() and vmalloc() directly. if you want to allocate small and physical continuous memory, use kmalloc().if you want to allocate very large(e.g. 10MB) and physical non-continuous memory, use vmalloc().  Best Regards
 Date: Wed, 18 Apr 2012 18:00:03 +0800
Subject: Re: how to use the memory allocated in kernel?
From: summerxyt at gmail.com
To: dhylands at gmail.com
CC: kernelnewbies at kernelnewbies.org

Hi Dave,

Thanks for reply. My English is not very good, and so I want to ask about a term:map. Does map mean that create a relationship between the virtual space and physical memory?

Thanks again!


在 2012年4月18日 下午4:17,Dave Hylands <dhylands at gmail.com>写道:

Hi ,



On Wed, Apr 18, 2012 at 12:44 AM, 夏业添 <summerxyt at gmail.com> wrote:

> Hi everyone,

>

> there are some functions can alloc memory in kernel, but it seems that I

> cannot use it directly. Here is my code:

>

> static int mytest_vm(){

>

>         struct vm_struct *v_start;

>

>         v_start=alloc_vm_area(PAGE_SIZE,NULL); //the kernel api has changed,

> I don't understand why there is a second parameter

>         if(v_start==NULL){

>                 printk("cannot alloc page\n");

>                 return -1;

>         }

>

>         sprintf((char *)v_start->addr,"this is a test.\n");

>         printk("after sprintk:%s",(char *)v_start->addr);

>

>         free_vm_area(v_start);

>

>         return 0;

> }

> module_init(mytest_vm);

>

> but it just got a kernel Oops. Can anyone explain this to me? Thanks very

> much!



If my understanding of things is correct, this just allocates virtual

space. That virtual space isn't backed by any physical pages.



The normal kernel allocators are things like kmalloc and vmalloc.



--

Dave Hylands

Shuswap, BC, Canada

http://www.davehylands.com




_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120418/c35d0104/attachment.html 


More information about the Kernelnewbies mailing list