[PATCH 3/4] cpumask: add cpumask_{first,next}_andnot() API

Yury Norov posted 4 patches 10 months, 1 week ago
There is a newer version of this series
[PATCH 3/4] cpumask: add cpumask_{first,next}_andnot() API
Posted by Yury Norov 10 months, 1 week ago
From: Yury Norov [NVIDIA] <yury.norov@gmail.com>

With the lack of the functions, client code has to abuse less efficient
cpumask_nth().

Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
---
 include/linux/cpumask.h | 59 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index 0f816092c891..9067c3411cd0 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -178,6 +178,19 @@ unsigned int cpumask_first_and(const struct cpumask *srcp1, const struct cpumask
 	return find_first_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
 }
 
+/**
+ * cpumask_first_andnot - return the first cpu from *srcp1 & ~*srcp2
+ * @srcp1: the first input
+ * @srcp2: the second input
+ *
+ * Return: >= nr_cpu_ids if no such cpu found.
+ */
+static __always_inline
+unsigned int cpumask_first_andnot(const struct cpumask *srcp1, const struct cpumask *srcp2)
+{
+	return find_first_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
+}
+
 /**
  * cpumask_first_and_and - return the first cpu from *srcp1 & *srcp2 & *srcp3
  * @srcp1: the first input
@@ -284,6 +297,25 @@ unsigned int cpumask_next_and(int n, const struct cpumask *src1p,
 		small_cpumask_bits, n + 1);
 }
 
+/**
+ * cpumask_next_andnot - get the next cpu in *src1p & ~*src2p
+ * @n: the cpu prior to the place to search (i.e. return will be > @n)
+ * @src1p: the first cpumask pointer
+ * @src2p: the second cpumask pointer
+ *
+ * Return: >= nr_cpu_ids if no further cpus set in both.
+ */
+static __always_inline
+unsigned int cpumask_next_andnot(int n, const struct cpumask *src1p,
+				 const struct cpumask *src2p)
+{
+	/* -1 is a legal arg here. */
+	if (n != -1)
+		cpumask_check(n);
+	return find_next_andnot_bit(cpumask_bits(src1p), cpumask_bits(src2p),
+		small_cpumask_bits, n + 1);
+}
+
 /**
  * cpumask_next_and_wrap - get the next cpu in *src1p & *src2p, starting from
  *			   @n+1. If nothing found, wrap around and start from
@@ -458,6 +490,33 @@ unsigned int cpumask_any_and_but(const struct cpumask *mask1,
 	return cpumask_next_and(cpu, mask1, mask2);
 }
 
+/**
+ * cpumask_andnot_any_but - pick an arbitrary cpu from *mask1 & ~*mask2, but not this one.
+ * @mask1: the first input cpumask
+ * @mask2: the second input cpumask
+ * @cpu: the cpu to ignore
+ *
+ * If @cpu == -1, the function returns the first matching cpu.
+ * Returns >= nr_cpu_ids if no cpus set.
+ */
+static __always_inline
+unsigned int cpumask_andnot_any_but(const struct cpumask *mask1,
+				    const struct cpumask *mask2,
+				    unsigned int cpu)
+{
+	unsigned int i;
+
+	/* -1 is a legal arg here. */
+	if (cpu != -1)
+		cpumask_check(cpu);
+
+	i = cpumask_first_andnot(mask1, mask2);
+	if (i != cpu)
+		return i;
+
+	return cpumask_next_andnot(cpu, mask1, mask2);
+}
+
 /**
  * cpumask_nth - get the Nth cpu in a cpumask
  * @srcp: the cpumask pointer
-- 
2.43.0
Re: [PATCH 3/4] cpumask: add cpumask_{first,next}_andnot() API
Posted by James Morse 9 months, 2 weeks ago
Hi Yury,

On 07/04/2025 16:38, Yury Norov wrote:
> From: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> 
> With the lack of the functions, client code has to abuse less efficient
> cpumask_nth().


> diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
> index 0f816092c891..9067c3411cd0 100644
> --- a/include/linux/cpumask.h
> +++ b/include/linux/cpumask.h

> @@ -458,6 +490,33 @@ unsigned int cpumask_any_and_but(const struct cpumask *mask1,
>  	return cpumask_next_and(cpu, mask1, mask2);
>  }
>  
> +/**
> + * cpumask_andnot_any_but - pick an arbitrary cpu from *mask1 & ~*mask2, but not this one.
> + * @mask1: the first input cpumask
> + * @mask2: the second input cpumask
> + * @cpu: the cpu to ignore
> + *
> + * If @cpu == -1, the function returns the first matching cpu.
> + * Returns >= nr_cpu_ids if no cpus set.
> + */
> +static __always_inline
> +unsigned int cpumask_andnot_any_but(const struct cpumask *mask1,
> +				    const struct cpumask *mask2,
> +				    unsigned int cpu)

Nit: Shouldn't this be named cpumask_any_andnot_but()?
It's any cpu from the first-mask, then 'andnot' the second. This fits with the other
instances of this, e.g. cpumask_any_and_but().


> +{
> +	unsigned int i;
> +
> +	/* -1 is a legal arg here. */
> +	if (cpu != -1)
> +		cpumask_check(cpu);
> +
> +	i = cpumask_first_andnot(mask1, mask2);
> +	if (i != cpu)
> +		return i;
> +
> +	return cpumask_next_andnot(cpu, mask1, mask2);
> +}


Thanks,

James
Re: [PATCH 3/4] cpumask: add cpumask_{first,next}_andnot() API
Posted by Reinette Chatre 9 months, 3 weeks ago
Hi Yury,

