From: Craig Gallek <kr...@google.com>

Add struct kobject to struct irq_desc to allow for easy export
to sysfs.  This allows for much simpler userspace-parsing of
the information contained in struct irq_desc.

Note that sysfs is not available at the time of early irq initialization.
These interrupts are accounted for using a postcore_initcall callback.

Signed-off-by: Craig Gallek <kr...@google.com>
---
v2 changes:
 * Several suggested stylistic changes.
 * Fix one NULL pointer dereference possibility.
 * Do not destroy kobjects in non-SPARSE_IRQ configuration.
 * Include #define CONFIG_SYSFS guards to selectively enable feature.

 include/linux/irqdesc.h |   3 +
 kernel/irq/irqdesc.c    | 189 +++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 190 insertions(+), 2 deletions(-)

diff --git a/include/linux/irqdesc.h b/include/linux/irqdesc.h
index b51beebf9804..aa41d39803b2 100644
--- a/include/linux/irqdesc.h
+++ b/include/linux/irqdesc.h
@@ -2,6 +2,7 @@
 #define _LINUX_IRQDESC_H
 
 #include <linux/rcupdate.h>
+#include <linux/kobject.h>
 
 /*
  * Core internal functions to deal with irq descriptors
@@ -45,6 +46,7 @@ struct pt_regs;
  * @rcu:               rcu head for delayed free
  * @dir:               /proc/irq/ procfs entry
  * @name:              flow handler name for /proc/interrupts output
+ * @kobj:              kobject used to represent this struct in sysfs
  */
 struct irq_desc {
        struct irq_common_data  irq_common_data;
@@ -92,6 +94,7 @@ struct irq_desc {
        int                     parent_irq;
        struct module           *owner;
        const char              *name;
+       struct kobject          kobj;
 } ____cacheline_internodealigned_in_smp;
 
 #ifdef CONFIG_SPARSE_IRQ
diff --git a/kernel/irq/irqdesc.c b/kernel/irq/irqdesc.c
index a623b44f2d4b..110d5aadc755 100644
--- a/kernel/irq/irqdesc.c
+++ b/kernel/irq/irqdesc.c
@@ -15,6 +15,7 @@
 #include <linux/radix-tree.h>
 #include <linux/bitmap.h>
 #include <linux/irqdomain.h>
+#include <linux/sysfs.h>
 
 #include "internals.h"
 
@@ -121,6 +122,154 @@ EXPORT_SYMBOL_GPL(nr_irqs);
 static DEFINE_MUTEX(sparse_irq_lock);
 static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS);
 
+static void irq_kobj_release(struct kobject *kobj);
+
+#ifdef CONFIG_SYSFS
+static struct kobject *irq_kobj_base;
+
+#define IRQ_ATTR_RO(_name) \
+static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
+
+static ssize_t per_cpu_count_show(struct kobject *kobj,
+                                 struct kobj_attribute *attr, char *buf)
+{
+       struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
+       int cpu, irq = desc->irq_data.irq;
+       ssize_t ret = 0;
+       char *p = "";
+
+       for_each_online_cpu(cpu) {
+               unsigned int c = kstat_irqs_cpu(irq, cpu);
+
+               ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%u", p, c);
+               p = ",";
+       }
+
+       ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
+       return ret;
+}
+IRQ_ATTR_RO(per_cpu_count);
+
+static ssize_t chip_name_show(struct kobject *kobj,
+                             struct kobj_attribute *attr, char *buf)
+{
+       struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
+       ssize_t ret = 0;
+
+       raw_spin_lock_irq(&desc->lock);
+       if (desc->irq_data.chip && desc->irq_data.chip->name) {
+               ret = scnprintf(buf, PAGE_SIZE, "%s\n",
+                               desc->irq_data.chip->name);
+       }
+       raw_spin_unlock_irq(&desc->lock);
+
+       return ret;
+}
+IRQ_ATTR_RO(chip_name);
+
+static ssize_t hwirq_show(struct kobject *kobj,
+                         struct kobj_attribute *attr, char *buf)
+{
+       struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
+       ssize_t ret = 0;
+
+       raw_spin_lock_irq(&desc->lock);
+       if (desc->irq_data.domain)
+               ret = sprintf(buf, "%d\n", (int)desc->irq_data.hwirq);
+       raw_spin_unlock_irq(&desc->lock);
+
+       return ret;
+}
+IRQ_ATTR_RO(hwirq);
+
+static ssize_t type_show(struct kobject *kobj,
+                        struct kobj_attribute *attr, char *buf)
+{
+       struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
+       ssize_t ret = 0;
+
+       raw_spin_lock_irq(&desc->lock);
+       ret = sprintf(buf, "%s\n",
+                     irqd_is_level_type(&desc->irq_data) ? "level" : "edge");
+       raw_spin_unlock_irq(&desc->lock);
+
+       return ret;
+
+}
+IRQ_ATTR_RO(type);
+
+static ssize_t name_show(struct kobject *kobj,
+                        struct kobj_attribute *attr, char *buf)
+{
+       struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
+       ssize_t ret = 0;
+
+       raw_spin_lock_irq(&desc->lock);
+       if (desc->name)
+               ret = scnprintf(buf, PAGE_SIZE, "%s\n", desc->name);
+       raw_spin_unlock_irq(&desc->lock);
+
+       return ret;
+}
+IRQ_ATTR_RO(name);
+
+static ssize_t actions_show(struct kobject *kobj,
+                           struct kobj_attribute *attr, char *buf)
+{
+       struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
+       struct irqaction *action;
+       ssize_t ret = 0;
+       char *p = "";
+
+       raw_spin_lock_irq(&desc->lock);
+       for (action = desc->action; action != NULL; action = action->next) {
+               ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
+                                p, action->name);
+               p = ",";
+       }
+       raw_spin_unlock_irq(&desc->lock);
+
+       if (ret)
+               ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
+
+       return ret;
+}
+IRQ_ATTR_RO(actions);
+
+static struct attribute *irq_attrs[] = {
+       &per_cpu_count_attr.attr,
+       &chip_name_attr.attr,
+       &hwirq_attr.attr,
+       &type_attr.attr,
+       &name_attr.attr,
+       &actions_attr.attr,
+       NULL
+};
+
+static struct kobj_type irq_kobj_type = {
+       .release        = irq_kobj_release,
+       .sysfs_ops      = &kobj_sysfs_ops,
+       .default_attrs  = irq_attrs,
+};
+
+static void irq_sysfs_add(int irq, struct irq_desc *desc)
+{
+       if (irq_kobj_base) {
+               if (kobject_add(&desc->kobj, irq_kobj_base, "%d", irq))
+                       pr_warn("Fail to add kobject for irq %d\n", irq);
+       }
+}
+
+#else /* !CONFIG_SYSFS */
+
+static struct kobj_type irq_kobj_type = {
+       .release        = irq_kobj_release,
+};
+
+static void irq_sysfs_add(int irq, struct irq_desc *desc) {}
+
+#endif /* CONFIG_SYSFS */
+
 #ifdef CONFIG_SPARSE_IRQ
 
 static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
