From: Bo Zhang <zhangbo0325@gmail.com>
To: aliceryhl@google.com
Cc: gregkh@linuxfoundation.org, cmllamas@google.com,
arve@android.com, tkjos@android.com, christian@brauner.io,
surenb@google.com, baohua@kernel.org, zhanghongru06@gmail.com,
linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH v4 1/2] binder: switch alloc->mutex to spinlock for buffer metadata
Date: Tue, 8 Sep 2026 14:44:06 +0800 [thread overview]
Message-ID: <20260908064406.1048059-1-zhangbo0325@gmail.com> (raw)
In-Reply-To: <20260907130028.807366-2-zhangbo0325@gmail.com>
Thanks for the review. Both are real regressions introduced by this
patch: the current tree uses a mutex here, and neither problem exists
under the mutex. Converting to a spinlock is what introduces them, so
they must be fixed rather than left as-is.
The root cause is that this patch (spinlock only) cannot fix either
problem on its own, because the fixes rely on the install_mutex added in
patch 2. I will therefore fold the two patches into one in v5, so the
spinlock and install_mutex land together and the intermediate state is
never reached.
1) Soft lockup holding the spinlock across cleanup
Sashiko says
"this loop iterates over all allocated buffers and pages, it can execute
up to 4MB of memset operations and 1024 calls to binder_free_page() while
preemption is disabled by alloc->lock."
Correct. Under the mutex this loop is preemptible; under the spinlock it
is not, so unprivileged userspace can keep a CPU with preemption disabled.
In v5, binder_alloc_deferred_release() drops alloc->lock around the
sleeping/long-running work: the clear-on-free memset and binder_free_page()
run outside the spinlock, while alloc->lock only covers the rb-tree and
LRU bookkeeping.
2) Use-after-free from the early spin_unlock() in the shrinker
Sashiko says
"By dropping alloc->lock here, the shrinker allows a concurrent
binder_alloc_deferred_release() ... to acquire the lock ... The release
function can then complete its cleanup ... and eventually free the
binder_alloc structure. When the shrinker resumes execution, it accesses
the freed alloc structure when calling trace_binder_unmap_user_end()."
Correct. Under the mutex the shrinker's zap and trace ran inside
alloc->mutex, which deferred_release() also took, so release waited for
the shrinker. Dropping the spinlock early breaks that. In v5 the shrinker
already holds install_mutex across the zap/trace (from the folded patch 2),
so binder_alloc_deferred_release() takes install_mutex too and waits for
the shrinker to finish before freeing the alloc.
Note that deferred_release() runs from binder_free_proc(), after all
threads are released and there are no in-flight transactions, so no page
install can run concurrently; the only concurrent writer to pages[] is the
shrinker, which install_mutex now serializes against.
v5 will fold the two patches and carry both fixes.
Bo
next prev parent reply other threads:[~2026-09-08 6:44 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-07 13:00 [RFC PATCH v4 0/2] binder: split alloc->mutex to improve performance Bo Zhang
2026-09-07 13:00 ` [RFC PATCH v4 1/2] binder: switch alloc->mutex to spinlock for buffer metadata Bo Zhang
2026-09-08 6:44 ` Bo Zhang [this message]
2026-09-07 13:00 ` [RFC PATCH v4 2/2] binder: add install_mutex to serialize page install and shrinker zap Bo Zhang
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=20260908064406.1048059-1-zhangbo0325@gmail.com \
--to=zhangbo0325@gmail.com \
--cc=aliceryhl@google.com \
--cc=arve@android.com \
--cc=baohua@kernel.org \
--cc=christian@brauner.io \
--cc=cmllamas@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=surenb@google.com \
--cc=tkjos@android.com \
--cc=zhanghongru06@gmail.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®