Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2905247rwb; Thu, 17 Nov 2022 18:40:48 -0800 (PST) X-Google-Smtp-Source: AA0mqf5ceXVrrAIT3PhhcpPQ8vlstz3/bGKCTRRueejTcV4O5vmFIFuUpVZeE5LUKQpxZcT+6A5V X-Received: by 2002:a17:906:a843:b0:79e:1059:6d65 with SMTP id dx3-20020a170906a84300b0079e10596d65mr4106067ejb.695.1668739248088; Thu, 17 Nov 2022 18:40:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668739248; cv=none; d=google.com; s=arc-20160816; b=CDARNURt+PjKlavij1tFEjXwqgx+kdQFl5kSEExdqqVa7dz12F04Dv3oUTLn+zBJf2 veKImp2cMKajWqlJS42j3Vw08Nun5phulHflFEEBH24aD9/fLDvbjSdDdBpvThKrdiig J5OZRJ2gaH3tg1MdYoEJ6twoRRyVxDQ7oE0Jt699QBPDlitVoWMgk2Skv6LJd+JIbBgZ oENmFXJ/7IWItPkPFi2OplOpNSISeOpyrPHhrC30G8kBWIKZeKXKDo/S++E/ywWRh2s8 aMTlsbOn2/7idnya0Rw/70ASR94UTIRZ9OI5i8Z9s1xPGHeS1eNc0o4MtHF/P1b06TVH g35Q== 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=jqegOKwKp0b5k01538L+8elHQKTxwvJermmvNJSeQ8I=; b=RFCWKX59hKB6S2a0r9wythbCYUltK6GcbsakLladijjWrdUsfymy+SWCliwnndXd3+ iaagX+GL1NumAALEJAysQQQ1mqa1skxbcXHXtUGXuZtSBiD+xleaJFmpZ5NAm5BaCQGb /Jxyci3TwzmcepeMBkCJjf105RxqOEMiu51qc6zZPIsIpuOkCLe4CHbJQx0PyMNieVGb EdNV5hcqhBDoj92bHtNiZr6sZZ605A5bDbY7KbDUtvHdYQa5CKsHF/t2NFHpIB21CNZl PmKim6n4dt8bQ6tP2RdBOGBc6xxVn2RiBWQsvH8WZ8MiW55lieB5wjMoEVBHmuzHt+DD 880w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ctDmf8Ci; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h22-20020a056402281600b00461c3758704si2674095ede.36.2022.11.17.18.40.25; Thu, 17 Nov 2022 18:40:48 -0800 (PST) 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=ctDmf8Ci; 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 S240701AbiKRCWc (ORCPT + 91 others); Thu, 17 Nov 2022 21:22:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240686AbiKRCWR (ORCPT ); Thu, 17 Nov 2022 21:22:17 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25EC17A345 for ; Thu, 17 Nov 2022 18:20:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668738030; 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=jqegOKwKp0b5k01538L+8elHQKTxwvJermmvNJSeQ8I=; b=ctDmf8Ci3zeu1GekZ9LWgomXnVpu9MhMJLOTGHjTs9FcNQfpYCoGeejEo1pQnL/CWGEPcL uz0ETaJmcbiEtdvRp1IdrU2zh90MNygG/NLGJpIZ9EsAAutl2IlRy8Cn2o4zVU07ic0YhS 68hOiuyhb02DxDNos9Uwv75DclZyY3g= 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-596-Zc78k2iTOTONzrFTiSYv2Q-1; Thu, 17 Nov 2022 21:20:24 -0500 X-MC-Unique: Zc78k2iTOTONzrFTiSYv2Q-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A121629DD9A3; Fri, 18 Nov 2022 02:20:23 +0000 (UTC) Received: from llong.com (unknown [10.22.16.250]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3C55149BB60; Fri, 18 Nov 2022 02:20:23 +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?q?Ting11=20Wang=20=E7=8E=8B=E5=A9=B7?= , Waiman Long , stable@vger.kernel.org Subject: [PATCH v6 1/6] locking/rwsem: Prevent non-first waiter from spinning in down_write() slowpath Date: Thu, 17 Nov 2022 21:20:11 -0500 Message-Id: <20221118022016.462070-2-longman@redhat.com> In-Reply-To: <20221118022016.462070-1-longman@redhat.com> References: <20221118022016.462070-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 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_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 A non-first waiter can potentially spin in the for loop of rwsem_down_write_slowpath() without sleeping but fail to acquire the lock even if the rwsem is free if the following sequence happens: Non-first RT waiter First waiter Lock holder ------------------- ------------ ----------- Acquire wait_lock rwsem_try_write_lock(): Set handoff bit if RT or wait too long Set waiter->handoff_set Release wait_lock Acquire wait_lock Inherit waiter->handoff_set Release wait_lock Clear owner Release lock if (waiter.handoff_set) { rwsem_spin_on_owner((); if (OWNER_NULL) goto trylock_again; } trylock_again: Acquire wait_lock rwsem_try_write_lock(): if (first->handoff_set && (waiter != first)) return false; Release wait_lock A non-first waiter cannot really acquire the rwsem even if it mistakenly believes that it can spin on OWNER_NULL value. If that waiter happens to be an RT task running on the same CPU as the first waiter, it can block the first waiter from acquiring the rwsem leading to live lock. Fix this problem by making sure that a non-first waiter cannot spin in the slowpath loop without sleeping. Fixes: d257cc8cb8d5 ("locking/rwsem: Make handoff bit handling more consistent") Reviewed-and-tested-by: Mukesh Ojha Signed-off-by: Waiman Long Cc: stable@vger.kernel.org --- kernel/locking/rwsem.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/kernel/locking/rwsem.c b/kernel/locking/rwsem.c index 44873594de03..be2df9ea7c30 100644 --- a/kernel/locking/rwsem.c +++ b/kernel/locking/rwsem.c @@ -624,18 +624,16 @@ static inline bool rwsem_try_write_lock(struct rw_semaphore *sem, */ if (first->handoff_set && (waiter != first)) return false; - - /* - * First waiter can inherit a previously set handoff - * bit and spin on rwsem if lock acquisition fails. - */ - if (waiter == first) - waiter->handoff_set = true; } new = count; if (count & RWSEM_LOCK_MASK) { + /* + * A waiter (first or not) can set the handoff bit + * if it is an RT task or wait in the wait queue + * for too long. + */ if (has_handoff || (!rt_task(waiter->task) && !time_after(jiffies, waiter->timeout))) return false; @@ -651,11 +649,12 @@ static inline bool rwsem_try_write_lock(struct rw_semaphore *sem, } while (!atomic_long_try_cmpxchg_acquire(&sem->count, &count, new)); /* - * We have either acquired the lock with handoff bit cleared or - * set the handoff bit. + * We have either acquired the lock with handoff bit cleared or set + * the handoff bit. Only the first waiter can have its handoff_set + * set here to enable optimistic spinning in slowpath loop. */ if (new & RWSEM_FLAG_HANDOFF) { - waiter->handoff_set = true; + first->handoff_set = true; lockevent_inc(rwsem_wlock_handoff); return false; } -- 2.31.1