All of lore.kernel.org
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Segher Boessenkool <segher@kernel.crashing.org>
Cc: Sudip Mukherjee <sudipm.mukherjee@gmail.com>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Benjamin Herrenschmidt <benh@kernel.crashing.org>,
	Paul Mackerras <paulus@samba.org>,
	Kees Cook <keescook@chromium.org>,
	linuxppc-dev <linuxppc-dev@lists.ozlabs.org>,
	linux-kernel <linux-kernel@vger.kernel.org>,
	linux-hardening@vger.kernel.org
Subject: Re: mainline build failure of powerpc allmodconfig for prom_init_check
Date: Sun, 17 Jul 2022 18:38:56 -0700	[thread overview]
Message-ID: <CAHk-=wg1LeRWPmA-2pit+aH0LKZoBbhnGzmwWrPz6bbchMkCzw@mail.gmail.com> (raw)
In-Reply-To: <20220717214508.GD25951@gate.crashing.org>

On Sun, Jul 17, 2022 at 2:49 PM Segher Boessenkool
<segher@kernel.crashing.org> wrote:
>
> > I can *kind of* see the logic that when you do a whole struct
> > assignment, it turns into a "memcpy" without regard for volatile
> > members. You're not actually accessing the volatile members in some
> > particular order, so the struct assignment arguably does not really
> > have an access ordering that needs to be preserved.
>
> The order is not defined, correct.  But a "volatile int" can only be
> accessed as an int, and an external memcpy will typically use different
> size accesses, and can even access some fields more than once (or
> partially); all not okay for a volatile object.

That is not actually a valid or realistic argument in the general case.

The thing is, an operation on an aggregate type in C is fundamentally
different from the "do the same operation on the individual parts of
the struct".

