Skip to contentSkip to conversation

devres: Really align data field to unsigned long long

PATCH RESEND In review Alexey Brodkin 5 participants 19 messages Jul 9, 2018 – Jul 10, 2018

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

PATCH RESEND v2 1/1 devres: Really align data field to unsigned long long

Depending on ABI "long long" type of a particular 32-bit CPU might be aligned by either word (32-bits) or double word (64-bits). Make sure "data" is really 64-bit aligned for any 32-bit CPU.

At least for 32-bit ARC cores ABI requires "long long" types to be aligned by normal 32-bit word. This makes "data" field aligned to 12 bytes. Which is still OK as long as we use 32-bit data only.

But once we want to use native atomic64_t type (i.e. when we use special instructions LLOCKD/SCONDD for accessing 64-bit data) we easily hit misaligned access exception.

That's because even on CPUs capable of non-aligned data access LL/SC instructions require strict alignment.

Cc Greg Kroah-Hartman <[email protected]>
Cc Thomas Gleixner <[email protected]>

---

Changes v1 -> v2:

* Reworded commit message
* Inserted comment right in source [Thomas]

 drivers/base/devres.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/base/devres.c +6 -2
index f98a097e73f2..466fa59c866a 100644
@@ -24,8 +24,12 @@ struct devres_node {
 
 struct devres {
 	struct devres_node		node;
-	/* -- 3 pointers */
-	unsigned long long		data[];	/* guarantee ull alignment */
+	/*
+	 * Depending on ABI "long long" type of a particular 32-bit CPU
+	 * might be aligned by either word (32-bits) or double word (64-bits).
+	 * Make sure "data" is really 64-bit aligned for any 32-bit CPU.
+	 */
+	unsigned long long		data[] __aligned(sizeof(unsigned long long));
 };
 
 struct devres_group {
Signature · 1 line
2.17.1
#1 lore raw
Quoted 11 lines · Alexey Brodkin

Depending on ABI "long long" type of a particular 32-bit CPU might be aligned by either word (32-bits) or double word (64-bits). Make sure "data" is really 64-bit aligned for any 32-bit CPU.

At least for 32-bit ARC cores ABI requires "long long" types to be aligned by normal 32-bit word. This makes "data" field aligned to 12 bytes. Which is still OK as long as we use 32-bit data only.

But once we want to use native atomic64_t type (i.e. when we use special instructions LLOCKD/SCONDD for accessing 64-bit data) we easily hit misaligned access exception.

So is this something you hit today? If not, why is this needed for stable kernels?

Quoted 2 lines

That's because even on CPUs capable of non-aligned data access LL/SC instructions require strict alignment.

Are you going to hit this code with all types of structures? What happens when you do have an unaligned access?

Quoted 3 lines
Cc Greg Kroah-Hartman <[email protected]>

You didn't cc: this address :(

Quoted 19 lines
Cc Thomas Gleixner <[email protected]>

---

Changes v1 -> v2:

* Reworded commit message
* Inserted comment right in source [Thomas]

 drivers/base/devres.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/base/devres.c +0 -0
index f98a097e73f2..466fa59c866a 100644
@@ -24,8 +24,12 @@ struct devres_node {
 
 struct devres {

drivers/base/devres.c struct devres_node { · line 33

Quoted 10 lines
drivers/base/devres.c +6 -2
 	struct devres_node		node;
-	/* -- 3 pointers */
-	unsigned long long		data[];	/* guarantee ull alignment */
+	/*
+	 * Depending on ABI "long long" type of a particular 32-bit CPU
+	 * might be aligned by either word (32-bits) or double word (64-bits).
+	 * Make sure "data" is really 64-bit aligned for any 32-bit CPU.
+	 */
+	unsigned long long		data[] __aligned(sizeof(unsigned long long));
 };

Does this change the padding today for any other arches? If so, does the increased memory usage cause any noticable issues?

thanks,

greg k-h

#2 lore raw

Hi Greg,

Quoted 9 lines · Greg KH
Quoted 8 lines · Alexey Brodkin

Depending on ABI "long long" type of a particular 32-bit CPU might be aligned by either word (32-bits) or double word (64-bits). Make sure "data" is really 64-bit aligned for any 32-bit CPU.

At least for 32-bit ARC cores ABI requires "long long" types to be aligned by normal 32-bit word. This makes "data" field aligned to 12 bytes. Which is still OK as long as we use 32-bit data only.

Quoted 6 lines
Quoted 3 lines

But once we want to use native atomic64_t type (i.e. when we use special instructions LLOCKD/SCONDD for accessing 64-bit data) we easily hit misaligned access exception.

So is this something you hit today? If not, why is this needed for stable kernels?

Indeed we hit that problem recently when Etnaviv driver was switched to DRM GPU scheduler, see
commit e93b6deeb45a ("drm/etnaviv: hook up DRM GPU scheduler"). The most important part of DRM GPU scheduler is "job_id_count" member of "drm_gpu_scheduler" structure of type "atomic64_t". This structure is put in a buffer allocated by devm_kzalloc() and if "job_id_count" is not 64-bit aligned atomic instruction fails with an exception.

As for stable requirements - mentioned commit was a part of 4.17 kernel which broke GPU driver for one of our HSDK board so I guess back-porting to 4.17 is a no-brainer.

Now given a nature of the problem at hand I may expect other breakages whenever another driver/fs etc that use "atomic64_t" gets enabled.

Quoted 4 lines
Quoted 2 lines

That's because even on CPUs capable of non-aligned data access LL/SC instructions require strict alignment.

Are you going to hit this code with all types of structures?

If there're other cases which lead to 4-byte aligned "atomic64_t" variables there will be a problem as well but it's quite hard to predict those cases. That said if we manage to reproduce more similar issues there will be more patches with fixes :)

Quoted 1 line

What happens when you do have an unaligned access?

Atomic instructions are a bit special as compared to normal loads and stores. Even if normal loads and stores may deal with unaligned data atomic instructions still require data to be aligned because it's hard to manage atomic value that spans through multiple cache lines or even MMU pages. And hardware just raises an alignment fault exception.

And that's not something special for ARC, I guess all CPUs are the same in that regard, see here's an extract from ARM(r) Architecture Reference Manual ARMv7-A and ARMv7-R edition: https://lkml.org/lkml/2017/12/5/440
From "Table A3-1 Alignment requirements of load/store instructions" it's seen that LDREXD, STREXD instructions will cause alignment fault even if SCTLR.A=0 (strict alignment fault checking disabled) for non double-word-aligned data.

Quoted 5 lines
Quoted 3 lines
Cc Greg Kroah-Hartman <[email protected]>

You didn't cc: this address :(

Sorry, which one?
That what I have in .mbox, see https://patchwork.ozlabs.org/patch/941092/mbox/:
------------------------------>8------------------------------
Cc: [email protected], Greg KH <[email protected]>,
Alexey Brodkin <[email protected]>, [email protected], Greg Kroah-Hartman <[email protected]>,
Thomas Gleixner <[email protected]>, [email protected]
------------------------------>8------------------------------

-Alexey

#3 lore raw
Quoted 27 lines · Alexey Brodkin

Hi Greg,

Quoted 9 lines · Greg KH
Quoted 8 lines · Alexey Brodkin

Depending on ABI "long long" type of a particular 32-bit CPU might be aligned by either word (32-bits) or double word (64-bits). Make sure "data" is really 64-bit aligned for any 32-bit CPU.

At least for 32-bit ARC cores ABI requires "long long" types to be aligned by normal 32-bit word. This makes "data" field aligned to 12 bytes. Which is still OK as long as we use 32-bit data only.

Quoted 6 lines
Quoted 3 lines

But once we want to use native atomic64_t type (i.e. when we use special instructions LLOCKD/SCONDD for accessing 64-bit data) we easily hit misaligned access exception.

So is this something you hit today? If not, why is this needed for stable kernels?

Indeed we hit that problem recently when Etnaviv driver was switched to DRM GPU scheduler, see
commit e93b6deeb45a ("drm/etnaviv: hook up DRM GPU scheduler"). The most important part of DRM GPU scheduler is "job_id_count" member of "drm_gpu_scheduler" structure of type "atomic64_t". This structure is put in a buffer allocated by devm_kzalloc() and if "job_id_count" is not 64-bit aligned atomic instruction fails with an exception.

Quoted 3 lines

As for stable requirements - mentioned commit was a part of 4.17 kernel which broke GPU driver for one of our HSDK board so I guess back-porting to 4.17 is a no-brainer.

Ok, so 4.17 is as far back as you need? Please try to be specific when asking for stable backports.

Quoted 18 lines
Quoted 4 lines
Quoted 2 lines

That's because even on CPUs capable of non-aligned data access LL/SC instructions require strict alignment.

Are you going to hit this code with all types of structures?

If there're other cases which lead to 4-byte aligned "atomic64_t" variables there will be a problem as well but it's quite hard to predict those cases. That said if we manage to reproduce more similar issues there will be more patches with fixes :)

Quoted 1 line

What happens when you do have an unaligned access?

Atomic instructions are a bit special as compared to normal loads and stores. Even if normal loads and stores may deal with unaligned data atomic instructions still require data to be aligned because it's hard to manage atomic value that spans through multiple cache lines or even MMU pages. And hardware just raises an alignment fault exception.

Quoted 7 lines

And that's not something special for ARC, I guess all CPUs are the same in that regard, see here's an extract from ARM(r) Architecture Reference Manual ARMv7-A and ARMv7-R edition: https://lkml.org/lkml/2017/12/5/440
From "Table A3-1 Alignment requirements of load/store instructions" it's seen that LDREXD, STREXD instructions will cause alignment fault even if SCTLR.A=0 (strict alignment fault checking disabled) for non double-word-aligned data.

Thanks for the better explaination, that helps out a lot. Can you redo the patch with all of that information so that others do not have the same questions as I did?

thanks,

greg k-h

#6 lore raw
In reply to Greg KH
Quoted 3 lines · Greg KH
Quoted 2 lines · Alexey Brodkin

Depending on ABI "long long" type of a particular 32-bit CPU might be aligned by either word (32-bits) or double word (64-bits).

Or even 16-bit (on e.g. m68k).

Quoted 11 lines

drivers/base/devres.c struct devres_node { · line 29

Quoted 11 lines
drivers/base/devres.c +3 -2
@@ -24,8 +24,12 @@ struct devres_node {
 
 struct devres {
      struct devres_node              node;
-     /* -- 3 pointers */
-     unsigned long long              data[]; /* guarantee ull alignment */
+     /*
+      * Depending on ABI "long long" type of a particular 32-bit CPU
+      * might be aligned by either word (32-bits) or double word (64-bits).

or even 16-bit (on e.g. m68k).

Quoted 7 lines

drivers/base/devres.c struct devres_node { · line 33

Quoted 4 lines
drivers/base/devres.c +3 -0
+      * Make sure "data" is really 64-bit aligned for any 32-bit CPU.
+      */
+     unsigned long long              data[] __aligned(sizeof(unsigned long long));
 };

Does this change the padding today for any other arches? If so, does the increased memory usage cause any noticable issues?

Yes it does. struct devres_node contains an odd number of pointers, so there will be a hole between node and data on all 32-bit architectures.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#4 lore raw
In reply to [email protected]

Hi Greg,

Quoted 28 lines · On Mon, 2018-07-09 at 09:06 +0200, [email protected] wrote:
Quoted 27 lines · Alexey Brodkin

Hi Greg,

Quoted 9 lines · Greg KH
Quoted 8 lines · Alexey Brodkin

Depending on ABI "long long" type of a particular 32-bit CPU might be aligned by either word (32-bits) or double word (64-bits). Make sure "data" is really 64-bit aligned for any 32-bit CPU.

At least for 32-bit ARC cores ABI requires "long long" types to be aligned by normal 32-bit word. This makes "data" field aligned to 12 bytes. Which is still OK as long as we use 32-bit data only.

Quoted 6 lines
Quoted 3 lines

But once we want to use native atomic64_t type (i.e. when we use special instructions LLOCKD/SCONDD for accessing 64-bit data) we easily hit misaligned access exception.

So is this something you hit today? If not, why is this needed for stable kernels?

Indeed we hit that problem recently when Etnaviv driver was switched to DRM GPU scheduler, see
commit e93b6deeb45a ("drm/etnaviv: hook up DRM GPU scheduler"). The most important part of DRM GPU scheduler is "job_id_count" member of "drm_gpu_scheduler" structure of type "atomic64_t". This structure is put in a buffer allocated by devm_kzalloc() and if "job_id_count" is not 64-bit aligned atomic instruction fails with an exception.

Quoted 6 lines
Quoted 3 lines

As for stable requirements - mentioned commit was a part of 4.17 kernel which broke GPU driver for one of our HSDK board so I guess back-porting to 4.17 is a no-brainer.

Ok, so 4.17 is as far back as you need? Please try to be specific when asking for stable backports.

Well in that particular case I really wanted to get this fix back-ported starting from v4.8 so I guess that's what I need to add in Cc tag, right? ----------------------------->8---------------------
Cc: <[email protected]> # 4.8+ ----------------------------->8---------------------

Quoted 27 lines
Quoted 18 lines
Quoted 4 lines
Quoted 2 lines

That's because even on CPUs capable of non-aligned data access LL/SC instructions require strict alignment.

Are you going to hit this code with all types of structures?

If there're other cases which lead to 4-byte aligned "atomic64_t" variables there will be a problem as well but it's quite hard to predict those cases. That said if we manage to reproduce more similar issues there will be more patches with fixes :)

Quoted 1 line

What happens when you do have an unaligned access?

Atomic instructions are a bit special as compared to normal loads and stores. Even if normal loads and stores may deal with unaligned data atomic instructions still require data to be aligned because it's hard to manage atomic value that spans through multiple cache lines or even MMU pages. And hardware just raises an alignment fault exception.

Quoted 8 lines

And that's not something special for ARC, I guess all CPUs are the same in
that regard, see here's an extract from ARM(r) Architecture Reference
Manual ARMv7-A and ARMv7-R edition: https://urldefense.proofpoint.com/v2/url?u=https-3A__lkml.org_lkml_2017_12_5_440&d=DwIBAg&c=DPL6_X_6JkXFx7AXWq
B0tg&r=lqdeeSSEes0GFDDl656eViXO7breS55ytWkhpk5R81I&m=eT1OUXQEt6zlA0bABwdI7sFr7Hys3aHzoCXTDAkM6xY&s=-3n_Xurm4D2TIC-_G_GIvGj9P_3unmq839oGATLE5W0&e= From "Table A3-1 Alignment requirements of load/store instructions"
it's seen that LDREXD, STREXD instructions will cause alignment fault
even if SCTLR.A=0 (strict alignment fault checking disabled) for non
double-word-aligned data.

Quoted 3 lines

Thanks for the better explaination, that helps out a lot. Can you redo the patch with all of that information so that others do not have the same questions as I did?

Sure will do it soonish.

-Alexey

#7 lore raw
In reply to Geert Uytterhoeven

Hi Geert,

Quoted 6 lines · Geert Uytterhoeven
Quoted 3 lines · Greg KH
Quoted 2 lines · Alexey Brodkin

Depending on ABI "long long" type of a particular 32-bit CPU might be aligned by either word (32-bits) or double word (64-bits).

Or even 16-bit (on e.g. m68k).

Indeed, thanks for the note!
Will add this in my v3.

For ARC I'd like this fix to be back-ported starting
from v4.8 where we added support of "native" atomic64_t, see commit ce6365270ecd (" ARCv2: Implement atomic64 based on LLOCKD/SCONDD instructions").

What about m68k, do you have any preference of earliest kernel version where this fix might be useful?

-Alexey

#5 lore raw
In reply to Alexey Brodkin
Quoted 35 lines · Alexey Brodkin

Hi Greg,

Quoted 32 lines · On Mon, 2018-07-09 at 09:06 +0200, [email protected] wrote:
Quoted 30 lines · Alexey Brodkin

Hi Greg,

Quoted 9 lines · Greg KH
Quoted 8 lines · Alexey Brodkin

Depending on ABI "long long" type of a particular 32-bit CPU might be aligned by either word (32-bits) or double word (64-bits). Make sure "data" is really 64-bit aligned for any 32-bit CPU.

At least for 32-bit ARC cores ABI requires "long long" types to be aligned by normal 32-bit word. This makes "data" field aligned to 12 bytes. Which is still OK as long as we use 32-bit data only.

Quoted 6 lines
Quoted 3 lines

But once we want to use native atomic64_t type (i.e. when we use special instructions LLOCKD/SCONDD for accessing 64-bit data) we easily hit misaligned access exception.

So is this something you hit today? If not, why is this needed for stable kernels?

Indeed we hit that problem recently when Etnaviv driver was switched to DRM GPU scheduler, see
commit e93b6deeb45a ("drm/etnaviv: hook up DRM GPU scheduler"). The most important part of DRM GPU scheduler is "job_id_count" member of "drm_gpu_scheduler" structure of type "atomic64_t". This structure is put in a buffer allocated by devm_kzalloc() and if "job_id_count" is not 64-bit aligned atomic instruction fails with an exception.

As for stable requirements - mentioned commit was a part of 4.17 kernel which broke GPU driver for one of our HSDK board so I guess back-porting to 4.17 is a no-brainer.

Quoted 8 lines
Quoted 2 lines

Ok, so 4.17 is as far back as you need? Please try to be specific when asking for stable backports.

Well in that particular case I really wanted to get this fix back-ported starting from v4.8 so I guess that's what I need to add in Cc tag, right? ----------------------------->8---------------------
Cc: <[email protected]> # 4.8+ ----------------------------->8---------------------

Yes.

#8 lore raw
In reply to Alexey Brodkin

Hi Alexey,

Quoted 10 lines · Alexey Brodkin
Quoted 6 lines · Geert Uytterhoeven
Quoted 3 lines · Greg KH
Quoted 2 lines · Alexey Brodkin

Depending on ABI "long long" type of a particular 32-bit CPU might be aligned by either word (32-bits) or double word (64-bits).

Or even 16-bit (on e.g. m68k).

Indeed, thanks for the note!
Will add this in my v3.

Note that in this particular case, the field will probably always be aligned to 4 bytes,
as the struct will be allocated using *alloc().

Quoted 6 lines

For ARC I'd like this fix to be back-ported starting
from v4.8 where we added support of "native" atomic64_t, see commit ce6365270ecd (" ARCv2: Implement atomic64 based on LLOCKD/SCONDD instructions").

What about m68k, do you have any preference of earliest kernel version where this fix might be useful?

Given m68k is 32-bit, it will access atomic64_t variables while holding a spinlock,
so it should still be safe without this change.

Not to mention no one will try etnaviv on m68k ;-)

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#9 lore raw

Hi Geert,

Quoted 10 lines · Geert Uytterhoeven

Hi Alexey,

Quoted 6 lines · Alexey Brodkin
Quoted 5 lines · Geert Uytterhoeven
Quoted 3 lines · Greg KH
Quoted 2 lines · Alexey Brodkin

Depending on ABI "long long" type of a particular 32-bit CPU might be aligned by either word (32-bits) or double word (64-bits).

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Or even 16-bit (on e.g. m68k).

Indeed, thanks for the note!
Will add this in my v3.

Note that in this particular case, the field will probably always be aligned to 4 bytes,
as the struct will be allocated using *alloc().

Well I think maybe it worth mentioning only 32-bit and 64-bit alignments in this particular case because as it was mentioned initially in the comment there're 3 pointers before "data" field and for 32-bit machines I guess we may safely assume that a pointer size is 32-bit.

This leaves us only one problematic situation 32-bit instead of 64-bit alignment.

Quoted 10 lines
Quoted 6 lines

For ARC I'd like this fix to be back-ported starting
from v4.8 where we added support of "native" atomic64_t, see commit ce6365270ecd (" ARCv2: Implement atomic64 based on LLOCKD/SCONDD instructions").

What about m68k, do you have any preference of earliest kernel version where this fix might be useful?

Given m68k is 32-bit, it will access atomic64_t variables while holding a spinlock,
so it should still be safe without this change.

Well ARC and ARMv7 are 32-bit machines as well still both have a special instructions to read/write 64-bit data.

Quoted 1 line

Not to mention no one will try etnaviv on m68k ;-)

See it was just a trigger case but other GPUs that use or will use DRM GPU scheduler are good candidates to it that problem and not to mention some other users of atomic64_t.

But from you above comment I may deduce that m68k doesn't have instructions for 64-bit data; in that case there's no reason to worry at least about struct devres_node :)

