From patchwork Thu Nov 3 18:29:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 15096 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp698830wru; Thu, 3 Nov 2022 11:32:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7xAAbiP5uOWuoqesLFfk4KYdq+QJ5u62PBywJXaAx1lMBalKDbX8kn/kCRfePxB26rNaTH X-Received: by 2002:a63:5a08:0:b0:43c:9fcc:cc54 with SMTP id o8-20020a635a08000000b0043c9fcccc54mr27089197pgb.229.1667500370805; Thu, 03 Nov 2022 11:32:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667500370; cv=none; d=google.com; s=arc-20160816; b=k5bxeY4pHZzxArdqeRrQcHnSr7czRRiUA78m78UfahYD8xXR9lsmNCGkSn3iF3eFVt oGbOq5oZ1zqVS8r+KStEvw2ylfCu0qVrg1sD6qTs9gwAekHNqC1z2bMwTT0MqGpi86kq MUoLoI5kih6r+gsn5PkrsQko1tNCc7NQEonTHcBhGj2+insItnSdNljdiLKEBDInst8q WDKGJ/TkoWx+yblM+EI6At1an+wtVLcQhXFiQtcmzEsp9LRLNO7fpqyABkFUqUTey3ZI 22vBbbWqNfoWVp/+YmYEkdhDZXEBOnsKlW40CwtW/c2EQ++q/6w3Scu1vabUwCU1sT2F Q3nQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=rq8XGM/za3SECEC5EhKMn++1K/i+OiHH929IAZiyu1Q=; b=wsg0rFNbWrmTEktWQLjd/yKTqo0QmV/ZnYXwRaRUZGKDrzohPNqHY5KFFKFlcnxnYv PbMcYJPdxEUfnR84jH7Ax+U1Pkgoi6W8nIqZd+d3zUsu/JU5Qxf0KXOoIm7Kpz2gxOtk txKHyYUcC7EM1WVGfqOUk3i/w1Qnvtq7plaQ8CxATiH6kF4dzrR2DOm43Tqc92l7uQl1 JbnTa7QO8g0LCyo8ztIl2GCV+vUXu6rwXBz+rjOuNkqxSUc3SRxkPa37ujgZ2dt6uiF7 XTnz1fRK9NT7DUT/qxGeYlfdCve+s57Xh8n4BO9dU8sfmUfyBDZoj7bLeUzOX2dsOKqR u6fQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=P2qGaU9A; 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=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u11-20020a65670b000000b0045f83f1eb56si2034905pgf.234.2022.11.03.11.32.37; Thu, 03 Nov 2022 11:32:50 -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=@redhat.com header.s=mimecast20190719 header.b=P2qGaU9A; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231831AbiKCSbq (ORCPT + 99 others); Thu, 3 Nov 2022 14:31:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229672AbiKCSbk (ORCPT ); Thu, 3 Nov 2022 14:31:40 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF55095A3 for ; Thu, 3 Nov 2022 11:29:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667500191; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rq8XGM/za3SECEC5EhKMn++1K/i+OiHH929IAZiyu1Q=; b=P2qGaU9A9nr5mUiXQj3b2UWo5i9omcAOwW+i1ZQ/eAuuMA5ccH4CaDIZPLYtPDtj2jShWa jOzyg5+StEBzJ46F80benlzFnbqCm/BourbVa3j1w0IqoMlSilpg+mQRTL91iuWUzZaGm5 cktCOPSsqhC6oAYcH5COzeWCB1+udmc= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-615-pGWcr2k4OL-41WqZfZJuLQ-1; Thu, 03 Nov 2022 14:29:46 -0400 X-MC-Unique: pGWcr2k4OL-41WqZfZJuLQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 392C43C11EA4; Thu, 3 Nov 2022 18:29:45 +0000 (UTC) Received: from llong.com (unknown [10.22.33.38]) by smtp.corp.redhat.com (Postfix) with ESMTP id D13EE1121325; Thu, 3 Nov 2022 18:29:44 +0000 (UTC) From: Waiman Long To: Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: linux-kernel@vger.kernel.org, john.p.donnelly@oracle.com, Hillf Danton , Mukesh Ojha , =?utf-8?b?VGluZzExIFdhbmcg546L5am3?= , Waiman Long Subject: [PATCH v5 4/6] locking/rwsem: Change waiter->hanodff_set to a handoff_state enum Date: Thu, 3 Nov 2022 14:29:34 -0400 Message-Id: <20221103182936.217120-5-longman@redhat.com> In-Reply-To: <20221103182936.217120-1-longman@redhat.com> References: <20221103182936.217120-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748500868700936193?= X-GMAIL-MSGID: =?utf-8?q?1748500868700936193?= Change the boolean waiter->handoff_set to an enum type so that we can have more states in some later patches. Also use READ_ONCE() outside wait_lock critical sections for read and WRITE_ONCE() inside wait_lock critical sections for write for proper synchronization. There is no functional change. Signed-off-by: Waiman Long --- kernel/locking/rwsem.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c index 2953fa4dd790..d80f22f7ecb6 100644 --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -335,12 +335,17 @@ enum rwsem_waiter_type { RWSEM_WAITING_FOR_READ }; +enum rwsem_handoff_state { + HANDOFF_NONE = 0, + HANDOFF_REQUESTED, +}; + struct rwsem_waiter { struct list_head list; struct task_struct *task; enum rwsem_waiter_type type; + enum rwsem_handoff_state handoff_state; unsigned long timeout; - bool handoff_set; }; #define rwsem_first_waiter(sem) \ list_first_entry(&sem->wait_list, struct rwsem_waiter, list) @@ -467,7 +472,7 @@ static void rwsem_mark_wake(struct rw_semaphore *sem, adjustment -= RWSEM_FLAG_HANDOFF; lockevent_inc(rwsem_rlock_handoff); } - waiter->handoff_set = true; + WRITE_ONCE(waiter->handoff_state, HANDOFF_REQUESTED); } atomic_long_add(-adjustment, &sem->count); @@ -619,7 +624,7 @@ static inline bool rwsem_try_write_lock(struct rw_semaphore *sem, * waiter is the one that set it. Otherwisee, we * still try to acquire the rwsem. */ - if (first->handoff_set && (waiter != first)) + if (first->handoff_state && (waiter != first)) return false; } @@ -647,11 +652,11 @@ static inline bool rwsem_try_write_lock(struct rw_semaphore *sem, /* * We have either acquired the lock with handoff bit cleared or set - * the handoff bit. Only the first waiter can have its handoff_set + * the handoff bit. Only the first waiter can have its handoff_state * set here to enable optimistic spinning in slowpath loop. */ if (new & RWSEM_FLAG_HANDOFF) { - first->handoff_set = true; + WRITE_ONCE(first->handoff_state, HANDOFF_REQUESTED); lockevent_inc(rwsem_wlock_handoff); return false; } @@ -1035,7 +1040,7 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, long count, unsigned int stat waiter.task = current; waiter.type = RWSEM_WAITING_FOR_READ; waiter.timeout = jiffies + RWSEM_WAIT_TIMEOUT; - waiter.handoff_set = false; + waiter.handoff_state = HANDOFF_NONE; raw_spin_lock_irq(&sem->wait_lock); if (list_empty(&sem->wait_list)) { @@ -1122,7 +1127,7 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state) waiter.task = current; waiter.type = RWSEM_WAITING_FOR_WRITE; waiter.timeout = jiffies + RWSEM_WAIT_TIMEOUT; - waiter.handoff_set = false; + waiter.handoff_state = HANDOFF_NONE; raw_spin_lock_irq(&sem->wait_lock); rwsem_add_waiter(sem, &waiter); @@ -1167,7 +1172,7 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state) * In this case, we attempt to acquire the lock again * without sleeping. */ - if (waiter.handoff_set) { + if (READ_ONCE(waiter.handoff_state)) { enum owner_state owner_state; owner_state = rwsem_spin_on_owner(sem);