From nobody Sun Dec 14 08:03:34 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 2280D2C2AA8; Tue, 13 May 2025 15:13:36 +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=1747149217; cv=none; b=ltymLq5YQYFKt7Eo2D30vPiWyToLyJNVcvgtQmx6rTELTD9+oitOGojvUAvLAvOmmxsi4xBHeOvoXP1HUIN5kLuNP4awgg2Ao/OIUpZQ4ue0x3LnSVpRDdNugpNm6M+V+i/62bc33qhL75iIVS0wGAKrki8qGoay0q+w72xlpi0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747149217; c=relaxed/simple; bh=q0OLLkf2kUwS/yqPJjKV/EGMjakB97BItyqHxvQT36k=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=VehzDRxeP3Xed58cEtBoLBI1f1Lxou42ySPu1PZ6sKZOqDYsOxwkBTqBNx6tbtQ68CzTUVNUIMBtgDvfYEFAWJtz9oJ8WnSkgMTSa5DmWxLxNWpHycpaoPyq74HygP66b4Qj2ZBiYEyvE1bLK+OlmPai20Ro7uH2c7sBFKo0BrM= 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=fheNSDQK; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Y0ET2vDb; 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="fheNSDQK"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Y0ET2vDb" Message-ID: <20250513145137.919542613@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1747149214; 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=c3VDQSpxT2VVjCswcVkkZBuHllPOrYNSkdgO67m/MB8=; b=fheNSDQKMNENPum6m4F9BC4fjQceYCWPNb8dCdG+bPOPj9Ug2Lkxkycai66yVCi6e+kJjW QeBhk5A8GuwExw7wPOj/VwUmsdg1kRq732ObIS3SKcOjlCkng3WrkvwWeGMIGHnNwjxUZD Q3hE2CKkvRY6jU1ytmoLBO9LjWf9xLtBHdZb2ndiLIqwF6RaSOyUHeCXxywFDvWKavKr/D 3EFlqJ14goXPC2Tx1/T7/2Rc/wn3o/acJRs1NFkYPqsF7JuTFdcCEZQiCL6khg7HRGgs+r 04m3kOChV+WYGjRX/x16PHy6FzESDI7TCoQyfacbtjAT26THi6k7FCrtQsYNCw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1747149214; 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=c3VDQSpxT2VVjCswcVkkZBuHllPOrYNSkdgO67m/MB8=; b=Y0ET2vDbQcr24pKUVwgttwD15oau71IAMWtEuLLmrTkI+VYLZe/9tuLQeioyakBAmWsB04 cwlwejXWAqXiWfDQ== 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 21/26] timekeeping: Add PTP clock support to __timekeeping_inject_offset() 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:33 +0200 (CEST) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Redirect the relative offset adjustment to the PTP 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_PTP_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_ptp, 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_ptp =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; }