mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4] rust_binder: unregister shrinker if binderfs initialization fails
@ 2026-12-03  3:22 Poil Zero
  2026-12-03  5:28 ` Shuai Liu
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Poil Zero @ 2026-12-03  3:22 UTC (permalink / raw)
  To: Alice Ryhl, Greg Kroah-Hartman
  Cc: 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")
Signed-off-by: Poil Zero <poilandgginchi@gmail.com>
---
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).

I build-tested the patched Rust Binder object on x86_64 at 89a312991dc6
with CONFIG_ANDROID_BINDER_IPC_RUST=y. I checked the failure path by
source inspection on v7.4-rc5 (fe6ff8d5d675).

 drivers/android/binder/page_range.rs       | 16 ++++++++++++++++
 drivers/android/binder/rust_binder_main.rs |  6 +++++-
 2 files changed, 21 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,22 @@ impl Shrinker {
 
         Ok(())
     }
+
+    /// Unregister this shrinker and destroy its LRU.
+    ///
+    /// Must only be called after a successful `register`.
+    pub(crate) 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,11 @@ 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 {
+            BINDER_SHRINKER.unregister();
+            return Err(Error::from_errno(ret));
+        }
 
         Ok(Self { _netlink: netlink })
     }

base-commit: 89a312991dc6e638a36adc43ccb91dbc25504c04
-- 
Poil Zero

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4] rust_binder: unregister shrinker if binderfs initialization fails
  2026-12-03  3:22 [PATCH v4] rust_binder: unregister shrinker if binderfs initialization fails Poil Zero
@ 2026-12-03  5:28 ` Shuai Liu
  2026-12-03  8:19 ` Alice Ryhl
  2026-12-03 12:05 ` Greg Kroah-Hartman
  2 siblings, 0 replies; 4+ messages in thread
From: Shuai Liu @ 2026-12-03  5:28 UTC (permalink / raw)
  To: poilandgginchi, aliceryhl, gregkh
  Cc: arve, tkjos, brauner, cmllamas, rust-for-linux, linux-kernel

Hi Poil,

Reviewed-by: Shuai Liu <847807867@qq.com>

I reviewed the patch and verified it:

- Applied cleanly on top of master (d06ce6f1eb5ee).
- Build-tested with CONFIG_RUST=y and CONFIG_ANDROID_BINDER_IPC_RUST=y
  (clang 18, rustc 1.100); the Rust Binder object compiles cleanly.
- Boot-tested in QEMU: the Rust kernel KUnit suites pass and
  rust_binderfs mounts with the expected devices (binder, hwbinder,
  vndbinder, binder-control, features) and unmounts cleanly, no
  warnings in the boot log.
- The unregister() ordering is correct: shrinker_free() before
  list_lru_destroy(). mm/shrinker.c confirms shrinker_free() waits
  for in-flight callbacks via wait_for_completion() and never touches
  the caller-owned list_lru, so destroying the list_lru afterwards is
  safe and no double-destroy can happen (list_lru_destroy() is
  idempotent anyway).

One side note: the pre-existing Error::from_errno(ret) with ret == 0
in the shrinker_alloc() failure path of Shrinker::register() is
already addressed by Adhyansh Verma's pending patch "rust_binder:
return ENOMEM on shrinker allocation failure", so nothing to do here.

Thanks,
Shuai Liu


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4] rust_binder: unregister shrinker if binderfs initialization fails
  2026-12-03  3:22 [PATCH v4] rust_binder: unregister shrinker if binderfs initialization fails Poil Zero
  2026-12-03  5:28 ` Shuai Liu
@ 2026-12-03  8:19 ` Alice Ryhl
  2026-12-03 12:05 ` Greg Kroah-Hartman
  2 siblings, 0 replies; 4+ messages in thread
From: Alice Ryhl @ 2026-12-03  8:19 UTC (permalink / raw)
  To: Poil Zero
  Cc: Greg Kroah-Hartman, Arve Hjønnevåg, Todd Kjos,
	Christian Brauner, Carlos Llamas, linux-kernel, rust-for-linux

On Thu, Dec 3, 2026 at 4:23 AM Poil Zero <poilandgginchi@gmail.com> wrote:
>
> 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")
> Signed-off-by: Poil Zero <poilandgginchi@gmail.com>

I think it'd make sense to also update the safety comment on the
shrinker's new() method to say that the shrinker must not have been
unregistered. Or just make unregister() unsafe.

Alice

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4] rust_binder: unregister shrinker if binderfs initialization fails
  2026-12-03  3:22 [PATCH v4] rust_binder: unregister shrinker if binderfs initialization fails Poil Zero
  2026-12-03  5:28 ` Shuai Liu
  2026-12-03  8:19 ` Alice Ryhl
@ 2026-12-03 12:05 ` Greg Kroah-Hartman
  2 siblings, 0 replies; 4+ messages in thread
From: Greg Kroah-Hartman @ 2026-12-03 12:05 UTC (permalink / raw)
  To: Poil Zero
  Cc: Alice Ryhl, Arve Hjønnevåg, Todd Kjos,
	Christian Brauner, Carlos Llamas, linux-kernel, rust-for-linux

On Thu, Dec 03, 2026 at 11:22:06AM +0800, Poil Zero wrote:
> 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")
> Signed-off-by: Poil Zero <poilandgginchi@gmail.com>
> ---
> Changes in v4:
> - Resend with git send-email to preserve the diff headers.
> 
> Changes in v3:
> - Match the From and Signed-off-by names.

You might have matched it, but that's not the correct one to use, please
fix and use your real name for both, I can't take this as-is.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-03 12:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03  3:22 [PATCH v4] rust_binder: unregister shrinker if binderfs initialization fails Poil Zero
2026-12-03  5:28 ` Shuai Liu
2026-12-03  8:19 ` Alice Ryhl
2026-12-03 12:05 ` Greg Kroah-Hartman

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®