cpumask: factor out show_cpumap into separate helper function
authorSudeep Holla <sudeep.holla@arm.com>
Tue, 30 Sep 2014 13:48:22 +0000 (14:48 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 7 Nov 2014 19:45:00 +0000 (11:45 -0800)
Many sysfs *_show function use cpu{list,mask}_scnprintf to copy cpumap
to the buffer aligned to PAGE_SIZE, append '\n' and '\0' to return null
terminated buffer with newline.

This patch creates a new helper function cpumap_print_to_pagebuf in
cpumask.h using newly added bitmap_print_to_pagebuf and consolidates
most of those sysfs functions using the new helper function.

Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
Suggested-by: Stephen Boyd <sboyd@codeaurora.org>
Tested-by: Stephen Boyd <sboyd@codeaurora.org>
Acked-by: "Rafael J. Wysocki" <rjw@rjwysocki.net>
Acked-by: Bjorn Helgaas <bhelgaas@google.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: x86@kernel.org
Cc: linux-acpi@vger.kernel.org
Cc: linux-pci@vger.kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
12 files changed:
arch/x86/kernel/cpu/perf_event_amd_iommu.c
arch/x86/kernel/cpu/perf_event_amd_uncore.c
arch/x86/kernel/cpu/perf_event_intel_rapl.c
arch/x86/kernel/cpu/perf_event_intel_uncore.c
drivers/acpi/acpi_pad.c
drivers/base/cpu.c
drivers/base/node.c
drivers/base/topology.c
drivers/pci/pci-sysfs.c
include/linux/bitmap.h
include/linux/cpumask.h
lib/bitmap.c

index 639d1289b1ba9a16cc7b9684d3697f5b79df0d27..97242a9242bdfde7dabba99e9a0d245ffe770c01 100644 (file)
@@ -130,10 +130,7 @@ static ssize_t _iommu_cpumask_show(struct device *dev,
                                   struct device_attribute *attr,
                                   char *buf)
 {
-       int n = cpulist_scnprintf(buf, PAGE_SIZE - 2, &iommu_cpumask);
-       buf[n++] = '\n';
-       buf[n] = '\0';
-       return n;
+       return cpumap_print_to_pagebuf(true, buf, &iommu_cpumask);
 }
 static DEVICE_ATTR(cpumask, S_IRUGO, _iommu_cpumask_show, NULL);
 
index 30790d798e6bd27400a76d3181538cf9f75a8771..cc6cedb8f25d5da565f09c612eb612a0eb7db701 100644 (file)
@@ -219,7 +219,6 @@ static ssize_t amd_uncore_attr_show_cpumask(struct device *dev,
                                            struct device_attribute *attr,
                                            char *buf)
 {
-       int n;
        cpumask_t *active_mask;
        struct pmu *pmu = dev_get_drvdata(dev);
 
@@ -230,10 +229,7 @@ static ssize_t amd_uncore_attr_show_cpumask(struct device *dev,
        else
                return 0;
 
-       n = cpulist_scnprintf(buf, PAGE_SIZE - 2, active_mask);
-       buf[n++] = '\n';
-       buf[n] = '\0';
-       return n;
+       return cpumap_print_to_pagebuf(true, buf, active_mask);
 }
 static DEVICE_ATTR(cpumask, S_IRUGO, amd_uncore_attr_show_cpumask, NULL);
 
index d64f275fe274a14df186eb31a247f04d683058eb..673f930c700f3ae745d8a408111f072dcce00dd1 100644 (file)
@@ -365,11 +365,7 @@ static void rapl_pmu_event_read(struct perf_event *event)
 static ssize_t rapl_get_attr_cpumask(struct device *dev,
                                struct device_attribute *attr, char *buf)
 {
-       int n = cpulist_scnprintf(buf, PAGE_SIZE - 2, &rapl_cpu_mask);
-
-       buf[n++] = '\n';
-       buf[n] = '\0';
-       return n;
+       return cpumap_print_to_pagebuf(true, buf, &rapl_cpu_mask);
 }
 
 static DEVICE_ATTR(cpumask, S_IRUGO, rapl_get_attr_cpumask, NULL);
index 9762dbd9f3f7d7a4e11f040aa19b21e31fe62667..08f3fed2b0f2bfe27997088f683b9613ddf30fe9 100644 (file)
@@ -647,11 +647,7 @@ static int uncore_pmu_event_init(struct perf_event *event)
 static ssize_t uncore_get_attr_cpumask(struct device *dev,
                                struct device_attribute *attr, char *buf)
 {
-       int n = cpulist_scnprintf(buf, PAGE_SIZE - 2, &uncore_cpu_mask);
-
-       buf[n++] = '\n';
-       buf[n] = '\0';
-       return n;
+       return cpumap_print_to_pagebuf(true, buf, &uncore_cpu_mask);
 }
 
 static DEVICE_ATTR(cpumask, S_IRUGO, uncore_get_attr_cpumask, NULL);
index f148a0580e0404b64b9fe1a104e05c0c1aaa498b..c7b105c0e1d377991f1e324fae161535e01efe27 100644 (file)
@@ -350,12 +350,10 @@ static ssize_t acpi_pad_idlecpus_store(struct device *dev,
 static ssize_t acpi_pad_idlecpus_show(struct device *dev,
        struct device_attribute *attr, char *buf)
 {
-       int n = 0;
-       n = cpumask_scnprintf(buf, PAGE_SIZE-2, to_cpumask(pad_busy_cpus_bits));
-       buf[n++] = '\n';
-       buf[n] = '\0';
-       return n;
+       return cpumap_print_to_pagebuf(false, buf,
+                                      to_cpumask(pad_busy_cpus_bits));
 }
+
 static DEVICE_ATTR(idlecpus, S_IRUGO|S_IWUSR,
        acpi_pad_idlecpus_show,
        acpi_pad_idlecpus_store);
index 006b1bc5297d292ba4d6e649b87c24f9bf1a9a82..4d8a56406fbbf80487a3334ad288aee00aa81917 100644 (file)
@@ -207,11 +207,8 @@ static ssize_t show_cpus_attr(struct device *dev,
                              char *buf)
 {
        struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr);
-       int n = cpulist_scnprintf(buf, PAGE_SIZE-2, *(ca->map));
 
-       buf[n++] = '\n';
-       buf[n] = '\0';
-       return n;
+       return cpumap_print_to_pagebuf(true, buf, *ca->map);
 }
 
 #define _CPU_ATTR(name, map) \
index 472168cd0c9787a99e7d7d70d4a22cb8bf8680ab..a3b82e9c7f200283c1c4a6c67ef7028707dd9abc 100644 (file)
@@ -25,32 +25,26 @@ static struct bus_type node_subsys = {
 };
 
 
-static ssize_t node_read_cpumap(struct device *dev, int type, char *buf)
+static ssize_t node_read_cpumap(struct device *dev, bool list, char *buf)
 {
        struct node *node_dev = to_node(dev);
        const struct cpumask *mask = cpumask_of_node(node_dev->dev.id);
-       int len;
 
        /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */
        BUILD_BUG_ON((NR_CPUS/32 * 9) > (PAGE_SIZE-1));
 
-       len = type?
-               cpulist_scnprintf(buf, PAGE_SIZE-2, mask) :
-               cpumask_scnprintf(buf, PAGE_SIZE-2, mask);
-       buf[len++] = '\n';
-       buf[len] = '\0';
-       return len;
+       return cpumap_print_to_pagebuf(list, buf, mask);
 }
 
 static inline ssize_t node_read_cpumask(struct device *dev,
                                struct device_attribute *attr, char *buf)
 {
-       return node_read_cpumap(dev, 0, buf);
+       return node_read_cpumap(dev, false, buf);
 }
 static inline ssize_t node_read_cpulist(struct device *dev,
                                struct device_attribute *attr, char *buf)
 {
-       return node_read_cpumap(dev, 1, buf);
+       return node_read_cpumap(dev, true, buf);
 }
 
 static DEVICE_ATTR(cpumap,  S_IRUGO, node_read_cpumask, NULL);
index be7c1fb7c0c96fa4d19bda57bf465c836a630091..f7c353843ddf119905c9dbee404238f8201aa52a 100644 (file)
@@ -42,29 +42,11 @@ static ssize_t show_##name(struct device *dev,                      \
        return sprintf(buf, "%d\n", topology_##name(dev->id));  \
 }
 
-#if defined(topology_thread_cpumask) || defined(topology_core_cpumask) || \
-    defined(topology_book_cpumask)
-static ssize_t show_cpumap(int type, const struct cpumask *mask, char *buf)
-{
-       ptrdiff_t len = PTR_ALIGN(buf + PAGE_SIZE - 1, PAGE_SIZE) - buf;
-       int n = 0;
-
-       if (len > 1) {
-               n = type?
-                       cpulist_scnprintf(buf, len-2, mask) :
-                       cpumask_scnprintf(buf, len-2, mask);
-               buf[n++] = '\n';
-               buf[n] = '\0';
-       }
-       return n;
-}
-#endif
-
 #define define_siblings_show_map(name)                                 \
 static ssize_t show_##name(struct device *dev,                         \
                           struct device_attribute *attr, char *buf)    \
 {                                                                      \
-       return show_cpumap(0, topology_##name(dev->id), buf);           \
+       return cpumap_print_to_pagebuf(false, buf, topology_##name(dev->id));\
 }
 
 #define define_siblings_show_list(name)                                        \
@@ -72,7 +54,7 @@ static ssize_t show_##name##_list(struct device *dev,                 \
                                  struct device_attribute *attr,        \
                                  char *buf)                            \
 {                                                                      \
-       return show_cpumap(1, topology_##name(dev->id), buf);           \
+       return cpumap_print_to_pagebuf(true, buf, topology_##name(dev->id));\
 }
 
 #define define_siblings_show_func(name)                \
index 2c6643fdc0cf27bcb9b726bb6c9febf3ca137dc7..368bdac42603c73ef05c8003f1da49d1a4be5959 100644 (file)
@@ -77,11 +77,10 @@ static ssize_t broken_parity_status_store(struct device *dev,
 }
 static DEVICE_ATTR_RW(broken_parity_status);
 
-static ssize_t pci_dev_show_local_cpu(struct device *dev, int type,
+static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list,
                                      struct device_attribute *attr, char *buf)
 {
        const struct cpumask *mask;
-       int len;
 
 #ifdef CONFIG_NUMA
        mask = (dev_to_node(dev) == -1) ? cpu_online_mask :
@@ -89,59 +88,41 @@ static ssize_t pci_dev_show_local_cpu(struct device *dev, int type,
 #else
        mask = cpumask_of_pcibus(to_pci_dev(dev)->bus);
 #endif
-       len = type ?
-               cpumask_scnprintf(buf, PAGE_SIZE-2, mask) :
-               cpulist_scnprintf(buf, PAGE_SIZE-2, mask);
-
-       buf[len++] = '\n';
-       buf[len] = '\0';
-       return len;
+       return cpumap_print_to_pagebuf(list, buf, mask);
 }
 
 static ssize_t local_cpus_show(struct device *dev,
                               struct device_attribute *attr, char *buf)
 {
-       return pci_dev_show_local_cpu(dev, 1, attr, buf);
+       return pci_dev_show_local_cpu(dev, false, attr, buf);
 }
 static DEVICE_ATTR_RO(local_cpus);
 
 static ssize_t local_cpulist_show(struct device *dev,
                                  struct device_attribute *attr, char *buf)
 {
-       return pci_dev_show_local_cpu(dev, 0, attr, buf);
+       return pci_dev_show_local_cpu(dev, true, attr, buf);
 }
 static DEVICE_ATTR_RO(local_cpulist);
 
 /*
  * PCI Bus Class Devices
  */
-static ssize_t pci_bus_show_cpuaffinity(struct device *dev, int type,
-                                       struct device_attribute *attr,
-                                       char *buf)
-{
-       int ret;
-       const struct cpumask *cpumask;
-
-       cpumask = cpumask_of_pcibus(to_pci_bus(dev));
-       ret = type ?
-               cpulist_scnprintf(buf, PAGE_SIZE-2, cpumask) :
-               cpumask_scnprintf(buf, PAGE_SIZE-2, cpumask);
-       buf[ret++] = '\n';
-       buf[ret] = '\0';
-       return ret;
-}
-
 static ssize_t cpuaffinity_show(struct device *dev,
                                struct device_attribute *attr, char *buf)
 {
-       return pci_bus_show_cpuaffinity(dev, 0, attr, buf);
+       const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
+
+       return cpumap_print_to_pagebuf(false, buf, cpumask);
 }
 static DEVICE_ATTR_RO(cpuaffinity);
 
 static ssize_t cpulistaffinity_show(struct device *dev,
                                    struct device_attribute *attr, char *buf)
 {
-       return pci_bus_show_cpuaffinity(dev, 1, attr, buf);
+       const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev));
+
+       return cpumap_print_to_pagebuf(true, buf, cpumask);
 }
 static DEVICE_ATTR_RO(cpulistaffinity);
 
index e1c8d080c4271d655f75a771ec71ab6ee81c66e3..9d5c3224a1e23f11a761eff49c1d99c4b4ae3974 100644 (file)
@@ -60,6 +60,7 @@
  * bitmap_find_free_region(bitmap, bits, order)        Find and allocate bit region
  * bitmap_release_region(bitmap, pos, order)   Free specified bit region
  * bitmap_allocate_region(bitmap, pos, order)  Allocate specified bit region
+ * bitmap_print_to_pagebuf(list, buf, mask, nbits) Print bitmap src as list/hex
  */
 
 /*
@@ -145,6 +146,8 @@ extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int o
 extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
 extern void bitmap_copy_le(void *dst, const unsigned long *src, int nbits);
 extern int bitmap_ord_to_pos(const unsigned long *bitmap, int n, int bits);
+extern int bitmap_print_to_pagebuf(bool list, char *buf,
+                                  const unsigned long *maskp, int nmaskbits);
 
 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
 #define BITMAP_LAST_WORD_MASK(nbits)                                   \
index 0a9a6da21e74fdde326f9dc04685dc1ea6e481c3..b950e9d6008b1e611f0092117160d92c88ed76d4 100644 (file)
@@ -803,6 +803,23 @@ static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
 }
 #endif /* NR_CPUS > BITS_PER_LONG */
 
+/**
+ * cpumap_print_to_pagebuf  - copies the cpumask into the buffer either
+ *     as comma-separated list of cpus or hex values of cpumask
+ * @list: indicates whether the cpumap must be list
+ * @mask: the cpumask to copy
+ * @buf: the buffer to copy into
+ *
+ * Returns the length of the (null-terminated) @buf string, zero if
+ * nothing is copied.
+ */
+static inline ssize_t
+cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask)
+{
+       return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask),
+                                     nr_cpumask_bits);
+}
+
 /*
  *
  * From here down, all obsolete.  Use cpumask_ variants!
index b499ab6ada29a0d4db2e4692083c2d21441a42ee..5bc7a1128fe830ad4e22017413d144ddc8831351 100644 (file)
@@ -12,6 +12,8 @@
 #include <linux/bitmap.h>
 #include <linux/bitops.h>
 #include <linux/bug.h>
+
+#include <asm/page.h>
 #include <asm/uaccess.h>
 
 /*
@@ -583,6 +585,33 @@ int bitmap_scnlistprintf(char *buf, unsigned int buflen,
 }
 EXPORT_SYMBOL(bitmap_scnlistprintf);
 
+/**
+ * bitmap_print_to_pagebuf - convert bitmap to list or hex format ASCII string
+ * @list: indicates whether the bitmap must be list
+ * @buf: page aligned buffer into which string is placed
+ * @maskp: pointer to bitmap to convert
+ * @nmaskbits: size of bitmap, in bits
+ *
+ * Output format is a comma-separated list of decimal numbers and
+ * ranges if list is specified or hex digits grouped into comma-separated
+ * sets of 8 digits/set. Returns the number of characters written to buf.
+ */
+int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
+                           int nmaskbits)
+{
+       ptrdiff_t len = PTR_ALIGN(buf + PAGE_SIZE - 1, PAGE_SIZE) - buf - 2;
+       int n = 0;
+
+       if (len > 1) {
+               n = list ? bitmap_scnlistprintf(buf, len, maskp, nmaskbits) :
+                          bitmap_scnprintf(buf, len, maskp, nmaskbits);
+               buf[n++] = '\n';
+               buf[n] = '\0';
+       }
+       return n;
+}
+EXPORT_SYMBOL(bitmap_print_to_pagebuf);
+
 /**
  * __bitmap_parselist - convert list format ASCII string to bitmap
  * @buf: read nul-terminated user string from this buffer