Received: by 2002:ac0:a591:0:0:0:0:0 with SMTP id m17-v6csp1644037imm; Fri, 6 Jul 2018 04:02:42 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdIl8jiU5y0S/eMGSxRjJ/FAhE79U46E6qUkhfcMwy0vmP7m4aVgm+MQ9cajZap+mm+hN0x X-Received: by 2002:a62:8d84:: with SMTP id p4-v6mr10326623pfk.251.1530874962060; Fri, 06 Jul 2018 04:02:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530874962; cv=none; d=google.com; s=arc-20160816; b=AEldLet1TkcaLHL7UfVvFPVNR1z4kMytsIl7A0a0Jd45bYzkwkqvHPJWgklYzSLaqq k8e2ZjBLSjVdUKSgWBzk2PMnp3Et/5NfaTN+yyp6ivI8EMhIJk9zJ8YhHhcL/VcoBYTp eUqpXz15/t1bpoqNUtnp/EAoEgMa2J1o0Pie8+aHYwnJZBC+9fslszyw3AHwerMoIp2W gkl9YoMyuw2juB5ZSafWY7m0+1rzsk4ix0OMJBZzfayqF7JhAhazPG2WmkQii6CU7hZD kd9xhgeUDODlFSLc7w0DwqW8P92E6pUDvsYJ1+N9/pxaTnaTCQcvTZrIc+3b9fc03UlC ZNHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=ykCFeNM8obMwLFhhOd6hHzpS+2FAqro+91OZg4aW6hk=; b=fHxYHoPEv4B9pIYU0uByhFiac8g5zt41UDirLKgkQPDx74je0OeD5qe/B6p3r1766P jps/t73Pa9vWaNL+PtV2s8T8pw/Jp+tT905WOpKpcHwV09RPM2XV2Fd5Xa8oz//J4+9E JKOaxeSu8h2SfCKX+tpcXDC7P67dCI67iSfiCnbtXxIDZ+IDxKwTfHBYE0WBPdzYbwJw WbMlpxB/iclFoVYEsi43jIj9ezpTKb0zU6/+c8f1bDdpW/pgaTPuJhAPN9iUXcpI0NHR 6WhmiogoDOD5bGn8NPYHbWyyQOG8yCGVLn0nQMNghQsJOG2ikQ7BK3ZzURAVuaQMi2ly SZiQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 67-v6si7533682pgb.107.2018.07.06.04.02.21; Fri, 06 Jul 2018 04:02:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754151AbeGFLBq (ORCPT + 99 others); Fri, 6 Jul 2018 07:01:46 -0400 Received: from smtp2200-217.mail.aliyun.com ([121.197.200.217]:45108 "EHLO smtp2200-217.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753946AbeGFLBo (ORCPT ); Fri, 6 Jul 2018 07:01:44 -0400 X-Alimail-AntiSpam: AC=CONTINUE;BC=0.07493031|-1;CH=green;FP=0|0|0|0|0|-1|-1|-1;HT=e01l07447;MF=ren_guo@c-sky.com;NM=1;PH=DS;RN=13;RT=13;SR=0;TI=SMTPD_---.CN3WA2e_1530874891; Received: from localhost(mailfrom:ren_guo@c-sky.com fp:SMTPD_---.CN3WA2e_1530874891) by smtp.aliyun-inc.com(10.147.40.44); Fri, 06 Jul 2018 19:01:31 +0800 Date: Fri, 6 Jul 2018 19:01:31 +0800 From: Guo Ren To: Peter Zijlstra Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, tglx@linutronix.de, daniel.lezcano@linaro.org, jason@lakedaemon.net, arnd@arndb.de, c-sky_gcc_upstream@c-sky.com, gnu-csky@mentor.com, thomas.petazzoni@bootlin.com, wbx@uclibc-ng.org, green.hu@gmail.com, Will Deacon Subject: Re: [PATCH V2 11/19] csky: Atomic operations Message-ID: <20180706110129.GC8707@guoren> References: <860b8db036b33d7b3648cb1f4ec827a53dc1a01b.1530465326.git.ren_guo@c-sky.com> <20180705175059.GE2530@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180705175059.GE2530@hirez.programming.kicks-ass.net> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jul 05, 2018 at 07:50:59PM +0200, Peter Zijlstra wrote: > On Mon, Jul 02, 2018 at 01:30:14AM +0800, Guo Ren wrote: > > > +#include > > + > > +#define __xchg(new, ptr, size) \ > > +({ \ > > + __typeof__(ptr) __ptr = (ptr); \ > > + __typeof__(new) __new = (new); \ > > + __typeof__(*(ptr)) __ret; \ > > + unsigned long tmp; \ > > + switch (size) { \ > > + case 4: \ > > + asm volatile ( \ > > + "1: ldex.w %0, (%3) \n" \ > > + " mov %1, %2 \n" \ > > + " stex.w %1, (%3) \n" \ > > + " bez %1, 1b \n" \ > > + : "=&r" (__ret), "=&r" (tmp) \ > > + : "r" (__new), "r"(__ptr) \ > > + : "memory"); \ > > + smp_mb(); \ > > + break; \ > > + default: \ > > + BUILD_BUG(); \ > > + } \ > > + __ret; \ > > +}) > > + > > +#define xchg(ptr, x) (__xchg((x), (ptr), sizeof(*(ptr)))) > > + > > +#define __cmpxchg(ptr, old, new, size) \ > > +({ \ > > + __typeof__(ptr) __ptr = (ptr); \ > > + __typeof__(new) __new = (new); \ > > + __typeof__(new) __tmp; \ > > + __typeof__(old) __old = (old); \ > > + __typeof__(*(ptr)) __ret; \ > > + switch (size) { \ > > + case 4: \ > > + asm volatile ( \ > > + "1: ldex.w %0, (%3) \n" \ > > + " cmpne %0, %4 \n" \ > > + " bt 2f \n" \ > > + " mov %1, %2 \n" \ > > + " stex.w %1, (%3) \n" \ > > + " bez %1, 1b \n" \ > > + "2: \n" \ > > + : "=&r" (__ret), "=&r" (__tmp) \ > > + : "r" (__new), "r"(__ptr), "r"(__old) \ > > + : "memory"); \ > > + smp_mb(); \ > > + break; \ > > + default: \ > > + BUILD_BUG(); \ > > + } \ > > + __ret; \ > > +}) > > + > > +#define cmpxchg(ptr, o, n) \ > > + (__cmpxchg((ptr), (o), (n), sizeof(*(ptr)))) > > What's the memory ordering rules for your LDEX/STEX ? Every CPU has a local exclusive monitor. "Ldex rz, (rx, #off)" will add an entry into the local monitor, and the entry is composed of a address tag and a exclusive flag (inited with 1). Any stores (include other cores') will break the exclusive flag to 0 in the entry which could be indexed by the address tag. "Stex rz, (rx, #off)" has two condition: 1. Store Success: When the entry's exclusive flag is 1, it will store rz to the [rx + off] address and the rz will be set to 1. 2. Store Failure: When the entry's exclusive flag is 0, just rz will be set to 0. > The mandated semantics for xchg() / cmpxchg() is an effective smp_mb() > before _and_ after. switch (size) { \ case 4: \ smp_mb(); \ asm volatile ( \ "1: ldex.w %0, (%3) \n" \ " mov %1, %2 \n" \ " stex.w %1, (%3) \n" \ " bez %1, 1b \n" \ : "=&r" (__ret), "=&r" (tmp) \ : "r" (__new), "r"(__ptr) \ : "memory"); \ smp_mb(); \ break; \ Hmm? But I couldn't undertand what's wrong without the 1th smp_mb()? 1th smp_mb will make all ld/st finish before ldex.w. Is it necessary? > The above implementation suggests LDEX implies a SYNC.IS, is this > correct? No, ldex doesn't imply a sync.is. Guo Ren