From nobody Sat May 4 16:45:10 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=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1606301508; cv=none; d=zohomail.com; s=zohoarc; b=GVNbdd1wPiZ6wJcEhrJeQiDxDMOcb5SmgxQaxanX+sEK9BxBnNIUYEMPGm3TAd/eprcqBmB+iAOHKUl/JcZXlIHSTeAU6Z4Z4brel8Vp714cFPJTQVmZAmIiiiTaeoXFGa3rObJ1m7z7uNFV5Fh4LFqwuwmrv44MIJePEouPPQk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1606301508; 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=9Eg0Mg6cy2GDmCEZCPsyzzLpzDv8NGg/SYGtCOknElU=; b=ZzeIBuLBj4TKq5CzVrFzS2Ux+LpmlQ1y/QsvTrFAY1CpFulub6adBPH2Z0Uoi8+BYDP6FhgOBlhybSyBij1LCDn2Fv2XnT0TcULJcanHLDoWEZScQJzBrc6yR/quELhj0qytJVWnjHZR9mEf5Qwf7vMJa7nRDgcxKyqbO8MKl9M= 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=quarantine 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 1606301508505529.1396799482288; Wed, 25 Nov 2020 02:51:48 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.37537.69965 (Exim 4.92) (envelope-from ) id 1khsOa-0007CY-2X; Wed, 25 Nov 2020 10:51:32 +0000 Received: by outflank-mailman (output) from mailman id 37537.69965; Wed, 25 Nov 2020 10:51:32 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1khsOZ-0007CQ-UJ; Wed, 25 Nov 2020 10:51:31 +0000 Received: by outflank-mailman (input) for mailman id 37537; Wed, 25 Nov 2020 10:51:30 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1khsOY-00078P-Nr for xen-devel@lists.xenproject.org; Wed, 25 Nov 2020 10:51:30 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 15b0d67e-5c71-4322-8fde-7569aaecc862; Wed, 25 Nov 2020 10:51:25 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 76C72AC75; Wed, 25 Nov 2020 10:51:24 +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 1khsOY-00078P-Nr for xen-devel@lists.xenproject.org; Wed, 25 Nov 2020 10:51:30 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 15b0d67e-5c71-4322-8fde-7569aaecc862; Wed, 25 Nov 2020 10:51:25 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 76C72AC75; Wed, 25 Nov 2020 10:51:24 +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: 15b0d67e-5c71-4322-8fde-7569aaecc862 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=1606301484; h=from:from:reply-to: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=9Eg0Mg6cy2GDmCEZCPsyzzLpzDv8NGg/SYGtCOknElU=; b=cgFuGboLJhsfeevFj/HKdsBzy4EggHI0/RUa/dgCg+KFK0vTqHrgDpzoutlWcCrpc52wp8 aDaFoZI1wZG6f/t9tuXiLtoikpkuLPJcHJgpgSeMuEYNkdT3tageJPoaRJO41RLawcQKag 5ddCqdgXX4vlvvOgkDefvEZeCNoc7CU= 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 v8 1/3] xen/events: modify struct evtchn layout Date: Wed, 25 Nov 2020 11:51:20 +0100 Message-Id: <20201125105122.3650-2-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201125105122.3650-1-jgross@suse.com> References: <20201125105122.3650-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" In order to avoid latent races when updating an event channel put xen_consumer and pending fields in different bytes. This is no problem right now, but especially the pending indicator isn't used only when initializing an event channel (unlike xen_consumer), so any future addition to this byte would need to be done with a potential race kept in mind. At the same time move some other fields around to have less implicit paddings and to keep related fields more closely together. Finally switch struct evtchn to no longer use fixed sized types where not needed. Signed-off-by: Juergen Gross Reviewed-by: Jan Beulich --- V7: - new patch V8: - retain xen_consumer to be a bitfield (Jan Beulich) - switch to non fixed sizes types Signed-off-by: Juergen Gross --- xen/common/event_fifo.c | 4 ++-- xen/include/xen/sched.h | 34 ++++++++++++++++++---------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/xen/common/event_fifo.c b/xen/common/event_fifo.c index 79090c04ca..f39e61105f 100644 --- a/xen/common/event_fifo.c +++ b/xen/common/event_fifo.c @@ -200,7 +200,7 @@ static void evtchn_fifo_set_pending(struct vcpu *v, str= uct evtchn *evtchn) */ if ( unlikely(!word) ) { - evtchn->pending =3D 1; + evtchn->pending =3D true; return; } =20 @@ -535,7 +535,7 @@ static void setup_ports(struct domain *d, unsigned int = prev_evtchns) evtchn =3D evtchn_from_port(d, port); =20 if ( guest_test_bit(d, port, &shared_info(d, evtchn_pending)) ) - evtchn->pending =3D 1; + evtchn->pending =3D true; =20 evtchn_fifo_set_priority(d, evtchn, EVTCHN_FIFO_PRIORITY_DEFAULT); } diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index a345cc01f8..7afbae7dd1 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -93,31 +93,33 @@ struct evtchn #define ECS_PIRQ 4 /* Channel is bound to a physical IRQ line. = */ #define ECS_VIRQ 5 /* Channel is bound to a virtual IRQ line. = */ #define ECS_IPI 6 /* Channel is bound to a virtual IPI line. = */ - u8 state; /* ECS_* */ - u8 xen_consumer:XEN_CONSUMER_BITS; /* Consumer in Xen if nonzero */ - u8 pending:1; - u16 notify_vcpu_id; /* VCPU for local delivery notification */ - u32 port; + unsigned char state; /* ECS_* */ +#ifndef NDEBUG + unsigned char old_state; /* State when taking lock in write mode. */ +#endif + unsigned char xen_consumer:XEN_CONSUMER_BITS; /* Consumer in Xen if != =3D 0 */ + unsigned int port; union { struct { domid_t remote_domid; - } unbound; /* state =3D=3D ECS_UNBOUND */ + } unbound; /* state =3D=3D ECS_UNBOUND */ struct { evtchn_port_t remote_port; struct domain *remote_dom; - } interdomain; /* state =3D=3D ECS_INTERDOMAIN */ + } interdomain; /* state =3D=3D ECS_INTERDOMAIN */ struct { - u32 irq; + unsigned int irq; evtchn_port_t next_port; evtchn_port_t prev_port; - } pirq; /* state =3D=3D ECS_PIRQ */ - u16 virq; /* state =3D=3D ECS_VIRQ */ + } pirq; /* state =3D=3D ECS_PIRQ */ + unsigned int virq; /* state =3D=3D ECS_VIRQ */ } u; - u8 priority; -#ifndef NDEBUG - u8 old_state; /* State when taking lock in write mode. */ -#endif - u32 fifo_lastq; /* Data for fifo events identifying last queue. */ + + bool pending; /* FIFO event channels only. */ + unsigned char priority; /* FIFO event channels only. */ + unsigned short notify_vcpu_id; /* VCPU for local delivery notification= */ + uint32_t fifo_lastq; /* Data for identifying last queue. */ + #ifdef CONFIG_XSM union { #ifdef XSM_NEED_GENERIC_EVTCHN_SSID @@ -133,7 +135,7 @@ struct evtchn * allocations, and on 64-bit platforms with only FLASK enabled, * reduces the size of struct evtchn. */ - u32 flask_sid; + uint32_t flask_sid; #endif } ssid; #endif --=20 2.26.2 From nobody Sat May 4 16:45:10 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=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1606301510; cv=none; d=zohomail.com; s=zohoarc; b=edVzz+s20w6s2xMik1lMQTviAN2yIbmpX1apVK0KXMkgaElZVs1c9qK5pT9UqQtXdiDsn/JHWi5JSvJoVIVUELBXGKYneuONNZi5oxnywTS3nbJRO4KFMK/BQvQis32qNzG2HaivK5nnnt/ZGih3LH8DFNY+eIHrSl07FIAmD5o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1606301510; 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=Tg5vf3t7YsP9mKYoN8C2ngq6yNlhQuq9n9e2uzxQXtU=; b=eab3kp7lMyW6Z4lVMpqc8a1hrwDZTBvAKH6RVLsytNhrpVmjFnvj4Pa/rDWhd79JoTbiTpvDZSqHF5vdAqTQYc4rfiiTB9uW7Vqsf1D6DYcfVn/pkOYOcU27g1gAyipJ9WKNeJBV1izgFj/LJYw5+vTH2hvHuvtxDzJCo+jb14c= 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=quarantine 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 1606301510961836.4220741885864; Wed, 25 Nov 2020 02:51:50 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.37536.69953 (Exim 4.92) (envelope-from ) id 1khsOW-00079g-Mz; Wed, 25 Nov 2020 10:51:28 +0000 Received: by outflank-mailman (output) from mailman id 37536.69953; Wed, 25 Nov 2020 10:51:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1khsOW-00079Z-Hu; Wed, 25 Nov 2020 10:51:28 +0000 Received: by outflank-mailman (input) for mailman id 37536; Wed, 25 Nov 2020 10:51:27 +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 1khsOV-00078k-Gm for xen-devel@lists.xenproject.org; Wed, 25 Nov 2020 10:51:27 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id f2b96b92-b327-40c4-b27a-902de765b199; Wed, 25 Nov 2020 10:51:25 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id B39CDADCD; Wed, 25 Nov 2020 10:51:24 +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 1khsOV-00078k-Gm for xen-devel@lists.xenproject.org; Wed, 25 Nov 2020 10:51:27 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id f2b96b92-b327-40c4-b27a-902de765b199; Wed, 25 Nov 2020 10:51:25 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id B39CDADCD; Wed, 25 Nov 2020 10:51:24 +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: f2b96b92-b327-40c4-b27a-902de765b199 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=1606301484; h=from:from:reply-to: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=Tg5vf3t7YsP9mKYoN8C2ngq6yNlhQuq9n9e2uzxQXtU=; b=mratzSmJHcroCNNvBvMePISXhf44NkOCthoU477BA+ypVbIhdWtxakdvnPAK9gfx7de619 B+mv4WoerIQ/W134zLIIqPqxrc/B5C5ZSEGvu/f8OWpalwSd9/sU4NLRssvxcZ/pW8ViHZ LRRK6f66CRCYrLvTnxaQrJSNA3KCLOw= 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 v8 2/3] xen/events: rework fifo queue locking Date: Wed, 25 Nov 2020 11:51:21 +0100 Message-Id: <20201125105122.3650-3-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201125105122.3650-1-jgross@suse.com> References: <20201125105122.3650-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" Two cpus entering evtchn_fifo_set_pending() for the same event channel can race in case the first one gets interrupted after setting EVTCHN_FIFO_PENDING and when the other one manages to set EVTCHN_FIFO_LINKED before the first one is testing that bit. This can lead to evtchn_check_pollers() being called before the event is put properly into the queue, resulting eventually in the guest not seeing the event pending and thus blocking forever afterwards. Note that commit 5f2df45ead7c1195 ("xen/evtchn: rework per event channel lock") made the race just more obvious, while the fifo event channel implementation had this race from the beginning when an unmask operation was running in parallel with an event channel send operation. Using a spinlock for the per event channel lock is problematic due to some paths needing to take the lock are called with interrupts off, so the lock would need to disable interrupts, which in turn breaks some use cases related to vm events. For avoiding this race the queue locking in evtchn_fifo_set_pending() needs to be reworked to cover the test of EVTCHN_FIFO_PENDING, EVTCHN_FIFO_MASKED and EVTCHN_FIFO_LINKED, too. Additionally when an event channel needs to change queues both queues need to be locked initially. Reported-by: Jan Beulich Fixes: 5f2df45ead7c1195 ("xen/evtchn: rework per event channel lock") Fixes: de6acb78bf0e137c ("evtchn: use a per-event channel lock for sending = events") Signed-off-by: Juergen Gross Reviewed-by: Jan Beulich --- V7: - new patch V8: - update commit message (Jan Beulich) - update double locking (Jan Beulich) - add comment (Jan Beulich) - fix handling when not getting lock (Jan Beulich) - add const (Jan Beulich) Signed-off-by: Juergen Gross --- xen/common/event_fifo.c | 128 ++++++++++++++++++++++------------------ 1 file changed, 70 insertions(+), 58 deletions(-) diff --git a/xen/common/event_fifo.c b/xen/common/event_fifo.c index f39e61105f..443593c3b3 100644 --- a/xen/common/event_fifo.c +++ b/xen/common/event_fifo.c @@ -87,38 +87,6 @@ static void evtchn_fifo_init(struct domain *d, struct ev= tchn *evtchn) d->domain_id, evtchn->port); } =20 -static struct evtchn_fifo_queue *lock_old_queue(const struct domain *d, - struct evtchn *evtchn, - unsigned long *flags) -{ - struct vcpu *v; - struct evtchn_fifo_queue *q, *old_q; - unsigned int try; - union evtchn_fifo_lastq lastq; - - for ( try =3D 0; try < 3; try++ ) - { - lastq.raw =3D read_atomic(&evtchn->fifo_lastq); - v =3D d->vcpu[lastq.last_vcpu_id]; - old_q =3D &v->evtchn_fifo->queue[lastq.last_priority]; - - spin_lock_irqsave(&old_q->lock, *flags); - - v =3D d->vcpu[lastq.last_vcpu_id]; - q =3D &v->evtchn_fifo->queue[lastq.last_priority]; - - if ( old_q =3D=3D q ) - return old_q; - - spin_unlock_irqrestore(&old_q->lock, *flags); - } - - gprintk(XENLOG_WARNING, - "dom%d port %d lost event (too many queue changes)\n", - d->domain_id, evtchn->port); - return NULL; -} =20 - static int try_set_link(event_word_t *word, event_word_t *w, uint32_t link) { event_word_t new, old; @@ -190,6 +158,9 @@ static void evtchn_fifo_set_pending(struct vcpu *v, str= uct evtchn *evtchn) event_word_t *word; unsigned long flags; bool_t was_pending; + struct evtchn_fifo_queue *q, *old_q; + unsigned int try; + bool linked =3D true; =20 port =3D evtchn->port; word =3D evtchn_fifo_word_from_port(d, port); @@ -204,17 +175,67 @@ static void evtchn_fifo_set_pending(struct vcpu *v, s= truct evtchn *evtchn) return; } =20 + /* + * Lock all queues related to the event channel (in case of a queue ch= ange + * this might be two). + * It is mandatory to do that before setting and testing the PENDING b= it + * and to hold the current queue lock until the event has put into the + * list of pending events in order to avoid waking up a guest without = the + * event being visibly pending in the guest. + */ + for ( try =3D 0; try < 4; try++ ) + { + union evtchn_fifo_lastq lastq; + const struct vcpu *old_v; + + lastq.raw =3D read_atomic(&evtchn->fifo_lastq); + old_v =3D d->vcpu[lastq.last_vcpu_id]; + + q =3D &v->evtchn_fifo->queue[evtchn->priority]; + old_q =3D &old_v->evtchn_fifo->queue[lastq.last_priority]; + + if ( q =3D=3D old_q ) + spin_lock_irqsave(&q->lock, flags); + else if ( q < old_q ) + { + spin_lock_irqsave(&q->lock, flags); + spin_lock(&old_q->lock); + } + else + { + spin_lock_irqsave(&old_q->lock, flags); + spin_lock(&q->lock); + } + + lastq.raw =3D read_atomic(&evtchn->fifo_lastq); + old_v =3D d->vcpu[lastq.last_vcpu_id]; + if ( q =3D=3D &v->evtchn_fifo->queue[evtchn->priority] && + old_q =3D=3D &old_v->evtchn_fifo->queue[lastq.last_priority] ) + break; + + if ( q !=3D old_q ) + spin_unlock(&old_q->lock); + spin_unlock_irqrestore(&q->lock, flags); + } + was_pending =3D guest_test_and_set_bit(d, EVTCHN_FIFO_PENDING, word); =20 + /* If we didn't get the lock bail out. */ + if ( try =3D=3D 4 ) + { + gprintk(XENLOG_WARNING, + "dom%d port %d lost event (too many queue changes)\n", + d->domain_id, evtchn->port); + goto done; + } + /* * Link the event if it unmasked and not already linked. */ if ( !guest_test_bit(d, EVTCHN_FIFO_MASKED, word) && !guest_test_bit(d, EVTCHN_FIFO_LINKED, word) ) { - struct evtchn_fifo_queue *q, *old_q; event_word_t *tail_word; - bool_t linked =3D 0; =20 /* * Control block not mapped. The guest must not unmask an @@ -225,25 +246,11 @@ static void evtchn_fifo_set_pending(struct vcpu *v, s= truct evtchn *evtchn) { printk(XENLOG_G_WARNING "%pv has no FIFO event channel control block\n", v); - goto done; + goto unlock; } =20 - /* - * No locking around getting the queue. This may race with - * changing the priority but we are allowed to signal the - * event once on the old priority. - */ - q =3D &v->evtchn_fifo->queue[evtchn->priority]; - - old_q =3D lock_old_queue(d, evtchn, &flags); - if ( !old_q ) - goto done; - if ( guest_test_and_set_bit(d, EVTCHN_FIFO_LINKED, word) ) - { - spin_unlock_irqrestore(&old_q->lock, flags); - goto done; - } + goto unlock; =20 /* * If this event was a tail, the old queue is now empty and @@ -262,8 +269,8 @@ static void evtchn_fifo_set_pending(struct vcpu *v, str= uct evtchn *evtchn) lastq.last_priority =3D q->priority; write_atomic(&evtchn->fifo_lastq, lastq.raw); =20 - spin_unlock_irqrestore(&old_q->lock, flags); - spin_lock_irqsave(&q->lock, flags); + spin_unlock(&old_q->lock); + old_q =3D q; } =20 /* @@ -276,6 +283,7 @@ static void evtchn_fifo_set_pending(struct vcpu *v, str= uct evtchn *evtchn) * If the queue is empty (i.e., we haven't linked to the new * event), head must be updated. */ + linked =3D false; if ( q->tail ) { tail_word =3D evtchn_fifo_word_from_port(d, q->tail); @@ -284,15 +292,19 @@ static void evtchn_fifo_set_pending(struct vcpu *v, s= truct evtchn *evtchn) if ( !linked ) write_atomic(q->head, port); q->tail =3D port; + } =20 - spin_unlock_irqrestore(&q->lock, flags); + unlock: + if ( q !=3D old_q ) + spin_unlock(&old_q->lock); + spin_unlock_irqrestore(&q->lock, flags); =20 - if ( !linked - && !guest_test_and_set_bit(d, q->priority, - &v->evtchn_fifo->control_block->re= ady) ) - vcpu_mark_events_pending(v); - } done: + if ( !linked && + !guest_test_and_set_bit(d, q->priority, + &v->evtchn_fifo->control_block->ready) ) + vcpu_mark_events_pending(v); + if ( !was_pending ) evtchn_check_pollers(d, port); } --=20 2.26.2 From nobody Sat May 4 16:45:10 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=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1606301512; cv=none; d=zohomail.com; s=zohoarc; b=MjZhkWLLEMxbyd5IvyFd1+OPypOKf/TcSkp8331F1SAGo+aYGddXIsS58Y+4WkMrq0M2HjuRfkQdi+28VUsmk9eEn+TQ4soYIsRo9+yHI1IJVAV+9i5llTC42WIXQv9e09zwTUCk4POBfXIy5dFZcC6NqUIc5y9CbC03crjlgUM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1606301512; 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=PpgH835OJv8kLqZm+CjGqtlbK0CvTiSkCJEinT/4/4A=; b=fnmAa6luPFz4M85GPTETVkD1CiCvApq3/M0ns24O4+hEWw+nAnWd3F4xWEhJcxVbp4sVB05F+bvEfIR9md1KMsOxjayLIKm7NOmLMwB3r7s34LWMkRe9rlR+1WjkixThj7nJ9BUcpv3onoQSSDfNTwBtvSCFwv8xVLjjkmwK9l8= 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=quarantine 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 1606301512283774.3164123348379; Wed, 25 Nov 2020 02:51:52 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.37538.69977 (Exim 4.92) (envelope-from ) id 1khsOf-0007Hf-BP; Wed, 25 Nov 2020 10:51:37 +0000 Received: by outflank-mailman (output) from mailman id 37538.69977; Wed, 25 Nov 2020 10:51:37 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1khsOf-0007HU-86; Wed, 25 Nov 2020 10:51:37 +0000 Received: by outflank-mailman (input) for mailman id 37538; Wed, 25 Nov 2020 10:51:35 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1khsOd-00078P-O7 for xen-devel@lists.xenproject.org; Wed, 25 Nov 2020 10:51:35 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 5ca0cb6e-32be-44dd-8153-fbc6d1ce5e33; Wed, 25 Nov 2020 10:51:25 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id EE96EADAA; Wed, 25 Nov 2020 10:51:24 +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 1khsOd-00078P-O7 for xen-devel@lists.xenproject.org; Wed, 25 Nov 2020 10:51:35 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 5ca0cb6e-32be-44dd-8153-fbc6d1ce5e33; Wed, 25 Nov 2020 10:51:25 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id EE96EADAA; Wed, 25 Nov 2020 10:51:24 +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: 5ca0cb6e-32be-44dd-8153-fbc6d1ce5e33 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=1606301485; h=from:from:reply-to: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=PpgH835OJv8kLqZm+CjGqtlbK0CvTiSkCJEinT/4/4A=; b=nSnC7kr60X/sBbqFE000ZLKQyFQTvBtUKu2x/AGj7aChXmOIUTrpitVGUMYf+LK5+wx4LX PmYEtXQxYijVS4vIGSlBoqa/nQGy0EI+2OCUUxpMCU8eRv16ribc9X/UynAbwbPvyHblkR ypynLSaQOuv7STlzVZTQP9pDEY5w7r8= 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 v8 3/3] xen/events: do some cleanups in evtchn_fifo_set_pending() Date: Wed, 25 Nov 2020 11:51:22 +0100 Message-Id: <20201125105122.3650-4-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201125105122.3650-1-jgross@suse.com> References: <20201125105122.3650-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" evtchn_fifo_set_pending() can be simplified a little bit. Suggested-by: Jan Beulich Signed-off-by: Juergen Gross --- V8: - new patch --- xen/common/event_fifo.c | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/xen/common/event_fifo.c b/xen/common/event_fifo.c index 443593c3b3..77609539b1 100644 --- a/xen/common/event_fifo.c +++ b/xen/common/event_fifo.c @@ -175,6 +175,18 @@ static void evtchn_fifo_set_pending(struct vcpu *v, st= ruct evtchn *evtchn) return; } =20 + /* + * Control block not mapped. The guest must not unmask an + * event until the control block is initialized, so we can + * just drop the event. + */ + if ( unlikely(!v->evtchn_fifo->control_block) ) + { + printk(XENLOG_G_WARNING + "%pv has no FIFO event channel control block\n", v); + return; + } + /* * Lock all queues related to the event channel (in case of a queue ch= ange * this might be two). @@ -233,25 +245,8 @@ static void evtchn_fifo_set_pending(struct vcpu *v, st= ruct evtchn *evtchn) * Link the event if it unmasked and not already linked. */ if ( !guest_test_bit(d, EVTCHN_FIFO_MASKED, word) && - !guest_test_bit(d, EVTCHN_FIFO_LINKED, word) ) + !guest_test_and_set_bit(d, EVTCHN_FIFO_LINKED, word) ) { - event_word_t *tail_word; - - /* - * Control block not mapped. The guest must not unmask an - * event until the control block is initialized, so we can - * just drop the event. - */ - if ( unlikely(!v->evtchn_fifo->control_block) ) - { - printk(XENLOG_G_WARNING - "%pv has no FIFO event channel control block\n", v); - goto unlock; - } - - if ( guest_test_and_set_bit(d, EVTCHN_FIFO_LINKED, word) ) - goto unlock; - /* * If this event was a tail, the old queue is now empty and * its tail must be invalidated to prevent adding an event to @@ -286,6 +281,8 @@ static void evtchn_fifo_set_pending(struct vcpu *v, str= uct evtchn *evtchn) linked =3D false; if ( q->tail ) { + event_word_t *tail_word; + tail_word =3D evtchn_fifo_word_from_port(d, q->tail); linked =3D evtchn_fifo_set_link(d, tail_word, port); } @@ -294,7 +291,6 @@ static void evtchn_fifo_set_pending(struct vcpu *v, str= uct evtchn *evtchn) q->tail =3D port; } =20 - unlock: if ( q !=3D old_q ) spin_unlock(&old_q->lock); spin_unlock_irqrestore(&q->lock, flags); --=20 2.26.2