linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 00/27] IIO: Fused set 1 and 2 of timestamp alignment fixes
@ 2020-07-22 15:50 Jonathan Cameron
  2020-07-22 15:50 ` [PATCH v3 01/27] iio: accel: kxsd9: Fix alignment of local buffer Jonathan Cameron
                   ` (27 more replies)
  0 siblings, 28 replies; 72+ messages in thread
From: Jonathan Cameron @ 2020-07-22 15:50 UTC (permalink / raw)
  To: linux-iio
  Cc: Andy Shevchenko, Lars-Peter Clausen, Peter Meerwald, Jonathan Cameron

From: Jonathan Cameron <Jonathan.Cameron@huawei.com>

These are back again. Please take a look at them.  I'm fairly sure these
are all 'safe' but really don't like taking patches without the surety of
a second pair of eyes having taken a look at them. That is particularly
true of fixes for long term issues like these ones.

Changes since v2:
* bmc150-accel: Use sizeof() for channel size (Andy Shevchenko)
* st_uvis25: Use local variable for regmap call (Andy Shevchenko)
* st_lsm6dsx: Use array of scan[] rather than 3 structures (Lorenzo Bianconi)
* inv_mpu6050: Add patch switching to a regmap_noinc_read (Jean-Baptiste Maneyrol)
* ina2xx: Use a structure (previously failed to notice that works here)
* I've added clarifying notes to patch descriptions based on questions asked.
  These were mainly about why we didn't use the structure approach everywhere
  and why I've forced alignment in places it wasn't strictly needed.

Previous cover letter:
A few notes based on questions on v1.

1. Why not use put_unaligned to avoid the whole thing?
   This interface can pass directly to a variety of in kernel users
   who would reasonably assume that we have true natural alignment.
   When it gets passed directly is subtle as it depends on whether
   the demux has to realign the data or not.  So enabling an extra
   channel could result in a previously working alignment no longer
   being true.
   
   Even if this is fine for existing usecases we are likely to
   store up subtle long term issues if we don't fix it explicitly.
   It's also worth noting that the data channel sometimes suffered
   the same problem as the timestamp.

2. Why not specify explicit padding?
   In my view this is error prone in comparisom with relying on
   c to do the hard work for us.

3. Why not move the timestamp to the start?
   ABI breakage and as timestamp is optional (no obvious from the
   iio_push_to_buffers_with_timestamp call) we can end up having
   to shift the rest of the data within that call.
   
Changes since v1.

Andy Schevchenko pointed out that on x86_32 s64 elements are only
aligned to 4 bytes.  Where I had tried to use a structure to avoid
explicit need to list the padding, there were some cases where
this results in insufficient padding being inserted.

This doesn't affect the few patches that had already been applied and
sent upstream. (which was lucky ;)

The fix was to take advantage of __aligned(8) which (according to
my reading of the c spec and the gcc docs) enforces the alignment of
both the element within a structure and the structure itself.
The kernel now requires a recent enough version of GCC to ensure this
works both on the stack and heap.  This is done in lots of other
userspace interfaces. In some cases iio_push_to_buffers_with_ts
is aligning data for passing to userspace, be it via a kfifo
so it is sensible we should use the same solution.

Note that we could have used u64_aligned but there is no equivalent
for s64 and explicit use of __aligned(8) is common in
the kernel so we adopt this here.

