The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Bo Zhang <zhangbo0325@gmail.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,  Bo Zhang <zhangbo56@xiaomi.com>
Subject: Re: [RFC PATCH 0/1] binder: switch alloc->mutex back to spinlock
Date: Thu, 6 Aug 2026 09:01:19 +0000	[thread overview]
Message-ID: <anRNX4sftcuDXRj8@google.com> (raw)
In-Reply-To: <20260805152752.1924434-1-zhangbo56@xiaomi.com>

On Wed, Aug 05, 2026 at 11:27:51PM +0800, Bo Zhang wrote:
> Hi,
> 
> This patch switches the binder_alloc mutex back to a spinlock to reduce
> transaction latency.
> 
> Background:
> 
> Commit 7710e2cca32e ("binder: switch alloc->mutex to spinlock_t")
> originally converted the mutex to a spinlock for performance. It was
> later reverted by commit 8b52c7261e04 in preparation for commit
> d1716b4b78fb ("binder: concurrent page installation"), which states:
> 
>   "zap_page_range_single() is called under the alloc->mutex to avoid
>    racing with the shrinker."
> 
> Analysis:
> 
> After inspection, holding the lock across zap_vma_range() in the shrinker
> path is unnecessary. The page installation side does NOT hold alloc->mutex,
> so the mutex provides no mutual exclusion between install and zap. The 
> actual synchronization is guaranteed at the PTE lock level:
> 
>   1. vm_insert_page() acquires the PTE lock to set the PTE entry.
>   2. zap_vma_range() acquires the PTE lock to clear the PTE entry.
>   3. On race conditions, the install path calls binder_page_lookup
>      which uses get_user_pages_remote() to atomically pin the page
>      under PTE lock, preventing use-after-free regardless of zap timing.
> 
> The alloc lock only needs to protect buffer metadata (rb-trees, pages
> array, LRU list operations), all of which are non-sleeping and complete
> before zap_vma_range() is called.
> 
> By moving spin_unlock() before zap_vma_range() in the shrinker path, we
> can safely convert back to a spinlock.
> 
> Performance (binderThroughputTest, Qualcomm SM8850, 2 workers, 10 runs):
> 
>                     mutex               spinlock
>   throughput:    27k-59k iter/s       79k-84k iter/s  (~80% improvement)
>   average:       0.031-0.068ms        0.022-0.023ms   (~45% reduction)
>   P99:           0.088-0.148ms        0.050-0.062ms   (~55% reduction)
>   variance:      high (2x spread)     low (stable)
> 
> The spinlock eliminates priority inversion where low-priority tasks
> holding the mutex sleep, blocking high-priority binder transactions.
> 
> Looking forward to feedback on this analysis.

This will not work due to the following race condition between T1 and T2:

T1 binder_alloc_free_page() removes the page from alloc->pages
T2 binder_alloc_new_buf_locked() runs
T2 binder_install_single_page() gets -EBUSY
T2 binder_page_lookup() looks up the page in the vma
T1 binder_alloc_free_page() calls zap_vma_range()

In this scenario, T2 looks up a page that it expects to be "pinned" and
not removable by the shrinker, but the shrinker zaps it anyway.

Today this cannot happen because binder_alloc_new_buf_locked() runs
under the mutex, which ensures that zap_vma_range() finishes removing
the page before binder_install_single_page() runs.

Alice

  parent reply	other threads:[~2026-08-06  9:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-05 15:27 [RFC PATCH 0/1] binder: switch alloc->mutex back to spinlock Bo Zhang
2026-08-05 15:27 ` [RFC PATCH 1/1] " Bo Zhang
2026-08-06  9:01 ` Alice Ryhl [this message]
2026-08-06 14:23   ` [RFC PATCH 0/1] " 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=anRNX4sftcuDXRj8@google.com \
    --to=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=zhangbo0325@gmail.com \
    --cc=zhangbo56@xiaomi.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