From nobody Mon Feb 9 01:20:13 2026 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1565330936; cv=none; d=zoho.com; s=zohoarc; b=FpCtlUVxgetkWwUj0dW13wGEUVxsPXdYrSpLAbKMFbHCTJawamZmyBcmjJu8p9qw6cDPPoh3oXdIy0As8iJuCOrluiAMiruNtoYqF9XY1QAJyR6qsH5txlaKbarR4iZBJVQqqlPEEyBvos6sMF6xSiKBxm8xac9yt/pkhzlFqU4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1565330936; h=Content-Type: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:ARC-Authentication-Results; bh=wiRjHPcNoSh0Yhe/t6GEWPzELf0hWv50sQV6BLHYsjU=; b=GC8qU+msnvkLcdsa6sqzFwADSOHuGbvGS0/V/ov4YoP7Grs2unGkRY+za19D7sa7zAW/ElF7t6r0wpplQE8NSwX/S5wD47RAwCRj58jD6ef/28uwNGrGwyoYrWEl5qoDVS6LqTKUCTHuXYrUr3FviK2Hq1IKEywHoCsUhhW2KTk= ARC-Authentication-Results: i=1; mx.zoho.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1565330936554531.2348972376842; Thu, 8 Aug 2019 23:08:56 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hvy4D-0006eH-5k; Fri, 09 Aug 2019 06:07:57 +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.89) (envelope-from ) id 1hvy4C-0006dX-4B for xen-devel@lists.xenproject.org; Fri, 09 Aug 2019 06:07:56 +0000 Received: from mx1.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 045452fc-ba6c-11e9-bb68-83c39d6ce56b; Fri, 09 Aug 2019 06:07:50 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id CB197B077; Fri, 9 Aug 2019 06:07:48 +0000 (UTC) X-Inumbo-ID: 045452fc-ba6c-11e9-bb68-83c39d6ce56b X-Virus-Scanned: by amavisd-new at test-mx.suse.de From: Juergen Gross To: xen-devel@lists.xenproject.org Date: Fri, 9 Aug 2019 08:07:40 +0200 Message-Id: <20190809060744.8985-2-jgross@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20190809060744.8985-1-jgross@suse.com> References: <20190809060744.8985-1-jgross@suse.com> Subject: [Xen-devel] [PATCH v2 1/5] xen/spinlocks: in debug builds store cpu holding the lock X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Juergen Gross , Stefano Stabellini , Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Andrew Cooper , Ian Jackson , Tim Deegan , Julien Grall , Jan Beulich MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Add the cpu currently holding the lock to struct lock_debug. This makes analysis of locking errors easier and it can be tested whether the correct cpu is releasing a lock again. Signed-off-by: Juergen Gross --- V2: adjust types (Jan Beulich) --- xen/common/spinlock.c | 33 ++++++++++++++++++++++++++------- xen/include/xen/spinlock.h | 17 ++++++++++++----- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/xen/common/spinlock.c b/xen/common/spinlock.c index 6bc52d70c0..1be1b5ebe6 100644 --- a/xen/common/spinlock.c +++ b/xen/common/spinlock.c @@ -13,9 +13,9 @@ =20 static atomic_t spin_debug __read_mostly =3D ATOMIC_INIT(0); =20 -static void check_lock(struct lock_debug *debug) +static void check_lock(union lock_debug *debug) { - int irq_safe =3D !local_irq_is_enabled(); + bool irq_safe =3D !local_irq_is_enabled(); =20 if ( unlikely(atomic_read(&spin_debug) <=3D 0) ) return; @@ -43,18 +43,21 @@ static void check_lock(struct lock_debug *debug) */ if ( unlikely(debug->irq_safe !=3D irq_safe) ) { - int seen =3D cmpxchg(&debug->irq_safe, -1, irq_safe); + union lock_debug seen, new =3D { 0 }; =20 - if ( seen =3D=3D !irq_safe ) + new.irq_safe =3D irq_safe; + seen.val =3D cmpxchg(&debug->val, LOCK_DEBUG_INITVAL, new.val); + + if ( !seen.unseen && seen.irq_safe =3D=3D !irq_safe ) { printk("CHECKLOCK FAILURE: prev irqsafe: %d, curr irqsafe %d\n= ", - seen, irq_safe); + seen.irq_safe, irq_safe); BUG(); } } } =20 -static void check_barrier(struct lock_debug *debug) +static void check_barrier(union lock_debug *debug) { if ( unlikely(atomic_read(&spin_debug) <=3D 0) ) return; @@ -70,7 +73,18 @@ static void check_barrier(struct lock_debug *debug) * However, if we spin on an IRQ-unsafe lock with IRQs disabled then t= hat * is clearly wrong, for the same reason outlined in check_lock() abov= e. */ - BUG_ON(!local_irq_is_enabled() && (debug->irq_safe =3D=3D 0)); + BUG_ON(!local_irq_is_enabled() && !debug->irq_safe); +} + +static void got_lock(union lock_debug *debug) +{ + debug->cpu =3D smp_processor_id(); +} + +static void rel_lock(union lock_debug *debug) +{ + ASSERT(debug->cpu =3D=3D smp_processor_id()); + debug->cpu =3D SPINLOCK_NO_CPU; } =20 void spin_debug_enable(void) @@ -87,6 +101,8 @@ void spin_debug_disable(void) =20 #define check_lock(l) ((void)0) #define check_barrier(l) ((void)0) +#define got_lock(l) ((void)0) +#define rel_lock(l) ((void)0) =20 #endif =20 @@ -150,6 +166,7 @@ void inline _spin_lock_cb(spinlock_t *lock, void (*cb)(= void *), void *data) cb(data); arch_lock_relax(); } + got_lock(&lock->debug); LOCK_PROFILE_GOT; preempt_disable(); arch_lock_acquire_barrier(); @@ -181,6 +198,7 @@ void _spin_unlock(spinlock_t *lock) arch_lock_release_barrier(); preempt_enable(); LOCK_PROFILE_REL; + rel_lock(&lock->debug); add_sized(&lock->tickets.head, 1); arch_lock_signal(); } @@ -224,6 +242,7 @@ int _spin_trylock(spinlock_t *lock) if ( cmpxchg(&lock->tickets.head_tail, old.head_tail, new.head_tail) !=3D old.head_tail ) return 0; + got_lock(&lock->debug); #ifdef CONFIG_LOCK_PROFILE if (lock->profile) lock->profile->time_locked =3D NOW(); diff --git a/xen/include/xen/spinlock.h b/xen/include/xen/spinlock.h index a811b73bf3..97b6200bf5 100644 --- a/xen/include/xen/spinlock.h +++ b/xen/include/xen/spinlock.h @@ -7,14 +7,21 @@ #include =20 #ifndef NDEBUG -struct lock_debug { - s16 irq_safe; /* +1: IRQ-safe; 0: not IRQ-safe; -1: don't know yet */ +union lock_debug { + uint16_t val; +#define LOCK_DEBUG_INITVAL 0xffff + struct { + uint16_t cpu:12; + uint16_t pad:2; + bool irq_safe:1; + bool unseen:1; + }; }; -#define _LOCK_DEBUG { -1 } +#define _LOCK_DEBUG { LOCK_DEBUG_INITVAL } void spin_debug_enable(void); void spin_debug_disable(void); #else -struct lock_debug { }; +union lock_debug { }; #define _LOCK_DEBUG { } #define spin_debug_enable() ((void)0) #define spin_debug_disable() ((void)0) @@ -143,7 +150,7 @@ typedef struct spinlock { #define SPINLOCK_NO_CPU 0xfffu u16 recurse_cnt:4; #define SPINLOCK_MAX_RECURSE 0xfu - struct lock_debug debug; + union lock_debug debug; #ifdef CONFIG_LOCK_PROFILE struct lock_profile *profile; #endif --=20 2.16.4 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel