[v5,2/3] sched/topology: Introduce for_each_numa_hop_mask()

Message ID 20221021121927.2893692-3-vschneid@redhat.com
State New
Headers
Series sched, net: NUMA-aware CPU spreading interface |

Commit Message

Valentin Schneider Oct. 21, 2022, 12:19 p.m. UTC
  The recently introduced sched_numa_hop_mask() exposes cpumasks of CPUs
reachable within a given distance budget, wrap the logic for iterating over
all (distance, mask) values inside an iterator macro.

Signed-off-by: Valentin Schneider <vschneid@redhat.com>
---
 include/linux/topology.h | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)
  

Comments

Andy Shevchenko Oct. 21, 2022, 1:16 p.m. UTC | #1
On Fri, Oct 21, 2022 at 01:19:26PM +0100, Valentin Schneider wrote:
> The recently introduced sched_numa_hop_mask() exposes cpumasks of CPUs
> reachable within a given distance budget, wrap the logic for iterating over
> all (distance, mask) values inside an iterator macro.

...

>  #ifdef CONFIG_NUMA
> -extern const struct cpumask *sched_numa_hop_mask(int node, int hops);
> +extern const struct cpumask *sched_numa_hop_mask(unsigned int node, unsigned int hops);
>  #else
> -static inline const struct cpumask *sched_numa_hop_mask(int node, int hops)
> +static inline const struct cpumask *
> +sched_numa_hop_mask(unsigned int node, unsigned int hops)
>  {
> -	if (node == NUMA_NO_NODE && !hops)
> -		return cpu_online_mask;
> -
>  	return ERR_PTR(-EOPNOTSUPP);
>  }
>  #endif	/* CONFIG_NUMA */

I didn't get how the above two changes are related to the 3rd one which
introduces a for_each type of macro.

If you need change int --> unsigned int, perhaps it can be done in a separate
patch.

The change inside inliner I dunno about. Not an expert.

...

> +#define for_each_numa_hop_mask(mask, node)				     \
> +	for (unsigned int __hops = 0;					     \
> +	     /*								     \
> +	      * Unsightly trickery required as we can't both initialize	     \
> +	      * @mask and declare __hops in for()'s first clause	     \
> +	      */							     \
> +	     mask = __hops > 0 ? mask :					     \
> +		    node == NUMA_NO_NODE ?				     \
> +		    cpu_online_mask : sched_numa_hop_mask(node, 0),	     \
> +	     !IS_ERR_OR_NULL(mask);					     \

> +	     __hops++,							     \
> +	     mask = sched_numa_hop_mask(node, __hops))

This can be unified with conditional, see for_each_gpio_desc_with_flag() as
example how.
  
Andy Shevchenko Oct. 21, 2022, 1:34 p.m. UTC | #2
On Fri, Oct 21, 2022 at 04:16:17PM +0300, Andy Shevchenko wrote:
> On Fri, Oct 21, 2022 at 01:19:26PM +0100, Valentin Schneider wrote:

...

> > +#define for_each_numa_hop_mask(mask, node)				     \
> > +	for (unsigned int __hops = 0;					     \
> > +	     /*								     \
> > +	      * Unsightly trickery required as we can't both initialize	     \
> > +	      * @mask and declare __hops in for()'s first clause	     \
> > +	      */							     \
> > +	     mask = __hops > 0 ? mask :					     \
> > +		    node == NUMA_NO_NODE ?				     \
> > +		    cpu_online_mask : sched_numa_hop_mask(node, 0),	     \
> > +	     !IS_ERR_OR_NULL(mask);					     \
> 
> > +	     __hops++,							     \
> > +	     mask = sched_numa_hop_mask(node, __hops))
> 
> This can be unified with conditional, see for_each_gpio_desc_with_flag() as
> example how.

Something like

	mask = (__hops || node != NUMA_NO_NODE) ? sched_numa_hop_mask(node, __hops) : cpu_online_mask
  
