Skip to contentSkip to conversation

Input: Fix insufficent DMA alignment.

PATCH In review Jonathan Cameron 5 participants 20 messages Nov 27, 2022 – Nov 30, 2022

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover Input: Fix insufficent DMA alignment.

From: Jonathan Cameron <[email protected]>

This problem was discovered in IIO as a side effect of the discussions about relaxing kmalloc alignment on arm64 and resulted in a series of large patch sets.

https://lore.kernel.org/linux-iio/[email protected]/

Unsurprisingly there are cases of it in other subsystems.

The short version of this is that there are a few known arm64 chips where ___cacheline_aligned enforces 64 byte alignment which is what we typically want for performance optimization as the size of the L1 cache lines. However, further out in the cache hierarchy we have caches with 128 byte lines. Those are the ones that matter for DMA safety.
So we need the larger alignment guarantees of ARCH_KMALLOC_MINALIGN which in this case is 128 bytes.

There is one other use of ____cacheline_aligned in input:
joystick/iforce/iforce-usb.c

Whilst suspicious I'm not sure enough of the requirements of USB to know if they are there for DMA safety or some other constraint.

Jonathan

Cc Daniel Mack <[email protected]>
Cc Michael Hennerich <[email protected]>
Cc Tomohiro Yoshidomi <[email protected]>
Cc Javier Martinez Canillas <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Benjamin Tissoires <[email protected]>
Cc Lauri Kasanen <[email protected]>
Cc Daniel Hung-yu Wu <[email protected]>

