linux-arch.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] devres: Explicitly align datai[] to 64-bit
@ 2018-07-09 13:45 Alexey Brodkin
  2018-07-09 13:45 ` Alexey Brodkin
                   ` (2 more replies)
  0 siblings, 3 replies; 38+ messages in thread
From: Alexey Brodkin @ 2018-07-09 13:45 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-snps-arc, linux-arch, Alexey Brodkin, Greg Kroah-Hartman,
	Geert Uytterhoeven, David Laight, Peter Zijlstra,
	Thomas Gleixner, Will Deacon, Greg KH, stable

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

One example is "atomic64_t" type accessed by special atomic instructions
which may read/write entire 64-bit word.

Atomic instructions are a bit special 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.

The problem with previously used approach is that depending on ABI
"long long" type of a particular 32-bit CPU might be aligned to
8-, 16-, 32- or 64-bit boundary. Which will get in the way of mentioned
above atomic instructions.

Consider the following snippet:
|        struct mystruct {
|                atomic64_t myvar;
|        }
|
|        struct mystruct *p;
|        p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);

Here address of "myvar" will match  data[] in "struct devres",
that said if "data" is not 64-bit aligned atomic instruction will
fail on the first access to "myvar".

Signed-off-by: Alexey Brodkin <abrodkin@synopsys.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: David Laight <David.Laight@ACULAB.COM>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Will Deacon <will.deacon@arm.com>
Cc: Greg KH <greg@kroah.com>
Cc: <stable@vger.kernel.org> # 4.8+
---

Changes v2 -> v3:

 * Align explicitly to 8 bytes [David]
 * Rephrased in-line comment [David]
 * Added more techinical details to commit message [Greg]
 * Mention more alignment options in commit message [Geert]

Changes v1 -> v2:

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

 drivers/base/devres.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index f98a097e73f2..d65327cb83c9 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -24,8 +24,12 @@ struct devres_node {
 
 struct devres {
 	struct devres_node		node;
-	/* -- 3 pointers */
-	unsigned long long		data[];	/* guarantee ull alignment */
+	/*
+	 * data[] must be 64 bit aligned even on 32 bit architectures
+	 * because it might be accessed by instructions that require
+	 * aligned memory arguments such as atomic64_t.
+	 */
+	u8 __aligned(8)			data[];
 };
 
 struct devres_group {
-- 
2.17.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

end of thread, other threads:[~2018-10-04 23:13 UTC | newest]

Thread overview: 38+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-09 13:45 [PATCH v3] devres: Explicitly align datai[] to 64-bit Alexey Brodkin
2018-07-09 13:45 ` Alexey Brodkin
2018-07-09 13:54 ` Peter Zijlstra
2018-07-09 13:54   ` Peter Zijlstra
2018-07-09 14:04   ` Mark Rutland
2018-07-09 14:04     ` Mark Rutland
2018-07-09 14:10     ` Geert Uytterhoeven
2018-07-09 14:10       ` Geert Uytterhoeven
2018-07-09 14:07   ` Alexey Brodkin
2018-07-09 14:07     ` Alexey Brodkin
2018-07-09 14:07 ` Peter Zijlstra
2018-07-09 14:07   ` Peter Zijlstra
2018-07-09 14:10   ` Peter Zijlstra
2018-07-09 14:10     ` Peter Zijlstra
2018-07-09 14:33     ` Alexey Brodkin
2018-07-09 14:33       ` Alexey Brodkin
2018-07-09 14:49       ` Peter Zijlstra
2018-07-09 14:49         ` Peter Zijlstra
2018-07-09 14:53         ` Alexey Brodkin
2018-07-09 14:53           ` Alexey Brodkin
2018-10-04 16:19           ` Alexey Brodkin
2018-10-04 16:19             ` Alexey Brodkin
2018-07-09 15:02         ` David Laight
2018-07-09 15:02           ` David Laight
2018-07-09 15:14           ` Peter Zijlstra
2018-07-09 15:14             ` Peter Zijlstra
2018-07-09 15:29         ` Mark Rutland
2018-07-09 15:29           ` Mark Rutland
2018-07-09 15:34           ` Peter Zijlstra
2018-07-09 15:34             ` Peter Zijlstra
2018-07-09 15:45             ` Peter Zijlstra
2018-07-09 15:45               ` Peter Zijlstra
2018-07-09 15:48               ` Mark Rutland
2018-07-09 15:48                 ` Mark Rutland
2018-07-09 16:10                 ` David Laight
2018-07-09 16:10                   ` David Laight
2018-07-09 20:28                   ` Vineet Gupta
2018-07-09 20:28                     ` Vineet Gupta

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).