Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934627AbdCXSCE (ORCPT ); Fri, 24 Mar 2017 14:02:04 -0400 Received: from merlin.infradead.org ([205.233.59.134]:60832 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754298AbdCXSBv (ORCPT ); Fri, 24 Mar 2017 14:01:51 -0400 Date: Fri, 24 Mar 2017 19:00:25 +0100 From: Peter Zijlstra To: Andy Lutomirski Cc: Dmitry Vyukov , Andrew Morton , Andy Lutomirski , Borislav Petkov , Brian Gerst , Denys Vlasenko , "H. Peter Anvin" , Josh Poimboeuf , Linus Torvalds , Paul McKenney , Thomas Gleixner , Ingo Molnar , LKML Subject: Re: locking/atomic: Introduce atomic_try_cmpxchg() Message-ID: <20170324180025.tumlczzf4ji3uzpo@hirez.programming.kicks-ass.net> References: <20170324142140.vpyzl755oj6rb5qv@hirez.programming.kicks-ass.net> <20170324164108.ibcxxqbhvx6ao54r@hirez.programming.kicks-ass.net> <20170324172342.radlrhk2z6mwmdgk@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20170324172342.radlrhk2z6mwmdgk@hirez.programming.kicks-ass.net> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1040 Lines: 36 On Fri, Mar 24, 2017 at 06:23:42PM +0100, Peter Zijlstra wrote: > I tried a few variants, but nothing really made it better. > > Find the tiny.c file below; I'm using: > > gcc (Debian 6.3.0-5) 6.3.0 20170124 > > it has both an inline and an stmt-expr try_cmpxchg variant to play with; > the 'expected' output is at the bottom (same as above left). > > Note that clang doesn't compile this stuff due to missing features. > static inline bool try_cmpxchg2(unsigned int *ptr, unsigned int *val, unsigned int new) { unsigned int old = *val; bool success; asm volatile goto("lock cmpxchgl %[new], %[ptr]; " "jnz %[label]" : /* no output */ : [ptr] "+m" (*ptr), [old] "+a" (old) [new] "r" (new) : "memory" : label); return 1; label: *val = old; return 0; } generates better code than the @cc output with extra if (!success), but not as good as the original.