Skip to contentSkip to conversation

mm/mempolicy: some fix and semantics cleanup

PATCH Superseded Feng Tang 1 participant 4 messages May 28, 2021

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Feng Tang

author
#0 lore raw

v2 cover mm/mempolicy: some fix and semantics cleanup

Hi All,

We've posted v4 patchset introducing a new "perfer-many" memory policy
https://lore.kernel.org/lkml/[email protected]/ , for which Michal Hocko gave many comments while pointing out some problems, and we also found some semantics confusion about 'prefer'
and 'local' policy, as well as some duplicated code. This patchset tries to address them. Please help to review, thanks!

The patchset has been run with some sanity test like 'stress-ng' and 'ltp', and no problem found.

Thanks,
Feng

Changelogs:
v2:
* rename mempolicy_nodemask_intersects() to mempolicy_in_oom_domain() and correct commit log (Michal Hocko)
* change the mpol syscall param sanity check (Michal Hocko)
* combine the 3/4 and 4/4 in v1 into one patch,
and further clean the logic (Michal Hocko)

v1:
* use helper func instead of macro for patch 2/4 (David Rientjes)
* fix a possible null pointer case in patch 3/4
* update commit log for 1/4

RFC v2:
* add for oom check fix patch 1/4
* add the unification patch for mpol preprocess 2/4

Feng Tang (3):
mm/mempolicy: cleanup nodemask intersection check for oom mm/mempolicy: don't handle MPOL_LOCAL like a fake MPOL_PREFERRED
policy
mm/mempolicy: unify the parameter sanity check for mbind and
set_mempolicy

 include/linux/mempolicy.h      |   2 +-
 include/uapi/linux/mempolicy.h |   1 -
 mm/mempolicy.c                 | 205 ++++++++++++++++++++---------------------
 mm/oom_kill.c                  |   2 +-
 4 files changed, 103 insertions(+), 107 deletions(-)
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v2 1/3 mm/mempolicy: cleanup nodemask intersection check for oom

mempolicy_nodemask_intersects() is used in oom case to check if a task may have memory allocated on some memory nodes.

As it's only used by OOM check, rename it to mempolicy_in_oom_domain() to reduce confusion.

As only for 'bind' policy, the nodemask is a force requirement for from where to allocate memory, only do the intesection check for it, and return true for all other policies.

---
 include/linux/mempolicy.h |  2 +-
 mm/mempolicy.c            | 34 +++++++++-------------------------
 mm/oom_kill.c             |  2 +-
 3 files changed, 11 insertions(+), 27 deletions(-)
include/linux/mempolicy.h +1 -1
index 5f1c74d..8773c55 100644
@@ -150,7 +150,7 @@ extern int huge_node(struct vm_area_struct *vma,
 				unsigned long addr, gfp_t gfp_flags,
 				struct mempolicy **mpol, nodemask_t **nodemask);
 extern bool init_nodemask_of_mempolicy(nodemask_t *mask);
-extern bool mempolicy_nodemask_intersects(struct task_struct *tsk,
+extern bool mempolicy_in_oom_domain(struct task_struct *tsk,
 				const nodemask_t *mask);
 extern nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy);
 
mm/mempolicy.c +9 -25
index d79fa29..6795a6a 100644
@@ -2094,16 +2094,16 @@ bool init_nodemask_of_mempolicy(nodemask_t *mask)
 #endif
 
 /*
- * mempolicy_nodemask_intersects
+ * mempolicy_in_oom_domain
  *
- * If tsk's mempolicy is "default" [NULL], return 'true' to indicate default
- * policy.  Otherwise, check for intersection between mask and the policy
- * nodemask for 'bind' or 'interleave' policy.  For 'preferred' or 'local'
- * policy, always return true since it may allocate elsewhere on fallback.
+ * If tsk's mempolicy is "bind", check for intersection between mask and
+ * the policy nodemask. Otherwise, return true for all other policies
+ * including "interleave", as a tsk with "interleave" policy may have
+ * memory allocated from all nodes in system.
  *
  * Takes task_lock(tsk) to prevent freeing of its mempolicy.
  */
