netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
To: Parav.Pandit-iH1Dq9VlAzfQT0dZR+AlfA@public.gmane.org
Cc: David.Laight-ZS65k/vG3HxXrIkS9f7CXA@public.gmane.org,
	roland-BHEL68pLQRGGvPXPguhicg@public.gmane.org,
	linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	netdev-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Re: [PATCH 2/9] ocrdma: Driver for Emulex OneConnect RDMA adapter
Date: Thu, 22 Mar 2012 16:44:29 -0600	[thread overview]
Message-ID: <20120322224429.GA12980@obsidianresearch.com> (raw)
In-Reply-To: <3ae9829d-f8dd-4268-918a-94616eff0915-nbYkmrCdWxmgMrCBcu8zE0EOCMrvLtNR@public.gmane.org>

On Thu, Mar 22, 2012 at 02:20:28PM -0700, Parav.Pandit-iH1Dq9VlAzfQT0dZR+AlfA@public.gmane.org wrote:
> I got a question here lately.
> 
> aligned directive will ensure that it will fall on boundary.  Say
> aligned(4) ensures that structure is aligned to 4 byte boundary.
> Compiler can (at least theoretically) still have 4 byte structure
> aligned to 8 byte boundary on 64-bit platform (which is 4 byte
> aligned too).

There are very specific rules defined in the platform's ABI for how C
structures are layed out in memory, each ABI (ie CPU) has its own
specific quirks, but broadly in Linux land you can boil it down to:

1) The alignment of a structure is the greatest alignment of all the
   members
2) Each member is aligned to its alignment.

The alignment of the structure drives the total size of the structure,
and specifically the padding added at the end to reach that alignment.

So, no, a compiler that increased the alignment of a struct with one
u32 to 8 would violate the various ABIs and not be usuable for
Linux. It is important to bear in mind that Linux targets a particular
set of ABI conventions, and it is not 'anything goes'.

> struct {
> 	u32 field;
> };

So in this case: the u32 is aligned to 4, the structure is aligned to
4 and the total size of the structure is 4 on everything linux
supports.

> struct {
>       u64 fielda
> 	u32 field;
> };

In this case: On 64 bit: the u64 is aligned to 8 and the u32 is aligned to 4. So
the structure is aligned to 8. A pad is inserted at the end of the
struct to bring it out. On 32 bit, the u64 is aligned to 4, so the
struct is aligned to 4, so no pad is added.
 
> struct {
>       __float128 fielda
> 	u32 field;
> };

In this case the float128 is aligned to 16 and thus the structure is
aligned to 16 and 12 pad bytes are added.

> However requirement is to have this structure only 4 byte size(
> because adapter excepts it to be 4B sise) and therefor packed is
> used.  I don't know the way to ensure size of 4 byte and alignment
> too.  Or I am misunderstanding?

Yes, you are mis-understanding the rules for padding.. Structures are
only padded out to their alignment, which depends on their constituent
types. This is so arrays of structures have each array element
starting on its natural alignment.

The aligned attribute overrides the automatic determination of the
alignment based on the contents and just forces it.

So, as an example, if you have this hardware layout:

struct {
  u32 fielda;
  u64 fieldb;
} attribute ((aligned(4));

David is saying you will get a 12 byte struct and fieldb will be
unaligned. Since 12 is aligned to 4 no padding is added.

For hardware facing structures I'd combine this with a static assert
to verify structure size at compile time.

So..

1) Avoid using attributes unless the structure has unaligned members.
2) Avoid creating structures with unaligned members (eg for userspace
   communication)
3) Frown at hardware/firmware developers who make communication
   structures with unaligned members :)
4) Be explicit about padding in your layout for 64/32
   compatibility.

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2012-03-22 22:44 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1332283154-16369-1-git-send-email-parav.pandit@emulex.com>
     [not found] ` <1332283154-16369-2-git-send-email-parav.pandit@emulex.com>
     [not found]   ` <24c5b654-d6a5-418d-8187-fba4ad47a3ce@exht1.ad.emulex.com>
     [not found]     ` <24c5b654-d6a5-418d-8187-fba4ad47a3ce-nbYkmrCdWxmgMrCBcu8zE0EOCMrvLtNR@public.gmane.org>
2012-03-21 16:20       ` [PATCH 2/9] ocrdma: Driver for Emulex OneConnect RDMA adapter Roland Dreier
     [not found]         ` <CAL1RGDVxCE--P78bk0Me5o+ekSzgBYG0UJT6y3O7cK3mUGBjuQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-03-21 16:31           ` David Laight
     [not found]             ` <AE90C24D6B3A694183C094C60CF0A2F6026B6EB4-CgBM+Bx2aUAnGFn1LkZF6NBPR1lH4CV8@public.gmane.org>
2012-03-22 20:52               ` Parav.Pandit-iH1Dq9VlAzfQT0dZR+AlfA
     [not found]                 ` <88B766C272F2C64B944B21AD078333151C964A66EF-/SwythR3zqxVRK6PHKByhFaTQe2KTcn/@public.gmane.org>
2012-03-22 20:58                   ` Jason Gunthorpe
     [not found]                     ` <20120322205824.GC9614-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2012-03-22 21:10                       ` Parav.Pandit-iH1Dq9VlAzfQT0dZR+AlfA
     [not found]                         ` <88B766C272F2C64B944B21AD078333151C964A670B-/SwythR3zqxVRK6PHKByhFaTQe2KTcn/@public.gmane.org>
2012-03-22 21:20                           ` Parav.Pandit-iH1Dq9VlAzfQT0dZR+AlfA
     [not found]                             ` <3ae9829d-f8dd-4268-918a-94616eff0915-nbYkmrCdWxmgMrCBcu8zE0EOCMrvLtNR@public.gmane.org>
2012-03-22 22:44                               ` Jason Gunthorpe [this message]
     [not found]                                 ` <20120322224429.GA12980-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2012-03-22 23:45                                   ` Roland Dreier
