From nobody Tue Feb 10 03:44:41 2026 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=1611735438; cv=none; d=zohomail.com; s=zohoarc; b=lb3cyog2P6XWco7uwDUeDvIjEi0DH2jTJsSH8qGxpVoMqAzzbwuPCOOWaMijo4NlBNpe1FaT4v7IHfb5sW4++nj3jsCT4jeR5DkK85mNV4hsLdgCxtTiXSax1cMzIPuA8SXaWvQuHQzop08SydmK1ewH7QttLpdZOAkVKpH9v1A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1611735438; 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; bh=gEGTEYpdjKYg3S3Ltj5tTKIk5+nFXk5YGvD0lOGX3do=; b=IkVBhpJyxcu+tLDgV0Yt0G8jcSB3TRfivXJ6atPoGUFuPOCiY0tHWo3b6u4PpdLAtWX7Ze6sdSVLDIAaEp1uFuWRGaHtdGrgiahNyf5oqLINaI4iWFzE6TVRJzVs/xRN3lVy2yqlvzXXYKtvFTrEIduUyDq5Ve9er2VtypWT0kE= 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 1611735438012829.7471959605595; Wed, 27 Jan 2021 00:17:18 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.75804.136584 (Exim 4.92) (envelope-from ) id 1l4g0e-0005rb-2K; Wed, 27 Jan 2021 08:17:04 +0000 Received: by outflank-mailman (output) from mailman id 75804.136584; Wed, 27 Jan 2021 08:17:04 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l4g0d-0005rU-V3; Wed, 27 Jan 2021 08:17:03 +0000 Received: by outflank-mailman (input) for mailman id 75804; Wed, 27 Jan 2021 08:17:02 +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 1l4g0c-0005rK-Mj for xen-devel@lists.xenproject.org; Wed, 27 Jan 2021 08:17:02 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 73088352-a3cb-4101-97d0-defbae38bcdc; Wed, 27 Jan 2021 08:17:00 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id D6C8AAF17; Wed, 27 Jan 2021 08:16:59 +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: 73088352-a3cb-4101-97d0-defbae38bcdc 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=1611735420; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gEGTEYpdjKYg3S3Ltj5tTKIk5+nFXk5YGvD0lOGX3do=; b=dM4GiI1fRZ4aTkYEKrCcaEnsfHx7/mSrbluDCPLEJH8zn/go4wVrf6mxeiGbkNODIUEkm8 /AKTS1oPdNPA39tJkwdqRbmPI2nyLvwTsGEh5+HpXD+MBoWYh8BjjMI9iBjRsJPxytTOK5 ihUgX1lgScKG0ojFxNb54k9M8D5wJoo= Subject: [PATCH v5 4/6] evtchn: add helper for port_is_valid() + evtchn_from_port() From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , George Dunlap , Ian Jackson , Julien Grall , Wei Liu , Stefano Stabellini References: <306e62e8-9070-2db9-c959-858465c50c1d@suse.com> Message-ID: Date: Wed, 27 Jan 2021 09:16:59 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: <306e62e8-9070-2db9-c959-858465c50c1d@suse.com> Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" The combination is pretty common, so adding a simple local helper seems worthwhile. Make it const- and type-correct, in turn requiring the two called function to also be const-correct (and at this occasion also make them type-correct). Signed-off-by: Jan Beulich Acked-by: Julien Grall --- v4: New. --- a/xen/common/event_channel.c +++ b/xen/common/event_channel.c @@ -147,6 +147,11 @@ static bool virq_is_global(unsigned int return true; } =20 +static struct evtchn *_evtchn_from_port(const struct domain *d, + evtchn_port_t port) +{ + return port_is_valid(d, port) ? evtchn_from_port(d, port) : NULL; +} =20 static struct evtchn *alloc_evtchn_bucket(struct domain *d, unsigned int p= ort) { @@ -369,9 +374,9 @@ static long evtchn_bind_interdomain(evtc ERROR_EXIT(lport); lchn =3D evtchn_from_port(ld, lport); =20 - if ( !port_is_valid(rd, rport) ) + rchn =3D _evtchn_from_port(rd, rport); + if ( !rchn ) ERROR_EXIT_DOM(-EINVAL, rd); - rchn =3D evtchn_from_port(rd, rport); if ( (rchn->state !=3D ECS_UNBOUND) || (rchn->u.unbound.remote_domid !=3D ld->domain_id) ) ERROR_EXIT_DOM(-EINVAL, rd); @@ -606,15 +611,12 @@ static long evtchn_bind_pirq(evtchn_bind int evtchn_close(struct domain *d1, int port1, bool guest) { struct domain *d2 =3D NULL; - struct evtchn *chn1, *chn2; - int port2; + struct evtchn *chn1 =3D _evtchn_from_port(d1, port1), *chn2; long rc =3D 0; =20 - if ( !port_is_valid(d1, port1) ) + if ( !chn1 ) return -EINVAL; =20 - chn1 =3D evtchn_from_port(d1, port1); - again: write_lock(&d1->event_lock); =20 @@ -700,10 +702,8 @@ int evtchn_close(struct domain *d1, int goto out; } =20 - port2 =3D chn1->u.interdomain.remote_port; - BUG_ON(!port_is_valid(d2, port2)); - - chn2 =3D evtchn_from_port(d2, port2); + chn2 =3D _evtchn_from_port(d2, chn1->u.interdomain.remote_port); + BUG_ON(!chn2); BUG_ON(chn2->state !=3D ECS_INTERDOMAIN); BUG_ON(chn2->u.interdomain.remote_dom !=3D d1); =20 @@ -741,15 +741,13 @@ int evtchn_close(struct domain *d1, int =20 int evtchn_send(struct domain *ld, unsigned int lport) { - struct evtchn *lchn, *rchn; + struct evtchn *lchn =3D _evtchn_from_port(ld, lport), *rchn; struct domain *rd; int rport, ret =3D 0; =20 - if ( !port_is_valid(ld, lport) ) + if ( !lchn ) return -EINVAL; =20 - lchn =3D evtchn_from_port(ld, lport); - evtchn_read_lock(lchn); =20 /* Guest cannot send via a Xen-attached event channel. */ @@ -961,7 +959,6 @@ int evtchn_status(evtchn_status_t *statu { struct domain *d; domid_t dom =3D status->dom; - int port =3D status->port; struct evtchn *chn; long rc =3D 0; =20 @@ -969,14 +966,13 @@ int evtchn_status(evtchn_status_t *statu if ( d =3D=3D NULL ) return -ESRCH; =20 - if ( !port_is_valid(d, port) ) + chn =3D _evtchn_from_port(d, status->port); + if ( !chn ) { rcu_unlock_domain(d); return -EINVAL; } =20 - chn =3D evtchn_from_port(d, port); - evtchn_read_lock(chn); =20 if ( consumer_is_xen(chn) ) @@ -1041,11 +1037,10 @@ long evtchn_bind_vcpu(unsigned int port, if ( (v =3D domain_vcpu(d, vcpu_id)) =3D=3D NULL ) return -ENOENT; =20 - if ( !port_is_valid(d, port) ) + chn =3D _evtchn_from_port(d, port); + if ( !chn ) return -EINVAL; =20 - chn =3D evtchn_from_port(d, port); - write_lock(&d->event_lock); =20 /* Guest cannot re-bind a Xen-attached event channel. */ @@ -1091,13 +1086,11 @@ long evtchn_bind_vcpu(unsigned int port, int evtchn_unmask(unsigned int port) { struct domain *d =3D current->domain; - struct evtchn *evtchn; + struct evtchn *evtchn =3D _evtchn_from_port(d, port); =20 - if ( unlikely(!port_is_valid(d, port)) ) + if ( unlikely(!evtchn) ) return -EINVAL; =20 - evtchn =3D evtchn_from_port(d, port); - evtchn_read_lock(evtchn); =20 evtchn_port_unmask(d, evtchn); @@ -1180,14 +1173,12 @@ static long evtchn_set_priority(const st { struct domain *d =3D current->domain; unsigned int port =3D set_priority->port; - struct evtchn *chn; + struct evtchn *chn =3D _evtchn_from_port(d, port); long ret; =20 - if ( !port_is_valid(d, port) ) + if ( !chn ) return -EINVAL; =20 - chn =3D evtchn_from_port(d, port); - evtchn_read_lock(chn); =20 ret =3D evtchn_port_set_priority(d, chn, set_priority->priority); @@ -1413,10 +1404,10 @@ void free_xen_event_channel(struct domai =20 void notify_via_xen_event_channel(struct domain *ld, int lport) { - struct evtchn *lchn, *rchn; + struct evtchn *lchn =3D _evtchn_from_port(ld, lport), *rchn; struct domain *rd; =20 - if ( !port_is_valid(ld, lport) ) + if ( !lchn ) { /* * Make sure ->is_dying is read /after/ ->valid_evtchns, pairing @@ -1427,8 +1418,6 @@ void notify_via_xen_event_channel(struct return; } =20 - lchn =3D evtchn_from_port(ld, lport); - if ( !evtchn_read_trylock(lchn) ) return; =20 @@ -1582,16 +1571,17 @@ static void domain_dump_evtchn_info(stru "Polling vCPUs: {%*pbl}\n" " port [p/m/s]\n", d->domain_id, d->max_vcpus, d->poll_mask); =20 - for ( port =3D 1; port_is_valid(d, port); ++port ) + for ( port =3D 1; ; ++port ) { - struct evtchn *chn; + struct evtchn *chn =3D _evtchn_from_port(d, port); char *ssid; =20 + if ( !chn ) + break; + if ( !(port & 0x3f) ) process_pending_softirqs(); =20 - chn =3D evtchn_from_port(d, port); - if ( !evtchn_read_trylock(chn) ) { printk(" %4u in flux\n", port); --- a/xen/include/xen/event.h +++ b/xen/include/xen/event.h @@ -120,7 +120,7 @@ static inline void evtchn_read_unlock(st read_unlock(&evtchn->lock); } =20 -static inline bool_t port_is_valid(struct domain *d, unsigned int p) +static inline bool port_is_valid(const struct domain *d, evtchn_port_t p) { if ( p >=3D read_atomic(&d->valid_evtchns) ) return false; @@ -135,7 +135,8 @@ static inline bool_t port_is_valid(struc return true; } =20 -static inline struct evtchn *evtchn_from_port(struct domain *d, unsigned i= nt p) +static inline struct evtchn *evtchn_from_port(const struct domain *d, + evtchn_port_t p) { if ( p < EVTCHNS_PER_BUCKET ) return &d->evtchn[array_index_nospec(p, EVTCHNS_PER_BUCKET)];