Skip to contentSkip to conversation

IIO: Alignment fixes part 3 - __aligned(8) used to ensure alignment

PATCH Superseded Jonathan Cameron 6 participants 23 messages May 1, 2021 – May 27, 2021

Subsystems (7)
lore
Share & tools
lore

Jonathan Cameron

author maintainer
#0 lore raw

cover IIO: Alignment fixes part 3 - __aligned(8) used to ensure alignment

From: Jonathan Cameron <[email protected]>

I finally got around to do a manual audit of all the calls to iio_push_to_buffers_with_timestamp() which has the somewhat odd requirements of:
1. 8 byte alignment of the provided buffer.
2. space for an 8 byte naturally aligned timestamp to be inserted at the end.

Unfortunately there were rather a lot of these left, but time to bite the bullet and clean them up.

As discussed previous in
https://lore.kernel.org/linux-iio/[email protected]/
it is not easy to fix the alignment issue without requiring a bounce buffer (see part 4 of the alignment fixes for a proposal for that where it is absolutely necessary).

Part 3 contains the cases where the struct approach in part 2 did not seem appropriate. Normally there are two possible reasons for this.
1. Would have required an additional memset operation to avoid any possibility of leaking kernel data.
2. The location of the timestamp may depend on the channels enabled. This normally happens when the max sizeof channels is greater than 8 bytes.

Once all cases are fixes, I'll take a look at hardening against any accidental reintroductions. Note that on many platforms and usecases the bug in question will never occur.

Cc Eugen Hristev <[email protected]>
Cc Andreas Klinger <[email protected]>
Cc Matt Ranostay <[email protected]>
Cc Gwendal Grignou <[email protected]>
Cc Song Qiang <[email protected]>
Cc Mathieu Othacehe <[email protected]>
Cc Parthiban Nallathambi <[email protected]>

Jonathan Cameron (11):
iio: adc: at91-sama5d2: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: adc: hx711: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: adc: mxs-lradc: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: adc: ti-ads8688: Fix alignment of buffer in
iio_push_to_buffers_with_timestamp()
iio: chemical: atlas: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: cros_ec_sensors: Fix alignment of buffer in
iio_push_to_buffers_with_timestamp()
iio: potentiostat: lmp91000: Fix alignment of buffer in
iio_push_to_buffers_with_timestamp()
iio: magn: rm3100: Fix alignment of buffer in
iio_push_to_buffers_with_timestamp()
iio: light: vcnl4000: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: light: vcnl4035: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: prox: isl29501: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()

 drivers/iio/adc/at91-sama5d2_adc.c              | 3 ++-
 drivers/iio/adc/hx711.c                         | 4 ++--
 drivers/iio/adc/mxs-lradc-adc.c                 | 3 ++-
 drivers/iio/adc/ti-ads8688.c                    | 3 ++-
 drivers/iio/chemical/atlas-sensor.c             | 4 ++--
 drivers/iio/light/vcnl4000.c                    | 2 +-
 drivers/iio/light/vcnl4035.c                    | 3 ++-
 drivers/iio/magnetometer/rm3100-core.c          | 3 ++-
 drivers/iio/potentiostat/lmp91000.c             | 4 ++--
 drivers/iio/proximity/isl29501.c                | 2 +-
 include/linux/iio/common/cros_ec_sensors_core.h | 2 +-
 11 files changed, 19 insertions(+), 14 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH 1/11 iio: adc: at91-sama5d2: Fix buffer alignment in iio_push_to_buffers_with_timestamp()

From: Jonathan Cameron <[email protected]>

To make code more readable, use a structure to express the channel layout and ensure the timestamp is 8 byte aligned.

Found during an audit of all calls of this function.

Fixes 5e1a1da0f8c9 ("iio: adc: at91-sama5d2_adc: add hw trigger and buffer support")
Cc Eugen Hristev <[email protected]>
---
 drivers/iio/adc/at91-sama5d2_adc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/adc/at91-sama5d2_adc.c +2 -1
