public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lib/mpi: Fix kernel unaligned acces in mpi_write_to_sgl
@ 2016-04-21 11:07 Sowmini Varadhan
  2016-04-21 17:23 ` Tadeusz Struk
  0 siblings, 1 reply; 3+ messages in thread
From: Sowmini Varadhan @ 2016-04-21 11:07 UTC (permalink / raw)
  To: sowmini.varadhan, tadeusz.struk, linux-kernel
  Cc: herbert, smueller, mmarek, arnd, andrew.zaborowski


Commit 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers") added
mpi_write_to_sgl() which generates traps due to unaligned
access on some platforms like sparc. Fix this by using
the get_unaligned* and put_unaligned* functions.

Fixes: 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers")
Signed-off-by: Sowmini Varadhan <sowmini.varadhan@oracle.com>
---
 lib/mpi/mpicoder.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c
index eb15e7d..6771378 100644
--- a/lib/mpi/mpicoder.c
+++ b/lib/mpi/mpicoder.c
@@ -21,6 +21,7 @@
 #include <linux/bitops.h>
 #include <linux/count_zeros.h>
 #include "mpi-internal.h"
+#include <asm/unaligned.h>
 
 #define MAX_EXTERN_MPI_BITS 16384
 
@@ -405,10 +406,13 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
 				p -= sizeof(alimb);
 				continue;
 			} else {
-				mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
-				mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
-							+ lzeros;
-				*limb1 = *limb2;
+				mpi_limb_t tmp;
+
+				tmp = get_unaligned_be32((void *)p -
+							 sizeof(alimb) +
+							 lzeros);
+				put_unaligned_be32(tmp,
+						   (void *)p - sizeof(alimb));
 				p -= lzeros;
 				y = lzeros;
 			}
-- 
1.7.1

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

* Re: [PATCH] lib/mpi: Fix kernel unaligned acces in mpi_write_to_sgl
  2016-04-21 11:07 [PATCH] lib/mpi: Fix kernel unaligned acces in mpi_write_to_sgl Sowmini Varadhan
@ 2016-04-21 17:23 ` Tadeusz Struk
  2016-04-21 17:44   ` Sowmini Varadhan
  0 siblings, 1 reply; 3+ messages in thread
From: Tadeusz Struk @ 2016-04-21 17:23 UTC (permalink / raw)
  To: Sowmini Varadhan, linux-kernel
  Cc: herbert, smueller, mmarek, arnd, andrew.zaborowski

On 04/21/2016 04:07 AM, Sowmini Varadhan wrote:
> 
> Commit 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers") added
> mpi_write_to_sgl() which generates traps due to unaligned
> access on some platforms like sparc. Fix this by using
> the get_unaligned* and put_unaligned* functions.
> 
> Fixes: 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers")
> Signed-off-by: Sowmini Varadhan <sowmini.varadhan@oracle.com>
> ---
>  lib/mpi/mpicoder.c |   12 ++++++++----
>  1 files changed, 8 insertions(+), 4 deletions(-)
> 
> diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c
> index eb15e7d..6771378 100644
> --- a/lib/mpi/mpicoder.c
> +++ b/lib/mpi/mpicoder.c
> @@ -21,6 +21,7 @@
>  #include <linux/bitops.h>
>  #include <linux/count_zeros.h>
>  #include "mpi-internal.h"
> +#include <asm/unaligned.h>
>  
>  #define MAX_EXTERN_MPI_BITS 16384
>  
> @@ -405,10 +406,13 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
>  				p -= sizeof(alimb);
>  				continue;
>  			} else {
> -				mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
> -				mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
> -							+ lzeros;
> -				*limb1 = *limb2;
> +				mpi_limb_t tmp;
> +
> +				tmp = get_unaligned_be32((void *)p -
> +							 sizeof(alimb) +
> +							 lzeros);
> +				put_unaligned_be32(tmp,
> +						   (void *)p - sizeof(alimb));
>  				p -= lzeros;
>  				y = lzeros;
>  			}

What if the mpi_limb_t will happen to be 64 bit? 
Thanks,
-- 
TS

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

* Re: [PATCH] lib/mpi: Fix kernel unaligned acces in mpi_write_to_sgl
  2016-04-21 17:23 ` Tadeusz Struk
@ 2016-04-21 17:44   ` Sowmini Varadhan
  0 siblings, 0 replies; 3+ messages in thread
From: Sowmini Varadhan @ 2016-04-21 17:44 UTC (permalink / raw)
  To: Tadeusz Struk
  Cc: linux-kernel, herbert, smueller, mmarek, arnd, andrew.zaborowski

On (04/21/16 10:23), Tadeusz Struk wrote:
> 
> What if the mpi_limb_t will happen to be 64 bit? 
> Thanks,

When I checked this with cscope, I found

typedef unsigned long int mpi_limb_t;

thus I used the *32 functions. 

But you obviously know better, since you wrote this code (and bug).
If you anticipate that mpi_limb_t in some environment today,
I can check for sizeof(mpi_limb_t), and predicate it to
use the *32 or *64 functions based on the result. Do you think that
is necessary?

Regards,
--Sowmini

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

end of thread, other threads:[~2016-04-21 17:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-04-21 11:07 [PATCH] lib/mpi: Fix kernel unaligned acces in mpi_write_to_sgl Sowmini Varadhan
2016-04-21 17:23 ` Tadeusz Struk
2016-04-21 17:44   ` Sowmini Varadhan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox