Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

staging:iio:sysfs-trigger: Use irq_work to properly active trigger

Since iio_trigger_poll() calls generic_handle_irq() it need to be called from
hardirq context. The sysfs trigger is kicked from userspace, so it is obviously
not possible to fulfill this requirement by calling iio_trigger_poll directly.
As a workaround commit 1f785681 ("staging:iio:trigger sysfs userspace trigger
rework.") added iio_trigger_poll_chained() which uses handle_nested_irq instead
of generic_handle_irq. This in itself is a hack and only works by chance.
handle_nested_irq is intended to be called from the threaded interrupt handler
of the parent IRQ. Using handle_nested_irq is also problematic since it will
only call the threaded handler of the IRQ. But quite a few IIO drivers rely on
their hardirq handler being called or undefined behaviour will occur.

This patch uses the irq_work framework to schedule the call to
iio_trigger_poll() from hardirq context, which fixes the issues described above.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>

authored by

Lars-Peter Clausen and committed by
Jonathan Cameron
f38bc926 ed5514c9

+18 -1
+2
drivers/staging/iio/trigger/Kconfig
··· 21 21 config IIO_SYSFS_TRIGGER 22 22 tristate "SYSFS trigger" 23 23 depends on SYSFS 24 + depends on HAVE_IRQ_WORK 25 + select IRQ_WORK 24 26 help 25 27 Provides support for using SYSFS entry as IIO triggers. 26 28 If unsure, say N (but it's safe to say "Y").
+16 -1
drivers/staging/iio/trigger/iio-trig-sysfs.c
··· 10 10 #include <linux/platform_device.h> 11 11 #include <linux/slab.h> 12 12 #include <linux/list.h> 13 + #include <linux/irq_work.h> 13 14 14 15 #include <linux/iio/iio.h> 15 16 #include <linux/iio/trigger.h> 16 17 17 18 struct iio_sysfs_trig { 18 19 struct iio_trigger *trig; 20 + struct irq_work work; 19 21 int id; 20 22 struct list_head l; 21 23 }; ··· 91 89 .release = &iio_trigger_sysfs_release, 92 90 }; 93 91 92 + static void iio_sysfs_trigger_work(struct irq_work *work) 93 + { 94 + struct iio_sysfs_trig *trig = container_of(work, struct iio_sysfs_trig, 95 + work); 96 + 97 + iio_trigger_poll(trig->trig, 0); 98 + } 99 + 94 100 static ssize_t iio_sysfs_trigger_poll(struct device *dev, 95 101 struct device_attribute *attr, const char *buf, size_t count) 96 102 { 97 103 struct iio_trigger *trig = to_iio_trigger(dev); 98 - iio_trigger_poll_chained(trig, 0); 104 + struct iio_sysfs_trig *sysfs_trig = trig->private_data; 105 + 106 + irq_work_queue(&sysfs_trig->work); 99 107 100 108 return count; 101 109 } ··· 160 148 t->trig->dev.groups = iio_sysfs_trigger_attr_groups; 161 149 t->trig->ops = &iio_sysfs_trigger_ops; 162 150 t->trig->dev.parent = &iio_sysfs_trig_dev; 151 + t->trig->private_data = t; 152 + 153 + init_irq_work(&t->work, iio_sysfs_trigger_work); 163 154 164 155 ret = iio_trigger_register(t->trig); 165 156 if (ret)