Jonathan Cameron (9):
Input: psxpad - Fix padding for DMA safe buffers.
Input: ad714x - Fix padding for DMA safe buffers.
Input: ad7887 - Fix padding for DMA safe buffers.
Input: ads7846 - Fix padding for DMA safe buffers.
Input: cyttsp - Fix padding for DMA safe buffers.
Input: surface3 - Fix padding for DMA safe buffers.
Input: n64joy - Fix DMA buffer alignment.
Input: atmel_captouch - Avoid suspect DMA buffer alignment.
Input: elants - Fix suspect DMA buffer alignment

 drivers/input/joystick/n64joy.c          | 6 +++---
 drivers/input/joystick/psxpad-spi.c      | 4 ++--
 drivers/input/misc/ad714x.h              | 2 +-
 drivers/input/misc/atmel_captouch.c      | 2 +-
 drivers/input/touchscreen/ad7877.c       | 4 ++--
 drivers/input/touchscreen/ads7846.c      | 4 ++--
 drivers/input/touchscreen/cyttsp_core.h  | 2 +-
 drivers/input/touchscreen/elants_i2c.c   | 2 +-
 drivers/input/touchscreen/surface3_spi.c | 2 +-
 9 files changed, 14 insertions(+), 14 deletions(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH 1/9 Input: psxpad - Fix padding for DMA safe buffers.

From: Jonathan Cameron <[email protected]>

On some architectures (e.g. arm64), ____cachline_aligned only aligns to the cacheline size of the L1 cache size. L1_CACHE_BYTES in arch64/include/asm/cache.h Unfortunately DMA safety on these architectures requires the buffer no share a last level cache cacheline given by ARCH_DMA_MINALIGN which has a greater granularity. ARCH_DMA_MINALIGN is not defined for all architectures, but when it is defined it is used to set the size of ARCH_KMALLOC_MINALIGN to allow DMA safe buffer allocations.

As such the correct alignment requirement is __aligned(ARCH_KMALLOC_MINALIGN).
This has recently been fixed in other subsystems such as IIO.

Fixes 8be193c7b1f4 ("Input: add support for PlayStation 1/2 joypads connected via SPI")
Cc Tomohiro Yoshidomi <[email protected]>
---
 drivers/input/joystick/psxpad-spi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/joystick/psxpad-spi.c +2 -2
index a32656064f39..8098d205b58d 100644
@@ -65,8 +65,8 @@ struct psxpad {
 	bool motor2enable;
 	u8 motor1level;
 	u8 motor2level;
-	u8 sendbuf[0x20] ____cacheline_aligned;
-	u8 response[sizeof(PSX_CMD_POLL)] ____cacheline_aligned;
+	u8 sendbuf[0x20] __aligned(ARCH_KMALLOC_MINALIGN);
+	u8 response[sizeof(PSX_CMD_POLL)] __aligned(ARCH_KMALLOC_MINALIGN);
 };
 
 static int psxpad_command(struct psxpad *pad, const u8 sendcmdlen)
Signature · 1 line
2.38.1
#2 lore raw

PATCH 2/9 Input: ad714x - Fix padding for DMA safe buffers.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

On some architectures (e.g. arm64), ____cachline_aligned only aligns to the cacheline size of the L1 cache size. L1_CACHE_BYTES in arch64/include/asm/cache.h Unfortunately DMA safety on these architectures requires the buffer no share a last level cache cacheline given by ARCH_DMA_MINALIGN which has a greater granularity. ARCH_DMA_MINALIGN is not defined for all architectures, but when it is defined it is used to set the size of ARCH_KMALLOC_MINALIGN to allow DMA safe buffer allocations.

As such the correct alignment requirement is __aligned(ARCH_KMALLOC_MINALIGN).
This has recently been fixed in other subsystems such as IIO.

Fixes tag is inprecise because there may not have been any architectures where the two values were different at the time of the earlier fix.

Fixes c0409feb8689 ("Input: ad714x - use DMA-safe buffers for spi_write()")
Cc Michael Hennerich <[email protected]>
---
 drivers/input/misc/ad714x.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/misc/ad714x.h +1 -1
index af847b5f0d0e..2b8b901183be 100644
@@ -41,7 +41,7 @@ struct ad714x_chip {
 	unsigned product;
 	unsigned version;
 
-	__be16 xfer_buf[16] ____cacheline_aligned;
+	__be16 xfer_buf[16] __aligned(ARCH_KMALLOC_MINALIGN);
 
 };
 
Signature · 1 line
2.38.1
maintainer Acked-by on v1 2/9
#3 lore raw

RE: [PATCH 2/9] Input: ad714x - Fix padding for DMA safe buffers.

Quoted 27 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Sonntag, 27. November 2022 15:41
To: [email protected]; Dmitry Torokhov <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Hennerich, Michael <[email protected]>
Subject: [PATCH 2/9] Input: ad714x - Fix padding for DMA safe buffers.

From: Jonathan Cameron <[email protected]>

On some architectures (e.g. arm64), ____cachline_aligned only aligns to the cacheline size of the L1 cache size. L1_CACHE_BYTES in arch64/include/asm/cache.h Unfortunately DMA safety on these architectures requires the buffer no share a last level cache cacheline given by ARCH_DMA_MINALIGN which has a greater granularity. ARCH_DMA_MINALIGN is not defined for all architectures, but when it is defined it is used to set the size of ARCH_KMALLOC_MINALIGN to allow DMA safe buffer allocations.

As such the correct alignment requirement is __aligned(ARCH_KMALLOC_MINALIGN).
This has recently been fixed in other subsystems such as IIO.

Quoted 6 lines

Fixes tag is inprecise because there may not have been any architectures where the two values were different at the time of the earlier fix.

Fixes c0409feb8689 ("Input: ad714x - use DMA-safe buffers for spi_write()")
Cc Michael Hennerich <[email protected]>
Quoted 19 lines
---
 drivers/input/misc/ad714x.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/misc/ad714x.h index renamed from drivers/input/misc/ad714x.h +0 -0

af847b5f0d0e..2b8b901183be 100644

drivers/input/misc/ad714x.h +1 -2
@@ -41,7 +41,7 @@ struct ad714x_chip {
 	unsigned product;
 	unsigned version;
 
-	__be16 xfer_buf[16] ____cacheline_aligned;
+	__be16 xfer_buf[16] __aligned(ARCH_KMALLOC_MINALIGN);
 
 };
 
--

2.38.1

#4 lore raw

PATCH 3/9 Input: ad7887 - Fix padding for DMA safe buffers.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

On some architectures (e.g. arm64), ____cachline_aligned only aligns to the cacheline size of the L1 cache size. L1_CACHE_BYTES in arch64/include/asm/cache.h Unfortunately DMA safety on these architectures requires the buffer no share a last level cache cacheline given by ARCH_DMA_MINALIGN which has a greater granularity. ARCH_DMA_MINALIGN is not defined for all architectures, but when it is defined it is used to set the size of ARCH_KMALLOC_MINALIGN to allow DMA safe buffer allocations.

As such the correct alignment requirement is __aligned(ARCH_KMALLOC_MINALIGN).
This has recently been fixed in other subsystems such as IIO.

Fixes tag is imprecise as there may have been no architectures where the two alignments differed at the time of that patch.

Fixes 3843384a0554 ("Input: ad7877 - keep dma rx buffers in seperate cache lines")
Cc Michael Hennerich <[email protected]>
---
 drivers/input/touchscreen/ad7877.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/touchscreen/ad7877.c +2 -2
index 08f5372f0bfd..eaf11dffb28e 100644
@@ -150,7 +150,7 @@ struct ser_req {
 	 * DMA (thus cache coherency maintenance) requires the
 	 * transfer buffers to live in their own cache lines.
 	 */
-	u16 sample ____cacheline_aligned;
+	u16 sample __aligned(ARCH_KMALLOC_MINALIGN);
 };
 
 struct ad7877 {
@@ -189,7 +189,7 @@ struct ad7877 {
 	 * DMA (thus cache coherency maintenance) requires the
 	 * transfer buffers to live in their own cache lines.
 	 */
-	u16 conversion_data[AD7877_NR_SENSE] ____cacheline_aligned;
+	u16 conversion_data[AD7877_NR_SENSE] __aligned(ARCH_KMALLOC_MINALIGN);
 };
 
 static bool gpio3;
Signature · 1 line
2.38.1
maintainer Acked-by on v1 3/9
#5 lore raw

RE: [PATCH 3/9] Input: ad7887 - Fix padding for DMA safe buffers.

Quoted 27 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Sonntag, 27. November 2022 15:41
To: [email protected]; Dmitry Torokhov <[email protected]>
Cc: Jonathan Cameron <[email protected]>; Hennerich, Michael <[email protected]>
Subject: [PATCH 3/9] Input: ad7887 - Fix padding for DMA safe buffers.

From: Jonathan Cameron <[email protected]>

On some architectures (e.g. arm64), ____cachline_aligned only aligns to the cacheline size of the L1 cache size. L1_CACHE_BYTES in arch64/include/asm/cache.h Unfortunately DMA safety on these architectures requires the buffer no share a last level cache cacheline given by ARCH_DMA_MINALIGN which has a greater granularity. ARCH_DMA_MINALIGN is not defined for all architectures, but when it is defined it is used to set the size of ARCH_KMALLOC_MINALIGN to allow DMA safe buffer allocations.

As such the correct alignment requirement is __aligned(ARCH_KMALLOC_MINALIGN).
This has recently been fixed in other subsystems such as IIO.

Quoted 7 lines

Fixes tag is imprecise as there may have been no architectures where the two alignments differed at the time of that patch.

Fixes 3843384a0554 ("Input: ad7877 - keep dma rx buffers in seperate cache

lines")

Cc Michael Hennerich <[email protected]>
Quoted 30 lines
---
 drivers/input/touchscreen/ad7877.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/touchscreen/ad7877.c +2 -2
index 08f5372f0bfd..eaf11dffb28e 100644
@@ -150,7 +150,7 @@ struct ser_req {
 	 * DMA (thus cache coherency maintenance) requires the
 	 * transfer buffers to live in their own cache lines.
 	 */
-	u16 sample ____cacheline_aligned;
+	u16 sample __aligned(ARCH_KMALLOC_MINALIGN);
 };
 
 struct ad7877 {
@@ -189,7 +189,7 @@ struct ad7877 {
 	 * DMA (thus cache coherency maintenance) requires the
 	 * transfer buffers to live in their own cache lines.
 	 */
-	u16 conversion_data[AD7877_NR_SENSE] ____cacheline_aligned;
+	u16 conversion_data[AD7877_NR_SENSE]
 __aligned(ARCH_KMALLOC_MINALIGN);
 };
 

static bool gpio3;
--
2.38.1

#6 lore raw

PATCH 4/9 Input: ads7846 - Fix padding for DMA safe buffers.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

On some architectures (e.g. arm64), ____cachline_aligned only aligns to the cacheline size of the L1 cache size. L1_CACHE_BYTES in arch64/include/asm/cache.h Unfortunately DMA safety on these architectures requires the buffer no share a last level cache cacheline given by ARCH_DMA_MINALIGN which has a greater granularity. ARCH_DMA_MINALIGN is not defined for all architectures, but when it is defined it is used to set the size of ARCH_KMALLOC_MINALIGN to allow DMA safe buffer allocations.

As such the correct alignment requirement is __aligned(ARCH_KMALLOC_MINALIGN).
This has recently been fixed in other subsystems such as IIO.

Fixes tag for this is complex as at the time of original introduction, it is likely that there were no cases where the two alignments were different.

Fixes 1dbe7dada2d0 ("Input: ads7846 - make transfer buffers DMA safe")
Cc Daniel Mack <[email protected]>
---
 drivers/input/touchscreen/ads7846.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/touchscreen/ads7846.c +2 -2
index bed68a68f330..074ca9f59788 100644
@@ -337,7 +337,7 @@ struct ser_req {
 	 * DMA (thus cache coherency maintenance) requires the
 	 * transfer buffers to live in their own cache lines.
 	 */
-	__be16 sample ____cacheline_aligned;
+	__be16 sample __aligned(ARCH_KMALLOC_MINALIGN);
 };
 
 struct ads7845_ser_req {
@@ -348,7 +348,7 @@ struct ads7845_ser_req {
 	 * DMA (thus cache coherency maintenance) requires the
 	 * transfer buffers to live in their own cache lines.
 	 */
-	u8 sample[3] ____cacheline_aligned;
+	u8 sample[3] __aligned(ARCH_KMALLOC_MINALIGN);
 };
 
 static int ads7846_read12_ser(struct device *dev, unsigned command)
Signature · 1 line
2.38.1
#7 lore raw

PATCH 5/9 Input: cyttsp - Fix padding for DMA safe buffers.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

On some architectures (e.g. arm64), ____cachline_aligned only aligns to the cacheline size of the L1 cache size. L1_CACHE_BYTES in arch64/include/asm/cache.h Unfortunately DMA safety on these architectures requires the buffer no share a last level cache cacheline given by ARCH_DMA_MINALIGN which has a greater granularity. ARCH_DMA_MINALIGN is not defined for all architectures, but when it is defined it is used to set the size of ARCH_KMALLOC_MINALIGN to allow DMA safe buffer allocations.

As such the correct alignment requirement is __aligned(ARCH_KMALLOC_MINALIGN).
This has recently been fixed in other subsystems such as IIO.

Fixes tag for this is complex as at the time of original introduction, it is likely that there were no cases where the two alignments were different.

Fixes 4065d1e7b216 ("Input: add Cypress TTSP capacitive multi-touch screen support")
Cc Javier Martinez Canillas <[email protected]>
Cc Linus Walleij <[email protected]>
---
 drivers/input/touchscreen/cyttsp_core.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/touchscreen/cyttsp_core.h +1 -1
index 075509e695a2..e87cb323623c 100644
@@ -131,7 +131,7 @@ struct cyttsp {
 	u8 lp_intrvl;
 	u8 *bl_keys;
 
-	u8 xfer_buf[] ____cacheline_aligned;
+	u8 xfer_buf[] __aligned(ARCH_KMALLOC_MINALIGN);
 };
 
 struct cyttsp *cyttsp_probe(const struct cyttsp_bus_ops *bus_ops,
Signature · 1 line
2.38.1
#8 lore raw

PATCH 6/9 Input: surface3 - Fix padding for DMA safe buffers.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

On some architectures (e.g. arm64), ____cachline_aligned only aligns to the cacheline size of the L1 cache size. L1_CACHE_BYTES in arch64/include/asm/cache.h Unfortunately DMA safety on these architectures requires the buffer no share a last level cache cacheline given by ARCH_DMA_MINALIGN which has a greater granularity. ARCH_DMA_MINALIGN is not defined for all architectures, but when it is defined it is used to set the size of ARCH_KMALLOC_MINALIGN to allow DMA safe buffer allocations.

As such the correct alignment requirement is __aligned(ARCH_KMALLOC_MINALIGN).
This has recently been fixed in other subsystems such as IIO.

Presumably this part is little used on boards where this could actually matter so this is mostly about removing code that might be coppied elsewhere.

Fixes 4feacbc24eea ("Input: add new driver for the Surface 3")
Cc Benjamin Tissoires <[email protected]>
---
 drivers/input/touchscreen/surface3_spi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/touchscreen/surface3_spi.c +1 -1
index 1da23e5585a0..6c884fc2b332 100644
@@ -32,7 +32,7 @@ struct surface3_ts_data {
 	struct input_dev *pen_input_dev;
 	int pen_tool;
 
-	u8 rd_buf[SURFACE3_PACKET_SIZE]		____cacheline_aligned;
+	u8 rd_buf[SURFACE3_PACKET_SIZE] __aligned(ARCH_KMALLOC_MINALIGN;
 };
 
 struct surface3_ts_data_finger {
Signature · 1 line
2.38.1
#9 lore raw

Re: [PATCH 6/9] Input: surface3 - Fix padding for DMA safe buffers.

Quoted 15 lines · On Sun, 27 Nov 2022 14:41:13 +0000 Jonathan Cameron <[email protected]> wrote:

From: Jonathan Cameron <[email protected]>

On some architectures (e.g. arm64), ____cachline_aligned only aligns to the cacheline size of the L1 cache size. L1_CACHE_BYTES in arch64/include/asm/cache.h Unfortunately DMA safety on these architectures requires the buffer no share a last level cache cacheline given by ARCH_DMA_MINALIGN which has a greater granularity. ARCH_DMA_MINALIGN is not defined for all architectures, but when it is defined it is used to set the size of ARCH_KMALLOC_MINALIGN to allow DMA safe buffer allocations.

As such the correct alignment requirement is __aligned(ARCH_KMALLOC_MINALIGN).
This has recently been fixed in other subsystems such as IIO.

Quoted 7 lines

Presumably this part is little used on boards where this could actually matter so this is mostly about removing code that might be coppied elsewhere.

Fixes 4feacbc24eea ("Input: add new driver for the Surface 3")
Cc Benjamin Tissoires <[email protected]>

Sigh. Sunday afternoon incompetence on my part...

Dmitry, if everything else is fine with this series I can resend this or if you are feeling generous feel free to fix it up whilst applying ;)

Jonathan

Quoted 9 lines
---
 drivers/input/touchscreen/surface3_spi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/touchscreen/surface3_spi.c +0 -0
index 1da23e5585a0..6c884fc2b332 100644

drivers/input/touchscreen/surface3_spi.c struct surface3_ts_data { · line 35

Quoted 5 lines
drivers/input/touchscreen/surface3_spi.c +1 -1
@@ -32,7 +32,7 @@ struct surface3_ts_data {
 	struct input_dev *pen_input_dev;
 	int pen_tool;
 
-	u8 rd_buf[SURFACE3_PACKET_SIZE]		____cacheline_aligned;
+	u8 rd_buf[SURFACE3_PACKET_SIZE] __aligned(ARCH_KMALLOC_MINALIGN;

Missing bracket.

Quoted 3 lines
drivers/input/touchscreen/surface3_spi.c +0 -0
 };
 
 struct surface3_ts_data_finger {
#10 lore raw

PATCH 7/9 Input: n64joy - Fix DMA buffer alignment.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

The use of ____cacheline_aligned to ensure a buffer is DMA safe only enforces the start of the buffer alignment. In this case, sufficient alignment is already ensured by the use of kzalloc(). ____cacheline_aligned does not ensure that no other members of the structure are placed in the same cacheline after the end of the buffer marked. Thus to ensure a DMA safe buffer it must be at the end of the structure.

Whilst here switch from ____cacheline_aligned to __aligned(ARCH_KMALLOC_MINALIGN) as on some architectures, with variable sized cacheline lines across their cache hierarchy, require this greater alignment guarantee for DMA safety. Make this change throughout the driver as it reduces need for a reader to know about the particular architecture.

--
Only partly compile tested as I don't have a mips toolchain set up. Would be great to add the stubs to be able to build these drivers with COMPILE_TEST.

---
 drivers/input/joystick/n64joy.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/input/joystick/n64joy.c +3 -3
index 9dbca366613e..d8c50103c108 100644
@@ -44,12 +44,12 @@ static const char *n64joy_phys[MAX_CONTROLLERS] = {
 };
 
 struct n64joy_priv {
-	u64 si_buf[8] ____cacheline_aligned;
 	struct timer_list timer;
 	struct mutex n64joy_mutex;
 	struct input_dev *n64joy_dev[MAX_CONTROLLERS];
 	u32 __iomem *reg_base;
 	u8 n64joy_opened;
+	u64 si_buf[8] __aligned(ARCH_KMALLOC_MINALIGN);
 };
 
 struct joydata {
@@ -129,7 +129,7 @@ static void n64joy_exec_pif(struct n64joy_priv *priv, const u64 in[8])
 	local_irq_restore(flags);
 }
 
-static const u64 polldata[] ____cacheline_aligned = {
+static const u64 polldata[] __aligned(ARCH_KMALLOC_MINALIGN) = {
 	0xff010401ffffffff,
 	0xff010401ffffffff,
 	0xff010401ffffffff,
@@ -222,7 +222,7 @@ static void n64joy_close(struct input_dev *dev)
 	mutex_unlock(&priv->n64joy_mutex);
 }
 
-static const u64 __initconst scandata[] ____cacheline_aligned = {
+static const u64 __initconst scandata[] __aligned(ARCH_KMALLOC_MINALIGN) = {
 	0xff010300ffffffff,
 	0xff010300ffffffff,
 	0xff010300ffffffff,
Signature · 1 line
2.38.1
#11 lore raw

Re: [PATCH 7/9] Input: n64joy - Fix DMA buffer alignment.

Quoted 9 lines · On Sun, 27 Nov 2022 14:41:14 +0000 Jonathan Cameron <[email protected]> wrote:

From: Jonathan Cameron <[email protected]>

The use of ____cacheline_aligned to ensure a buffer is DMA safe only enforces the start of the buffer alignment. In this case, sufficient alignment is already ensured by the use of kzalloc(). ____cacheline_aligned does not ensure that no other members of the structure are placed in the same cacheline after the end of the buffer marked. Thus to ensure a DMA safe buffer it must be at the end of the structure.

This move is unnecessary, because the cacheline is 16 bytes and the buffer is 64 bytes.

Quoted 6 lines

Whilst here switch from ____cacheline_aligned to __aligned(ARCH_KMALLOC_MINALIGN) as on some architectures, with variable sized cacheline lines across their cache hierarchy, require this greater alignment guarantee for DMA safety. Make this change throughout the driver as it reduces need for a reader to know about the particular architecture.

This change looks ok.

- Lauri

#12 lore raw

Re: [PATCH 7/9] Input: n64joy - Fix DMA buffer alignment.

Quoted 13 lines · On Sun, 27 Nov 2022 18:48:44 +0200 Lauri Kasanen <[email protected]> wrote:
Quoted 9 lines · On Sun, 27 Nov 2022 14:41:14 +0000 Jonathan Cameron <[email protected]> wrote:

From: Jonathan Cameron <[email protected]>

The use of ____cacheline_aligned to ensure a buffer is DMA safe only enforces the start of the buffer alignment. In this case, sufficient alignment is already ensured by the use of kzalloc(). ____cacheline_aligned does not ensure that no other members of the structure are placed in the same cacheline after the end of the buffer marked. Thus to ensure a DMA safe buffer it must be at the end of the structure.

Quoted 2 lines

This move is unnecessary, because the cacheline is 16 bytes and the buffer is 64 bytes.

Ah. That particular option hadn't occurred to me (and I'd failed to notice how big the buffer is :( ).
The marking isn't needed at all then as the allocation is already guaranteed to be sufficiently aligned. However, maybe that is a bit subtle and having some sort of marking is useful.

Curious question though, why is the buffer so big?
Each struct joydata is 8 bytes I think, but the driver only accesses 4 of them.

Is the hardware putting garbage into the remaining 2 cachelines or is there something subtle going on?

Or given my earlier success, maybe I'm misreading the code entirely.

Jonathan

Quoted 11 lines
Quoted 7 lines

Whilst here switch from ____cacheline_aligned to __aligned(ARCH_KMALLOC_MINALIGN) as on some architectures, with variable sized cacheline lines across their cache hierarchy, require this greater alignment guarantee for DMA safety. Make this change throughout the driver as it reduces need for a reader to know about the particular architecture.

This change looks ok.

- Lauri

#13 lore raw

Re: [PATCH 7/9] Input: n64joy - Fix DMA buffer alignment.

Quoted 8 lines · On Sun, 27 Nov 2022 18:01:26 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 2 lines

This move is unnecessary, because the cacheline is 16 bytes and the buffer is 64 bytes.

Ah. That particular option hadn't occurred to me (and I'd failed to notice how big the buffer is :( ).
The marking isn't needed at all then as the allocation is already guaranteed to be sufficiently aligned. However, maybe that is a bit subtle and having some sort of marking is useful.

You can replace the __cacheline annotation with a comment, that's totally fine.

Quoted 5 lines

Curious question though, why is the buffer so big?
Each struct joydata is 8 bytes I think, but the driver only accesses 4 of them.

Is the hardware putting garbage into the remaining 2 cachelines or is there something subtle going on?

That chip operates on 64-byte units. I don't remember whether the remaining area is garbage or the input bytes or something else.

- Lauri

maintainer
#14 lore raw

Re: [PATCH 7/9] Input: n64joy - Fix DMA buffer alignment.

In reply to Lauri Kasanen
Quoted 13 lines · Lauri Kasanen
Quoted 9 lines · On Sun, 27 Nov 2022 14:41:14 +0000 Jonathan Cameron <[email protected]> wrote:

From: Jonathan Cameron <[email protected]>

The use of ____cacheline_aligned to ensure a buffer is DMA safe only enforces the start of the buffer alignment. In this case, sufficient alignment is already ensured by the use of kzalloc(). ____cacheline_aligned does not ensure that no other members of the structure are placed in the same cacheline after the end of the buffer marked. Thus to ensure a DMA safe buffer it must be at the end of the structure.

Quoted 2 lines

This move is unnecessary, because the cacheline is 16 bytes and the buffer is 64 bytes.

I think it is still worth moving it or alternatively adding a comment why we believe the following member will not be sharing cacheline with the buffer.

Thanks.

Signature · 1 line
Dmitry
#15 lore raw

PATCH 8/9 Input: atmel_captouch - Avoid suspect DMA buffer alignment.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

On some architectures (e.g. arm64), ____cachline_aligned only aligns to the cacheline size of the L1 cache size. L1_CACHE_BYTES in arch64/include/asm/cache.h Unfortunately DMA safety on these architectures requires the buffer no share a last level cache cacheline given by ARCH_DMA_MINALIGN which has a greater granularity. ARCH_DMA_MINALIGN is not defined for all architectures, but when it is defined it is used to set the size of ARCH_KMALLOC_MINALIGN to allow DMA safe buffer allocations.

As such the correct alignment requirement is __aligned(ARCH_KMALLOC_MINALIGN).
This has recently been fixed in other subsystems such as IIO.

This is probably not a fix as such, because likely this particular device is only used on devices where the given alignment is sufficient.

Cc Daniel Hung-yu Wu <[email protected]>
---
 drivers/input/misc/atmel_captouch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/misc/atmel_captouch.c +1 -1
index 051aded6815a..9681c763356c 100644
@@ -71,7 +71,7 @@ struct atmel_captouch_device {
 	u32 num_btn;
 	u32 keycodes[MAX_NUM_OF_BUTTONS];
 	u8 prev_btn;
-	u8 xfer_buf[8] ____cacheline_aligned;
+	u8 xfer_buf[8] __aligned(ARCH_KMALLOC_MINALIGN);
 };
 
 /*
Signature · 1 line
2.38.1
#16 lore raw

PATCH 9/9 Input: elants - Fix suspect DMA buffer alignment

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

On some architectures (e.g. arm64), ____cachline_aligned only aligns to the cacheline size of the L1 cache size. L1_CACHE_BYTES in arch64/include/asm/cache.h Unfortunately DMA safety on these architectures requires the buffer no share a last level cache cacheline given by ARCH_DMA_MINALIGN which has a greater granularity. ARCH_DMA_MINALIGN is not defined for all architectures, but when it is defined it is used to set the size of ARCH_KMALLOC_MINALIGN to allow DMA safe buffer allocations.

As such the correct alignment requirement is __aligned(ARCH_KMALLOC_MINALIGN).
This has recently been fixed in other subsystems such as IIO.

Fixes 00f73f97527f ("Input: elants_i2c - use DMA safe i2c when possible")
Cc Daniel Hung-yu Wu <[email protected]>
---
 drivers/input/touchscreen/elants_i2c.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/touchscreen/elants_i2c.c +1 -1
index 879a4d984c90..7ad58518c651 100644
@@ -184,7 +184,7 @@ struct elants_data {
 	bool keep_power_in_suspend;
 
 	/* Must be last to be used for DMA operations */
-	u8 buf[MAX_PACKET_SIZE] ____cacheline_aligned;
+	u8 buf[MAX_PACKET_SIZE] __aligned(ARCH_KMALLOC_MINALIGN);
 };
 
 static int elants_i2c_send(struct i2c_client *client,
Signature · 1 line
2.38.1
maintainer
#17 lore raw
In reply to Jonathan Cameron

Hi Jonathan,

Quoted 10 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

This problem was discovered in IIO as a side effect of the discussions about relaxing kmalloc alignment on arm64 and resulted in a series of large patch sets.

https://lore.kernel.org/linux-iio/[email protected]/

Unsurprisingly there are cases of it in other subsystems.

Quoted 7 lines

The short version of this is that there are a few known arm64 chips where ___cacheline_aligned enforces 64 byte alignment which is what we typically want for performance optimization as the size of the L1 cache lines. However, further out in the cache hierarchy we have caches with 128 byte lines. Those are the ones that matter for DMA safety.
So we need the larger alignment guarantees of ARCH_KMALLOC_MINALIGN which in this case is 128 bytes.

I wonder if we could have something like ____dmasafe_aligned instead of sprinkling ARCH_KMALLOC_MINALIGN around?

Quoted 6 lines

There is one other use of ____cacheline_aligned in input:
joystick/iforce/iforce-usb.c

Whilst suspicious I'm not sure enough of the requirements of USB to know if they are there for DMA safety or some other constraint.

Yes, USB has requirements similar to SPI.

Thanks.

Signature · 1 line
Dmitry
#18 lore raw
Quoted 21 lines · On Mon, 28 Nov 2022 10:16:31 -0800 Dmitry Torokhov <[email protected]>…

Hi Jonathan,

Quoted 10 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

This problem was discovered in IIO as a side effect of the discussions about relaxing kmalloc alignment on arm64 and resulted in a series of large patch sets.

https://lore.kernel.org/linux-iio/[email protected]/

Unsurprisingly there are cases of it in other subsystems.

Quoted 7 lines

The short version of this is that there are a few known arm64 chips where ___cacheline_aligned enforces 64 byte alignment which is what we typically want for performance optimization as the size of the L1 cache lines. However, further out in the cache hierarchy we have caches with 128 byte lines. Those are the ones that matter for DMA safety.
So we need the larger alignment guarantees of ARCH_KMALLOC_MINALIGN which in this case is 128 bytes.

Quoted 2 lines

I wonder if we could have something like ____dmasafe_aligned instead of sprinkling ARCH_KMALLOC_MINALIGN around?

I agree in principle and eventually that will be ARCH_DMA_MINALIGN. But it isn't useable yet for backports.
Catalin has worked on several approaches to reducing the alignment of kmalloc. I may well be lagging on the current plan...
https://lore.kernel.org/linux-arm-kernel/[email protected]/#r

The way crypto and IIO handled this was to add a local define IIO_DMA_MINALIGN, CRYPTO_MINALIGN

Catalin, if you do forwards with making ARCH_DMA_MINALIGN global available, make sure to include IIO_DMA_MINALIGN. I'm fine with the approach you used for crypto of changing the local define. Note that in IIO this typically only bloats a single structure per device instance, so it's not worth the complexity of dynamic alignment. As we are marking structure elements with it, in all cases they are multiples of IIO_DMA_MINALIGN in size, so everything is easy.

On the other side of things, we might be able to relax most of these alignment tricks in IIO (and input) if swiotlb is the approach eventually used. I'm personally not that keen on that transition but meh, this stuff usually involves a serial bus, so the extra bounce isnt' too painful. Note that we regularly do fun things like setting tx buffer to be ARCH_KMALLOC_MINALIGN and the rx buffer to be that +n bytes on the basis that a given serial bus master won't trash itself and hence this is safe. I think your approach will bounce all of those which is somewhat unfortunate. Other fun things include DMA mapping a series of small buffers in one allocation for a series of serial messages. Again, that is guaranteed to be safe.

Jonathan

Quoted 12 lines
Quoted 7 lines

There is one other use of ____cacheline_aligned in input:
joystick/iforce/iforce-usb.c

Whilst suspicious I'm not sure enough of the requirements of USB to know if they are there for DMA safety or some other constraint.

Yes, USB has requirements similar to SPI.

Thanks.

maintainer
#19 lore raw
Quoted 24 lines · Jonathan Cameron
Quoted 21 lines · On Mon, 28 Nov 2022 10:16:31 -0800 Dmitry Torokhov <[email protected]>…

Hi Jonathan,

Quoted 17 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

This problem was discovered in IIO as a side effect of the discussions about relaxing kmalloc alignment on arm64 and resulted in a series of large patch sets.

https://lore.kernel.org/linux-iio/[email protected]/

Unsurprisingly there are cases of it in other subsystems.

The short version of this is that there are a few known arm64 chips where ___cacheline_aligned enforces 64 byte alignment which is what we typically want for performance optimization as the size of the L1 cache lines. However, further out in the cache hierarchy we have caches with 128 byte lines. Those are the ones that matter for DMA safety.
So we need the larger alignment guarantees of ARCH_KMALLOC_MINALIGN which in this case is 128 bytes.

Quoted 5 lines
Quoted 2 lines

I wonder if we could have something like ____dmasafe_aligned instead of sprinkling ARCH_KMALLOC_MINALIGN around?

I agree in principle and eventually that will be ARCH_DMA_MINALIGN. But it isn't useable yet for backports.

Sorry, I do not follow. Are talking about backports because the code is broken in the mainline right now, or it will become broken when Catalin's changes land? And even if it is broken right now why can't we add

#define ____dmasafe_aligned __attribute__((__aligned__(ARCH_KMALLOC_MINALIGN)))

somewhere in include/linux/cache.h? Then you can tweak it as needed independently of kmalloc alignment and without need to touch drivers again and it should be easy to backport still.

Thanks.

Signature · 1 line
Dmitry