From nobody Sat Nov 15 17:47:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1748497677; cv=none; d=zohomail.com; s=zohoarc; b=Ug+tpNJI01ndKCa+PLldNRiRegjWbHF0i1ajntkldDqcMbJFSYvQ556M9xOgzk8LFnua0LlK8C5JoLYd1Gxv7wMMN5bD2Epp2Cqt+ZVitrA4WLXt9nzy8IjhrR8A/IB54s/KC6lX5g6J30XMIwtyjvbyu8urHnsDJ06KPHOgV88= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748497677; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=yTPfHSkJeZJYv7e6JgZMLSvgYFLhEBeDXdNvOQ5ttIE=; b=lq8FobX9pxxa3wHxIxlvYTJdZOLCezjewiLv43LXui47c5OS8Feb+ftkd19qFTOPwWkmaVLiwhrFkgYLbPhmjH7PpIOBF69Aw9z6S54Q9bNwjMQBb6ZSi9YFxeFhwHCjfGDZ7wRxyac8uNF/aeYwaP8N9KPFyiL4oZpoE1IGHHk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1748497677565935.6683755968594; Wed, 28 May 2025 22:47:57 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uKW5p-0002xO-KU; Thu, 29 May 2025 01:46:17 -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 1uKW5i-0002vo-Ho for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:12 -0400 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uKW5c-00039M-BA for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:09 -0400 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-23461842024so5475395ad.0 for ; Wed, 28 May 2025 22:46:04 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-23506cd3739sm4972805ad.121.2025.05.28.22.46.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 May 2025 22:46:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748497563; x=1749102363; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=yTPfHSkJeZJYv7e6JgZMLSvgYFLhEBeDXdNvOQ5ttIE=; b=KSSBuTYX/8FoiWKnHXA00+pqWAchXh20pr4IcSfh4WdzipxeLnltQfIQvc9yqsKH0U Jn9NWybIWIaGEVyNaST2aEucB1Ah7C4+HL3dVLQ+n0PTXkcZsCBUc5tMhgew73kjAwuk lBtcPn0INvzOHFHITrduMElhxjlf1cabzTZ5x5+gWVgDLjNhraBNdtjtMSaUD6bSE2Po KNN+cj5zY3oXy5mNX7dKNL51rRP8Es9uwndhuE/3jtxwS44ffrqKLC8aHr/UFA04BAAY SnVAOjmTako8/gmGmnnmfGppfCi25mPnph0sW5qTSKixiztNkP26nDqs2ghRh9B+GQCC pVxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748497563; x=1749102363; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yTPfHSkJeZJYv7e6JgZMLSvgYFLhEBeDXdNvOQ5ttIE=; b=gBZl0KSEr+TWBucXWz8wEjKC/Bie0kIJlp6PUyYmQttwNYcStY5th0ogBsP4jxPu4U c7To5bW0SlmSdEIaFzMEdiUYRDRXDamdyVRKK/1RjiMLUa+opcbMjCU419NznUu4GlgT ELRSdTZE9LpVOAKUqNpdc1KIVFoErfJ8beACf0nzksKPdlIOimA7QnkPMfyJl9I9eetL 9wkuHTwM27K6ESI1OUHDaFr+hKdhQY1nag4Xz5uzxOgOD/BrWHSlD09nfv4p4NRjYasP ZECnLV6MIh8yXaIDxUPtTR5zHcGVVSCOgPscVJe5xI18+6F1raMopDy09KIencuqKBRM CdcQ== X-Forwarded-Encrypted: i=1; AJvYcCXTyoxX8Il/4fGkI9HqFo1PoAHV4pm/dDRgJsZpLSwvJfFuVamHfudH7pfYDIizSlHve8lS9LeHOabX@nongnu.org X-Gm-Message-State: AOJu0YwWDI0oqnTdEYYeATb2oQmMGdvuKCVBdsg2ufP3dvimCDNCZcqi f8Px89FCap37XXJsZ4wR6xN7rXyLNw+vrYajl3ewM81Ua/Uu9K0lVYgNZAmbfa7flwY= X-Gm-Gg: ASbGncuriK5QaP+e1guwm9rJUQApZlg6CEAuk5jR444DrwjG5E0hyUOinWv7g3Uk65t VJNUx23mvij1Pc54dyhLEx3CpiiFxXvRw5N/aLS64c5zSo8nGioSdqDZq2fek9sqpkBA4EOd3Sj Qf6cWua88o0EbryW6yieKnH9ynalgXh74ZyL2gW8Wm+dtsnnVm4xn8qTr1nLt8WYCCB/qc2RlZf ISS4tb1YkOOHtc+6V+MVnWX6m64B2xiF5rlVJQIGL32AjmDfzdh6WI5yCYmF5NpdJTqEaJmtg9c nGoT4B0gyzdFaM6GJyX3U/2VwxJksGhYJ0w1MTET8mgl+wWai0Zx X-Google-Smtp-Source: AGHT+IETkAV62v2PtvSSYGejLtPv1IBIkk6o4AmjkPg3xMqOHdi5Jy2lQ5IDW7AGiL+Q/dWcaaQAIw== X-Received: by 2002:a17:902:ce06:b0:234:b430:cea7 with SMTP id d9443c01a7336-234b430d040mr150993615ad.22.1748497563001; Wed, 28 May 2025 22:46:03 -0700 (PDT) From: Akihiko Odaki Date: Thu, 29 May 2025 14:45:50 +0900 Subject: [PATCH v5 01/13] futex: Check value after qemu_futex_wait() MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250529-event-v5-1-53b285203794@daynix.com> References: <20250529-event-v5-0-53b285203794@daynix.com> In-Reply-To: <20250529-event-v5-0-53b285203794@daynix.com> To: Paolo Bonzini , Stefan Weil , Peter Xu , Fabiano Rosas , Hailiang Zhang Cc: Phil Dennis-Jordan , qemu-devel@nongnu.org, devel@daynix.com, =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::636; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x636.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1748497679870116600 futex(2) - Linux manual page https://man7.org/linux/man-pages/man2/futex.2.html > Note that a wake-up can also be caused by common futex usage patterns > in unrelated code that happened to have previously used the futex > word's memory location (e.g., typical futex-based implementations of > Pthreads mutexes can cause this under some conditions). Therefore, > callers should always conservatively assume that a return value of 0 > can mean a spurious wake-up, and use the futex word's value (i.e., > the user-space synchronization scheme) to decide whether to continue > to block or not. Signed-off-by: Akihiko Odaki --- include/qemu/futex.h | 9 +++++++++ tests/unit/test-aio-multithread.c | 4 +++- util/qemu-thread-posix.c | 35 +++++++++++++---------------------- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/include/qemu/futex.h b/include/qemu/futex.h index 91ae88966e12..f57774005330 100644 --- a/include/qemu/futex.h +++ b/include/qemu/futex.h @@ -24,6 +24,15 @@ static inline void qemu_futex_wake(void *f, int n) qemu_futex(f, FUTEX_WAKE, n, NULL, NULL, 0); } =20 +/* + * Note that a wake-up can also be caused by common futex usage patterns in + * unrelated code that happened to have previously used the futex word's + * memory location (e.g., typical futex-based implementations of Pthreads + * mutexes can cause this under some conditions). Therefore, callers shou= ld + * always conservatively assume that it is a spurious wake-up, and use the= futex + * word's value (i.e., the user-space synchronization scheme) to decide wh= ether + * to continue to block or not. + */ static inline void qemu_futex_wait(void *f, unsigned val) { while (qemu_futex(f, FUTEX_WAIT, (int) val, NULL, NULL, 0)) { diff --git a/tests/unit/test-aio-multithread.c b/tests/unit/test-aio-multit= hread.c index 08d4570ccb14..8c2e41545a29 100644 --- a/tests/unit/test-aio-multithread.c +++ b/tests/unit/test-aio-multithread.c @@ -305,7 +305,9 @@ static void mcs_mutex_lock(void) prev =3D qatomic_xchg(&mutex_head, id); if (prev !=3D -1) { qatomic_set(&nodes[prev].next, id); - qemu_futex_wait(&nodes[id].locked, 1); + while (qatomic_read(&nodes[id].locked) =3D=3D 1) { + qemu_futex_wait(&nodes[id].locked, 1); + } } } =20 diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index b2e26e21205b..04fc3bf2298e 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -394,11 +394,7 @@ void qemu_event_set(QemuEvent *ev) */ smp_mb(); if (qatomic_read(&ev->value) !=3D EV_SET) { - int old =3D qatomic_xchg(&ev->value, EV_SET); - - /* Pairs with memory barrier in kernel futex_wait system call. */ - smp_mb__after_rmw(); - if (old =3D=3D EV_BUSY) { + if (qatomic_xchg(&ev->value, EV_SET) =3D=3D EV_BUSY) { /* There were waiters, wake them up. */ qemu_futex_wake(ev, INT_MAX); } @@ -428,17 +424,17 @@ void qemu_event_wait(QemuEvent *ev) =20 assert(ev->initialized); =20 - /* - * qemu_event_wait must synchronize with qemu_event_set even if it does - * not go down the slow path, so this load-acquire is needed that - * synchronizes with the first memory barrier in qemu_event_set(). - * - * If we do go down the slow path, there is no requirement at all: we - * might miss a qemu_event_set() here but ultimately the memory barrie= r in - * qemu_futex_wait() will ensure the check is done correctly. - */ - value =3D qatomic_load_acquire(&ev->value); - if (value !=3D EV_SET) { + while (true) { + /* + * qemu_event_wait must synchronize with qemu_event_set even if it= does + * not go down the slow path, so this load-acquire is needed that + * synchronizes with the first memory barrier in qemu_event_set(). + */ + value =3D qatomic_load_acquire(&ev->value); + if (value =3D=3D EV_SET) { + break; + } + if (value =3D=3D EV_FREE) { /* * Leave the event reset and tell qemu_event_set that there are @@ -452,15 +448,10 @@ void qemu_event_wait(QemuEvent *ev) * like the load above. */ if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) =3D=3D EV_SE= T) { - return; + break; } } =20 - /* - * This is the final check for a concurrent set, so it does need - * a smp_mb() pairing with the second barrier of qemu_event_set(). - * The barrier is inside the FUTEX_WAIT system call. - */ qemu_futex_wait(ev, EV_BUSY); } } --=20 2.49.0 From nobody Sat Nov 15 17:47:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1748497660; cv=none; d=zohomail.com; s=zohoarc; b=EMoGkCWI3nFH98mZRyXRzizcWBZifp3yUMMV2gGGnkGL2BsoBY4iQGI4FKRhR91cvbQuxYHkTomMkCQDFnRSBAvDckos00ti6dQCRShbUUXv0pRYx6Hq6v/E5xM6fhF+j97zgQoM+A6aAmjStKEnbxJLJdbewrpe6LOA+dLMlXI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748497660; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=PBZfXv948HfseffVRyzxzpmxhQPEU+TqtO1OogUChiE=; b=fjPYnGGGPjh0gK4gg++vAOALsvAaWT+fyCrg4E+kSbsT90z4ZEszDJFoGo1IX9ItES4lI8BZxTY6SDT21yP+BxcRAJGFp+vPLpvDsHAICUYr2SeW3Aq3sT7I78o2u+4fS5Sen0oOErQAaa5zTkB5d6qzqDXZ9y5kqxTDiLNiqYw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1748497660438548.0227930599738; Wed, 28 May 2025 22:47:40 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uKW5p-0002xG-Am; Thu, 29 May 2025 01:46:17 -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 1uKW5k-0002wG-C8 for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:12 -0400 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uKW5f-00039U-C0 for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:12 -0400 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-7376dd56f8fso574147b3a.2 for ; Wed, 28 May 2025 22:46:07 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-747afff72a5sm563313b3a.170.2025.05.28.22.46.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 May 2025 22:46:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748497566; x=1749102366; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=PBZfXv948HfseffVRyzxzpmxhQPEU+TqtO1OogUChiE=; b=0Nypep0+IjHwWNFjdiLVI1kUSUBna5YD4hNSui8aMjcTT3Tz2ybFw883a4LW1BQkCC Q+yylvFnKUh650JuA5z40WWH4mIE/s58mqxgc3+dNA2q9zJHMYxADOcH6vDoxtvSkhcP veykJMl93KpCUHwJEBTjdE98Fnqv6bbnmXrmWWaiAzce8bT4aIc3Lfj0Q5WAC4GvjFz6 eBvbB4dnslqW0SkCzYhdIqRng5e5BU5oMzxBvIfkUa/LMzv9Abja/swcJDixBQq3VGMj RilFt4vQpTni9fdta45blAvo8Pql8MAD4mv8jESBo4RoRoB1l+T3SM5Gz+2F/3Q2vdfA /+uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748497566; x=1749102366; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PBZfXv948HfseffVRyzxzpmxhQPEU+TqtO1OogUChiE=; b=ZxdiGAgCY4kwIs0o6CtPN/HL4trJvNT2JqhSNjotU4JW5e4xGhnSFb2OAZLvgcL2fb KoMrW2DDg0CPAY7aAhGEhAw3moHSjh1X9dwIJUYBzmtA4I7RNjKDlovlRiTC3sKIc5/L lb7jIpE7M+LDtXLyEt9sByjqMbou0S+6mDv4Z/76hQiueLXVZHPbb5ARWJZ5aWGaNMK4 OQwpQi8m8/NIVLQ4Ij5GEVzKiyu9UNIYCNjEO2HcM0y24UKkW2tLNNmXb2/XWahqKvxb aPIdWqDh4fzqPkEJa2oblI5nh4dMUdbrebNL8iQxV84vEi71VA6tLkfuhOvuUB8T4k3T nFsw== X-Forwarded-Encrypted: i=1; AJvYcCWPPAZcQeAd/UNLo3dvOjIyonyXlGnGuXvoD6c7/1GX14ynbzQaqwRXBsgytd68JhZqEcMLut71h7tt@nongnu.org X-Gm-Message-State: AOJu0YxYhwjQLdLml35ZHIJQp1NiPRZNNhG/bkE4JKSWr4rCsKQqhpbV Pg/6hii+HT0Pmr4DEndyWEqHQ+LmjFvOeYnws6/+dGvGBRMcZ3K5v/IM5MbXlX3hSbQ= X-Gm-Gg: ASbGnctXNXGzmMy+gBWURNFVGfgvUwEogH5u5HqP6rVcw0uiRO4ikxHY1sdviwBQJST dKz1JvhK7sa/hjhJt8kMfcTcJqxe/MwXhBvVGJ/Qv/mUNfGqTTULdnyDSbBMvYoFz8bgSOR1jeW ZSAfMT7yFJHUJCrcy9x2R5OyhiP0b8Zk2qAiuZNVrz+3PfpN6sEgMQM/WG5sGulZU5noQbcNcej PdMyQUD+vwXJGDiJdN9ldnfwL8WJ9X4LaZeaM26iPNGFjD4967asLeco31e2f1soj7IbKeN63Mq Pbt4dZvhzpDVaFHfUn5/ZgSlkUk+CKk6kFp2rWd8Us1javwIWNkS X-Google-Smtp-Source: AGHT+IF4Rh/S5ZiauhCJpQWBQujyCnsuxddNt32oEpEKm3yf87/C3KxHcdUbaQR9pOtHw93XZiAaJA== X-Received: by 2002:a05:6a00:2289:b0:73e:30dc:bb9b with SMTP id d2e1a72fcca58-747b0c8d05amr1354880b3a.2.1748497566010; Wed, 28 May 2025 22:46:06 -0700 (PDT) From: Akihiko Odaki Date: Thu, 29 May 2025 14:45:51 +0900 Subject: [PATCH v5 02/13] futex: Support Windows MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250529-event-v5-2-53b285203794@daynix.com> References: <20250529-event-v5-0-53b285203794@daynix.com> In-Reply-To: <20250529-event-v5-0-53b285203794@daynix.com> To: Paolo Bonzini , Stefan Weil , Peter Xu , Fabiano Rosas , Hailiang Zhang Cc: Phil Dennis-Jordan , qemu-devel@nongnu.org, devel@daynix.com, =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::42d; envelope-from=akihiko.odaki@daynix.com; helo=mail-pf1-x42d.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1748497661509116600 Windows supports futex-like APIs since Windows 8 and Windows Server 2012. Signed-off-by: Akihiko Odaki --- meson.build | 2 ++ include/qemu/futex.h | 53 ++++++++++++++++++++++++++++++-----= ---- tests/unit/test-aio-multithread.c | 2 +- util/lockcnt.c | 2 +- util/qemu-thread-posix.c | 4 +-- util/meson.build | 2 +- 6 files changed, 48 insertions(+), 17 deletions(-) diff --git a/meson.build b/meson.build index ad2053f968b8..d865a2ff778e 100644 --- a/meson.build +++ b/meson.build @@ -837,11 +837,13 @@ emulator_link_args =3D [] midl =3D not_found widl =3D not_found pathcch =3D not_found +synchronization =3D not_found host_dsosuf =3D '.so' if host_os =3D=3D 'windows' midl =3D find_program('midl', required: false) widl =3D find_program('widl', required: false) pathcch =3D cc.find_library('pathcch') + synchronization =3D cc.find_library('Synchronization') socket =3D cc.find_library('ws2_32') winmm =3D cc.find_library('winmm') =20 diff --git a/include/qemu/futex.h b/include/qemu/futex.h index f57774005330..607613eec835 100644 --- a/include/qemu/futex.h +++ b/include/qemu/futex.h @@ -1,5 +1,5 @@ /* - * Wrappers around Linux futex syscall + * Wrappers around Linux futex syscall and similar * * Copyright Red Hat, Inc. 2017 * @@ -11,28 +11,37 @@ * */ =20 +/* + * Note that a wake-up can also be caused by common futex usage patterns in + * unrelated code that happened to have previously used the futex word's + * memory location (e.g., typical futex-based implementations of Pthreads + * mutexes can cause this under some conditions). Therefore, qemu_futex_w= ait() + * callers should always conservatively assume that it is a spurious wake-= up, + * and use the futex word's value (i.e., the user-space synchronization sc= heme) + * to decide whether to continue to block or not. + */ + #ifndef QEMU_FUTEX_H #define QEMU_FUTEX_H =20 +#define HAVE_FUTEX + +#ifdef CONFIG_LINUX #include #include =20 #define qemu_futex(...) syscall(__NR_futex, __VA_ARGS__) =20 -static inline void qemu_futex_wake(void *f, int n) +static inline void qemu_futex_wake_all(void *f) { - qemu_futex(f, FUTEX_WAKE, n, NULL, NULL, 0); + qemu_futex(f, FUTEX_WAKE, INT_MAX, NULL, NULL, 0); +} + +static inline void qemu_futex_wake_single(void *f) +{ + qemu_futex(f, FUTEX_WAKE, 1, NULL, NULL, 0); } =20 -/* - * Note that a wake-up can also be caused by common futex usage patterns in - * unrelated code that happened to have previously used the futex word's - * memory location (e.g., typical futex-based implementations of Pthreads - * mutexes can cause this under some conditions). Therefore, callers shou= ld - * always conservatively assume that it is a spurious wake-up, and use the= futex - * word's value (i.e., the user-space synchronization scheme) to decide wh= ether - * to continue to block or not. - */ static inline void qemu_futex_wait(void *f, unsigned val) { while (qemu_futex(f, FUTEX_WAIT, (int) val, NULL, NULL, 0)) { @@ -46,5 +55,25 @@ static inline void qemu_futex_wait(void *f, unsigned val) } } } +#elif defined(CONFIG_WIN32) +#include + +static inline void qemu_futex_wake_all(void *f) +{ + WakeByAddressAll(f); +} + +static inline void qemu_futex_wake_single(void *f) +{ + WakeByAddressSingle(f); +} + +static inline void qemu_futex_wait(void *f, unsigned val) +{ + WaitOnAddress(f, &val, sizeof(val), INFINITE); +} +#else +#undef HAVE_FUTEX +#endif =20 #endif /* QEMU_FUTEX_H */ diff --git a/tests/unit/test-aio-multithread.c b/tests/unit/test-aio-multit= hread.c index 8c2e41545a29..0ead6bf34ad1 100644 --- a/tests/unit/test-aio-multithread.c +++ b/tests/unit/test-aio-multithread.c @@ -330,7 +330,7 @@ static void mcs_mutex_unlock(void) /* Wake up the next in line. */ next =3D qatomic_read(&nodes[id].next); nodes[next].locked =3D 0; - qemu_futex_wake(&nodes[next].locked, 1); + qemu_futex_wake_single(&nodes[next].locked); } =20 static void test_multi_fair_mutex_entry(void *opaque) diff --git a/util/lockcnt.c b/util/lockcnt.c index d07c6cc5cee4..ca27d8e61a5c 100644 --- a/util/lockcnt.c +++ b/util/lockcnt.c @@ -106,7 +106,7 @@ static bool qemu_lockcnt_cmpxchg_or_wait(QemuLockCnt *l= ockcnt, int *val, static void lockcnt_wake(QemuLockCnt *lockcnt) { trace_lockcnt_futex_wake(lockcnt); - qemu_futex_wake(&lockcnt->count, 1); + qemu_futex_wake_single(&lockcnt->count); } =20 void qemu_lockcnt_inc(QemuLockCnt *lockcnt) diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index 04fc3bf2298e..4d6f24d705c7 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -345,7 +345,7 @@ static inline void qemu_futex_wait(QemuEvent *ev, unsig= ned val) =20 /* Valid transitions: * - free->set, when setting the event - * - busy->set, when setting the event, followed by qemu_futex_wake + * - busy->set, when setting the event, followed by qemu_futex_wake_all * - set->free, when resetting the event * - free->busy, when waiting * @@ -396,7 +396,7 @@ void qemu_event_set(QemuEvent *ev) if (qatomic_read(&ev->value) !=3D EV_SET) { if (qatomic_xchg(&ev->value, EV_SET) =3D=3D EV_BUSY) { /* There were waiters, wake them up. */ - qemu_futex_wake(ev, INT_MAX); + qemu_futex_wake_all(ev); } } } diff --git a/util/meson.build b/util/meson.build index 1adff96ebd51..5735f65f1994 100644 --- a/util/meson.build +++ b/util/meson.build @@ -27,7 +27,7 @@ else util_ss.add(files('event_notifier-win32.c')) util_ss.add(files('oslib-win32.c')) util_ss.add(files('qemu-thread-win32.c')) - util_ss.add(winmm, pathcch) + util_ss.add(winmm, pathcch, synchronization) endif util_ss.add(when: linux_io_uring, if_true: files('fdmon-io_uring.c')) if glib_has_gslice --=20 2.49.0 From nobody Sat Nov 15 17:47:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1748497693; cv=none; d=zohomail.com; s=zohoarc; b=g1+14DNVTcnoxevQl3fXvVbKfrbmcxhrz1XB3Bk1U5ya5HHaevxxrC/Ih+KKLNIMUReBJxbseBoy2hOetqXZHQwZC798LQHGVl1uSg9l2xvoToeFlnM7VmrvvGYBzQZyEYzywAv+jCqArH/9m56h9MwQfOtlYSFo8MR+ygZgHPE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748497693; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=tnhDHBHT8UpHOJ0AArQ0HTYILZ+Nf8KL3aRskmQW/xU=; b=M5ZetsJJTJXpSAKWm2w+Rn8Hm+ulsTrIVUu9XpltNQdhdB9oqCN93Mv/jV+AUMvYVyQtbduU9zZRqiWOYxw44NChwLkii2z3fD2AFLi/i6UXeDV5Slcx26s6v1ByPSvSJhAM1jQh8iQ8fPZJUZrGhkx/G+h5NiK1BstBAuzi50k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1748497693351437.77790288784456; Wed, 28 May 2025 22:48:13 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uKW5q-0002xg-8c; Thu, 29 May 2025 01:46:18 -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 1uKW5k-0002wO-KK for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:12 -0400 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uKW5i-00039m-MT for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:12 -0400 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-742c5f3456fso378610b3a.0 for ; Wed, 28 May 2025 22:46:10 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-747afeaba4fsm567686b3a.63.2025.05.28.22.46.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 May 2025 22:46:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748497569; x=1749102369; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=tnhDHBHT8UpHOJ0AArQ0HTYILZ+Nf8KL3aRskmQW/xU=; b=O4aQz2Lo5icVm2ElyHoSslEpVJJ6cF3xyN7JP9sJbq5/UrSgBJywUzuiOW46pKNFJa CnJwarpVL0DB+G91KmqcEXBnTl2WRc76xGgadlDgI/Qac+0a4HdeEokRzbLizNcKZvrz Ev3KNBgDJrRS9hkTABbI2MhVL7FgIci/77yaZ7jIYVivq+Y05kwtsNQuz9hlxByzivq4 BTdwr2cJ80C1TPh5xAEuHo9dpNoF9YRmMIbgjYD9v/M1uL2mGWkRdhUdKCCN6dPHCx2J 57Cyi8xN8qKjvhP1k6mdWdolMNrYKh18j5uqGKsUU40CXPh6vdE6vMH/2t1pjUmJoRVQ 5CSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748497569; x=1749102369; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tnhDHBHT8UpHOJ0AArQ0HTYILZ+Nf8KL3aRskmQW/xU=; b=RAuzuZ/HGuGhiYvReGztiI2TddyS70H6Cdvj5mlKwcMAYyAcfZvdZ3WdYjo6KdIf9H 4ygiVYdbNsRJ4+YDl8KphevhZwFD97YUJXT16wQFRkJ3Dq8FoLWYbb/y6oL+W4OIFGFA g7HLMcuEkYYyZyymuiiGnqqWof6lcDm0UC5ahwkQfjuJXAT7g3tGHmtda4R6lLp2rW5Y JnSAi+i5zSSqk5pC33cUrBVlWIDxEF4lSBdfPBEmy9G15jgi78FnMJ1k2q/Mpnasduov jzM91IzQjxotHZ6UFESPgX1tJRc3VEhSDKMcJxoVhGae8xC+4UEprBVcSbw1+F9Shgki +n5A== X-Forwarded-Encrypted: i=1; AJvYcCUyc2LqF+zp3KYlZ5Z7RLc6GY+YaHQcarzP/SiyqCmryg3IkjBgeNb9KSitjpl44+A2Y1PIXKBVuKMi@nongnu.org X-Gm-Message-State: AOJu0Yy8q1xTH6eEotY068Lfj4hcXkcBawS/EiFlX0yWmb0vQ6wsa4aA 3MrcMePYhZFimwruLlZnM9oHfkErrB1ES31zwe4PYes1XMpQgvjO+8cwYSSm6fiSZmQ= X-Gm-Gg: ASbGncvc1z+wN0gVfGs5zyTHFkyEDuBQ0gmdKjYJopD9pMTl3ceuMSgOKMJcXKidLQE 0WF9nAf7FNNqqqiihKBrQ4eH5kbx6wiwF36M0Y55ya4q1yxeHHARwjO7u1vfLMjjgqF8B5IcJCI Rm/Jn/Z8OTLXN4qbRbuJatggcGCFx9+KQad4yp7voVzwZ6UOAHRscExYpEs/TKub62GmgUXFG+J IYxU5lp/kyC/VroYwiWh16K+qw60i6zWjPX5cQ8mP1NBoz4mYDHjKPnbgstSjXyzMnsiC6klF2o w7ReJF5pvQ7eUYuOf+EoJpLW4hB20zJT4zKQhNbayImPgkxUWHCh4MsqeZB3/r8= X-Google-Smtp-Source: AGHT+IGbvxN6Qo6GM6mEN++Vg1ZAJFx2h79irkAF1x8Fx0th1UhjHdKtQRXXkPndmwwDDViWDeUrVA== X-Received: by 2002:a05:6a00:a96:b0:740:6f69:f52a with SMTP id d2e1a72fcca58-745fdac80demr28303009b3a.0.1748497569098; Wed, 28 May 2025 22:46:09 -0700 (PDT) From: Akihiko Odaki Date: Thu, 29 May 2025 14:45:52 +0900 Subject: [PATCH v5 03/13] qemu-thread: Replace __linux__ with CONFIG_LINUX MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250529-event-v5-3-53b285203794@daynix.com> References: <20250529-event-v5-0-53b285203794@daynix.com> In-Reply-To: <20250529-event-v5-0-53b285203794@daynix.com> To: Paolo Bonzini , Stefan Weil , Peter Xu , Fabiano Rosas , Hailiang Zhang Cc: Phil Dennis-Jordan , qemu-devel@nongnu.org, devel@daynix.com, =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::42e; envelope-from=akihiko.odaki@daynix.com; helo=mail-pf1-x42e.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1748497693763116600 scripts/checkpatch.pl warns for __linux__ saying "architecture specific defines should be avoided". Signed-off-by: Akihiko Odaki Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- include/qemu/thread-posix.h | 2 +- util/qemu-thread-posix.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/qemu/thread-posix.h b/include/qemu/thread-posix.h index 5f2f3d1386bc..c412623a9143 100644 --- a/include/qemu/thread-posix.h +++ b/include/qemu/thread-posix.h @@ -33,7 +33,7 @@ struct QemuSemaphore { }; =20 struct QemuEvent { -#ifndef __linux__ +#ifndef CONFIG_LINUX pthread_mutex_t lock; pthread_cond_t cond; #endif diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index 4d6f24d705c7..21ed12ddb56b 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -317,7 +317,7 @@ void qemu_sem_wait(QemuSemaphore *sem) qemu_mutex_unlock(&sem->mutex); } =20 -#ifdef __linux__ +#ifdef CONFIG_LINUX #include "qemu/futex.h" #else static inline void qemu_futex_wake(QemuEvent *ev, int n) @@ -363,7 +363,7 @@ static inline void qemu_futex_wait(QemuEvent *ev, unsig= ned val) =20 void qemu_event_init(QemuEvent *ev, bool init) { -#ifndef __linux__ +#ifndef CONFIG_LINUX pthread_mutex_init(&ev->lock, NULL); pthread_cond_init(&ev->cond, NULL); #endif @@ -376,7 +376,7 @@ void qemu_event_destroy(QemuEvent *ev) { assert(ev->initialized); ev->initialized =3D false; -#ifndef __linux__ +#ifndef CONFIG_LINUX pthread_mutex_destroy(&ev->lock); pthread_cond_destroy(&ev->cond); #endif --=20 2.49.0 From nobody Sat Nov 15 17:47:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1748497689; cv=none; d=zohomail.com; s=zohoarc; b=jAT+NGq/Twi70jDBqtOVm9mIITuYr+ltKrSL4AptiiAp/ksJxUSsj5XPcxmpOMHpIXSLEqI2oXVwsuwXK9litYg9jLXUaeCmUFNw7rsf1XnOeNl9yc1v1ZsgJeSn03cQoLLCOFInqUyqjnCr8wk9IgMLtdwD1hmzK8qSpEWk0Ss= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748497689; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=48X/OtmA+PKwfi2j51VbH6eWdr4k3IlX3/dT9ZbelTw=; b=c2YeP80996++/7tD8eZVJPtnLh/ptq4MCQh2Nrxdr0B73L94cJ9ojq7RK1sLYRtpeWBi6m3HZTGOAnE4jStVL73PNV8UpWD6OhVjL6jgSTeGWu6cLQ7cJ3mJkKs4oSrzwAfaF7YfWM3mTc3eWEnotcnsSNoAcdLf4iO4T9lRlwo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 174849768994897.45577387993649; Wed, 28 May 2025 22:48:09 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uKW5r-0002xv-OW; Thu, 29 May 2025 01:46:19 -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 1uKW5n-0002x5-Dd for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:15 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uKW5l-0003AK-O5 for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:15 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-22e09f57ed4so15172905ad.0 for ; Wed, 28 May 2025 22:46:13 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-23506cfcbb9sm4980285ad.208.2025.05.28.22.46.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 May 2025 22:46:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748497572; x=1749102372; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=48X/OtmA+PKwfi2j51VbH6eWdr4k3IlX3/dT9ZbelTw=; b=oZKqknpyU0rRP3qoixdk7n2nyMhC2EJUAirJTPEF7iUwn1YcTZBhBWmJtBt9BxC0H8 ijOUofDmLrUwAfun+litk8pjzewwmJCLtFa9iXf+jMUsbDOzuP/lbbmjw9+Q+HTlHqDT kuH/U/lMMqKKsIzvDQpVk4tgUExy/BVc6qUvXNqrd07pRpwSah23G52uuz5dMN7JIlbm 0nTtZVfEVKUA2oAmMEATC/T76C72LAUoBFrrnY6OX8KUyPs2uOJeDS8WmH8CEUnSxfdJ CunHe9Ddp4xsvr2pBnW92fR1MNaebggT9IA8Yct88VJjN0RQOYARmQlIk8slo622zWsP xLcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748497572; x=1749102372; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=48X/OtmA+PKwfi2j51VbH6eWdr4k3IlX3/dT9ZbelTw=; b=ZqCLoSqWd3iC2GON7GA6ZVKS7KnfAALONKNYE0cjyKphg9gkBq9ck1sLe//hPQmh0D 89u3S8q3O0WnCHqOchNZNqkYXutYGHtzsVcnVLIBSX1CO5LoXhYddDRkKXau7Db0ZScl /ydvxMvTUWA/k6A1l0IJLGSXRjDdhQM1oTaVbcMb/fo1d5Eyg3LodAQo51woD0ZnTF9U vekd9yAnGIVk9OeRLJLzVOM3nuBGH6YHkxX0WZZUfD76hQR7NzvK++IWKiykvjQTrOgb SpvDYFbZJpMz0atSDeUziiYqNS7VFc837dlJJwz0SI1hA4L1QlrVFLrFEweG5kOo22gg uIKQ== X-Forwarded-Encrypted: i=1; AJvYcCUM1SmiXDzZL0KpFh5MyAnpRBILPv2aSeG0+XL8tVPWwwUQgQXtLi+VKP4YCPX0Eu+uwpFZ08k7KC7J@nongnu.org X-Gm-Message-State: AOJu0Yywx7yp2EsQr8PFFzUcKne1Kvs4Q/gmbW8+FYw7cokpI84lmoAd SqhdR+IUw4aiJxILtvPG654mDh7KwM8g4JqFotCraF/VnYIZ/VgMhwupryF4UmLzFvQ= X-Gm-Gg: ASbGncstny7EIvZA7ZH9nST/OlFYYAqopSJJ+VoJr6BSSXrZ1Pq1AuKJCq8EIK+OkBS aiPx7OiAQQk62Ys0S28Zc53ZtFXAw1RY64cxA4Xr6v6bO3vVk/gt44COB+cegs/ELyegT+29MDN ycvQV5KWoZBIiqPLJaoV0RdmQnxEnKp14NKjDmzc3duqpWb5zzpJoQIuaIrzULIDGhjqo9yDf+L Mdoq2Pk8Zse74vrBGdvBOFf1Yu5F/OjOEiYAsv3dau7v75faFrz93mg5OKKiqq++3bn+g8dNUQJ I3Nef//hAosFyx2C1SbZQxlhuD5+G7uUUQ04MuT8/Wr5y5JPev/n X-Google-Smtp-Source: AGHT+IEMmuUo6nqdmK7xUpAjL7XIVrVsIQ6VjJqAV34R58fLhBm6Pgd/jEsLxgrJfQm3Phx4/MfDng== X-Received: by 2002:a17:903:32cf:b0:233:c5ef:3b88 with SMTP id d9443c01a7336-234f6780f92mr32126515ad.5.1748497572194; Wed, 28 May 2025 22:46:12 -0700 (PDT) From: Akihiko Odaki Date: Thu, 29 May 2025 14:45:53 +0900 Subject: [PATCH v5 04/13] qemu-thread: Avoid futex abstraction for non-Linux MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250529-event-v5-4-53b285203794@daynix.com> References: <20250529-event-v5-0-53b285203794@daynix.com> In-Reply-To: <20250529-event-v5-0-53b285203794@daynix.com> To: Paolo Bonzini , Stefan Weil , Peter Xu , Fabiano Rosas , Hailiang Zhang Cc: Phil Dennis-Jordan , qemu-devel@nongnu.org, devel@daynix.com, =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x632.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1748497691842116600 qemu-thread used to abstract pthread primitives into futex for the QemuEvent implementation of POSIX systems other than Linux. However, this abstraction has one key difference: unlike futex, pthread primitives require an explicit destruction, and it must be ordered after wait and wake operations. It would be easier to perform destruction if a wait operation ensures the corresponding wake operation finishes as POSIX semaphore does, but that requires to protect state accesses in qemu_event_set() and qemu_event_wait() with a mutex. On the other hand, real futex does not need such a protection but needs complex barrier and atomic operations to ensure ordering between the two functions. Add special implementations of qemu_event_set() and qemu_event_wait() using pthread primitives. qemu_event_wait() will ensure qemu_event_set() finishes, and these functions will avoid complex barrier and atomic operations to ensure ordering between them. Signed-off-by: Akihiko Odaki Tested-by: Phil Dennis-Jordan Reviewed-by: Phil Dennis-Jordan --- util/qemu-thread-posix.c | 50 +++++++++++++++++++++++---------------------= ---- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index 21ed12ddb56b..dc13b61f1bd8 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -319,36 +319,21 @@ void qemu_sem_wait(QemuSemaphore *sem) =20 #ifdef CONFIG_LINUX #include "qemu/futex.h" -#else -static inline void qemu_futex_wake(QemuEvent *ev, int n) -{ - assert(ev->initialized); - pthread_mutex_lock(&ev->lock); - if (n =3D=3D 1) { - pthread_cond_signal(&ev->cond); - } else { - pthread_cond_broadcast(&ev->cond); - } - pthread_mutex_unlock(&ev->lock); -} - -static inline void qemu_futex_wait(QemuEvent *ev, unsigned val) -{ - assert(ev->initialized); - pthread_mutex_lock(&ev->lock); - if (ev->value =3D=3D val) { - pthread_cond_wait(&ev->cond, &ev->lock); - } - pthread_mutex_unlock(&ev->lock); -} #endif =20 /* Valid transitions: * - free->set, when setting the event - * - busy->set, when setting the event, followed by qemu_futex_wake_all + * - busy->set, when setting the event * - set->free, when resetting the event * - free->busy, when waiting * + * With futex, the waking and blocking operations follow busy->set and + * free->busy, respectively. + * + * Without futex, busy->set and free->busy never happen. Instead, the waki= ng + * operation follows free->set and the blocking operation will happen when + * waiting if the event is not set. + * * set->busy does not happen (it can be observed from the outside but * it really is set->free->busy). * @@ -386,6 +371,7 @@ void qemu_event_set(QemuEvent *ev) { assert(ev->initialized); =20 +#ifdef CONFIG_LINUX /* * Pairs with both qemu_event_reset() and qemu_event_wait(). * @@ -399,6 +385,12 @@ void qemu_event_set(QemuEvent *ev) qemu_futex_wake_all(ev); } } +#else + pthread_mutex_lock(&ev->lock); + qatomic_store_release(&ev->value, EV_SET); + pthread_cond_broadcast(&ev->cond); + pthread_mutex_unlock(&ev->lock); +#endif } =20 void qemu_event_reset(QemuEvent *ev) @@ -420,17 +412,16 @@ void qemu_event_reset(QemuEvent *ev) =20 void qemu_event_wait(QemuEvent *ev) { - unsigned value; - assert(ev->initialized); =20 +#ifdef CONFIG_LINUX while (true) { /* * qemu_event_wait must synchronize with qemu_event_set even if it= does * not go down the slow path, so this load-acquire is needed that * synchronizes with the first memory barrier in qemu_event_set(). */ - value =3D qatomic_load_acquire(&ev->value); + unsigned value =3D qatomic_load_acquire(&ev->value); if (value =3D=3D EV_SET) { break; } @@ -454,6 +445,13 @@ void qemu_event_wait(QemuEvent *ev) =20 qemu_futex_wait(ev, EV_BUSY); } +#else + pthread_mutex_lock(&ev->lock); + while (qatomic_read(&ev->value) !=3D EV_SET) { + pthread_cond_wait(&ev->cond, &ev->lock); + } + pthread_mutex_unlock(&ev->lock); +#endif } =20 static __thread NotifierList thread_exit; --=20 2.49.0 From nobody Sat Nov 15 17:47:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1748497711; cv=none; d=zohomail.com; s=zohoarc; b=O1fJXLDneJBV3kAJNUVNmIl1RHY9Uzl0Vwyu5BnLe1VEqhsU/oCzxKlS/tfaGUBnRB/UVAnJ+wcT53xJTrRcGs1/zmxo4TrGSMlmSgEz+dLURl3uFibGbDB1sGXB8yBd1qCgzoOJqcvB7sS5FW3GUAtjwF9icTno/Y76+GEqFFM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748497711; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Ht3SM+0+jB27uCclc/G29FDcuTXTxlxDzjlbSsqKrWU=; b=f4O3u8wzXkb6lxGh7rczrswA9vsuh2kRT6sxmUxaAl0bJDI2GR9ur5Ppj7laetN6HV83Fxzhp/3G3OJL94ZvEjJXBSsQIDueKlnx5ns9d3vKT6b20d25TFs1YCJpYC2Mr+w+wxokUP4ryPCjAYMfjhCHYEfwBw2ETBtBobIr+4E= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1748497711931493.9177621169614; Wed, 28 May 2025 22:48:31 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uKW5u-0002zQ-Aj; Thu, 29 May 2025 01:46:22 -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 1uKW5r-0002yA-GR for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:19 -0400 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uKW5o-0003Ad-UO for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:19 -0400 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-7399838db7fso548813b3a.0 for ; Wed, 28 May 2025 22:46:16 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-747affd478bsm548531b3a.155.2025.05.28.22.46.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 May 2025 22:46:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748497575; x=1749102375; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Ht3SM+0+jB27uCclc/G29FDcuTXTxlxDzjlbSsqKrWU=; b=hlKttj0k48awKuRremAIYQmStSdMHb23vkb9VqZMSPt+hEGHQT4zOd3FFK1eRIZvEm qOazhYDXFDLNKqRPFR93aNrM6w2MWaRGaKdPxXrVPhSOxioochs13qb9+YtMwRefQywn 4fWtOWOHcEfc4UrVSuqAfblXb552awTKfasHtEv39YofjwYZk8X0iRtzK3ka7vYA3s8Y 8o7/M1JZ/90RYuSn1cYjzwo6TQUdOBeiQyebttQxEmEVxE1IciOSipG9r9BvXA2e9YIX 32IN2JI6wroV9sy//ViHVYDhmkOG1OuYoBpV6zxLIK6lIb2rsnRLT0CTIfUKWmVscKoK 2daA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748497575; x=1749102375; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ht3SM+0+jB27uCclc/G29FDcuTXTxlxDzjlbSsqKrWU=; b=oCGNXTj3qcYJEomcal92C61Q52vi/bcOHnjY+aXEGAC0c50agABV/nTYiRJRakbFhX IvZog9lG/K4Vc38y2Xi7sX4kvo56tRkkSS5MCtUxRSXGNwyVmbctzmEqeqBOCoAvUfKO jPb+07RpNPHJ9gD+UaxopoFkF3UKUJf/Bp/Nz5JRnye3z8dNWNGgBkx/073KCzgbL381 eXBONMNELSmXN9D0UqCiW39X77rALHy8bZFP4h9w3HD9JGUApnv21kbAU8LcDoeicX3B zKE7DX/mGRYtzy6hk/PKtFepcOd06EqeQq//wLR96BD2b5wwFlgFqeXEpR0L5YLREwhW KOeg== X-Forwarded-Encrypted: i=1; AJvYcCWcEZtwrK66yOSRr7G/uHXi77n4DosEU8XWtrkxmLvZghcex1yM6gtvrEgrL1yStsz1fCEERsaTvTVl@nongnu.org X-Gm-Message-State: AOJu0YwXsKWLkaKlQIfNLN+R4dpD7+8ZjGfxCoitJJwZug9quMT2IgbN VRMnhihit6NkmD/lDgyoL2QHenphLvBaaZ1EGv4z5Vg+lKjhbIAFQenz8NkPnLTG5uQ= X-Gm-Gg: ASbGncumrhQYORRKKmjxdSznH3rjvTnNsbtRzG6PBdrvBuFpMWmtvihENkk1Y1bEM7c Nr7ZnE+18MPbFFAeh5mni4kuhYU6B/WUP4+6iKJgz5FJ4uBV7ekD20yy4hGmbRHiVVhFVGNSBVi OqH7WWqGgGPt5F+WUtjm5y1icO2Hdnyf+IXgBmuN6yW/GWdlrBblFsWKugwrFXhjOMT+xROKWbn pawRg37G1TFsGRoBIg7iEruv1OHBKg7o/3f6k1jGsM4v/HHyasOU20xS5jGUSRXEjwaJ8kKp8mw OcfK3TObiR+Oa+0ZQwzgRKeQfoD34sipYYvWxk14ktL9PFMdmHJV X-Google-Smtp-Source: AGHT+IHEE/R5lJQsWEwmjuBX77b6pyIOkjWtMtBjycog7eUSbZEjWknccI5mh7Ig9emBPETOB4M5aQ== X-Received: by 2002:a05:6a21:7316:b0:215:d64d:412e with SMTP id adf61e73a8af0-21ac5cd92d0mr3435381637.20.1748497575231; Wed, 28 May 2025 22:46:15 -0700 (PDT) From: Akihiko Odaki Date: Thu, 29 May 2025 14:45:54 +0900 Subject: [PATCH v5 05/13] qemu-thread: Use futex for QemuEvent on Windows MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250529-event-v5-5-53b285203794@daynix.com> References: <20250529-event-v5-0-53b285203794@daynix.com> In-Reply-To: <20250529-event-v5-0-53b285203794@daynix.com> To: Paolo Bonzini , Stefan Weil , Peter Xu , Fabiano Rosas , Hailiang Zhang Cc: Phil Dennis-Jordan , qemu-devel@nongnu.org, devel@daynix.com, =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::431; envelope-from=akihiko.odaki@daynix.com; helo=mail-pf1-x431.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1748497714239116600 Use the futex-based implementation of QemuEvent on Windows to remove code duplication and remove the overhead of event object construction and destruction. Signed-off-by: Akihiko Odaki Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- include/qemu/thread-posix.h | 9 --- include/qemu/thread-win32.h | 6 -- include/qemu/thread.h | 11 +++- util/event.c | 138 ++++++++++++++++++++++++++++++++++++++++= ++++ util/qemu-thread-posix.c | 137 ----------------------------------------= --- util/qemu-thread-win32.c | 129 ----------------------------------------- util/meson.build | 1 + 7 files changed, 149 insertions(+), 282 deletions(-) diff --git a/include/qemu/thread-posix.h b/include/qemu/thread-posix.h index c412623a9143..758808b705e4 100644 --- a/include/qemu/thread-posix.h +++ b/include/qemu/thread-posix.h @@ -32,15 +32,6 @@ struct QemuSemaphore { unsigned int count; }; =20 -struct QemuEvent { -#ifndef CONFIG_LINUX - pthread_mutex_t lock; - pthread_cond_t cond; -#endif - unsigned value; - bool initialized; -}; - struct QemuThread { pthread_t thread; }; diff --git a/include/qemu/thread-win32.h b/include/qemu/thread-win32.h index d95af4498fc9..da9e7322990c 100644 --- a/include/qemu/thread-win32.h +++ b/include/qemu/thread-win32.h @@ -28,12 +28,6 @@ struct QemuSemaphore { bool initialized; }; =20 -struct QemuEvent { - int value; - HANDLE event; - bool initialized; -}; - typedef struct QemuThreadData QemuThreadData; struct QemuThread { QemuThreadData *data; diff --git a/include/qemu/thread.h b/include/qemu/thread.h index 6f800aad31a9..573f8c9ede20 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -3,13 +3,22 @@ =20 #include "qemu/processor.h" #include "qemu/atomic.h" +#include "qemu/futex.h" =20 typedef struct QemuCond QemuCond; typedef struct QemuSemaphore QemuSemaphore; -typedef struct QemuEvent QemuEvent; typedef struct QemuLockCnt QemuLockCnt; typedef struct QemuThread QemuThread; =20 +typedef struct QemuEvent { +#ifndef HAVE_FUTEX + pthread_mutex_t lock; + pthread_cond_t cond; +#endif + unsigned value; + bool initialized; +} QemuEvent; + #ifdef _WIN32 #include "qemu/thread-win32.h" #else diff --git a/util/event.c b/util/event.c new file mode 100644 index 000000000000..e937804a92a8 --- /dev/null +++ b/util/event.c @@ -0,0 +1,138 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "qemu/osdep.h" +#include "qemu/thread.h" + +/* + * Valid transitions: + * - free->set, when setting the event + * - busy->set, when setting the event + * - set->free, when resetting the event + * - free->busy, when waiting + * + * With futex, the waking and blocking operations follow busy->set and + * free->busy, respectively. + * + * Without futex, busy->set and free->busy never happen. Instead, the waki= ng + * operation follows free->set and the blocking operation will happen when + * waiting if the event is not set. + * + * set->busy does not happen (it can be observed from the outside but + * it really is set->free->busy). + * + * busy->free provably cannot happen; to enforce it, the set->free transit= ion + * is done with an OR, which becomes a no-op if the event has concurrently + * transitioned to free or busy. + */ + +#define EV_SET 0 +#define EV_FREE 1 +#define EV_BUSY -1 + +void qemu_event_init(QemuEvent *ev, bool init) +{ +#ifndef HAVE_FUTEX + pthread_mutex_init(&ev->lock, NULL); + pthread_cond_init(&ev->cond, NULL); +#endif + + ev->value =3D (init ? EV_SET : EV_FREE); + ev->initialized =3D true; +} + +void qemu_event_destroy(QemuEvent *ev) +{ + assert(ev->initialized); + ev->initialized =3D false; +#ifndef HAVE_FUTEX + pthread_mutex_destroy(&ev->lock); + pthread_cond_destroy(&ev->cond); +#endif +} + +void qemu_event_set(QemuEvent *ev) +{ + assert(ev->initialized); + +#ifdef HAVE_FUTEX + /* + * Pairs with both qemu_event_reset() and qemu_event_wait(). + * + * qemu_event_set has release semantics, but because it *loads* + * ev->value we need a full memory barrier here. + */ + smp_mb(); + if (qatomic_read(&ev->value) !=3D EV_SET) { + if (qatomic_xchg(&ev->value, EV_SET) =3D=3D EV_BUSY) { + /* There were waiters, wake them up. */ + qemu_futex_wake_all(ev); + } + } +#else + pthread_mutex_lock(&ev->lock); + qatomic_store_release(&ev->value, EV_SET); + pthread_cond_broadcast(&ev->cond); + pthread_mutex_unlock(&ev->lock); +#endif +} + +void qemu_event_reset(QemuEvent *ev) +{ + assert(ev->initialized); + + /* + * If there was a concurrent reset (or even reset+wait), + * do nothing. Otherwise change EV_SET->EV_FREE. + */ + qatomic_or(&ev->value, EV_FREE); + + /* + * Order reset before checking the condition in the caller. + * Pairs with the first memory barrier in qemu_event_set(). + */ + smp_mb__after_rmw(); +} + +void qemu_event_wait(QemuEvent *ev) +{ + assert(ev->initialized); + +#ifdef HAVE_FUTEX + while (true) { + /* + * qemu_event_wait must synchronize with qemu_event_set even if it= does + * not go down the slow path, so this load-acquire is needed that + * synchronizes with the first memory barrier in qemu_event_set(). + */ + unsigned value =3D qatomic_load_acquire(&ev->value); + if (value =3D=3D EV_SET) { + break; + } + + if (value =3D=3D EV_FREE) { + /* + * Leave the event reset and tell qemu_event_set that there are + * waiters. No need to retry, because there cannot be a concu= rrent + * busy->free transition. After the CAS, the event will be ei= ther + * set or busy. + * + * This cmpxchg doesn't have particular ordering requirements = if it + * succeeds (moving the store earlier can only cause + * qemu_event_set() to issue _more_ wakeups), the failing case= needs + * acquire semantics like the load above. + */ + if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) =3D=3D EV_SE= T) { + break; + } + } + + qemu_futex_wait(ev, EV_BUSY); + } +#else + pthread_mutex_lock(&ev->lock); + while (qatomic_read(&ev->value) !=3D EV_SET) { + pthread_cond_wait(&ev->cond, &ev->lock); + } + pthread_mutex_unlock(&ev->lock); +#endif +} diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index dc13b61f1bd8..ba725444ba63 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -317,143 +317,6 @@ void qemu_sem_wait(QemuSemaphore *sem) qemu_mutex_unlock(&sem->mutex); } =20 -#ifdef CONFIG_LINUX -#include "qemu/futex.h" -#endif - -/* Valid transitions: - * - free->set, when setting the event - * - busy->set, when setting the event - * - set->free, when resetting the event - * - free->busy, when waiting - * - * With futex, the waking and blocking operations follow busy->set and - * free->busy, respectively. - * - * Without futex, busy->set and free->busy never happen. Instead, the waki= ng - * operation follows free->set and the blocking operation will happen when - * waiting if the event is not set. - * - * set->busy does not happen (it can be observed from the outside but - * it really is set->free->busy). - * - * busy->free provably cannot happen; to enforce it, the set->free transit= ion - * is done with an OR, which becomes a no-op if the event has concurrently - * transitioned to free or busy. - */ - -#define EV_SET 0 -#define EV_FREE 1 -#define EV_BUSY -1 - -void qemu_event_init(QemuEvent *ev, bool init) -{ -#ifndef CONFIG_LINUX - pthread_mutex_init(&ev->lock, NULL); - pthread_cond_init(&ev->cond, NULL); -#endif - - ev->value =3D (init ? EV_SET : EV_FREE); - ev->initialized =3D true; -} - -void qemu_event_destroy(QemuEvent *ev) -{ - assert(ev->initialized); - ev->initialized =3D false; -#ifndef CONFIG_LINUX - pthread_mutex_destroy(&ev->lock); - pthread_cond_destroy(&ev->cond); -#endif -} - -void qemu_event_set(QemuEvent *ev) -{ - assert(ev->initialized); - -#ifdef CONFIG_LINUX - /* - * Pairs with both qemu_event_reset() and qemu_event_wait(). - * - * qemu_event_set has release semantics, but because it *loads* - * ev->value we need a full memory barrier here. - */ - smp_mb(); - if (qatomic_read(&ev->value) !=3D EV_SET) { - if (qatomic_xchg(&ev->value, EV_SET) =3D=3D EV_BUSY) { - /* There were waiters, wake them up. */ - qemu_futex_wake_all(ev); - } - } -#else - pthread_mutex_lock(&ev->lock); - qatomic_store_release(&ev->value, EV_SET); - pthread_cond_broadcast(&ev->cond); - pthread_mutex_unlock(&ev->lock); -#endif -} - -void qemu_event_reset(QemuEvent *ev) -{ - assert(ev->initialized); - - /* - * If there was a concurrent reset (or even reset+wait), - * do nothing. Otherwise change EV_SET->EV_FREE. - */ - qatomic_or(&ev->value, EV_FREE); - - /* - * Order reset before checking the condition in the caller. - * Pairs with the first memory barrier in qemu_event_set(). - */ - smp_mb__after_rmw(); -} - -void qemu_event_wait(QemuEvent *ev) -{ - assert(ev->initialized); - -#ifdef CONFIG_LINUX - while (true) { - /* - * qemu_event_wait must synchronize with qemu_event_set even if it= does - * not go down the slow path, so this load-acquire is needed that - * synchronizes with the first memory barrier in qemu_event_set(). - */ - unsigned value =3D qatomic_load_acquire(&ev->value); - if (value =3D=3D EV_SET) { - break; - } - - if (value =3D=3D EV_FREE) { - /* - * Leave the event reset and tell qemu_event_set that there are - * waiters. No need to retry, because there cannot be a concu= rrent - * busy->free transition. After the CAS, the event will be ei= ther - * set or busy. - * - * This cmpxchg doesn't have particular ordering requirements = if it - * succeeds (moving the store earlier can only cause qemu_even= t_set() - * to issue _more_ wakeups), the failing case needs acquire se= mantics - * like the load above. - */ - if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) =3D=3D EV_SE= T) { - break; - } - } - - qemu_futex_wait(ev, EV_BUSY); - } -#else - pthread_mutex_lock(&ev->lock); - while (qatomic_read(&ev->value) !=3D EV_SET) { - pthread_cond_wait(&ev->cond, &ev->lock); - } - pthread_mutex_unlock(&ev->lock); -#endif -} - static __thread NotifierList thread_exit; =20 /* diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c index a7fe3cc345f0..ca2e0b512e26 100644 --- a/util/qemu-thread-win32.c +++ b/util/qemu-thread-win32.c @@ -231,135 +231,6 @@ void qemu_sem_wait(QemuSemaphore *sem) } } =20 -/* Wrap a Win32 manual-reset event with a fast userspace path. The idea - * is to reset the Win32 event lazily, as part of a test-reset-test-wait - * sequence. Such a sequence is, indeed, how QemuEvents are used by - * RCU and other subsystems! - * - * Valid transitions: - * - free->set, when setting the event - * - busy->set, when setting the event, followed by SetEvent - * - set->free, when resetting the event - * - free->busy, when waiting - * - * set->busy does not happen (it can be observed from the outside but - * it really is set->free->busy). - * - * busy->free provably cannot happen; to enforce it, the set->free transit= ion - * is done with an OR, which becomes a no-op if the event has concurrently - * transitioned to free or busy (and is faster than cmpxchg). - */ - -#define EV_SET 0 -#define EV_FREE 1 -#define EV_BUSY -1 - -void qemu_event_init(QemuEvent *ev, bool init) -{ - /* Manual reset. */ - ev->event =3D CreateEvent(NULL, TRUE, TRUE, NULL); - ev->value =3D (init ? EV_SET : EV_FREE); - ev->initialized =3D true; -} - -void qemu_event_destroy(QemuEvent *ev) -{ - assert(ev->initialized); - ev->initialized =3D false; - CloseHandle(ev->event); -} - -void qemu_event_set(QemuEvent *ev) -{ - assert(ev->initialized); - - /* - * Pairs with both qemu_event_reset() and qemu_event_wait(). - * - * qemu_event_set has release semantics, but because it *loads* - * ev->value we need a full memory barrier here. - */ - smp_mb(); - if (qatomic_read(&ev->value) !=3D EV_SET) { - int old =3D qatomic_xchg(&ev->value, EV_SET); - - /* Pairs with memory barrier after ResetEvent. */ - smp_mb__after_rmw(); - if (old =3D=3D EV_BUSY) { - /* There were waiters, wake them up. */ - SetEvent(ev->event); - } - } -} - -void qemu_event_reset(QemuEvent *ev) -{ - assert(ev->initialized); - - /* - * If there was a concurrent reset (or even reset+wait), - * do nothing. Otherwise change EV_SET->EV_FREE. - */ - qatomic_or(&ev->value, EV_FREE); - - /* - * Order reset before checking the condition in the caller. - * Pairs with the first memory barrier in qemu_event_set(). - */ - smp_mb__after_rmw(); -} - -void qemu_event_wait(QemuEvent *ev) -{ - unsigned value; - - assert(ev->initialized); - - /* - * qemu_event_wait must synchronize with qemu_event_set even if it does - * not go down the slow path, so this load-acquire is needed that - * synchronizes with the first memory barrier in qemu_event_set(). - * - * If we do go down the slow path, there is no requirement at all: we - * might miss a qemu_event_set() here but ultimately the memory barrie= r in - * qemu_futex_wait() will ensure the check is done correctly. - */ - value =3D qatomic_load_acquire(&ev->value); - if (value !=3D EV_SET) { - if (value =3D=3D EV_FREE) { - /* - * Here the underlying kernel event is reset, but qemu_event_s= et is - * not yet going to call SetEvent. However, there will be ano= ther - * check for EV_SET below when setting EV_BUSY. At that point= it - * is safe to call WaitForSingleObject. - */ - ResetEvent(ev->event); - - /* - * It is not clear whether ResetEvent provides this barrier; k= ernel - * APIs (KeResetEvent/KeClearEvent) do not. Better safe than = sorry! - */ - smp_mb(); - - /* - * Leave the event reset and tell qemu_event_set that there are - * waiters. No need to retry, because there cannot be a concu= rrent - * busy->free transition. After the CAS, the event will be ei= ther - * set or busy. - */ - if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) =3D=3D EV_SE= T) { - return; - } - } - - /* - * ev->value is now EV_BUSY. Since we didn't observe EV_SET, - * qemu_event_set() must observe EV_BUSY and call SetEvent(). - */ - WaitForSingleObject(ev->event, INFINITE); - } -} - struct QemuThreadData { /* Passed to win32_start_routine. */ void *(*start_routine)(void *); diff --git a/util/meson.build b/util/meson.build index 5735f65f1994..35029380a376 100644 --- a/util/meson.build +++ b/util/meson.build @@ -35,6 +35,7 @@ if glib_has_gslice endif util_ss.add(files('defer-call.c')) util_ss.add(files('envlist.c', 'path.c', 'module.c')) +util_ss.add(files('event.c')) util_ss.add(files('host-utils.c')) util_ss.add(files('bitmap.c', 'bitops.c')) util_ss.add(files('fifo8.c')) --=20 2.49.0 From nobody Sat Nov 15 17:47:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1748497648; cv=none; d=zohomail.com; s=zohoarc; b=kOxIaqX/Q46sAx0eTtbg8pBybNKwOQB6GuGAUZWUh32xQFN7PkBuv60W2Zt0EuLouskPztcjONN7G5fJ1UBz/O1lLv7tdu7ZCKv2C2+IIQYJKzWEwmsStHkzPQQdn8cVIUvMYCzUSEZcxMjXSiawoqSCE3xVii6rowvzK7aJHVw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748497648; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=tpEeShMgw5miBNRuoBOGCybk4Qn7aitvfKahD59h2tI=; b=CqRjbATKy8JU8wQ08G6KlggEdJBjSzIecp9K3itqCDDI7flRjg9BQhcxROo5PJYvBhaxVdYgD1mKWn4+M2am3gEjb4RxW2gZWMTkQp3nByO7fPYFcR70lZ9+398CslDg/rns5xj3cM/tPnxNgsOtVLzk+YuCMEsbWZ7FuGCiM5E= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1748497648540340.09070203777344; Wed, 28 May 2025 22:47:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uKW5w-00030U-Jd; Thu, 29 May 2025 01:46:24 -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 1uKW5t-0002zN-3x for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:22 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uKW5r-0003Bc-J4 for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:20 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-234f17910d8so5319125ad.3 for ; Wed, 28 May 2025 22:46:19 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-23506bdb532sm5078045ad.81.2025.05.28.22.46.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 May 2025 22:46:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748497578; x=1749102378; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=tpEeShMgw5miBNRuoBOGCybk4Qn7aitvfKahD59h2tI=; b=eZ9bDppiwoM1SXYSHxH/qfzffpg4O4OmM6zv6X54LCadYOfg48WsldW959gq2zUOAP vwyt9RJE+O+fhcQnFpBDqKCxRtlVd21BNUlzrLzC5US9uGDyI6yAeJR5V/hSUn4/c10S dlBxynmaXjkl42DdH++BwZMLomERrsS9YjEQ5aTWmpkq2IujtL9+L5Xcf8xHgNIdS3iq jqRvZ5ECcSWo5XzfA1ZrxAuGT1Wq8BZ8IH652PXGGmqeEbL7p0Jb3Og/F5vL5rVBkDrp pcGSn6mJlmRBJVFa5uD4fv4T3ibtdJaudXIMtLBxvF5lAhOgYEV4edTCJN7pyaQX89sP Zy+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748497578; x=1749102378; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tpEeShMgw5miBNRuoBOGCybk4Qn7aitvfKahD59h2tI=; b=ueNjd0WmkRFiI4+4TtDDdR+Mggh/s6RMdy86oraGfc/taPVr90gSTYxTaSp7WpY6J0 3IlAvs5y1pY6RMZgzSslzKzJpfjPsFu7/C+Iwwcw6HRsV7lLiHp6snGd5xdrCqyGquq+ B9wEParXLxIfgeW9NKf8DgFHcfN2b2fOwOTyZhinXK7YXnh3uD7HbvOAq+IUn/DZMcyc O5qmC/cd4U+8r+/GaPrRBXOdqyhOQk2y9lw1KDYO/7+GfQdAmyCYjm1s4nHq1fLw7RcE +uXzNYUb+h27uxRE8jQpuPSHGYmMEFNY9vHQU3fwfoCPx3jFhjhLNRrivhW4d/MtZNO7 eJoA== X-Forwarded-Encrypted: i=1; AJvYcCVBr0B8efgzWMVYrnmnppyxG2WR9xfITxGV1pzejZZX19HhumcU75q2cgTN/Rnnu4YZIHDk5OPsSTwm@nongnu.org X-Gm-Message-State: AOJu0Yy1Yg4xJ3GXft4fNJi6vkg8ospxZLRE2IQ66McVAlNqnExdWHx+ R5H2s61lpfLvCey5as+VogLCf1fmMxmQCam4+v0aZ5h6hZoHLDtqac6TKf0aK/wrGta/6ZFffZx gGXHq X-Gm-Gg: ASbGnctlJKN88nLZrNj1N4RT0h09rfF3eaXna606Sp0Fk4FnzW7NrVCtgRrQAb79mU9 2eGQhr/Scst1Lx/jdVUmaRj660FnfHM1HsDL+5uCkzo0vzVF8T9QfBRqlr1He66NiGEGq6iqStz ZghZzUlDBijvNByWUHhbyM93WxA+AyBne2nGYturulUOyEQqLontktJu6ZsY0CFWDkXZdPjWx11 4VwoLh5zBOU+PL1TYpAtKpi0ZOtC/7YXoQF6DYq8loYtI2kYAzx9gJ9N9arni2Ht7ZqvBvZnnj4 F/lEZ9IpTA61/60xRc3FRJoTfz6KMjTWEtI+j1Wkto5PruLQCIv3 X-Google-Smtp-Source: AGHT+IFGFfwYtfqqoWbUrux+PrgQ6ADwgdosJvR7J44Erx/OzxG3ES6qAdWdge204GhlPL32ivvf7g== X-Received: by 2002:a17:903:15c7:b0:235:2e0:aa9 with SMTP id d9443c01a7336-23502e00e47mr33557045ad.14.1748497578152; Wed, 28 May 2025 22:46:18 -0700 (PDT) From: Akihiko Odaki Date: Thu, 29 May 2025 14:45:55 +0900 Subject: [PATCH v5 06/13] qemu-thread: Use futex if available for QemuLockCnt MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250529-event-v5-6-53b285203794@daynix.com> References: <20250529-event-v5-0-53b285203794@daynix.com> In-Reply-To: <20250529-event-v5-0-53b285203794@daynix.com> To: Paolo Bonzini , Stefan Weil , Peter Xu , Fabiano Rosas , Hailiang Zhang Cc: Phil Dennis-Jordan , qemu-devel@nongnu.org, devel@daynix.com, =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x632.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1748497649178116600 This unlocks the futex-based implementation of QemuLockCnt to Windows. Signed-off-by: Akihiko Odaki --- include/qemu/lockcnt.h | 2 +- util/lockcnt.c | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/qemu/lockcnt.h b/include/qemu/lockcnt.h index f4b62a3f7011..5a2800e3f182 100644 --- a/include/qemu/lockcnt.h +++ b/include/qemu/lockcnt.h @@ -17,7 +17,7 @@ typedef struct QemuLockCnt QemuLockCnt; =20 struct QemuLockCnt { -#ifndef CONFIG_LINUX +#ifndef HAVE_FUTEX QemuMutex mutex; #endif unsigned count; diff --git a/util/lockcnt.c b/util/lockcnt.c index ca27d8e61a5c..92c9f8ceca88 100644 --- a/util/lockcnt.c +++ b/util/lockcnt.c @@ -12,10 +12,11 @@ #include "qemu/atomic.h" #include "trace.h" =20 -#ifdef CONFIG_LINUX -#include "qemu/futex.h" +#ifdef HAVE_FUTEX =20 -/* On Linux, bits 0-1 are a futex-based lock, bits 2-31 are the counter. +/* + * When futex is available, bits 0-1 are a futex-based lock, bits 2-31 are= the + * counter. * For the mutex algorithm see Ulrich Drepper's "Futexes Are Tricky" (ok, * this is not the most relaxing citation I could make...). It is similar * to mutex2 in the paper. --=20 2.49.0 From nobody Sat Nov 15 17:47:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1748497648; cv=none; d=zohomail.com; s=zohoarc; b=mgYIoZVdSi2HSRrMWuyofwzYxPxfvl1s+UeC/R11OI9vJZa4TDJyDLS9CCslOxTHCRH5rDLoFn2VWNUe1C4hOa0CDV5yiZ9jPKWD1xus4unKxQMoX7/JD/tBE9npBsH9zfuSQuR+fT2RBA7K1H52JPk4MlJbKmi4VqtXh+MXVyI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748497648; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=awxH8im/pfEWosRDyhhjz6r63zsYHaX2/Wznr/3cAS0=; b=mrQRXmuRoWEvL2Txbg7coFD3oJ3k3uS4H7hxJzQigWOvFxNq3SPrYIgrwU0L40aVFBoUiiVjlrQGZx6ft1flZFma5IZzpaH1/tD9ua5CjqMwYiSS60+x4mD+YpuR3ix397u/6ZZwUwtiIiv83ojsqNOytv8Qsn7mXw/NOxUcezA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1748497648131320.70150456323097; Wed, 28 May 2025 22:47:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uKW69-00032V-83; Thu, 29 May 2025 01:46:37 -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 1uKW5x-00030c-H7 for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:25 -0400 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uKW5u-0003CJ-PM for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:25 -0400 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-2349f096605so6816225ad.3 for ; Wed, 28 May 2025 22:46:22 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-23506cf9100sm4998315ad.198.2025.05.28.22.46.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 May 2025 22:46:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748497581; x=1749102381; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=awxH8im/pfEWosRDyhhjz6r63zsYHaX2/Wznr/3cAS0=; b=B+BFS4eRqEz4tXIOKD6j4WMZwz2kIaI5lZpg3oUZHI0sQc0nMTfRllN3+cmFD6hgOe JzfKpJzyJykC01tw52vVI2dCbs8cauGFhBAP2OuXViJqQWTuWhx9FbLGvjhKH6liMjw7 p4ZLVzWWbvGhFuSZa0tshJtjLVMrcPDyLMdRGXww0eS837BW0PDbbewGlTushsjyT5BX thDfLAFaJa+ylF9shKqyrVsY1+8ChbSGlBmiY90OLxNofmq0jdkD42kdOCiOOTCwiZhb ph4tsq0PGaTS4dP2FJSDN34B28vB/ymhu0Ap9RVVjNOW7+5q3kSDDKGQbMglgcTacyJZ QJ4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748497581; x=1749102381; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=awxH8im/pfEWosRDyhhjz6r63zsYHaX2/Wznr/3cAS0=; b=dZUbU34l8q1Rb+SjSsVWPaT29hkxOAGgCDvYPUL270fSjmfDWrSXMvn+NOj4jPhgSd gwIJX3+P9eqKRLQp6u8S4T4mfGbEgszTMlzJDLlD0UZXMAuOjOC3MlMcB6zBxqC4BmFb EV0tF6gtNan+H7ykOn/kX9+tnWSO2nLPse2/bNTKXHW591/fG7DWiCVcWwU+zCRDcS3m e/S+M8TZVPt5VCzhYaZR3YsgISTvXPZqzKENeoc4ZvbglFCYAcp1rUDcdi+gO8R29JMC mnJjapSiUgbHMOUVczNcfAQV4Wy5u6SxvHSXYV9rIM86gJT02lC8wJ41pPU4htYKqcGp Ymmw== X-Forwarded-Encrypted: i=1; AJvYcCWq3dJ6MlGnXYbk4WkfUDcxOKjW27BWBBW7q7OwvBLhoEAfUh3JQ2QDBbD+XCmP3sWRb18HRVCxYpsR@nongnu.org X-Gm-Message-State: AOJu0YyXHQJkkMNyvSsQ6qLYqA5p2KgXqe00MV50LfhsC3kqGwAFXBFP 7xZ2SH5LOVYZ/7HYkyUO2QdPd8qg+X2+gO8GNO3+Zi0C/p1JqUJYfoNZ9Dqm6k8Ph+M= X-Gm-Gg: ASbGncvS2+Urf/K1LEJlo6HS+IZJyYWSuzozO+6XOQEcKabsC6Yh/UPmlnfOKOLFcQt HMU1tYuLDUKumm7YOYY/60a8PIVsJi/V1qhCetNVdfVZkIaov+AjBNGCR9lNgRoINHF2R7RXwTH 7waRB8WFda27hh7n48wuPLA0F1t2BpbyM3TSRd0XZl1zBYEHRvzs16Xj/2Nhq+DJW8u4moIUO6I JeI6/2ywiam2rS59B72yGx97n6H7+Nrtcci3REAU58481jVWouYegk/l0E2fWGaMWGdTM/PA2Ro mUgQM7ORjlwVYinMQKWZE5s/WgS4EE3C2uj17G/Zno23iEIYhXQC X-Google-Smtp-Source: AGHT+IH94dbSz+AzpWq7pM3Z8WnPNOLqseWVB1L+39u485bC+5JJe0Eq76WMXK6gaLby0FEbZm3lFg== X-Received: by 2002:a17:902:c941:b0:234:db06:ac0 with SMTP id d9443c01a7336-234db060c39mr58367655ad.45.1748497581020; Wed, 28 May 2025 22:46:21 -0700 (PDT) From: Akihiko Odaki Date: Thu, 29 May 2025 14:45:56 +0900 Subject: [PATCH v5 07/13] migration: Replace QemuSemaphore with QemuEvent MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250529-event-v5-7-53b285203794@daynix.com> References: <20250529-event-v5-0-53b285203794@daynix.com> In-Reply-To: <20250529-event-v5-0-53b285203794@daynix.com> To: Paolo Bonzini , Stefan Weil , Peter Xu , Fabiano Rosas , Hailiang Zhang Cc: Phil Dennis-Jordan , qemu-devel@nongnu.org, devel@daynix.com, =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::636; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x636.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1748497649275116600 pause_event can utilize qemu_event_reset() to discard events. Signed-off-by: Akihiko Odaki Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Peter Xu --- migration/migration.h | 2 +- migration/migration.c | 21 +++++++++------------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/migration/migration.h b/migration/migration.h index d53f7cad84d8..21aa6a3c8fee 100644 --- a/migration/migration.h +++ b/migration/migration.h @@ -379,7 +379,7 @@ struct MigrationState { QemuSemaphore wait_unplug_sem; =20 /* Migration is paused due to pause-before-switchover */ - QemuSemaphore pause_sem; + QemuEvent pause_event; =20 /* The semaphore is used to notify COLO thread that failover is finish= ed */ QemuSemaphore colo_exit_sem; diff --git a/migration/migration.c b/migration/migration.c index 4697732bef91..4098870bce33 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -1630,7 +1630,7 @@ void migration_cancel(void) } /* If the migration is paused, kick it out of the pause */ if (old_state =3D=3D MIGRATION_STATUS_PRE_SWITCHOVER) { - qemu_sem_post(&s->pause_sem); + qemu_event_set(&s->pause_event); } migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLIN= G); } while (s->state !=3D MIGRATION_STATUS_CANCELLING); @@ -2342,7 +2342,7 @@ void qmp_migrate_continue(MigrationStatus state, Erro= r **errp) MigrationStatus_str(s->state)); return; } - qemu_sem_post(&s->pause_sem); + qemu_event_set(&s->pause_event); } =20 int migration_rp_wait(MigrationState *s) @@ -2911,21 +2911,18 @@ static bool migration_switchover_prepare(MigrationS= tate *s) return true; } =20 - /* Since leaving this state is not atomic with posting the semaphore + /* + * Since leaving this state is not atomic with setting the event * it's possible that someone could have issued multiple migrate_conti= nue - * and the semaphore is incorrectly positive at this point; - * the docs say it's undefined to reinit a semaphore that's already - * init'd, so use timedwait to eat up any existing posts. + * and the event is incorrectly set at this point so reset it. */ - while (qemu_sem_timedwait(&s->pause_sem, 1) =3D=3D 0) { - /* This block intentionally left blank */ - } + qemu_event_reset(&s->pause_event); =20 /* Update [POSTCOPY_]ACTIVE to PRE_SWITCHOVER */ migrate_set_state(&s->state, s->state, MIGRATION_STATUS_PRE_SWITCHOVER= ); bql_unlock(); =20 - qemu_sem_wait(&s->pause_sem); + qemu_event_wait(&s->pause_event); =20 bql_lock(); /* @@ -4057,7 +4054,7 @@ static void migration_instance_finalize(Object *obj) qemu_mutex_destroy(&ms->qemu_file_lock); qemu_sem_destroy(&ms->wait_unplug_sem); qemu_sem_destroy(&ms->rate_limit_sem); - qemu_sem_destroy(&ms->pause_sem); + qemu_event_destroy(&ms->pause_event); qemu_sem_destroy(&ms->postcopy_pause_sem); qemu_sem_destroy(&ms->rp_state.rp_sem); qemu_sem_destroy(&ms->rp_state.rp_pong_acks); @@ -4072,7 +4069,7 @@ static void migration_instance_init(Object *obj) ms->state =3D MIGRATION_STATUS_NONE; ms->mbps =3D -1; ms->pages_per_second =3D -1; - qemu_sem_init(&ms->pause_sem, 0); + qemu_event_init(&ms->pause_event, false); qemu_mutex_init(&ms->error_mutex); =20 migrate_params_init(&ms->parameters); --=20 2.49.0 From nobody Sat Nov 15 17:47:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1748497712; cv=none; d=zohomail.com; s=zohoarc; b=MxESHljK2Xj+lnntu/D1x/Kvl/cqskOf5cP/EtVzgiRrh8YoXmd1iy3QIf5ev0JloAHaOzk1VJNNwqJ81c3G39KHO0Qwi8UdMlxTtxTp8BRur1W+CrJ5CKwA+DtUBZzwA6gF6uSuMk+bZppRP0rFuMnWnmPE5Smbe+tPQjNfPyg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748497712; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=VGMllNp11Bzuz5gkfMDJ/VK+KdieqBQO5cT9kGzga8M=; b=Hy/UkRU0doUM6sVsnDaVk6sX32xgxnA0LM7ZPBovgQy8iimBcEO6B2ydshWTsMDuTEzE9eXseBrYb1c32qcnBSGYCV4i66XlX7DCTStniLl9iHGDbhCdApSixfze8JmjEaA8iiPVoYllcCCLFY3exDhn2d4iYdnlX7GkvVC3kLk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 174849771297429.756776885096087; Wed, 28 May 2025 22:48:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uKW6O-00037S-LE; Thu, 29 May 2025 01:46:52 -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 1uKW5y-00030o-SS for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:30 -0400 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uKW5x-0003Ci-5O for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:26 -0400 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-7390d21bb1cso279068b3a.2 for ; Wed, 28 May 2025 22:46:24 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-747afeabb13sm571096b3a.62.2025.05.28.22.46.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 May 2025 22:46:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748497584; x=1749102384; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=VGMllNp11Bzuz5gkfMDJ/VK+KdieqBQO5cT9kGzga8M=; b=xp7K0wtuLoiUBVglce1BvmXNdZfiHxHyEHgZ4Whw+e84LiOZfl5WibgxFW2/bEyPoH En/LsjgX+B21G4iGazvBTCnt7A8KGhQOV3Kgom24fkmCIc3TvtdbD07+O5IdMXPRejnA 5d8gjLy5+nSVbCqH1JACDGzqO3L/X+3sXxhunRg0OcYZtZRk7OSOvQ9p90DUa/s4vwzx g0n3rd6TTXRbz+ZHID/f/N7C9TPRUzLV0Rzsl3j8Sx1c6cwELkbvnt3L8Sww9MLBBEaL Ee6Tl79ATpeRLK+fUjDFLjDVWqv+qa/9DGjvbI9vOXlQ5pMkaZQkAB+Oi8JpU+UFOzxq p5Tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748497584; x=1749102384; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VGMllNp11Bzuz5gkfMDJ/VK+KdieqBQO5cT9kGzga8M=; b=p5tREhCGJA4RjuHuvad93Q8064WmV+BprE/O0wuMRgdXLItFUAqMe0XY4ZdZ5rMeqV jnREjNpx0a7RvJ14b2pd9yO96z4j/U3C2NqqPSTYWt1xKUM0ZoIQHpVljribgptpK8Iy s8OHk7spJQrEUOBg3582eQme91dcxC4+1HrNaygsnz1yMUrz9TNdvR2CnI/KQ+URWUa+ 8a1+N6NZH0V2CYq971+FOJ1RodjU7/2WWOoNHaKJgwgeK455DSFpCiAnvTs8FMULftcP dIf1Nl0xwgIwvNhRY+quav/RYl+nK7zLr9mmzxwz3IKQLBDMBX2lf7HsxvA6EdlCClDB EkpA== X-Forwarded-Encrypted: i=1; AJvYcCWWrFuQTJOq2HN7L276YNiYX965viVoW5RmGtt3eVlvJ1VvPd0gxO0mFIta/YEbXeXBijMb/aQxEKWZ@nongnu.org X-Gm-Message-State: AOJu0YyL6eMyTu5ZKVlyBzW9g7w6A9lZjv6OYM/GuvR4yuXhpOBQdzRb b73pBj2PLG8/XjgxXH87bS5SrukJRiS+2P7LJ1Buk8PK+6D34Gfbmmppnxv3Q+KYID4= X-Gm-Gg: ASbGnctSlM8LFRWoRpmQD42Eb84vIeF+dQTQQhzusSNtY1TzhLNUAe4pZaFfgIQ1dhg loEx88sXbEDePpwVnvdx8b6mcy3ik1ZC1cpV3C/M9qTYOWefCSBxSXvJgeQF3HiFVo5bOvqYhWR loGUDNV3kH8uOepkeRZ3cDERxJzGHUSWTJTix7IQm+92tKzoGmj+GdbBHlkDqwJLdN+Fm6BxyB6 upnb+zT/HRC1ODb/w11FV9hnmKFJiFYjjv9pfAXAzgn96UwballAKHw7pxec1vyqXAhfjXHzrqI PDjg1b7SsW2AeDcKzZRTTYTaWC4L1xsue0J2TIV33yNvpQIGIDnO7qL+iqpYfvk= X-Google-Smtp-Source: AGHT+IFPuA/G8lHqHLXe6+LqzjLsfhKkBa6Rgvc0RJd4f4ToAPYDCTa+zQtCsdw6hFGyE6R74EoPgw== X-Received: by 2002:a05:6a20:734c:b0:1ee:dcd3:80d7 with SMTP id adf61e73a8af0-21a9caef890mr8965126637.0.1748497583869; Wed, 28 May 2025 22:46:23 -0700 (PDT) From: Akihiko Odaki Date: Thu, 29 May 2025 14:45:57 +0900 Subject: [PATCH v5 08/13] migration/colo: Replace QemuSemaphore with QemuEvent MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250529-event-v5-8-53b285203794@daynix.com> References: <20250529-event-v5-0-53b285203794@daynix.com> In-Reply-To: <20250529-event-v5-0-53b285203794@daynix.com> To: Paolo Bonzini , Stefan Weil , Peter Xu , Fabiano Rosas , Hailiang Zhang Cc: Phil Dennis-Jordan , qemu-devel@nongnu.org, devel@daynix.com, =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::431; envelope-from=akihiko.odaki@daynix.com; helo=mail-pf1-x431.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1748497714104116600 colo_exit_sem and colo_incoming_sem represent one-shot events so they can be converted into QemuEvent, which is more lightweight. Signed-off-by: Akihiko Odaki Reviewed-by: Fabiano Rosas Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- migration/migration.h | 6 +++--- migration/colo.c | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/migration/migration.h b/migration/migration.h index 21aa6a3c8fee..aaec471c00f8 100644 --- a/migration/migration.h +++ b/migration/migration.h @@ -186,7 +186,7 @@ struct MigrationIncomingState { =20 /* The coroutine we should enter (back) after failover */ Coroutine *colo_incoming_co; - QemuSemaphore colo_incoming_sem; + QemuEvent colo_incoming_event; =20 /* Optional load threads pool and its thread exit request flag */ ThreadPool *load_threads; @@ -381,8 +381,8 @@ struct MigrationState { /* Migration is paused due to pause-before-switchover */ QemuEvent pause_event; =20 - /* The semaphore is used to notify COLO thread that failover is finish= ed */ - QemuSemaphore colo_exit_sem; + /* The event is used to notify COLO thread that failover is finished */ + QemuEvent colo_exit_event; =20 /* The event is used to notify COLO thread to do checkpoint */ QemuEvent colo_checkpoint_event; diff --git a/migration/colo.c b/migration/colo.c index c976b3ff344d..e0f713c837f5 100644 --- a/migration/colo.c +++ b/migration/colo.c @@ -146,7 +146,7 @@ static void secondary_vm_do_failover(void) return; } /* Notify COLO incoming thread that failover work is finished */ - qemu_sem_post(&mis->colo_incoming_sem); + qemu_event_set(&mis->colo_incoming_event); =20 /* For Secondary VM, jump to incoming co */ if (mis->colo_incoming_co) { @@ -195,7 +195,7 @@ static void primary_vm_do_failover(void) } =20 /* Notify COLO thread that failover work is finished */ - qemu_sem_post(&s->colo_exit_sem); + qemu_event_set(&s->colo_exit_event); } =20 COLOMode get_colo_mode(void) @@ -620,8 +620,8 @@ out: } =20 /* Hope this not to be too long to wait here */ - qemu_sem_wait(&s->colo_exit_sem); - qemu_sem_destroy(&s->colo_exit_sem); + qemu_event_wait(&s->colo_exit_event); + qemu_event_destroy(&s->colo_exit_event); =20 /* * It is safe to unregister notifier after failover finished. @@ -651,7 +651,7 @@ void migrate_start_colo_process(MigrationState *s) s->colo_delay_timer =3D timer_new_ms(QEMU_CLOCK_HOST, colo_checkpoint_notify_timer, NULL); =20 - qemu_sem_init(&s->colo_exit_sem, 0); + qemu_event_init(&s->colo_exit_event, false); colo_process_checkpoint(s); bql_lock(); } @@ -808,11 +808,11 @@ void colo_shutdown(void) case COLO_MODE_PRIMARY: s =3D migrate_get_current(); qemu_event_set(&s->colo_checkpoint_event); - qemu_sem_post(&s->colo_exit_sem); + qemu_event_set(&s->colo_exit_event); break; case COLO_MODE_SECONDARY: mis =3D migration_incoming_get_current(); - qemu_sem_post(&mis->colo_incoming_sem); + qemu_event_set(&mis->colo_incoming_event); break; default: break; @@ -827,7 +827,7 @@ static void *colo_process_incoming_thread(void *opaque) Error *local_err =3D NULL; =20 rcu_register_thread(); - qemu_sem_init(&mis->colo_incoming_sem, 0); + qemu_event_init(&mis->colo_incoming_event, false); =20 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, MIGRATION_STATUS_COLO); @@ -923,8 +923,8 @@ out: } =20 /* Hope this not to be too long to loop here */ - qemu_sem_wait(&mis->colo_incoming_sem); - qemu_sem_destroy(&mis->colo_incoming_sem); + qemu_event_wait(&mis->colo_incoming_event); + qemu_event_destroy(&mis->colo_incoming_event); =20 rcu_unregister_thread(); return NULL; --=20 2.49.0 From nobody Sat Nov 15 17:47:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1748497676; cv=none; d=zohomail.com; s=zohoarc; b=S+l4jWIHgjdqN7yAFi+/wCzwD98q7IZiiGgzCV+6BcVx1Pk0DXzOis0NVRu3+hAha/x7cb7/HCgGDT8nLQlFZt+mdBlidXXrW5g2dg6/VyxBTA3u3QL1p5L/SD9Ne0E3WkO6mE7ewYztqqPER6s2NKmL7yyRlYRHbwH+R6GmHBo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748497676; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=kMU5RXGBbkZhXJ9lXd+1Fr6sB8OdX216UKmxJPnd6lo=; b=Rfl+pIgwQImtjd0R/QC0kFGtlIuu4Du4ss0M/EHOGYz1bnjSw1FVYcyFUeO8n7CEY0YWYxuP4f6tjA3vTP1b2oVRtBnOzcr+XONIbD0T5k7IJPI0SbPtOi/H5ISLl4zyyS/IIK6VqJSwexQa1feS+LX3C2Hl5d2Ox94UnUolBfg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1748497676311756.311146231326; Wed, 28 May 2025 22:47:56 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uKW6L-00036q-B0; Thu, 29 May 2025 01:46:49 -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 1uKW61-00031j-W4 for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:31 -0400 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uKW60-0003DG-6a for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:29 -0400 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-742caef5896so357203b3a.3 for ; Wed, 28 May 2025 22:46:27 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-747afe966easm554014b3a.4.2025.05.28.22.46.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 May 2025 22:46:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748497587; x=1749102387; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=kMU5RXGBbkZhXJ9lXd+1Fr6sB8OdX216UKmxJPnd6lo=; b=Fx+bkKLYALBZ8oourcIz77uNRgF/7LNsCYFpj7XqdakAv+9xp+jeNpeMb6bmjbomUV GCFr2UEZtqq0kMqJ/iTQcqeTOrypmxuKCl3DHmtetAIt2mSs6Dqb5lIvRLZdpBnHCL3G sSCNQST6AZUkvH3pjxMmMU7dDfVCdcDl8ADwcGMAbyPOBTDzIi/0OQ0Jp4CBAON3pXNk f3BAoek37tSHoy87ftcHvOmcAMaBMz3SQ55ATbcxYIeLo3I2xgbRrjHJh2AM9kl+MxIP GUXtJaBkOhlC0EsUHyz0RpSETIBCYl69VqrD09GlqY/IPSEuzrOS/xx6JRC1nAhaOqS1 n1Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748497587; x=1749102387; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kMU5RXGBbkZhXJ9lXd+1Fr6sB8OdX216UKmxJPnd6lo=; b=cXhUYNAGQRIYB/Nikmh9J9R5DUdIbTa/x/6KOGEimDeSR3SpU/cv1a95jh3N7ISIzj Lgz98Jv3ewA5nTNtgnbb4qa4/pscd9ZzdCevzFYdGscUZuVQOMd/utSs/1YQo+lWu6j0 G7gXXlwtVjUcbBqsN00oTSQkacHmnViHPWDhUDVzAlVGAK3KNYrc0SutVhPgLfoHMpvJ xCF5UGAQAZwyLGQBxSgbC0WRU1xDG+Ao5xcnLzONOQFFTxW698xdRBXfNqnqEuEJ2wap 9BCNpc7Gl40VjSlH31UX80IfhIK0up7guQ3fSLBMTo6Fqq4u1gsoX6YCoAwbq9vJ/eVK TKdw== X-Forwarded-Encrypted: i=1; AJvYcCXmtXxoUi5zEVzRWQFEj95vKvsd4k6TF413EhCnS+czra6XN7B+pNgpXa8RznqEIq5C1VqNamIoNiyG@nongnu.org X-Gm-Message-State: AOJu0YwstGPKAFbHgFLx9bewreh7sPpsl1N60cl9EUSN3T96vmjB+AuD CEvwU4/To6NLeFofwbVt/ssNFR4vOSts+qjRSe06jeEi8HrYdjG+ApmdnXpuJFyK/io= X-Gm-Gg: ASbGncsr0k50nUOyCxENxHSax6MxJL+vxU5weu/8KX2U29V+uaWjIWTvXO0pDbKN6YB 1aSuKc70zRUPTp1xfJb/Rhhf2gTqif8AVAJcQ2xIb6f8QY9345oH1VAgaGBfyGnwutO3QmgXS8j bIEcGkQs0oUo6JND2HSOgLsdQVSnILpoPyTbL7NLznwGgP7Iq17GlNQ23zxg0jR1dCwfqIKuYSQ K7k1rcNsd1HoWOEoPce8jtvyi1MaH5HDWWwoqM+Qs7O9SEnbwhzo3BjS/JOhF36wD4RlSf56LuC xiJZef+Z4idhDCIusWbC0FbNNhcVx1z78CfYEIUnbKATHbWdvwh7F8gxJWa9dPo= X-Google-Smtp-Source: AGHT+IG0PmseUyyqUy+mDG0GYcFeHv0ZOD3dpBvwnhmJ9To1EySl8DGNfP7xO1j9uzJ8Bxk3QcJDCg== X-Received: by 2002:a05:6a00:2da7:b0:730:9801:d3e2 with SMTP id d2e1a72fcca58-747b0c8ef15mr1679404b3a.8.1748497586945; Wed, 28 May 2025 22:46:26 -0700 (PDT) From: Akihiko Odaki Date: Thu, 29 May 2025 14:45:58 +0900 Subject: [PATCH v5 09/13] migration/postcopy: Replace QemuSemaphore with QemuEvent MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250529-event-v5-9-53b285203794@daynix.com> References: <20250529-event-v5-0-53b285203794@daynix.com> In-Reply-To: <20250529-event-v5-0-53b285203794@daynix.com> To: Paolo Bonzini , Stefan Weil , Peter Xu , Fabiano Rosas , Hailiang Zhang Cc: Phil Dennis-Jordan , qemu-devel@nongnu.org, devel@daynix.com, =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::42e; envelope-from=akihiko.odaki@daynix.com; helo=mail-pf1-x42e.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1748497677896116600 thread_sync_sem is an one-shot event so it can be converted into QemuEvent, which is more lightweight. Signed-off-by: Akihiko Odaki Reviewed-by: Fabiano Rosas --- migration/migration.h | 4 ++-- migration/postcopy-ram.c | 10 +++++----- migration/savevm.c | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/migration/migration.h b/migration/migration.h index aaec471c00f8..739289de9342 100644 --- a/migration/migration.h +++ b/migration/migration.h @@ -98,9 +98,9 @@ struct MigrationIncomingState { void (*transport_cleanup)(void *data); /* * Used to sync thread creations. Note that we can't create threads in - * parallel with this sem. + * parallel with this event. */ - QemuSemaphore thread_sync_sem; + QemuEvent thread_sync_event; /* * Free at the start of the main state load, set as the main thread fi= nishes * loading state. diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c index 995614b38c9d..75fd310fb2b0 100644 --- a/migration/postcopy-ram.c +++ b/migration/postcopy-ram.c @@ -90,10 +90,10 @@ void postcopy_thread_create(MigrationIncomingState *mis, QemuThread *thread, const char *name, void *(*fn)(void *), int joinable) { - qemu_sem_init(&mis->thread_sync_sem, 0); + qemu_event_init(&mis->thread_sync_event, false); qemu_thread_create(thread, name, fn, mis, joinable); - qemu_sem_wait(&mis->thread_sync_sem); - qemu_sem_destroy(&mis->thread_sync_sem); + qemu_event_wait(&mis->thread_sync_event); + qemu_event_destroy(&mis->thread_sync_event); } =20 /* Postcopy needs to detect accesses to pages that haven't yet been copied @@ -964,7 +964,7 @@ static void *postcopy_ram_fault_thread(void *opaque) trace_postcopy_ram_fault_thread_entry(); rcu_register_thread(); mis->last_rb =3D NULL; /* last RAMBlock we sent part of */ - qemu_sem_post(&mis->thread_sync_sem); + qemu_event_set(&mis->thread_sync_event); =20 struct pollfd *pfd; size_t pfd_len =3D 2 + mis->postcopy_remote_fds->len; @@ -1716,7 +1716,7 @@ void *postcopy_preempt_thread(void *opaque) =20 rcu_register_thread(); =20 - qemu_sem_post(&mis->thread_sync_sem); + qemu_event_set(&mis->thread_sync_event); =20 /* * The preempt channel is established in asynchronous way. Wait diff --git a/migration/savevm.c b/migration/savevm.c index 006514c3e301..52105dd2f10b 100644 --- a/migration/savevm.c +++ b/migration/savevm.c @@ -2078,7 +2078,7 @@ static void *postcopy_ram_listen_thread(void *opaque) =20 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, MIGRATION_STATUS_POSTCOPY_ACTIVE); - qemu_sem_post(&mis->thread_sync_sem); + qemu_event_set(&mis->thread_sync_event); trace_postcopy_ram_listen_thread_start(); =20 rcu_register_thread(); --=20 2.49.0 From nobody Sat Nov 15 17:47:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1748497645; cv=none; d=zohomail.com; s=zohoarc; b=YiZ9lfgkrTZ+hxGAhpeTbaU4OFMiRfaNHU9yx1gZFY1MFiAO5KKwfAdS4p6J2Tg7ny+Yk9xIIzqRnvHEPsoImwvJnggtK0RrSEAdqYo/7DD2oH1iKcU/0GFdxhTC71X+RokShBj+UfTypxRllcaQYZRQvhRbfcKAuuVvVpFWbLk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748497645; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=qqii+YjN2Cl6J4vYDZM5guaZ91Y62BmZ1UnSf5EAOao=; b=HANxiXIVbP0zkOR0KC8DFRmgxxBuI/lSxrH28qjVoBcy6kqK65Eeo/zLjMy1NqVuGmVwHfkW/O4Oe9Cp2uEQPuxDRIGezzdxO8+3t3vQJkrhHo1x1RVh6P8iHGwK3S6jzUlFFZM/eYJx/w8mcf6noWlCdJw56112OXYamXdtUM0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1748497645632978.6062111620614; Wed, 28 May 2025 22:47:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uKW6K-000367-6s; Thu, 29 May 2025 01:46:48 -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 1uKW64-00032Q-QK for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:36 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uKW63-0003DP-7n for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:32 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-234e48b736aso6837935ad.3 for ; Wed, 28 May 2025 22:46:30 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-23506d16e04sm4957575ad.212.2025.05.28.22.46.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 May 2025 22:46:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748497590; x=1749102390; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=qqii+YjN2Cl6J4vYDZM5guaZ91Y62BmZ1UnSf5EAOao=; b=v3KgyQgSLssZYzsrlFVdnDHUSLfGa+rkaBIg+w/H/nBzMP2397eJiZRdzzzYyCS9Ko QnfEr8e2i85NQtAtHWZys7HXh9qVo1wdUHM/1aPL0VsuqOcZxqgJJfT0t4UJmWoU9GIy 3mOUVcvm8np+fq94eIVELrrrLDOnb2tcLs2mbL3eiBxDwExMkp8SwCuBl9OMweCtYlw9 UUqsj34EuDn3rmuyQD9uTl3xRqFwLezRmnCzn+ArRsJ4pGMOo6vKJN3mgqLlySwO9+Hn gewzO/cBhk9OG4IlEs9Y8oPHZpkOLHqTDeLn18XxNJffSVG2qXcPXEbzSS6qD4n3fROt HRpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748497590; x=1749102390; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qqii+YjN2Cl6J4vYDZM5guaZ91Y62BmZ1UnSf5EAOao=; b=F/jNY2mNAhlksQ5aC1zp75Mfv5HpSmeQlpp0VgWWkTuqWBYJO/UIMgNs32VzJCCd9P rCYlcfYdQWIMIcU5GHCIFS/h6hNlBG0mApGzlsNHQIFJMKaFEbgDIPELkuaEllnh9mWy fQzsknYfIe0y2RRyte2sg1/f1bD+q3yrtRwseZl3EWFDEAtQGPITkmwh5UpwE5IcshFO C5tDOJP9g0n+H7BwNeD4x1JBYvrlbGxT4s+OMjJOwXTwIvvBuJbbvifDOnZOcj5j7C8q TJ6VBegrQKvi/CdEFjecM4cOdRFuhygMEUg2v2rpiw8j9yBZOCwl2I1mcP64MdX5HqrA nOuw== X-Forwarded-Encrypted: i=1; AJvYcCWk4Iny9LnfhmoQePdEb1JQ28295lFwRw9j6N0QuCaZ9I5zmYiXBAqJ4ABmyO+nwVICAhhlkjq604c7@nongnu.org X-Gm-Message-State: AOJu0YytmYvWi3ETuGNlj4/PcDN7uCevJ8x9Chudnqbc+hTeSBlEROMK 5c/Tyz1qaq+CFc8l7RHWjSb0okrA7UD8yRnwNxrUMFr4stvKtoIB5tm1LygVcX6hwMU= X-Gm-Gg: ASbGncvxBqFGM9cWyALESrJAouafkMYAZKaN+297ybFy9bDPsqUFlrjA4NlLW82u4SS G6wAdfpJROH1Jfy2R6HxmdGWETdpDFn2WKLOCStCcHdFuV7UrRfD9+9FRTN6Ro7fVM4s5NljDht iF/7veKRLnsW6hb+1eah/fw725XEWtdcCA4wLlNLVprLAPCbeTavsi7th0LY27qLSct0AKuv6lW MZzx7Aed0RMJoREqHPsEZZEPzaJUSW1b3HwgKoYUmscsIG5JsgGjvtQzo7JoIa9rIJv8dMXk4VP zVy9cSUTVq5kKRE/yX6lEDDoJud9xxqui8kiXr4bkkOhVANiCWCzfOPr3eXi3FU= X-Google-Smtp-Source: AGHT+IElIO1iKrYjzsW2mTypaj7HvcccM9R6tmZ96Dkxj2q+y/oUboQUNldMzILEQOeouFTqlZhx5A== X-Received: by 2002:a17:903:1a04:b0:234:8f5d:e3b6 with SMTP id d9443c01a7336-23507fd74f1mr10971865ad.3.1748497589798; Wed, 28 May 2025 22:46:29 -0700 (PDT) From: Akihiko Odaki Date: Thu, 29 May 2025 14:45:59 +0900 Subject: [PATCH v5 10/13] hw/display/apple-gfx: Replace QemuSemaphore with QemuEvent MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250529-event-v5-10-53b285203794@daynix.com> References: <20250529-event-v5-0-53b285203794@daynix.com> In-Reply-To: <20250529-event-v5-0-53b285203794@daynix.com> To: Paolo Bonzini , Stefan Weil , Peter Xu , Fabiano Rosas , Hailiang Zhang Cc: Phil Dennis-Jordan , qemu-devel@nongnu.org, devel@daynix.com, =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x634.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1748497647858116600 sem in AppleGFXReadMemoryJob is an one-shot event so it can be converted into QemuEvent, which is more specialized for such a use case. Signed-off-by: Akihiko Odaki Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- hw/display/apple-gfx.m | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/hw/display/apple-gfx.m b/hw/display/apple-gfx.m index 8dde1f138dce..174d56ae05bc 100644 --- a/hw/display/apple-gfx.m +++ b/hw/display/apple-gfx.m @@ -454,7 +454,7 @@ static void set_cursor_glyph(void *opaque) /* ------ DMA (device reading system memory) ------ */ =20 typedef struct AppleGFXReadMemoryJob { - QemuSemaphore sem; + QemuEvent event; hwaddr physical_address; uint64_t length; void *dst; @@ -470,7 +470,7 @@ static void apple_gfx_do_read_memory(void *opaque) job->dst, job->length, MEMTXATTRS_UNSPECIFIED); job->success =3D (r =3D=3D MEMTX_OK); =20 - qemu_sem_post(&job->sem); + qemu_event_set(&job->event); } =20 static bool apple_gfx_read_memory(AppleGFXState *s, hwaddr physical_addres= s, @@ -483,11 +483,11 @@ static bool apple_gfx_read_memory(AppleGFXState *s, h= waddr physical_address, trace_apple_gfx_read_memory(physical_address, length, dst); =20 /* Performing DMA requires BQL, so do it in a BH. */ - qemu_sem_init(&job.sem, 0); + qemu_event_init(&job.event, 0); aio_bh_schedule_oneshot(qemu_get_aio_context(), apple_gfx_do_read_memory, &job); - qemu_sem_wait(&job.sem); - qemu_sem_destroy(&job.sem); + qemu_event_wait(&job.event); + qemu_event_destroy(&job.event); return job.success; } =20 --=20 2.49.0 From nobody Sat Nov 15 17:47:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1748497654; cv=none; d=zohomail.com; s=zohoarc; b=lzlynr/pW+7Cj3iV5k6ctVYncN2+Fb89NDROroIJZdjODSD9fWGtS7qTnXN7rZbcbT5wGFhlteH4rvSjtvE6NQpVf0IG7JzQiC3NegSH9E/kDerRAZXeIIzp0f39FvR0ANeafizR7UA/OEUeZgnTcIElvogA5NB4mp982Z/zQIs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748497654; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=4oYy3DOZjN8AesbwD4Qmt0S6RXrih5MYv8h9FL1wh/k=; b=OSXZ8rpfQ2Fuy0OE8u+HZqGZ373cQnMpgVPyxzYaESw8072EemrrhUUkiJUGvyodRDh8DFsep0L+NR2QkRNxHU+A48KOqDiq6tobkKhDscQTNqd8nTvle/pfAN2mYlpxeCz2GmEDFniJURFMXbMGvbe1suAoPiEf2UY0vQkDrfc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1748497654451266.45204222375537; Wed, 28 May 2025 22:47:34 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uKW6M-000377-Ig; Thu, 29 May 2025 01:46:51 -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 1uKW67-00032a-NP for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:36 -0400 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 1uKW66-0003DZ-1R for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:35 -0400 Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-310cf8f7301so434180a91.1 for ; Wed, 28 May 2025 22:46:33 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-3121b202763sm602184a91.0.2025.05.28.22.46.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 May 2025 22:46:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748497592; x=1749102392; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=4oYy3DOZjN8AesbwD4Qmt0S6RXrih5MYv8h9FL1wh/k=; b=kwsUH6zxLqllAsVU+4J8Hum4ZmYfajZH8M6YfE3Rj4yVcaR5X/Q9irllro68QUdaCt BG+K4R9y3jTot+i2JigcuTFqesjhzgdxg4TDVLwQEzCFIm9+SJFMSrORczzj4+o5BBgB 11xhfT51QZX6X9IinvAxo4gygXAIoru9SyVXMU58KQbT7lsoTC3yCSB76kPTvesf8bP/ HqYSdL/4nAdAktyEfPn9zBNGanY4WQDEH5uF9gtgYZRgV04XEiWke6nkbDjo6X4+Oq3u SoXlDMkVzczeHWuia0ulp5i1H1pBusUJaDmR6RV3Nih3WevCLVzeaQF8Baurl11BzyJh eXWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748497592; x=1749102392; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4oYy3DOZjN8AesbwD4Qmt0S6RXrih5MYv8h9FL1wh/k=; b=FnuBTfyow8OAHoXJyhzMKfD+TwZtS6Qy0QlGHJ8oMb0GnWRb6ZWY5MdXt3es6YnE0y eIP8nuq6wWcgQHMoPCrz3IeOe8W8h5TAaCJ3eTShLPSWEw/cbSJuTqYiV3WU30t/krao INySEpjVGofvJb6/e4KAn1OMekkCWuv524Q1WJOzencVQEJakTRllZiN9AcCXxDGJ/Q4 T1V4RzgZkbZIOKwznCjye9t1TypA5uPe0B+TmJ9xyPJk9320+J2Yq7ebbOMknncjrRZs NWZsoYaNHhh05Ai3ip00YsXBtCwUjKKg+KanEe6y2DFxPKww9nq1LVitVuhbDS+g/piB rYxA== X-Forwarded-Encrypted: i=1; AJvYcCUQ5n0BnqC70sItEJXSaFaxNE6/4iDQhO0X+gIYUupHPS3v4u1kN4YWvbnDIsYOJHklsDhexBDUwYOS@nongnu.org X-Gm-Message-State: AOJu0Yxh6Hv3nUJva7RU3kK8GAErb8u+wq11b2rSY769PO3FDmcbXiQc XMEFdQGNqSFpt7AtQzXJwZN93BFfHPbvP83e/bPUoFysi43JKL084oFj0unkcz9rLqc= X-Gm-Gg: ASbGncv3+yIy3z48K9DUxZt7mzjt6rzUw2LuqgizMAgcShlLLEK3UfLfsFlS7iVr3ew A3SOrc61WNkkB0+4A78J9VPqprOGlYgOfCoPN0daX0qRcKTEvd2EDRpKGkmLhBE2xmSRNhcFhZm v2i18gJpO5xmy1WmQjra/Gml9qYoABom0w2abAisJdrlhWeE3HyHdwieOlFfED9aozvalAXbIXr CSB39KQm7ILrzjcUnJjhYKqBS4W52xcfdx97D+xHKpsaLbTSwg+8aYjeFtx4p7SO64XejljXUiX xbU5TXi1Zum2RfP9kkKgEjh0Hc4tUCEj+UMGUnfGAvgEQCOd/NxK X-Google-Smtp-Source: AGHT+IGZS1Djwi1muVSMCzzXzHtan1LTG34n7CPjasQ1v1pzl9wt4AohuU+iZDGyux27r+rvRYTV+A== X-Received: by 2002:a17:90b:264c:b0:311:1617:5bc4 with SMTP id 98e67ed59e1d1-31214ecdacamr3276778a91.12.1748497592618; Wed, 28 May 2025 22:46:32 -0700 (PDT) From: Akihiko Odaki Date: Thu, 29 May 2025 14:46:00 +0900 Subject: [PATCH v5 11/13] qemu-thread: Remove qatomic_read() in qemu_event_set() MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250529-event-v5-11-53b285203794@daynix.com> References: <20250529-event-v5-0-53b285203794@daynix.com> In-Reply-To: <20250529-event-v5-0-53b285203794@daynix.com> To: Paolo Bonzini , Stefan Weil , Peter Xu , Fabiano Rosas , Hailiang Zhang Cc: Phil Dennis-Jordan , qemu-devel@nongnu.org, devel@daynix.com, =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1034; envelope-from=akihiko.odaki@daynix.com; helo=mail-pj1-x1034.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1748497655457116600 The pair of smp_mb() and qatomic_read() sometimes allows skipping the following qatomic_xchg() call, but it is unclear if it improves performance so remove it. Commit 374293ca6fb0 ("qemu-thread: use acquire/release to clarify semantics of QemuEvent") replaced atomic_mb_read() in qemu_event_set() with a pair of smp_mb() and atomic_read(). atomic_mb_read() was actually cheaper than atomic_xchg(). include/qemu/atomic.h at that time had the following comment: /* atomic_mb_read/set semantics map Java volatile variables. They are * less expensive on some platforms (notably POWER & ARMv7) than fully * sequentially consistent operations. * * As long as they are used as paired operations they are safe to * use. See docs/atomic.txt for more discussion. */ However, smp_mb() enforces full sequential consistency, so we cannot use the same reasoning to claim that the pair of it and qatomic_read() is cheaper than qatomic_xchg(). Therefore remove the pair and simplify the code instead. Signed-off-by: Akihiko Odaki --- util/event.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/util/event.c b/util/event.c index e937804a92a8..df6d60836041 100644 --- a/util/event.c +++ b/util/event.c @@ -55,18 +55,9 @@ void qemu_event_set(QemuEvent *ev) assert(ev->initialized); =20 #ifdef HAVE_FUTEX - /* - * Pairs with both qemu_event_reset() and qemu_event_wait(). - * - * qemu_event_set has release semantics, but because it *loads* - * ev->value we need a full memory barrier here. - */ - smp_mb(); - if (qatomic_read(&ev->value) !=3D EV_SET) { - if (qatomic_xchg(&ev->value, EV_SET) =3D=3D EV_BUSY) { - /* There were waiters, wake them up. */ - qemu_futex_wake_all(ev); - } + if (qatomic_xchg(&ev->value, EV_SET) =3D=3D EV_BUSY) { + /* There were waiters, wake them up. */ + qemu_futex_wake_all(ev); } #else pthread_mutex_lock(&ev->lock); @@ -88,7 +79,7 @@ void qemu_event_reset(QemuEvent *ev) =20 /* * Order reset before checking the condition in the caller. - * Pairs with the first memory barrier in qemu_event_set(). + * Pairs with the store-release in qemu_event_set(). */ smp_mb__after_rmw(); } @@ -102,7 +93,7 @@ void qemu_event_wait(QemuEvent *ev) /* * qemu_event_wait must synchronize with qemu_event_set even if it= does * not go down the slow path, so this load-acquire is needed that - * synchronizes with the first memory barrier in qemu_event_set(). + * synchronizes with the store-release in qemu_event_set(). */ unsigned value =3D qatomic_load_acquire(&ev->value); if (value =3D=3D EV_SET) { --=20 2.49.0 From nobody Sat Nov 15 17:47:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1748497665; cv=none; d=zohomail.com; s=zohoarc; b=TP31JFWLUJlZW+Gso9bNyp7IYOwA6+LrnbwQ691TotqeTQ0PxDni/3EQQEcX33bNl5dWQ/Ul0jpG/V3dUUzi1Umh2oU8jVTqxCjYfc7ObdYpmpI2jrzG4j2QiILLYVeqkNAzZ8wk4hUf6DwcLxcNV+xOOTe4wA2Wx8Mu561obm4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748497665; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=zcXBpjTHPNeeSaV+SDkbnQDyNsA4TkuiYdbQXIhIDo8=; b=Pit/KG13/ukQB6dYUI+m9o5kwvSuFPCauNRTqhoqFpLBmolrvkZ5Fl5joqBBgE4OKEHUteKXk/RtHO0aEGOoq/BJxgFmxndlGV3t+GJIm7zkXyv4a/publmFJEL2/RFJuO/kG2TlrZUDKFGIlQa1tdlcfPM5kvXnRliXK7UPa9g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1748497665140554.747832027688; Wed, 28 May 2025 22:47:45 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uKW6L-000373-VX; Thu, 29 May 2025 01:46:49 -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 1uKW6B-00034F-6u for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:39 -0400 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uKW69-0003Dm-9B for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:38 -0400 Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-310cf8f7301so434224a91.1 for ; Wed, 28 May 2025 22:46:36 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-3121ba3e4f9sm597141a91.42.2025.05.28.22.46.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 May 2025 22:46:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748497596; x=1749102396; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=zcXBpjTHPNeeSaV+SDkbnQDyNsA4TkuiYdbQXIhIDo8=; b=LWFHFdLsz5ReaFxoPA1qg9I4Kvn++JDmXk0CtbecPvpfXIN6hmBagL9KtWj1GCx+My Dksg2ddO7TbI00naJ5zDOJvEwGD9cYTy7/7Ztsf90s9kZXUNwlcfn0Bt33BKFmOs4LGN qiRlGS7TDRk3LBkTa4cprXHBGXOu//PG0xeb8+igWSR7YzAjH/lVN+tmCJLxkDc5OEvM HuVrw/WQVplbbWRDbGqj04ijVtWedLmVojutEllCSmkFphzxDDyEpJEPlTKWOXS4F76N IhPl0/3nTW0iwqAR09RDaJr0UrfZpwL6CctjGcjQBO3J9W4NCr+lzSaB4crnAlaB8d2M IEnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748497596; x=1749102396; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zcXBpjTHPNeeSaV+SDkbnQDyNsA4TkuiYdbQXIhIDo8=; b=Qg+EduiWj4PudDcbUokZx8W56mBIC4GY4L8uAeeAxP9u1JVSu673BKAoBrF+v/99F7 F79SdeY58P5yPnVUK0XU1LW56NSr2ABfFwQ4CAooW5qT5EqTW+zPUIj9Y8G4PKJtONz7 0e7ntDoRf4Gxyl4+mb9jDP+44W72gXelqwh2+L0J+5c+0Lc4F/q5nfQQsbPNx2ItKNVB Fxi5DdVSm1rbrTY+41lgUNsm1k5I+dT7xq552mUYCxbPM9+jCbkr/zkUEonbQhquoOZC zWBEQ/Nx3jpTBZqVp8isma/vA2A4gIUxvCGlTkhHY+Xa3yrCA7PxUHKnTNvgMOrFo3KM uXfg== X-Forwarded-Encrypted: i=1; AJvYcCXWpx56SeRp43mgwOib6dhb0KxbW8Hr/KcLxKt0njlFuXP1yYE8Qy/ns5prqp9kl8WhVHyAhZ4lJcEp@nongnu.org X-Gm-Message-State: AOJu0YyRDsN+MFm75J93FUqBVtJ7mWnxJAU2X/xVOHYFYYWhp3WJnbuA YCpXISUI0/DKALzttKXW3qt601329ZhxAh8j76j6ykRtyTW/1AcfPP5AOUJKerjg6dw= X-Gm-Gg: ASbGncuqbdC41HgxfQfP2SMg+wn4Te1e7OTLtNDRWjfUyJ1pX75OWv89cDYJfa0Gj6C /v+aRYU72eeNgYPqs6i/CgSpUwfHPI/uHFzQ696bEygFU4g3SoPmFTSgUjD6oqvUI+fEt1oVYFN gU+24KxMj9Z+BN2aey4x/ftjMXsgnp03zi2nXo8X93TawX27GTt0f0/6EkvCJsF5kAKyRmNkkWH 0e3rhgbMjTID/2jDyx5MgZX64xbh5FAdBM4LyrQp0EMreSOAr0/nwdZ1C0dbk4RkB0U+3GydRw5 uI5nuA+sM3xBzZcyFAlC1XgjVECOoUXIlW2lOG8hYtURYpFsxXrh X-Google-Smtp-Source: AGHT+IEfbEv7t24Tj9e6sN3FwDLlIg19PxaHiqPP1FnX+c3EPYstUl4tFFykwY2pmoQc9GRETOlkCg== X-Received: by 2002:a17:90b:2e04:b0:2fa:562c:c1cf with SMTP id 98e67ed59e1d1-31214e1228dmr3591766a91.1.1748497595494; Wed, 28 May 2025 22:46:35 -0700 (PDT) From: Akihiko Odaki Date: Thu, 29 May 2025 14:46:01 +0900 Subject: [PATCH v5 12/13] qemu-thread: Document QemuEvent MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250529-event-v5-12-53b285203794@daynix.com> References: <20250529-event-v5-0-53b285203794@daynix.com> In-Reply-To: <20250529-event-v5-0-53b285203794@daynix.com> To: Paolo Bonzini , Stefan Weil , Peter Xu , Fabiano Rosas , Hailiang Zhang Cc: Phil Dennis-Jordan , qemu-devel@nongnu.org, devel@daynix.com, =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102e; envelope-from=akihiko.odaki@daynix.com; helo=mail-pj1-x102e.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1748497667780116600 Document QemuEvent to help choose an appropriate synchronization primitive. Signed-off-by: Akihiko Odaki --- include/qemu/thread.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/qemu/thread.h b/include/qemu/thread.h index 573f8c9ede20..f0302ed01fdb 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -10,6 +10,16 @@ typedef struct QemuSemaphore QemuSemaphore; typedef struct QemuLockCnt QemuLockCnt; typedef struct QemuThread QemuThread; =20 +/* + * QemuEvent + * =3D=3D=3D=3D=3D=3D=3D=3D=3D + * + * QemuEvent is an implementation of Win32 manual-reset event object. + * For details, refer to: + * https://learn.microsoft.com/en-us/windows/win32/sync/using-event-objects + * + * QemuEvent is more lightweight than QemuSemaphore when HAVE_FUTEX is def= ined. + */ typedef struct QemuEvent { #ifndef HAVE_FUTEX pthread_mutex_t lock; --=20 2.49.0 From nobody Sat Nov 15 17:47:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1748497697; cv=none; d=zohomail.com; s=zohoarc; b=CKp4F9pRQv4VjEUWS+OkoCc6ZuCvIp67+7/CpDuq1mN8MgKTuub32YBUqLiX1DdhvbCF1L6SMXsrE9YmMuIOXCfAMGbaNkb9gjsspXpbKzlT3YnsEEra6V9jeWk5KYH2yXi5DUnftN7AZm8C+DPFtvC9oY7A5Gu0gQOfw1v0JLM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748497697; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=FcEa4ndMyii+7LB2M35RVE9s5452FNapzNXg78hJqtg=; b=ZfLUPdy2jzlWBLpBKjL26NSE1HNNhrN4PXbgMj9Uo/frj0z7WJoNJKHc3i6+xMvvh8bAVOywap8y3VjP2sn8v5oh5o3rSg+5DDM5S/QM6HbUq+ElSzUHbWa3Y1uefbjKlwV+IfwVobvrHMmQds28MVXzWyd9V4yYigaa2SK/nyw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1748497697248537.4782873517091; Wed, 28 May 2025 22:48:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uKW6P-000381-Md; Thu, 29 May 2025 01:46:53 -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 1uKW6F-00035Z-3H for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:46 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uKW6C-0003E0-Ql for qemu-devel@nongnu.org; Thu, 29 May 2025 01:46:42 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-23035b3edf1so5205035ad.3 for ; Wed, 28 May 2025 22:46:39 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-23506bc8695sm5088325ad.34.2025.05.28.22.46.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 May 2025 22:46:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748497599; x=1749102399; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=FcEa4ndMyii+7LB2M35RVE9s5452FNapzNXg78hJqtg=; b=UlsPQmlGnTiqb8+jAzpeP4QOBaI3W5RrXmDQCgyO49rCnRM1Wx0BIa0fdVsaCd+wNy qaRphVa+/anWYgSA3IQw3/BWBCwfVw7kc6GgT5fD1ULlu2p85dk3SW9Ig18Es2b6qFev cB3BJ/TfHedYGwljEKpXL3NcQRfL3cxEP2rYU7QvU/s5PSzSYLyd8l6wWbIsTNiqWFOp BuQugQW2fkVhiAceCS30/oJoUTdL+uCjbDopSRZSDYGSASc5IPbrJgTG6KbP5CLyaMdZ LCKSP7fJdA9uMyXlrariKmL2rg0onUJ8gfFBQMcjEbpE0RWYRkpega7S/BrXH+wQHoYy u+xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748497599; x=1749102399; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FcEa4ndMyii+7LB2M35RVE9s5452FNapzNXg78hJqtg=; b=q56CQg0IimKiZusEFU2Cr89iz566Y0t/Fa72mmyjD+OkNWsVlE/HF8j+gR/7K6bzNR xCus73WBT7v4bHgRiBp6zegzknHnwNc7fyZEBh7OpVuqBuV/ybR68Ou5M0acbtMv33k+ RAVuh0liOh2Krn+rKqFkjUy3r0ijjJBgMuh6c2do23VcIYlNM9K4QQnGoTzURiZL80m5 3xxUSFXZ0E2OK008IlQFLCSMn7moMoTllHUQdZZs36xcDGS8wg9fDsRkNgOyzpAuzJzd 7niKTxUIrt9ul8aMVvEmv1iWT/bgwovhFF7P75UsleEjJFyyq2gGq40qF8ebORfOvA5K 7BUw== X-Forwarded-Encrypted: i=1; AJvYcCUsUNgMFMtJmJj7Ye51HVhSLW4/AfSG84MdENpFEV81wPcbU4sMO+I2XnBh2JP3pXR2Tg0gNzVd2ke1@nongnu.org X-Gm-Message-State: AOJu0YzcI0rDCCYAoHix28bUwsLHC9lRKVgmetokW1XbkW6iSG+6eifK lAIa+ypTJmPxBF4KYqlJmm6+cy/0TEBIb2CenOVD29UvDeKYCUjFzDecmX+EnBN2pyUMAIvE0iU iNtKz X-Gm-Gg: ASbGnctOPgBCXKJJMkjLMENBFo05p/wAD62aHCZxxbRTHkYUVXPW86RzN55THZUxSaa BTU22qIWUs4tlCTEBootDrAMGjX27rXipsQRBKRYnymodMqPg+t2xO/+fzLk/oySmI4+GcaZBka NNMOwhRH/mBriqKngCT4pj3ZD+vR3iKSn7V2Sa6Ls4DwuDIMgLzwX25FZ70paWrbj6KKfc40C8F gJBd5a3T5uzE5lZNkwNQ8u+GQ8qjHTtmeN3W9mgs2AR5+9xI4lP8uWlT6Y1QpJzGwBC9eK7VyfS ZkvwLC9ikb1HBtB9lvBeNZR60iJx4QvKpi4eN6M7tawwUiYWcqPSe7bDtCXCkeg= X-Google-Smtp-Source: AGHT+IGrejCONCdvVLxo+X//pJFVNA2Y2WK6y6ZF1UUdGJOU5fAEPpmGjMOH4BHUtuq9JSeePHwSAw== X-Received: by 2002:a17:902:ce0a:b0:234:9375:e07c with SMTP id d9443c01a7336-234d2c4aa60mr80984545ad.46.1748497598762; Wed, 28 May 2025 22:46:38 -0700 (PDT) From: Akihiko Odaki Date: Thu, 29 May 2025 14:46:02 +0900 Subject: [PATCH v5 13/13] qemu-thread: Document QemuEvent memory ordering MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250529-event-v5-13-53b285203794@daynix.com> References: <20250529-event-v5-0-53b285203794@daynix.com> In-Reply-To: <20250529-event-v5-0-53b285203794@daynix.com> To: Paolo Bonzini , Stefan Weil , Peter Xu , Fabiano Rosas , Hailiang Zhang Cc: Phil Dennis-Jordan , qemu-devel@nongnu.org, devel@daynix.com, =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x632.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1748497697971116600 Background ---------- I added a variant of QemuEvent for platforms without futex and refactored qemu_event_set() to find it is difficult to show the correctness of these changes. Analyzing the code, I found there are three different levels of memory ordering constraints: 1. The requirement of QemuEvent users. 2. The constraints that needs to be imposed on memory operations to satisfy 1. 3. The actual constraints imposed on memory operations. Distinguishing these levels make it easier to show the correctness of the mentioned changes. Constraint 1 is not affected by the mentioned changes, so we only need to focus on contraint 2 and 3. It also allows sharing constraints 1 and 2 for the variants of QemuEvent implementations. Change description ------------------ The code already included some documentation of memory ordering as inline comments. This change splits the documentation into the three levels, and place them to appropriate places. Each of the documentations of constraint 2 and 3 includes references to one of its higher level. Constraint 1 is now documented in include/qemu/thread.h so that the users of QemuEvent can refer to it without reading the implementation. Constraint 2 follows the documentation of the state transitions in util/event.c. The inline comments describe constraint 3. The documentation of memory operations already describe the memory ordering of them well, so they usually contain only references to constraint 2, which memory operations are intended to satisfy. Signed-off-by: Akihiko Odaki --- include/qemu/thread.h | 19 ++++++++++++ util/event.c | 84 ++++++++++++++++++++++++++++-------------------= ---- 2 files changed, 66 insertions(+), 37 deletions(-) diff --git a/include/qemu/thread.h b/include/qemu/thread.h index f0302ed01fdb..8fafff73de7d 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -19,6 +19,25 @@ typedef struct QemuThread QemuThread; * https://learn.microsoft.com/en-us/windows/win32/sync/using-event-objects * * QemuEvent is more lightweight than QemuSemaphore when HAVE_FUTEX is def= ined. + * + * Memory ordering + * --------------- + * + * The documentation of Win32 manual-reset event objects do not specify + * memory ordering. Below describes the memory ordering QemuEvent establis= hes. + * + * Assume each of the following two orders is specified in a different thr= ead: + * - X -> qemu_event_set() + * - qemu_event_reset() or qemu_event_wait() -> Y + * + * X -> Y will be ensured for the two threads if any of the following is + * satisfied: + * - qemu_event_set() happens before qemu_event_reset(). + * - qemu_event_set() happens before qemu_event_wait(). + * - qemu_event_wait() waits for qemu_event_set(). + * + * Note that this is true even when the value is already set before + * qemu_event_set(). */ typedef struct QemuEvent { #ifndef HAVE_FUTEX diff --git a/util/event.c b/util/event.c index df6d60836041..d0dc6ebc00ff 100644 --- a/util/event.c +++ b/util/event.c @@ -5,24 +5,33 @@ =20 /* * Valid transitions: - * - free->set, when setting the event - * - busy->set, when setting the event - * - set->free, when resetting the event - * - free->busy, when waiting + * - FREE -> SET (qemu_event_set) + * - BUSY -> SET (qemu_event_set) + * - SET -> FREE (qemu_event_reset) + * - FREE -> BUSY (qemu_event_wait) * - * With futex, the waking and blocking operations follow busy->set and - * free->busy, respectively. + * With futex, the waking and blocking operations follow + * BUSY -> SET and FREE -> BUSY, respectively. * - * Without futex, busy->set and free->busy never happen. Instead, the waki= ng - * operation follows free->set and the blocking operation will happen when - * waiting if the event is not set. + * Without futex, BUSY -> SET and FREE -> BUSY never happen. Instead, the = waking + * operation follows FREE -> SET and the blocking operation will happen in + * qemu_event_wait() if the event is not SET. * - * set->busy does not happen (it can be observed from the outside but - * it really is set->free->busy). + * The following orders specified in a thread are preserved for any other = thread + * accessing the event value: + * 1. qemu_event_set: X -> store SET + * 2. qemu_event_reset: store FREE -> X + * 3. qemu_event_wait: load SET -> X + * 4. qemu_event_set: store SET -> wake * - * busy->free provably cannot happen; to enforce it, the set->free transit= ion - * is done with an OR, which becomes a no-op if the event has concurrently - * transitioned to free or busy. + * Different combinations of orders 1, 2, 3, and 4 establish the order vis= ible + * to the users of QemuEvent in different situations: + * When qemu_event_set() happens before qemu_event_reset(): orders 1 and 2 + * When qemu_event_set() happens before qemu_event_wait(): orders 1 and 3 + * when qemu_event_wait() waits for qemu_event_set(): orders 1 and 3 + * + * Order 4 ensures that qemu_event_set() wakes qemu_event_wait() if it is + * blocked. */ =20 #define EV_SET 0 @@ -55,12 +64,23 @@ void qemu_event_set(QemuEvent *ev) assert(ev->initialized); =20 #ifdef HAVE_FUTEX + /* + * Transitions: + * - FREE -> SET + * - BUSY -> SET + * + * Order 1. X -> store SET + */ if (qatomic_xchg(&ev->value, EV_SET) =3D=3D EV_BUSY) { - /* There were waiters, wake them up. */ + /* Order 4. store SET -> wake */ qemu_futex_wake_all(ev); } #else pthread_mutex_lock(&ev->lock); + /* + * Transition FREE -> SET + * Order 1. X -> store SET + */ qatomic_store_release(&ev->value, EV_SET); pthread_cond_broadcast(&ev->cond); pthread_mutex_unlock(&ev->lock); @@ -72,15 +92,14 @@ void qemu_event_reset(QemuEvent *ev) assert(ev->initialized); =20 /* - * If there was a concurrent reset (or even reset+wait), - * do nothing. Otherwise change EV_SET->EV_FREE. + * Transition SET -> FREE + * + * Ensure that BUSY -> FREE never happens with an OR, which becomes a = no-op + * if the event has concurrently transitioned to FREE or BUSY. */ qatomic_or(&ev->value, EV_FREE); =20 - /* - * Order reset before checking the condition in the caller. - * Pairs with the store-release in qemu_event_set(). - */ + /* Order 2. store FREE -> X */ smp_mb__after_rmw(); } =20 @@ -90,28 +109,14 @@ void qemu_event_wait(QemuEvent *ev) =20 #ifdef HAVE_FUTEX while (true) { - /* - * qemu_event_wait must synchronize with qemu_event_set even if it= does - * not go down the slow path, so this load-acquire is needed that - * synchronizes with the store-release in qemu_event_set(). - */ + /* Order 3. load SET -> X */ unsigned value =3D qatomic_load_acquire(&ev->value); if (value =3D=3D EV_SET) { break; } =20 if (value =3D=3D EV_FREE) { - /* - * Leave the event reset and tell qemu_event_set that there are - * waiters. No need to retry, because there cannot be a concu= rrent - * busy->free transition. After the CAS, the event will be ei= ther - * set or busy. - * - * This cmpxchg doesn't have particular ordering requirements = if it - * succeeds (moving the store earlier can only cause - * qemu_event_set() to issue _more_ wakeups), the failing case= needs - * acquire semantics like the load above. - */ + /* Order 3. load SET -> X */ if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) =3D=3D EV_SE= T) { break; } @@ -120,6 +125,11 @@ void qemu_event_wait(QemuEvent *ev) qemu_futex_wait(ev, EV_BUSY); } #else + /* + * Order 3. load SET -> X + * + * qatomic_read() loads SET. ev->lock ensures the order. + */ pthread_mutex_lock(&ev->lock); while (qatomic_read(&ev->value) !=3D EV_SET) { pthread_cond_wait(&ev->cond, &ev->lock); --=20 2.49.0