mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Philipp Stanner <phasta@mailbox.org>
To: "Danilo Krummrich" <dakr@redhat.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Boris Brezillon" <boris.brezillon@collabora.com>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>,
	"Christian König" <christian.koenig@amd.com>,
	manos@pitsidianak.is, "Lyude Paul" <lyude@redhat.com>,
	lossin@kernel.org, gnurou@gnurou.org, airlied@kernel.org
Cc: r4l <rust-for-linux@vger.kernel.org>, linux-kernel@vger.kernel.org
Subject: Rust Discussion: What to do about payload data's drop() vs atomic context
Date: Mon, 08 Jun 2026 10:25:13 +0200	[thread overview]
Message-ID: <09832bfe83bce37cdaadc11ea009e3dfc1597ddb.camel@mailbox.org> (raw)

Yo folks,

we (DRM crowd) are currently in the process of upstreaming DmaFences,
data structures for communication between GPU / driver and userspace.
They are inherently difficult to get right. Our discussions [1] have
now repeatedly circled around a supposedly extremely difficult problem:

struct Foo<T> {
data: T, // `T` defined & passed by API user
}


Sometimes we, the API backend, will drop Foo in atomic context. Should
T have a drop implementation, and should that implementation do
something illegal in atomic context, we might deadlock.

It gets further complicated by the fact that T might often be
refcounted, and illegal actions might only take place sometimes, when
the refcount finally hits 0.

Since T is defined by the user / driver, it can become arbitrarily
complex and might include all sorts of other Rust data types, each with
their own Drop implementation.

So to get this deadlock-safe you'd need to audit all inherited data
types' drop implementations.

Suggested solutions for this problem that I've heard are:

   1. Demand in your API that T implements an unsafe trait, for which
      you document what the rules are (no sleeping etc.).
   2. Demand that T does not implement Drop.
   3. Only allow a T that gets dropped in a deferred way outside of
      atomic context.
   4. Try to ensure that Foo<T> never drops in atomic context (not
      really fully achievable, because the user can always pass
      Option<T>, and take the value in the mut borrow our API needs to
      implement to give (temporary) access to T back to the user in a
      callback.
   5. In your API, implement the most common use-cases (like kicking
      off a work item) in a way that passed data does not need to drop
      from atomic context.

IIRC Gary has it on his radar to get klint to check everything within T
for invalid drop operations? Is that doable any time soon? Would that
be 100% bullet-proof or would it work like "best effort"?

The C side seems to address those problems with might_sleep() et al.,
only detecting problems at runtime.

I'm opening this thread because I suspect that "drop() must not
sleep()" is a very common problem in Kernel Rust and I'd like to hear
how other people have solved the problem.

If we'd agree that demanding an unsafe-trait for T for such APIs is the
strategy with the lowest cost-benefit ratio, I suspect that it would
make sense to define such a trait kernel-wide.

Please share all your thoughts.

For DmaFence, we now need to decide how to deal with the user data,
i.e. which strategy to implement before we land it upstream.

I'm bringing this up because we really want to get DmaFence right.


Greetings
Philipp


[1] https://lore.kernel.org/all/20260530143541.229628-2-phasta@kernel.org/

                 reply	other threads:[~2026-06-08  8:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=09832bfe83bce37cdaadc11ea009e3dfc1597ddb.camel@mailbox.org \
    --to=phasta@mailbox.org \
    --cc=a.hindborg@kernel.org \
    --cc=airlied@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=boqun@kernel.org \
    --cc=boris.brezillon@collabora.com \
    --cc=christian.koenig@amd.com \
    --cc=dakr@redhat.com \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=gnurou@gnurou.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=lyude@redhat.com \
    --cc=manos@pitsidianak.is \
    --cc=ojeda@kernel.org \
    --cc=phasta@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®