[PATCH v2] linux-user: Fix a memory leak when pthread_create fails

Warner Losh posted 1 patch 2 weeks, 4 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260507-linux-user-bug-v2-1-26ec5da22f6c@bsdimp.com
Maintainers: Laurent Vivier <laurent@vivier.eu>, Helge Deller <deller@gmx.de>, Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
linux-user/syscall.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
[PATCH v2] linux-user: Fix a memory leak when pthread_create fails
Posted by Warner Losh 2 weeks, 4 days ago
Fix one of the TODO items when creating a new thread: release the copied
cpu and free the task state.

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
Free the new task state and drop references to copied cpu structure when
pthread_create failes.
---
Changes in v2:
- Add ifdef for aarch64 so we don't leak stacks
- set errno = ret to fix error propagation.
- Link to v1: https://lore.kernel.org/qemu-devel/20260507-linux-user-bug-v1-1-25831e9bc22e@bsdimp.com
---
 linux-user/syscall.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index d3d9fffb54..c7357bf208 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -7005,7 +7005,6 @@ static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
         cpu->random_seed = qemu_guest_random_seed_thread_part1();
 
         ret = pthread_create(&info.thread, &attr, clone_func, &info);
-        /* TODO: Free new CPU state if thread creation failed.  */
 
         sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
         pthread_attr_destroy(&attr);
@@ -7014,7 +7013,16 @@ static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
             pthread_cond_wait(&info.cond, &info.mutex);
             ret = info.tid;
         } else {
+            errno = ret;
             ret = -1;
+            object_unparent(OBJECT(new_cpu));
+            object_unref(OBJECT(new_cpu));
+#ifdef TARGET_AARCH64
+            if (ts->gcs_base) {
+                target_munmap(ts->gcs_base, ts->gcs_size);
+            }
+#endif
+            g_free(ts);
         }
         pthread_mutex_unlock(&info.mutex);
         pthread_cond_destroy(&info.cond);

---
base-commit: ac0cc20ad2fe0b8df2e5d9458e90a095ac711ab1
change-id: 20260507-linux-user-bug-6a5e4524d2db

Best regards,
-- 
Warner Losh <imp@bsdimp.com>
Re: [PATCH v2] linux-user: Fix a memory leak when pthread_create fails
Posted by Helge Deller 6 days, 5 hours ago
On 5/7/26 21:24, Warner Losh wrote:
> Fix one of the TODO items when creating a new thread: release the copied
> cpu and free the task state.
> 
> Signed-off-by: Warner Losh <imp@bsdimp.com>
> ---
> Free the new task state and drop references to copied cpu structure when
> pthread_create failes.
> ---
> Changes in v2:
> - Add ifdef for aarch64 so we don't leak stacks
> - set errno = ret to fix error propagation.
> - Link to v1: https://lore.kernel.org/qemu-devel/20260507-linux-user-bug-v1-1-25831e9bc22e@bsdimp.com
> ---
>   linux-user/syscall.c | 10 +++++++++-
>   1 file changed, 9 insertions(+), 1 deletion(-)

Reviewed-by: Helge Deller <deller@gmx.de>

applied to linux-user-for-next git tree.

Thanks!
Helge