Skip to contentSkip to conversation

cpuset: separate generate_sched_domains for v1 and v2

bug report Dan Carpenter 2 participants 2 messages Dec 20, 2025 cgroup

lore
Share & tools
lore
#0 lore raw

Hello Chen Ridong,

Commit 6e1d31ce495c ("cpuset: separate generate_sched_domains for v1 and v2") from Dec 18, 2025 (linux-next), leads to the following Smatch static checker warning:

kernel/cgroup/cpuset-v1.c:641 cpuset1_generate_sched_domains()
warn: duplicate check 'root_load_balance' (previous on line 618)

kernel/cgroup/cpuset-v1.c

    596 int cpuset1_generate_sched_domains(cpumask_var_t **domains,
    597                         struct sched_domain_attr **attributes)
    598 {
    599         struct cpuset *cp;        /* top-down scan of cpusets */
    600         struct cpuset **csa;        /* array of all cpuset ptrs */
    601         int csn;                /* how many cpuset ptrs in csa so far */
    602         int i, j;                /* indices for partition finding loops */
    603         cpumask_var_t *doms;        /* resulting partition; i.e. sched domains */
    604         struct sched_domain_attr *dattr;  /* attributes for custom domains */
    605         int ndoms = 0;                /* number of sched domains in result */
    606         int nslot;                /* next empty doms[] struct cpumask slot */
    607         struct cgroup_subsys_state *pos_css;
    608         bool root_load_balance = is_sched_load_balance(&top_cpuset);
    609         int nslot_update;
    610 
    611         lockdep_assert_cpuset_lock_held();
    612 
    613         doms = NULL;
    614         dattr = NULL;
    615         csa = NULL;
    616 
    617         /* Special case for the 99% of systems with one, full, sched domain */
    618         if (root_load_balance) {
    619                 ndoms = 1;
    620                 doms = alloc_sched_domains(ndoms);
    621                 if (!doms)
    622                         goto done;
    623 
    624                 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
    625                 if (dattr) {
    626                         *dattr = SD_ATTR_INIT;
    627                         update_domain_attr_tree(dattr, &top_cpuset);
    628                 }
    629                 cpumask_and(doms[0], top_cpuset.effective_cpus,
    630                             housekeeping_cpumask(HK_TYPE_DOMAIN));
    631 
    632                 goto done;

If root_load_balance is true we are done.

    633         }
    634 
    635         csa = kmalloc_array(nr_cpusets(), sizeof(cp), GFP_KERNEL);
    636         if (!csa)
    637                 goto done;
    638         csn = 0;
    639 
    640         rcu_read_lock();
--> 641         if (root_load_balance)
    642                 csa[csn++] = &top_cpuset;

Dead code.

regards,
dan carpenter

    643         cpuset_for_each_descendant_pre(cp, pos_css, &top_cpuset) {
    644                 if (cp == &top_cpuset)
    645                         continue;
    646 
    647                 /*
    648                  * Continue traversing beyond @cp iff @cp has some CPUs and
    649                  * isn't load balancing.  The former is obvious.  The
    650                  * latter: All child cpusets contain a subset of the
    651                  * parent's cpus, so just skip them, and then we call
    652                  * update_domain_attr_tree() to calc relax_domain_level of
    653                  * the corresponding sched domain.
    654                  */
    655                 if (!cpumask_empty(cp->cpus_allowed) &&
    656                     !(is_sched_load_balance(cp) &&
    657                       cpumask_intersects(cp->cpus_allowed,
    658                                          housekeeping_cpumask(HK_TYPE_DOMAIN))))
    659                         continue;
    660 
    661                 if (is_sched_load_balance(cp) &&
    662                     !cpumask_empty(cp->effective_cpus))
    663                         csa[csn++] = cp;
    664 
    665                 /* skip @cp's subtree */
    666                 pos_css = css_rightmost_descendant(pos_css);
    667                 continue;
    668         }
    669         rcu_read_unlock();
    670 
    671         for (i = 0; i < csn; i++)
    672                 uf_node_init(&csa[i]->node);
    673 
    674         /* Merge overlapping cpusets */
    675         for (i = 0; i < csn; i++) {
    676                 for (j = i + 1; j < csn; j++) {
    677                         if (cpusets_overlap(csa[i], csa[j]))
    678                                 uf_union(&csa[i]->node, &csa[j]->node);
    679                 }
    680         }
    681 
    682         /* Count the total number of domains */
    683         for (i = 0; i < csn; i++) {
    684                 if (uf_find(&csa[i]->node) == &csa[i]->node)
    685                         ndoms++;
    686         }
    687 
    688         /*
    689          * Now we know how many domains to create.
    690          * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
    691          */
    692         doms = alloc_sched_domains(ndoms);
    693         if (!doms)
    694                 goto done;
    695 
    696         /*
    697          * The rest of the code, including the scheduler, can deal with
    698          * dattr==NULL case. No need to abort if alloc fails.
    699          */
    700         dattr = kmalloc_array(ndoms, sizeof(struct sched_domain_attr),
    701                               GFP_KERNEL);
    702 
    703         for (nslot = 0, i = 0; i < csn; i++) {
    704                 nslot_update = 0;
    705                 for (j = i; j < csn; j++) {
    706                         if (uf_find(&csa[j]->node) == &csa[i]->node) {
    707                                 struct cpumask *dp = doms[nslot];
    708 
    709                                 if (i == j) {
    710                                         nslot_update = 1;
    711                                         cpumask_clear(dp);
    712                                         if (dattr)
    713                                                 *(dattr + nslot) = SD_ATTR_INIT;
    714                                 }
    715                                 cpumask_or(dp, dp, csa[j]->effective_cpus);
    716                                 cpumask_and(dp, dp, housekeeping_cpumask(HK_TYPE_DOMAIN));
    717                                 if (dattr)
    718                                         update_domain_attr_tree(dattr + nslot, csa[j]);
    719                         }
    720                 }
    721                 if (nslot_update)
    722                         nslot++;
    723         }
    724         BUG_ON(nslot != ndoms);
    725 
    726 done:
    727         kfree(csa);
    728 
    729         /*
    730          * Fallback to the default domain if kmalloc() failed.
    731          * See comments in partition_sched_domains().
    732          */
    733         if (doms == NULL)
    734                 ndoms = 1;
    735 
    736         *domains    = doms;
    737         *attributes = dattr;
    738         return ndoms;
    739 }
