From patchwork Mon Aug 15 21:15:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoffer Dall X-Patchwork-Id: 73929 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp1645347qga; Mon, 15 Aug 2016 14:15:13 -0700 (PDT) X-Received: by 10.66.132.38 with SMTP id or6mr57344889pab.84.1471295713000; Mon, 15 Aug 2016 14:15:13 -0700 (PDT) Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id p28si28307406pfk.183.2016.08.15.14.15.12 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 15 Aug 2016 14:15:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) client-ip=2001:1868:205::9; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) smtp.mailfrom=linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1bZPD0-00055Y-2N; Mon, 15 Aug 2016 21:14:10 +0000 Received: from mail-wm0-x22f.google.com ([2a00:1450:400c:c09::22f]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1bZPCu-0004uA-Eh for linux-arm-kernel@lists.infradead.org; Mon, 15 Aug 2016 21:14:06 +0000 Received: by mail-wm0-x22f.google.com with SMTP id o80so127604683wme.1 for ; Mon, 15 Aug 2016 14:13:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=wniC1EO6O2tD2gV8+NG0uYuQO9m4zRnyEz3Clo4AJuk=; b=L1ebZlyYSAVH4TgXI7K0vudZHexqKQzQ72QgOELUfg7lCtgUvGyalcH7LP4etbGzIP b3pCsQjT2Gct6QZNTUedqpzvcjRgvD4Gp+jealpPc4zWlYGzJk56cuyPEka3aRX+UZVU WI1k9gwmtTNWJZA1E69MiFP46euNsvkQ6Ln5s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=wniC1EO6O2tD2gV8+NG0uYuQO9m4zRnyEz3Clo4AJuk=; b=Wd/GMqfs0TlkSJhxKs/pGVL9JBnEhenP83+KdJ5ZrZo6/2fGr8x99nu72nyXhW7ktE sV/J5jYg4VaVcxOZaS+BSB2O4j0hNB2eVusIvRkeHLxImuaZ2RXf8ELdwahBp4PYSR1d KQKMMu7/u7z0ebw6ax+4BsLeq4HVvLhSxLDXB1gYtpmWgsd4zR9i2A3zanIwPDrfDLQ0 fNqc1AcwTQ5kt2ANq97OCgAICvFEa2dJe8u8Ufxl4Hhfg40SlGuLAAg0l6cgzGclxSoM WPlpYtO+5r1s7W5fKM7Xs08L2O44FplMpJQK6i5D8KS1rtxg58C9wMT5yzOThKIHHvY7 3MeA== X-Gm-Message-State: AEkoouvznV/IR9C/ZZJ9nins5Xw+w8TEii2/9UjGxXzCsEZR1a+v1SzkHzTk2ZnodMwgGnI4 X-Received: by 10.194.83.72 with SMTP id o8mr2066800wjy.187.1471295622099; Mon, 15 Aug 2016 14:13:42 -0700 (PDT) Received: from localhost.localdomain ([94.18.191.146]) by smtp.gmail.com with ESMTPSA id a9sm23340835wjf.16.2016.08.15.14.13.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Aug 2016 14:13:39 -0700 (PDT) From: Christoffer Dall To: Andre Przywara Subject: [PATCH v3] KVM: arm64: vgic-its: Make updates to propbaser/pendbaser atomic Date: Mon, 15 Aug 2016 23:15:31 +0200 Message-Id: <20160815211531.30350-1-christoffer.dall@linaro.org> X-Mailer: git-send-email 2.9.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160815_141404_710362_73641E39 X-CRM114-Status: GOOD ( 15.09 ) X-Spam-Score: -2.7 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2a00:1450:400c:c09:0:0:0:22f listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , kvm@vger.kernel.org, Eric Auger , Marc Zyngier , Christoffer Dall , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org There are two problems with the current implementation of the MMIO handlers for the propbaser and pendbaser: First, the write to the value itself is not guaranteed to be an atomic 64-bit write so two concurrent writes to the structure field could be intermixed. Second, because we do a read-modify-update operation without any synchronization, if we have two 32-bit accesses to separate parts of the register, we can loose one of them. By using the atomic cmpxchg64 we should cover both issues above. Signed-off-by: Christoffer Dall --- Changes since v2: - I misread the implementation and example uses of cmpxchg64 and checked the return value for non-zero instead of comparing with the old value. - Dropped Andre's R-B Changes since v1: - Use atomic cmpxchg64 instead of taking a lock virt/kvm/arm/vgic/vgic-mmio-v3.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) -- 2.9.0 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel diff --git a/virt/kvm/arm/vgic/vgic-mmio-v3.c b/virt/kvm/arm/vgic/vgic-mmio-v3.c index ff668e0..90d8181 100644 --- a/virt/kvm/arm/vgic/vgic-mmio-v3.c +++ b/virt/kvm/arm/vgic/vgic-mmio-v3.c @@ -306,16 +306,19 @@ static void vgic_mmio_write_propbase(struct kvm_vcpu *vcpu, { struct vgic_dist *dist = &vcpu->kvm->arch.vgic; struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; - u64 propbaser = dist->propbaser; + u64 old_propbaser, propbaser; /* Storing a value with LPIs already enabled is undefined */ if (vgic_cpu->lpis_enabled) return; - propbaser = update_64bit_reg(propbaser, addr & 4, len, val); - propbaser = vgic_sanitise_propbaser(propbaser); - - dist->propbaser = propbaser; + do { + old_propbaser = dist->propbaser; + propbaser = old_propbaser; + propbaser = update_64bit_reg(propbaser, addr & 4, len, val); + propbaser = vgic_sanitise_propbaser(propbaser); + } while (cmpxchg64(&dist->propbaser, old_propbaser, + propbaser) != old_propbaser); } static unsigned long vgic_mmio_read_pendbase(struct kvm_vcpu *vcpu, @@ -331,16 +334,19 @@ static void vgic_mmio_write_pendbase(struct kvm_vcpu *vcpu, unsigned long val) { struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; - u64 pendbaser = vgic_cpu->pendbaser; + u64 old_pendbaser, pendbaser; /* Storing a value with LPIs already enabled is undefined */ if (vgic_cpu->lpis_enabled) return; - pendbaser = update_64bit_reg(pendbaser, addr & 4, len, val); - pendbaser = vgic_sanitise_pendbaser(pendbaser); - - vgic_cpu->pendbaser = pendbaser; + do { + old_pendbaser = vgic_cpu->pendbaser; + pendbaser = old_pendbaser; + pendbaser = update_64bit_reg(pendbaser, addr & 4, len, val); + pendbaser = vgic_sanitise_pendbaser(pendbaser); + } while (cmpxchg64(&vgic_cpu->pendbaser, old_pendbaser, + pendbaser) != old_pendbaser); } /*