mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: "Arnd Bergmann" <arnd@arndb.de>
Cc: "Ian Rogers" <irogers@google.com>,
	"Andy Lutomirski" <luto@kernel.org>,
	"Thomas Gleixner" <tglx@kernel.org>,
	"Vincenzo Frascino" <vincenzo.frascino@arm.com>,
	"Arnaldo Carvalho de Melo" <acme@redhat.com>,
	"Stefan Kerkmann" <s.kerkmann@pengutronix.de>,
	"Marc Kleine-Budde" <mkl@pengutronix.de>,
	"Christophe Leroy" <chleroy@kernel.org>,
	"Helge Deller" <deller@gmx.de>,
	"James E . J . Bottomley" <James.Bottomley@hansenpartnership.com>,
	"Jason A . Donenfeld" <Jason@zx2c4.com>,
	"Alexander Viro" <viro@zeniv.linux.org.uk>,
	"Eric Biggers" <ebiggers@google.com>,
	linux-kernel@vger.kernel.org, linux-parisc@vger.kernel.org
Subject: Re: [PATCH v2 1/2] vdso: Add VDSO_UNALIGNED_PACKED config option
Date: Sat, 24 Oct 2026 09:32:09 +0100	[thread overview]
Message-ID: <20261024093209.09367c73@pumpkin> (raw)
In-Reply-To: <9ad54286-4337-4022-8657-65e1ed86b6df@app.fastmail.com>

On Fri, 23 Oct 2026 23:49:50 +0200
"Arnd Bergmann" <arnd@arndb.de> wrote:

> On Tue, Oct 20, 2026, at 20:07, David Laight wrote:
> > On Tue, 20 Oct 2026 09:32:09 -0700 Ian Rogers <irogers@google.com> wrote:  
> 
> >>  
> >>  #include <linux/compiler_types.h>
> >>  
> >> +#ifdef CONFIG_VDSO_UNALIGNED_PACKED
> >> +
> >> +#define __get_unaligned_t(type, ptr) ({							\
> >> +	const struct { type x; } __packed * __get_pptr = (typeof(__get_pptr))(ptr);	\
> >> +	__get_pptr->x;									\
> >> +})
> >> +
> >> +#define __put_unaligned_t(type, val, ptr) do {						\
> >> +	struct { type x; } __packed * __put_pptr = (typeof(__put_pptr))(ptr);		\
> >> +	__put_pptr->x = (val);								\
> >> +} while (0)  
> >
> > I'm confused (easily done).
> > That code relies on strict alignment being disabled regardless of whether
> > misaligned accesses fault or not.
> >
> > So it is either always valid or always wrong.
> >
> > Additionally I think the compiler can sometimes track the alignment through
> > casts (although it didn't for a quick check I did earlier).  
> 
> This is the traditional way we have been doing the unaligned access
> in the kernel before the change. By annotating the structure as
> __packed, we tell the compiler that it cannot rely on alignment here,
> so it uses whatever method it knows to access unaligned data depending
> on the architecture and flags.

That relies on the compiler not tracking the alignment through the casts.
The only way the C language lets you get a 'foo *' pointer is taking the
address of a 'foo' data item so it must be aligned.
So even if you cast it to another type that alignment can be assumed.
I've seen crashes from memcpy() using word accesses when trying to
copy from a misaligned pointer into an aligned copy of the structure.
Now the current code must be avoiding that - but I think the compiler
can do it.

> The reason we need the struct here is that you cannot use __packed
> as a qualifier on a variable, only on a type or struct member.

I know all about that.
Somewhere it also says that you can only increase the alignment (I think
of a type), but you can definitely have a type for 'u64 with 32bit
alignment' and use it in a structure.

> 
> > Since having misaligned non-packed structures really doesn't work, the only
> > places these functions should be used is on u8[] buffers.
> > Pointers into those can (probably) be cast to packed structure types
> > without causing issues if strict aliasing is enabled.  
> 
> strict aliasing is thankfully never enabled in the kernel.
> 
> The cast is from a higher alignment to byte alignment, and that
> is always allowed unlike the reverse. Technically the only
> reason we ever need the get_unaligned() helpers is to work
> around pointers to objects that should exist because the
> ABI requires them to be aligned but the pointer instead
> came out of some byte stream or from taking a (non-__packed)
> pointer of a __packed struct member.

I think those ought to be found and marked packed.
I doubt any of them get used other than part of the byte stream.
That leaves the explicit get_unaligned_le32() used to pull 32bit
values from a byte stream (that isn't defined by a structure).

But we're probably arguing in basic agreement.  

> 
>      Arnd


  reply	other threads:[~2026-10-24  8:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-20 16:32 Ian Rogers
2026-10-20 16:32 ` [PATCH v2 2/2] tools headers: Update the vdso/unaligned.h copy with the kernel sources Ian Rogers
2026-10-20 18:07 ` [PATCH v2 1/2] vdso: Add VDSO_UNALIGNED_PACKED config option David Laight
2026-10-23 21:49   ` Arnd Bergmann
2026-10-24  8:32     ` David Laight [this message]
2026-10-24 11:05       ` Arnd Bergmann

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=20261024093209.09367c73@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=James.Bottomley@hansenpartnership.com \
    --cc=Jason@zx2c4.com \
    --cc=acme@redhat.com \
    --cc=arnd@arndb.de \
    --cc=chleroy@kernel.org \
    --cc=deller@gmx.de \
    --cc=ebiggers@google.com \
    --cc=irogers@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-parisc@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=s.kerkmann@pengutronix.de \
    --cc=tglx@kernel.org \
    --cc=vincenzo.frascino@arm.com \
    --cc=viro@zeniv.linux.org.uk \
    /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

all inboxes | Powered by JetHome®