From: Gregory Price <gourry@gourry.net>
To: linux-mm@kvack.org
Cc: kvm@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org, kernel-team@meta.com,
pbonzini@redhat.com, seanjc@google.com,
akpm@linux-foundation.org, david@kernel.org, ziy@nvidia.com,
matthew.brost@intel.com, joshua.hahnjy@gmail.com,
rakie.kim@sk.com, byungchul@sk.com, gourry@gourry.net,
ying.huang@linux.alibaba.com, apopple@nvidia.com,
shuah@kernel.org
Subject: [PATCH 1/5] mm/mempolicy: add mempolicy_create()
Date: Wed, 2 Sep 2026 15:46:53 -0400 [thread overview]
Message-ID: <20260902194657.79075-2-gourry@gourry.net> (raw)
In-Reply-To: <20260902194657.79075-1-gourry@gourry.net>
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 <gourry@gourry.net>
---
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
next prev parent reply other threads:[~2026-09-02 19:47 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-02 19:46 [PATCH 0/5] KVM: guest_memfd: bind backing memory to a NUMA node Gregory Price
2026-09-02 19:46 ` Gregory Price [this message]
2026-09-02 19:46 ` [PATCH 2/5] mm/mempolicy: add mpol_set_shared_policy_range() Gregory Price
2026-09-02 19:46 ` [PATCH 3/5] KVM: guest_memfd: bind backing memory to a NUMA node at creation Gregory Price
2026-09-02 20:00 ` sashiko-bot
2026-09-02 19:46 ` [PATCH 4/5] selftests: KVM: guest_memfd: let the gmem_test() harness bind a node Gregory Price
2026-09-02 19:59 ` sashiko-bot
2026-09-02 19:46 ` [PATCH 5/5] selftests: KVM: guest_memfd: test GUEST_MEMFD_FLAG_BIND_NODE Gregory Price
2026-09-02 20:04 ` sashiko-bot
2026-09-09 22:41 ` [PATCH 0/5] KVM: guest_memfd: bind backing memory to a NUMA node Ackerley Tng
2026-09-09 23:10 ` Gregory Price
2026-09-09 23:23 ` Ackerley Tng
2026-09-09 23:41 ` Gregory Price
2026-09-10 11:32 ` David Hildenbrand (Arm)
2026-09-10 13:39 ` Gregory Price
2026-09-10 14:00 ` David Hildenbrand (Arm)
2026-09-10 11:30 ` David Hildenbrand (Arm)
2026-09-10 13:40 ` Gregory Price
2026-09-10 14:05 ` David Hildenbrand (Arm)
2026-09-10 14:46 ` Gregory Price
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260902194657.79075-2-gourry@gourry.net \
--to=gourry@gourry.net \
--cc=akpm@linux-foundation.org \
--cc=apopple@nvidia.com \
--cc=byungchul@sk.com \
--cc=david@kernel.org \
--cc=joshua.hahnjy@gmail.com \
--cc=kernel-team@meta.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=matthew.brost@intel.com \
--cc=pbonzini@redhat.com \
--cc=rakie.kim@sk.com \
--cc=seanjc@google.com \
--cc=shuah@kernel.org \
--cc=ying.huang@linux.alibaba.com \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.