Just to make a very concrete example of that, it's not at all
unreasonable to have a struct like this:

    struct io_accessor {
        union {
            volatile unsigned char byte[8];
            volatile unsigned short word[4];
        ...

and while that wasn't the example here, it's not completely insane as
a concept to use as a helper type so that you can do volatile accesses
of different sizes.

And while you'd be right to say that "assigning that kind of struct is
probably insane", and I wouldn't argue against it, I also think that
basically *any* union member is basically an argument that a structure
assignment is *NOT* about "assign all the individual members", and
never really can be.

In the above union, make one union member be a non-volatile type, and
suddenly it actually *can* be ok to copy the struct. Even though it
also has volatile bytes.

So once you start doing structure assignments but argue about
individual fields being volatile, I think you're on very shaky ground.

And I think "memcpy" is a reasonable way to say "we don't care - and
in the general case we CANNOT know - what the individual members are,
so we'll just copy it as one thing".

So the compiler emitting a "memcpy()" to assign a structure sounds
fine. Even in theory. Because the "but individual fields.." argument
just cannot work in general.

In contrast, when you access the members individually (like the kernel
does in this powerpc case), there is no such ambiguity.

There is no way in hell that it is ever ok to do a "memcpy()" when the
user has done the assignments one volatile member at a time.

So that's why I don't think your test-case with the struct assignment
is very good. I think it's very reasonable for a compiler person to
say "you assigned the whole struct, you get what you asked for, you
get a memcpy".

But when you do a loop that assigns individual volatile fields? No
such problem. Completely unambiguous that you need to do them one at a
time as individual accesses.

                Linus

WARNING: multiple messages have this Message-ID (diff)
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Segher Boessenkool <segher@kernel.crashing.org>
Cc: Kees Cook <keescook@chromium.org>,
	linux-kernel <linux-kernel@vger.kernel.org>,
	Paul Mackerras <paulus@samba.org>,
	linux-hardening@vger.kernel.org,
	linuxppc-dev <linuxppc-dev@lists.ozlabs.org>,
	Sudip Mukherjee <sudipm.mukherjee@gmail.com>
Subject: Re: mainline build failure of powerpc allmodconfig for prom_init_check
Date: Sun, 17 Jul 2022 18:38:56 -0700	[thread overview]
Message-ID: <CAHk-=wg1LeRWPmA-2pit+aH0LKZoBbhnGzmwWrPz6bbchMkCzw@mail.gmail.com> (raw)
In-Reply-To: <20220717214508.GD25951@gate.crashing.org>

On Sun, Jul 17, 2022 at 2:49 PM Segher Boessenkool
<segher@kernel.crashing.org> wrote:
>
> > I can *kind of* see the logic that when you do a whole struct
> > assignment, it turns into a "memcpy" without regard for volatile
> > members. You're not actually accessing the volatile members in some
> > particular order, so the struct assignment arguably does not really
> > have an access ordering that needs to be preserved.
>
> The order is not defined, correct.  But a "volatile int" can only be
> accessed as an int, and an external memcpy will typically use different
> size accesses, and can even access some fields more than once (or
> partially); all not okay for a volatile object.

That is not actually a valid or realistic argument in the general case.

The thing is, an operation on an aggregate type in C is fundamentally
different from the "do the same operation on the individual parts of
the struct".

Just to make a very concrete example of that, it's not at all
unreasonable to have a struct like this:

    struct io_accessor {
        union {
            volatile unsigned char byte[8];
            volatile unsigned short word[4];
        ...

and while that wasn't the example here, it's not completely insane as
a concept to use as a helper type so that you can do volatile accesses
of different sizes.

And while you'd be right to say that "assigning that kind of struct is
probably insane", and I wouldn't argue against it, I also think that
basically *any* union member is basically an argument that a structure
assignment is *NOT* about "assign all the individual members", and
never really can be.

In the above union, make one union member be a non-volatile type, and
suddenly it actually *can* be ok to copy the struct. Even though it
also has volatile bytes.

So once you start doing structure assignments but argue about
individual fields being volatile, I think you're on very shaky ground.

And I think "memcpy" is a reasonable way to say "we don't care - and
in the general case we CANNOT know - what the individual members are,
so we'll just copy it as one thing".

So the compiler emitting a "memcpy()" to assign a structure sounds
fine. Even in theory. Because the "but individual fields.." argument
just cannot work in general.

In contrast, when you access the members individually (like the kernel
does in this powerpc case), there is no such ambiguity.

There is no way in hell that it is ever ok to do a "memcpy()" when the
user has done the assignments one volatile member at a time.

So that's why I don't think your test-case with the struct assignment
is very good. I think it's very reasonable for a compiler person to
say "you assigned the whole struct, you get what you asked for, you
get a memcpy".

But when you do a loop that assigns individual volatile fields? No
such problem. Completely unambiguous that you need to do them one at a
time as individual accesses.

                Linus

  reply	other threads:[~2022-07-18  1:39 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-14  8:55 mainline build failure of powerpc allmodconfig for prom_init_check Sudip Mukherjee (Codethink)
2022-07-14  8:55 ` Sudip Mukherjee (Codethink)
2022-07-17  9:12 ` Sudip Mukherjee
2022-07-17  9:12   ` Sudip Mukherjee
2022-07-17 14:44   ` Linus Torvalds
2022-07-17 14:44     ` Linus Torvalds
2022-07-17 19:54     ` Segher Boessenkool
2022-07-17 19:54       ` Segher Boessenkool
2022-07-18  3:52       ` Michael Ellerman
2022-07-18  3:52         ` Michael Ellerman
2022-07-18 14:56         ` Segher Boessenkool
2022-07-18 14:56           ` Segher Boessenkool
2022-07-17 20:25     ` Sudip Mukherjee
2022-07-17 20:25       ` Sudip Mukherjee
2022-07-17 20:29       ` Linus Torvalds
2022-07-17 20:29         ` Linus Torvalds
2022-07-17 20:38         ` Sudip Mukherjee
2022-07-17 20:38           ` Sudip Mukherjee
2022-07-17 20:56           ` Linus Torvalds
2022-07-17 20:56             ` Linus Torvalds
2022-07-17 20:56         ` Segher Boessenkool
2022-07-17 20:56           ` Segher Boessenkool
2022-07-17 21:11           ` Linus Torvalds
2022-07-17 21:11             ` Linus Torvalds
2022-07-17 21:45             ` Segher Boessenkool
2022-07-17 21:45               ` Segher Boessenkool
2022-07-18  1:38               ` Linus Torvalds [this message]
2022-07-18  1:38                 ` Linus Torvalds
2022-07-18  4:41   ` Michael Ellerman
2022-07-18  4:41     ` Michael Ellerman
2022-07-18  7:51     ` David Laight
2022-07-18  7:51       ` David Laight
2022-07-18 13:44     ` [PATCH] powerpc/64s: Disable stack variable initialisation for prom_init Michael Ellerman
2022-07-18 13:44       ` Michael Ellerman
2022-07-18 15:03       ` Sudip Mukherjee
2022-07-18 15:03         ` Sudip Mukherjee
2022-07-18 18:34       ` Linus Torvalds
2022-07-18 18:34         ` Linus Torvalds
2022-07-27 12:02       ` Michael Ellerman
2022-07-18 19:06     ` mainline build failure of powerpc allmodconfig for prom_init_check Linus Torvalds
2022-07-18 19:06       ` Linus Torvalds
2022-07-18 22:08       ` Segher Boessenkool
2022-07-18 22:08         ` Segher Boessenkool
2022-07-18 22:55         ` Linus Torvalds
2022-07-18 22:55           ` Linus Torvalds
2022-07-19 13:35       ` Michael Ellerman
2022-07-19 13:35         ` Michael Ellerman

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='CAHk-=wg1LeRWPmA-2pit+aH0LKZoBbhnGzmwWrPz6bbchMkCzw@mail.gmail.com' \
    --to=torvalds@linux-foundation.org \
    --cc=benh@kernel.crashing.org \
    --cc=keescook@chromium.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=mpe@ellerman.id.au \
    --cc=paulus@samba.org \
    --cc=segher@kernel.crashing.org \
    --cc=sudipm.mukherjee@gmail.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.