[PATCH 2/3] Make IRQ handlers typesafe. - Kernel

This is a discussion on [PATCH 2/3] Make IRQ handlers typesafe. - Kernel ; This patch lets interrupt handler functions have their natural type (ie. exactly match the data pointer type); for transition it allows the old irq_handler_t type as well. To do this it uses a gcc extension, cast-to-union, which allows a type ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: [PATCH 2/3] Make IRQ handlers typesafe.

  1. [PATCH 2/3] Make IRQ handlers typesafe.

    This patch lets interrupt handler functions have their natural type
    (ie. exactly match the data pointer type); for transition it allows
    the old irq_handler_t type as well.

    To do this it uses a gcc extension, cast-to-union, which allows a type
    to be cast to any type within the union. When used in a wrapper
    macro, it's a simple way of allowing one of several types.

    (Some drivers now need to be cleaned up to compile, previous patch).

    Signed-off-by: Rusty Russell
    ---
    include/linux/interrupt.h | 17 +++++++++++++++--
    include/linux/kernel.h | 7 +++++++
    kernel/irq/devres.c | 10 +++++-----
    kernel/irq/manage.c | 6 +++---
    4 files changed, 30 insertions(+), 10 deletions(-)

    diff -r 0fe1a980708b include/linux/interrupt.h
    --- a/include/linux/interrupt.h Thu Jan 17 14:48:56 2008 +1100
    +++ b/include/linux/interrupt.h Thu Jan 17 15:42:01 2008 +1100
    @@ -69,13 +69,26 @@ struct irqaction {
    };

    extern irqreturn_t no_action(int cpl, void *dev_id);
    -extern int __must_check request_irq(unsigned int, irq_handler_t handler,
    +
    +/* Typesafe version of request_irq. Also takes old-style void * handlers. */
    +#define request_irq(irq, handler, flags, name, dev_id) \
    + __request_irq((irq), \
    + check_either_type((handler), irq_handler_t, \
    + int (*)(int, typeof(dev_id))), \
    + (flags), (name), (dev_id))
    +
    +extern int __must_check __request_irq(unsigned int, irq_handler_t handler,
    unsigned long, const char *, void *);
    extern void free_irq(unsigned int, void *);

    struct device;

    -extern int __must_check devm_request_irq(struct device *dev, unsigned int irq,
    +#define devm_request_irq(dev, irq, handler, flags, name, dev_id) \
    + __devm_request_irq((dev), (irq), \
    + check_either_type((handler), irq_handler_t, \
    + int (*)(int, typeof(dev_id))), \
    + (flags), (name), (dev_id))
    +extern int __must_check __devm_request_irq(struct device *dev, unsigned int irq,
    irq_handler_t handler, unsigned long irqflags,
    const char *devname, void *dev_id);
    extern void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id);
    diff -r 0fe1a980708b include/linux/kernel.h
    --- a/include/linux/kernel.h Thu Jan 17 14:48:56 2008 +1100
    +++ b/include/linux/kernel.h Thu Jan 17 15:42:01 2008 +1100
    @@ -379,6 +379,13 @@ static inline int __attribute__ ((format
    (void)__tmp; \
    })

    +/*
    + * Returns var as a type1. If it's not a type1 or type2 you'll get:
    + * "error: cast to union type from type not present in union"
    + */
    +#define check_either_type(var, type1, type2) \
    + (((union { typeof(type1) _t1; typeof(type2) _t2; })var)._t1)
    +
    struct sysinfo;
    extern int do_sysinfo(struct sysinfo *info);

    diff -r 0fe1a980708b kernel/irq/devres.c
    --- a/kernel/irq/devres.c Thu Jan 17 14:48:56 2008 +1100
    +++ b/kernel/irq/devres.c Thu Jan 17 15:42:01 2008 +1100
    @@ -41,9 +41,9 @@ static int devm_irq_match(struct device
    * If an IRQ allocated with this function needs to be freed
    * separately, dev_free_irq() must be used.
    */
    -int devm_request_irq(struct device *dev, unsigned int irq,
    - irq_handler_t handler, unsigned long irqflags,
    - const char *devname, void *dev_id)
    +int __devm_request_irq(struct device *dev, unsigned int irq,
    + irq_handler_t handler, unsigned long irqflags,
    + const char *devname, void *dev_id)
    {
    struct irq_devres *dr;
    int rc;
    @@ -53,7 +53,7 @@ int devm_request_irq(struct device *dev,
    if (!dr)
    return -ENOMEM;

    - rc = request_irq(irq, handler, irqflags, devname, dev_id);
    + rc = __request_irq(irq, handler, irqflags, devname, dev_id);
    if (rc) {
    devres_free(dr);
    return rc;
    @@ -65,7 +65,7 @@ int devm_request_irq(struct device *dev,

    return 0;
    }
    -EXPORT_SYMBOL(devm_request_irq);
    +EXPORT_SYMBOL(__devm_request_irq);

    /**
    * devm_free_irq - free an interrupt
    diff -r 0fe1a980708b kernel/irq/manage.c
    --- a/kernel/irq/manage.c Thu Jan 17 14:48:56 2008 +1100
    +++ b/kernel/irq/manage.c Thu Jan 17 15:42:01 2008 +1100
    @@ -514,8 +514,8 @@ EXPORT_SYMBOL(free_irq);
    * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy
    *
    */
    -int request_irq(unsigned int irq, irq_handler_t handler,
    - unsigned long irqflags, const char *devname, void *dev_id)
    +int __request_irq(unsigned int irq, irq_handler_t handler,
    + unsigned long irqflags, const char *devname, void *dev_id)
    {
    struct irqaction *action;
    int retval;
    @@ -576,4 +576,4 @@ int request_irq(unsigned int irq, irq_ha

    return retval;
    }
    -EXPORT_SYMBOL(request_irq);
    +EXPORT_SYMBOL(__request_irq);
    --
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to majordomo@vger.kernel.org
    More majordomo info at http://vger.kernel.org/majordomo-info.html
    Please read the FAQ at http://www.tux.org/lkml/

  2. [PATCH 3/3] Makes lguest's irq handler typesafe

    Just a trivial example.
    ---
    drivers/lguest/lguest_device.c | 3 +--
    1 file changed, 1 insertion(+), 2 deletions(-)

    diff -r 00ab7672f658 drivers/lguest/lguest_device.c
    --- a/drivers/lguest/lguest_device.c Thu Jan 17 16:54:00 2008 +1100
    +++ b/drivers/lguest/lguest_device.c Thu Jan 17 16:59:46 2008 +1100
    @@ -179,9 +179,8 @@ static void lg_notify(struct virtqueue *
    hcall(LHCALL_NOTIFY, lvq->config.pfn << PAGE_SHIFT, 0, 0);
    }

    -static irqreturn_t lguest_interrupt(int irq, void *_vq)
    +static irqreturn_t lguest_interrupt(int irq, struct virtqueue *vq)
    {
    - struct virtqueue *vq = _vq;
    struct lguest_device_desc *desc = to_lgdev(vq->vdev)->desc;

    if (unlikely(desc->config_change)) {
    --
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to majordomo@vger.kernel.org
    More majordomo info at http://vger.kernel.org/majordomo-info.html
    Please read the FAQ at http://www.tux.org/lkml/

+ Reply to Thread