Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1986076imm; Thu, 24 May 2018 04:04:15 -0700 (PDT) X-Google-Smtp-Source: AB8JxZppyzPQcfSYrHw6gFTfxXIBs9hbgvGG9Je4jUhxZ/gKA3jSY3xDSf8JGfMMgszttUxHyCz5 X-Received: by 2002:a65:65d2:: with SMTP id y18-v6mr5331316pgv.186.1527159855108; Thu, 24 May 2018 04:04:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527159855; cv=none; d=google.com; s=arc-20160816; b=Ys6W0lk/HjhzhQtdqqmL1SP2Gs/CUD07ASlWrp961YPAUGiyBJUKrQJjq7DGpwjC9j //BcR65mpipeUB4pQrhxGD3ggJtd8sn35R/wiRpb2M29eb0a+RxLiSgPlIGmFcE/ggkO rdNKY+gV78TZf+Er9qd/0QCS0ISNCQSoAfuksVKJglD3E0e1DZjckNfxw+2WB2mZqL+e Dlz1SPQAI1OlhA9McDbTEMFPRT9E8ZV3qtVc/YPBCHNUtpZcM0tP/6sDlxibTegnslXP gpwwqsLtUOEwWvCVkSKzeI1BUta8t2TGj3U2xRlOUbL1IfCUtBlC80tL4NNad2C1xl4D aAwQ== 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 f18-v6si1035790pgn.100.2018.05.24.04.03.59; Thu, 24 May 2018 04:04:15 -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 S1030324AbeEXLCl (ORCPT + 99 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(-) 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_ */ -- 2.1.4