From patchwork Mon Feb 26 15:04:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 129671 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp301139lja; Mon, 26 Feb 2018 07:06:35 -0800 (PST) X-Google-Smtp-Source: AH8x224ljnCjGzkVsNGGUho9Y+nHv1Mc0yQnwitE4OIYd6GC3fz+WbcSfq2ZFC+rFCwCC//BTAbM X-Received: by 10.99.154.73 with SMTP id e9mr8474106pgo.26.1519657595197; Mon, 26 Feb 2018 07:06:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519657595; cv=none; d=google.com; s=arc-20160816; b=mO0ZcJjg4ZqeBjX/tnOdWoKJU2OPECypBewboLlUPWZ0xfbnQWNur7kSziLSncEHzw +SLK9Sw6AjTiz4U5FPhA3mZfanEdMja4kV6wh5nC76UzdlfVhHRgoA3bfMFSVHwrJhHM l2Tuf3YKC5bZ0LslhcSD3LAU1yM65ZCD3OsGkEc4eel9DHsFIz26w3y+9Z6CLjDjfnPc 2dSgXZhzhqYtOAOWFlEIyzUJ0BYGOMmfd2dJVdOvS5k0agumkVyx0aCgePi5OMh9qIjH 1pabD6TzC2ILAUjvZF/WXE6/E5JkZWNwiU3AtRAF3seyDXMsbTFfpBtFyoSr/DLZ3Rkr Zfig== 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=emCbpjuZ5C/MQFCRko2+iHxV7ipRXKkc8DJld0LW6/F22O7tps0Jx3HjNStjX09rqT ZU/+CtEHRkCAirjLTkjPxdqyG54CKCh2sKZZuQLJ4h2/HjxtLFOWt7dfm+MDkuJ4nPzo eu6vVnctv8xyzvBfKAIL+v6Gdd5RPXLS9ImoU9eA1l8Y6eR4sOPQOg6mt/qKEyMKuP3m Udb/1t0mOc2wnwg0c12HLF9a3/E+gkh3a3T1vlzXWIwiQQEDE9GTLeeWzuHhLbZoY0qU eztCDjDuUQM0ZarOP3SskEt6ntXrOkypgBIYsfZRHiXGNiXOLiGihWrYoERszfPsY+bZ cxDg== 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 l14si5597947pgc.615.2018.02.26.07.06.34; Mon, 26 Feb 2018 07:06:35 -0800 (PST) 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 S1754190AbeBZPGb (ORCPT + 28 others); Mon, 26 Feb 2018 10:06:31 -0500 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:51196 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754003AbeBZPFB (ORCPT ); Mon, 26 Feb 2018 10:05:01 -0500 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 058FE1715; Mon, 26 Feb 2018 07:05:01 -0800 (PST) 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 CBE973F487; Mon, 26 Feb 2018 07:05:00 -0800 (PST) Received: by edgewater-inn.cambridge.arm.com (Postfix, from userid 1000) id 04FC31AE53B2; Mon, 26 Feb 2018 15:05:01 +0000 (GMT) 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: [RFC PATCH v2 10/12] asm-generic/bitops/lock.h: Rewrite using atomic_fetch_* Date: Mon, 26 Feb 2018 15:04:58 +0000 Message-Id: <1519657500-15094-11-git-send-email-will.deacon@arm.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1519657500-15094-1-git-send-email-will.deacon@arm.com> References: <1519657500-15094-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_ */