From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 4FE2840B6CF for ; Thu, 4 Jun 2026 11:17:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780571836; cv=none; b=d/jhUmaQfrpxo40/HMSjJ1An9au6ozJvgW2VSkI6m7gbwvDtEV0Zsi0R/8mhKHaZVyo+CiV3HsHSX0okeKn0H2OEFf47cv3Ny/qlkd8gMYuks9xShRyB6mtVxcuihsXHrsTTvCxWKB3LqrpetaKdu4s3Bi8pmJuimfNGsirdexM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780571836; c=relaxed/simple; bh=Li3Wd9vZ2do7YIFFIrvPQ4L5MznI5BwPjL7AunXsO9M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZtFXCywAVZjeZ1aVvjJgs01awXB1p46v7f3IV3SF5Fr9ba5IzRfHvejUz8KY0Bdl5PWgKh56jZPjXtMiR+b4QXOQedUNSDQUF91BO44SlmKPUWLI77lhyqqB49Os+Yo3kGlT4fhdpqkpa+RSnpHqpnYLmzJaKIon8BVtTCPzCSw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ivsLnTTa; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ivsLnTTa" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-45efa12a788so405710f8f.2 for ; Thu, 04 Jun 2026 04:17:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780571833; x=1781176633; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bL3cr/N4yYTt9Xr62pbCqd/6k/zRQ+lTKB/ioJiYtd8=; b=ivsLnTTanW/nUJfToVzGLrP8aPC39hcFvo9+j6K+4wVvBmgyLxwQkR0ZQPK5SA3XV4 Tjagv/7GALQCeBLxZee7yQxdIn5PAgZ0iD4GiZ+vCIFwLcRxIbFEUX3dUnrHC2818WMg bONOgIwPUbhDO2jB5s9y+K6fOvN9W1YiOFPFpI/xmtZyL4J3fXl8KJNT+79+Kmt42ehP EPjlx5QajL2pXk0D431dpu9WRZdAEL3Gx2dk8va/yXNlVF3Vi1+NVYY/v1ZGvwrT2/C2 83fR5nKoM0+wm6Le0MbAKEsETFRBqvnNmcoEsFicptWqLDrXB5uX4jeK/rzuR0swcrVy LJdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780571833; x=1781176633; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bL3cr/N4yYTt9Xr62pbCqd/6k/zRQ+lTKB/ioJiYtd8=; b=oXF92JWnJ9zrQIQbBiAoh+GqD5xf9lboCDgq5HewEQTVaF+Jo7x4o0C0VgGWzoiy68 mj5MYKHILJvz7PJI4tqjBkwum7BP/afLt/aW+8SUiYsEUzQyr30NHJU1KK0Pl3tj434N 7Wlltoi3gXGfa8Hzr3vjrB4i8Lk2DVp9V5uNcAVQmrCgB0lKJKfH6FzDBAKEkIcITndR MZ0QnH4DjFj8uXbPWW8po1TqLkzXohsVrVUnyjeVoVMTCziuFHBFRWmbQXNbLtpY4wka 7QQJIQJzmPfeV2e4Rm+uL/r3t+XqXqFGxVRpw4pxPW+axIWdrPTLWjTycWWvyiq6i6K/ ey9A== X-Forwarded-Encrypted: i=1; AFNElJ/AFvko5GwBCZ76KYJCCU35yYONv50uYWG0tvV3BtSBuF6SbLy2kWDH71lCvxLQ3SXPxprPid1XZ4uJP6U=@vger.kernel.org X-Gm-Message-State: AOJu0YzzQMC3PzzZcBVJFQ7d6PCQizO7FYmDOHYn5o3YMvM17ITVMt31 yJAxOAo2+9FChbaMCMypeE9wumXsYz4DBGQ4pnbjFvkMl8kmYp8P14Rywrl5N4b3ACv+AsPzrk2 UWZMzDehUkxsDpHyxNA== X-Received: from wrvx5.prod.google.com ([2002:a5d:54c5:0:b0:45e:ee1e:37d3]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1993:b0:490:bb19:b110 with SMTP id 5b1f17b1804b1-490bb19b19amr73563895e9.27.1780571832368; Thu, 04 Jun 2026 04:17:12 -0700 (PDT) Date: Thu, 4 Jun 2026 11:17:11 +0000 In-Reply-To: <20260526-rate-exclusive-get-th1520-v1-1-34cf034e1764@mailbox.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260526-rate-exclusive-get-th1520-v1-0-34cf034e1764@mailbox.org> <20260526-rate-exclusive-get-th1520-v1-1-34cf034e1764@mailbox.org> Message-ID: Subject: Re: [PATCH 1/2] rust: clk: Add ExclusiveClk wrapper for clk_rate_exclusive_get From: Alice Ryhl To: mhi@mailbox.org Cc: Michael Turquette , Stephen Boyd , Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?B?QmrDtnJu?= Roy Baron" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Drew Fustini , Guo Ren , Fu Wei , Michal Wilczynski , "Uwe =?utf-8?Q?Kleine-K=C3=B6nig?=" , linux-clk@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-pwm@vger.kernel.org Content-Type: text/plain; charset="utf-8" On Tue, May 26, 2026 at 07:04:57PM +0200, Maurice Hieronymus via B4 Relay wrote: > From: Maurice Hieronymus > > Add Rust bindings for clk_rate_exclusive_get() and > clk_rate_exclusive_put(). > > Clk::rate_exclusive_get() consumes the Clk and returns an ExclusiveClk; > the matching put is issued from its Drop impl. ExclusiveClk derefs to > Clk so existing rate / prepare / enable APIs remain available on the > locked handle. > > Signed-off-by: Maurice Hieronymus Overall looks okay to me. With below comments fixed: Reviewed-by: Alice Ryhl > rust/kernel/clk.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 65 insertions(+) > > diff --git a/rust/kernel/clk.rs b/rust/kernel/clk.rs > index 7abbd0767d8c..8cda40cb01e4 100644 > --- a/rust/kernel/clk.rs > +++ b/rust/kernel/clk.rs > @@ -249,6 +249,23 @@ pub fn set_rate(&self, rate: Hertz) -> Result { > // [`clk_set_rate`]. > to_result(unsafe { bindings::clk_set_rate(self.as_raw(), rate.as_hz()) }) > } > + > + /// Acquire exclusive control over the clock's rate. > + /// > + /// Consumes the [`Clk`] and returns an [`ExclusiveClk`] that releases the exclusivity > + /// when dropped. While held, no other consumer may change the clock's rate. > + /// > + /// Equivalent to the kernel's [`clk_rate_exclusive_get`] API. Must not be called from > + /// atomic context. > + /// > + /// [`clk_rate_exclusive_get`]: > + /// https://docs.kernel.org/core-api/kernel-api.html#c.clk_rate_exclusive_get Avoid the newline here. Links are long, and it's okay that they are long. They must be on one line. > + pub fn rate_exclusive_get(self) -> Result { > + // SAFETY: By the type invariants, self.as_raw() is a valid argument for > + // [`clk_rate_exclusive_get`]. > + to_result(unsafe { bindings::clk_rate_exclusive_get(self.as_raw()) })?; > + Ok(ExclusiveClk(self)) > + } > } > > impl Drop for Clk { > @@ -329,6 +346,54 @@ fn deref(&self) -> &Clk { > &self.0 > } > } > + > + /// A [`Clk`] with exclusive control over its rate. > + /// > + /// While an [`ExclusiveClk`] exists, no other consumer of the same clock may change its rate. > + /// Obtained by calling [`Clk::rate_exclusive_get`]; the exclusivity is released automatically > + /// when the value is dropped, after which the inner [`Clk`] is dropped as usual. > + /// > + /// # Invariants > + /// > + /// An [`ExclusiveClk`] instance owns a [`Clk`] for which `clk_rate_exclusive_get` has been > + /// called and the matching `clk_rate_exclusive_put` has not yet been called. > + /// > + /// # Examples > + /// > + /// ``` > + /// use kernel::clk::{Clk, ExclusiveClk}; > + /// use kernel::device::Device; > + /// use kernel::error::Result; > + /// > + /// fn lock_rate(dev: &Device) -> Result { > + /// let clk = Clk::get(dev, None)?; > + /// clk.prepare_enable()?; > + /// clk.rate_exclusive_get() > + /// } > + /// ``` > + /// > + /// [`struct clk`]: https://docs.kernel.org/driver-api/clk.html Unused link. > + pub struct ExclusiveClk(Clk); > + > + // Make [`ExclusiveClk`] behave like [`Clk`]. > + impl Deref for ExclusiveClk { > + type Target = Clk; > + > + fn deref(&self) -> &Clk { > + &self.0 > + } > + } > + > + impl Drop for ExclusiveClk { > + fn drop(&mut self) { > + // SAFETY: By the type invariants, self.as_raw() is a valid argument for > + // [`clk_rate_exclusive_put`] and balances the [`clk_rate_exclusive_get`] call from > + // [`Clk::rate_exclusive_get`]. > + unsafe { > + bindings::clk_rate_exclusive_put(self.as_raw()); > + } Nit: usually we format this with the semicolon outside the unsafe block. unsafe { bindings::clk_rate_exclusive_put(self.as_raw()) }; Alice