From patchwork Tue Jan 24 18:01:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 646052 Delivered-To: patch@linaro.org Received: by 2002:a17:522:b9de:b0:4b9:b062:db3b with SMTP id fj30csp341904pvb; Tue, 24 Jan 2023 10:08:59 -0800 (PST) X-Google-Smtp-Source: AMrXdXsadj+ow3mgAiNzkHESafufaaQNliPXVYUNq6BUbXX4r9rA9z8SJxPaoJ4dY2iXwT13tsFJ X-Received: by 2002:a05:622a:1b11:b0:3b6:33eb:370b with SMTP id bb17-20020a05622a1b1100b003b633eb370bmr44809592qtb.34.1674583739707; Tue, 24 Jan 2023 10:08:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674583739; cv=none; d=google.com; s=arc-20160816; b=jGL2JiWWXz4di9oQCzB4TwRQRikuI0hlMAO4EaqGSqNz3exovlhmtMVUufcmSkEG+p DtMhqSdUR7bTQYCjkpxoOhglg2gnUCZRnb+lTaYNLOixTG4T3leJhYBcTMaSXHajaT0/ 0NZElFZrVEmWzwwYASlR2IXbKEQceMriPPPKJ4UEA1kTAhZaQ49ZZvyV1aLxov8k/gaI WPnDYDu4/MMPKXPoEe284Ms3b1Js5/gXF0C3C8uKrYlQsPOz61Ps4RejF8nz6XJ3k8NT eiSXEE6iUiNUqa/2rva8J1YS9Hixts7srO8SldsB6Jy8UWVK60SuaQakhnFeqB6kdy55 kInA== 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=Kicu2o7DliK9NIrjxHX2SYsQRxeUGLNQgh1V/I/tfuk=; b=MdAifAT5bNdJV/yM/FFGYn2TeUpfYZSvfBgEyaoN7gTnOdvUS/4tafDEULCxgI4v+W yzhYEx68HJG9QJEqBiB7Wl4nZNSY9jsA/OCige9Shxz5lO1ZUikaqxgE0kC1IAIdGE4C rUR5jbdd3tO2ZUykz5g0d4F4unZDG18rrWRH4j+x810YK1Ui3tCWB9rqO95axP7eSNOh 4HFYnUzbBhmUY764CrRIvC1QwnXH/Cv2Azjylh7R2H8WA96PZsTTCVEoMjtWDO+zAHWm FESBjdXwA4FwUCfrMahGQ1F08ephtSnZzWAb6VFfAWAhqrpnasLSmthuQt4RC++A2FOP MPMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=c0q+eM7w; 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 d21-20020ac851d5000000b003b697c2aabasi1949910qtn.101.2023.01.24.10.08.59 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 24 Jan 2023 10:08:59 -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=c0q+eM7w; 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 1pKNi5-0005rI-Qe; Tue, 24 Jan 2023 13:07:53 -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 1pKNhG-0004yk-6M for qemu-devel@nongnu.org; Tue, 24 Jan 2023 13:07:03 -0500 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pKNhD-00073h-CL for qemu-devel@nongnu.org; Tue, 24 Jan 2023 13:07:01 -0500 Received: by mail-wr1-x429.google.com with SMTP id e3so14697215wru.13 for ; Tue, 24 Jan 2023 10:06:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=Kicu2o7DliK9NIrjxHX2SYsQRxeUGLNQgh1V/I/tfuk=; b=c0q+eM7wHxnoHder73creO8u6wW6XHcaMHVuqAGuq+kfL6t0txTLBYk/KiggZ1mxQ5 3aJ/XlEbjdsRJJWI/D/52oYCY207Ef778QKXGBc/vI+6mo1lwWp/lGbMLavDnE5VEVzq EjBVmmYd69bg6Oc7KOxwz5+digdnhWrSB/jAIUgKhmoSyHrA/VbfsHI9H+6Bg0Bd/VhQ wZL9SnFx2v4Ms8wSc86AtPnRAQHj+P/CFklUMLno32jJvj0SDKpFXTIr+863WvMoXkH+ i9cLEV6/shLWkjrveFNRuvaIAB+/E3zjEBEmH1S3xoeX5yEV95HwJNqCVHXUE4JGEWVc uXrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=Kicu2o7DliK9NIrjxHX2SYsQRxeUGLNQgh1V/I/tfuk=; b=CG/IBeQ2l9kxhCxwBW4IQTTQjWtfEYxcYYpa/xtICkv/HhzQPwTWOdta1JXcMga+U4 26zq4jLi+bpppBZDLVYB5w4m/B2l3x3oZglSGnjUTCDDRmyTr7ai2OyQ0ZUCbBPtGgUk QEuEBocnIujAxSFf93oWH2E8SCkTaG4gq/JrdTVY2r3KalbfYsyWAmb2SG6fUHNI8CfE XAhcAu9Ba1+3y6x+q2AOn/dhGgS0cCEuwO2MWqXG+L1hB2o4Wyqdacaj6tu25oSWpc2f uqLpHbDAzFjv302THlJ+LXwKH8SXoEX7IuHANyDwdj8LAB7JbWnFMlFIwzxa05Hcw0Dl Smhg== X-Gm-Message-State: AFqh2kqKfEmjBkvwNWmR1ypNhYynLljZWVGj8C8Qrci8E6wr8jPTTDA2 kFYliiy/psubMz65ta7IHM8uZw== X-Received: by 2002:a5d:6b0e:0:b0:2bd:e13f:48b8 with SMTP id v14-20020a5d6b0e000000b002bde13f48b8mr25960250wrw.3.1674583617056; Tue, 24 Jan 2023 10:06:57 -0800 (PST) Received: from zen.linaroharston ([185.81.254.11]) by smtp.gmail.com with ESMTPSA id l10-20020a05600012ca00b002bfb02153d1sm2308716wrx.45.2023.01.24.10.06.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Jan 2023 10:06:56 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 54FCB1FFB8; Tue, 24 Jan 2023 18:01:31 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Richard Henderson , Ed Maste , Thomas Huth , Alexandre Iooss , Markus Armbruster , Jagannathan Raman , John G Johnson , Mahmoud Mandour , =?utf-8?q?Daniel_P=2E_Berrang?= =?utf-8?q?=C3=A9?= , Bandan Das , Elena Ufimtseva , Michael Roth , Eduardo Habkost , Beraldo Leal , =?utf-8?q?Alex_Benn=C3=A9e?= , =?utf-8?q?Marc-Andr?= =?utf-8?q?=C3=A9_Lureau?= , Qiuhao Li , Stefan Hajnoczi , Alexander Bulekov , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , John Snow , Paolo Bonzini , Darren Kenny , qemu-arm@nongnu.org, Yanan Wang , Cleber Rosa , Wainer dos Santos Moschetta , Marcel Apfelbaum , Li-Wen Hsu , Peter Maydell , Emilio Cota Subject: [PATCH v2 31/35] plugins: fix optimization in plugin_gen_disable_mem_helpers Date: Tue, 24 Jan 2023 18:01:23 +0000 Message-Id: <20230124180127.1881110-32-alex.bennee@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230124180127.1881110-1-alex.bennee@linaro.org> References: <20230124180127.1881110-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x429.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 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: Emilio Cota We were mistakenly checking tcg_ctx->plugin_insn as a canary to know whether the TB had emitted helpers that might have accessed memory. The problem is that tcg_ctx->plugin_insn gets updated on every instruction in the TB, which results in us wrongly performing the optimization (i.e. not clearing cpu->plugin_mem_cbs) way too often, since it's not rare that the last instruction in the TB doesn't use helpers. Fix it by tracking a per-TB canary. While at it, expand documentation. Related: #1381 Signed-off-by: Emilio Cota Message-Id: <20230108164731.61469-2-cota@braap.org> Signed-off-by: Alex Bennée --- include/qemu/plugin.h | 7 +++++++ accel/tcg/plugin-gen.c | 26 ++++++++++++++++++-------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/include/qemu/plugin.h b/include/qemu/plugin.h index a772e14193..e0ebedef84 100644 --- a/include/qemu/plugin.h +++ b/include/qemu/plugin.h @@ -118,7 +118,10 @@ struct qemu_plugin_insn { void *haddr; GArray *cbs[PLUGIN_N_CB_TYPES][PLUGIN_N_CB_SUBTYPES]; bool calls_helpers; + + /* if set, the instruction calls helpers that might access guest memory */ bool mem_helper; + bool mem_only; }; @@ -158,6 +161,10 @@ struct qemu_plugin_tb { void *haddr1; void *haddr2; bool mem_only; + + /* if set, the TB calls helpers that might access guest memory */ + bool mem_helper; + GArray *cbs[PLUGIN_N_CB_SUBTYPES]; }; diff --git a/accel/tcg/plugin-gen.c b/accel/tcg/plugin-gen.c index c7d6514840..17a686bd9e 100644 --- a/accel/tcg/plugin-gen.c +++ b/accel/tcg/plugin-gen.c @@ -579,7 +579,8 @@ static void inject_mem_helper(TCGOp *begin_op, GArray *arr) * is possible that the code we generate after the instruction is * dead, we also add checks before generating tb_exit etc. */ -static void inject_mem_enable_helper(struct qemu_plugin_insn *plugin_insn, +static void inject_mem_enable_helper(struct qemu_plugin_tb *ptb, + struct qemu_plugin_insn *plugin_insn, TCGOp *begin_op) { GArray *cbs[2]; @@ -599,6 +600,7 @@ static void inject_mem_enable_helper(struct qemu_plugin_insn *plugin_insn, rm_ops(begin_op); return; } + ptb->mem_helper = true; arr = g_array_sized_new(false, false, sizeof(struct qemu_plugin_dyn_cb), n_cbs); @@ -626,15 +628,22 @@ void plugin_gen_disable_mem_helpers(void) { TCGv_ptr ptr; - if (likely(tcg_ctx->plugin_insn == NULL || - !tcg_ctx->plugin_insn->mem_helper)) { + /* + * We could emit the clearing unconditionally and be done. However, this can + * be wasteful if for instance plugins don't track memory accesses, or if + * most TBs don't use helpers. Instead, emit the clearing iff the TB calls + * helpers that might access guest memory. + * + * Note: we do not reset plugin_tb->mem_helper here; a TB might have several + * exit points, and we want to emit the clearing from all of them. + */ + if (!tcg_ctx->plugin_tb->mem_helper) { return; } ptr = tcg_const_ptr(NULL); tcg_gen_st_ptr(ptr, cpu_env, offsetof(CPUState, plugin_mem_cbs) - offsetof(ArchCPU, env)); tcg_temp_free_ptr(ptr); - tcg_ctx->plugin_insn->mem_helper = false; } static void plugin_gen_tb_udata(const struct qemu_plugin_tb *ptb, @@ -682,14 +691,14 @@ static void plugin_gen_mem_inline(const struct qemu_plugin_tb *ptb, inject_inline_cb(cbs, begin_op, op_rw); } -static void plugin_gen_enable_mem_helper(const struct qemu_plugin_tb *ptb, +static void plugin_gen_enable_mem_helper(struct qemu_plugin_tb *ptb, TCGOp *begin_op, int insn_idx) { struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); - inject_mem_enable_helper(insn, begin_op); + inject_mem_enable_helper(ptb, insn, begin_op); } -static void plugin_gen_disable_mem_helper(const struct qemu_plugin_tb *ptb, +static void plugin_gen_disable_mem_helper(struct qemu_plugin_tb *ptb, TCGOp *begin_op, int insn_idx) { struct qemu_plugin_insn *insn = g_ptr_array_index(ptb->insns, insn_idx); @@ -750,7 +759,7 @@ static void pr_ops(void) #endif } -static void plugin_gen_inject(const struct qemu_plugin_tb *plugin_tb) +static void plugin_gen_inject(struct qemu_plugin_tb *plugin_tb) { TCGOp *op; int insn_idx = -1; @@ -870,6 +879,7 @@ bool plugin_gen_tb_start(CPUState *cpu, const DisasContextBase *db, ptb->haddr1 = db->host_addr[0]; ptb->haddr2 = NULL; ptb->mem_only = mem_only; + ptb->mem_helper = false; plugin_gen_empty_callback(PLUGIN_GEN_FROM_TB); }