From patchwork Mon Sep 16 08:53:56 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: 828994 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:4187:b0:367:895a:4699 with SMTP id ei7csp681846wrb; Mon, 16 Sep 2024 01:57:07 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUQNQkM2+QJ2RVB5aagxq3UY4rOgjSkwUBi7QNNdSHUQfWMjs+t9aGfbRi7GyaMMd1fZTinqg==@linaro.org X-Google-Smtp-Source: AGHT+IHnqgZAD8bliSySytOtWLo6loC6HDAZ6/2DbwbMVss0OzXfd2204y+t6/XJRCZJHSm0QO23 X-Received: by 2002:a05:620a:4593:b0:7a9:db7d:11f8 with SMTP id af79cd13be357-7a9e5ee21bemr2631962785a.7.1726477026931; Mon, 16 Sep 2024 01:57:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1726477026; cv=none; d=google.com; s=arc-20240605; b=N22pshawnLuqDw4zOq0GBmCgdrn067WIhP/vPjIiQxH/1JkzAwvR5kVRhXBy77MB21 S4OJhPWcahN+c95Mj4lexid4TYwz/wxrsmu4EN/I7GwY79y3KYfkPH+sWy7HYiSXik3O RJZmOsVzlq0RGfP5ddkm/5jq/lchSLAST3u/MwB8soEBMAnaMYkCECRKmkzZLXLawpw5 GWdqI0lohEn+B/EUYK/ii25YEsfB23qC/DCeoMxnyYfb1F6zm/Fj+kH5surZk6P1dwK3 lD1VhVvBaLEqXmQ7qYzDXzYKwVuTM3PLL/THF7bLbt3BbxDbCMIvlu9Krf34kGPONdl+ ydUQ== 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=2OJksstmsifZmCUPWtvTuS/7OYa4HHWDbSpalmz/esw=; fh=EUSMNgYjwj4GGVay3LBMQ2Gpqe70dd1O82wcFkfan2U=; b=clpwjDFKY9KwqrUv+YNKTNFzYYmyk2HKpPTLiROHdN05u4ZcARGaCqszgRPPm8q1lc pq5sAa1woC3lyWgbGyQiBrO6Ws3dpGZWW/mWxn0lo2Eq1p1geSBZUpMwJSDC0dnUFX8b 2nTWt6bpKjyZA6g/vEMmuRuwfHydluGNC4l7FflEuGjNsDSYPWN/b0XuE3mfIUlBYvUO xeX0OTRKUo/o5vQzLyFlZwcj0mluki32G/pGg8q38SdkK7+0YcTHEQJRYlKFBjxUb8kr gbSF2azUhmsomDoWH2inuOR8RFWTNskWQVXRK69mLgSgMSpk6uja4Pkzs8dMnvvJVSWR /LQA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MYMZz8df; 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 af79cd13be357-7ab3e97179bsi536146685a.29.2024.09.16.01.57.06 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 16 Sep 2024 01:57:06 -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=MYMZz8df; 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 1sq7Ve-0002Cc-9f; Mon, 16 Sep 2024 04:55:02 -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 1sq7VG-00010N-Gk for qemu-devel@nongnu.org; Mon, 16 Sep 2024 04:54:42 -0400 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sq7Uv-0005F1-IX for qemu-devel@nongnu.org; Mon, 16 Sep 2024 04:54:36 -0400 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-42cacabd2e0so35331715e9.3 for ; Mon, 16 Sep 2024 01:54:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1726476855; x=1727081655; 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=2OJksstmsifZmCUPWtvTuS/7OYa4HHWDbSpalmz/esw=; b=MYMZz8dfTHhx+zBGnP1Eo9BsA2oXgrI8uqfrr/7s5fod+WjeLcjn4FL1iwmnu+HekP O1zpQCNMKM/cPQz3x8+Xmul/R4OXY3kBQ4eu4lqZOSKnSPek/lAfEF/z6liTTuL9Jws6 vK72ctw78hL3rgT6vRs7JP60v+7sNel6wkEDWFhugZlXJPv1ZogM+j4poVWDy31dBEOp 73L8xgYURYbB5oFwfxd1OOY5ynDxzpxyLyv8TfY5wcYH15geHtgqV3FQCSXh79BLwn4h Pz5/Rm/kd+Wla+7IaGtKgbobk0k8HkB0Qhh5WZlWkgCjRK0rP1AAhABI4BoqjBYhcWrU mfpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726476855; x=1727081655; 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=2OJksstmsifZmCUPWtvTuS/7OYa4HHWDbSpalmz/esw=; b=RsXjyzpz1axHdW1LeLcH9ARt6nbXHUsL4+9+s6xSSHFZrRfgbBq9I92q7Am+0w6wRx Qti4ptuhgiME68QI57HISsqw/6/YmN9gkLeVuE86JKZ+VImtmGY40YjVqSLK9ZA5fXgX Wzp4wgn8nX0xupa31LHACkOdTn2erwcogRb5+eoicKj5uBxDOWgst7vaZ249DcDW4oaW KqdoF2F884S1pbFqpV5iKbsemKFIdFi89ZWJDOaVfxpH+WazosWD5bTB0Y27zBrDLPK5 WYAFG4IsdpNbqQ2rMKibt2Yqh6jNHnBZ9o9tHBrMYtDEru1MRK95hNq+8KU0EnIGPOeC 8UPA== X-Gm-Message-State: AOJu0YzTiQaQoaFp19eQ+1GS3Jv2GEWHEM+VV+QYwNpzSyKXlb675YnZ eITflD04829bnrw1ScehUKxpGqJMTUR22MD2jqhWXaDaFpc27zvvo/xnUGn9ELk= X-Received: by 2002:a05:600c:468a:b0:42c:b309:8d18 with SMTP id 5b1f17b1804b1-42cdb53be54mr96726175e9.19.1726476854713; Mon, 16 Sep 2024 01:54:14 -0700 (PDT) Received: from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42d9b15d5bcsm104745815e9.26.2024.09.16.01.54.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2024 01:54:09 -0700 (PDT) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id A39BF5FCAE; Mon, 16 Sep 2024 09:54:01 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: qemu-s390x@nongnu.org, Thomas Huth , Richard Henderson , qemu-ppc@nongnu.org, Ilya Leoshkevich , Paolo Bonzini , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Marcel Apfelbaum , Alexandre Iooss , Eduardo Habkost , Daniel Henrique Barboza , Yanan Wang , Nicholas Piggin , David Hildenbrand , devel@lists.libvirt.org, Mahmoud Mandour , Zhao Liu , =?utf-8?q?Alex_Benn=C3=A9e?= , Pierrick Bouvier , Elisha Hollander Subject: [PATCH v2 14/18] util/timer: avoid deadlock when shutting down Date: Mon, 16 Sep 2024 09:53:56 +0100 Message-Id: <20240916085400.1046925-15-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20240916085400.1046925-1-alex.bennee@linaro.org> References: <20240916085400.1046925-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::332; envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x332.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 Reviewed-by: Pierrick Bouvier --- 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);