From patchwork Fri Sep 13 17:26:52 2024 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: 828211 Delivered-To: patch@linaro.org Received: by 2002:adf:a2d0:0:b0:367:895a:4699 with SMTP id t16csp362661wra; Fri, 13 Sep 2024 10:29:56 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWwPHrw4+VD7tUl9jJUhqpCJ/Oz4tKhS65bzeHnBGNB7qd8N9zJ+9q9bFsa9QPOvcqlm0yqXA==@linaro.org X-Google-Smtp-Source: AGHT+IENSaNF4m12MDzwDJyvdGkRChjsiS1TO6PObJFsHhga+g8ams8NkPnK+phiS4UlZpRlg5HL X-Received: by 2002:a05:690c:f96:b0:6d3:82d0:fe4f with SMTP id 00721157ae682-6dbb6add3edmr80490997b3.6.1726248596686; Fri, 13 Sep 2024 10:29:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1726248596; cv=none; d=google.com; s=arc-20240605; b=IVsEVhYnAPYFQoe99DqCqtmb5WruAiQq44hKD79K2eQ/h0aN2p1/Q2OUJNhq+qjG1E SBeaKS/3EZPKHi+mE1M/sui+R2Kt9NU9VxcvJ1GCPezbTvEeR8imocw9pL2eF1nyon0a 1rndeoocXqK2bOiMKMZKAAdxucaTBOAowLREDwOwmsQab1pPudjsxX/NGqtkZhAz20wU j5yef7VmwithNCATwvb1h7rW7bOcY1gZvuHtmU2uXWWuQbgnS/NNuLH5ehOzW5GRhGHu YvZwUtXatzPOaA4uZ5FmkhiyIiWfWN44vE+oMUZ1Ou+W1PTXXhWVcpJC4Vwm60ku08+V 5YXw== 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:dkim-signature; bh=cRFhj2SyvQ/EIwHMRij/KsWIl6x9+a8v4XWFbD+c1P0=; fh=q5khnLXJ27a1TDUqc7RHOp19ruAkYYnLBIeCzqGkcps=; b=gEZgw7AA+0lBho2OaE3qFFr5fM8kN1eR05P1tb6M73i1ZODEuGtrhIbA3rd2AEyLdB PB/RvDopDsQodzJ2InyO7KxB4IL8uCH7xERwOGloKC1TDWStxk1jFRxiht5i5xZd/RbK Nikp9PsIVdTM4hz4LPYuiub9t/IwbnYtV81LIgXlX1u1JwJ0fTh4W0BcbaGUr5Od+3JV XXjppfEVk1zLyxOuUYnjHf/EpXATDk7z8apL9lys1ThLB8ybIgB/CDD1zYRYDYM3sGdm ExGSE2/rnSUpO7AZwHfqDTaOyZ3GCfxEwEr5f0+zSYkpwxdMA7VxQMWOdHspS7fzPRUw CQNg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=DX6jTn7s; 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; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id 6a1803df08f44-6c5343302bdsi156444106d6.55.2024.09.13.10.29.56 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 13 Sep 2024 10:29:56 -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; dkim=pass header.i=@linaro.org header.s=google header.b=DX6jTn7s; 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; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1spA4r-00070a-Nq; Fri, 13 Sep 2024 13:27:25 -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 1spA4l-0006ZL-SV for qemu-devel@nongnu.org; Fri, 13 Sep 2024 13:27:20 -0400 Received: from mail-ej1-x62a.google.com ([2a00:1450:4864:20::62a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1spA4b-0005wk-0T for qemu-devel@nongnu.org; Fri, 13 Sep 2024 13:27:16 -0400 Received: by mail-ej1-x62a.google.com with SMTP id a640c23a62f3a-a8d56155f51so287872766b.2 for ; Fri, 13 Sep 2024 10:27:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1726248427; x=1726853227; 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=cRFhj2SyvQ/EIwHMRij/KsWIl6x9+a8v4XWFbD+c1P0=; b=DX6jTn7seE8vwRwsLwL+3+FTsU1smTjiFVjGrTqcKNA6gC0gvJOJGUepSEV1pODg69 HZSdq8ffQ7r6kfDUHuhHoYjULl7hHR0C1Gfo6yWO7rxfeGUPmdg6lryZSUdoSYJTEnMT SVg7tcsZidJ3esPbBpju/CPg/gn0crSRgCFgF8mhkkZv7nZ202Xx20XA89FtHNQqCVbd FF/pGEA3LXXTiXNv2pnBqC2VWgyo0zA87JLYVFg2RbSFi5pR+DCdLTxyXjyys/Nh+au+ c0c+PBSqPiF7ASuJmWjk6o7NDhQlzAh61A2VZCCXQIR+ITYDwRHWoz3eJjwtTZPBcIhA tDhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726248427; x=1726853227; 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=cRFhj2SyvQ/EIwHMRij/KsWIl6x9+a8v4XWFbD+c1P0=; b=RnmyMZ0PYthmvWUdkDYmArtXeMui5l6U4tvqS9R0cubyeFpJWcJyqz/CpBMe8F0Egg a9QVtv21o4d7sM0nc91hu1OITLKmlZSkIIJZcjFTzmy7NmBX2DF+3vsDF16IdfHWEFdi E3SVMA2zPCx1l4flQaHPQuzwbUbZ8lm9SlPbqhS4WYrqv+16VMB0tUw0PSXsOer7qWUZ PGgK0T6a8fR4TB4E66sPf6RxXbIrIFK4FRz0ltu4nRqkA4E8FQNt1hlvSEBqzhOG3MsN MCWaTcTDBfeD1s5WnKauZNcLY4/nUWBbl6ien8YnJ6Wmub4XLY6amX2sKRDZxS15d1+o gTPA== X-Gm-Message-State: AOJu0YzzCOf2offFyWFKeB+S1Afv9SQfKmih8RwZ+KYW6B7fsGhv33Vf PExEK12dk7RzlUa4hKgmkS9AsKo7bZgg/+pwZP+mOX1014sG5XSgd7cVT8st4yM= X-Received: by 2002:a17:907:60d5:b0:a8a:cc5a:7f3c with SMTP id a640c23a62f3a-a90296715f7mr632864466b.58.1726248427294; Fri, 13 Sep 2024 10:27:07 -0700 (PDT) Received: from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a8d25d657fesm896408366b.221.2024.09.13.10.27.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Sep 2024 10:27:06 -0700 (PDT) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id 832615FB3A; Fri, 13 Sep 2024 18:26:57 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: Richard Henderson , devel@lists.libvirt.org, Thomas Huth , Mahmoud Mandour , Paolo Bonzini , David Hildenbrand , Ilya Leoshkevich , qemu-ppc@nongnu.org, Zhao Liu , =?utf-8?q?Alex_Benn=C3=A9e?= , =?utf-8?q?Philippe_M?= =?utf-8?q?athieu-Daud=C3=A9?= , Yanan Wang , Eduardo Habkost , qemu-s390x@nongnu.org, Alexandre Iooss , Pierrick Bouvier , Nicholas Piggin , Daniel Henrique Barboza , Marcel Apfelbaum , Elisha Hollander Subject: [PATCH 14/17] util/timer: avoid deadlock when shutting down Date: Fri, 13 Sep 2024 18:26:52 +0100 Message-Id: <20240913172655.173873-15-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240913172655.173873-1-alex.bennee@linaro.org> References: <20240913172655.173873-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::62a; envelope-from=alex.bennee@linaro.org; helo=mail-ej1-x62a.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 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. Signed-off-by: Alex Bennée Reported-by: Elisha Hollander --- util/qemu-timer.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) 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);