From nobody Sat Nov 30 07:29:37 2024 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 355981AB6DE; Wed, 11 Sep 2024 13:30:09 +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=1726061412; cv=none; b=uE+asqOHpF9MsGQyhc7GCQ/ODAGbKt+ypLgejJeqJMvcFAbb9o+dnZBGQUrxQxyKADhT+QVk3Nq3QxpCa0i73qxEXAYFKXa9PVRaAgqQPtWGnd9SCjexeLTjz/Qgtsse1ad1p8SbaqakjUHWuqwXh5eSvNAUlJzxVmvJWdis1+M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061412; c=relaxed/simple; bh=ox0s9PJXWFpTCrPvfAeVPOVh8wK4ESKTGk2UMgK6/to=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BsKjPQlNGfL7Fw8+J+AGVHKG6F/2OPNza4zStVX6DmEvwEkJHjMQJRgoEvefTJ2FfnlprjISZAa1vHZD0CBq5S4MgFxIrR27nLD/NYc9UIqn2iW+61ol0sOKtvTIojnt4f2HtKauVMY2jeYWqRn0hk1vwyN5f55As6abPK/Sc/s= 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=epnJ3ol6; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=AyrFDUBw; 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="epnJ3ol6"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="AyrFDUBw" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061408; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=66toSkU2qPO2OJQytBwFOIzsoNiVM6CXHSHR4Cf98KQ=; b=epnJ3ol6a8ShnmCJPZbtvdoL+6frs3Xlewbi9i8ccYJ0LGRldmcBrq9MxEvWd7c0GYPogv LfrRKDLFV19EUGb/mS0F1y4UPpi6nvPftZ/jqJpAVyeCBQbahKGJZfCjxIknVzPKCXG/TP 1ZiHCyDWFwi/kdN9oQ+h4VOv1rKWyaw0s3rVbk50w0VyTz5uoAaIUYy+571ZhhHw/yMqCe F6Yu3ybYxlMzaUcrv204PQrnNC/rgV8vbCngeLg3VS3+Tlf8XkHTjXCodaUEGcHe9R4TDY S6BDII/oz4Q9seNVOk08DTz07Tk/h2ea8ZVHcSHk5vI0A1bgPPRjKOa1OVCxCA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061408; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=66toSkU2qPO2OJQytBwFOIzsoNiVM6CXHSHR4Cf98KQ=; b=AyrFDUBwsrGpMfboCXqoPMXzAgEXfrYYmzJQOm1fj2utrMy1uzgagiAB/mSDLkVbatdy4L QP9Yt9psxtMBxZDA== Date: Wed, 11 Sep 2024 15:30:00 +0200 Subject: [PATCH 16/24] timekeeping: Rework timekeeping_inject_offset() to use shadow_timekeeper Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-16-f7cae09e25d6@linutronix.de> References: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-0-f7cae09e25d6@linutronix.de> In-Reply-To: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-0-f7cae09e25d6@linutronix.de> To: John Stultz , Frederic Weisbecker , Thomas Gleixner Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, Miroslav Lichvar , Richard Cochran , Christopher S Hall , Anna-Maria Behnsen Updates of the timekeeper can be done by operating on the shadow timekeeper and afterwards copying the result into the real timekeeper. This has the advantage, that the sequence count write protected region is kept as small as possible. Convert timekeeping_inject_offset() to use this scheme. That allows to use a scoped_guard() for locking the timekeeper lock as the usage of the shadow timekeeper allows a rollback in the error case instead of the full timekeeper update of the original code. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index efcfb0da351e..8b8d77463f3e 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1519,40 +1519,31 @@ EXPORT_SYMBOL(do_settimeofday64); */ static int timekeeping_inject_offset(const struct timespec64 *ts) { - struct timekeeper *tk =3D &tk_core.timekeeper; - unsigned long flags; - struct timespec64 tmp; - int ret =3D 0; - if (ts->tv_nsec < 0 || ts->tv_nsec >=3D NSEC_PER_SEC) return -EINVAL; =20 - raw_spin_lock_irqsave(&tk_core.lock, flags); - write_seqcount_begin(&tk_core.seq); - - timekeeping_forward_now(tk); - - /* Make sure the proposed value is valid */ - tmp =3D timespec64_add(tk_xtime(tk), *ts); - if (timespec64_compare(&tk->wall_to_monotonic, ts) > 0 || - !timespec64_valid_settod(&tmp)) { - ret =3D -EINVAL; - goto error; - } + scoped_guard (raw_spinlock_irqsave, &tk_core.lock) { + struct timekeeper *tk =3D &tk_core.shadow_timekeeper; + struct timespec64 tmp; =20 - tk_xtime_add(tk, ts); - tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *ts)); + timekeeping_forward_now(tk); =20 -error: /* even if we error out, we forwarded the time, so call update */ - timekeeping_update(&tk_core, tk, TK_UPDATE_ALL | TK_MIRROR); + /* Make sure the proposed value is valid */ + tmp =3D timespec64_add(tk_xtime(tk), *ts); + if (timespec64_compare(&tk->wall_to_monotonic, ts) > 0 || + !timespec64_valid_settod(&tmp)) { + timekeeping_restore_shadow(&tk_core); + return -EINVAL; + } =20 - write_seqcount_end(&tk_core.seq); - raw_spin_unlock_irqrestore(&tk_core.lock, flags); + tk_xtime_add(tk, ts); + tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *ts)); + timekeeping_update_staged(&tk_core, TK_UPDATE_ALL); + } =20 /* Signal hrtimers about time change */ clock_was_set(CLOCK_SET_WALL); - - return ret; + return 0; } =20 /* --=20 2.39.2