how to use the memory allocated in kernel?
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!
Hi , On Wed, Apr 18, 2012 at 12:44 AM, 夏业添 <summerxyt@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
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@gmail.com>写道:
Hi ,
On Wed, Apr 18, 2012 at 12:44 AM, 夏业添 <summerxyt@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
2012/4/18 夏业添 <summerxyt@gmail.com>:
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!
As Dave said, unless you know what you are doing, you should really use kmalloc() and friends (kzalloc(), etc) for assigning physically contiguous kernel memory or vmalloc() that assigns virtually (but not necessarily physical) contiguous memory. vmalloc() performs worse that kmalloc() but has more changes to succeed when trying to get large memory chunks. Best regards, -- Javier Martínez Canillas (+34) 682 39 81 69 Barcelona, Spain
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@gmail.com To: dhylands@gmail.com CC: kernelnewbies@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@gmail.com>写道: Hi , On Wed, Apr 18, 2012 at 12:44 AM, 夏业添 <summerxyt@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@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Hi, 2012/4/18 夏业添 <summerxyt@gmail.com>:
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?
In this case yes. The MMU translates virtual addresses to physical addresses. So when dealing with linux you have a virtual memory space, and a totally separate physical memory space. The MMU translates (or maps) virtual addresses to physical addresses. alloc_vm_space allocates some address space from virtual memory, but doesn't set up any mapping to physical memory. So the complete process of allocating memory goes something like this: 1 - Allocate some virtual address space 2 - Allocate some physical pages 3 - Setup the MMU to map your virtual address space to the physical pages. alloc_vm_pages just does step 1. vmalloc and kmalloc do steps 1, 2, and 3 -- Dave Hylands Shuswap, BC, Canada http://www.davehylands.com
participants (4)
-
Dave Hylands -
Javier Martinez Canillas -
卜弋天 -
夏业添