From nobody Wed Apr 02 15:14:50 2025 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4ZST1k5jzSz5sQMq; Wed, 02 Apr 2025 15:14:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4ZST1k34qbz3gMm; Wed, 02 Apr 2025 15:14:50 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1743606890; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=4lpPZQiTWomBBpLtGfFZ8hCmag8wRvoMukytbRLL3J0=; b=sPpMBdYzcVq7Z2Lu7m7WJnqgeOznZxQv+n+lh7W25++jdKgtJKmlikq/88clpjX5Onl8gd z9ySG/iHTdMSZcYUjWnzFErGzZJi3diov7WCDEDv4CEpkm/4SwedGf97BHbCFXXHrIhl+Z gHmRSFBueMYG7XdVog14+YeSnTq+hT/Dc13G+SUJIYaS1HC1IycDh8B7mvdm+u4rKPKbKD 2Ly4hTxS/6OpNGlqPan9fJkSBLIaEXqKnMlV3BdFNhKm+EQ6o6imKrQuqvNpkU3ia4703+ LdA4y76btaMCHYyFg1eJJtqEuHeORaXrXywQRsTeG9HtcuG4Xyct1KVp3f1jGQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1743606890; a=rsa-sha256; cv=none; b=G/2nv0dmwWmpOeXJxo+251T1onsEJAkuyJGRlqqtXVQNEtFl/KfcAkVOoPAG6BMYKVhtFx rcI3/idvLwEzlv/St1w7OyoFs/UII3ePBvcIwAX8I3XF8sWlAxaAt/hdxjIYPY9wCgdLBy CSUjnjW6mQ9fEDhrIc/rZMnrDgGtZ787yAzl0GLQXMtW5TuI0oIXU7B4Bw8ItsRsmUbtti hMqAy2TzN0lT9nxyaoxFplTabMXEqz+qQ0FmdodCBuo7tyiF022OgFJWjmXq3MbbgNjfnf BcEhIjK8vOa62xFFGPl7jicmmWtAjzIcJ4CfE5ZjaSbnM9ZCURSXL7K7SatjMQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1743606890; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=4lpPZQiTWomBBpLtGfFZ8hCmag8wRvoMukytbRLL3J0=; b=PVBGiDZxikASlBL+aNVm2dHe5zGdCrvKhceZNYRX+nFYbfU1Gu+x+4wflgeM5Cv4Fq+m0/ OzNtPNWTJAgZKbGvNi7k3xtNThhUnSZTnadLCfu0f08uVWhcIkN+E1qdD4/apUNEn3ZYkf 4UL4+swKN1EwB5Kliu4MWgXOYIbVUZiwWEoKZSu2blCza86BuzrUok9BnK02pyqmPObwpK haPFFD8kFK6Z41u7wPnOAndK+AtkKS3eidN+E1mZiEhAKYDPLD8H7ehRJN2/gpDOmjJhBn rddwjwuQN/SiMWvKfGHo2VllOizXQ1N2K+1BPorUt28Oyndd3e8HnsW1f3iugA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4ZST1k2TDJz61N; Wed, 02 Apr 2025 15:14:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 532FEoTk061409; Wed, 2 Apr 2025 15:14:50 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 532FEoLv061406; Wed, 2 Apr 2025 15:14:50 GMT (envelope-from git) Date: Wed, 2 Apr 2025 15:14:50 GMT Message-Id: <202504021514.532FEoLv061406@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 294cda7e4e33 - stable/14 - dtrace/arm64: Fix dtrace_gethrtime() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 294cda7e4e33cc3f7adcd0300e3868db62cd46f8 Auto-Submitted: auto-generated The branch stable/14 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=294cda7e4e33cc3f7adcd0300e3868db62cd46f8 commit 294cda7e4e33cc3f7adcd0300e3868db62cd46f8 Author: Mark Johnston AuthorDate: 2025-03-10 03:01:13 +0000 Commit: Mark Johnston CommitDate: 2025-04-02 15:14:37 +0000 dtrace/arm64: Fix dtrace_gethrtime() This routine returns a monotonic count of the number of nanoseconds elapsed since the previous call. On arm64 it uses the generic system timer. The implementation multiplies the counter value by 10**9 then divides by the counter frequency, but this multiplication can overflow. This can result in trace records with non-monotonic timestamps, which breaks libdtrace's temporal ordering algorithm. An easy fix is to reverse the order of operations, since the counter frequency will in general be smaller than 10**9. (In fact, it's mandated to be 1Ghz in ARMv9, which makes life simple.) However, this can give a fair bit of error. Adopt the calculation used on amd64, with tweaks to handle frequencies as low as 1MHz: the ARM generic timer documentation suggests that ARMv8 timers are typically in the 1MHz-50MHz range, which is true on arm64 systems that I have access to. MFC after: 2 weeks Sponsored by: Innovate UK Differential Revision: https://reviews.freebsd.org/D49244 (cherry picked from commit 36ae5ce2f2fda35763c2655a19bf1b0ee22fdf3c) --- sys/cddl/dev/dtrace/aarch64/dtrace_subr.c | 33 +++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/sys/cddl/dev/dtrace/aarch64/dtrace_subr.c b/sys/cddl/dev/dtrace/aarch64/dtrace_subr.c index 20418e3a475b..32e84d8fbfe9 100644 --- a/sys/cddl/dev/dtrace/aarch64/dtrace_subr.c +++ b/sys/cddl/dev/dtrace/aarch64/dtrace_subr.c @@ -151,6 +151,32 @@ dtrace_sync(void) dtrace_xcall(DTRACE_CPUALL, (dtrace_xcall_t)dtrace_sync_func, NULL); } +static uint64_t nsec_scale; + +#define SCALE_SHIFT 25 + +/* + * Choose scaling factors which let us convert a cntvct_el0 value to nanoseconds + * without overflow, as in the amd64 implementation. + * + * Documentation for the ARM generic timer states that typical counter + * frequencies are in the range 1Mhz-50Mhz; in ARMv9 the frequency is fixed at + * 1GHz. The lower bound of 1MHz forces the shift to be at most 25 bits. At + * that frequency, the calculation (hi * scale) << (32 - shift) will not + * overflow for over 100 years, assuming that the counter value starts at 0 upon + * boot. + */ +static void +dtrace_gethrtime_init(void *arg __unused) +{ + uint64_t freq; + + freq = READ_SPECIALREG(cntfrq_el0); + nsec_scale = ((uint64_t)NANOSEC << SCALE_SHIFT) / freq; +} +SYSINIT(dtrace_gethrtime_init, SI_SUB_DTRACE, SI_ORDER_ANY, + dtrace_gethrtime_init, NULL); + /* * DTrace needs a high resolution time function which can be called from a * probe context and guaranteed not to have instrumented with probes itself. @@ -161,10 +187,13 @@ uint64_t dtrace_gethrtime(void) { uint64_t count, freq; + uint32_t lo, hi; count = READ_SPECIALREG(cntvct_el0); - freq = READ_SPECIALREG(cntfrq_el0); - return ((1000000000UL * count) / freq); + lo = count; + hi = count >> 32; + return (((lo * nsec_scale) >> SCALE_SHIFT) + + ((hi * nsec_scale) << (32 - SCALE_SHIFT))); } /*