From patchwork Sun Jan 28 04:41:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 767223 Delivered-To: patch@linaro.org Received: by 2002:adf:ee41:0:b0:33a:e5bd:fedd with SMTP id w1csp348119wro; Sat, 27 Jan 2024 20:45:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IHQKzp0D4QKbhQcsGRzx9rPiuMXXjagy+40WkYg4BPnDIO0OQJr5SrjVCNL+ddhDM0lE3Ma X-Received: by 2002:a05:620a:34e:b0:783:bd38:aca2 with SMTP id t14-20020a05620a034e00b00783bd38aca2mr2454005qkm.103.1706417147245; Sat, 27 Jan 2024 20:45:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1706417147; cv=none; d=google.com; s=arc-20160816; b=P3QAP6TMCAe9pm09YoVLMv6p8psRGNuKfwm4Q6maqU8Om2e18ylZNojFNx6vYHeN1T CARKpRvlPh+i4Ndklshox79OdWDTY1QopW6/NpAw/cY90pvRJlEFySjgYOrI6socunrF vhsBbV5R5guf89H4MxSSZxCB08xJi+U8QtvjkVWCbffKOCxNsb7GdvRn9yUOrYJ+iDhW rbjmZBx8lDy4WfUxgAM2EPn783/+2G5qMtyrulD4FXUvm0E+tjljPz75fcZF12GRnysa 2lRDi9qMySS8mBkXUYfXg6zZc2G25bjmy4zOIvhlnrypTHHt3xerNDaSMk8Gp72oxsxK Q2Qg== 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=qYS/soxWe3h+ML5uFbbfcdAcoE+eOI+G62ajUSGtp3A=; fh=A9DkFQDNW1+J681dY8xUFFh1hZg0HqQnIWvE/IjOhMA=; b=T6A+4edsAJ7BRGfgWdbTfHYzVRgGfq9tMsABOHvTUPtgKy7L+gmhvjod5ONrOQgUxT 71acS5FrdwhM/ghNpxVKwRX7s4MiEChyT27lgwNPJ646MXWDzKoCOUaJtEzoMIrrAL1s wgqAdCuhq9LBkNF14DISgAWzqkOfQheuIHyDngidHvp7BKqsJ8wYnQancr//ZmNFMpiB 8Cm/Ld95vpHoG/edDlfNTovo2r0mZtitbXd8U1Wvv0+LVVN/7T3vAIWLKpf3nLoJibqL oug27pbDvhfzfDiwAI2F4HQmciBjrAh3JYXo++txbsbbj+dx30wwwIdFebPBaVg58Wlj M4cQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NV03rp4b; 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 t29-20020a05620a035d00b00783baf0a38fsi4926484qkm.744.2024.01.27.20.45.47 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 27 Jan 2024 20:45:47 -0800 (PST) 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=NV03rp4b; 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 1rTwzy-0004dm-J7; Sat, 27 Jan 2024 23:42:26 -0500 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 1rTwzx-0004de-CD for qemu-devel@nongnu.org; Sat, 27 Jan 2024 23:42:25 -0500 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rTwzv-0004FP-CV for qemu-devel@nongnu.org; Sat, 27 Jan 2024 23:42:25 -0500 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1d8a66a2976so16568165ad.2 for ; Sat, 27 Jan 2024 20:42:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1706416942; x=1707021742; darn=nongnu.org; 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=qYS/soxWe3h+ML5uFbbfcdAcoE+eOI+G62ajUSGtp3A=; b=NV03rp4b5mca3d5+wtx2dlG+sEm8M5fPIhm0xpVxRB2sT0bV1gHbxeS1Kg1HUMamv4 KmzLSZQ2TSwcN8cPt13X3hpUTAjLJgG5DKMQOlkg/9nIaV7ozNoLmW3PGPWdyU+3shKW VoHTbxYISyx6yukFYNynYfpcTpMGkm9Iule9rfdS0bk+X3Hu/TaFH4UqFwWkZESXcUBr YbxMfGffC3f6grFvu7qHxbFKXVhDNWuZG4zODVWfQ9fRGv3CGILQX651QOuQyMz5lXKP HPFhxD6yI7D5VFbECD64Ond0r9/G0J72KOtq8saRUVsMju7Nbh9Z40M27NDT+ts0mVEZ XQdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706416942; x=1707021742; 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=qYS/soxWe3h+ML5uFbbfcdAcoE+eOI+G62ajUSGtp3A=; b=HB/e4ZDDITxowWQGGaBPmk31/mUOUBJW93Dn5u3nQkug679cjUzAYWatzeIUh1O8iL Jxu/OIeAKCM2Ptc+QoXBxZqdi3eWanP+k23/3qHepHvhNT2LIsGD9VLtRneZz0miqYIi ryaPRl+Ds6LHAOoxJ1F06PvyBUKVAoScjnfWMyL+uOUYxJzT23DJoYbgiMk6ewVAVCJR el4LPtDrdKdHG14RRyEZexLnOccegx0BXDHy8tXDmU07Uct3A3+WBt+HqBD1Z+YDVRVE 02CYU9XGfAgmedlukWU5Gf10Y0KVy93F0lQsVhjynJa6tLWuyh102S03zfw1qNjhODzD pGmg== X-Gm-Message-State: AOJu0YzF3nPV5rEoQi7JuwpvPXfH2DZr6BXWBsKvyUr9BS+TxdfY8s+Z 6Oa28IZGQrB9zxJaRVLlfDRHY0PovXnWohVq+2+kF1o1sKN//BmVzwds1rZrLD8mvL3O5zPNxXo iUdA= X-Received: by 2002:a17:902:e84b:b0:1d8:d71b:8801 with SMTP id t11-20020a170902e84b00b001d8d71b8801mr119288plg.54.1706416941824; Sat, 27 Jan 2024 20:42:21 -0800 (PST) Received: from stoup.. (ppp121-45-131-89.bri-pow-que-bras31.tpg.internode.on.net. [121.45.131.89]) by smtp.gmail.com with ESMTPSA id w24-20020a17090aaf9800b002906e09e1d1sm5631873pjq.18.2024.01.27.20.42.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Jan 2024 20:42:21 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Paolo Bonzini , =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PATCH 01/33] cpu-exec: simplify jump cache management Date: Sun, 28 Jan 2024 14:41:41 +1000 Message-Id: <20240128044213.316480-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240128044213.316480-1-richard.henderson@linaro.org> References: <20240128044213.316480-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::636; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x636.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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: Paolo Bonzini Unless I'm missing something egregious, the jmp cache is only every populated with a valid entry by the same thread that reads the cache. Therefore, the contents of any valid entry are always consistent and there is no need for any acquire/release magic. Indeed ->tb has to be accessed with atomics, because concurrent invalidations would otherwise cause data races. But ->pc is only ever accessed by one thread, and accesses to ->tb and ->pc within tb_lookup can never race with another tb_lookup. While the TranslationBlock (especially the flags) could be modified by a concurrent invalidation, store-release and load-acquire operations on the cache entry would not add any additional ordering beyond what you get from performing the accesses within a single thread. Because of this, there is really nothing to win in splitting the CF_PCREL and !CF_PCREL paths. It is easier to just always use the ->pc field in the jump cache. I noticed this while working on splitting commit 8ed558ec0cb ("accel/tcg: Introduce TARGET_TB_PCREL", 2022-10-04) into multiple pieces, for the sake of finding a more fine-grained bisection result for https://gitlab.com/qemu-project/qemu/-/issues/2092. It does not (and does not intend to) fix that issue; therefore it may make sense to not commit it until the root cause of issue #2092 is found. Signed-off-by: Paolo Bonzini Tested-by: Alex Bennée Reviewed-by: Alex Bennée Reviewed-by: Richard Henderson Message-Id: <20240122153409.351959-1-pbonzini@redhat.com> Signed-off-by: Richard Henderson --- accel/tcg/tb-jmp-cache.h | 8 +++-- accel/tcg/cpu-exec.c | 66 ++++++++++++++-------------------------- 2 files changed, 28 insertions(+), 46 deletions(-) diff --git a/accel/tcg/tb-jmp-cache.h b/accel/tcg/tb-jmp-cache.h index bb424c8a05..4ab8553afc 100644 --- a/accel/tcg/tb-jmp-cache.h +++ b/accel/tcg/tb-jmp-cache.h @@ -13,9 +13,11 @@ #define TB_JMP_CACHE_SIZE (1 << TB_JMP_CACHE_BITS) /* - * Accessed in parallel; all accesses to 'tb' must be atomic. - * For CF_PCREL, accesses to 'pc' must be protected by a - * load_acquire/store_release to 'tb'. + * Invalidated in parallel; all accesses to 'tb' must be atomic. + * A valid entry is read/written by a single CPU, therefore there is + * no need for qatomic_rcu_read() and pc is always consistent with a + * non-NULL value of 'tb'. Strictly speaking pc is only needed for + * CF_PCREL, but it's used always for simplicity. */ struct CPUJumpCache { struct rcu_head rcu; diff --git a/accel/tcg/cpu-exec.c b/accel/tcg/cpu-exec.c index 67eda9865e..40c268bfa1 100644 --- a/accel/tcg/cpu-exec.c +++ b/accel/tcg/cpu-exec.c @@ -253,43 +253,29 @@ static inline TranslationBlock *tb_lookup(CPUState *cpu, vaddr pc, hash = tb_jmp_cache_hash_func(pc); jc = cpu->tb_jmp_cache; - if (cflags & CF_PCREL) { - /* Use acquire to ensure current load of pc from jc. */ - tb = qatomic_load_acquire(&jc->array[hash].tb); - - if (likely(tb && - jc->array[hash].pc == pc && - tb->cs_base == cs_base && - tb->flags == flags && - tb_cflags(tb) == cflags)) { - return tb; - } - tb = tb_htable_lookup(cpu, pc, cs_base, flags, cflags); - if (tb == NULL) { - return NULL; - } - jc->array[hash].pc = pc; - /* Ensure pc is written first. */ - qatomic_store_release(&jc->array[hash].tb, tb); - } else { - /* Use rcu_read to ensure current load of pc from *tb. */ - tb = qatomic_rcu_read(&jc->array[hash].tb); - - if (likely(tb && - tb->pc == pc && - tb->cs_base == cs_base && - tb->flags == flags && - tb_cflags(tb) == cflags)) { - return tb; - } - tb = tb_htable_lookup(cpu, pc, cs_base, flags, cflags); - if (tb == NULL) { - return NULL; - } - /* Use the pc value already stored in tb->pc. */ - qatomic_set(&jc->array[hash].tb, tb); + tb = qatomic_read(&jc->array[hash].tb); + if (likely(tb && + jc->array[hash].pc == pc && + tb->cs_base == cs_base && + tb->flags == flags && + tb_cflags(tb) == cflags)) { + goto hit; } + tb = tb_htable_lookup(cpu, pc, cs_base, flags, cflags); + if (tb == NULL) { + return NULL; + } + + jc->array[hash].pc = pc; + qatomic_set(&jc->array[hash].tb, tb); + +hit: + /* + * As long as tb is not NULL, the contents are consistent. Therefore, + * the virtual PC has to match for non-CF_PCREL translations. + */ + assert((tb_cflags(tb) & CF_PCREL) || tb->pc == pc); return tb; } @@ -1012,14 +998,8 @@ cpu_exec_loop(CPUState *cpu, SyncClocks *sc) */ h = tb_jmp_cache_hash_func(pc); jc = cpu->tb_jmp_cache; - if (cflags & CF_PCREL) { - jc->array[h].pc = pc; - /* Ensure pc is written first. */ - qatomic_store_release(&jc->array[h].tb, tb); - } else { - /* Use the pc value already stored in tb->pc. */ - qatomic_set(&jc->array[h].tb, tb); - } + jc->array[h].pc = pc; + qatomic_set(&jc->array[h].tb, tb); } #ifndef CONFIG_USER_ONLY