Skip to contentSkip to conversation

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

PATCH RESEND In review Jonathan Cameron 2 participants 11 messages Jun 13, 2021 – Jun 14, 2021

Subsystems (6)
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]>

Resent as none of these have had any comments since being sent out over a month ago. Note these are very straight forward, but I've messed up such things in the past so really appreciate it if someone takes the time to do a quick sanity check.

I have picked up those patches that did get review in first posting and dropped them from this posting.

Cover letter from first posting.

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 Song Qiang <[email protected]>
Cc Mathieu Othacehe <[email protected]>
Cc Parthiban Nallathambi <[email protected]>

Jonathan Cameron (8):
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: 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/light/vcnl4000.c           | 2 +-
 drivers/iio/light/vcnl4035.c           | 3 ++-
 drivers/iio/magnetometer/rm3100-core.c | 3 ++-
 drivers/iio/proximity/isl29501.c       | 2 +-
 8 files changed, 14 insertions(+), 9 deletions(-)
Signature · 1 line
2.32.0
author maintainer
#1 lore raw

PATCH RESEND 1/8 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 6e8c28675947..ea5ca163d879 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.32.0
author maintainer
#2 lore raw

PATCH RESEND 2/8 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.32.0
author maintainer
#3 lore raw

PATCH RESEND 3/8 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 1d99170d3328..bca79a93cbe4 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.32.0
author maintainer
#4 lore raw

PATCH RESEND 4/8 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.32.0
author maintainer
#5 lore raw

PATCH RESEND 5/8 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 4df5887fd04c..13914273c999 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.32.0
author maintainer
#6 lore raw

PATCH RESEND 6/8 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 01772327a947..e02e92bc2928 100644
@@ -908,7 +908,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.32.0
author maintainer
#7 lore raw

PATCH RESEND 7/8 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 fd2f181b16db..0db306ee910e 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.32.0
author maintainer
#8 lore raw

PATCH RESEND 8/8 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.32.0
reviewer Reviewed-by on the v1 series
#9 lore raw
In reply to Jonathan Cameron

Hi Jonathan,

Quoted 95 lines

From: Jonathan Cameron <[email protected]>
Sent: Sunday, June 13, 2021 5:23 PM
To: [email protected]
Cc: Jonathan Cameron <[email protected]>; Eugen Hristev <[email protected]>; Andreas Klinger <ak@it- klinger.d>; Song Qiang <[email protected]>; Mathieu Othacehe <[email protected]>; Parthiban Nallathambi <[email protected]>
Subject: [PATCH RESEND 0/8] IIO: Alignment fixes part 3 - __aligned(8) used to ensure alignment

From: Jonathan Cameron <[email protected]>

Resent as none of these have had any comments since being sent out over
a month ago. Note these are very straight forward, but I've messed up such things in the past so really appreciate it if someone takes the time to do a quick sanity check.

I have picked up those patches that did get review in first posting and dropped them from this posting.

Cover letter from first posting.

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://urldefense.com/v3/__https://lore.kernel.org/linux-
iio/20200920112742.170751-1- [email protected]/__;!!A3Ni8CS0y2Y!uCn6NSpGUvHYdcFazn7flxxjh8RA RqPCumitrRDDgGAjXF7crgi911KY2v_iyw$
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 Song Qiang <[email protected]>
Cc Mathieu Othacehe <[email protected]>
Cc Parthiban Nallathambi <[email protected]>

Jonathan Cameron (8):
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: 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/light/vcnl4000.c           | 2 +-
 drivers/iio/light/vcnl4035.c           | 3 ++-
 drivers/iio/magnetometer/rm3100-core.c | 3 ++-
 drivers/iio/proximity/isl29501.c       | 2 +-
 8 files changed, 14 insertions(+), 9 deletions(-)
Quoted 2 lines

--
2.32.0

LGTM. For the whole series:

- Nuno Sá

author maintainer
#10 lore raw
Quoted 97 lines · Nuno

Hi Jonathan,

Quoted 95 lines

From: Jonathan Cameron <[email protected]>
Sent: Sunday, June 13, 2021 5:23 PM
To: [email protected]
Cc: Jonathan Cameron <[email protected]>; Eugen Hristev <[email protected]>; Andreas Klinger <ak@it- klinger.d>; Song Qiang <[email protected]>; Mathieu Othacehe <[email protected]>; Parthiban Nallathambi <[email protected]>
Subject: [PATCH RESEND 0/8] IIO: Alignment fixes part 3 - __aligned(8) used to ensure alignment

From: Jonathan Cameron <[email protected]>

Resent as none of these have had any comments since being sent out over
a month ago. Note these are very straight forward, but I've messed up such things in the past so really appreciate it if someone takes the time to do a quick sanity check.

I have picked up those patches that did get review in first posting and dropped them from this posting.

Cover letter from first posting.

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://urldefense.com/v3/__https://lore.kernel.org/linux-
iio/20200920112742.170751-1- [email protected]/__;!!A3Ni8CS0y2Y!uCn6NSpGUvHYdcFazn7flxxjh8RA RqPCumitrRDDgGAjXF7crgi911KY2v_iyw$
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 Song Qiang <[email protected]>
Cc Mathieu Othacehe <[email protected]>
Cc Parthiban Nallathambi <[email protected]>

Jonathan Cameron (8):
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: 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/light/vcnl4000.c           | 2 +-
 drivers/iio/light/vcnl4035.c           | 3 ++-
 drivers/iio/magnetometer/rm3100-core.c | 3 ++-
 drivers/iio/proximity/isl29501.c       | 2 +-
 8 files changed, 14 insertions(+), 9 deletions(-)
Quoted 6 lines
Quoted 2 lines

--
2.32.0

LGTM. For the whole series:

Thanks!

Given how long they've been sitting around on list...

Applied to the togreg branch of iio.git and pushed out as testing for all the normal reasons.

Thanks,

Jonathan

Quoted 2 lines

- Nuno Sá