[PATCH 1/4] cpumask: relax cpumask_any_but()

Yury Norov posted 4 patches 10 months, 1 week ago
There is a newer version of this series
[PATCH 1/4] cpumask: relax cpumask_any_but()
Posted by Yury Norov 10 months, 1 week ago
From: Yury Norov [NVIDIA] <yury.norov@gmail.com>

Similarly to other cpumask search functions, accept -1, and consider
it as 'any cpu' hint. This helps users to avoid coding special cases.

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

diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index beff4d26e605..0f816092c891 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -413,6 +413,7 @@ unsigned int cpumask_next_wrap(int n, const struct cpumask *src)
  * @cpu: the cpu to ignore.
  *
  * Often used to find any cpu but smp_processor_id() in a mask.
+ * If @cpu == -1, the function is equivalent to cpumask_any().
  * Return: >= nr_cpu_ids if no cpus set.
  */
 static __always_inline
@@ -420,7 +421,10 @@ unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
 {
 	unsigned int i;
 
-	cpumask_check(cpu);
+	/* -1 is a legal arg here. */
+	if (cpu != -1)
+		cpumask_check(cpu);
+
 	for_each_cpu(i, mask)
 		if (i != cpu)
 			break;
@@ -433,6 +437,7 @@ unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
  * @mask2: the second input cpumask
  * @cpu: the cpu to ignore
  *
+ * If @cpu == -1, the function is equivalent to cpumask_any_and().
  * Returns >= nr_cpu_ids if no cpus set.
  */
 static __always_inline
@@ -442,7 +447,10 @@ unsigned int cpumask_any_and_but(const struct cpumask *mask1,
 {
 	unsigned int i;
 
-	cpumask_check(cpu);
+	/* -1 is a legal arg here. */
+	if (cpu != -1)
+		cpumask_check(cpu);
+
 	i = cpumask_first_and(mask1, mask2);
 	if (i != cpu)
 		return i;
-- 
2.43.0
Re: [PATCH 1/4] cpumask: relax cpumask_any_but()
Posted by Reinette Chatre 9 months, 3 weeks ago
Hi Yury,

On 4/7/25 8:38 AM, Yury Norov wrote:
> From: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> 
> Similarly to other cpumask search functions, accept -1, and consider
> it as 'any cpu' hint. This helps users to avoid coding special cases.
> 
> Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> ---
>  include/linux/cpumask.h | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
> 
> diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
> index beff4d26e605..0f816092c891 100644
> --- a/include/linux/cpumask.h
> +++ b/include/linux/cpumask.h
> @@ -413,6 +413,7 @@ unsigned int cpumask_next_wrap(int n, const struct cpumask *src)
>   * @cpu: the cpu to ignore.
>   *
>   * Often used to find any cpu but smp_processor_id() in a mask.
> + * If @cpu == -1, the function is equivalent to cpumask_any().

Now that -1 is a legal argument, should the "cpu" parameter be of "int" type (instead of
"unsigned int")?


>   * Return: >= nr_cpu_ids if no cpus set.
>   */
>  static __always_inline
> @@ -420,7 +421,10 @@ unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
>  {
>  	unsigned int i;
>  
> -	cpumask_check(cpu);
> +	/* -1 is a legal arg here. */
> +	if (cpu != -1)
> +		cpumask_check(cpu);
> +
>  	for_each_cpu(i, mask)
>  		if (i != cpu)
>  			break;
> @@ -433,6 +437,7 @@ unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
>   * @mask2: the second input cpumask
>   * @cpu: the cpu to ignore
>   *
> + * If @cpu == -1, the function is equivalent to cpumask_any_and().
>   * Returns >= nr_cpu_ids if no cpus set.
>   */
>  static __always_inline
> @@ -442,7 +447,10 @@ unsigned int cpumask_any_and_but(const struct cpumask *mask1,

Same question here regarding type of "cpu" parameter.

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

Reinette
Re: [PATCH 1/4] cpumask: relax cpumask_any_but()
Posted by Yury Norov 9 months, 3 weeks ago
On Wed, Apr 23, 2025 at 02:28:03PM -0700, Reinette Chatre wrote:
> Hi Yury,
> 
> On 4/7/25 8:38 AM, Yury Norov wrote:
> > From: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> > 
> > Similarly to other cpumask search functions, accept -1, and consider
> > it as 'any cpu' hint. This helps users to avoid coding special cases.
> > 
> > Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> > ---
> >  include/linux/cpumask.h | 12 ++++++++++--
> >  1 file changed, 10 insertions(+), 2 deletions(-)
> > 
> > diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
> > index beff4d26e605..0f816092c891 100644
> > --- a/include/linux/cpumask.h
> > +++ b/include/linux/cpumask.h
> > @@ -413,6 +413,7 @@ unsigned int cpumask_next_wrap(int n, const struct cpumask *src)
> >   * @cpu: the cpu to ignore.
> >   *
> >   * Often used to find any cpu but smp_processor_id() in a mask.
> > + * If @cpu == -1, the function is equivalent to cpumask_any().
> 
> Now that -1 is a legal argument, should the "cpu" parameter be of "int" type (instead of
> "unsigned int")?

Yes, you're right. Need to fix this.