From nobody Sat Nov 15 19:07:36 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=1748237414; cv=none; d=zohomail.com; s=zohoarc; b=DSyNlLvsVVfkdEEYqBgcXIzrt/KWZXUMWdxvytJJISZ4LCOdPvNlJ5O+6yNAXPzoLO5qClnVQVcqLesBoWLeT7IY3NztQCiaEqlgCrbuDlvN8U5zMXbKeIk0HCB0n630I87VBp0g0RbXBVRgdZKhrJIxtMld/uS/0tIcL8XXyVg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748237414; 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=OXGMcazcM2rKnn6Nd+UVNUJIlmAbqbaSAuB+iOS7kzKyjptkwNE+p4PGbgHISH0kAo7bjtncqqUF6DM98KvZcFV/MpsBwYCN8dYCUf/Zmuy7mIwcv5rQzqveVfljbAhi8dbBtDWAxBi9gxoKVNFyEWCdTTvGmgWUfYgktD8AMLA= 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 1748237414633906.7595917397383; Sun, 25 May 2025 22:30:14 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uJQP2-0006zK-Vl; Mon, 26 May 2025 01:29: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 1uJQOs-0006ye-3p for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:27 -0400 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uJQOp-0001wr-Db for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:25 -0400 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-2347d505445so671145ad.2 for ; Sun, 25 May 2025 22:29:23 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-2340c4eb87dsm38075125ad.17.2025.05.25.22.29.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 25 May 2025 22:29:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748237362; x=1748842162; 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=hLYrT2XHtV1lMUXPyJj0Vm+mdBW9NQTwEwekJ3PjtWouxB1BxVNI5yYIAo55bMO6nd 1zrATict/H73kKGtY7f6YZVQnuyYzvd5h4Cg1XhFECW+6umdiNG7Zu4BQujinTAgZwJ2 v800V6DqoWaWSdKGW+0JH7lDLoo85lUZz09bQLm/h6eZ9JN/yviGRytoqc1VQWFRciBK jM9D2f2Dz/2ZEr1jffbVPXDFGMj72XhgKPVeVNTvQNLwV26wW4XTw49uNShQyvIuC70c HmgTJUyzmy6nOjimoUB+OUD5Liug/Lb50ix97Y5pTzC8mUAuitTrKo335EhAmve9C2pD BPug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748237362; x=1748842162; 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=Aj1TUWcNODT6EAiMRmgMzK/qTfMS4ZjEBpNRl0Wv9z/fFKViDEiyuoPkj4Ohy5yJxe uW1MzwvhVE4Uo+JQmBXw+PEsteNqePkkAQ6c4hL4qv1R7SQswFz59I5fb/v8SYTFnBFC XtLklmhhhl/VEUZ2KyWM85R+E0T6cY9Bpb186R3ufvePF2qITgdJmlpfzRsLqM7aO6r4 ds67RUf3Mr1iea0k1lh474PHbz6ejdK510HDVgcpvefDUL4ToF0/iXBJtSgaoCxxdcjm mTPDE5OhtLgghEEEB5+FTbHUiyxAIor6WKF8DsQLwbas1dBX1Wy0LqFYxxmEP+qL4wh9 jg2g== X-Forwarded-Encrypted: i=1; AJvYcCWSYGcZJCHRjUWYtRqNY1vY517B8Uxx1Br+EuXKW2ByGa4Xg2TjaY9nGYDJDskB9x1g3j1yjz7pQUiv@nongnu.org X-Gm-Message-State: AOJu0Yw7X6gqeYxSK0XEpv25pI7fg/E8cVs2ZLt1jjWllFa5wVRnGdo3 kg3REwa2+nq0RbHQQAS09sREDLR0zbjyAq5Pj1qCYfwRnxcfj5BlDlb8FZXMDHslo4ukZFws27t wfbYT X-Gm-Gg: ASbGncvVG91Txl9qDFAHsGBcusJ+C3X1s+2iUXXZ8/5WPUlm0nFeBRlM6Wqwon6g+Vt 4mYAqi/9xCHkDNBIQgIcWwbvrX08sVHpzF6vNBmL8vIhMLWei2RKT1vF+9AWINVfDEn+M3h9uWK XOwRoSsUOydMMJOcTwjJ2SfKOVExKE4B9i3h77bKn5aKahvgxdu5CoVrQWiOiFEn3WGYegOA2ry L5Pl7pR/rfQINm1/xWALtO6v9dETxdtbsUmHPNIcOa1YKp41w2iFnHcS+kNZXK/U5P/Xwh/0c87 Dmd73Z0ottc1QcY9+Y8Q/wHtxU3uxtxi9b0JMejeL3Wj9Kk2HxTG X-Google-Smtp-Source: AGHT+IG6D00Vkzrdtz1BoGZzsKkk7Rvyr6qPMSJbV5WbXbynlqwoRgbeJZ7kFMDxZePvjbZI2Dnw3g== X-Received: by 2002:a17:903:3a86:b0:231:9902:1519 with SMTP id d9443c01a7336-23414fd3e28mr125388425ad.39.1748237361709; Sun, 25 May 2025 22:29:21 -0700 (PDT) From: Akihiko Odaki Date: Mon, 26 May 2025 14:29:11 +0900 Subject: [PATCH v4 01/11] 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: <20250526-event-v4-1-5b784cc8e1de@daynix.com> References: <20250526-event-v4-0-5b784cc8e1de@daynix.com> In-Reply-To: <20250526-event-v4-0-5b784cc8e1de@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::633; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x633.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: 1748237417532116600 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 19:07:36 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=1748237468; cv=none; d=zohomail.com; s=zohoarc; b=box92SV1oAhoyn4F6NFIe/uwLs+f+yCwyr5vN3C4L6PSsYgdCNOMsY2uD9iXGus9qGgGWTqsb0ciXoZPiKqOjw4/pqv6d45oU8bFBwPu0lPdllTxb8s69abKt8OhU9wnpJohngnutEyGLQQDxK0PF0yTZGIrlgZp1gm7jbAzMUc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748237468; 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=koGNyjR3rY+UipmHujnUvKkM0ko0t/B8eI22XAgl/IRLYYpwElX0SfsByXBzRzfS/yaPfefSdhTdftzh87tNpelOnefrakxjrKVg/vujJQa/uJlMTxNihx5RiQ7himmX3A7U1iMf612uPxs4CDpWTcOQ7K7vkJa7qPV/VDhrQFE= 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 1748237468118448.93715426889503; Sun, 25 May 2025 22:31:08 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uJQPC-00071q-6m; Mon, 26 May 2025 01:29:46 -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 1uJQOw-0006yy-3e for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:30 -0400 Received: from mail-pf1-x429.google.com ([2607:f8b0:4864:20::429]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uJQOt-0001x9-Vi for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:29 -0400 Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-74267c68c11so1149539b3a.0 for ; Sun, 25 May 2025 22:29:27 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-742bab0bc35sm14896335b3a.13.2025.05.25.22.29.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 25 May 2025 22:29:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748237366; x=1748842166; 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=w9dWtm/lfTnhCLoGrN9Toao5NOZ3/M4OBGc7UPscM3ePJn+T2QGlw1LMdQLbE2k7Hw Bm0/r35JgyaIpzUODnPkTqrf+8RKkLorqKV15Uueb2ZDJgjxxKnYjPJ1bxOq+VByjjzn rODybUUiCPUSXA3yMLeBYOZTT28vrpAWGUexNQ2i0RdKVO0OsmKdHIbCKrzI+RZQUINM 0dQlc4tHe/O6kXFx9dQFE8qXm1+CAvWuYM0GK/CccwqkCHf1hxo1ShLBmhZb2samLGUL CSUrQ2w7V9pOn4f14NWNT5d+Mi+srVybwMmdf9iiDfvwuoVyvOZ2kcNQ9OL70EiUN11q TLLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748237366; x=1748842166; 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=nl/uzFB1vNJ9EaWleU+DL3W5Vof/lvbv8nAcpQHSaAPMD4zbqD4vprsNWbGNnEBTHF c0J/hql7uiRvMq3XSs+RuymrkrqrZ6GKUeGYwToWFk9s5fSQdiJsfqaE9CuEUSKLKNrW qjStY6dYzRs+NNYFPKaqcUEBO4450LyDcIH6WHQ8ADzUKUHEGTJtV81IXET+h/G+4pga Zlk9dsvDMdAQ7eEgnxA0X8Gpuzoga7GdKSJZX8qug7p+8D6hWUqqrUNNZl+KjXJxLeum 8QtvyAQpAljAHkvD+nVuu9oJ9jdlDr+q8eMyE+F1CPa3jUcFJ0qR8LOXFJfkcjVmTynm wclg== X-Forwarded-Encrypted: i=1; AJvYcCWt4L1ZG7V/2ELv8F3LttGN5lJPTKz8W6A3U+uNMTSWLQ72rqJM2eVDW4ZPJrh/byl3FkGbJVc2oW+Q@nongnu.org X-Gm-Message-State: AOJu0YzcZDQpi4fx3lRALnd4koQ8X+8J1ouIGUqaPzfO3tCiimKhgJjV ishpaHJiEqJvui5j5RLRkknmB9oSPKfiVd74fsTl39AMopaoxMeC0rXUo5EvmsVpb8LBcgI9TCl NTuME X-Gm-Gg: ASbGncu7sr+20/qeRsC7EmjRdrMMFp8JTuh9TRhda5z8gPlVJj+iCD2x96jWCAfI4XN 8AzAozEtWvUCadxYoQLprUkSXPd+uorGo3DwjktJhf349f/BZukXzoAunS0q2yTKoVma81gXR0o oyhASGrFHqvLwQCcv7ghxLx8hgVM9WUeXF4bP0JlPm8mToHXM608paK9Y/DUOsl2tk5THFUIPZs yQP7SdakovQbdXWefVOuZFjMNnlDOmm/ZVVfNM/I0EJuCfqnRT+I20PrlzqzlyunN4RC7NY59tN QV04Q062ZLsiQQ5IwRUFrzcJwG6DM2dCUOscimX3ZvFSI1ABl/Zz X-Google-Smtp-Source: AGHT+IFtn+lHZzY0SuaRGyutdLFj1Dvy+moWDSbrdegdL7QybXmPdUcMMP5rq1aBg4Zq6GCgVhqQ6Q== X-Received: by 2002:a05:6a00:2443:b0:736:50d1:fc84 with SMTP id d2e1a72fcca58-745fe05709dmr12336819b3a.21.1748237365744; Sun, 25 May 2025 22:29:25 -0700 (PDT) From: Akihiko Odaki Date: Mon, 26 May 2025 14:29:12 +0900 Subject: [PATCH v4 02/11] futex: Support Windows MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250526-event-v4-2-5b784cc8e1de@daynix.com> References: <20250526-event-v4-0-5b784cc8e1de@daynix.com> In-Reply-To: <20250526-event-v4-0-5b784cc8e1de@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::429; envelope-from=akihiko.odaki@daynix.com; helo=mail-pf1-x429.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: 1748237469424116600 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 19:07:36 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=1748237432; cv=none; d=zohomail.com; s=zohoarc; b=i34k6QS9ux7JlxwE1MZU3jjycWM9ohNg2a/ZcB6xT3lbsKt2fwPTOKW5QGM8ZZEwYJAkJIwxkHjlWmgILIP+NFYgJsGACSMSlULm/KGcQPzK8OGlsgsOKScBsYaOwkByIb76BzY2T896RsHBbUbssuidwXOhtASRO4FoDOBhAPc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748237432; 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=mxBo8L3NXbm7EbxkgT2admP/cVTk72z8UPC5EzeX5+I=; b=IFUxzvPzouCCi0SJwkd42IKvhFICmB3xkrWrgPDoq4VaQnekHm/a8t1oYV2eZ+ZQjd0lF0bX/8L9jSmiXDl94e22VU7RhzqdhsV7SC9s1F2LiGnrQej2DRN2snNvFVBYjx+DGO+ZCCID/Fq1zYFBPt6YeH4ntGhb5Hr3k0qw2BY= 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 174823743225243.79903235305267; Sun, 25 May 2025 22:30:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uJQP8-00070S-W5; Mon, 26 May 2025 01:29:43 -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 1uJQP0-0006zW-NY for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:36 -0400 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uJQOy-0001xT-PJ for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:34 -0400 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-73972a54919so1304385b3a.3 for ; Sun, 25 May 2025 22:29:31 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-742a9709293sm16225456b3a.37.2025.05.25.22.29.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 25 May 2025 22:29:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748237370; x=1748842170; 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=mxBo8L3NXbm7EbxkgT2admP/cVTk72z8UPC5EzeX5+I=; b=msTLa6Omk96FQz+vFfaE0gAcKLK0m0jhBh3JF7SvxXcAB23OOEPbPedUjp7Jtl45qX TojULu/PHGpSHdjDOIXioMRvC+FFX9TT156MEdWyyRItOSTxJQhV2QSAwGj7ABT7at+P VPAUHMVlC63JvuMQoI67pB95ffa9vVr6jgAUdvv5mnP1LWfBvTVheayEa6UwQqxkMyK1 0wNknkbgr3RjBZyYGUwa10bJzgYcC0XjUd2VJ0XWPUlFYwAbQDAokgyELjHQ9S/YG7Wx dI/QFmi3216zV++lmBh/TW9eMAYp/xUD2Dse9aDIflEZt93E89XiPUTruEW/Y7Qpn7df qAPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748237370; x=1748842170; 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=mxBo8L3NXbm7EbxkgT2admP/cVTk72z8UPC5EzeX5+I=; b=M5o5NweNfFjS/kjvZ1n9ubg36XY1rA+bd1rxrlKnV/0ARubGjVmHxk0s2EoXcWJR56 LHs5J+KCTjuy0C0xD1Za5qB4nwOUSaH/heKIu1c0ZV94eK8p4e7YRI2s/AX0AXx0Tqkq A6jsgqNwYFhXhoimZF89c0BfkBSbQsHmpVhI4FpAgOC9ssupz/nqxvJoeqI827CXCEbN ceRKlpCpWCbWB6y50tIY6uimgXGzQk4Ssw/Nuk/EQShJ0yOFJx+MAAaj37Xd1PZNWynE 218GYGuHM1LHcdwxAzsVkJKQiqDPfltSwNv2K9sOpEu/mYCrdWfBM8QoqKvN/3hTvmYx LlFQ== X-Forwarded-Encrypted: i=1; AJvYcCWpAwFf+kOk2Wkc7hrkITLSyFwqD27aN6lkn06hRcKLeaBOynrn1QGlQCD2n/Z5o9PHSEbw+W7qTLce@nongnu.org X-Gm-Message-State: AOJu0Ywik6bGJJsf59+0m+aVMFSqUyPTwhOLB9jSa79CvitPZJubu2uu q86dTvoX4irvqfMOHqeOHwluisPrhnJvlbASDGsn6ZivmbxIvIUTc57bcbOnvmZp0G9bF1MM6lQ Aoh7p X-Gm-Gg: ASbGncumvTG4ZlroArtV8PkzM6JX5C+QZxXjKiUFX3bjyNHi9/RTTmOvH85H4q6Bvn5 HZSHDmfCRBcmyeVP4Uj5d0Nb9DeHUvheSl2S5KzhWdkpUgHMq1PbqMQTxx9o21LBD0UK1Nqe8EA +VyHLrz8k0P+kRZB2gR7CwWLz5nt1VZA3+n+zhxAFF93BkrQI/iGBSHhACEMuapjAsxM7+gUL3U 7VSEafctQZxMRk70dT83/yTPvsY+Huvm9sN6aNYrgGbI1fEHWnRLoZUfMKYvWs2oDShLpa31HM8 1tzX1AVMX2dx8m2Jref5DyHvWP4WCnX5gjD2rrfSioRRNYNEBClHZa3TfCi19x8= X-Google-Smtp-Source: AGHT+IFCiCnXkD/+u+VbK/twYmO1gTXsoe0kOBCF5EU+t/vqURAxy8cbeE2ysCqAFwQanBS1LCW0rg== X-Received: by 2002:a05:6a00:2287:b0:73f:f623:55f8 with SMTP id d2e1a72fcca58-745fde779e7mr10655890b3a.5.1748237370458; Sun, 25 May 2025 22:29:30 -0700 (PDT) From: Akihiko Odaki Date: Mon, 26 May 2025 14:29:13 +0900 Subject: [PATCH v4 03/11] 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: <20250526-event-v4-3-5b784cc8e1de@daynix.com> References: <20250526-event-v4-0-5b784cc8e1de@daynix.com> In-Reply-To: <20250526-event-v4-0-5b784cc8e1de@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::42b; envelope-from=akihiko.odaki@daynix.com; helo=mail-pf1-x42b.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: 1748237432826116600 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/qemu-thread-posix.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index 4d6f24d705c7..a1b592d358c3 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -386,18 +386,9 @@ void qemu_event_set(QemuEvent *ev) { assert(ev->initialized); =20 - /* - * 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); } } =20 @@ -413,7 +404,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(); } @@ -428,7 +419,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(). */ value =3D qatomic_load_acquire(&ev->value); if (value =3D=3D EV_SET) { --=20 2.49.0 From nobody Sat Nov 15 19:07:36 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=1748237467; cv=none; d=zohomail.com; s=zohoarc; b=IZDPNuBUnd+l4FwpRdy5BjUU3ivBzUg+yP0XHDQrbtTvg9ubBqnV2ogHUvsex2yBiBVZXtjW5us9HYKsWj48nfpl9LpmOdpZI+02UnmX2V9q2ttqkNmyVqtfCliPDR+dFjznTf7lsSxD7Nu+kZ0MVM0ZBPogQr6h99Ua587cVm8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748237467; 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=ylcfQZWNhdn6C7MJjX4V0g4UDPBlgC/24CBbOFlS6Js=; b=Ffpdm7gyaSXa5S+MmPVgxqnFPzC87Iye/g0DpZe//A1wkknlnAcD2qpdtCmuNGpvVsSw9ZYcN+kIPMM8ip/x/M5qlZlgitjRjiFutwOZSh0bIyGsdwyB8S83Oche1Cnk+/kQPKNGtlHyn/RTQszRyW5DcdRQBd6BgciDP9WohM4= 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 1748237467235273.0338795654401; Sun, 25 May 2025 22:31:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uJQPE-000727-JI; Mon, 26 May 2025 01:29: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 1uJQP4-000708-9S for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:40 -0400 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uJQP2-0001xr-AP for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:37 -0400 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-2346765d5b0so4064535ad.2 for ; Sun, 25 May 2025 22:29:35 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-2340c0e72fasm39210075ad.239.2025.05.25.22.29.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 25 May 2025 22:29:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748237374; x=1748842174; 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=ylcfQZWNhdn6C7MJjX4V0g4UDPBlgC/24CBbOFlS6Js=; b=rsNM/6FZzcqGEXVdaOyEhLFtXqf42OuO29VMiIIhzl1awW3RrvpwJeBJfmB6nDbwpz OBWarzgZYEcppRAG+fO9Evb/DiOkE1Fzlkh0e7+LInnxWUj87QTneczuflG87oHT/rfc pDcUTcikKKzrLIqB+kliCV/lQvTRMJI8IJO/SjJaJnG70GwLTFzmZ6E3EF9sPIpZuahw W2EYrSg9/B4Whsw9E1JNzUps4+o5pGAyqgRq89zy5/+dvxzEmRp1agIqUoN4sFp2U+8f k91j7QCBPQO7PT/41/gJ5QDKPlg1d2346rg/ecPkL6cfQdxKhwgRgGFEIb+XDa7mGLUU X/xA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748237374; x=1748842174; 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=ylcfQZWNhdn6C7MJjX4V0g4UDPBlgC/24CBbOFlS6Js=; b=D7FJRulAc88anUYL3nPExUjuW9obObql5nTwOUnIwvVrKPJEneyeh14Pk3CXa7sYLb tn9Zygdo4DcQHqcs1QkLwl+0ZIN76SeYyLYoNCHSiXiZRD8ocMxs6jyrLtxvcJwELLi5 vvRzzyZMDAynyf19vC1AjrDmgsN9padZFzKWmAFCmRJ9E2TzekYLmpLocHD4SXKCzTid Fb3T7OhOv6c+naGZZrUwvVwJz0dwZBgidQc7QIpsPBBpzH0alIN0ofafh+CRpIf87yPR PehBPwyr+o8VJjgGMZkB3XmLvOrR4J6S2j9m8tRbndfetNPM7C2u/80dxOZLiRV0mOCj Vf7Q== X-Forwarded-Encrypted: i=1; AJvYcCVclUlEDa5dkXduclvu8hv++nSJzwNyD1SlWnMaFEmmiDMcZRG5wM8Uh8d/Uy/yr5UzgYglHXyLdr7d@nongnu.org X-Gm-Message-State: AOJu0YywYwpoOSGKAp6v+rIFRCTabzLagcgsAojjYY7R6RUtCsg/4Wc0 HMgjCmYgfezP3OhrDIpNUKIQ0oeo/2uxxwy4r+/9RYuGApI3jGgnYWW8s+DvfHowCv7Qx6hd9C3 uLxZU X-Gm-Gg: ASbGncsG/q6Uc4BPQB0wlF5rMStgVRznaZTUCAJGu1FSIBVSeIrWez6Dg5kMIKzaAre yflOBKrPfyocJE3HFqlHY3vdt8vl45ISmUxOd0m9mDJkwEPEQ//bmGOGXD9PWUgjXX9jCWw9zAQ ElgWv6zMrhZDV12EL3U/Lj1dM6K/HGekV1qDawkdv4x71PUMLpoMUZawpth+IbfUPcEqE9Aslbh Cu4rVBUNdhrpX9IbrdV+cSwhQt2E1ckkfgzNfdF5diJFyIXRJRBswgLD67Jd06MDgXZ1vhOwjHc 4SSgZyPTP+eVTuKUycvrgIa0JQQZPeMNxyIlWMlyb5yTKVilkVWT X-Google-Smtp-Source: AGHT+IEdD0Ly+wv0LIoFBAJn7ydVZgF7NMPPSAGo1KhepTrmdYF3uSBcF/J0LJuA+Mgb8VpGG8EPQA== X-Received: by 2002:a17:903:244a:b0:220:e362:9b1a with SMTP id d9443c01a7336-23414f66af8mr119065675ad.25.1748237374487; Sun, 25 May 2025 22:29:34 -0700 (PDT) From: Akihiko Odaki Date: Mon, 26 May 2025 14:29:14 +0900 Subject: [PATCH v4 04/11] 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: <20250526-event-v4-4-5b784cc8e1de@daynix.com> References: <20250526-event-v4-0-5b784cc8e1de@daynix.com> In-Reply-To: <20250526-event-v4-0-5b784cc8e1de@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::62c; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x62c.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: 1748237469294116600 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 a1b592d358c3..0d6344f4d216 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 19:07:36 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=1748237457; cv=none; d=zohomail.com; s=zohoarc; b=XdU9xgc3B9OnbX6vfACf6iE/vD1pkLwnMs4OPOmcrOw5Tfj6E/qVTnM14djGv7ZGUH6F3JoHqpSTlTlFnjxjFbmhGr+5QKNtfvaTp2cxkVJywk98r7pnxwTMjFHcCrHWwr12bDpJetL/49uBqsVia0g9rtHLuPAyb2MGshU38Lg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748237457; 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=HaTVJl5X/iMHZrzBG8WLsBUCVEErv5uxJdxFT0AP/9o=; b=NxXe4bX9/yZQ9xKAhdswYOSieF3p4SNKNdpEP5bXRtQbwG8LYDYLV2QPOHoUrxR5ONjfR89uSbxLjH/gBwmuDc2pa4vwJO4bOZSM5xEL7mWWNmRUtdj22yyrQOM0eqAUuPMO1dtQ8FU31HTm2+Hsx7NuDJyTODejV94r5DW1ack= 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 1748237457263270.5620183417003; Sun, 25 May 2025 22:30:57 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uJQPD-000723-Ra; Mon, 26 May 2025 01:29:47 -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 1uJQP9-000711-Cd for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:43 -0400 Received: from mail-pg1-x536.google.com ([2607:f8b0:4864:20::536]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uJQP7-0001yC-8z for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:43 -0400 Received: by mail-pg1-x536.google.com with SMTP id 41be03b00d2f7-b1396171fb1so825779a12.2 for ; Sun, 25 May 2025 22:29:40 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-3111f4647dasm3379133a91.48.2025.05.25.22.29.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 25 May 2025 22:29:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748237379; x=1748842179; 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=HaTVJl5X/iMHZrzBG8WLsBUCVEErv5uxJdxFT0AP/9o=; b=BgtiJlcOjpn6aLN2WW86qQbjmQjZBUsBEY7gUiXHi3Y5COWVzzWezO/zogXhQSOwgA YHZ6YSitxkIctiMvX04ZPyFMCZN2VZnGAK5+9+niAL14Pta3ZfdpXAWF40MzWxYOzybi WHCAqxVRp0c1tR5BvUkBteHjs8oIOcaSAhh8z/ttwXdJ8DfjeUKBjbwTCpV/259pjkUv 7K56yx5VKy//glrv8cPX0DZnPBbZ02tk5owzEkl5FBHKRsDlx2HbBrl/vtFqyO2UfAkL IisVvUrZHXaGecQc6yRHixfkpf2+34tgq53lvdoKKRbndff2ooocGaIMj0LY4dsfYBy0 l+fQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748237379; x=1748842179; 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=HaTVJl5X/iMHZrzBG8WLsBUCVEErv5uxJdxFT0AP/9o=; b=OIcpDPbIKeEoahMZ7Sfa10IRAQaUsjAZ3OM/kV5SJWrGgxBuy7NKiZr++CzZHfQJ0i c3M7pCG57cM5TNZv0YOJ54mn+Xod4lYIVENCcp6ZIIhM4cDaEtZ8Iq7k6DKtTpKVEHMu FffBUKHA2JL9lX5VdPQW3zpHUZMku0Ct9R2oFTgudVLB1KMrXR40awCOkAMf/vQzUfMH d8f/uiphJ8umY9hP5+z93QCW1ikMa4Nd2VjeMoUsF9YAyr/7X2uLWYRWCMuvoZi3XWLA aweFTgBoStP2RlmGtl+INCqTi2YydRy/UG6tgkw0IJ+XnT1S96BGUCqq3NNr/565U6Xu 8T+A== X-Forwarded-Encrypted: i=1; AJvYcCWedI36aYERfr68Wu6D/xL1hn3Gff4CuMRVLLYVJ7pk4Q0lmc+z6XKWohWck9vjZeskEJD7y5ocUAmn@nongnu.org X-Gm-Message-State: AOJu0YzIPUHuI43Vd4HGhftEQkU9D7izFVhWSYZ4c6ZNwC+4cTEa8YgN vYp8eXzUk9aJDNwMjVszqiaonHXJCP2KTo1cEromk2ZxI6V6CjxfV1CBPZWZniU19jIimnsZIiU kqARd X-Gm-Gg: ASbGnctnck2IcsvZqkY5Xzg6r/JclOUHCITxXSQE4ttz72kzMrLzxvpq6+qM9Fl5kTl 2UGoKA1I/Vg6spUn9mRp4gkwt1t/u+a5q9/nxXlZBnR0bT+5rrp90Lg3uuBoFtwLMJKL1eyHxgz lL096KrXncsT1PrtBOhBKHG+zMy5oJOipWx9SKgOKiUoQOLQoF0yO9aXx1fZEI935i8xpMfsU5f YKtn5NLaFkJs29qd5ZMw9ODy8p8WZGgeVlWpffV420ioee8PKepuT6yeduAqC+afgKCntTvhrBw ILVdd+o5SUN/JnWmQdg0FZxB739/sQReeO3Z7gkM01suh5nT2Qhl X-Google-Smtp-Source: AGHT+IH03++i2pmBxdfeS59pJcyBNoHbez51/FuB8e50sXU/AfQ63FMq6wGaB5wsS6XToYgKVo/g7g== X-Received: by 2002:a17:90b:3c90:b0:2ef:31a9:95c6 with SMTP id 98e67ed59e1d1-3110f30f0d6mr14311900a91.14.1748237379145; Sun, 25 May 2025 22:29:39 -0700 (PDT) From: Akihiko Odaki Date: Mon, 26 May 2025 14:29:15 +0900 Subject: [PATCH v4 05/11] 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: <20250526-event-v4-5-5b784cc8e1de@daynix.com> References: <20250526-event-v4-0-5b784cc8e1de@daynix.com> In-Reply-To: <20250526-event-v4-0-5b784cc8e1de@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::536; envelope-from=akihiko.odaki@daynix.com; helo=mail-pg1-x536.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: 1748237459333116600 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 | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index 0d6344f4d216..be7f25f9b31d 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -319,28 +319,6 @@ 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: @@ -386,10 +364,17 @@ void qemu_event_set(QemuEvent *ev) { assert(ev->initialized); =20 +#ifdef CONFIG_LINUX 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 } =20 void qemu_event_reset(QemuEvent *ev) @@ -411,17 +396,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 store-release 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; } @@ -445,6 +429,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 19:07:36 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=1748237425; cv=none; d=zohomail.com; s=zohoarc; b=nEFq2JTeyRsUAbBSANQmpdYN9pyAaRW0+VO9cM8DfcgZ6wICH/Wwri2GbogZmNBlD1RmeJjtGGckd90hDw1seI8IsLdlVvpoRxRTzGWoFt4AEdKI3sqiHTytsNhLCEdlupPUjkmMbmXt5FGpLpWSfv85vJtdaTNfvLWuQFT96Fk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748237425; 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=LRmGqS56UwtxXjkW8h/cpyE/WS/5VcSCCyYow5d7ec8=; b=QGeJg4ppBWreaXwflsuD38XXPhY+4PZEzy4pBG9u/edYJfCo7vndCv/GU/4DVPJDscjhSHcK5VhjtFcAj4YmmvwKFKmpfPWOMc0O8v3sN4VCIr+WEajV1LuURNg8SDceShFBDtTllLsEwutv7P5tMnZNRGC2ccO8WFqV5LF8Gt8= 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 1748237424935812.8746759675585; Sun, 25 May 2025 22:30:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uJQPG-00072l-W1; Mon, 26 May 2025 01:29: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 1uJQPE-00072O-K2 for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:48 -0400 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uJQPB-0001yd-S7 for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:48 -0400 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-234488c2ea6so7581475ad.3 for ; Sun, 25 May 2025 22:29:45 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-23211e75f58sm134799125ad.252.2025.05.25.22.29.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 25 May 2025 22:29:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748237384; x=1748842184; 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=LRmGqS56UwtxXjkW8h/cpyE/WS/5VcSCCyYow5d7ec8=; b=azlclMwo75RFV2GB782oyYQ3ZWfsMPP8iSs3cEIZFJ155waNc9+wirtgvbynO/WXiG of0LqBzly0vaq9OxpF6Bp9XrmV+TYnkhgmnJ1vTgwl+nHXTyoA1ZH2H9GmcaCTCWJhxz DMZ1/QSZvq/u5ouqLIaRNEn1vjF+Fketj2sZ4WyL1EgOSCdu3YTMEG/MRgur6F2Oy0Ll roIYAIum5xUnhueQ+ss2A/2fQqmgmuCbJymealxplfXZGIDrywioPlvUcL6u7Eqj0ako euWLAqHrmn+sPpb4ijUDJTzh/5IuKhTDS9Jjv0BmfF5tMIV+QRfaGl0YmqvXIvhuP93v uylg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748237384; x=1748842184; 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=LRmGqS56UwtxXjkW8h/cpyE/WS/5VcSCCyYow5d7ec8=; b=cHdWrDhuf89l81FAZW3+lk2zUgIABg5+ok0uODt7Q+gK12oIBxtkHvaPHzSXnrDa7r qU6NqGpu1bo9LYbo91VX90+wVKNLdhoGJk8Fyv72pO4nypt4XPucuzU0tXU3O2vN7tbp d2zKrTq/Wyak4Wt6vAd9QRUx7NHfdrnmDr/J+FlkMTiqgR6HjJ2qelhUVxY77VAZDgfZ x5DURSnkFDjmwTtNXxvr6jZvpCrgIb9qTW/fZrjQMclBfa8YMfUsDnxHQCJmo+LYdJ2P nr6QkF/FyLirmMQjTrDw6bwFSY/MWPugO3Jsyk3yg2XXDU/Eko3VrKxtT2jdziu0mP5J CXQA== X-Forwarded-Encrypted: i=1; AJvYcCURQavL1woPYmxRA9A691jyA1Ft7v3hNxmIwMZ/2rmq61cSa9mEKDF5+caEm7wN8vt9QwOlQSay6qeP@nongnu.org X-Gm-Message-State: AOJu0YwUvYJQ2uzrEW27OM8EVghv+FfXPOaq4Ur3i8JqFLj9ABCMtoyH gF3eu/vegN/4tumXauAv/3BaoncT1B+9Or0m4Hb+Y5HGrjWsxOyXdo2LQruRMCR8m69G2hUEyjs ntZ9W X-Gm-Gg: ASbGncvjvFdazxajh5OCWowz+ddOI69Ea9Fx6esUurY16VNb7Xs0pptFgE/T53HXYrX za2mliNqPT43N2rDaiPVXJn7kiuSzti7C3CGo7G9a/aqS8XWHNIdiSy3nyUbCbg83k7ROCvIew+ X3aOYDuCFYZ7WPQUwjZ9Pikx/9aFERi3em9W8F/DqfayCMlABZ6uenCw6kf8ii355p9mWGSK8R4 VLl6zF41ylF/hBgXQvCMcKLDfD8nJ0P/XKWObTqudOnlxtojt6MPzGHZuCubzhHPwxTyga/Q7rr zsIjYbyyiJVD61UWnsZMgPyrkuiJ0R17mOovDT98GldqbdUmmrg2YThgDZQqTkI= X-Google-Smtp-Source: AGHT+IFAVMCWqvCWcbzYyhhLcTub/jdYX1Ud9BQaknYQXC8INqEZfZepiBQLrCg5cLtRRpng39o0dQ== X-Received: by 2002:a17:903:32c2:b0:22e:3eb9:471b with SMTP id d9443c01a7336-23414f60848mr120699775ad.23.1748237383959; Sun, 25 May 2025 22:29:43 -0700 (PDT) From: Akihiko Odaki Date: Mon, 26 May 2025 14:29:16 +0900 Subject: [PATCH v4 06/11] 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: <20250526-event-v4-6-5b784cc8e1de@daynix.com> References: <20250526-event-v4-0-5b784cc8e1de@daynix.com> In-Reply-To: <20250526-event-v4-0-5b784cc8e1de@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::62c; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x62c.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: 1748237427094116600 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 | 122 +++++++++++++++++++++++++++++++++++++++++ util/qemu-thread-posix.c | 121 ----------------------------------------- util/qemu-thread-win32.c | 129 ----------------------------------------= ---- util/meson.build | 1 + 7 files changed, 133 insertions(+), 266 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..663b7042b17a --- /dev/null +++ b/util/event.c @@ -0,0 +1,122 @@ +/* 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, followed by qemu_futex_wake_all + * - 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. + */ + +#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 + 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 store-release 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 store-release 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 be7f25f9b31d..ba725444ba63 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -317,127 +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, followed by qemu_futex_wake_all - * - 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. - */ - -#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 - 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 store-release 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 store-release 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 19:07:36 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=1748237467; cv=none; d=zohomail.com; s=zohoarc; b=VszG6xOxWDgpyRS1VZ49B88liUWDrJ9Y39EHdq/03InuDTAkPTA1slwVsUNvRGD8gYupb1AAzbWyKx1nAEE1Y7Sgczu7Ql2GbiX4lGYNCfoEoTZC1GD0tjqGsOChEFGjGHRyuqk67duX7RHhdIw/TxgnUt+zxw4kfDsp165BPRs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748237467; 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=OjfCJiN/0cVyTNhwokKmBbyQ/2ihKP+1DwipmqAN5jprr7MZs4ql7dkZv5jQ1WBU7un2aOeLEoQP3OtTNgxRkoCO4T92olHzfnlPjV0KHea5GKLEZb8ghYj49RGkPtZNZnVfwaLnw863ErTJnRaUTS8SvrcFwFdfOQ10nIpK/U4= 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 1748237467213945.1920205152067; Sun, 25 May 2025 22:31:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uJQPK-00073F-UM; Mon, 26 May 2025 01:29:54 -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 1uJQPI-00072w-63 for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:52 -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 1uJQPG-0001yy-B4 for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:51 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-23428770e1fso8239625ad.0 for ; Sun, 25 May 2025 22:29:49 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-30f364907d4sm11457853a91.24.2025.05.25.22.29.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 25 May 2025 22:29:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748237388; x=1748842188; 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=OTgSycgq6dGaoV/G2Tq2gDzIBdBxY+kSMcIyowI/jetZTBabAWioSPO6Lkt+/o6GRp AulvXMjt2aGTi/+mbWvvxt2hlUXB1H/Bk9mr7xzc44eeHRF6fuOGvQzjrIPQhxQFzTen HQLIxKuydVnIQRG8go4pTvdFbgUTVZ/U9S+5m5e6EC9p073BJI/7eFY9B4Sg9UB3yW31 zTQgSASAUHjgOxO7N4jZLzGjxpCpCPtqpfQLZo1Nn/lFWwdrO2TbXlBSGWmYd6V+n9je owTetqxPtoek9zTIwZBl7k90fi2XepTw/wsM5+qpLgSM9mopTHchHCiyOiksJZwCoZ41 gQAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748237388; x=1748842188; 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=U/YjyYTWo8ZhNK6mFweoXIjViRz/u48/K9Pj0IS3GIgXr284OzN6MOr8sCZhVPpZwn 9diGHvEfjyCoa+FSIPDO/HfZHvOozFIUjS2paFsr1nNvtKfjO9Zch8La4/Hy9yPyOHkq DoflY2a0NXTLBUKkD6j1p/5OGG4armZBTUU+F74bbTXjuupLzE7MokSFPYhdkVa3k/5O 9g9lREf97x+U0Gzez7YCR6JwwJ8txJPd7YCaDcvOo169aI0dozl83LYZsUy//aPIMpqg 6Vr7w7RRMnnKmbmjQxmCsLrVtUrH8cMTeWWlEWIPqFW4l+YfalCMc+MY9Bm85j59uvDz NoxA== X-Forwarded-Encrypted: i=1; AJvYcCUpZWTZ/aHk5zif+QVpw8dvgtJQ2COo0BuTN4RLni2gpWPRLyuhNSYbvlBUCFycuKfNwXHHbD213Gf+@nongnu.org X-Gm-Message-State: AOJu0YxFxvOtrRglILjEKNHrGQcl2rx35MQpNqOGEBG7jaCfb2KCnn+A 8qJCaDwxyDBXyn2PVI4lFK2McL4R+B5kHjmPWvhrfP+ALp9Iihh3oSbvu8z7sFlSt/fl+jsUg5h 6osCy X-Gm-Gg: ASbGncsKCErmKM6XHKTAxSp66hLt2mWWDtifClZZ/cMI5+fiGddxn+2oqkVT8zlMYfj MAAyxfFK8jqrrIUTcRtHJ9a+eallKnDzoLGsMonaGYFPFSxRvslO0twtRcVZGwP2csMczIpE/Vq vx3jgJjIlQv62lEHlqu09+V9Xi117AS3P2j6NN3kZLT9F0pZuzJIIESF2N/mCKLeU7qXfWKjbLs Wn4WZYISqMFPEX3/IIbTTL48PyBPldVkQ2ihA4I6osYmHnKnF+ZvlfiMc+C9SDCMuis2Vq+cPHZ 0oICypY5BeZ2ijCOGK+0CYEzX4vLq/6ixzr734lg5NUQUSXPOM67X1UX3PLY7WI= X-Google-Smtp-Source: AGHT+IF6WmjZSaw/GAef5CH/6zOASqT9GmonH8ySxMIVaI06S7WghtXQ/SA9TE/UNx6hvTZoZr4uwA== X-Received: by 2002:a17:902:da47:b0:234:854:20ba with SMTP id d9443c01a7336-23414fc8775mr131458255ad.40.1748237388681; Sun, 25 May 2025 22:29:48 -0700 (PDT) From: Akihiko Odaki Date: Mon, 26 May 2025 14:29:17 +0900 Subject: [PATCH v4 07/11] 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: <20250526-event-v4-7-5b784cc8e1de@daynix.com> References: <20250526-event-v4-0-5b784cc8e1de@daynix.com> In-Reply-To: <20250526-event-v4-0-5b784cc8e1de@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: 1748237469279116600 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 19:07:36 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=1748237498; cv=none; d=zohomail.com; s=zohoarc; b=Lqu6+Wgczd7FLWgGw8xJaPhbGxcDUNIPzuziFoOPLoz88tVgIym9ncIebjIqjZ8bIuFZ8shTuQzxT/7OIEbbAzxbvmwDX2SFSS3VEcHn2nh1M2BTaR3TQMeA2C8oWX4bzP2Qgtzw4tL1DsyoBqkLq9ZSTJAC2Q7wUlFbFrZLgj8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748237498; 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=Dr5YmlBt9mn7VaBQhTTUFs3rHk8A9DyBMWMlV/sn2BA=; b=AQnfHseX5uDCMzUGsGyiOfcqzSKBgWsRuKnGoLmNyT+YRb529Skm/2DdNotpZJYC7gTtU71KWJp59/1PwjqeXzNOH0NzxybJhGGy871UWN/VYsTyZyBGdu2rx7+h6Hdyync6o53wWJGXdgqCkE4TVUdavNgi1lrnnAWYtYowsWQ= 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 174823749861489.71218370988458; Sun, 25 May 2025 22:31:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uJQPb-00074Z-R8; Mon, 26 May 2025 01:30:13 -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 1uJQPN-00073X-7f for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:57 -0400 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uJQPL-0001zP-4X for qemu-devel@nongnu.org; Mon, 26 May 2025 01:29:56 -0400 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-22e033a3a07so13502275ad.0 for ; Sun, 25 May 2025 22:29:54 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-2344fbd44aesm14493965ad.249.2025.05.25.22.29.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 25 May 2025 22:29:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748237393; x=1748842193; 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=Dr5YmlBt9mn7VaBQhTTUFs3rHk8A9DyBMWMlV/sn2BA=; b=pMtVqXdA0YIx1RBVI04msUfZuZ3mxqQXsL0PgUnqx2v9iQOa3Ji6ysCPZy/PkAdGZ4 XUHfZrHewynrO7yuYtM3NQ47coGhdbJIOwMYHaESJxrtcdYw+EY67guhvnhiyLjHwac5 +94Zlk5m5dvKRv2tboFRDMdJtf0jCZUklPfU9HYR98PrPIGY3u1amHUaDXINp9d0FKZa UvK16cKi/nkSR2aXfpCAZ3PGailxxSvFta769lBDqW9Qoz3YQqM2gKxr1QwMV/VqCfHK qKWNU7EiNasOrt8blmEeS66vDgiRM+ex5L9nMI50KirS6P/UN+2HkF7OXZ9AOdkRLEKU dhhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748237393; x=1748842193; 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=Dr5YmlBt9mn7VaBQhTTUFs3rHk8A9DyBMWMlV/sn2BA=; b=Pb/m8lxRZXiD4aLsbDXLCKDsNx+LNt7mOcMZDo5fZGgZOoBqbYBW291rxrytucaM+b +093HS/8HgLeo432NW+yy+8AMxQo8xSzmYqQ+hQkq6Dxc5OKFQd1reFXv1NHk2gjOByU nW/bIS+e6Rv33H9G5w+UkSOP6ScKI/He8swHYhhf3VrWoLzX8nCTNBPRSgfFHTsdaBhF dmrl9XR5LljAuVo/gMDDnS6okC6v5EOZbrcQcWSYyaNfv6fveM3yqtJGkQOPIIQcGr+o LE1aD1zcNWCAOiKzGeof2IKgFIudS8grfMbANIskp1SwqapuTy0aC3Oi/WkjuYPtRHMC g+EA== X-Forwarded-Encrypted: i=1; AJvYcCUgMmRtk0KOfEuzkM0K8L4CVxUkgGTtMGMbl2Nl32NOPY/nZEHbwQm5TcwYbupWNvdX1gsZ08Kc+X3c@nongnu.org X-Gm-Message-State: AOJu0YzCj+WIlpSgs/7xhx5HbMM9DI7nrwH+7zbn7xhFgmMbnirhDawS l/l/+qBt67Aoc1lmOCV5DztOMbOxH9pnD5or8S6ZNX7oRc56W3in8KYZwAquChx0GqVK5WoHuPG tWWpt X-Gm-Gg: ASbGnct6SiRXO70+053hiVrnmPv+lVt2BDWppEzB2u7ToNnLSbVZUar9JBwoFIclrLE Mh50kp95hOnBHXkfDDhR5nPPU2L2AaygXvDENc8VRHIdF5jCzFOgF4INhyZIMrG5/kHnFu4L50y V9C1TPks/bhqb99kGxacg6TDTuS1trqhj9RqtTZb/xe0ImRjcCpri7IboCTwvVMD8mmG/Sbg20A H9lBgTIaJjWBO8kmPen+nddGvuRP03XBl1cJb4BkalUj8joJtVVf6Zz0PNPDTmhWn1AOOT4IUsW pF3AN9pdIZ8H2rEekuhQ/HiYdvw5MM0vkSL4OgZNM0a0lg0Bm9V8 X-Google-Smtp-Source: AGHT+IHSBQDnt8h48OlOv7oXZF0Rg9lIlYacOqnRWwrpGpslWc6dqH/g2sIhvMaY0+VV3wf7E9l7Fw== X-Received: by 2002:a17:903:26c9:b0:231:d0c4:e806 with SMTP id d9443c01a7336-23414fd3bb5mr91711225ad.32.1748237393322; Sun, 25 May 2025 22:29:53 -0700 (PDT) From: Akihiko Odaki Date: Mon, 26 May 2025 14:29:18 +0900 Subject: [PATCH v4 08/11] migration: Replace QemuSemaphore with QemuEvent MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250526-event-v4-8-5b784cc8e1de@daynix.com> References: <20250526-event-v4-0-5b784cc8e1de@daynix.com> In-Reply-To: <20250526-event-v4-0-5b784cc8e1de@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::62f; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x62f.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: 1748237499740116600 pause_event can utilize qemu_event_reset() to discard events. Signed-off-by: Akihiko Odaki Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- 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 19:07:36 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=1748237428; cv=none; d=zohomail.com; s=zohoarc; b=AFSUbxMqL9+V7dUMhXGaOjwswJASaaKYDid4AVGr1fgab74Ko+KpfLk0T347ppdJbeOnByITmqACLFQAJWNZZB1xqNNWrKT7wuoiWz/p9RWdjHf9xYRqNIaoTKpA635Iufyu+2tGpe+tdooAp+Y9U7bX1IagLdGEc3tuVgB19B4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748237428; 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=Y56cHzHZWlV77Wo3JGLP7D1wbcZaYNSJhjNY7y/xjyk=; b=GpObpdhrYLWgR8HvclwzhWJIaPHgpGfIZRgq7Z66aZC4i6J0tJkG+KQ2i4e/+Bm5xBZn19AcIJ4gJAAY8BMNWGTbJv8woVqlptmH+wyPm2F7qYvoNPK5VyL0AVusP5zRwfDBJ/Tcm4m3pQf3k/nI5tuvLzgkA+vqbDlWFTMtJsI= 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 1748237428980947.1272544219211; Sun, 25 May 2025 22:30:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uJQPj-0007CP-Pi; Mon, 26 May 2025 01:30: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 1uJQPR-00074C-RP for qemu-devel@nongnu.org; Mon, 26 May 2025 01:30:03 -0400 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uJQPP-000201-RW for qemu-devel@nongnu.org; Mon, 26 May 2025 01:30:01 -0400 Received: by mail-pg1-x533.google.com with SMTP id 41be03b00d2f7-af523f4511fso1337519a12.0 for ; Sun, 25 May 2025 22:29:59 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id 98e67ed59e1d1-310cec3f320sm6925739a91.9.2025.05.25.22.29.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 25 May 2025 22:29:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748237398; x=1748842198; 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=Y56cHzHZWlV77Wo3JGLP7D1wbcZaYNSJhjNY7y/xjyk=; b=sNl2fY2sYTebBEkr6Ii5r5aWAk3Jz6udeORoCRQkmmaY+yaWt0JVAQVmJ146CXSgKB xK6V+C2h43n353htT9c/4GP4LS8kMzecgP3ZoJOLCS/OzL3ifMcMPA0RJvc5Wc/wGM4X OC0g+Jve6dntpATMlgP+mP8s0nTSlvJAj+LkdfFHZjCFyZVuynzNwAy4mavOcP5cKNh5 J0QO7cnBf6NYhaSF8xE4axMT9BIrMFokDFB+Bux4LBQ44PAk5GKT9JvaOEKcVDqxCpL1 3vXHX/HYF6PLcZu4qx5a9CZ92GFjfKqkqoXCn89VQhIbgxR3XliioEk5M0cFNvYGuNLG cLxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748237398; x=1748842198; 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=Y56cHzHZWlV77Wo3JGLP7D1wbcZaYNSJhjNY7y/xjyk=; b=E/WXykFjYxhj/DSQhxh/ZBxELhhJsklB76ka6EgRbBeEIgHNZ2QE2tmYyFSRCevRw1 9syGxQhrfhQPtqjcY9tiKV5GEDTCt7UCY7J8lChIRgjHGErsKuFtuwb+2dTzslyRF8t8 Fx6Qo93anhGhmlasHXQWWc7JIjpXMIBlpJgpUAgIRGuYXnyj8wtRBrzwschqEQxF+ta/ ex74dyQjV78UJ+CvnDRbGKXM8t53RPAdLbiRpcD2DzmfnVzv4GrT+zVrE4WCNnnURNfN KlP4WQoeR+Ga+9jrFcCOiiucKNBu2cfsmC8JHXebtNqpCkcBtzhAPJi7E/ppoZmzXw9l qKkg== X-Forwarded-Encrypted: i=1; AJvYcCVne3sDweTKieoSo+HA2jnqWkUBMoHN4fKHFZ/y9WM29DhMWSRcNJD1XgkAuwgtHX1t0PKX3Mw4cSmQ@nongnu.org X-Gm-Message-State: AOJu0YxthtKjnN2Gegdk+NpZGylP4bFr4LHh9c1UZXfH8/QTkgjSSczU oCP8R4os+X/UfLOrjAXAT1pU9J4VNau2ftdrEPkGVJVOIRahTxPPez7Ng9wJNIMVSaw1l7AYeJb QxgIX X-Gm-Gg: ASbGncsGrH4UP0yNFiDCnjCkNdawygFmhxzc3JQcCf3rvsJC998qpfzdXr1V/iDc3eD UEyLNPveQRXgXv4Dp0mBUbJLtsdtE28NE8c4kRYMwdmyJ1dFerIFw8mnCDVrW1HSyV7wPMkjRlE LlOwm7ykkwHPJwd5kkhQYCO5H3CN2pwFFa+AFEGRGHGeCX/jKSmShokAhFqp96c4/UpxrFWIIXu Yt7SacsW1BNJwUsKD2fiXDkCBGqEAiMeqq/T/ICr3wR/Jz3o2vKr9YmvO6p6MyZwTY4igE6bYs0 Y50KVEGrla8rN1zsmsSI2wD0ZTzPMeWHhAPOI4JYqGmci/0Os+Pi X-Google-Smtp-Source: AGHT+IHDJjlrQBldMt9ASthhJIK+Jb5llLpjxAHKZxE+OsbwDSgqUuRVtQXVbkHAeWBFDZmeyd6nxA== X-Received: by 2002:a17:90b:390a:b0:2ff:58e1:2bb1 with SMTP id 98e67ed59e1d1-3111203488dmr10246656a91.32.1748237398035; Sun, 25 May 2025 22:29:58 -0700 (PDT) From: Akihiko Odaki Date: Mon, 26 May 2025 14:29:19 +0900 Subject: [PATCH v4 09/11] migration/colo: Replace QemuSemaphore with QemuEvent MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250526-event-v4-9-5b784cc8e1de@daynix.com> References: <20250526-event-v4-0-5b784cc8e1de@daynix.com> In-Reply-To: <20250526-event-v4-0-5b784cc8e1de@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::533; envelope-from=akihiko.odaki@daynix.com; helo=mail-pg1-x533.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: 1748237431244116600 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 19:07:36 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=1748237513; cv=none; d=zohomail.com; s=zohoarc; b=UdooJtMaRrHU90jQiecudv9fy74jRDZmU0oOd8WluzHD5Oghz4bCMdy3iuLq1TmBS+kVxdIDpzvBpRaJ3yGxp9kQwTKMTybL0m4GomWwCevf5xjG2Kb4cqykWYwZIaEn9WRErHUKUlBDWJCqeuQbFwiQ7Kn+MQMUDp3d425cDiA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748237513; 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=gyctTxW9YWwQQsr7j0HURwnrUtNOJcCIcfQWTmt+VU4B3hCH92FBcYirRi9pTBBRxyXJMsNZWYa43hxJgdwSy3KS146/0npVi0hVFGlAPyWjFb4ozuq9pY3S4sSbshRfqy1AixojsSVuwpB0V6oBO4g1/EhvBvfBmCcfCYCfZv4= 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 174823751300436.46917552734499; Sun, 25 May 2025 22:31:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uJQPl-0007FH-QY; Mon, 26 May 2025 01:30:21 -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 1uJQPc-00076N-0z for qemu-devel@nongnu.org; Mon, 26 May 2025 01:30:13 -0400 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uJQPW-00021a-5G for qemu-devel@nongnu.org; Mon, 26 May 2025 01:30:07 -0400 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-7376dd56f8fso1919940b3a.2 for ; Sun, 25 May 2025 22:30:04 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-742a985faf3sm16295007b3a.125.2025.05.25.22.30.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 25 May 2025 22:30:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748237404; x=1748842204; 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=fQUMqTu8eb/nlY9+NQlyWEuAqvlltwWrjQDUf3rmxmrZ3MZZZs3YroGvxYTFPyQBqb 183KFIyQCxtNYcfkaUadSZE8vv9PBzx80num2+lUptMYbLjBh8zst9MCAf36+X0NDat2 JJkveB4zdCa0C5uPTrToBJXGAie8OePLMiN2mgO57+uMiav458K7Fi3I20TXR+cmDK/C He6s2Bj9hC2nbXftw85dFdHxYDLxp+SQEMXEAv0DMDtEsX8XCEezVWqZ+ZgaUwXRu2lS Z7FmfreUoszarSlEcLyl0WgCiVloVLN5FrIAVpbP4zUJKuEKGYMJCVrfngMdrJv/LIHh rDKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748237404; x=1748842204; 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=F7Bzbg1Kcz18eyOYL/wXwY95e0z4OpEomCApGkS29G6RoCqNmEYcRU+O1GxWhdBZTI 9cznNSmfmSUAOnOvLzVya51P9C8jEXjSFDFWXMsb701Q2stk+V/Mm3m3KpTi38n3GyUF i5hzNn2/wGKR1wEuXcOSUGyCSJmWgPRoKHz9rMGpmf7hTXxweKwHC5FK1pcb2GHi2BK3 cDrtnFt3TF+Qbt9nPsplH18qvsYb25s31if15AAA6P/I4sOBP7dw3uGhPEd9bOLfzjws +Tog/IIeqUc2TKqlDq/Mi+nSfUMSzE+dinR0TJD1/B2jlmUKZGNu8LyFW0K7HgesTnTk MpgA== X-Forwarded-Encrypted: i=1; AJvYcCX4qXAPxpHuFLilsVSxvNWVe6keTpWRGBcimb9QpXiqOvKnpSxFys1oa5uQUyMyHv/uQ1YNLFs2SqOw@nongnu.org X-Gm-Message-State: AOJu0YxZ6VAk4THPNFsR8ZXquW8RyZzt4WHkEZ7Hr9p6teMlsUW4zxyC c1o9f+NSHEaQX+TT3ds227fQKYarEUwi+pzJ2dZLmgEUj+UqIV+IkZ2gye7kGjHefSB/gOc3y+l zwX6G X-Gm-Gg: ASbGncvPo1TKJUWdw7tuVaKq4Fp/2uNRdB2N5VwQGfokdZnBroVrLpQlocYtPqLyDMf 21PTb9CRv+1Mz50+IWAotqB2NrAzWTUl4cyn5PlqpP0WacqPpFp4xIuxDYpKyB3jRgXVvXJUCHR Zg5OYT27GUOE2r/e/NSyBM7VA3G7GnWLrNoA/4l/LznYNVpSpkje2VuKyGfQX6i5gI7q1fVstzF TZKl4L2w0EQ8Wlt2fi6pRL/XEfp8r3d7ZW6KYyyb5Gm6Wn6ZJV0GVhFsvDIN+G1QSwo/kYKSHDM OgG41xrRr7FoKLd2GXlb3ucTzcWcUoC+M3sALQKwQV6KIwkGGeIWnLtNnkDoQt8= X-Google-Smtp-Source: AGHT+IF/Wgl3eKMvYPj+QZ1PydCvAXCCXs4C5WqZ5o689BC6724+uth80ICzdwwcbZourzgjUcZ5Sw== X-Received: by 2002:a05:6a00:3a1e:b0:736:43d6:f008 with SMTP id d2e1a72fcca58-745fdf77ab7mr12173469b3a.12.1748237402664; Sun, 25 May 2025 22:30:02 -0700 (PDT) From: Akihiko Odaki Date: Mon, 26 May 2025 14:29:20 +0900 Subject: [PATCH v4 10/11] migration/postcopy: Replace QemuSemaphore with QemuEvent MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250526-event-v4-10-5b784cc8e1de@daynix.com> References: <20250526-event-v4-0-5b784cc8e1de@daynix.com> In-Reply-To: <20250526-event-v4-0-5b784cc8e1de@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::430; envelope-from=akihiko.odaki@daynix.com; helo=mail-pf1-x430.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: 1748237513959116600 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 19:07:36 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=1748237542; cv=none; d=zohomail.com; s=zohoarc; b=ERRq9w7zGH5Tog98KHnypaAYL7RVXl+Lj7/mOw/B61AaKqqXZs8BqFReYLEcGbDgKmEtkPi4xkh0VM9YFnbYWnvrn5rNA/ImxeJ5b8bhcgMMFuK/2dCSTaJHo6ivZn5GYdJnAYTG4mMXYhtJ+3iGI4aVPsVpRgf6Oluwr28B620= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748237542; 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=ZYkQmyhIuCHC0Np+Abs5u1kXwWUosVTvjnG7zE5d+24=; b=iq2Xcqnoyfdnyhwy4EK986BQMgOK6r1mYQKeAxWfobuiTTmvLlzzpiO5krdp94iU0N7EGEqHjXvlXnTd2dW5NSIsWbfbSNvquHYAnbqyhaZHvBSrFkDPBgeOL7gR1NAsBEebf2BOVLEaE3XVe3RrY9lRBCjKz+dNmAlQv6tgMy8= 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 1748237542933147.16908399508327; Sun, 25 May 2025 22:32:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uJQPn-0007Gr-8K; Mon, 26 May 2025 01:30:23 -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 1uJQPe-0007AO-18 for qemu-devel@nongnu.org; Mon, 26 May 2025 01:30:15 -0400 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uJQPa-00021s-0k for qemu-devel@nongnu.org; Mon, 26 May 2025 01:30:13 -0400 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-2346765d5b0so4067315ad.2 for ; Sun, 25 May 2025 22:30:08 -0700 (PDT) Received: from localhost ([157.82.128.1]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-233fd7d561csm43057825ad.101.2025.05.25.22.30.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 25 May 2025 22:30:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1748237407; x=1748842207; 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=ZYkQmyhIuCHC0Np+Abs5u1kXwWUosVTvjnG7zE5d+24=; b=LA7a44MYtt3/bPzbIBBqtC3CzedhOcQiDrnbe9uNG1/WJ9MheCzgFSq/hifqOlkPpA TnISPKxVcbIJcOCra5U04jM4n2w0yOG6Dq9BOxVnY9BtxorCjeB0dnoMVl7s5VkFRMUz eq6dxxU20IZN4/McOsaVwj4ngPLk8ouHrM1dhmKIhXN5Nt958h7sP1qT7blwR6TP3OdY y0XFEBkWvTNN5s8qHZ6PVX3sVOVVDGkPV8IPmJAiOjQhJNYc+mmyitrkC+iePJKO6uA7 b9NwJgqlTabIiYQ/CcG1lioG5x6I3gED2P6aS6XRXpoyu6l6FdqBL/CgFFs0/6l8npfS ufTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748237407; x=1748842207; 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=ZYkQmyhIuCHC0Np+Abs5u1kXwWUosVTvjnG7zE5d+24=; b=Xy+zu/jV//12KMHcsray//50EodHceLhyOT9ZdllDYpjWcHG7swbMMFHD2VJf6z+Ir Wz53qFXtRFQTn+icIptws3pcMeO1tgyKKO4j5xbsPvQcOGNUM7lVyBUks5kFqUZ5DBQ1 TXu8cCH0JwBvxmUJi/+W7ILswOakvEBAD2h0QZmGKvnlPHQVvEw5TW/qWblvgiuwJiP6 GxP8Zrh6OqE6qHvROKYPvOBifo3UDVdzSYP8oD3138L7vKCRyhlzaI4sEYYd+QWzBroQ TzkFfeTOw6oigNjLxhSfrkdYMpnRx/GN2LwcPL0dsiit8t5VfWAYLubSXyVvdN9ICIe8 YNvQ== X-Forwarded-Encrypted: i=1; AJvYcCWThdi/5g9EdA7UNyWGdFp1Q4DZnDhaLdMtIO6tWdWDV6EGX2cUYjL5kEPndvJJ52ozkG+6y59XVN6w@nongnu.org X-Gm-Message-State: AOJu0YyVHGWvwU506UxC3wHab0ZfjlN+ReH/QXQikq+ZjeMmQ+s3+5Eo 69+L7pawXNQoXft5eF+iYZzafLLkg7Bt+uZ0JitRTNRT1GGUSXaIIjZ9OLFqH2kYG4PNoJLRLd/ ww4vo X-Gm-Gg: ASbGncvuNOGyB22q0BwVOfP/Iiyvqc7tnhmArnAeRWS8kZYOtC0EgTt7fc7haW4sDZa XVUcMT4Ru4oZF9Ss/NT8S/g83vlOvCmOiNxffkJMoZYcKlcX4GCYPLtRMGsuC280E0fxsiT89MI o9Oi24O6i6lryT/qtsfQ1Pb4IDuLQ8JVA1ItviYvogj97MUdWi42m+rS/wF80A6WYmzjBikhkU/ FDudwHrrEPL7NebvTMxl2ii+ou8UQjEt3EtTH7Yp3rHXKtIlWTyml3RmmdVB2WxsCIHfcIUVPnZ sW8LfYYaxBQZyw/z+UFBOV9gX3lNbvBLIn8k+RBBrLg/9/ziPuHqaIpN/YMKPOA= X-Google-Smtp-Source: AGHT+IH1thV4tHLooqSK1qFnlEmeAuBlEHlijRiTgWl0W8c8q/ocx1mqT73FChp6hTXhbvrbKUwZuQ== X-Received: by 2002:a17:903:228c:b0:22d:b305:e097 with SMTP id d9443c01a7336-23414fd3e53mr107642665ad.50.1748237407282; Sun, 25 May 2025 22:30:07 -0700 (PDT) From: Akihiko Odaki Date: Mon, 26 May 2025 14:29:21 +0900 Subject: [PATCH v4 11/11] 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: <20250526-event-v4-11-5b784cc8e1de@daynix.com> References: <20250526-event-v4-0-5b784cc8e1de@daynix.com> In-Reply-To: <20250526-event-v4-0-5b784cc8e1de@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::629; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x629.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: 1748237544256116600 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