diff mbox

[v3,1/1] IB/mlx4: Unaligned access in send_reply_to_slave

Message ID 1463566690-22881-1-git-send-email-shamir.rabinovitch@oracle.com (mailing list archive)
State Accepted
Headers show

Commit Message

Shamir Rabinovitch May 18, 2016, 10:18 a.m. UTC
From: shamir rabinovitch <shamir.rabinovitch@oracle.com>

The problem is that the function 'send_reply_to_slave' get the
'req_sa_mad' as pointer whose address can be unaligned to 8 bytes.
In this case the compiler cannot know in advance what will be the
alignment of the 'data' field.

Sowmini Varadhan pointed to this reply from Dave Miller that say
that memcpy should not be used to solve alignment issues:
https://lkml.org/lkml/2015/10/21/352

Optimization of memcpy to 'ldx' instruction can only happen if the
compiler see that the size of the data we copy is 8 bytes and it
assume it is aligned to 8. If the compiler know the type is not
aligned to 8 it must not optimize the 8 bytes copy. Defining the
data type as aligned to 4 force the compiler to assume it is not
aligned to 8 at all time.

Full credit for the idea goes to Jason Gunthorpe
<jgunthorpe@obsidianresearch.com>.

v3: Change patch according to suggestion by Jason Gunthorpe
<jgunthorpe@obsidianresearch.com>

v2: Minor fix in patch headline by Or Gerlitz <gerlitz.or@gmail.com>

Signed-off-by: shamir rabinovitch <shamir.rabinovitch@oracle.com>
---
 drivers/infiniband/hw/mlx4/mcg.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

Comments

Yuval Shaia May 18, 2016, 10:30 a.m. UTC | #1
On Wed, May 18, 2016 at 06:18:10AM -0400, Shamir Rabinovitch wrote:
> From: shamir rabinovitch <shamir.rabinovitch@oracle.com>
> 
> The problem is that the function 'send_reply_to_slave' get the
> 'req_sa_mad' as pointer whose address can be unaligned to 8 bytes.
> In this case the compiler cannot know in advance what will be the
> alignment of the 'data' field.
> 
> Sowmini Varadhan pointed to this reply from Dave Miller that say
> that memcpy should not be used to solve alignment issues:
> https://lkml.org/lkml/2015/10/21/352
> 
> Optimization of memcpy to 'ldx' instruction can only happen if the
> compiler see that the size of the data we copy is 8 bytes and it
> assume it is aligned to 8. If the compiler know the type is not
> aligned to 8 it must not optimize the 8 bytes copy. Defining the
> data type as aligned to 4 force the compiler to assume it is not
> aligned to 8 at all time.
> 
> Full credit for the idea goes to Jason Gunthorpe
> <jgunthorpe@obsidianresearch.com>.
You can use Suggested-By tag for that purpose.
> 
> v3: Change patch according to suggestion by Jason Gunthorpe
> <jgunthorpe@obsidianresearch.com>
> 
> v2: Minor fix in patch headline by Or Gerlitz <gerlitz.or@gmail.com>
> 
> Signed-off-by: shamir rabinovitch <shamir.rabinovitch@oracle.com>
s/shamir/Shamir
s/rabinovitch/Rabinovitch
> ---
>  drivers/infiniband/hw/mlx4/mcg.c |    2 +-
>  1 files changed, 1 insertions(+), 1 deletions(-)
> 
> diff --git a/drivers/infiniband/hw/mlx4/mcg.c b/drivers/infiniband/hw/mlx4/mcg.c
> index 99451d8..0550017 100644
> --- a/drivers/infiniband/hw/mlx4/mcg.c
> +++ b/drivers/infiniband/hw/mlx4/mcg.c
> @@ -96,7 +96,7 @@ struct ib_sa_mcmember_data {
>  	u8		scope_join_state;
>  	u8		proxy_join;
>  	u8		reserved[2];
> -};
> +} __packed __aligned(4);
>  
>  struct mcast_group {
>  	struct ib_sa_mcmember_data rec;
> -- 
> 1.7.1
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Doug Ledford May 18, 2016, 2:41 p.m. UTC | #2
On 05/18/2016 06:18 AM, shamir.rabinovitch@oracle.com wrote:
> From: shamir rabinovitch <shamir.rabinovitch@oracle.com>
> 
> The problem is that the function 'send_reply_to_slave' get the
> 'req_sa_mad' as pointer whose address can be unaligned to 8 bytes.
> In this case the compiler cannot know in advance what will be the
> alignment of the 'data' field.
> 
> Sowmini Varadhan pointed to this reply from Dave Miller that say
> that memcpy should not be used to solve alignment issues:
> https://lkml.org/lkml/2015/10/21/352
> 
> Optimization of memcpy to 'ldx' instruction can only happen if the
> compiler see that the size of the data we copy is 8 bytes and it
> assume it is aligned to 8. If the compiler know the type is not
> aligned to 8 it must not optimize the 8 bytes copy. Defining the
> data type as aligned to 4 force the compiler to assume it is not
> aligned to 8 at all time.
> 
> Full credit for the idea goes to Jason Gunthorpe
> <jgunthorpe@obsidianresearch.com>.
> 
> v3: Change patch according to suggestion by Jason Gunthorpe
> <jgunthorpe@obsidianresearch.com>
> 
> v2: Minor fix in patch headline by Or Gerlitz <gerlitz.or@gmail.com>
> 
> Signed-off-by: shamir rabinovitch <shamir.rabinovitch@oracle.com>

I considerably reworked the commit message, but otherwise applied.  Thanks.
diff mbox

Patch

diff --git a/drivers/infiniband/hw/mlx4/mcg.c b/drivers/infiniband/hw/mlx4/mcg.c
index 99451d8..0550017 100644
--- a/drivers/infiniband/hw/mlx4/mcg.c
+++ b/drivers/infiniband/hw/mlx4/mcg.c
@@ -96,7 +96,7 @@  struct ib_sa_mcmember_data {
 	u8		scope_join_state;
 	u8		proxy_join;
 	u8		reserved[2];
-};
+} __packed __aligned(4);
 
 struct mcast_group {
 	struct ib_sa_mcmember_data rec;