linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] regmap: fix alignment issue
@ 2020-05-29 19:25 Jens Thoms Toerring
  2020-05-29 22:14 ` Mark Brown
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Jens Thoms Toerring @ 2020-05-29 19:25 UTC (permalink / raw)
  To: Mark Brown, linux-kernel

The assembly and disassembly of data to be sent to or received from a
device invoke functions (regmap_format_XXX() and regmap_parse_XXX())
that extract or insert data items from or into a buffer, using
assignments. In some cases those functions are called with buffer
pointers with odd addresses. On architectures with strict alignment
requirements this results in a kernel crash for u16 and u32 values.
The assignments have been replaced by methods that take alignment
into consideration.

Signed-off-by: Jens Thoms Toerring <jt@toerring.de>
---
 drivers/base/regmap/regmap.c | 38 ++++++++++++++++++--------------------
 1 file changed, 18 insertions(+), 20 deletions(-)

diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 58cfb32..70f470e 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -193,15 +193,15 @@ static void regmap_format_8(void *buf, unsigned int val, unsigned int shift)
 
 static void regmap_format_16_be(void *buf, unsigned int val, unsigned int shift)
 {
-	__be16 *b = buf;
-
-	b[0] = cpu_to_be16(val << shift);
+	put_unaligned_be16(val << shift, buf);
 }
 
 static void regmap_format_16_native(void *buf, unsigned int val,
 				    unsigned int shift)
 {
-	*(u16 *)buf = val << shift;
+	u16 v = val << shift;
+
+	memcpy(buf, &v, sizeof(v));
 }
 
 static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
@@ -217,15 +217,15 @@ static void regmap_format_24(void *buf, unsigned int val, unsigned int shift)
 
 static void regmap_format_32_be(void *buf, unsigned int val, unsigned int shift)
 {
-	__be32 *b = buf;
-
-	b[0] = cpu_to_be32(val << shift);
+	put_unaligned_be32(val << shift, buf);
 }
 
 static void regmap_format_32_native(void *buf, unsigned int val,
 				    unsigned int shift)
 {
-	*(u32 *)buf = val << shift;
+	u32 = val << shift;
+
+	memcpy(buf, &v, sizeof(v));
 }
 
 static unsigned int regmap_parse_8(void *buf)
@@ -237,16 +237,15 @@ static unsigned int regmap_parse_8(void *buf)
 
 static unsigned int regmap_parse_16_be(void *buf)
 {
-	__be16 *b = buf;
-
-	b[0] = be16_to_cpu(b[0]);
-
-	return b[0];
+	return get_unaligned_be16(buf);
 }
 
 static unsigned int regmap_parse_16_native(void *buf)
 {
-	return *(u16 *)buf;
+	u16 v;
+
+	memcpy(&v, buf, sizeof(v));
+	return v;
 }
 
 static unsigned int regmap_parse_24(void *buf)
@@ -261,17 +260,15 @@ static unsigned int regmap_parse_24(void *buf)
 
 static unsigned int regmap_parse_32_be(void *buf)
 {
-	__be32 *b = buf;
-
-	b[0] = be32_to_cpu(b[0]);
-
-	return b[0];
+	return get_unaligned_be32(buf);
 }
 
 static unsigned int regmap_parse_32_native(void *buf)
 {
-	return *(u32 *)buf;
+	u32 v;
+
+	memcpy(&v, buf, sizeof(v));
+	return v;
 }
 
 static void regmap_lock_mutex(void *__map)
-- 
1.9.1


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

* Re: [PATCH v3] regmap: fix alignment issue
  2020-05-29 19:25 [PATCH v3] regmap: fix alignment issue Jens Thoms Toerring
@ 2020-05-29 22:14 ` Mark Brown
  2020-05-29 22:20 ` Mark Brown
  2020-06-01 11:54 ` Mark Brown
  2 siblings, 0 replies; 4+ messages in thread
From: Mark Brown @ 2020-05-29 22:14 UTC (permalink / raw)
  To: Jens Thoms Toerring; +Cc: linux-kernel

[-- Attachment #1: Type: text/plain, Size: 248 bytes --]

On Fri, May 29, 2020 at 09:25:38PM +0200, Jens Thoms Toerring wrote:

> pointers with odd addresses. On architectures with strict alignment
> requirements this results in a kernel crash for u16 and u32 values.

Which architectures out of interest?

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH v3] regmap: fix alignment issue
  2020-05-29 19:25 [PATCH v3] regmap: fix alignment issue Jens Thoms Toerring
  2020-05-29 22:14 ` Mark Brown
@ 2020-05-29 22:20 ` Mark Brown
  2020-06-01 11:54 ` Mark Brown
  2 siblings, 0 replies; 4+ messages in thread
From: Mark Brown @ 2020-05-29 22:20 UTC (permalink / raw)
  To: Jens Thoms Toerring; +Cc: linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1114 bytes --]

On Fri, May 29, 2020 at 09:25:38PM +0200, Jens Thoms Toerring wrote:
> The assembly and disassembly of data to be sent to or received from a
> device invoke functions (regmap_format_XXX() and regmap_parse_XXX())
> that extract or insert data items from or into a buffer, using
> assignments. In some cases those functions are called with buffer

This doesn't apply against current code, please check and resend:

HEAD is now at 93b929922dba Merge series "regmap: provide simple bitops and use them in a driver" from Bartosz Golaszewski <brgl@bgdev.pl> Bartosz Golaszewski <bgolaszewski@baylibre.com>:
Applying: regmap: fix alignment issue
Using index info to reconstruct a base tree...
M	drivers/base/regmap/regmap.c
error: patch failed: drivers/base/regmap/regmap.c:261
error: drivers/base/regmap/regmap.c: patch does not apply
error: Did you hand edit your patch?
It does not apply to blobs recorded in its index.
Patch failed at 0001 regmap: fix alignment issue

(it also didn't apply against v5.7-rc1 and git didn't know the blobs it
was based on, the most recent change to that code was in 2015 it
seems...).

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH v3] regmap: fix alignment issue
  2020-05-29 19:25 [PATCH v3] regmap: fix alignment issue Jens Thoms Toerring
  2020-05-29 22:14 ` Mark Brown
  2020-05-29 22:20 ` Mark Brown
@ 2020-06-01 11:54 ` Mark Brown
  2 siblings, 0 replies; 4+ messages in thread
From: Mark Brown @ 2020-06-01 11:54 UTC (permalink / raw)
  To: Jens Thoms Toerring, linux-kernel

On Fri, 29 May 2020 21:25:38 +0200, Jens Thoms Toerring wrote:
> The assembly and disassembly of data to be sent to or received from a
> device invoke functions (regmap_format_XXX() and regmap_parse_XXX())
> that extract or insert data items from or into a buffer, using
> assignments. In some cases those functions are called with buffer
> pointers with odd addresses. On architectures with strict alignment
> requirements this results in a kernel crash for u16 and u32 values.
> The assignments have been replaced by methods that take alignment
> into consideration.

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git for-next

Thanks!

[1/1] regmap: fix alignment issue
      commit: 53d860952c8215cf9ae1ea33409c8cb71ad6ad3d

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark

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

end of thread, other threads:[~2020-06-01 11:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-29 19:25 [PATCH v3] regmap: fix alignment issue Jens Thoms Toerring
2020-05-29 22:14 ` Mark Brown
2020-05-29 22:20 ` Mark Brown
2020-06-01 11:54 ` Mark Brown

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).