All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: Theodore Ts'o <tytso@mit.edu>
Cc: harshad shirwadkar <harshadshirwadkar@gmail.com>,
	Andreas Dilger <adilger@dilger.ca>,
	Ext4 Developers List <linux-ext4@vger.kernel.org>,
	Harshad Shirwadkar <harshads@google.com>
Subject: Re: [PATCH] e2fsck: fix portability problems caused by unaligned accesses
Date: Tue, 4 May 2021 14:30:50 -0700	[thread overview]
Message-ID: <YJG9CjVXKkha57RU@gmail.com> (raw)
In-Reply-To: <YJG4SrJ/ZEjv3Ha0@mit.edu>

On Tue, May 04, 2021 at 05:10:34PM -0400, Theodore Ts'o wrote:
> 
> Basically, what gcc (and presumably clang) is doing is it is special
> casing packed_ptr->field so that the compiled code will work
> regardless of the alignment of packed_ptr.  This isn't documented
> anywhere, but it apparently is the case.  (I had assumed that it would
> only generate unaligned access for those fields that are not aligned
> if the structure started on an aligned boundary.)

I don't think it's related to the pointer dereference per se, but rather the
compiler assigns an alignment of 1 to all fields in a packed struct (even the
field at the beginning of the struct).  If you had a packed struct as a global
variable and did 'packed_struct.field', the behavior would be the same.

> > If we really don't want to use __attribute__((packed)) that is fine, but then
> > we'll need to remember to use an unaligned accessor *every* field access (except
> > for bytes), which seems harder to me -- and the compiler won't warn when one of
> > these is missing.  (They can only be detected at runtime using UBSAN.)
> 
> One reason not to use the __packed__ attribute is that there are cases
> where people attempt to build e2fsprogs on non-gcc/non-clang binaries.
> At one point FreeBSD was trying to use pcc to build e2fsprogs IIRC.
> And certainly there are people who try to build e2fsprogs on MSVC on
> Windows.

Is that really true, given that e2fsprogs already uses a lot of gcc/clang
extensions, including __attribute((packed))__ already?

> So maybe the memcpy to a local copy is the better way to go, and
> hopefully the C compiler will optimize away the local copy on
> architectures where it is safe to do so.  And in the unlikely case
> that it is a performance bottleneck, we could add a -DUBSAN when
> configure --enable-ubsan is in force, which switches in the memcpy
> when only when ubsan is enabled.

These days the memcpy() approach does get optimized properly.  armv6 and armv7
with gcc used to be a notable exception, but it got fixed in gcc 6
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67366).

- Eric

  reply	other threads:[~2021-05-04 21:30 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-04  3:10 [PATCH] e2fsck: fix portability problems caused by unaligned accesses Theodore Ts'o
2021-05-04  6:29 ` Andreas Dilger
2021-05-04  9:40   ` harshad shirwadkar
2021-05-04 13:49     ` Theodore Ts'o
2021-05-04 16:46       ` Eric Biggers
2021-05-04 17:55         ` harshad shirwadkar
2021-05-04 19:14           ` Eric Biggers
2021-05-04 19:53             ` Theodore Ts'o
2021-05-04 20:14               ` harshad shirwadkar
2021-05-04 20:45                 ` Eric Biggers
2021-05-04 21:10                   ` Theodore Ts'o
2021-05-04 21:30                     ` Eric Biggers [this message]
2021-05-07  6:45                       ` Eric Biggers
2021-05-07 15:56                         ` Theodore Ts'o
2021-05-07 16:22                           ` harshad shirwadkar
2021-05-04 20:35               ` Eric Biggers
2021-05-04 15:18   ` Theodore Ts'o
2021-05-06 18:30 ` [PATCH -v2] " Theodore Ts'o
2021-05-06 23:30   ` harshad shirwadkar
2021-05-07  1:50     ` Theodore Ts'o

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=YJG9CjVXKkha57RU@gmail.com \
    --to=ebiggers@kernel.org \
    --cc=adilger@dilger.ca \
    --cc=harshads@google.com \
    --cc=harshadshirwadkar@gmail.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=tytso@mit.edu \
    /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.