From nobody Sun Dec 14 07:53:22 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 8D3672C1E00; Tue, 13 May 2025 15:13: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=1747149214; cv=none; b=koBUdH2ZtLBabN32pNGZuUlilAaEXYlNcVZh9MYq3TzAOG+k11bA8IVtg/25GkOZXkC5xMHRcN5o+p3htEV4D7jmGEDUus12oAyrfqVN0RvNZer60s5Q2r4dd6l6ZGxSAv7aGA9NsFQnzryPdeUvlhj0zidirmZ7w1/Z06hzMKI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149214; c=relaxed/simple; bh=Z3OeIgNNvf+8U8VB8QsiVfDcY9uW7DIKl84gjExM2zY=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=OsiLTtFpZ5jKbQjGBPKKoIBMYbFjaciWCvb/doBNXDPYQ8hvAFptS/vyySc78rUsZGxIbP46Vq5v6lKpKmYeovtNr2ugbntRK3ByKGegiK4yq5Ogm1oHLanYlXQlDEJcaRYLiaUHOO/xk8NQwzdurCjIoLHmpkBn1s79pZEuvxY= 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=NMZXkrIO; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=VGc7tcpW; 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="NMZXkrIO"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="VGc7tcpW" Message-ID: <20250513145137.799907954@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149210; 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=/YoB2Y/szX/3aLjPyHcg6yaHtAbNxEpl5yLobOxiZKI=; b=NMZXkrIO0c/JypbhWv9LrorjGB3VdMX1Q3X/aIFmrmiYhgPsBN2l/bCZChg43HmtHnSPXg casq8pmhPQaoDqjAAXYeayP99ILpHy6/VLrC/NPMWW3ZOK+OYaeieYHKqUEw8Zo3T9lBh4 /6DG9Svjq7GCWHJownNtEwOee1UgtwziRT2Niy5Ohrmpu8mc33ADcQmPxBFBtvx2IciIv/ nyMAP848DlkT1EjLIossoSOJ2w4olP/+sVon+2eWGfi2gxV2sg0GY/qMwJzWnMC7VhbnTj Rf14P+i/i5Mqmko5NpoUqYFzBJebOiKjb/OZerbuoT5OsYwS3M3Ga7c3/uETew== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149210; 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=/YoB2Y/szX/3aLjPyHcg6yaHtAbNxEpl5yLobOxiZKI=; b=VGc7tcpWniRriVVr83N+/ev9FzqjDYtEhbsqBMaRD8At6ikrxN4ecX3G2LZJJvPbFOqKBz J6ZPH1VohS/iUNBA== From: Thomas Gleixner To: LKML Cc: netdev@vger.kernel.org, Richard Cochran , Christopher Hall , David Zage , 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 , Alex Gieringer Subject: [patch 19/26] timekeeping: Provide time setter for PTP clocks References: <20250513144615.252881431@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Tue, 13 May 2025 17:13:30 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add clocK_settime(2) support for PTP clocks. The function affects the PTP offset which is added to the "monotonic" clock readout. 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 @@ -2759,9 +2759,48 @@ static int ptp_get_timespec(clockid_t id return ktime_get_ptp_ts64(id, tp) ? 0 : -ENODEV; } =20 +static int ptp_clock_set(const clockid_t id, const struct timespec64 *tnew) +{ + struct tk_data *tkd =3D ptp_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 PTP offset */ + tks->offs_ptp =3D ktime_sub(timespec64_to_ktime(*tnew), tnow); + + timekeeping_update_from_shadow(tkd, TK_UPDATE_ALL); + return 0; +} + const struct k_clock clock_ptp =3D { .clock_getres =3D ptp_get_res, .clock_get_timespec =3D ptp_get_timespec, + .clock_set =3D ptp_clock_set, }; =20 static __init void tk_ptp_setup(void)