All of lore.kernel.org
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Andy Lutomirski <luto@amacapital.net>,
	Dmitry Vyukov <dvyukov@google.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Andy Lutomirski <luto@kernel.org>, Borislav Petkov <bp@alien8.de>,
	Brian Gerst <brgerst@gmail.com>,
	Denys Vlasenko <dvlasenk@redhat.com>,
	"H. Peter Anvin" <hpa@zytor.com>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	Paul McKenney <paulmck@linux.vnet.ibm.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@kernel.org>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: locking/atomic: Introduce atomic_try_cmpxchg()
Date: Fri, 24 Mar 2017 12:08:32 -0700	[thread overview]
Message-ID: <CA+55aFzxsGUesnq7mqyas=8ZK5fvPc0Zs_=OO3ttZ6Cb7Fbz-A@mail.gmail.com> (raw)
In-Reply-To: <20170324172342.radlrhk2z6mwmdgk@hirez.programming.kicks-ass.net>

On Fri, Mar 24, 2017 at 10:23 AM, Peter Zijlstra <peterz@infradead.org> wrote:
>
> I tried a few variants, but nothing really made it better.

So I really hate how your thing has two return values, and fakes the
second one using the pointer value. I dislike it for two different
reasons:

 - it's bad for type checking: it makes the "real" pointer value and
the "old value" pointer value both be pointers of the same type

 - I think it's part of the reason why gcc easily generates crap code.

And I think the problem is fundamental to your interface.

So how about we change the interface entirely, with the goal being
both type safety and good code generation?

In particular, let's make the interface something that might be
optimal given possible future gcc improvements, like the ability to
use "asm goto" with outputs. We can't do that today, but maybe some
day...

So I would suggest that the format of the "try_cmpxhg()" thing be
something like this:

#define try_cmpxchg(ptr, value, new, success_label) ({  \
        bool __txchg_success;                           \
        __typeof__(*(ptr)) __old;                       \
        asm volatile("lock cmpxchgl %3, %1"             \
                : "=@ccz" (__txchg_success),            \
                  "+m" (*ptr),                          \
                  "=a" (__old)                          \
                : "r" (new),                            \
                  "2" (value)                           \
                : "memory");                            \
        if (__txchg_success) goto success_label;        \
        __old; })

which seems to be fairly natural.

Then you do your refcount loop (or pretty much *any* cmpxchg loop) with

        for (;;) {
                if (unlikely(val == UINT_MAX))
                        goto saturated;

                if (unlikely(!val))
                        goto use_after_free;

                new = val + 1;
                val = try_cmpxchg(r, val, new, success);
        }

    success:
        return;

    saturated: use_after_free: .. whatever error handling ..

and I think gcc should generate reasonable code.

Hmm?

NOTE! I would suggest that this odd "macro with a success label" model
of try_cmpxchg() never be used for something that people are expected
to use directly. So I don't think "normal" code should use this label
form.

But it's useful as a helper function that is then used to implement
the "real" ABI, ie to implement that "refcount_inc()" and other things
like that..

                               Linus

  parent reply	other threads:[~2017-03-24 19:08 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-24 12:44 locking/atomic: Introduce atomic_try_cmpxchg() Dmitry Vyukov
2017-03-24 14:21 ` Peter Zijlstra
2017-03-24 14:23   ` Dmitry Vyukov
2017-03-24 16:41   ` Peter Zijlstra
2017-03-24 16:54     ` Andy Lutomirski
2017-03-24 17:23       ` Peter Zijlstra
2017-03-24 17:51         ` Dmitry Vyukov
2017-03-24 18:08           ` Peter Zijlstra
2017-03-24 18:13             ` Peter Zijlstra
2017-03-24 19:16               ` Andy Lutomirski
2017-03-24 19:20                 ` Linus Torvalds
2017-03-24 19:27                   ` Andy Lutomirski
2017-03-24 20:15                   ` Peter Zijlstra
2017-03-24 20:14                 ` Peter Zijlstra
2017-03-24 20:21                   ` Andy Lutomirski
2017-03-24 18:16             ` Dmitry Vyukov
2017-03-24 18:00         ` Peter Zijlstra
2017-03-24 18:04           ` Peter Zijlstra
2017-03-24 18:45         ` Andy Lutomirski
2017-03-24 19:17           ` Linus Torvalds
2017-03-24 21:23             ` Peter Zijlstra
2017-03-25  7:51               ` Peter Zijlstra
2017-03-25 18:00                 ` Linus Torvalds
2017-03-25 18:20                   ` Peter Zijlstra
2017-03-25 18:28                     ` Linus Torvalds
2017-03-25 18:34                       ` Linus Torvalds
2017-03-25 21:13                         ` Peter Zijlstra
2017-03-25 22:08                           ` Linus Torvalds
2017-03-27  9:48                             ` Peter Zijlstra
2017-03-24 20:22           ` Peter Zijlstra
2017-03-24 20:27             ` Andy Lutomirski
2017-03-24 21:07               ` Peter Zijlstra
2017-03-24 19:08         ` Linus Torvalds [this message]
2017-03-24 20:46           ` Peter Zijlstra
2017-03-24 20:58             ` Linus Torvalds
2017-03-27 12:16 ` Peter Zijlstra
2017-03-27 13:45   ` Dmitry Vyukov

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='CA+55aFzxsGUesnq7mqyas=8ZK5fvPc0Zs_=OO3ttZ6Cb7Fbz-A@mail.gmail.com' \
    --to=torvalds@linux-foundation.org \
    --cc=akpm@linux-foundation.org \
    --cc=bp@alien8.de \
    --cc=brgerst@gmail.com \
    --cc=dvlasenk@redhat.com \
    --cc=dvyukov@google.com \
    --cc=hpa@zytor.com \
    --cc=jpoimboe@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=luto@kernel.org \
    --cc=mingo@kernel.org \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    /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.