Skip to content

Commit

Permalink
sched/topology: introduce sched_numa_find_next_cpu()
Browse files Browse the repository at this point in the history
The function searches for a next CPU in a given cpumask according to
NUMA topology, so that it traverses CPUs per-hop.

If the CPU is the last CPU in a given hop, sched_numa_find_next_cpu()
switches to the next hop, and picks the first CPU from there, excluding
those already traversed.

Because only online CPUs are presented in the NUMA topology masks, offline
CPUs will be skipped even if presented in the 'cpus' mask provided in the
arguments.

Signed-off-by: Yury Norov <yury.norov@gmail.com>
  • Loading branch information
Yury Norov committed Jul 18, 2023
1 parent fad6da0 commit 310ae5d
Showing 2 changed files with 51 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/linux/topology.h
Original file line number Diff line number Diff line change
@@ -245,15 +245,27 @@ static inline const struct cpumask *cpu_cpu_mask(int cpu)
return cpumask_of_node(cpu_to_node(cpu));
}

/*
* sched_numa_find_*_cpu() functions family traverses only accessible CPUs,
* i.e. those listed in cpu_online_mask.
*/
#ifdef CONFIG_NUMA
int sched_numa_find_nth_cpu(const struct cpumask *cpus, int cpu, int node);
int sched_numa_find_next_cpu(const struct cpumask *cpus, int cpu, int node, unsigned int *hop);
extern const struct cpumask *sched_numa_hop_mask(unsigned int node, unsigned int hops);
#else
static __always_inline int sched_numa_find_nth_cpu(const struct cpumask *cpus, int cpu, int node)
{
return cpumask_nth_and(cpu, cpus, cpu_online_mask);
}

static __always_inline
int sched_numa_find_next_cpu(const struct cpumask *cpus, int cpu, int node, unsigned int *hop)
{
return find_next_and_bit(cpumask_bits(cpus), cpumask_bits(cpu_online_mask),
small_cpumask_bits, cpu);
}

static inline const struct cpumask *
sched_numa_hop_mask(unsigned int node, unsigned int hops)
{
39 changes: 39 additions & 0 deletions kernel/sched/topology.c
Original file line number Diff line number Diff line change
@@ -2135,6 +2135,45 @@ int sched_numa_find_nth_cpu(const struct cpumask *cpus, int cpu, int node)
}
EXPORT_SYMBOL_GPL(sched_numa_find_nth_cpu);

/*
* sched_numa_find_next_cpu() - given the NUMA topology, find the next cpu
* cpumask: cpumask to find a CPU from
* cpu: current CPU
* node: local node
* hop: (in/out) indicates distance order of current CPU to a local node
*
* The function searches for a next CPU at a given NUMA distance, indicated
* by hop, and if nothing found, tries to find CPUs at a greater distance,
* starting from the beginning.
*
* Return: cpu, or >= nr_cpu_ids when nothing found.
*/
int sched_numa_find_next_cpu(const struct cpumask *cpus, int cpu, int node, unsigned int *hop)
{
unsigned long *cur, *prev;
struct cpumask ***masks;
unsigned int ret;

if (*hop >= sched_domains_numa_levels)
return nr_cpu_ids;

masks = rcu_dereference(sched_domains_numa_masks);
cur = cpumask_bits(masks[*hop][node]);
if (*hop == 0)
ret = find_next_and_bit(cpumask_bits(cpus), cur, nr_cpu_ids, cpu);
else {
prev = cpumask_bits(masks[*hop - 1][node]);
ret = find_next_and_andnot_bit(cpumask_bits(cpus), cur, prev, nr_cpu_ids, cpu);
}

if (ret < nr_cpu_ids)
return ret;

*hop += 1;
return sched_numa_find_next_cpu(cpus, 0, node, hop);
}
EXPORT_SYMBOL_GPL(sched_numa_find_next_cpu);

/**
* sched_numa_hop_mask() - Get the cpumask of CPUs at most @hops hops away from
* @node

0 comments on commit 310ae5d

Please sign in to comment.