[RFC PATCH v2 1/4] mm/madvise: split out mmap locking operations for madvise()

From: SeongJae Park
Date: Thu Jan 16 2025 - 20:31:19 EST


Split out the madvise behavior-dependent mmap_lock operations from
do_madvise(), for easier reuse of the logic in an upcoming change.

Signed-off-by: SeongJae Park <sj@xxxxxxxxxx>
---
mm/madvise.c | 45 ++++++++++++++++++++++++++++++++-------------
1 file changed, 32 insertions(+), 13 deletions(-)

diff --git a/mm/madvise.c b/mm/madvise.c
index 49f3a75046f6..ae0964bc4d88 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -1565,6 +1565,33 @@ int madvise_set_anon_name(struct mm_struct *mm, unsigned long start,
madvise_vma_anon_name);
}
#endif /* CONFIG_ANON_VMA_NAME */
+
+static int madvise_lock(struct mm_struct *mm, int behavior)
+{
+
+#ifdef CONFIG_MEMORY_FAILURE
+ if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
+ return 0;
+#endif
+
+ if (madvise_need_mmap_write(behavior)) {
+ if (mmap_write_lock_killable(mm))
+ return -EINTR;
+ } else {
+ mmap_read_lock(mm);
+ }
+ return 0;
+
+}
+
+static void madvise_unlock(struct mm_struct *mm, int behavior)
+{
+ if (madvise_need_mmap_write(behavior))
+ mmap_write_unlock(mm);
+ else
+ mmap_read_unlock(mm);
+}
+
/*
* The madvise(2) system call.
*
@@ -1641,7 +1668,6 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh
{
unsigned long end;
int error;
- int write;
size_t len;
struct blk_plug plug;

@@ -1663,19 +1689,15 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh
if (end == start)
return 0;

+ error = madvise_lock(mm, behavior);
+ if (error)
+ return error;
+
#ifdef CONFIG_MEMORY_FAILURE
if (behavior == MADV_HWPOISON || behavior == MADV_SOFT_OFFLINE)
return madvise_inject_error(behavior, start, start + len_in);
#endif

- write = madvise_need_mmap_write(behavior);
- if (write) {
- if (mmap_write_lock_killable(mm))
- return -EINTR;
- } else {
- mmap_read_lock(mm);
- }
-
start = untagged_addr_remote(mm, start);
end = start + len;

@@ -1692,10 +1714,7 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh
}
blk_finish_plug(&plug);

- if (write)
- mmap_write_unlock(mm);
- else
- mmap_read_unlock(mm);
+ madvise_unlock(mm, behavior);

return error;
}
--
2.39.5