From nobody Tue Feb 10 04:15:59 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=1613038688; cv=none; d=zohomail.com; s=zohoarc; b=BZDxapseON0FLdQonHLqkfxE2PDLqITzDapwvwgFvV1+IoN3DD9SduqSn17u2d5Vy2NqrGaLUDbsEN4p+Lnf1T9STIYvK1W82xhA+AH/iHK9jVN7nnnnd9SlY4N6AQs780UYxV3Ef+SldEuISN+hscWjSIdbxYnBQqvB4HQFwBE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1613038688; 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=lYH24KPMu2VG4s1NBLZlXtQTIXh2Tv25DANk+uAGYuo=; b=kPabflopz2xBbUwumIuwNUpn7EZHLyyn3n3tGTq0HQGuTDezh9xBAcLGdekcRpEXVpePBkJ/JjwKtHbJBXhFRtd+vZ4J0cr2FL0JAAIx8t0DSUnpkQdMDrXQdXT4s/4xOsCtmL7Zq3/0q790e3cilmT402uAZBxa/71GSUPdU+Y= 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 1613038688049136.2284750918916; Thu, 11 Feb 2021 02:18:08 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.83798.156835 (Exim 4.92) (envelope-from ) id 1lA92n-0001fJ-5Y; Thu, 11 Feb 2021 10:17:53 +0000 Received: by outflank-mailman (output) from mailman id 83798.156835; Thu, 11 Feb 2021 10:17:53 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lA92n-0001fA-1v; Thu, 11 Feb 2021 10:17:53 +0000 Received: by outflank-mailman (input) for mailman id 83798; Thu, 11 Feb 2021 10:17:51 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lA92l-0001Uu-O2 for xen-devel@lists.xenproject.org; Thu, 11 Feb 2021 10:17:51 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 8cb82252-c671-4eed-821b-f7d2d3151973; Thu, 11 Feb 2021 10:17:42 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 98FF9AF50; Thu, 11 Feb 2021 10:17:41 +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: 8cb82252-c671-4eed-821b-f7d2d3151973 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=1613038661; 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=lYH24KPMu2VG4s1NBLZlXtQTIXh2Tv25DANk+uAGYuo=; b=u1DgoxnpEm4vOpyw48CO18ajkHrDW1uvBzT3RfgWYFijSi0+reRlAnvlKP1gg0r4P9Pi7i Vu5847ApQb2x84mlacvWnCjsZcJwu0yHNhZeZttlgZKVZRpMXgD4XgZOCcNZ5ySrNx83tW UvkSODeyNjTFab6FqCgs6SMINmH3iSc= From: Juergen Gross To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini Subject: [PATCH v2 8/8] xen/evtchn: use READ/WRITE_ONCE() for accessing ring indices Date: Thu, 11 Feb 2021 11:16:16 +0100 Message-Id: <20210211101616.13788-9-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210211101616.13788-1-jgross@suse.com> References: <20210211101616.13788-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" For avoiding read- and write-tearing by the compiler use READ_ONCE() and WRITE_ONCE() for accessing the ring indices in evtchn.c. Signed-off-by: Juergen Gross --- V2: - modify all accesses (Julien Grall) --- drivers/xen/evtchn.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/drivers/xen/evtchn.c b/drivers/xen/evtchn.c index 421382c73d88..620008f89dbe 100644 --- a/drivers/xen/evtchn.c +++ b/drivers/xen/evtchn.c @@ -162,6 +162,7 @@ static irqreturn_t evtchn_interrupt(int irq, void *data) { struct user_evtchn *evtchn =3D data; struct per_user_data *u =3D evtchn->user; + unsigned int prod, cons; =20 WARN(!evtchn->enabled, "Interrupt for port %u, but apparently not enabled; per-user %p\n", @@ -171,10 +172,14 @@ static irqreturn_t evtchn_interrupt(int irq, void *da= ta) =20 spin_lock(&u->ring_prod_lock); =20 - if ((u->ring_prod - u->ring_cons) < u->ring_size) { - *evtchn_ring_entry(u, u->ring_prod) =3D evtchn->port; + prod =3D READ_ONCE(u->ring_prod); + cons =3D READ_ONCE(u->ring_cons); + + if ((prod - cons) < u->ring_size) { + *evtchn_ring_entry(u, prod) =3D evtchn->port; smp_wmb(); /* Ensure ring contents visible */ - if (u->ring_cons =3D=3D u->ring_prod++) { + if (cons =3D=3D prod++) { + WRITE_ONCE(u->ring_prod, prod); wake_up_interruptible(&u->evtchn_wait); kill_fasync(&u->evtchn_async_queue, SIGIO, POLL_IN); @@ -210,8 +215,8 @@ static ssize_t evtchn_read(struct file *file, char __us= er *buf, if (u->ring_overflow) goto unlock_out; =20 - c =3D u->ring_cons; - p =3D u->ring_prod; + c =3D READ_ONCE(u->ring_cons); + p =3D READ_ONCE(u->ring_prod); if (c !=3D p) break; =20 @@ -221,7 +226,7 @@ static ssize_t evtchn_read(struct file *file, char __us= er *buf, return -EAGAIN; =20 rc =3D wait_event_interruptible(u->evtchn_wait, - u->ring_cons !=3D u->ring_prod); + READ_ONCE(u->ring_cons) !=3D READ_ONCE(u->ring_prod)); if (rc) return rc; } @@ -251,7 +256,7 @@ static ssize_t evtchn_read(struct file *file, char __us= er *buf, copy_to_user(&buf[bytes1], &u->ring[0], bytes2))) goto unlock_out; =20 - u->ring_cons +=3D (bytes1 + bytes2) / sizeof(evtchn_port_t); + WRITE_ONCE(u->ring_cons, c + (bytes1 + bytes2) / sizeof(evtchn_port_t)); rc =3D bytes1 + bytes2; =20 unlock_out: @@ -552,7 +557,9 @@ static long evtchn_ioctl(struct file *file, /* Initialise the ring to empty. Clear errors. */ mutex_lock(&u->ring_cons_mutex); spin_lock_irq(&u->ring_prod_lock); - u->ring_cons =3D u->ring_prod =3D u->ring_overflow =3D 0; + WRITE_ONCE(u->ring_cons, 0); + WRITE_ONCE(u->ring_prod, 0); + u->ring_overflow =3D 0; spin_unlock_irq(&u->ring_prod_lock); mutex_unlock(&u->ring_cons_mutex); rc =3D 0; @@ -595,7 +602,7 @@ static __poll_t evtchn_poll(struct file *file, poll_tab= le *wait) struct per_user_data *u =3D file->private_data; =20 poll_wait(file, &u->evtchn_wait, wait); - if (u->ring_cons !=3D u->ring_prod) + if (READ_ONCE(u->ring_cons) !=3D READ_ONCE(u->ring_prod)) mask |=3D EPOLLIN | EPOLLRDNORM; if (u->ring_overflow) mask =3D EPOLLERR; --=20 2.26.2