From nobody Sun May 19 19:40:28 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=1604067938; cv=none; d=zohomail.com; s=zohoarc; b=HHttmgedqgMXsDBNtAoK0PZoyjvCOMy2J6+CgbsKs262D2611oaN1WaS/g+wBBLzBAEpOloRl+v5yljeQ/x58m+xJMUNROIVf72l7uuHtWjwLkSo833IimMWzyyBsK2EsNhQ2mI8gAaNQistU8UmIydXpBZ8S1AWp31sxmf7+aM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1604067938; 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=5gKiiKos/dApKc2beKCGTAc1Rx9gih7YAhJwI1zCh3E=; b=WAxrlLsmdlasSOBjAF+3NWyEA+r5l00Rg0VfzDLvQk39WEuhPAStIKcW463MhMMX6mJb642/3S8rCgPYzgiSK0sLPjtAzb3xGQYLsV8Onon3aGtCI+6TDSmP0LHZwfl4/NqFo41Yn0B38Kfw9ZvbfG3mGY/D4zjRxXgYY1+PfAg= 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 1604067938089890.149887729039; Fri, 30 Oct 2020 07:25:38 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.15900.39151 (Exim 4.92) (envelope-from ) id 1kYVL4-0000km-FG; Fri, 30 Oct 2020 14:25:10 +0000 Received: by outflank-mailman (output) from mailman id 15900.39151; Fri, 30 Oct 2020 14:25:10 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kYVL4-0000kc-CC; Fri, 30 Oct 2020 14:25:10 +0000 Received: by outflank-mailman (input) for mailman id 15900; Fri, 30 Oct 2020 14:25:09 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kYVL2-0000hB-Vc for xen-devel@lists.xenproject.org; Fri, 30 Oct 2020 14:25:09 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 24413e0e-f2b8-46c8-9fe3-74a28d1bb41c; Fri, 30 Oct 2020 14:25:03 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 8B54DABD1; Fri, 30 Oct 2020 14:25:02 +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 1kYVL2-0000hB-Vc for xen-devel@lists.xenproject.org; Fri, 30 Oct 2020 14:25:09 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 24413e0e-f2b8-46c8-9fe3-74a28d1bb41c; Fri, 30 Oct 2020 14:25:03 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 8B54DABD1; Fri, 30 Oct 2020 14:25:02 +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: 24413e0e-f2b8-46c8-9fe3-74a28d1bb41c 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=1604067902; 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=5gKiiKos/dApKc2beKCGTAc1Rx9gih7YAhJwI1zCh3E=; b=d/9UWwuOw44S/0Dnua9hZLPFL7oLqVNAb6BTZa6QwkYJ0D1sZjhA5BcwZuVfRjyVjmB+H4 tfVeMSdkw8Lb6za77huWQFmYYTigKHXzx6l6Q1MbhWdBUn9kth+zK6IT39V9tzca7ZRbgD W0HV+jHxh7DD1JXdWX98sKXFxX36Kck= 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 1/2] xen/spinlocks: spin_trylock with interrupts off is always fine Date: Fri, 30 Oct 2020 15:24:59 +0100 Message-Id: <20201030142500.5464-2-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201030142500.5464-1-jgross@suse.com> References: <20201030142500.5464-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 --- 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..54f0c55dc2 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() or spin_is_locked() with interrupts off is always + * fine, as those can't block and above deadlock scenario doesn't appl= y. */ + 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 Sun May 19 19:40:28 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=1604067939; cv=none; d=zohomail.com; s=zohoarc; b=nnSJ0OkUE81ba2u45t+9T2al6yndnFnUZUDZdoFwrp5VCoi5e/l/KHX2W3snfcWbO0873QAnsKu6Rx286OJlOQApwp3yvvRG9aBltt6AJU+xDjvat38SXOQoJh9hc4j7iaQTH2bEDWVQZh4WxlyQcM0IA5c4UdqTxaUya6II+CI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1604067939; 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=ZRO626gkCC58/+xHp5IDaonkexTsr38HffOaqWn+6/w=; b=Y8zEmpdNcexIlCPuptTLAGxmEOkSE/BHHZxmMIQjDYmN4cttfElhuN4V6jlQu10zSfKOqi519IU8H1RLNIS1ebAnvut7sXMxuTbvaUa01Z0xlvhTG/eHokw0Y7o9zU9vYEC5Js5juPZPTYlCG65wL0mVW2vdLNevs30qfA8MbZc= 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 1604067939921314.8803674627036; Fri, 30 Oct 2020 07:25:39 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.15899.39139 (Exim 4.92) (envelope-from ) id 1kYVL1-0000iM-6T; Fri, 30 Oct 2020 14:25:07 +0000 Received: by outflank-mailman (output) from mailman id 15899.39139; Fri, 30 Oct 2020 14:25:07 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kYVL1-0000iF-3O; Fri, 30 Oct 2020 14:25:07 +0000 Received: by outflank-mailman (input) for mailman id 15899; Fri, 30 Oct 2020 14:25:05 +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 1kYVKz-0000i7-IQ for xen-devel@lists.xenproject.org; Fri, 30 Oct 2020 14:25:05 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 48b2fdfd-684a-4f63-a173-17789522b443; Fri, 30 Oct 2020 14:25:03 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id C2C50AE1A; Fri, 30 Oct 2020 14:25:02 +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 1kYVKz-0000i7-IQ for xen-devel@lists.xenproject.org; Fri, 30 Oct 2020 14:25:05 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 48b2fdfd-684a-4f63-a173-17789522b443; Fri, 30 Oct 2020 14:25:03 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id C2C50AE1A; Fri, 30 Oct 2020 14:25:02 +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: 48b2fdfd-684a-4f63-a173-17789522b443 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=1604067902; 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=ZRO626gkCC58/+xHp5IDaonkexTsr38HffOaqWn+6/w=; b=d6qra57GhXRqkpVwBrQobeF5Y0zM4Ap5B7NJ7WU3E02GRIpTPb1IuJwNvOim9rda2EYzhd VksYmMAgpp51M6dXHfdIZ5vQlPEjv56wkYUf4YsYMdcrAetywYWxe+qZ7R6wLWYgv1gB7M HljXnBlOoSrUKHQN1Ojc3PnUs9B8J7k= 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 2/2] xen/rwlock: add check_lock() handling to rwlocks Date: Fri, 30 Oct 2020 15:25:00 +0100 Message-Id: <20201030142500.5464-3-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201030142500.5464-1-jgross@suse.com> References: <20201030142500.5464-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 --- xen/common/spinlock.c | 3 +-- xen/include/xen/rwlock.h | 14 ++++++++++++++ xen/include/xen/spinlock.h | 2 ++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/xen/common/spinlock.c b/xen/common/spinlock.c index 54f0c55dc2..acb3f86339 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..c302644705 100644 --- a/xen/include/xen/rwlock.h +++ b/xen/include/xen/rwlock.h @@ -65,7 +65,11 @@ static inline int _read_trylock(rwlock_t *lock) * arch_lock_acquire_barrier(). */ if ( likely(_can_read_lock(cnts)) ) + { + check_lock(&lock->lock.debug, true); return 1; + } + atomic_sub(_QR_BIAS, &lock->cnts); } preempt_enable(); @@ -87,7 +91,10 @@ static inline void _read_lock(rwlock_t *lock) * arch_lock_acquire_barrier(). */ if ( likely(_can_read_lock(cnts)) ) + { + check_lock(&lock->lock.debug, false); return; + } =20 /* The slowpath will decrement the reader count, if necessary. */ queue_read_lock_slowpath(lock); @@ -162,7 +169,10 @@ static inline void _write_lock(rwlock_t *lock) * arch_lock_acquire_barrier(). */ if ( atomic_cmpxchg(&lock->cnts, 0, _write_lock_val()) =3D=3D 0 ) + { + check_lock(&lock->lock.debug, false); return; + } =20 queue_write_lock_slowpath(lock); /* @@ -205,6 +215,8 @@ static inline int _write_trylock(rwlock_t *lock) return 0; } =20 + check_lock(&lock->lock.debug, true); + /* * atomic_cmpxchg() is a full barrier so no need for an * arch_lock_acquire_barrier(). @@ -328,6 +340,8 @@ 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 + 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