* [PATCH v2 0/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS
@ 2025-04-22 22:07 David Lechner
2025-04-22 22:07 ` [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros David Lechner
` (5 more replies)
0 siblings, 6 replies; 21+ messages in thread
From: David Lechner @ 2025-04-22 22:07 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Lars-Peter Clausen, Michael Hennerich, Eugen Hristev,
Nicolas Ferre, Alexandre Belloni, Claudiu Beznea
Cc: linux-iio, linux-kernel, linux-arm-kernel
Creating a buffer of the proper size and correct alignment for use with
iio_push_to_buffers_with_ts() is commonly used and not easy to get
right (as seen by a number of recent fixes on the mailing list).
In general, we prefer to use this pattern for creating such buffers:
struct {
u16 data[2];
aligned_s64 timestamp;
} buffer;
However, there are many cases where a driver may have a large number of
channels that can be optionally enabled or disabled in a scan or the
driver might support a range of chips that have different numbers of
channels or different storage sizes for the data. In these cases, the
timestamp may not always be at the same place relative to the data. To
handle these, we allocate a buffer large enough for the largest possible
case and don't care exactly where the timestamp ends up in the buffer.
For these cases, we propose to introduce new macros to make it easier
it easier for both the authors to get it right and for readers of the
code to not have to do all of the math to verify that it is correct.
I have just included a few examples of drivers that can make use of this
new macro, but there are dozens more.
---
Changes in v2:
- Add 2nd macro for case where we need DMA alignment.
- Add new patch for ad4695 to convert buffer from u8 to u16 before
making use of the new macro.
- Drop the bmp280 patch since it was determined to have a better
alternative not using these macros.
- Add a few more examples to show the non-DMA case, both in a struct and
stack allocated.
- Link to v1: https://lore.kernel.org/r/20250418-iio-introduce-iio_declare_buffer_with_ts-v1-0-ee0c62a33a0f@baylibre.com
---
David Lechner (6):
iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros
iio: adc: ad4695: use u16 for buffer elements
iio: adc: ad4695: use IIO_DECLARE_BUFFER_WITH_TS
iio: adc: ad7380: use IIO_DECLARE_BUFFER_WITH_TS
iio: accel: sca3300: use IIO_DECLARE_BUFFER_WITH_TS
iio: adc: at91-sama5d2: use IIO_DECLARE_BUFFER_WITH_TS
drivers/iio/accel/sca3300.c | 18 ++----------------
drivers/iio/adc/ad4695.c | 8 ++------
drivers/iio/adc/ad7380.c | 4 ++--
drivers/iio/adc/at91-sama5d2_adc.c | 13 ++-----------
include/linux/iio/iio.h | 36 ++++++++++++++++++++++++++++++++++++
5 files changed, 44 insertions(+), 35 deletions(-)
---
base-commit: aff301f37e220970c2f301b5c65a8bfedf52058e
change-id: 20250418-iio-introduce-iio_declare_buffer_with_ts-2f8773f7dad6
Best regards,
--
David Lechner <dlechner@baylibre.com>
^ permalink raw reply [flat|nested] 21+ messages in thread* [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros 2025-04-22 22:07 [PATCH v2 0/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS David Lechner @ 2025-04-22 22:07 ` David Lechner 2025-04-22 22:30 ` Andy Shevchenko ` (3 more replies) 2025-04-22 22:07 ` [PATCH v2 2/6] iio: adc: ad4695: use u16 for buffer elements David Lechner ` (4 subsequent siblings) 5 siblings, 4 replies; 21+ messages in thread From: David Lechner @ 2025-04-22 22:07 UTC (permalink / raw) To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea Cc: linux-iio, linux-kernel, linux-arm-kernel Add new macros to help with the common case of declaring a buffer that is safe to use with iio_push_to_buffers_with_ts(). This is not trivial to do correctly because of the alignment requirements of the timestamp. This will make it easier for both authors and reviewers. To avoid double __align() attributes in cases where we also need DMA alignment, add a 2nd variant IIO_DECLARE_DMA_BUFFER_WITH_TS. Signed-off-by: David Lechner <dlechner@baylibre.com> --- include/linux/iio/iio.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h index 638cf2420fbd85cf2924d09d061df601d1d4bb2a..4dd811e3530e228a6fadbd80cfb2f5068c3d6a9a 100644 --- a/include/linux/iio/iio.h +++ b/include/linux/iio/iio.h @@ -7,6 +7,7 @@ #ifndef _INDUSTRIAL_IO_H_ #define _INDUSTRIAL_IO_H_ +#include <linux/align.h> #include <linux/device.h> #include <linux/cdev.h> #include <linux/compiler_types.h> @@ -777,6 +778,41 @@ static inline void *iio_device_get_drvdata(const struct iio_dev *indio_dev) * them safe for use with non-coherent DMA. */ #define IIO_DMA_MINALIGN ARCH_DMA_MINALIGN + +#define _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ + type name[ALIGN((count), sizeof(s64) / sizeof(type)) + sizeof(s64) / sizeof(type)] + +/** + * IIO_DECLARE_BUFFER_WITH_TS() - Declare a buffer with timestamp + * @type: element type of the buffer + * @name: identifier name of the buffer + * @count: number of elements in the buffer + * + * Declares a buffer that is safe to use with iio_push_to_buffer_with_ts(). In + * addition to allocating enough space for @count elements of @type, it also + * allocates space for a s64 timestamp at the end of the buffer and ensures + * proper alignment of the timestamp. + */ +#define IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ + _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(sizeof(s64)) + +/** + * IIO_DECLARE_DMA_BUFFER_WITH_TS() - Declare a DMA-aligned buffer with timestamp + * @type: element type of the buffer + * @name: identifier name of the buffer + * @count: number of elements in the buffer + * + * Same as IIO_DECLARE_BUFFER_WITH_TS(), but is uses __aligned(IIO_DMA_MINALIGN) + * to ensure that the buffer doesn't share cachelines with anything that comes + * before it in a struct. This should not be used for stack-allocated buffers + * as stack memory cannot generally be used for DMA. + */ +#define IIO_DECLARE_DMA_BUFFER_WITH_TS(type, name, count) \ + _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(IIO_DMA_MINALIGN) + +_Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, + "macros above assume that IIO_DMA_MINALIGN also ensures s64 timestamp alignment"); + struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv); /* The information at the returned address is guaranteed to be cacheline aligned */ -- 2.43.0 ^ permalink raw reply related [flat|nested] 21+ messages in thread
* Re: [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros 2025-04-22 22:07 ` [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros David Lechner @ 2025-04-22 22:30 ` Andy Shevchenko 2025-04-22 22:37 ` David Lechner 2025-04-23 9:18 ` Nuno Sá ` (2 subsequent siblings) 3 siblings, 1 reply; 21+ messages in thread From: Andy Shevchenko @ 2025-04-22 22:30 UTC (permalink / raw) To: David Lechner Cc: Jonathan Cameron, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea, linux-iio, linux-kernel, linux-arm-kernel On Wed, Apr 23, 2025 at 1:08 AM David Lechner <dlechner@baylibre.com> wrote: > > Add new macros to help with the common case of declaring a buffer that > is safe to use with iio_push_to_buffers_with_ts(). This is not trivial > to do correctly because of the alignment requirements of the timestamp. > This will make it easier for both authors and reviewers. > > To avoid double __align() attributes in cases where we also need DMA > alignment, add a 2nd variant IIO_DECLARE_DMA_BUFFER_WITH_TS. ... > +#define _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ > + type name[ALIGN((count), sizeof(s64) / sizeof(type)) + sizeof(s64) / sizeof(type)] Single leading underscore seems to me not so usual, I saw people use double underscores to make sure that it will be visible that it's an internal one (kinda). ... > +_Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, Why not static_assert() ? Because of the message? But static_assert() supports messages AFAICS. > + "macros above assume that IIO_DMA_MINALIGN also ensures s64 timestamp alignment"); -- With Best Regards, Andy Shevchenko ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros 2025-04-22 22:30 ` Andy Shevchenko @ 2025-04-22 22:37 ` David Lechner 2025-04-22 22:50 ` Andy Shevchenko 0 siblings, 1 reply; 21+ messages in thread From: David Lechner @ 2025-04-22 22:37 UTC (permalink / raw) To: Andy Shevchenko Cc: Jonathan Cameron, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea, linux-iio, linux-kernel, linux-arm-kernel On 4/22/25 5:30 PM, Andy Shevchenko wrote: > On Wed, Apr 23, 2025 at 1:08 AM David Lechner <dlechner@baylibre.com> wrote: >> ... >> +_Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, > > Why not static_assert() ? Because of the message? But static_assert() > supports messages AFAICS. > >> + "macros above assume that IIO_DMA_MINALIGN also ensures s64 timestamp alignment"); > I just knew that was standard C. But I support BUILD_BUG_ON_MSG or static_assert would work just as well here. ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros 2025-04-22 22:37 ` David Lechner @ 2025-04-22 22:50 ` Andy Shevchenko 0 siblings, 0 replies; 21+ messages in thread From: Andy Shevchenko @ 2025-04-22 22:50 UTC (permalink / raw) To: David Lechner Cc: Jonathan Cameron, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea, linux-iio, linux-kernel, linux-arm-kernel On Wed, Apr 23, 2025 at 1:37 AM David Lechner <dlechner@baylibre.com> wrote: > On 4/22/25 5:30 PM, Andy Shevchenko wrote: > > On Wed, Apr 23, 2025 at 1:08 AM David Lechner <dlechner@baylibre.com> wrote: ... > >> +_Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, > > > > Why not static_assert() ? Because of the message? But static_assert() > > supports messages AFAICS. > > > >> + "macros above assume that IIO_DMA_MINALIGN also ensures s64 timestamp alignment"); > > I just knew that was standard C. But I support BUILD_BUG_ON_MSG or static_assert > would work just as well here. In the kernel we use u8, for example, however in the standard it's uint8_t :-) Same with many compiler attributes and wrappers on top of the compiler things. According to v6.14 codebase the only one driver uses _Static_assert() for that (there are many in tools/ and more in BPF, with a few headers where it's fine (esp. in UAPI where no static_assert() available). -- With Best Regards, Andy Shevchenko ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros 2025-04-22 22:07 ` [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros David Lechner 2025-04-22 22:30 ` Andy Shevchenko @ 2025-04-23 9:18 ` Nuno Sá 2025-04-23 14:51 ` David Lechner 2025-04-23 17:25 ` kernel test robot 2025-04-23 17:46 ` kernel test robot 3 siblings, 1 reply; 21+ messages in thread From: Nuno Sá @ 2025-04-23 9:18 UTC (permalink / raw) To: David Lechner, Jonathan Cameron, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea Cc: linux-iio, linux-kernel, linux-arm-kernel Hi David, Nice patch, I really think these will be very helpful... Just one comment bellow On Tue, 2025-04-22 at 17:07 -0500, David Lechner wrote: > Add new macros to help with the common case of declaring a buffer that > is safe to use with iio_push_to_buffers_with_ts(). This is not trivial > to do correctly because of the alignment requirements of the timestamp. > This will make it easier for both authors and reviewers. > > To avoid double __align() attributes in cases where we also need DMA > alignment, add a 2nd variant IIO_DECLARE_DMA_BUFFER_WITH_TS. > > Signed-off-by: David Lechner <dlechner@baylibre.com> > --- > include/linux/iio/iio.h | 36 ++++++++++++++++++++++++++++++++++++ > 1 file changed, 36 insertions(+) > > diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h > index > 638cf2420fbd85cf2924d09d061df601d1d4bb2a..4dd811e3530e228a6fadbd80cfb2f5068c3d > 6a9a 100644 > --- a/include/linux/iio/iio.h > +++ b/include/linux/iio/iio.h > @@ -7,6 +7,7 @@ > #ifndef _INDUSTRIAL_IO_H_ > #define _INDUSTRIAL_IO_H_ > > +#include <linux/align.h> > #include <linux/device.h> > #include <linux/cdev.h> > #include <linux/compiler_types.h> > @@ -777,6 +778,41 @@ static inline void *iio_device_get_drvdata(const struct > iio_dev *indio_dev) > * them safe for use with non-coherent DMA. > */ > #define IIO_DMA_MINALIGN ARCH_DMA_MINALIGN > + > +#define _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ > + type name[ALIGN((count), sizeof(s64) / sizeof(type)) + sizeof(s64) / > sizeof(type)] > + > +/** > + * IIO_DECLARE_BUFFER_WITH_TS() - Declare a buffer with timestamp > + * @type: element type of the buffer > + * @name: identifier name of the buffer > + * @count: number of elements in the buffer > + * > + * Declares a buffer that is safe to use with iio_push_to_buffer_with_ts(). > In > + * addition to allocating enough space for @count elements of @type, it also > + * allocates space for a s64 timestamp at the end of the buffer and ensures > + * proper alignment of the timestamp. > + */ > +#define IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ > + _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(sizeof(s64)) > + > +/** > + * IIO_DECLARE_DMA_BUFFER_WITH_TS() - Declare a DMA-aligned buffer with > timestamp > + * @type: element type of the buffer > + * @name: identifier name of the buffer > + * @count: number of elements in the buffer > + * > + * Same as IIO_DECLARE_BUFFER_WITH_TS(), but is uses > __aligned(IIO_DMA_MINALIGN) > + * to ensure that the buffer doesn't share cachelines with anything that > comes > + * before it in a struct. This should not be used for stack-allocated buffers > + * as stack memory cannot generally be used for DMA. > + */ > +#define IIO_DECLARE_DMA_BUFFER_WITH_TS(type, name, count) \ > + _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) > __aligned(IIO_DMA_MINALIGN) > + > +_Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, > + "macros above assume that IIO_DMA_MINALIGN also ensures s64 timestamp > alignment"); > I wonder about the usefulness of the above assert... AFAICT, the default alignment is 8 bytes and I could not find any arch defining ARCH_DMA_MINALIGN smaller than that (would be very odd to have a cacheline smaller than that these days). For bigger values, nowadays they are all power of 2 and I would be surprised otherwise. But the more important question to me is what if the above assert fails? Will we not allow IIO or some drivers to be used in that architecture? It can become a very "painful" situation (assuming these macros get widely used). So, IMHO, either we assume the above can happen and rework the macros to make it work for that hypotetical case or we assume the above is always true and drop the assert. TBH, I think it would be a fair assumption... On top of that the assertion is wrong: sizeof(IIO_DMA_MINALIGN) != IIO_DMA_MINALIGN :) On the other hand, as I mentioned in V1, I think that an assertion or BUILD_BUG_ON_MSG for making sure 'count' is a compile time constant expression would be helpful. Sure, we'll get -Wvla but some developers might still ignore the warning and send patches with these arrays. So, it would be neater if we fail to build and force them to fix their code. - Nuno Sá ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros 2025-04-23 9:18 ` Nuno Sá @ 2025-04-23 14:51 ` David Lechner 2025-04-23 15:43 ` Andy Shevchenko 2025-04-26 11:19 ` Jonathan Cameron 0 siblings, 2 replies; 21+ messages in thread From: David Lechner @ 2025-04-23 14:51 UTC (permalink / raw) To: Nuno Sá, Jonathan Cameron, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea Cc: linux-iio, linux-kernel, linux-arm-kernel On 4/23/25 4:18 AM, Nuno Sá wrote: > Hi David, > > Nice patch, I really think these will be very helpful... Just one comment bellow > > On Tue, 2025-04-22 at 17:07 -0500, David Lechner wrote: >> Add new macros to help with the common case of declaring a buffer that >> is safe to use with iio_push_to_buffers_with_ts(). This is not trivial >> to do correctly because of the alignment requirements of the timestamp. >> This will make it easier for both authors and reviewers. >> >> To avoid double __align() attributes in cases where we also need DMA >> alignment, add a 2nd variant IIO_DECLARE_DMA_BUFFER_WITH_TS. >> >> Signed-off-by: David Lechner <dlechner@baylibre.com> >> --- >> include/linux/iio/iio.h | 36 ++++++++++++++++++++++++++++++++++++ >> 1 file changed, 36 insertions(+) >> >> diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h >> index >> 638cf2420fbd85cf2924d09d061df601d1d4bb2a..4dd811e3530e228a6fadbd80cfb2f5068c3d >> 6a9a 100644 >> --- a/include/linux/iio/iio.h >> +++ b/include/linux/iio/iio.h >> @@ -7,6 +7,7 @@ >> #ifndef _INDUSTRIAL_IO_H_ >> #define _INDUSTRIAL_IO_H_ >> >> +#include <linux/align.h> >> #include <linux/device.h> >> #include <linux/cdev.h> >> #include <linux/compiler_types.h> >> @@ -777,6 +778,41 @@ static inline void *iio_device_get_drvdata(const struct >> iio_dev *indio_dev) >> * them safe for use with non-coherent DMA. >> */ >> #define IIO_DMA_MINALIGN ARCH_DMA_MINALIGN >> + >> +#define _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ >> + type name[ALIGN((count), sizeof(s64) / sizeof(type)) + sizeof(s64) / >> sizeof(type)] >> + >> +/** >> + * IIO_DECLARE_BUFFER_WITH_TS() - Declare a buffer with timestamp >> + * @type: element type of the buffer >> + * @name: identifier name of the buffer >> + * @count: number of elements in the buffer >> + * >> + * Declares a buffer that is safe to use with iio_push_to_buffer_with_ts(). >> In >> + * addition to allocating enough space for @count elements of @type, it also >> + * allocates space for a s64 timestamp at the end of the buffer and ensures >> + * proper alignment of the timestamp. >> + */ >> +#define IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ >> + _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(sizeof(s64)) >> + >> +/** >> + * IIO_DECLARE_DMA_BUFFER_WITH_TS() - Declare a DMA-aligned buffer with >> timestamp >> + * @type: element type of the buffer >> + * @name: identifier name of the buffer >> + * @count: number of elements in the buffer >> + * >> + * Same as IIO_DECLARE_BUFFER_WITH_TS(), but is uses >> __aligned(IIO_DMA_MINALIGN) >> + * to ensure that the buffer doesn't share cachelines with anything that >> comes >> + * before it in a struct. This should not be used for stack-allocated buffers >> + * as stack memory cannot generally be used for DMA. >> + */ >> +#define IIO_DECLARE_DMA_BUFFER_WITH_TS(type, name, count) \ >> + _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) >> __aligned(IIO_DMA_MINALIGN) >> + >> +_Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, >> + "macros above assume that IIO_DMA_MINALIGN also ensures s64 timestamp >> alignment"); >> > > I wonder about the usefulness of the above assert... AFAICT, the default Jonathan seemed minorly concerned that a strange new architecture might have IIO_DMA_MINALIGN is < 8 some day, so I threw it in there. But agree, it seems highly unlikely to actually happen. > alignment is 8 bytes and I could not find any arch defining ARCH_DMA_MINALIGN > smaller than that (would be very odd to have a cacheline smaller than that these > days). For bigger values, nowadays they are all power of 2 and I would be > surprised otherwise. But the more important question to me is what if the above > assert fails? Will we not allow IIO or some drivers to be used in that > architecture? It can become a very "painful" situation (assuming these macros > get widely used). So, IMHO, either we assume the above can happen and rework the > macros to make it work for that hypotetical case or we assume the above is > always true and drop the assert. TBH, I think it would be a fair assumption... > > On top of that the assertion is wrong: > > sizeof(IIO_DMA_MINALIGN) != IIO_DMA_MINALIGN :) Doh! > > On the other hand, as I mentioned in V1, I think that an assertion or > BUILD_BUG_ON_MSG for making sure 'count' is a compile time constant expression > would be helpful. Sure, we'll get -Wvla but some developers might still ignore > the warning and send patches with these arrays. So, it would be neater if we > fail to build and force them to fix their code. BUILD_BUG_ON_MSG() won't work because it expands to a do/while loop which won't work in static struct declarations. But I can try to see if we can come up with something that works. ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros 2025-04-23 14:51 ` David Lechner @ 2025-04-23 15:43 ` Andy Shevchenko 2025-04-24 7:44 ` Nuno Sá 2025-04-26 11:19 ` Jonathan Cameron 1 sibling, 1 reply; 21+ messages in thread From: Andy Shevchenko @ 2025-04-23 15:43 UTC (permalink / raw) To: David Lechner Cc: Nuno Sá, Jonathan Cameron, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea, linux-iio, linux-kernel, linux-arm-kernel On Wed, Apr 23, 2025 at 5:51 PM David Lechner <dlechner@baylibre.com> wrote: > On 4/23/25 4:18 AM, Nuno Sá wrote: > > On Tue, 2025-04-22 at 17:07 -0500, David Lechner wrote: ... > > On the other hand, as I mentioned in V1, I think that an assertion or > > BUILD_BUG_ON_MSG for making sure 'count' is a compile time constant expression > > would be helpful. Sure, we'll get -Wvla but some developers might still ignore > > the warning and send patches with these arrays. So, it would be neater if we > > fail to build and force them to fix their code. > BUILD_BUG_ON_MSG() won't work because it expands to a do/while loop which won't > work in static struct declarations. But I can try to see if we can come up with > something that works. I guess Nuno is okay with static_assert() and TBH nowadays the BUILD_BUG() as is most likely historical. -- With Best Regards, Andy Shevchenko ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros 2025-04-23 15:43 ` Andy Shevchenko @ 2025-04-24 7:44 ` Nuno Sá 0 siblings, 0 replies; 21+ messages in thread From: Nuno Sá @ 2025-04-24 7:44 UTC (permalink / raw) To: Andy Shevchenko, David Lechner Cc: Jonathan Cameron, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea, linux-iio, linux-kernel, linux-arm-kernel On Wed, 2025-04-23 at 18:43 +0300, Andy Shevchenko wrote: > On Wed, Apr 23, 2025 at 5:51 PM David Lechner <dlechner@baylibre.com> wrote: > > On 4/23/25 4:18 AM, Nuno Sá wrote: > > > On Tue, 2025-04-22 at 17:07 -0500, David Lechner wrote: > > ... > > > > On the other hand, as I mentioned in V1, I think that an assertion or > > > BUILD_BUG_ON_MSG for making sure 'count' is a compile time constant > > > expression > > > would be helpful. Sure, we'll get -Wvla but some developers might still > > > ignore > > > the warning and send patches with these arrays. So, it would be neater if > > > we > > > fail to build and force them to fix their code. > > > BUILD_BUG_ON_MSG() won't work because it expands to a do/while loop which > > won't > > work in static struct declarations. But I can try to see if we can come up > > with > > something that works. > > I guess Nuno is okay with static_assert() and TBH nowadays the > BUILD_BUG() as is most likely historical. Yes... "...I think that an __assertion__ or BUILD_BUG_ON_MSG..." - Nuno Sá ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros 2025-04-23 14:51 ` David Lechner 2025-04-23 15:43 ` Andy Shevchenko @ 2025-04-26 11:19 ` Jonathan Cameron 1 sibling, 0 replies; 21+ messages in thread From: Jonathan Cameron @ 2025-04-26 11:19 UTC (permalink / raw) To: David Lechner Cc: Nuno Sá, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea, linux-iio, linux-kernel, linux-arm-kernel On Wed, 23 Apr 2025 09:51:25 -0500 David Lechner <dlechner@baylibre.com> wrote: > On 4/23/25 4:18 AM, Nuno Sá wrote: > > Hi David, > > > > Nice patch, I really think these will be very helpful... Just one comment bellow > > > > On Tue, 2025-04-22 at 17:07 -0500, David Lechner wrote: > >> Add new macros to help with the common case of declaring a buffer that > >> is safe to use with iio_push_to_buffers_with_ts(). This is not trivial > >> to do correctly because of the alignment requirements of the timestamp. > >> This will make it easier for both authors and reviewers. > >> > >> To avoid double __align() attributes in cases where we also need DMA > >> alignment, add a 2nd variant IIO_DECLARE_DMA_BUFFER_WITH_TS. > >> > >> Signed-off-by: David Lechner <dlechner@baylibre.com> > >> --- > >> include/linux/iio/iio.h | 36 ++++++++++++++++++++++++++++++++++++ > >> 1 file changed, 36 insertions(+) > >> > >> diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h > >> index > >> 638cf2420fbd85cf2924d09d061df601d1d4bb2a..4dd811e3530e228a6fadbd80cfb2f5068c3d > >> 6a9a 100644 > >> --- a/include/linux/iio/iio.h > >> +++ b/include/linux/iio/iio.h > >> @@ -7,6 +7,7 @@ > >> #ifndef _INDUSTRIAL_IO_H_ > >> #define _INDUSTRIAL_IO_H_ > >> > >> +#include <linux/align.h> > >> #include <linux/device.h> > >> #include <linux/cdev.h> > >> #include <linux/compiler_types.h> > >> @@ -777,6 +778,41 @@ static inline void *iio_device_get_drvdata(const struct > >> iio_dev *indio_dev) > >> * them safe for use with non-coherent DMA. > >> */ > >> #define IIO_DMA_MINALIGN ARCH_DMA_MINALIGN > >> + > >> +#define _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ > >> + type name[ALIGN((count), sizeof(s64) / sizeof(type)) + sizeof(s64) / > >> sizeof(type)] > >> + > >> +/** > >> + * IIO_DECLARE_BUFFER_WITH_TS() - Declare a buffer with timestamp > >> + * @type: element type of the buffer > >> + * @name: identifier name of the buffer > >> + * @count: number of elements in the buffer > >> + * > >> + * Declares a buffer that is safe to use with iio_push_to_buffer_with_ts(). > >> In > >> + * addition to allocating enough space for @count elements of @type, it also > >> + * allocates space for a s64 timestamp at the end of the buffer and ensures > >> + * proper alignment of the timestamp. > >> + */ > >> +#define IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ > >> + _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(sizeof(s64)) > >> + > >> +/** > >> + * IIO_DECLARE_DMA_BUFFER_WITH_TS() - Declare a DMA-aligned buffer with > >> timestamp > >> + * @type: element type of the buffer > >> + * @name: identifier name of the buffer > >> + * @count: number of elements in the buffer > >> + * > >> + * Same as IIO_DECLARE_BUFFER_WITH_TS(), but is uses > >> __aligned(IIO_DMA_MINALIGN) > >> + * to ensure that the buffer doesn't share cachelines with anything that > >> comes > >> + * before it in a struct. This should not be used for stack-allocated buffers > >> + * as stack memory cannot generally be used for DMA. > >> + */ > >> +#define IIO_DECLARE_DMA_BUFFER_WITH_TS(type, name, count) \ > >> + _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) > >> __aligned(IIO_DMA_MINALIGN) > >> + > >> +_Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, > >> + "macros above assume that IIO_DMA_MINALIGN also ensures s64 timestamp > >> alignment"); > >> > > > > I wonder about the usefulness of the above assert... AFAICT, the default > > Jonathan seemed minorly concerned that a strange new architecture might have > IIO_DMA_MINALIGN is < 8 some day, so I threw it in there. But agree, it seems > highly unlikely to actually happen. Yeah, it's unlikely. Architectures using small sizes is not about cacheline length any more but rather than they guarantee that the system will work fine irrespective of the cacheline length. (e.g. x86_64 where the min align has been 8 for a long time - possibly always? and cachelines are generally 64 bytes) It seems very unlikely anyone will care about smaller than that so such a macro is really just paranoia! The ARCH_DMA_MINALIGN fallback is sizeof(unsigned long long). Basically I want the assert so I don't have to pay attention to weird new architectures. I'm not that fussed though if it is hard to do for some reason. Jonathan ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros 2025-04-22 22:07 ` [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros David Lechner 2025-04-22 22:30 ` Andy Shevchenko 2025-04-23 9:18 ` Nuno Sá @ 2025-04-23 17:25 ` kernel test robot 2025-04-23 17:46 ` kernel test robot 3 siblings, 0 replies; 21+ messages in thread From: kernel test robot @ 2025-04-23 17:25 UTC (permalink / raw) To: David Lechner, Jonathan Cameron, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea Cc: oe-kbuild-all, linux-iio, linux-kernel, linux-arm-kernel Hi David, kernel test robot noticed the following build errors: [auto build test ERROR on aff301f37e220970c2f301b5c65a8bfedf52058e] url: https://github.com/intel-lab-lkp/linux/commits/David-Lechner/iio-introduce-IIO_DECLARE_BUFFER_WITH_TS-macros/20250423-061049 base: aff301f37e220970c2f301b5c65a8bfedf52058e patch link: https://lore.kernel.org/r/20250422-iio-introduce-iio_declare_buffer_with_ts-v2-1-3fd36475c706%40baylibre.com patch subject: [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros config: sh-randconfig-001-20250424 (https://download.01.org/0day-ci/archive/20250424/202504240112.hZy9LpvD-lkp@intel.com/config) compiler: sh4-linux-gcc (GCC) 12.4.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250424/202504240112.hZy9LpvD-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202504240112.hZy9LpvD-lkp@intel.com/ All errors (new ones prefixed by >>): In file included from drivers/iio/industrialio-buffer.c:29: >> include/linux/iio/iio.h:813:1: error: static assertion failed: "macros above assume that IIO_DMA_MINALIGN also ensures s64 timestamp alignment" 813 | _Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, | ^~~~~~~~~~~~~~ vim +813 include/linux/iio/iio.h 781 782 #define _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ 783 type name[ALIGN((count), sizeof(s64) / sizeof(type)) + sizeof(s64) / sizeof(type)] 784 785 /** 786 * IIO_DECLARE_BUFFER_WITH_TS() - Declare a buffer with timestamp 787 * @type: element type of the buffer 788 * @name: identifier name of the buffer 789 * @count: number of elements in the buffer 790 * 791 * Declares a buffer that is safe to use with iio_push_to_buffer_with_ts(). In 792 * addition to allocating enough space for @count elements of @type, it also 793 * allocates space for a s64 timestamp at the end of the buffer and ensures 794 * proper alignment of the timestamp. 795 */ 796 #define IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ 797 _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(sizeof(s64)) 798 799 /** 800 * IIO_DECLARE_DMA_BUFFER_WITH_TS() - Declare a DMA-aligned buffer with timestamp 801 * @type: element type of the buffer 802 * @name: identifier name of the buffer 803 * @count: number of elements in the buffer 804 * 805 * Same as IIO_DECLARE_BUFFER_WITH_TS(), but is uses __aligned(IIO_DMA_MINALIGN) 806 * to ensure that the buffer doesn't share cachelines with anything that comes 807 * before it in a struct. This should not be used for stack-allocated buffers 808 * as stack memory cannot generally be used for DMA. 809 */ 810 #define IIO_DECLARE_DMA_BUFFER_WITH_TS(type, name, count) \ 811 _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(IIO_DMA_MINALIGN) 812 > 813 _Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, 814 "macros above assume that IIO_DMA_MINALIGN also ensures s64 timestamp alignment"); 815 -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros 2025-04-22 22:07 ` [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros David Lechner ` (2 preceding siblings ...) 2025-04-23 17:25 ` kernel test robot @ 2025-04-23 17:46 ` kernel test robot 3 siblings, 0 replies; 21+ messages in thread From: kernel test robot @ 2025-04-23 17:46 UTC (permalink / raw) To: David Lechner, Jonathan Cameron, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea Cc: llvm, oe-kbuild-all, linux-iio, linux-kernel, linux-arm-kernel Hi David, kernel test robot noticed the following build errors: [auto build test ERROR on aff301f37e220970c2f301b5c65a8bfedf52058e] url: https://github.com/intel-lab-lkp/linux/commits/David-Lechner/iio-introduce-IIO_DECLARE_BUFFER_WITH_TS-macros/20250423-061049 base: aff301f37e220970c2f301b5c65a8bfedf52058e patch link: https://lore.kernel.org/r/20250422-iio-introduce-iio_declare_buffer_with_ts-v2-1-3fd36475c706%40baylibre.com patch subject: [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros config: riscv-randconfig-002-20250424 (https://download.01.org/0day-ci/archive/20250424/202504240137.4mJaj0GN-lkp@intel.com/config) compiler: clang version 21.0.0git (https://github.com/llvm/llvm-project f819f46284f2a79790038e1f6649172789734ae8) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250424/202504240137.4mJaj0GN-lkp@intel.com/reproduce) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <lkp@intel.com> | Closes: https://lore.kernel.org/oe-kbuild-all/202504240137.4mJaj0GN-lkp@intel.com/ All errors (new ones prefixed by >>): In file included from drivers/iio/industrialio-buffer.c:18: In file included from include/linux/dma-buf.h:16: In file included from include/linux/iosys-map.h:10: In file included from include/linux/io.h:12: In file included from arch/riscv/include/asm/io.h:136: include/asm-generic/io.h:804:2: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] 804 | insb(addr, buffer, count); | ^~~~~~~~~~~~~~~~~~~~~~~~~ arch/riscv/include/asm/io.h:104:53: note: expanded from macro 'insb' 104 | #define insb(addr, buffer, count) __insb(PCI_IOBASE + (addr), buffer, count) | ~~~~~~~~~~ ^ In file included from drivers/iio/industrialio-buffer.c:18: In file included from include/linux/dma-buf.h:16: In file included from include/linux/iosys-map.h:10: In file included from include/linux/io.h:12: In file included from arch/riscv/include/asm/io.h:136: include/asm-generic/io.h:812:2: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] 812 | insw(addr, buffer, count); | ^~~~~~~~~~~~~~~~~~~~~~~~~ arch/riscv/include/asm/io.h:105:53: note: expanded from macro 'insw' 105 | #define insw(addr, buffer, count) __insw(PCI_IOBASE + (addr), buffer, count) | ~~~~~~~~~~ ^ In file included from drivers/iio/industrialio-buffer.c:18: In file included from include/linux/dma-buf.h:16: In file included from include/linux/iosys-map.h:10: In file included from include/linux/io.h:12: In file included from arch/riscv/include/asm/io.h:136: include/asm-generic/io.h:820:2: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] 820 | insl(addr, buffer, count); | ^~~~~~~~~~~~~~~~~~~~~~~~~ arch/riscv/include/asm/io.h:106:53: note: expanded from macro 'insl' 106 | #define insl(addr, buffer, count) __insl(PCI_IOBASE + (addr), buffer, count) | ~~~~~~~~~~ ^ In file included from drivers/iio/industrialio-buffer.c:18: In file included from include/linux/dma-buf.h:16: In file included from include/linux/iosys-map.h:10: In file included from include/linux/io.h:12: In file included from arch/riscv/include/asm/io.h:136: include/asm-generic/io.h:829:2: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] 829 | outsb(addr, buffer, count); | ^~~~~~~~~~~~~~~~~~~~~~~~~~ arch/riscv/include/asm/io.h:118:55: note: expanded from macro 'outsb' 118 | #define outsb(addr, buffer, count) __outsb(PCI_IOBASE + (addr), buffer, count) | ~~~~~~~~~~ ^ In file included from drivers/iio/industrialio-buffer.c:18: In file included from include/linux/dma-buf.h:16: In file included from include/linux/iosys-map.h:10: In file included from include/linux/io.h:12: In file included from arch/riscv/include/asm/io.h:136: include/asm-generic/io.h:838:2: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] 838 | outsw(addr, buffer, count); | ^~~~~~~~~~~~~~~~~~~~~~~~~~ arch/riscv/include/asm/io.h:119:55: note: expanded from macro 'outsw' 119 | #define outsw(addr, buffer, count) __outsw(PCI_IOBASE + (addr), buffer, count) | ~~~~~~~~~~ ^ In file included from drivers/iio/industrialio-buffer.c:18: In file included from include/linux/dma-buf.h:16: In file included from include/linux/iosys-map.h:10: In file included from include/linux/io.h:12: In file included from arch/riscv/include/asm/io.h:136: include/asm-generic/io.h:847:2: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] 847 | outsl(addr, buffer, count); | ^~~~~~~~~~~~~~~~~~~~~~~~~~ arch/riscv/include/asm/io.h:120:55: note: expanded from macro 'outsl' 120 | #define outsl(addr, buffer, count) __outsl(PCI_IOBASE + (addr), buffer, count) | ~~~~~~~~~~ ^ In file included from drivers/iio/industrialio-buffer.c:18: In file included from include/linux/dma-buf.h:16: In file included from include/linux/iosys-map.h:10: In file included from include/linux/io.h:12: In file included from arch/riscv/include/asm/io.h:136: include/asm-generic/io.h:1175:55: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] 1175 | return (port > MMIO_UPPER_LIMIT) ? NULL : PCI_IOBASE + port; | ~~~~~~~~~~ ^ In file included from drivers/iio/industrialio-buffer.c:29: >> include/linux/iio/iio.h:813:16: error: static assertion failed due to requirement 'sizeof (__alignof(unsigned long long)) % sizeof(long long) == 0': macros above assume that IIO_DMA_MINALIGN also ensures s64 timestamp alignment 813 | _Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ include/linux/iio/iio.h:813:55: note: expression evaluates to '4 == 0' 813 | _Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~ 7 warnings and 1 error generated. -- In file included from drivers/iio/industrialio-sw-device.c:14: In file included from include/linux/iio/sw_device.h:13: >> include/linux/iio/iio.h:813:16: error: static assertion failed due to requirement 'sizeof (__alignof(unsigned long long)) % sizeof(long long) == 0': macros above assume that IIO_DMA_MINALIGN also ensures s64 timestamp alignment 813 | _Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ include/linux/iio/iio.h:813:55: note: expression evaluates to '4 == 0' 813 | _Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~ 1 error generated. -- In file included from drivers/iio/industrialio-triggered-event.c:9: >> include/linux/iio/iio.h:813:16: error: static assertion failed due to requirement 'sizeof (__alignof(unsigned long long)) % sizeof(long long) == 0': macros above assume that IIO_DMA_MINALIGN also ensures s64 timestamp alignment 813 | _Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ include/linux/iio/iio.h:813:55: note: expression evaluates to '4 == 0' 813 | _Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~ In file included from drivers/iio/industrialio-triggered-event.c:10: In file included from include/linux/iio/triggered_event.h:5: In file included from include/linux/interrupt.h:11: In file included from include/linux/hardirq.h:11: In file included from ./arch/riscv/include/generated/asm/hardirq.h:1: In file included from include/asm-generic/hardirq.h:17: In file included from include/linux/irq.h:20: In file included from include/linux/io.h:12: In file included from arch/riscv/include/asm/io.h:136: include/asm-generic/io.h:804:2: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] 804 | insb(addr, buffer, count); | ^~~~~~~~~~~~~~~~~~~~~~~~~ arch/riscv/include/asm/io.h:104:53: note: expanded from macro 'insb' 104 | #define insb(addr, buffer, count) __insb(PCI_IOBASE + (addr), buffer, count) | ~~~~~~~~~~ ^ In file included from drivers/iio/industrialio-triggered-event.c:10: In file included from include/linux/iio/triggered_event.h:5: In file included from include/linux/interrupt.h:11: In file included from include/linux/hardirq.h:11: In file included from ./arch/riscv/include/generated/asm/hardirq.h:1: In file included from include/asm-generic/hardirq.h:17: In file included from include/linux/irq.h:20: In file included from include/linux/io.h:12: In file included from arch/riscv/include/asm/io.h:136: include/asm-generic/io.h:812:2: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] 812 | insw(addr, buffer, count); | ^~~~~~~~~~~~~~~~~~~~~~~~~ arch/riscv/include/asm/io.h:105:53: note: expanded from macro 'insw' 105 | #define insw(addr, buffer, count) __insw(PCI_IOBASE + (addr), buffer, count) | ~~~~~~~~~~ ^ In file included from drivers/iio/industrialio-triggered-event.c:10: In file included from include/linux/iio/triggered_event.h:5: In file included from include/linux/interrupt.h:11: In file included from include/linux/hardirq.h:11: In file included from ./arch/riscv/include/generated/asm/hardirq.h:1: In file included from include/asm-generic/hardirq.h:17: In file included from include/linux/irq.h:20: In file included from include/linux/io.h:12: In file included from arch/riscv/include/asm/io.h:136: include/asm-generic/io.h:820:2: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] 820 | insl(addr, buffer, count); | ^~~~~~~~~~~~~~~~~~~~~~~~~ arch/riscv/include/asm/io.h:106:53: note: expanded from macro 'insl' 106 | #define insl(addr, buffer, count) __insl(PCI_IOBASE + (addr), buffer, count) | ~~~~~~~~~~ ^ In file included from drivers/iio/industrialio-triggered-event.c:10: In file included from include/linux/iio/triggered_event.h:5: In file included from include/linux/interrupt.h:11: In file included from include/linux/hardirq.h:11: In file included from ./arch/riscv/include/generated/asm/hardirq.h:1: In file included from include/asm-generic/hardirq.h:17: In file included from include/linux/irq.h:20: In file included from include/linux/io.h:12: In file included from arch/riscv/include/asm/io.h:136: include/asm-generic/io.h:829:2: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] 829 | outsb(addr, buffer, count); | ^~~~~~~~~~~~~~~~~~~~~~~~~~ arch/riscv/include/asm/io.h:118:55: note: expanded from macro 'outsb' 118 | #define outsb(addr, buffer, count) __outsb(PCI_IOBASE + (addr), buffer, count) | ~~~~~~~~~~ ^ In file included from drivers/iio/industrialio-triggered-event.c:10: In file included from include/linux/iio/triggered_event.h:5: In file included from include/linux/interrupt.h:11: In file included from include/linux/hardirq.h:11: In file included from ./arch/riscv/include/generated/asm/hardirq.h:1: In file included from include/asm-generic/hardirq.h:17: In file included from include/linux/irq.h:20: In file included from include/linux/io.h:12: In file included from arch/riscv/include/asm/io.h:136: include/asm-generic/io.h:838:2: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] 838 | outsw(addr, buffer, count); | ^~~~~~~~~~~~~~~~~~~~~~~~~~ arch/riscv/include/asm/io.h:119:55: note: expanded from macro 'outsw' 119 | #define outsw(addr, buffer, count) __outsw(PCI_IOBASE + (addr), buffer, count) | ~~~~~~~~~~ ^ In file included from drivers/iio/industrialio-triggered-event.c:10: In file included from include/linux/iio/triggered_event.h:5: In file included from include/linux/interrupt.h:11: In file included from include/linux/hardirq.h:11: In file included from ./arch/riscv/include/generated/asm/hardirq.h:1: In file included from include/asm-generic/hardirq.h:17: In file included from include/linux/irq.h:20: In file included from include/linux/io.h:12: In file included from arch/riscv/include/asm/io.h:136: include/asm-generic/io.h:847:2: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] 847 | outsl(addr, buffer, count); | ^~~~~~~~~~~~~~~~~~~~~~~~~~ arch/riscv/include/asm/io.h:120:55: note: expanded from macro 'outsl' 120 | #define outsl(addr, buffer, count) __outsl(PCI_IOBASE + (addr), buffer, count) | ~~~~~~~~~~ ^ In file included from drivers/iio/industrialio-triggered-event.c:10: In file included from include/linux/iio/triggered_event.h:5: In file included from include/linux/interrupt.h:11: In file included from include/linux/hardirq.h:11: In file included from ./arch/riscv/include/generated/asm/hardirq.h:1: vim +813 include/linux/iio/iio.h 781 782 #define _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ 783 type name[ALIGN((count), sizeof(s64) / sizeof(type)) + sizeof(s64) / sizeof(type)] 784 785 /** 786 * IIO_DECLARE_BUFFER_WITH_TS() - Declare a buffer with timestamp 787 * @type: element type of the buffer 788 * @name: identifier name of the buffer 789 * @count: number of elements in the buffer 790 * 791 * Declares a buffer that is safe to use with iio_push_to_buffer_with_ts(). In 792 * addition to allocating enough space for @count elements of @type, it also 793 * allocates space for a s64 timestamp at the end of the buffer and ensures 794 * proper alignment of the timestamp. 795 */ 796 #define IIO_DECLARE_BUFFER_WITH_TS(type, name, count) \ 797 _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(sizeof(s64)) 798 799 /** 800 * IIO_DECLARE_DMA_BUFFER_WITH_TS() - Declare a DMA-aligned buffer with timestamp 801 * @type: element type of the buffer 802 * @name: identifier name of the buffer 803 * @count: number of elements in the buffer 804 * 805 * Same as IIO_DECLARE_BUFFER_WITH_TS(), but is uses __aligned(IIO_DMA_MINALIGN) 806 * to ensure that the buffer doesn't share cachelines with anything that comes 807 * before it in a struct. This should not be used for stack-allocated buffers 808 * as stack memory cannot generally be used for DMA. 809 */ 810 #define IIO_DECLARE_DMA_BUFFER_WITH_TS(type, name, count) \ 811 _IIO_DECLARE_BUFFER_WITH_TS(type, name, count) __aligned(IIO_DMA_MINALIGN) 812 > 813 _Static_assert(sizeof(IIO_DMA_MINALIGN) % sizeof(s64) == 0, 814 "macros above assume that IIO_DMA_MINALIGN also ensures s64 timestamp alignment"); 815 -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki ^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v2 2/6] iio: adc: ad4695: use u16 for buffer elements 2025-04-22 22:07 [PATCH v2 0/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS David Lechner 2025-04-22 22:07 ` [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros David Lechner @ 2025-04-22 22:07 ` David Lechner 2025-04-22 22:07 ` [PATCH v2 3/6] iio: adc: ad4695: use IIO_DECLARE_BUFFER_WITH_TS David Lechner ` (3 subsequent siblings) 5 siblings, 0 replies; 21+ messages in thread From: David Lechner @ 2025-04-22 22:07 UTC (permalink / raw) To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea Cc: linux-iio, linux-kernel, linux-arm-kernel Change the type of the buffer elements to u16 since we currently only support 16-bit word size. The code was originally written to also allow for 32-bit word size when oversampling is enabled, but so far, oversampling is only implemented when using SPI offload and therefore doesn't use this buffer. AD4695_MAX_CHANNEL_SIZE macro is dropped since it no longer adds any value. AD4695_MAX_CHANNELS + 2 is changed to AD4695_MAX_CHANNELS + 1 because previously we were overallocating. AD4695_MAX_CHANNELS is the number of of voltage channels and + 1 is for the temperature channel. Signed-off-by: David Lechner <dlechner@baylibre.com> --- drivers/iio/adc/ad4695.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/drivers/iio/adc/ad4695.c b/drivers/iio/adc/ad4695.c index 68c6625db0d75f4cade7cb029e94191118dbcaa0..0c633d43e480d5404074e9fa35f1d330b448f0a2 100644 --- a/drivers/iio/adc/ad4695.c +++ b/drivers/iio/adc/ad4695.c @@ -106,8 +106,6 @@ /* Max number of voltage input channels. */ #define AD4695_MAX_CHANNELS 16 -/* Max size of 1 raw sample in bytes. */ -#define AD4695_MAX_CHANNEL_SIZE 2 enum ad4695_in_pair { AD4695_IN_PAIR_REFGND, @@ -162,8 +160,8 @@ struct ad4695_state { struct spi_transfer buf_read_xfer[AD4695_MAX_CHANNELS * 2 + 3]; struct spi_message buf_read_msg; /* Raw conversion data received. */ - u8 buf[ALIGN((AD4695_MAX_CHANNELS + 2) * AD4695_MAX_CHANNEL_SIZE, - sizeof(s64)) + sizeof(s64)] __aligned(IIO_DMA_MINALIGN); + u16 buf[ALIGN((AD4695_MAX_CHANNELS + 1) * sizeof(u16), + sizeof(s64)) + sizeof(s64)] __aligned(IIO_DMA_MINALIGN); u16 raw_data; /* Commands to send for single conversion. */ u16 cnv_cmd; @@ -660,9 +658,8 @@ static int ad4695_buffer_preenable(struct iio_dev *indio_dev) iio_for_each_active_channel(indio_dev, bit) { xfer = &st->buf_read_xfer[num_xfer]; xfer->bits_per_word = 16; - xfer->rx_buf = &st->buf[rx_buf_offset]; + xfer->rx_buf = &st->buf[rx_buf_offset++]; xfer->len = 2; - rx_buf_offset += xfer->len; if (bit == temp_chan_bit) { temp_en = 1; -- 2.43.0 ^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH v2 3/6] iio: adc: ad4695: use IIO_DECLARE_BUFFER_WITH_TS 2025-04-22 22:07 [PATCH v2 0/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS David Lechner 2025-04-22 22:07 ` [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros David Lechner 2025-04-22 22:07 ` [PATCH v2 2/6] iio: adc: ad4695: use u16 for buffer elements David Lechner @ 2025-04-22 22:07 ` David Lechner 2025-04-22 22:34 ` Andy Shevchenko 2025-04-22 22:07 ` [PATCH v2 4/6] iio: adc: ad7380: " David Lechner ` (2 subsequent siblings) 5 siblings, 1 reply; 21+ messages in thread From: David Lechner @ 2025-04-22 22:07 UTC (permalink / raw) To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea Cc: linux-iio, linux-kernel, linux-arm-kernel Use IIO_DECLARE_BUFFER_WITH_TS to declare the buffer that gets used with iio_push_to_buffers_with_ts(). This makes the code a bit easier to read and understand. Signed-off-by: David Lechner <dlechner@baylibre.com> --- drivers/iio/adc/ad4695.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/iio/adc/ad4695.c b/drivers/iio/adc/ad4695.c index 0c633d43e480d5404074e9fa35f1d330b448f0a2..992abf6c63b51dee222caf624e172455fb9b9900 100644 --- a/drivers/iio/adc/ad4695.c +++ b/drivers/iio/adc/ad4695.c @@ -160,8 +160,7 @@ struct ad4695_state { struct spi_transfer buf_read_xfer[AD4695_MAX_CHANNELS * 2 + 3]; struct spi_message buf_read_msg; /* Raw conversion data received. */ - u16 buf[ALIGN((AD4695_MAX_CHANNELS + 1) * sizeof(u16), - sizeof(s64)) + sizeof(s64)] __aligned(IIO_DMA_MINALIGN); + IIO_DECLARE_DMA_BUFFER_WITH_TS(u16, buf, AD4695_MAX_CHANNELS + 1); u16 raw_data; /* Commands to send for single conversion. */ u16 cnv_cmd; -- 2.43.0 ^ permalink raw reply related [flat|nested] 21+ messages in thread
* Re: [PATCH v2 3/6] iio: adc: ad4695: use IIO_DECLARE_BUFFER_WITH_TS 2025-04-22 22:07 ` [PATCH v2 3/6] iio: adc: ad4695: use IIO_DECLARE_BUFFER_WITH_TS David Lechner @ 2025-04-22 22:34 ` Andy Shevchenko 0 siblings, 0 replies; 21+ messages in thread From: Andy Shevchenko @ 2025-04-22 22:34 UTC (permalink / raw) To: David Lechner Cc: Jonathan Cameron, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea, linux-iio, linux-kernel, linux-arm-kernel On Wed, Apr 23, 2025 at 1:08 AM David Lechner <dlechner@baylibre.com> wrote: > > Use IIO_DECLARE_BUFFER_WITH_TS to declare the buffer that gets used with I would use the IIO_DECLARE_BUFFER_WITH_TS() form since it's a macro with parameters. > iio_push_to_buffers_with_ts(). This makes the code a bit easier to read > and understand. -- With Best Regards, Andy Shevchenko ^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v2 4/6] iio: adc: ad7380: use IIO_DECLARE_BUFFER_WITH_TS 2025-04-22 22:07 [PATCH v2 0/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS David Lechner ` (2 preceding siblings ...) 2025-04-22 22:07 ` [PATCH v2 3/6] iio: adc: ad4695: use IIO_DECLARE_BUFFER_WITH_TS David Lechner @ 2025-04-22 22:07 ` David Lechner 2025-04-22 22:33 ` Andy Shevchenko 2025-04-22 22:07 ` [PATCH v2 5/6] iio: accel: sca3300: " David Lechner 2025-04-22 22:07 ` [PATCH v2 6/6] iio: adc: at91-sama5d2: " David Lechner 5 siblings, 1 reply; 21+ messages in thread From: David Lechner @ 2025-04-22 22:07 UTC (permalink / raw) To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea Cc: linux-iio, linux-kernel, linux-arm-kernel Use IIO_DECLARE_BUFFER_WITH_TS to declare the buffer that gets used with iio_push_to_buffers_with_ts(). This makes the code a bit easier to read and understand. Signed-off-by: David Lechner <dlechner@baylibre.com> --- As discussed in v1, this one stays u8 because it is used with both 16 and 32-bit word sizes. --- drivers/iio/adc/ad7380.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/iio/adc/ad7380.c b/drivers/iio/adc/ad7380.c index f93e6c67766aa89b18c1a7dec02ae8912f65261c..f89b195c644024151c14977fd43e279a67439fb1 100644 --- a/drivers/iio/adc/ad7380.c +++ b/drivers/iio/adc/ad7380.c @@ -909,8 +909,8 @@ struct ad7380_state { * Make the buffer large enough for MAX_NUM_CHANNELS 32-bit samples and * one 64-bit aligned 64-bit timestamp. */ - u8 scan_data[ALIGN(MAX_NUM_CHANNELS * sizeof(u32), sizeof(s64)) - + sizeof(s64)] __aligned(IIO_DMA_MINALIGN); + IIO_DECLARE_BUFFER_WITH_TS(u8, scan_data, MAX_NUM_CHANNELS * sizeof(u32)) + __aligned(IIO_DMA_MINALIGN); /* buffers for reading/writing registers */ u16 tx; u16 rx; -- 2.43.0 ^ permalink raw reply related [flat|nested] 21+ messages in thread
* Re: [PATCH v2 4/6] iio: adc: ad7380: use IIO_DECLARE_BUFFER_WITH_TS 2025-04-22 22:07 ` [PATCH v2 4/6] iio: adc: ad7380: " David Lechner @ 2025-04-22 22:33 ` Andy Shevchenko 2025-04-22 22:41 ` David Lechner 0 siblings, 1 reply; 21+ messages in thread From: Andy Shevchenko @ 2025-04-22 22:33 UTC (permalink / raw) To: David Lechner Cc: Jonathan Cameron, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea, linux-iio, linux-kernel, linux-arm-kernel On Wed, Apr 23, 2025 at 1:08 AM David Lechner <dlechner@baylibre.com> wrote: > > Use IIO_DECLARE_BUFFER_WITH_TS to declare the buffer that gets used with > iio_push_to_buffers_with_ts(). This makes the code a bit easier to read > and understand. ... > + IIO_DECLARE_BUFFER_WITH_TS(u8, scan_data, MAX_NUM_CHANNELS * sizeof(u32)) Btw, why not DECLARE_IIO_...() as other DECLARE_*() look like? > + __aligned(IIO_DMA_MINALIGN); Forgot to drop and use the DMA variant? -- With Best Regards, Andy Shevchenko ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH v2 4/6] iio: adc: ad7380: use IIO_DECLARE_BUFFER_WITH_TS 2025-04-22 22:33 ` Andy Shevchenko @ 2025-04-22 22:41 ` David Lechner 2025-04-22 22:53 ` Andy Shevchenko 0 siblings, 1 reply; 21+ messages in thread From: David Lechner @ 2025-04-22 22:41 UTC (permalink / raw) To: Andy Shevchenko Cc: Jonathan Cameron, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea, linux-iio, linux-kernel, linux-arm-kernel On 4/22/25 5:33 PM, Andy Shevchenko wrote: > On Wed, Apr 23, 2025 at 1:08 AM David Lechner <dlechner@baylibre.com> wrote: >> >> Use IIO_DECLARE_BUFFER_WITH_TS to declare the buffer that gets used with >> iio_push_to_buffers_with_ts(). This makes the code a bit easier to read >> and understand. > > ... > >> + IIO_DECLARE_BUFFER_WITH_TS(u8, scan_data, MAX_NUM_CHANNELS * sizeof(u32)) > > Btw, why not DECLARE_IIO_...() as other DECLARE_*() look like? IMHO, namespace should always go first and people who write DECLARE_NS_... are doing it wrong. :-) There is not existing DECLARE_IIO_ to match anyway. > >> + __aligned(IIO_DMA_MINALIGN); > > Forgot to drop and use the DMA variant? > oops! ^ permalink raw reply [flat|nested] 21+ messages in thread
* Re: [PATCH v2 4/6] iio: adc: ad7380: use IIO_DECLARE_BUFFER_WITH_TS 2025-04-22 22:41 ` David Lechner @ 2025-04-22 22:53 ` Andy Shevchenko 0 siblings, 0 replies; 21+ messages in thread From: Andy Shevchenko @ 2025-04-22 22:53 UTC (permalink / raw) To: David Lechner Cc: Jonathan Cameron, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea, linux-iio, linux-kernel, linux-arm-kernel On Wed, Apr 23, 2025 at 1:41 AM David Lechner <dlechner@baylibre.com> wrote: > On 4/22/25 5:33 PM, Andy Shevchenko wrote: > > On Wed, Apr 23, 2025 at 1:08 AM David Lechner <dlechner@baylibre.com> wrote: ... > >> + IIO_DECLARE_BUFFER_WITH_TS(u8, scan_data, MAX_NUM_CHANNELS * sizeof(u32)) > > > > Btw, why not DECLARE_IIO_...() as other DECLARE_*() look like? > > IMHO, namespace should always go first and people who write DECLARE_NS_... are > doing it wrong. :-) Not really. AFAICT it depends on the globality of the macro. Those, which are defined in types.h are all DECLARE_something(). Which makes sense. So the Q here is if the IIO macros like these ever go out for a wider audience. But in any case this can be amended later (with maybe a bit of additional churn). > There is not existing DECLARE_IIO_ to match anyway. True. -- With Best Regards, Andy Shevchenko ^ permalink raw reply [flat|nested] 21+ messages in thread
* [PATCH v2 5/6] iio: accel: sca3300: use IIO_DECLARE_BUFFER_WITH_TS 2025-04-22 22:07 [PATCH v2 0/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS David Lechner ` (3 preceding siblings ...) 2025-04-22 22:07 ` [PATCH v2 4/6] iio: adc: ad7380: " David Lechner @ 2025-04-22 22:07 ` David Lechner 2025-04-22 22:07 ` [PATCH v2 6/6] iio: adc: at91-sama5d2: " David Lechner 5 siblings, 0 replies; 21+ messages in thread From: David Lechner @ 2025-04-22 22:07 UTC (permalink / raw) To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea Cc: linux-iio, linux-kernel, linux-arm-kernel Use IIO_DECLARE_BUFFER_WITH_TS to declare the buffer that gets used with iio_push_to_buffers_with_ts(). This makes the code a bit easier to read and understand. Signed-off-by: David Lechner <dlechner@baylibre.com> --- This is an alternative to [1]. Also, this serves as a test to see if we can get a rule of thumb to decide how much is too much to put on the stack vs. needing to put the buffer in a static struct. SCA3300_SCAN_MAX is 7, so this add a bit over 64 bytes to the stack, make the stack now roughly double what it was before. [1]: https://lore.kernel.org/linux-iio/20250418-iio-prefer-aligned_s64-timestamp-v1-1-4c6080710516@baylibre.com/ --- drivers/iio/accel/sca3300.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/drivers/iio/accel/sca3300.c b/drivers/iio/accel/sca3300.c index 1132bbaba75bcca525fac2f3e19f63546380fd4f..67416a406e2f43e4e417210410904d44c93111d2 100644 --- a/drivers/iio/accel/sca3300.c +++ b/drivers/iio/accel/sca3300.c @@ -58,15 +58,6 @@ enum sca3300_scan_indexes { SCA3300_SCAN_MAX }; -/* - * Buffer size max case: - * Three accel channels, two bytes per channel. - * Temperature channel, two bytes. - * Three incli channels, two bytes per channel. - * Timestamp channel, eight bytes. - */ -#define SCA3300_MAX_BUFFER_SIZE (ALIGN(sizeof(s16) * SCA3300_SCAN_MAX, sizeof(s64)) + sizeof(s64)) - #define SCA3300_ACCEL_CHANNEL(index, reg, axis) { \ .type = IIO_ACCEL, \ .address = reg, \ @@ -193,9 +184,6 @@ struct sca3300_chip_info { * @spi: SPI device structure * @lock: Data buffer lock * @chip: Sensor chip specific information - * @buffer: Triggered buffer: - * -SCA3300: 4 channel 16-bit data + 64-bit timestamp - * -SCL3300: 7 channel 16-bit data + 64-bit timestamp * @txbuf: Transmit buffer * @rxbuf: Receive buffer */ @@ -203,7 +191,6 @@ struct sca3300_data { struct spi_device *spi; struct mutex lock; const struct sca3300_chip_info *chip; - u8 buffer[SCA3300_MAX_BUFFER_SIZE] __aligned(sizeof(s64)); u8 txbuf[4] __aligned(IIO_DMA_MINALIGN); u8 rxbuf[4]; }; @@ -492,7 +479,7 @@ static irqreturn_t sca3300_trigger_handler(int irq, void *p) struct iio_dev *indio_dev = pf->indio_dev; struct sca3300_data *data = iio_priv(indio_dev); int bit, ret, val, i = 0; - s16 *channels = (s16 *)data->buffer; + IIO_DECLARE_BUFFER_WITH_TS(s16, channels, SCA3300_SCAN_MAX); iio_for_each_active_channel(indio_dev, bit) { ret = sca3300_read_reg(data, indio_dev->channels[bit].address, &val); @@ -505,8 +492,7 @@ static irqreturn_t sca3300_trigger_handler(int irq, void *p) channels[i++] = val; } - iio_push_to_buffers_with_ts(indio_dev, data->buffer, - sizeof(data->buffer), + iio_push_to_buffers_with_ts(indio_dev, channels, sizeof(channels), iio_get_time_ns(indio_dev)); out: iio_trigger_notify_done(indio_dev->trig); -- 2.43.0 ^ permalink raw reply related [flat|nested] 21+ messages in thread
* [PATCH v2 6/6] iio: adc: at91-sama5d2: use IIO_DECLARE_BUFFER_WITH_TS 2025-04-22 22:07 [PATCH v2 0/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS David Lechner ` (4 preceding siblings ...) 2025-04-22 22:07 ` [PATCH v2 5/6] iio: accel: sca3300: " David Lechner @ 2025-04-22 22:07 ` David Lechner 5 siblings, 0 replies; 21+ messages in thread From: David Lechner @ 2025-04-22 22:07 UTC (permalink / raw) To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko, Lars-Peter Clausen, Michael Hennerich, Eugen Hristev, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea Cc: linux-iio, linux-kernel, linux-arm-kernel Use IIO_DECLARE_BUFFER_WITH_TS to declare the buffer that gets used with iio_push_to_buffers_with_ts(). This makes the code a bit easier to read and understand. Signed-off-by: David Lechner <dlechner@baylibre.com> --- This is an alternative to [1]. [1]: https://lore.kernel.org/linux-iio/20250418-iio-prefer-aligned_s64-timestamp-v1-2-4c6080710516@baylibre.com/ --- drivers/iio/adc/at91-sama5d2_adc.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/drivers/iio/adc/at91-sama5d2_adc.c b/drivers/iio/adc/at91-sama5d2_adc.c index 414610afcb2c4128a63cf76767803c32cb01ac5e..4ebaeb41aa4568e2461506471af0540af9d1a041 100644 --- a/drivers/iio/adc/at91-sama5d2_adc.c +++ b/drivers/iio/adc/at91-sama5d2_adc.c @@ -586,15 +586,6 @@ struct at91_adc_temp { u16 saved_oversampling; }; -/* - * Buffer size requirements: - * No channels * bytes_per_channel(2) + timestamp bytes (8) - * Divided by 2 because we need half words. - * We assume 32 channels for now, has to be increased if needed. - * Nobody minds a buffer being too big. - */ -#define AT91_BUFFER_MAX_HWORDS ((32 * 2 + 8) / 2) - struct at91_adc_state { void __iomem *base; int irq; @@ -616,8 +607,8 @@ struct at91_adc_state { struct at91_adc_temp temp_st; struct iio_dev *indio_dev; struct device *dev; - /* Ensure naturally aligned timestamp */ - u16 buffer[AT91_BUFFER_MAX_HWORDS] __aligned(8); + /* We assume 32 channels for now, has to be increased if needed.*/ + IIO_DECLARE_BUFFER_WITH_TS(u16, buffer, 32); /* * lock to prevent concurrent 'single conversion' requests through * sysfs. -- 2.43.0 ^ permalink raw reply related [flat|nested] 21+ messages in thread
end of thread, other threads:[~2025-04-26 11:19 UTC | newest] Thread overview: 21+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2025-04-22 22:07 [PATCH v2 0/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS David Lechner 2025-04-22 22:07 ` [PATCH v2 1/6] iio: introduce IIO_DECLARE_BUFFER_WITH_TS macros David Lechner 2025-04-22 22:30 ` Andy Shevchenko 2025-04-22 22:37 ` David Lechner 2025-04-22 22:50 ` Andy Shevchenko 2025-04-23 9:18 ` Nuno Sá 2025-04-23 14:51 ` David Lechner 2025-04-23 15:43 ` Andy Shevchenko 2025-04-24 7:44 ` Nuno Sá 2025-04-26 11:19 ` Jonathan Cameron 2025-04-23 17:25 ` kernel test robot 2025-04-23 17:46 ` kernel test robot 2025-04-22 22:07 ` [PATCH v2 2/6] iio: adc: ad4695: use u16 for buffer elements David Lechner 2025-04-22 22:07 ` [PATCH v2 3/6] iio: adc: ad4695: use IIO_DECLARE_BUFFER_WITH_TS David Lechner 2025-04-22 22:34 ` Andy Shevchenko 2025-04-22 22:07 ` [PATCH v2 4/6] iio: adc: ad7380: " David Lechner 2025-04-22 22:33 ` Andy Shevchenko 2025-04-22 22:41 ` David Lechner 2025-04-22 22:53 ` Andy Shevchenko 2025-04-22 22:07 ` [PATCH v2 5/6] iio: accel: sca3300: " David Lechner 2025-04-22 22:07 ` [PATCH v2 6/6] iio: adc: at91-sama5d2: " David Lechner
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox