From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E56322AE5E; Mon, 19 May 2025 08:33:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643599; cv=none; b=QEb+IysEYG/XGTwJaVBuGGD25mRobqSdjFeuQQR8F7JeYRD8m9Fj7yUnZFhRiajwjmP3WsL70/8/Q43RYnXDIPTLutC+hMF1V94HjlU2w7b1fkSu09F+mMbVDLNtUFu63oDV7Na0kcGpbvi7p2f0HQ4eByYm9JpwgjFlipxTWT8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643599; c=relaxed/simple; bh=H5LZ7r0BO9UJwvNwXDzJW1WeWZKHWrNuAEaUA+3FKfU=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Wm0u6Pqz07Rhy8BixSbgA9IXSUuORmQ/IdgYRcF1LbV/DSEdfyXX2jGa+ReceraY55+kXRADthBZqcEoinNGiosYYX+29FjsU+fRVuJBwafLyIu9RvjA6GrGgqxRwsGv5o+tL4wE+Fd6uu3+70rqJQ1/mGj/hOqPCor3Y19dYsg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=b9DPsI0B; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=q+zizoG2; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="b9DPsI0B"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="q+zizoG2" Message-ID: <20250519083025.652611452@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643595; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=L4D9JRJvf/qDRslz7xOrYCaTiKUlQohZZE5tQ+PqCuE=; b=b9DPsI0BJ2Xs31QO5b3Kbpw+2wNdxYc+iCGmpuYq/BErwvTLjUImW5lE3z02uFtd/hXXz9 2zv11Ht+4PCMGIRkOTuPSaUEi4GtI9ik8faT3fxbHqQp4KEGkeBmXdR6DGQ5Oq3vH/rxdS E0gHYd/RH3nPe24n8zFiTLeh7qPVMJ1IbVHLvdxYvt/ZQ98kGGU+vnTrOghuHsaVjV2wbX yC1zrEhaVgwQ26vbNOBqaSqv+YbM+xArYaUnq0oGtCSfkd2czQmDB9Xn2v+Pxl7A3hqbzk dGyYBK5k36/avVUVikvKWPCNtXvpiRFGf9eSeuZ1seoI+yDyHF5/OarXIrd+EA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643595; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=L4D9JRJvf/qDRslz7xOrYCaTiKUlQohZZE5tQ+PqCuE=; b=q+zizoG2VwQjhUVyzxmMJhM18LH66zqBww+UTPZb6k7Ej/XqJWRATlQyAf/dx03U4Ljdph zG4kBGoCZbwx3XDg== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 01/26] timekeeping: Remove hardcoded access to tk_core References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:15 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This was overlooked in the initial conversion. Use the provided pointer to access the shadow timekeeper. Signed-off-by: Thomas Gleixner Acked-by: John Stultz --- kernel/time/timekeeping.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -663,7 +663,7 @@ static void timekeeping_restore_shadow(s =20 static void timekeeping_update_from_shadow(struct tk_data *tkd, unsigned i= nt action) { - struct timekeeper *tk =3D &tk_core.shadow_timekeeper; + struct timekeeper *tk =3D &tkd->shadow_timekeeper; =20 lockdep_assert_held(&tkd->lock); From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A55426A0F3; Mon, 19 May 2025 08:33:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643602; cv=none; b=rVqS9JyyKiWoAkxLP7XeMgcibkTqlCN6WY9FHEjFAy7lxhaxpg71526XJQe3MPPz3hEG/yvcjePUicwm+hpAZcF6vbFg8rfR7KxRvRslBp2ls5FY+Ox6ngTlZiK7ZlUIAC+jwIz7CUq7JYnyF5W/5OMqqa+YLFMjx9c5a0BW5TE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643602; c=relaxed/simple; bh=wlpwNyknMfR1RoDXQOsQOLe9nGuz+tJKa17ta22S/mA=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=SXifZBjoQqDuG3nWbIZVqgBoBjQhL5IDs/8pi5eknBgYNXQhGnQNwG4lk9K0Ce+/BhuBm+M2Kyl4II6xa7KrRNU0YZ+6lYwd08IuxpxHYtcb+lmHkaraRpcfQu9wR9Lhh4Hov3JB6ndWiHR9d+Wck2gZyVHXv5q9FQ6ZeieUZVw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=TdDbqeW7; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Fk9TJmpO; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="TdDbqeW7"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Fk9TJmpO" Message-ID: <20250519083025.715836017@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643596; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=PLK3LOofu2itK9FE/VIdqa3Xxrw13vea1Zi9cuZZRM0=; b=TdDbqeW7xJSZBq+Ley0iGfyW1zImBQjGBoX/+xnFmUAnvfOaSSO8zR/WDq13cpjMZ8jpUG edJODaexIBsLFPsZuU+AT/KiiXz3oFbpYBiDqar/z8FuBRmkxvvxQNMn7i9G1SpxCKbI0p 6QYHcHuuepNkWWpyaHcOAoxEpO0gC7TnJ/DaJCC2L262lZj3jp+7rWK7rbjXfPFciRAWxT ce1DbH40cM97HJv7GgXbONzAo7NJR5MkFdXrz63aSLyJhCKhUaNhW9ZXwHwEMLIvaZa1LC LalBAq3HT7hlMcGufAryu/+9IQeJ7FeL/9HNc5gM9MWFA/7BslSod+yhTu225A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643596; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=PLK3LOofu2itK9FE/VIdqa3Xxrw13vea1Zi9cuZZRM0=; b=Fk9TJmpO579fid/aMb5B2w9Cl7R1XgjdG0C5Rue0dEh7x2hXfdKH9BNuWmE9GYeHvt5gzz 75hoAcA5zqlCWCAA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 02/26] timekeeping: Cleanup kernel doc of __ktime_get_real_seconds() References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:16 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Signed-off-by: Thomas Gleixner Acked-by: John Stultz --- kernel/time/timekeeping.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -975,9 +975,14 @@ time64_t ktime_get_real_seconds(void) EXPORT_SYMBOL_GPL(ktime_get_real_seconds); =20 /** - * __ktime_get_real_seconds - The same as ktime_get_real_seconds - * but without the sequence counter protect. This internal function - * is called just when timekeeping lock is already held. + * __ktime_get_real_seconds - Unprotected access to CLOCK_REALTIME seconds + * + * The same as ktime_get_real_seconds() but without the sequence counter + * protection. This function is used in restricted contexts like the x86 M= CE + * handler and in KGDB. It's unprotected on 32-bit vs. concurrent half + * completed modification and only to be used for such critical contexts. + * + * Returns: Racy snapshot of the CLOCK_REALTIME seconds value */ noinstr time64_t __ktime_get_real_seconds(void) { From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C660926A1AA; Mon, 19 May 2025 08:33:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643602; cv=none; b=CO7ExytcyqEHONifCWvkD17tEXRBymQ/KD7FPc++Nt28vHlXUpeRi6ATI3+UTJl31LX7zLkDzN23I76j8pxU1KXnLaKZ5/Z1Kq9nz9AzQ6xBqezym7x9+hnpcvoe0Yebd/SxNG6g2foV7ysL9L6EWIPDBn5G8NujFZ2h004iDgA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643602; c=relaxed/simple; bh=gyOJgOUoQbY5FBMQmvSUz4XJj9IBjeHrsRkyBykdNIs=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=n+4D7VkU3ZIAdWqYFgwgUrBUg4tZQStk6PCmzdRmlW9fRCf/P4OsQ1/9qIgVJI9s6ZhESZ4vxpAg8CJH4GMOaagMTtUcIDS25zAYQ6xIfC+FFuJTqTVp+6R/1bJoQvsUcawnvbDyp8CH928TQa+2rFHBpsJwmK8VNzh7ueRTX38= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=RQpUXCb0; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=nMCINpJd; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="RQpUXCb0"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="nMCINpJd" Message-ID: <20250519083025.779267274@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643598; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=kvTMA6AVgoKok8yWA4AdNkJvlsrtitDM+TfHbVeiIw4=; b=RQpUXCb0L9j/pQgVUYYZZHVs6oG9YpLbgzjkQ2XhBFieVdttBZS+P+gqqYrNJmxbq9lt3X bBmN0cYUeF+kMgyRF2fGh7aWf5v5s7ZzSaJRN+Yniq+SSmpmih8tp3gOSv0qwEDHFjI3Pb wDqWfa0285w8TPt+cpx6dzGleZBG/uvmpD5ySEhYT22WouAOK/1xXX16gra02ira/079eX dVpG8kvD2SpjxwQvZeo/uE7THNnz6dX3Pr5pUuHeZwS7+rzT6c782qcUN6DcGFkgMlsrqL wWzSPLI+KZOJfhqitFIUc05baXbw57tixVxEeD6X0dLTp+uKL4sejrnN6lNi/g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643598; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=kvTMA6AVgoKok8yWA4AdNkJvlsrtitDM+TfHbVeiIw4=; b=nMCINpJdScnIdJ3o3eF8J24Xq2VZ8+E/+cFV8oHIm7Vh0TcPylztzvt6vi/6vUGXfYnpuF 1fsB1p3FOVbVwTCw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 03/26] timekeeping: Avoid double notification in do_adjtimex() References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:17 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Consolidate do_adjtimex() so that it does not notify about clock changes twice. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 98 ++++++++++++++++++++++++++---------------= ----- 1 file changed, 56 insertions(+), 42 deletions(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1418,40 +1418,49 @@ int do_settimeofday64(const struct times EXPORT_SYMBOL(do_settimeofday64); =20 /** - * timekeeping_inject_offset - Adds or subtracts from the current time. + * __timekeeping_inject_offset - Adds or subtracts from the current time. * @ts: Pointer to the timespec variable containing the offset * * Adds or subtracts an offset value from the current time. */ -static int timekeeping_inject_offset(const struct timespec64 *ts) +static int __timekeeping_inject_offset(const struct timespec64 *ts) { + struct timekeeper *tks =3D &tk_core.shadow_timekeeper; + struct timespec64 tmp; + if (ts->tv_nsec < 0 || ts->tv_nsec >=3D NSEC_PER_SEC) return -EINVAL; =20 - scoped_guard (raw_spinlock_irqsave, &tk_core.lock) { - struct timekeeper *tks =3D &tk_core.shadow_timekeeper; - struct timespec64 tmp; - - timekeeping_forward_now(tks); =20 - /* Make sure the proposed value is valid */ - tmp =3D timespec64_add(tk_xtime(tks), *ts); - if (timespec64_compare(&tks->wall_to_monotonic, ts) > 0 || - !timespec64_valid_settod(&tmp)) { - timekeeping_restore_shadow(&tk_core); - return -EINVAL; - } + timekeeping_forward_now(tks); =20 - tk_xtime_add(tks, ts); - tk_set_wall_to_mono(tks, timespec64_sub(tks->wall_to_monotonic, *ts)); - timekeeping_update_from_shadow(&tk_core, TK_UPDATE_ALL); + /* Make sure the proposed value is valid */ + tmp =3D timespec64_add(tk_xtime(tks), *ts); + if (timespec64_compare(&tks->wall_to_monotonic, ts) > 0 || + !timespec64_valid_settod(&tmp)) { + timekeeping_restore_shadow(&tk_core); + return -EINVAL; } =20 - /* Signal hrtimers about time change */ - clock_was_set(CLOCK_SET_WALL); + tk_xtime_add(tks, ts); + tk_set_wall_to_mono(tks, timespec64_sub(tks->wall_to_monotonic, *ts)); + timekeeping_update_from_shadow(&tk_core, TK_UPDATE_ALL); return 0; } =20 +static int timekeeping_inject_offset(const struct timespec64 *ts) +{ + int ret; + + scoped_guard (raw_spinlock_irqsave, &tk_core.lock) + ret =3D __timekeeping_inject_offset(ts); + + /* Signal hrtimers about time change */ + if (!ret) + clock_was_set(CLOCK_SET_WALL); + return ret; +} + /* * Indicates if there is an offset between the system clock and the hardwa= re * clock/persistent clock/rtc. @@ -2186,7 +2195,7 @@ static u64 logarithmic_accumulation(stru * timekeeping_advance - Updates the timekeeper to the current time and * current NTP tick length */ -static bool timekeeping_advance(enum timekeeping_adv_mode mode) +static bool __timekeeping_advance(enum timekeeping_adv_mode mode) { struct timekeeper *tk =3D &tk_core.shadow_timekeeper; struct timekeeper *real_tk =3D &tk_core.timekeeper; @@ -2194,8 +2203,6 @@ static bool timekeeping_advance(enum tim int shift =3D 0, maxshift; u64 offset, orig_offset; =20 - guard(raw_spinlock_irqsave)(&tk_core.lock); - /* Make sure we're fully resumed: */ if (unlikely(timekeeping_suspended)) return false; @@ -2249,6 +2256,12 @@ static bool timekeeping_advance(enum tim return !!clock_set; } =20 +static bool timekeeping_advance(enum timekeeping_adv_mode mode) +{ + guard(raw_spinlock_irqsave)(&tk_core.lock); + return __timekeeping_advance(mode); +} + /** * update_wall_time - Uses the current clocksource to increment the wall t= ime * @@ -2537,10 +2550,10 @@ EXPORT_SYMBOL_GPL(random_get_entropy_fal */ int do_adjtimex(struct __kernel_timex *txc) { + struct timespec64 delta, ts; struct audit_ntp_data ad; bool offset_set =3D false; bool clock_set =3D false; - struct timespec64 ts; int ret; =20 /* Validate the data before disabling interrupts */ @@ -2549,21 +2562,6 @@ int do_adjtimex(struct __kernel_timex *t return ret; add_device_randomness(txc, sizeof(*txc)); =20 - if (txc->modes & ADJ_SETOFFSET) { - struct timespec64 delta; - - delta.tv_sec =3D txc->time.tv_sec; - delta.tv_nsec =3D txc->time.tv_usec; - if (!(txc->modes & ADJ_NANO)) - delta.tv_nsec *=3D 1000; - ret =3D timekeeping_inject_offset(&delta); - if (ret) - return ret; - - offset_set =3D delta.tv_sec !=3D 0; - audit_tk_injoffset(delta); - } - audit_ntp_init(&ad); =20 ktime_get_real_ts64(&ts); @@ -2573,6 +2571,19 @@ int do_adjtimex(struct __kernel_timex *t struct timekeeper *tks =3D &tk_core.shadow_timekeeper; s32 orig_tai, tai; =20 + if (txc->modes & ADJ_SETOFFSET) { + delta.tv_sec =3D txc->time.tv_sec; + delta.tv_nsec =3D txc->time.tv_usec; + if (!(txc->modes & ADJ_NANO)) + delta.tv_nsec *=3D 1000; + ret =3D __timekeeping_inject_offset(&delta); + if (ret) + return ret; + + offset_set =3D delta.tv_sec !=3D 0; + clock_set =3D true; + } + orig_tai =3D tai =3D tks->tai_offset; ret =3D __do_adjtimex(txc, &ts, &tai, &ad); =20 @@ -2583,13 +2594,16 @@ int do_adjtimex(struct __kernel_timex *t } else { tk_update_leap_state_all(&tk_core); } + + /* Update the multiplier immediately if frequency was set directly */ + if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK)) + clock_set |=3D __timekeeping_advance(TK_ADV_FREQ); } =20 - audit_ntp_log(&ad); + if (txc->modes & ADJ_SETOFFSET) + audit_tk_injoffset(delta); =20 - /* Update the multiplier immediately if frequency was set directly */ - if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK)) - clock_set |=3D timekeeping_advance(TK_ADV_FREQ); + audit_ntp_log(&ad); =20 if (clock_set) clock_was_set(CLOCK_SET_WALL); From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE24F26A1AF; Mon, 19 May 2025 08:33:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643602; cv=none; b=Hsr1tXk10z0avP/+cUnSk93P4niKJmXDM0CtMTrZ33TuYkWUY92mUo0LH3kN7mUlTtvSeGFDkz0HOGFaEyS7C68WYjARSalOCKgghp3y+G7EjUN15rXQGXNzHpVS/62QsQ0oSs3zSJjb7w7LkB6vIpIVUbl2onpEb/RRP0byJ4U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643602; c=relaxed/simple; bh=Sx6rncooqkhKOzat2v2on7x2T8s4Z9UMGZUwfEwbSWE=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=n4YvH1n1DLM65XuJbwC4EHkSZHwo6zVRPvKAQ2prJ4uOBEkyrtK/sfPKUmlJ0vy+j3+iC4v9rjSfpwJNG1oyd+WKY7Q2C8CrlZkjvvQCjxrDzeFG9RxLhMTCq+fKbih7lMNcB3ZHJLEa7yFzHGldgKAR7o35F1fD0+lZ8JH8s4k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=p/HtvDsb; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ygLGRzFg; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="p/HtvDsb"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ygLGRzFg" Message-ID: <20250519083025.842476378@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643599; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=3906OWgNj05DnYmkA1aciBB3X1N0MG0VN+Nbx7YbBLY=; b=p/HtvDsb27Py71nJos+HecxF56B6gdkErX21gDy8TcXL56d68Gj4X/Kiq9V3iaPcFPeUtK ejx1NQX/nlDfOBwypvDgd0gUdECOu6LNg2oPrVhcMDcqDu4aMXqkI7NaJYa+15+72gJsoE QBQGUnftOGAHUT/XuxFlREgCUhlGPWykepxqYahCnl9QJv+4Jg4IlQt1a5Dqw574HxmtFc 72LMQuXzJzO4BWZbu2psgW04p1CZW26ShcLNc9uWGz1tm/cYHPUV6bUcMaC6XzI5wW8IYt TPTIHeqSdUFtgZX34RbrPug5/SkPskX82z/aXF5THO6+MkaKt6sBWq/TZBtPVw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643599; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=3906OWgNj05DnYmkA1aciBB3X1N0MG0VN+Nbx7YbBLY=; b=ygLGRzFgM/88PJRFPuOSRaxDi3MF+SQXqHf8px/QOkuhS2rc5EFOHv2oMj6EJdaDqe5pkX ANAA9BR2Wi8gopDw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 04/26] timekeeping: Introduce timekeeper ID References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:19 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Anna-Maria Behnsen As long as there is only a single timekeeper, there is no need to clarify which timekeeper is used. But with the upcoming reusage of the timekeeper infrastructure for auxiliary clock timekeepers, an ID is required to differentiate. Introduce an enum for timekeeper IDs, introduce a field in struct tk_data to store this timekeeper id and add also initialization. The id struct field is added at the end of the second cachline, as there is a 4 byte hole anyway. Signed-off-by: Anna-Maria Behnsen Signed-off-by: Thomas Gleixner Acked-by: John Stultz --- include/linux/timekeeper_internal.h | 14 +++++++++++++- kernel/time/timekeeping.c | 5 +++-- 2 files changed, 16 insertions(+), 3 deletions(-) --- --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -12,6 +12,16 @@ #include =20 /** + * timekeeper_ids - IDs for various time keepers in the kernel + * @TIMEKEEPER_CORE: The central core timekeeper managing system time + * @TIMEKEEPERS_MAX: The maximum number of timekeepers managed + */ +enum timekeeper_ids { + TIMEKEEPER_CORE, + TIMEKEEPERS_MAX, +}; + +/** * struct tk_read_base - base structure for timekeeping readout * @clock: Current clocksource used for timekeeping. * @mask: Bitmask for two's complement subtraction of non 64bit clocks @@ -52,6 +62,7 @@ struct tk_read_base { * @offs_boot: Offset clock monotonic -> clock boottime * @offs_tai: Offset clock monotonic -> clock tai * @coarse_nsec: The nanoseconds part for coarse time getters + * @id: The timekeeper ID * @tkr_raw: The readout base structure for CLOCK_MONOTONIC_RAW * @raw_sec: CLOCK_MONOTONIC_RAW time in seconds * @clock_was_set_seq: The sequence number of clock was set events @@ -101,7 +112,7 @@ struct tk_read_base { * which results in the following cacheline layout: * * 0: seqcount, tkr_mono - * 1: xtime_sec ... coarse_nsec + * 1: xtime_sec ... id * 2: tkr_raw, raw_sec * 3,4: Internal variables * @@ -123,6 +134,7 @@ struct timekeeper { ktime_t offs_boot; ktime_t offs_tai; u32 coarse_nsec; + enum timekeeper_ids id; =20 /* Cacheline 2: */ struct tk_read_base tkr_raw; --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1663,10 +1663,11 @@ read_persistent_wall_and_boot_offset(str *boot_offset =3D ns_to_timespec64(local_clock()); } =20 -static __init void tkd_basic_setup(struct tk_data *tkd) +static __init void tkd_basic_setup(struct tk_data *tkd, enum timekeeper_id= s tk_id) { raw_spin_lock_init(&tkd->lock); seqcount_raw_spinlock_init(&tkd->seq, &tkd->lock); + tkd->timekeeper.id =3D tkd->shadow_timekeeper.id =3D tk_id; } =20 /* @@ -1696,7 +1697,7 @@ void __init timekeeping_init(void) struct timekeeper *tks =3D &tk_core.shadow_timekeeper; struct clocksource *clock; =20 - tkd_basic_setup(&tk_core); + tkd_basic_setup(&tk_core, TIMEKEEPER_CORE); =20 read_persistent_wall_and_boot_offset(&wall_time, &boot_offset); if (timespec64_valid_settod(&wall_time) && From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33DD226AA9E; Mon, 19 May 2025 08:33:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643603; cv=none; b=rW9qgp18c0F0Dur95xW7YjVv9sSE3XYdNpvTqtDGdu0DQasYoV8ewk9grR4LLijXDPBMKhmwJqAg//dwIxv5mU0D0XqA+kY9voKzzF+83MzMLXt7eXSOVYTAY7BlxT2eOtM/RNZxsyo1VkjRyTS6lCpYm8rN6PK8wgaqetsBle4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643603; c=relaxed/simple; bh=yZ2CxMBFDslosCsaU9H6xC8XDWQs6w4ZmIcPTpft4A4=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=cF740MirehckAbaoBnXU42pR/9TWthysMwtfzdU6lBu1keOP2u7Z5jlH6JbmjLlQzuImLb6xB/jdEJVy8DE4m//uFexGdMpFzKIptpLMDEXsIBdideOJPHX9z817wTn6raEImZ+jvobwhYHwNa4iNyMEyDFHDYvF41UrHm8UIN4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=p+/DUNs0; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=pT391Mru; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="p+/DUNs0"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="pT391Mru" Message-ID: <20250519083025.905800695@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643600; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=tDFFxWQgqXHC1kYIc4ua9v4PhXUgIm5hvteunQy/FmE=; b=p+/DUNs0fwBiQlFFRDFKUekpBBGL8l535M5GVBlTlzWtRJBi9gaYl7MoTsd79s5SmBbNuG JfcxBKEXox9D5EWzCYHTGMKZXgbesKCN0nwgD1oIbOiIrgzsV15tc62wfLFOjkq6td+Bwq aSinChCf9bcXuhMxk4I34D/phV6KODgkv3cOJHhKs3Z1opv96l9yZVEaqgxd/QmgR7RA4w iwWgUwUsG/6Dcx0XZI+fz0fNekpQCWS8LFd9fcLwztSwSMCjDIvbdcu23Zn7NV0ijGOJ0v 5Mw7cieTfpQzK5oTa6t9hmTYrtydfFJQgb0wxlR4CafyRQ8j1PBSTYhe3jeW1g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643600; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=tDFFxWQgqXHC1kYIc4ua9v4PhXUgIm5hvteunQy/FmE=; b=pT391MruRl+C92/xlOm2g2pnfLwazPEUucBB7+6/vKBxtt029OieyYx3/pV1zfMmt93VMx OktfVfCaaFoLZdBg== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 05/26] time: Introduce auxiliary POSIX clocks References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:20 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Anna-Maria Behnsen To support auxiliary timekeeping and the related user space interfaces, it's required to define a clock ID range for them. Reserve 8 auxiliary clock IDs after the regular timekeeping clock ID space. This is the maximum number of auxiliary clocks the kernel can support. The = actual number of supported clocks depends obviously on the presence of related dev= ices and might be constraint by the available VDSO space. Add the corresponding timekeeper IDs as well. Signed-off-by: Anna-Maria Behnsen Signed-off-by: Thomas Gleixner Acked-by: John Stultz --- include/linux/timekeeper_internal.h | 6 ++++++ include/uapi/linux/time.h | 11 +++++++++++ kernel/time/Kconfig | 15 +++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) --- --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -14,10 +14,16 @@ /** * timekeeper_ids - IDs for various time keepers in the kernel * @TIMEKEEPER_CORE: The central core timekeeper managing system time + * @TIMEKEEPER_AUX: The first AUX timekeeper + * @TIMEKEEPER_AUX_LAST:The last AUX timekeeper * @TIMEKEEPERS_MAX: The maximum number of timekeepers managed */ enum timekeeper_ids { TIMEKEEPER_CORE, +#ifdef CONFIG_POSIX_AUX_CLOCKS + TIMEKEEPER_AUX, + TIMEKEEPER_AUX_LAST =3D TIMEKEEPER_AUX + MAX_AUX_CLOCKS - 1, +#endif TIMEKEEPERS_MAX, }; =20 --- a/include/uapi/linux/time.h +++ b/include/uapi/linux/time.h @@ -64,6 +64,17 @@ struct timezone { #define CLOCK_TAI 11 =20 #define MAX_CLOCKS 16 + +/* + * AUX clock support. AUXiliary clocks are dynamically configured by + * enabling a clock ID. These clock can be steered independently of the + * core timekeeper. The kernel can support up to 8 auxiliary clocks, but + * the actual limit depends on eventual architecture constraints vs. VDSO. + */ +#define CLOCK_AUX MAX_CLOCKS +#define MAX_AUX_CLOCKS 8 +#define CLOCK_AUX_LAST (CLOCK_AUX + MAX_AUX_CLOCKS - 1) + #define CLOCKS_MASK (CLOCK_REALTIME | CLOCK_MONOTONIC) #define CLOCKS_MONO CLOCK_MONOTONIC =20 --- a/kernel/time/Kconfig +++ b/kernel/time/Kconfig @@ -82,9 +82,9 @@ config CONTEXT_TRACKING_IDLE help Tracks idle state on behalf of RCU. =20 -if GENERIC_CLOCKEVENTS menu "Timers subsystem" =20 +if GENERIC_CLOCKEVENTS # Core internal switch. Selected by NO_HZ_COMMON / HIGH_RES_TIMERS. This is # only related to the tick functionality. Oneshot clockevent devices # are supported independent of this. @@ -208,6 +208,17 @@ config CLOCKSOURCE_WATCHDOG_MAX_SKEW_US interval and NTP's maximum frequency drift of 500 parts per million. If the clocksource is good enough for NTP, it is good enough for the clocksource watchdog! +endif + +config POSIX_AUX_CLOCKS + bool "Enable auxiliary POSIX clocks" + depends on POSIX_TIMERS + help + Auxiliary POSIX clocks are clocks which can be steered + independently of the core timekeeper, which controls the + MONOTONIC, REALTIME, BOOTTIME and TAI clocks. They are useful to + provide e.g. lockless time accessors to independent PTP clocks + and other clock domains, which are not correlated to the TAI/NTP + notion of time. =20 endmenu -endif From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13D5726B0A7; Mon, 19 May 2025 08:33:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643604; cv=none; b=VRpbg7DdIVe2XcngczrnGwwh2rfssnKniwqnwSV6TvjO2H0GhSCVCicNZLgbuugh1eAzn0RqzCGxJaI3nPAHJF8uMt/fNHw+Kzbu/4ZZvh2rhBQd2pxyiYcudeClPafLMFknTMk/DVVkaBV73RXXqdApo5d+JaGwN5zh3VtxTTo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643604; c=relaxed/simple; bh=oAwwXd29RmLztsc7SAntcL4APp16zBohgk7uM9zwQWA=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=RskvYAQxIVaOVWEsDMng5yKAXEEZrLiBLw6xNknu/9tx61mrqiCIEHzS7hnpnI/Qrxi3TMGyEI5w19gv5nqWaxYF6OqFqznXse0qhmrO5UXnboJqViMG45UwqKSmyYOHRExS3kQSP8H/DMFKO6KWA9kcG6NPeqAyW2MzIsAmlxo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=KexKH+k5; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=wxtwz27F; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="KexKH+k5"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="wxtwz27F" Message-ID: <20250519083025.969000914@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643601; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=fuJ7o+F9Ll8i3WU2Ep3XtBcWSbqbcijosAFfflFA/wk=; b=KexKH+k56yF/n2IUNWbzusciqEdMhv2G0ZAuklgI7H8NtmSj0qFc1ELpkUaTBvLorbUhhh OyeQHfJQ6xW5z99KtdKeAI3Cln18zhO9tDv/+pRYcxcc3KDJDVVosjZNwCMX1K7hq4pzhb hLkg+yQtm/3Rwh6BGS2+AtYhEz7eGIlRT+PM2TK7S7GphgYfc9CnW2sAwNvp/yN/ytfuxf GJ+olk2G+iyXekAPVe0ghxx6EQATliRB/NcvsxQ0DPGTSdNYRHXLoJMmS7hwFJwZZf8vLA Jkm/Z9CIGLMwamPDsJxX9vLGV7zi30VytCxNwUzPhYKLLrinEsFfKm53Zk8fRw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643601; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=fuJ7o+F9Ll8i3WU2Ep3XtBcWSbqbcijosAFfflFA/wk=; b=wxtwz27FUXF9wkNYkA5oJMBrrDLQ+yN2BDkW5oBRhE63vRPNaA0fCdfFy7crLkPpZTvpdM dGhRFZfbu4cXLkBg== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 06/26] ntp: Add support for auxiliary timekeepers References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:21 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If auxiliary clocks are enabled, provide an array of NTP data so that the auxiliary timekeepers can be steered independently of the core timekeeper. Signed-off-by: Thomas Gleixner Acked-by: John Stultz --- kernel/time/ntp.c | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) --- --- a/kernel/time/ntp.c +++ b/kernel/time/ntp.c @@ -18,6 +18,7 @@ #include #include #include +#include =20 #include "ntp_internal.h" #include "timekeeping_internal.h" @@ -86,14 +87,16 @@ struct ntp_data { #endif }; =20 -static struct ntp_data tk_ntp_data =3D { - .tick_usec =3D USER_TICK_USEC, - .time_state =3D TIME_OK, - .time_status =3D STA_UNSYNC, - .time_constant =3D 2, - .time_maxerror =3D NTP_PHASE_LIMIT, - .time_esterror =3D NTP_PHASE_LIMIT, - .ntp_next_leap_sec =3D TIME64_MAX, +static struct ntp_data tk_ntp_data[TIMEKEEPERS_MAX] =3D { + [ 0 ... TIMEKEEPERS_MAX - 1 ] =3D { + .tick_usec =3D USER_TICK_USEC, + .time_state =3D TIME_OK, + .time_status =3D STA_UNSYNC, + .time_constant =3D 2, + .time_maxerror =3D NTP_PHASE_LIMIT, + .time_esterror =3D NTP_PHASE_LIMIT, + .ntp_next_leap_sec =3D TIME64_MAX, + }, }; =20 #define SECS_PER_DAY 86400 @@ -351,13 +354,13 @@ static void __ntp_clear(struct ntp_data */ void ntp_clear(void) { - __ntp_clear(&tk_ntp_data); + __ntp_clear(&tk_ntp_data[TIMEKEEPER_CORE]); } =20 =20 u64 ntp_tick_length(void) { - return tk_ntp_data.tick_length; + return tk_ntp_data[TIMEKEEPER_CORE].tick_length; } =20 /** @@ -368,7 +371,7 @@ u64 ntp_tick_length(void) */ ktime_t ntp_get_next_leap(void) { - struct ntp_data *ntpdata =3D &tk_ntp_data; + struct ntp_data *ntpdata =3D &tk_ntp_data[TIMEKEEPER_CORE]; ktime_t ret; =20 if ((ntpdata->time_state =3D=3D TIME_INS) && (ntpdata->time_status & STA_= INS)) @@ -389,7 +392,7 @@ ktime_t ntp_get_next_leap(void) */ int second_overflow(time64_t secs) { - struct ntp_data *ntpdata =3D &tk_ntp_data; + struct ntp_data *ntpdata =3D &tk_ntp_data[TIMEKEEPER_CORE]; s64 delta; int leap =3D 0; s32 rem; @@ -605,7 +608,7 @@ static inline int update_rtc(struct time */ static inline bool ntp_synced(void) { - return !(tk_ntp_data.time_status & STA_UNSYNC); + return !(tk_ntp_data[TIMEKEEPER_CORE].time_status & STA_UNSYNC); } =20 /* @@ -762,7 +765,7 @@ static inline void process_adjtimex_mode int __do_adjtimex(struct __kernel_timex *txc, const struct timespec64 *ts, s32 *time_tai, struct audit_ntp_data *ad) { - struct ntp_data *ntpdata =3D &tk_ntp_data; + struct ntp_data *ntpdata =3D &tk_ntp_data[TIMEKEEPER_CORE]; int result; =20 if (txc->modes & ADJ_ADJTIME) { @@ -1031,8 +1034,8 @@ static void hardpps_update_phase(struct */ void __hardpps(const struct timespec64 *phase_ts, const struct timespec64 = *raw_ts) { + struct ntp_data *ntpdata =3D &tk_ntp_data[TIMEKEEPER_CORE]; struct pps_normtime pts_norm, freq_norm; - struct ntp_data *ntpdata =3D &tk_ntp_data; =20 pts_norm =3D pps_normalize_ts(*phase_ts); =20 @@ -1083,18 +1086,18 @@ void __hardpps(const struct timespec64 * =20 static int __init ntp_tick_adj_setup(char *str) { - int rc =3D kstrtos64(str, 0, &tk_ntp_data.ntp_tick_adj); + int rc =3D kstrtos64(str, 0, &tk_ntp_data[TIMEKEEPER_CORE].ntp_tick_adj); if (rc) return rc; =20 - tk_ntp_data.ntp_tick_adj <<=3D NTP_SCALE_SHIFT; + tk_ntp_data[TIMEKEEPER_CORE].ntp_tick_adj <<=3D NTP_SCALE_SHIFT; return 1; } - __setup("ntp_tick_adj=3D", ntp_tick_adj_setup); =20 void __init ntp_init(void) { - ntp_clear(); + for (int id =3D 0; id < TIMEKEEPERS_MAX; id++) + __ntp_clear(tk_ntp_data + id); ntp_init_cmos_sync(); } From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB50F26B97D; Mon, 19 May 2025 08:33:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643606; cv=none; b=SFtD40bsJGQOTB9jMwsjPcLws1op9kK+nBwFeA8WzrE86yxPt9Fs6RYQhrIw7mgorZUhJ+h2Ip+kfM0ldnvQxmUax2jTZq0YJDlAVkvRHq4Z3FLlekpv9wfBFxFwmkcZz4wUAmLFc6XDO0FHPWRLxBoc2qZi1dAImKkl35hAxmI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643606; c=relaxed/simple; bh=4oSyT8c87swIdB49sX0Y4s+hFyQeoBovFZ/8/TiDCSQ=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=C8ZW8qlkBbqC2Y7a5HGE2FtLNETG55WAxQbvFEV/9cjJ9wVCgDFTpBxJRVYJWzn0EgymJza8VSEEXr91dCqu6b2ROimqqwpU7jcERtSY/DlNgnOvlmRjzgajGBx6earSY00V3zhDxvXhjqTns3mLPHXaOPTNN0CywvXcE2S0i2w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=nZxJsh4K; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=EhC+gmva; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="nZxJsh4K"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="EhC+gmva" Message-ID: <20250519083026.032425931@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643603; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=nsMXYnVhmeWV+rct6a1vAML/qWO67maRFKGbU8FjRDI=; b=nZxJsh4KIt3uclp6UdINIiX0P+Euu01OBve+BN2vZ+od1KsSl+4SNC7BOjVlsNiceY7q43 /P1d9gpjhnQFwR1wZ8qd7DONx2M6xCgc4svitTlb0xYIiZcr/r/JvQCiEWpSoivVQRvL/q gPcave98QOb5tLP9q4WBvoj6EcAy3IJnn7SoexSKl59+I1xbejPUJd9XCVGaUoHNZyotNl vvp/bJfkzADOe1R6/+fvaGYdU+Wi3F36RMMzPy1uVbUKOjTsRl/toig+Y/GZm8sbatM0S8 9xqIatpeU11s4TReuOlmEaLcpQkg8E8MCHG6ebBiQhz+6Bve0HgA8259XPSlJg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643603; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=nsMXYnVhmeWV+rct6a1vAML/qWO67maRFKGbU8FjRDI=; b=EhC+gmva+dPEb2ArVxqMjRURyYo1T3/uXvduM2J0spnwOnVy/hctFmTzoq2JH6pDHRD2/v saFdtYhGte/vABBQ== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 07/26] ntp: Add timekeeper ID arguments to public functions References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:22 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for supporting auxiliary POSIX clocks, add a timekeeper ID to the relevant functions. Signed-off-by: Thomas Gleixner Acked-by: John Stultz --- kernel/time/ntp.c | 33 +++++++++++++++++++-------------- kernel/time/ntp_internal.h | 11 +++++------ kernel/time/timekeeping.c | 12 ++++++------ 3 files changed, 30 insertions(+), 26 deletions(-) --- --- a/kernel/time/ntp.c +++ b/kernel/time/ntp.c @@ -351,33 +351,38 @@ static void __ntp_clear(struct ntp_data =20 /** * ntp_clear - Clears the NTP state variables + * @tkid: Timekeeper ID to be able to select proper ntp data array member */ -void ntp_clear(void) +void ntp_clear(unsigned int tkid) { - __ntp_clear(&tk_ntp_data[TIMEKEEPER_CORE]); + __ntp_clear(&tk_ntp_data[tkid]); } =20 =20 -u64 ntp_tick_length(void) +u64 ntp_tick_length(unsigned int tkid) { - return tk_ntp_data[TIMEKEEPER_CORE].tick_length; + return tk_ntp_data[tkid].tick_length; } =20 /** * ntp_get_next_leap - Returns the next leapsecond in CLOCK_REALTIME ktime= _t + * @tkid: Timekeeper ID * - * Provides the time of the next leapsecond against CLOCK_REALTIME in - * a ktime_t format. Returns KTIME_MAX if no leapsecond is pending. + * Returns: For @tkid =3D=3D TIMEKEEPER_CORE this provides the time of the= next + * leap second against CLOCK_REALTIME in a ktime_t format if a + * leap second is pending. KTIME_MAX otherwise. */ -ktime_t ntp_get_next_leap(void) +ktime_t ntp_get_next_leap(unsigned int tkid) { struct ntp_data *ntpdata =3D &tk_ntp_data[TIMEKEEPER_CORE]; - ktime_t ret; + + if (tkid !=3D TIMEKEEPER_CORE) + return KTIME_MAX; =20 if ((ntpdata->time_state =3D=3D TIME_INS) && (ntpdata->time_status & STA_= INS)) return ktime_set(ntpdata->ntp_next_leap_sec, 0); - ret =3D KTIME_MAX; - return ret; + + return KTIME_MAX; } =20 /* @@ -390,9 +395,9 @@ ktime_t ntp_get_next_leap(void) * * Also handles leap second processing, and returns leap offset */ -int second_overflow(time64_t secs) +int second_overflow(unsigned int tkid, time64_t secs) { - struct ntp_data *ntpdata =3D &tk_ntp_data[TIMEKEEPER_CORE]; + struct ntp_data *ntpdata =3D &tk_ntp_data[tkid]; s64 delta; int leap =3D 0; s32 rem; @@ -762,10 +767,10 @@ static inline void process_adjtimex_mode * adjtimex() mainly allows reading (and writing, if superuser) of * kernel time-keeping variables. used by xntpd. */ -int __do_adjtimex(struct __kernel_timex *txc, const struct timespec64 *ts, +int __do_adjtimex(unsigned int tkid, struct __kernel_timex *txc, const str= uct timespec64 *ts, s32 *time_tai, struct audit_ntp_data *ad) { - struct ntp_data *ntpdata =3D &tk_ntp_data[TIMEKEEPER_CORE]; + struct ntp_data *ntpdata =3D &tk_ntp_data[tkid]; int result; =20 if (txc->modes & ADJ_ADJTIME) { --- a/kernel/time/ntp_internal.h +++ b/kernel/time/ntp_internal.h @@ -3,13 +3,12 @@ #define _LINUX_NTP_INTERNAL_H =20 extern void ntp_init(void); -extern void ntp_clear(void); +extern void ntp_clear(unsigned int tkid); /* Returns how long ticks are at present, in ns / 2^NTP_SCALE_SHIFT. */ -extern u64 ntp_tick_length(void); -extern ktime_t ntp_get_next_leap(void); -extern int second_overflow(time64_t secs); -extern int __do_adjtimex(struct __kernel_timex *txc, - const struct timespec64 *ts, +extern u64 ntp_tick_length(unsigned int tkid); +extern ktime_t ntp_get_next_leap(unsigned int tkid); +extern int second_overflow(unsigned int tkid, time64_t secs); +extern int __do_adjtimex(unsigned int tkid, struct __kernel_timex *txc, co= nst struct timespec64 *ts, s32 *time_tai, struct audit_ntp_data *ad); extern void __hardpps(const struct timespec64 *phase_ts, const struct time= spec64 *raw_ts); =20 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -601,7 +601,7 @@ EXPORT_SYMBOL_GPL(pvclock_gtod_unregiste */ static inline void tk_update_leap_state(struct timekeeper *tk) { - tk->next_leap_ktime =3D ntp_get_next_leap(); + tk->next_leap_ktime =3D ntp_get_next_leap(tk->id); if (tk->next_leap_ktime !=3D KTIME_MAX) /* Convert to monotonic time */ tk->next_leap_ktime =3D ktime_sub(tk->next_leap_ktime, tk->offs_real); @@ -678,7 +678,7 @@ static void timekeeping_update_from_shad =20 if (action & TK_CLEAR_NTP) { tk->ntp_error =3D 0; - ntp_clear(); + ntp_clear(tk->id); } =20 tk_update_leap_state(tk); @@ -2049,7 +2049,7 @@ static __always_inline void timekeeping_ */ static void timekeeping_adjust(struct timekeeper *tk, s64 offset) { - u64 ntp_tl =3D ntp_tick_length(); + u64 ntp_tl =3D ntp_tick_length(tk->id); u32 mult; =20 /* @@ -2130,7 +2130,7 @@ static inline unsigned int accumulate_ns } =20 /* Figure out if its a leap sec and apply if needed */ - leap =3D second_overflow(tk->xtime_sec); + leap =3D second_overflow(tk->id, tk->xtime_sec); if (unlikely(leap)) { struct timespec64 ts; =20 @@ -2227,7 +2227,7 @@ static bool __timekeeping_advance(enum t shift =3D ilog2(offset) - ilog2(tk->cycle_interval); shift =3D max(0, shift); /* Bound shift to one less than what overflows tick_length */ - maxshift =3D (64 - (ilog2(ntp_tick_length())+1)) - 1; + maxshift =3D (64 - (ilog2(ntp_tick_length(tk->id)) + 1)) - 1; shift =3D min(shift, maxshift); while (offset >=3D tk->cycle_interval) { offset =3D logarithmic_accumulation(tk, offset, shift, &clock_set); @@ -2586,7 +2586,7 @@ int do_adjtimex(struct __kernel_timex *t } =20 orig_tai =3D tai =3D tks->tai_offset; - ret =3D __do_adjtimex(txc, &ts, &tai, &ad); + ret =3D __do_adjtimex(tks->id, txc, &ts, &tai, &ad); =20 if (tai !=3D orig_tai) { __timekeeping_set_tai_offset(tks, tai); From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E098226C3BB; Mon, 19 May 2025 08:33:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643607; cv=none; b=FmjDR20bFmFr5idWhCh9WK5KavRsYIcNAPJpSsoPZRYX/hcnTvD2zl+iIH8ydcQGrL3GqNM10WHO+HbKNDVC06/8DWDivExCnnOV4J5XlQFnBdIqHwq7YL40ZSRx0KHWICnAIsy/b25k1BCZ+/EupVmhAmL4krvF8zHT3IYC84Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643607; c=relaxed/simple; bh=aep2icN5cFnOirjS2mGyNdZUKxHqz9ryb7dHiUjfhEg=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=tauS4w0w0oH0z9NW/rflChlmYkeBBfCrLO2TWYYQu2BBtWHbmGurJHxz4x+N4zJ8qMWbbyS0ZybMwF6/cGQFu8W9mvc2ZUW358Hi+Zte3WqAOykDZ4SQfqym50bHV1Gj6fe/bv8g4Jwe/zBzrObee8rguFbobz+wemMa8MPqHZY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=PZuxHFWy; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=WCB6uxOl; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="PZuxHFWy"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="WCB6uxOl" Message-ID: <20250519083026.095637820@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643604; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=YLdik8p+akr6/2708As9+5DTo8ghQQC0aWkttak5lwc=; b=PZuxHFWyzUI2RPQL4MItkc8RoECK9eR35GUIazGT0LO0tHrSFXDB/3Fxh3vwkum8PhhXTy 906pq2WQrf5ijKyzsUtS8LfCO5HJ3rNUdbQ8F1ZWr4va4VrLDuiYkzpFYnQLC64wGjhzZ8 oBmAJY7o4wQPhUCiVyLLtadDy61POYHX3bBMy28iD3PsbuK+eLevcNORBQpP5U4D4HxvJK w3IpPkQpv2K5M+AsZavulxP64oTxrGPGySt1z02p7izs20l2xS+eLVn2BHZRm+h+QPV4eo Z7j1MarYKQnbOTa84jcfiJN54wJp9dGfurNMKBvwsFCuAiWRGLWXd7k7mW2+3A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643604; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=YLdik8p+akr6/2708As9+5DTo8ghQQC0aWkttak5lwc=; b=WCB6uxOljqyHX7LCc52+jcpcAFaqK1DiC0sWAkBvo5C/ZCAZ6s1ZNJnQzub9IWmGFwTp+D vRGKUAFAqG0htBBg== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 08/26] ntp: Rename __do_adjtimex() to ntp_adjtimex() References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:23 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Clean up the name space. No functional change. Signed-off-by: Thomas Gleixner Acked-by: John Stultz --- kernel/time/ntp.c | 4 ++-- kernel/time/ntp_internal.h | 4 ++-- kernel/time/timekeeping.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) --- --- a/kernel/time/ntp.c +++ b/kernel/time/ntp.c @@ -767,8 +767,8 @@ static inline void process_adjtimex_mode * adjtimex() mainly allows reading (and writing, if superuser) of * kernel time-keeping variables. used by xntpd. */ -int __do_adjtimex(unsigned int tkid, struct __kernel_timex *txc, const str= uct timespec64 *ts, - s32 *time_tai, struct audit_ntp_data *ad) +int ntp_adjtimex(unsigned int tkid, struct __kernel_timex *txc, const stru= ct timespec64 *ts, + s32 *time_tai, struct audit_ntp_data *ad) { struct ntp_data *ntpdata =3D &tk_ntp_data[tkid]; int result; --- a/kernel/time/ntp_internal.h +++ b/kernel/time/ntp_internal.h @@ -8,8 +8,8 @@ extern void ntp_clear(unsigned int tkid) extern u64 ntp_tick_length(unsigned int tkid); extern ktime_t ntp_get_next_leap(unsigned int tkid); extern int second_overflow(unsigned int tkid, time64_t secs); -extern int __do_adjtimex(unsigned int tkid, struct __kernel_timex *txc, co= nst struct timespec64 *ts, - s32 *time_tai, struct audit_ntp_data *ad); +extern int ntp_adjtimex(unsigned int tkid, struct __kernel_timex *txc, con= st struct timespec64 *ts, + s32 *time_tai, struct audit_ntp_data *ad); extern void __hardpps(const struct timespec64 *phase_ts, const struct time= spec64 *raw_ts); =20 #if defined(CONFIG_GENERIC_CMOS_UPDATE) || defined(CONFIG_RTC_SYSTOHC) --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2586,7 +2586,7 @@ int do_adjtimex(struct __kernel_timex *t } =20 orig_tai =3D tai =3D tks->tai_offset; - ret =3D __do_adjtimex(tks->id, txc, &ts, &tai, &ad); + ret =3D ntp_adjtimex(tks->id, txc, &ts, &tai, &ad); =20 if (tai !=3D orig_tai) { __timekeeping_set_tai_offset(tks, tai); From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BAE4626D4E0; Mon, 19 May 2025 08:33:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643608; cv=none; b=FyvSsKEs9xR9SXC7+m1q2r71pZAYngoiV1NaKrBezvD4mPvMkCQ6PEglgtPMw8yvDrjDAtr/u6UnSLnxkOE2JjlYOmv+9IzSRpnqOyzwij3gcFWbqfIoZVyARai6eVweQszruhNcAdfN6SPOKFktMbzYhLsEX+BdtwdQU3iNtSk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643608; c=relaxed/simple; bh=GioWYcEPVBLBlgv0bwyNh9fZ/8oRyz0T+6HGCQdNxag=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=kP8SyH2gG6GmTLxIHYoamNzt2UtZPSot75AljNmqenMtIj8uThQL2DmkjCkZGShcAV2htbCekm+CSot5q3NCNJf1B2IgHGY6HZ96iUzbq9b2YwTFwvIIrt6Z8HehXoCr5hDzCgJuEbPyeZV6WH4ZCKKUMldK4MyHFCUPSj7brew= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=MCgMmD64; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=jYUjOODm; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="MCgMmD64"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="jYUjOODm" Message-ID: <20250519083026.160967312@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643605; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=L59LdIitUfKjSYztV2EKhNf1AwVq0IEXHtjU+QOwZVk=; b=MCgMmD64q2Q/1G3bOPJRGxXbjJyX/YCoAxxHayGxJBXhgOXmEO7VvbgvtV3ZfdsgjZEy0A NKZiAd00rVJh7xs5Sh+1E9gi2HSbyTa0FY1gMwpnSWIiXxpB0xeMP2M+nHQ3gaKho5QCkN kr+13FUpbvM4LkPCRDMFp8TaKRdR+3zJk5+FdStHbcxTzWrdJE5QKqdZqvyl+4yx9DqzCM cG8/eh90tBXe1r/4RWtX8lzaoFuSRs2j8XJnkjo08XQIgY/swaswgo4Q+S6ovXRQSBmFv2 N4riTyspDl9wQhokgmP3Qi7TgHCOPlEhRTxw0Krp12DYdqRSqk9HafNYdFB3qw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643605; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=L59LdIitUfKjSYztV2EKhNf1AwVq0IEXHtjU+QOwZVk=; b=jYUjOODm3rTJVmZ1tRUpDt7jzlJHhQzdN87AsLbAvAKJ7WW45hERx/FfXRCE5Hg13kU6nD mNXu7brEbp5wJjDQ== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 09/26] timekeeping: Make __timekeeping_advance() reusable References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:25 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Anna-Maria Behnsen In __timekeeping_advance() the pointer to struct tk_data is hardcoded by the use of &tk_core. As long as there is only a single timekeeper (tk_core), this is not a problem. But when __timekeeping_advance() will be reused for per auxiliary timekeepers, __timekeeping_advance() needs to be generalized. Add a pointer to struct tk_data as function argument of __timekeeping_advance() and adapt all call sites. Signed-off-by: Anna-Maria Behnsen Signed-off-by: Thomas Gleixner Acked-by: John Stultz --- kernel/time/timekeeping.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2196,10 +2196,10 @@ static u64 logarithmic_accumulation(stru * timekeeping_advance - Updates the timekeeper to the current time and * current NTP tick length */ -static bool __timekeeping_advance(enum timekeeping_adv_mode mode) +static bool __timekeeping_advance(struct tk_data *tkd, enum timekeeping_ad= v_mode mode) { - struct timekeeper *tk =3D &tk_core.shadow_timekeeper; - struct timekeeper *real_tk =3D &tk_core.timekeeper; + struct timekeeper *tk =3D &tkd->shadow_timekeeper; + struct timekeeper *real_tk =3D &tkd->timekeeper; unsigned int clock_set =3D 0; int shift =3D 0, maxshift; u64 offset, orig_offset; @@ -2252,7 +2252,7 @@ static bool __timekeeping_advance(enum t if (orig_offset !=3D offset) tk_update_coarse_nsecs(tk); =20 - timekeeping_update_from_shadow(&tk_core, clock_set); + timekeeping_update_from_shadow(tkd, clock_set); =20 return !!clock_set; } @@ -2260,7 +2260,7 @@ static bool __timekeeping_advance(enum t static bool timekeeping_advance(enum timekeeping_adv_mode mode) { guard(raw_spinlock_irqsave)(&tk_core.lock); - return __timekeeping_advance(mode); + return __timekeeping_advance(&tk_core, mode); } =20 /** @@ -2598,7 +2598,7 @@ int do_adjtimex(struct __kernel_timex *t =20 /* Update the multiplier immediately if frequency was set directly */ if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK)) - clock_set |=3D __timekeeping_advance(TK_ADV_FREQ); + clock_set |=3D __timekeeping_advance(&tk_core, TK_ADV_FREQ); } =20 if (txc->modes & ADJ_SETOFFSET) From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 57CEA26C398; Mon, 19 May 2025 08:33:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643609; cv=none; b=gRBo1PvDVxhvas5OppnBCOZDNnhT28aJ2q6x95ZeE2gpqKthWWrzypGR6gbKqTCZF6f+4sD8RMRGWlzO82hzlw2XJIXGE0X3W5QlHnag4maPomuXzBdZhgh0pzNDIC1TFCuT3+9VCcMIZM4EY9wByFwBj/bhd4M1K+TLDplJRc4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643609; c=relaxed/simple; bh=sxnsxo8NePjVpwqKQXtJgvbXVeOovTkoNLs8rQJcnsE=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=BjVGTsVOUdyJrncHqJQXSrlsm+PwHq9R11tErlnDqF4T0TLPCNBOYhTincY3fjrTLC8QF2/pPFYcyWPxiptG3wclUjBzJVHZ/K7M3HaUzw4REFtLL26VmvwNSOLzWiTPqvUnh8vF1LKe59SS3jsY7WuA/c1aNHbxL6YPQitlWz8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=TWRTwa77; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=JjeeJbY9; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="TWRTwa77"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="JjeeJbY9" Message-ID: <20250519083026.223876435@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643606; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=HuVmpPi2kJmcidRqbYSEzF2Cc0Jdkoji3gG8ZP/9JpY=; b=TWRTwa77HcGPi/OVVF/nDaWEUaHWVqQYGsccHIEKo7QsMFXGHUHUtTv44fSd4+ycBNtLWj JacNpeU8LON6eTvv3JiLUQhTsF9mmBgzTkKeWdBz59GSavAxA55s7MjikyXefD+1Y+jhca kgu6QS2a+sZDk6ip/z2KalS8VrqDeNWbEaPLX8M2aCZZIFZ3T7CZvToh0SEDw58I7XJ7KS wW2FYnObnAAqfnRzNzo+A6jqC+TnIYtDGKuowll7TnjffEOle5y/bziBHl7cinfDyn/h9+ WRItoFfjcidmpaejQSHjdaOHDg+H3RCe/cYyfyQdsN/96qyFhGhFiuys7s+5KQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643606; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=HuVmpPi2kJmcidRqbYSEzF2Cc0Jdkoji3gG8ZP/9JpY=; b=JjeeJbY9U12r1troW7dSdwJRNiPbutfeVHqW1M0xITz+lu4HjTdtA93sIMA4xr5G6PQkxQ AESsBxENUtRZOKCg== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 10/26] timekeeping: Prepare timekeeping_update_from_shadow() References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:26 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Don't invoke the VDSO and paravirt updates when utilized for auxiliary clocks. This is a temporary workaround until the VDSO and paravirt interfaces have been worked out. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -683,13 +683,15 @@ static void timekeeping_update_from_shad =20 tk_update_leap_state(tk); tk_update_ktime_data(tk); + tk->tkr_mono.base_real =3D tk->tkr_mono.base + tk->offs_real; =20 - update_vsyscall(tk); - update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET); + if (tk->id =3D=3D TIMEKEEPER_CORE) { + update_vsyscall(tk); + update_pvclock_gtod(tk, action & TK_CLOCK_WAS_SET); =20 - tk->tkr_mono.base_real =3D tk->tkr_mono.base + tk->offs_real; - update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono); - update_fast_timekeeper(&tk->tkr_raw, &tk_fast_raw); + update_fast_timekeeper(&tk->tkr_mono, &tk_fast_mono); + update_fast_timekeeper(&tk->tkr_raw, &tk_fast_raw); + } =20 if (action & TK_CLOCK_WAS_SET) tk->clock_was_set_seq++; From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9113526FA50; Mon, 19 May 2025 08:33:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643611; cv=none; b=Uu5ozdwgsDAx1FtEpJFED7kkBCm5kwQhA0JOM2ism7sn7TtbV+m5U4sbQLHz42sXo+L2qFdtOcOKZPlcqEf8liS1chPESJwWcXGwgcXFNd9rEPxqmx/VrGf9CEaKBZ2f57yqZfAzK1BQ2FVmFtpFU6vsaZv31aUTpXCgWoXaomI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643611; c=relaxed/simple; bh=v2CnstdLIXMf6usycAYVnie0z2f9rUAXKX2icLofJLY=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=o4Q9l8lhHBMaYn2VSkSuSw02+JzCEjaf+i9I/EfR1dl8ILFAmM4Kka31d3/rmZ4NhwZVZzHFwvSw6677f54X6sdMuWXU/XIJEWSAOldpvk9I9kkgHcfb/miDZGDezJfWwN6vt5D7F06so2HDx65yPILJYGA42KCyRuJcjvEWxGM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=lQ8VNKyO; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=KVNm7qRF; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="lQ8VNKyO"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="KVNm7qRF" Message-ID: <20250519083026.287145536@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643607; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=KU3LaI1wfhZWx046ayWmUOBaJZ+N4sC2ljBYeM/Rzlk=; b=lQ8VNKyOx9Nyv/C0fuO+6qs24jw7XNfMURf6YaKmKPhFmVvUhfVfVuZ96H3/0KtRNEqoyj lHNufJfcfulrYM1oNxW9ql05gaXlMHrXM0I1U2qwjmQcDt6sr+74CHtXkOXnredgn+j8PN 7IVo02PyYUtSzPcVybBKlOvoPmij0/Kr5qfcnJJolV2F9rs/L9++rT4uH3w5tr8fQidVgf rBQDz30Z/L6J/b4d7SD0uSA0aZiNRYJ1MDXonXyK9+kInVTrZ76ZtwkcFVB7qpxG2I4Kq7 1N7oytuaWLWl/YEw+Ja6pS0axaBJmzbRQ8P737t6hF5XGZ7FrRYsqTdaGvyrIg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643607; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=KU3LaI1wfhZWx046ayWmUOBaJZ+N4sC2ljBYeM/Rzlk=; b=KVNm7qRF+fM81O67uDIUxjCJD1LhOL5OMe6Vw3XVNMKRayg47o8SLvlAExb2hvrPOMtAGN sIaaNX4CbVHajSCw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 11/26] timekeeping: Add clock_valid flag to timekeeper References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:27 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner In preparation for supporting independent auxiliary timekeepers, add a clock valid field and set it to true for the system timekeeper. Signed-off-by: Thomas Gleixner Acked-by: John Stultz --- include/linux/timekeeper_internal.h | 2 ++ kernel/time/timekeeping.c | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) --- --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -73,6 +73,7 @@ struct tk_read_base { * @raw_sec: CLOCK_MONOTONIC_RAW time in seconds * @clock_was_set_seq: The sequence number of clock was set events * @cs_was_changed_seq: The sequence number of clocksource change events + * @clock_valid: Indicator for valid clock * @monotonic_to_boot: CLOCK_MONOTONIC to CLOCK_BOOTTIME offset * @cycle_interval: Number of clock cycles in one NTP interval * @xtime_interval: Number of clock shifted nano seconds in one NTP @@ -149,6 +150,7 @@ struct timekeeper { /* Cachline 3 and 4 (timekeeping internal variables): */ unsigned int clock_was_set_seq; u8 cs_was_changed_seq; + u8 clock_valid; =20 struct timespec64 monotonic_to_boot; =20 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1665,11 +1665,12 @@ read_persistent_wall_and_boot_offset(str *boot_offset =3D ns_to_timespec64(local_clock()); } =20 -static __init void tkd_basic_setup(struct tk_data *tkd, enum timekeeper_id= s tk_id) +static __init void tkd_basic_setup(struct tk_data *tkd, enum timekeeper_id= s tk_id, bool valid) { raw_spin_lock_init(&tkd->lock); seqcount_raw_spinlock_init(&tkd->seq, &tkd->lock); tkd->timekeeper.id =3D tkd->shadow_timekeeper.id =3D tk_id; + tkd->timekeeper.clock_valid =3D tkd->shadow_timekeeper.clock_valid =3D va= lid; } =20 /* @@ -1699,7 +1700,7 @@ void __init timekeeping_init(void) struct timekeeper *tks =3D &tk_core.shadow_timekeeper; struct clocksource *clock; =20 - tkd_basic_setup(&tk_core, TIMEKEEPER_CORE); + tkd_basic_setup(&tk_core, TIMEKEEPER_CORE, true); =20 read_persistent_wall_and_boot_offset(&wall_time, &boot_offset); if (timespec64_valid_settod(&wall_time) && From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D367A26FD8E; Mon, 19 May 2025 08:33:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643612; cv=none; b=VN+IoTtCvHpRc/aoQknrcTr5Ry2I6nf1pMXuIaRL1ZJGMDpNHbjHVTvQXg6bNhl0XYZZMy3SYd/1pXicFxuUfu7KxckX3P3SJaI3ctyFWSet+m1YTPTQUE7PSahtYql7TE9fzqTUYo7CfjxJsART3T2dviqd3U0MU736120eqU4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643612; c=relaxed/simple; bh=YUzu3k5X/EMVbk9mZye4LenY4n2dKppugU0I7/ADlcg=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=IQWXDGj01E36la0IE5SEP+v0dXcsgIdUwDvBgM/PHvQoEB0ELz/8Dm9UeOCELP1BBbrKMYBtEL9/SqfBzqsoKxqMxCr7XsfATqQrLj/xh/VneypP8Dc7pK9ZNgGI3xHMuzD1GJb2Bkk7Q72ymEm+hSLmQJ21mpcxODwZGHCJatw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=c5BPJZjr; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=/dRXXhes; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="c5BPJZjr"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="/dRXXhes" Message-ID: <20250519083026.350061049@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643609; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=/SRQDM/A5IpK3yyd3ilOVzrwfbDRClBN+BY9c73LlaI=; b=c5BPJZjr/m4JBW+rucByllJGl3mYSXES014qyBleX8RBPYOp7ygOFm9d0Y66Wgj//gz4g5 asTWV8NEenXVDFBqQPMpDm6rIi73XjsOAzWPvKitGaoyzCb1UZkQTrlGBA3Eti4aO0fdHL p1SmzIxyvuQwqXdLaV1OSgFYHSN1wifda7ehug1oAc/pN96DFFvH8ooCqr+vUg6nGXY7jp I/B5DhiwejF4mlJeLFrhbhWzbAGP1746jGoRGlkSGP8TILBKRRWOwtjy5JIITfyc7it5Hc 1n7GzfeTCPKgv0wLuf989oz1iEH4rTX6x0mLsw6DtgXkYTR/FyzP8PXAxvMefQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643609; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=/SRQDM/A5IpK3yyd3ilOVzrwfbDRClBN+BY9c73LlaI=; b=/dRXXhesStOgjsrlliity3AFepopBsumjgNq633rxs2bd2NpucQccfeYdajpriN6imooTm jRzPPcliupPI3KDA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 12/26] timekeeping: Introduce auxiliary timekeepers References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:28 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Anna-Maria Behnsen Provide timekeepers for auxiliary clocks and initialize them during boot. Signed-off-by: Anna-Maria Behnsen Signed-off-by: Thomas Gleixner Acked-by: John Stultz --- kernel/time/timekeeping.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -53,7 +53,11 @@ struct tk_data { raw_spinlock_t lock; } ____cacheline_aligned; =20 -static struct tk_data tk_core; +static struct tk_data timekeeper_data[TIMEKEEPERS_MAX]; + +/* The core timekeeper */ +#define tk_core (timekeeper_data[TIMEKEEPER_CORE]) + =20 /* flag for if timekeeping is suspended */ int __read_mostly timekeeping_suspended; @@ -113,6 +117,12 @@ static struct tk_fast tk_fast_raw ____c .base[1] =3D FAST_TK_INIT, }; =20 +#ifdef CONFIG_POSIX_AUX_CLOCKS +static __init void tk_aux_setup(void); +#else +static inline void tk_aux_setup(void) { } +#endif + unsigned long timekeeper_lock_irqsave(void) { unsigned long flags; @@ -1589,7 +1599,6 @@ void ktime_get_raw_ts64(struct timespec6 } EXPORT_SYMBOL(ktime_get_raw_ts64); =20 - /** * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres */ @@ -1701,6 +1710,7 @@ void __init timekeeping_init(void) struct clocksource *clock; =20 tkd_basic_setup(&tk_core, TIMEKEEPER_CORE, true); + tk_aux_setup(); =20 read_persistent_wall_and_boot_offset(&wall_time, &boot_offset); if (timespec64_valid_settod(&wall_time) && @@ -2630,3 +2640,11 @@ void hardpps(const struct timespec64 *ph } EXPORT_SYMBOL(hardpps); #endif /* CONFIG_NTP_PPS */ + +#ifdef CONFIG_POSIX_AUX_CLOCKS +static __init void tk_aux_setup(void) +{ + for (int i =3D TIMEKEEPER_AUX; i <=3D TIMEKEEPER_AUX_LAST; i++) + tkd_basic_setup(&timekeeper_data[i], i, false); +} +#endif /* CONFIG_POSIX_AUX_CLOCKS */ From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1189E2701D4; Mon, 19 May 2025 08:33:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643614; cv=none; b=N9FbtUN+QDUirGa0DQfiZHJ9lZc+6NnYte5RKrH3aHfNzkFmKRjjUxnnxdevrX4gDG2TGJ0zCaFynIEYkEYZ7nrDQUQqip7kVYCYeTdk9x+tFFGpa90wWjKLAi7sxZ6S6Uf9UMkuTL1+jDQQBZBWKf1nA/Y+jev4dfM3Ne1InZA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643614; c=relaxed/simple; bh=FM9ELVeqz4TUsKpyJHGSgjwI5KFnezd+C6Mvdegl1pE=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Mun/nXjYsMEsogU5jeBDuoakyA/nkbbN5+s1wy7eR0olfQU7vaLDhVzf1DB2r1zlM0BxRhm0Gm4xSLa0rwpJ0C0Jlz2ZtDqwrK6ULmSTnTumge8f7hBc5dWJmz8q/PH+o3NivNkRfT6lZ4BKrlmZGEowt3OwwWMzXm+JXmy234M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=jToyYKZv; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=AISqXRdl; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="jToyYKZv"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="AISqXRdl" Message-ID: <20250519083026.411809421@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643610; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=1v1pAT49VMlkOWBDIvngj4OT88UyCqJmnvYZ4oJqhv4=; b=jToyYKZvwkC3JcQOhodrq7x/nVjuIB9mBdYySunQp0g1n5qX26/crAoA5zXYHEmdjwvUVn eajKLGOK2pYbrlY0xr2ucM/MqO4EdznL9sRpNWLAIB1p8XzI2E4rLHgD/X6j2FQhmH2TlF ZYXUxWTVSA3C4MpGMMAFw9FAmnXkQgsBB1lc6QMEGKLlFnLI2cWX9kWWf8Z2qmvS4M73R5 bRbaiY/EXL2kI5zjDSHYeLjCPRdU3P441Cm6TJcc/jKlczRrcftpA6W7WfClEb0Vj8YEif WgSCtRg/flT0MFpAYXvgDvCGazEXQGo8e0mWrQrKYy3WSGMiG2a3357AlXRZrg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643610; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=1v1pAT49VMlkOWBDIvngj4OT88UyCqJmnvYZ4oJqhv4=; b=AISqXRdlhD/0o0O59uRlbj36ghoiLIyq+rL/YHfoLNNc8vjLsKt/NNssM2uLaPjWmNo5p8 ogWD7EKVf6DCt+CQ== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 13/26] timekeeping: Provide ktime_get_ntp_seconds() References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:30 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" ntp_adjtimex() requires access to the actual time keeper per timekeeper ID. Provide an interface. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 9 +++++++++ kernel/time/timekeeping_internal.h | 3 +++ 2 files changed, 12 insertions(+) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2627,6 +2627,15 @@ int do_adjtimex(struct __kernel_timex *t return ret; } =20 +/* + * Invoked from NTP with the time keeper lock held, so lockless access is + * fine. + */ +long ktime_get_ntp_seconds(unsigned int id) +{ + return timekeeper_data[id].timekeeper.xtime_sec; +} + #ifdef CONFIG_NTP_PPS /** * hardpps() - Accessor function to NTP __hardpps function --- a/kernel/time/timekeeping_internal.h +++ b/kernel/time/timekeeping_internal.h @@ -45,4 +45,7 @@ static inline u64 clocksource_delta(u64 unsigned long timekeeper_lock_irqsave(void); void timekeeper_unlock_irqrestore(unsigned long flags); =20 +/* NTP specific interface to access the current seconds value */ +long ktime_get_ntp_seconds(unsigned int id); + #endif /* _TIMEKEEPING_INTERNAL_H */ From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E6CE22701CF; Mon, 19 May 2025 08:33:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643614; cv=none; b=Q0UaVytRM7269C7CU9fzc4A8hlmi0H9uHj0zAvNN34ilvJtnzTuE/+HIapBWOD4OX48MDGxPvsl/MbXMIFv2SlKs+OBinjyCpyWqa+NyVhRvijwuu0xaRkri5MvGYQBtso0JMPNCxTIw/w90ECf9w/Mc/LJ3H8ijR8DsxHvGsmo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643614; c=relaxed/simple; bh=PFTKjntnYzQGNP+r1AaA3SG71QP/EDqas/WFTRwWtu8=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=TsXUgg47NWrSt8Tzv/IAgUCVcc4dLGD54yjnjhSY5oIYRAfBxZ7C6C+oDxa9nE9vti85DXMgXM0swj3tWjv2kj1/KUf/KV9mAA9ZdSpfLt/W1/L8DWd9q9Z6GKHFQZZPVrfNGCX54zrYcYwrBAGIcu7jcyz0eh3h5tdJBFE9Y1A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=jyCJknbi; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=D8Ck+jKI; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="jyCJknbi"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="D8Ck+jKI" Message-ID: <20250519083026.472512636@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643611; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=9QsTzgHBJ42Ub04898XgpgHcyA6q4XFW9DFcftyvhXU=; b=jyCJknbipGhq0WYF9kbQEZInAVrWqqTybWVxaxUU3++la/wJ7sLxe9iCSiFq7EydiMz41r oSw4ecJ9boJMsAUPUtvODZM/H8OvxH5iysRAZ+JLk3nPTFzUuW+glCsJZ6PUcEpZCs1teO qD1RG1gboLnDkVGHgYX69QwTW3VBJevyK+zN5avbf6ubKIZxfvUAfuNG9ZfZNrNmVPR4Hv TvOvZJftLxvfXi38HnnMNfcsWSGEo0O2uHVHgpW/c5ApfSpkGWL8jGzYq2KrpqkmbbBMg7 RD59CH20Usep82I0Fkozxd/mUSA8h26bkKHoo7hAFK2CYWzZw0NXDNYcT7NDnw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643611; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=9QsTzgHBJ42Ub04898XgpgHcyA6q4XFW9DFcftyvhXU=; b=D8Ck+jKIrRW+Vf1dowxd3rPU0L3o/MzezbyxFwVtp+OskRxAnezubNYVH4CPPmW3JBQgD4 OZ7Mk7aeqUIKXVDQ== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 14/26] ntp: Use ktime_get_ntp_seconds() References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:31 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use ktime_get_ntp_seconds() to prepare for auxiliary clocks so that the readout becomes per timekeeper. Signed-off-by: Thomas Gleixner --- kernel/time/ntp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- --- a/kernel/time/ntp.c +++ b/kernel/time/ntp.c @@ -303,7 +303,7 @@ static void ntp_update_offset(struct ntp * Select how the frequency is to be controlled * and in which mode (PLL or FLL). */ - real_secs =3D __ktime_get_real_seconds(); + real_secs =3D ktime_get_ntp_seconds(ntpdata - tk_ntp_data); secs =3D (long)(real_secs - ntpdata->time_reftime); if (unlikely(ntpdata->time_status & STA_FREQHOLD)) secs =3D 0; @@ -710,7 +710,7 @@ static inline void process_adj_status(st * reference time to current time. */ if (!(ntpdata->time_status & STA_PLL) && (txc->status & STA_PLL)) - ntpdata->time_reftime =3D __ktime_get_real_seconds(); + ntpdata->time_reftime =3D ktime_get_ntp_seconds(ntpdata - tk_ntp_data); =20 /* only set allowed bits */ ntpdata->time_status &=3D STA_RONLY; From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E05C27054E; Mon, 19 May 2025 08:33:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643615; cv=none; b=AosxhD0xzFJzPLDZkRTLzSzRCgRsIwqUKlNSMDvuzck/bKoLXjDGwXsNWT0ZZI2DF+XcrGp627Dz6/ErZKQCB8zOizrk7bqKi8ouUI+iKuPAzuq5UorvBCmkGQN1iJ9cZq38JkAqRzRtTDz0VGfEfyoMBJ5npEYmwtpz0T3EYyY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643615; c=relaxed/simple; bh=imEIHk/EPAtoQkxPLF4zSpQlLZtLGmQXx7lY6KkYsEY=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=LKsO2tiBTmxQGYDzyVQmHmYd2608rtNeEMIJgOolb1+UIjIf75Sr5aDYKozsfifiRcv0e2nCa4V+ehFFvyjTglv0px+vT5IHDeHM+W2kf+e1cnCO84wbZW6Lv72zdn8CHHHbRJvJC1TVBtDDDk7w6c5mRYzo1eSJv6jqr9mzISk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=wmV0e4Uh; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=dXYVZJ+b; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="wmV0e4Uh"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="dXYVZJ+b" Message-ID: <20250519083026.533486349@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643612; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=rlC+rFNEoM7UPzOHkW+/tvvtNAKD9S1u+ELe68/TTuI=; b=wmV0e4Uh9fdijt6SSytVnFzVYRLeMfY6fDOwVnB7OcGnt8BbF6yN8CC7EVB3OwiQG3F5xP 0AuZQbRCLJpq1rxIb2cxNXzsXnxTeyEiHq+KjZ1yWj/tfsi2kx9FUvm/88UtB6XJScxd4e BRfcUTnMJVXbftXsCSuNGt+jHbCdgWQu3j5yYHzmKLgxu+HhkbR5ITriUzdckg4d3RhE3i XuhabMW/aMZjSikfwvvohBPBc+xa6g3q0UT0jtdaB2ZsFZbwS+1siCcLI9ip8B9vazhWvp 43kf8z+KrCTKriOkLT/UMf2BSLZcWmfHF2ljbXMtTk5veD6+QPNqQMYsaUbYtA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643612; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=rlC+rFNEoM7UPzOHkW+/tvvtNAKD9S1u+ELe68/TTuI=; b=dXYVZJ+bTydoz5XwCxVi2vZFt7ahUJig9uUd3rUe4XD/qFe6f8ZMjMNseeQv3Cz9QIwc1u gPpHz3nlr6JWSWAw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 15/26] timekeeping: Add AUX offset to struct timekeeper References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:32 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This offset will be used in the time getters of auxiliary clocks. It is added to the "monotonic" clock readout. As auxiliary clocks do not utilize the offset fields of the core time keeper, this is just an alias for offs_tai, so that the cache line layout stays the same. Signed-off-by: Thomas Gleixner --- include/linux/timekeeper_internal.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) --- --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -67,6 +67,7 @@ struct tk_read_base { * @offs_real: Offset clock monotonic -> clock realtime * @offs_boot: Offset clock monotonic -> clock boottime * @offs_tai: Offset clock monotonic -> clock tai + * @offs_aux: Offset clock monotonic -> clock AUX * @coarse_nsec: The nanoseconds part for coarse time getters * @id: The timekeeper ID * @tkr_raw: The readout base structure for CLOCK_MONOTONIC_RAW @@ -139,7 +140,10 @@ struct timekeeper { struct timespec64 wall_to_monotonic; ktime_t offs_real; ktime_t offs_boot; - ktime_t offs_tai; + union { + ktime_t offs_tai; + ktime_t offs_aux; + }; u32 coarse_nsec; enum timekeeper_ids id; From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6759E270EAC; Mon, 19 May 2025 08:33:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643616; cv=none; b=DDNag+ahCBVCeDVs9TZgog0fGDqNDlWBonQP2y0Jg8ZmQ5zvaRKHqDVEjd/fXzjbtvFxIE9CkgsisCiGgi+aSdD0Vdh9xz+6ewWeonwqcspw5XpLGeVHnoctJwXHt7KqHpw+iBZIspbBYp9Vevg7mIGUMHf9RV/g/b9YZyvGsik= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643616; c=relaxed/simple; bh=T65jdGNGXmtGGOUz33OVaG2sLwQXgcw4amezguV8uno=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=Wi3BHGktrF1w8SzLZ2Eknrr1M8XMgr2z6cX3CqLG5ISL/Wuq8wGuxZ3fE4MRCcDu7Fme0TIWam3E9MCEETLWY8A6cti2dgaYoKrJC0JpqZIZNqT304f0PtbiqJRDFhofaEtvHkA8bI/NNoDXFJe/UpSS/rG0MRnWR9GFgfbCj08= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=J2vL4TTp; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=nHv/QDiy; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="J2vL4TTp"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="nHv/QDiy" Message-ID: <20250519083026.594820760@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643614; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=yjQaFwdaDWpoRr2nUd9ek6VPNntBmypVCgJfP9Yy+oE=; b=J2vL4TTpCXrbQKebRBKYDVM8auCSWgsulFyNaaREeAeCADoGgqX9zevvy0xHZPfJq525i2 B2jEh2ov5tphqOAS7BxVXqNfMlNA87t4DdyJl1WHCcticae/6kadKMPGsEPGz6Iftm2H0G UhuQQcFRm8b/vdMybxY7t7FH7+6AFucWOJw97wlcdYVq3xcyTCAovSPKlBduMDi1RcuEwv D7PyKYqcEJJw1gxrVW1c0t+P7Vh+sgNvRsX2Ms+5dyaMaKBuyA1/bLH5o8VkHz+3aOoUwc Kpo1kZWyX0i3SSPwB+zmiR4l+WjjV/vWdluWahmBvbEEjuSCb+gjI1BtrKL6GQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643614; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=yjQaFwdaDWpoRr2nUd9ek6VPNntBmypVCgJfP9Yy+oE=; b=nHv/QDiynJCZgeKZePdVc/U0d5EE3NBxCuwXDa2BTlfcQManZZAAsoHYZKZvv7geiYMEH6 i9qhdRunDEO0nIAw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 16/26] timekeeping: Update auxiliary timekeepers on clocksource change References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:33 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Propagate a system clocksource change to the auxiliary timekeepers so that they can pick up the new clocksource. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -119,8 +119,10 @@ static struct tk_fast tk_fast_raw ____c =20 #ifdef CONFIG_POSIX_AUX_CLOCKS static __init void tk_aux_setup(void); +static void tk_aux_update_clocksource(void); #else static inline void tk_aux_setup(void) { } +static inline void tk_aux_update_clocksource(void) { } #endif =20 unsigned long timekeeper_lock_irqsave(void) @@ -1548,6 +1550,8 @@ static int change_clocksource(void *data timekeeping_update_from_shadow(&tk_core, TK_UPDATE_ALL); } =20 + tk_aux_update_clocksource(); + if (old) { if (old->disable) old->disable(old); @@ -2651,6 +2655,30 @@ EXPORT_SYMBOL(hardpps); #endif /* CONFIG_NTP_PPS */ =20 #ifdef CONFIG_POSIX_AUX_CLOCKS + +/* Bitmap for the activated auxiliary timekeepers */ +static unsigned long aux_timekeepers; + +/* Invoked from timekeeping after a clocksource change */ +static void tk_aux_update_clocksource(void) +{ + unsigned long active =3D READ_ONCE(aux_timekeepers); + unsigned int id; + + for_each_set_bit(id, &active, BITS_PER_LONG) { + struct tk_data *tkd =3D &timekeeper_data[id + TIMEKEEPER_AUX]; + struct timekeeper *tks =3D &tkd->shadow_timekeeper; + + guard(raw_spinlock_irqsave)(&tkd->lock); + if (!tks->clock_valid) + continue; + + timekeeping_forward_now(tks); + tk_setup_internals(tks, tk_core.timekeeper.tkr_mono.clock); + timekeeping_update_from_shadow(tkd, TK_UPDATE_ALL); + } +} + static __init void tk_aux_setup(void) { for (int i =3D TIMEKEEPER_AUX; i <=3D TIMEKEEPER_AUX_LAST; i++) From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8EC6F2741CA; Mon, 19 May 2025 08:33:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643620; cv=none; b=neWqpnZAeA7vN2IUWW48Tl04Yz8uFlZ2vmtfS071j+AvN7vHdGneJxd/0Wp1PYg8OVq+XzwTRi/EDbgLH5pIEI6q42wDJVQ22Nue7s4HgtvRXH2l2UX/xDnw4BQ3adj2X1LWsTkD0Mb40Ie3kxpOzL6JRMVXenOCghL0P0Ds4vY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643620; c=relaxed/simple; bh=no92kLIwjLeFJ2b8otG0qvCk3qoDGVcDj7yfnjmkOKI=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=jhdA0Fl5RoX65gRk1+mBAQw4uXt+NxcVqITozAwM771Q4x2tNK2Ov166H8fJCfSwrYfGTPdeeBDGM9To48HF4d6b3FRPE25IKg4eayU8GZjaz5gg6cgXsxN+Ri7jdtEjsk2MviVqctBTE/tzhnzbfOPUjCV9ITNhOWdN3NLXWY8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=u1CwmAzO; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=UzFioUAo; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="u1CwmAzO"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="UzFioUAo" Message-ID: <20250519083026.655171665@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643615; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=XpdXKR0LjhP1+em4VmwJU235Z2KZZt3yqEw6zu3whRs=; b=u1CwmAzOg2e3lvf2iCSLEEM8afj26YyCdFVAOaigpKBVFDgXh+BtUndR9+gGliQEtZXKKB wunLW5raf8oeaXxGVkkABSETDqCYK9s+DA3MK4+Pg2rgvEuHsapa4fqZjdVmmAQqyM81Tu IfE1cGqjwhm9kb3nYysoNg4fB5HLb1RtaUil3fWHV5LEaLPyTllaRF3bFI37LQ6kMDftdI 3qXTh5TvN5mzHYMHgC0z2dYPoUVGROEQ7B/RdNfYzwCGTJPA0UADAKli/Lxd16mBVjV9p+ WXgTYOg3dthk8VaSXEzcuBgfnD2bt5j5K6fr564S/NiYLoNCdiftV511B2xCzw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643615; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=XpdXKR0LjhP1+em4VmwJU235Z2KZZt3yqEw6zu3whRs=; b=UzFioUAoqne1+tTxgWGBtCg/5TuWdSq0ZvH2cQ4bEjRq888SocJhR0fHnK4/zSPI4vfjOl yUN44YxWfPu3x5Aw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 17/26] timekeeping: Provide time getters for auxiliary clocks References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:35 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Provide interfaces similar to the ktime_get*() family which provide access to the auxiliary clocks. These interfaces have a boolean return value, which indicates whether the accessed clock is valid or not. Signed-off-by: Thomas Gleixner --- include/linux/timekeeping.h | 11 +++++++ kernel/time/timekeeping.c | 62 +++++++++++++++++++++++++++++++++++++++= +++++ 2 files changed, 73 insertions(+) --- --- a/include/linux/timekeeping.h +++ b/include/linux/timekeeping.h @@ -263,6 +263,17 @@ extern bool timekeeping_rtc_skipresume(v =20 extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta); =20 +/* + * Auxiliary clock interfaces + */ +#ifdef CONFIG_POSIX_AUX_CLOCKS +extern bool ktime_get_aux(clockid_t id, ktime_t *kt); +extern bool ktime_get_aux_ts64(clockid_t id, struct timespec64 *kt); +#else +static inline bool ktime_get_aux(clockid_t id, ktime_t *kt) { return false= ; } +static inline bool ktime_get_aux_ts64(clockid_t id, struct timespec64 *kt)= { return false; } +#endif + /** * struct system_time_snapshot - simultaneous raw/real time capture with * counter value --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2659,6 +2659,23 @@ EXPORT_SYMBOL(hardpps); /* Bitmap for the activated auxiliary timekeepers */ static unsigned long aux_timekeepers; =20 +static inline bool aux_valid_clockid(clockid_t id) +{ + return id >=3D CLOCK_AUX && id <=3D CLOCK_AUX_LAST; +} + +static inline unsigned int clockid_to_tkid(unsigned int id) +{ + return TIMEKEEPER_AUX + id - CLOCK_AUX; +} + +static inline struct tk_data *aux_get_tk_data(clockid_t id) +{ + if (!aux_valid_clockid(id)) + return NULL; + return &timekeeper_data[clockid_to_tkid(id)]; +} + /* Invoked from timekeeping after a clocksource change */ static void tk_aux_update_clocksource(void) { @@ -2679,6 +2696,51 @@ static void tk_aux_update_clocksource(vo } } =20 +/** + * ktime_get_aux - Get TAI time for a AUX clock + * @id: ID of the clock to read (CLOCK_AUX...) + * @kt: Pointer to ktime_t to store the time stamp + * + * Returns: True if the timestamp is valid, false otherwise + */ +bool ktime_get_aux(clockid_t id, ktime_t *kt) +{ + struct tk_data *tkd =3D aux_get_tk_data(id); + struct timekeeper *tk; + unsigned int seq; + ktime_t base; + u64 nsecs; + + WARN_ON(timekeeping_suspended); + + if (!tkd) + return false; + + tk =3D &tkd->timekeeper; + do { + seq =3D read_seqcount_begin(&tkd->seq); + if (!tk->clock_valid) + return false; + + base =3D ktime_add(tk->tkr_mono.base, tk->offs_aux); + nsecs =3D timekeeping_get_ns(&tk->tkr_mono); + } while (read_seqcount_retry(&tkd->seq, seq)); + + *kt =3D ktime_add_ns(base, nsecs); + return true; +} +EXPORT_SYMBOL_GPL(ktime_get_aux); + +bool ktime_get_aux_ts64(clockid_t id, struct timespec64 *ts) +{ + ktime_t now; + + if (!ktime_get_aux(id, &now)) + return false; + *ts =3D ktime_to_timespec64(now); + return true; +} + static __init void tk_aux_setup(void) { for (int i =3D TIMEKEEPER_AUX; i <=3D TIMEKEEPER_AUX_LAST; i++) From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2AD9326A0B7; Mon, 19 May 2025 08:33:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643619; cv=none; b=VlmqPIPMJ3K1YowA+I5D3N/yB7m7M5SUopOirg3MbExkKur4SBk/sssAVHsnZ+sGXTgcQnDX3PnL/vafMXUgQSOJ2b4H6+KFuFdIKOso/yF1YlfI0Jcom/Fv80N/5umnTR8f3ege5MMa2w/T9W1Ztr3vvKYdisTX0nIm7LrZJf4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643619; c=relaxed/simple; bh=P+IAhiMIcgpF3Jrm9ck9UV2UsqNIxRlBV8q+5p3M7LM=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=sMRhcrgx3Gxt4HgdxfJx2DVa/E2qMc0NVY4bn5UoPOSWDL+98eDeVvZR9qJVRUwD8+/1ecWNtXlPvc/pscGVEe6xfd/BEbXRKdsAs7HQISMlkNRboFBRl9ihBTPbKyPyQaCL4sHsBuvIeRjUsDFz9UGl3JI750svWftuKHcYQQI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=JlkUpmjm; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ufBKYnxt; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="JlkUpmjm"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ufBKYnxt" Message-ID: <20250519083026.717614086@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643616; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Uq204vGdukuNtGTu4sXgHo4KBp09iA6KFw5f5y1ADAc=; b=JlkUpmjm15g3pRItJu1gVrEBCVdq33V9xgO3wubJjdISUCNqy3YEkda+DHYUbg0eklf/si BpFDMLDFLcHI5BQrPtXCG2PIY6RMthY2LhFt6gWarMojQLYsisNy8ktCdnhyzEHQIDVCTK nvQy2V7z8XDnfm5ki4uPamgHXCKMD7ueWgivHoyS3sHHJVo6xWRosPvWrOUR6Pwwt2AQBQ MikEU/OA6c/R0VyjZkhmbHu7K+UqHgKPkqhMDZu1c2zbYCeaVoQFrvPbj95CS/80d81IS8 mh/p6aU2JxYLH/tJzAG2AE3wOL0mnghMs6yXEjx4WaGgHBmV7u5W/ktUsWxQMg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643616; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=Uq204vGdukuNtGTu4sXgHo4KBp09iA6KFw5f5y1ADAc=; b=ufBKYnxtRZ4RLJxeZ5myBjd7KWfGZ3K/52hf2QTuAsZ/d5fAfnfE5bOtKRSNaEXXHrRH8K oMEoaFqtAEvoFNAw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 18/26] timekeeping: Add minimal posix-timers support for auxiliary clocks References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:36 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Provide clock_getres(2) and clock_gettime(2) for auxiliary clocks. Signed-off-by: Thomas Gleixner --- kernel/time/posix-timers.c | 3 +++ kernel/time/posix-timers.h | 1 + kernel/time/timekeeping.c | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+) --- --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -1526,6 +1526,9 @@ static const struct k_clock * const posi [CLOCK_REALTIME_ALARM] =3D &alarm_clock, [CLOCK_BOOTTIME_ALARM] =3D &alarm_clock, [CLOCK_TAI] =3D &clock_tai, +#ifdef CONFIG_POSIX_AUX_CLOCKS + [CLOCK_AUX ... CLOCK_AUX_LAST] =3D &clock_aux, +#endif }; =20 static const struct k_clock *clockid_to_kclock(const clockid_t id) --- a/kernel/time/posix-timers.h +++ b/kernel/time/posix-timers.h @@ -41,6 +41,7 @@ extern const struct k_clock clock_posix_ extern const struct k_clock clock_process; extern const struct k_clock clock_thread; extern const struct k_clock alarm_clock; +extern const struct k_clock clock_aux; =20 void posix_timer_queue_signal(struct k_itimer *timr); =20 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2655,6 +2655,7 @@ EXPORT_SYMBOL(hardpps); #endif /* CONFIG_NTP_PPS */ =20 #ifdef CONFIG_POSIX_AUX_CLOCKS +#include "posix-timers.h" =20 /* Bitmap for the activated auxiliary timekeepers */ static unsigned long aux_timekeepers; @@ -2741,6 +2742,26 @@ bool ktime_get_aux_ts64(clockid_t id, st return true; } =20 +static int aux_get_res(clockid_t id, struct timespec64 *tp) +{ + if (!aux_valid_clockid(id)) + return -ENODEV; + + tp->tv_sec =3D 0; + tp->tv_nsec =3D 1; + return 0; +} + +static int aux_get_timespec(clockid_t id, struct timespec64 *tp) +{ + return ktime_get_aux_ts64(id, tp) ? 0 : -ENODEV; +} + +const struct k_clock clock_aux =3D { + .clock_getres =3D aux_get_res, + .clock_get_timespec =3D aux_get_timespec, +}; + static __init void tk_aux_setup(void) { for (int i =3D TIMEKEEPER_AUX; i <=3D TIMEKEEPER_AUX_LAST; i++) From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7FABE2741D0; Mon, 19 May 2025 08:33:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643621; cv=none; b=JvGwUK2X1xS77+L00IyPPnRWYfAkvf9ByRT/g0UsBFIQYhaS8h/in2bQWOHOKndmt0l4vl/A9BE4109Cf3g2SMoOaslcxsvI8TjEyqqN20njToksrwHMGaYxgKxeojzEy8MKenRgNVNwRcNhZtisxnccGrNoelrOTGRVcTdLILE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643621; c=relaxed/simple; bh=wcet+KQ7mfGuaicX90+NDM/XbprRB5yDlwpHaOex4Es=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=mNm4NW6mpBIex6682JBGtMsYroo5i9j+EZXQd3gam7GDZhVzU8HLBA73feqNwkN+rqi71plj8AYzMyAnI4TQkG8u1og4r7nDw/2wlL2Hrt+o96V7e9VlqbClMRLFcdwUwfaP5eLeigSHNPenuaaxK+j3ybNLkjweSsvdUHkKpCQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=QzuyXGJH; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ScG57x8M; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="QzuyXGJH"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ScG57x8M" Message-ID: <20250519083026.781170326@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643617; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=oEIEdaPxk47WUwPdYP4TPLC5Q2UNTcvOI//fl8tbabw=; b=QzuyXGJHLdBJh0i2oIW+km+kTYnCMcJbHWQ0qcmREAHYpBKjgtxgYOxlpJy0GEV0nMGRzR 2yZzEH/Gg7H8rm+NapIMEEMUYOQHtTKT48dmY4jERq+pL/3kvG0hCueLulmWeheKx3aXDn EP69OMIiedaz55Y7PJblUtlxZnf8k6yXum0DwPzPBAqJi+XZ4ehGYsLxwh/L46/9WWm603 VbYVgLlgzSRqjA6265fJNTW5Hy8sFXOcx0Ev7SjxEQ0OuPE3Wtj3JSwluA8XE6Ye+Nq+fw lgGGkhIOBXfiKuvVX3XbChzHhCLVPnufYD4uuLvdzJFTC9v4D3e1Ab2dbiWzyQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643617; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=oEIEdaPxk47WUwPdYP4TPLC5Q2UNTcvOI//fl8tbabw=; b=ScG57x8Mw4ut+O8DN2G7623GJbptNOuYrYnZFTAZVjMzsctNBIY/vfcNGkftPw/EswiWOD MgAC2i4EzCNCWDDg== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 19/26] timekeeping: Provide time setter for auxiliary clocks References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:37 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add clock_settime(2) support for auxiliary clocks. The function affects the AUX offset which is added to the "monotonic" clock readout of these clocks. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2757,9 +2757,48 @@ static int aux_get_timespec(clockid_t id return ktime_get_aux_ts64(id, tp) ? 0 : -ENODEV; } =20 +static int aux_clock_set(const clockid_t id, const struct timespec64 *tnew) +{ + struct tk_data *tkd =3D aux_get_tk_data(id); + struct timekeeper *tks; + ktime_t tnow, nsecs; + + if (!timespec64_valid_settod(tnew)) + return -EINVAL; + if (!tkd) + return -ENODEV; + + tks =3D &tkd->shadow_timekeeper; + + guard(raw_spinlock_irq)(&tkd->lock); + if (!tks->clock_valid) + return -ENODEV; + + /* Forward the timekeeper base time */ + timekeeping_forward_now(tks); + /* + * Get the updated base time. tkr_mono.base has not been + * updated yet, so do that first. That makes the update + * in timekeeping_update_from_shadow() redundant, but + * that's harmless. After that @tnow can be calculated + * by using tkr_mono::cycle_last, which has been set + * by timekeeping_forward_now(). + */ + tk_update_ktime_data(tks); + nsecs =3D timekeeping_cycles_to_ns(&tks->tkr_mono, tks->tkr_mono.cycle_la= st); + tnow =3D ktime_add(tks->tkr_mono.base, nsecs); + + /* Calculate the new AUX offset */ + tks->offs_aux =3D ktime_sub(timespec64_to_ktime(*tnew), tnow); + + timekeeping_update_from_shadow(tkd, TK_UPDATE_ALL); + return 0; +} + const struct k_clock clock_aux =3D { .clock_getres =3D aux_get_res, .clock_get_timespec =3D aux_get_timespec, + .clock_set =3D aux_clock_set, }; =20 static __init void tk_aux_setup(void) From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B16F22749EB; Mon, 19 May 2025 08:33:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643623; cv=none; b=aLgmwN+jjB51uPmXW5jWPc56aFp6Mgo1rv3hGpyIBydmJaTLHK2c6tKbVaoNb+BfYExWYkDWCi1Y4fNLCCtQ9kieIw92q8yG+zeCe2h0ndRMd4B880mGQPwq6zd+ibMWOZUlSozVxvBijpPGv26V6YvjezOzrf7A6yV9LPh0IWY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643623; c=relaxed/simple; bh=bmswsEHRCiYtGAKJWD1cabCPAbE600f4P99guyPMzww=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=gpqNzboiF+kxXK+8z+lWKLWNZp6Lok0Ku1DbUGCnXTSr4vkpxEDFapfp4Vw282OO9VieQV2RE7sJDH3I32wxlQBsz1ajw/DUeXZRJyEaeWQMlu7l4OBInY6Q9jPxfEROiSTv1H6D8l/6CEdK0twR/fVvtj8Yde5jBRz7uin2Doo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=xrA6rCCf; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Kcc2hrow; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="xrA6rCCf"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Kcc2hrow" Message-ID: <20250519083026.841511545@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643619; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=88U+TlNxDjc05fy5ggh1gh3HtGRG6wxT0jHWuxpiG24=; b=xrA6rCCfS6XDu/KgxgJr+pIQFTwzwYoJE3kj2er5PBPkOPtS3ThfC5Lxwk4gDkdq6j5y/G ZAuO/5Fww/kCvOE1EM3RvbyPtJiVmsGFmJ87NPYVd1nhy6KRMBO9z3Ka6HgR40W1iR/UnB 7mLnUh5Ux+o+lPCS+nufm7ZIiTZhNgCbaNADur6zv4QPLX++9njqDlJwOrCuKsIpXzAzoI Q1Y3qzKJUoWlCxLJ9c4ty3C7qIRe4LyiWB2LgqM8ArqqmoNYBqEw11mMnW07Iqidu2csz3 Fg58nYYeqnXgojQvIsYMfS2leFzcO4VfNij58n35dj7y/m7ABDEdzUv/hKLlRQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643619; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=88U+TlNxDjc05fy5ggh1gh3HtGRG6wxT0jHWuxpiG24=; b=Kcc2hrow6Tph3ks+v0nkkrYueGdRgeFOtaBJzPGPYrw3/v7Ur/IucMW2d4LXfviD8Kg28z wfwItWuvFKaUq8AA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 20/26] timekeeping: Make timekeeping_inject_offset() reusable References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:38 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Split out the inner workings for auxiliary clock support and feed the core = time keeper into it. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1433,32 +1433,32 @@ EXPORT_SYMBOL(do_settimeofday64); =20 /** * __timekeeping_inject_offset - Adds or subtracts from the current time. + * @tkd: Pointer to the timekeeper to modify * @ts: Pointer to the timespec variable containing the offset * * Adds or subtracts an offset value from the current time. */ -static int __timekeeping_inject_offset(const struct timespec64 *ts) +static int __timekeeping_inject_offset(struct tk_data *tkd, const struct t= imespec64 *ts) { - struct timekeeper *tks =3D &tk_core.shadow_timekeeper; + struct timekeeper *tks =3D &tkd->shadow_timekeeper; struct timespec64 tmp; =20 if (ts->tv_nsec < 0 || ts->tv_nsec >=3D NSEC_PER_SEC) return -EINVAL; =20 - timekeeping_forward_now(tks); =20 /* Make sure the proposed value is valid */ tmp =3D timespec64_add(tk_xtime(tks), *ts); if (timespec64_compare(&tks->wall_to_monotonic, ts) > 0 || !timespec64_valid_settod(&tmp)) { - timekeeping_restore_shadow(&tk_core); + timekeeping_restore_shadow(tkd); return -EINVAL; } =20 tk_xtime_add(tks, ts); tk_set_wall_to_mono(tks, timespec64_sub(tks->wall_to_monotonic, *ts)); - timekeeping_update_from_shadow(&tk_core, TK_UPDATE_ALL); + timekeeping_update_from_shadow(tkd, TK_UPDATE_ALL); return 0; } =20 @@ -1467,7 +1467,7 @@ static int timekeeping_inject_offset(con int ret; =20 scoped_guard (raw_spinlock_irqsave, &tk_core.lock) - ret =3D __timekeeping_inject_offset(ts); + ret =3D __timekeeping_inject_offset(&tk_core, ts); =20 /* Signal hrtimers about time change */ if (!ret) @@ -2568,6 +2568,7 @@ EXPORT_SYMBOL_GPL(random_get_entropy_fal */ int do_adjtimex(struct __kernel_timex *txc) { + struct tk_data *tkd =3D &tk_core; struct timespec64 delta, ts; struct audit_ntp_data ad; bool offset_set =3D false; @@ -2585,16 +2586,19 @@ int do_adjtimex(struct __kernel_timex *t ktime_get_real_ts64(&ts); add_device_randomness(&ts, sizeof(ts)); =20 - scoped_guard (raw_spinlock_irqsave, &tk_core.lock) { - struct timekeeper *tks =3D &tk_core.shadow_timekeeper; + scoped_guard (raw_spinlock_irqsave, &tkd->lock) { + struct timekeeper *tks =3D &tkd->shadow_timekeeper; s32 orig_tai, tai; =20 + if (!tks->clock_valid) + return -ENODEV; + if (txc->modes & ADJ_SETOFFSET) { delta.tv_sec =3D txc->time.tv_sec; delta.tv_nsec =3D txc->time.tv_usec; if (!(txc->modes & ADJ_NANO)) delta.tv_nsec *=3D 1000; - ret =3D __timekeeping_inject_offset(&delta); + ret =3D __timekeeping_inject_offset(tkd, &delta); if (ret) return ret; =20 @@ -2607,7 +2611,7 @@ int do_adjtimex(struct __kernel_timex *t =20 if (tai !=3D orig_tai) { __timekeeping_set_tai_offset(tks, tai); - timekeeping_update_from_shadow(&tk_core, TK_CLOCK_WAS_SET); + timekeeping_update_from_shadow(tkd, TK_CLOCK_WAS_SET); clock_set =3D true; } else { tk_update_leap_state_all(&tk_core); @@ -2615,7 +2619,7 @@ int do_adjtimex(struct __kernel_timex *t =20 /* Update the multiplier immediately if frequency was set directly */ if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK)) - clock_set |=3D __timekeeping_advance(&tk_core, TK_ADV_FREQ); + clock_set |=3D __timekeeping_advance(tkd, TK_ADV_FREQ); } =20 if (txc->modes & ADJ_SETOFFSET) From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F1644274FE2; Mon, 19 May 2025 08:33:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643624; cv=none; b=I5Bh4Xpc+OMbosbnT1Vbsh7nemFD7LM+1tK1VrPZjVYYOBsogIxZOVFicdlxR67HqaUuH5gXokCuybSEHYDwlbv6PYe52EBDP+iITsaSoXUmxr1SiTl6gFFTS4ilCROgG5Kp5y8p7c/USkzPU6nFaGHx1kqrJwMVXq5ZaoPTOAg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643624; c=relaxed/simple; bh=nFcMK+6lPZe+YpkhPLYFJyLJekReTYSnzfGNuzN6WJk=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=RYATq/tUBa6qmmhi5M3wT37u4Ug2ATyayJejdnNDul5YbL/gUMLZWN6ic09rwf8IlDuDR/QrEAkzQw7SR8PuHxEPI9WzLB0MidbI7M2IIpaPA6QfHVwH73NdprGmTD6ThkH7WIWcpBxO3W4EX2R9RbDOGlsarbrZBv2tDflzkTk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=feMAx2Pc; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=u5Sd0nyM; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="feMAx2Pc"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="u5Sd0nyM" Message-ID: <20250519083026.902859779@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643620; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=BlCFLlYGEJMn/hJAuqbje0heodY/q/pXL2lfUeZb078=; b=feMAx2PcECUhCm9/UTStnSM/24poCWSQqc/2FgsgkT+Q+LpN3pc2fmiPWiFlUT/ucTyRNe NxJn8GlYAYH/6KScsfdJowCjEBdEa0aIBfvZ3JO2GfbFVk5zelTol+JucXC+GeEbJqpMgD 2OhafMeDOluq4C+BcRuA6C4O9JDzqL5oH9eeaf03q3ZsFXQKUShDLbALXfKwLTyLC6nkD2 cMsOw6GoEAEhYN8ewsYe5ldEMFn1PvkWuxsgP9ANYDgUtKSi+rkc5Duelv+JUHksLLVdxe 053YtoNcs2ffUB8T9KVQf6IjqQkJnPoUZFQIy5oJrzB0GU0Uq5q02qkpG9Ne1A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643620; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=BlCFLlYGEJMn/hJAuqbje0heodY/q/pXL2lfUeZb078=; b=u5Sd0nyMQEgf5OwVEWBuZ9USopGkGyjW7Z9wiqBsDcTxfMcMqIxChsrcez4up2/g1/bj24 FnP4aG3dhV5LK4Cw== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 21/26] timekeeping: Add auxiliary clock support to __timekeeping_inject_offset() References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:40 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Redirect the relative offset adjustment to the auxiliary clock offset instead of modifying CLOCK_REALTIME, which has no meaning in context of these clocks. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1448,16 +1448,34 @@ static int __timekeeping_inject_offset(s =20 timekeeping_forward_now(tks); =20 - /* Make sure the proposed value is valid */ - tmp =3D timespec64_add(tk_xtime(tks), *ts); - if (timespec64_compare(&tks->wall_to_monotonic, ts) > 0 || - !timespec64_valid_settod(&tmp)) { - timekeeping_restore_shadow(tkd); - return -EINVAL; + if (!IS_ENABLED(CONFIG_POSIX_AUX_CLOCKS) || tks->id =3D=3D TIMEKEEPER_COR= E) { + /* Make sure the proposed value is valid */ + tmp =3D timespec64_add(tk_xtime(tks), *ts); + if (timespec64_compare(&tks->wall_to_monotonic, ts) > 0 || + !timespec64_valid_settod(&tmp)) { + timekeeping_restore_shadow(tkd); + return -EINVAL; + } + + tk_xtime_add(tks, ts); + tk_set_wall_to_mono(tks, timespec64_sub(tks->wall_to_monotonic, *ts)); + } else { + struct tk_read_base *tkr_mono =3D &tks->tkr_mono; + ktime_t now, offs; + + /* Get the current time */ + now =3D ktime_add_ns(tkr_mono->base, timekeeping_get_ns(tkr_mono)); + /* Add the relative offset change */ + offs =3D ktime_add(tks->offs_aux, timespec64_to_ktime(*ts)); + + /* Prevent that the resulting time becomes negative */ + if (ktime_add(now, offs) < 0) { + timekeeping_restore_shadow(tkd); + return -EINVAL; + } + tks->offs_aux =3D offs; } =20 - tk_xtime_add(tks, ts); - tk_set_wall_to_mono(tks, timespec64_sub(tks->wall_to_monotonic, *ts)); timekeeping_update_from_shadow(tkd, TK_UPDATE_ALL); return 0; } From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3F0452750E5; Mon, 19 May 2025 08:33:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643625; cv=none; b=aT1Vaa11t+OH9XQ/lgVemnUe4V/7E/jBdhsUP1lsAObrHDGY18ixiFscyW2LBstWOanXOw+XInbQPDm8zVvXpmblI5O7DFpcsv7Qi5wR94JNojj9lJT1IDUbbaOADyQZQqkJPxMI8pUhSY5ILQxeUd2qInYKtiHW/aulq9jrIys= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643625; c=relaxed/simple; bh=dAeTY3KMBv2SQl7QumOQiobaigHO/XaHRHIJXipOfJ0=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=FUv/zEg2XL8cN4iCTCox2b4mtY+5rAMMdz8DMsxap/2EBwXt7UGHXA4s+BcDrNv7EHsleZEh/CbDgY41YZOHqFmuf8+4dTD+lAMQFB/Ugp5EHb/x73NPcuQDwu9b/yMrVlpfAOqph7Nmyz/jUITsDg3M5yOCmkZvp2UNtGcKEIU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=WKY/FgFR; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=VwNnkRGT; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="WKY/FgFR"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="VwNnkRGT" Message-ID: <20250519083026.962968376@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643621; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=LE5oO6Rh+o+d8C1+DdeABbq86jkYnys4AQ+wx71IDAw=; b=WKY/FgFR5XSUTloHAVba0AqaoJCUEx3a9YLyz8g/jNHfCTvu+cpT0Q2dpi6/qrtWUV0CXw f8xXOuVWgWyy/ISvElRkXOVMMTPDi4aMnv0wCEdFETV1nbkGonk4gJGC8hE6RuIAMMdZP+ GBhzO+Epf0oeSmjG3GmBPPS8AHphxvGNPaKeR+MOdSTJ9BbKFDp0HYrVlAGqAgcyi0GDNJ ODT41DSNx3N6O1g1Sj62jVoku1G1tpzFKm9XamCpztZ2ikRJfDMqj7EuedV5uXxSIVrNpu QuhN7bYFwI1sln3oeiBWNmx0cd0BWE1MtBiOzvrggQW0s9nlRhvoNs8lM/odgw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643621; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=LE5oO6Rh+o+d8C1+DdeABbq86jkYnys4AQ+wx71IDAw=; b=VwNnkRGTUT9l3UlwEWRwaTQ6GpLrrASLDTCXlkuKaXdK5E7Vtt6xlwHTmi483/vjde7rJq gUCVXzP64ILK5qAA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 22/26] timekeeping: Make do_adjtimex() reusable References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:41 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Split out the actual functionality of adjtimex() and make do_adjtimex() a wrapper which feeds the core timekeeper into it and handles the result including audit at the call site. This allows to reuse the actual functionality for auxiliary clocks. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 110 +++++++++++++++++++++++++----------------= ----- 1 file changed, 60 insertions(+), 50 deletions(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2580,17 +2580,18 @@ unsigned long random_get_entropy_fallbac } EXPORT_SYMBOL_GPL(random_get_entropy_fallback); =20 -/** - * do_adjtimex() - Accessor function to NTP __do_adjtimex function - * @txc: Pointer to kernel_timex structure containing NTP parameters - */ -int do_adjtimex(struct __kernel_timex *txc) +struct adjtimex_result { + struct audit_ntp_data ad; + struct timespec64 delta; + bool clock_set; +}; + +static int __do_adjtimex(struct tk_data *tkd, struct __kernel_timex *txc, + struct adjtimex_result *result) { - struct tk_data *tkd =3D &tk_core; - struct timespec64 delta, ts; - struct audit_ntp_data ad; - bool offset_set =3D false; - bool clock_set =3D false; + struct timekeeper *tks =3D &tkd->shadow_timekeeper; + struct timespec64 ts; + s32 orig_tai, tai; int ret; =20 /* Validate the data before disabling interrupts */ @@ -2599,56 +2600,65 @@ int do_adjtimex(struct __kernel_timex *t return ret; add_device_randomness(txc, sizeof(*txc)); =20 - audit_ntp_init(&ad); - ktime_get_real_ts64(&ts); add_device_randomness(&ts, sizeof(ts)); =20 - scoped_guard (raw_spinlock_irqsave, &tkd->lock) { - struct timekeeper *tks =3D &tkd->shadow_timekeeper; - s32 orig_tai, tai; - - if (!tks->clock_valid) - return -ENODEV; - - if (txc->modes & ADJ_SETOFFSET) { - delta.tv_sec =3D txc->time.tv_sec; - delta.tv_nsec =3D txc->time.tv_usec; - if (!(txc->modes & ADJ_NANO)) - delta.tv_nsec *=3D 1000; - ret =3D __timekeeping_inject_offset(tkd, &delta); - if (ret) - return ret; - - offset_set =3D delta.tv_sec !=3D 0; - clock_set =3D true; - } - - orig_tai =3D tai =3D tks->tai_offset; - ret =3D ntp_adjtimex(tks->id, txc, &ts, &tai, &ad); - - if (tai !=3D orig_tai) { - __timekeeping_set_tai_offset(tks, tai); - timekeeping_update_from_shadow(tkd, TK_CLOCK_WAS_SET); - clock_set =3D true; - } else { - tk_update_leap_state_all(&tk_core); - } - - /* Update the multiplier immediately if frequency was set directly */ - if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK)) - clock_set |=3D __timekeeping_advance(tkd, TK_ADV_FREQ); + guard(raw_spinlock_irqsave)(&tkd->lock); + + if (!tks->clock_valid) + return -ENODEV; + + if (txc->modes & ADJ_SETOFFSET) { + result->delta.tv_sec =3D txc->time.tv_sec; + result->delta.tv_nsec =3D txc->time.tv_usec; + if (!(txc->modes & ADJ_NANO)) + result->delta.tv_nsec *=3D 1000; + ret =3D __timekeeping_inject_offset(tkd, &result->delta); + if (ret) + return ret; + result->clock_set =3D true; + } + + orig_tai =3D tai =3D tks->tai_offset; + ret =3D ntp_adjtimex(tks->id, txc, &ts, &tai, &result->ad); + + if (tai !=3D orig_tai) { + __timekeeping_set_tai_offset(tks, tai); + timekeeping_update_from_shadow(tkd, TK_CLOCK_WAS_SET); + result->clock_set =3D true; + } else { + tk_update_leap_state_all(&tk_core); } =20 + /* Update the multiplier immediately if frequency was set directly */ + if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK)) + result->clock_set |=3D __timekeeping_advance(tkd, TK_ADV_FREQ); + + return ret; +} + +/** + * do_adjtimex() - Accessor function to NTP __do_adjtimex function + * @txc: Pointer to kernel_timex structure containing NTP parameters + */ +int do_adjtimex(struct __kernel_timex *txc) +{ + struct adjtimex_result result =3D { }; + int ret; + + ret =3D __do_adjtimex(&tk_core, txc, &result); + if (ret < 0) + return ret; + if (txc->modes & ADJ_SETOFFSET) - audit_tk_injoffset(delta); + audit_tk_injoffset(result.delta); =20 - audit_ntp_log(&ad); + audit_ntp_log(&result.ad); =20 - if (clock_set) + if (result.clock_set) clock_was_set(CLOCK_SET_WALL); =20 - ntp_notify_cmos_timer(offset_set); + ntp_notify_cmos_timer(result.delta.tv_sec !=3D 0); =20 return ret; } From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0669E2741D0; Mon, 19 May 2025 08:33:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643625; cv=none; b=CqTXxjnkR7nCVPYUsdEDWG6Lwjth+lTmeSGm7fvwAcdh9HutaV1yzegdH6OCEF6QOddM53xWdkIvWEd5aussW8Rmhnu8PMtCbe2v7RUmbZGi+FFvNqtcRdITAf5QoN4cys/ABGZn8lGx5VEgqSUhE+xMEma9zg+dEOaa3V+Ox/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643625; c=relaxed/simple; bh=GRP0gjAGrn/citbYtuiPWdcwsdlss+TGtUZ0SDP84BM=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=JMtZvYTb4nvOKsW3q7OT/nA7cNQbpUNFbR5nCxcfnpTKcsssgNwPGfevZ4LbXQM9Jhgm9W8VJDMblChoSembcFBkWSQC+0+zW5tJxuiRMJB6W/Vv/DLiIv6GDuPyyUkllnZlgRWH+KyWv9gnAwRsYom4xDn0AiWgNo3DDcX/O5c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=uYH33D9d; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=rxKLUGFY; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="uYH33D9d"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="rxKLUGFY" Message-ID: <20250519083027.023158795@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643622; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=QHFCCWtgxJo9J0DtoRfT+72OmxVX42D6q0paXfZdXuM=; b=uYH33D9daUwl2TGXRYuCv9UO6a8r5qge1T7r5kOXptL/hiIPfeE/m+gklzEq+ii8SIwDcp DUIY+InFOiuQV7cIXjIQsgotKEV0MShNAPyfPZRmqvYg+jfZRSTSqgtD/PhhKsFgReuyiA IN6q8DhLFmBtfWUc2bWNmarVKh9TxIGN/laKnEZYZpVZc7XBnM7CEn6upewf7NZMUHEESD CcjHfAkrjCgnD8K0oMK0eAVdJDDaLReWrgXzsqUOGMG7uP3ZT4RtF4rno5ZvK3nY1IwQEX nasM9rOHUGaxS0Y+pqOZF1dJcLDgFP/PcVGIhwMtDGyCru7sLDEwjMb8iQ6CuA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643622; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=QHFCCWtgxJo9J0DtoRfT+72OmxVX42D6q0paXfZdXuM=; b=rxKLUGFYd7nDDOrLYUzoC4+xAqIgZlJ6OpHemWQiZ1zoc3x0RVwgAeKElRBjDAVG0mz50t Rju/Cq2aBs/aOdAA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 23/26] timekeeping: Prepare do_adtimex() for auxiliary clocks References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:42 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Exclude ADJ_TAI, leap seconds and PPS functionality as they make no sense in the context of auxiliary clocks and provide a time stamp based on the actual clock. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -58,6 +58,17 @@ static struct tk_data timekeeper_data[TI /* The core timekeeper */ #define tk_core (timekeeper_data[TIMEKEEPER_CORE]) =20 +#ifdef CONFIG_POSIX_AUX_CLOCKS +static inline bool tk_get_aux_ts64(unsigned int tkid, struct timespec64 *t= s) +{ + return ktime_get_aux_ts64(CLOCK_AUX + tkid - TIMEKEEPER_AUX, ts); +} +#else +static inline bool tk_get_aux_ts64(unsigned int tkid, struct timespec64 *t= s) +{ + return false; +} +#endif =20 /* flag for if timekeeping is suspended */ int __read_mostly timekeeping_suspended; @@ -2503,7 +2514,7 @@ ktime_t ktime_get_update_offsets_now(uns /* * timekeeping_validate_timex - Ensures the timex is ok for use in do_adjt= imex */ -static int timekeeping_validate_timex(const struct __kernel_timex *txc) +static int timekeeping_validate_timex(const struct __kernel_timex *txc, bo= ol aux_clock) { if (txc->modes & ADJ_ADJTIME) { /* singleshot must not be used with any other mode bits */ @@ -2562,6 +2573,21 @@ static int timekeeping_validate_timex(co return -EINVAL; } =20 + if (!aux_clock) + return 0; + + /* Auxiliary clocks are similar to TAI and do not have leap seconds */ + if (txc->status & (STA_INS | STA_DEL)) + return -EINVAL; + + /* No TAI offset setting */ + if (txc->modes & ADJ_TAI) + return -EINVAL; + + /* No PPS support either */ + if (txc->status & (STA_PPSFREQ | STA_PPSTIME)) + return -EINVAL; + return 0; } =20 @@ -2592,15 +2618,22 @@ static int __do_adjtimex(struct tk_data struct timekeeper *tks =3D &tkd->shadow_timekeeper; struct timespec64 ts; s32 orig_tai, tai; + bool aux_clock; int ret; =20 + aux_clock =3D IS_ENABLED(CONFIG_POSIX_AUX_CLOCKS) && tkd->timekeeper.id != =3D TIMEKEEPER_CORE; + /* Validate the data before disabling interrupts */ - ret =3D timekeeping_validate_timex(txc); + ret =3D timekeeping_validate_timex(txc, aux_clock); if (ret) return ret; add_device_randomness(txc, sizeof(*txc)); =20 - ktime_get_real_ts64(&ts); + if (!aux_clock) + ktime_get_real_ts64(&ts); + else + tk_get_aux_ts64(tkd->timekeeper.id, &ts); + add_device_randomness(&ts, sizeof(ts)); =20 guard(raw_spinlock_irqsave)(&tkd->lock); From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4DB7D275111; Mon, 19 May 2025 08:33:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643626; cv=none; b=qjyqWmt0jU5XF1UF43927GAo0u5rmidTdY6XO6sjCqtC/iN63X+HAcpg/dazjsKCgQ32Dvk2oXCBcE2GMLKu52YrF9dLP7Wm61ALBCxUqE4lXD6wWC/+bFQLLVNIQUSBGpg8BrjoQTaqmUw8NfuuXyMFA6Z+dyqCyyJlyCbANQU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643626; c=relaxed/simple; bh=/yvQ5d85ORUDtBs4xTz9aRwb5nKG4+IlF2ndIStdpeM=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=HdWDAWcdp4yQiHo6oG8kWO9e/Wz8NiH0WsFtjYFnuppgtnIkQL11O5BFLTho8IjeUow5M7NVAMe8yfRa4GUJRGVgcmJ2Ik+SrdKVyTR0BgXXqFpDdk/D0ckrU+UdBtgpKgR5N8pw8BqnTL2fjbr21rzOzE3oi2+Lit6jHcQzRQ0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=kdUe7wne; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ft1ugcU/; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="kdUe7wne"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ft1ugcU/" Message-ID: <20250519083027.084196985@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643623; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=xOJpwZxqnxHnJfwGF114Z3PUlwN9EdN52IlzQAPgVbw=; b=kdUe7wnevZ4tF5B8BACtqXOdd6UtmQ8bt5lYryDyBJQa8iWxIg6CvbYpIC++PHoX5KxFYb tID5a0GqNOoHrylyIFCL3h7i49vwT++qQaTxCXExluzzCkT/nQt03j0GH6U7NSUbHOL/Jp uhwjKQyv67hRXub0KDuUKzFCKxHLB2Dd/X1xH5haacHthw0FqQPCdqyjUfr6NXo9dn/zBm k3x+fbDfBIeS40cB5yb79s3QQiZEpbBcGpQFgCM93Gz5FqXlVeDzivq64Dln5z5F626gLY 7OS/YhwgDcYSSizMmWeWbWJc+vbRCxeCqg7S+C5uOjfEeG+KCQSRpx+1IV1KpA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643623; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=xOJpwZxqnxHnJfwGF114Z3PUlwN9EdN52IlzQAPgVbw=; b=ft1ugcU/HXbaxmcG2/oXcrJ21nP3iERg88KQ741ZRKEnJHvzbVQP35sMhsz7d+l4pW3tnb cWb4JRdHvuQE0rBQ== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 24/26] timekeeping: Provide adjtimex() for auxiliary clocks References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:43 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The behaviour is close to clock_adtime(CLOCK_REALTIME) with the following differences: 1) ADJ_SETOFFSET adjusts the auxiliary clock offset =20 2) ADJ_TAI is not supported 3) Leap seconds are not supported 4) PPS is not supported Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2860,10 +2860,26 @@ static int aux_clock_set(const clockid_t return 0; } =20 +static int aux_clock_adj(const clockid_t id, struct __kernel_timex *txc) +{ + struct tk_data *tkd =3D aux_get_tk_data(id); + struct adjtimex_result result =3D { }; + + if (!tkd) + return -ENODEV; + + /* + * @result is ignored for now as there are neither hrtimers nor a + * RTC related to auxiliary clocks for now. + */ + return __do_adjtimex(tkd, txc, &result); +} + const struct k_clock clock_aux =3D { .clock_getres =3D aux_get_res, .clock_get_timespec =3D aux_get_timespec, .clock_set =3D aux_clock_set, + .clock_adj =3D aux_clock_adj, }; =20 static __init void tk_aux_setup(void) From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0E74526A0FC; Mon, 19 May 2025 08:33:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643628; cv=none; b=f45skPl3C8tlgJisSSQYzeYyDn/rMzjOgAoUXRn9DGfFTP1xWbCyeR81uNjTWQrVrFtn6O+7rfhR+rD4nceEyt6XuGVV+BTk+rXrTFaVTgVMk18B6+YpX8txkpr5AfJ0pmPzQvNQsI+Vv+BjWrsUC9G+HfkLK/cxqIP+m4NK4Z0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643628; c=relaxed/simple; bh=pwZXtl/WvAETVW2Q9kzZx42EnMu4I6m9m2c2dTOLLAU=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=KX6N4i8ftyYB+OqxOUcEOqVxv60s4Km2ilPzI6U7bBXKOjGP4YVtWdTftqjLF7torMoQ/BpQSb8d307LXN+rGEpUipYPfaltH1ESAJH7w9OVqDaZoiEXgaJvpK0WifR7pVhWsxvrruE1mTdj+bSMCag5lCR19Wn5DYtqjSP+lKk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=3QQh7B/J; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=prdP/94x; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="3QQh7B/J"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="prdP/94x" Message-ID: <20250519083027.146958941@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643625; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=PCA8zcYARmCtjK0DZG8HOTyBlYYfkMraqZDIC30JbuM=; b=3QQh7B/J2X7PG2PHnvqTeLRKRL28cid3zt9CtR6doa/32ySyBx4LDPuJrkljQoawQ/uLxF u3gIjcAfJzClEB3glfYaR+CnzXVwsasSEQMeIQWLWZcnyICGhgkjO2PnZ2h8HMDrtglC69 fKQEyjyK7nfLAPZHVw1GFrq4Q6Y2m7YiKCf1LPUqKey8jo8XV2MKP6307BcUE0jHwLCEB3 3u9Zm+V9BlufZuZZKjk+5OWmpX2sqeZEYb/oOAC1bq2YbQvrnFyssZ/btVGVNm3nNPHGqJ EpByuQz6ruEVBPTJEZsDWP3YsYdW4YrH4IdoIEKuWLPlvyDSlZdpcmmhmbLxFQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643625; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=PCA8zcYARmCtjK0DZG8HOTyBlYYfkMraqZDIC30JbuM=; b=prdP/94xr3oBJJsoVGryQaTzpivn1oQLX5k7XXf/iMU7ivv8X7YYRp5xJHtP8h9bDKVMhR db03X8I5U6nCHbDA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 25/26] timekeeping: Provide update for auxiliary timekeepers References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:44 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Update the auxiliary timekeepers periodically. For now this is tied to the = system timekeeper update from the tick. This might be revisited and moved out of t= he tick. Signed-off-by: Thomas Gleixner --- kernel/time/timekeeping.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) --- --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -131,9 +131,11 @@ static struct tk_fast tk_fast_raw ____c #ifdef CONFIG_POSIX_AUX_CLOCKS static __init void tk_aux_setup(void); static void tk_aux_update_clocksource(void); +static void tk_aux_advance(void); #else static inline void tk_aux_setup(void) { } static inline void tk_aux_update_clocksource(void) { } +static inline void tk_aux_advance(void) { } #endif =20 unsigned long timekeeper_lock_irqsave(void) @@ -2312,11 +2314,13 @@ static bool timekeeping_advance(enum tim /** * update_wall_time - Uses the current clocksource to increment the wall t= ime * + * It also updates the enabled auxiliary clock timekeepers */ void update_wall_time(void) { if (timekeeping_advance(TK_ADV_TICK)) clock_was_set_delayed(); + tk_aux_advance(); } =20 /** @@ -2762,6 +2766,20 @@ static void tk_aux_update_clocksource(vo } } =20 +static void tk_aux_advance(void) +{ + unsigned long active =3D READ_ONCE(aux_timekeepers); + unsigned int id; + + for_each_set_bit(id, &active, BITS_PER_LONG) { + struct tk_data *tkd =3D &timekeeper_data[id + TIMEKEEPER_AUX]; + + guard(raw_spinlock)(&tkd->lock); + if (tkd->shadow_timekeeper.clock_valid) + __timekeeping_advance(tkd, TK_ADV_TICK); + } +} + /** * ktime_get_aux - Get TAI time for a AUX clock * @id: ID of the clock to read (CLOCK_AUX...) From nobody Fri Dec 19 15:48:08 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 39A49277800; Mon, 19 May 2025 08:33:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643629; cv=none; b=NMS0cIeV52dRCS9ko24Y4XfirnkQxDhVuyyH8cnN6qNepshkNwL3xJ45Asqdl85iSCDguwZkv5rv7MiWF+zGlmKvNi7atqSvBEeJSDUN3DNVQ2gpj4MHV5WsLveZXtqZBfzq5dW4dIyp+juYproUSgiCT+mAmvMv8KeLe6Z+B6g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747643629; c=relaxed/simple; bh=9U7iTSAF6zkbJ6asv5JJaKoo4ejTpSehXvpToCZTnmM=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=eHSkbJ4UOrRwklpp0x9tBpzgFj/WmCt10OwqrGnhaPl+tL5e1LRCUrexhWxXDA/5lf8GfFD8KZ6DMPKgYrhrPILdlOEDnjL+baqj3zJYU/AaxJzy8V2InHZTx+S1y3rW6FO4g+lXcoxpkibHw1k4iZ3GN9SDyE8Kp2HUvKRywCI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=bPlkKWF3; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=cNC42jHk; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="bPlkKWF3"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="cNC42jHk" Message-ID: <20250519083027.209080298@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747643626; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=1D0z/j/auw0PNfUJbi9PcKAbzfkUI9RWpEnF/3jaOYY=; b=bPlkKWF3C3QGb/AxjSDd6jKF3MM7FfI7fmEKRPJyf9w/Tpa3/Hlq3hhQqW+09YiXGu7b8m yRfoROqGeKwhDA2r3+t25o9ugRJARK90tAPYodZkN9LXFzwKkATrxadub9Kn0H+Ihhj3hS PUj+AlCm7QMpn0vUzmBirxiBszys3w1LQ5aXQ9Or48HbOw+mKc3y5oyrTLHrhrrVbPWgKe kl+816S+uJQ615lCzFl3J6AhPIZ7yIb8gReCTgCUeVk2qwD/sBOFQ9g0MaLUTK3CtyPA70 ma1Nb2X0M/GQJg+F/cJ2YNT3tXBxyW2if8gtJx3qtZ9b7gCfZU/6LIjulzVxfg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747643626; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=1D0z/j/auw0PNfUJbi9PcKAbzfkUI9RWpEnF/3jaOYY=; b=cNC42jHkWgdPRmX6/OVa6PrctCPzyQ9pc8h7B9O787EzQasKq4rXNr+nw/C5WCTdQs8Ttj /ExQ5OxvsohmRLAA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , John Stultz , Frederic Weisbecker , Anna-Maria Behnsen , Miroslav Lichvar , Werner Abt , David Woodhouse , Stephen Boyd , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Kurt Kanzenbach , Nam Cao , Antoine Tenart Subject: [patch V2 26/26] timekeeping: Provide interface to control auxiliary clocks References: <20250519082042.742926976@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 19 May 2025 10:33:46 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Auxiliary clocks are disabled by default and attempts to access them fail. Provide an interface to enable/disable them at run-time. Signed-off-by: Thomas Gleixner --- Documentation/ABI/stable/sysfs-kernel-time-aux-clocks | 5=20 kernel/time/timekeeping.c | 125 +++++++++++++= +++++ 2 files changed, 130 insertions(+) --- /dev/null +++ b/Documentation/ABI/stable/sysfs-kernel-time-aux-clocks @@ -0,0 +1,5 @@ +What: /sys/kernel/time/aux_clocks//enable +Date: May 2025 +Contact: Thomas Gleixner +Description: + Controls the enablement of auxiliary clock timekeepers. --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -2900,6 +2901,130 @@ const struct k_clock clock_aux =3D { .clock_adj =3D aux_clock_adj, }; =20 +static void aux_clock_enable(unsigned int id) +{ + struct tk_read_base *tkr_raw =3D &tk_core.timekeeper.tkr_raw; + struct tk_data *tkd =3D aux_get_tk_data(id); + struct timekeeper *tks =3D &tkd->shadow_timekeeper; + + /* Prevent the core timekeeper from changing. */ + guard(raw_spinlock_irq)(&tk_core.lock); + + /* + * Setup the auxiliary clock assuming that the raw core timekeeper + * clock frequency conversion is close enough. Userspace has to + * adjust for the deviation via clock_adjtime(2). + */ + guard(raw_spinlock_nested)(&tkd->lock); + + /* Remove leftovers of a previous registration */ + memset(tks, 0, sizeof(*tks)); + /* Restore the timekeeper id */ + tks->id =3D tkd->timekeeper.id; + /* Setup the timekeeper based on the current system clocksource */ + tk_setup_internals(tks, tkr_raw->clock); + + /* Mark it valid and set it live */ + tks->clock_valid =3D true; + timekeeping_update_from_shadow(tkd, TK_UPDATE_ALL); +} + +static void aux_clock_disable(unsigned int id) +{ + struct tk_data *tkd =3D aux_get_tk_data(id); + + guard(raw_spinlock_irq)(&tkd->lock); + tkd->shadow_timekeeper.clock_valid =3D false; + timekeeping_update_from_shadow(tkd, TK_UPDATE_ALL); +} + +static DEFINE_MUTEX(aux_clock_mutex); + +static ssize_t aux_clock_enable_store(struct kobject *kobj, struct kobj_at= tribute *attr, + const char *buf, size_t count) +{ + /* Lazy atoi() as name is "0..7" */ + int id =3D kobj->name[0] & 0x7; + bool enable; + + if (!capable(CAP_SYS_TIME)) + return -EPERM; + + if (kstrtobool(buf, &enable) < 0) + return -EINVAL; + + guard(mutex)(&aux_clock_mutex); + if (enable =3D=3D test_bit(id, &aux_timekeepers)) + return count; + + if (enable) { + aux_clock_enable(CLOCK_AUX + id); + set_bit(id, &aux_timekeepers); + } else { + aux_clock_disable(CLOCK_AUX + id); + clear_bit(id, &aux_timekeepers); + } + return count; +} + +static ssize_t aux_clock_enable_show(struct kobject *kobj, struct kobj_att= ribute *attr, char *buf) +{ + unsigned long active =3D READ_ONCE(aux_timekeepers); + /* Lazy atoi() as name is "0..7" */ + int id =3D kobj->name[0] & 0x7; + + return sysfs_emit(buf, "%d\n", test_bit(id, &active)); +} + +static struct kobj_attribute aux_clock_enable_attr =3D __ATTR_RW(aux_clock= _enable); + +static struct attribute *aux_clock_enable_attrs[] =3D { + &aux_clock_enable_attr.attr, + NULL, +}; + +static const struct attribute_group aux_clock_enable_attr_group =3D { + .attrs =3D aux_clock_enable_attrs, +}; + +static int __init tk_aux_sysfs_init(void) +{ + struct kobject *auxo, *tko =3D kobject_create_and_add("time", kernel_kobj= ); + + if (!tko) { + pr_warn("Unable to create /sys/kernel/time/. POSIX AUX clocks disabled.\= n"); + return -ENOMEM; + } + + auxo =3D kobject_create_and_add("aux_clocks", tko); + if (!auxo) { + pr_warn("Unable to create /sys/kernel/time/aux_clocks. POSIX AUX clocks = disabled.\n"); + kobject_put(tko); + return -ENOMEM; + } + + for (int i =3D TIMEKEEPER_AUX; i <=3D TIMEKEEPER_AUX_LAST; i++) { + char id[2] =3D { [0] =3D '0' + (i - TIMEKEEPER_AUX), }; + struct kobject *clk =3D kobject_create_and_add(id, auxo); + + if (!clk) { + pr_warn("Unable to create /sys/kernel/time/aux_clocks/%d\n", + i - TIMEKEEPER_AUX); + return -ENOMEM; + } + + int ret =3D sysfs_create_group(clk, &aux_clock_enable_attr_group); + + if (ret) { + pr_warn("Unable to create /sys/kernel/time/aux_clocks/%d/enable\n", + i - TIMEKEEPER_AUX); + return ret; + } + } + return 0; +} +late_initcall(tk_aux_sysfs_init); + static __init void tk_aux_setup(void) { for (int i =3D TIMEKEEPER_AUX; i <=3D TIMEKEEPER_AUX_LAST; i++)