Commit a1a8e1dc authored by Lars-Peter Clausen's avatar Lars-Peter Clausen Committed by Jonathan Cameron

iio:trigger: Fix use_count race condition

When using more than one trigger consumer it can happen that multiple threads
perform a read-modify-update cycle on 'use_count' concurrently. This can cause
updates to be lost and use_count can get stuck at non-zero value, in which case
the IIO core assumes that at least one thread is still running and will wait for
it to finish before running any trigger handlers again. This effectively renders
the trigger disabled and a reboot is necessary before it can be used again. To
fix this make use_count an atomic variable. Also set it to the number of
consumers before starting the first consumer, otherwise it might happen that
use_count drops to 0 even though not all consumers have been run yet.
Signed-off-by: default avatarLars-Peter Clausen <lars@metafoo.de>
Tested-by: default avatarDenis Ciocca <denis.ciocca@st.com>
Signed-off-by: default avatarJonathan Cameron <jic23@kernel.org>
parent 78077256
...@@ -127,12 +127,17 @@ static struct iio_trigger *iio_trigger_find_by_name(const char *name, ...@@ -127,12 +127,17 @@ static struct iio_trigger *iio_trigger_find_by_name(const char *name,
void iio_trigger_poll(struct iio_trigger *trig, s64 time) void iio_trigger_poll(struct iio_trigger *trig, s64 time)
{ {
int i; int i;
if (!trig->use_count)
for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) if (!atomic_read(&trig->use_count)) {
if (trig->subirqs[i].enabled) { atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
trig->use_count++;
for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
if (trig->subirqs[i].enabled)
generic_handle_irq(trig->subirq_base + i); generic_handle_irq(trig->subirq_base + i);
} else
iio_trigger_notify_done(trig);
}
}
} }
EXPORT_SYMBOL(iio_trigger_poll); EXPORT_SYMBOL(iio_trigger_poll);
...@@ -146,19 +151,24 @@ EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll); ...@@ -146,19 +151,24 @@ EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
void iio_trigger_poll_chained(struct iio_trigger *trig, s64 time) void iio_trigger_poll_chained(struct iio_trigger *trig, s64 time)
{ {
int i; int i;
if (!trig->use_count)
for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) if (!atomic_read(&trig->use_count)) {
if (trig->subirqs[i].enabled) { atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
trig->use_count++;
for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
if (trig->subirqs[i].enabled)
handle_nested_irq(trig->subirq_base + i); handle_nested_irq(trig->subirq_base + i);
} else
iio_trigger_notify_done(trig);
}
}
} }
EXPORT_SYMBOL(iio_trigger_poll_chained); EXPORT_SYMBOL(iio_trigger_poll_chained);
void iio_trigger_notify_done(struct iio_trigger *trig) void iio_trigger_notify_done(struct iio_trigger *trig)
{ {
trig->use_count--; if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
if (trig->use_count == 0 && trig->ops && trig->ops->try_reenable) trig->ops->try_reenable)
if (trig->ops->try_reenable(trig)) if (trig->ops->try_reenable(trig))
/* Missed an interrupt so launch new poll now */ /* Missed an interrupt so launch new poll now */
iio_trigger_poll(trig, 0); iio_trigger_poll(trig, 0);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
*/ */
#include <linux/irq.h> #include <linux/irq.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/atomic.h>
#ifndef _IIO_TRIGGER_H_ #ifndef _IIO_TRIGGER_H_
#define _IIO_TRIGGER_H_ #define _IIO_TRIGGER_H_
...@@ -61,7 +62,7 @@ struct iio_trigger { ...@@ -61,7 +62,7 @@ struct iio_trigger {
struct list_head list; struct list_head list;
struct list_head alloc_list; struct list_head alloc_list;
int use_count; atomic_t use_count;
struct irq_chip subirq_chip; struct irq_chip subirq_chip;
int subirq_base; int subirq_base;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment