问题
I have build an character driver in which i am allocating two PAGESIZE buffer using
dma_alloc_coherent()
Now i am passing the PHYSICAL address of these BUFFER [src_ptr & dest_ptr] to user space using
ioctl()
assource_offset
anddest_offset
.In user space this offeset is used as an offset for mmap call. So on same /dev file say /dev/250 i am making TWO MMAP call
usr_src_ptr= mmap(0,page_size, PROT_READ|PROT_WRITE, MAP_SHARED,dev_FD, src_offset ); if (usr_src_ptr == MAP_FAILED){ printf("USR[UPP]:SOURCE MMAP FAiled \n\n"); close(dev_FD); exit(-1); }else{ printf("USR[UPP]:SOURCE MMAP is %X..\n",usr_src_ptr); } usr_dest_ptr= mmap(0,page_size, PROT_READ|PROT_WRITE,MAP_SHARED, dev_FD,dest_offset ); if (usr_dest_ptr == MAP_FAILED){ printf("USR[UPP]:DEST MMAP FAiled \n\n"); close(dev_FD); exit(-1); }else{ printf("USR[UPP]:DEST MMAP is %X..\n",usr_dest_ptr); }
I am writing
0x77
inuser_src_ptr
in user space & printinguser_src_ptr
in user space anddest_src_ptr
in KERNEL space . I get correct data for both user and kernel spaceI am writng
0x55
indest_ptr
in kernel space & printingdest_ptr
in kernel space andusr_dest_ptr
in user space. Now for this case I get the correct data e.g.0X55
in kernel bufferdest_ptr
, but the user space buffer of destination always return0x77
. i.e the data which I have written intousr_src_ptr
.
Can any one please let me know if we can do two mmap()
operations on same file at different OFFSET?
Instead of:
if ((ret = remap_pfn_range(vma,vma->vm_start,
(virt_to_phys((void *)src_ptr) >> PAGE_SHIFT),
size,vma->vm_page_prot)) < 0) return ret;
The correct one is:
if ((ret = remap_pfn_range(vma,vma->vm_start,
vma->vm_pgoff,
size,vma->vm_page_prot)) < 0)
Thanks BЈовић for your input ....
回答1:
Yes, as you already found out with your example, you can do mmap() on the same file with different offsets in the same process. The man page for mmap(2) says nothing about the offset limits (except for it has to be a multiple of the page size as returned by sysconf(_SC_PAGE_SIZE), and off course it shouldn't go beyond the file).
There is only one limit : when mmap()-ing the same file in one process, you have to use the same file ID. You did this in your example, and that's is why it works fine.
来源:https://stackoverflow.com/questions/14581968/can-we-two-mmap-on-same-dev-file