mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v5] rust_binder: unregister shrinker if binderfs initialization fails
@ 2026-12-03 13:43 Shengqiang Zeng
  0 siblings, 0 replies; only message in thread
From: Shengqiang Zeng @ 2026-12-03 13:43 UTC (permalink / raw)
  To: Alice Ryhl, Greg Kroah-Hartman
  Cc: Shuai Liu, Arve Hjønnevåg, Todd Kjos,
	Christian Brauner, Carlos Llamas, linux-kernel, rust-for-linux

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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-03 13:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03 13:43 [PATCH v5] rust_binder: unregister shrinker if binderfs initialization fails Shengqiang Zeng

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®