#1 lore raw
Quoted 61 lines · Dan Carpenter

Hello Chen Ridong,

Commit 6e1d31ce495c ("cpuset: separate generate_sched_domains for v1 and v2") from Dec 18, 2025 (linux-next), leads to the following Smatch static checker warning:

kernel/cgroup/cpuset-v1.c:641 cpuset1_generate_sched_domains()
warn: duplicate check 'root_load_balance' (previous on line 618)

kernel/cgroup/cpuset-v1.c

    596 int cpuset1_generate_sched_domains(cpumask_var_t **domains,
    597                         struct sched_domain_attr **attributes)
    598 {
    599         struct cpuset *cp;        /* top-down scan of cpusets */
    600         struct cpuset **csa;        /* array of all cpuset ptrs */
    601         int csn;                /* how many cpuset ptrs in csa so far */
    602         int i, j;                /* indices for partition finding loops */
    603         cpumask_var_t *doms;        /* resulting partition; i.e. sched domains */
    604         struct sched_domain_attr *dattr;  /* attributes for custom domains */
    605         int ndoms = 0;                /* number of sched domains in result */
    606         int nslot;                /* next empty doms[] struct cpumask slot */
    607         struct cgroup_subsys_state *pos_css;
    608         bool root_load_balance = is_sched_load_balance(&top_cpuset);
    609         int nslot_update;
    610 
    611         lockdep_assert_cpuset_lock_held();
    612 
    613         doms = NULL;
    614         dattr = NULL;
    615         csa = NULL;
    616 
    617         /* Special case for the 99% of systems with one, full, sched domain */
    618         if (root_load_balance) {
    619                 ndoms = 1;
    620                 doms = alloc_sched_domains(ndoms);
    621                 if (!doms)
    622                         goto done;
    623 
    624                 dattr = kmalloc(sizeof(struct sched_domain_attr), GFP_KERNEL);
    625                 if (dattr) {
    626                         *dattr = SD_ATTR_INIT;
    627                         update_domain_attr_tree(dattr, &top_cpuset);
    628                 }
    629                 cpumask_and(doms[0], top_cpuset.effective_cpus,
    630                             housekeeping_cpumask(HK_TYPE_DOMAIN));
    631 
    632                 goto done;

If root_load_balance is true we are done.

    633         }
    634 
    635         csa = kmalloc_array(nr_cpusets(), sizeof(cp), GFP_KERNEL);
    636         if (!csa)
    637                 goto done;
    638         csn = 0;
    639 
    640         rcu_read_lock();
--> 641         if (root_load_balance)
    642                 csa[csn++] = &top_cpuset;
Quoted 2 lines

Dead code.

Thank you for pointing this out.

You're correct that this can be safely removed. I'll make the fix.

Quoted 99 lines
    643         cpuset_for_each_descendant_pre(cp, pos_css, &top_cpuset) {
    644                 if (cp == &top_cpuset)
    645                         continue;
    646 
    647                 /*
    648                  * Continue traversing beyond @cp iff @cp has some CPUs and
    649                  * isn't load balancing.  The former is obvious.  The
    650                  * latter: All child cpusets contain a subset of the
    651                  * parent's cpus, so just skip them, and then we call
    652                  * update_domain_attr_tree() to calc relax_domain_level of
    653                  * the corresponding sched domain.
    654                  */
    655                 if (!cpumask_empty(cp->cpus_allowed) &&
    656                     !(is_sched_load_balance(cp) &&
    657                       cpumask_intersects(cp->cpus_allowed,
    658                                          housekeeping_cpumask(HK_TYPE_DOMAIN))))
    659                         continue;
    660 
    661                 if (is_sched_load_balance(cp) &&
    662                     !cpumask_empty(cp->effective_cpus))
    663                         csa[csn++] = cp;
    664 
    665                 /* skip @cp's subtree */
    666                 pos_css = css_rightmost_descendant(pos_css);
    667                 continue;
    668         }
    669         rcu_read_unlock();
    670 
    671         for (i = 0; i < csn; i++)
    672                 uf_node_init(&csa[i]->node);
    673 
    674         /* Merge overlapping cpusets */
    675         for (i = 0; i < csn; i++) {
    676                 for (j = i + 1; j < csn; j++) {
    677                         if (cpusets_overlap(csa[i], csa[j]))
    678                                 uf_union(&csa[i]->node, &csa[j]->node);
    679                 }
    680         }
    681 
    682         /* Count the total number of domains */
    683         for (i = 0; i < csn; i++) {
    684                 if (uf_find(&csa[i]->node) == &csa[i]->node)
    685                         ndoms++;
    686         }
    687 
    688         /*
    689          * Now we know how many domains to create.
    690          * Convert <csn, csa> to <ndoms, doms> and populate cpu masks.
    691          */
    692         doms = alloc_sched_domains(ndoms);
    693         if (!doms)
    694                 goto done;
    695 
    696         /*
    697          * The rest of the code, including the scheduler, can deal with
    698          * dattr==NULL case. No need to abort if alloc fails.
    699          */
    700         dattr = kmalloc_array(ndoms, sizeof(struct sched_domain_attr),
    701                               GFP_KERNEL);
    702 
    703         for (nslot = 0, i = 0; i < csn; i++) {
    704                 nslot_update = 0;
    705                 for (j = i; j < csn; j++) {
    706                         if (uf_find(&csa[j]->node) == &csa[i]->node) {
    707                                 struct cpumask *dp = doms[nslot];
    708 
    709                                 if (i == j) {
    710                                         nslot_update = 1;
    711                                         cpumask_clear(dp);
    712                                         if (dattr)
    713                                                 *(dattr + nslot) = SD_ATTR_INIT;
    714                                 }
    715                                 cpumask_or(dp, dp, csa[j]->effective_cpus);
    716                                 cpumask_and(dp, dp, housekeeping_cpumask(HK_TYPE_DOMAIN));
    717                                 if (dattr)
    718                                         update_domain_attr_tree(dattr + nslot, csa[j]);
    719                         }
    720                 }
    721                 if (nslot_update)
    722                         nslot++;
    723         }
    724         BUG_ON(nslot != ndoms);
    725 
    726 done:
    727         kfree(csa);
    728 
    729         /*
    730          * Fallback to the default domain if kmalloc() failed.
    731          * See comments in partition_sched_domains().
    732          */
    733         if (doms == NULL)
    734                 ndoms = 1;
    735 
    736         *domains    = doms;
    737         *attributes = dattr;
    738         return ndoms;
    739 }
Signature · 2 lines
Best regards,
Ridong