From nobody Fri Jan 10 16:00:45 2025 X-Original-To: dev-commits-src-main@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 4YV5wY4XSZz5kYl6; Fri, 10 Jan 2025 16:00:45 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4YV5wY43BMz58rb; Fri, 10 Jan 2025 16:00:45 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1736524845; 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=8MShbtqpzzab+ZvCFT+mzBIuEPAc6HYBTrYxOROjBUU=; b=q3lziTN4xm8COR+8RPWZsj2wHdVHOG6OQVgOn0b22h2PJKuNPdSoHvITqcU5qDYvGaeBsN zYE8LQJBH6XVm5kC7EwP+lrrj8GjKpKciDsfN9QvLH3WvMZlZcFLvxzYwPNRZlJBOsPg/6 YBFhHVYzgkCV1S5ytp+94Bay6OwiAnBnkbUWcGx1NlVycEgRiBsJWG+P5XlPxxo0inp42n r0USWBHTom/U9zF9hb0dA5PfZGEASjJpwYPxHfEcProPJ9v03o+7zEmg+BxUWYHWku7PQ6 qsU+mctBVdVS9car0pI8pK43S6oCvYjbryoN5GiLpqXV6z3Nof3VwFNBvpaZVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1736524845; 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=8MShbtqpzzab+ZvCFT+mzBIuEPAc6HYBTrYxOROjBUU=; b=uEAchUuddM4feTd5sTi2TriZBYLamYSYeA9ZFuslPMwV69WsldtZEW0dlW2OwnFEBsmIBC TWWP+slpyHpZXUgi7GNDxjt7MUkShRdAwuoVKpjdfx6YatiepBl1KeBnm7Vv7NI5YZhCES rv0cY7I9f5gCwidniViMSf1HZrqjjF5TELU2I64TZbRQOEKVYTsVG9fNjYOsKwKIjaro7J SoG2EvpeMksqroUUzIgaSGLOHVwAjV/MyZC1yX26GCvphGQN1oB2r5B3haKLiWUWUNJT6H 4hlxiSx734ye/pUHGR0XPpP+J7UWduakmTwzNWebDt+JbzmZF3FiSW+j44wubQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1736524845; a=rsa-sha256; cv=none; b=vMjEbK9WvPMObzKObOEiLLu6PeT8y/++j+AXKomk27WsGd6LouCO6fj4lKnny/YYLVcVIC bOPlMMuISt+EvvStbnnTOYRZH8S1qXQTPbOyc7LG+XbUGaTTYnWye/i2iD00O/QJlWSFvY m01FJpWl6JJlOmO55Lh7i1J08W2pm42gLA68tohxQy2RLqKhPYf66cBxknTzFLNNG/fOIs aWDFmMhmB44Y9NJyCrUNitd3D7fibmTeMNqoWXcb5p/BgmXLCNWrzWCHF/7vhpA6i4cZtW 7xKv9lA+d7K9amw94dBtSGRLx7h468yse+QNrOsqbHq1cOuIrvglK6k5Qcb56Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none 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 4YV5wY3QLJz3CF; Fri, 10 Jan 2025 16:00:45 +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 50AG0j5i062311; Fri, 10 Jan 2025 16:00:45 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 50AG0jk6062308; Fri, 10 Jan 2025 16:00:45 GMT (envelope-from git) Date: Fri, 10 Jan 2025 16:00:45 GMT Message-Id: <202501101600.50AG0jk6062308@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 6b82130e6c9a - main - clock: Add a long ticks variable, ticksl List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: 6b82130e6c9add4a8892ca897df5a0ec04663ea2 Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=6b82130e6c9add4a8892ca897df5a0ec04663ea2 commit 6b82130e6c9add4a8892ca897df5a0ec04663ea2 Author: Mark Johnston AuthorDate: 2025-01-10 15:37:07 +0000 Commit: Mark Johnston CommitDate: 2025-01-10 15:42:59 +0000 clock: Add a long ticks variable, ticksl For compatibility with Linux, it's useful to have a tick counter of width sizeof(long), but our tick counter is an int. Currently the linuxkpi tries paper over this difference, but this cannot really be done reliably, so it's desirable to have a wider tick counter. This change introduces ticksl, keeping the existing ticks variable. Follow a suggestion from kib to avoid having to maintain two separate counters and to avoid converting existing code to use ticksl: change hardclock() to update ticksl instead of ticks, and then use assembler directives to make ticks and ticksl overlap such that loading ticks gives the bottom 32 bits. This makes it possible to use ticksl in the linuxkpi without having to convert any native code, and without making hardclock() more complicated or expensive. Then, the linuxkpi can be modified to use ticksl instead of ticks. Reviewed by: olce, kib, emaste MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D48383 --- sys/conf/files | 1 + sys/kern/kern_clock.c | 26 ++++++++++++++------------ sys/kern/kern_tc.c | 4 ++-- sys/kern/subr_param.c | 2 +- sys/kern/subr_ticks.s | 44 ++++++++++++++++++++++++++++++++++++++++++++ sys/sys/kernel.h | 9 +++++++++ sys/sys/timetc.h | 2 +- 7 files changed, 72 insertions(+), 16 deletions(-) diff --git a/sys/conf/files b/sys/conf/files index d358737c5613..a630d9dd72bc 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -3932,6 +3932,7 @@ kern/subr_stack.c optional ddb | stack | ktr kern/subr_stats.c optional stats kern/subr_taskqueue.c standard kern/subr_terminal.c optional vt +kern/subr_ticks.s standard kern/subr_trap.c standard kern/subr_turnstile.c standard kern/subr_uio.c standard diff --git a/sys/kern/kern_clock.c b/sys/kern/kern_clock.c index 6fa2272ed54a..b11c0d235139 100644 --- a/sys/kern/kern_clock.c +++ b/sys/kern/kern_clock.c @@ -323,7 +323,7 @@ read_cpu_time(long *cp_time) #include -static int watchdog_ticks; +static long watchdog_ticks; static int watchdog_enabled; static void watchdog_fire(void); static void watchdog_config(void *, u_int, int *); @@ -369,10 +369,9 @@ watchdog_attach(void) int stathz; int profhz; int profprocs; -volatile int ticks; int psratio; -DPCPU_DEFINE_STATIC(int, pcputicks); /* Per-CPU version of ticks. */ +DPCPU_DEFINE_STATIC(long, pcputicks); /* Per-CPU version of ticks. */ #ifdef DEVICE_POLLING static int devpoll_run = 0; #endif @@ -480,14 +479,14 @@ hardclock(int cnt, int usermode) struct pstats *pstats; struct thread *td = curthread; struct proc *p = td->td_proc; - int *t = DPCPU_PTR(pcputicks); - int global, i, newticks; + long global, newticks, *t; /* * Update per-CPU and possibly global ticks values. */ + t = DPCPU_PTR(pcputicks); *t += cnt; - global = ticks; + global = atomic_load_long(&ticksl); do { newticks = *t - global; if (newticks <= 0) { @@ -496,7 +495,7 @@ hardclock(int cnt, int usermode) newticks = 0; break; } - } while (!atomic_fcmpset_int(&ticks, &global, *t)); + } while (!atomic_fcmpset_long(&ticksl, &global, *t)); /* * Run current process's virtual and profile time, as needed. @@ -525,8 +524,10 @@ hardclock(int cnt, int usermode) } #endif /* DEVICE_POLLING */ if (watchdog_enabled > 0) { - i = atomic_fetchadd_int(&watchdog_ticks, -newticks); - if (i > 0 && i <= newticks) + long left; + + left = atomic_fetchadd_long(&watchdog_ticks, -newticks); + if (left > 0 && left <= newticks) watchdog_fire(); } intr_event_handle(clk_intr_event, NULL); @@ -540,11 +541,12 @@ hardclock(int cnt, int usermode) void hardclock_sync(int cpu) { - int *t; + long *t; + KASSERT(!CPU_ABSENT(cpu), ("Absent CPU %d", cpu)); - t = DPCPU_ID_PTR(cpu, pcputicks); - *t = ticks; + t = DPCPU_ID_PTR(cpu, pcputicks); + *t = ticksl; } /* diff --git a/sys/kern/kern_tc.c b/sys/kern/kern_tc.c index 26f09cb60260..a797a101bf6f 100644 --- a/sys/kern/kern_tc.c +++ b/sys/kern/kern_tc.c @@ -1916,9 +1916,9 @@ SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0, "Approximate number of hardclock ticks in a millisecond"); void -tc_ticktock(int cnt) +tc_ticktock(long cnt) { - static int count; + static long count; if (mtx_trylock_spin(&tc_setclock_mtx)) { count += cnt; diff --git a/sys/kern/subr_param.c b/sys/kern/subr_param.c index 19169ba63061..f4359efec466 100644 --- a/sys/kern/subr_param.c +++ b/sys/kern/subr_param.c @@ -197,7 +197,7 @@ init_param1(void) * Arrange for ticks to wrap 10 minutes after boot to help catch * sign problems sooner. */ - ticks = INT_MAX - (hz * 10 * 60); + ticksl = INT_MAX - (hz * 10 * 60); vn_lock_pair_pause_max = hz / 100; if (vn_lock_pair_pause_max == 0) diff --git a/sys/kern/subr_ticks.s b/sys/kern/subr_ticks.s new file mode 100644 index 000000000000..6565ba424137 --- /dev/null +++ b/sys/kern/subr_ticks.s @@ -0,0 +1,44 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2025 Mark Johnston + */ + +/* + * Define the "ticks" and "ticksl" variables. The former is overlaid onto the + * low bits of the latter. + */ + +#if defined(__aarch64__) +#include +#include + +GNU_PROPERTY_AARCH64_FEATURE_1_NOTE(GNU_PROPERTY_AARCH64_FEATURE_1_VAL) +#endif + +#ifdef _ILP32 +#define SIZEOF_TICKSL 4 +#define TICKSL_INIT .long 0 +#else +#define SIZEOF_TICKSL 8 +#define TICKSL_INIT .quad 0 +#endif + +#if defined(_ILP32) || __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#define TICKS_OFFSET 0 +#else +#define TICKS_OFFSET 4 +#endif + + .data + + .global ticksl + .type ticksl, %object + .align SIZEOF_TICKSL +ticksl: TICKSL_INIT + .size ticksl, SIZEOF_TICKSL + + .global ticks + .type ticks, %object +ticks =ticksl + TICKS_OFFSET + .size ticks, 4 diff --git a/sys/sys/kernel.h b/sys/sys/kernel.h index 3144b5a50c98..380099092107 100644 --- a/sys/sys/kernel.h +++ b/sys/sys/kernel.h @@ -65,7 +65,16 @@ extern int psratio; /* ratio: prof / stat */ extern int stathz; /* statistics clock's frequency */ extern int profhz; /* profiling clock's frequency */ extern int profprocs; /* number of process's profiling */ + +/* + * The ticks and ticksl symbols overlap, giving a 64-bit tick counter on 64-bit + * platforms while still maintaining compatibility with the legacy 32-bit + * counter. Either value can be used, but rollover must be handled; at 1000Hz, + * ticks (and ticksl on 32-bit platforms) roll over roughly every 25 days. On + * 64-bit platforms, ticksl will not roll over in the foreseeable future. + */ extern volatile int ticks; +extern volatile long ticksl; #endif /* _KERNEL */ diff --git a/sys/sys/timetc.h b/sys/sys/timetc.h index 1d9b18620e96..522770868425 100644 --- a/sys/sys/timetc.h +++ b/sys/sys/timetc.h @@ -87,7 +87,7 @@ extern int tc_min_ticktock_freq; /* u_int64_t tc_getfrequency(void); void tc_init(struct timecounter *tc); void tc_setclock(struct timespec *ts); -void tc_ticktock(int cnt); +void tc_ticktock(long cnt); void cpu_tick_calibration(void); #ifdef SYSCTL_DECL