Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp2082872rdd; Thu, 11 Jan 2024 20:52:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IHELKRjU4Zcx98QlzB7U3tZnJdWpI3P1h/4rUlGHgAr/LpED3AlPy/gR69DCO8y7AmIZ3mI X-Received: by 2002:a05:6e02:ee9:b0:35f:8205:7b06 with SMTP id j9-20020a056e020ee900b0035f82057b06mr440115ilk.4.1705035152206; Thu, 11 Jan 2024 20:52:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705035152; cv=none; d=google.com; s=arc-20160816; b=RWe/FCbp8/lzR0DGBIYcD1w0aV/cwcPAXMT/e/tvwbkaxofklaRCddRxMWxRyx6aAL Vo/i+3ONGb6vDcYLw0uZZM1EFqYMrEEDYRCCb2ZCzY2ib3PsG4zzp33jxB4j3Sxsl/a4 6kNIHe1s46cjvMd0MxzpHUcYI+KjCfm6HH1a57JMEPRdHruVnFlQVRvOqqAto5k6nw/g AvAfuzXtknRd/zVqiN5Aybh3tnXg329yassYqHIN56GYoE2mdAGkC1Ol10EqKjBe5CD6 Yr6G26bfCa8zVsR7Br9azt6ABwrWIwyDHfVN+wQitgQpgTz3XLKjXK8w9SNEhVApDmIv KdEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=PQJ9hOz9qN8Mqz2MuXKMKk0sGzrFbCj0fCm3cv5Fn6A=; fh=p+Aefk6Luv9ZaQIZk0inK+8n4VAOrWiAS5J+msjBzfs=; b=u7Pcf0T0SueYhpp6XDCulu1ZbK6y0ELOuxQQ59OiLMjK/Fg8d5bit/rr7PodrRUKzF jfKkx29Lsn+24v2nYm7vPTSGp2/3wSnpYgGOjcm72ENgVgelhBQKHJLV/IL5cF87olp+ cVfamk/c9U1UOGdKfTFZ3nDJVF5LEdulbU0eDxrOxCymrZHoUMjsoyqOVTZW9rsqjlmz M9l7sdMIQ4ru8iI9OV1UMHEFTH6SEczve0zrScPmfMALJI5AVHDoyhvTVNw59HQIj9aQ mr2ZYblQvkyWmrAnWW8PDZynKlXDtKVRKWkjLR5ytgZ0oP6hrRDywFRMEaW7jd2JWt4/ 0x4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fkl8goRk; spf=pass (google.com: domain of linux-kernel+bounces-24293-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-24293-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id x38-20020a634a26000000b005c200b11630si2514487pga.45.2024.01.11.20.52.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 20:52:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-24293-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fkl8goRk; spf=pass (google.com: domain of linux-kernel+bounces-24293-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-24293-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 8DB61B2476D for ; Fri, 12 Jan 2024 04:52:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A21CD5B5B8; Fri, 12 Jan 2024 04:52:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="fkl8goRk" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F39B05B5AA for ; Fri, 12 Jan 2024 04:52:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705035135; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PQJ9hOz9qN8Mqz2MuXKMKk0sGzrFbCj0fCm3cv5Fn6A=; b=fkl8goRkPhDKzhX7IAkmTslwPXHXhEZJn89cyoiO4I/2saJfpDUsFWjqGHur4arLS9bkE/ obmlpvKAIYdWOqb+Y6snBWE2u7SGpCPb+9ZcPxm4odK/4RppBeKQgJiCfxkLCRhl2w4FIx rUkcixYtQRO07fZGSvcWkvJAYwSILL0= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-67-Pzar8MfmPUiRq9c0eNUBEw-1; Thu, 11 Jan 2024 23:52:12 -0500 X-MC-Unique: Pzar8MfmPUiRq9c0eNUBEw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8C855282FA19; Fri, 12 Jan 2024 04:52:11 +0000 (UTC) Received: from [10.22.16.77] (unknown [10.22.16.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id 10CB92166B33; Fri, 12 Jan 2024 04:52:11 +0000 (UTC) Message-ID: Date: Thu, 11 Jan 2024 23:52:10 -0500 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 1/3] locking: Add rwsem_assert_held() and rwsem_assert_held_write() Content-Language: en-US To: "Matthew Wilcox (Oracle)" , Chandan Babu R Cc: linux-kernel@vger.kernel.org, linux-xfs@vger.kernel.org, "Darrick J . Wong" , Mateusz Guzik , Peter Zijlstra , Ingo Molnar , Will Deacon References: <20240111212424.3572189-1-willy@infradead.org> <20240111212424.3572189-2-willy@infradead.org> From: Waiman Long In-Reply-To: <20240111212424.3572189-2-willy@infradead.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 On 1/11/24 16:24, Matthew Wilcox (Oracle) wrote: > 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. I don't mind the new *assert_held*nolockdep APIs. The only nit that I have is that their behavior is slightly different from the corresponding lockdep counterparts as they don't imply the current process is holding the lock. So we may need to have some comment to document the difference and set the right expectation. Of course it can be done with a follow-up patch. Acked-by: Waiman Long > > Signed-off-by: Matthew Wilcox (Oracle) > Acked-by: Peter Zijlstra (Intel) > --- > include/linux/rwbase_rt.h | 9 ++++++-- > include/linux/rwsem.h | 46 ++++++++++++++++++++++++++++++++++----- > 2 files changed, 48 insertions(+), 7 deletions(-) > > diff --git a/include/linux/rwbase_rt.h b/include/linux/rwbase_rt.h > index 1d264dd08625..29c4e4f243e4 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) > +{ > + WARN_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 9c29689ff505..4f1c18992f76 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_nolockdep(const struct rw_semaphore *sem) > +{ > + WARN_ON(atomic_long_read(&sem->count) == RWSEM_UNLOCKED_VALUE); > +} > + > +static inline void rwsem_assert_held_write_nolockdep(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_nolockdep(const struct rw_semaphore *sem) > +{ > + WARN_ON(!rwsem_is_locked(sem)); > +} > + > +static inline void rwsem_assert_held_write_nolockdep(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,22 @@ 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) > +{ > + if (IS_ENABLED(CONFIG_LOCKDEP)) > + lockdep_assert_held(sem); > + else > + rwsem_assert_held_nolockdep(sem); > +} > + > +static inline void rwsem_assert_held_write(const struct rw_semaphore *sem) > +{ > + if (IS_ENABLED(CONFIG_LOCKDEP)) > + lockdep_assert_held_write(sem); > + else > + rwsem_assert_held_write_nolockdep(sem); > +} > + > /* > * lock for reading > */