From nobody Mon Nov 23 04:26:02 2026 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; dmarc=pass(p=quarantine dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1793116580; cv=none; d=zohomail.com; s=zohoarc; b=mdILJILGlazhwHKfU/nmln6FMi0XyOcShEfiex0I6zpqcOSjcelQAGR6apUdNFQ/Wm0PWlfkxoTmYt8WoiLcEdbpq07Rui2L3qaL9cUk8cZvlEqYpXMYP72MY85pKG8mE4LId0MC4+goalqybz/jkOPhZtQfnF6fZXJwwD3rroI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793116580; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=0ZPfkwD3Efjl9oAFfI7L82kv1ZC8oIEpcm83zienuYA=; b=EVN7Kgf5XA43UZ2ctmTRWJBH8O8UxfMxzKterLauv80IAxrdT2MbT3G+HRAkLUDBEPRqkQIfaKVBA10jLxH8XxWVyf8S6cuphAh5Bcww7kEm4zC0YYMbk/Bgi9Ue7w4WkmMjH+vpWQFK7y3zAbD3igDNfn7neJ5/r8uRoZf7bdg= 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793116580186311.2936645151028; Tue, 27 Oct 2026 08:56:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xLjWr-00088h-2y; Tue, 27 Oct 2026 11:56:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xLjWY-00086B-MY for qemu-devel@nongnu.org; Tue, 27 Oct 2026 11:55:45 -0400 Received: from sea.source.kernel.org ([2600:3c0a:e001:78e:0:1991:8:25]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xLjWW-0005aA-Np for qemu-devel@nongnu.org; Tue, 27 Oct 2026 11:55:42 -0400 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 02B6A42AC6; Tue, 27 Oct 2026 15:55:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1CA5F1F01825; Tue, 27 Oct 2026 15:55:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793116531; bh=0ZPfkwD3Efjl9oAFfI7L82kv1ZC8oIEpcm83zienuYA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=grfQYFYk3t9wbMu760vTvCMOfSwrFF+V5RnqrBYw/yX6c2Ro1LpGCZOR45kBoCMzW TTU7Zl67wYUH8FL7kxR1d9+fHauL7h0kax6qUGrLZb8wtl5xEc9N0zhyFvQUSscHDc tv7qPnNNzGiix8EA1A2wjXR2FPo9HdldNv/DcOKSek79yeprhZmbtJOCx3YdX2zcGy Cwcb01r2F7nZg5Cq3FHLxKtcpEUD3Y/6bKFIaUuC3TQBHPOVCvqylB5uohLQZJHCiV fb9PWzPZXeBJ91sb5sxx6rTPU/wPuGHdReN7fxR1RoJHiNt8GRs5OeUt6QRoe4VT4h JZKw/Obwz6Bgw== From: Helge Deller To: qemu-devel@nongnu.org Cc: Helge Deller , Peter Maydell Subject: [PULL 1/2] linux-user/loongarch64: In extframe_alloc, align before subtracting sctx_info size Date: Tue, 27 Oct 2026 16:55:27 +0100 Message-ID: <20261027155528.5040-2-deller@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261027155528.5040-1-deller@kernel.org> References: <20261027155528.5040-1-deller@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=2600:3c0a:e001:78e:0:1991:8:25; envelope-from=deller@kernel.org; helo=sea.source.kernel.org X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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: qemu development 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 @kernel.org) X-ZM-MESSAGEID: 1793116580979158500 Content-Type: text/plain; charset="utf-8" From: Peter Maydell In extframe_alloc() we allocate space on the guest stack for a context struct, which consists of a target_sctx_info plus a more specific struct such as target_lasx_context. The specific struct may have a higher alignment requirement than the basic target_sctx_info. The Linux kernel code for this is in arch/loongarch/kernel/signal.c in extframe_alloc(): unsigned long new_base =3D base - size; new_base =3D round_down(new_base, (align < 16 ? 16 : align)); new_base -=3D sizeof(struct sctx_info); and it first adjusts the base (SP) address down by the size of the specific struct, then rounds that down to provide the required alignment, and finally subtracts the size of the sctx_info. QEMU's version does this in the wrong order, subtracting both the specific struct size and the sctx_info size before rounding down for alignment. This means that for the target_lasx_context which needs 32-alignment, we instead give the start of sctx_info 32-alignment, which (since that is 16 bytes long) means that the target_lasx_context is only 16-aligned. This manifests as a failure in the TCG tests such as loongarch64-linux-user-signals.test if you build with the clang sanitizers enabled: ../../linux-user/loongarch64/signal.c:182:13: runtime error: member access = within misaligned address 0x7dc793950b90 for type 'struct target_lasx_conte= xt', which requires 32 byte alignment 0x7dc793950b90: note: pointer points here 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00= 00 00 00 00 00 00 00 00 ^ #0 0x57216113d881 in setup_sigframe /home/pm215/qemu/build/clang/../../= linux-user/loongarch64/signal.c:182:13 #1 0x57216113d881 in setup_rt_frame /home/pm215/qemu/build/clang/../../= linux-user/loongarch64/signal.c:389:5 #2 0x5721612a5041 in handle_pending_signal /home/pm215/qemu/build/clang= /../../linux-user/signal.c:1349:9 #3 0x5721612a46ef in process_pending_signals /home/pm215/qemu/build/cla= ng/../../linux-user/signal.c:1401:17 #4 0x57216113baa3 in cpu_loop /home/pm215/qemu/build/clang/../../linux-= user/loongarch64/cpu_loop.c:121:9 Correct setup_extcontext() to match the order the kernel does it. Cc: qemu-stable@nongnu.org Fixes: 9d5cd6587a70d613 ("linux-user: Add LoongArch signal support") Signed-off-by: Peter Maydell Reviewed-by: Bibo Mao Signed-off-by: Helge Deller --- linux-user/loongarch64/signal.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/linux-user/loongarch64/signal.c b/linux-user/loongarch64/signa= l.c index eff75bcdc8..04015e06fd 100644 --- a/linux-user/loongarch64/signal.c +++ b/linux-user/loongarch64/signal.c @@ -111,9 +111,10 @@ static abi_ptr extframe_alloc(struct extctx_layout *ex= tctx, { abi_ptr sp =3D orig_sp; =20 - sp -=3D sizeof(struct target_sctx_info) + size; + sp -=3D size; align =3D MAX(align, CONTEXT_INFO_ALIGN); sp =3D ROUND_DOWN(sp, align); + sp -=3D sizeof(struct target_sctx_info); sctx->gaddr =3D sp; =20 size =3D orig_sp - sp; --=20 2.54.0 From nobody Mon Nov 23 04:26:02 2026 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; dmarc=pass(p=quarantine dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1793116583; cv=none; d=zohomail.com; s=zohoarc; b=EfOjdUCi2AOOFBnNeXgVsp2jGXi6wBP8NkGcqVrzTAtPapQJzNEribqN+2On18qscxFdfKHziq/S45e++i3KdvxSldvqOHVVGWRLu0B0BXjodaN/c9Q36gkql4OzZgjBsEnDOZQWUdcdRTqDjzdyfeGy+wH5jrtrGAKreACYjOQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793116583; h=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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=yRw4GZUuOUp8HRjWU6tIX56Qk4m0IW3baJENpWMjjKA=; b=YDopj5/FyokJ5AUnJ3ivbjsTKE5TcrnMRVOvDMRTmQ/FIrvSIfEi6tAE1EXGdKdsuCdnNsFWB79xSXf2rAn+ADatdph/XoGxrRAXBBXamnrk/QB5VOZVY+cF7DlN/nHvX9oSX/GwXA/sY/x0rV6jxwn5aH3AUuu4SW8Lch+34n4= 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793116583856716.2559229090376; Tue, 27 Oct 2026 08:56:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xLjWj-00087e-O3; Tue, 27 Oct 2026 11:55:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xLjWa-00086L-J9 for qemu-devel@nongnu.org; Tue, 27 Oct 2026 11:55:45 -0400 Received: from tor.source.kernel.org ([2600:3c04:e001:324:0:1991:8:25]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xLjWX-0005aI-LT for qemu-devel@nongnu.org; Tue, 27 Oct 2026 11:55:43 -0400 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 5F32C60219; Tue, 27 Oct 2026 15:55:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4C3B11F031E3; Tue, 27 Oct 2026 15:55:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793116533; bh=yRw4GZUuOUp8HRjWU6tIX56Qk4m0IW3baJENpWMjjKA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=H77bIdohXCfwoD4yTndJb/H+/2KWwW8B12y9Var0tA8VET1Vvc82bEV1qmWM8Aodw 1cZx4dhXpOkmLTuSFd4QguufAxzjc+X6oeW3btDGZj05F9nMHyc2snMq2jxKjHATVE /5zisu5Hw66y2KL6eF7mB6HLxHgw52rtEYDXsybTTYAd+l6k3znc3eTxmK5T64WUM2 GvSZI8KmvHhwJUeF8qIPzCYuo9br66NcGDg1Tzz414bJn1QKS+Upiuo+rV5TiS2Aqc UPvmKIPEDWFdJTrPaDOveE8ZHhu6XMNCAItGq0FXBOr+oGWHOnzii69N7hccOiHnu2 P1seMXBcYKpgQ== From: Helge Deller To: qemu-devel@nongnu.org Cc: Helge Deller , Peter Maydell Subject: [PULL 2/2] linux-user: Use QemuSemaphore to wait for clone child thread Date: Tue, 27 Oct 2026 16:55:28 +0100 Message-ID: <20261027155528.5040-3-deller@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261027155528.5040-1-deller@kernel.org> References: <20261027155528.5040-1-deller@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=2600:3c04:e001:324:0:1991:8:25; envelope-from=deller@kernel.org; helo=tor.source.kernel.org X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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: qemu development 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 @kernel.org) X-ZM-MESSAGEID: 1793116585223158500 Content-Type: text/plain; charset="utf-8" From: Peter Maydell In do_fork() and clone_func() we currently use a pthread_mutex_t and a pthread_cond_t to allow the parent thread to wait until the child thread has started and e.g. set fields like info->tid, before the parent reads those fields and otherwise proceeds. Coverity points out that this use of a pthread condition variable isn't reliable: pthread_cond_wait() may wake up spuriously, so the idiom for "wait on a condition" should be "loop around calling pthread_cond_wait() until a predicate that was updated by the caller of pthread_cond_broadcast() is true". This is quite old code; rather than fixing up its use of raw pthread condition variables, we can use a QemuSemaphore. This gives us the "thread A waits until thread B signals it" semantics we want. (Under the hood it is implemented with a mutex and condition variable.) Cc: qemu-stable@nongnu.org Resolves: CID 1527417 Signed-off-by: Peter Maydell Reviewed-by: Pierrick Bouvier Signed-off-by: Helge Deller --- linux-user/syscall.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 0380691a8b..9d85a193ef 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -6932,8 +6932,7 @@ static abi_long do_map_shadow_stack(CPUArchState *env= , abi_ulong addr, static pthread_mutex_t clone_lock =3D PTHREAD_MUTEX_INITIALIZER; typedef struct { CPUArchState *env; - pthread_mutex_t mutex; - pthread_cond_t cond; + QemuSemaphore sem; pthread_t thread; uint32_t tid; abi_ulong child_tidptr; @@ -6964,9 +6963,7 @@ static void *clone_func(void *arg) /* Enable signals. */ sigprocmask(SIG_SETMASK, &info->sigmask, NULL); /* Signal to the parent that we're ready. */ - pthread_mutex_lock(&info->mutex); - pthread_cond_broadcast(&info->cond); - pthread_mutex_unlock(&info->mutex); + qemu_sem_post(&info->sem); /* Wait until the parent has finished initializing the tls state. */ pthread_mutex_lock(&clone_lock); pthread_mutex_unlock(&clone_lock); @@ -7071,9 +7068,7 @@ static int do_fork(CPUArchState *env, unsigned int fl= ags, abi_ulong newsp, } =20 memset(&info, 0, sizeof(info)); - pthread_mutex_init(&info.mutex, NULL); - pthread_mutex_lock(&info.mutex); - pthread_cond_init(&info.cond, NULL); + qemu_sem_init(&info.sem, 0); info.env =3D new_env; if (flags & CLONE_CHILD_SETTID) { info.child_tidptr =3D child_tidptr; @@ -7097,7 +7092,7 @@ static int do_fork(CPUArchState *env, unsigned int fl= ags, abi_ulong newsp, pthread_attr_destroy(&attr); if (ret =3D=3D 0) { /* Wait for the child to initialize. */ - pthread_cond_wait(&info.cond, &info.mutex); + qemu_sem_wait(&info.sem); ret =3D info.tid; } else { errno =3D ret; @@ -7111,9 +7106,7 @@ static int do_fork(CPUArchState *env, unsigned int fl= ags, abi_ulong newsp, #endif g_free(ts); } - pthread_mutex_unlock(&info.mutex); - pthread_cond_destroy(&info.cond); - pthread_mutex_destroy(&info.mutex); + qemu_sem_destroy(&info.sem); pthread_mutex_unlock(&clone_lock); } else { /* if no CLONE_VM, we consider it is a fork */ --=20 2.54.0