Welcome! Log In Create A New Profile

Map a chunk of memory allocated in user space to another process

Posted by karthik sharma 
Map a chunk of memory allocated in user space to another process
August 22, 2009 01:34AM
I have a chunk of memory allocated from user space (say 4.M.cool smiley.I give this address to a driver module through an IOCTL.I want to map the physical memory to the Virtual address space of another process.I know that I can use a shared memroy for this but my project requires that mapping be done from the kernel space.


In the kernel space I do the following.

1.I use get_user_pages() to get the list of pages which corrosponds to the physical memory.(the allocated space is mlocked())
-this function returns the correct number of pages so i assume it is correct.

2.I want to map the page list thus obtained to the Virtual Address Space of Process 2.


I tried the following two approaches

Approach 1:
for (loop = 0 ;loop < number_pages ; loop++)
{
err = vm_insert_page(vma,(vma->vm_start + (loop & PAGE_MASK)<< PAGE_SHIFT)),page_list[loop]);
if(err)
{
DD(("error value %d\n",err));
break;
}
}

Result:
mmap function from user process 2 is failing.

Approach 2:
down_read(&current->mm->mmap_sem);
for(loop = 0 ;loop < number_pages ; loop++)
{
unsigned long pfn = page_to_pfn(page_list[loop]);
DD(("loop %d pfn %x\n",loop,pfn));
if ((err = remap_pfn_range(vma, start, pfn, PAGE_SIZE,
PAGE_SHARED)) < 0) {
DA( (KERN_WARNING "dag%u: Failed remap!\n", sc->unit) );
return -EAGAIN;
}

start += PAGE_SIZE;
length -= PAGE_SIZE;
}
up_read(&current->mm->mmap_sem);

Result:
I am not able to get the semaphore i assume.If i try without the lock there is a kernel panic.

I am a linux kernel newbie.Any help to this problem will be greatly apprecitated

Cheers
Karthik.
Author:

Your Email:


Subject:


Message: