[PATCH 4/4] x86/resctrl: optimize cpumask_any_housekeeping()

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

With the lack of cpumask_andnot_any_but(), users have to abuse
cpumask_nth() functions which are O(N*log(N)), comparing to O(N)
for cpumask_any().

This series adds missing cpumask_andnot_any_but() and makes
cpumask_any_but() understanding the RESCTRL_PICK_ANY_CPU hint.
This simplifies cpumask_any_housekeeping() significantly.

Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
---
 arch/x86/kernel/cpu/resctrl/internal.h | 28 +++++++-------------------
 1 file changed, 7 insertions(+), 21 deletions(-)

diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
index 20c898f09b7e..1db02bab9743 100644
--- a/arch/x86/kernel/cpu/resctrl/internal.h
+++ b/arch/x86/kernel/cpu/resctrl/internal.h
@@ -71,30 +71,16 @@
 static inline unsigned int
 cpumask_any_housekeeping(const struct cpumask *mask, int exclude_cpu)
 {
-	unsigned int cpu, hk_cpu;
-
-	if (exclude_cpu == RESCTRL_PICK_ANY_CPU)
-		cpu = cpumask_any(mask);
-	else
-		cpu = cpumask_any_but(mask, exclude_cpu);
-
-	/* Only continue if tick_nohz_full_mask has been initialized. */
-	if (!tick_nohz_full_enabled())
-		return cpu;
-
-	/* If the CPU picked isn't marked nohz_full nothing more needs doing. */
-	if (cpu < nr_cpu_ids && !tick_nohz_full_cpu(cpu))
-		return cpu;
+	unsigned int cpu;
 
 	/* Try to find a CPU that isn't nohz_full to use in preference */
-	hk_cpu = cpumask_nth_andnot(0, mask, tick_nohz_full_mask);
-	if (hk_cpu == exclude_cpu)
-		hk_cpu = cpumask_nth_andnot(1, mask, tick_nohz_full_mask);
-
-	if (hk_cpu < nr_cpu_ids)
-		cpu = hk_cpu;
+	if (tick_nohz_full_enabled()) {
+		cpu = cpumask_andnot_any_but(mask, tick_nohz_full_mask, exclude_cpu);
+		if (cpu < nr_cpu_ids)
+			return cpu;
+	}
 
-	return cpu;
+	return cpumask_any_but(mask, exclude_cpu);
 }
 
 struct rdt_fs_context {
-- 
2.43.0
Re: [PATCH 4/4] x86/resctrl: optimize cpumask_any_housekeeping()
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>
> 
> With the lack of cpumask_andnot_any_but(), users have to abuse
> cpumask_nth() functions which are O(N*log(N)), comparing to O(N)
> for cpumask_any().
> 
> This series adds missing cpumask_andnot_any_but() and makes
> cpumask_any_but() understanding the RESCTRL_PICK_ANY_CPU hint.
> This simplifies cpumask_any_housekeeping() significantly.

The "This series ..." language is more appropriate for the cover
letter. 

This changelog could be something like:

	With the lack of cpumask_andnot_any_but(), cpumask_any_housekeeping()
	abused cpumask_nth() functions which are O(N*log(N)), compared to O(N)                
	for cpumask_any().                                                              
                                                                                
	Update cpumask_any_housekeeping() to use the new cpumask_any_but() and          
	cpumask_andnot_any_but(). These two functions understand RESCTRL_PICK_ANY_CPU   
	and simplifies cpumask_any_housekeeping() significantly.                        


Also, could you please have the subject of this patch start with an
upper case: "x86/resctrl: Optimize cpumask_any_housekeeping()"?

> 
> Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> ---
>  arch/x86/kernel/cpu/resctrl/internal.h | 28 +++++++-------------------
>  1 file changed, 7 insertions(+), 21 deletions(-)
> 
> diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
> index 20c898f09b7e..1db02bab9743 100644
> --- a/arch/x86/kernel/cpu/resctrl/internal.h
> +++ b/arch/x86/kernel/cpu/resctrl/internal.h
> @@ -71,30 +71,16 @@
>  static inline unsigned int
>  cpumask_any_housekeeping(const struct cpumask *mask, int exclude_cpu)
>  {
> -	unsigned int cpu, hk_cpu;
> -
> -	if (exclude_cpu == RESCTRL_PICK_ANY_CPU)
> -		cpu = cpumask_any(mask);
> -	else
> -		cpu = cpumask_any_but(mask, exclude_cpu);
> -
> -	/* Only continue if tick_nohz_full_mask has been initialized. */
> -	if (!tick_nohz_full_enabled())
> -		return cpu;
> -
> -	/* If the CPU picked isn't marked nohz_full nothing more needs doing. */
> -	if (cpu < nr_cpu_ids && !tick_nohz_full_cpu(cpu))
> -		return cpu;
> +	unsigned int cpu;
>  
>  	/* Try to find a CPU that isn't nohz_full to use in preference */
> -	hk_cpu = cpumask_nth_andnot(0, mask, tick_nohz_full_mask);
> -	if (hk_cpu == exclude_cpu)
> -		hk_cpu = cpumask_nth_andnot(1, mask, tick_nohz_full_mask);
> -
> -	if (hk_cpu < nr_cpu_ids)
> -		cpu = hk_cpu;
> +	if (tick_nohz_full_enabled()) {
> +		cpu = cpumask_andnot_any_but(mask, tick_nohz_full_mask, exclude_cpu);
> +		if (cpu < nr_cpu_ids)
> +			return cpu;
> +	}
>  
> -	return cpu;
> +	return cpumask_any_but(mask, exclude_cpu);
>  }
>  
>  struct rdt_fs_context {

This looks good to me. Thank you very much for doing this.

Reinette
Re: [PATCH 4/4] x86/resctrl: optimize cpumask_any_housekeeping()
Posted by Yury Norov 9 months, 3 weeks ago
On Wed, Apr 23, 2025 at 02:29:48PM -0700, Reinette Chatre wrote:
> Hi Yury,
> 
> On 4/7/25 8:38 AM, Yury Norov wrote:
> > From: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> > 
> > With the lack of cpumask_andnot_any_but(), users have to abuse
> > cpumask_nth() functions which are O(N*log(N)), comparing to O(N)
> > for cpumask_any().
> > 
> > This series adds missing cpumask_andnot_any_but() and makes
> > cpumask_any_but() understanding the RESCTRL_PICK_ANY_CPU hint.
> > This simplifies cpumask_any_housekeeping() significantly.
> 
> The "This series ..." language is more appropriate for the cover
> letter. 
> 
> This changelog could be something like:
> 
> 	With the lack of cpumask_andnot_any_but(), cpumask_any_housekeeping()
> 	abused cpumask_nth() functions which are O(N*log(N)), compared to O(N)                
> 	for cpumask_any().                                                              
>                                                                                 
> 	Update cpumask_any_housekeeping() to use the new cpumask_any_but() and          
> 	cpumask_andnot_any_but(). These two functions understand RESCTRL_PICK_ANY_CPU   
> 	and simplifies cpumask_any_housekeeping() significantly.                        
> 
> 
> Also, could you please have the subject of this patch start with an
> upper case: "x86/resctrl: Optimize cpumask_any_housekeeping()"?

Yep, I'll reword.

> > Signed-off-by: Yury Norov [NVIDIA] <yury.norov@gmail.com>
> > ---
> >  arch/x86/kernel/cpu/resctrl/internal.h | 28 +++++++-------------------
> >  1 file changed, 7 insertions(+), 21 deletions(-)
> > 
> > diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
> > index 20c898f09b7e..1db02bab9743 100644
> > --- a/arch/x86/kernel/cpu/resctrl/internal.h
> > +++ b/arch/x86/kernel/cpu/resctrl/internal.h
> > @@ -71,30 +71,16 @@
> >  static inline unsigned int
> >  cpumask_any_housekeeping(const struct cpumask *mask, int exclude_cpu)
> >  {
> > -	unsigned int cpu, hk_cpu;
> > -
> > -	if (exclude_cpu == RESCTRL_PICK_ANY_CPU)
> > -		cpu = cpumask_any(mask);
> > -	else
> > -		cpu = cpumask_any_but(mask, exclude_cpu);
> > -
> > -	/* Only continue if tick_nohz_full_mask has been initialized. */
> > -	if (!tick_nohz_full_enabled())
> > -		return cpu;
> > -
> > -	/* If the CPU picked isn't marked nohz_full nothing more needs doing. */
> > -	if (cpu < nr_cpu_ids && !tick_nohz_full_cpu(cpu))
> > -		return cpu;
> > +	unsigned int cpu;
> >  
> >  	/* Try to find a CPU that isn't nohz_full to use in preference */
> > -	hk_cpu = cpumask_nth_andnot(0, mask, tick_nohz_full_mask);
> > -	if (hk_cpu == exclude_cpu)
> > -		hk_cpu = cpumask_nth_andnot(1, mask, tick_nohz_full_mask);
> > -
> > -	if (hk_cpu < nr_cpu_ids)
> > -		cpu = hk_cpu;
> > +	if (tick_nohz_full_enabled()) {
> > +		cpu = cpumask_andnot_any_but(mask, tick_nohz_full_mask, exclude_cpu);
> > +		if (cpu < nr_cpu_ids)
> > +			return cpu;
> > +	}
> >  
> > -	return cpu;
> > +	return cpumask_any_but(mask, exclude_cpu);
> >  }
> >  
> >  struct rdt_fs_context {
> 
> This looks good to me. Thank you very much for doing this.

Thanks for the feedback! I'll send v2 before the end of week.

Thanks,
Yury