public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Carlos Llamas <cmllamas@google.com>
To: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Arve Hjønnevåg" <arve@android.com>,
	"Todd Kjos" <tkjos@android.com>,
	"Martijn Coenen" <maco@android.com>,
	"Joel Fernandes" <joel@joelfernandes.org>,
	"Christian Brauner" <brauner@kernel.org>,
	"Carlos Llamas" <cmllamas@google.com>,
	"Suren Baghdasaryan" <surenb@google.com>
Cc: linux-kernel@vger.kernel.org, kernel-team@android.com,
	 "Liam R. Howlett" <Liam.Howlett@oracle.com>
Subject: [PATCH v5 9/9] binder: use per-vma lock in page reclaiming
Date: Tue, 26 Nov 2024 18:40:12 +0000	[thread overview]
Message-ID: <20241126184021.45292-10-cmllamas@google.com> (raw)
In-Reply-To: <20241126184021.45292-1-cmllamas@google.com>

Use per-vma locking in the shrinker's callback when reclaiming pages,
similar to the page installation logic. This minimizes contention with
unrelated vmas improving performance. The mmap_sem is still acquired if
the per-vma lock cannot be obtained.

Cc: Suren Baghdasaryan <surenb@google.com>
Suggested-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Reviewed-by: Suren Baghdasaryan <surenb@google.com>
Signed-off-by: Carlos Llamas <cmllamas@google.com>
---
 drivers/android/binder_alloc.c | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c
index 339db88c1522..8c10c1a6f459 100644
--- a/drivers/android/binder_alloc.c
+++ b/drivers/android/binder_alloc.c
@@ -1128,19 +1128,28 @@ enum lru_status binder_alloc_free_page(struct list_head *item,
 	struct mm_struct *mm = alloc->mm;
 	struct vm_area_struct *vma;
 	unsigned long page_addr;
+	int mm_locked = 0;
 	size_t index;
 
 	if (!mmget_not_zero(mm))
 		goto err_mmget;
-	if (!mmap_read_trylock(mm))
-		goto err_mmap_read_lock_failed;
-	if (!mutex_trylock(&alloc->mutex))
-		goto err_get_alloc_mutex_failed;
 
 	index = page->index;
 	page_addr = alloc->vm_start + index * PAGE_SIZE;
 
-	vma = vma_lookup(mm, page_addr);
+	/* attempt per-vma lock first */
+	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 (!mutex_trylock(&alloc->mutex))
+		goto err_get_alloc_mutex_failed;
+
 	/* ensure the vma corresponds to the binder mapping */
 	if (vma && !binder_alloc_is_mapped(alloc))
 		goto err_invalid_vma;
@@ -1163,7 +1172,10 @@ enum lru_status binder_alloc_free_page(struct list_head *item,
 	}
 
 	mutex_unlock(&alloc->mutex);
-	mmap_read_unlock(mm);
+	if (mm_locked)
+		mmap_read_unlock(mm);
+	else
+		vma_end_read(vma);
 	mmput_async(mm);
 	__free_page(page);
 
@@ -1172,7 +1184,10 @@ enum lru_status binder_alloc_free_page(struct list_head *item,
 err_invalid_vma:
 	mutex_unlock(&alloc->mutex);
 err_get_alloc_mutex_failed:
-	mmap_read_unlock(mm);
+	if (mm_locked)
+		mmap_read_unlock(mm);
+	else
+		vma_end_read(vma);
 err_mmap_read_lock_failed:
 	mmput_async(mm);
 err_mmget:
-- 
2.47.0.338.g60cca15819-goog


  parent reply	other threads:[~2024-11-26 18:40 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-26 18:40 [PATCH v5 0/9] binder: faster page installations Carlos Llamas
2024-11-26 18:40 ` [PATCH v5 1/9] Revert "binder: switch alloc->mutex to spinlock_t" Carlos Llamas
2024-11-26 18:40 ` [PATCH v5 2/9] binder: concurrent page installation Carlos Llamas
2024-11-26 18:40 ` [PATCH v5 3/9] binder: select correct nid for pages in LRU Carlos Llamas
2024-11-26 18:40 ` [PATCH v5 4/9] binder: remove struct binder_lru_page Carlos Llamas
2024-11-26 19:46   ` Matthew Wilcox
2024-11-26 22:38     ` Carlos Llamas
2024-11-26 18:40 ` [PATCH v5 5/9] binder: replace alloc->vma with alloc->mapped Carlos Llamas
2024-11-26 18:40 ` [PATCH v5 6/9] binder: rename alloc->buffer to vm_start Carlos Llamas
2024-11-26 18:40 ` [PATCH v5 7/9] binder: use per-vma lock in page installation Carlos Llamas
2024-11-26 18:40 ` [PATCH v5 8/9] binder: propagate vm_insert_page() errors Carlos Llamas
2024-11-26 18:40 ` Carlos Llamas [this message]
2024-11-26 18:45   ` [PATCH v5 9/9] binder: use per-vma lock in page reclaiming Suren Baghdasaryan
2024-11-26 18:46     ` Suren Baghdasaryan
2024-11-26 19:11       ` Carlos Llamas
2024-11-26 20:05         ` Suren Baghdasaryan
2024-11-26 23:32           ` Carlos Llamas

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=20241126184021.45292-10-cmllamas@google.com \
    --to=cmllamas@google.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=arve@android.com \
    --cc=brauner@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=joel@joelfernandes.org \
    --cc=kernel-team@android.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maco@android.com \
    --cc=surenb@google.com \
    --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