Skip to content

Commit 0dd1c7b

Browse files
JoonsooKimtorvalds
authored andcommitted
mm/rmap: extend rmap_walk_xxx() to cope with different cases
There are a lot of common parts in traversing functions, but there are also a little of uncommon parts in it. By assigning proper function pointer on each rmap_walker_control, we can handle these difference correctly. Following are differences we should handle. 1. difference of lock function in anon mapping case 2. nonlinear handling in file mapping case 3. prechecked condition: checking memcg in page_referenced(), checking VM_SHARE in page_mkclean() checking temporary vma in try_to_unmap() 4. exit condition: checking page_mapped() in try_to_unmap() So, in this patch, I introduce 4 function pointers to handle above differences. Signed-off-by: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com> Cc: Mel Gorman <mgorman@suse.de> Cc: Hugh Dickins <hughd@google.com> Cc: Rik van Riel <riel@redhat.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Hillf Danton <dhillf@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 051ac83 commit 0dd1c7b

3 files changed

Lines changed: 51 additions & 8 deletions

File tree

‎include/linux/rmap.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,25 @@ struct anon_vma *page_lock_anon_vma_read(struct page *page);
235235
void page_unlock_anon_vma_read(struct anon_vma *anon_vma);
236236
int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma);
237237

238+
/*
239+
* rmap_walk_control: To control rmap traversing for specific needs
240+
*
241+
* arg: passed to rmap_one() and invalid_vma()
242+
* rmap_one: executed on each vma where page is mapped
243+
* done: for checking traversing termination condition
244+
* file_nonlinear: for handling file nonlinear mapping
245+
* anon_lock: for getting anon_lock by optimized way rather than default
246+
* invalid_vma: for skipping uninterested vma
247+
*/
238248
struct rmap_walk_control {
239249
void *arg;
240250
int (*rmap_one)(struct page *page, struct vm_area_struct *vma,
241251
unsigned long addr, void *arg);
252+
int (*done)(struct page *page);
253+
int (*file_nonlinear)(struct page *, struct address_space *,
254+
struct vm_area_struct *vma);
255+
struct anon_vma *(*anon_lock)(struct page *page);
256+
bool (*invalid_vma)(struct vm_area_struct *vma, void *arg);
242257
};
243258

244259
/*

‎mm/ksm.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,12 +2032,19 @@ int rmap_walk_ksm(struct page *page, struct rmap_walk_control *rwc)
20322032
if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
20332033
continue;
20342034

2035+
if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
2036+
continue;
2037+
20352038
ret = rwc->rmap_one(page, vma,
20362039
rmap_item->address, rwc->arg);
20372040
if (ret != SWAP_AGAIN) {
20382041
anon_vma_unlock_read(anon_vma);
20392042
goto out;
20402043
}
2044+
if (rwc->done && rwc->done(page)) {
2045+
anon_vma_unlock_read(anon_vma);
2046+
goto out;
2047+
}
20412048
}
20422049
anon_vma_unlock_read(anon_vma);
20432050
}

‎mm/rmap.c‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,10 +1684,14 @@ void __put_anon_vma(struct anon_vma *anon_vma)
16841684
}
16851685

16861686
#ifdef CONFIG_MIGRATION
1687-
static struct anon_vma *rmap_walk_anon_lock(struct page *page)
1687+
static struct anon_vma *rmap_walk_anon_lock(struct page *page,
1688+
struct rmap_walk_control *rwc)
16881689
{
16891690
struct anon_vma *anon_vma;
16901691

1692+
if (rwc->anon_lock)
1693+
return rwc->anon_lock(page);
1694+
16911695
/*
16921696
* Note: remove_migration_ptes() cannot use page_lock_anon_vma_read()
16931697
* because that depends on page_mapped(); but not all its usages
@@ -1713,16 +1717,22 @@ static int rmap_walk_anon(struct page *page, struct rmap_walk_control *rwc)
17131717
struct anon_vma_chain *avc;
17141718
int ret = SWAP_AGAIN;
17151719

1716-
anon_vma = rmap_walk_anon_lock(page);
1720+
anon_vma = rmap_walk_anon_lock(page, rwc);
17171721
if (!anon_vma)
17181722
return ret;
17191723

17201724
anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
17211725
struct vm_area_struct *vma = avc->vma;
17221726
unsigned long address = vma_address(page, vma);
1727+
1728+
if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
1729+
continue;
1730+
17231731
ret = rwc->rmap_one(page, vma, address, rwc->arg);
17241732
if (ret != SWAP_AGAIN)
17251733
break;
1734+
if (rwc->done && rwc->done(page))
1735+
break;
17261736
}
17271737
anon_vma_unlock_read(anon_vma);
17281738
return ret;
@@ -1740,15 +1750,26 @@ static int rmap_walk_file(struct page *page, struct rmap_walk_control *rwc)
17401750
mutex_lock(&mapping->i_mmap_mutex);
17411751
vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
17421752
unsigned long address = vma_address(page, vma);
1753+
1754+
if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
1755+
continue;
1756+
17431757
ret = rwc->rmap_one(page, vma, address, rwc->arg);
17441758
if (ret != SWAP_AGAIN)
1745-
break;
1759+
goto done;
1760+
if (rwc->done && rwc->done(page))
1761+
goto done;
17461762
}
1747-
/*
1748-
* No nonlinear handling: being always shared, nonlinear vmas
1749-
* never contain migration ptes. Decide what to do about this
1750-
* limitation to linear when we need rmap_walk() on nonlinear.
1751-
*/
1763+
1764+
if (!rwc->file_nonlinear)
1765+
goto done;
1766+
1767+
if (list_empty(&mapping->i_mmap_nonlinear))
1768+
goto done;
1769+
1770+
ret = rwc->file_nonlinear(page, mapping, vma);
1771+
1772+
done:
17521773
mutex_unlock(&mapping->i_mmap_mutex);
17531774
return ret;
17541775
}

0 commit comments

Comments
 (0)