lib/cpumask: update comment for cpumask_local_spread()
authorYury Norov <yury.norov@gmail.com>
Sat, 21 Jan 2023 04:24:36 +0000 (20:24 -0800)
committerJakub Kicinski <kuba@kernel.org>
Wed, 8 Feb 2023 02:20:00 +0000 (18:20 -0800)
Now that we have an iterator-based alternative for a very common case
of using cpumask_local_spread for all cpus in a row, it's worth to
mention that in comment to cpumask_local_spread().

Signed-off-by: Yury Norov <yury.norov@gmail.com>
Reviewed-by: Valentin Schneider <vschneid@redhat.com>
Reviewed-by: Tariq Toukan <tariqt@nvidia.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
lib/cpumask.c

index 10aa15715c0d29731dc7acba367a34c084d815e8..e7258836b60b7fc96f11ea05b3b61653441e9021 100644 (file)
@@ -114,11 +114,29 @@ void __init free_bootmem_cpumask_var(cpumask_var_t mask)
  * @i: index number
  * @node: local numa_node
  *
- * This function selects an online CPU according to a numa aware policy;
- * local cpus are returned first, followed by non-local ones, then it
- * wraps around.
+ * Returns online CPU according to a numa aware policy; local cpus are returned
+ * first, followed by non-local ones, then it wraps around.
  *
- * It's not very efficient, but useful for setup.
+ * For those who wants to enumerate all CPUs based on their NUMA distances,
+ * i.e. call this function in a loop, like:
+ *
+ * for (i = 0; i < num_online_cpus(); i++) {
+ *     cpu = cpumask_local_spread(i, node);
+ *     do_something(cpu);
+ * }
+ *
+ * There's a better alternative based on for_each()-like iterators:
+ *
+ *     for_each_numa_hop_mask(mask, node) {
+ *             for_each_cpu_andnot(cpu, mask, prev)
+ *                     do_something(cpu);
+ *             prev = mask;
+ *     }
+ *
+ * It's simpler and more verbose than above. Complexity of iterator-based
+ * enumeration is O(sched_domains_numa_levels * nr_cpu_ids), while
+ * cpumask_local_spread() when called for each cpu is
+ * O(sched_domains_numa_levels * nr_cpu_ids * log(nr_cpu_ids)).
  */
 unsigned int cpumask_local_spread(unsigned int i, int node)
 {