Whamcloud - gitweb
LU-9019 libcfs: migrate watchdog to 64 bit time
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-cpu.c
index 369f146..3f4f8e7 100644 (file)
@@ -47,7 +47,7 @@
  *  1 : disable multiple partitions
  * >1 : specify number of partitions
  */
-static int     cpu_npartitions;
+static int cpu_npartitions;
 module_param(cpu_npartitions, int, 0444);
 MODULE_PARM_DESC(cpu_npartitions, "# of CPU partitions");
 
@@ -64,27 +64,22 @@ MODULE_PARM_DESC(cpu_npartitions, "# of CPU partitions");
  *
  * NB: If user specified cpu_pattern, cpu_npartitions will be ignored
  */
-static char    *cpu_pattern = "N";
+static char *cpu_pattern = "N";
 module_param(cpu_pattern, charp, 0444);
 MODULE_PARM_DESC(cpu_pattern, "CPU partitions pattern");
 
-/* return number of HTs in the same core of \a cpu */
-int
-cfs_cpu_ht_nsiblings(int cpu)
-{
-       return cpumask_weight(topology_sibling_cpumask(cpu));
-}
-EXPORT_SYMBOL(cfs_cpu_ht_nsiblings);
-
-void
-cfs_cpt_table_free(struct cfs_cpt_table *cptab)
+void cfs_cpt_table_free(struct cfs_cpt_table *cptab)
 {
        int i;
 
        if (cptab->ctb_cpu2cpt != NULL) {
                LIBCFS_FREE(cptab->ctb_cpu2cpt,
-                           num_possible_cpus() *
-                           sizeof(cptab->ctb_cpu2cpt[0]));
+                           nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
+       }
+
+       if (cptab->ctb_node2cpt != NULL) {
+               LIBCFS_FREE(cptab->ctb_node2cpt,
+                           nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
        }
 
        for (i = 0; cptab->ctb_parts != NULL && i < cptab->ctb_nparts; i++) {
@@ -97,6 +92,12 @@ cfs_cpt_table_free(struct cfs_cpt_table *cptab)
 
                if (part->cpt_cpumask != NULL)
                        LIBCFS_FREE(part->cpt_cpumask, cpumask_size());
+
+               if (part->cpt_distance) {
+                       LIBCFS_FREE(part->cpt_distance,
+                               cptab->ctb_nparts *
+                                       sizeof(part->cpt_distance[0]));
+               }
        }
 
        if (cptab->ctb_parts != NULL) {
@@ -113,11 +114,10 @@ cfs_cpt_table_free(struct cfs_cpt_table *cptab)
 }
 EXPORT_SYMBOL(cfs_cpt_table_free);
 
-struct cfs_cpt_table *
-cfs_cpt_table_alloc(unsigned int ncpt)
+struct cfs_cpt_table *cfs_cpt_table_alloc(int ncpt)
 {
        struct cfs_cpt_table *cptab;
-       int     i;
+       int i;
 
        LIBCFS_ALLOC(cptab, sizeof(*cptab));
        if (cptab == NULL)
@@ -132,12 +132,20 @@ cfs_cpt_table_alloc(unsigned int ncpt)
                goto failed;
 
        LIBCFS_ALLOC(cptab->ctb_cpu2cpt,
-                    num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
+                    nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
        if (cptab->ctb_cpu2cpt == NULL)
                goto failed;
 
        memset(cptab->ctb_cpu2cpt, -1,
-              num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
+              nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
+
+       LIBCFS_ALLOC(cptab->ctb_node2cpt,
+                    nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
+       if (cptab->ctb_node2cpt == NULL)
+               goto failed;
+
+       memset(cptab->ctb_node2cpt, -1,
+              nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
 
        LIBCFS_ALLOC(cptab->ctb_parts, ncpt * sizeof(cptab->ctb_parts[0]));
        if (cptab->ctb_parts == NULL)
@@ -147,8 +155,16 @@ cfs_cpt_table_alloc(unsigned int ncpt)
                struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
 
                LIBCFS_ALLOC(part->cpt_cpumask, cpumask_size());
+               if (!part->cpt_cpumask)
+                       goto failed;
+
                LIBCFS_ALLOC(part->cpt_nodemask, sizeof(*part->cpt_nodemask));
-               if (part->cpt_cpumask == NULL || part->cpt_nodemask == NULL)
+               if (!part->cpt_nodemask)
+                       goto failed;
+
+               LIBCFS_ALLOC(part->cpt_distance,
+                       cptab->ctb_nparts * sizeof(part->cpt_distance[0]));
+               if (!part->cpt_distance)
                        goto failed;
        }
 
@@ -160,33 +176,29 @@ failed:
 }
 EXPORT_SYMBOL(cfs_cpt_table_alloc);
 
-int
-cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
+int cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
 {
        char    *tmp = buf;
-       int     rc = 0;
+       int     rc = -EFBIG;
        int     i;
        int     j;
 
        for (i = 0; i < cptab->ctb_nparts; i++) {
-               if (len > 0) {
-                       rc = snprintf(tmp, len, "%d\t: ", i);
-                       len -= rc;
-               }
+               if (len <= 0)
+                       goto out;
 
-               if (len <= 0) {
-                       rc = -EFBIG;
+               rc = snprintf(tmp, len, "%d\t:", i);
+               len -= rc;
+
+               if (len <= 0)
                        goto out;
-               }
 
                tmp += rc;
                for_each_cpu(j, cptab->ctb_parts[i].cpt_cpumask) {
-                       rc = snprintf(tmp, len, "%d ", j);
+                       rc = snprintf(tmp, len, " %d", j);
                        len -= rc;
-                       if (len <= 0) {
-                               rc = -EFBIG;
+                       if (len <= 0)
                                goto out;
-                       }
                        tmp += rc;
                }
 
@@ -194,8 +206,8 @@ cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
                tmp++;
                len--;
        }
-
-out:
+       rc = 0;
+ out:
        if (rc < 0)
                return rc;
 
@@ -203,15 +215,53 @@ out:
 }
 EXPORT_SYMBOL(cfs_cpt_table_print);
 
-int
-cfs_cpt_number(struct cfs_cpt_table *cptab)
+int cfs_cpt_distance_print(struct cfs_cpt_table *cptab, char *buf, int len)
+{
+       char    *tmp = buf;
+       int     rc = -EFBIG;
+       int     i;
+       int     j;
+
+       for (i = 0; i < cptab->ctb_nparts; i++) {
+               if (len <= 0)
+                       goto out;
+
+               rc = snprintf(tmp, len, "%d\t:", i);
+               len -= rc;
+
+               if (len <= 0)
+                       goto out;
+
+               tmp += rc;
+               for (j = 0; j < cptab->ctb_nparts; j++) {
+                       rc = snprintf(tmp, len, " %d:%d",
+                               j, cptab->ctb_parts[i].cpt_distance[j]);
+                       len -= rc;
+                       if (len <= 0)
+                               goto out;
+                       tmp += rc;
+               }
+
+               *tmp = '\n';
+               tmp++;
+               len--;
+       }
+       rc = 0;
+ out:
+       if (rc < 0)
+               return rc;
+
+       return tmp - buf;
+}
+EXPORT_SYMBOL(cfs_cpt_distance_print);
+
+int cfs_cpt_number(struct cfs_cpt_table *cptab)
 {
        return cptab->ctb_nparts;
 }
 EXPORT_SYMBOL(cfs_cpt_number);
 
-int
-cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
+int cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
 {
        LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
 
@@ -221,8 +271,7 @@ cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
 }
 EXPORT_SYMBOL(cfs_cpt_weight);
 
-int
-cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
+int cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
 {
        LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
 
@@ -234,8 +283,7 @@ cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
 }
 EXPORT_SYMBOL(cfs_cpt_online);
 
-cpumask_t *
-cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
+cpumask_t *cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
 {
        LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
 
@@ -244,8 +292,7 @@ cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
 }
 EXPORT_SYMBOL(cfs_cpt_cpumask);
 
-nodemask_t *
-cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
+nodemask_t *cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
 {
        LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
 
@@ -254,11 +301,137 @@ cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
 }
 EXPORT_SYMBOL(cfs_cpt_nodemask);
 
-int
-cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
+unsigned cfs_cpt_distance(struct cfs_cpt_table *cptab, int cpt1, int cpt2)
+{
+       LASSERT(cpt1 == CFS_CPT_ANY || (cpt1 >= 0 && cpt1 < cptab->ctb_nparts));
+       LASSERT(cpt2 == CFS_CPT_ANY || (cpt2 >= 0 && cpt2 < cptab->ctb_nparts));
+
+       if (cpt1 == CFS_CPT_ANY || cpt2 == CFS_CPT_ANY)
+               return cptab->ctb_distance;
+
+       return cptab->ctb_parts[cpt1].cpt_distance[cpt2];
+}
+EXPORT_SYMBOL(cfs_cpt_distance);
+
+/*
+ * Calculate the maximum NUMA distance between all nodes in the
+ * from_mask and all nodes in the to_mask.
+ */
+static unsigned cfs_cpt_distance_calculate(nodemask_t *from_mask,
+                                          nodemask_t *to_mask)
+{
+       unsigned maximum;
+       unsigned distance;
+       int to;
+       int from;
+
+       maximum = 0;
+       for_each_node_mask(from, *from_mask) {
+               for_each_node_mask(to, *to_mask) {
+                       distance = node_distance(from, to);
+                       if (maximum < distance)
+                               maximum = distance;
+               }
+       }
+       return maximum;
+}
+
+static void cfs_cpt_add_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
 {
-       int     node;
+       cptab->ctb_cpu2cpt[cpu] = cpt;
 
+       cpumask_set_cpu(cpu, cptab->ctb_cpumask);
+       cpumask_set_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
+}
+
+static void cfs_cpt_del_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
+{
+       cpumask_clear_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
+       cpumask_clear_cpu(cpu, cptab->ctb_cpumask);
+
+       cptab->ctb_cpu2cpt[cpu] = -1;
+}
+
+static void cfs_cpt_add_node(struct cfs_cpt_table *cptab, int cpt, int node)
+{
+       int cpt2;
+       struct cfs_cpu_partition *part;
+       struct cfs_cpu_partition *part2;
+
+       if (!node_isset(node, *cptab->ctb_nodemask)) {
+               /* first time node is added to the CPT table */
+               node_set(node, *cptab->ctb_nodemask);
+               cptab->ctb_node2cpt[node] = cpt;
+               cptab->ctb_distance = cfs_cpt_distance_calculate(
+                                                       cptab->ctb_nodemask,
+                                                       cptab->ctb_nodemask);
+       }
+
+       part = &cptab->ctb_parts[cpt];
+       if (!node_isset(node, *part->cpt_nodemask)) {
+               /* first time node is added to this CPT */
+               node_set(node, *part->cpt_nodemask);
+               for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
+                       part2 = &cptab->ctb_parts[cpt2];
+                       part->cpt_distance[cpt2] = cfs_cpt_distance_calculate(
+                                               part->cpt_nodemask,
+                                               part2->cpt_nodemask);
+                       part2->cpt_distance[cpt] = cfs_cpt_distance_calculate(
+                                               part2->cpt_nodemask,
+                                               part->cpt_nodemask);
+               }
+       }
+}
+
+static void cfs_cpt_del_node(struct cfs_cpt_table *cptab, int cpt, int node)
+{
+       int cpu;
+       int cpt2;
+       struct cfs_cpu_partition *part;
+       struct cfs_cpu_partition *part2;
+
+       part = &cptab->ctb_parts[cpt];
+
+       for_each_cpu(cpu, part->cpt_cpumask) {
+               /* this CPT has other CPU belonging to this node? */
+               if (cpu_to_node(cpu) == node)
+                       break;
+       }
+
+       if (cpu >= nr_cpu_ids && node_isset(node,  *part->cpt_nodemask)) {
+               /* No more CPUs in the node for this CPT. */
+               node_clear(node, *part->cpt_nodemask);
+               for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
+                       part2 = &cptab->ctb_parts[cpt2];
+                       if (node_isset(node, *part2->cpt_nodemask))
+                               cptab->ctb_node2cpt[node] = cpt2;
+                       part->cpt_distance[cpt2] = cfs_cpt_distance_calculate(
+                                               part->cpt_nodemask,
+                                               part2->cpt_nodemask);
+                       part2->cpt_distance[cpt] = cfs_cpt_distance_calculate(
+                                               part2->cpt_nodemask,
+                                               part->cpt_nodemask);
+               }
+       }
+
+       for_each_cpu(cpu, cptab->ctb_cpumask) {
+               /* this CPT-table has other CPUs belonging to this node? */
+               if (cpu_to_node(cpu) == node)
+                       break;
+       }
+
+       if (cpu >= nr_cpu_ids && node_isset(node, *cptab->ctb_nodemask)) {
+               /* No more CPUs in the table for this node. */
+               node_clear(node, *cptab->ctb_nodemask);
+               cptab->ctb_node2cpt[node] = -1;
+               cptab->ctb_distance =
+                       cfs_cpt_distance_calculate(cptab->ctb_nodemask,
+                                       cptab->ctb_nodemask);
+       }
+}
+
+int cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
+{
        LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
 
        if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_online(cpu)) {
@@ -272,34 +445,18 @@ cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
                return 0;
        }
 
-       cptab->ctb_cpu2cpt[cpu] = cpt;
-
        LASSERT(!cpumask_test_cpu(cpu, cptab->ctb_cpumask));
        LASSERT(!cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
 
-       cpumask_set_cpu(cpu, cptab->ctb_cpumask);
-       cpumask_set_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
-
-       node = cpu_to_node(cpu);
-
-       /* first CPU of @node in this CPT table */
-       if (!node_isset(node, *cptab->ctb_nodemask))
-               node_set(node, *cptab->ctb_nodemask);
-
-       /* first CPU of @node in this partition */
-       if (!node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask))
-               node_set(node, *cptab->ctb_parts[cpt].cpt_nodemask);
+       cfs_cpt_add_cpu(cptab, cpt, cpu);
+       cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
 
        return 1;
 }
 EXPORT_SYMBOL(cfs_cpt_set_cpu);
 
-void
-cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
+void cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
 {
-       int     node;
-       int     i;
-
        LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
 
        if (cpu < 0 || cpu >= nr_cpu_ids) {
@@ -325,37 +482,13 @@ cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
        LASSERT(cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
        LASSERT(cpumask_test_cpu(cpu, cptab->ctb_cpumask));
 
-       cpumask_clear_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
-       cpumask_clear_cpu(cpu, cptab->ctb_cpumask);
-       cptab->ctb_cpu2cpt[cpu] = -1;
-
-       node = cpu_to_node(cpu);
-
-       LASSERT(node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask));
-       LASSERT(node_isset(node, *cptab->ctb_nodemask));
-
-       for_each_cpu(i, cptab->ctb_parts[cpt].cpt_cpumask) {
-               /* this CPT has other CPU belonging to this node? */
-               if (cpu_to_node(i) == node)
-                       break;
-       }
-
-       if (i >= nr_cpu_ids)
-               node_clear(node, *cptab->ctb_parts[cpt].cpt_nodemask);
-
-       for_each_cpu(i, cptab->ctb_cpumask) {
-               /* this CPT-table has other CPU belonging to this node? */
-               if (cpu_to_node(i) == node)
-                       break;
-       }
-
-       if (i >= nr_cpu_ids)
-               node_clear(node, *cptab->ctb_nodemask);
+       cfs_cpt_del_cpu(cptab, cpt, cpu);
+       cfs_cpt_del_node(cptab, cpt, cpu_to_node(cpu));
 }
 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
 
-int
-cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt, const cpumask_t *mask)
+int cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt,
+                       const cpumask_t *mask)
 {
        int cpu;
 
@@ -367,17 +500,16 @@ cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt, const cpumask_t *mask)
        }
 
        for_each_cpu(cpu, mask) {
-               if (!cfs_cpt_set_cpu(cptab, cpt, cpu))
-                       return 0;
+               cfs_cpt_add_cpu(cptab, cpt, cpu);
+               cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
        }
 
        return 1;
 }
 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
 