On 4/7/25 8:38 AM, Yury Norov wrote:

...

> @@ -284,6 +297,25 @@ unsigned int cpumask_next_and(int n, const struct cpumask *src1p,
>  		small_cpumask_bits, n + 1);
>  }
>  
> +/**
> + * cpumask_next_andnot - get the next cpu in *src1p & ~*src2p
> + * @n: the cpu prior to the place to search (i.e. return will be > @n)
> + * @src1p: the first cpumask pointer
> + * @src2p: the second cpumask pointer
> + *
> + * Return: >= nr_cpu_ids if no further cpus set in both.
> + */
> +static __always_inline
> +unsigned int cpumask_next_andnot(int n, const struct cpumask *src1p,
> +				 const struct cpumask *src2p)

Looks like the custom followed here is to name parameter that can have
-1 as value "n" and let it have type "int". Should this also apply to
cpumask_andnot_any_but(), cpumask_any_but(), and cpumask_any_and_but()
modified/introduced in this series?

> +{
> +	/* -1 is a legal arg here. */
> +	if (n != -1)
> +		cpumask_check(n);
> +	return find_next_andnot_bit(cpumask_bits(src1p), cpumask_bits(src2p),
> +		small_cpumask_bits, n + 1);
> +}
> +
>  /**
>   * cpumask_next_and_wrap - get the next cpu in *src1p & *src2p, starting from
>   *			   @n+1. If nothing found, wrap around and start from
> @@ -458,6 +490,33 @@ unsigned int cpumask_any_and_but(const struct cpumask *mask1,
>  	return cpumask_next_and(cpu, mask1, mask2);
>  }
>  
> +/**
> + * cpumask_andnot_any_but - pick an arbitrary cpu from *mask1 & ~*mask2, but not this one.
> + * @mask1: the first input cpumask
> + * @mask2: the second input cpumask
> + * @cpu: the cpu to ignore
> + *
> + * If @cpu == -1, the function returns the first matching cpu.
> + * Returns >= nr_cpu_ids if no cpus set.
> + */
> +static __always_inline
> +unsigned int cpumask_andnot_any_but(const struct cpumask *mask1,
> +				    const struct cpumask *mask2,
> +				    unsigned int cpu)

Since -1 is legal argument I expect "cpu" to be int.

> +{
> +	unsigned int i;
> +
> +	/* -1 is a legal arg here. */
> +	if (cpu != -1)
> +		cpumask_check(cpu);
> +
> +	i = cpumask_first_andnot(mask1, mask2);
> +	if (i != cpu)
> +		return i;
> +
> +	return cpumask_next_andnot(cpu, mask1, mask2);
> +}
> +
>  /**
>   * cpumask_nth - get the Nth cpu in a cpumask
>   * @srcp: the cpumask pointer

Reinette
Re: [PATCH 3/4] cpumask: add cpumask_{first,next}_andnot() API
Posted by Yury Norov 9 months, 3 weeks ago
On Wed, Apr 23, 2025 at 02:28:38PM -0700, Reinette Chatre wrote:
> Hi Yury,
> 
> On 4/7/25 8:38 AM, Yury Norov wrote:
> 
> ...
> 
> > @@ -284,6 +297,25 @@ unsigned int cpumask_next_and(int n, const struct cpumask *src1p,
> >  		small_cpumask_bits, n + 1);
> >  }
> >  
> > +/**
> > + * cpumask_next_andnot - get the next cpu in *src1p & ~*src2p
> > + * @n: the cpu prior to the place to search (i.e. return will be > @n)
> > + * @src1p: the first cpumask pointer
> > + * @src2p: the second cpumask pointer
> > + *
> > + * Return: >= nr_cpu_ids if no further cpus set in both.
> > + */
> > +static __always_inline
> > +unsigned int cpumask_next_andnot(int n, const struct cpumask *src1p,
> > +				 const struct cpumask *src2p)
> 
> Looks like the custom followed here is to name parameter that can have
> -1 as value "n" and let it have type "int". Should this also apply to
> cpumask_andnot_any_but(), cpumask_any_but(), and cpumask_any_and_but()
> modified/introduced in this series?
> 
> > +{
> > +	/* -1 is a legal arg here. */
> > +	if (n != -1)
> > +		cpumask_check(n);
> > +	return find_next_andnot_bit(cpumask_bits(src1p), cpumask_bits(src2p),
> > +		small_cpumask_bits, n + 1);
> > +}
> > +
> >  /**
> >   * cpumask_next_and_wrap - get the next cpu in *src1p & *src2p, starting from
> >   *			   @n+1. If nothing found, wrap around and start from
> > @@ -458,6 +490,33 @@ unsigned int cpumask_any_and_but(const struct cpumask *mask1,
> >  	return cpumask_next_and(cpu, mask1, mask2);
> >  }
> >  
> > +/**
> > + * cpumask_andnot_any_but - pick an arbitrary cpu from *mask1 & ~*mask2, but not this one.
> > + * @mask1: the first input cpumask
> > + * @mask2: the second input cpumask
> > + * @cpu: the cpu to ignore
> > + *
> > + * If @cpu == -1, the function returns the first matching cpu.
> > + * Returns >= nr_cpu_ids if no cpus set.
> > + */
> > +static __always_inline
> > +unsigned int cpumask_andnot_any_but(const struct cpumask *mask1,
> > +				    const struct cpumask *mask2,
> > +				    unsigned int cpu)
> 
> Since -1 is legal argument I expect "cpu" to be int.

Yes, you're right. I overlooked this. Will fix in v2