-Alexey

#10 lore raw

Hi Alexey,

Quoted 32 lines · Alexey Brodkin
Quoted 8 lines · Geert Uytterhoeven
Quoted 6 lines · Alexey Brodkin
Quoted 5 lines · Geert Uytterhoeven
Quoted 3 lines · Greg KH
Quoted 2 lines · Alexey Brodkin

Depending on ABI "long long" type of a particular 32-bit CPU might be aligned by either word (32-bits) or double word (64-bits).

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Or even 16-bit (on e.g. m68k).

Indeed, thanks for the note!
Will add this in my v3.

Note that in this particular case, the field will probably always be aligned to 4 bytes,
as the struct will be allocated using *alloc().

Well I think maybe it worth mentioning only 32-bit and 64-bit alignments in this particular case because as it was mentioned initially in the comment there're 3 pointers before "data" field and for 32-bit machines I guess we may safely assume that a pointer size is 32-bit.

This leaves us only one problematic situation 32-bit instead of 64-bit alignment.

Quoted 7 lines
Quoted 6 lines

For ARC I'd like this fix to be back-ported starting
from v4.8 where we added support of "native" atomic64_t, see commit ce6365270ecd (" ARCv2: Implement atomic64 based on LLOCKD/SCONDD instructions").

What about m68k, do you have any preference of earliest kernel version where this fix might be useful?

