do_set_mempolicy() builds a validated policy and installs it into
the current running task.

An in-kernel user that wants to construct a mempolicy wants the same
validation without the install step.

Add mempolicy_create(mode, flags, nodes): it allocates the policy and
contextualises it against the calling task's cpuset, and returns the
mempolicy to the caller (or ERR_PTR on error).

do_set_mempolicy() is deliberately left alone rather than reimplemented
on top of it.  For syscall users, mpol_set_nodemask() and the current
task policy swap must run under the task_lock(current) acquisition.

Export the symbol to kvm for use in guest_memfd integration.

Signed-off-by: Gregory Price <[email protected]>
---
 include/linux/mempolicy.h |  3 +++
 mm/mempolicy.c            | 40 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/include/linux/mempolicy.h b/include/linux/mempolicy.h
index 65c732d440d2f..aef018ad92317 100644
--- a/include/linux/mempolicy.h
+++ b/include/linux/mempolicy.h
@@ -128,6 +128,9 @@ void mpol_free_shared_policy(struct shared_policy *sp);
 struct mempolicy *mpol_shared_policy_lookup(struct shared_policy *sp,
                                            pgoff_t idx);
 
+struct mempolicy *mempolicy_create(unsigned short mode, unsigned short flags,
+                                  nodemask_t *nodes);
+
 struct mempolicy *get_task_policy(struct task_struct *p);
 struct mempolicy *__get_vma_policy(struct vm_area_struct *vma,
                unsigned long addr, pgoff_t *ilx);
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 2ad0a5f18280a..da133ffe0b1c8 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -1085,7 +1085,45 @@ static int mbind_range(struct vma_iterator *vmi, struct 
vm_area_struct *vma,
        return vma_replace_policy(vma, new_pol);
 }
 
-/* Set the process memory policy */
+/**
+ * mempolicy_create - build a validated, cpuset-contextualised mempolicy
+ * @mode: MPOL_* mode
+ * @flags: MPOL_F_* flags
+ * @nodes: target nodemask, or NULL (interpreted per @mode; see mpol_new())
+ *
+ * Creates a new policy and constrains it to the task's cpuset.
+ *
+ * The caller owns the returned reference and frees it with mpol_put().
+ *
+ * Return: the policy (NULL for a default policy), or an ERR_PTR on failure.
+ */
+struct mempolicy *mempolicy_create(unsigned short mode, unsigned short flags,
+               nodemask_t *nodes)
+{
+       struct mempolicy *pol;
+       NODEMASK_SCRATCH(scratch);
+       int err;
+
+       if (!scratch)
+               return ERR_PTR(-ENOMEM);
+
+       pol = mpol_new(mode, flags, nodes);
+       if (IS_ERR(pol))
+               goto out;
+
+       task_lock(current);
+       err = mpol_set_nodemask(pol, nodes, scratch);
+       task_unlock(current);
+       if (err) {
+               mpol_put(pol);
+               pol = ERR_PTR(err);
+       }
+out:
+       NODEMASK_SCRATCH_FREE(scratch);
+       return pol;
+}
+EXPORT_SYMBOL_FOR_MODULES(mempolicy_create, "kvm");
+
 static long do_set_mempolicy(unsigned short mode, unsigned short flags,
                             nodemask_t *nodes)
 {
-- 
2.53.0-Meta


Reply via email to