index a7826f097b95..d356b515df09 100644
@@ -403,7 +403,8 @@ struct at91_adc_state {
 	struct at91_adc_dma		dma_st;
 	struct at91_adc_touch		touch_st;
 	struct iio_dev			*indio_dev;
-	u16				buffer[AT91_BUFFER_MAX_HWORDS];
+	/* Ensure naturally aligned timestamp */
+	u16				buffer[AT91_BUFFER_MAX_HWORDS] __aligned(8);
 	/*
 	 * lock to prevent concurrent 'single conversion' requests through
 	 * sysfs.
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH 01/11] iio: adc: at91-sama5d2: Fix buffer alignment in iio_push_to_buffers_with_timestamp()

Quoted 19 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

To make code more readable, use a structure to express the channel layout and ensure the timestamp is 8 byte aligned.

Found during an audit of all calls of this function.

Fixes 5e1a1da0f8c9 ("iio: adc: at91-sama5d2_adc: add hw trigger and buffer support")
Cc Eugen Hristev <[email protected]>
---
  drivers/iio/adc/at91-sama5d2_adc.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/adc/at91-sama5d2_adc.c +0 -0
index a7826f097b95..d356b515df09 100644

drivers/iio/adc/at91-sama5d2_adc.c struct at91_adc_state { · line 407

Quoted 6 lines
drivers/iio/adc/at91-sama5d2_adc.c +2 -1
@@ -403,7 +403,8 @@ struct at91_adc_state {
         struct at91_adc_dma             dma_st;
         struct at91_adc_touch           touch_st;
         struct iio_dev                  *indio_dev;
-       u16                             buffer[AT91_BUFFER_MAX_HWORDS];
+       /* Ensure naturally aligned timestamp */
+       u16                             buffer[AT91_BUFFER_MAX_HWORDS] __aligned(8);

Hello Jonathan,

I am preparing to change this buffer to a dynamically allocated buffer... because we want to support several versions of the ADC with this driver, having an arbitrary number of channels..

You think it's possible to have this alignment when I move to a devm_kzalloc call ?

Thanks,
Eugen

Quoted 6 lines
drivers/iio/adc/at91-sama5d2_adc.c +0 -1
         /*
          * lock to prevent concurrent 'single conversion' requests through
          * sysfs.
--

2.31.1

#3 lore raw

Re: [PATCH 01/11] iio: adc: at91-sama5d2: Fix buffer alignment in iio_push_to_buffers_with_timestamp()

Quoted 29 lines · On Wed, 26 May 2021 17:23:11 +0000 <[email protected]> wrote:
Quoted 19 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

To make code more readable, use a structure to express the channel layout and ensure the timestamp is 8 byte aligned.

Found during an audit of all calls of this function.

Fixes 5e1a1da0f8c9 ("iio: adc: at91-sama5d2_adc: add hw trigger and buffer support")
Cc Eugen Hristev <[email protected]>
---
  drivers/iio/adc/at91-sama5d2_adc.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/adc/at91-sama5d2_adc.c +0 -0
index a7826f097b95..d356b515df09 100644

drivers/iio/adc/at91-sama5d2_adc.c struct at91_adc_state { · line 407

Quoted 6 lines
drivers/iio/adc/at91-sama5d2_adc.c +2 -1
@@ -403,7 +403,8 @@ struct at91_adc_state {
         struct at91_adc_dma             dma_st;
         struct at91_adc_touch           touch_st;
         struct iio_dev                  *indio_dev;
-       u16                             buffer[AT91_BUFFER_MAX_HWORDS];
+       /* Ensure naturally aligned timestamp */
+       u16                             buffer[AT91_BUFFER_MAX_HWORDS] __aligned(8);  

Hello Jonathan,

Quoted 6 lines

I am preparing to change this buffer to a dynamically allocated buffer... because we want to support several versions of the ADC with this driver, having an arbitrary number of channels..

You think it's possible to have this alignment when I move to a devm_kzalloc call ?

Hmm. You should be fine, but looking through the docs, guaranteed alignment for devm_kmalloc is unsigned long long. Sounds worryingly vague, but c99 (which kernel uses) guarantees at least 64 bits so that's fine.

However, unless your maximum size is very large, I'd be cynical and just leave it where it is. Chances are it won't actually make any difference to the real amount of memory allocated for at91_adc_state and it'll make your code simpler.

No one minds a buffer being too big :)

Jonathan

Quoted 11 lines

Thanks,
Eugen

Quoted 6 lines
drivers/iio/adc/at91-sama5d2_adc.c +0 -1
         /*
          * lock to prevent concurrent 'single conversion' requests through
          * sysfs.
--

2.31.1

author maintainer
#4 lore raw

PATCH 2/11 iio: adc: hx711: Fix buffer alignment in iio_push_to_buffers_with_timestamp()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

To make code more readable, use a structure to express the channel layout and ensure the timestamp is 8 byte aligned.

Found during an audit of all calls of this function.

Fixes d3bf60450d47 ("iio: hx711: add triggered buffer support")
Cc Andreas Klinger <[email protected]>
---
 drivers/iio/adc/hx711.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/adc/hx711.c +2 -2
index 6a173531d355..f7ee856a6b8b 100644
@@ -86,9 +86,9 @@ struct hx711_data {
 	struct mutex		lock;
 	/*
 	 * triggered buffer
-	 * 2x32-bit channel + 64-bit timestamp
+	 * 2x32-bit channel + 64-bit naturally aligned timestamp
 	 */
