From 15896211004977c0252dc8327aef3d85110e825e Mon Sep 17 00:00:00 2001 From: Olivier Houchard Date: Wed, 27 May 2026 11:13:52 +0200 Subject: [PATCH] BUG/MEDIUM: cpu-topo: Enforce thread-hard-limit on policy When a policy is set, and the number of threads is calculated dynamically, make sure we enforce thread-hard-limit, and do not create thread groups based on how many thread we would have created without the limit. This should be backported to 3.3 and 3.2. The patch won't apply cleanly there, because the code has changed since then, but it should be very similar, only we'll have to check "cpu_count" there, where in 3.4 we check "thr_count". --- src/cpu_topo.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/cpu_topo.c b/src/cpu_topo.c index bfb7177cd..a9ba92fb5 100644 --- a/src/cpu_topo.c +++ b/src/cpu_topo.c @@ -1520,7 +1520,8 @@ static int cpu_policy_group_by_cluster(int policy, int tmin, int tmax, int gmin, div = ha_cpu_policy[policy].arg; div = div ? div : 1; - while (global.nbtgroups < MAX_TGROUPS && global.nbthread < MAX_THREADS) { + while (global.nbtgroups < MAX_TGROUPS && (global.nbthread < MAX_THREADS) && + (global.thread_limit == 0 || global.nbthread < global.thread_limit)) { ha_cpuset_zero(&node_cpu_set); ha_cpuset_zero(&touse_tsid); ha_cpuset_zero(&touse_ccx); @@ -1550,6 +1551,10 @@ static int cpu_policy_group_by_cluster(int policy, int tmin, int tmax, int gmin, ha_cpuset_set(&touse_tsid, ha_cpu_topo[cpu].ts_id); } else if (!(cpu_policy_conf.flags & CPU_POLICY_ONE_THREAD_PER_CORE)) thr_count++; + + if (global.thread_limit != 0 && + thr_count + global.nbthread >= global.thread_limit) + break; } /* now cid = next cluster_id or -1 if none; cpu_count is the @@ -1608,7 +1613,8 @@ static int cpu_policy_group_by_ccx(int policy, int tmin, int tmax, int gmin, int div = ha_cpu_policy[policy].arg; div = div ? div : 1; - while (global.nbtgroups < MAX_TGROUPS && global.nbthread < MAX_THREADS) { + while (global.nbtgroups < MAX_TGROUPS && global.nbthread < MAX_THREADS && + (global.thread_limit == 0 || global.nbthread < global.thread_limit)) { ha_cpuset_zero(&node_cpu_set); ha_cpuset_zero(&touse_tsid); ha_cpuset_zero(&touse_ccx); @@ -1638,6 +1644,9 @@ static int cpu_policy_group_by_ccx(int policy, int tmin, int tmax, int gmin, int ha_cpuset_set(&touse_tsid, ha_cpu_topo[cpu].ts_id); } else if (!(cpu_policy_conf.flags & CPU_POLICY_ONE_THREAD_PER_CORE)) thr_count++; + if (global.thread_limit != 0 && + global.nbthread + thr_count >= global.thread_limit) + break; } /* now l3id = next L3 ID or -1 if none; cpu_count is the -- 2.47.3