Skip to contentSkip to conversation

iio: adc: ina2xx: avoid kthread_stop() with stale task_struct

PATCH In review Akinobu Mita 2 participants 4 messages Jun 24, 2018 – Jul 7, 2018 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: adc: ina2xx: avoid kthread_stop() with stale task_struct

When the buffer is enabled for ina2xx driver, a dedicated kthread is invoked to capture mesurement data. When the buffer is disabled, the kthread is stopped.

However if the kthread gets register access errors, it immediately exits and when the malfunctional buffer is disabled, the stale task_struct pointer is accessed as there is no kthread to be stopped.

A similar issue in the usbip driver is prevented by kthread_get_run and kthread_stop_put helpers by increasing usage count of the task_struct. This change applies the same solution.

Cc Stefan Brüns <[email protected]>
Cc Jonathan Cameron <[email protected]>
---
 drivers/iio/adc/ina2xx-adc.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/iio/adc/ina2xx-adc.c +13 -4
index 0635a79..d123962 100644
@@ -30,6 +30,7 @@
 #include <linux/module.h>
 #include <linux/of_device.h>
 #include <linux/regmap.h>
+#include <linux/sched/task.h>
 #include <linux/util_macros.h>
 
 #include <linux/platform_data/ina2xx.h>
@@ -826,6 +827,7 @@ static int ina2xx_buffer_enable(struct iio_dev *indio_dev)
 {
 	struct ina2xx_chip_info *chip = iio_priv(indio_dev);
 	unsigned int sampling_us = SAMPLING_PERIOD(chip);
+	struct task_struct *task;
 
 	dev_dbg(&indio_dev->dev, "Enabling buffer w/ scan_mask %02x, freq = %d, avg =%u\n",
 		(unsigned int)(*indio_dev->active_scan_mask),
@@ -835,11 +837,17 @@ static int ina2xx_buffer_enable(struct iio_dev *indio_dev)
 	dev_dbg(&indio_dev->dev, "Async readout mode: %d\n",
 		chip->allow_async_readout);
 
-	chip->task = kthread_run(ina2xx_capture_thread, (void *)indio_dev,
-				 "%s:%d-%uus", indio_dev->name, indio_dev->id,
-				 sampling_us);
+	task = kthread_create(ina2xx_capture_thread, (void *)indio_dev,
+			      "%s:%d-%uus", indio_dev->name, indio_dev->id,
+			      sampling_us);
+	if (IS_ERR(task))
+		return PTR_ERR(task);
+
+	get_task_struct(task);
+	wake_up_process(task);
+	chip->task = task;
 
-	return PTR_ERR_OR_ZERO(chip->task);
+	return 0;
 }
 
 static int ina2xx_buffer_disable(struct iio_dev *indio_dev)
@@ -848,6 +856,7 @@ static int ina2xx_buffer_disable(struct iio_dev *indio_dev)
 
 	if (chip->task) {
 		kthread_stop(chip->task);
+		put_task_struct(chip->task);
 		chip->task = NULL;
 	}
 
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 12 lines · On Mon, 25 Jun 2018 00:05:21 +0900 Akinobu Mita <[email protected]> wrote:

When the buffer is enabled for ina2xx driver, a dedicated kthread is invoked to capture mesurement data. When the buffer is disabled, the kthread is stopped.
=20
However if the kthread gets register access errors, it immediately exits and when the malfunctional buffer is disabled, the stale task_struct pointer is accessed as there is no kthread to be stopped. =20
A similar issue in the usbip driver is prevented by kthread_get_run and kthread_stop_put helpers by increasing usage count of the task_struct. This change applies the same solution.
=20

Quoted 3 lines
Cc Stefan Br=C3=BCns <[email protected]>
Cc Jonathan Cameron <[email protected]>

Seems fine, but this is a fix so should have an appropriate fixes tag. Feel free to send one in reply to this thread rather than a v2.

Without a fixes tag it can be very hard to know exactly where a patch 'should' apply. I also have little visibility on how important backporting htis issue is. What would actually trigger the issue and is it likely to be seen in the wild?

Thanks,

Jonathan

drivers/iio/adc/ina2xx-adc.c static int ina2xx_buffer_enable(struct iio_dev *indio=

Quoted 17 lines
---
 drivers/iio/adc/ina2xx-adc.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

=20

drivers/iio/adc/ina2xx-adc.c +1 -0
index 0635a79..d123962 100644
@@ -30,6 +30,7 @@
 #include <linux/module.h>
 #include <linux/of_device.h>
 #include <linux/regmap.h>
+#include <linux/sched/task.h>
 #include <linux/util_macros.h>

=20
#include <linux/platform_data/ina2xx.h>

drivers/iio/adc/ina2xx-adc.c +0 -0
@@ -826,6 +827,7 @@ static int ina2xx_buffer_enable(struct iio_dev *indio=

_dev)

Quoted 6 lines
drivers/iio/adc/ina2xx-adc.c +1 -0
 {
 	struct ina2xx_chip_info *chip =3D iio_priv(indio_dev);
 	unsigned int sampling_us =3D SAMPLING_PERIOD(chip);
+	struct task_struct *task;

=20
dev_dbg(&indio_dev->dev, "Enabling buffer w/ scan_mask %02x, freq =3D %=

d, avg =3D%u\n",

drivers/iio/adc/ina2xx-adc.c static int ina2xx_buffer_enable(struct iio_dev *ind=

Quoted 2 lines
drivers/iio/adc/ina2xx-adc.c +0 -0
 		(unsigned int)(*indio_dev->active_scan_mask),
@@ -835,11 +837,17 @@ static int ina2xx_buffer_enable(struct iio_dev *ind=

io_dev)

drivers/iio/adc/ina2xx-adc.c static int ina2xx_buffer_disable(struct iio_dev *indi=

Quoted 22 lines
drivers/iio/adc/ina2xx-adc.c +0 -0
 	dev_dbg(&indio_dev->dev, "Async readout mode: %d\n",
 		chip->allow_async_readout);

=20

drivers/iio/adc/ina2xx-adc.c +9 -3
-	chip->task =3D kthread_run(ina2xx_capture_thread, (void *)indio_dev,
-				 "%s:%d-%uus", indio_dev->name, indio_dev->id,
-				 sampling_us);
+	task =3D kthread_create(ina2xx_capture_thread, (void *)indio_dev,
+			      "%s:%d-%uus", indio_dev->name, indio_dev->id,
+			      sampling_us);
+	if (IS_ERR(task))
+		return PTR_ERR(task);
+
+	get_task_struct(task);
+	wake_up_process(task);
+	chip->task =3D task;

=20

drivers/iio/adc/ina2xx-adc.c +1 -1
-	return PTR_ERR_OR_ZERO(chip->task);
+	return 0;
 }

=20
static int ina2xx_buffer_disable(struct iio_dev *indio_dev)

drivers/iio/adc/ina2xx-adc.c +0 -0
@@ -848,6 +856,7 @@ static int ina2xx_buffer_disable(struct iio_dev *indi=

o_dev)

Quoted 7 lines

=20

drivers/iio/adc/ina2xx-adc.c +1 -0
 	if (chip->task) {
 		kthread_stop(chip->task);
+		put_task_struct(chip->task);
 		chip->task =3D NULL;
 	}

=20

#2 lore raw

2018=E5=B9=B47=E6=9C=881=E6=97=A5(=E6=97=A5) 2:40 Jonathan Cameron <jic23@k= ernel.org>:

Quoted 9 lines
Quoted 5 lines · On Mon, 25 Jun 2018 00:05:21 +0900 Akinobu Mita <[email protected]> wrote:

When the buffer is enabled for ina2xx driver, a dedicated kthread is invoked to capture mesurement data. When the buffer is disabled, the kthread is stopped.

However if the kthread gets register access errors, it immediately exit=

s

Quoted 7 lines
Quoted 7 lines

and when the malfunctional buffer is disabled, the stale task_struct pointer is accessed as there is no kthread to be stopped.

A similar issue in the usbip driver is prevented by kthread_get_run and kthread_stop_put helpers by increasing usage count of the task_struct. This change applies the same solution.

Quoted 5 lines
Quoted 3 lines
Cc Stefan Br=C3=BCns <[email protected]>
Cc Jonathan Cameron <[email protected]>

Seems fine, but this is a fix so should have an appropriate fixes tag. Feel free to send one in reply to this thread rather than a v2.

It seems like this problem has been there ever since the ina2xx driver was added.

Fixes c43a102e67db ("iio: ina2xx: add support for TI INA2xx Power Monitors=

")

Quoted 4 lines

Without a fixes tag it can be very hard to know exactly where a patch 'should' apply. I also have little visibility on how important backporting htis issue is. What would actually trigger the issue and is it likely to be seen in the wild?

This issue was actually triggered in the system with an I2C controller that occasionally malfunctions due to a software bug in the controller driver.

BTW, there is no way to notify this error to the process that is calling poll() or read() to the buffer. Maybe we should implement the mechanism for returning POLLHUP or POLLERR events.

maintainer
#3 lore raw
Quoted 1 line · On Mon, 2 Jul 2018 17:44:42 +0900 Akinobu Mita <[email protected]> wrote:

2018=E5=B9=B47=E6=9C=881=E6=97=A5(=E6=97=A5) 2:40 Jonathan Cameron <jic23=

@kernel.org>:

Quoted 9 lines
Quoted 9 lines

On Mon, 25 Jun 2018 00:05:21 +0900
Akinobu Mita <[email protected]> wrote:
=20

Quoted 5 lines

When the buffer is enabled for ina2xx driver, a dedicated kthread is invoked to capture mesurement data. When the buffer is disabled, the kthread is stopped.

However if the kthread gets register access errors, it immediately ex=

its

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

and when the malfunctional buffer is disabled, the stale task_struct pointer is accessed as there is no kthread to be stopped.

A similar issue in the usbip driver is prevented by kthread_get_run a=

nd

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

kthread_stop_put helpers by increasing usage count of the task_struct. This change applies the same solution.

Cc Stefan Br=C3=BCns <[email protected]>
Cc Jonathan Cameron <[email protected]>

Seems fine, but this is a fix so should have an appropriate fixes tag. Feel free to send one in reply to this thread rather than a v2. =

=20

Quoted 5 lines

=20
It seems like this problem has been there ever since the ina2xx driver was added.
=20

Fixes c43a102e67db ("iio: ina2xx: add support for TI INA2xx Power Monito=

rs")

Quoted 1 line

=20

Thanks, added.

Quoted 8 lines
Quoted 4 lines

Without a fixes tag it can be very hard to know exactly where a patch 'should' apply. I also have little visibility on how important backporting htis issue is. What would actually trigger the issue and is it likely to be seen in the wild? =20

=20
This issue was actually triggered in the system with an I2C controller that occasionally malfunctions due to a software bug in the controller driver.

In that case I'm not going to rush it, but rather will take it via the next merge window.

Quoted 4 lines

=20
BTW, there is no way to notify this error to the process that is calling poll() or read() to the buffer. Maybe we should implement the mechanism for returning POLLHUP or POLLERR events.

I'm certainly not against adding something like that to notify on error cases. In general, minor hardware failures are not well handled in the kernel - there is no 'standard' way of doing it. Every now and then it is discussed but no one ever takes it any further.

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to play with it.

Thanks,

Jonathan

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html