Skip to contentSkip to conversation

iio: adc: exynos: do not rely on 'users' counter in ISR

PATCH Superseded [email protected] 3 participants 4 messages Oct 6, 2020

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iio: adc: exynos: do not rely on 'users' counter in ISR

The order in which 'users' counter is decremented vs calling drivers' close() method is implementation specific, and we should not rely on it. Let's introduce driver private flag and use it to signal ISR to exit when device is being closed.

This has a side-effect of fixing issue of accessing inut->users outside of input->mutex protection.

---

v2: switched from ordinary read/write to READ_ONCE/WRITE_ONCE per Michał Mirosław

 drivers/iio/adc/exynos_adc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iio/adc/exynos_adc.c +6 -1
index 22131a677445..6c705fe599a3 100644
@@ -7,6 +7,7 @@
  *  Copyright (C) 2013 Naveen Krishna Chatradhi <[email protected]>
  */
 
+#include <linux/compiler.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
 #include <linux/interrupt.h>
@@ -135,6 +136,8 @@ struct exynos_adc {
 	u32			value;
 	unsigned int            version;
 
+	bool			ts_enabled;
+
 	bool			read_ts;
 	u32			ts_x;
 	u32			ts_y;
@@ -633,7 +636,7 @@ static irqreturn_t exynos_ts_isr(int irq, void *dev_id)
 	bool pressed;
 	int ret;
 
-	while (info->input->users) {
+	while (READ_ONCE(info->ts_enabled)) {
 		ret = exynos_read_s3c64xx_ts(dev, &x, &y);
 		if (ret == -ETIMEDOUT)
 			break;
@@ -712,6 +715,7 @@ static int exynos_adc_ts_open(struct input_dev *dev)
 {
 	struct exynos_adc *info = input_get_drvdata(dev);
 
+	WRITE_ONCE(info->ts_enabled, true);
 	enable_irq(info->tsirq);
 
 	return 0;
@@ -721,6 +725,7 @@ static void exynos_adc_ts_close(struct input_dev *dev)
 {
 	struct exynos_adc *info = input_get_drvdata(dev);
 
+	WRITE_ONCE(info->ts_enabled, true);
 	disable_irq(info->tsirq);
 }
 
Signature · 5 lines
2.28.0.806.g8561365e88-goog


-- 
Dmitry
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 11 lines · On Tue, 6 Oct 2020 at 06:12, <[email protected]> wrote:

The order in which 'users' counter is decremented vs calling drivers' close() method is implementation specific, and we should not rely on it. Let's introduce driver private flag and use it to signal ISR to exit when device is being closed.

This has a side-effect of fixing issue of accessing inut->users outside of input->mutex protection.

Quoted 7 lines

---

v2: switched from ordinary read/write to READ_ONCE/WRITE_ONCE per Michał Mirosław

 drivers/iio/adc/exynos_adc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Best regards,
Krzysztof

Reviewed-by on v2 1/1
#2 lore raw
In reply to [email protected]
Quoted 7 lines · On Mon, Oct 05, 2020 at 09:12:14PM -0700, [email protected] wrote:

The order in which 'users' counter is decremented vs calling drivers' close() method is implementation specific, and we should not rely on it. Let's introduce driver private flag and use it to signal ISR to exit when device is being closed.

This has a side-effect of fixing issue of accessing inut->users outside of input->mutex protection.

[...]

(after with a fix mentioned below)

Quoted 2 lines
drivers/iio/adc/exynos_adc.c +0 -0

[...]

Quoted 9 lines
drivers/iio/adc/exynos_adc.c +1 -0
@@ -712,6 +715,7 @@ static int exynos_adc_ts_open(struct input_dev *dev)
 {
 	struct exynos_adc *info = input_get_drvdata(dev);
 
+	WRITE_ONCE(info->ts_enabled, true);
 	enable_irq(info->tsirq);
 
 	return 0;

drivers/iio/adc/exynos_adc.c static void exynos_adc_ts_close(struct input_dev *dev) · line 729

Quoted 5 lines
drivers/iio/adc/exynos_adc.c +1 -0
@@ -721,6 +725,7 @@ static void exynos_adc_ts_close(struct input_dev *dev)
 {
 	struct exynos_adc *info = input_get_drvdata(dev);
 
+	WRITE_ONCE(info->ts_enabled, true);
 	disable_irq(info->tsirq);

Shouldn't 'true' be 'false' here?

Best Regards,
Michał Mirosław

#3 lore raw
Quoted 28 lines · Michał Mirosław
Quoted 7 lines · On Mon, Oct 05, 2020 at 09:12:14PM -0700, [email protected] wrote:

The order in which 'users' counter is decremented vs calling drivers' close() method is implementation specific, and we should not rely on it. Let's introduce driver private flag and use it to signal ISR to exit when device is being closed.

This has a side-effect of fixing issue of accessing inut->users outside of input->mutex protection.

[...]

(after with a fix mentioned below)

Quoted 2 lines
drivers/iio/adc/exynos_adc.c +0 -0

[...]

Quoted 12 lines
drivers/iio/adc/exynos_adc.c +1 -0
@@ -712,6 +715,7 @@ static int exynos_adc_ts_open(struct input_dev *dev)
 {
 	struct exynos_adc *info = input_get_drvdata(dev);
 
+	WRITE_ONCE(info->ts_enabled, true);
 	enable_irq(info->tsirq);
 
 	return 0;
@@ -721,6 +725,7 @@ static void exynos_adc_ts_close(struct input_dev *dev)
 {
 	struct exynos_adc *info = input_get_drvdata(dev);
 
Quoted 4 lines

drivers/iio/adc/exynos_adc.c static void exynos_adc_ts_close(struct input_dev *dev) · line 729

Quoted 2 lines
drivers/iio/adc/exynos_adc.c +1 -0
+	WRITE_ONCE(info->ts_enabled, true);
 	disable_irq(info->tsirq);

Shouldn't 'true' be 'false' here?

I swear if we disable cut-n-paste functionality there will be markable reduction in bug rates...

Thanks for noticing this!

Signature · 1 line
Dmitry