Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp8115031pxb; Fri, 19 Feb 2021 07:45:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJxlX6YHrCpmB/HJUylJIUNd+oRxtNs56XCZ3puqx8LkZkrYaj5KLtESJo+b+FVDd3FzXfv7 X-Received: by 2002:a05:6402:35ca:: with SMTP id z10mr9815585edc.186.1613749556084; Fri, 19 Feb 2021 07:45:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613749556; cv=none; d=google.com; s=arc-20160816; b=w9mKmd1/EBhTu/ZrIL0vLdof/lkUct+svfIGTwV9m/NQ50cAEHHYdmbLMvvBPQtlIb UCFUMF5GWaSCNmLOR4uaaHd1nfhUnNVwLTKIcZYUOf7IKYMTWlgd5em5QDQAOv/x6s4i kSU3iv0Pvv6g8kJgidn+7eCFlg1QDhjPQpj7RGlrv/AACMqlW6dVHaoTsOifKgD9xE4B fE/3llkUzBmVdTccGjwQowc+H9nKUSWTj0tWfUKdzNfKzzlNHn/nSLce8Qi9LdoMhQm1 5isIu0lHV7V2l7tqaGBZFAJe+hBlCHtyw5gWoR7qRKssNIS/9YNDw3xYKUSZc0iT2zNW MYkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=dflticAMbc09PwXAGDjeFedypgLhrYxdjLRQqHKEDDU=; b=c1ylZTfFwctKivuImX0H8xRR+6cF/8j7Pg2hrZpPqVkIXtjVucQat80tBq+HyRIrEo vGR/u5w9R9YixZqa7qb79c3RY7ut8504me2sEqmZWKYJwKvsTsnIyuBWyvvzkqbQvf30 LKs75Asx7s5vslvuqXjn21OzuEArFVKHvLL3LNkuT4orKUpopY0poD1RlfECpLBy5x2a enpQcoMysYY3mJLQBRQQs4M3BierAGXk2sVbm/2KBz2zE55TGUcsPdUd1IzMHJwAHHNu aYXRwJDtyeIgo/WMnr/GTzydUyaipxL5g+A6aTNnV135/YF6mTdwALNyHsJUmTdUba1e AG/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b="cAj/RR/N"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h14si5046289ejd.309.2021.02.19.07.45.32; Fri, 19 Feb 2021 07:45:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b="cAj/RR/N"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229796AbhBSPo4 (ORCPT + 99 others); Fri, 19 Feb 2021 10:44:56 -0500 Received: from mx2.suse.de ([195.135.220.15]:48182 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229809AbhBSPmO (ORCPT ); Fri, 19 Feb 2021 10:42:14 -0500 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=1613749239; 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=dflticAMbc09PwXAGDjeFedypgLhrYxdjLRQqHKEDDU=; b=cAj/RR/Np2Vu9mTDqZssOVj4+tOadc3EcJK5OUtUYHgAsHlAkUaM7EnBllZPfqdGIbJiFN EixYe2DRhOopG/yuDiYRwWsU8do5usxDeCMFtoxvVAkcN6xMoINQmWwvZIObrIh6jbFbvN GI/e8gT4izrCIcwyQ6tiv+FG6FYEIsA= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id B65E9B115; Fri, 19 Feb 2021 15:40:39 +0000 (UTC) From: Juergen Gross To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini Subject: [PATCH v3 8/8] xen/evtchn: use READ/WRITE_ONCE() for accessing ring indices Date: Fri, 19 Feb 2021 16:40:30 +0100 Message-Id: <20210219154030.10892-9-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210219154030.10892-1-jgross@suse.com> References: <20210219154030.10892-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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) V3: - fix incrementing producer index (Ross Lagerwall) --- 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..c99415a70051 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 = data; struct per_user_data *u = evtchn->user; + unsigned int prod, cons; 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 *data) spin_lock(&u->ring_prod_lock); - if ((u->ring_prod - u->ring_cons) < u->ring_size) { - *evtchn_ring_entry(u, u->ring_prod) = evtchn->port; + prod = READ_ONCE(u->ring_prod); + cons = READ_ONCE(u->ring_cons); + + if ((prod - cons) < u->ring_size) { + *evtchn_ring_entry(u, prod) = evtchn->port; smp_wmb(); /* Ensure ring contents visible */ - if (u->ring_cons == u->ring_prod++) { + WRITE_ONCE(u->ring_prod, prod + 1); + if (cons == 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 __user *buf, if (u->ring_overflow) goto unlock_out; - c = u->ring_cons; - p = u->ring_prod; + c = READ_ONCE(u->ring_cons); + p = READ_ONCE(u->ring_prod); if (c != p) break; @@ -221,7 +226,7 @@ static ssize_t evtchn_read(struct file *file, char __user *buf, return -EAGAIN; rc = wait_event_interruptible(u->evtchn_wait, - u->ring_cons != u->ring_prod); + READ_ONCE(u->ring_cons) != READ_ONCE(u->ring_prod)); if (rc) return rc; } @@ -251,7 +256,7 @@ static ssize_t evtchn_read(struct file *file, char __user *buf, copy_to_user(&buf[bytes1], &u->ring[0], bytes2))) goto unlock_out; - u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t); + WRITE_ONCE(u->ring_cons, c + (bytes1 + bytes2) / sizeof(evtchn_port_t)); rc = bytes1 + bytes2; 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 = u->ring_prod = u->ring_overflow = 0; + WRITE_ONCE(u->ring_cons, 0); + WRITE_ONCE(u->ring_prod, 0); + u->ring_overflow = 0; spin_unlock_irq(&u->ring_prod_lock); mutex_unlock(&u->ring_cons_mutex); rc = 0; @@ -595,7 +602,7 @@ static __poll_t evtchn_poll(struct file *file, poll_table *wait) struct per_user_data *u = file->private_data; poll_wait(file, &u->evtchn_wait, wait); - if (u->ring_cons != u->ring_prod) + if (READ_ONCE(u->ring_cons) != READ_ONCE(u->ring_prod)) mask |= EPOLLIN | EPOLLRDNORM; if (u->ring_overflow) mask = EPOLLERR; -- 2.26.2