-	u32			buffer[4];
+	u32			buffer[4] __aligned(8);
 	/*
 	 * delay after a rising edge on SCK until the data is ready DOUT
 	 * this is dependent on the hx711 where the datasheet tells a
Signature · 1 line
2.31.1
author maintainer
#5 lore raw

PATCH 3/11 iio: adc: mxs-lradc: Fix buffer alignment in iio_push_to_buffers_with_timestamp()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

To make code more readable, use a structure to express the channel layout and ensure the timestamp is 8 byte aligned.
Add a comment on why the buffer is the size it is as not immediately obvious.

Found during an audit of all calls of this function.

Fixes 6dd112b9f85e ("iio: adc: mxs-lradc: Add support for ADC driver")
Cc Andreas Klinger <[email protected]>
---
 drivers/iio/adc/mxs-lradc-adc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/adc/mxs-lradc-adc.c +2 -1
index 30e29f44ebd2..c480cb489c1a 100644
@@ -115,7 +115,8 @@ struct mxs_lradc_adc {
 	struct device		*dev;
 
 	void __iomem		*base;
-	u32			buffer[10];
+	/* Maximum of 8 channels + 8 byte ts */
+	u32			buffer[10] __aligned(8);
 	struct iio_trigger	*trig;
 	struct completion	completion;
 	spinlock_t		lock;
Signature · 1 line
2.31.1
author maintainer
#6 lore raw

PATCH 4/11 iio: adc: ti-ads8688: Fix alignment of buffer in iio_push_to_buffers_with_timestamp()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Add __aligned(8) to ensure the buffer passed to iio_push_to_buffers_with_timestamp() is suitable for the naturally aligned timestamp that will be inserted.

Fixes f214ff521fb1 ("iio: ti-ads8688: Update buffer allocation for timestamps")
---
 drivers/iio/adc/ti-ads8688.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/adc/ti-ads8688.c +2 -1
