From patchwork Mon Jul 24 08:53:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 705737 Delivered-To: patch@linaro.org Received: by 2002:a5d:60ca:0:b0:317:2194:b2bc with SMTP id x10csp1664905wrt; Mon, 24 Jul 2023 01:54:02 -0700 (PDT) X-Google-Smtp-Source: APBJJlGK3JCbZ9dSBiBa2XZ/ARYbdn/zCaU5+c56XiITAd1rwQgIxQscL7ufmgkZ/LWaDTnz60V7 X-Received: by 2002:a0c:f593:0:b0:630:1732:7084 with SMTP id k19-20020a0cf593000000b0063017327084mr6426992qvm.61.1690188842747; Mon, 24 Jul 2023 01:54:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690188842; cv=none; d=google.com; s=arc-20160816; b=cxoDMLTrC9sPM5j41uRNZfLbW6HMtXUyH7ZXhg9p7DbDlysy2BYxDfWHfR1LZRoxks KIrvOo6MSEryH3aGKJJk+vCN4ATRIYCJYX8ynNbCjjfpts5ss3NqBJGs+sofosAEgEWu V1BTVT5JlX3Vs50g9Q5pGBWWzu2JomKwH5f/LbXgN3aBDRahuok1h/Ci3gzKitJlGCFn EWnZi0ABsVDyOcxzB52N9/6aMZvt+NCRKva5EaNt0I6vUNvwAcHEmTWtUZOP6QWIOAmp KgAljDxZPcCEYg0OLMhGEBVc0vni4UvVhaNnJMRYHeG5PgpjcfN3vEPrWBPf/ackH2P8 iSww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=o3gKpL8DiepyBHxNbIedU4D/NFgh/WMqJ9tvA2IW734=; fh=R2DFDnjXVhhOWsqShXj56qpqBRa2r/KSSSQq8BCMjd0=; b=Vgdo1BMKUN+Z0Lib4JklarQiBZf4VPqjjStr0QeC/NTyxh2jkIlVtrjU2ev3C7msB8 iAu3KUJz84Yec/ghfvLfl94Clmx8En6Jws7AQb/ksrMVkuDJhpFYQXB59hzJ6o9dUR04 QRD3uu7m6XxHgVqMLnZ13dCVV1qwWf3I2IcprnmiDTcTW6d911J3/VTC8uqXtWTs8YAN 4W4vvzgCdKdsgs8MVT2N7I4dY9PDlvtpxqG9ujhRHIvWUY3+TdIn5LHxaKgXxJG9Qkr/ MZaLJcwfDIfeAm9Mo5NVw49JBWWwJ7Uc6ayJ0z6O2RB7S9nsbXS5iK/ieIKyza+T6u2o GNoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BIuusPkj; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id h25-20020a0cb4d9000000b005eefc559ac9si1430954qvf.401.2023.07.24.01.54.02 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 24 Jul 2023 01:54:02 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BIuusPkj; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qNrJv-000207-Tf; Mon, 24 Jul 2023 04:53:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qNrJt-0001zY-Ub for qemu-devel@nongnu.org; Mon, 24 Jul 2023 04:53:33 -0400 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qNrJs-0000dc-3f for qemu-devel@nongnu.org; Mon, 24 Jul 2023 04:53:33 -0400 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-3fbc0609cd6so30298835e9.1 for ; Mon, 24 Jul 2023 01:53:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1690188811; x=1690793611; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=o3gKpL8DiepyBHxNbIedU4D/NFgh/WMqJ9tvA2IW734=; b=BIuusPkjdmdnR2lEx1QWO7sDaPIqJwwq9K8J7ymCrF5gQfE8MXaA+qTlhv75lOHcbH R5IrPmUacKR0HJdba32KsTaYQVbe4b5rTj1d/RGPQM6P2NQychtAUVJNZhgk0mvvPPtG pPuhjtm0jwXrlevE/J0EFdpZ7zdF5Gn4ggzXjSqPk/MiG64G+PD9KawMQ/05W8T4Zlh0 mFYZdAotDRVGiAj19quUYeeakoJj+5Z1KQ25FfcgYJDDqBzFWZetiv8wwT1dv3jtFIPa UP2Vc775TxUiZzHCWedWAO9EmY/kuE1HbZhuWHUOPfOkt+3LKMaXQw2ey0W20hSAUnj3 ggCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690188811; x=1690793611; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=o3gKpL8DiepyBHxNbIedU4D/NFgh/WMqJ9tvA2IW734=; b=hVkpsRE/Xsh3YcUHW5p+IXoSyOeTNfdmP2o9Lzk/a205reLl4tXzDjIBgRd0uWbFwv pKKFgg8dmKDlhxR4JuTzrD0gIx1mdNwMPGkWIdWhhJmX/0f74V4HpR+S99AexWVffwOO 254TVodb4bZ/lOPtIXyHy2Tt1nDBHsfVrbLDY9HLOsKBfcMp/wi03oLUPJlSg9/lu1xP Hf2/ERdJdNxSA0enZkWYA4Q5Y0A04BoTX/5w8nMOz6ADQKTGUhSIlNgAnnnYQ0OKSzo2 +kFRxOHwCljOK/GK1hnPXsAKLO5g9P4KmhxywwysMhtykHqaIYeMMcM/Xo3GzOJN1xZk Pc0Q== X-Gm-Message-State: ABy/qLbUQIQ/9CuppICMPbmxlXGJqgdaOqWHUGXufyiGn0yig1mILxag bSdOAowtqipLPL5RnHpwMDFbG32iZMK0L1MtriMseQ== X-Received: by 2002:a05:600c:2909:b0:3fc:85c:5ed2 with SMTP id i9-20020a05600c290900b003fc085c5ed2mr5823043wmd.11.1690188810698; Mon, 24 Jul 2023 01:53:30 -0700 (PDT) Received: from stoup.. ([62.252.144.58]) by smtp.gmail.com with ESMTPSA id t8-20020a1c7708000000b003fa74bff02asm9570994wmi.26.2023.07.24.01.53.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jul 2023 01:53:30 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, Jordan Niethe , Anushree Mathur , Michael Tokarev , Benjamin Gray Subject: [PULL 1/7] tcg/ppc: Fix race in goto_tb implementation Date: Mon, 24 Jul 2023 09:53:22 +0100 Message-Id: <20230724085328.4936-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230724085328.4936-1-richard.henderson@linaro.org> References: <20230724085328.4936-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32e; envelope-from=richard.henderson@linaro.org; helo=mail-wm1-x32e.google.com X-Spam_score_int: 12 X-Spam_score: 1.2 X-Spam_bar: + X-Spam_report: (1.2 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Jordan Niethe Commit 20b6643324 ("tcg/ppc: Reorg goto_tb implementation") modified goto_tb to ensure only a single instruction was patched to prevent incorrect behavior if a thread was in the middle of multiple instructions when they were replaced. However this introduced a race between loading the jmp target into TCG_REG_TB and patching and executing the direct branch. The relevant part of the goto_tb implementation: ld TCG_REG_TB, TARGET_ADDR_LOCATION(TCG_REG_TB) patch_location: mtctr TCG_REG_TB bctr tb_target_set_jmp_target() will replace 'patch_location' with a direct branch if the target is in range. The direct branch now relies on TCG_REG_TB being set up correctly by the ld. Prior to this commit multiple instructions were patched in for the direct branch case; these instructions would initialize TCG_REG_TB to the same value as the branch target. Imagine the following sequence: 1) Thread A is executing the goto_tb sequence and loads the jmp target into TCG_REG_TB. 2) Thread B updates the jmp target address and calls tb_target_set_jmp_target(). This patches a new direct branch into the goto_tb sequence. 3) Thread A executes the newly patched direct branch. The value in TCG_REG_TB still contains the old jmp target. TCG_REG_TB MUST contain the translation block's tc.ptr. Execution will eventually crash after performing memory accesses generated from a faulty value in TCG_REG_TB. This presents as segfaults or illegal instruction exceptions. Do not revert commit 20b6643324 as it did fix a different race condition. Instead remove the direct branch optimization and always use indirect branches. The direct branch optimization can be re-added later with a race free sequence. Fixes: 20b6643324 ("tcg/ppc: Reorg goto_tb implementation") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1726 Reported-by: Anushree Mathur Tested-by: Anushree Mathur Tested-by: Michael Tokarev Reviewed-by: Richard Henderson Co-developed-by: Benjamin Gray Signed-off-by: Jordan Niethe Signed-off-by: Benjamin Gray Message-Id: <20230717093001.13167-1-jniethe5@gmail.com> --- tcg/ppc/tcg-target.c.inc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tcg/ppc/tcg-target.c.inc b/tcg/ppc/tcg-target.c.inc index c866f2c997..511e14b180 100644 --- a/tcg/ppc/tcg-target.c.inc +++ b/tcg/ppc/tcg-target.c.inc @@ -2496,11 +2496,10 @@ static void tcg_out_goto_tb(TCGContext *s, int which) ptrdiff_t offset = tcg_tbrel_diff(s, (void *)ptr); tcg_out_mem_long(s, LD, LDX, TCG_REG_TB, TCG_REG_TB, offset); - /* Direct branch will be patched by tb_target_set_jmp_target. */ + /* TODO: Use direct branches when possible. */ set_jmp_insn_offset(s, which); tcg_out32(s, MTSPR | RS(TCG_REG_TB) | CTR); - /* When branch is out of range, fall through to indirect. */ tcg_out32(s, BCCTR | BO_ALWAYS); /* For the unlinked case, need to reset TCG_REG_TB. */ @@ -2528,10 +2527,12 @@ void tb_target_set_jmp_target(const TranslationBlock *tb, int n, intptr_t diff = addr - jmp_rx; tcg_insn_unit insn; + if (USE_REG_TB) { + return; + } + if (in_range_b(diff)) { insn = B | (diff & 0x3fffffc); - } else if (USE_REG_TB) { - insn = MTSPR | RS(TCG_REG_TB) | CTR; } else { insn = NOP; }