From mboxrd@z Thu Jan 1 00:00:00 1970 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 7403BEC875E for ; Thu, 7 Sep 2023 23:47:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 556CD8D0001; Thu, 7 Sep 2023 19:47:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5068C6B007B; Thu, 7 Sep 2023 19:47:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3CF208D0001; Thu, 7 Sep 2023 19:47:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2A3856B0078 for ; Thu, 7 Sep 2023 19:47:39 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id EC4F716035F for ; Thu, 7 Sep 2023 23:47:38 +0000 (UTC) X-FDA: 81211440996.04.62BA6C7 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf23.hostedemail.com (Postfix) with ESMTP id 14F7B140017 for ; Thu, 7 Sep 2023 23:47:36 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Wg7fl3Ux; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf23.hostedemail.com: domain of longman@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=longman@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694130457; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=q0DSZUVajRNBw/S+LqH8eIJWqxcuhAbEAg5nSMOQP+U=; b=79p7FbD3I5UZV9+vZ7RwJihsRpppxJGKswkFq9FQoTSJSV0QqQjBbleYVNj7SodtF6AFFI Z/jJK0W2W3VljNXuqffii8ikR8UUQDB/c+WFleGRf3qetXqSWrpUDlu4VbjifiwLHnm8y1 e/Gt+PSUVxResv3egvCUd0ksI273N/I= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Wg7fl3Ux; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf23.hostedemail.com: domain of longman@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=longman@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694130457; a=rsa-sha256; cv=none; b=5eke1xitODujOgX7bgfuQSJz5rAIg3O/D13TCDzKfDnJ30gfdzJfGQGTN07B90inr15PWU zQvjNJ4LS2so2gX1hRWkJMYbJxgN0D/1oCz1CtZsxb6mDEp8eJ/22DY0/vICQDujJoc/Ba tf1mmO9B1z4O184AtnJkBctPEqPdQ+E= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694130456; 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=q0DSZUVajRNBw/S+LqH8eIJWqxcuhAbEAg5nSMOQP+U=; b=Wg7fl3Ux8Vpr8BN47QCtULtFDRmhk5U+1IKs4PScxkooEBMjnDBSID/qhd9As1unNKSsdV kRvfwTNDFqtcSQvI5JuawRpbKBxWlzUECQhDrM5RvanBJUx4rh6jrVxwUFboFtmLV2+VCg meFTaOPbcLIWfkmO2zlsgRxgI3XHNEk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-358-VCCe6YDsPgyTRbBjYsc6Dw-1; Thu, 07 Sep 2023 19:47:32 -0400 X-MC-Unique: VCCe6YDsPgyTRbBjYsc6Dw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6BB32181B542; Thu, 7 Sep 2023 23:47:32 +0000 (UTC) Received: from [10.22.16.164] (unknown [10.22.16.164]) by smtp.corp.redhat.com (Postfix) with ESMTP id 056F82026D76; Thu, 7 Sep 2023 23:47:31 +0000 (UTC) Message-ID: Date: Thu, 7 Sep 2023 19:47:31 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 Subject: Re: [PATCH 1/5] locking: Add rwsem_is_write_locked() Content-Language: en-US From: Waiman Long To: Matthew Wilcox Cc: Peter Zijlstra , Ingo Molnar , Will Deacon , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Chandan Babu R , "Darrick J . Wong" , linux-xfs@vger.kernel.org References: <20230907174705.2976191-1-willy@infradead.org> <20230907174705.2976191-2-willy@infradead.org> <2cd975ec-f868-f180-350f-b1b704118777@redhat.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Rspamd-Queue-Id: 14F7B140017 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: k8zire448s5g7pk98u8ayitzom3z6zeu X-HE-Tag: 1694130456-926330 X-HE-Meta: U2FsdGVkX1/aXFgg++ccfMg7JkMYJikUjZlF+KMUZJylh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N/1gKZgduWuzYvzYJyehUyDmr/QcNJMhxmNdfu3BtZULYZ7WUHy5AnYLtWBKaQYtNw== 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: On 9/7/23 17:06, Waiman Long wrote: > > On 9/7/23 15:33, Matthew Wilcox wrote: >> On Thu, Sep 07, 2023 at 02:05:54PM -0400, Waiman Long wrote: >>> On 9/7/23 13:47, Matthew Wilcox (Oracle) wrote: >>>> +static inline int rwsem_is_write_locked(struct rw_semaphore *sem) >>>> +{ >>>> +    return atomic_long_read(&sem->count) & 1 /* >>>> RWSEM_WRITER_LOCKED */; >>>> +} >>> I would prefer you move the various RWSEM_* count bit macros from >>> kernel/locking/rwsem.c to under the !PREEMPT_RT block and directly use >>> RWSEM_WRITER_LOCKED instead of hardcoding a value of 1. >> Just to be clear, you want the ~50 lines from: >> >> /* >>   * On 64-bit architectures, the bit definitions of the count are: >> ... >> #define RWSEM_READ_FAILED_MASK (RWSEM_WRITER_MASK|RWSEM_FLAG_WAITERS|\ >> RWSEM_FLAG_HANDOFF|RWSEM_FLAG_READFAIL) >> >> moved from rwsem.c to rwsem.h? >> >> Or just these four lines: >> >> #define RWSEM_WRITER_LOCKED     (1UL << 0) >> #define RWSEM_FLAG_WAITERS      (1UL << 1) >> #define RWSEM_FLAG_HANDOFF      (1UL << 2) >> #define RWSEM_FLAG_READFAIL     (1UL << (BITS_PER_LONG - 1)) > > I think just the first 3 lines will be enough. Maybe a bit of comment > about these bit flags in the count atomic_long value. Actually, the old rwsem implementation won't allow you to reliably determine if a rwsem is write locked because the xadd instruction is used for write locking and the code had to back out the WRITER_BIAS if the attempt failed. Maybe that is why XFS has its own code to check if a rwsem is write locked which is needed with the old rwsem implementation. The new implementation makes this check reliable. Still it is not easy to check if a rwsem is read locked as the check will be rather complicated and probably racy. Cheers, Longman