From patchwork Wed Sep 11 13:29:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800572 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 DC76E1A3055; Wed, 11 Sep 2024 13:30:06 +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=1726061410; cv=none; b=UziTl1uFJstbjKV6crLzasBlIGfxKMhdNUK1LU4rLuZYNStLVXy4XRTzfRTu8jttO0J3+g5puwIq0upMbQ1BGy20HWkkCNsKHqfkyKn6yEVudnlXI7aT1I8V/IDVM09iy2kvKbkr9GsGe5SHkfKyFYceiAFIlsx7jhQO12wmGc0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061410; c=relaxed/simple; bh=vA7knna/LPwJfgUOP3ICSdr+zzkqbT3SeaB70fpeZYg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=OwKy7a4462I5F5tiqmFblrWKDj+UizrsWkpTL4uDeJt9rKIvpw+rrnLHw4F0WkWPJVb4aWYoa2Ut6Q+h6Qx4MevwsmCBm7zmXu2YHiRSXHa01uHvUlouoOW7fYC+S1TffZcuTxZ/nxyBi2JlJD9sUcSXrFgRDt5lyw1QOWq8xUk= 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=ERnRXz9t; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Zf5BhUST; 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="ERnRXz9t"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Zf5BhUST" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061401; 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=PA5NleTI3O9VX4sRx0U/XV0SMMtee51xgFSGfg7aJ7k=; b=ERnRXz9t6uXqimsAbceRTuGLpccpxh8saHvNsVM+/ZX59KDjwI8rTr6hVElM8ZiKzqc00f kQ1Qm6gmvFiX5g6vU0Pe6YkLX35VpBGyOQVTMMkTzj1HTTlXWr5djbFEKnc7hJMGbhW5hO n6SZHuHshYXYuh8skPHKFYQtbjr+lJ4Wq2TFVyi2YZF8qg6LSs7HxNA0ttEFO1g6o3CAgN Tt1cuPsZXH7lYkI/3NuCExVhN3wREoeho4bO32sBqOkcNKTXtqTgzcKr2mxXR1V2DKUkTd 8zA59sslR4rLZJTTTdPSgXOseX7boo1hMH9vhnPCz3+THAsHcngAjGjTP7H21g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061401; 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=PA5NleTI3O9VX4sRx0U/XV0SMMtee51xgFSGfg7aJ7k=; b=Zf5BhUSTJDSV0Do7rSkYhzvp/cA0MeEBykr3ustNj0XHvaanM0t77Wh3eJu04r9e+2scSK FsMdOi32w/MamACw== Date: Wed, 11 Sep 2024 15:29:45 +0200 Subject: [PATCH 01/24] timekeeping: Read NTP tick length only once Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-1-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 From: Thomas Gleixner No point in reading it a second time when the comparison fails. Signed-off-by: Thomas Gleixner Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 5391e4167d60..b877173f88bb 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2130,16 +2130,17 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk, */ static void timekeeping_adjust(struct timekeeper *tk, s64 offset) { + u64 ntp_tl = ntp_tick_length(); u32 mult; /* * Determine the multiplier from the current NTP tick length. * Avoid expensive division when the tick length doesn't change. */ - if (likely(tk->ntp_tick == ntp_tick_length())) { + if (likely(tk->ntp_tick == ntp_tl)) { mult = tk->tkr_mono.mult - tk->ntp_err_mult; } else { - tk->ntp_tick = ntp_tick_length(); + tk->ntp_tick = ntp_tl; mult = div64_u64((tk->ntp_tick >> tk->ntp_error_shift) - tk->xtime_remainder, tk->cycle_interval); } From patchwork Wed Sep 11 13:29:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800563 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 9141719CC2D; Wed, 11 Sep 2024 13:30:03 +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=1726061405; cv=none; b=bXiVW77jZdY0Xgr1sfpaD522tmXbf9X2pGqC+VykIyTYJkGaBAbKNHOKxKeIIrj8/z7g0FDPf7misiCKPYMpW1Nibp/AbyyfRl3AG5Ue7wCr3UzNRx71mtxJLXftWbK1GleVdDxYmrQckHaCUD6bOlc4WKtOpkqA8OgtyCNEEh0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061405; c=relaxed/simple; bh=8lkN6FU0ipTqqsDOVgVdI2Ku2G1wbeNB6dIpDrLcO2I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=D3UdFtMTBfdvqs0FV3yiusFObd410np/r7OsnCzP89aISLSo7KuPxtTxNOjfxND8K84lJW9LMAfVq6fZXp5bMsM3QwHuMEqmLTr4TsAHDsjMWjyNIBvThDRV9xwpJnFAiHxSVHoY9Cr4tf51+gpKpD+IbmJEPUZnbtEuME6SCLM= 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=dRxx1voS; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=j+iZwqD7; 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="dRxx1voS"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="j+iZwqD7" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061401; 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=dQuAsuMoVvyWJ2WjfEu5LT2yoBEdFYjUQ3IOgFGdbMU=; b=dRxx1voS1wXy+stIMSRhWowCDCQec/A8hKux6aVZyOVWUzDt36xLZ4PHT07bBJ9OHyQPpW +20/FnW24xWkfN0xQg6XhC5SyRQyKWS6qv63naDVx/a9Jxh+iAIZp/uFK977OxnTSaexuM wilyEOZ9jR8pjBqtWsJWI5b+v0/cIQz5syqzlewd0p/GcxXAOcfCvzjYPnpm463i/4v6Vx gnsR3gGC7aBOEuPZriDPjH2gy2mwOOYbcpQRIAOdL3CME6U7yLeBb0grASdRoFzuWZRlPP ot1aQeGPUXnz443GMUcEwZ52vOXBs+AfcaPQ+2IVEOUQiOTPzOfSpv88HzR8vg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061401; 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=dQuAsuMoVvyWJ2WjfEu5LT2yoBEdFYjUQ3IOgFGdbMU=; b=j+iZwqD7sZa1063J9nv+4IlmjtQ2pOG5rs/LOIwakSBFBq3NoJX/MGBylD2coxUODCdyZX oZal+QHBbVfl5mBw== Date: Wed, 11 Sep 2024 15:29:46 +0200 Subject: [PATCH 02/24] timekeeping: Don't stop time readers across hard_pps() update Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-2-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 From: Thomas Gleixner hard_pps() update does not modify anything which might be required by time readers so forcing readers out of the way during the update is a pointless exercise. The interaction with adjtimex() and timekeeper updates which call into the NTP code is properly serialized by timekeeper_lock. Signed-off-by: Thomas Gleixner Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index b877173f88bb..f09752bae05d 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2625,11 +2625,7 @@ void hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts) unsigned long flags; raw_spin_lock_irqsave(&timekeeper_lock, flags); - write_seqcount_begin(&tk_core.seq); - __hardpps(phase_ts, raw_ts); - - write_seqcount_end(&tk_core.seq); raw_spin_unlock_irqrestore(&timekeeper_lock, flags); } EXPORT_SYMBOL(hardpps); From patchwork Wed Sep 11 13:29:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800564 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 E786F19CC38; Wed, 11 Sep 2024 13:30:03 +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=1726061405; cv=none; b=uCkoEvwIc99lBfc9XsfWva/iYUS2r/QT3lvlSu22WiIZ4gArXsFbIkcLXUxIJxYMAiw5lyfWOSWsKfCrisHvsCgnBPEqEcGrYr74Bl6+tR4Y92UejD6hPQpJnf1qKBt4wgxAOWVHm0RrWl4Hqua+BOoP1hEwzBFFAZu1xSAHTk4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061405; c=relaxed/simple; bh=+/aatAAiCH15R6VhkwD1iHjBqaQ+y686NMi5tV/lbM8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Mc/mO5x/W6NoVJmlGk0LShChUIqj8ch9lgoaZ7quDYmyu4LOmX8sUU9zajVzpvH0DJ1Bc1sKMWIfZWM8bjoU7C1/Ay/NnP37tauWMu2VsFdwaM4Fpdb+MyeCjBluvAyNovOaiajIgbQtuBJeQNfaOTIuquoO1nuBU3pRpCpoKiw= 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=hcgx5shT; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=mTykQoqQ; 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="hcgx5shT"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="mTykQoqQ" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061402; 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=JF7nVikN+HjS8VpIH1kU477XnGTf/XwkOQenK2xlllM=; b=hcgx5shTMy9PKonKL10+O0zDf7HcpC6KKwx/Bl4CHTQyqtyJqoRMCrHu7Gv7g2mhC8yerA 9mQSCkk88NFe0C2uB8uRuB4TR9VkwejtlyXV6zBinPZeYalmc2uSx+ZMB1Qi8yWXRFUgjF SaLFD11QKQP1WFwSduINJGmV/QmUFETg7XyP/qALVQQVxhE11mlhsCtXYQztCy2ZO0Akod YID7ZqjZhtqfsMT9UtoQJlpfz4S1fRTHKgUh6rXFqGEO89Oa6uIixlQ3J1SpGapuv378hq lQQtdCdg27KpXaNj6Ylfmgby14RF3BqnhCtpRG2JXlCRlwvUqCsGnDGMer1AZQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061402; 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=JF7nVikN+HjS8VpIH1kU477XnGTf/XwkOQenK2xlllM=; b=mTykQoqQ/rFTp24vM45Zw3vIr9bHy7OHzyNAoVno4IMOUh4PlyWg5ZQtpGaYCyY2O7RrGJ rjhqLPRGW8RiS8BA== Date: Wed, 11 Sep 2024 15:29:47 +0200 Subject: [PATCH 03/24] timekeeping: Avoid duplicate leap state update Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-3-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 do_adjtimex() invokes tk_update_leap_state() unconditionally even when a previous invocation of timekeeping_update() already did that update. Put it into the else path which is invoked when timekeeping_update() is not called. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index f09752bae05d..bdc4a6fe040d 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2594,8 +2594,9 @@ int do_adjtimex(struct __kernel_timex *txc) __timekeeping_set_tai_offset(tk, tai); timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); clock_set = true; + } else { + tk_update_leap_state(tk); } - tk_update_leap_state(tk); write_seqcount_end(&tk_core.seq); raw_spin_unlock_irqrestore(&timekeeper_lock, flags); From patchwork Wed Sep 11 13:29:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800565 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 4352119E973; Wed, 11 Sep 2024 13:30:03 +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=1726061405; cv=none; b=Ab7B7SdGmS81FPZ2YRcnO0nNJp4HCwjelN5QdcgTYy3R1ptde/SmxRR3QHE3e0S64+8IRvf/KzJ4QJPc+HiE/TtoISZx0rYixUa8EtW8EpYfzsfXIY7osAIHVKhlsjsScZrtgtxwRFPqDDOJvC4LruE0i/L4wWluokMj3Nw2d2E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061405; c=relaxed/simple; bh=tt3A5eJHa/2iVsuRVo1WQOjrhyaCN2hfa0cAUYrZY+M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=LiI+0x8Y8YgXpIVE8r16O8kEUdCnYFMeB65Va5eOTVBcI2Iwrl72l4Ca5mnSYeS6T1T7aC5Sa9dXDBGv4u8hIzOSWfp6ZwAFs1T8/b+AbpB467r6Clc95GXxMTfAw/27W/dBbev1xADFKCl7J9tGEOT/7j83/uHI77EG7sm7l3g= 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=xH5zMWNZ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=bak4uDoW; 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="xH5zMWNZ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="bak4uDoW" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061402; 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=+1OCiJ956B+lVO6X7ZOjrSYj+KbykSrrWsWmDzkB17o=; b=xH5zMWNZZkjckTZVYIbFJ6mOwFSfew/0/gwIsEFt8jXVyLRIxEpVfHe1dQ644g9SZJz6cR Iha4PkUQiHdOYxUpH3590RYcnLhm7gPV21mOagyS8BIcgEUGC4wsVL6SLzcCeEMd4zv51e MzcxTsC4X2y2AczhWcstDjC0aVTRdtL9pLIFpBrWVoW2FjRdVNVbvGDDZ4ix5zVfnzK1VL BEeGPp+ybkm9SXoKS/1Xi/60Hk0YVlru2x/Qs2ed9qy8WAKRpVv3L6Af2C9L/HBk4mNd4J e7g/MUKP3T6DMtI4iU+ASJ1Lv7hCIdB3wPpk64E8jKkyKxodgQN7Kn4AnrIruw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061402; 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=+1OCiJ956B+lVO6X7ZOjrSYj+KbykSrrWsWmDzkB17o=; b=bak4uDoWNIpndtdnk2U6MZ5v3xTa/rc/o1MTRAMUpvF3feYVRdpL/xcxGb2RdbFVOU9Qza OQExpZNAezdg49Dw== Date: Wed, 11 Sep 2024 15:29:48 +0200 Subject: [PATCH 04/24] timekeeping: Abort clocksource change in case of failure Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-4-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 From: Thomas Gleixner There is no point to go through a full timekeeping update when acquiring a module reference or enabling the new clocksource fails. Signed-off-by: Thomas Gleixner Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index bdc4a6fe040d..7b80dd70062c 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1577,33 +1577,29 @@ static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset) static int change_clocksource(void *data) { struct timekeeper *tk = &tk_core.timekeeper; - struct clocksource *new, *old = NULL; + struct clocksource *new = data, *old = NULL; unsigned long flags; - bool change = false; - - new = (struct clocksource *) data; /* - * If the cs is in module, get a module reference. Succeeds - * for built-in code (owner == NULL) as well. + * If the clocksource is in a module, get a module reference. + * Succeeds for built-in code (owner == NULL) as well. Abort if the + * reference can't be acquired. */ - if (try_module_get(new->owner)) { - if (!new->enable || new->enable(new) == 0) - change = true; - else - module_put(new->owner); + if (!try_module_get(new->owner)) + return 0; + + /* Abort if the device can't be enabled */ + if (new->enable && new->enable(new) != 0) { + module_put(new->owner); + return 0; } raw_spin_lock_irqsave(&timekeeper_lock, flags); write_seqcount_begin(&tk_core.seq); timekeeping_forward_now(tk); - - if (change) { - old = tk->tkr_mono.clock; - tk_setup_internals(tk, new); - } - + old = tk->tkr_mono.clock; + tk_setup_internals(tk, new); timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); write_seqcount_end(&tk_core.seq); @@ -1612,7 +1608,6 @@ static int change_clocksource(void *data) if (old) { if (old->disable) old->disable(old); - module_put(old->owner); } From patchwork Wed Sep 11 13:29:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800566 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 D66551A3043; Wed, 11 Sep 2024 13:30:05 +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=1726061407; cv=none; b=Fun8U0+WYiL0rU3MWMbvBcOGCH4bz89mG44uIykIrZsxmwCXQrIIvLWiZNJq5OALhT5OJQwwy3SuCa/TW1PFroO9cI96coEKOLfH0MK9oBJs9zDcEQEd3rOsNeSXrFqeGtvlFnhvzwmua8rjbwasZXjPUJgkrhACuRd+8PSpvz4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061407; c=relaxed/simple; bh=PnO7iY0qb9wKBk+lNK9zKYpcCsnADcO7+We9SGiechU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KNROmnob7kCxdZSKSntnmssVBnU7zegrlL6t+SMehsdCrLlJYQ29y+A8WxyYP4UR7iqhH7XZ90/M24eDjci47YK3RyMv1n5rBlrlVLE4CjK3Kdn3GysIwmZ/y8cOMWlDB2N8kouHuTJKHCKsWm1uvFemXfjVg2exqqOQm8ZhKOw= 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=IQwDzQom; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=gjHI0JEH; 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="IQwDzQom"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="gjHI0JEH" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061402; 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=a0N/XaroQFICqgnhK3vGduw+f5at/sQsia+TyAf5Ymk=; b=IQwDzQom4mFyGgJS1W8v3bIBRxaR9WzXvmjao+NVfnKtY28JzDXBfewFhvvxNZ0zC91hac 8Scxeyuse+eYguoHe7Dr7wgA6VbyUPPZ6xMcDtBWjcrAc0N0rs0HECMVGi/Rke67zSIB80 qtRPJoK/Uu/0AsM4nfw7YypZJRki5gCzOV2c3SmxbIZ84vjsMnxEnpROoWSvW4JiGYn4ak J+/UtnPwqR3uEf9y2SzlsQ+WHgdTTHisq1K3runPd5L5EYZJ7TJZOglYJx1a7Kpzo7Nlut /kC/lhUltlfrKvHJsClVzowU1c1SnO0/ByA2QsTti1cYxws1tKTuOghFrKDf1g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061402; 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=a0N/XaroQFICqgnhK3vGduw+f5at/sQsia+TyAf5Ymk=; b=gjHI0JEHd359mnwhQO4wzoawo2qZtvNK/coT0tZUsg/Mp/nTE1iVkXkhdhvEYHdQ+Vd/O1 lnHEHjMx+2/elaDw== Date: Wed, 11 Sep 2024 15:29:49 +0200 Subject: [PATCH 05/24] timekeeping: Simplify code in timekeeping_advance() Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-5-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 From: Thomas Gleixner timekeeping_advance() takes the timekeeper_lock and releases it before returning. When an early return is required, goto statements are used to make sure the lock is realeased properly. When the code was written the locking guard() was not yet available. Use the guard() to simplify the code and while at it cleanup ordering of function variables. No functional change. Signed-off-by: Thomas Gleixner Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 7b80dd70062c..e528e283523b 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -2276,23 +2276,22 @@ static bool timekeeping_advance(enum timekeeping_adv_mode mode) { struct timekeeper *real_tk = &tk_core.timekeeper; struct timekeeper *tk = &shadow_timekeeper; - u64 offset; - int shift = 0, maxshift; unsigned int clock_set = 0; - unsigned long flags; + int shift = 0, maxshift; + u64 offset; - raw_spin_lock_irqsave(&timekeeper_lock, flags); + guard(raw_spinlock_irqsave)(&timekeeper_lock); /* Make sure we're fully resumed: */ if (unlikely(timekeeping_suspended)) - goto out; + return false; offset = clocksource_delta(tk_clock_read(&tk->tkr_mono), tk->tkr_mono.cycle_last, tk->tkr_mono.mask); /* Check if there's really nothing to do */ if (offset < real_tk->cycle_interval && mode == TK_ADV_TICK) - goto out; + return false; /* Do some additional sanity checking */ timekeeping_check_update(tk, offset); @@ -2311,8 +2310,7 @@ static bool timekeeping_advance(enum timekeeping_adv_mode mode) maxshift = (64 - (ilog2(ntp_tick_length())+1)) - 1; shift = min(shift, maxshift); while (offset >= tk->cycle_interval) { - offset = logarithmic_accumulation(tk, offset, shift, - &clock_set); + offset = logarithmic_accumulation(tk, offset, shift, &clock_set); if (offset < tk->cycle_interval< X-Patchwork-Id: 13800568 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 D7E641A302A; Wed, 11 Sep 2024 13:30:05 +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=1726061408; cv=none; b=jVYJEovc/cnXvuwT7MaxyXM8AJXvgogPyOoD0MOqKbSGBrWV1qCcUMYi70yOT058gU5KmKhVDwHGQYfadMNdIm1i3xUV2y7v0vJu5HKE/cLTQ3oG4fghkXnkfCTnsinDN0cxttq69TihN7xVg5ncDpDgFCIAdNDy6i4QlW2SxKU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061408; c=relaxed/simple; bh=h059DRDIOUm5DG+YjLjsJhhFglZkS1x9w23A7wdranc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=J4A/LuaY1I6UB259ygi5licMmt8JPMTNF+s2osOj2y7ULwkjOiiZDJ+QDUF5jZjpq+0hFNiEGLUWn6sJKF41Hx1ZlxVZuIra2/O42RPWOS5HelCeeV0nmtULyx5FYWB0B+NJAWAzPSYJX6otHoUUIhogzanRnuW8ZuU+AnOIVtY= 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=UnLpyDAA; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=/9pxoruL; 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="UnLpyDAA"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="/9pxoruL" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061403; 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=VA8zYGtXLl150Y6W6sqapaYVnfPkW2AtTY25TXW2wTM=; b=UnLpyDAAqNF2Cn3v8pv985iAs4L5Hd+QWMe7CZf66R+9H7a77XiOlsGojHO43f7kq483u5 G0ccJKiY/Jdx7amNrRqrLbfO7T5XL9CasYQ/xdpVuvJ0CSz7xW67RFgLGmTDGO/y0EDk++ XWNYQ29kDSF5rPq4051EOSAVHDijz++xnSVSJ4pAP3BXyutIKzSopgAo/8E6jKfT2FMFzi AwQYSZwzIAyyQVE+S5J0UICm19FzQgSM0Ymfh/JrzXvUTSP47GBQkBK52uriSBJ71ZAT4Z JnR0IkoCqd6Zilw6agxV0qdJ2RG0XcVEfhAhxMQ52Y8ViD9+3IACl0opg4OfcQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061403; 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=VA8zYGtXLl150Y6W6sqapaYVnfPkW2AtTY25TXW2wTM=; b=/9pxoruLxlvl732jAFd4XHnrophok9RniD7Q//5YHBf48sYL38PU6hMMozt1KVooYc4wC+ V2QRM+rmz9ruAIAw== Date: Wed, 11 Sep 2024 15:29:50 +0200 Subject: [PATCH 06/24] timekeeping: Reorder struct timekeeper Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-6-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 From: Thomas Gleixner struct timekeeper is ordered suboptimal vs. cachelines. The layout, including the preceding seqcount (see struct tk_core in timekeeper.c) is: cacheline 0: seqcount, tkr_mono cacheline 1: tkr_raw, xtime_sec cacheline 2: ktime_sec ... tai_offset, internal variables cacheline 3: next_leap_ktime, raw_sec, internal variables cacheline 4: internal variables So any access to via ktime_get*() except for access to CLOCK_MONOTONIC_RAW will use either cachelines 0 + 1 or cachelines 0 + 2. Access to CLOCK_MONOTONIC_RAW uses cachelines 0 + 1 + 3. Reorder the members so that the result is more efficient: cacheline 0: seqcount, tkr_mono cacheline 1: xtime_sec, ktime_sec ... tai_offset cacheline 2: tkr_raw, raw_sec cacheline 3: internal variables cacheline 4: internal variables That means ktime_get*() will access cacheline 0 + 1 and CLOCK_MONOTONIC_RAW access will use cachelines 0 + 2. Update kernel-doc and fix formatting issues while at it. Also fix a typo in struct tk_read_base kernel-doc. Signed-off-by: Thomas Gleixner Signed-off-by: Anna-Maria Behnsen --- include/linux/timekeeper_internal.h | 102 +++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 41 deletions(-) diff --git a/include/linux/timekeeper_internal.h b/include/linux/timekeeper_internal.h index 84ff2844df2a..5805c5f61df4 100644 --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -26,7 +26,7 @@ * occupies a single 64byte cache line. * * The struct is separate from struct timekeeper as it is also used - * for a fast NMI safe accessors. + * for the fast NMI safe accessors. * * @base_real is for the fast NMI safe accessor to allow reading clock * realtime from any context. @@ -44,33 +44,41 @@ struct tk_read_base { /** * struct timekeeper - Structure holding internal timekeeping values. - * @tkr_mono: The readout base structure for CLOCK_MONOTONIC - * @tkr_raw: The readout base structure for CLOCK_MONOTONIC_RAW - * @xtime_sec: Current CLOCK_REALTIME time in seconds - * @ktime_sec: Current CLOCK_MONOTONIC time in seconds - * @wall_to_monotonic: CLOCK_REALTIME to CLOCK_MONOTONIC offset - * @offs_real: Offset clock monotonic -> clock realtime - * @offs_boot: Offset clock monotonic -> clock boottime - * @offs_tai: Offset clock monotonic -> clock tai - * @tai_offset: The current UTC to TAI offset in seconds - * @clock_was_set_seq: The sequence number of clock was set events - * @cs_was_changed_seq: The sequence number of clocksource change events - * @next_leap_ktime: CLOCK_MONOTONIC time value of a pending leap-second - * @raw_sec: CLOCK_MONOTONIC_RAW time in seconds - * @monotonic_to_boot: CLOCK_MONOTONIC to CLOCK_BOOTTIME offset - * @cycle_interval: Number of clock cycles in one NTP interval - * @xtime_interval: Number of clock shifted nano seconds in one NTP - * interval. - * @xtime_remainder: Shifted nano seconds left over when rounding - * @cycle_interval - * @raw_interval: Shifted raw nano seconds accumulated per NTP interval. - * @ntp_error: Difference between accumulated time and NTP time in ntp - * shifted nano seconds. - * @ntp_error_shift: Shift conversion between clock shifted nano seconds and - * ntp shifted nano seconds. - * @last_warning: Warning ratelimiter (DEBUG_TIMEKEEPING) - * @underflow_seen: Underflow warning flag (DEBUG_TIMEKEEPING) - * @overflow_seen: Overflow warning flag (DEBUG_TIMEKEEPING) + * @tkr_mono: The readout base structure for CLOCK_MONOTONIC + * @xtime_sec: Current CLOCK_REALTIME time in seconds + * @ktime_sec: Current CLOCK_MONOTONIC time in seconds + * @wall_to_monotonic: CLOCK_REALTIME to CLOCK_MONOTONIC offset + * @offs_real: Offset clock monotonic -> clock realtime + * @offs_boot: Offset clock monotonic -> clock boottime + * @offs_tai: Offset clock monotonic -> clock tai + * @tai_offset: The current UTC to TAI offset in seconds + * @tkr_raw: The readout base structure for CLOCK_MONOTONIC_RAW + * @raw_sec: CLOCK_MONOTONIC_RAW time in seconds + * @clock_was_set_seq: The sequence number of clock was set events + * @cs_was_changed_seq: The sequence number of clocksource change events + * @monotonic_to_boot: CLOCK_MONOTONIC to CLOCK_BOOTTIME offset + * @cycle_interval: Number of clock cycles in one NTP interval + * @xtime_interval: Number of clock shifted nano seconds in one NTP + * interval. + * @xtime_remainder: Shifted nano seconds left over when rounding + * @cycle_interval + * @raw_interval: Shifted raw nano seconds accumulated per NTP interval. + * @next_leap_ktime: CLOCK_MONOTONIC time value of a pending leap-second + * @ntp_tick: The ntp_tick_length() value currently being + * used. This cached copy ensures we consistently + * apply the tick length for an entire tick, as + * ntp_tick_length may change mid-tick, and we don't + * want to apply that new value to the tick in + * progress. + * @ntp_error: Difference between accumulated time and NTP time in ntp + * shifted nano seconds. + * @ntp_error_shift: Shift conversion between clock shifted nano seconds and + * ntp shifted nano seconds. + * @ntp_err_mult: Multiplication factor for scaled math conversion + * @skip_second_overflow: Flag used to avoid updating NTP twice with same second + * @last_warning: Warning ratelimiter (DEBUG_TIMEKEEPING) + * @underflow_seen: Underflow warning flag (DEBUG_TIMEKEEPING) + * @overflow_seen: Overflow warning flag (DEBUG_TIMEKEEPING) * * Note: For timespec(64) based interfaces wall_to_monotonic is what * we need to add to xtime (or xtime corrected for sub jiffie times) @@ -88,10 +96,25 @@ struct tk_read_base { * * @monotonic_to_boottime is a timespec64 representation of @offs_boot to * accelerate the VDSO update for CLOCK_BOOTTIME. + * + * The cacheline ordering of the structure is optimized for in kernel usage + * of the ktime_get() and ktime_get_ts64() family of time accessors. Struct + * timekeeper is prepended in the core timekeeeping code with a sequence + * count, which results in the following cacheline layout: + * + * 0: seqcount, tkr_mono + * 1: xtime_sec ... tai_offset + * 2: tkr_raw, raw_sec + * 3,4: Internal variables + * + * Cacheline 0,1 contain the data which is used for accessing + * CLOCK_MONOTONIC/REALTIME/BOOTTIME/TAI, while cacheline 2 contains the + * data for accessing CLOCK_MONOTONIC_RAW. Cacheline 3,4 are internal + * variables which are only accessed during timekeeper updates once per + * tick. */ struct timekeeper { struct tk_read_base tkr_mono; - struct tk_read_base tkr_raw; u64 xtime_sec; unsigned long ktime_sec; struct timespec64 wall_to_monotonic; @@ -99,31 +122,28 @@ struct timekeeper { ktime_t offs_boot; ktime_t offs_tai; s32 tai_offset; + + struct tk_read_base tkr_raw; + u64 raw_sec; + + /* The following members are for timekeeping internal use */ unsigned int clock_was_set_seq; u8 cs_was_changed_seq; - ktime_t next_leap_ktime; - u64 raw_sec; + struct timespec64 monotonic_to_boot; - /* The following members are for timekeeping internal use */ u64 cycle_interval; u64 xtime_interval; s64 xtime_remainder; u64 raw_interval; - /* The ntp_tick_length() value currently being used. - * This cached copy ensures we consistently apply the tick - * length for an entire tick, as ntp_tick_length may change - * mid-tick, and we don't want to apply that new value to - * the tick in progress. - */ + + ktime_t next_leap_ktime; u64 ntp_tick; - /* Difference between accumulated time and NTP time in ntp - * shifted nano seconds. */ s64 ntp_error; u32 ntp_error_shift; u32 ntp_err_mult; - /* Flag used to avoid updating NTP twice with same second */ u32 skip_second_overflow; + #ifdef CONFIG_DEBUG_TIMEKEEPING long last_warning; /* From patchwork Wed Sep 11 13:29:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800567 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 552B51A38CF; Wed, 11 Sep 2024 13:30:06 +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=1726061408; cv=none; b=co/tW5oMc9xxmUvwW1Tnd4X7Evx0/ij5Dl8/UOaRJpoc8eNp12BjW+nYcS4OBe4O9YmIA29R7lMF8HdrMaFEJZd9gVGxaqdwPPmUveIKX80/sKQqDJzVcoYLECt7mVGG812pFWFN6lhE+OHPPq01wnx2GIs2fNgoPqHLoIwNMk8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061408; c=relaxed/simple; bh=HPWOFz6QlzqiLm2ycbh4HDRSVVxwgKQjS7UPR0/1PZQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dyo489kGHWHRMutec9esJEJuFSmjrfs0RArhyrO7B0L9XnlRLoFIcPXYXykqIWPOToNsQG+vYHx8KXIDt3nxxdJVdi4fipwhP0+9M51kFw51sauxujf0AIk6Zn/b9usJbaFgU6bzLyWEuJjekApj3BDATj6LvapP3PYPeGSDluw= 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=sK0U+gNR; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=tA5ZjM3s; 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="sK0U+gNR"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="tA5ZjM3s" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061403; 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=IS76DhjefyTyiG2SBhgBRCAZ9oiTQKrEuGQl4y3cz2A=; b=sK0U+gNRUgsHJFXftng12Ks1kJ44I8kLYqqQPdq8Nt4fSTVIZc4uACWl0Y3W46DyU5TOcb 0rrUVaue2jJJZP9D8flHeByawe2WVLJ93vUAZ9Ty7Pywc42l0pKfhXit9pYwfTQ+GQKwkJ NmglGzb2AQ9upqKlVDcc+pmEs7/0enEn8VXH0sxYZbMyq5YaIvVHuNXschPZyxQG+vjDGa u6oowoO8i06Ar4eniq0pVh68I8Q663agOTuBBIcJRX30veEAlO0VzKbH0+23/NC8dXL4Oq y114O2YLYxxd6anhCe/X4z50Kw6jGwDWMrmWCDTMKYCzzW/tDusSZBI4uXIe9A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061403; 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=IS76DhjefyTyiG2SBhgBRCAZ9oiTQKrEuGQl4y3cz2A=; b=tA5ZjM3sALYaMxc3hgTiB0EQDsPOsWq/HOB0NnvkGMTLqWvP86oMpAiJYdD4kWACAJwTJu uLoILAXj3nWAoyBQ== Date: Wed, 11 Sep 2024 15:29:51 +0200 Subject: [PATCH 07/24] timekeeping: Move shadow_timekeeper into tk_core Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-7-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 From: Thomas Gleixner tk_core requires shadow_timekeeper to allow timekeeping_advance() updating without holding the timekeeper sequence count write locked. This allows the readers to make progress up to the actual update where the shadow timekeeper is copied over to the real timekeeper. As long as there is only a single timekeeper, having them separate is fine. But when the timekeeper infrastructure will be reused for per ptp clock timekeepers, shadow_timekeeper needs to be part of tk_core. No functional change. Signed-off-by: Thomas Gleixner Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index e528e283523b..e35e00b0cdd4 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -50,11 +50,11 @@ DEFINE_RAW_SPINLOCK(timekeeper_lock); static struct { seqcount_raw_spinlock_t seq; struct timekeeper timekeeper; + struct timekeeper shadow_timekeeper; } tk_core ____cacheline_aligned = { .seq = SEQCNT_RAW_SPINLOCK_ZERO(tk_core.seq, &timekeeper_lock), }; -static struct timekeeper shadow_timekeeper; /* flag for if timekeeping is suspended */ int __read_mostly timekeeping_suspended; @@ -776,8 +776,7 @@ static void timekeeping_update(struct timekeeper *tk, unsigned int action) * timekeeper structure on the next update with stale data */ if (action & TK_MIRROR) - memcpy(&shadow_timekeeper, &tk_core.timekeeper, - sizeof(tk_core.timekeeper)); + memcpy(&tk_core.shadow_timekeeper, &tk_core.timekeeper, sizeof(tk_core.timekeeper)); } /** @@ -2274,8 +2273,8 @@ static u64 logarithmic_accumulation(struct timekeeper *tk, u64 offset, */ static bool timekeeping_advance(enum timekeeping_adv_mode mode) { + struct timekeeper *tk = &tk_core.shadow_timekeeper; struct timekeeper *real_tk = &tk_core.timekeeper; - struct timekeeper *tk = &shadow_timekeeper; unsigned int clock_set = 0; int shift = 0, maxshift; u64 offset; From patchwork Wed Sep 11 13:29:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800569 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 9C8431A3A92; Wed, 11 Sep 2024 13:30:06 +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=1726061408; cv=none; b=qZX7Ibl4N86w1QFTzgYEhnsoKK23Mf0Wo9cQL5DoqpvmbdYZ9fN2e6y0PzXOgmcT2bii7wYpDXN6jtIDq93iD9EbUwUgs4HHy8wjWyrFenwZx3AB0xNtw5xCuHaBlMaAoiaih/kmUW3hVONjIZlmyaUHkKKiicKhBhgQLkbmAPc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061408; c=relaxed/simple; bh=jSUFNViXFDFewvDCLCg5ppMtN092LyN9G4DUPCA35/g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Vx/r45llO/HmbUNDSAyFBFxKly9wSsDFbr6op00dvED7qQ8ajXcbRYJ94VWjxMop9M5IrvUMXXUymHJKrbthEj7PBml8tyEITwdgXB9uGTN7reYXmMETDp0oYS3Milg47W5GZhokpYKXUO3eNZAMYT5kGLjkgyXMiCR3MdrnOn8= 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=CXPoQ62Q; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=LW2CYqxm; 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="CXPoQ62Q"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="LW2CYqxm" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061404; 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=NeKBPpJLDsCADQvy1qvGnmjLCzx0cHKY8ieuOdOdZrE=; b=CXPoQ62QXUZGvjjtC0Elg8qGP+tU7uIrV5S8uFEwl5YRKsHegT+s4ELvalDOeLRPU58wUk kHP6CH/69DXMRoY5E8YfcwKRMQxMqCA63myKhyedAxnSFoak5JS/AemFMlq+5r7E60iUYz c5vxwQI8zXmmVrPg0uFSPhVNTga4HFuNdz2L/QF7VZFB1oRXtjf0p/uAIYNzBq5FvXIMBx HWsRqfpdbF3dPFNAxPq/Zs0j/7oqbC8h4rgg8PkM1e5vRienB8EIlJwLDx7KnK6uVGDZOE 6/C8asUjf/gwEJGzNmuA5XebPafDpcOzpzVcFI4NvZu5oHPzLbEy8L/0XBzKBA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061404; 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=NeKBPpJLDsCADQvy1qvGnmjLCzx0cHKY8ieuOdOdZrE=; b=LW2CYqxmjd/NnCMg5G325UGVbsMf3M5X9s9USOtIknpidpa0BF0F593WW8MAWyirX70Au7 WHBNBFtAZeA8ekAg== Date: Wed, 11 Sep 2024 15:29:52 +0200 Subject: [PATCH 08/24] timekeeping: Encapsulate locking/unlocking of timekeeper_lock Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-8-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 From: Thomas Gleixner timekeeper_lock protects updates of timekeeper (tk_core). It is also used by vdso_update_begin/end() and not only internally by the timekeeper code. As long as there is only a single timekeeper, this works fine. But when the timekeeper infrastructure will be reused for per ptp clock timekeepers, timekeeper_lock needs to be part of tk_core.. Therefore encapuslate locking/unlocking of timekeeper_lock and make the lock static. Signed-off-by: Thomas Gleixner Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 15 ++++++++++++++- kernel/time/timekeeping_internal.h | 3 ++- kernel/time/vsyscall.c | 5 ++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index e35e00b0cdd4..e0b6d088a33b 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -41,7 +41,7 @@ enum timekeeping_adv_mode { TK_ADV_FREQ }; -DEFINE_RAW_SPINLOCK(timekeeper_lock); +static DEFINE_RAW_SPINLOCK(timekeeper_lock); /* * The most important data for readout fits into a single 64 byte @@ -114,6 +114,19 @@ static struct tk_fast tk_fast_raw ____cacheline_aligned = { .base[1] = FAST_TK_INIT, }; +unsigned long timekeeper_lock_irqsave(void) +{ + unsigned long flags; + + raw_spin_lock_irqsave(&timekeeper_lock, flags); + return flags; +} + +void timekeeper_unlock_irqrestore(unsigned long flags) +{ + raw_spin_unlock_irqrestore(&timekeeper_lock, flags); +} + static inline void tk_normalize_xtime(struct timekeeper *tk) { while (tk->tkr_mono.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_mono.shift)) { diff --git a/kernel/time/timekeeping_internal.h b/kernel/time/timekeeping_internal.h index 4ca2787d1642..3babb2bac579 100644 --- a/kernel/time/timekeeping_internal.h +++ b/kernel/time/timekeeping_internal.h @@ -34,6 +34,7 @@ static inline u64 clocksource_delta(u64 now, u64 last, u64 mask) #endif /* Semi public for serialization of non timekeeper VDSO updates. */ -extern raw_spinlock_t timekeeper_lock; +unsigned long timekeeper_lock_irqsave(void); +void timekeeper_unlock_irqrestore(unsigned long flags); #endif /* _TIMEKEEPING_INTERNAL_H */ diff --git a/kernel/time/vsyscall.c b/kernel/time/vsyscall.c index 9193d6133e5d..98488b20b594 100644 --- a/kernel/time/vsyscall.c +++ b/kernel/time/vsyscall.c @@ -151,9 +151,8 @@ void update_vsyscall_tz(void) unsigned long vdso_update_begin(void) { struct vdso_data *vdata = __arch_get_k_vdso_data(); - unsigned long flags; + unsigned long flags = timekeeper_lock_irqsave(); - raw_spin_lock_irqsave(&timekeeper_lock, flags); vdso_write_begin(vdata); return flags; } @@ -172,5 +171,5 @@ void vdso_update_end(unsigned long flags) vdso_write_end(vdata); __arch_sync_vdso_data(vdata); - raw_spin_unlock_irqrestore(&timekeeper_lock, flags); + timekeeper_unlock_irqrestore(flags); } From patchwork Wed Sep 11 13:29:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800571 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 6A5691A303C; Wed, 11 Sep 2024 13:30:07 +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=1726061409; cv=none; b=AeBUf7W6t3lSUpwLv4U1V76pFYK8bzEmlRQfZ23vCgmluyREnK69dkPy0qGNTBsFTlio6an1x7gF6/4LphK/7NTQkDC4EpzCyLzYExB5CjHhgTohvDLpr2cK4z558OObdbbi4FhYS1ct5cpVQfiT8fNCjRTm6+DtSPVWh6EAabI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061409; c=relaxed/simple; bh=ODhXrGnVWSIGZQ2Jn4+Txxyjl2tcDSpnEmqplkegi80=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=AhXf+3UEKTFALdlugcGKByosb90+lJAx3WWXzx/vDAHGre2tlSkDF+4s2DITVVyNB61H+RjS6TrzlZXORo+4JhJrKsYgULjXv7XE5sBhVzX8PtGW5FwG1VedgOwvKQtiC/dTHp5qhabYYl+knSUYcmXD0D9z/Vyw0ick0bk5uSo= 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=Tfc1DX7P; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=C738bIN5; 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="Tfc1DX7P"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="C738bIN5" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061405; 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=5kLdaEQ7/PvnOBrd9Oxxc0GInZPXHgNgHezYdQRC81U=; b=Tfc1DX7PWMutc5w2uyFWoO0JhjiI5L1m83nnyTqjM1ZLPNNmyx38uJTZVk2/B23ucbxvnM +bA2hgh9s4dPKjRgjx2HZsnWry+bTIlx38f3msfLJxaiSKF4OyxcuU2TxMIrHgUBZ3XbU/ TG351n1gew8UtwNkRfT8rzdwCnTSSNj+ry4Agi83BVG33P791MRfo2QEylounDxFeguRc2 8AgilQJwUYwY3gEnrQ39H/R+9LAsOJC5tNzuzVUYpBxJZQq/JLjxOWC499oXPbbYnSFQMC Vuf8VV3R2osHDCfxGuT406MNmFNlMKblZQerQye5HcjNid0/Ep4Oa8uFQITOhw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061405; 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=5kLdaEQ7/PvnOBrd9Oxxc0GInZPXHgNgHezYdQRC81U=; b=C738bIN5F3dClma8gZNr8mmz4CNxIv9JblGaDVARlbRkEfFs8SfZbp/usUHwvqQRiWx+4c EuPCjR7u+W4POWAA== Date: Wed, 11 Sep 2024 15:29:53 +0200 Subject: [PATCH 09/24] timekeeping: Move timekeeper_lock into tk_core Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-9-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 timekeeper_lock protects updates to struct tk_core but is not part of struct tk_core. As long as there is only a single timekeeper, this is not a problem. But when the timekeeper infrastructure will be reused for per ptp clock timekeepers, timekeeper_lock needs to be part of tk_core. Move the lock into tk_core, move initialisation of the lock and sequence counter into timekeeping_init() and update all users of timekeeper_lock. As this is touching all lock sites, convert them to use: guard(raw_spinlock_irqsave)(&tk_core.lock); instead of lock/unlock functions whenever possible. Suggested-by: Thomas Gleixner Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 77 +++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 43 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index e0b6d088a33b..f8ffab5bb790 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -41,8 +41,6 @@ enum timekeeping_adv_mode { TK_ADV_FREQ }; -static DEFINE_RAW_SPINLOCK(timekeeper_lock); - /* * The most important data for readout fits into a single 64 byte * cache line. @@ -51,10 +49,8 @@ static struct { seqcount_raw_spinlock_t seq; struct timekeeper timekeeper; struct timekeeper shadow_timekeeper; -} tk_core ____cacheline_aligned = { - .seq = SEQCNT_RAW_SPINLOCK_ZERO(tk_core.seq, &timekeeper_lock), -}; - + raw_spinlock_t lock; +} tk_core ____cacheline_aligned; /* flag for if timekeeping is suspended */ int __read_mostly timekeeping_suspended; @@ -118,13 +114,13 @@ unsigned long timekeeper_lock_irqsave(void) { unsigned long flags; - raw_spin_lock_irqsave(&timekeeper_lock, flags); + raw_spin_lock_irqsave(&tk_core.lock, flags); return flags; } void timekeeper_unlock_irqrestore(unsigned long flags) { - raw_spin_unlock_irqrestore(&timekeeper_lock, flags); + raw_spin_unlock_irqrestore(&tk_core.lock, flags); } static inline void tk_normalize_xtime(struct timekeeper *tk) @@ -197,7 +193,7 @@ static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta) * the tkr's clocksource may change between the read reference, and the * clock reference passed to the read function. This can cause crashes if * the wrong clocksource is passed to the wrong read function. - * This isn't necessary to use when holding the timekeeper_lock or doing + * This isn't necessary to use when holding the tk_core.lock or doing * a read of the fast-timekeeper tkrs (which is protected by its own locking * and update logic). */ @@ -689,13 +685,11 @@ static void update_pvclock_gtod(struct timekeeper *tk, bool was_set) int pvclock_gtod_register_notifier(struct notifier_block *nb) { struct timekeeper *tk = &tk_core.timekeeper; - unsigned long flags; int ret; - raw_spin_lock_irqsave(&timekeeper_lock, flags); + guard(raw_spinlock_irqsave)(&tk_core.lock); ret = raw_notifier_chain_register(&pvclock_gtod_chain, nb); update_pvclock_gtod(tk, true); - raw_spin_unlock_irqrestore(&timekeeper_lock, flags); return ret; } @@ -708,14 +702,8 @@ EXPORT_SYMBOL_GPL(pvclock_gtod_register_notifier); */ int pvclock_gtod_unregister_notifier(struct notifier_block *nb) { - unsigned long flags; - int ret; - - raw_spin_lock_irqsave(&timekeeper_lock, flags); - ret = raw_notifier_chain_unregister(&pvclock_gtod_chain, nb); - raw_spin_unlock_irqrestore(&timekeeper_lock, flags); - - return ret; + guard(raw_spinlock_irqsave)(&tk_core.lock); + return raw_notifier_chain_unregister(&pvclock_gtod_chain, nb); } EXPORT_SYMBOL_GPL(pvclock_gtod_unregister_notifier); @@ -763,7 +751,7 @@ static inline void tk_update_ktime_data(struct timekeeper *tk) tk->tkr_raw.base = ns_to_ktime(tk->raw_sec * NSEC_PER_SEC); } -/* must hold timekeeper_lock */ +/* must hold tk_core.lock */ static void timekeeping_update(struct timekeeper *tk, unsigned int action) { if (action & TK_CLEAR_NTP) { @@ -1460,7 +1448,7 @@ int do_settimeofday64(const struct timespec64 *ts) if (!timespec64_valid_settod(ts)) return -EINVAL; - raw_spin_lock_irqsave(&timekeeper_lock, flags); + raw_spin_lock_irqsave(&tk_core.lock, flags); write_seqcount_begin(&tk_core.seq); timekeeping_forward_now(tk); @@ -1480,7 +1468,7 @@ int do_settimeofday64(const struct timespec64 *ts) timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); write_seqcount_end(&tk_core.seq); - raw_spin_unlock_irqrestore(&timekeeper_lock, flags); + raw_spin_unlock_irqrestore(&tk_core.lock, flags); /* Signal hrtimers about time change */ clock_was_set(CLOCK_SET_WALL); @@ -1510,7 +1498,7 @@ static int timekeeping_inject_offset(const struct timespec64 *ts) if (ts->tv_nsec < 0 || ts->tv_nsec >= NSEC_PER_SEC) return -EINVAL; - raw_spin_lock_irqsave(&timekeeper_lock, flags); + raw_spin_lock_irqsave(&tk_core.lock, flags); write_seqcount_begin(&tk_core.seq); timekeeping_forward_now(tk); @@ -1530,7 +1518,7 @@ static int timekeeping_inject_offset(const struct timespec64 *ts) timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); write_seqcount_end(&tk_core.seq); - raw_spin_unlock_irqrestore(&timekeeper_lock, flags); + raw_spin_unlock_irqrestore(&tk_core.lock, flags); /* Signal hrtimers about time change */ clock_was_set(CLOCK_SET_WALL); @@ -1606,7 +1594,7 @@ static int change_clocksource(void *data) return 0; } - raw_spin_lock_irqsave(&timekeeper_lock, flags); + raw_spin_lock_irqsave(&tk_core.lock, flags); write_seqcount_begin(&tk_core.seq); timekeeping_forward_now(tk); @@ -1615,7 +1603,7 @@ static int change_clocksource(void *data) timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); write_seqcount_end(&tk_core.seq); - raw_spin_unlock_irqrestore(&timekeeper_lock, flags); + raw_spin_unlock_irqrestore(&tk_core.lock, flags); if (old) { if (old->disable) @@ -1744,6 +1732,12 @@ read_persistent_wall_and_boot_offset(struct timespec64 *wall_time, *boot_offset = ns_to_timespec64(local_clock()); } +static __init void tkd_basic_setup(struct tk_data *tkd) +{ + raw_spin_lock_init(&tkd->lock); + seqcount_raw_spinlock_init(&tkd->seq, &tkd->lock); +} + /* * Flag reflecting whether timekeeping_resume() has injected sleeptime. * @@ -1770,7 +1764,8 @@ void __init timekeeping_init(void) struct timespec64 wall_time, boot_offset, wall_to_mono; struct timekeeper *tk = &tk_core.timekeeper; struct clocksource *clock; - unsigned long flags; + + tkd_basic_setup(&tk_core); read_persistent_wall_and_boot_offset(&wall_time, &boot_offset); if (timespec64_valid_settod(&wall_time) && @@ -1790,7 +1785,7 @@ void __init timekeeping_init(void) */ wall_to_mono = timespec64_sub(boot_offset, wall_time); - raw_spin_lock_irqsave(&timekeeper_lock, flags); + guard(raw_spinlock_irqsave)(&tk_core.lock); write_seqcount_begin(&tk_core.seq); ntp_init(); @@ -1807,7 +1802,6 @@ void __init timekeeping_init(void) timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); write_seqcount_end(&tk_core.seq); - raw_spin_unlock_irqrestore(&timekeeper_lock, flags); } /* time in seconds when suspend began for persistent clock */ @@ -1888,7 +1882,7 @@ void timekeeping_inject_sleeptime64(const struct timespec64 *delta) struct timekeeper *tk = &tk_core.timekeeper; unsigned long flags; - raw_spin_lock_irqsave(&timekeeper_lock, flags); + raw_spin_lock_irqsave(&tk_core.lock, flags); write_seqcount_begin(&tk_core.seq); suspend_timing_needed = false; @@ -1900,7 +1894,7 @@ void timekeeping_inject_sleeptime64(const struct timespec64 *delta) timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); write_seqcount_end(&tk_core.seq); - raw_spin_unlock_irqrestore(&timekeeper_lock, flags); + raw_spin_unlock_irqrestore(&tk_core.lock, flags); /* Signal hrtimers about time change */ clock_was_set(CLOCK_SET_WALL | CLOCK_SET_BOOT); @@ -1924,7 +1918,7 @@ void timekeeping_resume(void) clockevents_resume(); clocksource_resume(); - raw_spin_lock_irqsave(&timekeeper_lock, flags); + raw_spin_lock_irqsave(&tk_core.lock, flags); write_seqcount_begin(&tk_core.seq); /* @@ -1962,7 +1956,7 @@ void timekeeping_resume(void) timekeeping_suspended = 0; timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); write_seqcount_end(&tk_core.seq); - raw_spin_unlock_irqrestore(&timekeeper_lock, flags); + raw_spin_unlock_irqrestore(&tk_core.lock, flags); touch_softlockup_watchdog(); @@ -1993,7 +1987,7 @@ int timekeeping_suspend(void) suspend_timing_needed = true; - raw_spin_lock_irqsave(&timekeeper_lock, flags); + raw_spin_lock_irqsave(&tk_core.lock, flags); write_seqcount_begin(&tk_core.seq); timekeeping_forward_now(tk); timekeeping_suspended = 1; @@ -2032,7 +2026,7 @@ int timekeeping_suspend(void) timekeeping_update(tk, TK_MIRROR); halt_fast_timekeeper(tk); write_seqcount_end(&tk_core.seq); - raw_spin_unlock_irqrestore(&timekeeper_lock, flags); + raw_spin_unlock_irqrestore(&tk_core.lock, flags); tick_suspend(); clocksource_suspend(); @@ -2292,7 +2286,7 @@ static bool timekeeping_advance(enum timekeeping_adv_mode mode) int shift = 0, maxshift; u64 offset; - guard(raw_spinlock_irqsave)(&timekeeper_lock); + guard(raw_spinlock_irqsave)(&tk_core.lock); /* Make sure we're fully resumed: */ if (unlikely(timekeeping_suspended)) @@ -2587,7 +2581,7 @@ int do_adjtimex(struct __kernel_timex *txc) ktime_get_real_ts64(&ts); add_device_randomness(&ts, sizeof(ts)); - raw_spin_lock_irqsave(&timekeeper_lock, flags); + raw_spin_lock_irqsave(&tk_core.lock, flags); write_seqcount_begin(&tk_core.seq); orig_tai = tai = tk->tai_offset; @@ -2602,7 +2596,7 @@ int do_adjtimex(struct __kernel_timex *txc) } write_seqcount_end(&tk_core.seq); - raw_spin_unlock_irqrestore(&timekeeper_lock, flags); + raw_spin_unlock_irqrestore(&tk_core.lock, flags); audit_ntp_log(&ad); @@ -2626,11 +2620,8 @@ int do_adjtimex(struct __kernel_timex *txc) */ void hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts) { - unsigned long flags; - - raw_spin_lock_irqsave(&timekeeper_lock, flags); + guard(raw_spinlock_irqsave)(&tk_core.lock); __hardpps(phase_ts, raw_ts); - raw_spin_unlock_irqrestore(&timekeeper_lock, flags); } EXPORT_SYMBOL(hardpps); #endif /* CONFIG_NTP_PPS */ From patchwork Wed Sep 11 13:29:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800570 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 6EDC81A4AD0; Wed, 11 Sep 2024 13:30:07 +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=1726061409; cv=none; b=i4s1VJgvq9EatsGzVovZbuk+BRQywVHRJ8AzNTzNaUMLij7d2CqFcO46OWQBJc0lQhU4KwKwUFL6taJzUj2gtnBYhGtOCrCQTL4YtE2dEXV7Tqd5OhC44pMmc1BVawlJ306HrI0I7XvHAKIH8tslODecekuKKL03+AAhvyEkNak= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061409; c=relaxed/simple; bh=aeBtCMLHwDjW5RjaS1UYpwEZIfdhF+bv3HGtKlY3PgY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=q1S9I+HHwxobPfUcTCNITjqqgkfDgoSmeyzSbbZnqSy0P3vVdINu7RQwwSjLA0Pxzz1MHO4V0S3HOBeKBdTILTXXxAZj+9yUQDpSzfUuz3g0dXhttR2niDiCDYgvWSr7Yz2NiSmrXzPR3Xk5Alwz8mESKtCJiONGlCLQWcvv9nA= 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=YWgYzHzA; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=EPcm/mSM; 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="YWgYzHzA"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="EPcm/mSM" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061406; 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=5/yMD3R9spIxspDFxWIwUYuBkiPd/L8Pz+d/L53r+ac=; b=YWgYzHzA3cdcMVwu+fdQIrFpnLkcCN0OlsSOISqzfWICO6UKbEiK1kQl2PACalSFub+BI5 Kg2Fdb7XLrnLjnmjqWN2BtWOGGMD4GZKFRTEVK5McJidKvEYTYZ/Qqpp2U5TRXpUVhuE8D 5srfhmTjBt87jCYED5Q3E3+uBKoGP3UH6iYdgbn8zGs6o2dgpBqcTYkHAGX2gPavzJt0QF 0KjyZAWjYhRhZ6jCyJ/8Y/pxP+z1at/8IAHjdEUHnmF7R8R9fyZgGRTgdWnODXTCiov5yD XKzLE+a2sCFe3XEMLhZrRXouQLmTCWtt+GaL/MKrKZePwuZUzt4o8HuHWyeycg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061406; 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=5/yMD3R9spIxspDFxWIwUYuBkiPd/L8Pz+d/L53r+ac=; b=EPcm/mSMh42WIpzTvEpDqkN/t/wYov8XMzK5usKO1dt+oW8x3Y4OctekoHgUZm0G4Q+dDW psi2LeGVSgAD0zCw== Date: Wed, 11 Sep 2024 15:29:54 +0200 Subject: [PATCH 10/24] timekeeping: Define a struct type for tk_core to make it reusable Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-10-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 The struct tk_core uses is not reusable. As long as there is only a single timekeeper, this is not a problem. But when the timekeeper infrastructure will be reused for per ptp clock timekeepers, an explicit struct type is required. Define struct tk_data as explicit struct type for tk_core. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index f8ffab5bb790..be939ce3bcfc 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -45,12 +45,14 @@ enum timekeeping_adv_mode { * The most important data for readout fits into a single 64 byte * cache line. */ -static struct { +struct tk_data { seqcount_raw_spinlock_t seq; struct timekeeper timekeeper; struct timekeeper shadow_timekeeper; raw_spinlock_t lock; -} tk_core ____cacheline_aligned; +} ____cacheline_aligned; + +static struct tk_data tk_core; /* flag for if timekeeping is suspended */ int __read_mostly timekeeping_suspended; From patchwork Wed Sep 11 13:29:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800575 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 3180819E980; Wed, 11 Sep 2024 13:30:07 +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=1726061411; cv=none; b=Rs2bpRJwRLVDZtv5vE28cTZaeNlBOtlTXnKwZDeMlLemGMSdU/mKVOAE2NZGHUPwF+ClSLX7nnPY5exDYa873WyQ3NWgHuzV1gurllbxQRkv6CEnV6LK07/pu4Z9o065P3qzYFvQZSUgXwF73oQFWUl/WVdBXUffmsFn0zqxtbU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061411; c=relaxed/simple; bh=nc5U6KbcMYzQdnW/Lx4l8JRu9gnxWaIjx1tynImnxqE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=aqyRz9j2xbsfPMAWQTGKapDTW0Kvrjo0B14sB0BJY4sVZRNShJgHGAxbtjWYOOlXSujc6eFBi8rWkGNmwV93MW35TcRRVmN/+7/39YrQWG9h98VxLK+awDuN+XdVfalf1+OG0HcW7lzetPv7k8t0yawwskoyqSfalmLITajG9R0= 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=I244hn6q; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=WYphmXPT; 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="I244hn6q"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="WYphmXPT" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061406; 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=f976KFqjiKQ3mvq3MagOWQEMS56W5cXvrIUl7RiGrpQ=; b=I244hn6qTqSogLZD79rlHojFIW7L3nG1vF2lhLfcZMzj6Lakt1azuRxnNePTk76H6hw2Np p8VAQ83QoEkCLH3M5SHcx3aRVHLsvGkkWgDiVdXlIR9I3Xyf3cC73nehA+JUQ302ilO32T Qh3cXtdf7R6M5juDWnGoDwCUubLLFEc0lGgyK8kbUUt73HvfLtmQypr8Be9JdZRtWn1jUk QlGq3DJVKwMnLoQ41NWUKQz+eKgTTfDW9uXgVFn7ZR5jJCF4T2qgQvPPyrXhX54Afz+K2C A4SEHuD5pq8YUAHsCeQYI2UuPDfb8W5NRa465/qsyLkyYUEuySv6vaKiFGfUDw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061406; 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=f976KFqjiKQ3mvq3MagOWQEMS56W5cXvrIUl7RiGrpQ=; b=WYphmXPTlKG2YQm4eikQbX9lmlubII47PbBFguTtDfapMvE2EEYGon+4pjyDkO2mKiJnaq SPuzS1mHg5/o3kCw== Date: Wed, 11 Sep 2024 15:29:55 +0200 Subject: [PATCH 11/24] timekeeping: Add struct tk_data as argument to timekeeping_update() Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-11-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 are done in two ways: 1. Updating timekeeper and afterwards memcpy()'ing the result into shadow_timekeeper using timekeeping_update(). Used everywhere for updates except in timekeeping_advance(); the sequence counter protected region starts before the first change to the timekeeper is done. 2. Updating shadow_timekeeper and then memcpy()'ing the result into timekeeper. Used only by in timekeeping_advance(); The seqence counter protected region is only around timekeeping_update() and the memcpy for copy from shadow to timekeeper. The second option is fast path optimized. The sequence counter protected region is as short as possible. As this behaviour is mainly documented by commit messages, but not in code, it makes the not easy timekeeping code more complicated to read. There is no reason why updates to the timekeeper can't use the optimized version everywhere. With this, the code will be cleaner, as code is reused instead of duplicated. To be able to access tk_data which contains all required information, add a pointer to tk_data as an argument to timekeeping_update(). With that convert the comment about holding the lock into a lockdep assert. No functional change. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index be939ce3bcfc..d92b1f41ba1b 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -528,7 +528,7 @@ EXPORT_SYMBOL_GPL(ktime_get_raw_fast_ns); * timekeeping_inject_sleeptime64() * __timekeeping_inject_sleeptime(tk, delta); * timestamp(); - * timekeeping_update(tk, TK_CLEAR_NTP...); + * timekeeping_update(tkd, tk, TK_CLEAR_NTP...); * * (2) On 32-bit systems, the 64-bit boot offset (tk->offs_boot) may be * partially updated. Since the tk->offs_boot update is a rare event, this @@ -753,9 +753,10 @@ static inline void tk_update_ktime_data(struct timekeeper *tk) tk->tkr_raw.base = ns_to_ktime(tk->raw_sec * NSEC_PER_SEC); } -/* must hold tk_core.lock */ -static void timekeeping_update(struct timekeeper *tk, unsigned int action) +static void timekeeping_update(struct tk_data *tkd, struct timekeeper *tk, unsigned int action) { + lockdep_assert_held(&tkd->lock); + if (action & TK_CLEAR_NTP) { tk->ntp_error = 0; ntp_clear(); @@ -1467,7 +1468,7 @@ int do_settimeofday64(const struct timespec64 *ts) tk_set_xtime(tk, ts); out: - timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); + timekeeping_update(&tk_core, tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); write_seqcount_end(&tk_core.seq); raw_spin_unlock_irqrestore(&tk_core.lock, flags); @@ -1517,7 +1518,7 @@ static int timekeeping_inject_offset(const struct timespec64 *ts) tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *ts)); error: /* even if we error out, we forwarded the time, so call update */ - timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); + timekeeping_update(&tk_core, tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); write_seqcount_end(&tk_core.seq); raw_spin_unlock_irqrestore(&tk_core.lock, flags); @@ -1602,7 +1603,7 @@ static int change_clocksource(void *data) timekeeping_forward_now(tk); old = tk->tkr_mono.clock; tk_setup_internals(tk, new); - timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); + timekeeping_update(&tk_core, tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); write_seqcount_end(&tk_core.seq); raw_spin_unlock_irqrestore(&tk_core.lock, flags); @@ -1801,7 +1802,7 @@ void __init timekeeping_init(void) tk_set_wall_to_mono(tk, wall_to_mono); - timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); + timekeeping_update(&tk_core, tk, TK_MIRROR | TK_CLOCK_WAS_SET); write_seqcount_end(&tk_core.seq); } @@ -1893,7 +1894,7 @@ void timekeeping_inject_sleeptime64(const struct timespec64 *delta) __timekeeping_inject_sleeptime(tk, delta); - timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); + timekeeping_update(&tk_core, tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); write_seqcount_end(&tk_core.seq); raw_spin_unlock_irqrestore(&tk_core.lock, flags); @@ -1956,7 +1957,7 @@ void timekeeping_resume(void) tk->ntp_error = 0; timekeeping_suspended = 0; - timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); + timekeeping_update(&tk_core, tk, TK_MIRROR | TK_CLOCK_WAS_SET); write_seqcount_end(&tk_core.seq); raw_spin_unlock_irqrestore(&tk_core.lock, flags); @@ -2025,7 +2026,7 @@ int timekeeping_suspend(void) } } - timekeeping_update(tk, TK_MIRROR); + timekeeping_update(&tk_core, tk, TK_MIRROR); halt_fast_timekeeper(tk); write_seqcount_end(&tk_core.seq); raw_spin_unlock_irqrestore(&tk_core.lock, flags); @@ -2343,7 +2344,7 @@ static bool timekeeping_advance(enum timekeeping_adv_mode mode) * memcpy under the tk_core.seq against one before we start * updating. */ - timekeeping_update(tk, clock_set); + timekeeping_update(&tk_core, tk, clock_set); memcpy(real_tk, tk, sizeof(*tk)); /* The memcpy must come last. Do not put anything here! */ write_seqcount_end(&tk_core.seq); @@ -2591,7 +2592,7 @@ int do_adjtimex(struct __kernel_timex *txc) if (tai != orig_tai) { __timekeeping_set_tai_offset(tk, tai); - timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); + timekeeping_update(&tk_core, tk, TK_MIRROR | TK_CLOCK_WAS_SET); clock_set = true; } else { tk_update_leap_state(tk); From patchwork Wed Sep 11 13:29:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800584 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 9EBFE1A2C2B; Wed, 11 Sep 2024 13:30:08 +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=1726061415; cv=none; b=f1Q8sYmTfJ9u0fx1jX92vwki3EMxFoWHRV6+MadetotqtOcRZzX/B4vQ7uPinqC/lX8mLvoP6/agrKJT5Csq6hLHJ57vxXy+XwymFlYtcpY0hKM7fTwTSy+DWAFeoM8lnTXE6UYEVERpEOxO2Tjnj4KO1+skj++Mau8ya4o+J3E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061415; c=relaxed/simple; bh=Q1MxAK7evr1s/GvckwB/2NTo91/XE4dRZeng2bYX8rU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=LBsEhc+4BZ1Ell48kyh+8eKzAixOipgKz8nIiksFIZG5e8+3u/7bcEI2I81UGd9yXW7EUD4gUk/CbEP5cyEqLT3sDz2Zdou5ZRMJKEwBfShLCybPGbyGgSR5OQV+g5jDu/UCx87LczYqnmZxLH2B6Qhpt2TzwDslcJlULpXW2gk= 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=tEGMMMOF; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=TLAbylad; 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="tEGMMMOF"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="TLAbylad" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061406; 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=De5uORD/ZvJcTb/IBEuf6TAy+rJsZKOPp0VJdFPG1JM=; b=tEGMMMOFlJEk4IYebonlF5av9dqhRTIq7qypCQf2Ta/rddcqgF8mTThYUa/GCJpEcrHKuw Y1anFbZFyZ0q8KItP2d+8h7ujyn3+raUExgPeL8lc+8r8NfgXRPppATCn5O2Odz+efCuM/ 4uKTEER1zvPPjrqnes3GP8IxLFzJi71bx6h+T1oG7SZOwvj2GADc1wiY+tnMeZVRn62RFm if6JOlRuXl/ReqshPAiOkalXK81KzjEXsYgbCPV/DErAuXezwUG2nrPOz7FZDOmRYsXmTV e+MqX1YgwN9+LXgewxUuDPi7rd12pfcym4syNhi94QzXdVL4RF3GBHf6KMF79Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061406; 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=De5uORD/ZvJcTb/IBEuf6TAy+rJsZKOPp0VJdFPG1JM=; b=TLAbyladSg86uhL0wSOaCZe9JvfwuCaj65LOs5yRrJj/UNQCDDzErzhcTGBNc0ElO8xDg6 3ae77f+TN2/CnRBg== Date: Wed, 11 Sep 2024 15:29:56 +0200 Subject: [PATCH 12/24] timekeeping: Split out timekeeper update of timekeeping_advanced() Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-12-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 timekeeping_advance() is the only optimized function which uses shadow_timekeeper for updating the real timekeeper to keep the sequence counter protected region as small as possible. To be able to transform timekeeper updates in other functions to use the same logic, split out functionality into a separate function timekeeper_update_staged(). While at it, document the reason why the sequence counter must be write held over the call to timekeeping_update() and the copying to the real timekeeper and why using a pointer based update is suboptimal. No functional change. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index d92b1f41ba1b..bf8814508cdf 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -780,7 +780,32 @@ static void timekeeping_update(struct tk_data *tkd, struct timekeeper *tk, unsig * timekeeper structure on the next update with stale data */ if (action & TK_MIRROR) - memcpy(&tk_core.shadow_timekeeper, &tk_core.timekeeper, sizeof(tk_core.timekeeper)); + memcpy(&tkd->shadow_timekeeper, tk, sizeof(*tk)); +} + +static void timekeeping_update_staged(struct tk_data *tkd, unsigned int action) +{ + /* + * Block out readers before invoking timekeeping_update() because + * that updates VDSO and other time related infrastructure. Not + * blocking the readers might let a reader see time going backwards + * when reading from the VDSO after the VDSO update and then + * reading in the kernel from the timekeeper before that got updated. + */ + write_seqcount_begin(&tkd->seq); + + timekeeping_update(tkd, &tkd->shadow_timekeeper, action); + + /* + * Update the real timekeeper. + * + * We could avoid this memcpy() by switching pointers, but that has + * the downside that the reader side does not longer benefit from + * the cacheline optimized data layout of the timekeeper and requires + * another indirection. + */ + memcpy(&tkd->timekeeper, &tkd->shadow_timekeeper, sizeof(tkd->shadow_timekeeper)); + write_seqcount_end(&tkd->seq); } /** @@ -2333,21 +2358,7 @@ static bool timekeeping_advance(enum timekeeping_adv_mode mode) */ clock_set |= accumulate_nsecs_to_secs(tk); - write_seqcount_begin(&tk_core.seq); - /* - * Update the real timekeeper. - * - * We could avoid this memcpy by switching pointers, but that - * requires changes to all other timekeeper usage sites as - * well, i.e. move the timekeeper pointer getter into the - * spinlocked/seqcount protected sections. And we trade this - * memcpy under the tk_core.seq against one before we start - * updating. - */ - timekeeping_update(&tk_core, tk, clock_set); - memcpy(real_tk, tk, sizeof(*tk)); - /* The memcpy must come last. Do not put anything here! */ - write_seqcount_end(&tk_core.seq); + timekeeping_update_staged(&tk_core, clock_set); return !!clock_set; } From patchwork Wed Sep 11 13:29:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800573 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 0FB1B1A704B; Wed, 11 Sep 2024 13:30:08 +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=1726061411; cv=none; b=cJ8sUlXUq1QMvKqNHPC+tv8+2a05Oc2PxSPn2O/v34VFDqf/W5SR+gmxi8e1gLoYduRwnFaHcQs0+4t0fmGEVVuqabNE188MlcfniVX2O9xIMrbGphRtK3gsYy1P3cX01jtaJ/eCuScIyvxtNdZMjRctevMm1PSJcBKcpoDee04= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061411; c=relaxed/simple; bh=UfSgqr8w6OKStmTk6hQpgLtRu15vBBsI9MQBZa7hyhw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Ezfwy0Z2ouU8xGHXZFobFNwIdlRzqSXGBe8JKrGfnza9/qVplLAcR/408hoQ48Aar/fotZwEFJLBJ7SIh1B7enjnkLl8hdFaYPe3ZTRmYumQiizPRmdHa0PmqnRVatV52KWJsIHxUyGuPxezqkrHRq6lNPNbJojaV91qZjkqUrs= 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=C3LqpEes; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=vd8CpEOf; 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="C3LqpEes"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="vd8CpEOf" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061407; 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=I5ndr/9rOf/lZ1c5KbNITnRu8WY5tAixKqmuoEWpxWg=; b=C3LqpEes6t7sxAOxAa6DKyZTN6hRpdwMeXjBXPHfzi5B6QQyC/8LP7dcHaSNiyz5immxpS XO4PLeJKlq+6P9qRCxN4N/Y+6D74qeC9UrKAYHpxObHMY0RMhwvOuy+aluu7J9sU+Thvdw oek1WwBKkGClRdjaNunCsXFt16cQYK1K7BF3rWEj/4w4+ls2oqYhWgEZbpvHrSkh217o0j JCdv38ZeBVwk2INf0PvAGOuyYXfouGkSXN/+qrowAH86UhKdvyeVbDCZiYList0TfwSEd3 C0Kvb2PE67OwKifZsFOTCzuLESfEmtPzty9/inYiWnIDrEH74KR/ceUIjnjqOA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061407; 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=I5ndr/9rOf/lZ1c5KbNITnRu8WY5tAixKqmuoEWpxWg=; b=vd8CpEOf0Xp5BUUDfJFZ+iL94JwcboSGIfRigInU/HfDx7bQXNZTPchRE1JwiKXnGZD4cs 6o+2vKUD4crMe1CA== Date: Wed, 11 Sep 2024 15:29:57 +0200 Subject: [PATCH 13/24] timekeeping: Introduce combined timekeeping action flag Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-13-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 Instead of explicitly listing all the separate timekeeping actions flags, introduce a new one which covers all actions except TK_MIRROR action. No functional change. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index bf8814508cdf..a69850429fb4 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -33,6 +33,8 @@ #define TK_MIRROR (1 << 1) #define TK_CLOCK_WAS_SET (1 << 2) +#define TK_UPDATE_ALL (TK_CLEAR_NTP | TK_CLOCK_WAS_SET) + enum timekeeping_adv_mode { /* Update timekeeper when a tick has passed */ TK_ADV_TICK, @@ -1493,7 +1495,7 @@ int do_settimeofday64(const struct timespec64 *ts) tk_set_xtime(tk, ts); out: - timekeeping_update(&tk_core, tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); + timekeeping_update(&tk_core, tk, TK_UPDATE_ALL | TK_MIRROR); write_seqcount_end(&tk_core.seq); raw_spin_unlock_irqrestore(&tk_core.lock, flags); @@ -1543,7 +1545,7 @@ static int timekeeping_inject_offset(const struct timespec64 *ts) tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *ts)); error: /* even if we error out, we forwarded the time, so call update */ - timekeeping_update(&tk_core, tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); + timekeeping_update(&tk_core, tk, TK_UPDATE_ALL | TK_MIRROR); write_seqcount_end(&tk_core.seq); raw_spin_unlock_irqrestore(&tk_core.lock, flags); @@ -1628,7 +1630,7 @@ static int change_clocksource(void *data) timekeeping_forward_now(tk); old = tk->tkr_mono.clock; tk_setup_internals(tk, new); - timekeeping_update(&tk_core, tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); + timekeeping_update(&tk_core, tk, TK_UPDATE_ALL | TK_MIRROR); write_seqcount_end(&tk_core.seq); raw_spin_unlock_irqrestore(&tk_core.lock, flags); @@ -1919,7 +1921,7 @@ void timekeeping_inject_sleeptime64(const struct timespec64 *delta) __timekeeping_inject_sleeptime(tk, delta); - timekeeping_update(&tk_core, tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET); + timekeeping_update(&tk_core, tk, TK_UPDATE_ALL | TK_MIRROR); write_seqcount_end(&tk_core.seq); raw_spin_unlock_irqrestore(&tk_core.lock, flags); From patchwork Wed Sep 11 13:29:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800577 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 0F58C1A4B70; Wed, 11 Sep 2024 13:30:08 +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=Kc6AucarlHvFl2bOM548ya80jOmV8F2KrN7bNMjz0K5X7DZEAEcx6A06z59XailtpR4IoZ/ZYC1ITPrezJkeMCFU1oZjQ61KeumiVpEP3hlsMxTBsXw4F3CvbSO+O2fII+iDOxYRl5TzUaEhas73v2pJZZvucyf19ES8vzP5vsk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061412; c=relaxed/simple; bh=Hcnpfq43P0r406C0KqrzhFRGnzGoOGa7FAO1+nTIPdk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Ogm1iJ4A3J2yenms3/jgnZCbXl8Aerqxii9dQAx6r8YhI1XcnjbsfyXvL+wwxUZLAKCiixMETXjqu2v85yqJq40Gxfw3V/ETh7WoJloJI5K2rSrqRcLAKZv7sLCb9B1c9CeMFfV5KVQXo9FLsvlIN1SDDB2WA2615nh/+iqyGg4= 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=UWeolac5; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Znd8wQZa; 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="UWeolac5"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Znd8wQZa" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061407; 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=gq4WxA36B6P8L3S4KfzH2ULeNi1uLh9jaG6BaCuUoNo=; b=UWeolac5pPgCZibLv8hUQg9mpfA/U5bpagha86EMPx97FtY14mnqiFGYUDOeylScyMnvbw LTrQkRYOCsXhOUNEsP/3Wgfb3wb6g1HHfr755WCmH1zW2eWHux8YYBwu9tbbvUWAZRMXEm Vq1kOFNjuFDXU5OB8w5kjO1xdinoPJYGBfH85tC5590To91XaQ/bEie0tBlc6J2ztDV23I o2sT+AAHLLAtIO4ou2G3PYK4Ps856qarLl/+5O5GfYvD3cHdpbf0sMQZw8/I43+8KqLpvr K5LXsgCbO5AAzg4/++r6e/qwzAEHU+gI81gVwchZYsCcRuoxNg2i4v2ewnoTsA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061407; 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=gq4WxA36B6P8L3S4KfzH2ULeNi1uLh9jaG6BaCuUoNo=; b=Znd8wQZa6buFroSclP/dd2ojhqrdEdZr1ybZBmWj9vEiICHgZQG898RCnsXE066MdZbc1x U11SMP7LXttF1bAQ== Date: Wed, 11 Sep 2024 15:29:58 +0200 Subject: [PATCH 14/24] timekeeping: Provide timekeeping_restore_shadow() Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-14-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 From: Thomas Gleixner Functions which operate on the real timekeeper, e.g. do_settimeofday(), have error conditions. If they are hit a full timekeeping update is still required because the already committed operations modified the timekeeper. When switching these functions to operate on the shadow timekeeper then the full update can be avoided in the error case, but the modified shadow timekeeper has to be restored. Provide a helper function for that. Signed-off-by: Thomas Gleixner Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index a69850429fb4..345117ff665d 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -755,6 +755,15 @@ static inline void tk_update_ktime_data(struct timekeeper *tk) tk->tkr_raw.base = ns_to_ktime(tk->raw_sec * NSEC_PER_SEC); } +/* + * Restore the shadow timekeeper from the real timekeeper. + */ +static void timekeeping_restore_shadow(struct tk_data *tkd) +{ + lockdep_assert_held(&tkd->lock); + memcpy(&tkd->shadow_timekeeper, &tkd->timekeeper, sizeof(tkd->timekeeper)); +} + static void timekeeping_update(struct tk_data *tkd, struct timekeeper *tk, unsigned int action) { lockdep_assert_held(&tkd->lock); @@ -782,7 +791,7 @@ static void timekeeping_update(struct tk_data *tkd, struct timekeeper *tk, unsig * timekeeper structure on the next update with stale data */ if (action & TK_MIRROR) - memcpy(&tkd->shadow_timekeeper, tk, sizeof(*tk)); + timekeeping_restore_shadow(tkd); } static void timekeeping_update_staged(struct tk_data *tkd, unsigned int action) From patchwork Wed Sep 11 13:29:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800574 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 785CB1AB50B; 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=1726061411; cv=none; b=frHEn2nLs333FWHyNiGO8N6OEFjD1n36p0lF9Auby4EiH7NBC1MTqJa6KR20feTEFHA8tgXj94BdkkQ07oi9wrI5suNuozMyLaVVfGS3dmaujXGigkFHeHf2DSXmb2q5MfeompzTxL51gloYoKZo3x3Jm6OHHoN0Uz5oFH4nkp4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061411; c=relaxed/simple; bh=Zap0Yi50WGWo/Qq3DJFBEab4/jczvRrU1rzrv+RNAB0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=MFmuvSu4m0xofWYHm885FVkA/01bhIjzhHH7UGJ/AQIcwxYtbiQNwahT8/b/5YYsn7Q54WWvTU7UL1va27wsaRl2DPO8gP1T7WxlHUAkka+TYy9WP9l2HY0vuMRXb3il5OGmF1g7oiTfn9Jlq7/ad7JsVtxsS8QdWmMI25qXb3I= 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=J7bQGwF/; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=cj9j3dOf; 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="J7bQGwF/"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="cj9j3dOf" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061407; 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=iEUQhs/7XdMxctBGjdLZslFFIm8tsdgWFqBbte1umNA=; b=J7bQGwF/Ykf28YVcZIS/+8zktL8XcNHsqMvkuTWiP6VJKqspZ9unfDb7s0kNm5Bam8IhJF y8jYkGXtHMTz5rYbdC8ChX0323fNdXVQqD1+JqYYVOQYeX9lrzsWK2xv+cW6sJI32vUC7v AZR/TPbCmiZIn40wxG0ilGpYN4Y88hIZle5SGWa9BrFL3xj2CZ5rMpf65sEO9XRZSZ6ETa aGctmm2UYSaqCkKnssz9xiypkzC/sqPeSSMRi8UpMAZ3X1Ln2KzxqYV87YSUpgNFTvs2Dl oU4sXqkuYwl6j+Gdm+ctz71+T+WdwczuvZpk91TCtwx/3kpl29+EZzmHVfLlEA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061407; 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=iEUQhs/7XdMxctBGjdLZslFFIm8tsdgWFqBbte1umNA=; b=cj9j3dOfp6KG27Eo/id/h5JmNkTq8HL3W1p+pOwGAPymc1qaUwzP4DkH7waM1N0Bg4n16R Svk0BycF9KuRhYAQ== Date: Wed, 11 Sep 2024 15:29:59 +0200 Subject: [PATCH 15/24] timekeeping: Rework do_settimeofday64() to use shadow_timekeeper Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-15-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 do_settimeofday64() 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 | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 345117ff665d..efcfb0da351e 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1479,45 +1479,35 @@ EXPORT_SYMBOL_GPL(timekeeping_clocksource_has_base); */ int do_settimeofday64(const struct timespec64 *ts) { - struct timekeeper *tk = &tk_core.timekeeper; struct timespec64 ts_delta, xt; - unsigned long flags; - int ret = 0; if (!timespec64_valid_settod(ts)) return -EINVAL; - raw_spin_lock_irqsave(&tk_core.lock, flags); - write_seqcount_begin(&tk_core.seq); - - timekeeping_forward_now(tk); - - xt = tk_xtime(tk); - ts_delta = timespec64_sub(*ts, xt); + scoped_guard (raw_spinlock_irqsave, &tk_core.lock) { + struct timekeeper *tk = &tk_core.shadow_timekeeper; - if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) { - ret = -EINVAL; - goto out; - } + timekeeping_forward_now(tk); - tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta)); + xt = tk_xtime(tk); + ts_delta = timespec64_sub(*ts, xt); - tk_set_xtime(tk, ts); -out: - timekeeping_update(&tk_core, tk, TK_UPDATE_ALL | TK_MIRROR); + if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) { + timekeeping_restore_shadow(&tk_core); + return -EINVAL; + } - write_seqcount_end(&tk_core.seq); - raw_spin_unlock_irqrestore(&tk_core.lock, flags); + tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta)); + tk_set_xtime(tk, ts); + timekeeping_update_staged(&tk_core, TK_UPDATE_ALL); + } /* Signal hrtimers about time change */ clock_was_set(CLOCK_SET_WALL); - if (!ret) { - audit_tk_injoffset(ts_delta); - add_device_randomness(ts, sizeof(*ts)); - } - - return ret; + audit_tk_injoffset(ts_delta); + add_device_randomness(ts, sizeof(*ts)); + return 0; } EXPORT_SYMBOL(do_settimeofday64); From patchwork Wed Sep 11 13:30:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800578 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: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 = &tk_core.timekeeper; - unsigned long flags; - struct timespec64 tmp; - int ret = 0; - if (ts->tv_nsec < 0 || ts->tv_nsec >= NSEC_PER_SEC) return -EINVAL; - 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 = timespec64_add(tk_xtime(tk), *ts); - if (timespec64_compare(&tk->wall_to_monotonic, ts) > 0 || - !timespec64_valid_settod(&tmp)) { - ret = -EINVAL; - goto error; - } + scoped_guard (raw_spinlock_irqsave, &tk_core.lock) { + struct timekeeper *tk = &tk_core.shadow_timekeeper; + struct timespec64 tmp; - tk_xtime_add(tk, ts); - tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *ts)); + timekeeping_forward_now(tk); -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 = 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; + } - 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); + } /* Signal hrtimers about time change */ clock_was_set(CLOCK_SET_WALL); - - return ret; + return 0; } /* From patchwork Wed Sep 11 13:30:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800576 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 345CF1AB6DA; 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=KgL5yYTh9/0MS+x2KH720eGn6c9Xv9Ov9jujqaFlx+QKcnRWpQ86HkLqYZpp3QJ1T8a2VBWkTF9xCZFCEzCnzbBUYFmOLuW0H6FPvwI8kd1Wdiq9xMnDgc3XsjFNJT2kKLPpr5Lv1ev43OAkzaEP3OGjBx5b6B3VTviigR+HUek= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061412; c=relaxed/simple; bh=BvRw9iUIRqPjUujjNVsDLcQWcwHAFdeMgcXUK80VR0M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sJM2CE4jHURamsbuwkwxMIxDHNg2HiGYGPZEoLemPggJ9TCsPyijHv33ub4Wqaz6I4ZhgNWTVJ5xrYTXhiyiRyGby2c/rYplXcpjZGqeG96A5zBt6SwJYIKQ7RETRKT5As7T7wd9RuukYqawKm6cqHTLoiBmYu5/derb4GHXKKA= 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=d8LaImBH; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=BDFzFhUh; 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="d8LaImBH"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="BDFzFhUh" 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=+eQu+IcDEW2uo6h8G1deirSpZoVIHYB6Kt5NtOYSq4c=; b=d8LaImBHpXoY80T9g5KuWjTOo2sxiXdY/KtJzJloTC8Hk5l3H4MXwoYpIQqizic829LnLS miYlt9yEtREvqmjjuO/MvZ4B0qbHmC2CXsQsGn0ZZzFqZumGpWamiSrie1pzisFJWCClNX 4L5BfpjAnJN4QtscdwvGxd0BEKz2wEOIzvIs5CdyA3adTda2FaKsYMLskQPBNHlGGP1kk0 9tO/yHQM/VM4GuhwhsWfiRY68LBmjZuVaY4No7olYfD0tDE+dh9fzWr7nIMtGUYmRTkcyf dQ+Pr0fehyYaKyuCV5X72uqlD8sWh1cbFG1YwkMvHCQT7hyvXUbMgk9Sh1kg5A== 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=+eQu+IcDEW2uo6h8G1deirSpZoVIHYB6Kt5NtOYSq4c=; b=BDFzFhUh/pTPtZzxT3gFRZbV1zYJd+DV2vDv/Llg7KJ3AMIDO3CCUFqHfSP7a8ZDJNdaif 8dxhVoFR6843kdDw== Date: Wed, 11 Sep 2024 15:30:01 +0200 Subject: [PATCH 17/24] timekeeping: Rework change_clocksource() to use shadow_timekeeper Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-17-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 change_clocksource() to use this scheme. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 8b8d77463f3e..9b4fd620f895 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1596,9 +1596,7 @@ static void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset) */ static int change_clocksource(void *data) { - struct timekeeper *tk = &tk_core.timekeeper; struct clocksource *new = data, *old = NULL; - unsigned long flags; /* * If the clocksource is in a module, get a module reference. @@ -1614,16 +1612,14 @@ static int change_clocksource(void *data) return 0; } - raw_spin_lock_irqsave(&tk_core.lock, flags); - write_seqcount_begin(&tk_core.seq); - - timekeeping_forward_now(tk); - old = tk->tkr_mono.clock; - tk_setup_internals(tk, new); - timekeeping_update(&tk_core, tk, TK_UPDATE_ALL | TK_MIRROR); + scoped_guard (raw_spinlock_irqsave, &tk_core.lock) { + struct timekeeper *tk = &tk_core.shadow_timekeeper; - write_seqcount_end(&tk_core.seq); - raw_spin_unlock_irqrestore(&tk_core.lock, flags); + timekeeping_forward_now(tk); + old = tk->tkr_mono.clock; + tk_setup_internals(tk, new); + timekeeping_update_staged(&tk_core, TK_UPDATE_ALL); + } if (old) { if (old->disable) From patchwork Wed Sep 11 13:30:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800579 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 9E4B51ABEBA; Wed, 11 Sep 2024 13:30:10 +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=bpvco6mOVWpql5147xAdD3qvClesT3YS0c+3ZrPtDvLRxbnPjleoBxCBiybz8dY8lQzee1J4W70Qhl3FAX5SYjqSIjORCLJSKe9y/KoTXf7cqklkHxPfiQsjcBIn2d694p4kz1fDSsBvncU24umUkXiohZJxMfEEELJY1h+Wc2k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061412; c=relaxed/simple; bh=11k2zg2iWHkeGOqyjca+7sno045Z743cSq0hqnRE8VY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=btwz8XfcEXVG8O98nkAyP815bj9FY5F9wT0JhrD6VpRKuZd0+s4gBUiC+E6dKF17vKAbZ8P/6Q1GcUunwY4fwx01N7geOXxoPKF2hVGQ6H55D0+XwYpc2VPDTX7N/wmtfYqm5S6jJkq4Oh+AHZwvPmEfX0fro4xr0urI9ZmSe8o= 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=NE9e93mi; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=dzIAPHKj; 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="NE9e93mi"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="dzIAPHKj" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061409; 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=nAOR7ivePhht6tHAppOLIeJDMDAplgh861T+7mUgea0=; b=NE9e93mia4vb1EFuX+WN65jId2v4/VfCHFmtPUm4oXklvPhuUSB2h0OrOyAYw7Dfre5wQ0 OCJVzRMyY9+UpnsgvKTjT9aE+SwQL8v02eNuuesggS9+kYNciPI8O6eo6cIC+FC/n399Ov 6CWxhsG8L6Ie48VczJf0ay4fpg0T5wVO42/AwbF1UXu7S5XC9C2jWYgPi/HIsx8bt7TDvM 5tTDL3WS9XKTJ9CRvJsoNMKgEkVNPClbIY5cKpMji3fdWe9WArtwi7D/Xj20nqCPqu0Wm8 nBkI9KenRY0djYPQ0fjmXSgWKuxnZPyGzZLN68i44K0E365OInZBFmxeWaNwMA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061409; 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=nAOR7ivePhht6tHAppOLIeJDMDAplgh861T+7mUgea0=; b=dzIAPHKjMms06kzgzsbBsUZxfDHX5vEDjNOQHOMZjT6iX9PC7BzM6pduWnkDvxIR1+DQ1E u4cEqHjkvQHzNDCA== Date: Wed, 11 Sep 2024 15:30:02 +0200 Subject: [PATCH 18/24] timekeeping: Rework timekeeping_init() to use shadow_timekeeper Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-18-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 For timekeeping_init() the sequence count write held time is not relevant and it could keep working on the real timekeeper, but there is no reason to make it different from other timekeeper updates. Convert it to operate on the shadow timekeeper. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 9b4fd620f895..223429b1ca06 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1778,7 +1778,7 @@ static bool persistent_clock_exists; void __init timekeeping_init(void) { struct timespec64 wall_time, boot_offset, wall_to_mono; - struct timekeeper *tk = &tk_core.timekeeper; + struct timekeeper *tk = &tk_core.shadow_timekeeper; struct clocksource *clock; tkd_basic_setup(&tk_core); @@ -1802,7 +1802,7 @@ void __init timekeeping_init(void) wall_to_mono = timespec64_sub(boot_offset, wall_time); guard(raw_spinlock_irqsave)(&tk_core.lock); - write_seqcount_begin(&tk_core.seq); + ntp_init(); clock = clocksource_default_clock(); @@ -1815,9 +1815,7 @@ void __init timekeeping_init(void) tk_set_wall_to_mono(tk, wall_to_mono); - timekeeping_update(&tk_core, tk, TK_MIRROR | TK_CLOCK_WAS_SET); - - write_seqcount_end(&tk_core.seq); + timekeeping_update_staged(&tk_core, TK_CLOCK_WAS_SET); } /* time in seconds when suspend began for persistent clock */ From patchwork Wed Sep 11 13:30:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800585 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 AE0F31AC8A3; Wed, 11 Sep 2024 13:30:11 +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=1726061415; cv=none; b=r7Xb/acpwKuZYQUayGcSFTuT/ptoI1+AuT/vRiDXuVf2BeOaGciK0c8WHcpq3RFKIo8aIpV92YsO/WN+QkjRzGG450YoIl+dUeC+E5doAM/xZr6wCiwHaR4yJX0/DRRRu4YwMtYI9m6MnaMalBTh9I7sDyETiagxefv1RCtQwEM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061415; c=relaxed/simple; bh=5kskezftqXH5tjvjLiOu2UaXf3ww0dfSozmmELKWZMs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cQ3tfo9OzPk3HyTs1xD7MYVapzzQBuERme6ejh6NQkpQsCofjrZX3Mig41BNAghfgS980K8CZYothMR+T2HLkhsDIBdGSIFQLBEkyNMGUQIRupCGumH9tLyXwz7xZIEVXDDpBePIwIEVFJjXn9Yo5gQRv3MhOQohmjt5pYgJ8Qo= 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=k9Keo4Qo; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=8fgySdYY; 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="k9Keo4Qo"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="8fgySdYY" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061409; 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=tnkgY5EEf+tSEdiS0FM2oNqC9upPmA4xGg33OJxUNdM=; b=k9Keo4QonC0Yqm3cnM6LR0VGRcbSBSOBOvtIDt0tpMMwcd4sGkU5y+5FrRHDaBL2WqGLI3 ZRVXP6q2sTy7KtD0fTX0jv3lxytgfPFj+fhXyhPdq+h8ugaEYVvukEi6Anm8Re3BhhaUaa Dxyt8ijqRq1guF/MhuovMiKsALyNhx3PitAIj+HOcZQyPcfiwDAZS9cThvdUtOVSPxSKW5 xBXoT9+EmCtEtLdhyaKVBfqXqkNdd6juDOu+sMQ1iD2fBNF/PU2QjMMPC5wSX9wyeCNj6z Ob8ImQ9lk0XQgiDhmlmDfUOaWklFLetOBk4MJsZSJhhfWZ4/Rp73YgEXV46BCA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061409; 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=tnkgY5EEf+tSEdiS0FM2oNqC9upPmA4xGg33OJxUNdM=; b=8fgySdYYm6ifiAEEabzUWN89HWR37RTQwPj843g8TJvpes6WM1sgnuWXXMjzP2XCw17PaY HmIAMgEMGcvY1WAQ== Date: Wed, 11 Sep 2024 15:30:03 +0200 Subject: [PATCH 19/24] timekeeping: Rework timekeeping_inject_sleeptime64() to use shadow_timekeeper Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-19-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_sleeptime64() to use this scheme. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 223429b1ca06..244bb58dcda9 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1893,22 +1893,14 @@ bool timekeeping_rtc_skipsuspend(void) */ void timekeeping_inject_sleeptime64(const struct timespec64 *delta) { - struct timekeeper *tk = &tk_core.timekeeper; - unsigned long flags; - - raw_spin_lock_irqsave(&tk_core.lock, flags); - write_seqcount_begin(&tk_core.seq); - - suspend_timing_needed = false; - - timekeeping_forward_now(tk); - - __timekeeping_inject_sleeptime(tk, delta); - - timekeeping_update(&tk_core, tk, TK_UPDATE_ALL | TK_MIRROR); + scoped_guard(raw_spinlock_irqsave, &tk_core.lock) { + struct timekeeper *tk = &tk_core.shadow_timekeeper; - write_seqcount_end(&tk_core.seq); - raw_spin_unlock_irqrestore(&tk_core.lock, flags); + suspend_timing_needed = false; + timekeeping_forward_now(tk); + __timekeeping_inject_sleeptime(tk, delta); + timekeeping_update_staged(&tk_core, TK_UPDATE_ALL); + } /* Signal hrtimers about time change */ clock_was_set(CLOCK_SET_WALL | CLOCK_SET_BOOT); From patchwork Wed Sep 11 13:30:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800580 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 9185F190052; Wed, 11 Sep 2024 13:30:11 +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=1726061413; cv=none; b=iv6DuxjhN4KqUncfB8pt/i5C/xjI4X65rcb/cA0n9tl11txbEXFiigExVZMSF3doq4D2k0EJFCz+7ELQm/Ckxh5L1tN8CGG4aRiPcOsv0pBxjq9w8FfypbKDxnJmtovSPf05s9NYNkNYPREZVF3uVzC2fCga5g+w5jfONHH/gmo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061413; c=relaxed/simple; bh=cF6oOAMfCeoTCaLpR/GZtD9S8qUF3cOcM6UGbdVx5+Y=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=U2PK0FYtgRSgoEdnUd46hB4VWYaB2UgRMKpBvyme+drRrY6kTJt0tNRdOhe91niVeTVR31JfFb45WehWnIieuWYy7sCPcOxY9D/0c7AL3ncvoDKRRR9BPXDyrEXj6dIjV5CM44XV4NW3XOIwo7HsnRqPZujocEDnsZI8Fdey4KQ= 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=sfw1ByL7; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=RrpXnyUy; 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="sfw1ByL7"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="RrpXnyUy" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061409; 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=UDMkmxjITMPgOf2byw/Xt3H3B0E1TY+ptDWM2cYP80U=; b=sfw1ByL7bBp7d0WPlIuC0bO3X8BckkffXruGH3fOqv49Pctr0clIy4Vz669FXT+yd2S+QT U1cexsFbtmfRtYKNbNYo+kXOywPNP26TJauQR63Cl1CS/R2ZTHj0AWdAePF4BD9fDSHcaf 0qfkseWRQDReI48g5M7zVQtkRsJnn7+ko/Wx/0cwKBMgxRjT8MCoLB8kCEaQtWK78u0ZZG SiK/LMe9NuHYLJDr+dQYeDdUZCtHBlYmKBAuIZStho3fbPhw3k/xwlFYVcG94wfULKBnvC sUagte0O0+rsFVJVThSvNZ5nFeACQckp7vGIo5YQyUlEu0/SWqoX55AKmiUWzw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061409; 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=UDMkmxjITMPgOf2byw/Xt3H3B0E1TY+ptDWM2cYP80U=; b=RrpXnyUyhbcguowfOb2LjHWLvYxDsdqwm7bx5GdxQeYWcUQ1nf8ptDqhp0qdmXo7K/RsLV 3W1CjpokraucYGAw== Date: Wed, 11 Sep 2024 15:30:04 +0200 Subject: [PATCH 20/24] timekeeping: Rework timekeeping_resume() to use shadow_timekeeper Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-20-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. While the sequence count held time is not relevant for the resume path as there is no concurrency, there is no reason to have this function different than all the other update sites. Convert timekeeping_inject_offset() to use this scheme and cleanup the variable declaration while at it. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 244bb58dcda9..d4a0d37f88f5 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1912,12 +1912,12 @@ void timekeeping_inject_sleeptime64(const struct timespec64 *delta) */ void timekeeping_resume(void) { - struct timekeeper *tk = &tk_core.timekeeper; + struct timekeeper *tk = &tk_core.shadow_timekeeper; struct clocksource *clock = tk->tkr_mono.clock; - unsigned long flags; struct timespec64 ts_new, ts_delta; - u64 cycle_now, nsec; bool inject_sleeptime = false; + u64 cycle_now, nsec; + unsigned long flags; read_persistent_clock64(&ts_new); @@ -1925,7 +1925,6 @@ void timekeeping_resume(void) clocksource_resume(); raw_spin_lock_irqsave(&tk_core.lock, flags); - write_seqcount_begin(&tk_core.seq); /* * After system resumes, we need to calculate the suspended time and @@ -1960,8 +1959,7 @@ void timekeeping_resume(void) tk->ntp_error = 0; timekeeping_suspended = 0; - timekeeping_update(&tk_core, tk, TK_MIRROR | TK_CLOCK_WAS_SET); - write_seqcount_end(&tk_core.seq); + timekeeping_update_staged(&tk_core, TK_CLOCK_WAS_SET); raw_spin_unlock_irqrestore(&tk_core.lock, flags); touch_softlockup_watchdog(); From patchwork Wed Sep 11 13:30:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800581 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 DABC8191F9F; Wed, 11 Sep 2024 13:30:11 +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=1726061414; cv=none; b=czrv+5W/GDXGDXXIQ9mhCPqr/mlfV2zIn/MAtQj0WahsF2oMP5gnp9LA3XN+fYIG8bONcjsxsQwYpv62T4nKI5SKmiGLGZ9k1AT8TlidiULg/93XlXlI5oq5B6DOdpfx1X5H9hlL/OELohGrFfJZITLuEelaNhLcPbjqS0Tue6w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061414; c=relaxed/simple; bh=2Qm6nLpykvFlHhOJLqadWGJKowCKhpDGyESxT4y10a8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FUkLIxBcbFCQ+rtz8eCNCfGHcTILnl56vDqH9+DrBK3iWXSw//ouTcE7TiupJ1XqooFU9b2Yr5ZCRVsJqYNZNjsRq9GNLjX1UHdZ17T4dSPHB2Epp/QByp6mDe7AoNy79E2hArOfFG448uC3cu7nxnBsncI4uiKjI9f6+KZts04= 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=IYUP5zSf; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=hab2UX6Q; 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="IYUP5zSf"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="hab2UX6Q" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061410; 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=mGlb+KqZTo18QmNObNQ+c2RouGIN1eMARsowlyH2Frs=; b=IYUP5zSflb7vHETb6Z0jFkjtiHqXdYAEqjsqTJqZzbYBCRK53yx+CSiQGKPQIoDoicTfqH Q8VoiHwXWgcvic2m50eocJC/943ls7RN1XNgO8Uh3LCQJd/pWe2iAE5sqG8Z3F7lrkgiHZ xkKc+F3Tt3kdEftlu50I9TVf8tZdYt9wLjaF3wwHNn1QChK6hfKnLaiC/76nI0l3NhS4Uo VY5T6lJG2Z6khwoPFL4edVRSnCe6hXSJWkE9wUPGw8Omr4/wWvRqK+Od4Dvyj69pJpw75f i+IpMsgNYrrXhb+vkr3FdcrxqDs+3r78zsxY/2/OzfYqAgYoeRhBHnG9dggL5g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061410; 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=mGlb+KqZTo18QmNObNQ+c2RouGIN1eMARsowlyH2Frs=; b=hab2UX6QZYN6IHtvfQwFv7y2X2VjoUOjlrBwoz7RjCpCTH2sPuVUixzn6JkLA9xEdvpn44 yNZYimVN6SV0mXDQ== Date: Wed, 11 Sep 2024 15:30:05 +0200 Subject: [PATCH 21/24] timekeeping: Rework timekeeping_suspend() to use shadow_timekeeper Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-21-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. While the sequence count held time is not relevant for the resume path as there is no concurrency, there is no reason to have this function different than all the other update sites. Convert timekeeping_inject_offset() to use this scheme and cleanup the variable declarations while at it. As halt_fast_timekeeper() does not need protection sequence counter, it is no problem to move it with this change outside of the sequence counter protected area. But it still needs to be executed while holding the lock. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index d4a0d37f88f5..e3eb11ccdf5b 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1972,11 +1972,11 @@ void timekeeping_resume(void) int timekeeping_suspend(void) { - struct timekeeper *tk = &tk_core.timekeeper; - unsigned long flags; - struct timespec64 delta, delta_delta; - static struct timespec64 old_delta; + struct timekeeper *tk = &tk_core.shadow_timekeeper; + struct timespec64 delta, delta_delta; + static struct timespec64 old_delta; struct clocksource *curr_clock; + unsigned long flags; u64 cycle_now; read_persistent_clock64(&timekeeping_suspend_time); @@ -1992,7 +1992,6 @@ int timekeeping_suspend(void) suspend_timing_needed = true; raw_spin_lock_irqsave(&tk_core.lock, flags); - write_seqcount_begin(&tk_core.seq); timekeeping_forward_now(tk); timekeeping_suspended = 1; @@ -2027,9 +2026,8 @@ int timekeeping_suspend(void) } } - timekeeping_update(&tk_core, tk, TK_MIRROR); + timekeeping_update_staged(&tk_core, 0); halt_fast_timekeeper(tk); - write_seqcount_end(&tk_core.seq); raw_spin_unlock_irqrestore(&tk_core.lock, flags); tick_suspend(); From patchwork Wed Sep 11 13:30:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800582 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 005861ACDE8; Wed, 11 Sep 2024 13:30:11 +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=1726061414; cv=none; b=thYqwcwzszjpsryzXGjKseXoFGJCrkJYNDMxV+Xe67HALuR3rzsR5KmAwMjx3G9b04HxKo8Lo8Rt68sietrC7bzl1dcBdIlVw+dUNjnMqabnONIscY/f2UMtew2r3GSUxFJ9twnOuHPbZN8tc0SOkPT8/Cr+KwyYuty6xH4OV3c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061414; c=relaxed/simple; bh=ApDD4AjMoMOt1p1Qt0d2pl1wAFijoYUfYcuv+MwlkYE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rvsJO1WBTNUKgNmNm7JA5bqEo8xe/kLDgQ/w6636RL/sbOxMmIqo9dNbRFEdMcBmqKn8ITCSzOFudGvwE2POkPH21KW6wjj8DOJHsr6AHTwYNXWHJLnwRuhc3bmo/BLdFyDL5VdCfbWmWlWtkGO5Z1kuPgBU4yCadDKcKWSyBDw= 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=KPp133cg; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=RRR5RiMc; 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="KPp133cg"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="RRR5RiMc" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061410; 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=WXsIvVksy9ByUpQe81COBM2K1EqS1mTK0otA0frz9Zw=; b=KPp133cgyiMMQk64Brp3JIoh0OBJpPrlGz86kx6BNkCFWYLogfDjyxpPVezor3vtaXgSfD zRIxznRjQ/okDQBXRE8bFNE+QAsHQGik5SXN33YPjxsCvp/HjODKKfZpDkORJQGSTsEOWF 8Dp5OpXf1QdYVTh5m4d3puGgtZAbrFOY6EerI++TIGR53xwT5La1DRrvlwUFXU79EjfZE/ O3EQ6gKHfpzkt1AJhJdc4OpmDXSSKyy90//s/9G0y4nrL324UdkB0cTo/Nn8dajkKh0yMs jKhn75KdNpmDnyzULDmMIDmJV36uMBGRjgbp9vpmC2bg8CqrhfdJXf53ge3hbg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061410; 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=WXsIvVksy9ByUpQe81COBM2K1EqS1mTK0otA0frz9Zw=; b=RRR5RiMcFLoXrRe52rd+i3ovGWQ4Cp1vWIeUA1m7Qfu3G86TQkrch2seXIFhkIG/FfNpMy NvUHraO+/mgTyqBg== Date: Wed, 11 Sep 2024 15:30:06 +0200 Subject: [PATCH 22/24] timekeeping: Rework do_adjtimex() to use shadow_timekeeper Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-22-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 do_adjtimex() to use this scheme and take the opportunity to use a scoped_guard() for locking. That requires to have a separate function for updating the leap state so that the update is protected by the sequence count. This also brings the timekeeper and the shadow timekeeper in sync for this state, which was not the case so far. That's not a correctness problem as the state is only used at the read sides which use the real timekeeper, but it's inconsistent nevertheless. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index e3eb11ccdf5b..cbca0351ceca 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -722,6 +722,18 @@ static inline void tk_update_leap_state(struct timekeeper *tk) tk->next_leap_ktime = ktime_sub(tk->next_leap_ktime, tk->offs_real); } +/* + * Leap state update for both shadow and the real timekeeper + * Separate to spare a full memcpy() of the timekeeper. + */ +static void tk_update_leap_state_all(struct tk_data *tkd) +{ + write_seqcount_begin(&tkd->seq); + tk_update_leap_state(&tkd->shadow_timekeeper); + tkd->timekeeper.next_leap_ktime = tkd->shadow_timekeeper.next_leap_ktime; + write_seqcount_end(&tkd->seq); +} + /* * Update the ktime_t based scalar nsec members of the timekeeper */ @@ -2537,12 +2549,10 @@ EXPORT_SYMBOL_GPL(random_get_entropy_fallback); */ int do_adjtimex(struct __kernel_timex *txc) { - struct timekeeper *tk = &tk_core.timekeeper; + struct timekeeper *tk = &tk_core.shadow_timekeeper; struct audit_ntp_data ad; bool clock_set = false; struct timespec64 ts; - unsigned long flags; - s32 orig_tai, tai; int ret; /* Validate the data before disabling interrupts */ @@ -2569,23 +2579,21 @@ int do_adjtimex(struct __kernel_timex *txc) ktime_get_real_ts64(&ts); add_device_randomness(&ts, sizeof(ts)); - raw_spin_lock_irqsave(&tk_core.lock, flags); - write_seqcount_begin(&tk_core.seq); + scoped_guard (raw_spinlock_irqsave, &tk_core.lock) { + s32 orig_tai, tai; - orig_tai = tai = tk->tai_offset; - ret = __do_adjtimex(txc, &ts, &tai, &ad); + orig_tai = tai = tk->tai_offset; + ret = __do_adjtimex(txc, &ts, &tai, &ad); - if (tai != orig_tai) { - __timekeeping_set_tai_offset(tk, tai); - timekeeping_update(&tk_core, tk, TK_MIRROR | TK_CLOCK_WAS_SET); - clock_set = true; - } else { - tk_update_leap_state(tk); + if (tai != orig_tai) { + __timekeeping_set_tai_offset(tk, tai); + timekeeping_update_staged(&tk_core, TK_CLOCK_WAS_SET); + clock_set = true; + } else { + tk_update_leap_state_all(&tk_core); + } } - write_seqcount_end(&tk_core.seq); - raw_spin_unlock_irqrestore(&tk_core.lock, flags); - audit_ntp_log(&ad); /* Update the multiplier immediately if frequency was set directly */ From patchwork Wed Sep 11 13:30:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800583 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 74B8D1AD3E1; Wed, 11 Sep 2024 13:30:12 +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=1726061414; cv=none; b=qMLGS1TwyiGL9PkFlZ/tmPmrbSbg1Ox/oJpnphvKwEY5MJnIbO3Kon/DTsmrkw9T4t8GvxLc1T0tx5nC/tSeZKHJySQGuO5SLHRHvpb4EXbgCF81GB0gEFYU0tld7qPQHTUxYu+ftBRVWBpIb1+JeP6u0CA86O8sDR2oU/aOd+0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061414; c=relaxed/simple; bh=WJCbDopLz7Y/MGb+lX6DAG9uYE7sOy1FNUCeNdZnvNs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=X3LqUcfABzWQpahaGxVmPSZiY8n3bwxeVSuHsKOt+M3NxOVibu9SMFdJFbOYG7wNwGnaaYc2cUNN+Px4ZwWgS+tKcVcw6flsJxnvruqzPH2OC6icexnQUp8DF4lih3YHcZBk0Nrmcajwf2GIRiw8Db8SaGbHv0w141k9jqoaWPE= 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=h5VWUM0Z; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=LEKMwav+; 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="h5VWUM0Z"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="LEKMwav+" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061410; 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=I8CKPlfztLygrQgJmf1fIFEFAx796WpVuqoacpeXwfo=; b=h5VWUM0ZzJHCoQLt59tCScuC1/GosL6znGQ7dGlIaSMkgNuz7K30yJf8OiWzQtRJ5iS6nU AX3+d6h63qM6cKxlBOsKiCm2EHU/x18eo835ZycJIAz0yBJFwhhufACBiIdTJLTOShMyYK WVfhnp8acZC3hTsWfBxV9jq8sLOYgIr9Ppu9+Et3y/3rpiaBkwcL6PHrXFtLMTXuGyg3RG uqy/Y4gML6f8RLFxQka00DclOzQz0YoZEsUza+6FEPDz8vQ9fQuZxaFN+Kp+O75omc9ZzG kuyrRgBgmgI0xGVWQF7ZlIwm9uXyutf0XNjbmzOXGeMVl0KKHfPolD8jtTStoQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061410; 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=I8CKPlfztLygrQgJmf1fIFEFAx796WpVuqoacpeXwfo=; b=LEKMwav+iUSADNN3Y42GxMJnWdsR1/5JxaQmKdN/lA1AEJjXii0dr6TrSxv5iN7/ti2Zp4 Vuc57XUhS+4U5cDQ== Date: Wed, 11 Sep 2024 15:30:07 +0200 Subject: [PATCH 23/24] timekeeping: Remove TK_MIRROR timekeeping_update() action Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-23-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 All call sites of using TK_MIRROR flag in timekeeping_update() are gone. The TK_MIRROR dependent code path is therefore dead code. Remove it along with the TK_MIRROR define. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index cbca0351ceca..6aa77cec23f8 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -30,8 +30,7 @@ #include "timekeeping_internal.h" #define TK_CLEAR_NTP (1 << 0) -#define TK_MIRROR (1 << 1) -#define TK_CLOCK_WAS_SET (1 << 2) +#define TK_CLOCK_WAS_SET (1 << 1) #define TK_UPDATE_ALL (TK_CLEAR_NTP | TK_CLOCK_WAS_SET) @@ -797,13 +796,6 @@ static void timekeeping_update(struct tk_data *tkd, struct timekeeper *tk, unsig if (action & TK_CLOCK_WAS_SET) tk->clock_was_set_seq++; - /* - * The mirroring of the data to the shadow-timekeeper needs - * to happen last here to ensure we don't over-write the - * timekeeper structure on the next update with stale data - */ - if (action & TK_MIRROR) - timekeeping_restore_shadow(tkd); } static void timekeeping_update_staged(struct tk_data *tkd, unsigned int action) From patchwork Wed Sep 11 13:30:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anna-Maria Behnsen X-Patchwork-Id: 13800586 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 CAA751B29C8; Wed, 11 Sep 2024 13:30:13 +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=1726061415; cv=none; b=b+F2JeN4PHSHWHgtxyHt3D8hJHXFwwFibur4V+3ialP3wXe5ZrViWek+Ctdh5AoHF1y5ra6ypc3IvYCz/qXSCAD7BzEAmx6hEtMqOZAvKGMoBns1jhWm0gtaQ86Y2sQEbxfw0BYEbO9dczc6IzZiucARnVAUOXPhHf8bJH7FtfM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726061415; c=relaxed/simple; bh=2vSTUiy2M2+n/xBK3bnXTwUEf75LYiXCvnS+8X+/mUo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=JRYdgpzLOuqj89yhKz/1Z3JcCPjc4+UZoG9S9O0Q4jrz9qR41pL87R5qSSx6uA+2gJBji8VrxTneRU0NtpssZuNiEZiCqxiVtnVgc5tTfE45jvIx3q0JDKgMdfubpM+hdbUM+15w30AQUug7wFmN7cT+zejxM6lagM59ZoVb19k= 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=J6cBlxsy; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=bEX4E2RW; 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="J6cBlxsy"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="bEX4E2RW" From: Anna-Maria Behnsen DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1726061411; 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=ZLvuVEX65jfPzMF+gnRmcXqdmJBBBrgBEg+xdPrOC4o=; b=J6cBlxsyvhK1XfoP7bPr1KpQ7el46f35fm0Ln7Xqx9VVblqf+eX2dHx75+wgpXPxSrgG8u iRjZC8sRNVejV0gYyF3GFDBgPUdvf21A6/IwJt8H+EHE6holwEvynylOdfKYPdJz0OY2Xn CYqdmZmB2xAnOVXTJ64HoM5VR/jzPWzY7dbA+99PQXj4kUwff/aY3Y4nhXkL11T6yIXLsH 2t+dDneHDUd5HhL/UZP44BMfYg8bvZeL9Mk3Dzb1ffRT2ql2QJjFFA+4KAjqV7PD4n5fX8 AHuq4C3AzVF6v3mX56oGCy//grJkElR4x0i8Jxm2YVV+JqSPg/8dBslwgndxjQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1726061411; 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=ZLvuVEX65jfPzMF+gnRmcXqdmJBBBrgBEg+xdPrOC4o=; b=bEX4E2RWg+5y1kkwvvFRpRZwXZiJVbcXOpcFocfMwnJt4+z/qEuDrjD+fecOnYsV3dsuG+ hEd67BFaX6R5RgDA== Date: Wed, 11 Sep 2024 15:30:08 +0200 Subject: [PATCH 24/24] timekeeping: Merge timekeeping_update_staged() and timekeeping_update() Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240911-devel-anna-maria-b4-timers-ptp-timekeeping-v1-24-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 timekeeping_update_staged() is the only call site of timekeeping_update(). Merge those functions. No functional change. Signed-off-by: Anna-Maria Behnsen --- kernel/time/timekeeping.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 6aa77cec23f8..8e607413efad 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -529,7 +529,7 @@ EXPORT_SYMBOL_GPL(ktime_get_raw_fast_ns); * timekeeping_inject_sleeptime64() * __timekeeping_inject_sleeptime(tk, delta); * timestamp(); - * timekeeping_update(tkd, tk, TK_CLEAR_NTP...); + * timekeeping_update_staged(tkd, TK_CLEAR_NTP...); * * (2) On 32-bit systems, the 64-bit boot offset (tk->offs_boot) may be * partially updated. Since the tk->offs_boot update is a rare event, this @@ -775,10 +775,21 @@ static void timekeeping_restore_shadow(struct tk_data *tkd) memcpy(&tkd->shadow_timekeeper, &tkd->timekeeper, sizeof(tkd->timekeeper)); } -static void timekeeping_update(struct tk_data *tkd, struct timekeeper *tk, unsigned int action) +static void timekeeping_update_staged(struct tk_data *tkd, unsigned int action) { + struct timekeeper *tk = &tk_core.shadow_timekeeper; + lockdep_assert_held(&tkd->lock); + /* + * Block out readers before running the updates below because that + * updates VDSO and other time related infrastructure. Not blocking + * the readers might let a reader see time going backwards when + * reading from the VDSO after the VDSO update and then reading in + * the kernel from the timekeeper before that got updated. + */ + write_seqcount_begin(&tkd->seq); + if (action & TK_CLEAR_NTP) { tk->ntp_error = 0; ntp_clear(); @@ -796,20 +807,6 @@ static void timekeeping_update(struct tk_data *tkd, struct timekeeper *tk, unsig if (action & TK_CLOCK_WAS_SET) tk->clock_was_set_seq++; -} - -static void timekeeping_update_staged(struct tk_data *tkd, unsigned int action) -{ - /* - * Block out readers before invoking timekeeping_update() because - * that updates VDSO and other time related infrastructure. Not - * blocking the readers might let a reader see time going backwards - * when reading from the VDSO after the VDSO update and then - * reading in the kernel from the timekeeper before that got updated. - */ - write_seqcount_begin(&tkd->seq); - - timekeeping_update(tkd, &tkd->shadow_timekeeper, action); /* * Update the real timekeeper. @@ -819,7 +816,7 @@ static void timekeeping_update_staged(struct tk_data *tkd, unsigned int action) * the cacheline optimized data layout of the timekeeper and requires * another indirection. */ - memcpy(&tkd->timekeeper, &tkd->shadow_timekeeper, sizeof(tkd->shadow_timekeeper)); + memcpy(&tkd->timekeeper, tk, sizeof(*tk)); write_seqcount_end(&tkd->seq); }