From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 AA4C71D47B4 for ; Wed, 18 Jun 2025 02:28:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750213704; cv=none; b=MzBS0+oT6FlPFgVyhSX0YVgzLmRHqNekK3I2J/uAfZG6AHmPqMF40GW6JKD185CyRUusIFD6HGD7Xv4ZiOsjUMb0KgoFuTiiLqwbku2PvUXRGi4DGlm7FzXnkoEhUCDFC8/Vpppe9V0GQQgBoS5srEEJsLGl9diFn3WDK1Q1K1g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750213704; c=relaxed/simple; bh=oKl2z85IL+nZOqvaoXori7duEOA4yiwEy+q0plhD4uo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SCHKdlFK/NOxcIUJdXPFihrJ0qTb9UKujgF7LZLOrzXQWWV7fxzz8jPNrbUWis1OGCYHdw8IsxZiWMOM0H59cQF8GoJ67Wa7TNf9O/Jll+Mg7Hm/0sbRMTU6uC9fDD2wNZVPxhNuw5EP/4sPqOQn53VEmlDM6yb1arnFjJ/GViU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=2TpPziff; arc=none smtp.client-ip=209.85.216.73 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--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2TpPziff" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-31220ecc586so6179161a91.2 for ; Tue, 17 Jun 2025 19:28:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1750213702; x=1750818502; 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=Ji5LW4YxsgtGLzHKx3kLMSLSenw4FylgJNlVG7ZKEt0=; b=2TpPziffMtiOew/A55p7sPKNV1vEOYUxzHOjxRCWLk2Qs5yCWkzBPOIpAkBfrit2yR mXKuhCx9NVgrGPwaaZUL7RkaSv7ivmLFBtLzdaa0jW267hX8cCndsOBbbhw9F+bAD5uf jyNKo69te2PbepWyn6m+2qAwDANq1ApR4/j2Fp/yL90PCm8hySJGEgOI/add9McRF1Z1 2dc35t4BYFvub1G88dDW2Yfgwix3m6Gf9iAeFfxPJAJ5wH/epPSji00FTdlCiJRX/whc XeDmdujUgrh9aTrC8rKD3x2l3yF9K7WJN/ccT5PCycuY5DZe2EyUgYLpAXU9Egy5zz5y mBUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750213702; x=1750818502; 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=Ji5LW4YxsgtGLzHKx3kLMSLSenw4FylgJNlVG7ZKEt0=; b=fZA4FDM3oQ4NyLy+MRknL33zgduQCT+ttAadniLok3z3+bwIRAG4YDcIrRjqpMes5+ eyLcKJw2V8X2lM6IxHmVclloiHSWgjsC0GyMy5ybptQRYnF+t+5GK86kWSyvlVYwTUku pDrotSZx+cfIS98xOM5pUdOoGAKd8OyXrtcfwGdc510JhQEbatgu4lsn4qXFwF9PYgUu u/x6fOMYTXwAEL3/nxtSki8mKu30oq4WnGzvSkHNug4/b7oSjNz/JAi6BIopXvyKImLc P7HKYujERETxKgFGfvw9d/jQR85mHPr6Ly7oRnXpXxfTM076pkiIJpZdy0OdCLA5BzQZ B+Gg== X-Forwarded-Encrypted: i=1; AJvYcCUqbktcqb1iMDCUwceh89moVyT9L3pNqFf2qPOLRKW1VjJvgshvGn6aDtC417rV37rxMZaKRQLfrxX8ElOeKw==@vger.kernel.org X-Gm-Message-State: AOJu0YxkB0B8z0cBeqNJZLTUYAD0HId7yGK3m5eUsMVWmFXzDZQkUqnC 4HTcSWj2ohQ8n4TUrV01W1WmDx3x8hR92uzGlUmnKYtAgSAjKth4jFwi1uyJc+aOlBvf13F7322 DcK7GqmNdxQ== X-Google-Smtp-Source: AGHT+IHAQpDx0027kALvg1jSe/gar3r1FYw4JgvW4VzKKZpfVlGqi7+hrvadAqyyRO0ld8W2IiZRCSTaLsiI X-Received: from pjbtc8.prod.google.com ([2002:a17:90b:5408:b0:2fb:fa85:1678]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5847:b0:312:1b53:5ea8 with SMTP id 98e67ed59e1d1-313f1ce2aa1mr23177238a91.24.1750213702122; Tue, 17 Jun 2025 19:28:22 -0700 (PDT) Date: Wed, 18 Jun 2025 02:28:16 +0000 In-Reply-To: <20250618-debugfs-rust-v6-0-72cae211b133@google.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250618-debugfs-rust-v6-0-72cae211b133@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1750213694; l=4975; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=oKl2z85IL+nZOqvaoXori7duEOA4yiwEy+q0plhD4uo=; b=JfyQqUjWgCEhfJVm3XmVU9Mcp3CRaqdG8HbDyyicg5nbnzGjcEPq+0U/NFwsTKdij84wHXHye CL4qseLURoXBz69rJh20wKqp7a/H3zhKEQ6peB4FLh/bIGzwBVfAS90 X-Mailer: b4 0.14.2 Message-ID: <20250618-debugfs-rust-v6-4-72cae211b133@google.com> Subject: [PATCH v6 4/5] rust: debugfs: Support format hooks From: Matthew Maurer To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Sami Tolvanen , Timur Tabi , Benno Lossin Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Matthew Maurer Content-Type: text/plain; charset="utf-8" Rather than always using Display, allow hooking arbitrary functions to arbitrary files. Display technically has the expressiveness to do this, but requires a new type be declared for every different way to render things, which can be very clumsy. Signed-off-by: Matthew Maurer --- rust/kernel/debugfs.rs | 41 ++++++++++++++++++++++++++ rust/kernel/debugfs/display_file.rs | 57 ++++++++++++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs index cd83f21cf2818f406575941ebbc6c426575643e4..78544653b46d959c1520b1210170d22a0e973989 100644 --- a/rust/kernel/debugfs.rs +++ b/rust/kernel/debugfs.rs @@ -10,6 +10,7 @@ use crate::str::CStr; #[cfg(CONFIG_DEBUG_FS)] use crate::sync::Arc; +use core::fmt; use core::fmt::Display; use core::ops::Deref; @@ -153,6 +154,46 @@ pub fn display_file + 'static + Send + Sync, T: Display>( self.create_file(name, data) } + /// Create a file in a DebugFS directory with the provided name, and contents from invoking `f` + /// on the provided reference. + /// + /// `f` must be a function item or a non-capturing closure, or this will fail to compile. + /// + /// # Examples + /// + /// ``` + /// # use core::sync::atomic::{AtomicU32, Ordering}; + /// # use kernel::c_str; + /// # use kernel::debugfs::Dir; + /// let dir = Dir::new(c_str!("foo")); + /// static MY_ATOMIC: AtomicU32 = AtomicU32::new(3); + /// let file = dir.fmt_file(c_str!("bar"), &MY_ATOMIC, &|val, f| { + /// let out = val.load(Ordering::Relaxed); + /// writeln!(f, "{out:#010x}") + /// }); + /// MY_ATOMIC.store(10, Ordering::Relaxed); + /// ``` + pub fn fmt_file< + D: Deref + 'static + Send + Sync, + T: Sync, + F: Fn(&T, &mut fmt::Formatter<'_>) -> fmt::Result + Send + Sync, + >( + &self, + name: &CStr, + data: D, + f: &'static F, + ) -> File { + #[cfg(CONFIG_DEBUG_FS)] + let data_adapted = display_file::FormatAdapter::new(data, f); + #[cfg(not(CONFIG_DEBUG_FS))] + let data_adapted = { + // Mark used + let (_, _) = (data, f); + &0 + }; + self.display_file(name, data_adapted) + } + /// Create a new directory in DebugFS at the root. /// /// # Examples diff --git a/rust/kernel/debugfs/display_file.rs b/rust/kernel/debugfs/display_file.rs index 65e37e34d7b587482492dc296637a3bc517b9fe3..75f2e1c560840324117bc1bc73aa89e3823aaf0e 100644 --- a/rust/kernel/debugfs/display_file.rs +++ b/rust/kernel/debugfs/display_file.rs @@ -3,7 +3,9 @@ use crate::seq_file::SeqFile; use crate::seq_print; -use core::fmt::Display; +use core::fmt::{Display, Formatter, Result}; +use core::marker::PhantomData; +use core::ops::Deref; /// Implements `open` for `file_operations` via `single_open` to fill out a `seq_file`. /// @@ -58,3 +60,56 @@ pub(crate) trait DisplayFile: Display + Sized { } impl DisplayFile for T {} + +/// Adapter to implement `Display` via a callback with the same representation as `T`. +/// +/// # Invariants +/// +/// If an instance for `FormatAdapter<_, F>` is constructed, `F` is inhabited. +#[repr(transparent)] +pub(crate) struct FormatAdapter { + inner: D, + _formatter: PhantomData, +} + +impl FormatAdapter { + pub(crate) fn new(inner: D, _f: &'static F) -> Self { + // INVARIANT: We were passed a reference to F, so it is inhabited. + FormatAdapter { + inner, + _formatter: PhantomData, + } + } +} + +impl, T, F> Display for FormatAdapter +where + F: Fn(&T, &mut Formatter<'_>) -> Result + 'static, +{ + fn fmt(&self, fmt: &mut Formatter<'_>) -> Result { + // SAFETY: FormatAdapter<_, F> can only be constructed if F is inhabited + let f: &F = unsafe { materialize_zst_fmt() }; + f(&self.inner, fmt) + } +} + +impl Deref for FormatAdapter { + type Target = Self; + fn deref(&self) -> &Self { + self + } +} + +/// For types with a unique value, produce a static reference to it. +/// +/// # Safety +/// +/// The caller asserts that F is inhabited +unsafe fn materialize_zst_fmt() -> &'static F { + const { assert!(core::mem::size_of::() == 0) }; + let zst_dangle: core::ptr::NonNull = core::ptr::NonNull::dangling(); + // SAFETY: While the pointer is dangling, it is a dangling pointer to a ZST, based on the + // assertion above. The type is also inhabited, by the caller's assertion. This means + // we can materialize it. + unsafe { zst_dangle.as_ref() } +} -- 2.50.0.rc2.696.g1fc2a0284f-goog