Article View: linux.kernel.netdev
Article #11507[PATCH net-next v3 2/6] ptp: add functions to adjust system timestamps
From: James Clark
Date: Thu, 05 Nov 2026 05:48
Date: Thu, 05 Nov 2026 05:48
90 lines
14396 bytes
14396 bytes
Add ptp_adjust_system_prets() and ptp_adjust_system_postts(), which
shift the pre or post system timestamp by a given CLOCK_MONOTONIC_RAW
duration. The selected clock's time is shifted by the duration scaled by
that clock's current frequency relative to CLOCK_MONOTONIC_RAW, with
ktime_scale_raw_ns().
An MDIO bus driver can take system timestamps around the command that
starts a write, and knows how long after that command the write
completes, so it can shift the timestamps to bound the completion of
the write.
Signed-off-by: James Clark <jjc@jclark.com>
---
Changes in v2:
- Scale the shift of the selected clock's time by that clock's current
frequency relative to CLOCK_MONOTONIC_RAW with ktime_scale_raw_ns(),
in place of adding the CLOCK_MONOTONIC_RAW duration unscaled
- Move before the mdio core patch (was 3/5), which now uses these
helpers
include/linux/ptp_clock_kernel.h | 55 ++++++++++++++++++++++++++++++++
1 file changed, 55 insertions(+)
diff --git a/include/linux/ptp_clock_kernel.h b/include/linux/ptp_clock_kernel.h
index 36a27a910595..b952744ff696 100644
--- a/include/linux/ptp_clock_kernel.h
+++ b/include/linux/ptp_clock_kernel.h
@@ -520,4 +520,59 @@ static inline void ptp_read_system_postts(struct ptp_system_timestamp *sts)
ktime_get_snapshot_id(sts->clockid, &sts->post_sts);
}
+static inline void __ptp_adjust_snapshot(clockid_t clockid,
+ struct system_time_snapshot *snap,
+ s64 ns)
+{
+ s64 sys_ns;
+
+ if (!snap->valid)
+ return;
+ if (ns < 0)
+ sys_ns = -(s64)ktime_scale_raw_ns(clockid, -ns);
+ else
+ sys_ns = ktime_scale_raw_ns(clockid, ns);
+ snap->systime = ktime_add_ns(snap->systime, sys_ns);
+ snap->monoraw = ktime_add_ns(snap->monoraw, ns);
+ /* No counter value corresponds to the adjusted times. */
+ snap->cycles = 0;
+ snap->cs_id = CSID_GENERIC;
+ snap->hw_cycles = 0;
+ snap->hw_csid = CSID_GENERIC;
+}
+
+/**
+ * ptp_adjust_system_prets - Shift the lower system timestamp bound
+ * @sts: system timestamps, or NULL
+ * @ns: CLOCK_MONOTONIC_RAW nanoseconds to add
+ *
+ * Add @ns to the CLOCK_MONOTONIC_RAW time of the lower bound, and @ns
+ * scaled by the selected clock's current frequency relative to
+ * CLOCK_MONOTONIC_RAW to its time, and clear its clocksource counter
+ * values.
+ */
+static inline void ptp_adjust_system_prets(struct ptp_system_timestamp *sts,
+ s64 ns)
+{
+ if (sts)
+ __ptp_adjust_snapshot(sts->clockid, &sts->pre_sts, ns);
+}
+
+/**
+ * ptp_adjust_system_postts - Shift the upper system timestamp bound
+ * @sts: system timestamps, or NULL
+ * @ns: CLOCK_MONOTONIC_RAW nanoseconds to add
+ *
+ * Add @ns to the CLOCK_MONOTONIC_RAW time of the upper bound, and @ns
+ * scaled by the selected clock's current frequency relative to
+ * CLOCK_MONOTONIC_RAW to its time, and clear its clocksource counter
+ * values.
+ */
+static inline void ptp_adjust_system_postts(struct ptp_system_timestamp *sts,
+ s64 ns)
+{
+ if (sts)
+ __ptp_adjust_snapshot(sts->clockid, &sts->post_sts, ns);
+}
+
#endif
--
2.56.0
Technical Details
Message-ID:
<20261105054839.2248046-3-jjc@jclark.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-357077-linux-kernel-netdev=dewy.ruReferences:
<20261105054839.2248046-1-jjc@jclark.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-357077-linux-kernel-netdev=dewy.ru
From: jjc@jclark.com (James Clark)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net-next v3 2/6] ptp: add functions to adjust system timestamps
Date: Thu, 5 Nov 2026 12:48:35 +0700
Organization: Usenet Network
Message-ID: <20261105054839.2248046-3-jjc@jclark.com>
References: <20261105054839.2248046-1-jjc@jclark.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="3569885"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:zM58wKyVj3RhDk2QAvtdD6hsDos= sha256:2JK7EYRJNRxTsFZOWs2Yxdr9303PqnN4udKnEXxGkPE=
sha1:XagCOCdh7al3pOafHqSwK/nGYE4= sha256:WfF0qfcH1HgGNRkSxdsnjeFX3gBRkS9gqcsuTtIXD5s=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
by mail.dewy.ru (Postfix) with ESMTP id E98B2112E9F; Thu, 5 Nov 2026 08:50:32 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -2.951
X-Spam-Status: No, score=-2.951 tagged_above=-9999 required=6.31
tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1,
RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001]
autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
header.d=jclark-com.20260707.gappssmtp.com
X-Received: from mail.dewy.ru ([127.0.0.1])
by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id W83Un4qbDYCD;
Thu, 5 Nov 2026 08:50:29 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-357077-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=jclark.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
by mail.dewy.ru (Postfix) with ESMTPS id 8A452112E8A; Thu, 5 Nov 2026 08:50:21 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by sea.lore.kernel.org (Postfix) with ESMTP id 59A62345BE; Thu, 5 Nov 2026 05:49:04 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
t=1793857743;
b=vme/51a2NC6yiDq2coCRGNXfIWtA9wiUhJIaq9+TWHo8d5koMK6Lrr476mfb5GX55iEi
gBrbepnhzNWjfBBRgpMhWs7/ePRJWVv7GBDcSM7NtFye9o5aOEW9uCuQBJaSv2V+Gb7fD
1P6waoc4t7Jl2jijZ5VCPHxGhGfIhmUROGssvb2O9B6wCYn3EmfV+bQpyP2/tistBvvDo
YC6SZmZksTCOTy1YQc9nBpWKpnhCYJ+bM9O1K8J10YxVqy4aw5eMMC79PwwJ2VomHA82B
RfHdOmkoR/QioMphxU4zTVmZGy1beeKRZze6UTf10k+gDC8ogy9PrhGWBFUZdARCSHg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793857743;
h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
X-Google-DKIM-Signature:X-Forwarded-Encrypted:X-Gm-Message-State:
X-Gm-Gg:X-Received:Received:From:To:Cc:Subject:Date:Message-ID:
X-Mailer:In-Reply-To:References:X-Mailing-List:List-Id:List-Post:
List-Help:List-Subscribe:List-Unsubscribe:List-Owner:Precedence:
MIME-Version:Content-Transfer-Encoding;
bh=7vRCSNcyZCs3Byo+z/ZAnwu5XcUvruiiG1cQha9Q2S4=;
b=XerrmSt4jP82kFcXpk5waRmwv8NhE4561WBWGXSxyvhbJbGpiJtizKCE2B15LP6Kb5jy
vC5p9zIHKwAxTVhU92f8B5k81YTnnBoqhRdUE/ZIUHTxYthqqXUBHnIADvH60bi6R7h+O
3G0Mip0GG6x6XryF5daRUbxPPCsVbi2H6W8r7TaZJ7JN3dnuYPam665xg8EF6WJnydzsF
oauKsA/Zu7oEsnJ6GFZ96s0SuMV15mGVngtmzLDI6zNUofACWddCMJZUeexR9bVoNqXNF
Tf3b6MaFfl0GG3Wr9WhwN6i/EGTd5PfCkmU7EPHCLjkta3BGz9fseJ7dqmE7WO8NEnA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dkim=pass header.d=jclark-com.20260707.gappssmtp.com header.i=@jclark-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=VZvLZAcD;
dmarc=none header.from=jclark.com;
spf=pass smtp.mailfrom=jclark.com;
arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id F41C6606D80A;
Thu, 5 Nov 2026 05:49:02 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key, unprotected) header.d=jclark-com.20260707.gappssmtp.com header.i=@jclark-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=VZvLZAcD
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.179
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1793857742;
b=b+d8tBA1VVY40bkk1TMw3L3RlLjeUpqbBXtsdgztUAgDDXlIPspgBBVChyF+KewU4KW6
T/WdVPS3muV8rKPx11w3qDJVFdZrTRefXLUC6ZV/q/T5ZI+sSf9KrKVgugnIZ5Ai0m83w
O7BpftrteSkg6jFHuKkt30SF9UnEUXb9+s6lqgKZpmUg9vNcOCWWGHzQa+4CaCOcVMA6U
Qht7Dx+nC3pVbPnbZGlzA7J3fS6UqityU7ZN9yCptFiqOGKk2KbWFaRZnl4buDap8kXjE
C3KOexIGgGOs/hFcfDv/6j9lYSqhbYdfJ4LJf5IpIunkYrnf47TJPYHpJB1yXxAnVjQ==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793857742;
h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received:
From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:
MIME-Version:Content-Transfer-Encoding;
bh=7vRCSNcyZCs3Byo+z/ZAnwu5XcUvruiiG1cQha9Q2S4=;
b=10dYEKAuKeUjD5gFHmi/SAVIYJylfAktUO/HfRdP0B3OJ4pOqoguMXlz1ve3sqWIx9s/
cIltM/Gw1DH0kP1id/Caah0rpvN6aDkLUDPa/zldkypDT/B/wEBMjrRbSZRXVEODm5mZV
Vl0sS9MovIXZ9tmoQAiiBqrWTQ8KJHHeMRx2+S7G8Z5SyL2ZHWmXoRLrGitFm9GGFGNFC
wDfKpf1pX2nqHJQhTrXskzo9eVqhWuH0SZgeS7HpHxlhvyMUFdv+AjhVZsBQiyonjCnkC
Bl3k1kwGT0zn2M/ZXTYkO4tPYQutgzQ73LhORey4tnR+0n8KKyG3JHCvEE8ExRB2JIg==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
dmarc=none header.from=jclark.com;
spf=pass smtp.mailfrom=jclark.com;
dkim=pass header.d=jclark-com.20260707.gappssmtp.com header.i=@jclark-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=VZvLZAcD;
arc=none smtp.remote-ip=209.85.215.179
X-Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179])
(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D4D3606A328
for <netdev@vger.kernel.org>; Thu, 5 Nov 2026 05:49:01 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4D4D3606A328
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=jclark.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=jclark.com
X-Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-cc52c1b8286so141546a12.1
for <netdev@vger.kernel.org>; Wed, 04 Nov 2026 21:49:01 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=jclark-com.20260707.gappssmtp.com; s=20260707; t=1793857741; x=1794462541; darn=vger.kernel.org;
h=content-transfer-encoding:mime-version:references:in-reply-to
:message-id:date:subject:cc:to:from:from:to:cc:subject:date
:message-id:reply-to:content-type;
bh=7vRCSNcyZCs3Byo+z/ZAnwu5XcUvruiiG1cQha9Q2S4=;
b=VZvLZAcDX/9Zig8n+WP3af6cqSzQ9xFLCzOecmHMvPvnbKelKS6a5KSssA33wAcwKN
j50Gsh3bUwV1sjj2G+afpF+EDM60oWhRaYs5KbjutXmgsMvk2jhfc1KhsDzksERgp19u
3NW28rwInhnZH+UvIZF4tJEUtdpw1lPFj8kM8vha7aUQjed0hoVy/xBj240ybYsoAqNo
Mp83HFdzyU0Apdz3slFknlCMhZOhXXPpEGIdF9G8s3XLHRpIA9dIENA8vtFuiWnaWk+z
Cx9nKw0tdzy4hzwDa+At7sbQSwkp1Xti+NBUepOyrcvIhlgFvPgODKVIfbTICraghFoj
69+Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1793857741; x=1794462541;
h=content-transfer-encoding:mime-version:references:in-reply-to
:message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from
:to:cc:subject:date:message-id:reply-to:content-type;
bh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rLMf9VBLRQhJCLD/z9Fbcb/atVb1N6wfD
cK2S3SHjxTZeHonmWKqldvIkXC10SHWjXyr2RWPvQmkBGHhgUGtZ0R6PY7KpcALj0m+HljYcYLT
WRI7/R0Bl8zeRLYvJIFaRi9lON8mCyR04JJ7f/YxyXQEGDU8xNEmf79yAwRavAMoVMCZ5oI307T
PtmqmWh1PYhViroBzDRmONaBXkKldMqJWgHVYP0OYOJt7AqCVEJMavwUieLkEZLQwkMgOtDrxsB
SYgIcwMov/SRgqsg4m3qhwT5Fx2Ywbo1PTjaO/rY=
X-Received: by 2002:a17:90b:4486:b0:3a0:e568:ed63 with SMTP id 98e67ed59e1d1-3b0afbf69a0mr944027a91.8.1793857740594;
Wed, 04 Nov 2026 21:49:00 -0800 (PST)
X-Received: from cantal.lan ([58.136.77.237])
by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b0b319f2c9sm1989676a91.10.2026.11.04.21.48.54
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Wed, 04 Nov 2026 21:49:00 -0800 (PST)
X-Cc: Andrew Lunn <andrew@lunn.ch>,
Russell King <linux@armlinux.org.uk>,
Conor Dooley <conor.dooley@microchip.com>,
Broadcom internal kernel review list <bcm-kernel-feedback-list@broadcom.com>,
Thomas Gleixner <tglx@kernel.org>,
John Stultz <jstultz@google.com>,
Stephen Boyd <sboyd@kernel.org>,
Miroslav Lichvar <mlichvar@redhat.com>,
Vladimir Oltean <olteanv@gmail.com>,
Hubert Feurstein <h.feurstein@gmail.com>,
netdev@vger.kernel.org,
linux-kernel@vger.kernel.org
X-Mailer: git-send-email 2.56.0
X-In-Reply-To: <20261105054839.2248046-1-jjc@jclark.com>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list