From patchwork Mon Jun 26 18:58:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Tokarev X-Patchwork-Id: 696476 Delivered-To: patch@linaro.org Received: by 2002:adf:e885:0:0:0:0:0 with SMTP id d5csp3388904wrm; Mon, 26 Jun 2023 12:08:39 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5B9XKTp3p10KVrVypytaQ2ohvuwhUsTb+b6erqhZfL41lwI3SC1KEXZFbv6C0jwNM0UwSE X-Received: by 2002:a05:620a:8e11:b0:75b:23a1:3ee with SMTP id re17-20020a05620a8e1100b0075b23a103eemr20959089qkn.4.1687806519323; Mon, 26 Jun 2023 12:08:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687806519; cv=none; d=google.com; s=arc-20160816; b=Eck5/DG294dQqcqME84L62ulZ449HL9S9AlN0fIjLZWquTXM6UjUOToQfUOhFDGk7R SX4EXDTx/U7vaVXyuumRmBYwcaQewzP7ioj5scDL8ZuN68PgrroqCoZMm2AgR5uaooQj D2VNMabwICLuc34Wc2PWREuqaq6s0tnQ2HwltqwlOTQ9Fbt1Rb6Lj57FRSpXu6J1fqK6 jy2pVU9ury6wyeWbKWpllHRqsCooVPLIp8mhAspTryvYTw4F4NbPkhGbv5y+jnvytbC3 +xl5rymvbqxypoW3E23FmEKvrTnVCTgLGkhnHA6d2vSNwPWpKRYwhHdaFjK+xu5TY1TE gEeQ== 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=NhRi3p1hDy9Xk0RJq9I6RZyIc/6CLrr0Ob/vBCkatbc=; fh=9Puwj9MOBIDO5gzvzLBZlV9GG/phz4th2oI9KImXtng=; b=sPZE2WImT27UwJMnNTPNXP+UB33PV+a5VQSxN7877LUrMHrmKLL8+bFA2i4StL58bt H9dECW47dfNflHgdyA9SKlYrKtdtA5JMtt4kzu9p2hka0Wu6Be8YRqGKo6jH/XPqnKub suGgewlGgCBX+UXJd8sEW3vMy5TZrsZ6rMsoKB3N2hYjikjd8+Za23YAkpgDgDfJuj8B xgEJCEsUXrnsHG5lHB3WlqvR2WoNkybVJ/hAeBBeih1Bwkn3Jx87OHiFtpHjRqD+SAra XT3Mks8Z9r/LF4ZfAaDJ/g9dF5eXORyBI3DZW9m7osxjR9ZqjLhv89B0R5VM3SQEgjoQ gLzw== 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 dt45-20020a05620a47ad00b0075ec02993d5si1242891qkb.50.2023.06.26.12.08.39 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 26 Jun 2023 12:08:39 -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] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qDrTf-00027j-85; Mon, 26 Jun 2023 15:02:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qDrTH-0000WP-4U; Mon, 26 Jun 2023 15:01:55 -0400 Received: from isrv.corpit.ru ([86.62.121.231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qDrTF-0007rP-97; Mon, 26 Jun 2023 15:01:54 -0400 Received: from tsrv.corpit.ru (tsrv.tls.msk.ru [192.168.177.2]) by isrv.corpit.ru (Postfix) with ESMTP id 48C60EFC3; Mon, 26 Jun 2023 21:59:14 +0300 (MSK) Received: from tls.msk.ru (mjt.wg.tls.msk.ru [192.168.177.130]) by tsrv.corpit.ru (Postfix) with SMTP id B82F4F7FF; Mon, 26 Jun 2023 21:59:12 +0300 (MSK) Received: (nullmailer pid 1575379 invoked by uid 1000); Mon, 26 Jun 2023 18:59:05 -0000 From: Michael Tokarev To: qemu-devel@nongnu.org, qemu-stable@nongnu.org Cc: Peter Maydell , Joel Stanley , Michael Tokarev Subject: [Stable-7.2.4 34/43] hw/timer/nrf51_timer: Don't lose time when timer is queried in tight loop Date: Mon, 26 Jun 2023 21:58:52 +0300 Message-Id: <20230626185902.1575177-34-mjt@tls.msk.ru> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=86.62.121.231; envelope-from=mjt@tls.msk.ru; helo=isrv.corpit.ru X-Spam_score_int: -68 X-Spam_score: -6.9 X-Spam_bar: ------ X-Spam_report: (-6.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_HI=-5, 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Peter Maydell The nrf51_timer has a free-running counter which we implement using the pattern of using two fields (update_counter_ns, counter) to track the last point at which we calculated the counter value, and the counter value at that time. Then we can find the current counter value by converting the difference in wall-clock time between then and now to a tick count that we need to add to the counter value. Unfortunately the nrf51_timer's implementation of this has a bug which means it loses time every time update_counter() is called. After updating s->counter it always sets s->update_counter_ns to 'now', even though the actual point when s->counter hit the new value will be some point in the past (half a tick, say). In the worst case (guest code in a tight loop reading the counter, icount mode) the counter is continually queried less than a tick after it was last read, so s->counter never advances but s->update_counter_ns does, and the guest never makes forward progress. The fix for this is to only advance update_counter_ns to the timestamp of the last tick, not all the way to 'now'. (This is the pattern used in hw/misc/mps2-fpgaio.c's counter.) Cc: qemu-stable@nongnu.org Signed-off-by: Peter Maydell Reviewed-by: Joel Stanley Message-id: 20230606134917.3782215-1-peter.maydell@linaro.org (cherry picked from commit d2f9a79a8cf6ab992e1d0f27ad05b3e582d2b18a) Signed-off-by: Michael Tokarev diff --git a/hw/timer/nrf51_timer.c b/hw/timer/nrf51_timer.c index 42be79c736..50c6772383 100644 --- a/hw/timer/nrf51_timer.c +++ b/hw/timer/nrf51_timer.c @@ -45,7 +45,12 @@ static uint32_t update_counter(NRF51TimerState *s, int64_t now) uint32_t ticks = ns_to_ticks(s, now - s->update_counter_ns); s->counter = (s->counter + ticks) % BIT(bitwidths[s->bitmode]); - s->update_counter_ns = now; + /* + * Only advance the sync time to the timestamp of the last tick, + * not all the way to 'now', so we don't lose time if we do + * multiple resyncs in a single tick. + */ + s->update_counter_ns += ticks_to_ns(s, ticks); return ticks; }