From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F14B23101A7 for ; Fri, 8 May 2026 17:07:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778260026; cv=none; b=M6NtCI5SLC6Ou5qKq2FdyGVtuB52EjJh1EberIp31T5xH1pz05dqC54iWnbI4/h68XYASUgqBbUKFLn8f85nTX/zvuYXoOSO1RFl9EXy2WmrTnpkROM4TtzDNBWdvSeqzlcuokVKJkr5iUsT/I6UZqjYABYhlbdsxzohuoOI+IM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778260026; c=relaxed/simple; bh=nZlKOZi3D2FxQLGXjJnBU0HjVLTgkka2CeNKlpurw+s=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=iK3d5TuNHuEDzegl/6wVup5GracemdvgU+J8mEmgUCIWKVuH0BC2UfXPjIFSfm066CkQbh5wHQlzGaMaRZE9oY/5FQtfG/t0x3mxs1ac1gj6TSY5r758Y9Tpu/I2aFh6jv0dvrW3CI40D+9p6imcntojAMS07sc9+k8irDiqkGQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DvvE73lf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DvvE73lf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 72CC8C2BCC7; Fri, 8 May 2026 17:07:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778260025; bh=nZlKOZi3D2FxQLGXjJnBU0HjVLTgkka2CeNKlpurw+s=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=DvvE73lfByXNeN2jj9Xu6I68/hlJU8ru6ieTVyIwZmUi7KV1Cb04k6R23SnOoE9M6 2fm28DrRwlxKuBy0/OqOAIdrEjNPrzaMhNBdGqk8F/aRE0sk86TnTCrEVb56lyUjrK MBrMyHXIeD6xOgdQl5EYtj271+Q8hwqyLxRrwxw9d2FIjC8/iWfRfEpgOxXCBs7EXt k0z+LH4H7ilj2vDGBzNVIDKbVI4GCOy67wz2eWSFP25jAEKKyY4Spvz5OnMgUDETaS Nh1sAO6782AqIaflWmEcaNCb9VW+K5ioS8xsybEi6d6G4ctPDtotlBGc8I7lBJ2bSq 1YX9Qc+ZOUjyw== Date: Fri, 8 May 2026 18:06:59 +0100 From: Lorenzo Stoakes To: Dave Hansen Cc: linux-kernel@vger.kernel.org, Andrew Morton , "Liam R. Howlett" , linux-mm@kvack.org, Shakeel Butt , Suren Baghdasaryan , Vlastimil Babka Subject: Re: [PATCH 2/6] binder: Make shrinker rely solely on per-VMA lock Message-ID: References: <20260429181954.F50224AE@davehans-spike.ostc.intel.com> <20260429181957.7511C256@davehans-spike.ostc.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260429181957.7511C256@davehans-spike.ostc.intel.com> On Wed, Apr 29, 2026 at 11:19:57AM -0700, Dave Hansen wrote: > > 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: > > == Background == > > 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 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(). > > == Problem == > > 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. > > == Solution == > > Now that per-VMA locks are universally available, lock_vma_under_rcu() > will not persistently fail. Rely on it alone and simplify the code. > > 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 I mean this seems reasonable to me, I don't really understand why we'd fall back to mmap... try lock :) If semantically you're trylocking then as you say, lock_vma_under_rcu() already does that. Honestly I feel this could be submitted separate from the series. I'm not a binder guy, but this looks right to me so: Acked-by: Lorenzo Stoakes with nit below addressed. > Cc: Suren Baghdasaryan > Cc: Andrew Morton > Cc: "Liam R. Howlett" > Cc: Lorenzo Stoakes > Cc: Vlastimil Babka > Cc: Shakeel Butt > Cc: linux-mm@kvack.org > --- > > b/drivers/android/binder_alloc.c | 22 +++++----------------- > 1 file changed, 5 insertions(+), 17 deletions(-) > > diff -puN drivers/android/binder_alloc.c~binder-try-vma-lock drivers/android/binder_alloc.c > --- a/drivers/android/binder_alloc.c~binder-try-vma-lock 2026-04-29 11:18:50.066607065 -0700 > +++ b/drivers/android/binder_alloc.c 2026-04-29 11:18:50.069607180 -0700 > @@ -1142,7 +1142,6 @@ enum lru_status binder_alloc_free_page(s > struct vm_area_struct *vma; > struct page *page_to_free; > unsigned long page_addr; > - int mm_locked = 0; Man why are we using int instead of a bool in 2026 in the first place :P > size_t index; > > if (!mmget_not_zero(mm)) > @@ -1151,15 +1150,10 @@ enum lru_status binder_alloc_free_page(s > index = mdata->page_index; > page_addr = alloc->vm_start + index * PAGE_SIZE; > > - /* attempt per-vma lock first */ > + /* attempt per-vma lock */ > vma = 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 = 1; > - vma = vma_lookup(mm, page_addr); > - } > + if (!vma) > + goto err_mmap_read_lock_failed; Nit, but we probably want to rename that to err_vma_lock_failed or something! > > if (!mutex_trylock(&alloc->mutex)) > goto err_get_alloc_mutex_failed; > @@ -1191,10 +1185,7 @@ enum lru_status binder_alloc_free_page(s > } > > mutex_unlock(&alloc->mutex); > - if (mm_locked) > - mmap_read_unlock(mm); > - else > - vma_end_read(vma); > + vma_end_read(vma); > mmput_async(mm); > binder_free_page(page_to_free); > > @@ -1203,10 +1194,7 @@ enum lru_status binder_alloc_free_page(s > err_invalid_vma: > mutex_unlock(&alloc->mutex); > err_get_alloc_mutex_failed: > - if (mm_locked) > - mmap_read_unlock(mm); > - else > - vma_end_read(vma); > + vma_end_read(vma); > err_mmap_read_lock_failed: > mmput_async(mm); > err_mmget: > _