linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andreas Hindborg <a.hindborg@kernel.org>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Gary Guo" <gary@garyguo.net>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Lorenzo Stoakes" <ljs@kernel.org>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Uladzislau Rezki" <urezki@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Lyude Paul" <lyude@redhat.com>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Arve Hjønnevåg" <arve@android.com>,
	"Todd Kjos" <tkjos@android.com>,
	"Christian Brauner" <brauner@kernel.org>,
	"Carlos Llamas" <cmllamas@google.com>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Dave Ertman" <david.m.ertman@intel.com>,
	"Leon Romanovsky" <leon@kernel.org>,
	"Paul Moore" <paul@paul-moore.com>,
	"Serge Hallyn" <sergeh@kernel.org>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Alexander Viro" <viro@zeniv.linux.org.uk>,
	"Jan Kara" <jack@suse.cz>,
	"Igor Korotin" <igor.korotin@linux.dev>,
	"Viresh Kumar" <vireshk@kernel.org>, "Nishanth Menon" <nm@ti.com>,
	"Stephen Boyd" <sboyd@kernel.org>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
	"Pavel Tikhomirov" <ptikhomirov@virtuozzo.com>,
	"Michal Wilczynski" <m.wilczynski@samsung.com>,
	"Ira Weiny" <iweiny@kernel.org>,
	"Philipp Stanner" <phasta@kernel.org>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org, driver-core@lists.linux.dev,
	linux-block@vger.kernel.org,
	linux-security-module@vger.kernel.org,
	dri-devel@lists.freedesktop.org, linux-fsdevel@vger.kernel.org,
	linux-pm@vger.kernel.org, linux-pci@vger.kernel.org,
	linux-pwm@vger.kernel.org, linux-usb@vger.kernel.org,
	"Asahi Lina" <lina+kernel@asahilina.net>
Subject: Re: [PATCH v20 4/8] rust: page: convert to `Ownable`'
Date: Mon, 07 Sep 2026 15:58:35 +0200	[thread overview]
Message-ID: <87zextw4c4.fsf@t14s.mail-host-address-is-not-set> (raw)
In-Reply-To: <CAH5fLgjEfZO9DwcUxaKCWd_hFUrXU+cxiwmRg1UnM2pKD6gPvg@mail.gmail.com>

"Alice Ryhl" <aliceryhl@google.com> writes:

> On Mon, Sep 7, 2026 at 2:38 PM Andreas Hindborg <a.hindborg@kernel.org> wrote:
>>
>> Alice Ryhl <aliceryhl@google.com> writes:
>>
>> > On Sun, Sep 6, 2026 at 3:02 PM Gary Guo <gary@garyguo.net> wrote:
>> >>
>> >> On Tue Aug 25, 2026 at 2:20 PM BST, Alice Ryhl wrote:
>> >> > On Mon, Aug 24, 2026 at 01:17:56PM +0200, Andreas Hindborg wrote:
>> >> >> +        // SAFETY: We just successfully allocated a page, so we now have ownership of the newly
>> >> >> +        // allocated page. We transfer that ownership to the new `Owned<Page>` object.
>> >> >> +        // Since `Page` is transparent, we can cast the pointer directly.
>> >> >> +        Ok(unsafe { Owned::from_raw(page.cast()) })
>> >> >
>> >> > This doesn't satisfy the safety requirements of Owned::from_raw()
>> >> > because the page may be used with vm_insert_page(), which increments its
>> >> > refcount and causes it to be shared the vma system, and this occurs
>> >> > before Page::release() is called.
>> >>
>> >> I suppose the existing vm_insert_page() abstraction we have is already
>> >> problematic, because it uses `&Page`?
>> >>
>> >> Maybe we want to change the API to use `ARef<Page>` so it already has to be
>> >> shared? Conceptually it takes a reference count from a `&Page`, which isn't
>> >> possible because `Page` is not `AlwaysRefCounted`, so it needs a `&ARef<Page>`
>> >> to be able to do that op.
>> >
>> > Honestly, the problem is the safety requirements of Owned::from_raw().
>> > Pages have a "special" main reference, and free_page() does more than
>> > put_page(). It even does something when the refcount does not hit
>> > zero.
>> >
>> > The correct behavior for Page is to allow the user to hold one
>> > Owned<Page> whose drop calls free_page(), *plus* any number of
>> > ARef<Page> references that invoke put_page() on drop. This way, the
>> > owned page controls the special drop codepath.
>>
>> This does not mesh well with the model of `Owned` behaving like
>> `UniqueArc` to `ARef` behaving like an `Arc`.
>
> It's a different case, I agree.
>
>> Please help me understand; with page having a main ref and an auxiliary
>> refcount, if we model that with a single `Owned<Page>` and a number of
>> `ARef<Page>`, what would happen in the case where the main ref (`Owned`)
>> is dropped first? Is this legal?
>
> Yes, it's legal.
>
>> My intuition here would be to follow Garry's suggestion and have
>> `vm_insert_page` take an `ARef<Page>`. Can you elaborate why this is not
>> an option?
>
> That would be the wrong ownership semantics. The C side increments the
> refcount rather than take ownership of a passed-in refcount, so the
> correct argument type is &Page.

