[RFC PATCH 7/8] mm/madvise: deduplicate code in madvise_dontneed_free()

From: Nadav Amit
Date: Sun Sep 26 2021 - 19:44:18 EST


From: Nadav Amit <namit@xxxxxxxxxx>

Following the previous patches, madvise_dontneed_single_vma() and
madvise_free_single_vma() have redundant code. Consolidate it together
into madvise_dontneed_free().

Cc: Andrea Arcangeli <aarcange@xxxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Minchan Kim <minchan@xxxxxxxxxx>
Cc: Colin Cross <ccross@xxxxxxxxxx>
Cc: Suren Baghdasarya <surenb@xxxxxxxxxx>
Cc: Mike Rapoport <rppt@xxxxxxxxxxxxxxxxxx>
Signed-off-by: Nadav Amit <namit@xxxxxxxxxx>
---
mm/madvise.c | 40 +++++++++++++++-------------------------
1 file changed, 15 insertions(+), 25 deletions(-)

diff --git a/mm/madvise.c b/mm/madvise.c
index e679cfa94655..9528c38fb6a4 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -786,25 +786,14 @@ static int madvise_free_single_vma(struct mmu_gather *tlb,
struct vm_area_struct *vma,
unsigned long start_addr, unsigned long end_addr)
{
- struct mm_struct *mm = vma->vm_mm;
- struct mmu_notifier_range range;
-
/* MADV_FREE works for only anon vma at the moment */
if (!vma_is_anonymous(vma))
return -EINVAL;

- mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
- start_addr, end_addr);
-
- lru_add_drain();
- update_hiwater_rss(mm);
-
- mmu_notifier_invalidate_range_start(&range);
tlb_start_vma(&tlb, vma);
- walk_page_range(vma->vm_mm, range.start, range.end,
+ walk_page_range(vma->vm_mm, start_addr, end_addr,
&madvise_free_walk_ops, tlb);
tlb_end_vma(tlb, vma);
- mmu_notifier_invalidate_range_end(&range);

return 0;
}
@@ -832,18 +821,7 @@ static long madvise_dontneed_single_vma(struct mmu_gather *tlb,
struct vm_area_struct *vma,
unsigned long start, unsigned long end)
{
- struct mm_struct *mm = vma->vm_mm;
- struct mmu_notifier_range range;
-
- mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, start,
- end);
-
- lru_add_drain();
- update_hiwater_rss(mm);
-
- mmu_notifier_invalidate_range_start(&range);
unmap_single_vma(tlb, vma, start, end, NULL);
- mmu_notifier_invalidate_range_end(&range);

return 0;
}
@@ -855,7 +833,9 @@ static long madvise_dontneed_free(struct mmu_gather *tlb,
int behavior)
{
struct mm_struct *mm = vma->vm_mm;
+ struct mmu_notifier_range range;
unsigned long end = *pend;
+ long ret;

*prev = vma;
if (!can_madv_lru_vma(vma))
@@ -901,10 +881,20 @@ static long madvise_dontneed_free(struct mmu_gather *tlb,
VM_WARN_ON(start >= end);
}

+ mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, start,
+ end);
+
+ lru_add_drain();
+ update_hiwater_rss(mm);
+ mmu_notifier_invalidate_range_start(&range);
+
if (behavior == MADV_DONTNEED)
- return madvise_dontneed_single_vma(tlb, vma, start, end);
+ ret = madvise_dontneed_single_vma(tlb, vma, start, end);
else /* behavior == MADV_FREE */
- return madvise_free_single_vma(tlb, vma, start, end);
+ ret = madvise_free_single_vma(tlb, vma, start, end);
+
+ mmu_notifier_invalidate_range_end(&range);
+ return ret;
}

static long madvise_populate(struct vm_area_struct *vma,
--
2.25.1