Quoted 6 lines
Quoted 3 lines

Given m68k is 32-bit, it will access atomic64_t variables while holding a spinlock,
so it should still be safe without this change.

Well ARC and ARMv7 are 32-bit machines as well still both have a special instructions to read/write 64-bit data.

Sure.

Quoted 9 lines
Quoted 1 line

Not to mention no one will try etnaviv on m68k ;-)

See it was just a trigger case but other GPUs that use or will use DRM GPU scheduler are good candidates to it that problem and not to mention some other users of atomic64_t.

But from you above comment I may deduce that m68k doesn't have instructions for 64-bit data; in that case there's no reason to worry at least about struct devres_node :)

That's correct: m68k has no instructions for 64-bit data.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#11 lore raw
In reply to Alexey Brodkin

From: Alexey Brodkin

Quoted 5 lines

Sent: 09 July 2018 05:45
Depending on ABI "long long" type of a particular 32-bit CPU might be aligned by either word (32-bits) or double word (64-bits). Make sure "data" is really 64-bit aligned for any 32-bit CPU.

Quoted 7 lines

At least for 32-bit ARC cores ABI requires "long long" types to be aligned by normal 32-bit word. This makes "data" field aligned to 12 bytes. Which is still OK as long as we use 32-bit data only.

But once we want to use native atomic64_t type (i.e. when we use special instructions LLOCKD/SCONDD for accessing 64-bit data) we easily hit misaligned access exception.

