Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp2703332rdb; Wed, 6 Sep 2023 12:11:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHnr8LbKF34WQ8Pg3hyTb0r6w5+Q2x+tga2v5gAu0bZvB/gUBUcgTKzeB6aUBXK45PdAhCU X-Received: by 2002:a05:6a21:61e:b0:14c:76bd:10c1 with SMTP id ll30-20020a056a21061e00b0014c76bd10c1mr14048042pzb.21.1694027477761; Wed, 06 Sep 2023 12:11:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694027477; cv=none; d=google.com; s=arc-20160816; b=eu8WDKtVyZPG5K/AzzW6QPk7T6QS4SHOdOD4bL5mi/liOJKi4hFbt2SpQr6UTGp2uN oowOy0Wds8fnrdVyrOqm0EAh1/4ianmvXCkxP7gbYE9jc8nMxKeitcxTQCjAa/ZXn+Ik g0n1BnGwzHe3ocnO0AJ+HH/MX/AxP4B7OhU883iqHTsvrapyFVHX54HnRjmVQORRdhCw ABQH5q+vRahddZRhiI+KcGsmXOaEqn/RVhMIn001xdeyqZgMyZWhGo66htANIwMaBdgj HklmxL+1zi9iC/82FBulHrLXLxRu8HvKK1OHjkgN0mxHCe87cxGNyzEXiSXPE+SN2+Rd VYIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=rtNJgHEnPEAmoof0+AhtIdj8WtRN5Xl3EHMDg596DEk=; fh=YWwSkhIxKypvZgDFlOISrCsQpQaronisRiUUSKBnncA=; b=LBblpceKwgzNNPPqhwjgLLPuxCZ0lWqN5qlrYGteLjD1FbrUIu9eeKrPEWuJ4gvtdv mb0Uva+bifF4Vl9M43oyZRuMeNEbp1hFfzfpf0F3Hs3QL6gjeYCPMVt7v3RDArXg08pE MoD3xz8Rz27oAIdtbV7EygI4l6gXmNNtgTvvhoqQ1H9Xwy3tUlTF4IL+iwXEDQucimFk /iYdea68fUeKCwkm48JIubI+BFmXJrOWVYYNGuEQHnQLZnn5xcew2ZuRjaTlBUOap07l UH8XDeZVq6Gd6pEpG8wZ0w9MMSwgbqzzEtAyeDEPOlk5XyRSV7BqT1JZiroKP+BSw/kL TXrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=enVW8UqE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f2-20020a631002000000b005667bb2eb0dsi11613013pgl.276.2023.09.06.12.11.00; Wed, 06 Sep 2023 12:11:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=enVW8UqE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242505AbjIFRH3 (ORCPT + 99 others); Wed, 6 Sep 2023 13:07:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233048AbjIFRH3 (ORCPT ); Wed, 6 Sep 2023 13:07:29 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A52F313E; Wed, 6 Sep 2023 10:07:25 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2F7ECC433C8; Wed, 6 Sep 2023 17:07:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1694020045; bh=ZxQkFTwDYKxzh9YCfck5Vjc6iuQ1cgWZBvGEsAL/bDg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=enVW8UqEXJToj43GdFwEXyHRhQrkgMZq4vCj7yKYyXixdh80TgZyFIrxHLjkL5IGb VpKQnNom1y4JM1Zn/h/22TRB0m3coCwW8QLb9HcZv7pMaQ7OVqytWjem/0yNw2Jw5l MQ+PdvchQMadY9phxCJ8bZ+VXJlk1WV4jhzHZD2ysCZ6cUXkWKSr8ZFFyrUmoHWPS4 yoPoqlOZcpmf4FiFESMncVMZnK7AO0p9bKXFB46fFXaEX+/ZUn0hADM5ckrPK12Us6 mghukN8lN+X0fuDabbryarXoSA3bNAHGFieuwuB62GPrPd+nDxUF3nWi0jTnc1vZbA WYOrQqdxmcXnA== Date: Wed, 6 Sep 2023 10:07:24 -0700 From: "Darrick J. Wong" To: Matthew Wilcox Cc: Bernd Schubert , Mateusz Guzik , brauner@kernel.org, viro@zeniv.linux.org.uk, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: Re: [RFC PATCH] vfs: add inode lockdep assertions Message-ID: <20230906170724.GI28202@frogsfrogsfrogs> References: <20230831151414.2714750-1-mjguzik@gmail.com> <20230906152948.GE28160@frogsfrogsfrogs> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 06, 2023 at 05:00:14PM +0100, Matthew Wilcox wrote: > On Wed, Sep 06, 2023 at 08:29:48AM -0700, Darrick J. Wong wrote: > > Or hoist the XFS mrlock, because it actually /does/ know if the rwsem is > > held in shared or exclusive mode. > > ... or to put it another way, if we had rwsem_is_write_locked(), > we could get rid of mrlock? > > diff --git a/fs/xfs/mrlock.h b/fs/xfs/mrlock.h > index 79155eec341b..5530f03aaed1 100644 > --- a/fs/xfs/mrlock.h > +++ b/fs/xfs/mrlock.h > @@ -10,18 +10,10 @@ > > typedef struct { > struct rw_semaphore mr_lock; > -#if defined(DEBUG) || defined(XFS_WARN) > - int mr_writer; > -#endif > } mrlock_t; > > -#if defined(DEBUG) || defined(XFS_WARN) > -#define mrinit(mrp, name) \ > - do { (mrp)->mr_writer = 0; init_rwsem(&(mrp)->mr_lock); } while (0) > -#else > #define mrinit(mrp, name) \ > do { init_rwsem(&(mrp)->mr_lock); } while (0) > -#endif > > #define mrlock_init(mrp, t,n,s) mrinit(mrp, n) > #define mrfree(mrp) do { } while (0) > @@ -34,9 +26,6 @@ static inline void mraccess_nested(mrlock_t *mrp, int subclass) > static inline void mrupdate_nested(mrlock_t *mrp, int subclass) > { > down_write_nested(&mrp->mr_lock, subclass); > -#if defined(DEBUG) || defined(XFS_WARN) > - mrp->mr_writer = 1; > -#endif > } > > static inline int mrtryaccess(mrlock_t *mrp) > @@ -48,17 +37,11 @@ static inline int mrtryupdate(mrlock_t *mrp) > { > if (!down_write_trylock(&mrp->mr_lock)) > return 0; > -#if defined(DEBUG) || defined(XFS_WARN) > - mrp->mr_writer = 1; > -#endif > return 1; > } > > static inline void mrunlock_excl(mrlock_t *mrp) > { > -#if defined(DEBUG) || defined(XFS_WARN) > - mrp->mr_writer = 0; > -#endif > up_write(&mrp->mr_lock); > } > > @@ -69,9 +52,6 @@ static inline void mrunlock_shared(mrlock_t *mrp) > > static inline void mrdemote(mrlock_t *mrp) > { > -#if defined(DEBUG) || defined(XFS_WARN) > - mrp->mr_writer = 0; > -#endif > downgrade_write(&mrp->mr_lock); > } > > diff --git a/fs/xfs/xfs_inode.c b/fs/xfs/xfs_inode.c > index 9e62cc500140..b99c3bd78c5e 100644 > --- a/fs/xfs/xfs_inode.c > +++ b/fs/xfs/xfs_inode.c > @@ -361,7 +361,7 @@ xfs_isilocked( > { > if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) { > if (!(lock_flags & XFS_ILOCK_SHARED)) > - return !!ip->i_lock.mr_writer; > + return rwsem_is_write_locked(&ip->i_lock.mr_lock); You'd be better off converting this to: return __xfs_rwsem_islocked(&ip->i_lock.mr_lock, (lock_flags & XFS_ILOCK_SHARED)); And then fixing __xfs_rwsem_islocked to do: static inline bool __xfs_rwsem_islocked( struct rw_semaphore *rwsem, bool shared) { if (!debug_locks) { if (!shared) return rwsem_is_write_locked(rwsem); return rwsem_is_locked(rwsem); } ... } (and then getting rid of mrlock_t.h entirely) > return rwsem_is_locked(&ip->i_lock.mr_lock); > } > > diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h > index e05e167dbd16..277b8c96bbf9 100644 > --- a/include/linux/mmap_lock.h > +++ b/include/linux/mmap_lock.h > @@ -69,7 +69,7 @@ static inline void mmap_assert_locked(struct mm_struct *mm) > static inline void mmap_assert_write_locked(struct mm_struct *mm) > { > lockdep_assert_held_write(&mm->mmap_lock); > - VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm); > + VM_BUG_ON_MM(!rwsem_is_write_locked(&mm->mmap_lock), mm); > } > > #ifdef CONFIG_PER_VMA_LOCK > 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..241a12c6019e 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; atomic_long_read(&sem->count) & RWSEM_WRITER_LOCKED ? In one of the past "replace mrlock_t" threads I complained about hardcoding this value instead of using the symbol. I saw it go by, neglected to copy the url, and ten minutes later I can't find it. :( --D > +} > + > #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);