Received: by 2002:ac0:a591:0:0:0:0:0 with SMTP id m17-v6csp1681896imm; Fri, 6 Jul 2018 04:46:02 -0700 (PDT) X-Google-Smtp-Source: AAOMgpe/CUcpWqyxsnKyw1juY5VaWpHh4W4DRlsTO6lW73x0cpKNhdome4+IvuYmjn1+mwPnjft7 X-Received: by 2002:a63:1f4d:: with SMTP id q13-v6mr9194714pgm.241.1530877562743; Fri, 06 Jul 2018 04:46:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530877562; cv=none; d=google.com; s=arc-20160816; b=SXvlegW6CLAWiYeYfnJGdh9BXcCVX6w9jfDzE5/2TR5M7y7sEXHGPYP+Ef8+ylmWN+ eQI2sWnAilCpI6b7WSezJawnKgKGOakJ/DOkHitH8BmWzoJLo5TVsPHy3YLQlMnfeaph KdOrWLR0EZmDZbpCMOuk3m5CzDOYaqLjwVeAd5pJuaJaYQcXqMHTMCFilqzM1EDx2hhl /DqqVI4Ln/KnHbMXaqa7+/Z6oCXu6gdG9+XcdVc8DYbvuWWQ3IQ215M5Qs8Ypsb6U30g 5aBu20W4y0DEcBDLdYH7ZicId1xw6ShAHwBv2fMjGU7mvz15tyYT8tL6R7oCb7DjiNlg pGiQ== 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=Fo7gtdiFdtp/tF/A9oZJRRYkEYNsUsS+xBeLOrZaWn4=; b=XunBfQxLj6cHQpSc5l5Sa4gzMQ/WCgCIkkLC/BqwhhRfo+2g34Cy7pLKa7Di6MEDJX 6fwVhGfzhPMOva4/DUgsA5bCONE6eoDyM1Yfm0e72/xr3FoEbMVcJK5KtVCUCSuJH+0v /yf/XIcHifqQ2iUNCF4aIqUHLNm/7l5ysy5Gs5S+l8c6NIOa3BMOOHrOWayHowU4FLXh kzYftoPB5UEzoUolzCkHFCRcIsoaYetPVz5pQYO1aVBvXJUTN/BUCbcYY9c7YNfCbIbG 1iRFR1fuiNvKYtlTQNUFogjFhxXA+/Ckvx5t6tSLVNQiAm5r0B05XSMwDzpdH7kMa1wE I4IA== 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 k72-v6si8160904pgc.304.2018.07.06.04.45.47; Fri, 06 Jul 2018 04:46:02 -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 S932783AbeGFLoh (ORCPT + 99 others); Fri, 6 Jul 2018 07:44:37 -0400 Received: from smtp2200-217.mail.aliyun.com ([121.197.200.217]:56843 "EHLO smtp2200-217.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932348AbeGFLog (ORCPT ); Fri, 6 Jul 2018 07:44:36 -0400 X-Alimail-AntiSpam: AC=CONTINUE;BC=0.0747115|-1;CH=green;FP=0|0|0|0|0|-1|-1|-1;HT=e02c03295;MF=ren_guo@c-sky.com;NM=1;PH=DS;RN=12;RT=12;SR=0;TI=SMTPD_---.CN48llI_1530877443; Received: from localhost(mailfrom:ren_guo@c-sky.com fp:SMTPD_---.CN48llI_1530877443) by smtp.aliyun-inc.com(10.147.41.187); Fri, 06 Jul 2018 19:44:03 +0800 Date: Fri, 6 Jul 2018 19:44:03 +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 Subject: Re: [PATCH V2 11/19] csky: Atomic operations Message-ID: <20180706114402.GB27148@guoren> References: <860b8db036b33d7b3648cb1f4ec827a53dc1a01b.1530465326.git.ren_guo@c-sky.com> <20180705175902.GF2530@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180705175902.GF2530@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:59:02PM +0200, Peter Zijlstra wrote: > On Mon, Jul 02, 2018 at 01:30:14AM +0800, Guo Ren wrote: > > > +static inline void arch_spin_lock(arch_spinlock_t *lock) > > +{ > > + unsigned int *p = &lock->lock; > > + unsigned int tmp; > > + > > + asm volatile ( > > + "1: ldex.w %0, (%1) \n" > > + " bnez %0, 1b \n" > > + " movi %0, 1 \n" > > + " stex.w %0, (%1) \n" > > + " bez %0, 1b \n" > > + : "=&r" (tmp) > > + : "r"(p) > > + : "memory"); > > + smp_mb(); > > +} > > Test-and-set with MB acting as ACQUIRE, ok. Em ... Ok, I'll try to use test-and-set function instead of it. > > +static inline void arch_spin_unlock(arch_spinlock_t *lock) > > +{ > > + unsigned int *p = &lock->lock; > > + unsigned int tmp; > > + > > + smp_mb(); > > + asm volatile ( > > + "1: ldex.w %0, (%1) \n" > > + " movi %0, 0 \n" > > + " stex.w %0, (%1) \n" > > + " bez %0, 1b \n" > > + : "=&r" (tmp) > > + : "r"(p) > > + : "memory"); > > +} > > MB acting for RELEASE, but _why_ are you using a LDEX/STEX to clear the > lock word? Would not a normal store work? Normal store is enough, I'll fixup it in next version patch. > Also, the fact that you need MB for release implies your LDEX does not > in fact imply anything and your xchg/cmpxchg implementation is broken. xchg/cmxchg broken without 1th smp_mb()? Why we need protect the instructions flow before the ldex.w? > > +static inline int arch_spin_trylock(arch_spinlock_t *lock) > > +{ > > + unsigned int *p = &lock->lock; > > + unsigned int tmp; > > + > > + asm volatile ( > > + "1: ldex.w %0, (%1) \n" > > + " bnez %0, 2f \n" > > + " movi %0, 1 \n" > > + " stex.w %0, (%1) \n" > > + " bez %0, 1b \n" > > + " movi %0, 0 \n" > > + "2: \n" > > + : "=&r" (tmp) > > + : "r"(p) > > + : "memory"); > > + smp_mb(); > > + > > + return !tmp; > > +} > > Strictly speaking you can avoid the MB on failure. You only need to > provide ACQUIRE semantics on success. > > That said, I would really suggest you implement a ticket lock instead of > a test-and-set lock. They're not really all that complicated and do > provide better worst case behaviour. Ok, I'll try to implement ticket lock in next version patch. > > > > +/****** read lock/unlock/trylock ******/ > > Please have a look at using qrwlock -- esp. if you implement a ticket > lock, then the rwlock comes for 'free'. Ok, I'll try it. Guo Ren