From nobody Mon Feb 9 06:42:25 2026 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 672BF288CB9; Tue, 10 Jun 2025 09:33:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749548009; cv=none; b=pM/lndSEEtd3GEDHqolPl9x8micK1rhLWpJTb8tF5EStNCMPFn1miz9VBoUYzT/4qs+GsiyyeRbFc+/vib9FN6OcYw7nF1Yh5m82G4y3R+nI/Iu8LlzoqfZHWIYIh9OcaQUwsTrcBTSX4/bTeuigWaW3fJSTglb6PNHG4BXGFxc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749548009; c=relaxed/simple; bh=Bj9Sic5pV4QziwisslgX3CILsOrHnV/2KM9dO4tBN1k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=CsN3zC8ODZL/ofTr94W7t0wVVS8uqlooOZ5PEig7QH5bQsVBkGpbNVrteURCxtpIQ4zHAH9URdYYHaU1bk0Y/VBqdfddWU3SfkxBK2iGg+RLOAXA2urEl2XHp4WB2tzKL6LKAKigwFpi/qWcISVUJpsiatiTLx3+mj4irx+p3NY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ezzkQz69; arc=none smtp.client-ip=209.85.210.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ezzkQz69" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-747fc77ba9eso3817413b3a.0; Tue, 10 Jun 2025 02:33:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749548006; x=1750152806; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+adE9eGbJy3NX1RAQES9UdpfUgSvvwR4hpIPwunfIWY=; b=ezzkQz69jSLjLn9fMBbGL3v9IYrQZb6AFUF8FbiA+ATq+P2gdqC6E0KbzZAoK94eR2 gu+PSptv7I+Y8DuXBo22zHAWKV9u7QrxBwfmNL5GYJ/2DDsddYiLIPFym4rhtlnYw4+O y9yPoJePPcTPM4eZwol+1RmUpIqtZtRSJwf8hgMMjxk7QP+dWEfQNyR5u7pltq2rOBun pvMhji/CQr0AeTF6KQRcmo9+CjKuZ/4qoQlO88dyy5gJBElJ3P6NoBGvXJHGT88FbEef lieq1L5wgK6Nn0yVFvw7lJrcoLNlJdFskBZvUtxvcE4ugmcmwJAKV4GDl+3FCzhBwfo+ jOPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749548006; x=1750152806; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+adE9eGbJy3NX1RAQES9UdpfUgSvvwR4hpIPwunfIWY=; b=VFoK1YoU9Jiz0C0q9Z5IxPbj+d+1LzvsYriH7mKUbGynyxEsFj8QIIErgXogeQ6/Eh oamla1H3u/bIzaTt9mCJG++yCpLrxsDM7F4/1PcBiUPh4OiiHthESJQrJwoY/rN0TT+E 4XJY2uAK4H3hZX2ZUhMjaEnO82U2QTihNraeS4/7/BXkPYycIGd6ytR/n8iECve1nmv4 LhEfViPvA/jPnGyaXEJ9DGfpaDl2gG7pQ1JPOwKz8BCvLEBQDcTtFDT0iCF6YulEwPP9 l3jbqkUcWbkEt3MYPg6APv8zV2E4ahDTUW3y/3urAvTHYbPvm58ky+iHwT0I26kA03Yz omUQ== X-Forwarded-Encrypted: i=1; AJvYcCWk26PYcP6Tc5EkGN6xVgiEZxKBwwcZJIa0F9u6yG5WgmI+8lnBSmdxiszBuQ2U3rRrmhUs28KKGdUaYY0=@vger.kernel.org, AJvYcCXDs6RIn6hBvwX5wOoxyoi+SBPGRKEJYrlRDtcHdkj4nZYvypCXEwndkfbbyERxUDCraaE4B9BDSHil7032rCU=@vger.kernel.org X-Gm-Message-State: AOJu0YyQmYsha1gDv36Z2TyLIo5Y9PDwT36pNg3t36IMd4Hh9OUK1v5k 8qKDPusWAnp3ydrAPd6pj73Ph7zrbcat3FA10UuJiPTMHmfALxfjBDwj X-Gm-Gg: ASbGncuAXE6gr9yMo8gHHXgyAEf3TItYkqtbdgFve2wFAnrZClX/iHABnOJW8WMn9GB Tu2RMBGq1CrBY+cQXV0nm0e9KgrRNwHZmJtAHNR1jpME8PN/SJmNBpS98KOJew1nAbccE8qVNiH yK7efTheHp9YpzmxOmSzxi3S4F8V/+FVsIAru3QybxZoxF5zrocg3ZEZ28/df6RQVPl+SUBHSc2 JigeSDNw712p6E7BilpG+4A/kO9pC8FXQwNewFSidyIzFoIFBjshPR1+DLplHYhvyoE/0RB3p7W oqi7L3LcU/rTrPD3lg3u0NgJo7U2NQoijrGkDrAUNTamrYbYDVhqCRyoN6nS66i2tryC8jH+ytX BaTkWTwrlpb277Yqu96Ey/ktv47gJV2nS4ZhoaVm9JVFJ4g== X-Google-Smtp-Source: AGHT+IHG1xXP/ghbsduH5QnBJdiahgPcl7xssC8e17lcI1PdW+JMValcLHVRjZr8SrLUt+WHHm87yQ== X-Received: by 2002:a05:6a00:138c:b0:736:33fd:f57d with SMTP id d2e1a72fcca58-74827f12fbdmr20286435b3a.17.1749548006432; Tue, 10 Jun 2025 02:33:26 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7482b0847e9sm7037684b3a.87.2025.06.10.02.33.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 02:33:26 -0700 (PDT) From: FUJITA Tomonori To: a.hindborg@kernel.org, alex.gaynor@gmail.com, ojeda@kernel.org Cc: aliceryhl@google.com, anna-maria@linutronix.de, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, dakr@kernel.org, frederic@kernel.org, gary@garyguo.net, jstultz@google.com, linux-kernel@vger.kernel.org, lossin@kernel.org, lyude@redhat.com, rust-for-linux@vger.kernel.org, sboyd@kernel.org, tglx@linutronix.de, tmgross@umich.edu Subject: [PATCH v4 1/3] rust: time: Replace ClockId enum with ClockSource trait Date: Tue, 10 Jun 2025 18:32:54 +0900 Message-ID: <20250610093258.3435874-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250610093258.3435874-1-fujita.tomonori@gmail.com> References: <20250610093258.3435874-1-fujita.tomonori@gmail.com> 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 Replace the ClockId enum with a trait-based abstraction called ClockSource. This change enables expressing clock sources as types and leveraging the Rust type system to enforce clock correctness at compile time. This also sets the stage for future generic abstractions over Instant types such as Instant. Reviewed-by: Andreas Hindborg Reviewed-by: Boqun Feng Signed-off-by: FUJITA Tomonori --- rust/kernel/time.rs | 147 ++++++++++++++++++++---------------- rust/kernel/time/hrtimer.rs | 6 +- 2 files changed, 84 insertions(+), 69 deletions(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index a8089a98da9e..efe68462b899 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -49,6 +49,87 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies { unsafe { bindings::__msecs_to_jiffies(msecs) } } =20 +/// Trait for clock sources. +/// +/// Selection of the clock source depends on the use case. In some cases t= he usage of a +/// particular clock is mandatory, e.g. in network protocols, filesystems.= In other +/// cases the user of the clock has to decide which clock is best suited f= or the +/// purpose. In most scenarios clock [`Monotonic`] is the best choice as it +/// provides a accurate monotonic notion of time (leap second smearing ign= ored). +pub trait ClockSource { + /// The kernel clock ID associated with this clock source. + /// + /// This constant corresponds to the C side `clockid_t` value. + const ID: bindings::clockid_t; +} + +/// A monotonically increasing clock. +/// +/// A nonsettable system-wide clock that represents monotonic time since as +/// described by POSIX, "some unspecified point in the past". On Linux, th= at +/// point corresponds to the number of seconds that the system has been +/// running since it was booted. +/// +/// The CLOCK_MONOTONIC clock is not affected by discontinuous jumps in the +/// CLOCK_REAL (e.g., if the system administrator manually changes the +/// clock), but is affected by frequency adjustments. This clock does not +/// count time that the system is suspended. +pub struct Monotonic; + +impl ClockSource for Monotonic { + const ID: bindings::clockid_t =3D bindings::CLOCK_MONOTONIC as binding= s::clockid_t; +} + +/// A settable system-wide clock that measures real (i.e., wall-clock) tim= e. +/// +/// Setting this clock requires appropriate privileges. This clock is +/// affected by discontinuous jumps in the system time (e.g., if the system +/// administrator manually changes the clock), and by frequency adjustments +/// performed by NTP and similar applications via adjtime(3), adjtimex(2), +/// clock_adjtime(2), and ntp_adjtime(3). This clock normally counts the +/// number of seconds since 1970-01-01 00:00:00 Coordinated Universal Time +/// (UTC) except that it ignores leap seconds; near a leap second it may be +/// adjusted by leap second smearing to stay roughly in sync with UTC. Leap +/// second smearing applies frequency adjustments to the clock to speed up +/// or slow down the clock to account for the leap second without +/// discontinuities in the clock. If leap second smearing is not applied, +/// the clock will experience discontinuity around leap second adjustment. +pub struct RealTime; + +impl ClockSource for RealTime { + const ID: bindings::clockid_t =3D bindings::CLOCK_REALTIME as bindings= ::clockid_t; +} + +/// A monotonic that ticks while system is suspended. +/// +/// A nonsettable system-wide clock that is identical to CLOCK_MONOTONIC, +/// except that it also includes any time that the system is suspended. Th= is +/// allows applications to get a suspend-aware monotonic clock without +/// having to deal with the complications of CLOCK_REALTIME, which may have +/// discontinuities if the time is changed using settimeofday(2) or simila= r. +pub struct BootTime; + +impl ClockSource for BootTime { + const ID: bindings::clockid_t =3D bindings::CLOCK_BOOTTIME as bindings= ::clockid_t; +} + +/// International Atomic Time. +/// +/// A system-wide clock derived from wall-clock time but counting leap sec= onds. +/// +/// This clock is coupled to CLOCK_REALTIME and will be set when CLOCK_REA= LTIME is +/// set, or when the offset to CLOCK_REALTIME is changed via adjtimex(2). = This +/// usually happens during boot and **should** not happen during normal op= erations. +/// However, if NTP or another application adjusts CLOCK_REALTIME by leap = second +/// smearing, this clock will not be precise during leap second smearing. +/// +/// The acronym TAI refers to International Atomic Time. +pub struct Tai; + +impl ClockSource for Tai { + const ID: bindings::clockid_t =3D bindings::CLOCK_TAI as bindings::clo= ckid_t; +} + /// A specific point in time. /// /// # Invariants @@ -91,72 +172,6 @@ fn sub(self, other: Instant) -> Delta { } } =20 -/// An identifier for a clock. Used when specifying clock sources. -/// -/// -/// Selection of the clock depends on the use case. In some cases the usag= e of a -/// particular clock is mandatory, e.g. in network protocols, filesystems.= In other -/// cases the user of the clock has to decide which clock is best suited f= or the -/// purpose. In most scenarios clock [`ClockId::Monotonic`] is the best ch= oice as it -/// provides a accurate monotonic notion of time (leap second smearing ign= ored). -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -#[repr(u32)] -pub enum ClockId { - /// A settable system-wide clock that measures real (i.e., wall-clock)= time. - /// - /// Setting this clock requires appropriate privileges. This clock is - /// affected by discontinuous jumps in the system time (e.g., if the s= ystem - /// administrator manually changes the clock), and by frequency adjust= ments - /// performed by NTP and similar applications via adjtime(3), adjtimex= (2), - /// clock_adjtime(2), and ntp_adjtime(3). This clock normally counts t= he - /// number of seconds since 1970-01-01 00:00:00 Coordinated Universal = Time - /// (UTC) except that it ignores leap seconds; near a leap second it m= ay be - /// adjusted by leap second smearing to stay roughly in sync with UTC.= Leap - /// second smearing applies frequency adjustments to the clock to spee= d up - /// or slow down the clock to account for the leap second without - /// discontinuities in the clock. If leap second smearing is not appli= ed, - /// the clock will experience discontinuity around leap second adjustm= ent. - RealTime =3D bindings::CLOCK_REALTIME, - /// A monotonically increasing clock. - /// - /// A nonsettable system-wide clock that represents monotonic time sin= ce=E2=80=94as - /// described by POSIX=E2=80=94"some unspecified point in the past". O= n Linux, that - /// point corresponds to the number of seconds that the system has been - /// running since it was booted. - /// - /// The CLOCK_MONOTONIC clock is not affected by discontinuous jumps i= n the - /// CLOCK_REAL (e.g., if the system administrator manually changes the - /// clock), but is affected by frequency adjustments. This clock does = not - /// count time that the system is suspended. - Monotonic =3D bindings::CLOCK_MONOTONIC, - /// A monotonic that ticks while system is suspended. - /// - /// A nonsettable system-wide clock that is identical to CLOCK_MONOTON= IC, - /// except that it also includes any time that the system is suspended= . This - /// allows applications to get a suspend-aware monotonic clock without - /// having to deal with the complications of CLOCK_REALTIME, which may= have - /// discontinuities if the time is changed using settimeofday(2) or si= milar. - BootTime =3D bindings::CLOCK_BOOTTIME, - /// International Atomic Time. - /// - /// A system-wide clock derived from wall-clock time but counting leap= seconds. - /// - /// This clock is coupled to CLOCK_REALTIME and will be set when CLOCK= _REALTIME is - /// set, or when the offset to CLOCK_REALTIME is changed via adjtimex(= 2). This - /// usually happens during boot and **should** not happen during norma= l operations. - /// However, if NTP or another application adjusts CLOCK_REALTIME by l= eap second - /// smearing, this clock will not be precise during leap second smeari= ng. - /// - /// The acronym TAI refers to International Atomic Time. - TAI =3D bindings::CLOCK_TAI, -} - -impl ClockId { - fn into_c(self) -> bindings::clockid_t { - self as bindings::clockid_t - } -} - /// A span of time. /// /// This struct represents a span of time, with its value stored as nanose= conds. diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 9df3dcd2fa39..280128d7e982 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -67,7 +67,7 @@ //! A `restart` operation on a timer in the **stopped** state is equivalen= t to a //! `start` operation. =20 -use super::ClockId; +use super::ClockSource; use crate::{prelude::*, types::Opaque}; use core::marker::PhantomData; use pin_init::PinInit; @@ -112,7 +112,7 @@ unsafe impl Sync for HrTimer {} =20 impl HrTimer { /// Return an initializer for a new timer instance. - pub fn new(mode: HrTimerMode, clock: ClockId) -> impl PinInit + pub fn new(mode: HrTimerMode) -> impl PinInit where T: HrTimerCallback, { @@ -126,7 +126,7 @@ pub fn new(mode: HrTimerMode, clock: ClockId) -> impl P= inInit bindings::hrtimer_setup( place, Some(T::Pointer::run), - clock.into_c(), + U::ID, mode.into_c(), ); } --=20 2.43.0 From nobody Mon Feb 9 06:42:25 2026 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3DE0528B50E; Tue, 10 Jun 2025 09:33:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749548012; cv=none; b=GQL3RgqEkPEN0SouTDckmeeyrZtP5SAfLtF8ZIO6pgTG5YOAGzYvk3d6WekWUIsrMjGfpvtNFYJzcQvfLTcTNXA2njv6UBSAcsXIhXEgj/XCVDanusBq06BTPz0t5fDDrD47s3CWa1Q1+w7hl2+aDBBu2ei7MCHbd+Ixuy6/dqQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749548012; c=relaxed/simple; bh=MQCk59toxyngoPvhQ1aU6dcIzz1O1u3tDlMe5aRUD6E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Env9HhDW2t7NmN7bZCOJ7cdtc0AZONEdO/CclyoyVhvitBS5IVDsnZjNolSsUv6VWFTtYPPNu644L3HZQpuwpRQfHHRhnYiLOBMB+e7ZX4RDhB16yRp/ZwlxpigmH/Myj6+SV5vGWilzm7Qov3zQtk4qRWwuuk/tsVSh4Gz9fmY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=GtrvV6FQ; arc=none smtp.client-ip=209.85.210.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GtrvV6FQ" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-7424ccbef4eso4599594b3a.2; Tue, 10 Jun 2025 02:33:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749548010; x=1750152810; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=6R0AmsFSPzLdyf+EVvSE/Sup3od+E6kS5h9VH5CP9Xs=; b=GtrvV6FQqsquQ/cPijloZA9kyQIfBaTNhvLAyNt4k3+zNxB/zhKQwXp9Nd/oZVlVHx qsvI2N73+8zj8W7rpoy5erkt2ZRQxjM/Q/PmDqnyGNt9SEEsBjxSjTXmAXRSTAXCjR0K 4CEQOgTJKhz2K5TMzi5vvAdzIT0fcs78G+WMid6HYrE9t4yKL/HmUW7tUFTBRrKsB4X9 A4T/YfmiEj0JGmOjEHOb9CfNkAX+Zqbfp1roSYozIk662cs/b9pfdya8um7+tlLIOsiq coslR3crJF+/BUrKvzkC8w5Eef6zg747NXqFAffrFi2JdD//7MtA4wjXHj0mbcdybXdj vvgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749548010; x=1750152810; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6R0AmsFSPzLdyf+EVvSE/Sup3od+E6kS5h9VH5CP9Xs=; b=D6KgDx5qxj7YMxUDpEXugizo820MMdi1TQEJUJfaLGbkYxHTkhi0eOHJvNvgzkiHgs tpLcUGP4PSg91rOhDq+CNq4TkfQkNxqJWYbuFzq5hLplHtUgHnembh0ljYLWwx1Esfpe Ms3wq0tx3NndpJCAAziZ1hC5SHqkyA5QKwUkRW+oaP4nKq4/5tUOEZbidNLqDg3LnSXa Mbc5wFu07QFdW8Lsa68bWAWtAFVeEYf1eQ+yWtt/Wy/svHLlvm2LcXuR+VkosC2F/9tW W1a4dkbOKVLxCdSbin/kWioDnvu2Fo7Rz1gnnCw9ZO8MlLAaEmV7PPFOV6+s5gJsUUfU QwSA== X-Forwarded-Encrypted: i=1; AJvYcCUn/ZymD59NO1VDg8UiT+WP7MHMgfhF+hXMLDx3X6KV+fDzEEEa/xM/XCJhBrmZmjFNx6q6V0V+T9TFmmE=@vger.kernel.org, AJvYcCUtM5ZYYITWqbDgvhy8WO/iCznB9JFOa66fVodIP0lgLFcMMW9oDT9GFBW3SPG8SU+Sieg2uI5WHD5kGTFM8Q4=@vger.kernel.org X-Gm-Message-State: AOJu0YyoIh65KEGM5jcA17yRPNr0Owe4TyrHgj8xILU68Gt1Gq6BcTFc +RnUMUBK1s39329jmh+TiNBGS0CwqWqlyONJ+yZJo/i3H+BXQvqponSX X-Gm-Gg: ASbGncsYWcsCQBpYCvXvhgjE/yjCDFIJGAt046zk4b8R/d8H0U8JHKZZxwvqkPG0y2G tSaOGpGH3X9HFEqM/vT28VyGqh8zx0zCqcwzMpmz+cwHqCRZ7nV/QsWIo0edu+FJ4Q0BR0T06mj SzvGIckMTK/WI+1kRSD0Dbm2kkQ8I/q/rPITIN8r+DfIwkak2Kb1hcNHFvIJdC9UKWYeu1F6ZnC aZcCq0pfs3AOe4DU8Ng269ERsP4uus0k+XDXEtjXFbL6Tu06XpSH778w/EqMDjSYGYVr2kJtLMb 6ADVMSQxrZ3x2/EqVCS0hWWSyC1yf9KyUHKX5dRHgGXdbUQPMs/pYi/N648HYzrf5b0rHQJoJk5 a+CWTGhnLTgthQN098zkLJSXbKzPqujkN/I7gI8fCoFzvJQ== X-Google-Smtp-Source: AGHT+IHi5tXwv49dq0BMlRk96CKlxGxtXPynCyKPuIA/qU+xyW62iYHaJca+idkaB4rZI0FieUyflw== X-Received: by 2002:a05:6a21:495:b0:1f5:7f56:a649 with SMTP id adf61e73a8af0-21ee24fe763mr21991469637.13.1749548010406; Tue, 10 Jun 2025 02:33:30 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7482b0847e9sm7037684b3a.87.2025.06.10.02.33.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 02:33:30 -0700 (PDT) From: FUJITA Tomonori To: a.hindborg@kernel.org, alex.gaynor@gmail.com, ojeda@kernel.org Cc: aliceryhl@google.com, anna-maria@linutronix.de, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, dakr@kernel.org, frederic@kernel.org, gary@garyguo.net, jstultz@google.com, linux-kernel@vger.kernel.org, lossin@kernel.org, lyude@redhat.com, rust-for-linux@vger.kernel.org, sboyd@kernel.org, tglx@linutronix.de, tmgross@umich.edu Subject: [PATCH v4 2/3] rust: time: Make Instant generic over ClockSource Date: Tue, 10 Jun 2025 18:32:55 +0900 Message-ID: <20250610093258.3435874-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250610093258.3435874-1-fujita.tomonori@gmail.com> References: <20250610093258.3435874-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor the Instant type to be generic over a ClockSource type parameter, enabling static enforcement of clock correctness across APIs that deal with time. Previously, the clock source was implicitly fixed (typically CLOCK_MONOTONIC), and developers had to ensure compatibility manually. This design eliminates runtime mismatches between clock sources, and enables stronger type-level guarantees throughout the timer subsystem. Reviewed-by: Andreas Hindborg Reviewed-by: Boqun Feng Signed-off-by: FUJITA Tomonori --- rust/kernel/time.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index efe68462b899..bba62c7f37e4 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -24,6 +24,8 @@ //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). =20 +use core::marker::PhantomData; + pub mod hrtimer; =20 /// The number of nanoseconds per microsecond. @@ -136,12 +138,21 @@ impl ClockSource for Tai { /// /// The `inner` value is in the range from 0 to `KTIME_MAX`. #[repr(transparent)] -#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)] -pub struct Instant { +#[derive(PartialEq, PartialOrd, Eq, Ord)] +pub struct Instant { inner: bindings::ktime_t, + _c: PhantomData, } =20 -impl Instant { +impl Clone for Instant { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for Instant {} + +impl Instant { /// Get the current time using `CLOCK_MONOTONIC`. #[inline] pub fn now() -> Self { @@ -150,6 +161,7 @@ pub fn now() -> Self { Self { // SAFETY: It is always safe to call `ktime_get()` outside of = NMI context. inner: unsafe { bindings::ktime_get() }, + _c: PhantomData, } } =20 @@ -160,12 +172,12 @@ pub fn elapsed(&self) -> Delta { } } =20 -impl core::ops::Sub for Instant { +impl core::ops::Sub for Instant { type Output =3D Delta; =20 // By the type invariant, it never overflows. #[inline] - fn sub(self, other: Instant) -> Delta { + fn sub(self, other: Instant) -> Delta { Delta { nanos: self.inner - other.inner, } --=20 2.43.0 From nobody Mon Feb 9 06:42:25 2026 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 430A428BA9F; Tue, 10 Jun 2025 09:33:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749548016; cv=none; b=FUAvHj4hGmE+OFpo8BtA9xngUR+LnDJ4Gc0w0Y5lAe5kZjc+6S6BOk8reA+pvUnkubCY/kqhinOSH9gLntJDM5tH/5Wt2oBus9zQyS8PhKdKB1GX79rf1Qf9dajn2KJA8PL+pspEVwenVUEM71h7huDccKZJxTjSaCO5pUA6u6g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749548016; c=relaxed/simple; bh=W1d08jaSgIfruH3mOqOqSxuXLtN6w+hQDE9ys0a6DFY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nbcL4ddIViY1V1+jUsGBXKMJpQ3rsFBHK8FHGYWG++q1zrZk8s/5+/XnjljP62XPQVqLrm0e2U70gFya/jAo++ezdoxEeEav/huC+ctewcGcsTzqlNCV2/QWMNEtxOWhNF9wsIfVkLRHcO5tCCtJ5/60K9i/ZWpHUy7jHLdc1rU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UmvJhB85; arc=none smtp.client-ip=209.85.210.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UmvJhB85" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-7424ccbef4eso4599640b3a.2; Tue, 10 Jun 2025 02:33:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749548014; x=1750152814; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TRHF11saV1NN0W+sCw/Qk3BmNy4EmF/eTZscZe/XeMw=; b=UmvJhB85Xy8QhMiYvOdLt6bTY4u4ZZzizJ1/a8yOXvQBXVlCryhImrCao3crsNwxv4 ONwz9prURKR3wQEvEKAHOOrzH6n3naYIXWfo+6AAnE81sWLb+H46JOtMn3pWGdPZIKsP ITbB56rj6C35TV+PSd0vOBNYkmL+nNf6vCUNZH334ygNZIe1QCGessONagJmW7n3+Kwn GULe1BhY3P14N8qQJmKaLLsYQ/QZImEjS+ZEnOeaIEHoqBQztF0HZ9uWxEKCWBLT9dIV KnOWqj6y5dy81sgncZJOEPgU/QN5SphigYcyK6XT6F4yUzMOL0Kin7rV7Fe9yHK2HrFi 2kYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749548014; x=1750152814; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TRHF11saV1NN0W+sCw/Qk3BmNy4EmF/eTZscZe/XeMw=; b=m07MrckVK6QzsqETcQzEcr/4q8yEEP/TTEm1rLZsCcMGkdg1CQ9YKzxe5DpgscITkf hHYI77kYfpsLKQOYRLzEffd+KGwAZw2qcUjX1aryTdfypmz8kjbPyS3+fEE86A+LHhAz w4oSz8YSdubI1mDVIdylxcIhnJ9kygJz2Hn0yu1hGYuNVEN9mWxxLMXTGakR8ImKZZOd 8kJaAQDbjT0kwXQkIQDK0uRH2snI1BTE428/zaQdO8GYtQVoarpj0PTCjB0dyvHtoYRS h1dIyU0L22ZRqe8rzigDj4BtHeRnmV712QKTOghPeRmN/Hyx/1JLiUEg/Y7LXNa8GlxV tMsw== X-Forwarded-Encrypted: i=1; AJvYcCX62RJ1HpvkOoEKpVR7JKBrtdDR7JDeJxDIWUGwWH12vUHg3oMg0xY5A+fy9/eE8lu7YbbYlyU4pGM0CEv2EpA=@vger.kernel.org, AJvYcCXlv9XOfDCnX7vTD7JTWaqrMp0s+n7lyDg8h+fULEqk51VTTFcRr9uMWaAF+iy5CQMxUUA3McIUPuF3YA8=@vger.kernel.org X-Gm-Message-State: AOJu0YwwEXaW0jf946dxDePE3MeNzknmEEfKvFm7SpLaraOMwR5amp5J l0YggWmuoNvtPg/CPwpcBHmH8zb4wfGPGakIKkXUwiH6W5AXr/eIKHIu X-Gm-Gg: ASbGncvGD8jafLNuC0ChkLHsjVfMGCp7kjCj3nNTHjSvdXExC2rejs+KUr/pHEaxZSC r/x/A8fHomkIV2KZbWx4VVUZwDc+6NcI3Ia9VlTF4tUXcLaS36v/XyJtnkhVx7WCRP17qxHJtLv 0uAJGxnMuynx5lXPzKr+S7lnnICctMxQO4L+3mQB7m4P9NN2IIi4vuK4l7KK1fLFNBGkak5U6Jz HuegDCo4ocvyvRAD85Xzxj3I52MxKLbvT8nho5jeSZtFYhxVjoR9MzH9fP5RSOnq+PqY1SNvuJM O8ag5p8QZJwbLbXnTK71IFvx1LJDmAt6KT+JUTmr8Sc/tOAPTQIVh1g0ZTWklgbaDA+g/6NCAmn mwiNG/SIoc2loPjsJWsZgR7FgvqfgtFnETiQ= X-Google-Smtp-Source: AGHT+IE7HC00zZOwfrEaNrx2TbCqkBjgo1u5471RtIb0C6a0gVqVw5ZoJ8OhMNaYX05AdusU6nOxzg== X-Received: by 2002:a05:6a00:21d1:b0:730:75b1:7219 with SMTP id d2e1a72fcca58-74827ea2b39mr20183743b3a.12.1749548014364; Tue, 10 Jun 2025 02:33:34 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7482b0847e9sm7037684b3a.87.2025.06.10.02.33.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Jun 2025 02:33:34 -0700 (PDT) From: FUJITA Tomonori To: a.hindborg@kernel.org, alex.gaynor@gmail.com, ojeda@kernel.org Cc: aliceryhl@google.com, anna-maria@linutronix.de, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, dakr@kernel.org, frederic@kernel.org, gary@garyguo.net, jstultz@google.com, linux-kernel@vger.kernel.org, lossin@kernel.org, lyude@redhat.com, rust-for-linux@vger.kernel.org, sboyd@kernel.org, tglx@linutronix.de, tmgross@umich.edu Subject: [PATCH v4 3/3] rust: time: Add ktime_get() to ClockSource trait Date: Tue, 10 Jun 2025 18:32:56 +0900 Message-ID: <20250610093258.3435874-4-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250610093258.3435874-1-fujita.tomonori@gmail.com> References: <20250610093258.3435874-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce the ktime_get() associated function to the ClockSource trait, allowing each clock source to specify how it retrieves the current time. This enables Instant::now() to be implemented generically using the type-level ClockSource abstraction. This change enhances the type safety and extensibility of timekeeping by statically associating time retrieval mechanisms with their respective clock types. It also reduces the reliance on hardcoded clock logic within Instant. Signed-off-by: FUJITA Tomonori --- rust/helpers/helpers.c | 1 + rust/helpers/time.c | 18 ++++++++++++++++++ rust/kernel/time.rs | 32 ++++++++++++++++++++++++++++---- 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 rust/helpers/time.c diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index 0f1b5d115985..0613a849e05c 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -39,6 +39,7 @@ #include "spinlock.c" #include "sync.c" #include "task.c" +#include "time.c" #include "uaccess.c" #include "vmalloc.c" #include "wait.c" diff --git a/rust/helpers/time.c b/rust/helpers/time.c new file mode 100644 index 000000000000..c99ea77c5ceb --- /dev/null +++ b/rust/helpers/time.c @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +ktime_t rust_helper_ktime_get_real(void) +{ + return ktime_get_real(); +} + +ktime_t rust_helper_ktime_get_boottime(void) +{ + return ktime_get_boottime(); +} + +ktime_t rust_helper_ktime_get_clocktai(void) +{ + return ktime_get_clocktai(); +} diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index bba62c7f37e4..9fd487276457 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -63,6 +63,11 @@ pub trait ClockSource { /// /// This constant corresponds to the C side `clockid_t` value. const ID: bindings::clockid_t; + + /// Get the current time from the clock source. + /// + /// The function must return a value in the range from 0 to `KTIME_MAX= `. + fn ktime_get() -> bindings::ktime_t; } =20 /// A monotonically increasing clock. @@ -80,6 +85,11 @@ pub trait ClockSource { =20 impl ClockSource for Monotonic { const ID: bindings::clockid_t =3D bindings::CLOCK_MONOTONIC as binding= s::clockid_t; + + fn ktime_get() -> bindings::ktime_t { + // SAFETY: It is always safe to call `ktime_get()` outside of NMI = context. + unsafe { bindings::ktime_get() } + } } =20 /// A settable system-wide clock that measures real (i.e., wall-clock) tim= e. @@ -100,6 +110,11 @@ impl ClockSource for Monotonic { =20 impl ClockSource for RealTime { const ID: bindings::clockid_t =3D bindings::CLOCK_REALTIME as bindings= ::clockid_t; + + fn ktime_get() -> bindings::ktime_t { + // SAFETY: It is always safe to call `ktime_get_real()` outside of= NMI context. + unsafe { bindings::ktime_get_real() } + } } =20 /// A monotonic that ticks while system is suspended. @@ -113,6 +128,11 @@ impl ClockSource for RealTime { =20 impl ClockSource for BootTime { const ID: bindings::clockid_t =3D bindings::CLOCK_BOOTTIME as bindings= ::clockid_t; + + fn ktime_get() -> bindings::ktime_t { + // SAFETY: It is always safe to call `ktime_get_boottime()` outsid= e of NMI context. + unsafe { bindings::ktime_get_boottime() } + } } =20 /// International Atomic Time. @@ -130,6 +150,11 @@ impl ClockSource for BootTime { =20 impl ClockSource for Tai { const ID: bindings::clockid_t =3D bindings::CLOCK_TAI as bindings::clo= ckid_t; + + fn ktime_get() -> bindings::ktime_t { + // SAFETY: It is always safe to call `ktime_get_tai()` outside of = NMI context. + unsafe { bindings::ktime_get_clocktai() } + } } =20 /// A specific point in time. @@ -153,14 +178,13 @@ fn clone(&self) -> Self { impl Copy for Instant {} =20 impl Instant { - /// Get the current time using `CLOCK_MONOTONIC`. + /// Get the current time from the clock source. #[inline] pub fn now() -> Self { - // INVARIANT: The `ktime_get()` function returns a value in the ra= nge + // INVARIANT: The `ClockSource::ktime_get()` function returns a va= lue in the range // from 0 to `KTIME_MAX`. Self { - // SAFETY: It is always safe to call `ktime_get()` outside of = NMI context. - inner: unsafe { bindings::ktime_get() }, + inner: C::ktime_get(), _c: PhantomData, } } --=20 2.43.0