-void
-cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt,
-                     const cpumask_t *mask)
+void cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt,
+                          const cpumask_t *mask)
 {
        int cpu;
 
@@ -386,44 +518,50 @@ cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt,
 }
 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
 
-int
-cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
+int cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
 {
        const cpumask_t *mask;
-       int             rc;
+       int             cpu;
 
-       if (node < 0 || node >= MAX_NUMNODES) {
+       if (node < 0 || node >= nr_node_ids) {
                CDEBUG(D_INFO,
                       "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
                return 0;
        }
 
        mask = cpumask_of_node(node);
-       rc = cfs_cpt_set_cpumask(cptab, cpt, mask);
 
-       return rc;
+       for_each_cpu(cpu, mask)
+               cfs_cpt_add_cpu(cptab, cpt, cpu);
+
+       cfs_cpt_add_node(cptab, cpt, node);
+
+       return 1;
 }
 EXPORT_SYMBOL(cfs_cpt_set_node);
 
-void
-cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
+void cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
 {
        const cpumask_t *mask;
+       int cpu;
 
-       if (node < 0 || node >= MAX_NUMNODES) {
+       if (node < 0 || node >= nr_node_ids) {
                CDEBUG(D_INFO,
                       "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
                return;
        }
 
        mask = cpumask_of_node(node);
-       cfs_cpt_unset_cpumask(cptab, cpt, mask);
 
+       for_each_cpu(cpu, mask)
+               cfs_cpt_del_cpu(cptab, cpt, cpu);
+
+       cfs_cpt_del_node(cptab, cpt, node);
 }
 EXPORT_SYMBOL(cfs_cpt_unset_node);
 
-int
-cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
+int cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt,
+                        const nodemask_t *mask)
 {
        int     i;
 
@@ -436,8 +574,8 @@ cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
 }
 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
 
-void
-cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
+void cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt,
+                           const nodemask_t *mask)
 {
        int     i;
 
@@ -478,11 +616,10 @@ int cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
 }
 EXPORT_SYMBOL(cfs_cpt_spread_node);
 
-int
-cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
+int cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
 {
-       int     cpu = smp_processor_id();
-       int     cpt = cptab->ctb_cpu2cpt[cpu];
+       int cpu = smp_processor_id();
+       int cpt = cptab->ctb_cpu2cpt[cpu];
 
        if (cpt < 0) {
                if (!remap)
@@ -497,8 +634,7 @@ cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
 }
 EXPORT_SYMBOL(cfs_cpt_current);
 
-int
-cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
+int cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
 {
        LASSERT(cpu >= 0 && cpu < nr_cpu_ids);
 
@@ -506,8 +642,16 @@ cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
 }
 EXPORT_SYMBOL(cfs_cpt_of_cpu);
 
-int
-cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
+int cfs_cpt_of_node(struct cfs_cpt_table *cptab, int node)
+{
+       if (node < 0 || node > nr_node_ids)
+               return CFS_CPT_ANY;
+
+       return cptab->ctb_node2cpt[node];
+}
+EXPORT_SYMBOL(cfs_cpt_of_node);
+
+int cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
 {
        cpumask_t       *cpumask;
        nodemask_t      *nodemask;
@@ -552,14 +696,14 @@ EXPORT_SYMBOL(cfs_cpt_bind);
  * Choose max to \a number CPUs from \a node and set them in \a cpt.
  * We always prefer to choose CPU in the same core/socket.
  */
-static int
-cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
-                    cpumask_t *node, int number)
+static int cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
+                               cpumask_t *node, int number)
 {
-       cpumask_t       *socket = NULL;
-       cpumask_t       *core = NULL;
-       int             rc = 0;
-       int             cpu;
+       cpumask_t *socket = NULL;
+       cpumask_t *core = NULL;
+       int rc = 0;
+       int cpu;
+       int i;
 
        LASSERT(number > 0);
 
@@ -593,8 +737,6 @@ cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
                LASSERT(!cpumask_empty(socket));
 
                while (!cpumask_empty(socket)) {
-                       int     i;
-
                        /* get cpumask for hts in the same core */
                        cpumask_copy(core, topology_sibling_cpumask(cpu));
                        cpumask_and(core, core, node);
@@ -626,14 +768,13 @@ out:
        return rc;
 }
 
-#define CPT_WEIGHT_MIN  4u
+#define CPT_WEIGHT_MIN 4
 
-static unsigned int
-cfs_cpt_num_estimate(void)
+static int cfs_cpt_num_estimate(void)
 {
-       unsigned nnode = num_online_nodes();
-       unsigned ncpu  = num_online_cpus();
-       unsigned ncpt;
+       int nnode = num_online_nodes();
+       int ncpu  = num_online_cpus();
+       int ncpt;
 
        if (ncpu <= CPT_WEIGHT_MIN) {
                ncpt = 1;
@@ -660,7 +801,7 @@ out:
 #if (BITS_PER_LONG == 32)
        /* config many CPU partitions on 32-bit system could consume
         * too much memory */
-       ncpt = min(2U, ncpt);
+       ncpt = min(2, ncpt);
 #endif
        while (ncpu % ncpt != 0)
                ncpt--; /* worst case is 1 */
@@ -668,15 +809,14 @@ out:
        return ncpt;
 }
 
-static struct cfs_cpt_table *
-cfs_cpt_table_create(int ncpt)
+static struct cfs_cpt_table *cfs_cpt_table_create(int ncpt)
 {
        struct cfs_cpt_table *cptab = NULL;
-       cpumask_t       *mask = NULL;
-       int             cpt = 0;
-       int             num;
-       int             rc;
-       int             i;
+       cpumask_t *mask = NULL;
+       int cpt = 0;
+       int num;
+       int rc;
+       int i;
 
        rc = cfs_cpt_num_estimate();
        if (ncpt <= 0)
@@ -720,7 +860,7 @@ cfs_cpt_table_create(int ncpt)
 
                while (!cpumask_empty(mask)) {
                        struct cfs_cpu_partition *part;
-                       int    n;
+                       int n;
 
                        /* Each emulated NUMA node has all allowed CPUs in
                         * the mask.
@@ -834,7 +974,7 @@ cfs_cpt_table_create_pattern(char *pattern)
                return cptab;
        }
 
-       high = node ? MAX_NUMNODES - 1 : nr_cpu_ids - 1;
+       high = node ? nr_node_ids - 1 : nr_cpu_ids - 1;
 
        for (str = cfs_trimwhite(pattern), c = 0;; c++) {
                struct cfs_range_expr   *range;
@@ -921,11 +1061,11 @@ cfs_cpt_table_create_pattern(char *pattern)
 }
 
 #ifdef CONFIG_HOTPLUG_CPU
-static int
-cfs_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
+static int cfs_cpu_notify(struct notifier_block *self, unsigned long action,
+                         void *hcpu)
 {
-       unsigned int cpu = (unsigned long)hcpu;
-       bool         warn;
+       int cpu = (unsigned long)hcpu;
+       bool warn;
 
        switch (action) {
        case CPU_DEAD:
@@ -958,8 +1098,7 @@ static struct notifier_block cfs_cpu_notifier = {
 
 #endif
 
-void
-cfs_cpu_fini(void)
+void cfs_cpu_fini(void)
 {
        if (cfs_cpt_table != NULL)
                cfs_cpt_table_free(cfs_cpt_table);
@@ -969,8 +1108,7 @@ cfs_cpu_fini(void)
 #endif
 }
 
-int
-cfs_cpu_init(void)
+int cfs_cpu_init(void)
 {
        LASSERT(cfs_cpt_table == NULL);