From nobody Sun Apr 28 06:10:33 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 149039298485925.349313401546624; Fri, 24 Mar 2017 15:03:04 -0700 (PDT) Received: from localhost ([::1]:35097 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1crXIU-0002gL-Oy for importer@patchew.org; Fri, 24 Mar 2017 18:03:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40568) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1crXHd-0002Mi-JJ for qemu-devel@nongnu.org; Fri, 24 Mar 2017 18:02:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1crXHa-0007R7-7m for qemu-devel@nongnu.org; Fri, 24 Mar 2017 18:02:09 -0400 Received: from mail-dm3nam03on0098.outbound.protection.outlook.com ([104.47.41.98]:6257 helo=NAM03-DM3-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1crXHZ-0007Q5-N4 for qemu-devel@nongnu.org; Fri, 24 Mar 2017 18:02:06 -0400 Received: from vroomfondel.redmond.corp.microsoft.com (2001:4898:80e8:a::5cd) by DM2PR21MB0058.namprd21.prod.outlook.com (10.161.140.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1005.1; Fri, 24 Mar 2017 22:01:59 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=J6u0VKa9YxrQdVo30bm1ts7G/lY+dUtgIv1cNlc5aH8=; b=JGVfTvFn2YFLyicHw9lYuPoYkS7xPDJvjgM7tR0nOqhX9deJolNk+lZaF8FXb1IvuIRRfco9CnYjdwaNOtq2mHj/eP8Q3cUhKpvO8j6NwYImjmCVbCf7MR9TykoUAPxHHrplpE157wCqnX3T3grNFx/4wvq0S2gND1EspumLJNI= Authentication-Results: nongnu.org; dkim=none (message not signed) header.d=none; nongnu.org; dmarc=none action=none header.from=microsoft.com; From: Andrew Baumann To: Date: Fri, 24 Mar 2017 15:01:41 -0700 Message-ID: <20170324220141.10104-1-Andrew.Baumann@microsoft.com> X-Mailer: git-send-email 2.8.3 MIME-Version: 1.0 X-Originating-IP: [2001:4898:80e8:a::5cd] X-ClientProxiedBy: DM5PR06CA0057.namprd06.prod.outlook.com (10.168.110.147) To DM2PR21MB0058.namprd21.prod.outlook.com (10.161.140.24) X-MS-Office365-Filtering-Correlation-Id: d244b0e3-3868-4f99-ef08-08d473016563 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(48565401081)(201703131423014)(201703031133020); SRVR:DM2PR21MB0058; X-Microsoft-Exchange-Diagnostics: 1; DM2PR21MB0058; 3:6DuWpWjquYvVAn0XU3KxrCoM1GBJWRrrFrdWV8U1xTBZQXC6AltNr7mqqX3pC5pujTPoz4mOWuy/sEVCZfU9bso1CSZPziYWmDMsXxOsPpEPBZd/yZl6VVJ0Q1kWG+7V8JHAXxiBOE7g7dzlvMnRX/2gnvPn9D8alj2cst23fbZJAFlDhlBXkEjYPiqBAN99fMee1DWVIGq5dVGHBUx40MzscjQa7B4154W1FwozJsDmxua69joWPoqNcYx/FZq7FIq8W3vnGkF+jRwqp9o874F3ZYVj1c/XN5SUiNOMCO3ArhF7QvsvtQ2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V6z7kSFDuIvSkenMDu0ZVKbZO1CelGf/VbCf3H13xlpN9a3Ht5WNhByHTCSTnD61yizF+EB5PrT/LKyx8MVBWSTSFBzSZEjOP+/nVaepbSRIkCbmkGbGio2+JD10SeoH6ZcRcRUuGh/bZVrcyL7llY+PO/Q8RUIXMqZtpZHinFA3Z4pAPDrHwNNm9kWXCnnVi+xLdZ7LvcCofMj7TJ4sCdhyOBH+5U/kj6M7YL7CQTtCevep9bUK/j7EEQyMQOTMGS2DLX7RzESCpVIxtd3 X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(192374486261705)(788757137089)(5213294742642); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040391)(601004)(2401047)(8121501046)(5005006)(3002001)(93006021)(93001021)(10201501046)(6055026)(6041248)(201703131423016)(201702281528016)(201703061421016)(201703061406016)(20161123560025)(20161123564025)(20161123558025)(20161123562025)(20161123555025)(6072148); SRVR:DM2PR21MB0058; BCL:0; PCL:0; RULEID:; SRVR:DM2PR21MB0058; X-Microsoft-Exchange-Diagnostics: 1; DM2PR21MB0058; 4:TcXLrTyJv/7mJk9k7RcbA/+MqSPA897KgL/IBDeI6r2x8o5RCLdYimN2y69GOaLMsFjwZr8alYjDy4GWPqXDIZ/zLt21scJVsE4VKfIdH7ON/Kwu3mnrCbTDJgFUFQaZCO2VY5vTgR2HORz6WL+zh28k27BFNrSH1KphiHwJEhMhmHr3PMRJWwd0OQ65+7FPnjJ/XAqSUa5136m3YmDqby6fMEbxdwAkWk2HhfzIQ84glFPBvm28pXciMRbK1hXiiCSghRnuAHL/PeUefmxQe5eKjgw0DJpaAdXDkcyGw2aovDG6EpqM+9vmWAPkHa1qKx1WramV6bKIFHiSrPs5zm0lAdmyrX1NWDKcuvN1kP+P+b1sNdRcbbnwCjnTlrKzbQ9+A8tHRllIriEaKqLGvpKuRRsA6bKmgm20RCm9Lb7JM+4wiybmXqIW3gDnnJvcUp56IGRQ+eZsPbkmkKyvACz+aH92fCFnq6XnRB0L+2vwOUkJdG2LkvZ82ngGlB98kt1S9m45tHSvxj+XzItQtADwKmpEoSRCfc/9XrM4wKS/MlbmQUILJp6/RJ60BMmyhJP6z8+FERBNSz9agsszmv58r6GNtIC9zl89TXrXnMb+9+6G1H3sKhpF5ziP97T6djyEvjMuJYnT3yKPRnnjjedfW0GVKOtZ9+f8Q+hOCclNSgcDVcUTFG44aBsz1ArB1rJ1gEbI8+qlqLvMA3gHnRGslEXs+A/4Y3SKg1D7vCIr8sxxRGuzKWHJsN4OgIbWrCA3/KWZ7WCEq8PHbS5l169HhNzoh1XQpCpVkv8DlfWXUX5MQTTtT6XVd6yyF9tILRS8rGLB+5mg8C1/04DBJmv/+d3I4vf47B8D5TDOl4wfWJ/tcb4tmvqJZ+kT7POM7eGSegO1kpBuGgn2MPhtrQ== X-Forefront-PRVS: 0256C18696 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6009001)(39840400002)(39400400002)(39450400003)(39850400002)(39410400002)(39860400002)(305945005)(5660300001)(50226002)(50466002)(47776003)(53936002)(110136004)(42186005)(189998001)(8676002)(25786009)(4326008)(48376002)(2906002)(81166006)(86612001)(7736002)(38730400002)(6116002)(36756003)(10090500001)(107886003)(10290500002)(5003940100001)(584604001)(5005710100001)(54906002)(50986999)(1076002)(86362001)(2351001)(6666003)(6486002)(6916009); DIR:OUT; SFP:1102; SCL:1; SRVR:DM2PR21MB0058; H:vroomfondel.redmond.corp.microsoft.com; FPR:; SPF:None; MLV:sfv; LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; DM2PR21MB0058; 23:FXDDipqFtM2AbPaEP+1GaUwHhuiWzSxcxOU0HO3Bk?= =?us-ascii?Q?m18tHs9hB/Qdnv2A0R4aFmFPMTnM0wX4VlPYOg3we9rWk9UTxRjlnB1sZmSb?= =?us-ascii?Q?8fx/+ZEVD+GRtMgjLUG7LpqdHDrSqvZvGLvX0sxjj102X9tlXdUoPJ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1WCi79nrnYaaCvf3/Z8xNH1uzl5sAxE2?= =?us-ascii?Q?xujiUfMDx+v2wtymhS9xeI0SqTPdwVSO+1C9i0brtnXfG5YTw=3D=3D?= X-Microsoft-Exchange-Diagnostics: 1; DM2PR21MB0058; 6:22ZX2w0KRoVnYPPGft0I4vWmn9dOowUStDaxL3ab7GGYSzVjqGiZLOoRX8oM9nSaMBDC1nnZTqlO7QmKfX2enwEA1UeV+s79jN+USF/7GiUY7UL3b06Fmq8AKflT4XGenHmeoHnRRAB1ymF7IILcSzRuuThyB5Z7I/k2s/tjjgusABBMFX/qy2fribIqRQ03WBVGRGe8NctAL5hAfmoMkhZ6zCpkVPIyKMbAOscNadN9JhS6AKhSG5Z2kuD3++gXFzXm/Hj+pBIo0BMpv+vUF4qAfEMDC++HfqdbfGsU2TnWSn5KoLPcWxpNt+BI6ph69afx34SsmcxEubS09L68fd5kRGCnua/F+yj8IcCSn2xBrrWFzl10MsQ/gzZRBFFnxY0xIoZPyTs42tUf1ffBAZzDcIJmBvQ/wbF6B0Xj6fk=; 5:wH7R345nrsJzUVyL5Jc6LfesPWAEXvWXnuagX2wJkj4cPJ2XlWFf2CNEPoJdaeAbn/TaqcAOw9RIPY1jrbYNTPu5fJmlEKROdzKno1Vban6rWMVJoX40b3/JN1w5zXj0W23UCiunTZQgSSLXRf3Zeg==; 24:KeS/oxKgMbZ6+WwaklOqK6uiSe1H+IVReGsLAFWVegy83mk72fpIz4uo+fBadamli3UV4y6ahIGr2h6ofazYx/GX2HudJao2DODEE/O4b8c= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; DM2PR21MB0058; 7:8KNZ9mn3RK2jbZzPvMrjyI25/kEsy8XhQk0QWtVctS22nli8umm0WbueKwRtWxmP37lkVbBtW5a0aZ0SCZTYSN5GXzYtvMSw75ZljjPGlqVCRT2FHPcbbjn0aJgipj9AG+mNQHeJ7hforeZkyGxn+jPNT+IE3T6pb/XTSwlr4/dWnLB3g+qCXE45L8tegDzQrX0d1nLg5/t72l1aoYjzFf2VjcXfSn6MgtIDlWYkZxAFSo2TcyDHLHwNYN9ZLDgBWFUhY2YneP0HFxhYEWW1uQGrseaxD7do+p8RW3GGkT4xtOzwZZaZdAhwPdMg6X91lQkyCOIayfh7nhVH8RXlFA== X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2017 22:01:59.6323 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM2PR21MB0058 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 104.47.41.98 Subject: [Qemu-devel] [PATCH] win32: replace custom mutex and condition variable with native primitives X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrey Shedel , Stefan Weil , Andrew Baumann , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Andrey Shedel The multithreaded TCG implementation exposed deadlocks in the win32 condition variables: as implemented, qemu_cond_broadcast waited on receivers, whereas the pthreads API it was intended to emulate does not. This was causing a deadlock because broadcast was called while holding the IO lock, as well as all possible waiters blocked on the same lock. This patch replaces all the custom synchronisation code for mutexes and condition variables with native Windows primitives (SRWlocks and condition variables) with the same semantics as their POSIX equivalents. To enable that, it requires a Windows Vista or newer host OS. [AB: edited commit message] Signed-off-by: Andrew Baumann --- The major implication of this patch is that it drops support for pre-Vista versions of Windows. However, those OSes are past their end of life, and other OSS projects have dropped support. e.g.; the last Cygwin release supporting XP was in Jun 2016. It doesn't seem like a good tradeoff to invest effort in fixing broken code needed to support them, so hopefully this isn't too controversial. include/qemu/thread-win32.h | 7 +-- util/qemu-thread-win32.c | 136 +++++-----------------------------------= ---- 2 files changed, 17 insertions(+), 126 deletions(-) diff --git a/include/qemu/thread-win32.h b/include/qemu/thread-win32.h index 5fb6541..4c4a261 100644 --- a/include/qemu/thread-win32.h +++ b/include/qemu/thread-win32.h @@ -4,8 +4,7 @@ #include =20 struct QemuMutex { - CRITICAL_SECTION lock; - LONG owner; + SRWLOCK lock; }; =20 typedef struct QemuRecMutex QemuRecMutex; @@ -19,9 +18,7 @@ int qemu_rec_mutex_trylock(QemuRecMutex *mutex); void qemu_rec_mutex_unlock(QemuRecMutex *mutex); =20 struct QemuCond { - LONG waiters, target; - HANDLE sema; - HANDLE continue_event; + CONDITION_VARIABLE var; }; =20 struct QemuSemaphore { diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c index 29c3e4d..59befd5 100644 --- a/util/qemu-thread-win32.c +++ b/util/qemu-thread-win32.c @@ -10,6 +10,11 @@ * See the COPYING file in the top-level directory. * */ + +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x0600 +#endif + #include "qemu/osdep.h" #include "qemu-common.h" #include "qemu/thread.h" @@ -39,44 +44,30 @@ static void error_exit(int err, const char *msg) =20 void qemu_mutex_init(QemuMutex *mutex) { - mutex->owner =3D 0; - InitializeCriticalSection(&mutex->lock); + InitializeSRWLock(&mutex->lock); } =20 void qemu_mutex_destroy(QemuMutex *mutex) { - assert(mutex->owner =3D=3D 0); - DeleteCriticalSection(&mutex->lock); + InitializeSRWLock(&mutex->lock); } =20 void qemu_mutex_lock(QemuMutex *mutex) { - EnterCriticalSection(&mutex->lock); - - /* Win32 CRITICAL_SECTIONs are recursive. Assert that we're not - * using them as such. - */ - assert(mutex->owner =3D=3D 0); - mutex->owner =3D GetCurrentThreadId(); + AcquireSRWLockExclusive(&mutex->lock); } =20 int qemu_mutex_trylock(QemuMutex *mutex) { int owned; =20 - owned =3D TryEnterCriticalSection(&mutex->lock); - if (owned) { - assert(mutex->owner =3D=3D 0); - mutex->owner =3D GetCurrentThreadId(); - } + owned =3D TryAcquireSRWLockExclusive(&mutex->lock); return !owned; } =20 void qemu_mutex_unlock(QemuMutex *mutex) { - assert(mutex->owner =3D=3D GetCurrentThreadId()); - mutex->owner =3D 0; - LeaveCriticalSection(&mutex->lock); + ReleaseSRWLockExclusive(&mutex->lock); } =20 void qemu_rec_mutex_init(QemuRecMutex *mutex) @@ -107,124 +98,27 @@ void qemu_rec_mutex_unlock(QemuRecMutex *mutex) void qemu_cond_init(QemuCond *cond) { memset(cond, 0, sizeof(*cond)); - - cond->sema =3D CreateSemaphore(NULL, 0, LONG_MAX, NULL); - if (!cond->sema) { - error_exit(GetLastError(), __func__); - } - cond->continue_event =3D CreateEvent(NULL, /* security */ - FALSE, /* auto-reset */ - FALSE, /* not signaled */ - NULL); /* name */ - if (!cond->continue_event) { - error_exit(GetLastError(), __func__); - } + InitializeConditionVariable(&cond->var); } =20 void qemu_cond_destroy(QemuCond *cond) { - BOOL result; - result =3D CloseHandle(cond->continue_event); - if (!result) { - error_exit(GetLastError(), __func__); - } - cond->continue_event =3D 0; - result =3D CloseHandle(cond->sema); - if (!result) { - error_exit(GetLastError(), __func__); - } - cond->sema =3D 0; + InitializeConditionVariable(&cond->var); } =20 void qemu_cond_signal(QemuCond *cond) { - DWORD result; - - /* - * Signal only when there are waiters. cond->waiters is - * incremented by pthread_cond_wait under the external lock, - * so we are safe about that. - */ - if (cond->waiters =3D=3D 0) { - return; - } - - /* - * Waiting threads decrement it outside the external lock, but - * only if another thread is executing pthread_cond_broadcast and - * has the mutex. So, it also cannot be decremented concurrently - * with this particular access. - */ - cond->target =3D cond->waiters - 1; - result =3D SignalObjectAndWait(cond->sema, cond->continue_event, - INFINITE, FALSE); - if (result =3D=3D WAIT_ABANDONED || result =3D=3D WAIT_FAILED) { - error_exit(GetLastError(), __func__); - } + WakeConditionVariable(&cond->var); } =20 void qemu_cond_broadcast(QemuCond *cond) { - BOOLEAN result; - /* - * As in pthread_cond_signal, access to cond->waiters and - * cond->target is locked via the external mutex. - */ - if (cond->waiters =3D=3D 0) { - return; - } - - cond->target =3D 0; - result =3D ReleaseSemaphore(cond->sema, cond->waiters, NULL); - if (!result) { - error_exit(GetLastError(), __func__); - } - - /* - * At this point all waiters continue. Each one takes its - * slice of the semaphore. Now it's our turn to wait: Since - * the external mutex is held, no thread can leave cond_wait, - * yet. For this reason, we can be sure that no thread gets - * a chance to eat *more* than one slice. OTOH, it means - * that the last waiter must send us a wake-up. - */ - WaitForSingleObject(cond->continue_event, INFINITE); + WakeAllConditionVariable(&cond->var); } =20 void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex) { - /* - * This access is protected under the mutex. - */ - cond->waiters++; - - /* - * Unlock external mutex and wait for signal. - * NOTE: we've held mutex locked long enough to increment - * waiters count above, so there's no problem with - * leaving mutex unlocked before we wait on semaphore. - */ - qemu_mutex_unlock(mutex); - WaitForSingleObject(cond->sema, INFINITE); - - /* Now waiters must rendez-vous with the signaling thread and - * let it continue. For cond_broadcast this has heavy contention - * and triggers thundering herd. So goes life. - * - * Decrease waiters count. The mutex is not taken, so we have - * to do this atomically. - * - * All waiters contend for the mutex at the end of this function - * until the signaling thread relinquishes it. To ensure - * each waiter consumes exactly one slice of the semaphore, - * the signaling thread stops until it is told by the last - * waiter that it can go on. - */ - if (InterlockedDecrement(&cond->waiters) =3D=3D cond->target) { - SetEvent(cond->continue_event); - } - - qemu_mutex_lock(mutex); + SleepConditionVariableSRW(&cond->var, &mutex->lock, INFINITE, 0); } =20 void qemu_sem_init(QemuSemaphore *sem, int init) --=20 2.8.3