From patchwork Sat Oct 7 20:35:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13412476 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 10E5CE95A6B for ; Sat, 7 Oct 2023 20:36:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 07C568D0001; Sat, 7 Oct 2023 16:36:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F36168D0013; Sat, 7 Oct 2023 16:36:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DFC438D0012; Sat, 7 Oct 2023 16:36:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id D212F8D0001 for ; Sat, 7 Oct 2023 16:36:02 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 90B4514028C for ; Sat, 7 Oct 2023 20:36:02 +0000 (UTC) X-FDA: 81319822164.22.E0E0B2F Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf24.hostedemail.com (Postfix) with ESMTP id 000C818001F for ; Sat, 7 Oct 2023 20:36:00 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=WDbKmpqH; dmarc=none; spf=none (imf24.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=1696710961; 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=gK2PkVQedGYkiXZ+S6dDW/QdpinQQuQZI6/6Ogj39CE=; b=vmiB8vKM86Wd3jK1O8woyv/R9seVzT++v+7IBx7JE8mEuyjJN1u1H4gCsaQrZTxpZhNDSf 3A+WbTu/FGG+hAG5a6Lsd0fcdih72cs+bFkWXVNqt8FSzWP6s3F6qIicsUcrwYM8fPTxI6 OWsjOG9kVs2BVcEJj6rwEB039UVbFqU= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=WDbKmpqH; dmarc=none; spf=none (imf24.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=1696710961; a=rsa-sha256; cv=none; b=j4NDsRtf8uBs5OrW/UUWAN2kzrieRYKGkKd08sebavcjEWiI5oRY5HW4YrUCUY/fYj6VU9 KcCNm1KJCqG+3W9pXrXwkJ2oJmyMYflvdbBmv4B1c4h8NreOfiiAGONaIvaASfH41CZZBO XntxW+HClnP133FObUgbPrFAZQvNfVM= 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=gK2PkVQedGYkiXZ+S6dDW/QdpinQQuQZI6/6Ogj39CE=; b=WDbKmpqHsfQKSeC/TKnzhmuJiI XlF8tSu64opY8lCrqTKdNZn4EJxfi2q/PChuCviUe9nlITFxCNCS8H6sg23IVfp1KEV4oa3Uabg4v k/8SyjPEtT9LSef6M4Fq2ekcazpitwdP4ZUd/vMn5CV1MqbJnh/xZvWaVpuiRjU3ljPr6twQONFF8 NPVe2/HFVL0mnVB8pEWRl6UlGUioGDu/ZwK+lDNBHqEMNs0Gu5XNP0n60hTF12hgDjq5gOsVl8Smz Ha3qXka1ZbBhtaqJ99sTPBCiH6gvEXWtXloFKTA0k3TS48IMtX2jSAs2ZqNaDwUuU5DyZDgB5P+kr f9RhCEsQ==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qpE1Y-005mLA-Ih; Sat, 07 Oct 2023 20:35:44 +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, Mateusz Guzik Subject: [PATCH v2 1/5] locking: Add rwsem_assert_held() and rwsem_assert_held_write() Date: Sat, 7 Oct 2023 21:35:39 +0100 Message-Id: <20231007203543.1377452-2-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20231007203543.1377452-1-willy@infradead.org> References: <20231007203543.1377452-1-willy@infradead.org> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 000C818001F X-Stat-Signature: 6qiyigbunbq167qpcgefnprgufw3qqqq X-Rspam-User: X-HE-Tag: 1696710960-172896 X-HE-Meta: U2FsdGVkX1+nVfb6paK9U65CdjJE3a/lysdIH1p78zaBS04G7Bq20pco5B9bE18RMN4OeEzIQKuKkd1cEyO2P3GaKJxoLZb7vAW9zvpFvA+xUtPxsl7mPGq8p3pLHPFKMuOOLh/jJfS6mmkPftN5zmYlD/IXsbAvt+fn+gGn4fISSP9klSwFnrWYUj1ZfbeVYme3v8FguhRxK0WwMYb6ydFCV1PG+FaqvCKnWybNJMJAkhaCSZ8E/anfqBHZjFQdVnL4hXlkbaxV/m5jz9zA/PtzTBo8GcHYQLzPfHXsy84NyXm4P2G6llVL4Op0kdY9SPQ0c9Dz4hw95SwznUe6kcjnoavyYEPu8V8gesX1ELqLcPiNmpDnufC/HO/Weyd+TehRlI/AqlpC/kO7tG9fKtTIMpxbo5QRGoOtwJVrQMvYE2/9twJ5jHhKkZs6WrhVkJXkBA8ath1UQ5MrnJrbx1ebeOJ1DWjWQvI0YI1jac3XhG3gXJfVOoI8RQNAY2gdz6M5gsCwKZvUOFSvOhFW17B/r7hx0m5hUR/gIrGRvDuXHhRQP5TbboCwJzYOzj+g+2tzDG+b/gHEXsYB54n5p6b/Mph3c9RGjWcrb6/bekUO8hzWgdGN5kbBrU6NNBH92XNvfPCqodZd/16nGUE/E3o1mMO+iLRG6VSgoVKxC3/byI2P/YYeMHe/B5qdHtqPHMvYqLqhor5ZhyJmJNNPXbd2bwoUT+nyA7Cg1cUp+hC3UA1+Cx44E/3J25biXNopo3fJgNTPPfYLkgXOuOLTmirppajferFAwJoRefX81HiBB4SWaHVD0E5eOXUTYaqmyXe5/2Ew7AG6YDgGIrNpaRmsz+7ezg8Q3BYfNDMEEv/VljCbMepuHBtCWsQ/CWjhSD/CIUQ0GdLBeeWN2DJ3C3h9Vx9s6OBZOLfEj1Qjv3w/HgCtFi+/mNo8eqrWSO67TzSM8aTtUZFrisAtrHy Ma2c5Msm PiYSU0ZIHIpF6SalfXD+Jh6kbjVRLDhG+V/q896c2gTgfNwcyu+IQypHPGe143oet+c0B0lI+zBT6gcqiG0dxR9I2LVXlwfoKoYKj6tvvTtr4Gaey3mX+ShDisGMbd6Bp/f4P1Z8rCXVdxE9CovfpbLyA2QMgM63HShk3C4vBrciJC083Fu6hARN8GSGK1nTibxKZzE4CODn0IvXlmRbNvlC6vlIrHhNFTCh2OgiqfMhEsHw= 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: Modelled after lockdep_assert_held() and lockdep_assert_held_write(), but are always active, even when lockdep is disabled. Of course, they don't test that _this_ thread is the owner, but it's sufficient to catch many bugs and doesn't incur the same performance penalty as lockdep. Signed-off-by: Matthew Wilcox (Oracle) --- include/linux/rwbase_rt.h | 9 +++++++-- include/linux/rwsem.h | 42 ++++++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/include/linux/rwbase_rt.h b/include/linux/rwbase_rt.h index 1d264dd08625..a04acd85705b 100644 --- a/include/linux/rwbase_rt.h +++ b/include/linux/rwbase_rt.h @@ -26,12 +26,17 @@ struct rwbase_rt { } while (0) -static __always_inline bool rw_base_is_locked(struct rwbase_rt *rwb) +static __always_inline bool rw_base_is_locked(const struct rwbase_rt *rwb) { return atomic_read(&rwb->readers) != READER_BIAS; } -static __always_inline bool rw_base_is_contended(struct rwbase_rt *rwb) +static inline void rw_base_assert_held_write(const struct rwbase_rt *rwb) +{ + BUG_ON(atomic_read(&rwb->readers) != WRITER_BIAS); +} + +static __always_inline bool rw_base_is_contended(const struct rwbase_rt *rwb) { return atomic_read(&rwb->readers) > 0; } diff --git a/include/linux/rwsem.h b/include/linux/rwsem.h index 1dd530ce8b45..048149f781b3 100644 --- a/include/linux/rwsem.h +++ b/include/linux/rwsem.h @@ -66,14 +66,24 @@ struct rw_semaphore { #endif }; -/* In all implementations count != 0 means locked */ +#define RWSEM_UNLOCKED_VALUE 0UL +#define RWSEM_WRITER_LOCKED (1UL << 0) +#define __RWSEM_COUNT_INIT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE) + static inline int rwsem_is_locked(struct rw_semaphore *sem) { - return atomic_long_read(&sem->count) != 0; + return atomic_long_read(&sem->count) != RWSEM_UNLOCKED_VALUE; } -#define RWSEM_UNLOCKED_VALUE 0L -#define __RWSEM_COUNT_INIT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE) +static inline void __rwsem_assert_held(const struct rw_semaphore *sem) +{ + WARN_ON(atomic_long_read(&sem->count) == RWSEM_UNLOCKED_VALUE); +} + +static inline void __rwsem_assert_held_write(const struct rw_semaphore *sem) +{ + WARN_ON(!(atomic_long_read(&sem->count) & RWSEM_WRITER_LOCKED)); +} /* Common initializer macros and functions */ @@ -152,11 +162,21 @@ do { \ __init_rwsem((sem), #sem, &__key); \ } while (0) -static __always_inline int rwsem_is_locked(struct rw_semaphore *sem) +static __always_inline int rwsem_is_locked(const struct rw_semaphore *sem) { return rw_base_is_locked(&sem->rwbase); } +static inline void __rwsem_assert_held(const struct rw_semaphore *sem) +{ + BUG_ON(!rwsem_is_locked(sem)); +} + +static inline void __rwsem_assert_held_write(const struct rw_semaphore *sem) +{ + rw_base_assert_held_write(sem); +} + static __always_inline int rwsem_is_contended(struct rw_semaphore *sem) { return rw_base_is_contended(&sem->rwbase); @@ -169,6 +189,18 @@ static __always_inline int rwsem_is_contended(struct rw_semaphore *sem) * the RT specific variant. */ +static inline void rwsem_assert_held(const struct rw_semaphore *sem) +{ + lockdep_assert_held(sem); + __rwsem_assert_held(sem); +} + +static inline void rwsem_assert_held_write(const struct rw_semaphore *sem) +{ + lockdep_assert_held_write(sem); + __rwsem_assert_held_write(sem); +} + /* * lock for reading */