From patchwork Wed Sep 28 08:15:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Vivier X-Patchwork-Id: 610086 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp169329pvb; Wed, 28 Sep 2022 03:43:03 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5nvoUOnv38IOgojIT8ccVp3ZHa8bu1vHuDBogilQpHLuWWqa6q2rFSeNuqTtoFVrJwBtxj X-Received: by 2002:a05:620a:79b:b0:6ce:737:41fb with SMTP id 27-20020a05620a079b00b006ce073741fbmr20809851qka.783.1664361783005; Wed, 28 Sep 2022 03:43:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664361783; cv=none; d=google.com; s=arc-20160816; b=Zfaaf9w0NLW09PDn1UssdiIPcfoUxwV+xbPiy8ZJ82NV1FtJIXz2fV3fRW6EC5KpdM bDaEKK5EOsoGg+EiVdWSEw9CEEMCTU/suGG3aQr0er7zW8Ic+RM2O9g3lfOznpDX+jNa 0NSPN8rtrBsVxhDEi8A5j1LaIFhgYsYkOO5LppuQD9tgLA0h0lhQULGyYlm7vso6O9Im cOeXwrl0YXU/4nzimiRDEVvt67cnw4g5rZ2jNlaOCcTrZMSleznXa9hVxGwOEwg4KoG1 6z3+g/sCQUHcs79V0Aw4y+1ivOKnmiXyLHcLdtha42uOqPnb816nivK5M8z5NRwT6tnV QkFw== 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=orgTu+YfSEzrLKw0QD1lxbJQzU2xCWZj5ATt3jEAfgkqUFXBXbGNm0bD9wyW3Hljoq QzjPrsFlh3b1tbYWD8Qty7pydZnj6eJ4NEAt8DNT9Su+pEV52oLlW76363pwFYCStVJv NgGWmhxavS0YggFPMCIPevFZ9uOho7fl4akYVjt/FPU6E5WFo6HKIynlbLdc3BBQSFRL jFGsOaI2g8y98TpGVoLhkZV2k9HJ556SELF3MTp6ZbKXSyXjDPtpxinNfeD4d/umsp5R aXhvF2Svz9S1nYwF0PAY/ZS5/0MMMfSyU00e7Aht+aLF8D9glrrjlK/z4d/QEAQHSY6c zbAg== 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 oo24-20020a056214451800b004ad67db9446si2179809qvb.410.2022.09.28.03.43.02 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 28 Sep 2022 03:43:03 -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]:43578 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1odUWs-0003J0-Fp for patch@linaro.org; Wed, 28 Sep 2022 06:43:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34558) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1odSEL-0008FB-LE for qemu-devel@nongnu.org; Wed, 28 Sep 2022 04:15:52 -0400 Received: from mout.kundenserver.de ([212.227.17.10]:41871) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1odSEB-0008DW-9H for qemu-devel@nongnu.org; Wed, 28 Sep 2022 04:15:40 -0400 Received: from quad ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MysBI-1pQcdi1kPK-00vxKq; Wed, 28 Sep 2022 10:15:32 +0200 From: Laurent Vivier To: qemu-devel@nongnu.org Cc: Peter Maydell , Jon Alduan , Laurent Vivier Subject: [PULL 26/38] linux-user: Don't assume 0 is not a valid host timer_t value Date: Wed, 28 Sep 2022 10:15:05 +0200 Message-Id: <20220928081517.734954-27-laurent@vivier.eu> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220928081517.734954-1-laurent@vivier.eu> References: <20220928081517.734954-1-laurent@vivier.eu> MIME-Version: 1.0 X-Provags-ID: V03:K1:Tu/uklV94njT6bMi4+dmALEcFTu6CJq4x+cZNxI9FMT0RZ4r46k rPq2QIgpiy/CprG3R4Dc3e7s4a9tc4k01LyI1WWZ+WPIAIJunFwgJmizwG4JI1de8DJ4m0R 7CJ0pgt9FZi0gYj+c3ZGglgye5ovz2++1SGm/xnEINsJ43HQgtR3BvaXTFMIXm8fKSzcJh8 gp4NiOMwLW8gvqT/+dcBg== X-UI-Out-Filterresults: notjunk:1;V03:K0:B7Ln73FCUJg=:UquRtFbzNcH084GCd/yZU9 ONL5HeM86e1zfUzQfWq16QrYhoxrp5CyCyOR21rINTY5PH/3Z3yRQp45CJU6tmfcdqu7zEqLt PVtf2ufvRZ4dD8Y043lwMBFYFTgs2gRSWlnFSW9yjRQbglWyrxiFsZa+TOAUAGQeGUUZMvQdd TrgMDIFu8z4AeRd6Kfh/EzA7BQr5sJ3zgBqn765kbFVhL4hai4clEhj85GGChqabnxSeB5E6U 4HAACsGLzIyaizx5pL7bhc4pVqayRQKfnodzBbEKBuThu1qsYlQ+O7v6cTc+7Ll+0Jgx4mGEr 3nus7L5pzwzEj/153jjsVjXRD4abkr2Tslz3Q45kcRn+7MLTTseUm+eHEUQbu/Vz+GxZkZ1PS kB1ov6G2E/oNif4tnctcFWxlf3dbs1qZa2Ec5wZs2FFKE2SQlDzlLgRbrgpaoYVEH6ieacgYF BZb9HFhLxWLBTPfJUpYrq0EmcQzZkVqKvD1m1ds3IiifEsxLpTTgkshLNX/IijIjAA6vxs2cV 0Ps7GoJH4Mv9hsSHwEDUYMVlHc0u27nvfUfKWRR8ExyUuVno7PpxMqZDRAW4OjiXpT6E3EIz9 y3eiFtokEroB/CzS/eIdtRtM13Hh4Sp379Ran2uvaKTXfXh26+qNbtqTJ+sPPMoAEzFIiLPd0 +MpzMgyM9b5PEbiR4HCymDNzmqjVYBhmFJXspoV7yJQbx79K9US88PF4Z1k5KRInp5pARPwmN K/1TPcBOLqgdL1duOP7Pa8gL+ybiRDC0ueTxe+TLbG984ZeH/PRgArUPBQ8b+F7MtXAWw/ZSQ fpJRgyy Received-SPF: none client-ip=212.227.17.10; 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_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; }