Or `&ARef<Page>`? Because with `Page` being `Ownable` it would not be
OK to increment the refcount from just a `&Page`.

> Now, for the use-case in Binder I believe we could switch to
> put_page(), as we don't need the extra stuff from free_page(), in
> which case we only want ARef<Page> and do not require Owned support.
> But as long as we are calling free_page(), it needs to not be
> clonable.

You are calling `__free_page` directly in binder? I could not find this
with grep except in the C binder. Is this code you are calling into from
Rust binder?

From `__free_pages()` kernel-doc (`mm/page_alloc.c`):

> If the last reference to this page is speculative, it will be
> released by put_page() which only frees the first page of a
> non-compound allocation. [...] **If you want to use the page's
> reference count to decide when to free the allocation, you should
> allocate a compound page, and use put_page() instead of
> __free_pages().**

I guess this is what you are referring to? We could change `Page` to
this style as well and all would be fine I think?

At any rate, today I learned about Pfn walkers, compaction,
memory-failure, etc. that take transient refcounts
pages. With that in mind we need to reformulate the invariant of
`Ownable` to exclude optimistic transient references.

Best regards,
Andreas Hindborg



  reply	other threads:[~2026-09-07 13:58 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-24 11:17 [PATCH v20 0/8] rust: add `Ownable` trait and `Owned` type Andreas Hindborg
2026-08-24 11:17 ` [PATCH v20 1/8] rust: alloc: add `KBox::into_non_null` Andreas Hindborg
2026-08-25 13:01   ` Danilo Krummrich
2026-08-24 11:17 ` [PATCH v20 2/8] rust: types: Add Ownable/Owned types Andreas Hindborg
2026-08-24 11:17 ` [PATCH v20 3/8] rust: implement `ForeignOwnable` for `Owned` Andreas Hindborg
2026-08-24 11:17 ` [PATCH v20 4/8] rust: page: convert to `Ownable` Andreas Hindborg
2026-08-25 13:20   ` [PATCH v20 4/8] rust: page: convert to `Ownable`' Alice Ryhl
2026-09-06 13:02     ` Gary Guo
2026-09-07  9:24       ` Alice Ryhl
2026-09-07 12:38         ` Andreas Hindborg
2026-09-07 13:31           ` Alice Ryhl
2026-09-07 13:58             ` Andreas Hindborg [this message]
2026-09-07 14:06               ` Alice Ryhl
2026-09-08  8:46                 ` Andreas Hindborg
2026-09-08  9:18                   ` Alice Ryhl
2026-09-08 12:10                     ` Andreas Hindborg
2026-08-25 13:34   ` [PATCH v20 4/8] rust: page: convert to `Ownable` Danilo Krummrich
2026-08-24 11:17 ` [PATCH v20 5/8] rust: rename `AlwaysRefCounted` to `RefCounted` Andreas Hindborg
2026-08-24 12:26   ` Uwe Kleine-König
2026-08-24 11:17 ` [PATCH v20 6/8] rust: Add missing SAFETY documentation for `ARef` example Andreas Hindborg
2026-08-24 11:36   ` Miguel Ojeda
2026-08-26  9:29     ` Andreas Hindborg
2026-09-03 13:28       ` Miguel Ojeda
2026-08-24 11:17 ` [PATCH v20 7/8] rust: Add `OwnableRefCounted` Andreas Hindborg
2026-08-25 13:16   ` Danilo Krummrich
2026-08-25 13:37     ` Gary Guo
2026-08-24 11:18 ` [PATCH v20 8/8] rust: page: add `from_raw()` Andreas Hindborg

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=87zextw4c4.fsf@t14s.mail-host-address-is-not-set \
    --to=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=arve@android.com \
    --cc=bhelgaas@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=brauner@kernel.org \
    --cc=cmllamas@google.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=david.m.ertman@intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=driver-core@lists.linux.dev \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=igor.korotin@linux.dev \
    --cc=iweiny@kernel.org \
    --cc=jack@suse.cz \
    --cc=kwilczynski@kernel.org \
    --cc=leon@kernel.org \
    --cc=liam@infradead.org \
    --cc=lina+kernel@asahilina.net \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=ljs@kernel.org \
    --cc=lossin@kernel.org \
    --cc=lyude@redhat.com \
    --cc=m.wilczynski@samsung.com \
    --cc=nm@ti.com \
    --cc=ojeda@kernel.org \
    --cc=paul@paul-moore.com \
    --cc=phasta@kernel.org \
    --cc=ptikhomirov@virtuozzo.com \
    --cc=rafael@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sboyd@kernel.org \
    --cc=sergeh@kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tamird@kernel.org \
    --cc=tkjos@android.com \
    --cc=tmgross@umich.edu \
    --cc=urezki@gmail.com \
    --cc=vbabka@kernel.org \
    --cc=vireshk@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).