mm: introduce __linear_page_index()
Subsystems (14)
PATCH 1/1 mm: introduce __linear_page_index()
There are many places we should get the offset(in PAGE_SIZE unit) of an address within a non-hugetlb vma.
In order to simplify the code, add a new helper __linear_page_index() to do the work.
---
This patch is based on linux-next tree.
--- arch/arm/mm/fault-armv.c | 2 +- arch/powerpc/kvm/book3s_64_mmu_hv.c | 5 ++--- arch/x86/mm/hugetlbpage.c | 3 +-- include/linux/pagemap.h | 16 ++++++++++++---- ipc/shm.c | 7 ++----- mm/hugetlb.c | 3 +-- mm/madvise.c | 8 ++++---- mm/memory.c | 10 +++------- mm/mempolicy.c | 6 ++---- mm/mlock.c | 2 +- mm/mprotect.c | 2 +- mm/mremap.c | 14 +++++--------- mm/shmem.c | 5 ++--- virt/kvm/kvm_main.c | 3 +-- 14 files changed, 38 insertions(+), 48 deletions(-)
arch/arm/mm/fault-armv.c +1 -1
@@ -138,7 +138,7 @@ make_coherent(struct address_space *mapping, struct vm_area_struct *vma,pgoff_t pgoff;int aliases =0 ; - pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >>PAGE_SHIFT ); + pgoff =__linear_page_index (vma, addr);/* * If we have any shared mappings that are in the same mm
arch/powerpc/kvm/book3s_64_mmu_hv.c +2 -3
@@ -310,7 +310,7 @@ static long kvmppc_get_guest_page(struct kvm *kvm, unsigned long gfn, !(vma->vm_flags & VM_PFNMAP)) goto up_err; is_io =hpte_cache_bits (pgprot_val (vma->vm_page_prot)); - pfn = vma->vm_pgoff + ((start - vma->vm_start) >>PAGE_SHIFT ); + pfn =__linear_page_index (vma, start);/* check alignment of pfn vs. requested page size */ if (psize >PAGE_SIZE && (pfn & ((psize >>PAGE_SHIFT ) -1 ))) goto up_err;
@@ -658,8 +658,7 @@ int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu, vma =find_vma (current->mm, hva);if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end && (vma->vm_flags &VM_PFNMAP )) { - pfn = vma->vm_pgoff + - ((hva - vma->vm_start) >>PAGE_SHIFT ); + pfn =__linear_page_index (vma, hva); pte_size = psize; is_io =hpte_cache_bits (pgprot_val (vma->vm_page_prot)); write_ok = vma->vm_flags &VM_WRITE ;
arch/x86/mm/hugetlbpage.c +1 -2
@@ -69,8 +69,7 @@ huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) {struct vm_area_struct *vma =find_vma (mm, addr);struct address_space *mapping = vma->vm_file->f_mapping; -pgoff_t idx = ((addr - vma->vm_start) >>PAGE_SHIFT ) + - vma->vm_pgoff; +pgoff_t idx =__linear_page_index (vma, addr);struct vm_area_struct *svma;unsigned long saddr;pte_t *spte =NULL ;
include/linux/pagemap.h +12 -4
@@ -310,15 +310,23 @@ static inline loff_t page_file_offset(struct page *page)extern pgoff_t linear_hugepage_index (struct vm_area_struct *vma,unsigned long address); -static inline pgoff_t linear_page_index (struct vm_area_struct *vma, +/* The offset for an address within a non-hugetlb vma, in PAGE_SIZE unit. */ +static inline pgoff_t __linear_page_index (struct vm_area_struct *vma,unsigned long address) {pgoff_t pgoff; + + pgoff = (address - vma->vm_start) >>PAGE_SHIFT ; +return pgoff + vma->vm_pgoff; +} + +static inline pgoff_t linear_page_index (struct vm_area_struct *vma, +unsigned long address) +{if (unlikely (is_vm_hugetlb_page (vma)))return linear_hugepage_index (vma, address); - pgoff = (address - vma->vm_start) >>PAGE_SHIFT ; - pgoff += vma->vm_pgoff; -return pgoff >> (PAGE_CACHE_SHIFT -PAGE_SHIFT ); +return __linear_page_index (vma, address) >> + (PAGE_CACHE_SHIFT -PAGE_SHIFT ); }extern void __lock_page (struct page *page);
ipc/shm.c +2 -5
@@ -1172,9 +1172,7 @@ SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)* otherwise it starts at this address with no hassles. */ if ((vma->vm_ops == &shm_vm_ops) && - (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) { - - +0 ==__linear_page_index (vma, addr)) { size = vma->vm_file->f_path.dentry->d_inode->i_size;do_munmap (mm, vma->vm_start, vma->vm_end - vma->vm_start);/*
@@ -1201,8 +1199,7 @@ SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)/* finding a matching vma now does not alter retval */ if ((vma->vm_ops == &shm_vm_ops) && - (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) - +0 ==__linear_page_index (vma, addr))do_munmap (mm, vma->vm_start, vma->vm_end - vma->vm_start); vma = next; }
mm/hugetlb.c +1 -2
@@ -2477,8 +2477,7 @@ static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,* from page cache lookup which is in HPAGE_SIZE units. */ address = address &huge_page_mask (h); - pgoff = ((address - vma->vm_start) >>PAGE_SHIFT ) + - vma->vm_pgoff; + pgoff =__linear_page_index (vma, address); mapping = vma->vm_file->f_dentry->d_inode->i_mapping;/*
mm/madvise.c +4 -4
@@ -100,7 +100,7 @@ static long madvise_behavior(struct vm_area_struct * vma, goto out; } - pgoff = vma->vm_pgoff + ((start - vma->vm_start) >>PAGE_SHIFT ); + pgoff =__linear_page_index (vma, start); *prev =vma_merge (mm, *prev, start, end, new_flags, vma->anon_vma, vma->vm_file, pgoff,vma_policy (vma));if (*prev) {
@@ -193,7 +193,7 @@ static void force_shm_swapin_readahead(struct vm_area_struct *vma,swp_entry_t swap;for (; start < end; start +=PAGE_SIZE ) { - index = ((start - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff; + index =__linear_page_index (vma, start); page =find_get_page (mapping, index);if (!radix_tree_exceptional_entry (page)) {
@@ -242,10 +242,10 @@ static long madvise_willneed(struct vm_area_struct * vma, } *prev = vma; - start = ((start - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff; + start =__linear_page_index (vma, start);if (end > vma->vm_end) end = vma->vm_end; - end = ((end - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff; + end =__linear_page_index (vma, end);force_page_cache_readahead (file->f_mapping, file, start, end - start);return 0 ;
mm/memory.c +3 -7
@@ -798,9 +798,7 @@ struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,return NULL ; goto out; }else { -unsigned long off; - off = (addr - vma->vm_start) >>PAGE_SHIFT ; -if (pfn == vma->vm_pgoff + off) +if (pfn ==__linear_page_index (vma, addr))return NULL ;if (!is_cow_mapping (vma->vm_flags))return NULL ;
@@ -3404,11 +3402,9 @@ static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, pte_t *page_table, pmd_t *pmd, unsigned int flags, pte_t orig_pte) { - pgoff_t pgoff = (((address & PAGE_MASK) - - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff; -pte_unmap (page_table ); -return __do_fault (mm, vma, address, pmd, pgoff, flags, orig_pte); +return __do_fault (mm, vma, address, pmd, +__linear_page_index (vma, address), flags, orig_pte); }/*
mm/mempolicy.c +2 -4
@@ -725,8 +725,7 @@ static int mbind_range(struct mm_struct *mm, unsigned long start,if (mpol_equal (vma_policy (vma), new_pol))continue ; - pgoff = vma->vm_pgoff + - ((vmstart - vma->vm_start) >>PAGE_SHIFT ); + pgoff =__linear_page_index (vma, vmstart); prev =vma_merge (mm, prev, vmstart, vmend, vma->vm_flags, vma->anon_vma, vma->vm_file, pgoff, new_pol);
@@ -2257,8 +2256,7 @@ int mpol_misplaced(struct page *page, struct vm_area_struct *vma, unsigned longBUG_ON (addr >= vma->vm_end);BUG_ON (addr < vma->vm_start); - pgoff = vma->vm_pgoff; - pgoff += (addr - vma->vm_start) >>PAGE_SHIFT ; + pgoff =__linear_page_index (vma, addr); polnid =offset_il_node (pol, vma, pgoff);break ;
mm/mlock.c +1 -1
@@ -269,7 +269,7 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev, is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm)) goto out;/* don't set VM_LOCKED, don't count */ - pgoff = vma->vm_pgoff + ((start - vma->vm_start) >>PAGE_SHIFT ); + pgoff =__linear_page_index (vma, start); *prev =vma_merge (mm, *prev, start, end, newflags, vma->anon_vma, vma->vm_file, pgoff,vma_policy (vma));if (*prev) {
mm/mprotect.c +1 -1
@@ -269,7 +269,7 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,/* * First try to merge with previous and/or next vma. */ - pgoff = vma->vm_pgoff + ((start - vma->vm_start) >>PAGE_SHIFT ); + pgoff =__linear_page_index (vma, start); *pprev =vma_merge (mm, *pprev, start, end, newflags, vma->anon_vma, vma->vm_file, pgoff,vma_policy (vma));if (*pprev) {
mm/mremap.c +5 -9
@@ -240,7 +240,7 @@ static unsigned long move_vma(struct vm_area_struct *vma,if (err)return err; - new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >>PAGE_SHIFT ); + new_pgoff =__linear_page_index (vma, old_addr); new_vma =copy_vma (&vma, new_addr, new_len, new_pgoff, &need_rmap_locks);if (!new_vma)
@@ -327,8 +327,7 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,if (vma->vm_flags & (VM_DONTEXPAND |VM_PFNMAP )) goto Efault; - pgoff = (addr - vma->vm_start) >>PAGE_SHIFT ; - pgoff += vma->vm_pgoff; + pgoff =__linear_page_index (vma, addr);if (pgoff + (new_len >>PAGE_SHIFT ) < pgoff) goto Einval; }
@@ -409,9 +408,8 @@ static unsigned long mremap_to(unsigned long addr, unsigned long old_len,if (vma->vm_flags &VM_MAYSHARE ) map_flags |=MAP_SHARED ; - ret =get_unmapped_area (vma->vm_file, new_addr, new_len, vma->vm_pgoff + - ((addr - vma->vm_start) >>PAGE_SHIFT ), - map_flags); + ret =get_unmapped_area (vma->vm_file, new_addr, new_len, +__linear_page_index (vma, addr), map_flags);if (ret & ~PAGE_MASK ) goto out1;
@@ -538,9 +536,7 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, map_flags |=MAP_SHARED ; new_addr =get_unmapped_area (vma->vm_file,0 , new_len, - vma->vm_pgoff + - ((addr - vma->vm_start) >>PAGE_SHIFT ), - map_flags); +__linear_page_index (vma, addr), map_flags);if (new_addr & ~PAGE_MASK ) { ret = new_addr; goto out;
mm/shmem.c +2 -3
@@ -1322,10 +1322,9 @@ static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, unsigned long addr) {struct inode *inode = vma->vm_file->f_path.dentry->d_inode; -pgoff_t index; - index = ((addr - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff; -return mpol_shared_policy_lookup (&SHMEM_I (inode)->policy, index); +return mpol_shared_policy_lookup (&SHMEM_I (inode)->policy, +__linear_page_index (vma, addr)); }#endif
virt/kvm/kvm_main.c +1 -2
@@ -1240,8 +1240,7 @@ static pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,if (vma ==NULL ) pfn =KVM_PFN_ERR_FAULT ;else if ((vma->vm_flags &VM_PFNMAP )) { - pfn = ((addr - vma->vm_start) >>PAGE_SHIFT ) + - vma->vm_pgoff; + pfn =__linear_page_index (vma, addr);BUG_ON (!kvm_is_mmio_pfn (pfn)); }else {if (async &&vma_is_valid (vma, write_fault))
Signature · 8 lines
1.7.0.4 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerQuoted 6 lines · On Tue, 5 Feb 2013 15:03:39 +0800 Huang Shijie <[email protected]> wrote:
There are many places we should get the
offset(in PAGE_SIZE unit)of an address within a non-hugetlb vma.In order to simplify the code, add a new helper
__linear_page_index()to do the work.
Seems nice.
include/linux/pagemap.h static inline loff_t page_file_offset(struct page *page) · line 313
Quoted 8 lines
include/linux/pagemap.h +1
-1@@ -310,15 +310,23 @@ static inline loff_t page_file_offset(struct page *page)extern pgoff_t linear_hugepage_index (struct vm_area_struct *vma,unsigned long address); -static inline pgoff_t linear_page_index(struct vm_area_struct *vma, +/* The offset for an address within a non-hugetlb vma, in PAGE_SIZE unit. */
"The offset into the mapped file for ..."
Quoted 10 lines
include/linux/pagemap.h +7
-0+static inline pgoff_t __linear_page_index (struct vm_area_struct *vma,unsigned long address) {pgoff_t pgoff; + + pgoff = (address - vma->vm_start) >>PAGE_SHIFT ; +return pgoff + vma->vm_pgoff; +} + +static inline pgoff_t linear_page_index(struct vm_area_struct *vma,
include/linux/pagemap.h static inline loff_t page_file_offset(struct page *page) · line 330
Quoted 10 lines
include/linux/pagemap.h +4
-3+ unsigned long address) +{if (unlikely (is_vm_hugetlb_page (vma)))return linear_hugepage_index (vma, address); - pgoff = (address - vma->vm_start) >>PAGE_SHIFT ; - pgoff += vma->vm_pgoff; -return pgoff >> (PAGE_CACHE_SHIFT -PAGE_SHIFT ); +return __linear_page_index (vma, address) >> + (PAGE_CACHE_SHIFT -PAGE_SHIFT ); }
I don't think we need bother creating both linear_page_index() and __linear_page_index(). Realistically, we won't be supporting PAGE_SHIFT!=PAGE_CACHE_SHIFT. And most (or all?) of the sites which you changed should have been using PAGE_CACHE_SHIFT anyway!
include/linux/pagemap.h SYSCALL_DEFINE1(shmdt, char __user *, shmaddr) · line 1202
Quoted 7 lines
include/linux/pagemap.h +1
-2@@ -1201,8 +1199,7 @@ SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)/* finding a matching vma now does not alter retval */ if ((vma->vm_ops == &shm_vm_ops) && - (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) - +0 ==__linear_page_index (vma, addr))
erk, please don't do this - it makes kernel developers fall over in shock. Let's do
__linear_page_index(vma, addr) == 0
(This won't compile if someone forgets a `=', so the usual reason for the backward comparison isn't valid).
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
On Tue 05-02-13 13:27:41, Andrew Morton wrote:
[...]
Quoted 8 lines
Quoted 8 lines
include/linux/pagemap.h +3
-3+static inline pgoff_t linear_page_index (struct vm_area_struct *vma, +unsigned long address) +{if (unlikely (is_vm_hugetlb_page (vma)))return linear_hugepage_index (vma, address); - pgoff = (address - vma->vm_start) >>PAGE_SHIFT ; - pgoff += vma->vm_pgoff; -return pgoff >> (PAGE_CACHE_SHIFT -PAGE_SHIFT );
Quoted 8 lines
include/linux/pagemap.h static inline loff_t page_file_offset(struct page *page) · line 330
Quoted 3 lines
include/linux/pagemap.h +2
-0+return __linear_page_index (vma, address) >> + (PAGE_CACHE_SHIFT -PAGE_SHIFT ); }I don't think we need bother creating both
linear_page_index()and__linear_page_index(). Realistically, we won't be supporting PAGE_SHIFT!=PAGE_CACHE_SHIFT. And most (or all?) of the sites which you changed should have been usingPAGE_CACHE_SHIFTanyway!
Except for hugetlb (huge_pmd_share, unmap_ref_private) which uses PAGE_SHIFT to get an index into mapping. History proves there was some confusion about those in the past (36e4f20a fixing 0c176d52). So __linear_page_index makes some sense here.
Signature · 8 lines
Michal Hocko SUSE Labs -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 17 lines · Andrew Morton
Quoted 14 lines · On Tue, 5 Feb 2013 15:03:39 +0800 Huang Shijie<[email protected]> wrote:
include/linux/pagemap.h +9
-0+static inline pgoff_t __linear_page_index (struct vm_area_struct *vma,unsigned long address) {pgoff_t pgoff; + + pgoff = (address - vma->vm_start)>>PAGE_SHIFT ; +return pgoff + vma->vm_pgoff; +} + +static inline pgoff_t linear_page_index (struct vm_area_struct *vma, +unsigned long address) +{if (unlikely (is_vm_hugetlb_page (vma)))return linear_hugepage_index (vma, address);
Quoted 8 lines
include/linux/pagemap.h static inline loff_t page_file_offset(struct page *page) · line 330
Quoted 6 lines
include/linux/pagemap.h +2
-3- pgoff = (address - vma->vm_start)>>PAGE_SHIFT ; - pgoff += vma->vm_pgoff; -return pgoff>> (PAGE_CACHE_SHIFT -PAGE_SHIFT ); +return __linear_page_index (vma, address)>> + (PAGE_CACHE_SHIFT -PAGE_SHIFT ); }I don't think we need bother creating both
linear_page_index()and__linear_page_index(). Realistically, we won't be supporting
Just as Hocko said, the unmap_ref_private() (in hugetlb.c) may also uses the __linear_page_index(). So it's better to the two helpers : linear_page_index() and __linear_page_index(). do you agree?
thanks
Huang Shijie
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>