memory mapping of vmalloc

From: Prakash Bhurke
Date: Tue Mar 01 2005 - 08:09:32 EST


Hi ,

I am facing a problem -- memory mapping of proc
entry into user space using mmap syscall.
I have written a module which creates a proc entry &
provides read, write, mmap, etc.
Normal read, write etc file operation works, but
mmap is not working.
I am trying to map a vmalloc kernel buffer to user
space using remap_page_range(). In my module, this
function returns success if we call mmap() from user
space, but i can not access content of vmalloc buffer
from user space. Pointer returned by mmap() syscall
seems pointing to other memory page which contains
zeros. I am using linux 2.6.10 kernel on Pentium 4
system.

here is code of module_mmap();
static inline unsigned long kvirt_to_pa(unsigned long
adr)
{
unsigned long kva, ret;

kva = (unsigned long)
page_address(vmalloc_to_page((void *)adr));
kva |= adr & (PAGE_SIZE-1); /* restore the offset */
ret = __pa(kva);
return ret;
}

static int module_mmap(struct file *file, struct
vm_area_struct *vma)
{
unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
unsigned long len = vma->vm_end - vma->vm_start;
unsigned long pos = (unsigned long) test_tsc +
offset;

printk(KERN_INFO "PROCINFO : in mmap started, length
= %ld\n", len);

if (!tscinfo)
return -ENODEV;

if ((offset + len) > PAGE_ALIGN(sizeof(sizeof(struct
test_tsc_info)))))
return -ENXIO;

if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) ==
(VM_SHARED|VM_WRITE)) {
printk("PROCINFO : in mmap attempt to write to
mapping\n");
return -EPERM;
}

vma->vm_flags |= VM_SHM | VM_LOCKED ;

if (remap_pfn_range(vma, vma->vm_start,
kvirt_to_pa(pos) >> PAGE_SHIFT, \
len, vma->vm_page_prot)) {
printk(KERN_INFO "PROCINFO : in mmap remap_pfn_range
returns error\n");
return -EAGAIN;
}

printk(KERN_INFO "PROCINFO : in mmap ret 0 end\n");

return 0;
}

>From user space program i mapping kernel memory like
this
proc_fd = open("/proc/"PROC_ENTRY_FILENAME, O_RDONLY);
mem_base = mmap(NULL, sizeof(struct test_tsc_info),
PROT_READ, MAP_SHARED, proc_fd, 0);

Please let me know what wrong thing i m doing.

Regards,
Prakash.




__________________________________
Do you Yahoo!?
Yahoo! Sports - Sign up for Fantasy Baseball.
http://baseball.fantasysports.yahoo.com/
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/