- /* Note: nr_cpus_mask + 1 is always power of 2 */
- partition_len = len >> get_count_order_ulong(nr_cpus_mask + 1);
- work = calloc(nr_cpus_mask + 1, sizeof(*work));
- thread_id = calloc(nr_cpus_mask + 1, sizeof(*thread_id));
+ /*
+ * Note: nr_cpus_mask + 1 is always power of 2.
+ * We spawn just the number of threads we need to satisfy the minimum
+ * partition size, up to the number of CPUs in the system.
+ */
+ nr_threads = min(nr_cpus_mask + 1,
+ len >> MIN_PARTITION_PER_THREAD_ORDER);
+ partition_len = len >> get_count_order_ulong(nr_threads);
+ work = calloc(nr_threads, sizeof(*work));
+ thread_id = calloc(nr_threads, sizeof(*thread_id));