2012-03-23  9:41                                   ` David Laight
2012-03-23 14:03                                   ` Parav.Pandit-iH1Dq9VlAzfQT0dZR+AlfA
     [not found]                                     ` <88B766C272F2C64B944B21AD078333151C964A67E0-/SwythR3zqxVRK6PHKByhFaTQe2KTcn/@public.gmane.org>
2012-03-23 16:54                                       ` Jason Gunthorpe
     [not found]                                         ` <20120323165414.GA15260-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2012-03-23 19:16                                           ` Parav.Pandit-iH1Dq9VlAzfQT0dZR+AlfA
2012-03-21 19:02           ` Parav.Pandit-iH1Dq9VlAzfQT0dZR+AlfA
     [not found]   ` <1332283154-16369-3-git-send-email-parav.pandit@emulex.com>
     [not found]     ` <339d9e05-38fd-45b1-83ed-f06277bd1326@exht1.ad.emulex.com>
     [not found]       ` <339d9e05-38fd-45b1-83ed-f06277bd1326-nbYkmrCdWxmgMrCBcu8zE0EOCMrvLtNR@public.gmane.org>
2012-03-21 16:26         ` [PATCH 3/9] " Roland Dreier
     [not found]           ` <CAL1RGDWnc478=ToFQEC51Usn6LLZgLen=CymFZ0C0GnRp9BAsw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-03-21 16:33             ` David Laight
2012-03-21 19:04             ` Parav.Pandit-iH1Dq9VlAzfQT0dZR+AlfA
     [not found]               ` <88B766C272F2C64B944B21AD078333151C964A63F1-/SwythR3zqxVRK6PHKByhFaTQe2KTcn/@public.gmane.org>
2012-03-21 19:33                 ` Roland Dreier
     [not found]                   ` <CAL1RGDWmz4Yr8mqWMywN9X+refDEJ4W=ieBvOtZZc32qMDA5_w-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-03-21 20:54                     ` Jason Gunthorpe
     [not found]     ` <1332283154-16369-4-git-send-email-parav.pandit@emulex.com>
     [not found]       ` <a5e59a7c-d6ff-4c78-89a7-fad7492260b0@exht1.ad.emulex.com>
     [not found]         ` <a5e59a7c-d6ff-4c78-89a7-fad7492260b0-nbYkmrCdWxmgMrCBcu8zE0EOCMrvLtNR@public.gmane.org>
2012-03-21 16:34           ` [PATCH 4/9] " Roland Dreier
2012-03-21 19:09             ` Parav.Pandit
     [not found]               ` <88B766C272F2C64B944B21AD078333151C964A63FB-/SwythR3zqxVRK6PHKByhFaTQe2KTcn/@public.gmane.org>
2012-03-21 19:31                 ` Roland Dreier
     [not found]                   ` <CAL1RGDWrwP3mY2=W42_c5wpefzqx_BnXhnwfy2fPrP=12hrBOw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-03-21 19:46                     ` Parav.Pandit-iH1Dq9VlAzfQT0dZR+AlfA
     [not found]       ` <1332283154-16369-5-git-send-email-parav.pandit@emulex.com>
     [not found]         ` <1332283154-16369-6-git-send-email-parav.pandit@emulex.com>
     [not found]           ` <5abe3043-81f8-448a-9e55-b29e23f4eb9a@exht1.ad.emulex.com>
     [not found]             ` <5abe3043-81f8-448a-9e55-b29e23f4eb9a-nbYkmrCdWxmgMrCBcu8zE0EOCMrvLtNR@public.gmane.org>
2012-03-21 16:42               ` [PATCH 6/9] " Roland Dreier
2012-03-21 19:10                 ` Parav.Pandit-iH1Dq9VlAzfQT0dZR+AlfA
     [not found] ` <3f46a051-ee2e-4e18-becf-60f6c023c3c6@exht1.ad.emulex.com>
     [not found]   ` <3f46a051-ee2e-4e18-becf-60f6c023c3c6-nbYkmrCdWxmgMrCBcu8zE0EOCMrvLtNR@public.gmane.org>
2012-03-21 16:14     ` [PATCH 1/9] " Roland Dreier
2012-03-21 18:58       ` Parav.Pandit
2012-03-21 16:45     ` Roland Dreier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120322224429.GA12980@obsidianresearch.com \
    --to=jgunthorpe-epgobjl8dl3ta4ec/59zmfatqe2ktcn/@public.gmane.org \
    --cc=David.Laight-ZS65k/vG3HxXrIkS9f7CXA@public.gmane.org \
    --cc=Parav.Pandit-iH1Dq9VlAzfQT0dZR+AlfA@public.gmane.org \
    --cc=linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=netdev-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=roland-BHEL68pLQRGGvPXPguhicg@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).