From: Alvin Sun <alvin.sun@linux.dev>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
"Maxime Ripard" <mripard@kernel.org>,
"Thomas Zimmermann" <tzimmermann@suse.de>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Alexander Viro" <viro@zeniv.linux.org.uk>,
"Christian Brauner" <brauner@kernel.org>,
"Jan Kara" <jack@suse.cz>,
"Matthew Brost" <matthew.brost@intel.com>,
"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
"Maíra Canal" <mcanal@igalia.com>,
"Melissa Wen" <mwen@igalia.com>,
"Wambui Karuga" <wambui.karugax@gmail.com>,
"Eric Anholt" <eric@anholt.net>, "Ben Gamari" <bgamari@gmail.com>,
rust-for-linux@vger.kernel.org, driver-core@lists.linux.dev,
dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v2 2/9] rust: debugfs: add SeqShow trait and seq_file file operations
Date: Sat, 1 Aug 2026 00:52:06 +0800 [thread overview]
Message-ID: <d42d6ce2-7160-457f-be04-ff84551e9b9a@linux.dev> (raw)
In-Reply-To: <amyeMmmfEBpuVclw@google.com>
On 7/31/26 21:08, Alice Ryhl wrote:
> On Fri, Jul 31, 2026 at 01:05:40AM +0800, Alvin Sun wrote:
>> +/// Show callback for `SeqShow` types.
>> +///
>> +/// # Safety
>> +///
>> +/// The seq_file core guarantees that `seq` points to a live `seq_file` and that
>> +/// `seq->private` is a valid pointer to a `T` with no outstanding mutable
>> +/// references.
>> +unsafe extern "C" fn seq_file_show<S: SeqShow<T>, T: Sync>(
>> + seq: *mut bindings::seq_file,
>> + _: *mut crate::ffi::c_void,
>> +) -> crate::ffi::c_int {
>> + // SAFETY: `seq->private` is a valid `T` pointer with no outstanding
>> + // mutable references.
>> + let data = unsafe { &*((*seq).private.cast::<T>()) };
>> + // SAFETY: `seq` points to a live `seq_file`.
>> + let m = unsafe { SeqFile::from_raw(seq) };
>> + from_result(|| S::show(data, m).map(|()| 0))
>> +}
>> +
>> +/// Renders `data` into a seq_file.
>> +///
>> +/// `data` is the value stashed as the debugfs file's `i_private` at creation
>> +/// time. `show` is invoked on each read to produce the file's contents.
>> +///
>> +/// `open` and `release` are optional lifecycle hooks called during file open
>> +/// and release. They can be used to manage the lifetime of `data` (e.g.,
>> +/// reference counting). Default implementations are no-ops.
>> +pub trait SeqShow<T> {
>> + /// Writes debugfs output for the file.
>> + fn show(data: &T, m: &SeqFile) -> Result;
>> +
>> + /// Called during file open, before `single_open`.
>> + fn open(_data: &T) -> Result {
>> + Ok(())
>> + }
>> +
>> + /// Called during file release, before `single_release`.
>> + ///
>> + /// # Safety
>> + ///
>> + /// `data` must point to valid memory, kept alive by actions taken in
>> + /// [`Self::open`] (e.g., incrementing a reference count).
>> + unsafe fn release(_data: NonNull<T>) {}
>> +}
> Hmm. So in a later patch you implement SeqShow in the following manner:
>
> fn open(dev: &drm::Device<T, drm::Normal>) -> Result {
> // Hold a device reference so the device is not freed while the file is open.
> dev.inc_ref();
> Ok(())
> }
>
> unsafe fn release(dev: NonNull<drm::Device<T, drm::Normal>>) {
> // Drop the reference taken in `open`.
> // SAFETY: `dev` is valid per this function's safety contract.
> unsafe { drm::Device::<T>::dec_ref(dev) };
> }
>
> I don't understand why this increment/decrement pair is necessary.
> Just based on the code, it *looks* like you have them because otherwise
> `show()` might get called with a dangling pointer to the drm device, but
> the inc_ref() in open() ensures it stays alive.
>
> However, earlier in this patch you said:
>
>> The seq_file core guarantees that `seq` points to a live `seq_file` and
>> that `seq->private` is a valid pointer to a `T` with no outstanding
>> mutable references.
> So based on this, you are saying that open()/release() do not need to do
> anything special for the value to stay alive.
>
> These seem contradictory. Could you clarify the situation?
You're right. The `SeqShow` trait documentation was inaccurate -- the
framework does not manage the lifetime of `data`, so `inc_ref`/`dec_ref`
in `DrmSeqShow` is necessary to prevent `show()` from accessing a freed
device.
Will fix the doc in the next revision.
Best regards,
Alvin
>
> Alice
next prev parent reply other threads:[~2026-07-31 16:52 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-30 17:05 [PATCH v2 0/9] drm/tyr: add debugfs support Alvin Sun
2026-07-30 17:05 ` [PATCH v2 1/9] rust: seq_file: add as_raw() method Alvin Sun
2026-07-30 17:05 ` [PATCH v2 2/9] rust: debugfs: add SeqShow trait and seq_file file operations Alvin Sun
2026-07-31 13:08 ` Alice Ryhl
2026-07-31 16:52 ` Alvin Sun [this message]
2026-07-30 17:05 ` [PATCH v2 3/9] rust: debugfs: add Entry::from_raw and ScopedDir::from_dentry Alvin Sun
2026-07-30 17:05 ` [PATCH v2 4/9] drm: move debugfs_init after dev->registered is set Alvin Sun
2026-07-30 17:05 ` [PATCH v2 5/9] rust: drm: add debugfs_init callback to Driver trait Alvin Sun
2026-07-30 17:05 ` [PATCH v2 6/9] rust: drm: add DrmSeqShow seq_file adapter Alvin Sun
2026-07-30 17:05 ` [PATCH v2 7/9] rust: drm: gpuvm: add dump_gpuva_info to UniqueRefGpuVm Alvin Sun
2026-07-31 13:01 ` Alice Ryhl
2026-07-30 17:05 ` [PATCH v2 8/9] drm/tyr: add gpuvas debugfs file Alvin Sun
2026-07-31 12:59 ` Alice Ryhl
2026-07-31 16:29 ` Daniel Almeida
2026-07-30 17:05 ` [PATCH v2 9/9] drm/debugfs: hold device reference for the lifetime of open files Alvin Sun
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d42d6ce2-7160-457f-be04-ff84551e9b9a@linux.dev \
--to=alvin.sun@linux.dev \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=bgamari@gmail.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=brauner@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=driver-core@lists.linux.dev \
--cc=eric@anholt.net \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=jack@suse.cz \
--cc=lossin@kernel.org \
--cc=maarten.lankhorst@linux.intel.com \
--cc=matthew.brost@intel.com \
--cc=mcanal@igalia.com \
--cc=mripard@kernel.org \
--cc=mwen@igalia.com \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tamird@kernel.org \
--cc=thomas.hellstrom@linux.intel.com \
--cc=tmgross@umich.edu \
--cc=tzimmermann@suse.de \
--cc=viro@zeniv.linux.org.uk \
--cc=wambui.karugax@gmail.com \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.