Whamcloud - gitweb
LU-4588 code: replace semaphores with mutexes
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-cpu.c
index a708148..7318d81 100644 (file)
@@ -69,7 +69,7 @@ struct cfs_cpt_data {
        /* reserved for hotplug */
        unsigned long           cpt_version;
        /* mutex to protect cpt_cpumask */
-       struct semaphore        cpt_mutex;
+       struct mutex            cpt_mutex;
        /* scratch buffer for set/unset_node */
        cpumask_t               *cpt_cpumask;
 };
@@ -90,12 +90,12 @@ cfs_cpu_core_nsiblings(int cpu)
 {
        int     num;
 
-       down(&cpt_data.cpt_mutex);
+       mutex_lock(&cpt_data.cpt_mutex);
 
        cfs_cpu_core_siblings(cpu, cpt_data.cpt_cpumask);
        num = cpus_weight(*cpt_data.cpt_cpumask);
 
-       up(&cpt_data.cpt_mutex);
+       mutex_unlock(&cpt_data.cpt_mutex);
 
        return num;
 }
@@ -115,12 +115,12 @@ cfs_cpu_ht_nsiblings(int cpu)
 {
        int     num;
 
-       down(&cpt_data.cpt_mutex);
+       mutex_lock(&cpt_data.cpt_mutex);
 
        cfs_cpu_ht_siblings(cpu, cpt_data.cpt_cpumask);
        num = cpus_weight(*cpt_data.cpt_cpumask);
 
-       up(&cpt_data.cpt_mutex);
+       mutex_unlock(&cpt_data.cpt_mutex);
 
        return num;
 }
@@ -458,14 +458,14 @@ cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
                return 0;
        }
 
-       down(&cpt_data.cpt_mutex);
+       mutex_lock(&cpt_data.cpt_mutex);
 
        mask = cpt_data.cpt_cpumask;
        cfs_node_to_cpumask(node, mask);
 
        rc = cfs_cpt_set_cpumask(cptab, cpt, mask);
 
-       up(&cpt_data.cpt_mutex);
+       mutex_unlock(&cpt_data.cpt_mutex);
 
        return rc;
 }
@@ -482,14 +482,14 @@ cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
                return;
        }
 
-       down(&cpt_data.cpt_mutex);
+       mutex_lock(&cpt_data.cpt_mutex);
 
        mask = cpt_data.cpt_cpumask;
        cfs_node_to_cpumask(node, mask);
 
        cfs_cpt_unset_cpumask(cptab, cpt, mask);
 
-       up(&cpt_data.cpt_mutex);
+       mutex_unlock(&cpt_data.cpt_mutex);
 }
 EXPORT_SYMBOL(cfs_cpt_unset_node);
 
@@ -627,10 +627,10 @@ cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
                if (cpu_isset(i, *cpumask))
                        continue;
 
-               rc = set_cpus_allowed_ptr(cfs_current(), cpumask);
+               rc = set_cpus_allowed_ptr(current, cpumask);
                set_mems_allowed(*nodemask);
                if (rc == 0)
-                       cfs_schedule(); /* switch to allowed CPU */
+                       schedule(); /* switch to allowed CPU */
 
                return rc;
        }
@@ -814,8 +814,12 @@ cfs_cpt_table_create(int ncpt)
                        struct cfs_cpu_partition *part;
                        int    n;
 
-                       if (cpt >= ncpt)
-                               goto failed;
+                       /* Each emulated NUMA node has all allowed CPUs in
+                        * the mask.
+                        * End loop when all partitions have assigned CPUs.
+                        */
+                       if (cpt == ncpt)
+                               break;
 
                        part = &cptab->ctb_parts[cpt];
 
@@ -919,7 +923,7 @@ cfs_cpt_table_create_pattern(char *pattern)
                        break;
                }
 
-               if (sscanf(str, "%u%n", &cpt, &n) < 1) {
+               if (sscanf(str, "%d%n", &cpt, &n) < 1) {
                        CERROR("Invalid cpu pattern %s\n", str);
                        goto failed;
                }
@@ -954,7 +958,7 @@ cfs_cpt_table_create_pattern(char *pattern)
                        goto failed;
                }
 
-               cfs_list_for_each_entry(range, &el->el_exprs, re_link) {
+               list_for_each_entry(range, &el->el_exprs, re_link) {
                        for (i = range->re_lo; i <= range->re_hi; i++) {
                                if ((i - range->re_lo) % range->re_stride != 0)
                                        continue;
@@ -989,7 +993,8 @@ cfs_cpt_table_create_pattern(char *pattern)
 static int
 cfs_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
 {
-       unsigned int  cpu = (unsigned long)hcpu;
+       unsigned int cpu = (unsigned long)hcpu;
+       bool         warn;
 
        switch (action) {
        case CPU_DEAD:
@@ -1000,9 +1005,21 @@ cfs_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
                cpt_data.cpt_version++;
                spin_unlock(&cpt_data.cpt_lock);
        default:
-               CWARN("Lustre: can't support CPU hotplug well now, "
-                     "performance and stability could be impacted"
-                     "[CPU %u notify: %lx]\n", cpu, action);
+               if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
+                       CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
+                              cpu, action);
+                       break;
+               }
+
+               mutex_lock(&cpt_data.cpt_mutex);
+               /* if all HTs in a core are offline, it may break affinity */
+               cfs_cpu_ht_siblings(cpu, cpt_data.cpt_cpumask);
+               warn = any_online_cpu(*cpt_data.cpt_cpumask) >= nr_cpu_ids;
+               mutex_unlock(&cpt_data.cpt_mutex);
+               CDEBUG(warn ? D_WARNING : D_INFO,
+                      "Lustre: can't support CPU plug-out well now, "
+                      "performance and stability could be impacted"
+                      "[CPU %u action: %lx]\n", cpu, action);
        }
 
        return NOTIFY_OK;
@@ -1042,7 +1059,7 @@ cfs_cpu_init(void)
        }
 
        spin_lock_init(&cpt_data.cpt_lock);
-       sema_init(&cpt_data.cpt_mutex, 1);
+       mutex_init(&cpt_data.cpt_mutex);
 
 #ifdef CONFIG_HOTPLUG_CPU
        register_hotcpu_notifier(&cfs_cpu_notifier);