-bool mempolicy_nodemask_intersects(struct task_struct *tsk,
+bool mempolicy_in_oom_domain(struct task_struct *tsk,
 					const nodemask_t *mask)
 {
 	struct mempolicy *mempolicy;
@@ -2111,29 +2111,13 @@ bool mempolicy_nodemask_intersects(struct task_struct *tsk,
 
 	if (!mask)
 		return ret;
+
 	task_lock(tsk);
 	mempolicy = tsk->mempolicy;
-	if (!mempolicy)
-		goto out;
-
-	switch (mempolicy->mode) {
-	case MPOL_PREFERRED:
-		/*
-		 * MPOL_PREFERRED and MPOL_F_LOCAL are only preferred nodes to
-		 * allocate from, they may fallback to other nodes when oom.
-		 * Thus, it's possible for tsk to have allocated memory from
-		 * nodes in mask.
-		 */
-		break;
-	case MPOL_BIND:
-	case MPOL_INTERLEAVE:
+	if (mempolicy && mempolicy->mode == MPOL_BIND)
 		ret = nodes_intersects(mempolicy->v.nodes, *mask);
-		break;
-	default:
-		BUG();
-	}
-out:
 	task_unlock(tsk);
+
 	return ret;
 }
 
mm/oom_kill.c +1 -1
index eefd3f5..fcc29e9 100644
@@ -104,7 +104,7 @@ static bool oom_cpuset_eligible(struct task_struct *start,
 			 * mempolicy intersects current, otherwise it may be
 			 * needlessly killed.
 			 */
-			ret = mempolicy_nodemask_intersects(tsk, mask);
+			ret = mempolicy_in_oom_domain(tsk, mask);
 		} else {
 			/*
 			 * This is not a mempolicy constrained oom, so only
Signature · 1 line
2.7.4
author
#2 lore raw

PATCH v2 2/3 mm/mempolicy: don't handle MPOL_LOCAL like a fake MPOL_PREFERRED policy

In reply to Feng Tang

MPOL_LOCAL policy has been setup as a real policy, but it is still handled like a faked POL_PREFERRED policy with one internal MPOL_F_LOCAL flag bit set, and there are many places having to judge the real 'prefer' or the 'local' policy, which are quite confusing.

In current code, there are 4 cases that MPOL_LOCAL are used:
1. user specifies 'local' policy
2. user specifies 'prefer' policy, but with empty nodemask
3. system 'default' policy is used
4. 'prefer' policy + valid 'preferred' node with MPOL_F_STATIC_NODES flag set, and when it is 'rebind' to a nodemask which doesn't contains the 'preferred' node, it will perform as 'local' policy

So make 'local' a real policy instead of a fake 'prefer' one, and kill MPOL_F_LOCAL bit, which can greatly reduce the confusion for code reading.

For case 4, in theory it used to be able to be changed back to 'prefer' policy if it is rebind to a valid nodemask for the second time, but that will involves dirty tricky manipulation and is kind of over-engineering, as 'perfer' is just a hint and if it is once rebind to an invalid nodemask and acts as 'local', it doesn't make much sense to stick to underlying 'prefer' policy. So just handle it as 'local' policy.

---
 include/uapi/linux/mempolicy.h |   1 -
 mm/mempolicy.c                 | 124 +++++++++++++++++++++--------------------
 2 files changed, 63 insertions(+), 62 deletions(-)
include/uapi/linux/mempolicy.h +0 -1
index 4832fd0..19a00bc 100644
@@ -60,7 +60,6 @@ enum {
  * are never OR'ed into the mode in mempolicy API arguments.
  */
 #define MPOL_F_SHARED  (1 << 0)	/* identify shared policies */
-#define MPOL_F_LOCAL   (1 << 1)	/* preferred local allocation */
 #define MPOL_F_MOF	(1 << 3) /* this policy wants migrate on fault */
 #define MPOL_F_MORON	(1 << 4) /* Migrate On protnone Reference On Node */
 
mm/mempolicy.c +63 -61

270 lines not on this page · raw message

Signature · 1 line
2.7.4
author
#3 lore raw

PATCH v2 3/3 mm/mempolicy: unify the parameter sanity check for mbind and set_mempolicy

In reply to Feng Tang

Currently the kernel_mbind() and kernel_set_mempolicy() do almost the same operation for parameter sanity check.

Add a helper function to unify the code to reduce the redundancy, and make it easier for changing the pre-processing code in future.

[thanks to David Rientjes for suggesting using helper function instead of macro]

---
 mm/mempolicy.c | 47 +++++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 18 deletions(-)
mm/mempolicy.c +29 -18
index f9ab05b..e5a3e5e 100644
@@ -1467,26 +1467,37 @@ static int copy_nodes_to_user(unsigned long __user *mask, unsigned long maxnode,
 	return copy_to_user(mask, nodes_addr(*nodes), copy) ? -EFAULT : 0;
 }
 
+static inline int sanitize_mpol_flags(int *mode, unsigned short *flags)
+{
+	*flags = *mode & MPOL_MODE_FLAGS;
+	*mode &= ~MPOL_MODE_FLAGS;
+	if ((unsigned int)(*mode) >= MPOL_MAX)
+		return -EINVAL;
+	if ((*flags & MPOL_F_STATIC_NODES) && (*flags & MPOL_F_RELATIVE_NODES))
+		return -EINVAL;
+
+	return 0;
+}
+
 static long kernel_mbind(unsigned long start, unsigned long len,
 			 unsigned long mode, const unsigned long __user *nmask,
 			 unsigned long maxnode, unsigned int flags)
 {
+	unsigned short mode_flags;
 	nodemask_t nodes;
+	int lmode = mode;
 	int err;
-	unsigned short mode_flags;
 
 	start = untagged_addr(start);
-	mode_flags = mode & MPOL_MODE_FLAGS;
-	mode &= ~MPOL_MODE_FLAGS;
-	if (mode >= MPOL_MAX)
-		return -EINVAL;
-	if ((mode_flags & MPOL_F_STATIC_NODES) &&
-	    (mode_flags & MPOL_F_RELATIVE_NODES))
-		return -EINVAL;
+	err = sanitize_mpol_flags(&lmode, &mode_flags);
+	if (err)
+		return err;
+
 	err = get_nodes(&nodes, nmask, maxnode);
 	if (err)
 		return err;
-	return do_mbind(start, len, mode, mode_flags, &nodes, flags);
+
+	return do_mbind(start, len, lmode, mode_flags, &nodes, flags);
 }
 
 SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len,
@@ -1500,20 +1511,20 @@ SYSCALL_DEFINE6(mbind, unsigned long, start, unsigned long, len,
 static long kernel_set_mempolicy(int mode, const unsigned long __user *nmask,
 				 unsigned long maxnode)
 {
-	int err;
+	unsigned short mode_flags;
 	nodemask_t nodes;
-	unsigned short flags;
+	int lmode = mode;
+	int err;
+
+	err = sanitize_mpol_flags(&lmode, &mode_flags);
+	if (err)
+		return err;
 
-	flags = mode & MPOL_MODE_FLAGS;
-	mode &= ~MPOL_MODE_FLAGS;
-	if ((unsigned int)mode >= MPOL_MAX)
-		return -EINVAL;
-	if ((flags & MPOL_F_STATIC_NODES) && (flags & MPOL_F_RELATIVE_NODES))
-		return -EINVAL;
 	err = get_nodes(&nodes, nmask, maxnode);
 	if (err)
 		return err;
-	return do_set_mempolicy(mode, flags, &nodes);
+
+	return do_set_mempolicy(lmode, mode_flags, &nodes);
 }
 
 SYSCALL_DEFINE3(set_mempolicy, int, mode, const unsigned long __user *, nmask,
Signature · 1 line
2.7.4