From patchwork Wed Sep 28 20:27:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Vivier X-Patchwork-Id: 610094 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp501416pvb; Wed, 28 Sep 2022 13:54:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5xWxLPWoVKwXmXT5lSuAZ77JQlxfhfSyPpGYgBqjatBQp6iBkbFqYa0WN7JgBbp6j0K5aL X-Received: by 2002:a05:6214:2306:b0:473:f77a:85a7 with SMTP id gc6-20020a056214230600b00473f77a85a7mr26891092qvb.106.1664398454042; Wed, 28 Sep 2022 13:54:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664398454; cv=none; d=google.com; s=arc-20160816; b=0weeuvKTWB49GFBj6llLzOFzHVbsKtR15URw2GN/DuXxfmbmbPld3AQPvRiwxrFyYW P1ttrFD5ySXlO2O+pfVT7lcyR2P0gCGnmXwUApkItErgVJgrmJaXm7+j+7oSQRnHPrhw JFV6V9UC0Hbt+iHNjGEKWSgf4NNz7MOXu82ch1H4bSbyixdW1/8zp6zkDDpFRA+JJOxW RLb0bFwrFmSuRbtb+BEQcmHpLebMUZ2YevHGI+E1SftuKFFD0d+JoUM+Y57VacIvyWO5 Oxt9LMVBkYRdGyOwxKr1ulKmARPYaFLLDl555WfbUxSd5TaVg0AIvTJdN1DNPVJzXC1H xjRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=aVH6XgoRnj/lxNtTeBLZH7eFffPHtTxEsiDca0jShPg=; b=JQr8hLwpMWUlm0C4PDpuhzAXH7qdHzDkJNSMgMP+t5DIkqXqQ1ZcuYCzLxfxYHRUQ6 WJgsZjjhvIaBZpYdkJHsA6sIF4dk8yt4jqLiJ/aXGO7+QYC58AyCYh8mxGjGD+4Fs8C0 5znHAN5ja9JCKhxiOdC0EBP2AWi672tsyYXSQefdJtQFAz17+n4dUpN4w+8JHeXB/NKv mQ40QITexX0fewnAhVb2Czc+GSEvHnobm21mbm3wNKgXPV2woI0Zuo5OumAGLb4R+6ni HEYk3d8isQN9j7pdENuL+Sawer05x2NfIKvoYo8Sp+sebQc0QZ2VngY9YnPhLLhElp7u wZIw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org" Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id r10-20020a056214212a00b004ad7b54e002si3823000qvc.102.2022.09.28.13.54.13 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 28 Sep 2022 13:54:14 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org" Received: from localhost ([::1]:40446 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ode4L-0005hi-IY for patch@linaro.org; Wed, 28 Sep 2022 16:54:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33368) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oddf1-0000Qo-BA for qemu-devel@nongnu.org; Wed, 28 Sep 2022 16:28:03 -0400 Received: from mout.kundenserver.de ([212.227.126.131]:60277) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oddez-0006WM-Ip for qemu-devel@nongnu.org; Wed, 28 Sep 2022 16:28:03 -0400 Received: from quad ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue011 [212.227.15.167]) with ESMTPSA (Nemesis) id 1M7am5-1oacSJ2MzF-0085OZ; Wed, 28 Sep 2022 22:27:58 +0200 From: Laurent Vivier To: qemu-devel@nongnu.org Cc: Peter Maydell , Jon Alduan , Laurent Vivier Subject: [PULL 26/37] linux-user: Don't assume 0 is not a valid host timer_t value Date: Wed, 28 Sep 2022 22:27:26 +0200 Message-Id: <20220928202737.793171-27-laurent@vivier.eu> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220928202737.793171-1-laurent@vivier.eu> References: <20220928202737.793171-1-laurent@vivier.eu> MIME-Version: 1.0 X-Provags-ID: V03:K1:d0MrNzFRlhOdVfypNKkFitcF4YU1Y03IDUrK9VzxoFo39+aDiGu h35Mmo6dx8QqbtVPyGbkbM76Q75e6bD9xi7GmtO9YCSsKVlIVnmKGuCzrtKW7IU11lM9eGP RNIe+FyeZGXmu1qOMgHMjmlgcLNDlxDfPIvWVlVsdEFSRQAN+4h3JS7Yair452Vei+mnJAh pi17WHv70hKoFc7IBHX2A== X-UI-Out-Filterresults: notjunk:1;V03:K0:YAvLgBDKtRQ=:trvDAzaxeCI0LIO3SJy/1Z 5ch1fNVpADcikPEZT6kUkcTPXyY8j2u09QG4Z7viC+p9Jgm119xEzKj2fG8KY5Ld5SbIaotk0 tRdZOPfZRC01IAOu425BQzWnO+clO1eHFdgGpsbkML8ZR59iQSdXWwg40QQEbeC91OsMwgM0g c31MNhJJENIdo42OnHnzXYud1arrJJZTe51j4HLycWveIAZOp62AY3B8xzvjHuEjpfLJY1Ma4 sy33XYszirkbPPW7DN7Dt3zCn2/XFtun4yVqOG8qk9l2BegpS/YYzB/3Vn+hubCKT12/ull8x qYK4Sdx7fK4YhmeFW1k2f9rU1NP2sVkaen+iL5AcsEL+9v3D0A3WpSOGHc6z+jDnddxEG+giK 1uSZlzgXvqEqtBZeoXm+kgzHEcJ6j7/AHeRLDKSlCQ6NiDW8m9jxvGFTlPuZ3zMfT0LoYORsD K9p5JvlsmsNZgjZTic9ceqPkKvfF4610+WPjWJpBvI9mh6b3MRpXJ/F9fYcCGFvApGy6QbJuD CtHjMsXXxc0cKw1U76eWp38mgnNnWpCtv56jiDDckYkdeOkPYZvavI4O8qtlMt5gj3mlENxAQ +g+XgJnaf86OmEuwCZyqab7pQrTBQYF3XAQ30OOQIrD5atqoA2+HexKmKktALpMOc1+n+Gy0Q xD85Gj3RL7bNnkLQ39rnyHqbOmBEDhLybjTmDlZGfFRnZXcO5GxR81nOSr/9/RknTOGkndpWj e/6gWJ5GcF80nSUwUtZtTRK4kBjqRuY8G/TJiY91kojLl0i5rWba5/x5TAhoQxGVYwdI59SDK pyRMZMH Received-SPF: none client-ip=212.227.126.131; envelope-from=laurent@vivier.eu; helo=mout.kundenserver.de 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, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=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+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Peter Maydell 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 Message-Id: <20220725110035.1273441-1-peter.maydell@linaro.org> Signed-off-by: Laurent Vivier --- 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 54b29f3b406a..e0e0f058121f 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -525,20 +525,25 @@ _syscall4(int, sys_prlimit64, pid_t, pid, int, resource, #if defined(TARGET_NR_timer_create) /* Maximum of 32 active POSIX timers allowed at any one time. */ -static timer_t g_posix_timers[32] = { 0, } ; +#define GUEST_TIMER_MAX 32 +static timer_t g_posix_timers[GUEST_TIMER_MAX]; +static int g_posix_timer_allocated[GUEST_TIMER_MAX]; static inline int next_free_host_timer(void) { - int k ; - /* FIXME: Does finding the next free slot require a lock? */ - for (k = 0; k < ARRAY_SIZE(g_posix_timers); k++) { - if (g_posix_timers[k] == 0) { - g_posix_timers[k] = (timer_t) 1; + int k; + for (k = 0; k < ARRAY_SIZE(g_posix_timer_allocated); k++) { + if (qatomic_xchg(g_posix_timer_allocated + k, 1) == 0) { return k; } } return -1; } + +static inline void free_host_timer_slot(int id) +{ + qatomic_store_release(g_posix_timer_allocated + id, 0); +} #endif static inline int host_to_target_errno(int host_errno) @@ -12896,15 +12901,18 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, phost_sevp = &host_sevp; ret = target_to_host_sigevent(phost_sevp, arg2); if (ret != 0) { + free_host_timer_slot(timer_index); return ret; } } ret = get_errno(timer_create(clkid, phost_sevp, phtimer)); if (ret) { - phtimer = 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; } } @@ -13040,7 +13048,7 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, } else { timer_t htimer = g_posix_timers[timerid]; ret = get_errno(timer_delete(htimer)); - g_posix_timers[timerid] = 0; + free_host_timer_slot(timerid); } return ret; }