From patchwork Thu May 24 10:59:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 136738 Delivered-To: patch@linaro.org Received: by 2002:a2e:9706:0:0:0:0:0 with SMTP id r6-v6csp2043564lji; Thu, 24 May 2018 04:02:43 -0700 (PDT) X-Google-Smtp-Source: AB8JxZq9TU9QhPbV8smSOb9YrE0e6yJ0yltmeAQMVtoq/iP+aBW7HgX0FXb/tU5bzXow8xoogBPX X-Received: by 2002:a63:7d43:: with SMTP id m3-v6mr5526876pgn.117.1527159763800; Thu, 24 May 2018 04:02:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527159763; cv=none; d=google.com; s=arc-20160816; b=xUngUJBlUk0jjbKfkMPZG5vACVbQqeg003XV/oiJJ8QXi+zIecqRWSE+p6+xTUjPZo AVA9CbhJVvM+//Zf8SY88cuvf20bOuItggOORBZjNDHWSczhAQFA891pVXPlQuFs/6eC FNMQ6dGTsdL576SXGpxpLMmEGqgNXXYC684PFSHnFHF3BhxhZ5CpZD+hOo9PAG+XEwPV fKjMDvgdktO6PVMTD0wkh+OWdHl1zGK927IZOltSlJuVM1yeRJQfgCeTUqCB5VLRs4WM RSZSBSExzU7PyVpnYdEQVFk7YL9f4As/xGEZtZXeQG1fRgLzutot+Cdi6reUKSTVzxf6 FU2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=n3bChh5u1Vbmh9Huh5A1pqswxh6z1axa6kHAyv6GYH8=; b=IwlzP76N0buwQvUbCDVyHZJXk8JclkjqTQlDR8Y/O7LwfGLl6Co//XhnJLtWzINvhX 0kGrXE4T+MwiiqKlqYICg2CpRJzgFetivLCpg8C9/WrNZBaEUM2tasNMu8X68o9yZu8J AeXQrXuHhHYYsUm3rIlBeKYPeUpt+S4OvHBBOEPGYkbDXUh7bd277qesDRzK7sOcyt1g SR7ZcP7QEgiF04IRc7Gl1TPjuy7Y7fEYuKo8jZn73P5r8QgZySk2vwFhD3fjcaS2Blrk DawUr3SgK+fXYzrFyg4jRNRf8TmtTbTfSpFmz8BCbTY0IOhZPYFSAN3/Kx5XVuPE/Exn mmgg== 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 x32-v6si20519637pld.435.2018.05.24.04.02.43; Thu, 24 May 2018 04:02:43 -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 S1032419AbeEXLCl (ORCPT + 30 others); Thu, 24 May 2018 07:02:41 -0400 Received: from foss.arm.com ([217.140.101.70]:40986 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1032596AbeEXK7V (ORCPT ); Thu, 24 May 2018 06:59:21 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 77EF916EA; Thu, 24 May 2018 03:59:20 -0700 (PDT) Received: from edgewater-inn.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 484023F762; Thu, 24 May 2018 03:59:20 -0700 (PDT) Received: by edgewater-inn.cambridge.arm.com (Postfix, from userid 1000) id BEDB71AE38E7; Thu, 24 May 2018 11:59:47 +0100 (BST) From: Will Deacon To: linux-kernel@vger.kernel.org Cc: peterz@infradead.org, mingo@kernel.org, linux-arm-kernel@lists.infradead.org, yamada.masahiro@socionext.com, Will Deacon Subject: [PATCH 7/9] asm-generic/bitops/lock.h: Rewrite using atomic_fetch_* Date: Thu, 24 May 2018 11:59:44 +0100 Message-Id: <1527159586-8578-8-git-send-email-will.deacon@arm.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1527159586-8578-1-git-send-email-will.deacon@arm.com> References: <1527159586-8578-1-git-send-email-will.deacon@arm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The lock bitops can be implemented more efficiently using the atomic_fetch_* ops, which provide finer-grained control over the memory ordering semantics than the bitops. Cc: Peter Zijlstra Cc: Ingo Molnar Signed-off-by: Will Deacon --- include/asm-generic/bitops/lock.h | 68 ++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 12 deletions(-) -- 2.1.4 diff --git a/include/asm-generic/bitops/lock.h b/include/asm-generic/bitops/lock.h index 67ab280ad134..3ae021368f48 100644 --- a/include/asm-generic/bitops/lock.h +++ b/include/asm-generic/bitops/lock.h @@ -2,6 +2,10 @@ #ifndef _ASM_GENERIC_BITOPS_LOCK_H_ #define _ASM_GENERIC_BITOPS_LOCK_H_ +#include +#include +#include + /** * test_and_set_bit_lock - Set a bit and return its old value, for lock * @nr: Bit to set @@ -11,7 +15,20 @@ * the returned value is 0. * It can be used to implement bit locks. */ -#define test_and_set_bit_lock(nr, addr) test_and_set_bit(nr, addr) +static inline int test_and_set_bit_lock(unsigned int nr, + volatile unsigned long *p) +{ + long old; + unsigned long mask = BIT_MASK(nr); + + p += BIT_WORD(nr); + if (READ_ONCE(*p) & mask) + return 1; + + old = atomic_long_fetch_or_acquire(mask, (atomic_long_t *)p); + return !!(old & mask); +} + /** * clear_bit_unlock - Clear a bit in memory, for unlock @@ -20,11 +37,11 @@ * * This operation is atomic and provides release barrier semantics. */ -#define clear_bit_unlock(nr, addr) \ -do { \ - smp_mb__before_atomic(); \ - clear_bit(nr, addr); \ -} while (0) +static inline void clear_bit_unlock(unsigned int nr, volatile unsigned long *p) +{ + p += BIT_WORD(nr); + atomic_long_fetch_andnot_release(BIT_MASK(nr), (atomic_long_t *)p); +} /** * __clear_bit_unlock - Clear a bit in memory, for unlock @@ -37,11 +54,38 @@ do { \ * * See for example x86's implementation. */ -#define __clear_bit_unlock(nr, addr) \ -do { \ - smp_mb__before_atomic(); \ - clear_bit(nr, addr); \ -} while (0) +static inline void __clear_bit_unlock(unsigned int nr, + volatile unsigned long *p) +{ + unsigned long old; -#endif /* _ASM_GENERIC_BITOPS_LOCK_H_ */ + p += BIT_WORD(nr); + old = READ_ONCE(*p); + old &= ~BIT_MASK(nr); + atomic_long_set_release((atomic_long_t *)p, old); +} + +/** + * clear_bit_unlock_is_negative_byte - Clear a bit in memory and test if bottom + * byte is negative, for unlock. + * @nr: the bit to clear + * @addr: the address to start counting from + * + * This is a bit of a one-trick-pony for the filemap code, which clears + * PG_locked and tests PG_waiters, + */ +#ifndef clear_bit_unlock_is_negative_byte +static inline bool clear_bit_unlock_is_negative_byte(unsigned int nr, + volatile unsigned long *p) +{ + long old; + unsigned long mask = BIT_MASK(nr); + + p += BIT_WORD(nr); + old = atomic_long_fetch_andnot_release(mask, (atomic_long_t *)p); + return !!(old & BIT(7)); +} +#define clear_bit_unlock_is_negative_byte clear_bit_unlock_is_negative_byte +#endif +#endif /* _ASM_GENERIC_BITOPS_LOCK_H_ */