From: Shengqiang Zeng <poilandgginchi@gmail.com>
To: Alice Ryhl <aliceryhl@google.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Shuai Liu" <847807867@qq.com>,
"Arve Hjønnevåg" <arve@android.com>,
"Todd Kjos" <tkjos@android.com>,
"Christian Brauner" <brauner@kernel.org>,
"Carlos Llamas" <cmllamas@google.com>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: [PATCH v5] rust_binder: unregister shrinker if binderfs initialization fails
Date: Thu, 3 Dec 2026 21:43:53 +0800 [thread overview]
Message-ID: <20261203134353.528455-1-poilandgginchi@gmail.com> (raw)
BinderModule::init() registers the Binder shrinker before initializing
binderfs. If init_rust_binderfs() fails, the driver returns an error
but leaves the shrinker registered and its list_lru allocated.
Unregister the shrinker on this error path before destroying its
list_lru so that active reclaim callbacks finish before teardown.
Return the original binderfs error.
Fixes: eafedbc7c050 ("rust_binder: add Rust Binder driver")
Reviewed-by: Shuai Liu <847807867@qq.com>
Signed-off-by: Shengqiang Zeng <poilandgginchi@gmail.com>
---
Changes in v5:
- Use Shengqiang Zeng in From and Signed-off-by, as requested by Greg.
- Alice suggested extending the safety contract on Shrinker::new() or
making unregister() unsafe. I chose the latter: only the caller knows
whether a Binder page range can still use the LRU.
- Document the preconditions on unregister() and explain why the
binderfs init failure path satisfies them. This follows
ShrinkablePageRange::fill_zero() in drivers/android/binder/page_range.rs,
which also leaves a page-lifetime check to the caller.
Review: https://lore.kernel.org/r/CAH5fLgjDuAi6-QQ9q9X20oKZdOPr_j71Ncnzpyzmk+dY2jhbxQ@mail.gmail.com/
Rust safety documentation: https://docs.kernel.org/rust/coding-guidelines.html
Changes in v4:
- Resend with git send-email to preserve the diff headers.
Changes in v3:
- Match the From and Signed-off-by names.
- Use Error::from_errno() via the prelude (Alice).
Previous revision: build-tested the Rust Binder object on x86_64 at
89a312991dc6 with CONFIG_ANDROID_BINDER_IPC_RUST=y and checked the
failure path by source inspection on v7.4-rc5 (fe6ff8d5d675).
drivers/android/binder/page_range.rs | 20 ++++++++++++++++++++
drivers/android/binder/rust_binder_main.rs | 9 ++++++++-
2 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/drivers/android/binder/page_range.rs b/drivers/android/binder/page_range.rs
--- a/drivers/android/binder/page_range.rs
+++ b/drivers/android/binder/page_range.rs
@@ -103,6 +103,26 @@ impl Shrinker {
Ok(())
}
+
+ /// Unregister this shrinker and destroy its LRU.
+ ///
+ /// # Safety
+ ///
+ /// The shrinker must be registered. The caller must ensure it is
+ /// unregistered only once and that no `ShrinkablePageRange` can use it
+ /// during or after this call.
+ pub(crate) unsafe fn unregister(&'static self) {
+ // SAFETY: A successful register initialized list_lru and
+ // stored the registered shrinker in inner.
+ unsafe {
+ let shrinker = *self.inner.get();
+ if !shrinker.is_null() {
+ bindings::shrinker_free(shrinker);
+ self.inner.get().write(ptr::null_mut());
+ }
+ bindings::list_lru_destroy(self.list_lru.get());
+ }
+ }
}
/// A container that manages a page range in a vma.
diff --git a/drivers/android/binder/rust_binder_main.rs b/drivers/android/binder/rust_binder_main.rs
--- a/drivers/android/binder/rust_binder_main.rs
+++ b/drivers/android/binder/rust_binder_main.rs
@@ -309,7 +309,14 @@ impl kernel::Module for BinderModule {
BINDER_SHRINKER.register(c"android-binder")?;
// SAFETY: The module is being loaded, so we can initialize binderfs.
- unsafe { kernel::error::to_result(binderfs::init_rust_binderfs())? };
+ let ret = unsafe { binderfs::init_rust_binderfs() };
+ if ret != 0 {
+ // SAFETY: We registered the shrinker above. Binderfs registration
+ // did not succeed, so no Binder device was exposed and no page range
+ // can use the shrinker.
+ unsafe { BINDER_SHRINKER.unregister() };
+ return Err(Error::from_errno(ret));
+ }
Ok(Self { _netlink: netlink })
}
base-commit: 89a312991dc6e638a36adc43ccb91dbc25504c04
--
Shengqiang Zeng
reply other threads:[~2026-12-03 13:44 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=20261203134353.528455-1-poilandgginchi@gmail.com \
--to=poilandgginchi@gmail.com \
--cc=847807867@qq.com \
--cc=aliceryhl@google.com \
--cc=arve@android.com \
--cc=brauner@kernel.org \
--cc=cmllamas@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tkjos@android.com \
/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®