Shouldn't there be a typedef for the actual type. Perhaps it is even atomic64_t ?
And have the __aligned(8) applied to that typedef ??

Quoted 2 lines

That's because even on CPUs capable of non-aligned data access LL/SC instructions require strict alignment.

...

drivers/base/devres.c struct devres_node { · line 30

Quoted 12 lines
drivers/base/devres.c +4 -2
@@ -24,8 +24,12 @@ struct devres_node {
 
 struct devres {
 	struct devres_node		node;
-	/* -- 3 pointers */
-	unsigned long long		data[];	/* guarantee ull alignment */
+	/*
+	 * Depending on ABI "long long" type of a particular 32-bit CPU
+	 * might be aligned by either word (32-bits) or double word (64-bits).
+	 * Make sure "data" is really 64-bit aligned for any 32-bit CPU.

Just:
/* data[] must be 64 bit aligned even on 32 bit architectures
* because it might be accessed by instructions that require
* aligned memory arguments.

drivers/base/devres.c struct devres_node { · line 32

Quoted 2 lines
drivers/base/devres.c +2 -0
+	 */
+	unsigned long long		data[] __aligned(sizeof(unsigned long long));

One day assuming that 'unsigned long long' is exactly 64 bits will bite. This probably ought to be u64 (or similar).
(If not atomic64_t)

David

#12 lore raw
Quoted 10 lines · David Laight

From: Alexey Brodkin

Quoted 9 lines

Sent: 09 July 2018 05:45
Depending on ABI "long long" type of a particular 32-bit CPU might be aligned by either word (32-bits) or double word (64-bits). Make sure "data" is really 64-bit aligned for any 32-bit CPU.

At least for 32-bit ARC cores ABI requires "long long" types to be aligned by normal 32-bit word. This makes "data" field aligned to 12 bytes. Which is still OK as long as we use 32-bit data only.

Quoted 7 lines
Quoted 3 lines

But once we want to use native atomic64_t type (i.e. when we use special instructions LLOCKD/SCONDD for accessing 64-bit data) we easily hit misaligned access exception.

Shouldn't there be a typedef for the actual type. Perhaps it is even atomic64_t ?
And have the __aligned(8) applied to that typedef ??

That indeed sounds like the best thing to do, as it will fix this issue in other places, too.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#13 lore raw

From: Geert Uytterhoeven

Quoted 16 lines

Sent: 09 July 2018 10:23

Quoted 14 lines · David Laight

From: Alexey Brodkin

Quoted 12 lines

Sent: 09 July 2018 05:45
Depending on ABI "long long" type of a particular 32-bit CPU might be aligned by either word (32-bits) or double word (64-bits). Make sure "data" is really 64-bit aligned for any 32-bit CPU.

At least for 32-bit ARC cores ABI requires "long long" types to be aligned by normal 32-bit word. This makes "data" field aligned to 12 bytes. Which is still OK as long as we use 32-bit data only.

But once we want to use native atomic64_t type (i.e. when we use special instructions LLOCKD/SCONDD for accessing 64-bit data) we easily hit misaligned access exception.

Quoted 6 lines
Quoted 3 lines

Shouldn't there be a typedef for the actual type. Perhaps it is even atomic64_t ?
And have the __aligned(8) applied to that typedef ??

That indeed sounds like the best thing to do, as it will fix this issue in other places, too.

Something like:

typedef struct {
	u64 val __aligned(8);
} atomic64_t;

would pick up most errors.
Including all the places that fail to use atomic_read().

David

#14 lore raw
In reply to David Laight

Hi David,

Quoted 10 lines · David Laight

From: Alexey Brodkin

Quoted 9 lines

Sent: 09 July 2018 05:45
Depending on ABI "long long" type of a particular 32-bit CPU might be aligned by either word (32-bits) or double word (64-bits). Make sure "data" is really 64-bit aligned for any 32-bit CPU.

At least for 32-bit ARC cores ABI requires "long long" types to be aligned by normal 32-bit word. This makes "data" field aligned to 12 bytes. Which is still OK as long as we use 32-bit data only.

Quoted 7 lines
Quoted 3 lines

But once we want to use native atomic64_t type (i.e. when we use special instructions LLOCKD/SCONDD for accessing 64-bit data) we easily hit misaligned access exception.

Shouldn't there be a typedef for the actual type. Perhaps it is even atomic64_t ?
And have the __aligned(8) applied to that typedef ??

That's a good idea indeed but it doesn't solve the problem with struct devres_node. Consider the following snippet:
-------------------------------->8-------------------------------

	struct mystruct {
		atomic64_t myvar;
	}

	struct mystruct *p;
	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);

-------------------------------->8-------------------------------

Here myvar address will match address of "data" member of struct devres_node. So if "data" is has offset of 12 bytes from the beginning of a page then myvar won't be 64-bit aligned regardless of myvar's attribute, right?

Quoted 22 lines
Quoted 2 lines

That's because even on CPUs capable of non-aligned data access LL/SC instructions require strict alignment.

...

drivers/base/devres.c struct devres_node { · line 30

Quoted 12 lines
drivers/base/devres.c +4 -2
@@ -24,8 +24,12 @@ struct devres_node {
 
 struct devres {
 	struct devres_node		node;
-	/* -- 3 pointers */
-	unsigned long long		data[];	/* guarantee ull alignment */
+	/*
+	 * Depending on ABI "long long" type of a particular 32-bit CPU
+	 * might be aligned by either word (32-bits) or double word (64-bits).
+	 * Make sure "data" is really 64-bit aligned for any 32-bit CPU.

Just:
/* data[] must be 64 bit aligned even on 32 bit architectures
* because it might be accessed by instructions that require
* aligned memory arguments.

Quoted 5 lines

drivers/base/devres.c struct devres_node { · line 32

Quoted 2 lines
drivers/base/devres.c +2 -0
+	 */
+	unsigned long long		data[] __aligned(sizeof(unsigned long long));

One day assuming that 'unsigned long long' is exactly 64 bits will bite. This probably ought to be u64 (or similar).

I agree. Initially I wanted to keep as few changes as possible but IMHO switching to more predictable data type makes sense.

-Alexey

#15 lore raw

From: Alexey Brodkin

Quoted 1 line

Sent: 09 July 2018 11:00

...

Quoted 14 lines

That's a good idea indeed but it doesn't solve the problem with struct devres_node. Consider the following snippet:
-------------------------------->8-------------------------------

	struct mystruct {
		atomic64_t myvar;
	}

	struct mystruct *p;
	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);

-------------------------------->8-------------------------------

Here myvar address will match address of "data" member of struct devres_node. So if "data" is has offset of 12 bytes from the beginning of a page then myvar won't be 64-bit aligned regardless of myvar's attribute, right?

...

Quoted 1 line
Quoted 1 line

drivers/base/devres.c struct devres_node { · line 28

Quoted 1 line
drivers/base/devres.c +0 -1
-	unsigned long long		data[];	/* guarantee ull alignment */

Ahh, that line should be:

	u8 data[] __aligned(8); /* Guarantee 64bit alignment */

	David
#16 lore raw

Hi David,

Quoted 19 lines · David Laight

From: Alexey Brodkin

Quoted 1 line

Sent: 09 July 2018 11:00

...

Quoted 9 lines

That's a good idea indeed but it doesn't solve the problem with struct devres_node. Consider the following snippet:
-------------------------------->8-------------------------------

	struct mystruct {
		atomic64_t myvar;
	}

	struct mystruct *p;
	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
Quoted 5 lines

-------------------------------->8-------------------------------

Here myvar address will match address of "data" member of struct devres_node. So if "data" is has offset of 12 bytes from the beginning of a page then myvar won't be 64-bit aligned regardless of myvar's attribute, right?

Quoted 5 lines

...

Quoted 1 line
Quoted 1 line

drivers/base/devres.c struct devres_node { · line 28

Quoted 1 line
drivers/base/devres.c +0 -1
-	unsigned long long		data[];	/* guarantee ull alignment */

Ahh, that line should be:

	u8 data[] __aligned(8); /* Guarantee 64bit alignment */

And that pretty much what I suggested in my initial patch :)

For the record x86 has exactly the same atomic64_t as you suggested,
see https://elixir.bootlin.com/linux/latest/source/arch/x86/include/asm/atomic64_32.h#L13:
---------------------->8------------------

typedef struct {
	u64 __aligned(8) counter;
} atomic64_t;

---------------------->8------------------

-Alexey

#17 lore raw
Quoted 30 lines · Alexey Brodkin

Hi David,

Quoted 19 lines · David Laight

From: Alexey Brodkin

Quoted 1 line

Sent: 09 July 2018 11:00

...

Quoted 9 lines

That's a good idea indeed but it doesn't solve the problem with struct devres_node. Consider the following snippet:
-------------------------------->8-------------------------------

	struct mystruct {
		atomic64_t myvar;
	}

	struct mystruct *p;
	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
Quoted 5 lines

-------------------------------->8-------------------------------

Here myvar address will match address of "data" member of struct devres_node. So if "data" is has offset of 12 bytes from the beginning of a page then myvar won't be 64-bit aligned regardless of myvar's attribute, right?

Quoted 5 lines

...

Quoted 1 line
Quoted 1 line

drivers/base/devres.c struct devres_node { · line 28

Quoted 1 line
drivers/base/devres.c +0 -1
-	unsigned long long		data[];	/* guarantee ull alignment */

Ahh, that line should be:

	u8 data[] __aligned(8); /* Guarantee 64bit alignment */

And that pretty much what I suggested in my initial patch :)

Quoted 7 lines

For the record x86 has exactly the same atomic64_t as you suggested,
see https://elixir.bootlin.com/linux/latest/source/arch/x86/include/asm/atomic64_32.h#L13:
---------------------->8------------------

typedef struct {
	u64 __aligned(8) counter;
} atomic64_t;

---------------------->8------------------

And so does the ARC version since when the atomic64_t support was added by commit ce6365270ecd

Also, you should consider using the pre-canned type aligned_u64.

typedef struct {
       aligned_u64 counter;
       ^^^^^^^^^^^
} atomic64_t;
#18 lore raw

Hi Vineet,

Quoted 42 lines · Vineet Gupta
Quoted 30 lines · Alexey Brodkin

Hi David,

Quoted 19 lines · David Laight

From: Alexey Brodkin

Quoted 1 line

Sent: 09 July 2018 11:00

...

Quoted 9 lines

That's a good idea indeed but it doesn't solve the problem with struct devres_node. Consider the following snippet:
-------------------------------->8-------------------------------

	struct mystruct {
		atomic64_t myvar;
	}

	struct mystruct *p;
	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
Quoted 5 lines

-------------------------------->8-------------------------------

Here myvar address will match address of "data" member of struct devres_node. So if "data" is has offset of 12 bytes from the beginning of a page then myvar won't be 64-bit aligned regardless of myvar's attribute, right?

Quoted 5 lines

...

Quoted 1 line
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -1
-	unsigned long long		data[];	/* guarantee ull alignment */

Ahh, that line should be:

	u8 data[] __aligned(8); /* Guarantee 64bit alignment */

And that pretty much what I suggested in my initial patch :)

Quoted 7 lines

For the record x86 has exactly the same atomic64_t as you suggested,
see https://elixir.bootlin.com/linux/latest/source/arch/x86/include/asm/atomic64_32.h#L13:
---------------------->8------------------

typedef struct {
	u64 __aligned(8) counter;
} atomic64_t;

---------------------->8------------------

And so does the ARC version since when the atomic64_t support was added by commit ce6365270ecd

Quoted 6 lines

Also, you should consider using the pre-canned type aligned_u64.

typedef struct {
       aligned_u64 counter;
       ^^^^^^^^^^^
} atomic64_t;

As Peter mentioned earlier atomic is signed, see:
http://lists.infradead.org/pipermail/linux-snps-arc/2018-July/004030.html

So looks like we need this fix for ARC instead:
--------------------------------->8----------------------------

arch/arc/include/asm/atomic.h +1 -1
index 11859287c52a..ef94b7371de6 100644
@@ -367,7 +367,7 @@ ATOMIC_OPS(xor, ^=, CTOP_INST_AXOR_DI_R2_R2_R3)
  */
 
 typedef struct {
-       aligned_u64 counter;
+       long long __aligned(8) counter;
 } atomic64_t;
 
 #define ATOMIC64_INIT(a) { (a) }

--------------------------------->8----------------------------

-Alexey