mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Poil Zero <poilandgginchi@gmail.com>
To: Alice Ryhl <aliceryhl@google.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "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 v4] rust_binder: unregister shrinker if binderfs initialization fails
Date: Thu,  3 Dec 2026 11:22:06 +0800	[thread overview]
Message-ID: <20261203032206.1039135-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")
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

             reply	other threads:[~2026-12-03  3:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  3:22 Poil Zero [this message]
2026-12-03  5:28 ` Shuai Liu
2026-12-03  8:19 ` Alice Ryhl
2026-12-03 12:05 ` Greg Kroah-Hartman
     [not found]   ` <CAGdLfDdeZxW1QgWJaBu0g6La1vSMVduFnL9bN+zUrx6ife9ZrQ@mail.gmail.com>
2026-12-04  3:42     ` Greg Kroah-Hartman

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=20261203032206.1039135-1-poilandgginchi@gmail.com \
    --to=poilandgginchi@gmail.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®