Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932930AbcDTOYY (ORCPT ); Wed, 20 Apr 2016 10:24:24 -0400 Received: from merlin.infradead.org ([205.233.59.134]:38121 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932883AbcDTOYW (ORCPT ); Wed, 20 Apr 2016 10:24:22 -0400 Date: Wed, 20 Apr 2016 16:24:08 +0200 From: Peter Zijlstra To: Pan Xinhui Cc: linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, benh@kernel.crashing.org, paulus@samba.org, mpe@ellerman.id.au, boqun.feng@gmail.com, paulmck@linux.vnet.ibm.com, tglx@linutronix.de Subject: Re: [PATCH V3] powerpc: Implement {cmp}xchg for u8 and u16 Message-ID: <20160420142408.GF3430@twins.programming.kicks-ass.net> References: <5715D04E.9050009@linux.vnet.ibm.com> <571782F0.2020201@linux.vnet.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <571782F0.2020201@linux.vnet.ibm.com> User-Agent: Mutt/1.5.21 (2012-12-30) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1123 Lines: 31 On Wed, Apr 20, 2016 at 09:24:00PM +0800, Pan Xinhui wrote: > +#define __XCHG_GEN(cmp, type, sfx, skip, v) \ > +static __always_inline unsigned long \ > +__cmpxchg_u32##sfx(v unsigned int *p, unsigned long old, \ > + unsigned long new); \ > +static __always_inline u32 \ > +__##cmp##xchg_##type##sfx(v void *ptr, u32 old, u32 new) \ > +{ \ > + int size = sizeof (type); \ > + int off = (unsigned long)ptr % sizeof(u32); \ > + volatile u32 *p = ptr - off; \ > + int bitoff = BITOFF_CAL(size, off); \ > + u32 bitmask = ((0x1 << size * BITS_PER_BYTE) - 1) << bitoff; \ > + u32 oldv, newv, tmp; \ > + u32 ret; \ > + oldv = READ_ONCE(*p); \ > + do { \ > + ret = (oldv & bitmask) >> bitoff; \ > + if (skip && ret != old) \ > + break; \ > + newv = (oldv & ~bitmask) | (new << bitoff); \ > + tmp = oldv; \ > + oldv = __cmpxchg_u32##sfx((v u32*)p, oldv, newv); \ > + } while (tmp != oldv); \ > + return ret; \ > +} So for an LL/SC based arch using cmpxchg() like that is sub-optimal. Why did you choose to write it entirely in C?