mm/mempolicy: some fix and semantics cleanup
Subsystems (3)
v3 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:
v3:
* fix logic of mpol_rebind_preferred() (Michal Hocko)
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 | 212 ++++++++++++++++++----------------------- mm/oom_kill.c | 2 +- 4 files changed, 95 insertions(+), 122 deletions(-)
Signature · 1 line
2.7.4
PATCH v3 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
@@ -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
@@ -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
@@ -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
Re: [v3 PATCH 1/3] mm/mempolicy: cleanup nodemask intersection check for oom
Quoted 9 lines · Feng Tang
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.
I would slightly rephrase the above to
"mempolicy_nodemask_intersects seem to be a general purpose mempolicy function. In fact it is partially tailored for the OOM purpose instead. The oom proper is the only existing user so rename the function to make that purpose explicit.
While at it drop the MPOL_INTERLEAVE as those allocations never has a nodemask defined (see alloc_page_interleave) so this is a dead code and a confusing one because MPOL_INTERLEAVE is a hint rather than a hard requirement so it shouldn't be considered during the OOM.
The final code can be reduced to a check for MPOL_BIND which is the only memory policy that is a hard requirement and thus relevant to a constrained OOM logic.
"
Quoted 2 lines
Suggested-by Michal Hocko <[email protected]>Signed-off-by Feng Tang <[email protected]>
To the change itself
Quoted 94 lines
--- 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@@ -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@@ -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@@ -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
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [v3 PATCH 1/3] mm/mempolicy: cleanup nodemask intersection check for oom
Quoted 23 lines · Michal Hocko
Quoted 9 lines · Feng Tang
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.
I would slightly rephrase the above to
"mempolicy_nodemask_intersectsseem to be a general purpose mempolicy function. In fact it is partially tailored for the OOM purpose instead. The oom proper is the only existing user so rename the function to make that purpose explicit.While at it drop the
MPOL_INTERLEAVEas those allocations never has a nodemask defined (seealloc_page_interleave) so this is a dead code and a confusing one becauseMPOL_INTERLEAVEis a hint rather than a hard requirement so it shouldn't be considered during the OOM.
Quoted 4 lines
The final code can be reduced to a check for
MPOL_BINDwhich is the only memory policy that is a hard requirement and thus relevant to a constrained OOM logic.
"
This is much clearer, thanks!
Will change this and the descrition in over-letter.
Quoted 5 lines
Quoted 2 lines
Suggested-by Michal Hocko <[email protected]>Signed-off-by Feng Tang <[email protected]>To the change itself
Acked-by Michal Hocko <[email protected]>
Thanks!
- Feng
Andrew Morton
maintainerRe: [v3 PATCH 1/3] mm/mempolicy: cleanup nodemask intersection check for oom
Quoted 3 lines · On Tue, 1 Jun 2021 19:08:40 +0800 Feng Tang <[email protected]> wrote:
This is much clearer, thanks!
Will change this and the descrition in over-letter.
I pasted Michal's changelog into -mm's mm-mempolicy-cleanup-nodemask-intersection-check-for-oom.patch.
PATCH v3 2/3 mm/mempolicy: don't handle MPOL_LOCAL like a fake MPOL_PREFERRED policy
In reply to Feng TangMPOL_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, the logic of mpol_rebind_preferred() is confusing, as Michal Hocko pointed out:
"
I do believe that rebinding preferred policy is just bogus and it should be dropped altogether on the ground that a preference is a mere hint from userspace where to start the allocation. Unless I am missing something cpusets will be always authoritative for the final placement. The preferred node just acts as a starting point and it should be really preserved when cpusets changes. Otherwise we have a very subtle behavior corner cases.
"
So dump all the tricky transformation between 'prefer' and 'local', and just record the new nodemask of rebinding.
--- include/uapi/linux/mempolicy.h | 1 - mm/mempolicy.c | 131 +++++++++++++++++------------------------ 2 files changed, 55 insertions(+), 77 deletions(-)
include/uapi/linux/mempolicy.h +0 -1
@@ -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 +55 -76
274 lines not on this page · raw message
Signature · 1 line
2.7.4
Michal Hocko
maintainerRe: [v3 PATCH 2/3] mm/mempolicy: don't handle MPOL_LOCAL like a fake MPOL_PREFERRED policy
Quoted 33 lines · Feng Tang
MPOL_LOCALpolicy has been setup as a real policy, but it is still handled like a fakedPOL_PREFERREDpolicy with one internalMPOL_F_LOCALflag 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_LOCALare 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 withMPOL_F_STATIC_NODESflag set, and when it is 'rebind' to a nodemask which doesn't contains the 'preferred' node, it will perform as 'local' policySo make 'local' a real policy instead of a fake 'prefer' one, and kill
MPOL_F_LOCALbit, which can greatly reduce the confusion for code reading.For case 4, the logic of
mpol_rebind_preferred()is confusing, as Michal Hocko pointed out:"
I do believe that rebinding preferred policy is just bogus and it should be dropped altogether on the ground that a preference is a mere hint from userspace where to start the allocation. Unless I am missing something cpusets will be always authoritative for the final placement. The preferred node just acts as a starting point and it should be really preserved when cpusets changes. Otherwise we have a very subtle behavior corner cases.
"
So dump all the tricky transformation between 'prefer' and 'local', and just record the new nodemask of rebinding.
Quoted 2 lines
Suggested-by Michal Hocko <[email protected]>Signed-off-by Feng Tang <[email protected]>
I like this very much! It simplifies a tricky code and also a very dubious behavior. I would like to hear from others whether there might be some userspace depending on this obscure behavior though. One never knows...
Some more notes/questions below
[...]
Quoted 12 lines
mm/mempolicy.c +0
-7@@ -239,25 +240,19 @@ static int mpol_set_nodemask(struct mempolicy *pol, cpuset_current_mems_allowed, node_states[N_MEMORY]);VM_BUG_ON (!nodes); -if (pol->mode ==MPOL_PREFERRED &&nodes_empty (*nodes)) - nodes =NULL ;/* explicit local allocation */ -else { -if (pol->flags &MPOL_F_RELATIVE_NODES ) -mpol_relative_nodemask (&nsc->mask2, nodes, &nsc->mask1); -else -nodes_and (nsc->mask2, *nodes, nsc->mask1);
mm/mempolicy.c static int mpol_set_nodemask(struct mempolicy *pol, · line 247
Quoted 10 lines
mm/mempolicy.c +4
-6-if (mpol_store_user_nodemask (pol)) - pol->w.user_nodemask = *nodes; -else - pol->w.cpuset_mems_allowed = - cpuset_current_mems_allowed; - } +if (pol->flags &MPOL_F_RELATIVE_NODES ) +mpol_relative_nodemask (&nsc->mask2, nodes, &nsc->mask1); +else +nodes_and (nsc->mask2, *nodes, nsc->mask1);
Maybe I've just got lost here but why don't you need to check for the local policy anymore? mpol_new will take care of the MPOL_PREFERRED && nodes_empty special but why do we want/need all this for a local policy at all?
mm/mempolicy.c static int mpol_set_nodemask(struct mempolicy *pol, · line 253
Quoted 9 lines
mm/mempolicy.c +4
-3-if (nodes) - ret = mpol_ops[pol->mode].create (pol, &nsc->mask2); +if (mpol_store_user_nodemask (pol)) + pol->w.user_nodemask = *nodes;else - ret = mpol_ops[pol->mode].create (pol,NULL ); + pol->w.cpuset_mems_allowed = + cpuset_current_mems_allowed;
please use a single line. This is just harder to read. You will cross the line limit but readability should be preferred here.
[...]
I haven't spotted anything else.
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [v3 PATCH 2/3] mm/mempolicy: don't handle MPOL_LOCAL like a fake MPOL_PREFERRED policy
Quoted 57 lines · Michal Hocko
Quoted 33 lines · Feng Tang
MPOL_LOCALpolicy has been setup as a real policy, but it is still handled like a fakedPOL_PREFERREDpolicy with one internalMPOL_F_LOCALflag 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_LOCALare 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 withMPOL_F_STATIC_NODESflag set, and when it is 'rebind' to a nodemask which doesn't contains the 'preferred' node, it will perform as 'local' policySo make 'local' a real policy instead of a fake 'prefer' one, and kill
MPOL_F_LOCALbit, which can greatly reduce the confusion for code reading.For case 4, the logic of
mpol_rebind_preferred()is confusing, as Michal Hocko pointed out:"
I do believe that rebinding preferred policy is just bogus and it should be dropped altogether on the ground that a preference is a mere hint from userspace where to start the allocation. Unless I am missing something cpusets will be always authoritative for the final placement. The preferred node just acts as a starting point and it should be really preserved when cpusets changes. Otherwise we have a very subtle behavior corner cases.
"
So dump all the tricky transformation between 'prefer' and 'local', and just record the new nodemask of rebinding.Quoted 2 lines
Suggested-by Michal Hocko <[email protected]>Signed-off-by Feng Tang <[email protected]>I like this very much! It simplifies a tricky code and also a very dubious behavior. I would like to hear from others whether there might be some userspace depending on this obscure behavior though. One never knows...
Some more notes/questions below
[...]
Quoted 12 lines
mm/mempolicy.c +0
-7@@ -239,25 +240,19 @@ static int mpol_set_nodemask(struct mempolicy *pol, cpuset_current_mems_allowed, node_states[N_MEMORY]);VM_BUG_ON (!nodes); -if (pol->mode ==MPOL_PREFERRED &&nodes_empty (*nodes)) - nodes =NULL ;/* explicit local allocation */ -else { -if (pol->flags &MPOL_F_RELATIVE_NODES ) -mpol_relative_nodemask (&nsc->mask2, nodes, &nsc->mask1); -else -nodes_and (nsc->mask2, *nodes, nsc->mask1);
Quoted 15 lines
mm/mempolicy.c static int mpol_set_nodemask(struct mempolicy *pol, · line 247
Quoted 10 lines
mm/mempolicy.c +4
-6-if (mpol_store_user_nodemask (pol)) - pol->w.user_nodemask = *nodes; -else - pol->w.cpuset_mems_allowed = - cpuset_current_mems_allowed; - } +if (pol->flags &MPOL_F_RELATIVE_NODES ) +mpol_relative_nodemask (&nsc->mask2, nodes, &nsc->mask1); +else +nodes_and (nsc->mask2, *nodes, nsc->mask1);Maybe I've just got lost here but why don't you need to check for the local policy anymore?
mpol_newwill take care of theMPOL_PREFERRED&&nodes_emptyspecial but why do we want/need all this for a local policy at all?
You are right that 'local' policy doesn't need this, it should just return in the early port of this function, like 'default' policy, which can remove the useless nop mpol_new_local().
Quoted 12 lines
mm/mempolicy.c static int mpol_set_nodemask(struct mempolicy *pol, · line 253
Quoted 9 lines
mm/mempolicy.c +4
-3-if (nodes) - ret = mpol_ops[pol->mode].create (pol, &nsc->mask2); +if (mpol_store_user_nodemask (pol)) + pol->w.user_nodemask = *nodes;else - ret = mpol_ops[pol->mode].create (pol,NULL ); + pol->w.cpuset_mems_allowed = + cpuset_current_mems_allowed;please use a single line. This is just harder to read. You will cross the line limit but readability should be preferred here.
Will change.
Thanks,
Feng
Quoted 6 lines
[...]
I haven't spotted anything else.
Signature · 2 lines
Michal Hocko SUSE Labs
PATCH v3 3/3 mm/mempolicy: unify the parameter sanity check for mbind and set_mempolicy
In reply to Feng TangCurrently 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
@@ -1444,26 +1444,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,
@@ -1477,20 +1488,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
Re: [v3 PATCH 3/3] mm/mempolicy: unify the parameter sanity check for mbind and set_mempolicy
Quoted 10 lines · Feng Tang
Currently the
kernel_mbind()andkernel_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]
Signed-off-by Feng Tang <[email protected]>
sanitize_mpol_flags would benefit from some high level comments explaining those modifications but this can be done on top. This looks like a useful cleanup on its own
Thanks!
Quoted 87 lines
--- mm/mempolicy.c | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-)mm/mempolicy.c +29
-18@@ -1444,26 +1444,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,@@ -1477,20 +1488,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
Signature · 2 lines
Michal Hocko SUSE Labs
Andrew Morton
maintainerQuoted 9 lines · On Mon, 31 May 2021 22:05:53 +0800 Feng Tang <[email protected]> wrote:
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.
None of the above is suitable for the [0/n] overall description. I copied-n-pasted the v1 cover letter from the above link. Please check that it is all still correct and up to date. If not, please send along replacement text, thanks.
Hi Andrew,
Thanks for reviewing and taking the patches.
Quoted 9 lines · Andrew Morton
Quoted 7 lines · On Mon, 31 May 2021 22:05:53 +0800 Feng Tang <[email protected]> wrote:
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!
Quoted 7 lines
Quoted 2 lines
The patchset has been run with some sanity test like 'stress-ng' and 'ltp', and no problem found.
None of the above is suitable for the [0/n] overall description. I copied-n-pasted the v1 cover letter from the above link. Please check that it is all still correct and up to date. If not, please send along replacement text, thanks.
I should make the cover-letter more descriptive. The link above is another patchset to introduce a new memory policy MPOL_PREFERRED_MANY, while these 3 patches are preparation work for it, to make it easier for a new policy to be hooked in.
So how about the following text:
Current memory policy code has some confusing and ambiguous part about MPOL_LOCAL policy, as it is handled as a faked MPOL_PREFERRED one, and there are many places having to distinguish them. Also the nodemask intersection check needs cleanup to be more explicit for OOM use, and handle MPOL_INTERLEAVE correctly. This patchset cleans up these and unifies the parameter sanity check for mbind() and set_mempolicy().
Please feel free to modify it, thanks!
- Feng
Michal Hocko
maintainerOn Tue 01-06-21 08:55:13, Feng Tang wrote:
[...]
Quoted 6 lines
Current memory policy code has some confusing and ambiguous part about
MPOL_LOCALpolicy, as it is handled as a fakedMPOL_PREFERREDone, and there are many places having to distinguish them. Also the nodemask intersection check needs cleanup to be more explicit for OOM use, and handleMPOL_INTERLEAVEcorrectly. This patchset cleans up these and unifies the parameter sanity check formbind()andset_mempolicy().
Looks good to me. I would just add that this cleanup helps to make further changes easier (notably MPOL_PREFERRED_MANY)
Signature · 2 lines
Michal Hocko SUSE Labs