Skip to contentSkip to conversation

counter: fix privdata alignment

PATCH Superseded Nuno Sa 4 participants 6 messages Feb 5, 2024 – Feb 9, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Nuno Sa

author reviewer
#0 lore raw

PATCH 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]/

Fixes c18e2760308e ("counter: Provide alternative counter registration functions")

---
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
index 09c77afb33ca..073bf6b67a57 100644
@@ -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á

#1 lore raw
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")

---
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 -2
index 09c77afb33ca..073bf6b67a57 100644
@@ -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

#2 lore raw
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 registration

functions")

---
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 +0 -0
index 09c77afb33ca..073bf6b67a57 100644
@@ -34,7 +34,7 @@ struct counter_device_allochelper {
  	 * This is cache line aligned to ensure private data behaves like

if it
  * were kmalloced separately.
  */

drivers/counter/counter-core.c +1 -1
-	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 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á

#3 lore raw

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
index 09c77afb33ca..073bf6b67a57 100644
@@ -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 void counter_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/ |
#4 lore raw
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
index 09c77afb33ca..073bf6b67a57 100644

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

like 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 void counter_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á

#5 lore raw
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
index 09c77afb33ca..073bf6b67a57 100644

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

like 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 void counter_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