Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp2038113ybi; Sun, 16 Jun 2019 20:09:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqwtGo/H0xhF88anlzbEsyDFom9mEN6MdZYsOM344Iygj53dlQh7/5E8MRNGcbChwC8mpTWg X-Received: by 2002:a63:1d4:: with SMTP id 203mr21286795pgb.420.1560740992625; Sun, 16 Jun 2019 20:09:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560740992; cv=none; d=google.com; s=arc-20160816; b=KoKcJP8hzm1t7BaK7hEZyjgtlUuHFzjitjya5gpVsUEOSN1/gCySdKzblsiSQFfDPk wR896evVRvLH3Nm1PC5gfcaAEjZb7XW2z/HNqsudzLaDH/02/0hiMOvnnw4adTJr364N D4mlkn0TZBYbU1agILFR+Hr8s1rAjIx+nngXjtLF1gpSa3GvVHhIcoCOA6wsFkpC7s9E IZR0mk9hW2HzSUwfaBss5HBxtD61uKZXUAyJv8vMF8qZhhLqvZXGMkrJyqkIQ3CER3OA 7U2rlz/IW3Z+O8d2ALoPLRmSULDt/MppHiMUyntDhg2pM+Rpp8URkSAgOG3mHL2TAIFT GrSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:to:from:cc:in-reply-to:subject:date; bh=oUh6hfLSAJw1NhYrQppNGg6oKv5nxfUTJWLmGY+P1as=; b=wBfdbIDpv1X2P3kyArR9vwMQ/YuQujXa/zm+Bgx9bR6bnnQb3KH7yh1p6mH/2Cb3zl Ve7FDzqSEnAqESrzZhruiYXpFjKizv08wGF9OzdohnMZItthKWfOjHZQR/UMyBqbJ0qK QwqgSto4yTEAZWsoBOkcRRAyNWAFgteNKE58ixes5J0PjFxQfog1lb2eGFo627zPSdrG f4dZrr5dXki13cCXQn9d4QGLjXWz7TjzP0i3zQrUOYZdl27QKlqteUWEmbuVhZ0phNL1 Bw6j22ep8eUdR2EA0IhRqYsS1LZ7NLySJ1RQSGHEalQk9cF/Nej5++UpiMp1noVVoAy/ UvqQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g31si9046431plb.241.2019.06.16.20.09.37; Sun, 16 Jun 2019 20:09:52 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727535AbfFQDJa (ORCPT + 99 others); Sun, 16 Jun 2019 23:09:30 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:35340 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727413AbfFQDJa (ORCPT ); Sun, 16 Jun 2019 23:09:30 -0400 Received: by mail-pl1-f194.google.com with SMTP id p1so3467925plo.2 for ; Sun, 16 Jun 2019 20:09:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:subject:in-reply-to:cc:from:to:message-id :mime-version:content-transfer-encoding; bh=oUh6hfLSAJw1NhYrQppNGg6oKv5nxfUTJWLmGY+P1as=; b=U5L6Wlos7LggIg1TVyreoxJiIq3d0mjUjaVdRwFpNI4Dh9OS2keNYwYEmv9kXSm/LJ JLdw0EEODa8FCVDj0k8b5DfgKJTgkShimjPFAcuFyZ8b8RqrQH3FdeUhRX2e6QLxv+1O B4PjRallQM92DHH0CZVu69rf7RhvooMN5Sr2jLAtRiwWpW2S2HxrdMNVeuoxIlECoaXt eCTLGZSIGvvbbo7ryENuykI+B+WhbtBSeEa9hy6zYWcn4YZXsKsD0x+RdhXhLcSYUhHa JNzTgJJ+tMahVOT4In2BuUfv4C+fIzMyButuYckDR8df1xSwGwKiPW5O9mNXJjbq7s1I EgrA== X-Gm-Message-State: APjAAAWan8ml7rmHf9td7BCmgt1oe5F9RGaUc+6T88ReO3t2akTYeDOL UwSNvhh4bT9IBeq0pG2v+P8cQA== X-Received: by 2002:a17:902:6b44:: with SMTP id g4mr12630512plt.152.1560740969406; Sun, 16 Jun 2019 20:09:29 -0700 (PDT) Received: from localhost ([14.215.134.187]) by smtp.gmail.com with ESMTPSA id q7sm11042023pfb.32.2019.06.16.20.09.28 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Sun, 16 Jun 2019 20:09:28 -0700 (PDT) Date: Sun, 16 Jun 2019 20:09:28 -0700 (PDT) X-Google-Original-Date: Sun, 16 Jun 2019 20:08:25 PDT (-0700) Subject: Re: [PATCH v2] RISC-V: Break load reservations during switch_to In-Reply-To: <20190616175405.GF61734@hippo.sing.id.au> CC: linux-riscv@lists.infradead.org, Paul Walmsley , marco@decred.org, me@carlosedp.com, linux-kernel@vger.kernel.org From: Palmer Dabbelt To: joel@sing.id.au Message-ID: Mime-Version: 1.0 (MHng) Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, 16 Jun 2019 10:54:06 PDT (-0700), joel@sing.id.au wrote: > On 19-06-07 15:22:22, Palmer Dabbelt wrote: >> The comment describes why in detail. This was found because QEMU never >> gives up load reservations, the issue is unlikely to manifest on real >> hardware. > > Makes sense, however it obviously will not help until qemu actually > clears load reservations on SC (or otherwise handles the interleaved > SC case). Ya, I wasn't paying close enough attention. I think this should do it diff --git a/target/riscv/insn_trans/trans_rva.inc.c b/target/riscv/insn_trans/trans_rva.inc.c index f6dbbc065e15..001a68ced005 100644 --- a/target/riscv/insn_trans/trans_rva.inc.c +++ b/target/riscv/insn_trans/trans_rva.inc.c @@ -69,6 +69,7 @@ static inline bool gen_sc(DisasContext *ctx, arg_atomic *a, TCGMemOp mop) gen_set_gpr(a->rd, dat); gen_set_label(l2); + tcg_gen_movi_tl(load_res, -1); tcg_temp_free(dat); tcg_temp_free(src1); tcg_temp_free(src2); I'll send the patch out to the QEMU mailing list. > See comment inline. > >> Thanks to Carlos Eduardo for finding the bug! >> >> Signed-off-by: Palmer Dabbelt >> --- >> Changes since v1 <20190605231735.26581-1-palmer@sifive.com>: >> >> * REG_SC is now defined as a helper macro, for any code that wants to SC >> a register-sized value. >> * The explicit #ifdef to check that TASK_THREAD_RA_RA is 0 has been >> removed. Instead we rely on the assembler to catch non-zero SC >> offsets. I've tested this does actually work. >> >> arch/riscv/include/asm/asm.h | 1 + >> arch/riscv/kernel/entry.S | 11 +++++++++++ >> 2 files changed, 12 insertions(+) >> >> diff --git a/arch/riscv/include/asm/asm.h b/arch/riscv/include/asm/asm.h >> index 5ad4cb622bed..946b671f996c 100644 >> --- a/arch/riscv/include/asm/asm.h >> +++ b/arch/riscv/include/asm/asm.h >> @@ -30,6 +30,7 @@ >> >> #define REG_L __REG_SEL(ld, lw) >> #define REG_S __REG_SEL(sd, sw) >> +#define REG_SC __REG_SEL(sc.w, sc.d) > > The instructions appear to be inverted here (i.e. "sc.d, sc.w"). Thanks, I'll send a v3. >> #define SZREG __REG_SEL(8, 4) >> #define LGREG __REG_SEL(3, 2) >> >> diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S >> index 1c1ecc238cfa..af685782af17 100644 >> --- a/arch/riscv/kernel/entry.S >> +++ b/arch/riscv/kernel/entry.S >> @@ -330,6 +330,17 @@ ENTRY(__switch_to) >> add a3, a0, a4 >> add a4, a1, a4 >> REG_S ra, TASK_THREAD_RA_RA(a3) >> + /* >> + * The Linux ABI allows programs to depend on load reservations being >> + * broken on context switches, but the ISA doesn't require that the >> + * hardware ever breaks a load reservation. The only way to break a >> + * load reservation is with a store conditional, so we emit one here. >> + * Since nothing ever takes a load reservation on TASK_THREAD_RA_RA we >> + * know this will always fail, but just to be on the safe side this >> + * writes the same value that was unconditionally written by the >> + * previous instruction. >> + */ >> + REG_SC x0, ra, TASK_THREAD_RA_RA(a3) >> REG_S sp, TASK_THREAD_SP_RA(a3) >> REG_S s0, TASK_THREAD_S0_RA(a3) >> REG_S s1, TASK_THREAD_S1_RA(a3) >> -- >> 2.21.0 >>