From nobody Sat Apr 27 18:57:52 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1604477775; cv=none; d=zohomail.com; s=zohoarc; b=UMyavGOQN9H8Y27zUQaya/rNCRUj+bXzPLObu8cR5+e+5rvfhhEUSNw1pzz2YA9HNYon6dJaq7+tStK/pP6WLVorq9thznmOm0aH+iUR/oy+FVqZISVWe1q0GoV5UNlIoQlU0xq0Zdu/GVdbHwz4PZAohNHJkRxRAwb4B0qelR4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1604477775; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=SMWFDeqyO4Bqdr2XBTO3XuyCVTVJJjcu3BY2gEaI7+0=; b=QN1nSApEKBGVH9Kt9hnEqfzZ4WHHyoMjTuPZoCcLHCPmDD/5KofjhMq8lvAPzjyW7d6tM8FDnfCKmS25t7Jjqm9tnF2IdXK+EhQN9cKL/nzfzM7UGsnHj2o9rN8foES0RP+fTIYsZRt0Go5sWSoYxnlCDf8pv0RTrEmDOG1tgcI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1604477775353965.6247365274479; Wed, 4 Nov 2020 00:16:15 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.18837.43874 (Exim 4.92) (envelope-from ) id 1kaDxY-0000VD-9d; Wed, 04 Nov 2020 08:16:00 +0000 Received: by outflank-mailman (output) from mailman id 18837.43874; Wed, 04 Nov 2020 08:16:00 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kaDxY-0000V1-5H; Wed, 04 Nov 2020 08:16:00 +0000 Received: by outflank-mailman (input) for mailman id 18837; Wed, 04 Nov 2020 08:15:59 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kaDxW-0000Pk-Vc for xen-devel@lists.xenproject.org; Wed, 04 Nov 2020 08:15:59 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTP id b17a3e08-d071-4107-9a9a-62c7dbc0f131; Wed, 04 Nov 2020 08:15:51 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 7FB21AC23; Wed, 4 Nov 2020 08:15:51 +0000 (UTC) Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kaDxW-0000Pk-Vc for xen-devel@lists.xenproject.org; Wed, 04 Nov 2020 08:15:59 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTP id b17a3e08-d071-4107-9a9a-62c7dbc0f131; Wed, 04 Nov 2020 08:15:51 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 7FB21AC23; Wed, 4 Nov 2020 08:15:51 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: b17a3e08-d071-4107-9a9a-62c7dbc0f131 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1604477751; 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=SMWFDeqyO4Bqdr2XBTO3XuyCVTVJJjcu3BY2gEaI7+0=; b=dQMJ2oPyWtwflpsHfulQPgBtMzNX2MQIF21kPA26/35M0xKeNVqjfq6c1xWMK+m7FDKPmO zTkXnEpzbTzej/zjlWVmri5+f9+2ho75oKOVO5iLV2UpYlv0DIsdpbOhc4fbUE+C2eYeKV FCc72zmaABiCECj/knpDXrloiKhgKD0= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Andrew Cooper , George Dunlap , Ian Jackson , Jan Beulich , Julien Grall , Stefano Stabellini , Wei Liu Subject: [PATCH v3 1/3] xen/spinlocks: spin_trylock with interrupts off is always fine Date: Wed, 4 Nov 2020 09:15:47 +0100 Message-Id: <20201104081549.3712-2-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201104081549.3712-1-jgross@suse.com> References: <20201104081549.3712-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" Even if a spinlock was taken with interrupts on before calling spin_trylock() with interrupts off is fine, as it can't block. Add a bool parameter "try" to check_lock() for handling this case. Remove the call of check_lock() from _spin_is_locked(), as it really serves no purpose and it can even lead to false crashes, e.g. when a lock was taken correctly with interrupts enabled and the call of _spin_is_locked() happened with interrupts off. In case the lock is taken with wrong interrupt flags this will be catched when taking the lock. Signed-off-by: Juergen Gross Reviewed-by: Jan Beulich Acked-by: Julien Grall --- V2: - corrected comment (Jan Beulich) --- xen/common/spinlock.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/xen/common/spinlock.c b/xen/common/spinlock.c index ce3106e2d3..b4aaf6bce6 100644 --- a/xen/common/spinlock.c +++ b/xen/common/spinlock.c @@ -13,7 +13,7 @@ =20 static atomic_t spin_debug __read_mostly =3D ATOMIC_INIT(0); =20 -static void check_lock(union lock_debug *debug) +static void check_lock(union lock_debug *debug, bool try) { bool irq_safe =3D !local_irq_is_enabled(); =20 @@ -42,7 +42,13 @@ static void check_lock(union lock_debug *debug) *=20 * To guard against this subtle bug we latch the IRQ safety of every * spinlock in the system, on first use. + * + * A spin_trylock() with interrupts off is always fine, as this can't + * block and above deadlock scenario doesn't apply. */ + if ( try && irq_safe ) + return; + if ( unlikely(debug->irq_safe !=3D irq_safe) ) { union lock_debug seen, new =3D { 0 }; @@ -102,7 +108,7 @@ void spin_debug_disable(void) =20 #else /* CONFIG_DEBUG_LOCKS */ =20 -#define check_lock(l) ((void)0) +#define check_lock(l, t) ((void)0) #define check_barrier(l) ((void)0) #define got_lock(l) ((void)0) #define rel_lock(l) ((void)0) @@ -159,7 +165,7 @@ void inline _spin_lock_cb(spinlock_t *lock, void (*cb)(= void *), void *data) spinlock_tickets_t tickets =3D SPINLOCK_TICKET_INC; LOCK_PROFILE_VAR; =20 - check_lock(&lock->debug); + check_lock(&lock->debug, false); preempt_disable(); tickets.head_tail =3D arch_fetch_and_add(&lock->tickets.head_tail, tickets.head_tail); @@ -220,8 +226,6 @@ void _spin_unlock_irqrestore(spinlock_t *lock, unsigned= long flags) =20 int _spin_is_locked(spinlock_t *lock) { - check_lock(&lock->debug); - /* * Recursive locks may be locked by another CPU, yet we return * "false" here, making this function suitable only for use in @@ -236,7 +240,7 @@ int _spin_trylock(spinlock_t *lock) { spinlock_tickets_t old, new; =20 - check_lock(&lock->debug); + check_lock(&lock->debug, true); old =3D observe_lock(&lock->tickets); if ( old.head !=3D old.tail ) return 0; @@ -294,7 +298,7 @@ int _spin_trylock_recursive(spinlock_t *lock) BUILD_BUG_ON(NR_CPUS > SPINLOCK_NO_CPU); BUILD_BUG_ON(SPINLOCK_RECURSE_BITS < 3); =20 - check_lock(&lock->debug); + check_lock(&lock->debug, true); =20 if ( likely(lock->recurse_cpu !=3D cpu) ) { --=20 2.26.2 From nobody Sat Apr 27 18:57:52 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1604477779; cv=none; d=zohomail.com; s=zohoarc; b=c6iYp4eZKSkrLi7voXYAtVPjywaWhnOaW0VTzjlNci0Ae3RGojKMLZDMwYdS7PSmjWnu1vstJKLouZPpivXomjPYal4lTL9Xj7hcio9d6SQg9o4X58driFSw0yPyGM6co2ZNx0n4DqgfOgzibyvHTLsziG10F1MOy9ANbTkWE9A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1604477779; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=X4N92E0t7NY8cFsFrRFhreEx/aHGKkH4ttPqzLqhrP0=; b=hbkJ4rUKpZ1LjEeDyB8MoI4SGhqBJkPoZvotADae2q51AufSfo8T2UEYMCwJls3HmXLzn5vOBQWHetpvjk/HzD8O4lR+NvsIvhf1l1kJl2sq0e/ROYXHcxENqnzGdMDAa+Bx9BFjfVsGmnRu/l6has0GEly/FkyVeMHg2bW/mhY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1604477779217478.89799279007343; Wed, 4 Nov 2020 00:16:19 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.18838.43886 (Exim 4.92) (envelope-from ) id 1kaDxd-0000Zs-KM; Wed, 04 Nov 2020 08:16:05 +0000 Received: by outflank-mailman (output) from mailman id 18838.43886; Wed, 04 Nov 2020 08:16:05 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kaDxd-0000Zk-Gl; Wed, 04 Nov 2020 08:16:05 +0000 Received: by outflank-mailman (input) for mailman id 18838; Wed, 04 Nov 2020 08:16:04 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kaDxb-0000Pk-Vi for xen-devel@lists.xenproject.org; Wed, 04 Nov 2020 08:16:04 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTP id 9db5219b-9832-4dda-9a2e-46059e9e7c38; Wed, 04 Nov 2020 08:15:52 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id BA7DEACC6; Wed, 4 Nov 2020 08:15:51 +0000 (UTC) Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kaDxb-0000Pk-Vi for xen-devel@lists.xenproject.org; Wed, 04 Nov 2020 08:16:04 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTP id 9db5219b-9832-4dda-9a2e-46059e9e7c38; Wed, 04 Nov 2020 08:15:52 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id BA7DEACC6; Wed, 4 Nov 2020 08:15:51 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 9db5219b-9832-4dda-9a2e-46059e9e7c38 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1604477751; 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=X4N92E0t7NY8cFsFrRFhreEx/aHGKkH4ttPqzLqhrP0=; b=TA0qCEfg66+8vkDgjK6jJWLh9gwYmrr0METgNIVpEsr55+7OnQ0pkhihTDXnPbynRX4C13 MYySK5w1UrVlhhKfFpz3NLdoMCMDci8QlwUm1hvahwCzlpFT/yrj0xjnJMFgysPmoEvNHp lQ3TDSMNoyA+bl3Du3cjZ26C/fhxOWY= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Andrew Cooper , George Dunlap , Ian Jackson , Jan Beulich , Julien Grall , Stefano Stabellini , Wei Liu Subject: [PATCH v3 2/3] xen/locking: harmonize spinlocks and rwlocks regarding preemption Date: Wed, 4 Nov 2020 09:15:48 +0100 Message-Id: <20201104081549.3712-3-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201104081549.3712-1-jgross@suse.com> References: <20201104081549.3712-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" Spinlocks and rwlocks behave differently in the try variants regarding preemption: rwlocks are switching preemption off before testing the lock, while spinlocks do so only after the first check. Modify _spin_trylock() to disable preemption before testing the lock to be held in order to be preemption-ready. Signed-off-by: Juergen Gross Reviewed-by: Julien Grall --- V3: - new patch --- xen/common/spinlock.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xen/common/spinlock.c b/xen/common/spinlock.c index b4aaf6bce6..f4eb50f030 100644 --- a/xen/common/spinlock.c +++ b/xen/common/spinlock.c @@ -240,13 +240,16 @@ int _spin_trylock(spinlock_t *lock) { spinlock_tickets_t old, new; =20 + preempt_disable(); check_lock(&lock->debug, true); old =3D observe_lock(&lock->tickets); if ( old.head !=3D old.tail ) + { + preempt_enable(); return 0; + } new =3D old; new.tail++; - preempt_disable(); if ( cmpxchg(&lock->tickets.head_tail, old.head_tail, new.head_tail) !=3D old.head_tail ) { --=20 2.26.2 From nobody Sat Apr 27 18:57:52 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1604477771; cv=none; d=zohomail.com; s=zohoarc; b=dQPo6+EmfO/32XDgVwvyVKK5VsWD5bsvVmx04a5Z7gw2B7ENx262b1ylYTr+vt4KUqOpmN54IlycKw1DyA2FOz+RKvY6twoDBducc1bGUsUXrvUvFKFXfQpCZIXVqN4wvFw8cf7Ir80CB+ZBEhGwuZbmQDZ0+IULN4BofUX4u+8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1604477771; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=cUZq7FUinpan5b+B+0dHzJT3VyvkdYgZhHV/8DJXZyg=; b=D0Zt6bOIDk7BSYvG8Xyf0JwvcbUIi9fMhC+w57dfTzl6MitxA3jlA4EZTmZiSpEMHieEPdY6HSECLcr8DgI7KokS3mTCUnKTRUSJ7Xb9HBaqd/Mxs/Z6SVVn7pmMYPhmVnwU30/JXV2luob5gbKpUs0kPanwO2giZJtot+Nfxos= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1604477771776581.6241108750182; Wed, 4 Nov 2020 00:16:11 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.18835.43850 (Exim 4.92) (envelope-from ) id 1kaDxS-0000Pw-HF; Wed, 04 Nov 2020 08:15:54 +0000 Received: by outflank-mailman (output) from mailman id 18835.43850; Wed, 04 Nov 2020 08:15:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kaDxS-0000Pp-E7; Wed, 04 Nov 2020 08:15:54 +0000 Received: by outflank-mailman (input) for mailman id 18835; Wed, 04 Nov 2020 08:15:53 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kaDxR-0000Pf-Eu for xen-devel@lists.xenproject.org; Wed, 04 Nov 2020 08:15:53 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTP id db0330c7-59ee-4464-9368-ed68852fb91b; Wed, 04 Nov 2020 08:15:52 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id F1F17ACF1; Wed, 4 Nov 2020 08:15:51 +0000 (UTC) Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kaDxR-0000Pf-Eu for xen-devel@lists.xenproject.org; Wed, 04 Nov 2020 08:15:53 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTP id db0330c7-59ee-4464-9368-ed68852fb91b; Wed, 04 Nov 2020 08:15:52 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id F1F17ACF1; Wed, 4 Nov 2020 08:15:51 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: db0330c7-59ee-4464-9368-ed68852fb91b X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1604477752; 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=cUZq7FUinpan5b+B+0dHzJT3VyvkdYgZhHV/8DJXZyg=; b=blLJjIxBcUI0qmfuYyhXcjdGGlT5FjKf8X35uuEDD//NMLqxn0iA//AtqHCnHU7Wx+mR9i iEaP2cc8sb/dzVK0wgPyvh//ILZEY0V97pKU/5MB47bklfQlLk4XN1dv6cvuBBxxubbnKy PxkV2Ri4dXuBSogT/nzxTGuydug2Dm4= From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Andrew Cooper , George Dunlap , Ian Jackson , Jan Beulich , Julien Grall , Stefano Stabellini , Wei Liu Subject: [PATCH v3 3/3] xen/rwlock: add check_lock() handling to rwlocks Date: Wed, 4 Nov 2020 09:15:49 +0100 Message-Id: <20201104081549.3712-4-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201104081549.3712-1-jgross@suse.com> References: <20201104081549.3712-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" Checking whether a lock is consistently used regarding interrupts on or off is beneficial for rwlocks, too. So add check_lock() calls to rwlock functions. For this purpose make check_lock() globally accessible. Signed-off-by: Juergen Gross Reviewed-by: Jan Beulich Reviewed-by: Julien Grall --- V2: - call check_lock() unconditionally in try_lock variants (Jan Beulich) V3: - add comments (Jan Beulich) - place check_lock() calls inside preempt-off region (Jan Beulich) Signed-off-by: Juergen Gross --- xen/common/spinlock.c | 3 +-- xen/include/xen/rwlock.h | 15 +++++++++++++++ xen/include/xen/spinlock.h | 2 ++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/xen/common/spinlock.c b/xen/common/spinlock.c index f4eb50f030..b90981bb27 100644 --- a/xen/common/spinlock.c +++ b/xen/common/spinlock.c @@ -13,7 +13,7 @@ =20 static atomic_t spin_debug __read_mostly =3D ATOMIC_INIT(0); =20 -static void check_lock(union lock_debug *debug, bool try) +void check_lock(union lock_debug *debug, bool try) { bool irq_safe =3D !local_irq_is_enabled(); =20 @@ -108,7 +108,6 @@ void spin_debug_disable(void) =20 #else /* CONFIG_DEBUG_LOCKS */ =20 -#define check_lock(l, t) ((void)0) #define check_barrier(l) ((void)0) #define got_lock(l) ((void)0) #define rel_lock(l) ((void)0) diff --git a/xen/include/xen/rwlock.h b/xen/include/xen/rwlock.h index 427664037a..0cc9167715 100644 --- a/xen/include/xen/rwlock.h +++ b/xen/include/xen/rwlock.h @@ -56,6 +56,7 @@ static inline int _read_trylock(rwlock_t *lock) u32 cnts; =20 preempt_disable(); + check_lock(&lock->lock.debug, true); cnts =3D atomic_read(&lock->cnts); if ( likely(_can_read_lock(cnts)) ) { @@ -87,7 +88,11 @@ static inline void _read_lock(rwlock_t *lock) * arch_lock_acquire_barrier(). */ if ( likely(_can_read_lock(cnts)) ) + { + /* The slow path calls check_lock() via spin_lock(). */ + check_lock(&lock->lock.debug, false); return; + } =20 /* The slowpath will decrement the reader count, if necessary. */ queue_read_lock_slowpath(lock); @@ -162,7 +167,11 @@ static inline void _write_lock(rwlock_t *lock) * arch_lock_acquire_barrier(). */ if ( atomic_cmpxchg(&lock->cnts, 0, _write_lock_val()) =3D=3D 0 ) + { + /* The slow path calls check_lock() via spin_lock(). */ + check_lock(&lock->lock.debug, false); return; + } =20 queue_write_lock_slowpath(lock); /* @@ -197,6 +206,7 @@ static inline int _write_trylock(rwlock_t *lock) u32 cnts; =20 preempt_disable(); + check_lock(&lock->lock.debug, true); cnts =3D atomic_read(&lock->cnts); if ( unlikely(cnts) || unlikely(atomic_cmpxchg(&lock->cnts, 0, _write_lock_val()) !=3D 0= ) ) @@ -328,6 +338,11 @@ static inline void _percpu_read_lock(percpu_rwlock_t *= *per_cpudata, /* Drop the read lock because we don't need it anymore. */ read_unlock(&percpu_rwlock->rwlock); } + else + { + /* All other paths have implicit check_lock() calls via read_lock(= ). */ + check_lock(&percpu_rwlock->rwlock.lock.debug, false); + } } =20 static inline void _percpu_read_unlock(percpu_rwlock_t **per_cpudata, diff --git a/xen/include/xen/spinlock.h b/xen/include/xen/spinlock.h index ca13b600a0..9fa4e600c1 100644 --- a/xen/include/xen/spinlock.h +++ b/xen/include/xen/spinlock.h @@ -21,11 +21,13 @@ union lock_debug { }; }; #define _LOCK_DEBUG { LOCK_DEBUG_INITVAL } +void check_lock(union lock_debug *debug, bool try); void spin_debug_enable(void); void spin_debug_disable(void); #else union lock_debug { }; #define _LOCK_DEBUG { } +#define check_lock(l, t) ((void)0) #define spin_debug_enable() ((void)0) #define spin_debug_disable() ((void)0) #endif --=20 2.26.2