All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andy Isaacson <adi@hexapodia.org>
To: "David S. Miller" <davem@redhat.com>
Cc: Oliver Neukum <oliver@neukum.org>,
	scott@timesys.com, zaitcev@redhat.com, greg@kroah.com,
	arjanv@redhat.com, jgarzik@redhat.com, tburke@redhat.com,
	linux-kernel@vger.kernel.org, stern@rowland.harvard.edu,
	mdharm-usb@one-eyed-alien.net, david-b@pacbell.net,
	Robert White <rwhite@casabyte.com>
Subject: Re: drivers/block/ub.c
Date: Tue, 29 Jun 2004 13:31:53 -0500	[thread overview]
Message-ID: <20040629183153.GA11558@hexapodia.org> (raw)
In-Reply-To: <20040628191545.7a298bc3.davem@redhat.com> <20040628152208.20fe97f1.davem@redhat.com> <20040628140343.572a0944.davem@redhat.com>

Dave, you seem to be arguing "This is how __packed__ works, therefore
this is how __packed__ works, therefore anything else is now how
__packed__ works".  Oliver is trying to propose *new* semantics which
*differ* from __packed__ in a way that seems useful.

On Mon, Jun 28, 2004 at 02:03:43PM -0700, David S. Miller wrote:
> On Mon, 28 Jun 2004 22:57:11 +0200
> Oliver Neukum <oliver@neukum.org> wrote:
> > Am Montag, 28. Juni 2004 22:25 schrieb David S. Miller:
> > > That's true.  But if one were to propose such a feature to the gcc
> > > guys, I know the first question they would ask.  "If no padding of
> > > the structure is needed, why are you specifying this new
> > > __nopadding__ attribute?"
> > 
> > It would replace some uses of __packed__, where the first element
> > is aligned.
> 
> You have not considered what is supposed to happen when this
> structure is embedded within another one.  What kind of alignment
> rules apply in that case?  For example:
> 
> struct foo { u32 x; u8 y; u16 z; } __attribute__((__packed__));
> struct bar { u8  a; struct foo  b; };
> 
> That is why __packed__ can't assume the alignment of any structure
> instance whatsoever.  Your __nopadding__ attribute proposal would
> lay out struct bar differently in order to meet the alignment guarentees
> you say it will be able to meet.


Here's Oliver's suggestion, as I understand it:
 - a __nopadding__ struct is naturally aligned for its first member.
 - The compiler does not insert alignment into a __nopadding__ struct.
 - From the outside, a __nopadding__ struct does not differ from a
   normal struct (one lacking all attribute()s), except in its size.  So
   your "struct foo" above (with __nopadding__) would be 7 bytes with
   4-byte alignment for the u32.

As proposed, __nopadding__ is better than __packed__ because leading
correctly-aligned elements can be accessed directly with aligned loads
rather than requiring byte-at-a-time loads on platforms such as SPARC.

To answer your question:  a __nopadding__ struct embedded in another
struct will be naturally aligned just as a normal struct with the same
members would have been.  (Possible variation:  align it as necessary
for the first member, treat the rest as "bag 'o bits".)

It's unfortunate that GCC has conflated several not-necessarily-related
features into a single switch.

 1. no padding between elements
 2. no alignment internally
 3. no alignment externally

This results in confusion, as Scott shows below.  Worse, poorly-defined
semantics are a likely source of implementation bugs -- are you
confident that every aspect of __packed__ works the same in every
compiler that understands attribute((packed))?  Including ICC and
gcc-2.6.0?

On Mon, Jun 28, 2004 at 03:22:08PM -0700, David S. Miller wrote:
> On Mon, 28 Jun 2004 17:18:57 -0400
> Scott Wood <scott@timesys.com> wrote:
> > On Mon, Jun 28, 2004 at 02:03:43PM -0700, David S. Miller wrote:
> > > struct foo { u32 x; u8 y; u16 z; } __attribute__((__packed__));
> > > struct bar { u8  a; struct foo  b; };
> > 
> > As long as bar is not packed, why shouldn't the beginning of bar.b be
> > aligned?
> 
> No!  bar.b starts at offset 1 byte.  That's how this stuff works.
> 
> This is exactly why you cannot assume the alignment of any structure
> which is given attribute __packed__.  The example above shows that
> quite clearly.

