Skip to content

Commit a0f7a75

Browse files
Naoya Horiguchitorvalds
authored andcommitted
mm/rmap.c: fix pgoff calculation to handle hugepage correctly
I triggered VM_BUG_ON() in vma_address() when I tried to migrate an anonymous hugepage with mbind() in the kernel v3.16-rc3. This is because pgoff's calculation in rmap_walk_anon() fails to consider compound_order() only to have an incorrect value. This patch introduces page_to_pgoff(), which gets the page's offset in PAGE_CACHE_SIZE. Kirill pointed out that page cache tree should natively handle hugepages, and in order to make hugetlbfs fit it, page->index of hugetlbfs page should be in PAGE_CACHE_SIZE. This is beyond this patch, but page_to_pgoff() contains the point to be fixed in a single function. Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com> Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Hugh Dickins <hughd@google.com> Cc: Rik van Riel <riel@redhat.com> Cc: Hillf Danton <dhillf@gmail.com> Cc: Naoya Horiguchi <nao.horiguchi@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent aed8adb commit a0f7a75

3 files changed

Lines changed: 17 additions & 9 deletions

File tree

‎include/linux/pagemap.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,18 @@ static inline struct page *read_mapping_page(struct address_space *mapping,
398398
return read_cache_page(mapping, index, filler, data);
399399
}
400400

401+
/*
402+
* Get the offset in PAGE_SIZE.
403+
* (TODO: hugepage should have ->index in PAGE_SIZE)
404+
*/
405+
static inline pgoff_t page_to_pgoff(struct page *page)
406+
{
407+
if (unlikely(PageHeadHuge(page)))
408+
return page->index << compound_order(page);
409+
else
410+
return page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
411+
}
412+
401413
/*
402414
* Return byte-offset into filesystem object for page.
403415
*/

‎mm/memory-failure.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ static void collect_procs_anon(struct page *page, struct list_head *to_kill,
435435
if (av == NULL) /* Not actually mapped anymore */
436436
return;
437437

438-
pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
438+
pgoff = page_to_pgoff(page);
439439
read_lock(&tasklist_lock);
440440
for_each_process (tsk) {
441441
struct anon_vma_chain *vmac;
@@ -469,7 +469,7 @@ static void collect_procs_file(struct page *page, struct list_head *to_kill,
469469
mutex_lock(&mapping->i_mmap_mutex);
470470
read_lock(&tasklist_lock);
471471
for_each_process(tsk) {
472-
pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
472+
pgoff_t pgoff = page_to_pgoff(page);
473473
struct task_struct *t = task_early_kill(tsk, force_early);
474474

475475
if (!t)

‎mm/rmap.c‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,7 @@ void page_unlock_anon_vma_read(struct anon_vma *anon_vma)
517517
static inline unsigned long
518518
__vma_address(struct page *page, struct vm_area_struct *vma)
519519
{
520-
pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
521-
522-
if (unlikely(is_vm_hugetlb_page(vma)))
523-
pgoff = page->index << huge_page_order(page_hstate(page));
524-
520+
pgoff_t pgoff = page_to_pgoff(page);
525521
return vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
526522
}
527523

@@ -1639,7 +1635,7 @@ static struct anon_vma *rmap_walk_anon_lock(struct page *page,
16391635
static int rmap_walk_anon(struct page *page, struct rmap_walk_control *rwc)
16401636
{
16411637
struct anon_vma *anon_vma;
1642-
pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1638+
pgoff_t pgoff = page_to_pgoff(page);
16431639
struct anon_vma_chain *avc;
16441640
int ret = SWAP_AGAIN;
16451641

@@ -1680,7 +1676,7 @@ static int rmap_walk_anon(struct page *page, struct rmap_walk_control *rwc)
16801676
static int rmap_walk_file(struct page *page, struct rmap_walk_control *rwc)
16811677
{
16821678
struct address_space *mapping = page->mapping;
1683-
pgoff_t pgoff = page->index << compound_order(page);
1679+
pgoff_t pgoff = page_to_pgoff(page);
16841680
struct vm_area_struct *vma;
16851681
int ret = SWAP_AGAIN;
16861682

0 commit comments

Comments
 (0)