Whamcloud - gitweb
LU-5304: do not memset after LIBCFS_ALLOC
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-cpu.c
index 57cf5c7..7318d81 100644 (file)
@@ -22,7 +22,7 @@
  */
 /*
  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
- * Copyright (c) 2011, 2012, Whamcloud, Inc.
+ * Copyright (c) 2012, 2013, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -55,7 +55,7 @@ CFS_MODULE_PARM(cpu_npartitions, "i", int, 0444, "# of CPU partitions");
  * i.e: "0[0,1,2,3] 1[4,5,6,7]", number before bracket is CPU partition ID,
  *      number in bracket is processor ID (core or HT)
  *
- * i.e: "N 0[0,1] 1[2,3]" the first character 'n' means numbers in bracket
+ * i.e: "N 0[0,1] 1[2,3]" the first character 'N' means numbers in bracket
  *       are NUMA node ID, number before bracket is CPU partition ID.
  *
  * NB: If user specified cpu_pattern, cpu_npartitions will be ignored
@@ -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;
 };
@@ -79,15 +79,8 @@ static struct cfs_cpt_data   cpt_data;
 void
 cfs_cpu_core_siblings(int cpu, cpumask_t *mask)
 {
-#if defined(HAVE_TOPOLOGY_CORE_CPUMASK)
        /* return cpumask of cores in the same socket */
        cpumask_copy(mask, topology_core_cpumask(cpu));
-#elif defined(HAVE_TOPOLOGY_CORE_SIBLINGS)
-       *mask = topology_core_siblings(cpu);
-#else
-       cpus_clear(*mask);
-       cpu_set(cpu, *mask);
-#endif
 }
 EXPORT_SYMBOL(cfs_cpu_core_siblings);
 
@@ -97,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;
 }
@@ -112,14 +105,7 @@ EXPORT_SYMBOL(cfs_cpu_core_nsiblings);
 void
 cfs_cpu_ht_siblings(int cpu, cpumask_t *mask)
 {
-#if defined(HAVE_TOPOLOGY_THREAD_CPUMASK)
        cpumask_copy(mask, topology_thread_cpumask(cpu));
-#elif defined(HAVE_TOPOLOGY_THREAD_SIBLINGS)
-       *mask = topology_thread_siblings(cpu);
-#else
-       cpus_clear(*mask);
-       cpu_set(cpu, *mask);
-#endif
 }
 EXPORT_SYMBOL(cfs_cpu_ht_siblings);
 
@@ -129,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;
 }
@@ -143,17 +129,7 @@ EXPORT_SYMBOL(cfs_cpu_ht_nsiblings);
 void
 cfs_node_to_cpumask(int node, cpumask_t *mask)
 {
-#if defined(HAVE_CPUMASK_OF_NODE)
        cpumask_copy(mask, cpumask_of_node(node));
-#elif defined(HAVE_NODE_TO_CPUMASK)
-       *mask = node_to_cpumask(node);
-#else
-       cpus_clear(*mask);
-       for_each_online_cpu(i) {
-               if (cpu_to_node(i) == node)
-                       cpu_set(i, *mask);
-       }
-#endif
 }
 EXPORT_SYMBOL(cfs_node_to_cpumask);
 
@@ -482,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;
 }
@@ -506,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);
 
@@ -651,21 +627,15 @@ cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
                if (cpu_isset(i, *cpumask))
                        continue;
 
-#ifdef HAVE_SET_CPUS_ALLOWED
-               rc = set_cpus_allowed(cfs_current(), *cpumask);
-#else
-               rc = set_cpus_allowed_ptr(cfs_current(), cpumask);
-#endif
-#ifdef HAVE_SET_MEMS_ALLOWED
+               rc = set_cpus_allowed_ptr(current, cpumask);
                set_mems_allowed(*nodemask);
-#endif
                if (rc == 0)
-                       cfs_schedule(); /* switch to allowed CPU */
+                       schedule(); /* switch to allowed CPU */
 
                return rc;
        }
 
-       /* don't need to set affinity baecause all online CPUs are covered */
+       /* don't need to set affinity because all online CPUs are covered */
        return 0;
 }
 EXPORT_SYMBOL(cfs_cpt_bind);
@@ -844,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];
 
@@ -949,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;
                }
@@ -984,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;
@@ -1019,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:
@@ -1030,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;
@@ -1072,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);