index 16bcb37eebb7..79c803537dc4 100644
@@ -383,7 +383,8 @@ static irqreturn_t ads8688_trigger_handler(int irq, void *p)
 {
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
-	u16 buffer[ADS8688_MAX_CHANNELS + sizeof(s64)/sizeof(u16)];
+	/* Ensure naturally aligned timestamp */
+	u16 buffer[ADS8688_MAX_CHANNELS + sizeof(s64)/sizeof(u16)] __aligned(8);
 	int i, j = 0;
 
 	for (i = 0; i < indio_dev->masklength; i++) {
Signature · 1 line
2.31.1
author maintainer
#7 lore raw

PATCH 5/11 iio: chemical: atlas: Fix buffer alignment in iio_push_to_buffers_with_timestamp()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Variable location for the timestamp, so just use __aligned(8) to ensure it is always possible to naturally align it.

Found during an audit of all calls of uses of
iio_push_to_buffers_with_timestamp()

Fixes tag is not accurate, but it will need manual backporting beyond that point if anyone cares.

Fixes 0d15190f53b4 ("iio: chemical: atlas-ph-sensor: rename atlas-ph-sensor to atlas-sensor")
Cc Matt Ranostay <[email protected]>
---
 drivers/iio/chemical/atlas-sensor.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/chemical/atlas-sensor.c +2 -2
index 56ba6c82b501..6795722c68b2 100644
@@ -91,8 +91,8 @@ struct atlas_data {
 	struct regmap *regmap;
 	struct irq_work work;
 	unsigned int interrupt_enabled;
-
-	__be32 buffer[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */
+	/* 96-bit data + 32-bit pad + 64-bit timestamp */
+	__be32 buffer[6] __aligned(8);
 };
 
 static const struct regmap_config atlas_regmap_config = {
Signature · 1 line
2.31.1
Acked-by on v1 5/11
#8 lore raw

Re: [PATCH 05/11] iio: chemical: atlas: Fix buffer alignment in iio_push_to_buffers_with_timestamp()

Quoted 6 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Variable location for the timestamp, so just use __aligned(8) to ensure it is always possible to naturally align it.

Quoted 8 lines

Found during an audit of all calls of uses of
iio_push_to_buffers_with_timestamp()

Fixes tag is not accurate, but it will need manual backporting beyond that point if anyone cares.

Fixes 0d15190f53b4 ("iio: chemical: atlas-ph-sensor: rename atlas-ph-sensor to atlas-sensor")
Quoted 23 lines
Cc Matt Ranostay <[email protected]>
---
 drivers/iio/chemical/atlas-sensor.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/chemical/atlas-sensor.c +2 -3
index 56ba6c82b501..6795722c68b2 100644
@@ -91,8 +91,8 @@ struct atlas_data {
        struct regmap *regmap;
        struct irq_work work;
        unsigned int interrupt_enabled;
-
-       __be32 buffer[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */
+       /* 96-bit data + 32-bit pad + 64-bit timestamp */
+       __be32 buffer[6] __aligned(8);
 };
 
 static const struct regmap_config atlas_regmap_config = {
--

2.31.1

author maintainer
#9 lore raw

Re: [PATCH 05/11] iio: chemical: atlas: Fix buffer alignment in iio_push_to_buffers_with_timestamp()

Quoted 10 lines · On Wed, 5 May 2021 23:44:54 -0700 Matt Ranostay <[email protected]>…
Quoted 9 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Variable location for the timestamp, so just use __aligned(8) to ensure it is always possible to naturally align it.

Found during an audit of all calls of uses of
iio_push_to_buffers_with_timestamp()

Quoted 7 lines
Quoted 5 lines

Fixes tag is not accurate, but it will need manual backporting beyond that point if anyone cares.

Fixes 0d15190f53b4 ("iio: chemical: atlas-ph-sensor: rename atlas-ph-sensor to atlas-sensor")

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to have a go at breaking things.

Thanks,

Jonathan

Quoted 24 lines
Quoted 24 lines
Cc Matt Ranostay <[email protected]>
---
 drivers/iio/chemical/atlas-sensor.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/chemical/atlas-sensor.c +2 -3
index 56ba6c82b501..6795722c68b2 100644
@@ -91,8 +91,8 @@ struct atlas_data {
        struct regmap *regmap;
        struct irq_work work;
        unsigned int interrupt_enabled;
-
-       __be32 buffer[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */
+       /* 96-bit data + 32-bit pad + 64-bit timestamp */
+       __be32 buffer[6] __aligned(8);
 };
 
 static const struct regmap_config atlas_regmap_config = {
--

2.31.1

author maintainer
#10 lore raw

PATCH 6/11 iio: cros_ec_sensors: Fix alignment of buffer in iio_push_to_buffers_with_timestamp()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

The samples buffer is passed to iio_push_to_buffers_with_timestamp() which requires a buffer aligned to 8 bytes as it is assumed that the timestamp will be naturally aligned if present.

Fixes tag is inaccurate but prior to that likely manual backporting needed.

Fixes 5a0b8cb46624c ("iio: cros_ec: Move cros_ec_sensors_core.h in /include")
Cc Gwendal Grignou <[email protected]>
---
 include/linux/iio/common/cros_ec_sensors_core.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/iio/common/cros_ec_sensors_core.h +1 -1
index 7ce8a8adad58..c582e1a14232 100644
@@ -77,7 +77,7 @@ struct cros_ec_sensors_core_state {
 		u16 scale;
 	} calib[CROS_EC_SENSOR_MAX_AXIS];
 	s8 sign[CROS_EC_SENSOR_MAX_AXIS];
-	u8 samples[CROS_EC_SAMPLE_SIZE];
+	u8 samples[CROS_EC_SAMPLE_SIZE] __aligned(8);
 
 	int (*read_ec_sensors_data)(struct iio_dev *indio_dev,
 				    unsigned long scan_mask, s16 *data);
Signature · 1 line
2.31.1
Reviewed-by on v1 6/11
#11 lore raw

Re: [PATCH 06/11] iio: cros_ec_sensors: Fix alignment of buffer in iio_push_to_buffers_with_timestamp()

Fixes tag is correct up to kernel stable 4.18.
Before, the include file to fix is drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.h:
commit 974e6f02e27 ("iio: cros_ec_sensors_core: Add common functions for the ChromeOS EC Sensor Hub.") present since kernel stable 4.10.

Quoted 32 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

The samples buffer is passed to iio_push_to_buffers_with_timestamp() which requires a buffer aligned to 8 bytes as it is assumed that the timestamp will be naturally aligned if present.

Fixes tag is inaccurate but prior to that likely manual backporting needed.

Fixes 5a0b8cb46624c ("iio: cros_ec: Move cros_ec_sensors_core.h in /include")
Cc Gwendal Grignou <[email protected]>
---
 include/linux/iio/common/cros_ec_sensors_core.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/iio/common/cros_ec_sensors_core.h +1 -2
index 7ce8a8adad58..c582e1a14232 100644
@@ -77,7 +77,7 @@ struct cros_ec_sensors_core_state {
                u16 scale;
        } calib[CROS_EC_SENSOR_MAX_AXIS];
        s8 sign[CROS_EC_SENSOR_MAX_AXIS];
-       u8 samples[CROS_EC_SAMPLE_SIZE];
+       u8 samples[CROS_EC_SAMPLE_SIZE] __aligned(8);
 
        int (*read_ec_sensors_data)(struct iio_dev *indio_dev,
                                    unsigned long scan_mask, s16 *data);
--

2.31.1

author maintainer
#12 lore raw

Re: [PATCH 06/11] iio: cros_ec_sensors: Fix alignment of buffer in iio_push_to_buffers_with_timestamp()

Quoted 6 lines · On Sat, 1 May 2021 16:58:21 -0700 Gwendal Grignou <[email protected]> wrote:

Fixes tag is correct up to kernel stable 4.18.
Before, the include file to fix is drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.h:
commit 974e6f02e27 ("iio: cros_ec_sensors_core: Add common functions for the ChromeOS EC Sensor Hub.") present since kernel stable 4.10.

Applied with this extra info to the togreg branch of iio.git and pushed out as testing for the autobuilders to poke at it.

This series (in it's complete form) is large enough that I'm not comfortable rushing it in. The bug is also rarely seen in practice so this can wait for the next merge window.

Thanks,

Jonathan

Quoted 35 lines
Quoted 32 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

The samples buffer is passed to iio_push_to_buffers_with_timestamp() which requires a buffer aligned to 8 bytes as it is assumed that the timestamp will be naturally aligned if present.

Fixes tag is inaccurate but prior to that likely manual backporting needed.

Fixes 5a0b8cb46624c ("iio: cros_ec: Move cros_ec_sensors_core.h in /include")
Cc Gwendal Grignou <[email protected]>
---
 include/linux/iio/common/cros_ec_sensors_core.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/iio/common/cros_ec_sensors_core.h +1 -2
index 7ce8a8adad58..c582e1a14232 100644
@@ -77,7 +77,7 @@ struct cros_ec_sensors_core_state {
                u16 scale;
        } calib[CROS_EC_SENSOR_MAX_AXIS];
        s8 sign[CROS_EC_SENSOR_MAX_AXIS];
-       u8 samples[CROS_EC_SAMPLE_SIZE];
+       u8 samples[CROS_EC_SAMPLE_SIZE] __aligned(8);
 
        int (*read_ec_sensors_data)(struct iio_dev *indio_dev,
                                    unsigned long scan_mask, s16 *data);
--

2.31.1

author maintainer
#13 lore raw

PATCH 7/11 iio: potentiostat: lmp91000: Fix alignment of buffer in iio_push_to_buffers_with_timestamp()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Add __aligned(8) to ensure the buffer passed to iio_push_to_buffers_with_timestamp() is suitable for the naturally aligned timestamp that will be inserted.

Here structure is not used, because this buffer is also used elsewhere in the driver.

Fixes 67e17300dc1d ("iio: potentiostat: add LMP91000 support")
Cc Matt Ranostay <[email protected]>
---
 drivers/iio/potentiostat/lmp91000.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/potentiostat/lmp91000.c +2 -2
index 8a9c576616ee..ff39ba975da7 100644
@@ -71,8 +71,8 @@ struct lmp91000_data {
 
 	struct completion completion;
 	u8 chan_select;
-
-	u32 buffer[4]; /* 64-bit data + 64-bit timestamp */
+	/* 64-bit data + 64-bit naturally aligned timestamp */
+	u32 buffer[4] __aligned(8);
 };
 
 static const struct iio_chan_spec lmp91000_channels[] = {
Signature · 1 line
2.31.1
Acked-by on v1 7/11
#14 lore raw

Re: [PATCH 07/11] iio: potentiostat: lmp91000: Fix alignment of buffer in iio_push_to_buffers_with_timestamp()

Quoted 7 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Add __aligned(8) to ensure the buffer passed to iio_push_to_buffers_with_timestamp() is suitable for the naturally aligned timestamp that will be inserted.

Quoted 5 lines

Here structure is not used, because this buffer is also used elsewhere in the driver.

Fixes 67e17300dc1d ("iio: potentiostat: add LMP91000 support")
Quoted 23 lines
Cc Matt Ranostay <[email protected]>
---
 drivers/iio/potentiostat/lmp91000.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/potentiostat/lmp91000.c +2 -3
index 8a9c576616ee..ff39ba975da7 100644
@@ -71,8 +71,8 @@ struct lmp91000_data {
 
        struct completion completion;
        u8 chan_select;
-
-       u32 buffer[4]; /* 64-bit data + 64-bit timestamp */
+       /* 64-bit data + 64-bit naturally aligned timestamp */
+       u32 buffer[4] __aligned(8);
 };
 
 static const struct iio_chan_spec lmp91000_channels[] = {
--

2.31.1

author maintainer
#15 lore raw

Re: [PATCH 07/11] iio: potentiostat: lmp91000: Fix alignment of buffer in iio_push_to_buffers_with_timestamp()

Quoted 8 lines · On Wed, 5 May 2021 23:44:23 -0700 Matt Ranostay <[email protected]>…
Quoted 7 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Add __aligned(8) to ensure the buffer passed to iio_push_to_buffers_with_timestamp() is suitable for the naturally aligned timestamp that will be inserted.

Quoted 7 lines
Quoted 5 lines

Here structure is not used, because this buffer is also used elsewhere in the driver.

Fixes 67e17300dc1d ("iio: potentiostat: add LMP91000 support")

Thanks Matt.

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to see if they can break it.

Jonathan

Quoted 24 lines
Quoted 24 lines
Cc Matt Ranostay <[email protected]>
---
 drivers/iio/potentiostat/lmp91000.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/potentiostat/lmp91000.c +2 -3
index 8a9c576616ee..ff39ba975da7 100644
@@ -71,8 +71,8 @@ struct lmp91000_data {
 
        struct completion completion;
        u8 chan_select;
-
-       u32 buffer[4]; /* 64-bit data + 64-bit timestamp */
+       /* 64-bit data + 64-bit naturally aligned timestamp */
+       u32 buffer[4] __aligned(8);
 };
 
 static const struct iio_chan_spec lmp91000_channels[] = {
--

2.31.1

author maintainer
#16 lore raw

PATCH 8/11 iio: magn: rm3100: Fix alignment of buffer in iio_push_to_buffers_with_timestamp()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Add __aligned(8) to ensure the buffer passed to iio_push_to_buffers_with_timestamp() is suitable for the naturally aligned timestamp that will be inserted.

Here an explicit structure is not used, because this buffer is used in a non-trivial way for data repacking.

Fixes 121354b2eceb ("iio: magnetometer: Add driver support for PNI RM3100")
Cc Song Qiang <[email protected]>
---
 drivers/iio/magnetometer/rm3100-core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/magnetometer/rm3100-core.c +2 -1
index dd811da9cb6d..934da20781bb 100644
@@ -78,7 +78,8 @@ struct rm3100_data {
 	bool use_interrupt;
 	int conversion_time;
 	int scale;
-	u8 buffer[RM3100_SCAN_BYTES];
+	/* Ensure naturally aligned timestamp */
+	u8 buffer[RM3100_SCAN_BYTES] __aligned(8);
 	struct iio_trigger *drdy_trig;
 
 	/*
Signature · 1 line
2.31.1
author maintainer
#17 lore raw

PATCH 9/11 iio: light: vcnl4000: Fix buffer alignment in iio_push_to_buffers_with_timestamp()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Add __aligned(8) to ensure the buffer passed to iio_push_to_buffers_with_timestamp() is suitable for the naturally aligned timestamp that will be inserted.

Here an explicit structure is not used, because the holes would necessitate the addition of an explict memset(), to avoid a kernel data leak, making for a less minimal fix.

Found during an audit of all callers of iio_push_to_buffers_with_timestamp()

Fixes 8fe78d5261e7 ("iio: vcnl4000: Add buffer support for VCNL4010/20.")
Cc Mathieu Othacehe <[email protected]>
---
 drivers/iio/light/vcnl4000.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/light/vcnl4000.c +1 -1
index 2f7916f95689..3b5e27053ef2 100644
@@ -910,7 +910,7 @@ static irqreturn_t vcnl4010_trigger_handler(int irq, void *p)
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct vcnl4000_data *data = iio_priv(indio_dev);
 	const unsigned long *active_scan_mask = indio_dev->active_scan_mask;
-	u16 buffer[8] = {0}; /* 1x16-bit + ts */
+	u16 buffer[8] __aligned(8) = {0}; /* 1x16-bit + naturally aligned ts */
 	bool data_read = false;
 	unsigned long isr;
 	int val = 0;
Signature · 1 line
2.31.1
author maintainer
#18 lore raw

PATCH 10/11 iio: light: vcnl4035: Fix buffer alignment in iio_push_to_buffers_with_timestamp()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Add __aligned(8) to ensure the buffer passed to iio_push_to_buffers_with_timestamp() is suitable for the naturally aligned timestamp that will be inserted.

Here an explicit structure is not used, because the holes would necessitate the addition of an explict memset(), to avoid a potential kernel data leak, making for a less minimal fix.

Fixes 55707294c4eb ("iio: light: Add support for vishay vcnl40352")
Cc Parthiban Nallathambi <[email protected]>
---
 drivers/iio/light/vcnl4035.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/light/vcnl4035.c +2 -1
index ae87740d9cef..bc0777411712 100644
@@ -102,7 +102,8 @@ static irqreturn_t vcnl4035_trigger_consumer_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct vcnl4035_data *data = iio_priv(indio_dev);
-	u8 buffer[ALIGN(sizeof(u16), sizeof(s64)) + sizeof(s64)];
+	/* Ensure naturally aligned timestamp */
+	u8 buffer[ALIGN(sizeof(u16), sizeof(s64)) + sizeof(s64)]  __aligned(8);
 	int ret;
 
 	ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, (int *)buffer);
Signature · 1 line
2.31.1
author maintainer
#19 lore raw

PATCH 11/11 iio: prox: isl29501: Fix buffer alignment in iio_push_to_buffers_with_timestamp()

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Add __aligned(8) to ensure the buffer passed to iio_push_to_buffers_with_timestamp() is suitable for the naturally aligned timestamp that will be inserted.

Here an explicit structure is not used, because the holes would necessitate the addition of an explict memset(), to avoid a kernel data leak, making for a less minimal fix.

Fixes 1c28799257bc ("iio: light: isl29501: Add support for the ISL29501 ToF sensor.")
Cc Mathieu Othacehe <[email protected]>
---
 drivers/iio/proximity/isl29501.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/proximity/isl29501.c +1 -1
index 90e76451c972..5b6ea783795d 100644
@@ -938,7 +938,7 @@ static irqreturn_t isl29501_trigger_handler(int irq, void *p)
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct isl29501_private *isl29501 = iio_priv(indio_dev);
 	const unsigned long *active_mask = indio_dev->active_scan_mask;
-	u32 buffer[4] = {}; /* 1x16-bit + ts */
+	u32 buffer[4] __aligned(8) = {}; /* 1x16-bit + naturally aligned ts */
 
 	if (test_bit(ISL29501_DISTANCE_SCAN_INDEX, active_mask))
 		isl29501_register_read(isl29501, REG_DISTANCE, buffer);
Signature · 1 line
2.31.1
author maintainer
#20 lore raw
In reply to Jonathan Cameron
Quoted 1 line · On Sat, 1 May 2021 18:13:41 +0100 Jonathan Cameron <[email protected]> wrote:

From: Jonathan Cameron <[email protected]>

Thanks to those who have already provided reviews on this set.
If anyone has time to do a quick sanity check of the remaining patches it would be much appreciated. I'll pick up the ones which have been reviewed in a few mins.

+Cc Andy who was kind enough to look at the other 'parts' of this mega series.

Thanks

Jonathan

Quoted 74 lines

I finally got around to do a manual audit of all the calls to iio_push_to_buffers_with_timestamp() which has the somewhat odd requirements of:
1. 8 byte alignment of the provided buffer.
2. space for an 8 byte naturally aligned timestamp to be inserted at the end.

Unfortunately there were rather a lot of these left, but time to bite the bullet and clean them up.

As discussed previous in
https://lore.kernel.org/linux-iio/[email protected]/
it is not easy to fix the alignment issue without requiring a bounce buffer (see part 4 of the alignment fixes for a proposal for that where it is absolutely necessary).

Part 3 contains the cases where the struct approach in part 2 did not seem appropriate. Normally there are two possible reasons for this.
1. Would have required an additional memset operation to avoid any possibility of leaking kernel data.
2. The location of the timestamp may depend on the channels enabled. This normally happens when the max sizeof channels is greater than 8 bytes.

Once all cases are fixes, I'll take a look at hardening against any accidental reintroductions. Note that on many platforms and usecases the bug in question will never occur.

Cc Eugen Hristev <[email protected]>
Cc Andreas Klinger <[email protected]>
Cc Matt Ranostay <[email protected]>
Cc Gwendal Grignou <[email protected]>
Cc Song Qiang <[email protected]>
Cc Mathieu Othacehe <[email protected]>
Cc Parthiban Nallathambi <[email protected]>

Jonathan Cameron (11):
iio: adc: at91-sama5d2: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: adc: hx711: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: adc: mxs-lradc: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: adc: ti-ads8688: Fix alignment of buffer in
iio_push_to_buffers_with_timestamp()
iio: chemical: atlas: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: cros_ec_sensors: Fix alignment of buffer in
iio_push_to_buffers_with_timestamp()
iio: potentiostat: lmp91000: Fix alignment of buffer in
iio_push_to_buffers_with_timestamp()
iio: magn: rm3100: Fix alignment of buffer in
iio_push_to_buffers_with_timestamp()
iio: light: vcnl4000: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: light: vcnl4035: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: prox: isl29501: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()

 drivers/iio/adc/at91-sama5d2_adc.c              | 3 ++-
 drivers/iio/adc/hx711.c                         | 4 ++--
 drivers/iio/adc/mxs-lradc-adc.c                 | 3 ++-
 drivers/iio/adc/ti-ads8688.c                    | 3 ++-
 drivers/iio/chemical/atlas-sensor.c             | 4 ++--
 drivers/iio/light/vcnl4000.c                    | 2 +-
 drivers/iio/light/vcnl4035.c                    | 3 ++-
 drivers/iio/magnetometer/rm3100-core.c          | 3 ++-
 drivers/iio/potentiostat/lmp91000.c             | 4 ++--
 drivers/iio/proximity/isl29501.c                | 2 +-
 include/linux/iio/common/cros_ec_sensors_core.h | 2 +-
 11 files changed, 19 insertions(+), 14 deletions(-)
author maintainer
#21 lore raw
Quoted 11 lines · On Thu, 13 May 2021 18:58:32 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 1 line · On Sat, 1 May 2021 18:13:41 +0100 Jonathan Cameron <[email protected]> wrote:

From: Jonathan Cameron <[email protected]>

Thanks to those who have already provided reviews on this set.
If anyone has time to do a quick sanity check of the remaining patches it would be much appreciated. I'll pick up the ones which have been reviewed in a few mins.

+Cc Andy who was kind enough to look at the other 'parts' of this mega series.

Anyone bored? Still looking for a sanity check of the rest of this series before I apply it.

I aim to revisit part 4 in the next few days.

Jonathan

Quoted 80 lines

Thanks

Jonathan

Quoted 74 lines

I finally got around to do a manual audit of all the calls to iio_push_to_buffers_with_timestamp() which has the somewhat odd requirements of:
1. 8 byte alignment of the provided buffer.
2. space for an 8 byte naturally aligned timestamp to be inserted at the end.

Unfortunately there were rather a lot of these left, but time to bite the bullet and clean them up.

As discussed previous in
https://lore.kernel.org/linux-iio/[email protected]/
it is not easy to fix the alignment issue without requiring a bounce buffer (see part 4 of the alignment fixes for a proposal for that where it is absolutely necessary).

Part 3 contains the cases where the struct approach in part 2 did not seem appropriate. Normally there are two possible reasons for this.
1. Would have required an additional memset operation to avoid any possibility of leaking kernel data.
2. The location of the timestamp may depend on the channels enabled. This normally happens when the max sizeof channels is greater than 8 bytes.

Once all cases are fixes, I'll take a look at hardening against any accidental reintroductions. Note that on many platforms and usecases the bug in question will never occur.

Cc Eugen Hristev <[email protected]>
Cc Andreas Klinger <[email protected]>
Cc Matt Ranostay <[email protected]>
Cc Gwendal Grignou <[email protected]>
Cc Song Qiang <[email protected]>
Cc Mathieu Othacehe <[email protected]>
Cc Parthiban Nallathambi <[email protected]>

Jonathan Cameron (11):
iio: adc: at91-sama5d2: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: adc: hx711: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: adc: mxs-lradc: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: adc: ti-ads8688: Fix alignment of buffer in
iio_push_to_buffers_with_timestamp()
iio: chemical: atlas: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: cros_ec_sensors: Fix alignment of buffer in
iio_push_to_buffers_with_timestamp()
iio: potentiostat: lmp91000: Fix alignment of buffer in
iio_push_to_buffers_with_timestamp()
iio: magn: rm3100: Fix alignment of buffer in
iio_push_to_buffers_with_timestamp()
iio: light: vcnl4000: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: light: vcnl4035: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()
iio: prox: isl29501: Fix buffer alignment in
iio_push_to_buffers_with_timestamp()

 drivers/iio/adc/at91-sama5d2_adc.c              | 3 ++-
 drivers/iio/adc/hx711.c                         | 4 ++--
 drivers/iio/adc/mxs-lradc-adc.c                 | 3 ++-
 drivers/iio/adc/ti-ads8688.c                    | 3 ++-
 drivers/iio/chemical/atlas-sensor.c             | 4 ++--
 drivers/iio/light/vcnl4000.c                    | 2 +-
 drivers/iio/light/vcnl4035.c                    | 3 ++-
 drivers/iio/magnetometer/rm3100-core.c          | 3 ++-
 drivers/iio/potentiostat/lmp91000.c             | 4 ++--
 drivers/iio/proximity/isl29501.c                | 2 +-
 include/linux/iio/common/cros_ec_sensors_core.h | 2 +-
 11 files changed, 19 insertions(+), 14 deletions(-)
#22 lore raw
Quoted 14 lines · Jonathan Cameron
Quoted 10 lines · On Thu, 13 May 2021 18:58:32 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 1 line · On Sat, 1 May 2021 18:13:41 +0100 Jonathan Cameron <[email protected]> wrote:

From: Jonathan Cameron <[email protected]>

Thanks to those who have already provided reviews on this set.
If anyone has time to do a quick sanity check of the remaining patches it would be much appreciated. I'll pick up the ones which have been reviewed in a few mins.

Quoted 4 lines
Quoted 1 line

+Cc Andy who was kind enough to look at the other 'parts' of this mega series.

Anyone bored? Still looking for a sanity check of the rest of this series before I apply it.

Sorry, I have some high priority tasks, and this one is not in the list :-(

Signature · 2 lines
With Best Regards,
Andy Shevchenko