From patchwork Mon Oct 7 19:16:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Michael Tokarev X-Patchwork-Id: 833353 Delivered-To: patch@linaro.org Received: by 2002:adf:8b52:0:b0:367:895a:4699 with SMTP id v18csp2135286wra; Mon, 7 Oct 2024 12:24:10 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCV5vOeDKPYqFaXMk4HB3K6xyyNsteY9MVF6pX0J3z2bKTlJFpuw7+UijoER9kek4455e7qTtA==@linaro.org X-Google-Smtp-Source: AGHT+IH6D8PWebfngurW9ZrapBMtchfenOQtOzHYnDHWck2EOGyMxJ+mS0og5sBeBgUH0Z1ppE5r X-Received: by 2002:ac8:5a16:0:b0:458:3c2a:1d6f with SMTP id d75a77b69052e-45d9ba86d2dmr223086381cf.31.1728329049934; Mon, 07 Oct 2024 12:24:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1728329049; cv=none; d=google.com; s=arc-20240605; b=MLs83GcOMJ4hDNeS9uGQqTdibfDg8uhkPECvCy+sVkJQs4b36PAgNlBN4dcRFXSYCr dbKboe/9EGGP4W24nPDJgqm7rzLMDldH/bBU78xoAhMDXvbkvN1oYt1G2DbLJdNpYie4 hfSGic3nibzoqMmTGu8pP6QE5mDStGLSIEpRDDuAW4trsjc8YxVB2LyoBGx+KmiTYISM A0KBK0/aUnzGiWRZfg8RCd//h+eP9qxSNSTLZtbG5ZOxVu0v16pUDXoOzagAiNpGkPlG 1WTRcgESg5GlBv0fxaqDysUQhC/QxaXo4vnVSbi5+lAPuTyPIvSY/tVq3XDA58ZNhGZ+ QpGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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; bh=Zs/5zBVMboiX+kK8gXWYG0XCQlM5+QpfL4/YJu9XcLc=; fh=LfjYcB33YmYVUtk8kVIY91ik8BChF8/jYqjZTlCfyqk=; b=F+/QxNSIFsz0rS/HnjNl4iI5XPJ2G8W1iBmwMnH0CqLt277gAw38wwbENip6oA5Odw eIgZTxkKmbB+y+Fd1Bv1JKCtcpdFBFu2ZWR3B4nRvgS0gKR3732a4xdPyn6SzbhI/Pav bhOlOjjx2ytaLG/FHVJ+RqL3CO/AOeHPxzzYjTt6ZNx2lgKDGwwFGT57WlxLgC0M+fF4 +ZuUgq3TIML6kITeLvIP2stCYEOxF5x4mv8+D7vie9CcP9jVsbawxg0TvhloN2A85oww wSeUBcnsrExNrx5taTHNW1uSbAE8yK7HruTQVuGiNh4LXl45M4X+CBYCo1pO+iSgWST+ DIcg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; 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" Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id d75a77b69052e-45da74af2absi71789081cf.51.2024.10.07.12.24.09 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Oct 2024 12:24:09 -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; 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" Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sxtGx-0000fx-2F; Mon, 07 Oct 2024 15:19:59 -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 1sxtGG-0007AM-Qe; Mon, 07 Oct 2024 15:19:19 -0400 Received: from isrv.corpit.ru ([86.62.121.231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sxtGE-0004P8-Uy; Mon, 07 Oct 2024 15:19:16 -0400 Received: from tsrv.corpit.ru (tsrv.tls.msk.ru [192.168.177.2]) by isrv.corpit.ru (Postfix) with ESMTP id CEDC596258; Mon, 7 Oct 2024 22:16:49 +0300 (MSK) Received: from tls.msk.ru (mjt.wg.tls.msk.ru [192.168.177.130]) by tsrv.corpit.ru (Postfix) with SMTP id A8AE214F738; Mon, 7 Oct 2024 22:16:56 +0300 (MSK) Received: (nullmailer pid 2592783 invoked by uid 1000); Mon, 07 Oct 2024 19:16:54 -0000 From: Michael Tokarev To: qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, =?utf-8?q?Alex_Benn=C3=A9e?= , Elisha Hollander , Pierrick Bouvier , Michael Tokarev Subject: [Stable-9.1.1 27/32] util/timer: avoid deadlock when shutting down Date: Mon, 7 Oct 2024 22:16:44 +0300 Message-Id: <20241007191654.2592616-27-mjt@tls.msk.ru> X-Mailer: git-send-email 2.39.5 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=86.62.121.231; envelope-from=mjt@tls.msk.ru; helo=isrv.corpit.ru X-Spam_score_int: -68 X-Spam_score: -6.9 X-Spam_bar: ------ X-Spam_report: (-6.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_HI=-5, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, 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: Alex Bennée When we shut down a guest we disable the timers. However this can cause deadlock if the guest has queued some async work that is trying to advance system time and spins forever trying to wind time forward. Pay attention to the return code and bail early if we can't wind time forward. Reported-by: Elisha Hollander Signed-off-by: Alex Bennée Reviewed-by: Pierrick Bouvier Message-Id: <20240916085400.1046925-15-alex.bennee@linaro.org> (cherry picked from commit bc02be4508d8753d1f6071b77d10f4661587df6f) Signed-off-by: Michael Tokarev diff --git a/util/qemu-timer.c b/util/qemu-timer.c index 213114be68..6b1533bc2a 100644 --- a/util/qemu-timer.c +++ b/util/qemu-timer.c @@ -685,10 +685,17 @@ int64_t qemu_clock_advance_virtual_time(int64_t dest) { int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); AioContext *aio_context; + int64_t deadline; + aio_context = qemu_get_aio_context(); - while (clock < dest) { - int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL, + + deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL, QEMU_TIMER_ATTR_ALL); + /* + * A deadline of < 0 indicates this timer is not enabled, so we + * won't get far trying to run it forward. + */ + while (deadline >= 0 && clock < dest) { int64_t warp = qemu_soonest_timeout(dest - clock, deadline); qemu_virtual_clock_set_ns(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + warp); @@ -696,6 +703,9 @@ int64_t qemu_clock_advance_virtual_time(int64_t dest) qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL); timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]); clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + + deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL, + QEMU_TIMER_ATTR_ALL); } qemu_clock_notify(QEMU_CLOCK_VIRTUAL);