Note that there were about 8 drivers that would have been broken with
v1 of the patch.  I have also forced alignment of timestamps in cases
where (mostly by coincidence) we would have been fine (padding was
less than 4 bytes anyway.  I did this partly to reduce fragility if
other elements are added in future and also to avoid cut and paste
errors in new drivers.

There were a few other minor tidying up changes inline with reviews
of v1.

I've kept tags given for v1 on basis the changes are minor. Shout if
you disagree.

Version 1 part 1 cover letter.

Lars noted in a recent review [1] of the adis16475 that we had an issue around
the alignment requirements of iio_push_to_buffers_with_timestamp.
Whilst it's not documented, that function assumes that the overall buffer
is 8 byte aligned, to ensure the timestamp is itself naturally aligned.
We have drivers that use arrays (typically on the stack) that do
not guarantee this alignment.

We could have fixed this by using a put_unaligned to write the timestamp
but I think that just pushes the problem down the line.  If we were to
have a consumer buffer wanting all the channels in the current
active_scanmask then it will get the raw buffer from the driver passed
straight through.  It seems odd to me if we allow passing a buffer
that is not naturally aligned through to a consumer.
Hence I'm proposing to fix up all existing drivers that might pass
a buffer with insufficient alignment guarantees.
Sometimes the timestamp is guaranteed to be in a particular location,
in which case we can use C structure alignment guarantees to fix this
in a nice readable fashion.  In other cases, the timestamp location
depends on which channels are enabled, and in those case we can
use explicit alignment __aligned(8) to ensure the whole array is
appropriately aligned.

Lars-Peter also noted that, in many of these cases, there are holes
in the stack array that we never write.  Those provide a potential
leak of kernel data to userspace.  For drivers where this applies
we either need to zero those holes each time, or allocate the buffer
on the heap (only once), ensuring it is zeroed at that time.
We may leak previous values from the sensor but currently that seems
unlikely to present any form of security risk.

As such, this first set contains a mixture of fixes.  Where there
are no possible holes, the buffer is kept on the stack but a
c structure is used to guarantee appropriate alignment.  Where
there are holes, the buffer is moved into the iio_priv() accessed
data private structure. A c structure or __aligned(8) is used
as appropriate.

I've stopped at this point rather than doing all the drivers Lars
found in order to both throttle the review burden and also to
see find any general problems with the fixes before doign futher
similar series.  A few of the remaining ones will be rather more
complex to deal with.

These have been there a long time, so whilst they are fixes we
will want in stable I'm not that bothered if it takes us a little
while to get them there!

[1] https://www.spinics.net/lists/devicetree/msg350590.html
[2] https://patchwork.kernel.org/cover/11554215/

Jonathan Cameron (27):
  iio: accel: kxsd9: Fix alignment of local buffer.
  iio:accel:mma8452: Fix timestamp alignment and prevent data leak.
  iio:accel:bmc150-accel: Fix timestamp alignment and prevent data leak.
  iio:accel:mma7455: Fix timestamp alignment and prevent data leak.
  iio:gyro:itg3200: Fix timestamp alignment and prevent data leak.
  iio:proximity:mb1232: Fix timestamp alignment and prevent data leak.
  iio:chemical:ccs811: Fix timestamp alignment and prevent data leak.
  iio:light:si1145: Fix timestamp alignment and prevent data leak.
  iio:light:max44000 Fix timestamp alignment and prevent data leak.
  iio:light:rpr0521 Fix timestamp alignment and prevent data leak.
  iio:light:st_uvis25 Fix timestamp alignment and prevent data leak.
  iio:light:ltr501 Fix timestamp alignment issue.
  iio:magnetometer:ak8975 Fix alignment and data leak issues.
  iio:magnetometer:mag3110 Fix alignment and data leak issues.
  iio:imu:bmi160 Fix alignment and data leak issues
  iio:imu:st_lsm6dsx Fix alignment and data leak issues
  iio:imu:inv_mpu6050 Fix dma and ts alignment and data leak issues.
  iio:imu:inv_mpu6050: Use regmap_noinc_read for fifo reads.
  iio:pressure:mpl3115 Force alignment of buffer
  iio:adc:ti-adc081c Fix alignment and data leak issues
  iio:adc:ti-adc084s021 Fix alignment and data leak issues.
  iio:adc:ti-adc084s021 Tidy up endian types
  iio:adc:ti-ads124s08 Fix alignment and data leak issues.
  iio:adc:ti-adc0832 Fix alignment issue with timestamp
  iio:adc:ti-adc12138 Fix alignment issue with timestamp
  iio:adc:ina2xx Fix timestamp alignment issue.
  iio:adc:max1118 Fix alignment of timestamp and data leak issues

 drivers/iio/accel/bmc150-accel-core.c         | 15 ++++++--
 drivers/iio/accel/kxsd9.c                     | 16 ++++++---
 drivers/iio/accel/mma7455_core.c              | 16 ++++++---
 drivers/iio/accel/mma8452.c                   | 11 ++++--
 drivers/iio/adc/ina2xx-adc.c                  | 11 +++---
 drivers/iio/adc/max1118.c                     | 10 ++++--
 drivers/iio/adc/ti-adc081c.c                  | 11 ++++--
 drivers/iio/adc/ti-adc0832.c                  |  7 ++--
 drivers/iio/adc/ti-adc084s021.c               | 20 ++++++-----
 drivers/iio/adc/ti-adc12138.c                 |  9 ++---
 drivers/iio/adc/ti-ads124s08.c                | 10 ++++--
 drivers/iio/chemical/ccs811.c                 | 13 ++++---
 drivers/iio/gyro/itg3200_buffer.c             | 15 +++++---
 drivers/iio/imu/bmi160/bmi160.h               |  2 ++
 drivers/iio/imu/bmi160/bmi160_core.c          |  5 ++-
 drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h     |  8 +++--
 drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c    | 14 ++++----
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h       |  6 ++++
 .../iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c    | 36 ++++++++++---------
 drivers/iio/light/ltr501.c                    | 15 ++++----
 drivers/iio/light/max44000.c                  | 12 ++++---
 drivers/iio/light/rpr0521.c                   | 17 ++++++---
 drivers/iio/light/si1145.c                    |  7 ++--
 drivers/iio/light/st_uvis25.h                 |  5 +++
 drivers/iio/light/st_uvis25_core.c            |  8 +++--
 drivers/iio/magnetometer/ak8975.c             | 16 ++++++---
 drivers/iio/magnetometer/mag3110.c            | 13 ++++---
 drivers/iio/pressure/mpl3115.c                |  3 +-
 drivers/iio/proximity/mb1232.c                | 17 ++++-----
 29 files changed, 229 insertions(+), 119 deletions(-)

-- 
2.27.0


^ permalink raw reply	[flat|nested] 72+ messages in thread

end of thread, other threads:[~2020-11-29 13:28 UTC | newest]

Thread overview: 72+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-22 15:50 [PATCH v3 00/27] IIO: Fused set 1 and 2 of timestamp alignment fixes Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 01/27] iio: accel: kxsd9: Fix alignment of local buffer Jonathan Cameron
2020-08-09 17:07   ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 02/27] iio:accel:mma8452: Fix timestamp alignment and prevent data leak Jonathan Cameron
2020-08-09 17:14   ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 03/27] iio:accel:bmc150-accel: " Jonathan Cameron
2020-07-29 17:12   ` Srinivas Pandruvada
2020-08-09 17:18     ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 04/27] iio:accel:mma7455: " Jonathan Cameron
2020-08-09 17:19   ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 05/27] iio:gyro:itg3200: " Jonathan Cameron
2020-09-19 15:41   ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 06/27] iio:proximity:mb1232: " Jonathan Cameron
2020-08-09 17:20   ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 07/27] iio:chemical:ccs811: " Jonathan Cameron
2020-08-09 17:23   ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 08/27] iio:light:si1145: " Jonathan Cameron
2020-07-22 19:43   ` Andy Shevchenko
2020-07-22 19:45     ` Andy Shevchenko
2020-07-23 11:25       ` Jonathan Cameron
2020-09-19 15:46         ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 09/27] iio:light:max44000 " Jonathan Cameron
2020-08-09 17:24   ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 10/27] iio:light:rpr0521 " Jonathan Cameron
2020-07-22 19:47   ` Andy Shevchenko
2020-07-23 11:29     ` Jonathan Cameron
2020-09-19 16:31       ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 11/27] iio:light:st_uvis25 " Jonathan Cameron
2020-07-22 19:48   ` Andy Shevchenko
2020-07-22 15:50 ` [PATCH v3 12/27] iio:light:ltr501 Fix timestamp alignment issue Jonathan Cameron
2020-08-09 17:27   ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 13/27] iio:magnetometer:ak8975 Fix alignment and data leak issues Jonathan Cameron
2020-08-09 17:30   ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 14/27] iio:magnetometer:mag3110 " Jonathan Cameron
2020-07-23 12:13   ` Andy Shevchenko
2020-07-22 15:50 ` [PATCH v3 15/27] iio:imu:bmi160 " Jonathan Cameron
2020-09-19 15:58   ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 16/27] iio:imu:st_lsm6dsx " Jonathan Cameron
2020-09-19 16:09   ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 17/27] iio:imu:inv_mpu6050 Fix dma and ts " Jonathan Cameron
2020-07-24  8:27   ` Jean-Baptiste Maneyrol
2020-09-19 16:51     ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 18/27] iio:imu:inv_mpu6050: Use regmap_noinc_read for fifo reads Jonathan Cameron
2020-07-23 12:15   ` Andy Shevchenko
2020-07-23 12:28     ` Jonathan Cameron
2020-07-24  8:29   ` Jean-Baptiste Maneyrol
2020-09-19 16:55     ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 19/27] iio:pressure:mpl3115 Force alignment of buffer Jonathan Cameron
2020-07-23 12:17   ` Andy Shevchenko
2020-07-23 12:31     ` Jonathan Cameron
2020-09-19 17:02       ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 20/27] iio:adc:ti-adc081c Fix alignment and data leak issues Jonathan Cameron
2020-08-09 17:34   ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 21/27] iio:adc:ti-adc084s021 " Jonathan Cameron
2020-08-09 17:36   ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 22/27] iio:adc:ti-adc084s021 Tidy up endian types Jonathan Cameron
2020-08-09 17:47   ` Jonathan Cameron
2020-09-13 12:01     ` Jonathan Cameron
2020-09-13 12:06       ` Jonathan Cameron
2020-11-29 13:28         ` Jonathan Cameron
2020-07-22 15:50 ` [PATCH v3 23/27] iio:adc:ti-ads124s08 Fix alignment and data leak issues Jonathan Cameron
2020-07-22 20:54   ` Andy Shevchenko
2020-07-23 11:23     ` Jonathan Cameron
2020-07-22 15:51 ` [PATCH v3 24/27] iio:adc:ti-adc0832 Fix alignment issue with timestamp Jonathan Cameron
2020-09-19 16:18   ` Jonathan Cameron
2020-07-22 15:51 ` [PATCH v3 25/27] iio:adc:ti-adc12138 " Jonathan Cameron
2020-09-19 16:21   ` Jonathan Cameron
2020-07-22 15:51 ` [PATCH v3 26/27] iio:adc:ina2xx Fix timestamp alignment issue Jonathan Cameron
2020-08-09 17:38   ` Jonathan Cameron
2020-07-22 15:51 ` [PATCH v3 27/27] iio:adc:max1118 Fix alignment of timestamp and data leak issues Jonathan Cameron
2020-08-09 17:39   ` Jonathan Cameron
2020-07-23 12:23 ` [PATCH v3 00/27] IIO: Fused set 1 and 2 of timestamp alignment fixes Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).