linux-toolchains.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Florian Weimer' <fweimer@redhat.com>,
	"Cufi, Carles" <Carles.Cufi@nordicsemi.no>
Cc: "netdev@vger.kernel.org" <netdev@vger.kernel.org>,
	"jukka.rissanen@linux.intel.com" <jukka.rissanen@linux.intel.com>,
	"johan.hedberg@intel.com" <johan.hedberg@intel.com>,
	"Lubos, Robert" <Robert.Lubos@nordicsemi.no>,
	"Bursztyka, Tomasz" <tomasz.bursztyka@intel.com>,
	"linux-toolchains@vger.kernel.org"
	<linux-toolchains@vger.kernel.org>
Subject: RE: Non-packed structures in IP headers
Date: Sat, 2 Oct 2021 15:54:54 +0000	[thread overview]
Message-ID: <a8082bcaeb534ee5b24ea6dae4428547@AcuMS.aculab.com> (raw)
In-Reply-To: <87bl48v74v.fsf@oldenburg.str.redhat.com>

From: Florian Weimer
> Sent: 01 October 2021 21:10
> 
> * Carles Cufi:
> 
> > I was looking through the structures for IPv{4,6} packet headers and
> > noticed that several of those that seem to be used to parse a packet
> > directly from the wire are not declared as packed. This surprised me
> > because, although I did find that provisions are made so that the
> > alignment of the structure, it is still technically possible for the
> > compiler to inject padding bytes inside those structures, since AFAIK
> > the C standard makes no guarantees about padding unless it's
> > instructed to pack the structure.
> 
> The C standards do not make such guarantees, but the platform ABI
> standards describe struct layout and ensure that there is no padding.
> Linux relies on that not just for networking, but also for the userspace
> ABI, support for separately compiled kernel modules, and in other
> places.

In particular structures are used to map hardware register blocks.

> Sometimes there are alignment concerns in the way these structs are
> used, but I believe the kernel generally controls placement of the data
> that is being worked on, so that does not matter, either.
> 
> Therefore, I do not believe this is an actual problem.

And adding __packed forces the compiler to do byte accesses
(with shifts) on cpu that don't support misaligned memory accesses.

So it really is wrong to specify __packed unless the structure
can be unaligned in memory, or has a 'broken' definition
that has fields that aren't 'naturally aligned'.
In the latter case it is enough to mark the field that requires
the padding before it removed as (IIRC) __aligned(1).
The compiler will then remove the padding but still assume the
field is partially aligned - so my do two 32bit access instead
of 8 8bit ones).

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


  reply	other threads:[~2021-10-02 15:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <AS8PR05MB78952FE7E8D82245D309DEBCE7AA9@AS8PR05MB7895.eurprd05.prod.outlook.com>
2021-10-01 20:10 ` Non-packed structures in IP headers Florian Weimer
2021-10-02 15:54   ` David Laight [this message]
2021-10-04 10:41     ` Cufi, Carles
2021-10-04 12:18       ` David Laight
2021-10-04 10:30   ` Cufi, Carles
2021-10-09  6:56     ` Florian Weimer

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=a8082bcaeb534ee5b24ea6dae4428547@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=Carles.Cufi@nordicsemi.no \
    --cc=Robert.Lubos@nordicsemi.no \
    --cc=fweimer@redhat.com \
    --cc=johan.hedberg@intel.com \
    --cc=jukka.rissanen@linux.intel.com \
    --cc=linux-toolchains@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=tomasz.bursztyka@intel.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 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).