diff mbox series

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

Message ID 20210926161259.238054-8-namit@vmware.com (mailing list archive)
State New
Headers show
Series mm/madvise: support process_madvise(MADV_DONTNEED) | expand

Commit Message

Nadav Amit Sept. 26, 2021, 4:12 p.m. UTC
From: Nadav Amit <namit@vmware.com>

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@redhat.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Colin Cross <ccross@google.com>
Cc: Suren Baghdasarya <surenb@google.com>
Cc: Mike Rapoport <rppt@linux.vnet.ibm.com>
Signed-off-by: Nadav Amit <namit@vmware.com>
---
 mm/madvise.c | 40 +++++++++++++++-------------------------
 1 file changed, 15 insertions(+), 25 deletions(-)
diff mbox series

Patch

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,