1
votes
  1. I have build an character driver in which i am allocating two PAGESIZE buffer using dma_alloc_coherent()

  2. Now i am passing the PHYSICAL address of these BUFFER [src_ptr & dest_ptr] to user space using ioctl() as source_offset and dest_offset .

  3. 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);
    }
    
  4. I am writing 0x77 in user_src_ptr in user space & printing user_src_ptr in user space and dest_src_ptr in KERNEL space . I get correct data for both user and kernel space

  5. I am writng 0x55 in dest_ptr in kernel space & printing dest_ptr in kernel space and usr_dest_ptr in user space. Now for this case I get the correct data e.g. 0X55 in kernel buffer dest_ptr, but the user space buffer of destination always return 0x77 . i.e the data which I have written into usr_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

1 Answers

1
votes

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.