writing on mmap() ?

Andrea Gasparini gaspa at yattaweb.it
Thu Sep 15 04:03:18 EDT 2011


Hi, 
i've got a working version of an mmap()'ed I/O, though read-only.
A device puts its data on RAM through dma and userspace can read it (and 
consequentely dispatch it to other devices).

Now, I need to write some data on the buffer, before the data is sent out.
So, I added PROT_WRITE to the userspace mmap() and <bang>. :)

Data went written on the virtual address, by the userspace, and indeed the 
userspace app can read it correcly, but... the physical memory seems 
untouched.

Briefly, my driver allocate a page of memory this way:
    buffer =  __get_free_pages(GFP_KERNEL | GFP_DMA,  get_order(4K));
and fill it with a pattern:
    for(i=0; i<maxframebuffer; i++)
        buffer[i] = (char)(maxframebuffer - i);

That works fine. (I can inspect the physical memory through a JTAG 
debugger).

Then I define the device_mmap(), nothing more that the usual mmap stub:

int mmaptest_mmap(struct file *filp, struct vm_area_struct *vma)
{
    unsigned int buffersize = 0;

    buffersize = (4K >> PAGE_SHIFT)+1;
    if ( (vma->vm_end - vma->vm_start)>>PAGE_SHIFT > buffersize )
        return -ENOMEM;

    vma->vm_flags |= VM_IO | VM_RESERVED;
    vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);

    if (remap_pfn_range(vma, vma->vm_start, 
                    virt_to_phys(buffer) >> PAGE_SHIFT,
                    vma->vm_end - vma->vm_start, vma->vm_page_prot)){
        return -EAGAIN;
    }
    vma->vm_ops = &simple_remap_vm_ops;
    simple_vma_open(vma);
    return 0;
}

Finally, the userspace program do simply:
    pointer = mmap(blablabla); //with PROT_WRITE and MAP_PRIVATE contained
    *pointer = 0xA5; // just a random constant.

After this, the userspace pointer contains actually 0xA5, while both the 
virtual and physical address of kernel side still contain the values 
initialized before.
( a snapshot of both memories, user side (left) and kernel 
(right) could be found here: http://imageshack.us/f/18/screenshot1fo.png/ )

Some ideas of what happen here? 
Seems there is some kind of COW on pages used with mmap()... is there a way 
to disable it?

Thanks in advance. bye!
-- 
- Andrea Gasparini -
-----------------------------------------------
-------- https://launchpad.net/~gaspa ---------
----- HomePage: http://gaspa.yattaweb.it ------



More information about the Kernelnewbies mailing list