counter: fix privdata alignment
authorNuno Sa <nuno.sa@analog.com>
Fri, 9 Feb 2024 15:50:34 +0000 (16:50 +0100)
committerWilliam Breathitt Gray <wbg@ishicorp.com>
Fri, 16 Feb 2024 23:51:00 +0000 (18:51 -0500)
Aligning to the L1 cache does not guarantee the same alignment as
kmallocing an object [1]. Furthermore, in some platforms, that
alignment is not sufficient for DMA safety (in case someone wants
to have a DMA safe buffer in privdata) [2].

Sometime ago, we had the same fixes in IIO.

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/base/devres.c#n35
[2]: https://lore.kernel.org/linux-iio/20220508175712.647246-2-jic23@kernel.org/

Fixes: c18e2760308e ("counter: Provide alternative counter registration functions")
Signed-off-by: Nuno Sa <nuno.sa@analog.com>
Link: https://lore.kernel.org/r/20240209-counter-align-fix-v2-1-5777ea0a2722@analog.com
Signed-off-by: William Breathitt Gray <william.gray@linaro.org>
drivers/counter/counter-core.c

index 09c77afb33ca84e79c077c87659252c64840929a..3f24481fc04a1258624020a9a919f2d10640057a 100644 (file)
@@ -31,10 +31,11 @@ struct counter_device_allochelper {
        struct counter_device counter;
 
        /*
-        * This is cache line aligned to ensure private data behaves like if it
-        * were kmalloced separately.
+        * This ensures private data behaves like if it were kmalloced
+        * separately. Also ensures the minimum alignment for safe DMA
+        * operations (which may or may not mean cache alignment).
         */
-       unsigned long privdata[] ____cacheline_aligned;
+       unsigned long privdata[] __aligned(ARCH_DMA_MINALIGN);
 };
 
 static void counter_device_release(struct device *dev)