Re: [PATCH v4 03/13] mm/mempolicy: Add MPOL_PREFERRED_MANY for multiple preferred nodes

2021-04-20 Thread Feng Tang
On Wed, Apr 14, 2021 at 02:50:53PM +0200, Michal Hocko wrote:
> On Wed 17-03-21 11:40:00, Feng Tang wrote:
> > From: Dave Hansen 
> > 
> > MPOL_PREFERRED honors only a single node set in the nodemask.  Add the
> > bare define for a new mode which will allow more than one.
> > 
> > The patch does all the plumbing without actually adding the new policy
> > type.
> > 
> > v2:
> > Plumb most MPOL_PREFERRED_MANY without exposing UAPI (Ben)
> > Fixes for checkpatch (Ben)
> > 
> > Link: 
> > https://lore.kernel.org/r/20200630212517.308045-4-ben.widaw...@intel.com
> > Co-developed-by: Ben Widawsky 
> > Signed-off-by: Ben Widawsky 
> > Signed-off-by: Dave Hansen 
> > Signed-off-by: Feng Tang 
> > ---
> >  mm/mempolicy.c | 46 --
> >  1 file changed, 40 insertions(+), 6 deletions(-)
> > 
> > diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> > index 2b1e0e4..1228d8e 100644
> > --- a/mm/mempolicy.c
> > +++ b/mm/mempolicy.c
> > @@ -31,6 +31,9 @@
> >   *but useful to set in a VMA when you have a non default
> >   *process policy.
> >   *
> > + * preferred many Try a set of nodes first before normal fallback. This is
> > + *similar to preferred without the special case.
> > + *
> >   * defaultAllocate on the local node first, or when on a VMA
> >   *use the process policy. This is what Linux always did
> >   *   in a NUMA aware kernel and still does by, ahem, default.
> > @@ -105,6 +108,8 @@
> >  
> >  #include "internal.h"
> >  
> > +#define MPOL_PREFERRED_MANY MPOL_MAX
> > +
> >  /* Internal flags */
> >  #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0)   /* Skip checks 
> > for continuous vmas */
> >  #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1) /* Invert check 
> > for nodemask */
> > @@ -175,7 +180,7 @@ struct mempolicy *get_task_policy(struct task_struct *p)
> >  static const struct mempolicy_operations {
> > int (*create)(struct mempolicy *pol, const nodemask_t *nodes);
> > void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes);
> > -} mpol_ops[MPOL_MAX];
> > +} mpol_ops[MPOL_MAX + 1];
> >  
> >  static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
> >  {
> > @@ -415,7 +420,7 @@ void mpol_rebind_mm(struct mm_struct *mm, nodemask_t 
> > *new)
> > mmap_write_unlock(mm);
> >  }
> >  
> > -static const struct mempolicy_operations mpol_ops[MPOL_MAX] = {
> > +static const struct mempolicy_operations mpol_ops[MPOL_MAX + 1] = {
> > [MPOL_DEFAULT] = {
> > .rebind = mpol_rebind_default,
> > },
> > @@ -432,6 +437,10 @@ static const struct mempolicy_operations 
> > mpol_ops[MPOL_MAX] = {
> > .rebind = mpol_rebind_nodemask,
> > },
> > /* [MPOL_LOCAL] - see mpol_new() */
> > +   [MPOL_PREFERRED_MANY] = {
> > +   .create = NULL,
> > +   .rebind = NULL,
> > +   },
> >  };
> 
> I do get that you wanted to keep MPOL_PREFERRED_MANY unaccessible for
> the userspace but wouldn't it be much easier to simply check in two
> syscall entries rather than playing thise MAX+1 games which make the
> review more complicated than necessary?

I will check this way, and currently the user input paramter
handling are quite complex.

Also the sanity check in kernel_mbind() and kernel_set_mempolicy()
are almost identical, which can be unified.

> >  
> >  static int migrate_page_add(struct page *page, struct list_head *pagelist,
> > @@ -924,6 +933,9 @@ static void get_policy_nodemask(struct mempolicy *p, 
> > nodemask_t *nodes)
> > case MPOL_INTERLEAVE:
> > *nodes = p->v.nodes;
> > break;
> > +   case MPOL_PREFERRED_MANY:
> > +   *nodes = p->v.preferred_nodes;
> > +   break;
> > case MPOL_PREFERRED:
> > if (!(p->flags & MPOL_F_LOCAL))
> > *nodes = p->v.preferred_nodes;
> 
> Why those two do a slightly different thing? Is this because unlike
> MPOL_PREFERRED it can never have MPOL_F_LOCAL cleared? If that is the
> case I would still stick the two together and use the same code for
> both to make the code easier to follow. Now that both use the same
> nodemask it should really be just about syscall inputs sanitization and
> to keep the original behavior for MPOL_PREFERRED.
> 
> [...]

Our intention is to make MPOL_PREFERRED_MANY be similar to
MPOL_PREFERRED, except it perfers multiple nodes. So will try to
achieve this in following version.

Also for MPOL_LOCAL and MPOL_PREFERRED, current code logic is
turning 'MPOL_LOCAL' to 'MPOL_PREFERRED' with MPOL_F_LOCAL set.
I don't understand why not use the other way around, that
turning MPOL_PREFERRED with empty nodemask to MPOL_LOCAL, which
looks more logical.

Thanks,
Feng

> > @@ -2072,6 +2087,9 @@ bool init_nodemask_of_mempolicy(nodemask_t *mask)
> > task_lock(current);
> > mempolicy = current->mempolicy;
> > switch (mempolicy->mode) {
> > +   case MPOL_PREFERRED_MANY:
> > +

Re: [PATCH v4 03/13] mm/mempolicy: Add MPOL_PREFERRED_MANY for multiple preferred nodes

2021-04-14 Thread Michal Hocko
On Wed 17-03-21 11:40:00, Feng Tang wrote:
> From: Dave Hansen 
> 
> MPOL_PREFERRED honors only a single node set in the nodemask.  Add the
> bare define for a new mode which will allow more than one.
> 
> The patch does all the plumbing without actually adding the new policy
> type.
> 
> v2:
> Plumb most MPOL_PREFERRED_MANY without exposing UAPI (Ben)
> Fixes for checkpatch (Ben)
> 
> Link: https://lore.kernel.org/r/20200630212517.308045-4-ben.widaw...@intel.com
> Co-developed-by: Ben Widawsky 
> Signed-off-by: Ben Widawsky 
> Signed-off-by: Dave Hansen 
> Signed-off-by: Feng Tang 
> ---
>  mm/mempolicy.c | 46 --
>  1 file changed, 40 insertions(+), 6 deletions(-)
> 
> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> index 2b1e0e4..1228d8e 100644
> --- a/mm/mempolicy.c
> +++ b/mm/mempolicy.c
> @@ -31,6 +31,9 @@
>   *but useful to set in a VMA when you have a non default
>   *process policy.
>   *
> + * preferred many Try a set of nodes first before normal fallback. This is
> + *similar to preferred without the special case.
> + *
>   * defaultAllocate on the local node first, or when on a VMA
>   *use the process policy. This is what Linux always did
>   * in a NUMA aware kernel and still does by, ahem, default.
> @@ -105,6 +108,8 @@
>  
>  #include "internal.h"
>  
> +#define MPOL_PREFERRED_MANY MPOL_MAX
> +
>  /* Internal flags */
>  #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0) /* Skip checks for 
> continuous vmas */
>  #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1)   /* Invert check 
> for nodemask */
> @@ -175,7 +180,7 @@ struct mempolicy *get_task_policy(struct task_struct *p)
>  static const struct mempolicy_operations {
>   int (*create)(struct mempolicy *pol, const nodemask_t *nodes);
>   void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes);
> -} mpol_ops[MPOL_MAX];
> +} mpol_ops[MPOL_MAX + 1];
>  
>  static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
>  {
> @@ -415,7 +420,7 @@ void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
>   mmap_write_unlock(mm);
>  }
>  
> -static const struct mempolicy_operations mpol_ops[MPOL_MAX] = {
> +static const struct mempolicy_operations mpol_ops[MPOL_MAX + 1] = {
>   [MPOL_DEFAULT] = {
>   .rebind = mpol_rebind_default,
>   },
> @@ -432,6 +437,10 @@ static const struct mempolicy_operations 
> mpol_ops[MPOL_MAX] = {
>   .rebind = mpol_rebind_nodemask,
>   },
>   /* [MPOL_LOCAL] - see mpol_new() */
> + [MPOL_PREFERRED_MANY] = {
> + .create = NULL,
> + .rebind = NULL,
> + },
>  };

I do get that you wanted to keep MPOL_PREFERRED_MANY unaccessible for
the userspace but wouldn't it be much easier to simply check in two
syscall entries rather than playing thise MAX+1 games which make the
review more complicated than necessary?

>  
>  static int migrate_page_add(struct page *page, struct list_head *pagelist,
> @@ -924,6 +933,9 @@ static void get_policy_nodemask(struct mempolicy *p, 
> nodemask_t *nodes)
>   case MPOL_INTERLEAVE:
>   *nodes = p->v.nodes;
>   break;
> + case MPOL_PREFERRED_MANY:
> + *nodes = p->v.preferred_nodes;
> + break;
>   case MPOL_PREFERRED:
>   if (!(p->flags & MPOL_F_LOCAL))
>   *nodes = p->v.preferred_nodes;

Why those two do a slightly different thing? Is this because unlike
MPOL_PREFERRED it can never have MPOL_F_LOCAL cleared? If that is the
case I would still stick the two together and use the same code for
both to make the code easier to follow. Now that both use the same
nodemask it should really be just about syscall inputs sanitization and
to keep the original behavior for MPOL_PREFERRED.

[...]
> @@ -2072,6 +2087,9 @@ bool init_nodemask_of_mempolicy(nodemask_t *mask)
>   task_lock(current);
>   mempolicy = current->mempolicy;
>   switch (mempolicy->mode) {
> + case MPOL_PREFERRED_MANY:
> + *mask = mempolicy->v.preferred_nodes;
> + break;
>   case MPOL_PREFERRED:
>   if (mempolicy->flags & MPOL_F_LOCAL)
>   nid = numa_node_id();

Same here

> @@ -2126,6 +2144,9 @@ bool mempolicy_nodemask_intersects(struct task_struct 
> *tsk,
>* nodes in mask.
>*/
>   break;
> + case MPOL_PREFERRED_MANY:
> + ret = nodes_intersects(mempolicy->v.preferred_nodes, *mask);
> + break;

I do not think this is a correct behavior. Preferred policy, whether it
is a single node or a nodemask, is a hint not a requirement. So we
should always treat it as intersecting. I do understand that the naming
can be confusing because intersect operation should indeed check
nodemaska but this is yet another trap of the mempolicy code. It is
only used for the OOM selection.

B

[PATCH v4 03/13] mm/mempolicy: Add MPOL_PREFERRED_MANY for multiple preferred nodes

2021-03-16 Thread Feng Tang
From: Dave Hansen 

MPOL_PREFERRED honors only a single node set in the nodemask.  Add the
bare define for a new mode which will allow more than one.

The patch does all the plumbing without actually adding the new policy
type.

v2:
Plumb most MPOL_PREFERRED_MANY without exposing UAPI (Ben)
Fixes for checkpatch (Ben)

Link: https://lore.kernel.org/r/20200630212517.308045-4-ben.widaw...@intel.com
Co-developed-by: Ben Widawsky 
Signed-off-by: Ben Widawsky 
Signed-off-by: Dave Hansen 
Signed-off-by: Feng Tang 
---
 mm/mempolicy.c | 46 --
 1 file changed, 40 insertions(+), 6 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 2b1e0e4..1228d8e 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -31,6 +31,9 @@
  *but useful to set in a VMA when you have a non default
  *process policy.
  *
+ * preferred many Try a set of nodes first before normal fallback. This is
+ *similar to preferred without the special case.
+ *
  * defaultAllocate on the local node first, or when on a VMA
  *use the process policy. This is what Linux always did
  *   in a NUMA aware kernel and still does by, ahem, default.
@@ -105,6 +108,8 @@
 
 #include "internal.h"
 
+#define MPOL_PREFERRED_MANY MPOL_MAX
+
 /* Internal flags */
 #define MPOL_MF_DISCONTIG_OK (MPOL_MF_INTERNAL << 0)   /* Skip checks for 
continuous vmas */
 #define MPOL_MF_INVERT (MPOL_MF_INTERNAL << 1) /* Invert check for 
nodemask */
@@ -175,7 +180,7 @@ struct mempolicy *get_task_policy(struct task_struct *p)
 static const struct mempolicy_operations {
int (*create)(struct mempolicy *pol, const nodemask_t *nodes);
void (*rebind)(struct mempolicy *pol, const nodemask_t *nodes);
-} mpol_ops[MPOL_MAX];
+} mpol_ops[MPOL_MAX + 1];
 
 static inline int mpol_store_user_nodemask(const struct mempolicy *pol)
 {
@@ -415,7 +420,7 @@ void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
mmap_write_unlock(mm);
 }
 
-static const struct mempolicy_operations mpol_ops[MPOL_MAX] = {
+static const struct mempolicy_operations mpol_ops[MPOL_MAX + 1] = {
[MPOL_DEFAULT] = {
.rebind = mpol_rebind_default,
},
@@ -432,6 +437,10 @@ static const struct mempolicy_operations 
mpol_ops[MPOL_MAX] = {
.rebind = mpol_rebind_nodemask,
},
/* [MPOL_LOCAL] - see mpol_new() */
+   [MPOL_PREFERRED_MANY] = {
+   .create = NULL,
+   .rebind = NULL,
+   },
 };
 
 static int migrate_page_add(struct page *page, struct list_head *pagelist,
@@ -924,6 +933,9 @@ static void get_policy_nodemask(struct mempolicy *p, 
nodemask_t *nodes)
case MPOL_INTERLEAVE:
*nodes = p->v.nodes;
break;
+   case MPOL_PREFERRED_MANY:
+   *nodes = p->v.preferred_nodes;
+   break;
case MPOL_PREFERRED:
if (!(p->flags & MPOL_F_LOCAL))
*nodes = p->v.preferred_nodes;
@@ -1895,7 +1907,9 @@ nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy 
*policy)
 /* Return the node id preferred by the given mempolicy, or the given id */
 static int policy_node(gfp_t gfp, struct mempolicy *policy, int nd)
 {
-   if (policy->mode == MPOL_PREFERRED && !(policy->flags & MPOL_F_LOCAL)) {
+   if ((policy->mode == MPOL_PREFERRED ||
+policy->mode == MPOL_PREFERRED_MANY) &&
+   !(policy->flags & MPOL_F_LOCAL)) {
nd = first_node(policy->v.preferred_nodes);
} else {
/*
@@ -1938,6 +1952,7 @@ unsigned int mempolicy_slab_node(void)
return node;
 
switch (policy->mode) {
+   case MPOL_PREFERRED_MANY:
case MPOL_PREFERRED:
/*
 * handled MPOL_F_LOCAL above
@@ -2072,6 +2087,9 @@ bool init_nodemask_of_mempolicy(nodemask_t *mask)
task_lock(current);
mempolicy = current->mempolicy;
switch (mempolicy->mode) {
+   case MPOL_PREFERRED_MANY:
+   *mask = mempolicy->v.preferred_nodes;
+   break;
case MPOL_PREFERRED:
if (mempolicy->flags & MPOL_F_LOCAL)
nid = numa_node_id();
@@ -2126,6 +2144,9 @@ bool mempolicy_nodemask_intersects(struct task_struct 
*tsk,
 * nodes in mask.
 */
break;
+   case MPOL_PREFERRED_MANY:
+   ret = nodes_intersects(mempolicy->v.preferred_nodes, *mask);
+   break;
case MPOL_BIND:
case MPOL_INTERLEAVE:
ret = nodes_intersects(mempolicy->v.nodes, *mask);
@@ -2210,10 +2231,13 @@ alloc_pages_vma(gfp_t gfp, int order, struct 
vm_area_struct *vma,
 * node and don't fall back to other nodes, as the cost of
 * remote accesses would likely offset THP benefits.
 *
-* If the policy is in