From nobody Thu Apr 9 13:37:15 2026 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 60E4933372E for ; Fri, 27 Feb 2026 06:43:27 +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=1772174608; cv=none; b=tMDZPpVx0sYY7pKKCiRXWPHzKJgzqcqZNdieQvI+Fc9t3IbnFzIiR+obsKbuWM/b6Xvk+fpxDaYNmtYSpVOvXbA0gxRgq3vTEijbvO5R2n6lEsPZmptZe1AOEy8ggqgDX1gGnW1sGdH+5D9RxfPsTEmTqgm9y9kLVis69xDhRpw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772174608; c=relaxed/simple; bh=AhzHc5vSAExO+lhe/vNy090Akis21MibHZbbnMNOFJc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oKzyvplJGklVpVyWEx0Md1ae8aOgX9bZbpOTXFsUaZM47tyEKAXuRdTR4Ci/a2IPYzz1MwdV3ptrwwi9urz3bZ6aIwUPRpN0aYH0ip9MeTD+CdtpfgIjFIc04tLVsArl+IOGUjl7Fo6JHZ23GlcddRsoMcbHWd8x6ksMS4KGVYg= 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=djOz3uw+; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=gmTi5yPn; 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="djOz3uw+"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="gmTi5yPn" From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1772174605; 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=cczvBqSJPGWBfMRVBlyOa4GrbDaWpXXnucPOrVseTLA=; b=djOz3uw+JgerefZis0QkJ6XN1G6QqfbxSX5ZohitLKMj1Dlr7Ho0OdOWImhUw83lzg9aXF Q28kR6UZIpqq83Mv1mp6v1k/hC15JTgOdFuSZ7XIUbVIoOE4kO3huacmky/SsrG3ky7zs4 3Ky0pvGB2F+46aCKiQ0nuycVxEJwF6+YVS2ZzkbqfCM84qTjDuyyWwzxO1C3JRk+T7gJcP b9CSN7xjARiezSffdMCBnQyXu4LCFRNJLIS6hsNDomreIftm1v7HqcR1GB1G2g5UC5YONf MKkupb8LDP+KXcemXboOqnA3tBd2pyoIgr0+uy07rXGtzY9cDGhXISpcN/1Ugg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1772174605; 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=cczvBqSJPGWBfMRVBlyOa4GrbDaWpXXnucPOrVseTLA=; b=gmTi5yPnlRsnOzskdDQNj18/scosuVmLeihmRjiMSN1EmDPHmV0dwkWSTslotw09PG2lm/ Vb+kUT3OejPk6/CA== Date: Fri, 27 Feb 2026 07:43:21 +0100 Subject: [PATCH 2/4] vdso/gettimeofday: Add a helper to read the sequence lock of a time namespace aware clock Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260227-vdso-cleanups-v1-2-c848b4bc4850@linutronix.de> References: <20260227-vdso-cleanups-v1-0-c848b4bc4850@linutronix.de> In-Reply-To: <20260227-vdso-cleanups-v1-0-c848b4bc4850@linutronix.de> To: Andy Lutomirski , Thomas Gleixner , Vincenzo Frascino Cc: linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Developer-Signature: v=1; a=ed25519-sha256; t=1772174604; l=4589; i=thomas.weissschuh@linutronix.de; s=20240209; h=from:subject:message-id; bh=AhzHc5vSAExO+lhe/vNy090Akis21MibHZbbnMNOFJc=; b=ZirS7cGQtuyA6LTPAEnfZ/fJURK6KxOM6X8iwG6UOU24jeiKyKPHarrlcSvovc+iy6jvsNXop 60lMxatQuFBAwfIqBHq7NRKkWUWqpp7T1Lwk7ujWxoGGBS1q32a8UcU X-Developer-Key: i=thomas.weissschuh@linutronix.de; a=ed25519; pk=pfvxvpFUDJV2h2nY0FidLUml22uGLSjByFbM6aqQQws= Currently there are three different open-coded variants of a time namespace aware variant of vdso_read_begin(). They make the code hard to read and introduce an inconsistency, as only the first copy uses unlikely(). Split the code into a shared helper function. Move that next to the definition of the regular vdso_read_begin(), so that any future changes can be kept in sync easily. Signed-off-by: Thomas Wei=C3=9Fschuh --- include/vdso/helpers.h | 23 ++++++++++++++++++++++ lib/vdso/gettimeofday.c | 51 +++++++++------------------------------------= ---- 2 files changed, 32 insertions(+), 42 deletions(-) diff --git a/include/vdso/helpers.h b/include/vdso/helpers.h index 1a5ee9d9052c..0064c30cc40c 100644 --- a/include/vdso/helpers.h +++ b/include/vdso/helpers.h @@ -18,6 +18,29 @@ static __always_inline u32 vdso_read_begin(const struct = vdso_clock *vc) return seq; } =20 +/* + * Variant of vdso_read_begin() to handle + * VDSO_CLOCKMODE_TIMENS. Time namespace enabled tasks have a + * special VVAR page installed which has vc->seq set to 1 and + * vc->clock_mode set to VDSO_CLOCKMODE_TIMENS. For non time + * namespace affected tasks this does not affect performance + * because if vc->seq is odd, i.e. a concurrent update is in + * progress the extra check for vc->clock_mode is just a few + * extra instructions while spin waiting for vc->seq to become + * even again. + */ +static __always_inline bool vdso_read_begin_timens(const struct vdso_clock= *vc, u32 *seq) +{ + while (unlikely((*seq =3D READ_ONCE(vc->seq)) & 1)) { + if (IS_ENABLED(CONFIG_TIME_NS) && vc->clock_mode =3D=3D VDSO_CLOCKMODE_T= IMENS) + return true; + cpu_relax(); + } + smp_rmb(); + + return false; +} + static __always_inline u32 vdso_read_retry(const struct vdso_clock *vc, u32 start) { diff --git a/lib/vdso/gettimeofday.c b/lib/vdso/gettimeofday.c index 4939ee86af65..e49369676928 100644 --- a/lib/vdso/gettimeofday.c +++ b/lib/vdso/gettimeofday.c @@ -158,24 +158,8 @@ bool do_hres(const struct vdso_time_data *vd, const st= ruct vdso_clock *vc, return false; =20 do { - /* - * Open coded function vdso_read_begin() to handle - * VDSO_CLOCKMODE_TIMENS. Time namespace enabled tasks have a - * special VVAR page installed which has vc->seq set to 1 and - * vc->clock_mode set to VDSO_CLOCKMODE_TIMENS. For non time - * namespace affected tasks this does not affect performance - * because if vc->seq is odd, i.e. a concurrent update is in - * progress the extra check for vc->clock_mode is just a few - * extra instructions while spin waiting for vc->seq to become - * even again. - */ - while (unlikely((seq =3D READ_ONCE(vc->seq)) & 1)) { - if (IS_ENABLED(CONFIG_TIME_NS) && - vc->clock_mode =3D=3D VDSO_CLOCKMODE_TIMENS) - return do_hres_timens(vd, vc, clk, ts); - cpu_relax(); - } - smp_rmb(); + if (vdso_read_begin_timens(vc, &seq)) + return do_hres_timens(vd, vc, clk, ts); =20 if (!vdso_get_timestamp(vd, vc, clk, &sec, &ns)) return false; @@ -223,17 +207,8 @@ bool do_coarse(const struct vdso_time_data *vd, const = struct vdso_clock *vc, u32 seq; =20 do { - /* - * Open coded function vdso_read_begin() to handle - * VDSO_CLOCK_TIMENS. See comment in do_hres(). - */ - while ((seq =3D READ_ONCE(vc->seq)) & 1) { - if (IS_ENABLED(CONFIG_TIME_NS) && - vc->clock_mode =3D=3D VDSO_CLOCKMODE_TIMENS) - return do_coarse_timens(vd, vc, clk, ts); - cpu_relax(); - } - smp_rmb(); + if (vdso_read_begin_timens(vc, &seq)) + return do_coarse_timens(vd, vc, clk, ts); =20 ts->tv_sec =3D vdso_ts->sec; ts->tv_nsec =3D vdso_ts->nsec; @@ -256,20 +231,12 @@ bool do_aux(const struct vdso_time_data *vd, clockid_= t clock, struct __kernel_ti vc =3D &vd->aux_clock_data[idx]; =20 do { - /* - * Open coded function vdso_read_begin() to handle - * VDSO_CLOCK_TIMENS. See comment in do_hres(). - */ - while ((seq =3D READ_ONCE(vc->seq)) & 1) { - if (IS_ENABLED(CONFIG_TIME_NS) && vc->clock_mode =3D=3D VDSO_CLOCKMODE_= TIMENS) { - vd =3D __arch_get_vdso_u_timens_data(vd); - vc =3D &vd->aux_clock_data[idx]; - /* Re-read from the real time data page */ - continue; - } - cpu_relax(); + if (vdso_read_begin_timens(vc, &seq)) { + vd =3D __arch_get_vdso_u_timens_data(vd); + vc =3D &vd->aux_clock_data[idx]; + /* Re-read from the real time data page */ + continue; } - smp_rmb(); =20 /* Auxclock disabled? */ if (vc->clock_mode =3D=3D VDSO_CLOCKMODE_NONE) --=20 2.53.0