@@ -187,6 +336,7 @@ static struct irq_desc *alloc_desc(int irq, int node, 
unsigned int flags,
 
        desc_set_defaults(irq, desc, node, affinity, owner);
        irqd_set(&desc->irq_data, flags);
+       kobject_init(&desc->kobj, &irq_kobj_type);
 
        return desc;
 
@@ -197,20 +347,28 @@ err_desc:
        return NULL;
 }
 
-static void delayed_free_desc(struct rcu_head *rhp)
+static void irq_kobj_release(struct kobject *kobj)
 {
-       struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu);
+       struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
 
        free_masks(desc);
        free_percpu(desc->kstat_irqs);
        kfree(desc);
 }
 
+static void delayed_free_desc(struct rcu_head *rhp)
+{
+       struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu);
+
+       kobject_put(&desc->kobj);
+}
+
 static void free_desc(unsigned int irq)
 {
        struct irq_desc *desc = irq_to_desc(irq);
 
        unregister_irq_proc(irq, desc);
+       kobject_del(&desc->kobj);
 
        /*
         * sparse_irq_lock protects also show_interrupts() and
@@ -261,6 +419,7 @@ static int alloc_descs(unsigned int start, unsigned int 
cnt, int node,
                        goto err;
                mutex_lock(&sparse_irq_lock);
                irq_insert_desc(start + i, desc);
+               irq_sysfs_add(start + i, desc);
                mutex_unlock(&sparse_irq_lock);
        }
        return start;
@@ -339,6 +498,7 @@ int __init early_irq_init(void)
                raw_spin_lock_init(&desc[i].lock);
                lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
                desc_set_defaults(i, &desc[i], node, NULL, NULL);
+               kobject_init(&desc[i].kobj, &irq_kobj_type);
        }
        return arch_early_irq_init();
 }
@@ -349,11 +509,15 @@ struct irq_desc *irq_to_desc(unsigned int irq)
 }
 EXPORT_SYMBOL(irq_to_desc);
 
+/* Statically allocated irq_desc is never deallocated */
+static void irq_kobj_release(struct kobject *kobj) {}
+
 static void free_desc(unsigned int irq)
 {
        struct irq_desc *desc = irq_to_desc(irq);
        unsigned long flags;
 
+       kobject_del(&desc->kobj);
        raw_spin_lock_irqsave(&desc->lock, flags);
        desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
        raw_spin_unlock_irqrestore(&desc->lock, flags);
@@ -369,6 +533,7 @@ static inline int alloc_descs(unsigned int start, unsigned 
int cnt, int node,
                struct irq_desc *desc = irq_to_desc(start + i);
 
                desc->owner = owner;
+               irq_sysfs_add(start + i, desc);
        }
        return start;
 }
@@ -730,3 +895,23 @@ unsigned int kstat_irqs_usr(unsigned int irq)
        irq_unlock_sparse();
        return sum;
 }
+
+#ifdef CONFIG_SYSFS
+static int __init irq_sysfs_init(void)
+{
+       struct irq_desc *desc;
+       int irq;
+
+       irq_kobj_base = kobject_create_and_add("irq", kernel_kobj);
+       if (!irq_kobj_base)
+               return -ENOMEM;
+
+       irq_lock_sparse();
+       for_each_irq_desc(irq, desc)
+               irq_sysfs_add(irq, desc);
+       irq_unlock_sparse();
+
+       return 0;
+}
+postcore_initcall(irq_sysfs_init);
+#endif /* CONFIG_SYSFS */
-- 
2.8.0.rc3.226.g39d4020

Reply via email to