-andy

  parent reply	other threads:[~2004-06-29 18:32 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-26 20:06 drivers/block/ub.c Pete Zaitcev
2004-06-26 20:12 ` drivers/block/ub.c Matthew Dharm
2004-06-27  2:08   ` drivers/block/ub.c Pete Zaitcev
2004-06-27  3:30     ` drivers/block/ub.c Matthew Dharm
2004-07-12  0:10     ` [usb-storage] drivers/block/ub.c Pat LaVarre
2004-06-26 20:35 ` drivers/block/ub.c Oliver Neukum
2004-06-26 21:41   ` drivers/block/ub.c David S. Miller
2004-06-26 21:56     ` drivers/block/ub.c Oliver Neukum
2004-06-26 22:07       ` drivers/block/ub.c David S. Miller
2004-06-26 22:36         ` drivers/block/ub.c Oliver Neukum
2004-06-26 23:20           ` drivers/block/ub.c David S. Miller
2004-06-27  4:31             ` drivers/block/ub.c Oliver Neukum
2004-06-27  6:34               ` drivers/block/ub.c David S. Miller
2004-06-27 10:42                 ` drivers/block/ub.c Oliver Neukum
2004-06-27 21:26                   ` drivers/block/ub.c David S. Miller
2004-06-28 14:15                     ` drivers/block/ub.c Scott Wood
2004-06-28 20:25                       ` drivers/block/ub.c David S. Miller
2004-06-28 20:48                         ` drivers/block/ub.c Scott Wood
2004-06-28 20:58                           ` drivers/block/ub.c David S. Miller
2004-06-28 20:50                         ` drivers/block/ub.c Matthew Dharm
2004-06-28 20:59                           ` drivers/block/ub.c David S. Miller
2004-06-28 21:01                           ` drivers/block/ub.c Pete Zaitcev
2004-06-28 23:52                             ` drivers/block/ub.c Matthew Dharm
2004-06-28 20:57                         ` drivers/block/ub.c Oliver Neukum
2004-06-28 21:03                           ` drivers/block/ub.c David S. Miller
2004-06-28 21:18                             ` drivers/block/ub.c Scott Wood
2004-06-28 22:22                               ` drivers/block/ub.c David S. Miller
2004-06-28 22:31                                 ` drivers/block/ub.c Scott Wood
2004-06-28 22:40                                 ` drivers/block/ub.c Roland Dreier
2004-06-29  1:54                             ` drivers/block/ub.c Robert White
2004-06-29  2:15                               ` drivers/block/ub.c David S. Miller
2004-06-29  2:49                                 ` drivers/block/ub.c Robert White
2004-06-29 18:31                             ` Andy Isaacson [this message]
2004-07-05 10:01                             ` drivers/block/ub.c Roman Zippel
2004-06-29  7:12                         ` drivers/block/ub.c Vojtech Pavlik
2004-06-29  1:39                     ` drivers/block/ub.c Robert White
2004-06-29 17:02                     ` drivers/block/ub.c Kurt Garloff
2004-06-26 22:54   ` drivers/block/ub.c Andries Brouwer
2004-06-26 22:59     ` drivers/block/ub.c Oliver Neukum
2004-06-26 23:08       ` drivers/block/ub.c Andries Brouwer
2004-06-27  5:04         ` drivers/block/ub.c Oliver Neukum
2004-06-27 14:08           ` drivers/block/ub.c Andries Brouwer
2004-06-27 14:24             ` drivers/block/ub.c Oliver Neukum
2004-06-27 15:19               ` drivers/block/ub.c Alan Stern
2004-06-27 15:45                 ` drivers/block/ub.c Andries Brouwer
2004-06-28 23:58                   ` drivers/block/ub.c Jeff Garzik
2004-06-28  0:10                 ` drivers/block/ub.c Pete Zaitcev
2004-06-28 16:01                   ` drivers/block/ub.c Alan Stern
2004-06-27 15:23               ` drivers/block/ub.c Andries Brouwer
2004-06-27 16:11                 ` drivers/block/ub.c Oliver Neukum
2004-06-26 22:46 ` drivers/block/ub.c Oliver Neukum
2004-06-27  3:52   ` drivers/block/ub.c Alan Stern
2004-06-27  4:05 ` drivers/block/ub.c Alan Stern
2004-06-27  5:02   ` drivers/block/ub.c Greg KH
2004-06-27 15:23     ` drivers/block/ub.c Alan Stern
2004-06-27 20:29       ` drivers/block/ub.c Pete Zaitcev
2004-06-27 21:03         ` drivers/block/ub.c Matthew Dharm
2004-06-28 15:40         ` drivers/block/ub.c Alan Stern
2004-06-28 16:42           ` drivers/block/ub.c Oliver Neukum
2004-06-28 19:50             ` drivers/block/ub.c Alan Stern
2004-06-27  5:35   ` drivers/block/ub.c Matthew Dharm
2004-06-27 15:28     ` drivers/block/ub.c Alan Stern
2004-06-27 22:56 ` drivers/block/ub.c David Brownell
2004-06-27 23:43   ` drivers/block/ub.c Pete Zaitcev
2004-06-28 15:05     ` drivers/block/ub.c David Brownell
2004-06-28 15:56     ` drivers/block/ub.c Alan Stern
2004-06-28 16:23       ` drivers/block/ub.c David Brownell
2004-06-28 16:46         ` drivers/block/ub.c Oliver Neukum
2004-06-28 17:13           ` drivers/block/ub.c David Brownell
     [not found] ` <mailman.1088290201.14081.linux-kernel2news@redhat.com>
2004-06-27 23:57   ` drivers/block/ub.c Pete Zaitcev
2004-06-29 11:05 ` drivers/block/ub.c Jeff Garzik

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=20040629183153.GA11558@hexapodia.org \
    --to=adi@hexapodia.org \
    --cc=arjanv@redhat.com \
    --cc=davem@redhat.com \
    --cc=david-b@pacbell.net \
    --cc=greg@kroah.com \
    --cc=jgarzik@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mdharm-usb@one-eyed-alien.net \
    --cc=oliver@neukum.org \
    --cc=rwhite@casabyte.com \
    --cc=scott@timesys.com \
    --cc=stern@rowland.harvard.edu \
    --cc=tburke@redhat.com \
    --cc=zaitcev@redhat.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.