From patchwork Tue Jan 10 17:39:19 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: 640866 Delivered-To: patch@linaro.org Received: by 2002:a17:522:f3c4:b0:4b4:3859:abed with SMTP id in4csp2849968pvb; Tue, 10 Jan 2023 10:16:20 -0800 (PST) X-Google-Smtp-Source: AMrXdXv1ZwYx6G26tZbMZwT1jSAQ6krIHB8VMJ7CSrTJNdcmmwLuwO7p37WztIY5HnIAz5IhKw09 X-Received: by 2002:a0c:f204:0:b0:532:1fff:d170 with SMTP id h4-20020a0cf204000000b005321fffd170mr24725437qvk.11.1673374580202; Tue, 10 Jan 2023 10:16:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673374580; cv=none; d=google.com; s=arc-20160816; b=F+p3I1FAKxK/m4zvp+5pRGmbppKlLGaWI+AiJPd0sYvrbkIj97DUeBIbwWn41YV4ME 7ILzPKTTKK2LQgEk0uqUwO+Zv4wteFhlMqo+1GsRwLLq7J1kV37HCtjZTz523PgfwY+l 2fzhvfexjiY9+A8iJCKulYCyzKi4za+nKSsdw16yZQzWMVpdq0gfmxZec7CeHsBIP6NA v/jBCS/kZeCFqTkb7f0/ONChxfGTAdxAn7n5JOaTVYBSl/8VfAT8CobrjTkwotRSq03e /Wz/klSKyQB/IVeCcD70QJFt551hrXiXgjeALZNiHwNrqYGE7LRVJM++ntZ7bFnm5JvW Tsow== 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=DbsNfzKc5GiLTdIjPTKBcAApewM5tkfx21aL+dc6lqYZnk5M2aOqWxfWQz3X+HP0uH 4ZoqlhDS9AMfvAWfzUW/1wnAX1aRBGggLH8ruLgEe0c72lsLwGGYdYK/6wDYtmTmfts9 ngmrsPneow6UqAOGdl5J2D+BjFKagRBVnwkMqd38+6SpW1zTNxgjBuQx/2XA3g67o/mY g4fwAUktCXoWLPrX8TtO8o6P7pKLtiRXNTGcIXJMQ93wYK0JHM1oGVp6rVcQHKclmcAJ 02UHMx6cqC1iAbWyihfvO8WVjw0wCtPVcmB0qme5nYR84DI1niGK4TcjqPkzz3U7C4Em 5wxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=v9pZScaX; 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 jv6-20020a05621429e600b004c696df49d2si6534349qvb.66.2023.01.10.10.16.20 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 10 Jan 2023 10:16:20 -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=v9pZScaX; 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 1pFIbB-0003by-ER; Tue, 10 Jan 2023 12:39:45 -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 1pFIb8-0003Xt-Hx for qemu-devel@nongnu.org; Tue, 10 Jan 2023 12:39:42 -0500 Received: from mail-wm1-x32c.google.com ([2a00:1450:4864:20::32c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pFIb3-0001FA-DK for qemu-devel@nongnu.org; Tue, 10 Jan 2023 12:39:42 -0500 Received: by mail-wm1-x32c.google.com with SMTP id i17-20020a05600c355100b003d99434b1cfso10710559wmq.1 for ; Tue, 10 Jan 2023 09:39:36 -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=v9pZScaXYw5pWWMxBkvH94FPLOVUQwV2i2FdnAlbEv/tdMTUn8XNz9dxUuzoZ6dShc Q1SAdOQoS2V1ai6HtomZFMqyizFAUqzusgS5/I27a/e2mCUFAmuIg7pp5gGkCXOKdJFI krzR1ISCPTZK5R/JdeQhmwYPjtR9OWOf5TmhjV48Veu5NNF+NP9uKUL9F/8o3fousDOI EcD2F5KgMu9u999Ws5FZfDmgtjWXXKQdOc6qhPv3c73tlJXWdQP5QRw7H55o/AiA4eAF +dy6ACBmD0m8256MgnwX9Z8N7EdVLH77BwIYWLW5k5vA0XnqRNgkTUqv59mIg9T5VdxH 9KhQ== 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=YYO9JwIpjC9FPjZVmH8jL6ptN0iEFg7lO1cbRpsjDcwQGrK7y7gghe/Gp9hcuQo/Vb nonmTJK4l0vmruKuPhvE6Prx46Gg0pVLRq3dcnvOaS7XsnNxLLm45LxhlWuwTMDF6Jiq tkShsQbaUXFvaN705L3almLPnUn7ZvxE6h3b2+cNajARULVH7LiTxWtZ9LevQeTUCQuT pWR9K6LZeaWm9aMu9gXzjg/lhMr8qGYiRsZqelNwYZ85vvOiVUbY3cqDhmTel+xeuXgp LAdl1a7VY2m1xPu2gAMFycen0yQeU92EZH/OJTfa5NPbiUY+1MKLjTkOmrl5gaT368CQ yKmA== X-Gm-Message-State: AFqh2krVEWBm8j51CUlC7JTkRrL/MPFMsajF8hXBekB0RT3x5OzJupE3 5bzGM4FR0Pf371YMI3exTY3UdUi3CkptzQXu X-Received: by 2002:a05:600c:18a1:b0:3d2:3ec4:7eed with SMTP id x33-20020a05600c18a100b003d23ec47eedmr53404540wmp.10.1673372376666; Tue, 10 Jan 2023 09:39:36 -0800 (PST) Received: from zen.linaroharston ([185.81.254.11]) by smtp.gmail.com with ESMTPSA id i8-20020a1c5408000000b003da065105c9sm13487wmb.40.2023.01.10.09.39.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jan 2023 09:39:36 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id C82001FFB8; Tue, 10 Jan 2023 17:39:25 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Richard Henderson , Alexandre Iooss , Beraldo Leal , Thomas Huth , John Snow , Eduardo Habkost , Elena Ufimtseva , Ed Maste , Yanan Wang , Cleber Rosa , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , Li-Wen Hsu , Markus Armbruster , Jagannathan Raman , =?utf-8?q?Daniel_P=2E_Berrang?= =?utf-8?q?=C3=A9?= , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , Michael Roth , Wainer dos Santos Moschetta , =?utf-8?q?Alex_Benn?= =?utf-8?q?=C3=A9e?= , qemu-arm@nongnu.org, Marcel Apfelbaum , Peter Maydell , Paolo Bonzini , Mahmoud Mandour , John G Johnson , Emilio Cota Subject: [PATCH 23/26] plugins: fix optimization in plugin_gen_disable_mem_helpers Date: Tue, 10 Jan 2023 17:39:19 +0000 Message-Id: <20230110173922.265055-24-alex.bennee@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230110173922.265055-1-alex.bennee@linaro.org> References: <20230110173922.265055-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32c; envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x32c.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=unavailable 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); }