From patchwork Mon Nov 2 13:12:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?SsO8cmdlbiBHcm/Dnw==?= X-Patchwork-Id: 11873845 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0727E92C for ; Mon, 2 Nov 2020 13:13:33 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B668F21D40 for ; Mon, 2 Nov 2020 13:13:32 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="JJDx9OQI" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B668F21D40 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from list by lists.xenproject.org with outflank-mailman.17643.42428 (Exim 4.92) (envelope-from ) id 1kZZdh-0007uT-Lx; Mon, 02 Nov 2020 13:12:49 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 17643.42428; Mon, 02 Nov 2020 13:12:49 +0000 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" Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kZZdh-0007uM-IH; Mon, 02 Nov 2020 13:12:49 +0000 Received: by outflank-mailman (input) for mailman id 17643; Mon, 02 Nov 2020 13:12:48 +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 1kZZdg-0007rF-5g for xen-devel@lists.xenproject.org; Mon, 02 Nov 2020 13:12:48 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTP id c5cd9084-e640-4889-ab05-49d381c193a2; Mon, 02 Nov 2020 13:12:42 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id C226AB8EC; Mon, 2 Nov 2020 13:12:41 +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 1kZZdg-0007rF-5g for xen-devel@lists.xenproject.org; Mon, 02 Nov 2020 13:12:48 +0000 X-Inumbo-ID: c5cd9084-e640-4889-ab05-49d381c193a2 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTP id c5cd9084-e640-4889-ab05-49d381c193a2; Mon, 02 Nov 2020 13:12:42 +0000 (UTC) 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=1604322761; 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=JJDx9OQIvSVxsESeXmi9zIcNr8tsUuIarqK6CP/0pAG4ox1NPFkoPUl92GKoGN1awPs6va QLETHCgUOgD5qVHaXfXZu+GUBWq5RJE19pfDqzTVLrbPTzjKJEFbl8giH0jwd7Gk/NRfzw ZclF903txCmPv+XjdDooLn8+i+qrvkw= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id C226AB8EC; Mon, 2 Nov 2020 13:12:41 +0000 (UTC) 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 v2 1/2] xen/spinlocks: spin_trylock with interrupts off is always fine Date: Mon, 2 Nov 2020 14:12:38 +0100 Message-Id: <20201102131239.14134-2-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201102131239.14134-1-jgross@suse.com> References: <20201102131239.14134-1-jgross@suse.com> MIME-Version: 1.0 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 --- 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 @@ static atomic_t spin_debug __read_mostly = ATOMIC_INIT(0); -static void check_lock(union lock_debug *debug) +static void check_lock(union lock_debug *debug, bool try) { bool irq_safe = !local_irq_is_enabled(); @@ -42,7 +42,13 @@ static void check_lock(union lock_debug *debug) * * 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 != irq_safe) ) { union lock_debug seen, new = { 0 }; @@ -102,7 +108,7 @@ void spin_debug_disable(void) #else /* CONFIG_DEBUG_LOCKS */ -#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 = SPINLOCK_TICKET_INC; LOCK_PROFILE_VAR; - check_lock(&lock->debug); + check_lock(&lock->debug, false); preempt_disable(); tickets.head_tail = 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) 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; - check_lock(&lock->debug); + check_lock(&lock->debug, true); old = observe_lock(&lock->tickets); if ( old.head != 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); - check_lock(&lock->debug); + check_lock(&lock->debug, true); if ( likely(lock->recurse_cpu != cpu) ) { From patchwork Mon Nov 2 13:12:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?SsO8cmdlbiBHcm/Dnw==?= X-Patchwork-Id: 11873849 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 64AF692C for ; Mon, 2 Nov 2020 13:13:36 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2B37721D40 for ; Mon, 2 Nov 2020 13:13:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="cuMWbktm" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2B37721D40 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from list by lists.xenproject.org with outflank-mailman.17642.42409 (Exim 4.92) (envelope-from ) id 1kZZdd-0007ry-FH; Mon, 02 Nov 2020 13:12:45 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 17642.42409; Mon, 02 Nov 2020 13:12:45 +0000 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" Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kZZdd-0007rn-9k; Mon, 02 Nov 2020 13:12:45 +0000 Received: by outflank-mailman (input) for mailman id 17642; Mon, 02 Nov 2020 13:12:43 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kZZdb-0007rK-JQ for xen-devel@lists.xenproject.org; Mon, 02 Nov 2020 13:12:43 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTP id 7b84fdd6-28e7-4f23-99b9-3f92f0a91a01; Mon, 02 Nov 2020 13:12:42 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 0A637B8EF; Mon, 2 Nov 2020 13:12:42 +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 1kZZdb-0007rK-JQ for xen-devel@lists.xenproject.org; Mon, 02 Nov 2020 13:12:43 +0000 X-Inumbo-ID: 7b84fdd6-28e7-4f23-99b9-3f92f0a91a01 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTP id 7b84fdd6-28e7-4f23-99b9-3f92f0a91a01; Mon, 02 Nov 2020 13:12:42 +0000 (UTC) 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=1604322762; 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=T2Y6gfqjASfLX10Wg8Pwm6fRGu9Jh/ZF1PVmHXzkWYQ=; b=cuMWbktmuNjtb+fskPzMG61Ak3K97IFTgVS0UmXBlIfcAqmAhOslXS4IA33CvaVGOmLxoK Mx8W/FwYYFQ/KevfCJcYeV8NbEI96j8SDjsBgez/NcwqqYlVcrOtTPmjDfMqBXT+CHdJsX PK1BwVR3wN5wuB8OXjduqWcYKgvb1Ak= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 0A637B8EF; Mon, 2 Nov 2020 13:12:42 +0000 (UTC) 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 v2 2/2] xen/rwlock: add check_lock() handling to rwlocks Date: Mon, 2 Nov 2020 14:12:39 +0100 Message-Id: <20201102131239.14134-3-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201102131239.14134-1-jgross@suse.com> References: <20201102131239.14134-1-jgross@suse.com> MIME-Version: 1.0 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 --- V2: - call check_lock() unconditionally in try_lock variants (Jan Beulich) --- xen/common/spinlock.c | 3 +-- xen/include/xen/rwlock.h | 11 +++++++++++ xen/include/xen/spinlock.h | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/xen/common/spinlock.c b/xen/common/spinlock.c index b4aaf6bce6..405322c6b8 100644 --- a/xen/common/spinlock.c +++ b/xen/common/spinlock.c @@ -13,7 +13,7 @@ static atomic_t spin_debug __read_mostly = ATOMIC_INIT(0); -static void check_lock(union lock_debug *debug, bool try) +void check_lock(union lock_debug *debug, bool try) { bool irq_safe = !local_irq_is_enabled(); @@ -108,7 +108,6 @@ void spin_debug_disable(void) #else /* CONFIG_DEBUG_LOCKS */ -#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..94496a0f53 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; preempt_disable(); + check_lock(&lock->lock.debug, true); cnts = atomic_read(&lock->cnts); if ( likely(_can_read_lock(cnts)) ) { @@ -66,6 +67,7 @@ static inline int _read_trylock(rwlock_t *lock) */ if ( likely(_can_read_lock(cnts)) ) return 1; + atomic_sub(_QR_BIAS, &lock->cnts); } preempt_enable(); @@ -87,7 +89,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; + } /* The slowpath will decrement the reader count, if necessary. */ queue_read_lock_slowpath(lock); @@ -162,7 +167,10 @@ static inline void _write_lock(rwlock_t *lock) * arch_lock_acquire_barrier(). */ if ( atomic_cmpxchg(&lock->cnts, 0, _write_lock_val()) == 0 ) + { + check_lock(&lock->lock.debug, false); return; + } queue_write_lock_slowpath(lock); /* @@ -197,6 +205,7 @@ static inline int _write_trylock(rwlock_t *lock) u32 cnts; preempt_disable(); + check_lock(&lock->lock.debug, true); cnts = atomic_read(&lock->cnts); if ( unlikely(cnts) || unlikely(atomic_cmpxchg(&lock->cnts, 0, _write_lock_val()) != 0) ) @@ -328,6 +337,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); } 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