From nobody Sun Nov 24 11:43:27 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 0A3852141BB for ; Tue, 5 Nov 2024 20:03:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730836989; cv=none; b=sAeffVLdKlUz0RDFf/uWiCo1mVvur4K4RVSfGoqA8P2ZUBrvBTF9o7cieej10on1BcswE8w+XaIJa09c9y/+OF3VrqajTcg0zMiljZQkx96H7l12aOHVcsisFdxWxbZ48uEdm9I5Os0cZYKTn0Z+zGk8W8tx6BMnyObggO4EM9U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730836989; c=relaxed/simple; bh=88wq84S5d5ZZylLA/CwWmKpip9hM35aPvuKn6ree2C4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rZY7amyOAe5Wal0nV9WpevHD/K8ymN6omW64FFWyGFgWf4ZiF2aQA6oDugl7NVykR2n2a3ZpLgyMINktFAuvNUw2wisFafFMRmf1PDEFKkEyFg8igLOpQsLIn3eMhKqZjmFWHcdkCAC1BhDVlW9pm+AIk84NIJ1nwCFlhTOWCTc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--cmllamas.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=oSB0csZu; arc=none smtp.client-ip=209.85.128.201 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--cmllamas.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oSB0csZu" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e1fbe2a6b1so110049437b3.2 for ; Tue, 05 Nov 2024 12:03:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730836987; x=1731441787; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=pWKTVNEJbjLhQRSA7l18YBajLooEVjsYG+y5xx907h4=; b=oSB0csZuTDRh1AUpSmmH63wYJSKK4w9YQ6MtOlBlScs7O6hVFRdT0xC4PW1tDpPRfc Vm3vIoEvVq3W+vOGlDT7yQthB0wUIR1MgI7tys/7ipbfSq7HAGf27DmaeDNjbXrR+5qo GlqbFxasXIoj5SxfIgKXbFyYwknTMnCKmprac4JmivcaiH0VPd81YX29is60O5MDZ5lQ bTfc2dcY2PgArI1bjdSbQMay7ELY7mAsrAQA/p44fgdeuwJanCG6f5qwxFDNy7nlUIJp 2mDcx0pqY+IBdILjkseKT3mCGZ0KerT1aEkqoCRgjJiR/ejccgcTuQ4XuhTpP/tqw+g/ 1wTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730836987; x=1731441787; h=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; bh=pWKTVNEJbjLhQRSA7l18YBajLooEVjsYG+y5xx907h4=; b=I35cty0wJvyhNJCIzQe6yx/EOGjVf316ew3AoMUl3xn00S/0BoSHz14JI5ilPng10f rFkvaugz11mBSN6HfdGIuJbfwXusMGDrpRjhs92HnENLyITJ3nYKCQRZ7ql+BF1gZ6Se DQRucWQ8GNS8VXR6UOfw2eaUUSBX55PVe5bbzZTGEyGBsIWwiDVcVe4qTkVQngeV9hTc KMyM65HinXCUsO10m1ANbCNTmolwgyjmh8WlaW80ukClB4kqXlQ3SRRwyrsdRcMah0Dl qaMt3phD9mGos2oU/mqQPJzZ6AAM4CPLWYFW3B7UOAJFJZTHEstK/NbuIRCCDdR8hKgK zyvQ== X-Gm-Message-State: AOJu0Yzjgne/Q3k4uMJM+5HtijKLBKv55FkKa9H+fABib2bdZZ/crAzb 2DMl0qUteyLw5TeeHtFWuipV09tAXermDbeHFzLfpxwEY+BSISpzpE3+hZq+l7EzRsEYagfEmsG cFUC77kL6cw== X-Google-Smtp-Source: AGHT+IFhggG0d1flHKdWJ10w0VOVYG3EtxwtTTGBLii1VGQzrkxPc1xhRs+/VMAk7EJp6fHLZTSWJG5EffMlrQ== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a05:690c:6913:b0:6e3:1627:e866 with SMTP id 00721157ae682-6e9d8b8da3fmr10462627b3.3.1730836987089; Tue, 05 Nov 2024 12:03:07 -0800 (PST) Date: Tue, 5 Nov 2024 20:02:43 +0000 In-Reply-To: <20241105200258.2380168-1-cmllamas@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241105200258.2380168-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.199.ga7371fff76-goog Message-ID: <20241105200258.2380168-2-cmllamas@google.com> Subject: [PATCH 1/8] Revert "binder: switch alloc->mutex to spinlock_t" From: Carlos Llamas To: Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, Mukesh Ojha Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This reverts commit 7710e2cca32e7f3958480e8bd44f50e29d0c2509. In preparation for concurrent page installations, restore the original alloc->mutex which will serialize zap_page_range_single() against page installations in subsequent patches (instead of the mmap_sem). Cc: Mukesh Ojha [cmllamas: fix trivial conflict due to 2c10a20f5e84a] Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 46 +++++++++++++++++----------------- drivers/android/binder_alloc.h | 10 ++++---- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index b3acbc4174fb..7241bf4a3ff2 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -169,9 +169,9 @@ struct binder_buffer *binder_alloc_prepare_to_free(stru= ct binder_alloc *alloc, { struct binder_buffer *buffer; =20 - spin_lock(&alloc->lock); + mutex_lock(&alloc->mutex); buffer =3D binder_alloc_prepare_to_free_locked(alloc, user_ptr); - spin_unlock(&alloc->lock); + mutex_unlock(&alloc->mutex); return buffer; } =20 @@ -597,10 +597,10 @@ struct binder_buffer *binder_alloc_new_buf(struct bin= der_alloc *alloc, if (!next) return ERR_PTR(-ENOMEM); =20 - spin_lock(&alloc->lock); + mutex_lock(&alloc->mutex); buffer =3D binder_alloc_new_buf_locked(alloc, next, size, is_async); if (IS_ERR(buffer)) { - spin_unlock(&alloc->lock); + mutex_unlock(&alloc->mutex); goto out; } =20 @@ -608,7 +608,7 @@ struct binder_buffer *binder_alloc_new_buf(struct binde= r_alloc *alloc, buffer->offsets_size =3D offsets_size; buffer->extra_buffers_size =3D extra_buffers_size; buffer->pid =3D current->tgid; - spin_unlock(&alloc->lock); + mutex_unlock(&alloc->mutex); =20 ret =3D binder_install_buffer_pages(alloc, buffer, size); if (ret) { @@ -785,17 +785,17 @@ void binder_alloc_free_buf(struct binder_alloc *alloc, * We could eliminate the call to binder_alloc_clear_buf() * from binder_alloc_deferred_release() by moving this to * binder_free_buf_locked(). However, that could - * increase contention for the alloc->lock if clear_on_free - * is used frequently for large buffers. This lock is not + * increase contention for the alloc mutex if clear_on_free + * is used frequently for large buffers. The mutex is not * needed for correctness here. */ if (buffer->clear_on_free) { binder_alloc_clear_buf(alloc, buffer); buffer->clear_on_free =3D false; } - spin_lock(&alloc->lock); + mutex_lock(&alloc->mutex); binder_free_buf_locked(alloc, buffer); - spin_unlock(&alloc->lock); + mutex_unlock(&alloc->mutex); } =20 /** @@ -893,7 +893,7 @@ void binder_alloc_deferred_release(struct binder_alloc = *alloc) struct binder_buffer *buffer; =20 buffers =3D 0; - spin_lock(&alloc->lock); + mutex_lock(&alloc->mutex); BUG_ON(alloc->vma); =20 while ((n =3D rb_first(&alloc->allocated_buffers))) { @@ -940,7 +940,7 @@ void binder_alloc_deferred_release(struct binder_alloc = *alloc) page_count++; } } - spin_unlock(&alloc->lock); + mutex_unlock(&alloc->mutex); kvfree(alloc->pages); if (alloc->mm) mmdrop(alloc->mm); @@ -964,7 +964,7 @@ void binder_alloc_print_allocated(struct seq_file *m, struct binder_buffer *buffer; struct rb_node *n; =20 - spin_lock(&alloc->lock); + mutex_lock(&alloc->mutex); for (n =3D rb_first(&alloc->allocated_buffers); n; n =3D rb_next(n)) { buffer =3D rb_entry(n, struct binder_buffer, rb_node); seq_printf(m, " buffer %d: %lx size %zd:%zd:%zd %s\n", @@ -974,7 +974,7 @@ void binder_alloc_print_allocated(struct seq_file *m, buffer->extra_buffers_size, buffer->transaction ? "active" : "delivered"); } - spin_unlock(&alloc->lock); + mutex_unlock(&alloc->mutex); } =20 /** @@ -991,7 +991,7 @@ void binder_alloc_print_pages(struct seq_file *m, int lru =3D 0; int free =3D 0; =20 - spin_lock(&alloc->lock); + mutex_lock(&alloc->mutex); /* * Make sure the binder_alloc is fully initialized, otherwise we might * read inconsistent state. @@ -1007,7 +1007,7 @@ void binder_alloc_print_pages(struct seq_file *m, lru++; } } - spin_unlock(&alloc->lock); + mutex_unlock(&alloc->mutex); seq_printf(m, " pages: %d:%d:%d\n", active, lru, free); seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high); } @@ -1023,10 +1023,10 @@ int binder_alloc_get_allocated_count(struct binder_= alloc *alloc) struct rb_node *n; int count =3D 0; =20 - spin_lock(&alloc->lock); + mutex_lock(&alloc->mutex); for (n =3D rb_first(&alloc->allocated_buffers); n !=3D NULL; n =3D rb_nex= t(n)) count++; - spin_unlock(&alloc->lock); + mutex_unlock(&alloc->mutex); return count; } =20 @@ -1071,8 +1071,8 @@ enum lru_status binder_alloc_free_page(struct list_he= ad *item, goto err_mmget; if (!mmap_read_trylock(mm)) goto err_mmap_read_lock_failed; - if (!spin_trylock(&alloc->lock)) - goto err_get_alloc_lock_failed; + if (!mutex_trylock(&alloc->mutex)) + goto err_get_alloc_mutex_failed; if (!page->page_ptr) goto err_page_already_freed; =20 @@ -1091,7 +1091,7 @@ enum lru_status binder_alloc_free_page(struct list_he= ad *item, trace_binder_unmap_kernel_end(alloc, index); =20 list_lru_isolate(lru, item); - spin_unlock(&alloc->lock); + mutex_unlock(&alloc->mutex); spin_unlock(lock); =20 if (vma) { @@ -1111,8 +1111,8 @@ enum lru_status binder_alloc_free_page(struct list_he= ad *item, =20 err_invalid_vma: err_page_already_freed: - spin_unlock(&alloc->lock); -err_get_alloc_lock_failed: + mutex_unlock(&alloc->mutex); +err_get_alloc_mutex_failed: mmap_read_unlock(mm); err_mmap_read_lock_failed: mmput_async(mm); @@ -1147,7 +1147,7 @@ void binder_alloc_init(struct binder_alloc *alloc) alloc->pid =3D current->group_leader->pid; alloc->mm =3D current->mm; mmgrab(alloc->mm); - spin_lock_init(&alloc->lock); + mutex_init(&alloc->mutex); INIT_LIST_HEAD(&alloc->buffers); } =20 diff --git a/drivers/android/binder_alloc.h b/drivers/android/binder_alloc.h index 70387234477e..a5181916942e 100644 --- a/drivers/android/binder_alloc.h +++ b/drivers/android/binder_alloc.h @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include #include #include @@ -72,7 +72,7 @@ struct binder_lru_page { =20 /** * struct binder_alloc - per-binder proc state for binder allocator - * @lock: protects binder_alloc fields + * @mutex: protects binder_alloc fields * @vma: vm_area_struct passed to mmap_handler * (invariant after mmap) * @mm: copy of task->mm (invariant after open) @@ -96,7 +96,7 @@ struct binder_lru_page { * struct binder_buffer objects used to track the user buffers */ struct binder_alloc { - spinlock_t lock; + struct mutex mutex; struct vm_area_struct *vma; struct mm_struct *mm; unsigned long buffer; @@ -153,9 +153,9 @@ binder_alloc_get_free_async_space(struct binder_alloc *= alloc) { size_t free_async_space; =20 - spin_lock(&alloc->lock); + mutex_lock(&alloc->mutex); free_async_space =3D alloc->free_async_space; - spin_unlock(&alloc->lock); + mutex_unlock(&alloc->mutex); return free_async_space; } =20 --=20 2.47.0.199.ga7371fff76-goog From nobody Sun Nov 24 11:43:27 2024 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 B0CBE215C5C for ; Tue, 5 Nov 2024 20:03:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730836992; cv=none; b=OhB/HONstVxB66egn1xvvqqL4pOMuUQiP+R7oxmZa3mzPgCztXGzJhkXL+GH1k5dfhZ7PLlmwMsA11GwNshl51Wtef7KjNU7FOS0GM02SDMWvbgP8mth5s5cD8JGH0uchElaGgXmvVcWUJ1Zm49HqjMdwhqncUYHFZ9w7HZ8ft8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730836992; c=relaxed/simple; bh=54tc+22skwqLZjWpHpMI8mWFZ/nSFK2fHaRDE1iPbOg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NiVB3mqGTcFfslftm6sh9UoStGMwebXx/jwvbZXrdMfe2otFyqf4v9sNhnToO4zKXtBQx7qTq+V6REQ+hQBrINbVPnlfcg+FDI7k76I0tzSBuTcKuYp+9QrJyDPaQeayt9rDdyqtm6GDiqHjz/xx/IL533zke4erqRPMYFcDg/I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--cmllamas.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hYmsrwkm; arc=none smtp.client-ip=209.85.210.201 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--cmllamas.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hYmsrwkm" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7203cdc239dso7021805b3a.3 for ; Tue, 05 Nov 2024 12:03:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730836990; x=1731441790; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=6s9N8bXLfq0vyRr/QJi4V+6LVydH0wqVse2bO6v6lY0=; b=hYmsrwkmDeyyRPxoS/XtnHzzoKONESdBfUQTGYuoqcwKk+NZzv/EoTVEPrcTZTnQfq jplgkxwVDwJN4C8OUkbLvK1t81SW2Nz5r5BgQTg0HYUf/vy7uNE2U4/zlI47/I3nXP/E Rl2q5qMAJqn4cG2Zu4jt5oxmysjPFvqWhi1ESG6hG8RvLcDPBoKyaKN3uyY/2Q9hmude owWEqkERCJbf3juQNTBeqewYu8F927lDfgEWlxdFdOqoCnNB68uDm1yxiC1a59JYTt+u PlLHxOmM58cqTIrSK41k+vh4y9z8+CvNucFPVFA07+IKT/gr8SfIVbOvIdTeOgXvARlY 9QJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730836990; x=1731441790; h=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; bh=6s9N8bXLfq0vyRr/QJi4V+6LVydH0wqVse2bO6v6lY0=; b=nc2wmAAtB2QkXKCCXluG3sXXo/wHjnCApoqgamswhG5eab7BYvIXYUhkHQ+COUwr8A HyRyGKoseqQMquFHPssWLmNwG4NXnjz9iUJjBkiNZKZ8pdgXv67rkFxmhZbfz/e2Q2ll Tavhi9BnTCPeLGAmYveM0oqMH3mPrJZLJx9Sync9ZZTAQrv4UQQvrbE9HmsWDszS4JuY WbAJT+FFG6VosLpTJiDsvW3WBHVq81i3TAFVBK4Vtb9zNcRbuNDmFlBFbpdG8kfN+Ztu Qkv32YUdRHK9z0wVnpWd8Ks0n5WMqHX4s0rfhJBq3+qwzDrOJy6lIEOAt+RfQadc1xkp akqw== X-Gm-Message-State: AOJu0YydkQtKGRQ9FwW3M/2uE806YyWY+zZSl/J6kljw+TFGicP2pdXy sghRLn/QhcfZLDW74QSnYvZZWfdZkxajAI+FF43LWMaQl+UarLo/yOln+c11Q9UZojaWiZ5FBUr 9c72facrxmA== X-Google-Smtp-Source: AGHT+IHxxsnAPQRqCZHVHc1NYflGFybcvrbqekBw3mHRrFi9WlmKTQ2rw3L87rykSMzBO+mu0pt8HHYIn/LbgQ== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a05:6a00:6616:b0:71e:4c4f:9d28 with SMTP id d2e1a72fcca58-720ab42fb21mr52389b3a.2.1730836989139; Tue, 05 Nov 2024 12:03:09 -0800 (PST) Date: Tue, 5 Nov 2024 20:02:44 +0000 In-Reply-To: <20241105200258.2380168-1-cmllamas@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241105200258.2380168-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.199.ga7371fff76-goog Message-ID: <20241105200258.2380168-3-cmllamas@google.com> Subject: [PATCH 2/8] binder: concurrent page installation From: Carlos Llamas To: Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, David Hildenbrand , Barry Song , "Liam R. Howlett" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allow multiple callers to install pages simultaneously by downgrading the mmap_sem to non-exclusive mode. Races to the same PTE are handled using folio_walk_start() to retrieve the already installed page. This method significantly reduces contention in the mmap semaphore. To ensure safety, vma_lookup() is used (instead of alloc->vma) to avoid operating on an isolated VMA. In addition, zap_page_range_single() is called under the alloc->mutex to avoid racing with the shrinker. Many thanks to Barry Song who posted a similar approach [1]. Link: https://lore.kernel.org/all/20240902225009.34576-1-21cnbao@gmail.com/= [1] Cc: David Hildenbrand Cc: Barry Song Cc: Suren Baghdasaryan Cc: Liam R. Howlett Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 64 ++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index 7241bf4a3ff2..0fc6048cbc72 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "binder_alloc.h" #include "binder_trace.h" =20 @@ -221,26 +222,14 @@ static int binder_install_single_page(struct binder_a= lloc *alloc, struct binder_lru_page *lru_page, unsigned long addr) { + struct vm_area_struct *vma; + struct folio_walk fw; struct page *page; int ret =3D 0; =20 if (!mmget_not_zero(alloc->mm)) return -ESRCH; =20 - /* - * Protected with mmap_sem in write mode as multiple tasks - * might race to install the same page. - */ - mmap_write_lock(alloc->mm); - if (binder_get_installed_page(lru_page)) - goto out; - - if (!alloc->vma) { - pr_err("%d: %s failed, no vma\n", alloc->pid, __func__); - ret =3D -ESRCH; - goto out; - } - page =3D alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO); if (!page) { pr_err("%d: failed to allocate page\n", alloc->pid); @@ -248,19 +237,50 @@ static int binder_install_single_page(struct binder_a= lloc *alloc, goto out; } =20 - ret =3D vm_insert_page(alloc->vma, addr, page); - if (ret) { + mmap_read_lock(alloc->mm); + vma =3D vma_lookup(alloc->mm, addr); + if (!vma || vma !=3D alloc->vma) { + pr_err("%d: %s failed, no vma\n", alloc->pid, __func__); + ret =3D -ESRCH; + goto unlock; + } + + ret =3D vm_insert_page(vma, addr, page); + switch (ret) { + case -EBUSY: + /* + * EBUSY is ok. Someone installed the pte first but the + * lru_page->page_ptr has not been updated yet. Discard + * our page and look up the one already installed. + */ + ret =3D 0; + __free_page(page); + if (!folio_walk_start(&fw, vma, addr, 0)) { + pr_err("%d: failed to find page at offset %lx\n", + alloc->pid, addr - alloc->buffer); + ret =3D -ESRCH; + break; + } + page =3D fw.page; + folio_walk_end(&fw, vma); + fallthrough; + case 0: + /* Mark page installation complete and safe to use */ + binder_set_installed_page(lru_page, page); + page =3D NULL; + break; + default: pr_err("%d: %s failed to insert page at offset %lx with %d\n", alloc->pid, __func__, addr - alloc->buffer, ret); - __free_page(page); ret =3D -ENOMEM; - goto out; + break; } =20 - /* Mark page installation complete and safe to use */ - binder_set_installed_page(lru_page, page); +unlock: + mmap_read_unlock(alloc->mm); + if (page) + __free_page(page); out: - mmap_write_unlock(alloc->mm); mmput_async(alloc->mm); return ret; } @@ -1091,7 +1111,6 @@ enum lru_status binder_alloc_free_page(struct list_he= ad *item, trace_binder_unmap_kernel_end(alloc, index); =20 list_lru_isolate(lru, item); - mutex_unlock(&alloc->mutex); spin_unlock(lock); =20 if (vma) { @@ -1102,6 +1121,7 @@ enum lru_status binder_alloc_free_page(struct list_he= ad *item, trace_binder_unmap_user_end(alloc, index); } =20 + mutex_unlock(&alloc->mutex); mmap_read_unlock(mm); mmput_async(mm); __free_page(page_to_free); --=20 2.47.0.199.ga7371fff76-goog From nobody Sun Nov 24 11:43:27 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 4777B215C79 for ; Tue, 5 Nov 2024 20:03:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730836994; cv=none; b=tLQRQXMKPEq3JD9jpCESJCGuSqajfPenUCAN29tiWX/NSlCWKxDr78FKoQ0r27IUj17QyfHa08Z0b0+lJIETkwkVapVIhyQjCDeOAEmGT0EIlpqUdrSU4dc/QhbKCF0abWWoiFo4jNaZ6vAJFC2vcyh2G1PlP7GhuYDNYOYemUg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730836994; c=relaxed/simple; bh=mPllwDesyBEdlBxrZ5g/Firgv1x5wRgJxsWO2xuBapk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WF97OEwXHpQb0iSq5V7jWBKtSZAeiSHoo8G59GlVrlGmnAPsFK6iB/J4FngNlms3413ksrSerOgEiHBBe1RC6JD+YIPBZyBzrWG5G8YuIHdQGxml0U9ZX9J9P5YeBFtZ7tcPx3cVDVCKxJH8KeG80CqwdPQxEguzZyYETu3r3s0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--cmllamas.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=JI1CUy4l; arc=none smtp.client-ip=209.85.219.202 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--cmllamas.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="JI1CUy4l" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e28fc8902e6so10788065276.0 for ; Tue, 05 Nov 2024 12:03:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730836992; x=1731441792; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BzW62C5MAhdQp+8Al+uImyA19s54Ggsxbj0WN64RFYo=; b=JI1CUy4lyleY8c0uGZ6hCSF3mHyGs01PmlvtmEeHWEOg9JRpvb9QTTW6mvTEdFRFQt /3QPo9qnXSdelogaMLcjKX6JLKg3FG8xQCnZv9CHTilldCg7NMogsgKCEOemEGxBbTxz h2yKOX+gGglSV9O/S7lyvLDcmPe0su7OoIXodBx+dnjdm0JVeo4BovfkqBGZgxJO6eNQ MUcQStcueLUCq8XnlOQvnrsLaR8qt+6cyfluKYrI1xC98F1FiLCOigv46ioc4nvPyJ/d 3twPlatGQwEVRPYgfTpjvdVUfdSzWzb75I1OjDAK28mE4K+rSyCu2j1H4dSgDMihn+VK XwxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730836992; x=1731441792; h=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; bh=BzW62C5MAhdQp+8Al+uImyA19s54Ggsxbj0WN64RFYo=; b=N0O/r4MPkJ8geN31NbCIdfmUswLE6BXf8/OGuhd93WfqqlRGK1v818oIRNhYDInd31 bve0N8O9zeDANoUM7FMV2SAfxJIWAt+g0RqgHtYyNXdiGiaJkkDiGc3kjDIofBtaZg7L FBP52fg6wPmeP0vme7zm83u6doTv0ttETx3wHb2EkUTuRyzVNNdz/798+lc5Xk8ZzPST RvlLJ49R20Cv3izMceKLhEvM9E4h53oILn5lTDtMpRNUxo3plHRlRPCxONQk317IOAYv oQtEeJ2AX23LIq1LGcGkm//ij++/pRRECCcsVMPzbnSuSAXLjblCUEyG6/t0pKh/ix/1 QSnQ== X-Gm-Message-State: AOJu0YzzIv6Mky6CJz+32zat9cEaPgi0ot7KKcLvOmQ5JP060lND1qlB lqf0fNPZa0lHSB5YyJzEMaQziJpjoyfjiCHODFLwoMY5Fk68IgK9h5IruThA1V0cRTZp8Z8Gdsf S/WguF04QaA== X-Google-Smtp-Source: AGHT+IEu3UHssFcaZjCcLxb4vYdTRLy2JjU9p8QLbpFd3bQuV8lLSVUIMqmz/Wg7sU1mC2FqDOmCMdU0eHP3MA== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a25:a305:0:b0:e11:639b:6428 with SMTP id 3f1490d57ef6-e3087722c24mr78035276.0.1730836992271; Tue, 05 Nov 2024 12:03:12 -0800 (PST) Date: Tue, 5 Nov 2024 20:02:45 +0000 In-Reply-To: <20241105200258.2380168-1-cmllamas@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241105200258.2380168-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.199.ga7371fff76-goog Message-ID: <20241105200258.2380168-4-cmllamas@google.com> Subject: [PATCH 3/8] binder: select correct nid for pages in LRU From: Carlos Llamas To: Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, Nhat Pham , Johannes Weiner Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The numa node id for binder pages is currently being derived from the lru entry under struct binder_lru_page. However, this object doesn't reflect the node id of the struct page items allocated separately. Instead, select the correct node id from the page itself. This was made possible since commit 0a97c01cd20b ("list_lru: allow explicit memcg and NUMA node selection"). Cc: Nhat Pham Cc: Johannes Weiner Cc: Suren Baghdasaryan Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index 0fc6048cbc72..87011be7d8b7 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -211,7 +211,10 @@ static void binder_lru_freelist_add(struct binder_allo= c *alloc, =20 trace_binder_free_lru_start(alloc, index); =20 - ret =3D list_lru_add_obj(&binder_freelist, &page->lru); + ret =3D list_lru_add(&binder_freelist, + &page->lru, + page_to_nid(page->page_ptr), + NULL); WARN_ON(!ret); =20 trace_binder_free_lru_end(alloc, index); @@ -337,7 +340,10 @@ static void binder_lru_freelist_del(struct binder_allo= c *alloc, if (page->page_ptr) { trace_binder_alloc_lru_start(alloc, index); =20 - on_lru =3D list_lru_del_obj(&binder_freelist, &page->lru); + on_lru =3D list_lru_del(&binder_freelist, + &page->lru, + page_to_nid(page->page_ptr), + NULL); WARN_ON(!on_lru); =20 trace_binder_alloc_lru_end(alloc, index); @@ -950,8 +956,10 @@ void binder_alloc_deferred_release(struct binder_alloc= *alloc) if (!alloc->pages[i].page_ptr) continue; =20 - on_lru =3D list_lru_del_obj(&binder_freelist, - &alloc->pages[i].lru); + on_lru =3D list_lru_del(&binder_freelist, + &alloc->pages[i].lru, + page_to_nid(alloc->pages[i].page_ptr), + NULL); binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, "%s: %d: page %d %s\n", __func__, alloc->pid, i, --=20 2.47.0.199.ga7371fff76-goog From nobody Sun Nov 24 11:43:27 2024 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 CBC95215F58 for ; Tue, 5 Nov 2024 20:03:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730836997; cv=none; b=FhKEr0RdHSUt/XYJU0nwKtx0OZ3HP8gr/zclKmoMfePT1ILGF18tPAWiOtGRE9uSMfPLV/nCFi0scRtVMvc9IltRmDBkaW+q+j33g1e4uJHaTGYkLbo3+gOTe564stx9lMidit7oRLDaZe1U/TnYN1GIHis4g5risHr2xTJH4Og= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730836997; c=relaxed/simple; bh=WaEk+3ckmsAXcx7hqvxbBJTwANFNujzpQv6UwLeqpaA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EqGi2vD5Er9uRveu8/jPxECYuMxLRqeobqUu/UmBtHogRLqUYIfsO4JfL61zs5rA9c2sTryxIznTuwZBibnvZGcQiUO2zFZK49ejsQ0Lae1Eyx6lf+biubuYrqCnYaUEn6rez4V1XvAuox5ynFQgkV6yrPoJvfsPxTpjsE47bCU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--cmllamas.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ITn8g//6; arc=none smtp.client-ip=209.85.215.202 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--cmllamas.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ITn8g//6" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-7d4dee4dfdcso5589609a12.2 for ; Tue, 05 Nov 2024 12:03:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730836995; x=1731441795; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=swNgjjE0ZLrgGShA+Kd7LNNDUbW+nWT8OeDDtuwNCAQ=; b=ITn8g//6wNkfCNUTex0bbeF02fn8jm/dM4ECRhp9MA1lyWDxev5+f1obyg2YW8gh86 7D8wS7JPZvWQUbobdsCXx0/YMxm0u7Ud1ZoNbEXZ2a+XEYtpBitDqTn+nKbS/2bG+s8w 4MyY5yiAXTQeoOLhVvdwsQrVgUKqAPA5YV92ttuRiCeRHWvu0PvihKpedSlP0jvrK4tb 5GHq7aWdOJ3I3Ic9tZQ3Njkp04GIOuQWn6ScRSrtBGwfzrGJ2Reknvpj3nzFFgdR0cIO g/rHju6/N0K9rKr+PIgpThiBeP/FhbvtdmOfN5F5mEDS5S+syg4ghyhdoBhirRsWYAmD rvBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730836995; x=1731441795; h=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; bh=swNgjjE0ZLrgGShA+Kd7LNNDUbW+nWT8OeDDtuwNCAQ=; b=m7/V2arV50O8UtMGOrEKDCb2ouK8xO7xV8Sm3nI+hYYMB1aLpG03NTkTX8SgqFNu9f uC/M1rJVA583Y/LzDwEaIQPXBqiASi41hp8549qTuqLP/1O5oyBrX5QkiR8oLvSe94jf OIoa0VRLE2VZgsvG2aU6hw1q2RZXdD/PsYfetimbkRN7rSQ7Dv9YBSj8tLR8kr5I1re3 eY7ZoWJTsEWyYPSzbbmu3SwsN3i4GprrhyZe1IEyZy5uhwKRj++WArVa6iXRGdMssxr+ dehenjATz86dljNO73gEqo2SMIUT+LK/5MMmenzAh4pkKieKiBAUsYu1StCCbBGu7iIr Oisw== X-Gm-Message-State: AOJu0YznWXrVcoj9ATnnP1Q+oaqfT69DLI6iXIohI4uUfTvbUDbPMfDL jD8RLlQsrep1EUENLIaWR/mTgM0wLFKzjfrJ373RE5LBhD0HH4auEZDTh6Lcv5+diZJDvSjGHbb IlOXoJPsPeA== X-Google-Smtp-Source: AGHT+IHTQNLyomE96dbPozAY01t5quiLGcy+qe47HB0rULHIbWbdwhfKJK8iZ7HdXiCz/h/MDd2RaOrlbGiPIA== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a63:34c1:0:b0:7ea:c554:d831 with SMTP id 41be03b00d2f7-7ee290c2030mr27795a12.7.1730836994957; Tue, 05 Nov 2024 12:03:14 -0800 (PST) Date: Tue, 5 Nov 2024 20:02:46 +0000 In-Reply-To: <20241105200258.2380168-1-cmllamas@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241105200258.2380168-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.199.ga7371fff76-goog Message-ID: <20241105200258.2380168-5-cmllamas@google.com> Subject: [PATCH 4/8] binder: remove struct binder_lru_page From: Carlos Llamas To: Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, Matthew Wilcox , "Liam R. Howlett" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Remove the redundant struct binder_lru_page concept. Instead, let's use available struct page->lru and page->private members directly to achieve the same functionality. This reduces the maximum memory allocated for alloc->pages from 32768 down to 8192 bytes (aarch64). Savings are per binder instance. Cc: Matthew Wilcox Cc: Liam R. Howlett Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 86 ++++++++++++------------- drivers/android/binder_alloc.h | 16 +---- drivers/android/binder_alloc_selftest.c | 14 ++-- 3 files changed, 49 insertions(+), 67 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index 87011be7d8b7..98606e1ba687 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -177,25 +177,26 @@ struct binder_buffer *binder_alloc_prepare_to_free(st= ruct binder_alloc *alloc, } =20 static inline void -binder_set_installed_page(struct binder_lru_page *lru_page, +binder_set_installed_page(struct binder_alloc *alloc, + unsigned long index, struct page *page) { /* Pairs with acquire in binder_get_installed_page() */ - smp_store_release(&lru_page->page_ptr, page); + smp_store_release(&alloc->pages[index], page); } =20 static inline struct page * -binder_get_installed_page(struct binder_lru_page *lru_page) +binder_get_installed_page(struct binder_alloc *alloc, unsigned long index) { /* Pairs with release in binder_set_installed_page() */ - return smp_load_acquire(&lru_page->page_ptr); + return smp_load_acquire(&alloc->pages[index]); } =20 static void binder_lru_freelist_add(struct binder_alloc *alloc, unsigned long start, unsigned long end) { - struct binder_lru_page *page; unsigned long page_addr; + struct page *page; =20 trace_binder_update_page_range(alloc, false, start, end); =20 @@ -204,16 +205,15 @@ static void binder_lru_freelist_add(struct binder_all= oc *alloc, int ret; =20 index =3D (page_addr - alloc->buffer) / PAGE_SIZE; - page =3D &alloc->pages[index]; - - if (!binder_get_installed_page(page)) + page =3D binder_get_installed_page(alloc, index); + if (!page) continue; =20 trace_binder_free_lru_start(alloc, index); =20 ret =3D list_lru_add(&binder_freelist, &page->lru, - page_to_nid(page->page_ptr), + page_to_nid(page), NULL); WARN_ON(!ret); =20 @@ -222,7 +222,7 @@ static void binder_lru_freelist_add(struct binder_alloc= *alloc, } =20 static int binder_install_single_page(struct binder_alloc *alloc, - struct binder_lru_page *lru_page, + unsigned long index, unsigned long addr) { struct vm_area_struct *vma; @@ -240,6 +240,10 @@ static int binder_install_single_page(struct binder_al= loc *alloc, goto out; } =20 + page->private =3D (unsigned long)alloc; + INIT_LIST_HEAD(&page->lru); + page->index =3D index; + mmap_read_lock(alloc->mm); vma =3D vma_lookup(alloc->mm, addr); if (!vma || vma !=3D alloc->vma) { @@ -253,7 +257,7 @@ static int binder_install_single_page(struct binder_all= oc *alloc, case -EBUSY: /* * EBUSY is ok. Someone installed the pte first but the - * lru_page->page_ptr has not been updated yet. Discard + * alloc->pages[index] has not been updated yet. Discard * our page and look up the one already installed. */ ret =3D 0; @@ -269,7 +273,7 @@ static int binder_install_single_page(struct binder_all= oc *alloc, fallthrough; case 0: /* Mark page installation complete and safe to use */ - binder_set_installed_page(lru_page, page); + binder_set_installed_page(alloc, index, page); page =3D NULL; break; default: @@ -292,7 +296,6 @@ static int binder_install_buffer_pages(struct binder_al= loc *alloc, struct binder_buffer *buffer, size_t size) { - struct binder_lru_page *page; unsigned long start, final; unsigned long page_addr; =20 @@ -304,14 +307,12 @@ static int binder_install_buffer_pages(struct binder_= alloc *alloc, int ret; =20 index =3D (page_addr - alloc->buffer) / PAGE_SIZE; - page =3D &alloc->pages[index]; - - if (binder_get_installed_page(page)) + if (binder_get_installed_page(alloc, index)) continue; =20 trace_binder_alloc_page_start(alloc, index); =20 - ret =3D binder_install_single_page(alloc, page, page_addr); + ret =3D binder_install_single_page(alloc, index, page_addr); if (ret) return ret; =20 @@ -325,8 +326,8 @@ static int binder_install_buffer_pages(struct binder_al= loc *alloc, static void binder_lru_freelist_del(struct binder_alloc *alloc, unsigned long start, unsigned long end) { - struct binder_lru_page *page; unsigned long page_addr; + struct page *page; =20 trace_binder_update_page_range(alloc, true, start, end); =20 @@ -335,14 +336,14 @@ static void binder_lru_freelist_del(struct binder_all= oc *alloc, bool on_lru; =20 index =3D (page_addr - alloc->buffer) / PAGE_SIZE; - page =3D &alloc->pages[index]; + page =3D binder_get_installed_page(alloc, index); =20 - if (page->page_ptr) { + if (page) { trace_binder_alloc_lru_start(alloc, index); =20 on_lru =3D list_lru_del(&binder_freelist, &page->lru, - page_to_nid(page->page_ptr), + page_to_nid(page), NULL); WARN_ON(!on_lru); =20 @@ -763,11 +764,10 @@ static struct page *binder_alloc_get_page(struct bind= er_alloc *alloc, (buffer->user_data - alloc->buffer); pgoff_t pgoff =3D buffer_space_offset & ~PAGE_MASK; size_t index =3D buffer_space_offset >> PAGE_SHIFT; - struct binder_lru_page *lru_page; =20 - lru_page =3D &alloc->pages[index]; *pgoffp =3D pgoff; - return lru_page->page_ptr; + + return binder_get_installed_page(alloc, index); } =20 /** @@ -842,7 +842,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *allo= c, { struct binder_buffer *buffer; const char *failure_string; - int ret, i; + int ret; =20 if (unlikely(vma->vm_mm !=3D alloc->mm)) { ret =3D -EINVAL; @@ -865,17 +865,12 @@ int binder_alloc_mmap_handler(struct binder_alloc *al= loc, alloc->pages =3D kvcalloc(alloc->buffer_size / PAGE_SIZE, sizeof(alloc->pages[0]), GFP_KERNEL); - if (alloc->pages =3D=3D NULL) { + if (!alloc->pages) { ret =3D -ENOMEM; failure_string =3D "alloc page array"; goto err_alloc_pages_failed; } =20 - for (i =3D 0; i < alloc->buffer_size / PAGE_SIZE; i++) { - alloc->pages[i].alloc =3D alloc; - INIT_LIST_HEAD(&alloc->pages[i].lru); - } - buffer =3D kzalloc(sizeof(*buffer), GFP_KERNEL); if (!buffer) { ret =3D -ENOMEM; @@ -951,20 +946,22 @@ void binder_alloc_deferred_release(struct binder_allo= c *alloc) int i; =20 for (i =3D 0; i < alloc->buffer_size / PAGE_SIZE; i++) { + struct page *page; bool on_lru; =20 - if (!alloc->pages[i].page_ptr) + page =3D binder_get_installed_page(alloc, i); + if (!page) continue; =20 on_lru =3D list_lru_del(&binder_freelist, - &alloc->pages[i].lru, - page_to_nid(alloc->pages[i].page_ptr), + &page->lru, + page_to_nid(page), NULL); binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, "%s: %d: page %d %s\n", __func__, alloc->pid, i, on_lru ? "on lru" : "active"); - __free_page(alloc->pages[i].page_ptr); + __free_page(page); page_count++; } } @@ -1013,7 +1010,7 @@ void binder_alloc_print_allocated(struct seq_file *m, void binder_alloc_print_pages(struct seq_file *m, struct binder_alloc *alloc) { - struct binder_lru_page *page; + struct page *page; int i; int active =3D 0; int lru =3D 0; @@ -1026,8 +1023,8 @@ void binder_alloc_print_pages(struct seq_file *m, */ if (binder_alloc_get_vma(alloc) !=3D NULL) { for (i =3D 0; i < alloc->buffer_size / PAGE_SIZE; i++) { - page =3D &alloc->pages[i]; - if (!page->page_ptr) + page =3D binder_get_installed_page(alloc, i); + if (!page) free++; else if (list_empty(&page->lru)) active++; @@ -1087,8 +1084,8 @@ enum lru_status binder_alloc_free_page(struct list_he= ad *item, void *cb_arg) __must_hold(lock) { - struct binder_lru_page *page =3D container_of(item, typeof(*page), lru); - struct binder_alloc *alloc =3D page->alloc; + struct page *page =3D container_of(item, typeof(*page), lru); + struct binder_alloc *alloc =3D (struct binder_alloc *)page->private; struct mm_struct *mm =3D alloc->mm; struct vm_area_struct *vma; struct page *page_to_free; @@ -1101,10 +1098,8 @@ enum lru_status binder_alloc_free_page(struct list_h= ead *item, goto err_mmap_read_lock_failed; if (!mutex_trylock(&alloc->mutex)) goto err_get_alloc_mutex_failed; - if (!page->page_ptr) - goto err_page_already_freed; =20 - index =3D page - alloc->pages; + index =3D page->index; page_addr =3D alloc->buffer + index * PAGE_SIZE; =20 vma =3D vma_lookup(mm, page_addr); @@ -1113,8 +1108,8 @@ enum lru_status binder_alloc_free_page(struct list_he= ad *item, =20 trace_binder_unmap_kernel_start(alloc, index); =20 - page_to_free =3D page->page_ptr; - page->page_ptr =3D NULL; + page_to_free =3D page; + binder_set_installed_page(alloc, index, NULL); =20 trace_binder_unmap_kernel_end(alloc, index); =20 @@ -1138,7 +1133,6 @@ enum lru_status binder_alloc_free_page(struct list_he= ad *item, return LRU_REMOVED_RETRY; =20 err_invalid_vma: -err_page_already_freed: mutex_unlock(&alloc->mutex); err_get_alloc_mutex_failed: mmap_read_unlock(mm); diff --git a/drivers/android/binder_alloc.h b/drivers/android/binder_alloc.h index a5181916942e..5c2473e95494 100644 --- a/drivers/android/binder_alloc.h +++ b/drivers/android/binder_alloc.h @@ -58,18 +58,6 @@ struct binder_buffer { int pid; }; =20 -/** - * struct binder_lru_page - page object used for binder shrinker - * @page_ptr: pointer to physical page in mmap'd space - * @lru: entry in binder_freelist - * @alloc: binder_alloc for a proc - */ -struct binder_lru_page { - struct list_head lru; - struct page *page_ptr; - struct binder_alloc *alloc; -}; - /** * struct binder_alloc - per-binder proc state for binder allocator * @mutex: protects binder_alloc fields @@ -83,7 +71,7 @@ struct binder_lru_page { * @allocated_buffers: rb tree of allocated buffers sorted by address * @free_async_space: VA space available for async buffers. This is * initialized at mmap time to 1/2 the full VA space - * @pages: array of binder_lru_page + * @pages: array of struct page * * @buffer_size: size of address space specified via mmap * @pid: pid for associated binder_proc (invariant after in= it) * @pages_high: high watermark of offset in @pages @@ -104,7 +92,7 @@ struct binder_alloc { struct rb_root free_buffers; struct rb_root allocated_buffers; size_t free_async_space; - struct binder_lru_page *pages; + struct page **pages; size_t buffer_size; int pid; size_t pages_high; diff --git a/drivers/android/binder_alloc_selftest.c b/drivers/android/bind= er_alloc_selftest.c index 81442fe20a69..c6941b9abad9 100644 --- a/drivers/android/binder_alloc_selftest.c +++ b/drivers/android/binder_alloc_selftest.c @@ -105,10 +105,10 @@ static bool check_buffer_pages_allocated(struct binde= r_alloc *alloc, page_addr =3D buffer->user_data; for (; page_addr < end; page_addr +=3D PAGE_SIZE) { page_index =3D (page_addr - alloc->buffer) / PAGE_SIZE; - if (!alloc->pages[page_index].page_ptr || - !list_empty(&alloc->pages[page_index].lru)) { + if (!alloc->pages[page_index] || + !list_empty(&alloc->pages[page_index]->lru)) { pr_err("expect alloc but is %s at page index %d\n", - alloc->pages[page_index].page_ptr ? + alloc->pages[page_index] ? "lru" : "free", page_index); return false; } @@ -148,10 +148,10 @@ static void binder_selftest_free_buf(struct binder_al= loc *alloc, * if binder shrinker ran during binder_alloc_free_buf * calls above. */ - if (list_empty(&alloc->pages[i].lru)) { + if (list_empty(&alloc->pages[i]->lru)) { pr_err_size_seq(sizes, seq); pr_err("expect lru but is %s at page index %d\n", - alloc->pages[i].page_ptr ? "alloc" : "free", i); + alloc->pages[i] ? "alloc" : "free", i); binder_selftest_failures++; } } @@ -168,9 +168,9 @@ static void binder_selftest_free_page(struct binder_all= oc *alloc) } =20 for (i =3D 0; i < (alloc->buffer_size / PAGE_SIZE); i++) { - if (alloc->pages[i].page_ptr) { + if (alloc->pages[i]) { pr_err("expect free but is %s at page index %d\n", - list_empty(&alloc->pages[i].lru) ? + list_empty(&alloc->pages[i]->lru) ? "alloc" : "lru", i); binder_selftest_failures++; } --=20 2.47.0.199.ga7371fff76-goog From nobody Sun Nov 24 11:43:27 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 99BB0215C79 for ; Tue, 5 Nov 2024 20:03:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730837000; cv=none; b=bZxoX1IWwO6YwdN8awP43xoy/LFTnxFDax2JFl5NCU+rFTiUGLS2ETmZzd5bGqA2UF490Jg0NADs6Qmyetwwn+PZi+smCYqRgpR6qScWHN3iR9FbJFOhdaZXQCscZB4TNG6WZC7cXgJQHNe8B5AkblQWQO0kgH2tfQZPrPhnl2E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730837000; c=relaxed/simple; bh=4WMT7DSvO5PrYb9aiyo1LhXcfWflQ5QnPXv5axEuwRE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ExCQfMRia2fQopMnCPrhiuw2n2DL6qnmuUejAzPmxK2YHh8XAdqIMlOCt+cGnL9HR9cnm701V1OY8bRMM1A5ndE1XvApFqG+QHhr6xC0DjzdZbkcN07ch2s7eYMyJXGI3oFaWBEyHZYkpnS0NSv1YgE2bU6kxve0S5apg+ndvck= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--cmllamas.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wN5B0Jsy; arc=none smtp.client-ip=209.85.128.201 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--cmllamas.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wN5B0Jsy" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ea8a5e86e9so56429627b3.2 for ; Tue, 05 Nov 2024 12:03:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730836998; x=1731441798; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=1T3eDuGh4my+88RGLbqVsPKjKaUxdvhrUVyHawglmE8=; b=wN5B0JsyJd/89Ekq3jtMlw/e8YhmZDG8Xb5VNJkLD0yey1phEaga4smGhdJXnrixbj JFuNtRyt3UrRxTr0rJfvXDLprAy20Nuzxx50mkZR+yDCabilIvY6cYyR68SHbEr2sAU4 uJLxZpZt3SGr//KkhpUlfWuASlEdGgOkzmtq4b52hYACO4Iq8C9YupWrdV5YMXdW9Wfe O6I6pLJXxafAtw+mCgPL+jt2WoKLyKtHa6BfCcW6B++UA+sR0BjKo3KGmstLwvv0H4n1 ZQVPKIFK6vA8JT+AGNL2DVVVxDlPccPt7F6ukk6XuFWpRe6hdmfWPx24tnBw18Qtaq4h +10A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730836998; x=1731441798; h=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; bh=1T3eDuGh4my+88RGLbqVsPKjKaUxdvhrUVyHawglmE8=; b=wNva/tXfSsqipXg1Olw44338o9zUY0e8Xy0L/IiWYnSegWHRS9XbUI+pFZC8iCXsny YWuVrKEzCWY+Mo9Sa4GEAwKwa7zgYDR4yDj43gfDsVQPTZXjPWQ1DmE/njDBuZoVmbZS hAdu8Bfk8OagdilVo1f296Z3KQEyXRhEPQ7xf0eq1HF17xDMrtSlf82xUJzA+tmf8oEu fEZ21OjsEE76fhnbMJIbRGPxCzFvtT6BRE5yfvW1IUJHdD2PoFIRXvewkiUnE1sWjmky wE2AGhv12zWSR2rav/LkHDEWwZ3B2lmsEDr+8Z2XcIPCUaUgye6q1IsJvR1RTpfCAU+Q k3yw== X-Gm-Message-State: AOJu0YxUaobUqRFKEk8VjsOUEoKvGwqMPbIo52fJ31Lb1Wa4FQwqLAcC 6J2Hx96NFrtVtWffNDLLmgLF64CZPr0W8lboHaOe+JS0rWS89ThUoRirTPES+6HgbOhw8m4Y+Vy zYJNay3b3uw== X-Google-Smtp-Source: AGHT+IGCzflDdiS3gDFpeTWinGiE+gvnujs86BE/S/Czz64hAXB8e6C7dx9LQIf+i/lULwr+YSBJ3IbO81zShw== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a05:690c:67ca:b0:6e3:d670:f62a with SMTP id 00721157ae682-6ea52518e31mr1333147b3.3.1730836997754; Tue, 05 Nov 2024 12:03:17 -0800 (PST) Date: Tue, 5 Nov 2024 20:02:47 +0000 In-Reply-To: <20241105200258.2380168-1-cmllamas@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241105200258.2380168-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.199.ga7371fff76-goog Message-ID: <20241105200258.2380168-6-cmllamas@google.com> Subject: [PATCH 5/8] binder: use alloc->mapped to save the vma state From: Carlos Llamas To: Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, Minchan Kim , "Liam R. Howlett" , Matthew Wilcox Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It is unsafe to use alloc->vma outside of the mmap_sem. Instead, add a new boolean alloc->mapped to save the vma state (mapped or unmmaped) and use this to validate several paths. This change is in preparation for removing the alloc->vma pointer in a subsequent patch. Cc: Minchan Kim Cc: Liam R. Howlett Cc: Matthew Wilcox Cc: Suren Baghdasaryan Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 29 ++++++++++++++++++------- drivers/android/binder_alloc.h | 2 ++ drivers/android/binder_alloc_selftest.c | 2 +- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index 98606e1ba687..aa299cbdab92 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -221,6 +221,19 @@ static void binder_lru_freelist_add(struct binder_allo= c *alloc, } } =20 +static inline +void binder_alloc_set_mapped(struct binder_alloc *alloc, bool state) +{ + /* pairs with smp_load_acquire in binder_alloc_is_mapped() */ + smp_store_release(&alloc->mapped, state); +} + +static inline bool binder_alloc_is_mapped(struct binder_alloc *alloc) +{ + /* pairs with smp_store_release in binder_alloc_set_mapped() */ + return smp_load_acquire(&alloc->mapped); +} + static int binder_install_single_page(struct binder_alloc *alloc, unsigned long index, unsigned long addr) @@ -246,7 +259,7 @@ static int binder_install_single_page(struct binder_all= oc *alloc, =20 mmap_read_lock(alloc->mm); vma =3D vma_lookup(alloc->mm, addr); - if (!vma || vma !=3D alloc->vma) { + if (!vma || !binder_alloc_is_mapped(alloc)) { pr_err("%d: %s failed, no vma\n", alloc->pid, __func__); ret =3D -ESRCH; goto unlock; @@ -603,7 +616,7 @@ struct binder_buffer *binder_alloc_new_buf(struct binde= r_alloc *alloc, int ret; =20 /* Check binder_alloc is fully initialized */ - if (!binder_alloc_get_vma(alloc)) { + if (!binder_alloc_is_mapped(alloc)) { binder_alloc_debug(BINDER_DEBUG_USER_ERROR, "%d: binder_alloc_buf, no vma\n", alloc->pid); @@ -885,7 +898,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *allo= c, alloc->free_async_space =3D alloc->buffer_size / 2; =20 /* Signal binder_alloc is fully initialized */ - binder_alloc_set_vma(alloc, vma); + binder_alloc_set_mapped(alloc, true); =20 return 0; =20 @@ -915,7 +928,7 @@ void binder_alloc_deferred_release(struct binder_alloc = *alloc) =20 buffers =3D 0; mutex_lock(&alloc->mutex); - BUG_ON(alloc->vma); + BUG_ON(alloc->mapped); =20 while ((n =3D rb_first(&alloc->allocated_buffers))) { buffer =3D rb_entry(n, struct binder_buffer, rb_node); @@ -1021,7 +1034,7 @@ void binder_alloc_print_pages(struct seq_file *m, * Make sure the binder_alloc is fully initialized, otherwise we might * read inconsistent state. */ - if (binder_alloc_get_vma(alloc) !=3D NULL) { + if (binder_alloc_is_mapped(alloc)) { for (i =3D 0; i < alloc->buffer_size / PAGE_SIZE; i++) { page =3D binder_get_installed_page(alloc, i); if (!page) @@ -1061,12 +1074,12 @@ int binder_alloc_get_allocated_count(struct binder_= alloc *alloc) * @alloc: binder_alloc for this proc * * Called from binder_vma_close() when releasing address space. - * Clears alloc->vma to prevent new incoming transactions from + * Clears alloc->mapped to prevent new incoming transactions from * allocating more buffers. */ void binder_alloc_vma_close(struct binder_alloc *alloc) { - binder_alloc_set_vma(alloc, NULL); + binder_alloc_set_mapped(alloc, false); } =20 /** @@ -1103,7 +1116,7 @@ enum lru_status binder_alloc_free_page(struct list_he= ad *item, page_addr =3D alloc->buffer + index * PAGE_SIZE; =20 vma =3D vma_lookup(mm, page_addr); - if (vma && vma !=3D binder_alloc_get_vma(alloc)) + if (vma && !binder_alloc_is_mapped(alloc)) goto err_invalid_vma; =20 trace_binder_unmap_kernel_start(alloc, index); diff --git a/drivers/android/binder_alloc.h b/drivers/android/binder_alloc.h index 5c2473e95494..89f6fa25c670 100644 --- a/drivers/android/binder_alloc.h +++ b/drivers/android/binder_alloc.h @@ -75,6 +75,7 @@ struct binder_buffer { * @buffer_size: size of address space specified via mmap * @pid: pid for associated binder_proc (invariant after in= it) * @pages_high: high watermark of offset in @pages + * @mapped: whether the vm area is mapped * @oneway_spam_detected: %true if oneway spam detection fired, clear that * flag once the async buffer has returned to a healthy state * @@ -96,6 +97,7 @@ struct binder_alloc { size_t buffer_size; int pid; size_t pages_high; + bool mapped; bool oneway_spam_detected; }; =20 diff --git a/drivers/android/binder_alloc_selftest.c b/drivers/android/bind= er_alloc_selftest.c index c6941b9abad9..2dda82d0d5e8 100644 --- a/drivers/android/binder_alloc_selftest.c +++ b/drivers/android/binder_alloc_selftest.c @@ -291,7 +291,7 @@ void binder_selftest_alloc(struct binder_alloc *alloc) if (!binder_selftest_run) return; mutex_lock(&binder_selftest_lock); - if (!binder_selftest_run || !alloc->vma) + if (!binder_selftest_run || !alloc->mapped) goto done; pr_info("STARTED\n"); binder_selftest_alloc_offset(alloc, end_offset, 0); --=20 2.47.0.199.ga7371fff76-goog From nobody Sun Nov 24 11:43:27 2024 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 B7205214429 for ; Tue, 5 Nov 2024 20:03:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730837002; cv=none; b=uLU8PVcemwCpyH5QOOX2VZL3aEtX/zkLeiMoUcVnCOppI9gmS44qmZlAyu9MMHYLuqH+l2Dp8PF0QZbk8r5ySjMlSVgC76vLwX/QZfNfjBjT84tuYo4nSyfgyWoMFVrJg7U/CCmkmWvRzrPCV3ffOM4eNE6Gv7isYXwg2Bqft38= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730837002; c=relaxed/simple; bh=ByO9EbBsdbo8f4wzbHz7U6PsYIGoUUKB+DY0NL86V48=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CIxHmS9iCHib+dkrqu5M2vtwqh6ZKVu9RR4XrLhwcEVb3bwg0SdnwLnLKoAz6x2pF23X4xEEDeEHhsK2il+FDgKiil/4tlNNK0qLDDXtpW/pErEB/qIeVUUbE/L9GKQqvv0HhA03ztJC4vny3TR8PvtsWrhOJfG9zERTJtAOJMc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--cmllamas.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=1e3uYa3B; arc=none smtp.client-ip=209.85.210.201 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--cmllamas.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1e3uYa3B" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-72065695191so6218311b3a.1 for ; Tue, 05 Nov 2024 12:03:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730837000; x=1731441800; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=q5v3+KfpfwdILNkg4koDdfGwqNcMD/SqajiCjPCWk4k=; b=1e3uYa3BXVaD+hHEWsXPOMgxdJ1JWyKV5bcrVxohpnAsWEW5fu+NE3iQiO3aA2fBdm wCg91WXAMSCylw2vynQM+HQHWoaZ/fiDyQcaUWiJISfhRdJUpyr9bukrM9jKpvVopuf5 k+EM6LRcd8BhwVm4vDtGN4OUoXwcReHnHowK/uclEqig9Rwa7KKyqN+0ig+NF3vGwmXH 3O/DIqAtDP4WSFb6yePrO7xnsW7b1Nvcncgt1PtMrMUzrEtRFv6cUeBI9La8wzjHyDGy eSKEcj0uF2tAurkx+B/P3duIVdckg2JQYSLPLIhSh67vTzZPRQ4QX/D+/jA1SbP6Pc/r gj4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730837000; x=1731441800; h=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; bh=q5v3+KfpfwdILNkg4koDdfGwqNcMD/SqajiCjPCWk4k=; b=GEd910PTGQ3wffbb4NUwIdT6fG4GnztxuZLJfS8C63gT4VpyljDZ4oMGoKW5vhq+Ja X2Q+aqCmuk0o9JFOmfAsnaZ6ftde6qlgv7WVuYvrBTUG7fZ14Sgi68Te3CjwYmZsBzsf eqAgAl2vOUzOVbcb8smjn/mbevrm2k0SYe2XFxDAELOZOZFAK1QlFLCgZEJgLtOCR1fG 5xdKLHAFL6ld4Ia+cWLaOv7W0IuCwf2eWCll7YX9qjZ06ONZtWmbu0x3O6udMsFhykXC YwYjLdzMWLcvVahl88vX3Gm+yhk0KxypLyCB5NoMWdEY6njm5tPc+z10QwO1wvQEfn/C l9XA== X-Gm-Message-State: AOJu0YzcLnoyasArJ5PiZdlZqhU02p+hZGh2871IEDlaTrNKlG4FbkQ1 HKKOETeATB8F4aSG1OAeUH2z1FD6Suj/n6RzsScFAnzAczyzlNLTpUImYU2niJOM/7yaPkUiLsv wuPqJPxdZJA== X-Google-Smtp-Source: AGHT+IHfFpQRhSgwzNc3hWaihNKT1tw5eG3qTrjyrfrYDChT1ZCVMccErN3lVms4lIZml4bMQXPrb0Cq8Vui4Q== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a05:6a00:8b87:b0:71e:59e7:cbe2 with SMTP id d2e1a72fcca58-72063078393mr53593b3a.5.1730837000021; Tue, 05 Nov 2024 12:03:20 -0800 (PST) Date: Tue, 5 Nov 2024 20:02:48 +0000 In-Reply-To: <20241105200258.2380168-1-cmllamas@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241105200258.2380168-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.199.ga7371fff76-goog Message-ID: <20241105200258.2380168-7-cmllamas@google.com> Subject: [PATCH 6/8] binder: remove cached alloc->vma pointer From: Carlos Llamas To: Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, Minchan Kim , "Liam R. Howlett" , Matthew Wilcox Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" All usage of the alloc->vma pointer has been removed from binder. Instead, paths has been refactored to use either vma_lookup() or the alloc->mapped state. Using the alloc->vma was unsafe and required the mmap_sem in exclusive mode, which caused several performance and security issues in the past. Cc: Minchan Kim Cc: Liam R. Howlett Cc: Matthew Wilcox Cc: Suren Baghdasaryan Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 14 -------------- drivers/android/binder_alloc.h | 3 --- 2 files changed, 17 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index aa299cbdab92..a4b7bf8696dd 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -369,20 +369,6 @@ static void binder_lru_freelist_del(struct binder_allo= c *alloc, } } =20 -static inline void binder_alloc_set_vma(struct binder_alloc *alloc, - struct vm_area_struct *vma) -{ - /* pairs with smp_load_acquire in binder_alloc_get_vma() */ - smp_store_release(&alloc->vma, vma); -} - -static inline struct vm_area_struct *binder_alloc_get_vma( - struct binder_alloc *alloc) -{ - /* pairs with smp_store_release in binder_alloc_set_vma() */ - return smp_load_acquire(&alloc->vma); -} - static void debug_no_space_locked(struct binder_alloc *alloc) { size_t largest_alloc_size =3D 0; diff --git a/drivers/android/binder_alloc.h b/drivers/android/binder_alloc.h index 89f6fa25c670..b9e2e9dc90b3 100644 --- a/drivers/android/binder_alloc.h +++ b/drivers/android/binder_alloc.h @@ -61,8 +61,6 @@ struct binder_buffer { /** * struct binder_alloc - per-binder proc state for binder allocator * @mutex: protects binder_alloc fields - * @vma: vm_area_struct passed to mmap_handler - * (invariant after mmap) * @mm: copy of task->mm (invariant after open) * @buffer: base of per-proc address space mapped via mmap * @buffers: list of all buffers for this proc @@ -86,7 +84,6 @@ struct binder_buffer { */ struct binder_alloc { struct mutex mutex; - struct vm_area_struct *vma; struct mm_struct *mm; unsigned long buffer; struct list_head buffers; --=20 2.47.0.199.ga7371fff76-goog From nobody Sun Nov 24 11:43:27 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 7C1E22161E9 for ; Tue, 5 Nov 2024 20:03:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730837006; cv=none; b=esJmpcWMfqQJP8dMlWXX64cj19u8W1brJvYjDXMV0tYoX+SGvIL9tpyg1J7Hd2AQygFOF25MEAf9JW/1P4ssUw7aIa7QZFPIdzKJ6LVPqgxNec+1TexfT/hJaPbpIQ4axdF+sQLJHotUbT2pqK3ei9sHl/tuNENJN4kyAV+b/hI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730837006; c=relaxed/simple; bh=Ckm7wpbU5i1/hU2K+FldMuHODb874ygQ8Wx0As/XBXk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XoWYnHcG8y5baW59WDAznu3WuFQS4IV/fsJt3EorX5aeQZLDXJV0s5CVhKT8aNTooX7mQDF3x/uRv9J6Yl/0B8cLkTimuTj1ffEHCqEOLP1tZXF4OpiXW21zD8j14irysxoznBaULMdLI5dl06rNDl1MEHMLc7KJ7ZUhONYqJF8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--cmllamas.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jBbwL592; arc=none smtp.client-ip=209.85.128.201 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--cmllamas.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jBbwL592" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ea86f1df79so67696187b3.1 for ; Tue, 05 Nov 2024 12:03:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730837003; x=1731441803; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=DJYcKIvsfCYef6ki+cx028Fm/41DbvehREHf3NCMgSY=; b=jBbwL592xyu8XiY9bSf5TvN8xPGamIp5ggVKibz2xEFrcRqUba8MINup4MCDR3Qp+i hC9DUPcS7jGP+qm+EfitI1ffkvmT/SeUtH11EEK3droVMfVuBXULKF51bMeBAkNRiyvY O0z6Mg1DJqPfPGhAiR5EXJ0SxmgIedX70lqRaYK6BzXlI9xvRATcYYu4HsEih2xJ4uv7 0O6wmPXPD6VbNLEwpyPYdovTtTdlvSfc1vTJo6uwC454h+10ZBg8oS/VhoGuV7RJ52re mSSBwHRtoCV0AwfHS9Ds4vIg/qW0aNGRv70ehy0ESETZTCNY5TUEI0gx7VZM/qGmsnzE L8QQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730837003; x=1731441803; h=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; bh=DJYcKIvsfCYef6ki+cx028Fm/41DbvehREHf3NCMgSY=; b=po574pe8Xyx4j/7NAEbK2DVg5hWcgVfvtKZpPruiKmAM9dtvX6H2RM/M+0cA/JGGhA ZLUk3EpysLsLAWpK8QKL1avr1Vdjtbk9yVALnZ8OvZ0hvvMCUJx0zEm4Cz7FTGgTdsTh wDfLd1K63LbJxoAF6OnqC0HhIjIGlPk8aU9CZcEqx2Tqg+Eu0Nx4lukWdLs2JKUu2dSS fIs6hDkQtG2MsCfZFOXcIRwLI2QoiHKV7Al9ZbcAue2rcoeBgHEibN2WMlGHxKTtCtwY IPhl67KFb+BESHzjiRn+xjGUcm/BkMvXB11/1+70WvnNWVyMrrIKxsfxmnwlqbpWxBBY avFA== X-Gm-Message-State: AOJu0YzrSj8r+BTq1oGmlVI/Mi2k29705WwcqvJuQwdAKL02PJC/tB7x QNAUPLcExTFb1iSWInZ8OhkgzIPzwgj4OVFHa9OH/TVHrCaT5izTAZWxBGtBzH4IW/7cSBVkR6l x8fyUg5T6qA== X-Google-Smtp-Source: AGHT+IHQdRHCXzW6Yt0gtN2+GE1UnKlQbluksQ6XKPwYVvzl3YWUeoqF3aEW8WHxh181L4g37oAmmw36FJpBHg== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a05:690c:4a02:b0:6e3:b08:92c7 with SMTP id 00721157ae682-6e9d87070a8mr9489887b3.0.1730837003421; Tue, 05 Nov 2024 12:03:23 -0800 (PST) Date: Tue, 5 Nov 2024 20:02:49 +0000 In-Reply-To: <20241105200258.2380168-1-cmllamas@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241105200258.2380168-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.199.ga7371fff76-goog Message-ID: <20241105200258.2380168-8-cmllamas@google.com> Subject: [PATCH 7/8] binder: rename alloc->buffer to vm_start From: Carlos Llamas To: Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan Cc: linux-kernel@vger.kernel.org, kernel-team@android.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The alloc->buffer field in struct binder_alloc stores the starting address of the mapped vma, rename this field to alloc->vm_start to better reflect its purpose. It also avoids confusion with the binder buffer concept, e.g. transaction->buffer. No functional changes in this patch. Signed-off-by: Carlos Llamas --- drivers/android/binder.c | 2 +- drivers/android/binder_alloc.c | 28 ++++++++++++------------- drivers/android/binder_alloc.h | 4 ++-- drivers/android/binder_alloc_selftest.c | 2 +- drivers/android/binder_trace.h | 2 +- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/drivers/android/binder.c b/drivers/android/binder.c index 978740537a1a..57265cabec43 100644 --- a/drivers/android/binder.c +++ b/drivers/android/binder.c @@ -6350,7 +6350,7 @@ static void print_binder_transaction_ilocked(struct s= eq_file *m, seq_printf(m, " node %d", buffer->target_node->debug_id); seq_printf(m, " size %zd:%zd offset %lx\n", buffer->data_size, buffer->offsets_size, - proc->alloc.buffer - buffer->user_data); + proc->alloc.vm_start - buffer->user_data); } =20 static void print_binder_work_ilocked(struct seq_file *m, diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index a4b7bf8696dd..d505ffddb6db 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -62,7 +62,7 @@ static size_t binder_alloc_buffer_size(struct binder_allo= c *alloc, struct binder_buffer *buffer) { if (list_is_last(&buffer->entry, &alloc->buffers)) - return alloc->buffer + alloc->buffer_size - buffer->user_data; + return alloc->vm_start + alloc->buffer_size - buffer->user_data; return binder_buffer_next(buffer)->user_data - buffer->user_data; } =20 @@ -204,7 +204,7 @@ static void binder_lru_freelist_add(struct binder_alloc= *alloc, size_t index; int ret; =20 - index =3D (page_addr - alloc->buffer) / PAGE_SIZE; + index =3D (page_addr - alloc->vm_start) / PAGE_SIZE; page =3D binder_get_installed_page(alloc, index); if (!page) continue; @@ -277,7 +277,7 @@ static int binder_install_single_page(struct binder_all= oc *alloc, __free_page(page); if (!folio_walk_start(&fw, vma, addr, 0)) { pr_err("%d: failed to find page at offset %lx\n", - alloc->pid, addr - alloc->buffer); + alloc->pid, addr - alloc->vm_start); ret =3D -ESRCH; break; } @@ -291,7 +291,7 @@ static int binder_install_single_page(struct binder_all= oc *alloc, break; default: pr_err("%d: %s failed to insert page at offset %lx with %d\n", - alloc->pid, __func__, addr - alloc->buffer, ret); + alloc->pid, __func__, addr - alloc->vm_start, ret); ret =3D -ENOMEM; break; } @@ -319,7 +319,7 @@ static int binder_install_buffer_pages(struct binder_al= loc *alloc, unsigned long index; int ret; =20 - index =3D (page_addr - alloc->buffer) / PAGE_SIZE; + index =3D (page_addr - alloc->vm_start) / PAGE_SIZE; if (binder_get_installed_page(alloc, index)) continue; =20 @@ -348,7 +348,7 @@ static void binder_lru_freelist_del(struct binder_alloc= *alloc, unsigned long index; bool on_lru; =20 - index =3D (page_addr - alloc->buffer) / PAGE_SIZE; + index =3D (page_addr - alloc->vm_start) / PAGE_SIZE; page =3D binder_get_installed_page(alloc, index); =20 if (page) { @@ -700,8 +700,8 @@ static void binder_free_buf_locked(struct binder_alloc = *alloc, BUG_ON(buffer->free); BUG_ON(size > buffer_size); BUG_ON(buffer->transaction !=3D NULL); - BUG_ON(buffer->user_data < alloc->buffer); - BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size); + BUG_ON(buffer->user_data < alloc->vm_start); + BUG_ON(buffer->user_data > alloc->vm_start + alloc->buffer_size); =20 if (buffer->async_transaction) { alloc->free_async_space +=3D buffer_size; @@ -760,7 +760,7 @@ static struct page *binder_alloc_get_page(struct binder= _alloc *alloc, pgoff_t *pgoffp) { binder_size_t buffer_space_offset =3D buffer_offset + - (buffer->user_data - alloc->buffer); + (buffer->user_data - alloc->vm_start); pgoff_t pgoff =3D buffer_space_offset & ~PAGE_MASK; size_t index =3D buffer_space_offset >> PAGE_SHIFT; =20 @@ -859,7 +859,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *allo= c, SZ_4M); mutex_unlock(&binder_alloc_mmap_lock); =20 - alloc->buffer =3D vma->vm_start; + alloc->vm_start =3D vma->vm_start; =20 alloc->pages =3D kvcalloc(alloc->buffer_size / PAGE_SIZE, sizeof(alloc->pages[0]), @@ -877,7 +877,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *allo= c, goto err_alloc_buf_struct_failed; } =20 - buffer->user_data =3D alloc->buffer; + buffer->user_data =3D alloc->vm_start; list_add(&buffer->entry, &alloc->buffers); buffer->free =3D 1; binder_insert_free_buffer(alloc, buffer); @@ -892,7 +892,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *allo= c, kvfree(alloc->pages); alloc->pages =3D NULL; err_alloc_pages_failed: - alloc->buffer =3D 0; + alloc->vm_start =3D 0; mutex_lock(&binder_alloc_mmap_lock); alloc->buffer_size =3D 0; err_already_mapped: @@ -993,7 +993,7 @@ void binder_alloc_print_allocated(struct seq_file *m, buffer =3D rb_entry(n, struct binder_buffer, rb_node); seq_printf(m, " buffer %d: %lx size %zd:%zd:%zd %s\n", buffer->debug_id, - buffer->user_data - alloc->buffer, + buffer->user_data - alloc->vm_start, buffer->data_size, buffer->offsets_size, buffer->extra_buffers_size, buffer->transaction ? "active" : "delivered"); @@ -1099,7 +1099,7 @@ enum lru_status binder_alloc_free_page(struct list_he= ad *item, goto err_get_alloc_mutex_failed; =20 index =3D page->index; - page_addr =3D alloc->buffer + index * PAGE_SIZE; + page_addr =3D alloc->vm_start + index * PAGE_SIZE; =20 vma =3D vma_lookup(mm, page_addr); if (vma && !binder_alloc_is_mapped(alloc)) diff --git a/drivers/android/binder_alloc.h b/drivers/android/binder_alloc.h index b9e2e9dc90b3..05a01d980f61 100644 --- a/drivers/android/binder_alloc.h +++ b/drivers/android/binder_alloc.h @@ -62,7 +62,7 @@ struct binder_buffer { * struct binder_alloc - per-binder proc state for binder allocator * @mutex: protects binder_alloc fields * @mm: copy of task->mm (invariant after open) - * @buffer: base of per-proc address space mapped via mmap + * @vm_start: base of per-proc address space mapped via mmap * @buffers: list of all buffers for this proc * @free_buffers: rb tree of buffers available for allocation * sorted by size @@ -85,7 +85,7 @@ struct binder_buffer { struct binder_alloc { struct mutex mutex; struct mm_struct *mm; - unsigned long buffer; + unsigned long vm_start; struct list_head buffers; struct rb_root free_buffers; struct rb_root allocated_buffers; diff --git a/drivers/android/binder_alloc_selftest.c b/drivers/android/bind= er_alloc_selftest.c index 2dda82d0d5e8..d2d086d2c037 100644 --- a/drivers/android/binder_alloc_selftest.c +++ b/drivers/android/binder_alloc_selftest.c @@ -104,7 +104,7 @@ static bool check_buffer_pages_allocated(struct binder_= alloc *alloc, end =3D PAGE_ALIGN(buffer->user_data + size); page_addr =3D buffer->user_data; for (; page_addr < end; page_addr +=3D PAGE_SIZE) { - page_index =3D (page_addr - alloc->buffer) / PAGE_SIZE; + page_index =3D (page_addr - alloc->vm_start) / PAGE_SIZE; if (!alloc->pages[page_index] || !list_empty(&alloc->pages[page_index]->lru)) { pr_err("expect alloc but is %s at page index %d\n", diff --git a/drivers/android/binder_trace.h b/drivers/android/binder_trace.h index fe38c6fc65d0..16de1b9e72f7 100644 --- a/drivers/android/binder_trace.h +++ b/drivers/android/binder_trace.h @@ -328,7 +328,7 @@ TRACE_EVENT(binder_update_page_range, TP_fast_assign( __entry->proc =3D alloc->pid; __entry->allocate =3D allocate; - __entry->offset =3D start - alloc->buffer; + __entry->offset =3D start - alloc->vm_start; __entry->size =3D end - start; ), TP_printk("proc=3D%d allocate=3D%d offset=3D%zu size=3D%zu", --=20 2.47.0.199.ga7371fff76-goog From nobody Sun Nov 24 11:43:27 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 EB1EC215C68 for ; Tue, 5 Nov 2024 20:03:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730837010; cv=none; b=o7alSlg+Vou0JR6AnQeFHPo3YYuUtIt0w62tHzb7GM1jPAse+rqN5wP5ciJKIchTxLDRiVaEbeee6Dyf9/0wmGpff8n56OpDHQpOQu76tnWsfFnuLNM/uQ/nA+WPmzbUXOIyuLDuvrj4BV86FomRCXstxthieyBXHS8Qo2QJIu8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730837010; c=relaxed/simple; bh=axu+SuCUcLr8WtJ/zdsr2jK+nOb0rk9rZ16hbDD7jtY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=clBhSBI6ctGm5RpdjmzSs94djp+bZvO9Lt5K9vXYDkyLEp0RFNnC/d+63pSP8Pd9SuHdqIzF44kc3rPw4XpxvTLeGckRHnYBpsmxYlxvr9ys0CT4+GLxf0O29Kw00igqtw+9s8UAF//0vZS0zYBpC8B6OHqtDaUwR8PqCtgiCvo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--cmllamas.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Va1gCUVo; arc=none smtp.client-ip=209.85.128.202 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--cmllamas.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Va1gCUVo" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6ea95f530fdso46704697b3.1 for ; Tue, 05 Nov 2024 12:03:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730837006; x=1731441806; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Yd7j9aSxGIpn4d13uPZM6xFte0p1814h13XmA29rNnY=; b=Va1gCUVoTrtZHSC0tM9i2MUsYewhGRH4vO9VVx5RZGjuugMH+eG3F9GSpwtNFgu/UP DeUfv1F+6OpJqJ1Xnk0vzkfVSIwe/LgXea/vDgyz/P4wD2kTv5PMkCaKz+sIi+Wll/wc 9CWJpeu0w5erJ+bZNFY8f9rPNm8NnSffWaGSdFT+NzStPGjpC9zj7gVV/w/lcxIecBA3 oPpB4FVluVG2vRGW+OEU3/5X9p0nvLT5LhXCEgEHNgi9S9Iks2CdRm+ctyTll55U1hEI yY3xDYZV/WIfI/UPOwpgm9G2aTQAUVmIAt9c1WmgMx2iVsSEQLRkW+4y+wGfd6rVBHzj qJJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730837006; x=1731441806; h=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; bh=Yd7j9aSxGIpn4d13uPZM6xFte0p1814h13XmA29rNnY=; b=vumSPNHpxocjLcpY4GXxuYLJYtLsFKHJOta3UNSt/ybA0d4nRZKabFzhadJRw4OfYt rYSsvvNt9A20awfda5pgmPicRjz2pI/rSsTccfYnOca22pSatcDBY+l1XZzStMIsqNSW TxborTHGivuGJYyIA79TaMSZI/kiBUM9x/zA0JN8cUTRIQToQyqcn98CSdOIvCgZLCRi GpTcvRBmRjcJ6RnHWoBitpGLSz3C3kSNZKbkN54seLYnaq6CSjQZR8YgR+0+Wdw7d1RQ fdgFzvy7amZIabrHFHD1Hf5mZS52AJbSK9ZhA/wOsYkgKNWXScM+YRXAwzWj2wFWyUNE 7yVQ== X-Gm-Message-State: AOJu0Yy7jdV7SFlMzOQuBCR2gu3YED3ljmtYHALrnpClOllu7FF69X06 V39UN9trhf9HNCwInEebITe0uREWXibM+JsMX1fs2ASlcC0wf+K+HXGrWV0KAhhp0FYfVCClzPP XVH9WcUSfkw== X-Google-Smtp-Source: AGHT+IH0YVtb6fbOYKhMlgKfSIaLFwui3tGnWQHJ30WqiKR1QVTWDBUeeuoYelFC3rgUjGvVDQNzQTQodFhefg== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a25:e04e:0:b0:e30:cd7e:c0b5 with SMTP id 3f1490d57ef6-e30cd7ec150mr44863276.3.1730837005801; Tue, 05 Nov 2024 12:03:25 -0800 (PST) Date: Tue, 5 Nov 2024 20:02:50 +0000 In-Reply-To: <20241105200258.2380168-1-cmllamas@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241105200258.2380168-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.199.ga7371fff76-goog Message-ID: <20241105200258.2380168-9-cmllamas@google.com> Subject: [PATCH 8/8] binder: use per-vma lock in page installation From: Carlos Llamas To: Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, Nhat Pham , Johannes Weiner , Barry Song Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use per-vma locking for concurrent page installations, this minimizes contention with unrelated vmas improving performance. The mmap_lock is still acquired when needed though, e.g. before folio_walk_start(). Many thanks to Barry Song who posted a similar approach [1]. Link: https://lore.kernel.org/all/20240902225009.34576-1-21cnbao@gmail.com/= [1] Cc: Nhat Pham Cc: Johannes Weiner Cc: Barry Song Cc: Suren Baghdasaryan Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 70 +++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 10 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index d505ffddb6db..47cf1c354e6f 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -234,13 +234,63 @@ static inline bool binder_alloc_is_mapped(struct bind= er_alloc *alloc) return smp_load_acquire(&alloc->mapped); } =20 +static struct vm_area_struct *binder_find_lock_vma(struct binder_alloc *al= loc, + unsigned long addr, + bool *mm_locked) +{ + struct mm_struct *mm =3D alloc->mm; + struct vm_area_struct *vma; + + /* attempt per-vma lock first */ + vma =3D lock_vma_under_rcu(mm, addr); + if (vma) { + *mm_locked =3D false; + return vma; + } + + /* fallback to mmap_lock */ + mmap_read_lock(mm); + vma =3D vma_lookup(mm, addr); + if (!vma || !binder_alloc_is_mapped(alloc)) { + mmap_read_unlock(mm); + return NULL; + } + *mm_locked =3D true; + + return vma; +} + +static struct page *binder_page_lookup(struct mm_struct *mm, + struct vm_area_struct *vma, + unsigned long addr, + bool mm_locked) +{ + struct folio_walk fw; + struct page *page; + + /* folio_walk_start() requires the mmap_lock */ + if (!mm_locked) + mmap_read_lock(mm); + + if (!folio_walk_start(&fw, vma, addr, 0)) + return NULL; + + page =3D fw.page; + folio_walk_end(&fw, vma); + + if (!mm_locked) + mmap_read_unlock(mm); + + return page; +} + static int binder_install_single_page(struct binder_alloc *alloc, unsigned long index, unsigned long addr) { struct vm_area_struct *vma; - struct folio_walk fw; struct page *page; + bool mm_locked; int ret =3D 0; =20 if (!mmget_not_zero(alloc->mm)) @@ -257,12 +307,11 @@ static int binder_install_single_page(struct binder_a= lloc *alloc, INIT_LIST_HEAD(&page->lru); page->index =3D index; =20 - mmap_read_lock(alloc->mm); - vma =3D vma_lookup(alloc->mm, addr); - if (!vma || !binder_alloc_is_mapped(alloc)) { + vma =3D binder_find_lock_vma(alloc, addr, &mm_locked); + if (!vma) { pr_err("%d: %s failed, no vma\n", alloc->pid, __func__); ret =3D -ESRCH; - goto unlock; + goto out; } =20 ret =3D vm_insert_page(vma, addr, page); @@ -275,14 +324,13 @@ static int binder_install_single_page(struct binder_a= lloc *alloc, */ ret =3D 0; __free_page(page); - if (!folio_walk_start(&fw, vma, addr, 0)) { + page =3D binder_page_lookup(alloc->mm, vma, addr, mm_locked); + if (!page) { pr_err("%d: failed to find page at offset %lx\n", alloc->pid, addr - alloc->vm_start); ret =3D -ESRCH; break; } - page =3D fw.page; - folio_walk_end(&fw, vma); fallthrough; case 0: /* Mark page installation complete and safe to use */ @@ -296,8 +344,10 @@ static int binder_install_single_page(struct binder_al= loc *alloc, break; } =20 -unlock: - mmap_read_unlock(alloc->mm); + if (mm_locked) + mmap_read_unlock(alloc->mm); + else + vma_end_read(vma); if (page) __free_page(page); out: --=20 2.47.0.199.ga7371fff76-goog