From nobody Mon May 11 08:31:47 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2666EC433EF for ; Sat, 9 Apr 2022 08:13:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240708AbiDIIPd (ORCPT ); Sat, 9 Apr 2022 04:15:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234930AbiDIIP0 (ORCPT ); Sat, 9 Apr 2022 04:15:26 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26FE93B014; Sat, 9 Apr 2022 01:13:20 -0700 (PDT) From: Kurt Kanzenbach DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1649491998; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QzcH6z3Am+D81qDzofvGn1v+XN9UIqBR/iqwfKWEGtk=; b=g08lZTeDaqga7QeSnpS3Gg3UQWrGLCc57n0dC/Y9mMTfzATiUX+sDqZB1cjUMaJ0OOr6Tt 0U/CI/7/+SDBgKSg5VOanA3J/VatXIrFKbtl3CSh5CqKIcrLf10u7imYbYbE449JBs+C1m u2cnDn4+skwSdJJOEz0PbNpO273ph2VmWj3S/zFO6+J1XUztCMSN3wmuFSys7VVVUOnLFl aKmfCOGC14ekT27v+b5vquTt96UvHHUPIJNYFB4RqkkwE0uXN/2TY76rSmOgzlf5OhHAAY U7UGG/McpAl0dwUDDJpFvIiF3/TMpxP56njxcpnm2lbu4hvWm1RDUNacg+BguA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1649491998; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QzcH6z3Am+D81qDzofvGn1v+XN9UIqBR/iqwfKWEGtk=; b=+olXlv1t565OFPTGouHJINnEgCzS4ySza2oWb/PIRfv1NCCmbS61ALK9SVTg4bj0K/jXMz uYWfNC9bP9DYVLCg== To: John Stultz , Thomas Gleixner , Stephen Boyd , Steven Rostedt , Ingo Molnar , Jonathan Corbet Cc: Richard Cochran , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Kurt Kanzenbach Subject: [PATCH 1/3] timekeeping: Introduce fast accessor to clock tai Date: Sat, 9 Apr 2022 10:12:58 +0200 Message-Id: <20220409081300.4762-2-kurt@linutronix.de> In-Reply-To: <20220409081300.4762-1-kurt@linutronix.de> References: <20220409081300.4762-1-kurt@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Introduce fast/NMI safe accessor to clock tai for tracing. The Linux kernel tracing infrastructure has support for using different clocks to generate timestamps for trace events. Especially in TSN networks it's useful to have= TAI as trace clock, because the application scheduling is done in accordance to= the network time, which is based on TAI. With a tai trace_clock in place, it be= comes very convenient to correlate network activity with Linux kernel application traces. Use the same implementation as ktime_get_boot_fast_ns() does by reading the monotonic time and adding the TAI offset. The same limitations as for the f= ast boot implementation apply. The TAI offset may change at run time e.g., by setting the time or using adjtimex() with an offset. However, these kind of offset changes are rare events. Nevertheless, the user has to be aware and = deal with it in post processing. An alternative approach would be to use the same implementation as ktime_get_real_fast_ns() does. However, this requires to add an additional = u64 member to the tk_read_base struct. This struct together with a seqcount is designed to fit into a single cache line on 64 bit architectures. Adding a = new member would violate this constraint. Signed-off-by: Kurt Kanzenbach Reviewed-by: Thomas Gleixner --- include/linux/timekeeping.h | 1 + kernel/time/timekeeping.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/linux/timekeeping.h b/include/linux/timekeeping.h index 78a98bdff76d..fe1e467ba046 100644 --- a/include/linux/timekeeping.h +++ b/include/linux/timekeeping.h @@ -177,6 +177,7 @@ static inline u64 ktime_get_raw_ns(void) extern u64 ktime_get_mono_fast_ns(void); extern u64 ktime_get_raw_fast_ns(void); extern u64 ktime_get_boot_fast_ns(void); +extern u64 ktime_get_tai_fast_ns(void); extern u64 ktime_get_real_fast_ns(void); =20 /* diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index dcdcb85121e4..fd639a7e532e 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -532,6 +532,23 @@ u64 notrace ktime_get_boot_fast_ns(void) } EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns); =20 +/** + * ktime_get_tai_fast_ns - NMI safe and fast access to tai clock. + * + * The same limitations as described for ktime_get_boot_fast_ns() apply: T= he + * mono time and the TAI offset are not read atomically which may yield wr= ong + * readouts. However, an update of the TAI offset is an rare event e.g., c= aused + * by settime or adjtimex with an offset. The user of this function has to= deal + * with the possibility of wrong timestamps in post processing. + */ +u64 notrace ktime_get_tai_fast_ns(void) +{ + struct timekeeper *tk =3D &tk_core.timekeeper; + + return (ktime_get_mono_fast_ns() + ktime_to_ns(tk->offs_tai)); +} +EXPORT_SYMBOL_GPL(ktime_get_tai_fast_ns); + static __always_inline u64 __ktime_get_real_fast(struct tk_fast *tkf, u64 = *mono) { struct tk_read_base *tkr; --=20 2.30.2 From nobody Mon May 11 08:31:47 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0330FC433F5 for ; Sat, 9 Apr 2022 08:13:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239890AbiDIIPh (ORCPT ); Sat, 9 Apr 2022 04:15:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237199AbiDIIP0 (ORCPT ); Sat, 9 Apr 2022 04:15:26 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F00F102D; Sat, 9 Apr 2022 01:13:20 -0700 (PDT) From: Kurt Kanzenbach DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1649491998; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YlxWxeEGkgLnVt3u2pgdSqjRibYcLflLeZsegRG9WOc=; b=xDmm0sG3jd/oqIpzz8MQ38dxRoQ52XMer/238Lvsr3ZhBDH5h7w+uotPNjeiLa8xna5d2b 2YA0DbImJoadBLtlE5Zo2oDLiIkPb4aWcU5DuHqEysd9ba+a55Boo6oRI4h3Pou88EdW1V iLKGLVjUmPX4udhoz7gjJLy/ZiETfod2XfNrGERDtKq0hPCLxFPKRO5ZQv1VIHBAdequMX D7YfO77+caxoJBd4q80qDHqEbZLyQuVpt0x8gkvl1aU3XZ5EQj4hMI+MF7tAZ2vd61+be6 UaV6kEsriP66SRRPET3L4N2/pgU42xejczJiWEV7m5IWSZrCV2dQe1x0op+Wsg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1649491998; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YlxWxeEGkgLnVt3u2pgdSqjRibYcLflLeZsegRG9WOc=; b=cnO2eHfwQp0Gdg+4h4uQCl1yl1f8YhWosVFtQh7ktUuNizTZnxjdgfiHEf72xqFaCbMuWw CaH7olfYTqOcG6Bw== To: John Stultz , Thomas Gleixner , Stephen Boyd , Steven Rostedt , Ingo Molnar , Jonathan Corbet Cc: Richard Cochran , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Kurt Kanzenbach Subject: [PATCH 2/3] tracing: Introduce trace clock tai Date: Sat, 9 Apr 2022 10:12:59 +0200 Message-Id: <20220409081300.4762-3-kurt@linutronix.de> In-Reply-To: <20220409081300.4762-1-kurt@linutronix.de> References: <20220409081300.4762-1-kurt@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" A fast/NMI safe accessor for CLOCK_TAI has been introduced. Use it for adding the additional trace clock "tai". Signed-off-by: Kurt Kanzenbach --- kernel/trace/trace.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index f4de111fa18f..7be87b29c6c1 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1542,6 +1542,7 @@ static struct { { ktime_get_mono_fast_ns, "mono", 1 }, { ktime_get_raw_fast_ns, "mono_raw", 1 }, { ktime_get_boot_fast_ns, "boot", 1 }, + { ktime_get_tai_fast_ns, "tai", 1 }, ARCH_TRACE_CLOCKS }; =20 --=20 2.30.2 From nobody Mon May 11 08:31:47 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 72DE4C433EF for ; Sat, 9 Apr 2022 08:13:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240971AbiDIIPq (ORCPT ); Sat, 9 Apr 2022 04:15:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230343AbiDIIP0 (ORCPT ); Sat, 9 Apr 2022 04:15:26 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C387A674F0; Sat, 9 Apr 2022 01:13:20 -0700 (PDT) From: Kurt Kanzenbach DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1649491999; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oM+FoGv/BkTkG2TdHv8UkTd+jiQ2ziLqcW0PI5BxId8=; b=RJ3g47WFm501Wsc7a9Pc5BI3r/v+TZVkRFbVskQ9OA/vtf7diHW8GOxpwN0OjYnOl3cWvH V8zwNI+H645fJ8qK38iBrXLvxzt/M4zGpCRHCMGdjv/6JIKldxAXKb0+5488FVLRrAqTu4 FdhMYkIzer+9Pn+WIS6WVdjTYkLAkgAIOgo/1n46l68lbS2PXCEtZTBvBeQVd/OoYDHPLe rkZyayYQSeZ78dLxC5efEwKBYKSrL+T/wQKC02xj/R7xBxoGzfKQ8FAUAKWUQ3VRvlk2IM 8DfNxUS2JgJYxzfE471Rw3frvW/vj3QLmWQXNbbrU6LCQPCWhDE9dxRiRLhMzg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1649491999; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oM+FoGv/BkTkG2TdHv8UkTd+jiQ2ziLqcW0PI5BxId8=; b=v+XCuAeDY6E9LehoYbDZmkqdjG0BKLsseED+lKx6+nUBcdJnfzPjov3fYy203ZyXQFwslg n/BsRLw11Sgpx6Bw== To: John Stultz , Thomas Gleixner , Stephen Boyd , Steven Rostedt , Ingo Molnar , Jonathan Corbet Cc: Richard Cochran , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Kurt Kanzenbach Subject: [PATCH 3/3] tracing: Add documentation for trace clock tai Date: Sat, 9 Apr 2022 10:13:00 +0200 Message-Id: <20220409081300.4762-4-kurt@linutronix.de> In-Reply-To: <20220409081300.4762-1-kurt@linutronix.de> References: <20220409081300.4762-1-kurt@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add documentation for new introduced trace clock tai. This clock corresponds to CLOCK_TAI. Signed-off-by: Kurt Kanzenbach --- Documentation/trace/ftrace.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Documentation/trace/ftrace.rst b/Documentation/trace/ftrace.rst index 45b8c56af67a..b37dc19e4d40 100644 --- a/Documentation/trace/ftrace.rst +++ b/Documentation/trace/ftrace.rst @@ -517,6 +517,18 @@ of ftrace. Here is a list of some of the key files: processing should be able to handle them. See comments in the ktime_get_boot_fast_ns() function for more information. =20 + tai: + This is the tai clock (CLOCK_TAI) and is derived from the wall- + clock time. However, this clock does not experience + discontinuities and backwards jumps caused by NTP inserting leap + seconds. Since the clock access is designed for use in tracing, + side effects are possible. The clock access may yield wrong + readouts in case the internal TAI offset is updated e.g., caused + by setting the system time or using adjtimex() with an offset. + These effects are rare and post processing should be able to + handle them. See comments in the ktime_get_tai_fast_ns() + function for more information. + To set a clock, simply echo the clock name into this file:: =20 # echo global > trace_clock --=20 2.30.2