Santhosh Pai Santhosh Pai - 1 month ago 16
C Question

unsigned long vm_pgoff in vm_area_struct

I was reading the memory management chapter of Linux Device drivers and i came accross the vm_area_struct .The structure has an offfset field namely unsigned long vm_pgoff .

I checked the comment for the member as

/* Offset (within vm_file) in PAGE_SIZE units, *not* PAGE_CACHE_SIZE */
. I'm not quite sure whether it is the offset within the vma region or the offset in side the memory mapped file .

And i have seen driver's mmap implementation code to have the following line to recalculate the offset again by shifting the member by the PAGE_SHIFT bits .

unsigned long off = vma->vm_pgoff << PAGE_SHIFT;


I'm not able to understand the same and any help on this would be great .

Answer

In the source code (file mm.h) of function

 static inline unsigned long do_mmap(struct file *file, unsigned long addr,
unsigned long len, unsigned long prot,
unsigned long flag, unsigned long offset)

I have found expression offset >> PAGE_SHIFT (right shift) that is used as pgoff parameter of do_mmap_pgoff, so it is obvious, to "restore" original offset value we need to do Left Shift with the same PAGE_SHIFT (as function lowmem_page_address do that).