[RFC PATCH 2/6] mm: mempolicy: Refactor out mpol_init_from_nodemask

From: Ackerley Tng
Date: Thu Apr 13 2023 - 20:12:12 EST


Refactor out mpol_init_from_nodemask() to simplify logic in do_mbind().

mpol_init_from_nodemask() will be used to perform similar
functionality in do_memfd_restricted_bind() in a later patch.

Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx>
---
mm/mempolicy.c | 32 +++++++++++++++++++++-----------
1 file changed, 21 insertions(+), 11 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index a256a241fd1d..a2655b626731 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -1254,6 +1254,25 @@ static struct page *new_page(struct page *page, unsigned long start)
}
#endif

+static long mpol_init_from_nodemask(struct mempolicy *mpol, const nodemask_t *nmask,
+ bool always_unlock)
+{
+ long err;
+ NODEMASK_SCRATCH(scratch);
+
+ if (!scratch)
+ return -ENOMEM;
+
+ /* Cannot take lock before allocating in NODEMASK_SCRATCH */
+ mmap_write_lock(current->mm);
+ err = mpol_set_nodemask(mpol, nmask, scratch);
+ if (always_unlock || err)
+ mmap_write_unlock(current->mm);
+
+ NODEMASK_SCRATCH_FREE(scratch);
+ return err;
+}
+
static long do_mbind(unsigned long start, unsigned long len,
unsigned short mode, unsigned short mode_flags,
nodemask_t *nmask, unsigned long flags)
@@ -1306,17 +1325,8 @@ static long do_mbind(unsigned long start, unsigned long len,

lru_cache_disable();
}
- {
- NODEMASK_SCRATCH(scratch);
- if (scratch) {
- mmap_write_lock(mm);
- err = mpol_set_nodemask(new, nmask, scratch);
- if (err)
- mmap_write_unlock(mm);
- } else
- err = -ENOMEM;
- NODEMASK_SCRATCH_FREE(scratch);
- }
+
+ err = mpol_init_from_nodemask(new, nmask, false);
if (err)
goto mpol_out;

--
2.40.0.634.g4ca3ef3211-goog