Valentin Schneider Oct. 21, 2022, 1:57 p.m. UTC | #3
On 21/10/22 16:16, Andy Shevchenko wrote:
> On Fri, Oct 21, 2022 at 01:19:26PM +0100, Valentin Schneider wrote:
>> The recently introduced sched_numa_hop_mask() exposes cpumasks of CPUs
>> reachable within a given distance budget, wrap the logic for iterating over
>> all (distance, mask) values inside an iterator macro.
>
> ...
>
>>  #ifdef CONFIG_NUMA
>> -extern const struct cpumask *sched_numa_hop_mask(int node, int hops);
>> +extern const struct cpumask *sched_numa_hop_mask(unsigned int node, unsigned int hops);
>>  #else
>> -static inline const struct cpumask *sched_numa_hop_mask(int node, int hops)
>> +static inline const struct cpumask *
>> +sched_numa_hop_mask(unsigned int node, unsigned int hops)
>>  {
>> -	if (node == NUMA_NO_NODE && !hops)
>> -		return cpu_online_mask;
>> -
>>      return ERR_PTR(-EOPNOTSUPP);
>>  }
>>  #endif	/* CONFIG_NUMA */
>
> I didn't get how the above two changes are related to the 3rd one which
> introduces a for_each type of macro.
>
> If you need change int --> unsigned int, perhaps it can be done in a separate
> patch.
>
> The change inside inliner I dunno about. Not an expert.
>

That's a rebase fail, this should all be in the first patch, my bad.
  
Valentin Schneider Oct. 21, 2022, 2:06 p.m. UTC | #4
On 21/10/22 16:34, Andy Shevchenko wrote:
> On Fri, Oct 21, 2022 at 04:16:17PM +0300, Andy Shevchenko wrote:
>> On Fri, Oct 21, 2022 at 01:19:26PM +0100, Valentin Schneider wrote:
>
> ...
>
>> > +#define for_each_numa_hop_mask(mask, node)				     \
>> > +	for (unsigned int __hops = 0;					     \
>> > +	     /*								     \
>> > +	      * Unsightly trickery required as we can't both initialize	     \
>> > +	      * @mask and declare __hops in for()'s first clause	     \
>> > +	      */							     \
>> > +	     mask = __hops > 0 ? mask :					     \
>> > +		    node == NUMA_NO_NODE ?				     \
>> > +		    cpu_online_mask : sched_numa_hop_mask(node, 0),	     \
>> > +	     !IS_ERR_OR_NULL(mask);					     \
>>
>> > +	     __hops++,							     \
>> > +	     mask = sched_numa_hop_mask(node, __hops))
>>
>> This can be unified with conditional, see for_each_gpio_desc_with_flag() as
>> example how.
>
> Something like
>
>       mask = (__hops || node != NUMA_NO_NODE) ? sched_numa_hop_mask(node, __hops) : cpu_online_mask
>

That does simplify things somewhat, thanks!

> --
> With Best Regards,
> Andy Shevchenko
  

Patch

diff --git a/include/linux/topology.h b/include/linux/topology.h
index 3e91ae6d0ad58..8185e12ec1ccc 100644
--- a/include/linux/topology.h
+++ b/include/linux/topology.h
@@ -246,16 +246,36 @@  static inline const struct cpumask *cpu_cpu_mask(int cpu)
 }
 
 #ifdef CONFIG_NUMA
-extern const struct cpumask *sched_numa_hop_mask(int node, int hops);
+extern const struct cpumask *sched_numa_hop_mask(unsigned int node, unsigned int hops);
 #else
-static inline const struct cpumask *sched_numa_hop_mask(int node, int hops)
+static inline const struct cpumask *
+sched_numa_hop_mask(unsigned int node, unsigned int hops)
 {
-	if (node == NUMA_NO_NODE && !hops)
-		return cpu_online_mask;
-
 	return ERR_PTR(-EOPNOTSUPP);
 }
 #endif	/* CONFIG_NUMA */
 
+/**
+ * for_each_numa_hop_mask - iterate over cpumasks of increasing NUMA distance
+ *                          from a given node.
+ * @mask: the iteration variable.
+ * @node: the NUMA node to start the search from.
+ *
+ * Requires rcu_lock to be held.
+ *
+ * Yields cpu_online_mask for @node == NUMA_NO_NODE.
+ */
+#define for_each_numa_hop_mask(mask, node)				     \
+	for (unsigned int __hops = 0;					     \
+	     /*								     \
+	      * Unsightly trickery required as we can't both initialize	     \
+	      * @mask and declare __hops in for()'s first clause	     \
+	      */							     \
+	     mask = __hops > 0 ? mask :					     \
+		    node == NUMA_NO_NODE ?				     \
+		    cpu_online_mask : sched_numa_hop_mask(node, 0),	     \
+	     !IS_ERR_OR_NULL(mask);					     \
+	     __hops++,							     \
+	     mask = sched_numa_hop_mask(node, __hops))
 
 #endif /* _LINUX_TOPOLOGY_H */