counter: fix privdata alignment
Nuno Sa
reviewerPATCH 1/1 counter: fix privdata alignment
Aligning to the L1 cache does 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/[email protected]/
---
William, if you prefer, we can do something like in IIO and add a specific COUNTER_DMA_MINALIGN define
--- drivers/counter/counter-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/counter/counter-core.c +1 -1
@@ -34,7 +34,7 @@ struct counter_device_allochelper {* This is cache line aligned to ensure private data behaves like if it * were kmalloced separately. */ -unsigned long privdata[]____cacheline_aligned ; +unsigned long privdata[]__aligned(ARCH_DMA_MINALIGN) ; };static void counter_device_release (struct device *dev)
---
base-commit: 6613476e225e090cc9aad49be7fa504e290dd33d
change-id: 20240205-counter-align-fix-3faebfb572af --
Thanks!
- Nuno Sá
Quoted 40 lines · On Mon, Feb 05, 2024 at 04:58:14PM +0100, Nuno Sa via B4 Relay wrote:
From: Nuno Sa <[email protected]>
Aligning to the L1 cache does 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/[email protected]/Fixes c18e2760308e ("counter: Provide alternative counter registration functions")Signed-off-by Nuno Sa <[email protected]>---
William, if you prefer, we can do something like in IIO and add a specificCOUNTER_DMA_MINALIGNdefine--- drivers/counter/counter-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/counter/counter-core.c +1
-2@@ -34,7 +34,7 @@ struct counter_device_allochelper {* This is cache line aligned to ensure private data behaves like if it * were kmalloced separately. */ -unsigned long privdata[]____cacheline_aligned ; +unsigned long privdata[]__aligned(ARCH_DMA_MINALIGN) ; };static void counter_device_release (struct device *dev) ---base-commit:
6613476e225e090cc9aad49be7fa504e290dd33d
change-id: 20240205-counter-align-fix-3faebfb572af --
Quoted 2 lines
Thanks!
- Nuno Sá
Hi Nunon,
This change sounds reasonable, but should the comment block above privdata be updated to reflect the change?
Sincerely,
William Breathitt Gray
Quoted 45 lines · William Breathitt Gray
Quoted 44 lines · On Mon, Feb 05, 2024 at 04:58:14PM +0100, Nuno Sa via B4 Relay wrote:
From: Nuno Sa <[email protected]>
Aligning to the L1 cache does 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/[email protected]/Fixes c18e2760308e ("counter: Provide alternative counter registrationfunctions")
Signed-off-by Nuno Sa <[email protected]>---
William, if you prefer, we can do something like in IIO and add a specificCOUNTER_DMA_MINALIGNdefine
---
drivers/counter/counter-core.c | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)drivers/counter/counter-core.c +0
-0@@ -34,7 +34,7 @@ struct counter_device_allochelper { * This is cache line aligned to ensure private data behaves likeif it
* were kmalloced separately.
*/drivers/counter/counter-core.c +1
-1-unsigned long privdata[]____cacheline_aligned ; +unsigned long privdata[]__aligned(ARCH_DMA_MINALIGN) ;};
static voidcounter_device_release(struct device *dev)---
base-commit:6613476e225e090cc9aad49be7fa504e290dd33d
change-id: 20240205-counter-align-fix-3faebfb572af --
Quoted 9 lines
Quoted 2 lines
Thanks!
- Nuno SáHi Nunon,
This change sounds reasonable, but should the comment block above privdata be updated to reflect the change?
Hi William,
Yeah, maybe. I can spin a new version with that... To be sure, you mean (in the comment) private -> privdata, right?
Also realized a typo in the commit message:
"Aligning to the L1 cache does guarantee..." - Obviously, I meant "does *not*...".
Thanks!
- Nuno Sá
Hi Nuno,
Quoted 20 lines · Nuno Sá
Quoted 10 lines · William Breathitt Gray
Quoted 9 lines · On Mon, Feb 05, 2024 at 04:58:14PM +0100, Nuno Sa via B4 Relay wrote:
drivers/counter/counter-core.c +0
-1@@ -34,7 +34,7 @@ struct counter_device_allochelper {* This is cache line aligned to ensure private data behaves like if it * were kmalloced separately. */ -unsigned long privdata[]____cacheline_aligned ;Quoted 8 lines
Quoted 5 lines
drivers/counter/counter-core.c +1
-0+unsigned long privdata[]__aligned(ARCH_DMA_MINALIGN) ; };
static voidcounter_device_release(struct device *dev)This change sounds reasonable, but should the comment block above privdata be updated to reflect the change?
Quoted 2 lines
Yeah, maybe. I can spin a new version with that... To be sure, you mean (in the comment) private -> privdata, right?
I guess he means: "This is cache line aligned to ensure private data behaves like if it were kmalloced separately." After your change it's not cache line aligned any more. IMHO keeping "private" is fine.
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Quoted 25 lines · Uwe Kleine-König
Hi Nuno,
Quoted 22 lines · Nuno Sá
Quoted 20 lines · William Breathitt Gray
Quoted 12 lines · On Mon, Feb 05, 2024 at 04:58:14PM +0100, Nuno Sa via B4 Relay wrote:
drivers/counter/counter- renamed from drivers/counter/counter-core.c +0
-0
core.c
index09c77afb33ca..073bf6b67a57100644drivers/counter/counter-core.c +0
-0@@ -34,7 +34,7 @@ struct counter_device_allochelper { * This is cache line aligned to ensure private data behaveslike if it
* were kmalloced separately.
*/drivers/counter/counter-core.c +1
-1-unsigned long privdata[]____cacheline_aligned ; +unsigned long privdata[]__aligned(ARCH_DMA_MINALIGN) ;Quoted 4 lines
};
static voidcounter_device_release(struct device *dev)This change sounds reasonable, but should the comment block above privdata be updated to reflect the change?
Quoted 9 lines
Quoted 3 lines
Yeah, maybe. I can spin a new version with that... To be sure, you mean (in the
comment) private -> privdata, right?I guess he means: "This is cache line aligned to ensure private data behaves like if it were kmalloced separately." After your change it's not cache line aligned any more. IMHO keeping "private" is fine.
Oh yeah...
Yeah, it will depend on the platform. In some, it will still be cache aligned but in others (as x86 which is DMA coeherent I think), it won't be and we can actually safe some memory.
- Nuno Sá
Quoted 36 lines · Nuno Sá
Quoted 25 lines · Uwe Kleine-König
Hi Nuno,
Quoted 22 lines · Nuno Sá
Quoted 20 lines · William Breathitt Gray
Quoted 12 lines · On Mon, Feb 05, 2024 at 04:58:14PM +0100, Nuno Sa via B4 Relay wrote:
drivers/counter/counter- renamed from drivers/counter/counter-core.c +0
-0
core.c
index09c77afb33ca..073bf6b67a57100644drivers/counter/counter-core.c +0
-0@@ -34,7 +34,7 @@ struct counter_device_allochelper { * This is cache line aligned to ensure private data behaveslike if it
* were kmalloced separately.
*/drivers/counter/counter-core.c +1
-1-unsigned long privdata[]____cacheline_aligned ; +unsigned long privdata[]__aligned(ARCH_DMA_MINALIGN) ;Quoted 4 lines
};
static voidcounter_device_release(struct device *dev)This change sounds reasonable, but should the comment block above privdata be updated to reflect the change?
Quoted 9 lines
Quoted 3 lines
Yeah, maybe. I can spin a new version with that... To be sure, you mean (in the
comment) private -> privdata, right?I guess he means: "This is cache line aligned to ensure private data behaves like if it were kmalloced separately." After your change it's not cache line aligned any more. IMHO keeping "private" is fine.
Quoted 7 lines
Oh yeah...
Yeah, it will depend on the platform. In some, it will still be cache aligned but in others (as x86 which is DMA coeherent I think), it won't be and we can actually safe some memory.
- Nuno Sá
Yes, I was referring to the possibility that it won't be cache aligned anymore in some platforms as you mentioned, so a different comment would be better there now. You can keep the "private" wording if you like, or use "privdata" if you think it's clearer.
Thanks,
William Breathitt Gray