2023-04-23 23:23:47

by William Breathitt Gray

[permalink] [raw]
Subject: [PATCH 4.14 v4 1/5] iio: counter: 104-quad-8: Fix race condition between FLAG and CNTR reads

commit 4aa3b75c74603c3374877d5fd18ad9cc3a9a62ed upstream.

The Counter (CNTR) register is 24 bits wide, but we can have an
effective 25-bit count value by setting bit 24 to the XOR of the Borrow
flag and Carry flag. The flags can be read from the FLAG register, but a
race condition exists: the Borrow flag and Carry flag are instantaneous
and could change by the time the count value is read from the CNTR
register.

Since the race condition could result in an incorrect 25-bit count
value, remove support for 25-bit count values from this driver.

Fixes: 28e5d3bb0325 ("iio: 104-quad-8: Add IIO support for the ACCES 104-QUAD-8")
Cc: <[email protected]> # 4.14.x
Signed-off-by: William Breathitt Gray <[email protected]>
---
drivers/iio/counter/104-quad-8.c | 10 +---------
1 file changed, 1 insertion(+), 9 deletions(-)

diff --git a/drivers/iio/counter/104-quad-8.c b/drivers/iio/counter/104-quad-8.c
index 181585ae6e..bdb07694e2 100644
--- a/drivers/iio/counter/104-quad-8.c
+++ b/drivers/iio/counter/104-quad-8.c
@@ -64,9 +64,6 @@ static int quad8_read_raw(struct iio_dev *indio_dev,
{
struct quad8_iio *const priv = iio_priv(indio_dev);
const int base_offset = priv->base + 2 * chan->channel;
- unsigned int flags;
- unsigned int borrow;
- unsigned int carry;
int i;

switch (mask) {
@@ -76,12 +73,7 @@ static int quad8_read_raw(struct iio_dev *indio_dev,
return IIO_VAL_INT;
}

- flags = inb(base_offset + 1);
- borrow = flags & BIT(0);
- carry = !!(flags & BIT(1));
-
- /* Borrow XOR Carry effectively doubles count range */
- *val = (borrow ^ carry) << 24;
+ *val = 0;

/* Reset Byte Pointer; transfer Counter to Output Latch */
outb(0x11, base_offset + 1);

base-commit: df06e352f27a9f368ec6a3b077881c35d933e32c
--
2.40.0


2023-04-23 23:26:50

by William Breathitt Gray

[permalink] [raw]
Subject: [PATCH 4.19 v4 2/5] iio: counter: 104-quad-8: Fix race condition between FLAG and CNTR reads

commit 4aa3b75c74603c3374877d5fd18ad9cc3a9a62ed upstream.

The Counter (CNTR) register is 24 bits wide, but we can have an
effective 25-bit count value by setting bit 24 to the XOR of the Borrow
flag and Carry flag. The flags can be read from the FLAG register, but a
race condition exists: the Borrow flag and Carry flag are instantaneous
and could change by the time the count value is read from the CNTR
register.

Since the race condition could result in an incorrect 25-bit count
value, remove support for 25-bit count values from this driver.

Fixes: 28e5d3bb0325 ("iio: 104-quad-8: Add IIO support for the ACCES 104-QUAD-8")
Cc: <[email protected]> # 4.19.x
Signed-off-by: William Breathitt Gray <[email protected]>
---
drivers/iio/counter/104-quad-8.c | 14 +-------------
1 file changed, 1 insertion(+), 13 deletions(-)

diff --git a/drivers/iio/counter/104-quad-8.c b/drivers/iio/counter/104-quad-8.c
index 92be8d0f77..92e68cada8 100644
--- a/drivers/iio/counter/104-quad-8.c
+++ b/drivers/iio/counter/104-quad-8.c
@@ -61,10 +61,6 @@ struct quad8_iio {

#define QUAD8_REG_CHAN_OP 0x11
#define QUAD8_REG_INDEX_INPUT_LEVELS 0x16
-/* Borrow Toggle flip-flop */
-#define QUAD8_FLAG_BT BIT(0)
-/* Carry Toggle flip-flop */
-#define QUAD8_FLAG_CT BIT(1)
/* Error flag */
#define QUAD8_FLAG_E BIT(4)
/* Up/Down flag */
@@ -97,9 +93,6 @@ static int quad8_read_raw(struct iio_dev *indio_dev,
{
struct quad8_iio *const priv = iio_priv(indio_dev);
const int base_offset = priv->base + 2 * chan->channel;
- unsigned int flags;
- unsigned int borrow;
- unsigned int carry;
int i;

switch (mask) {
@@ -110,12 +103,7 @@ static int quad8_read_raw(struct iio_dev *indio_dev,
return IIO_VAL_INT;
}

- flags = inb(base_offset + 1);
- borrow = flags & QUAD8_FLAG_BT;
- carry = !!(flags & QUAD8_FLAG_CT);
-
- /* Borrow XOR Carry effectively doubles count range */
- *val = (borrow ^ carry) << 24;
+ *val = 0;

/* Reset Byte Pointer; transfer Counter to Output Latch */
outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP | QUAD8_RLD_CNTR_OUT,

base-commit: a5b79a58cfc02977cd4d5c1e20454cd98e88f749
--
2.40.0

2023-04-23 23:32:21

by William Breathitt Gray

[permalink] [raw]
Subject: [PATCH 5.10 v4 4/5] counter: 104-quad-8: Fix race condition between FLAG and CNTR reads

commit 4aa3b75c74603c3374877d5fd18ad9cc3a9a62ed upstream.

The Counter (CNTR) register is 24 bits wide, but we can have an
effective 25-bit count value by setting bit 24 to the XOR of the Borrow
flag and Carry flag. The flags can be read from the FLAG register, but a
race condition exists: the Borrow flag and Carry flag are instantaneous
and could change by the time the count value is read from the CNTR
register.

Since the race condition could result in an incorrect 25-bit count
value, remove support for 25-bit count values from this driver.

Fixes: 28e5d3bb0325 ("iio: 104-quad-8: Add IIO support for the ACCES 104-QUAD-8")
Cc: <[email protected]> # 5.10.x
Signed-off-by: William Breathitt Gray <[email protected]>
---
drivers/counter/104-quad-8.c | 18 +++---------------
1 file changed, 3 insertions(+), 15 deletions(-)

diff --git a/drivers/counter/104-quad-8.c b/drivers/counter/104-quad-8.c
index 21bb2bb767..1b4fdee9d9 100644
--- a/drivers/counter/104-quad-8.c
+++ b/drivers/counter/104-quad-8.c
@@ -62,10 +62,6 @@ struct quad8_iio {
#define QUAD8_REG_CHAN_OP 0x11
#define QUAD8_REG_INDEX_INPUT_LEVELS 0x16
#define QUAD8_DIFF_ENCODER_CABLE_STATUS 0x17
-/* Borrow Toggle flip-flop */
-#define QUAD8_FLAG_BT BIT(0)
-/* Carry Toggle flip-flop */
-#define QUAD8_FLAG_CT BIT(1)
/* Error flag */
#define QUAD8_FLAG_E BIT(4)
/* Up/Down flag */
@@ -643,17 +639,9 @@ static int quad8_count_read(struct counter_device *counter,
{
struct quad8_iio *const priv = counter->priv;
const int base_offset = priv->base + 2 * count->id;
- unsigned int flags;
- unsigned int borrow;
- unsigned int carry;
int i;

- flags = inb(base_offset + 1);
- borrow = flags & QUAD8_FLAG_BT;
- carry = !!(flags & QUAD8_FLAG_CT);
-
- /* Borrow XOR Carry effectively doubles count range */
- *val = (unsigned long)(borrow ^ carry) << 24;
+ *val = 0;

mutex_lock(&priv->lock);

@@ -1198,8 +1186,8 @@ static ssize_t quad8_count_ceiling_read(struct counter_device *counter,

mutex_unlock(&priv->lock);

- /* By default 0x1FFFFFF (25 bits unsigned) is maximum count */
- return sprintf(buf, "33554431\n");
+ /* By default 0xFFFFFF (24 bits unsigned) is maximum count */
+ return sprintf(buf, "16777215\n");
}

static ssize_t quad8_count_ceiling_write(struct counter_device *counter,

base-commit: 791a854ae5a5f5988f1291ae91168a149bd5ba57
--
2.40.0

2023-04-23 23:45:41

by William Breathitt Gray

[permalink] [raw]
Subject: [PATCH 5.15 v4 5/5] counter: 104-quad-8: Fix race condition between FLAG and CNTR reads

commit 4aa3b75c74603c3374877d5fd18ad9cc3a9a62ed upstream.

The Counter (CNTR) register is 24 bits wide, but we can have an
effective 25-bit count value by setting bit 24 to the XOR of the Borrow
flag and Carry flag. The flags can be read from the FLAG register, but a
race condition exists: the Borrow flag and Carry flag are instantaneous
and could change by the time the count value is read from the CNTR
register.

Since the race condition could result in an incorrect 25-bit count
value, remove support for 25-bit count values from this driver;
hard-coded maximum count values are replaced by a LS7267_CNTR_MAX define
for consistency and clarity.

Fixes: 28e5d3bb0325 ("iio: 104-quad-8: Add IIO support for the ACCES 104-QUAD-8")
Cc: <[email protected]> # 5.15.x
Signed-off-by: William Breathitt Gray <[email protected]>
---
drivers/counter/104-quad-8.c | 29 ++++++++---------------------
1 file changed, 8 insertions(+), 21 deletions(-)

diff --git a/drivers/counter/104-quad-8.c b/drivers/counter/104-quad-8.c
index e00cf156c6..ab2c49579b 100644
--- a/drivers/counter/104-quad-8.c
+++ b/drivers/counter/104-quad-8.c
@@ -61,10 +61,6 @@ struct quad8 {
#define QUAD8_REG_CHAN_OP 0x11
#define QUAD8_REG_INDEX_INPUT_LEVELS 0x16
#define QUAD8_DIFF_ENCODER_CABLE_STATUS 0x17
-/* Borrow Toggle flip-flop */
-#define QUAD8_FLAG_BT BIT(0)
-/* Carry Toggle flip-flop */
-#define QUAD8_FLAG_CT BIT(1)
/* Error flag */
#define QUAD8_FLAG_E BIT(4)
/* Up/Down flag */
@@ -97,6 +93,9 @@ struct quad8 {
#define QUAD8_CMR_QUADRATURE_X2 0x10
#define QUAD8_CMR_QUADRATURE_X4 0x18

+/* Each Counter is 24 bits wide */
+#define LS7267_CNTR_MAX GENMASK(23, 0)
+
static int quad8_signal_read(struct counter_device *counter,
struct counter_signal *signal,
enum counter_signal_level *level)
@@ -121,17 +120,9 @@ static int quad8_count_read(struct counter_device *counter,
{
struct quad8 *const priv = counter->priv;
const int base_offset = priv->base + 2 * count->id;
- unsigned int flags;
- unsigned int borrow;
- unsigned int carry;
int i;

- flags = inb(base_offset + 1);
- borrow = flags & QUAD8_FLAG_BT;
- carry = !!(flags & QUAD8_FLAG_CT);
-
- /* Borrow XOR Carry effectively doubles count range */
- *val = (unsigned long)(borrow ^ carry) << 24;
+ *val = 0;

mutex_lock(&priv->lock);

@@ -154,8 +145,7 @@ static int quad8_count_write(struct counter_device *counter,
const int base_offset = priv->base + 2 * count->id;
int i;

- /* Only 24-bit values are supported */
- if (val > 0xFFFFFF)
+ if (val > LS7267_CNTR_MAX)
return -ERANGE;

mutex_lock(&priv->lock);
@@ -627,8 +617,7 @@ static int quad8_count_preset_write(struct counter_device *counter,
{
struct quad8 *const priv = counter->priv;

- /* Only 24-bit values are supported */
- if (preset > 0xFFFFFF)
+ if (preset > LS7267_CNTR_MAX)
return -ERANGE;

mutex_lock(&priv->lock);
@@ -654,8 +643,7 @@ static int quad8_count_ceiling_read(struct counter_device *counter,
*ceiling = priv->preset[count->id];
break;
default:
- /* By default 0x1FFFFFF (25 bits unsigned) is maximum count */
- *ceiling = 0x1FFFFFF;
+ *ceiling = LS7267_CNTR_MAX;
break;
}

@@ -669,8 +657,7 @@ static int quad8_count_ceiling_write(struct counter_device *counter,
{
struct quad8 *const priv = counter->priv;

- /* Only 24-bit values are supported */
- if (ceiling > 0xFFFFFF)
+ if (ceiling > LS7267_CNTR_MAX)
return -ERANGE;

mutex_lock(&priv->lock);

base-commit: 3299fb36854fdc288bddc2c4d265f8a2e5105944
--
2.40.0

2023-04-24 00:34:51

by William Breathitt Gray

[permalink] [raw]
Subject: [PATCH 5.4 v4 3/5] counter: 104-quad-8: Fix race condition between FLAG and CNTR reads

commit 4aa3b75c74603c3374877d5fd18ad9cc3a9a62ed upstream.

The Counter (CNTR) register is 24 bits wide, but we can have an
effective 25-bit count value by setting bit 24 to the XOR of the Borrow
flag and Carry flag. The flags can be read from the FLAG register, but a
race condition exists: the Borrow flag and Carry flag are instantaneous
and could change by the time the count value is read from the CNTR
register.

Since the race condition could result in an incorrect 25-bit count
value, remove support for 25-bit count values from this driver.

Fixes: 28e5d3bb0325 ("iio: 104-quad-8: Add IIO support for the ACCES 104-QUAD-8")
Cc: <[email protected]> # 5.4.x
Signed-off-by: William Breathitt Gray <[email protected]>
---
drivers/counter/104-quad-8.c | 20 +++-----------------
1 file changed, 3 insertions(+), 17 deletions(-)

diff --git a/drivers/counter/104-quad-8.c b/drivers/counter/104-quad-8.c
index f261a57af1..48de69f58e 100644
--- a/drivers/counter/104-quad-8.c
+++ b/drivers/counter/104-quad-8.c
@@ -57,10 +57,6 @@ struct quad8_iio {

#define QUAD8_REG_CHAN_OP 0x11
#define QUAD8_REG_INDEX_INPUT_LEVELS 0x16
-/* Borrow Toggle flip-flop */
-#define QUAD8_FLAG_BT BIT(0)
-/* Carry Toggle flip-flop */
-#define QUAD8_FLAG_CT BIT(1)
/* Error flag */
#define QUAD8_FLAG_E BIT(4)
/* Up/Down flag */
@@ -639,19 +635,9 @@ static int quad8_count_read(struct counter_device *counter,
{
struct quad8_iio *const priv = counter->priv;
const int base_offset = priv->base + 2 * count->id;
- unsigned int flags;
- unsigned int borrow;
- unsigned int carry;
- unsigned long position;
+ unsigned long position = 0;
int i;

- flags = inb(base_offset + 1);
- borrow = flags & QUAD8_FLAG_BT;
- carry = !!(flags & QUAD8_FLAG_CT);
-
- /* Borrow XOR Carry effectively doubles count range */
- position = (unsigned long)(borrow ^ carry) << 24;
-
mutex_lock(&priv->lock);

/* Reset Byte Pointer; transfer Counter to Output Latch */
@@ -1204,8 +1190,8 @@ static ssize_t quad8_count_ceiling_read(struct counter_device *counter,

mutex_unlock(&priv->lock);

- /* By default 0x1FFFFFF (25 bits unsigned) is maximum count */
- return sprintf(buf, "33554431\n");
+ /* By default 0xFFFFFF (24 bits unsigned) is maximum count */
+ return sprintf(buf, "16777215\n");
}

static ssize_t quad8_count_ceiling_write(struct counter_device *counter,

base-commit: 58f42ed1cd31238745bddd943c4f5849dc83a2ac
--
2.40.0

2023-04-24 06:38:16

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH 5.10 v4 4/5] counter: 104-quad-8: Fix race condition between FLAG and CNTR reads

On Sun, Apr 23, 2023 at 07:20:46PM -0400, William Breathitt Gray wrote:
> commit 4aa3b75c74603c3374877d5fd18ad9cc3a9a62ed upstream.
>
> The Counter (CNTR) register is 24 bits wide, but we can have an
> effective 25-bit count value by setting bit 24 to the XOR of the Borrow
> flag and Carry flag. The flags can be read from the FLAG register, but a
> race condition exists: the Borrow flag and Carry flag are instantaneous
> and could change by the time the count value is read from the CNTR
> register.
>
> Since the race condition could result in an incorrect 25-bit count
> value, remove support for 25-bit count values from this driver.
>
> Fixes: 28e5d3bb0325 ("iio: 104-quad-8: Add IIO support for the ACCES 104-QUAD-8")
> Cc: <[email protected]> # 5.10.x
> Signed-off-by: William Breathitt Gray <[email protected]>
> ---
> drivers/counter/104-quad-8.c | 18 +++---------------
> 1 file changed, 3 insertions(+), 15 deletions(-)

This patch breaks the build:

CC [M] drivers/counter/104-quad-8.o
drivers/counter/104-quad-8.c: In function ‘quad8_read_raw’:
drivers/counter/104-quad-8.c:117:34: error: ‘QUAD8_FLAG_BT’ undeclared (first use in this function); did you mean ‘QUAD8_FLAG_E’?
117 | borrow = flags & QUAD8_FLAG_BT;
| ^~~~~~~~~~~~~
| QUAD8_FLAG_E
drivers/counter/104-quad-8.c:117:34: note: each undeclared identifier is reported only once for each function it appears in
drivers/counter/104-quad-8.c:118:36: error: ‘QUAD8_FLAG_CT’ undeclared (first use in this function); did you mean ‘QUAD8_FLAG_E’?
118 | carry = !!(flags & QUAD8_FLAG_CT);
| ^~~~~~~~~~~~~
| QUAD8_FLAG_E


2023-04-24 06:39:35

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH 5.4 v4 3/5] counter: 104-quad-8: Fix race condition between FLAG and CNTR reads

On Sun, Apr 23, 2023 at 07:20:45PM -0400, William Breathitt Gray wrote:
> commit 4aa3b75c74603c3374877d5fd18ad9cc3a9a62ed upstream.
>
> The Counter (CNTR) register is 24 bits wide, but we can have an
> effective 25-bit count value by setting bit 24 to the XOR of the Borrow
> flag and Carry flag. The flags can be read from the FLAG register, but a
> race condition exists: the Borrow flag and Carry flag are instantaneous
> and could change by the time the count value is read from the CNTR
> register.
>
> Since the race condition could result in an incorrect 25-bit count
> value, remove support for 25-bit count values from this driver.
>
> Fixes: 28e5d3bb0325 ("iio: 104-quad-8: Add IIO support for the ACCES 104-QUAD-8")
> Cc: <[email protected]> # 5.4.x
> Signed-off-by: William Breathitt Gray <[email protected]>
> ---
> drivers/counter/104-quad-8.c | 20 +++-----------------
> 1 file changed, 3 insertions(+), 17 deletions(-)
>

This patch also breaks the build, same error as on 5.10.y.

Can you send new 5.4.y and 5.10.y versions? All the other ones were
fine.

thanks,

greg k-h

2023-04-24 15:53:07

by William Breathitt Gray

[permalink] [raw]
Subject: Re: [PATCH 5.10 v4 4/5] counter: 104-quad-8: Fix race condition between FLAG and CNTR reads

On Mon, Apr 24, 2023 at 08:33:18AM +0200, Greg Kroah-Hartman wrote:
> On Sun, Apr 23, 2023 at 07:20:46PM -0400, William Breathitt Gray wrote:
> > commit 4aa3b75c74603c3374877d5fd18ad9cc3a9a62ed upstream.
> >
> > The Counter (CNTR) register is 24 bits wide, but we can have an
> > effective 25-bit count value by setting bit 24 to the XOR of the Borrow
> > flag and Carry flag. The flags can be read from the FLAG register, but a
> > race condition exists: the Borrow flag and Carry flag are instantaneous
> > and could change by the time the count value is read from the CNTR
> > register.
> >
> > Since the race condition could result in an incorrect 25-bit count
> > value, remove support for 25-bit count values from this driver.
> >
> > Fixes: 28e5d3bb0325 ("iio: 104-quad-8: Add IIO support for the ACCES 104-QUAD-8")
> > Cc: <[email protected]> # 5.10.x
> > Signed-off-by: William Breathitt Gray <[email protected]>
> > ---
> > drivers/counter/104-quad-8.c | 18 +++---------------
> > 1 file changed, 3 insertions(+), 15 deletions(-)
>
> This patch breaks the build:
>
> CC [M] drivers/counter/104-quad-8.o
> drivers/counter/104-quad-8.c: In function ‘quad8_read_raw’:
> drivers/counter/104-quad-8.c:117:34: error: ‘QUAD8_FLAG_BT’ undeclared (first use in this function); did you mean ‘QUAD8_FLAG_E’?
> 117 | borrow = flags & QUAD8_FLAG_BT;
> | ^~~~~~~~~~~~~
> | QUAD8_FLAG_E
> drivers/counter/104-quad-8.c:117:34: note: each undeclared identifier is reported only once for each function it appears in
> drivers/counter/104-quad-8.c:118:36: error: ‘QUAD8_FLAG_CT’ undeclared (first use in this function); did you mean ‘QUAD8_FLAG_E’?
> 118 | carry = !!(flags & QUAD8_FLAG_CT);
> | ^~~~~~~~~~~~~
> | QUAD8_FLAG_E
>
>

Ah, the 5.4 and 5.10 patches are missing the IIO changes from the 4.19
patch. I'll adjust and resubmit these two patches.

William Breathitt Gray


Attachments:
(No filename) (2.10 kB)
signature.asc (235.00 B)
Download all attachments