From patchwork Thu Sep 7 17:47:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13376736 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB30EEC8742 for ; Thu, 7 Sep 2023 17:47:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0979D6B007E; Thu, 7 Sep 2023 13:47:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F3B2A8E0002; Thu, 7 Sep 2023 13:47:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DDBBD8E0001; Thu, 7 Sep 2023 13:47:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BCAFD6B007E for ; Thu, 7 Sep 2023 13:47:29 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 8F2EC1A1005 for ; Thu, 7 Sep 2023 17:47:29 +0000 (UTC) X-FDA: 81210533418.22.1330113 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf09.hostedemail.com (Postfix) with ESMTP id D9ACF140005 for ; Thu, 7 Sep 2023 17:47:27 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=jAKEfKsh; dmarc=none; spf=none (imf09.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694108848; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=LLFJT9lhOndsodIHfPQ9LEtDlXX6YGclPNMO2t1USgU=; b=iRFlADJGqXVETJiJo7s2uvQ+2nDcvFCes1EHckfkCAviDpKxhXAj990PyPMFLW4CSeFpv1 01zw5xHy919ZUidmJOSPTZvvLtMqD9sJTG25oSnNWMEcPPhNeZb3yBh8a3leFo4FvCe02e c7Q/JLyTaH9ppUnoM24NUG0j++3SZ4Y= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=jAKEfKsh; dmarc=none; spf=none (imf09.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694108848; a=rsa-sha256; cv=none; b=NCylGJioUfIbk4+n7KwpfE8dlO89cJ/ksTduSyNhcZ1T9rffGwR86RZhK6905VW6ZsS1GF Q6b7y5C1TE1oU3+Bv9yAy0kDpdgKdtiMo304nCAeEXsSd34R+WfMbusTR+iDK3730jboR3 mBqLpZ2wABrTxyI/JHO0oN5j60P6Fkg= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=LLFJT9lhOndsodIHfPQ9LEtDlXX6YGclPNMO2t1USgU=; b=jAKEfKshiWNUs4KA27DrmnJak0 fotEfuCnFgJxrpNUy54FTDDByW/E9sBz1SE2zakIlZJhp1hvXQ/JrczFhZlH5vBtzyTCrBEViEZ1r rD2XlLJb/lmeXQvvhgAR1iFsykO9s7L/WtUzp4faEt4pLZWRXY68YIibxqDRTv/VmZzG6Z/HQjVcK WuDOyAggJnPFYDxeVSPWQmtmGsGIvkM67ia+qMuyl1Axj3bfOOoVGq16Elsg7Un3O2gVO45V950KH Cs5NbUja00oSR1Ep8FiYLK7eeJGKRtDPUrjGr35KP12gtU4KJaIFuyWbCHsCgPd1jB3NrkeDddkYO lp9gc8kQ==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qeJ5v-00CUFJ-Uv; Thu, 07 Sep 2023 17:47:07 +0000 From: "Matthew Wilcox (Oracle)" To: Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long Cc: "Matthew Wilcox (Oracle)" , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Chandan Babu R , "Darrick J . Wong" , linux-xfs@vger.kernel.org Subject: [PATCH 1/5] locking: Add rwsem_is_write_locked() Date: Thu, 7 Sep 2023 18:47:01 +0100 Message-Id: <20230907174705.2976191-2-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230907174705.2976191-1-willy@infradead.org> References: <20230907174705.2976191-1-willy@infradead.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: D9ACF140005 X-Stat-Signature: 3oquhfbywrmh9ghnfm4aoksm3k7hc3bu X-HE-Tag: 1694108847-528856 X-HE-Meta: U2FsdGVkX1/JYZnDX+Z5H2rJb+ieWICwuBh06eGqhmLrugEiswPdFq4rgtQ5CHWPyfvqHFqic8OLA2jUh++k8Ap0N0AuBFjqufS6fNePa41e9gEtDDk2FFRMF1YYgYRflPq0PEqwrXRdZNHDnz2+1m34P19SDamd5sK5pLnIa336Zx7T6YmNdrlhsphuEXcgt2aluZYKrSX+/9J9aTBRoWAC2jDQkBoA5oQdgA6ceRrNoL3/enMrrVo66PKa9VlyLn10LNvLGi9ANzeZnMCQljLYKSV8OpBTdcGZoZZ1YVoTa5CzJKS1OTIocKrgv6lObDVTUSPNuJE1A3W/z35jme3bGWAEav3gFDEvHyzlDsKUpcu6+qKBNocQoC1xiFWwh3jdbkQjg2kF5/xOvOo0Vac9S9JgEHgZfOjHKzaKPHbiQ2ClfhqzObfETOn9OdQS7DR0K/blSK3/xkgLL8ZWIFJ90fITWJrbcRzvU4VRJ5+4y1fxX3NesrW6GWYVP0a3D5Kc6bo0kjvwDlp6LvlGNh9koOEmkkaVNoxjGjdUD6ePs4dTzbSYkvmOEuccAAw2nXbkMcsmLLOLv1J8iV5TstqmeMeobSG5QArWNy0HScFU/y1+JShDGybFE2tCwSkhQkKh7A3xrGPMbfAC1iieb4xacIZFU4dTEH05QQVW6zfGcoyoh4aj6wpCOvR8c3WD3kwP5X/UjVYDnlEeaPrxbdOwKbWyeySw4mVWX6ZFpb1oXPOrbIEKHmH+R8wO+33p6uNuKPasBW9ctYgUK4+tv0Xij7JTPeOxKDc+Bcbv7oJ8Aw2DyE+wn9cJK1AjmrMdlXCUJmyHwAFvcL9rdsDnXDJQnsj8Sm+W5LnRAwOy9g48YUcUqGKkoR+yBDHH3MoYWImHF19cAfF1xQeJ5ATfYYdbQed0Cn3L1kAVj858wXs3F72IaCLgZOvypIn7lcxFWAgxWZMuxjqxpog3hB7 zTF4ybo+ Bq18b/EKK56/TYm0yKWaw6htdYt8G3L9ezI1tYI/wb/+GLehk4bv4O/tH+nAsVAOLwx6n3CXeXY0aMT0lMhxBdk+BVzi5rvo9tVKaJIA4R3CKm5AG1NZs5gs39W2+WVRLPnAWRupgzjvi7X2r5euk33kqO/xe/PEBcmJwQu3Vpr3LCAjy/ejsWQvbLVoqZu7vPdEVoKgoP5U73+C90Vz4yme5JZN/S7473Tnm X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Several places want to know whether the lock is held by a writer, instead of just whether it's held. We can implement this for both normal and rt rwsems. RWSEM_WRITER_LOCKED is declared in rwsem.c and exposing it outside that file might tempt other people to use it, so just use a comment to note that's what the 1 means, and help anybody find it if they're looking to change the implementation. Signed-off-by: Matthew Wilcox (Oracle) --- include/linux/rwbase_rt.h | 5 +++++ include/linux/rwsem.h | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/linux/rwbase_rt.h b/include/linux/rwbase_rt.h index 1d264dd08625..3c25b14edc05 100644 --- a/include/linux/rwbase_rt.h +++ b/include/linux/rwbase_rt.h @@ -31,6 +31,11 @@ static __always_inline bool rw_base_is_locked(struct rwbase_rt *rwb) return atomic_read(&rwb->readers) != READER_BIAS; } +static __always_inline bool rw_base_is_write_locked(struct rwbase_rt *rwb) +{ + return atomic_read(&rwb->readers) == WRITER_BIAS; +} + static __always_inline bool rw_base_is_contended(struct rwbase_rt *rwb) { return atomic_read(&rwb->readers) > 0; diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h index 1dd530ce8b45..0f78b8d2e653 100644 --- a/include/linux/rwsem.h +++ b/include/linux/rwsem.h @@ -72,6 +72,11 @@ static inline int rwsem_is_locked(struct rw_semaphore *sem) return atomic_long_read(&sem->count) != 0; } +static inline int rwsem_is_write_locked(struct rw_semaphore *sem) +{ + return atomic_long_read(&sem->count) & 1 /* RWSEM_WRITER_LOCKED */; +} + #define RWSEM_UNLOCKED_VALUE 0L #define __RWSEM_COUNT_INIT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE) @@ -157,6 +162,11 @@ static __always_inline int rwsem_is_locked(struct rw_semaphore *sem) return rw_base_is_locked(&sem->rwbase); } +static __always_inline int rwsem_is_write_locked(struct rw_semaphore *sem) +{ + return rw_base_is_write_locked(&sem->rwbase); +} + static __always_inline int rwsem_is_contended(struct rw_semaphore *sem) { return rw_base_is_contended(&sem->rwbase);