Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp1526477ima; Sun, 21 Oct 2018 13:59:51 -0700 (PDT) X-Google-Smtp-Source: ACcGV601b9N0stjNjgFJYvxDcpKgzqrtauc9aBRi7uBhOCGgElOqawVMkjLFYVhk7b8jelBA6UuA X-Received: by 2002:a62:302:: with SMTP id 2-v6mr42201040pfd.145.1540155591777; Sun, 21 Oct 2018 13:59:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540155591; cv=none; d=google.com; s=arc-20160816; b=QEYx444g/5GLK3Ccn/U5duU2a/Hbh3bcO6gBJtLP4a6Yb94ApIzJ7N6m9RKjSET16q Cqs+GiMvsr9YxjKFGA5QXowu1C/kUpXRTLhDM6/lfJsq9jRnwgtoi+iZ+8UTpkGALVDY wbVjOQkcinXSDn7g+hVXMGQ4Pg7oRWluvJWwCq5j0gEDFW8hTvAr+g78Asvb+eUsM9Bb 8umGKOFf+yHj/2aafzHZcVrZ9Dk8rf7Ap9HRyccilnYaddDI7+3oSrAMhW2Iy2XmwaqU W0hAb4p6yKSPDZ5LyIDY66Nif7bvpk9FLZNWtvXAGOt8Im7Fun2w/LNZchXhZoA77k4R KAzA== 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:dkim-signature; bh=yxLYSLwiK458PCfnApAUce3EGbQxYsHnAB/ClpvaiiA=; b=WZrJ+TRWnY44uBOTEJg5FSck5B2+Upf+4pfyZzgJ/db+W7yxCIdWHCLhLLYdGG8Ikw nRFL1AEd5Yhmxe/WBk7H89oJBcW8n/BGe6hXtcKG9M/f+D7ZL94y2r0BSM0Iv2YltXmZ lpIFlu1KXJ/UqcUi+kWuLP13n45JQe7bqdmulRQkM9wZsiPJU3r1101YrdZb76/BSDpF 5TmOp8cZjGOmVQrWfW2vrx+d8jPWnopSMlI6hIJ0QjovM+TxhSCSqw6LTNkRN248SfIb fQRzUYmfD7fuE32nJbra+HWQV2uct6Ao36didRueVPbcdEkkdDSCUzTeIlUcsDVnqx8E UNeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=Pe6RW2fO; 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 h10-v6si12395784pgk.570.2018.10.21.13.59.37; Sun, 21 Oct 2018 13:59:51 -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; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=Pe6RW2fO; 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 S1728177AbeJVFKt (ORCPT + 99 others); Mon, 22 Oct 2018 01:10:49 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:54540 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727256AbeJVFKt (ORCPT ); Mon, 22 Oct 2018 01:10:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=yxLYSLwiK458PCfnApAUce3EGbQxYsHnAB/ClpvaiiA=; b=Pe6RW2fOEBV8vWhuE0pEYORjy qz+mg2LUKNg0xgn8swK1JcnJNaGXxwXi+ejpe/PryYd1nR/XlWzitok6oNGsBsJ4ZDG5SmyntjYG+ zuPEKFVBNNuFgZftLGxbPKL/CQhu4yKoXEjD76XjPmOL2IHuhx+9umIUgHt/wD86hAGGMBCelv6JF DV/GppgXJwhC50kbMJBk+CyE8LL4yXjlRgwkWxTtvVmLGbiG5Ihr9JGOUuF2CpV/7PURnDciEvP6F hq2mEdUjNs2XVm2AklEIsUpk2065dRY7yObffhAZ02O8phAGgiNtIwMAh9Q1BqcP84XDxzZe+gH2u h2eVyA/WQ==; Received: from [167.98.65.38] (helo=worktop) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1gEKkW-0008Mm-2U; Sun, 21 Oct 2018 20:55:00 +0000 Received: by worktop (Postfix, from userid 1000) id 236A06E0A72; Sun, 21 Oct 2018 22:55:08 +0200 (CEST) Date: Sun, 21 Oct 2018 22:55:08 +0200 From: Peter Zijlstra To: Guo Ren Cc: akpm@linux-foundation.org, arnd@arndb.de, daniel.lezcano@linaro.org, davem@davemloft.net, gregkh@linuxfoundation.org, hch@infradead.org, marc.zyngier@arm.com, mark.rutland@arm.com, robh@kernel.org, tglx@linutronix.de, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, devicetree@vger.kernel.org, robh+dt@kernel.org, c-sky_gcc_upstream@c-sky.com, Andrea Parri Subject: Re: [PATCH V9 11/21] csky: Atomic operations Message-ID: <20181021205508.GJ4931@worktop.programming.kicks-ass.net> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.22.1 (2013-10-16) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 16, 2018 at 10:58:30AM +0800, Guo Ren wrote: > diff --git a/arch/csky/include/asm/spinlock.h b/arch/csky/include/asm/spinlock.h > new file mode 100644 > index 0000000..0474603 > --- /dev/null > +++ b/arch/csky/include/asm/spinlock.h > @@ -0,0 +1,274 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > + > +#ifndef __ASM_CSKY_SPINLOCK_H > +#define __ASM_CSKY_SPINLOCK_H > + > +#include > +#include > + > +#ifdef CONFIG_QUEUED_RWLOCKS > + > +/* > + * Ticket-based spin-locking. > + */ > +static inline void arch_spin_lock(arch_spinlock_t *lock) > +{ > + arch_spinlock_t lockval; > + u32 ticket_next = 1 << TICKET_NEXT; > + u32 *p = &lock->lock; > + u32 tmp; > + > + asm volatile ( > + "1: ldex.w %0, (%2) \n" > + " mov %1, %0 \n" > + " add %0, %3 \n" > + " stex.w %0, (%2) \n" > + " bez %0, 1b \n" > + : "=&r" (tmp), "=&r" (lockval) > + : "r"(p), "r"(ticket_next) > + : "cc"); > + > + while (lockval.tickets.next != lockval.tickets.owner) > + lockval.tickets.owner = READ_ONCE(lock->tickets.owner); > + > + smp_mb(); > +} > + > +static inline int arch_spin_trylock(arch_spinlock_t *lock) > +{ > + u32 tmp, contended, res; > + u32 ticket_next = 1 << TICKET_NEXT; > + u32 *p = &lock->lock; > + > + do { > + asm volatile ( > + " ldex.w %0, (%3) \n" > + " movi %2, 1 \n" > + " rotli %1, %0, 16 \n" > + " cmpne %1, %0 \n" > + " bt 1f \n" > + " movi %2, 0 \n" > + " add %0, %0, %4 \n" > + " stex.w %0, (%3) \n" > + "1: \n" > + : "=&r" (res), "=&r" (tmp), "=&r" (contended) > + : "r"(p), "r"(ticket_next) > + : "cc"); > + } while (!res); > + > + if (!contended) > + smp_mb(); > + > + return !contended; > +} > + > +static inline void arch_spin_unlock(arch_spinlock_t *lock) > +{ > + smp_mb(); > + lock->tickets.owner++; WRITE_ONCE(lock->tickets.owner, lock->tickets.owner + 1); > +} > + > +static inline int arch_spin_value_unlocked(arch_spinlock_t lock) > +{ > + return lock.tickets.owner == lock.tickets.next; > +} > + > +static inline int arch_spin_is_locked(arch_spinlock_t *lock) > +{ > + return !arch_spin_value_unlocked(READ_ONCE(*lock)); > +} > + > +static inline int arch_spin_is_contended(arch_spinlock_t *lock) > +{ > + struct __raw_tickets tickets = READ_ONCE(lock->tickets); > + > + return (tickets.next - tickets.owner) > 1; > +} > +#define arch_spin_is_contended arch_spin_is_contended > + > +#include > + > +/* See include/linux/spinlock.h */ > +#define smp_mb__after_spinlock() smp_mb() > + > +#else /* CONFIG_QUEUED_RWLOCKS */ > + > +/* > + * Test-and-set spin-locking. > + */ I'm still not entirely sure why you want to have two spinlock implementations; to me that is just extra maintenance overhead. > +static inline void arch_spin_lock(arch_spinlock_t *lock) > +{ > + u32 *p = &lock->lock; > + u32 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) > + : "cc"); > + smp_mb(); > +} > + > +static inline void arch_spin_unlock(arch_spinlock_t *lock) > +{ > + u32 *p = &lock->lock; > + u32 tmp; > + > + smp_mb(); > + asm volatile ( > + " movi %0, 0 \n" > + " stw %0, (%1) \n" > + : "=&r" (tmp) > + : "r"(p) > + : "cc"); WRITE_ONCE(lock->lock, 0); ? > +} > + > +static inline int arch_spin_trylock(arch_spinlock_t *lock) > +{ > + u32 *p = &lock->lock; > + u32 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) > + : "cc"); > + > + if (!tmp) > + smp_mb(); > + > + return !tmp; > +} > + > +#define arch_spin_is_locked(x) (READ_ONCE((x)->lock) != 0) > + > +/* > + * read lock/unlock/trylock > + */ Idem, why do you want a second rwlock_t implementation? > +/* > + * write lock/unlock/trylock > + */ > +static inline void arch_write_lock(arch_rwlock_t *lock) > +{ > + u32 *p = &lock->lock; > + u32 tmp; > + > + asm volatile ( > + "1: ldex.w %0, (%1) \n" > + " bnez %0, 1b \n" > + " subi %0, 1 \n" > + " stex.w %0, (%1) \n" > + " bez %0, 1b \n" > + : "=&r" (tmp) > + : "r"(p) > + : "cc"); > + smp_mb(); > +} > + > +static inline void arch_write_unlock(arch_rwlock_t *lock) > +{ > + u32 *p = &lock->lock; > + u32 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) > + : "cc"); Isn't that: WRITE_ONCE(lock->lock, 0); > +} > + > +static inline int arch_write_trylock(arch_rwlock_t *lock) > +{ > + u32 *p = &lock->lock; > + u32 tmp; > + > + asm volatile ( > + "1: ldex.w %0, (%1) \n" > + " bnez %0, 2f \n" > + " subi %0, 1 \n" > + " stex.w %0, (%1) \n" > + " bez %0, 1b \n" > + " movi %0, 0 \n" > + "2: \n" > + : "=&r" (tmp) > + : "r"(p) > + : "cc"); > + > + if (!tmp) > + smp_mb(); > + > + return !tmp; > +} > diff --git a/arch/csky/kernel/atomic.S b/arch/csky/kernel/atomic.S > new file mode 100644 > index 0000000..d2357c8 > --- /dev/null > +++ b/arch/csky/kernel/atomic.S > @@ -0,0 +1,87 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +// Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd. > + > +#include > +#include > + > +.text > + > +/* > + * int csky_cmpxchg(int oldval, int newval, int *ptr) > + * > + * If *ptr != oldval && return 1, > + * else *ptr = newval return 0. > + */ > +#ifdef CONFIG_CPU_HAS_LDSTEX > +ENTRY(csky_cmpxchg) > + USPTOKSP > + mfcr a3, epc > + INCTRAP a3 > + > + subi sp, 8 > + stw a3, (sp, 0) > + mfcr a3, epsr > + stw a3, (sp, 4) > + > + psrset ee > +1: > + ldex a3, (a2) > + cmpne a0, a3 > + bt16 2f > + mov a3, a1 > + stex a3, (a2) > + bez a3, 1b > +2: > + sync.is > + mvc a0 > + ldw a3, (sp, 0) > + mtcr a3, epc > + ldw a3, (sp, 4) > + mtcr a3, epsr > + addi sp, 8 > + KSPTOUSP > + rte > +END(csky_cmpxchg) I don't understand why you have this; if the CPU has ll/sc, why do you need syscall support? In any case, nothing terminally broken; so I suppose that's good enough for starters. I just really don't understand some decisions (like having two lock implementations and having that cmpxchg syscall when you have hardware ll/sc). Acked-by: Peter Zijlstra (Intel)