From nobody Sat May 4 01:18:16 2024 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=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1658746955; cv=none; d=zohomail.com; s=zohoarc; b=KqS8Y5QNmOvO/znDY3/DxOItvy3lvoRq7xv0tHuF27H89cy32to+UEuUXeA9ZBTqLC8VKQboG1J6CjDIp8Kyh5hbUUTgxsonXNqwCFedp2b0uR2Qhqz+74g08x+80Wu9QsnxD9bZkm5vdRokcFDeKfHGYmpPH8nVbU0httGwONU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658746955; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=mEQoXa7IYp0GkwsZ9db5gJWuruSBrDHu6zu3PBQmph0=; b=QX7B0SG/ShdNVl5FoLoYnA8TRprL3LqNM4FZc6/t52vv6tM6mbftCoOlipzEMz1rk0vu7yhuFTsnkbDBelv/7iWr+C/CYSTMYvFXYV7qB/OGudvVxtuI0x7XsAQNTFWQQ4dniAk+QI20rGn/BlMq1n0EDYH4hSJemRM3gzW0IUs= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1658746955052704.0821955918617; Mon, 25 Jul 2022 04:02:35 -0700 (PDT) Received: from localhost ([::1]:34434 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oFvr6-0007Ou-NZ for importer@patchew.org; Mon, 25 Jul 2022 07:02:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59940) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oFvpJ-00067x-W4 for qemu-devel@nongnu.org; Mon, 25 Jul 2022 07:00:42 -0400 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]:37510) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oFvpI-0008Vl-A4 for qemu-devel@nongnu.org; Mon, 25 Jul 2022 07:00:41 -0400 Received: by mail-wr1-x42e.google.com with SMTP id u5so15425797wrm.4 for ; Mon, 25 Jul 2022 04:00:39 -0700 (PDT) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id g18-20020a05600c001200b003a31169a7f4sm17309943wmc.12.2022.07.25.04.00.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 04:00:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=mEQoXa7IYp0GkwsZ9db5gJWuruSBrDHu6zu3PBQmph0=; b=zOHorEBOyQTRwZ/lXObi5bIjfsn3BXYFOtRHJIbU/6D4Bs5YafBSEFArISDe4JluhE MIY7DrpYPgyKeFZIsGmcD0AFMmH5MI/a3sbkGppm8AQ+PFLNqEefC+Z8zC10rV6H3XHz W7Q6I/4UPXKoUMlUg26XKJjNl//scTMt9g5rtp9SueCIE7+BJeK3pRYh7V+/BwGRzgly g8t5BVTFTBtqTrEPSeOFj6H4voqqfTXqjobHfAAZMTB2JZIqKtExOHlj45y1jIe/Y8Bk 2chdrNNI5oguU8WKX0rfIYtmvmx5ueleoFYyx7sEy3QEYgKbvvKdwoo8Ir3A6CF1ppXR BAbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=mEQoXa7IYp0GkwsZ9db5gJWuruSBrDHu6zu3PBQmph0=; b=kzJYLb7/ogLAirjC5MXfvwYt1mln7L//Bg1y8liNOpHAgh6MbcRxg6ZWglAGl8e/3Q HbIWfz9JY403ehi2jAU7KTFiB2K6W7bDR57tRq5JHp+br0t+ytobC81m7rPUUiR/cOPN 7grkwTg67kQpFKSkWtzpxdIDTUdk9RM3jWf6P1KMAA7QYjvVrestsI5aIOYcXidxaLfe zmstqSlSajVhytPygkPA88XTlLS8sz3mD+2bpL06AG1M9kLoa6/IKMlW8LdhF/b+c2Fm oTzyUYePytGgZidUWVhnrPXHwwM98nWh5FnHxYtCbjo16Xz9L/yFJgiljaDODISIBbtD Iv8g== X-Gm-Message-State: AJIora8+A6lVB54cVQo9Wj8zOXcw3tqjRQNlhCFI2K+Kb4sYQqsKG2Cb fbu6B8zhx7K4GfO0bIrxF/F5VgUy9Li0hw== X-Google-Smtp-Source: AGRyM1uUJRCMSF7MUq6SZwhn5qY77bIskKXIsGWHby7WC8GRVbrroJeFUwxzZMSxRCkNLCsRaSzRBA== X-Received: by 2002:a5d:5112:0:b0:21e:5755:e9ae with SMTP id s18-20020a5d5112000000b0021e5755e9aemr7686089wrt.638.1658746837818; Mon, 25 Jul 2022 04:00:37 -0700 (PDT) From: Peter Maydell To: qemu-devel@nongnu.org Cc: Laurent Vivier , Jon Alduan Subject: [PATCH] linux-user: Don't assume 0 is not a valid host timer_t value Date: Mon, 25 Jul 2022 12:00:35 +0100 Message-Id: <20220725110035.1273441-1-peter.maydell@linaro.org> X-Mailer: git-send-email 2.25.1 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=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::42e; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x42e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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" X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1658746957277100001 Content-Type: text/plain; charset="utf-8" For handling guest POSIX timers, we currently use an array g_posix_timers[], whose entries are a host timer_t value, or 0 for "this slot is unused". When the guest calls the timer_create syscall we look through the array for a slot containing 0, and use that for the new timer. This scheme assumes that host timer_t values can never be zero. This is unfortunately not a valid assumption -- for some host libc versions, timer_t values are simply indexes starting at 0. When using this kind of host libc, the effect is that the first and second timers end up sharing a slot, and so when the guest tries to operate on the first timer it changes the second timer instead. Rework the timer allocation code, so that: * the 'slot in use' indication uses a separate array from the host timer_t array * we grab the free slot atomically, to avoid races when multiple threads call timer_create simultaneously * releasing an allocated slot is abstracted out into a new free_host_timer_slot() function called in the correct places This fixes: * problems on hosts where timer_t 0 is valid * the FIXME in next_free_host_timer() about locking * bugs in the error paths in timer_create where we forgot to release the slot we grabbed, or forgot to free the host timer Reported-by: Jon Alduan Signed-off-by: Peter Maydell --- linux-user/syscall.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 991b85e6b4d..b75de1bc8d6 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -497,20 +497,25 @@ _syscall4(int, sys_prlimit64, pid_t, pid, int, resour= ce, =20 #if defined(TARGET_NR_timer_create) /* Maximum of 32 active POSIX timers allowed at any one time. */ -static timer_t g_posix_timers[32] =3D { 0, } ; +#define GUEST_TIMER_MAX 32 +static timer_t g_posix_timers[GUEST_TIMER_MAX]; +static int g_posix_timer_allocated[GUEST_TIMER_MAX]; =20 static inline int next_free_host_timer(void) { - int k ; - /* FIXME: Does finding the next free slot require a lock? */ - for (k =3D 0; k < ARRAY_SIZE(g_posix_timers); k++) { - if (g_posix_timers[k] =3D=3D 0) { - g_posix_timers[k] =3D (timer_t) 1; + int k; + for (k =3D 0; k < ARRAY_SIZE(g_posix_timer_allocated); k++) { + if (qatomic_xchg(g_posix_timer_allocated + k, 1) =3D=3D 0) { return k; } } return -1; } + +static inline void free_host_timer_slot(int id) +{ + qatomic_store_release(g_posix_timer_allocated + id, 0); +} #endif =20 static inline int host_to_target_errno(int host_errno) @@ -12832,15 +12837,18 @@ static abi_long do_syscall1(CPUArchState *cpu_env= , int num, abi_long arg1, phost_sevp =3D &host_sevp; ret =3D target_to_host_sigevent(phost_sevp, arg2); if (ret !=3D 0) { + free_host_timer_slot(timer_index); return ret; } } =20 ret =3D get_errno(timer_create(clkid, phost_sevp, phtimer)); if (ret) { - phtimer =3D NULL; + free_host_timer_slot(timer_index); } else { if (put_user(TIMER_MAGIC | timer_index, arg3, target_timer= _t)) { + timer_delete(*phtimer); + free_host_timer_slot(timer_index); return -TARGET_EFAULT; } } @@ -12976,7 +12984,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, = int num, abi_long arg1, } else { timer_t htimer =3D g_posix_timers[timerid]; ret =3D get_errno(timer_delete(htimer)); - g_posix_timers[timerid] =3D 0; + free_host_timer_slot(timerid); } return ret; } --=20 2.25.1