mbox series

[v2,0/2] Add ADXL367 driver

Message ID 20211207094337.59300-1-cosmin.tanislav@analog.com (mailing list archive)
Headers show
Series Add ADXL367 driver | expand

Message

Cosmin Tanislav Dec. 7, 2021, 9:43 a.m. UTC
The ADXL367 is an ultralow power, 3-axis MEMS accelerometer.

The ADXL367 does not alias input signals to achieve ultralow power
consumption, it samples the full bandwidth of the sensor at all
data rates. Measurement ranges of +-2g, +-4g, and +-8g are available,
with a resolution of 0.25mg/LSB on the +-2 g range.

In addition to its ultralow power consumption, the ADXL367
has many features to enable true system level power reduction.
It includes a deep multimode output FIFO, a built-in micropower
temperature sensor, and an internal ADC for synchronous conversion
of an additional analog input.

V1 -> V2
 * add support for vdd and vddio supplies
 * lock fifo_watermark retrieval
 * fix indentation of sysfs_emit for fifo_mode
 * dt-bindings: add spi-max-frequency: true
 * dt-bindings: remove cs-gpios property
 * dt-bindings: remove status property
 * dt-bindings: add support for vdd

I have one question that is not actually specific to this driver but would
help me clear up some issues.

I used mutex_lock and mutex_unlock when accessing anything in driver's
state that could potentially be written by another process in parallel.

I heard mixed opinions about this. Some people said that it is not
necessary to lock everywhere because loads and stores for data with size
smaller or equal than register size would be done in one single atomic
instruction.

On the other hand, I also heard that this is not true unless WRITE_ONCE
and READ_ONCE is used.

It felt weird using WRITE_ONCE and READ_ONCE in this driver, so I kept
using mutexes.

Could I get some opinions on this matter?

Cosmin Tanislav (2):
  dt-bindings: iio: accel: add ADXL367
  iio: accel: add ADXL367 driver

 .../bindings/iio/accel/adi,adxl367.yaml       |   79 +
 MAINTAINERS                                   |   11 +
 drivers/iio/accel/Kconfig                     |   27 +
 drivers/iio/accel/Makefile                    |    3 +
 drivers/iio/accel/adxl367.c                   | 1696 +++++++++++++++++
 drivers/iio/accel/adxl367.h                   |   24 +
 drivers/iio/accel/adxl367_i2c.c               |   89 +
 drivers/iio/accel/adxl367_spi.c               |  151 ++
 8 files changed, 2080 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/accel/adi,adxl367.yaml
 create mode 100644 drivers/iio/accel/adxl367.c
 create mode 100644 drivers/iio/accel/adxl367.h
 create mode 100644 drivers/iio/accel/adxl367_i2c.c
 create mode 100644 drivers/iio/accel/adxl367_spi.c

Comments

Lars-Peter Clausen Dec. 13, 2021, 11:34 a.m. UTC | #1
On 12/7/21 10:43 AM, Cosmin Tanislav wrote:
> I have one question that is not actually specific to this driver but would
> help me clear up some issues.
>
> I used mutex_lock and mutex_unlock when accessing anything in driver's
> state that could potentially be written by another process in parallel.
>
> I heard mixed opinions about this. Some people said that it is not
> necessary to lock everywhere because loads and stores for data with size
> smaller or equal than register size would be done in one single atomic
> instruction.
>
> On the other hand, I also heard that this is not true unless WRITE_ONCE
> and READ_ONCE is used.
>
> It felt weird using WRITE_ONCE and READ_ONCE in this driver, so I kept
> using mutexes.
>
> Could I get some opinions on this matter?

What you wrote sums it up very well. READ_ONCE/WRITE_ONCE are required 
for correctness when no lock is used. The compiler is allowed to do all 
sorts of optimizations that could break multi-threading, when 
READ_ONCE/WRITE_ONCE is not used. E.g.

if (x)
   foo->bar = 10;
else
   foo->bar = 20;

Could be implemented as

foo->bar = 20;
if (x)
   foo->bar = 10;

In the absence of multi-threading the result will be the same. But if 
another thread reads foo->bar just at the right time it will read the 
incorrect 20.

For simple things like `foo->bar = x;` it is unlikely that the compiler 
will do anything other than the single store. But it could and the code 
is not correct without the WRITE_ONCE.

Using a mutex is OK, since non of this is performance critical.
Nuno Sa Dec. 14, 2021, 3:50 p.m. UTC | #2
> From: Lars-Peter Clausen <lars@metafoo.de>
> Sent: Monday, December 13, 2021 12:34 PM
> To: Cosmin Tanislav <demonsingur@gmail.com>
> Cc: Tanislav, Cosmin <Cosmin.Tanislav@analog.com>; Hennerich,
> Michael <Michael.Hennerich@analog.com>; Rob Herring
> <robh+dt@kernel.org>; linux-iio@vger.kernel.org;
> devicetree@vger.kernel.org; linux-kernel@vger.kernel.org
> Subject: Re: [PATCH v2 0/2] Add ADXL367 driver
> 
> [External]
> 
> On 12/7/21 10:43 AM, Cosmin Tanislav wrote:
> > I have one question that is not actually specific to this driver but
> would
> > help me clear up some issues.
> >
> > I used mutex_lock and mutex_unlock when accessing anything in
> driver's
> > state that could potentially be written by another process in parallel.
> >
> > I heard mixed opinions about this. Some people said that it is not
> > necessary to lock everywhere because loads and stores for data with
> size
> > smaller or equal than register size would be done in one single
> atomic
> > instruction.
> >
> > On the other hand, I also heard that this is not true unless
> WRITE_ONCE
> > and READ_ONCE is used.
> >
> > It felt weird using WRITE_ONCE and READ_ONCE in this driver, so I
> kept
> > using mutexes.
> >
> > Could I get some opinions on this matter?
> 
> What you wrote sums it up very well. READ_ONCE/WRITE_ONCE are
> required
> for correctness when no lock is used. The compiler is allowed to do all
> sorts of optimizations that could break multi-threading, when
> READ_ONCE/WRITE_ONCE is not used. E.g.
> 
> if (x)
>    foo->bar = 10;
> else
>    foo->bar = 20;
> 
> Could be implemented as
> 
> foo->bar = 20;
> if (x)
>    foo->bar = 10;

This example can even be more trickier than simple {WRITE|READ}_ONCE
(not sure though) as we have a control dependency and compilers not
always respect them apparently [but this is out of scope :D]...

> In the absence of multi-threading the result will be the same. But if
> another thread reads foo->bar just at the right time it will read the
> incorrect 20.
> 
> For simple things like `foo->bar = x;` it is unlikely that the compiler
> will do anything other than the single store. But it could and the code
> is not correct without the WRITE_ONCE.

True and things like load/store tearing were already seen in the wild
according to:

https://lwn.net/Articles/793253/

Some time ago I was wondering if this could still be an issue for single
byte stores and loads. Maybe for that case it's not but better not to
assume we know what  the compiler will do. The next bullet sums things
pretty well and is a very nice guideline :)

https://elixir.bootlin.com/linux/latest/source/Documentation/memory-barriers.txt#L269

- Nuno Sá