From patchwork Mon Jan 16 22:36:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 643111 Delivered-To: patch@linaro.org Received: by 2002:a17:522:b9de:b0:4b9:b062:db3b with SMTP id fj30csp842322pvb; Mon, 16 Jan 2023 14:38:12 -0800 (PST) X-Google-Smtp-Source: AMrXdXvzaMBwPS8oznb7UQtgFhXlY5i0Bi+GMv/z9tupt4hrou1zxj5sSEOQ8Qaw0XsOGI50dOBZ X-Received: by 2002:a05:6214:5d82:b0:4c7:7237:76ff with SMTP id mf2-20020a0562145d8200b004c7723776ffmr1878843qvb.42.1673908692161; Mon, 16 Jan 2023 14:38:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673908692; cv=none; d=google.com; s=arc-20160816; b=e3q2RzhxVhLGB+DwdK6gKdujTiiJkOC4BRfRecr8J/uDhuXNGz0iKFXrc5kYHVUEfW XGUWnOn6MM8eo5NKIeF7q0WBo46Tx/PXXa48UymCFgbWnAuKDON/9qHe84J8mZJAGPSY 0fDM6CupohCHm6P58Y7oln4N/uZ8eMqaOT93j5J+1o9gy6SmtKl3Qi7hNw7RlSDuRs+i /G0+8VOhpfn2ml5xXO3aWsTKIX74A7LJJVjq+3AzXzuou2Drhp99HHfpNlZSBElxSdH7 i+7uzkunKlnwXxDjW41HOslmaJVVlhAFXrYRziuPQaV+J9Bm7SSFb2QPW768lbs/ZDCg awDQ== 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=0l2tmn7cqPSHygkIlv8PBR3kE2IV45gjKZ9i2g3E1kk=; b=xUEP88rj1slLlNghSrm5ncqZxJjpDLyskUD/MPz6FzJwWbkWWRIRy6sqJQZUWYh4+4 LWICkxqj+cIZuhzi1uEd/xcqyoImeJ/K2ulvFThT1kOr6Ty9oA4/j4HqMcyQJc784vjz KZItDrEkLosgKc2+4I4Ns5nW4lBdkb98NgZYVJsDMFOWZsqieU9I6atAvWDOZtRWhXN3 dknBMfVBNvja0cdK4J6tTGWIpKBebj1IEtJBr31g3KyrGcGtGJobQqlxDOwlMMKB0ifZ bAB6FN30Ls++JJ87EZfv9zCrnZYbIGzEYOZrcVnCejm0UihBJb+atFBUOsGXMUA+4Ij7 kgiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=G9yOxsLG; 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 jx10-20020a0562142b0a00b00535256ea443si699724qvb.3.2023.01.16.14.38.12 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 16 Jan 2023 14:38:12 -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=G9yOxsLG; 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 1pHY6E-0000gy-3r; Mon, 16 Jan 2023 17:37:06 -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 1pHY64-0000a8-Cb for qemu-devel@nongnu.org; Mon, 16 Jan 2023 17:36:59 -0500 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pHY62-0001Mr-0y for qemu-devel@nongnu.org; Mon, 16 Jan 2023 17:36:55 -0500 Received: by mail-pj1-x1034.google.com with SMTP id o13so26963573pjg.2 for ; Mon, 16 Jan 2023 14:36:49 -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=0l2tmn7cqPSHygkIlv8PBR3kE2IV45gjKZ9i2g3E1kk=; b=G9yOxsLGKG4hd4ZroJ9tV/vKLiM1AxaTdxUobDNumRGFf3ujeidAfb0McXLJNxdw6F ajcLejxsnayTe7/CmUyALsJ/Bg5cHH159P/CMrnvb0ILvw99+RG0vBQ/mudGXFhNEGAZ V9ATeSDclmU1ZS2ADCwjaVAQGDVxPCj/N61KtvMxh9ZtOt1ct1/U8jIwxVETRUskt/+6 gpj+fyEK4WpOrN6f4uh90wUtrNUYaShBW49aDOgQlWUOf0U9h4zNY0DKMi2aO7OICCQl IL+K5CpeaUqKIAg0fPyzDLWjzsLktt40Ew4JAk982Vsd/KgSsAgciDTrm6lyH3X1c2vJ 7yHg== 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=0l2tmn7cqPSHygkIlv8PBR3kE2IV45gjKZ9i2g3E1kk=; b=ULyQoD9/5UYCollGGtYXyGFrvvPuh1u1tolONXSiLO/1J6TYZimN/z1Nyx3kYUu7sA /VlYPT0MBaCfFtCpJj7q/SB+2t9B+0xCTgjEPe7tvirGHrskHUGSZ9KWH8rNtZKTmJyG 56zbn4hvzBhU45j6mtowgGqlSuk1bybWtpWW0m3zT+l951ONddUQHTFHj/NKAQiaXTlz Rfiu/rXt6Qfo5lKifeyjzSLh7+663HsDkVzJt0ZCNmnCbisN+oi0m6gbZ/TYETBh9N07 aZufA6AlYbgzLLnKag0P5l1Fl5plJu0R/3HPjWPaNgGySRTLCBQLFlaGdjVlJRavQGEi 6YQQ== X-Gm-Message-State: AFqh2kpP5U7ruFTxxUtlh9u1C787FK7aN+Di4Vs5dYWKRjGn6MRZ/+lz gAvqP2IdJl+g2VYWEoX2ix6zCQNJjPtmJgVu X-Received: by 2002:a17:902:968f:b0:194:79b1:ea27 with SMTP id n15-20020a170902968f00b0019479b1ea27mr1113546plp.50.1673908608682; Mon, 16 Jan 2023 14:36:48 -0800 (PST) Received: from stoup.. (rrcs-173-198-77-218.west.biz.rr.com. [173.198.77.218]) by smtp.gmail.com with ESMTPSA id n3-20020a170903110300b00189f2fdbdd0sm19742492plh.234.2023.01.16.14.36.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Jan 2023 14:36:48 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PULL 5/5] accel/tcg: Split out cpu_exec_{setjmp,loop} Date: Mon, 16 Jan 2023 12:36:37 -1000 Message-Id: <20230116223637.3512814-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230116223637.3512814-1-richard.henderson@linaro.org> References: <20230116223637.3512814-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1034; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1034.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 Recently the g_assert(cpu == current_cpu) test has been intermittently failing with gcc. Reorg the code around the setjmp to minimize the lifetime of the cpu variable affected by the setjmp. This appears to fix the existing issue with clang as well. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1147 Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Richard Henderson --- accel/tcg/cpu-exec.c | 111 +++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 57 deletions(-) diff --git a/accel/tcg/cpu-exec.c b/accel/tcg/cpu-exec.c index 356fe348de..8927092537 100644 --- a/accel/tcg/cpu-exec.c +++ b/accel/tcg/cpu-exec.c @@ -909,64 +909,10 @@ static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb, /* main execution loop */ -int cpu_exec(CPUState *cpu) +static int __attribute__((noinline)) +cpu_exec_loop(CPUState *cpu, SyncClocks *sc) { int ret; - SyncClocks sc = { 0 }; - - /* replay_interrupt may need current_cpu */ - current_cpu = cpu; - - if (cpu_handle_halt(cpu)) { - return EXCP_HALTED; - } - - rcu_read_lock(); - - cpu_exec_enter(cpu); - - /* Calculate difference between guest clock and host clock. - * This delay includes the delay of the last cycle, so - * what we have to do is sleep until it is 0. As for the - * advance/delay we gain here, we try to fix it next time. - */ - init_delay_params(&sc, cpu); - - /* prepare setjmp context for exception handling */ - if (sigsetjmp(cpu->jmp_env, 0) != 0) { -#if defined(__clang__) - /* - * Some compilers wrongly smash all local variables after - * siglongjmp (the spec requires that only non-volatile locals - * which are changed between the sigsetjmp and siglongjmp are - * permitted to be trashed). There were bug reports for gcc - * 4.5.0 and clang. The bug is fixed in all versions of gcc - * that we support, but is still unfixed in clang: - * https://bugs.llvm.org/show_bug.cgi?id=21183 - * - * Reload an essential local variable here for those compilers. - * Newer versions of gcc would complain about this code (-Wclobbered), - * so we only perform the workaround for clang. - */ - cpu = current_cpu; -#else - /* Non-buggy compilers preserve this; assert the correct value. */ - g_assert(cpu == current_cpu); -#endif - -#ifndef CONFIG_SOFTMMU - clear_helper_retaddr(); - if (have_mmap_lock()) { - mmap_unlock(); - } -#endif - if (qemu_mutex_iothread_locked()) { - qemu_mutex_unlock_iothread(); - } - qemu_plugin_disable_mem_helpers(cpu); - - assert_no_pages_locked(); - } /* if an exception is pending, we execute it here */ while (!cpu_handle_exception(cpu, &ret)) { @@ -1033,9 +979,60 @@ int cpu_exec(CPUState *cpu) /* Try to align the host and virtual clocks if the guest is in advance */ - align_clocks(&sc, cpu); + align_clocks(sc, cpu); } } + return ret; +} + +static int cpu_exec_setjmp(CPUState *cpu, SyncClocks *sc) +{ + /* Prepare setjmp context for exception handling. */ + if (unlikely(sigsetjmp(cpu->jmp_env, 0) != 0)) { + /* Non-buggy compilers preserve this; assert the correct value. */ + g_assert(cpu == current_cpu); + +#ifndef CONFIG_SOFTMMU + clear_helper_retaddr(); + if (have_mmap_lock()) { + mmap_unlock(); + } +#endif + if (qemu_mutex_iothread_locked()) { + qemu_mutex_unlock_iothread(); + } + qemu_plugin_disable_mem_helpers(cpu); + + assert_no_pages_locked(); + } + + return cpu_exec_loop(cpu, sc); +} + +int cpu_exec(CPUState *cpu) +{ + int ret; + SyncClocks sc = { 0 }; + + /* replay_interrupt may need current_cpu */ + current_cpu = cpu; + + if (cpu_handle_halt(cpu)) { + return EXCP_HALTED; + } + + rcu_read_lock(); + cpu_exec_enter(cpu); + + /* + * Calculate difference between guest clock and host clock. + * This delay includes the delay of the last cycle, so + * what we have to do is sleep until it is 0. As for the + * advance/delay we gain here, we try to fix it next time. + */ + init_delay_params(&sc, cpu); + + ret = cpu_exec_setjmp(cpu, &sc); cpu_exec_exit(cpu); rcu_read_unlock();