From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8819E44E046 for ; Thu, 6 Aug 2026 20:05:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.69 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786046776; cv=none; b=lZZEOSzR2asi1ULiH5bKc6XWHqb5MCOUMFsxLGPP9PR7gu/GXhV6AHDG7n/yjOEact30CuGqXv95DGN2NvWYR8dpl+DzX2iU5h697O0gAgFPuFUvVYuSXypgaaCazZqk228J73hr9+AyrOhUGOxQMYN62YOq+FHPo0GZnm3g5i8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786046776; c=relaxed/simple; bh=4yjw2zsE7pLA/X5wVKxsc5NH0WMSyFKwZIveFBbGQnc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iNYdwZaaqvVzkxs1FHmpq/Nuh6ckccpHC0ROJMaJlKYtHDKCvJTJ8IdqJeKgPVhD2zgt3K2SzgJR1rh9fOFNEg7yrRDJtC1oqCjKIHFHyQz6v332hoyARk3nGMLwMGQL/OVF/qIFHO2iYpjhW/b+AZtZjnrAcl8ArdTwvyPHQHw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=nsu7R9d3; arc=none smtp.client-ip=209.85.216.69 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nsu7R9d3" Received: by mail-pj1-f69.google.com with SMTP id 98e67ed59e1d1-38ecc48b3c2so3674906a91.1 for ; Thu, 06 Aug 2026 13:05:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786046756; x=1786651556; darn=vger.kernel.org; h=content-transfer-encoding:content-type:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:from:to:cc :subject:date:message-id:reply-to:content-type; bh=myw2oDUjCx3VpKcfHammnirgTkZxfVTM2uRSXVRFIyw=; b=nsu7R9d3bYSUsNSYKO9jwj47dB1QV+W0VAZi+JivXuIxyODm7RO7AFWcssnaTRrwxx Gg6c19gGOS1Wqi2qv5DUklx5Lo5Ivb8RYzbQa8Pen+8uPFYX9p+OT6SyoyepyIXq9u5c m1weamEblrhO5J/vhdFVJcMCPVXJ0SraBjVp5qd/HBG0O+zTnB31trFp2yWj+cSfQxKa 95b5Pde8eUFHqkXaXIME0g2NsyyuNP2IX4yVKGNm6Af67wETsllF7XfFebFxd5YSZfYh CMKAaRHZ6m6zjR3D5b+ITn5FZ/mi4x5n9DgnCI6URlYHLEO0LOGmJ9smUomntkEkFqmu 7HgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786046756; x=1786651556; h=content-transfer-encoding:content-type:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=myw2oDUjCx3VpKcfHammnirgTkZxfVTM2uRSXVRFIyw=; b=GpgSimatefhvSOilAPS1dv6wGFL9t0yjO3G8GAVxkuhB0tHw5/5GIiyX+R9No/R1tU 2HY8D/AQARk4b8jKgsCbwQ5h3jcyuv/K8Hna/5vMq5LJoXFyFeBDx1mXJruEGcpNI1Rt sdRHZ09wfAZ/Q2YKyFabPYjrWQCytNB0fr1OTT1pjzxrofN+5QK8dwTAaeTzq/3ol5oT aPV8ItwfGwgC0i2MhCDbvp3E9fUlRSjFyHZk6fFcQi3LTJ17mEt+hl9hUlo6y0WZYecM vN7r/y9bkPLCj1Ha9xKFf5kmsCDiA+d4mTYC6OHKdIVXE3RPPgDYyxnvvsDR6U4d1naG vfMw== X-Forwarded-Encrypted: i=1; AHgh+RqJJi5YR+M1QWHJ1YtAGqQ4Tqv1E73iRx0JX4JnRi/P+Q//XBIoMX+1Wafg0SmKLzsqwEwxN1k=@vger.kernel.org X-Gm-Message-State: AOJu0YwWaoR+FG5xnA091oB0gdjK1SBAokiv5q0sjlUJVGoB4BmyKNCa oyieoMDxtWJH94ywQMD9CNmmSlYlj472gIs+oujVDP5LsaVxCOkt92BccKV8uDrM2ALDbflvjiC N1HRHoA== X-Received: from dlam21-n2.prod.google.com ([2002:a05:701b:2095:20b0:13c:f3ec:de27]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2d50:b0:380:f34a:6ff1 with SMTP id 98e67ed59e1d1-3903c5e0213mr17427991a91.17.1786046755489; Thu, 06 Aug 2026 13:05:55 -0700 (PDT) Date: Thu, 6 Aug 2026 13:05:45 -0700 In-Reply-To: <20260806200548.3124802-1-surenb@google.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260806200548.3124802-1-surenb@google.com> X-Mailer: git-send-email 2.55.0.654.g21b8a5bc05-goog Message-ID: <20260806200548.3124802-3-surenb@google.com> Subject: [PATCH v4 2/5] binder: Make shrinker rely solely on per-VMA lock From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: dave.hansen@linux.intel.com, Liam.Howlett@oracle.com, ljs@kernel.org, david@kernel.org, willy@infradead.org, shakeel.butt@linux.dev, vbabka@kernel.org, jannh@google.com, aliceryhl@google.com, arve@android.com, cmllamas@google.com, christian@brauner.io, tkjos@android.com, dsahern@kernel.org, davem@davemloft.net, gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, netdev@vger.kernel.org, surenb@google.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: Dave Hansen tl;dr: lock_vma_under_rcu() is already a trylock. No need to do both it and mmap_read_trylock(). Long Version: =3D=3D Background =3D=3D Historically, binder used an mmap_read_trylock() in its shrinker code. This ensures that reclaim is not blocked on an mmap_lock. Commit 95bc2d4a9020 ("binder: use per-vma lock in page reclaiming") added support for the per-VMA lock, but left mmap_read_trylock() as a fallback. This was presumably because the per-VMA locking can fail for several reasons and most (all?) lock_vma_under_rcu() callers have a fallback to mmap_read_trylock(). =3D=3D Problem =3D=3D The fallback is not worth the complexity here. lock_vma_under_rcu() is essentially already a non-blocking trylock. The main reason it fails is also the reason mmap_read_trylock() fails: something is holding mmap_write_lock(). The only remedy for a collision with mmap_write_lock() is to wait, which this code can not do. So the "fallback" after lock_vma_under_rcu() failure is not really a fallback: it is really likely to just be retrying in vain. That retry in an of itself isn't horrible. But it adds complexity. =3D=3D Solution =3D=3D Now that per-VMA locks are universally available, lock_vma_under_rcu() will not persistently fail. Rely on it alone and simplify the code. The removal of the fallback does not affect NOMMU case because binder driver depends on CONFIG_MMU. Full disclosure: I originally tried to do this with lock_vma_under_rcu_wait(), but it did not fit well with the mmap_lock trylock semantics. Claude caught this in a review and suggested the approach in this path. It seemed sane to me. So, Suggesed-by: Claude, I guess. Signed-off-by: Dave Hansen Signed-off-by: Suren Baghdasaryan Cc: Andrew Morton Cc: "Liam R. Howlett" Cc: Vlastimil Babka Cc: Shakeel Butt Cc: linux-mm@kvack.org Cc: Greg Kroah-Hartman Cc: Arve Hj=C3=B8nnev=C3=A5g Cc: Todd Kjos Cc: Christian Brauner Cc: Carlos Llamas Cc: Alice Ryhl Cc: "David S. Miller" Cc: David Ahern Cc: netdev@vger.kernel.org --- drivers/android/binder_alloc.c | 45 ++++++++++++++++------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.= c index e4488ad86a65..c13a588c37de 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -1142,7 +1142,6 @@ enum lru_status binder_alloc_free_page(struct list_he= ad *item, struct vm_area_struct *vma; struct page *page_to_free; unsigned long page_addr; - int mm_locked =3D 0; size_t index; =20 if (!mmget_not_zero(mm)) @@ -1151,27 +1150,25 @@ enum lru_status binder_alloc_free_page(struct list_= head *item, index =3D mdata->page_index; page_addr =3D alloc->vm_start + index * PAGE_SIZE; =20 - /* attempt per-vma lock first */ + /* + * Attempt per-vma lock. This is essentially a + * "trylock". It can fail even if the VMA exists + * for 'page_addr'. + */ vma =3D lock_vma_under_rcu(mm, page_addr); if (!vma) { - /* fall back to mmap_lock */ - if (!mmap_read_trylock(mm)) - goto err_mmap_read_lock_failed; - mm_locked =3D 1; - vma =3D vma_lookup(mm, page_addr); + /* + * If the vma exists, we can't continue because we cannot + * remove the page from the vma. However, if the vma was + * unmapped, it's okay to continue. + */ + if (binder_alloc_is_mapped(alloc)) + goto err_vma_lock_failed; } =20 if (!mutex_trylock(&alloc->mutex)) goto err_get_alloc_mutex_failed; =20 - /* - * Since a binder_alloc can only be mapped once, we ensure - * the vma corresponds to this mapping by checking whether - * the binder_alloc is still mapped. - */ - if (vma && !binder_alloc_is_mapped(alloc)) - goto err_invalid_vma; - trace_binder_unmap_kernel_start(alloc, index); =20 page_to_free =3D alloc->pages[index]; @@ -1182,7 +1179,12 @@ enum lru_status binder_alloc_free_page(struct list_h= ead *item, list_lru_isolate(lru, item); spin_unlock(&lru->lock); =20 - if (vma) { + /* + * Since a binder_alloc can only be mapped once, we ensure + * the vma corresponds to this mapping by checking whether + * the binder_alloc is still mapped. + */ + if (vma && binder_alloc_is_mapped(alloc)) { trace_binder_unmap_user_start(alloc, index); =20 zap_vma_range(vma, page_addr, PAGE_SIZE); @@ -1191,23 +1193,18 @@ enum lru_status binder_alloc_free_page(struct list_= head *item, } =20 mutex_unlock(&alloc->mutex); - if (mm_locked) - mmap_read_unlock(mm); - else + if (vma) vma_end_read(vma); mmput_async(mm); binder_free_page(page_to_free); =20 return LRU_REMOVED_RETRY; =20 -err_invalid_vma: mutex_unlock(&alloc->mutex); err_get_alloc_mutex_failed: - if (mm_locked) - mmap_read_unlock(mm); - else + if (vma) vma_end_read(vma); -err_mmap_read_lock_failed: +err_vma_lock_failed: mmput_async(mm); err_mmget: return LRU_SKIP; --=20 2.55.0.654.g21b8a5bc05-goog