Input: Fix insufficent DMA alignment.
Subsystems (5)
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
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
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.
--- drivers/input/joystick/psxpad-spi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/joystick/psxpad-spi.c +2 -2
@@ -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
PATCH 2/9 Input: ad714x - Fix padding for DMA safe buffers.
In reply to Jonathan CameronFrom: 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.
--- drivers/input/misc/ad714x.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/misc/ad714x.h +1 -1
@@ -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
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_alignedonly aligns to the cacheline size of the L1 cache size.L1_CACHE_BYTESinarch64/include/asm/cache.hUnfortunately DMA safety on these architectures requires the buffer no share a last level cache cacheline given byARCH_DMA_MINALIGNwhich has a greater granularity.ARCH_DMA_MINALIGNis not defined for all architectures, but when it is defined it is used to set the size ofARCH_KMALLOC_MINALIGNto 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()")Signed-off-by Jonathan Cameron <[email protected]>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..2b8b901183be100644drivers/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
PATCH 3/9 Input: ad7887 - Fix padding for DMA safe buffers.
In reply to Jonathan CameronFrom: 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.
--- drivers/input/touchscreen/ad7877.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/touchscreen/ad7877.c +2 -2
@@ -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
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_alignedonly aligns to the cacheline size of the L1 cache size.L1_CACHE_BYTESinarch64/include/asm/cache.hUnfortunately DMA safety on these architectures requires the buffer no share a last level cache cacheline given byARCH_DMA_MINALIGNwhich has a greater granularity.ARCH_DMA_MINALIGNis not defined for all architectures, but when it is defined it is used to set the size ofARCH_KMALLOC_MINALIGNto 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 cachelines")
Signed-off-by Jonathan Cameron <[email protected]>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@@ -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
PATCH 4/9 Input: ads7846 - Fix padding for DMA safe buffers.
In reply to Jonathan CameronFrom: 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.
--- drivers/input/touchscreen/ads7846.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/touchscreen/ads7846.c +2 -2
@@ -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
PATCH 5/9 Input: cyttsp - Fix padding for DMA safe buffers.
In reply to Jonathan CameronFrom: 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.
--- drivers/input/touchscreen/cyttsp_core.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/touchscreen/cyttsp_core.h +1 -1
@@ -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
PATCH 6/9 Input: surface3 - Fix padding for DMA safe buffers.
In reply to Jonathan CameronFrom: 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.
--- drivers/input/touchscreen/surface3_spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
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; };struct surface3_ts_data_finger {
Signature · 1 line
2.38.1
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_alignedonly aligns to the cacheline size of the L1 cache size.L1_CACHE_BYTESinarch64/include/asm/cache.hUnfortunately DMA safety on these architectures requires the buffer no share a last level cache cacheline given byARCH_DMA_MINALIGNwhich has a greater granularity.ARCH_DMA_MINALIGNis not defined for all architectures, but when it is defined it is used to set the size ofARCH_KMALLOC_MINALIGNto 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")Signed-off-by Jonathan Cameron <[email protected]>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
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 {
PATCH 7/9 Input: n64joy - Fix DMA buffer alignment.
In reply to Jonathan CameronFrom: 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
@@ -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
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_alignedto 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 ofkzalloc().____cacheline_aligneddoes 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_alignedto__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
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_alignedto 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 ofkzalloc().____cacheline_aligneddoes 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_alignedto__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
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
Dmitry Torokhov
maintainerRe: [PATCH 7/9] Input: n64joy - Fix DMA buffer alignment.
In reply to Lauri KasanenQuoted 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_alignedto 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 ofkzalloc().____cacheline_aligneddoes 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
PATCH 8/9 Input: atmel_captouch - Avoid suspect DMA buffer alignment.
In reply to Jonathan CameronFrom: 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.
--- drivers/input/misc/atmel_captouch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/misc/atmel_captouch.c +1 -1
@@ -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
PATCH 9/9 Input: elants - Fix suspect DMA buffer alignment
In reply to Jonathan CameronFrom: 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.
--- drivers/input/touchscreen/elants_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/touchscreen/elants_i2c.c +1 -1
@@ -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
Dmitry Torokhov
maintainerHi 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_alignedenforces 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 ofARCH_KMALLOC_MINALIGNwhich 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_alignedin input:joystick/iforce/iforce-usb.cWhilst 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
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_alignedenforces 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 ofARCH_KMALLOC_MINALIGNwhich in this case is 128 bytes.
Quoted 2 lines
I wonder if we could have something like
____dmasafe_alignedinstead of sprinklingARCH_KMALLOC_MINALIGNaround?
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_alignedin input:joystick/iforce/iforce-usb.cWhilst 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.
Dmitry Torokhov
maintainerQuoted 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_alignedenforces 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 ofARCH_KMALLOC_MINALIGNwhich in this case is 128 bytes.
Quoted 5 lines
Quoted 2 lines
I wonder if we could have something like
____dmasafe_alignedinstead of sprinklingARCH_KMALLOC_MINALIGNaround?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