From: "Paul E. McKenney" <paulmck@kernel.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: linux-toolchains@vger.kernel.org, peterz@infradead.org,
hpa@zytor.com, rostedt@goodmis.org, gregkh@linuxfoundation.org,
keescook@chromium.org
Subject: Re: A few proposals, this time from the C++ standards committee
Date: Sun, 17 Mar 2024 15:02:17 -0700 [thread overview]
Message-ID: <25e38fde-ad0c-451e-8e42-6c328bea5a73@paulmck-laptop> (raw)
In-Reply-To: <CAHk-=wi60pRZyt3oAPS84unsWMcbGsFt9R7n=DhP-CQ=JFL9Ow@mail.gmail.com>
On Sun, Mar 17, 2024 at 02:44:09PM -0700, Linus Torvalds wrote:
> On Sun, 17 Mar 2024 at 13:50, Linus Torvalds
> <torvalds@linux-foundation.org> wrote:
> >
> > Looking closer at this one, it seems to be purely a compiler bug.
>
> Side note: it may be that all that protects us in the kernel from this
> compiler bug is the fact that we do not let the compiler know that
> "kmalloc()" returns some private memory. So for that particular
> pattern, the compiler doesn't actuially know that 'p' is some private
> pointer and not visible to anybody else.
Sadly, we really do let the compiler know:
static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t flags)
#define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc
#define __malloc __attribute__((__malloc__))
Maybe we should stop doing so:
------------------------------------------------------------------------
diff --git a/include/linux/compiler_attributes.h b/include/linux/compiler_attributes.h
index 28566624f008f..7b4db0cd093a2 100644
--- a/include/linux/compiler_attributes.h
+++ b/include/linux/compiler_attributes.h
@@ -181,7 +181,7 @@
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute
* clang: https://clang.llvm.org/docs/AttributeReference.html#malloc
*/
-#define __malloc __attribute__((__malloc__))
+#define __malloc
/*
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-mode-type-attribute
------------------------------------------------------------------------
> In C++, particularly with 'new', the compiler might be much more aware
> of the fact that nobody can possibly see 'p' outside of that function
> until after the return.
>
> So a scarier example without that kind of issue might be something like this:
>
> extern void unlock(void);
> extern void lock(void);
> extern void wait_for_x(int *);
>
> void buggy(void)
> {
> int p = 5;
> unlock();
> wait_for_x(&p);
> lock();
> }
>
> where the basic theory of operation is that we're calling that
> function with a lock held, and then that "wait_for_x()" thing does
> something that exposes the value and waits for it to be changed.
>
> And at the time of the "unlock()", a buggy compiler *might* think that
> the value of "p" is entirely private to that function, so the compiler
> might decide to compile this as
>
> - call "unlock()"
>
> - *then* set 'p' to 5, and pass off the address to the wait function
>
> and that is very buggy on weakly ordered machines for the very same
> reasons that that github issue was raised - it is re-ordering the
> store wrt the store-release inherent in the 'unlock()'.
>
> Now, in my quick tests, that doesn't actually happen. I sincerely hope
> it is because the compiler sees "Oh, somebody is taking the address of
> 'p'" and just the act of that address-of will make the compiler know
> that it has to serialize stores to 'p' with any function calls - even
> function calls that happen before the address is taken.
>
> But that
>
> https://github.com/llvm/llvm-project/issues/64188
>
> that you linked to certainly seems to imply that some versions of
> clang have made the equivalent of that mistake, and could possibly
> hoist the assignment to 'p' to after the 'unlock()' call.
Yes, it really does happen in some cases.
And I agree that there are likely a great many failure cases.
The initial examples were user error where the pointer was handed off to
some other thread without synchronizing the lifetime of the pointed-to
object. I chastised them for this, and they eventually came up with
the external function hiding the atomic_thread_fence() from the compiler.
Thanx, Paul
next prev parent reply other threads:[~2024-03-17 22:02 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-17 9:14 A few proposals, this time from the C++ standards committee Paul E. McKenney
2024-03-17 18:50 ` Linus Torvalds
2024-03-17 20:56 ` Paul E. McKenney
2024-03-17 20:50 ` Linus Torvalds
2024-03-17 21:04 ` Paul E. McKenney
2024-03-17 21:44 ` Linus Torvalds
2024-03-17 22:02 ` Paul E. McKenney [this message]
2024-03-17 22:34 ` Linus Torvalds
2024-03-17 23:46 ` Jonathan Martin
2024-03-18 0:42 ` Paul E. McKenney
2024-03-18 1:49 ` Linus Torvalds
2024-03-18 2:44 ` Paul E. McKenney
2024-03-18 2:57 ` Randy Dunlap
2024-03-18 4:42 ` Paul E. McKenney
2024-03-18 4:45 ` Randy Dunlap
2024-03-18 16:32 ` Linus Torvalds
2024-03-18 16:48 ` H. Peter Anvin
2024-03-19 7:41 ` Marco Elver
2024-03-19 8:07 ` Jakub Jelinek
2024-06-05 13:52 ` Paul E. McKenney
2024-06-05 18:08 ` Linus Torvalds
2024-06-05 18:24 ` Linus Torvalds
2024-06-05 19:16 ` Paul E. McKenney
2024-06-05 19:12 ` Paul E. McKenney
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=25e38fde-ad0c-451e-8e42-6c328bea5a73@paulmck-laptop \
--to=paulmck@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=hpa@zytor.com \
--cc=keescook@chromium.org \
--cc=linux-toolchains@vger.kernel.org \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=torvalds@linux-foundation.org \
/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).