From nobody Sat Nov 23 23:34:17 2024 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 54C081D2F64 for ; Tue, 19 Nov 2024 18:32:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732041178; cv=none; b=qDRqKqy35Jv6KCWcM0f8t9mMNRYHhq47TesfR8v9WW/F00UccX1RSVIkk/1fvP6NnU8oPuOS5hJSXkbrAW3LN5X/yYA9SGIKxKf/gLu9VX78VXU0ukE2nb1/WmDhXuQEOk2oY6LOoC75VfKpr/5P1UeeGjfCKNLObVdCs90UCac= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732041178; c=relaxed/simple; bh=abkWcQruZQAyyxfLTzCFwzWlOdQCppfUVoHd5xUV384=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=r6HUc2Hhu29W2xjuDol3k4OcUQ2DtEQqi806R8NbpAMJ+5GmOV22h8+FJcjksHi3mCPV/hppwyaAOXE89F7TIOesghPXUS6elEMS2DPQ6m6O9gQluUWqx3WBWEA07VIlbQqi+fA+5BT7trxkQAroqisiJb3dwvw4mEvP3CYAP9Y= 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=stHdq2yh; arc=none smtp.client-ip=209.85.215.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="stHdq2yh" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-7edb8c3e743so4279219a12.1 for ; Tue, 19 Nov 2024 10:32:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732041177; x=1732645977; 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=UdHU7bnKrsMN/HRqqjAQBAinJJB2+tdB55dkQ2YqJy4=; b=stHdq2yhQ7Qhz6id9lSO8KPxl5WxX60rUe46Hl/8RrRGRJJL5fEVJjb3GHtN1s68pP XXnD4iReXAwI5pN1aaAVwAq+WGque3eG+9LmjeTUIWTMCOOJQIdfsqSwz3cNuHdKqiV1 2XtuF17d0liRO5HS5cSLYZm5vk+kEURY2FcQ65u6B0u5SZ8NBYRU043VBjfb3dY4M+MO wVoeBeB2lKe0EktQ87vIZPzIN263zybp7Ox5WJoZZsB3eNgQJlzT2aWtKdwwCyFeA7rD e6Xo5pYyVQzUOVIbLmAGL4BFCKFcV8v32bLEqMJJk6PUUssdSWVEaejNVb8MJGwCM9NM tAeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732041177; x=1732645977; 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=UdHU7bnKrsMN/HRqqjAQBAinJJB2+tdB55dkQ2YqJy4=; b=Y4KiEkJGWkP/bVi2eY4vUR9VEMOkGOYF7376xwjL11P/4UI09cp7muQCID1fHnmlV+ afaZTBMF4kjIhsHYTZ+HvVcBuGg5LJAmwqfpeOELb9hQr3pXfHt+hCcdd4CBM/XQ97zv +6F47O1J1gm+L+L+vJutLHv5AuMRqYTCh05jG+YosMZ6kfrlRemmWSgYkD7hHJIJQ+a6 9kIJXyiZ3YPG9DVpIS0TWTKJTKUMg+olKzVOQ/yLWwvQgBSbeGi5DrVxJsJsypOcQo7Y IyWekWNWJTlbxwMKcEXHAV2rUON2ih49fhHzAK7kli/BEZAZokN509js5dcSrwSDFbnb wTfw== X-Gm-Message-State: AOJu0YxB9FHBdkKItUzrnsASYtJSJg/ectSf/DdL9vhUgk04s3dehD9g 61UTyW9iRe4JiKBt1IcEe2uK+fRoI44K4V+yjD8j6UjHGYrEbHsTovwfBPpixZ2qIUk2iuoBbUB p1ZOxI/+UQQ== X-Google-Smtp-Source: AGHT+IG2VmBOS7/l+3pwD1KJEQHP3gK28h0IxSgyMtEcEHQ7e345ie+tE8fsYZoS7CO07KO0+UPNK6EXBj4xug== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a63:ef4c:0:b0:7ee:4cbe:9f3f with SMTP id 41be03b00d2f7-7f8c20224bbmr22859a12.8.1732041176661; Tue, 19 Nov 2024 10:32:56 -0800 (PST) Date: Tue, 19 Nov 2024 18:32:34 +0000 In-Reply-To: <20241119183250.3497547-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: <20241119183250.3497547-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241119183250.3497547-2-cmllamas@google.com> Subject: [PATCH v4 1/9] 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). Trivial conflicts with commit 2c10a20f5e84a ("binder_alloc: Fix sleeping function called from invalid context") were resolved. Cc: Mukesh Ojha Reviewed-by: Suren Baghdasaryan 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.338.g60cca15819-goog From nobody Sat Nov 23 23:34:17 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 C8E841D3581 for ; Tue, 19 Nov 2024 18:32:59 +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=1732041181; cv=none; b=tn1uShyUYEZhDdV3KGt52GcFBtNt7FNQff9o+69hp1hrJGaE1fNJ1VlnKfbioVILzYbrkQarh/crhTVxdLQ3Xnpcdrcyrhczi7gzXFYfnzfhcbM/4Yn4+wDPWEbU+mWIlI4CbjSfuJm75NLkmDZnSm+a5q05Ke6w3bQHz9Hjkgs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732041181; c=relaxed/simple; bh=A0WBGcylZLU3DcaShibb41JThSy1QYaFV30IJDZAYCw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=S45VTVBGV/bDAqT9QbMrk6WeE7vQ6JuKV82n9F/95DTqsba2oHZrcGZGh8KCLQR2okcXENSxFUVZvHZ5TOsgUhV6oDT+1YwWNUguDzvhQKus00iTHNEl0iwZDpE6eHzClaNH4icS6OezugZ6oTt0pJMY/R7fdCjrwOPe772eKOs= 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=Ymc7r9IC; 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="Ymc7r9IC" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ee7ccc0286so48219347b3.2 for ; Tue, 19 Nov 2024 10:32:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732041179; x=1732645979; 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=Bn6utr72tyIQMc2MXS808aNFB+NOUPMTkmuXUr1q1p0=; b=Ymc7r9ICYXmepd5OB69bpKbwO5QMdZ8Q+C19HvG9c1u5XgieUGcv5FFLz0giLj24st CwLfCZ2KWeCyAF71ocByleaUbwMWKZukZ9m3pmweCs+pJT274MQFyaKWBTboHdk9NQUe sWZygbFpobhiRvDihNKJCqgjIRPTbhzyUrMUJFa0RCMoKeH7A9nn+KwuxZETicPBPq7L zVJI+R0QaoyuEDqj1MhwCjCLTQYmb1LO49LS50EMY8vRoEMAUDF8PizaiFyXodj23dCs y1zXICGgM/Y2F8pegJWJIJmL5rnXJuVer7zh+D8m5ByVGqcZq8e/bwvkBeAhos4VWg2U 06Zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732041179; x=1732645979; 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=Bn6utr72tyIQMc2MXS808aNFB+NOUPMTkmuXUr1q1p0=; b=fTFTizMriwC4m6I3+NG4gjqqKcSy9vFKdHGOB55JnxOWuEeBeHENlY7UefJQ78a4kk tbnJf8KA1bbM/24m7x6YTKiWG5tW6oSBjCKbPx/IyBRcETOiP3Uf8rWaBuQVbl3Bdsro DOih64v5l5NTIVDvDlDPVUdLQpOoJsHnGFmCYn4S6JxoWFM68BlrokzTyRRlu3BMo17W 7yBs/IVMtMbDE5Csim2mX6VW5TWRqIp0ypsoMJ8oSAhy0lG0Epwv0ijwzCh8j8VLu02H bzwUY/d0uQbPKH64uisTXjsrj+Kwn9wJU3AcR0/ZXsuFSQ65gzzQaqYyxlq4mQQ3sI33 ZPVw== X-Gm-Message-State: AOJu0YziDJdhl5AsxijoRw5pPBU8SZXrvLP9V++t5x4rXZKZF1cCJj20 TkcRnx5PYdxcErsvjHdImTbLZq7lTwLXakj5T+PWQbecTMvAiFTogfmN+T78OWLs0wwQd+eu420 Rf7AYAfNfZw== X-Google-Smtp-Source: AGHT+IGKteloRNN2ipmuKHTr64YcfKxjm1l/LDGUff+hIxE0X7lX5+uMsAykjcSsXJDZLODr8Et4wvAO6jh70Q== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a25:3055:0:b0:e38:115f:eeb0 with SMTP id 3f1490d57ef6-e3826614647mr11313276.10.1732041178757; Tue, 19 Nov 2024 10:32:58 -0800 (PST) Date: Tue, 19 Nov 2024 18:32:35 +0000 In-Reply-To: <20241119183250.3497547-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: <20241119183250.3497547-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241119183250.3497547-3-cmllamas@google.com> Subject: [PATCH v4 2/9] 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 switching the mmap_sem from write-mode to read-mode. Races to the same PTE are handled using get_user_pages_remote() 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 Reviewed-by: Suren Baghdasaryan Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 65 +++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index 7241bf4a3ff2..0bc6ff1c341f 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -221,26 +221,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 page *page; - int ret =3D 0; + long npages; + int ret; =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 +236,48 @@ 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) { + __free_page(page); + 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); + npages =3D get_user_pages_remote(alloc->mm, addr, 1, + FOLL_NOFAULT, &page, NULL); + if (npages <=3D 0) { + pr_err("%d: failed to find page at offset %lx\n", + alloc->pid, addr - alloc->buffer); + ret =3D -ESRCH; + break; + } + fallthrough; + case 0: + /* Mark page installation complete and safe to use */ + binder_set_installed_page(lru_page, page); + break; + default: + __free_page(page); 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; } - - /* Mark page installation complete and safe to use */ - binder_set_installed_page(lru_page, page); +unlock: + mmap_read_unlock(alloc->mm); out: - mmap_write_unlock(alloc->mm); mmput_async(alloc->mm); return ret; } @@ -1091,7 +1108,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 +1118,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.338.g60cca15819-goog From nobody Sat Nov 23 23:34:17 2024 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 A559B1D3625 for ; Tue, 19 Nov 2024 18:33:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732041183; cv=none; b=hmXojaIktYPnsdpHCZ42EIN595XrGBn0zwnmh+m/wVhjYNgtpf8gyAyug+3lOiJx1eR1WYCflsSowUpw19mJuziCwSRbt+I3zXa1NGj3tfyQB7qdF/SsOzp1x41lBL0UrhTJ9Hjs4iIWv49ekp/7nSbr5ZNYmRMEwv0Afpv53rA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732041183; c=relaxed/simple; bh=Q0SgoeotoT2Y54W79Z28gQJZJWWn+2M7R80hDomp1z8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rnTYHzpbYiR+qFvfnACeQbwd4luLwkWjgJxfU5WFpCIWoWcpW+aQadVq9vTPkVE1wvtW7tOFzmfIm9fa7qk2L1mphxKh8bi0gfRdp5n6zL79SkCIFU3LaNenngc0HfQmzc1zYr/3qCMzZhXuPf71PsrVqCT9W+HkPJv6+jbZNAU= 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=qkVDwVx+; arc=none smtp.client-ip=209.85.216.73 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="qkVDwVx+" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2e9b243dccdso3482414a91.2 for ; Tue, 19 Nov 2024 10:33:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732041181; x=1732645981; 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=8XasUXjmO5ZGyzj3NfSvgHBs7VRqzc0tw6KFEM3UDYU=; b=qkVDwVx+HXElHuIzAALhOovm+4tdPkAhitdFt9w2e5zOrILXygFKWlzpSVh0fMif6J 1YwMvw1jitPi54sJT8IVFcM0PFW+MZGA+Ht5t0Uof3GzBT/o/RcPPPaNdLdR8eSC1/NR rJbnljvTHGX3197Wz2/TfVmem6Gp075BqJEETwQAtxWvFtNcIif0yTOHarQu5Ezzrdh9 pj4EdWqzeboccYAMTUelMPgrZsSnr+WJUNi0fUQhbNuKycOKxuhamMyKJ78tT9Kw2w/h rdk88zip1leIUg8R5UALtNNEtViIeCossMK1/WoyWKKuYv3p3ey87hOuVFo/71LclEGq LQIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732041181; x=1732645981; 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=8XasUXjmO5ZGyzj3NfSvgHBs7VRqzc0tw6KFEM3UDYU=; b=iUxn+o3wW727zJIr+UAWs/MHb3ldqoy9CKcdXxtNmwJ0+C3uVHhykSRq9kowRCYvoJ wGiQRi3h9RuISb/yabO2phdREQ4Cv8bUzTm0ZeeGE2HivDMpwJIeuD+92Vo9iOZsgvv0 PzI/wh7tTT9UhNY5jHEvjvpyJC3HJ3GvdTFfY0FiEDUVKVGXC64pL2ffTrqjfLkrWbcN 10DtKcrxEXF5woEqr55DbaTtb7eIW1tJro9kMAU7eAva+DjBE1sLYlqNCXlfFMLmeAHc hit28quOvRMCkFsZvMg3t/B1BIgwIZ9GMbWAiJAF2jcBKLCmBVQ+0nRvlHvWLoZ8TJtv ytZg== X-Gm-Message-State: AOJu0YxOxQxXQAAmNGwfehZBoI9buQKHLgs+t8LgqycxGS4nlmX47r1Q zvIKQObKtfYAuhfwivwdvtF/JgicznNX+WRRNdQ2+7GV50UeeIV1Mb8VQBEWBPMMYUY6WxV5lDb LDrzAKqhv6g== X-Google-Smtp-Source: AGHT+IF0dBBqOXmShmD77EFUnhMQTTOcrvsChfMv+7+SGG8FozE1AKx9g8DO/CsiKm2xbeDgm/Lon1y+9TCqtQ== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a17:90b:149:b0:2ea:563e:fc0d with SMTP id 98e67ed59e1d1-2ea563efc6fmr27075a91.6.1732041180916; Tue, 19 Nov 2024 10:33:00 -0800 (PST) Date: Tue, 19 Nov 2024 18:32:36 +0000 In-Reply-To: <20241119183250.3497547-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: <20241119183250.3497547-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241119183250.3497547-4-cmllamas@google.com> Subject: [PATCH v4 3/9] 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 Reviewed-by: 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 0bc6ff1c341f..c1a9a11d2632 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -210,7 +210,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); @@ -334,7 +337,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); @@ -947,8 +953,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.338.g60cca15819-goog From nobody Sat Nov 23 23:34:17 2024 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 5F4B61D4329 for ; Tue, 19 Nov 2024 18:33:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732041186; cv=none; b=S/Z/2aq1Rhkzkkk6pfVRDT0vqIPD/IUsjj0IJyaGU1vltx4MUZF99gST9MLVhhHQUSJJY7Q3urlZhEG0zlGSxfMDyY+iiAqZk/RdKhBgpGNvJlhZ1fGDwg73w7kKXIj/mu0e9PFyzwGjOwKcZqglzltv2KVUYcXXWwTFkYPw2zA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732041186; c=relaxed/simple; bh=EfcoNG9okG2g4k5D+Jznjitl9kCkxR3Tw4wms6XJ2+E=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bY02Uq0qlFdjzUU+u+QEqoojCC6mS3QT39Z/jlkWd83JNVU8Cwre+Dc7CLxvei6YOljZ6zF6GlKlJsmVRojQMUE8DPXQFHyJWzxK960BxkMYQI5G3av7UXTr6UBmG6vHskL8S8OymT2y1Ou9wTTJe4aC+vdj9qbPP4RmNjdLgEA= 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=n05DM4sg; arc=none smtp.client-ip=209.85.210.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="n05DM4sg" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-71e58e838d5so1212534b3a.1 for ; Tue, 19 Nov 2024 10:33:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732041184; x=1732645984; 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=zMN0k5svveXvC2sH2pHSPqK7eKbC2pditVCZqBEVvus=; b=n05DM4sgMwV9G/cCPH4cSluMz80hbcYpkmJi/WtbcGvyzSOVFVWvk7QWPymSvPiW0n 5GJFBhg1/7K6oGSC84b7YSIZoSX0zS8bGrqfJyY1ite+1lQVTNck3+Q4PUlqx238YsJC C2bRYpOmLbvkfxM3e6r5nsCMFPsGSYaB3jw79jDzLkULTC+HH+AGNvUfh9CkFze16VDl S/90m2KxcZjpKMud+ErD7jl+DIQxYs2rHlsBFrx+sVKV5r3X2o9eMk6ACsPBScDqf0vQ HgiWYrRJURcpVTbo3gzD7qEuqWiHkMS5/4w7khz08bGYMJ4J5qI45Mjo76tBAJ66gLfp QN1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732041184; x=1732645984; 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=zMN0k5svveXvC2sH2pHSPqK7eKbC2pditVCZqBEVvus=; b=cWNNfEMZTdTQ1GF1wnZ2cW8vURivuBlBSbZWEMKWwxQim3DeFgAHc00IpIvU5DWxGs DmrqW385Y9gPwE99e8kLEuL0YC5vesZc1Qc4CICsEMNG1MzTtrvqOnbxoTC7UBbpjgmf Ga24/WgqDAR0OIxOtvdzdR67I+Q1L96DPJM2glJ8PUBHSm0sN5cLhWH+rgXjM1IgIaUI tvKPqQtjZ6HOdTnVvQaqcOMVRUliypjyYG3mxe+wcWYOHrLLqJ+SaMd7ajVKNDUGJ3mv HpHr0IdXaoN9qwpYYoPy71yFGLctkwESvUzWYNepP3pC40Eg1hrYH5WN6EFPv+Us+oBR jvpg== X-Gm-Message-State: AOJu0YzehEqQiRtvyQZbT6sOvjcEG+3bpGnt2/MyWaxS/1lgNFEGaHL4 Mjbv7pmHtMhksqCH8B7DoaTlQAOsFbZOhVo9WvCm9SHxmaqJ8lUwmaMjUYphdQZ6g4yQDv6A4jH bFbxBs1nxZg== X-Google-Smtp-Source: AGHT+IGuPsyAO34xBw2ldZBPymcD4qim52fCTMxGEf5mCEmKpjnN7L2HnioprXOg1Cua+8a36Aib67v42ScaLg== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a05:6a00:1893:b0:720:2fb7:bcf6 with SMTP id d2e1a72fcca58-72476f7dc3fmr369321b3a.3.1732041183769; Tue, 19 Nov 2024 10:33:03 -0800 (PST) Date: Tue, 19 Nov 2024 18:32:37 +0000 In-Reply-To: <20241119183250.3497547-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: <20241119183250.3497547-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241119183250.3497547-5-cmllamas@google.com> Subject: [PATCH v4 4/9] 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 Reviewed-by: Suren Baghdasaryan Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 104 ++++++++++++------------ drivers/android/binder_alloc.h | 16 +--- drivers/android/binder_alloc_selftest.c | 14 ++-- 3 files changed, 63 insertions(+), 71 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index c1a9a11d2632..7afb3f74b19f 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -176,25 +176,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 @@ -203,16 +204,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 @@ -220,8 +220,25 @@ static void binder_lru_freelist_add(struct binder_allo= c *alloc, } } =20 +static struct page *binder_page_alloc(struct binder_alloc *alloc, + unsigned long index, + unsigned long addr) +{ + struct page *page; + + page =3D alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO); + if (!page) + return NULL; + + page->private =3D (unsigned long)alloc; + INIT_LIST_HEAD(&page->lru); + page->index =3D index; + + return page; +} + 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; @@ -232,9 +249,8 @@ static int binder_install_single_page(struct binder_all= oc *alloc, if (!mmget_not_zero(alloc->mm)) return -ESRCH; =20 - page =3D alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO); + page =3D binder_page_alloc(alloc, index, addr); if (!page) { - pr_err("%d: failed to allocate page\n", alloc->pid); ret =3D -ENOMEM; goto out; } @@ -253,7 +269,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 +285,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); break; default: __free_page(page); @@ -289,7 +305,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 @@ -301,14 +316,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 @@ -322,8 +335,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 @@ -332,14 +345,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 @@ -760,11 +773,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 /** @@ -839,7 +851,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; @@ -862,17 +874,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; @@ -948,20 +955,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++; } } @@ -1010,7 +1019,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; @@ -1023,8 +1032,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++; @@ -1084,11 +1093,10 @@ enum lru_status binder_alloc_free_page(struct list_= head *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; unsigned long page_addr; size_t index; =20 @@ -1098,10 +1106,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); @@ -1110,8 +1116,7 @@ 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; + binder_set_installed_page(alloc, index, NULL); =20 trace_binder_unmap_kernel_end(alloc, index); =20 @@ -1129,13 +1134,12 @@ enum lru_status binder_alloc_free_page(struct list_= head *item, mutex_unlock(&alloc->mutex); mmap_read_unlock(mm); mmput_async(mm); - __free_page(page_to_free); + __free_page(page); =20 spin_lock(lock); 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.338.g60cca15819-goog From nobody Sat Nov 23 23:34:17 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 F08771D5ABA for ; Tue, 19 Nov 2024 18:33:07 +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=1732041189; cv=none; b=mGQakq11iIllk94k3QnuDgLpg0ipdTrMsgsI0M++jl4Cp2gwgH3/x8SFv8jQel+JfH1waMPoKjaAzFsaE/Ruv2MZY4zRtzipa79NBowbDhhfhJSTCq+eg6aTBMk9hNLv7L/ELTB/EcFNSETpIp52mK/iX0hRHgHLTJX32AvVNC4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732041189; c=relaxed/simple; bh=aCbsPgQYe39IyV/L0vtNyOQErRLIEY5HkWRButB7hHg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=c8OXTpx6B7ihwnEtZv4dwz/Ua4pv0rqSiy/CXWIkabPE22yIhO/2fiPuc1GsJIfc9r5w9KkW/ytyRatPy9YoXw93zfi4C3rLT/vD2qZuVaMMzLyb9QaeWI++0yaQrtvcdoDOVGIysJdG2xbtt0Uzsyjb1AH5TW3dbyPJa/13swU= 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=fGCt+PKq; 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="fGCt+PKq" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6ee7ccc0283so54420627b3.2 for ; Tue, 19 Nov 2024 10:33:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732041187; x=1732645987; 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=9QP22PXIuQsNxGf7geF7h77az312wscnwPc4kqV8q1Y=; b=fGCt+PKqr5BswDKXXQmiiSBw89CQpYx/NlWZzIUW+Uhor1CdU7j0dsdGwH4yrMUvgf Y/a82vIi94lNUFuQKjRR24AS05/XAgqMRDCqt5qpt2p6qZiFi8iz7i+oHif5GuxEvgkU e0YWqRNIYtGiiGzHaqeC1rD4oJo7XVgIZQ8agzaV7bJJZf+BR4LkNBMT59TBcWAvtQOJ YQtgi9iZw3XcF9FGGua6ieFQWEUgKLTYC000+DlnUy3wnuAYdtf4DkUc5hDyGLaNeFM0 k7tu+3bOeclXNNsfj9o+h3iWUA9fzuwTyRN3BaUYHx4NHoLf9Q2sRnSP1QzglAWcMoeW jW3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732041187; x=1732645987; 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=9QP22PXIuQsNxGf7geF7h77az312wscnwPc4kqV8q1Y=; b=FyuOX1u3JPHJwD+voYS/gOQGCobx3lFhrh5u0PzOiqdA1fIteyDmhswMh2Y0zrOhmU dp0Ui/sclcLZOh1qJMao1aY2/6TBkx2rmvAOpTsdkl8dRDRvGabdntKsDGSZMk4Iua1c YgoJRlFs7powgTf0dvkf7H5wWfjD39o9xWJmuTMHfUcglnjIRoYPTrjk+nsZ7QtmUPz1 SnxfOi8qyyt/zn61vkeffAA4hDAptRDqMSme5fWPhhRlsc6CHmGPOYYFWGcDfunuVr2R WyH/w/FZiu4bEDwWu9a/D+bprOrXOt+FLS77WJwy7W6ExQDFpKslgYGb8FCps2b6hJ00 lTqw== X-Gm-Message-State: AOJu0YyxryT8/j+9kXT2Bq4o52Bi9ADO4PhzA91WzVv8GZU0foZPShLA SEgUZj7q/LozTHN1d6T9F8HWi/nv0iZJ3CzaoxsLEyb+kCUEYALYml12U1KRrjyf/ugWFJMK8g/ moOvNHY36zQ== X-Google-Smtp-Source: AGHT+IGsvmrUdOiN1NBs0S1fEfmlHcEeE3qXgyFCfMVqguLbsutiKYNYA4aN1DygLUVHjM1DwhhvKk6XCqdwyA== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a25:bb0e:0:b0:e38:2136:9482 with SMTP id 3f1490d57ef6-e3826614e75mr12184276.10.1732041186532; Tue, 19 Nov 2024 10:33:06 -0800 (PST) Date: Tue, 19 Nov 2024 18:32:38 +0000 In-Reply-To: <20241119183250.3497547-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: <20241119183250.3497547-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241119183250.3497547-6-cmllamas@google.com> Subject: [PATCH v4 5/9] binder: replace alloc->vma with alloc->mapped 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 as a replacement for alloc->vma to validate several paths. Using the alloc->vma caused several performance and security issues in the past. Now that it has been replaced with either vm_lookup() or the alloc->mapped state, we can finally remove it. Cc: Minchan Kim Cc: Liam R. Howlett Cc: Matthew Wilcox Cc: Suren Baghdasaryan Reviewed-by: Suren Baghdasaryan Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 43 ++++++++++++------------- drivers/android/binder_alloc.h | 6 ++-- drivers/android/binder_alloc_selftest.c | 2 +- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index 7afb3f74b19f..faf27f963088 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -220,6 +220,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 struct page *binder_page_alloc(struct binder_alloc *alloc, unsigned long index, unsigned long addr) @@ -257,7 +270,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)) { __free_page(page); pr_err("%d: %s failed, no vma\n", alloc->pid, __func__); ret =3D -ESRCH; @@ -365,20 +378,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; @@ -612,7 +611,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); @@ -894,7 +893,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 @@ -924,7 +923,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); @@ -1030,7 +1029,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) @@ -1070,12 +1069,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 /** @@ -1111,7 +1110,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..a3c043cb8343 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 @@ -75,6 +73,8 @@ 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, each binder instanc= e is + * allowed a single mapping throughout its lifetime * @oneway_spam_detected: %true if oneway spam detection fired, clear that * flag once the async buffer has returned to a healthy state * @@ -85,7 +85,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; @@ -96,6 +95,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.338.g60cca15819-goog From nobody Sat Nov 23 23:34:17 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 2B4551D63D5 for ; Tue, 19 Nov 2024 18:33:10 +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=1732041192; cv=none; b=Z+rYAbkrb2cPAoja96jjifSB6CWs05089QLJzHnPVmahX7gbaOTbDpdQtQBIEIgRy7m2KQtD0wLP00+oHm6TwQ4u8rOYZCwMe97BXU4jApRUgx5NlaNLuuV+UEVtuxNcNpio3YSutvy9PL6YA3qN+fxpUistOdtGX8hkaSXvdgA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732041192; c=relaxed/simple; bh=/1UaBQ1iarIIUt1zJJQ8peycZ7UmEw232o49gW35zGk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rtI4sZdTnBgLDBg/0enftY2DDTqhiGGmd4/lY+ALJx1T1HjTkptfT2HdT0ACsNNqD7E7HVcS5jre/onfrPO1auyX4KBnQc3ZI14FNAdY0RCwi8WXKgjQMN3sZC/jrgWLLTHJkR5ufACJ2C08DNUtVHjGmeyPr5aIcXHiDnmuIcM= 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=ttW742TK; 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="ttW742TK" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e30cf48435fso7806890276.0 for ; Tue, 19 Nov 2024 10:33:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732041190; x=1732645990; 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=tjpoD3HNpZrLOWq+OlzUXd1MP+5ZmqXB7txxxH70GZ4=; b=ttW742TKP2nRmzwpHTERVC/LRBu//zlJq3HYzyNv32FW8WnTBJLKVWkvuvkBN1MXWR CjRhxNoJUa1M6fxDfuWW4u7Ug2aMjKFh8/duzljNFPJjvHyTAtvxUO84b2TZoEGuK4e/ kVzQr46bvXRvMnYBYV9WLH3qI5cd6bXDPESrBBGztukOr/WwV3xtBdn4WE2Qn2LIwFzt oKx6mTdfZi35fwUEInTzK6Yy4uDdtkYe0VgSpi1Iwp9JLVv5N75zpl9DHnLTxO2mUCm+ BGWi1ur6DEor4sqCDlNZ9HnmX+L65g/6MomgpWjCD3QGfnwC63nSQ+whKOs41Aiz1Tc1 7YgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732041190; x=1732645990; 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=tjpoD3HNpZrLOWq+OlzUXd1MP+5ZmqXB7txxxH70GZ4=; b=RzqIZV/5mx1Ko2eUR6KlFD3AosNvRzSiIVbVyRP3mL+pnDPfN3KgUxUvzvkr+oYXSs PLimKR0KCULio24tHL3HV+rR1XK/GHnMIdn2AsBdO58tvNgOreHFugaI23zzw3pAAnRl xMefOpNOM1skmt4Gn4Ai2fMhwgq2LdinmkAXDtNDwiSJVNqwm01Bi+7Lzm0Efxu2OiW8 mn2uwBsyIUZLqQxm+MyPyKNqgvLqdrGUpqoyjQpdQPOmLop5/EeXinyIPFD3YwC4lBCO HMofEbRNKSeC06HHEVVBPvqY/KnDWzPKRfSkXQyQPNcRS4XRJ4KZtOc+Vkuy/DN6uyvM l4lQ== X-Gm-Message-State: AOJu0YzlQ2b/UdVuKsjlHxVFTgFDk78meBI/dLPkr6O+aVnZmFQcI9KY QY1dGscNmYyFfDHWFqOgleX//SdgY4FFUDKDhUPalh+xmQfnHh+GcUu3ww1AsDTbn0SLywhbDcZ 52AjbrYmbhA== X-Google-Smtp-Source: AGHT+IEySf6SaBJAnudCAfDN/8jGNoXX9M0UHEmEJAk1B3jsse6L6pPMY8M4WvvgfXfMV4OKqxDGBbq1uSq6eA== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a25:2e4a:0:b0:e33:111b:c6a4 with SMTP id 3f1490d57ef6-e3825d38cdbmr11692276.1.1732041190080; Tue, 19 Nov 2024 10:33:10 -0800 (PST) Date: Tue, 19 Nov 2024 18:32:39 +0000 In-Reply-To: <20241119183250.3497547-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: <20241119183250.3497547-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241119183250.3497547-7-cmllamas@google.com> Subject: [PATCH v4 6/9] 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. Reviewed-by: Suren Baghdasaryan 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 faf27f963088..553efe110aac 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -61,7 +61,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 @@ -203,7 +203,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; @@ -291,7 +291,7 @@ static int binder_install_single_page(struct binder_all= oc *alloc, FOLL_NOFAULT, &page, NULL); if (npages <=3D 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; } @@ -303,7 +303,7 @@ static int binder_install_single_page(struct binder_all= oc *alloc, default: __free_page(page); 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; } @@ -328,7 +328,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 @@ -357,7 +357,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) { @@ -709,8 +709,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; @@ -769,7 +769,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 @@ -868,7 +868,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]), @@ -886,7 +886,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); @@ -901,7 +901,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: @@ -1002,7 +1002,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"); @@ -1107,7 +1107,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 a3c043cb8343..088829ce6668 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 @@ -86,7 +86,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.338.g60cca15819-goog From nobody Sat Nov 23 23:34:17 2024 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 E7BEA1D6DA9 for ; Tue, 19 Nov 2024 18:33:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732041194; cv=none; b=I4kewWhcA4lDt9D06qQ17YZkVgrieWYa/eOGUzKjre4Iv0x241NZL+6CDJuWpxEtOgGyRoOrC912SST2LLcz8AirTKFZWyqY3eQcH2rMWK5ZyX60TCVdAPIsqM36Y3ijV5Z8V0DjaPu9MwEvsZZMS6G4Ul1NR3lL6B4u7BTQPoU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732041194; c=relaxed/simple; bh=ZFfObOEuO1vLP5HevUD6BdJRdLDoEknLUF/lgQIYqoQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Kl4IS20DpcUB85Y6T626gDvFczntkUcilMHqj9LjEQ/6RnxHZVbYgvD00jj0TCPuXFQb2/yYbP9XQj9oxbTYox604/9SP8sz2eKIOLESvz4sRC/aTRIEfu6KO20AAtGr1JkvEnK0AVK/y4LpTDXAv8Hm5fhA5Nom7GAsQV54Tnk= 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=JmJXj3Ri; arc=none smtp.client-ip=209.85.210.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="JmJXj3Ri" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-71e51a31988so3776718b3a.1 for ; Tue, 19 Nov 2024 10:33:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732041192; x=1732645992; 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=1MLxi0fM3sWNzEe2yYI4m7Zvs0DMIqXAh40zCoKo1As=; b=JmJXj3Rig9bbakwtiPe/voachTnT0u56h20PmWm+XwC3lbooUW1UVJugPzt7EfDJPR w1ps67zKR2Or+UGktrqtRB7tPhwyMFAGj6Nr4haJWh412fbG2oj+X5Ll5/0f5x9naJlC R/LQtQR0ZRxHQb7jccI8hggiry3uYr9I1zDR1ILkjfn7zXrgeK8Bz8uRLGM6qwxFdxb1 gaaeQai0ou8l4ckLPGnlrI9BJvrnvcAOcVnnYm+li+dNchw2ObKDaCJm2HF8FOd28stB Un0pxeqTsrmBj7SOFp/5wdW5ptzZ1KlSh3Cz7ohowMhdQnptL755T/8Bu1Nrga80CTzs dqyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732041192; x=1732645992; 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=1MLxi0fM3sWNzEe2yYI4m7Zvs0DMIqXAh40zCoKo1As=; b=LwyBp032+qjdlUqqRcNXzn4SuVaujC0pY6gDkZFUotBtDrrNLUxOa11IrJkz7QtYYr B246nxBYAJNo6+4m3BTzScjjKV1C0vfJAIzYEob+8qy5uX28nBIq4O26UCDSeJkn8mG5 hUWI5bMj+0zLF8VQn+Zb1n+faxXwrJjB/hDlS3L3O3EgjkYU7va14XVKz8YZOBEeqG9a QSbqcmUXkX6LlKIcyxh2ZTQjHdLOtya1KcMN7nZlkre13QiGkaIEBPPk6W2Z0QG9wrI9 qQYjL+NUoCbpuQtWu/OhQONCTT+pRqRikDBl/cxP65ctLTr5h5Nq9QOA4Ostb4OJMrxq /Qxg== X-Gm-Message-State: AOJu0YwakUep228Aw+9CK3q7XTKoasye3yZr1tCrHLqNr1+nZgVZ8trB 15Fa2L7RUfbP/NmDGdmlwIcFJr2kiHAODkNQKyi3Qda/qV/KBltID8Q2HICc9UKTAr/qXXg+wTM M99YLZEmesw== X-Google-Smtp-Source: AGHT+IGm507puIcsXl7gmG3qfuGnLlzJrXUJHjRkZ/l4ObK+tYKQGV+VAOBGu7wl2abL/zkVrlUZOw+ZXHkp6A== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:aa7:88c9:0:b0:71e:5e50:755e with SMTP id d2e1a72fcca58-72477110770mr83380b3a.6.1732041192296; Tue, 19 Nov 2024 10:33:12 -0800 (PST) Date: Tue, 19 Nov 2024 18:32:40 +0000 In-Reply-To: <20241119183250.3497547-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: <20241119183250.3497547-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241119183250.3497547-8-cmllamas@google.com> Subject: [PATCH v4 7/9] 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 , Hillf Danton , Lorenzo Stoakes 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 get_user_pages_remote(). 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 Cc: Hillf Danton Cc: Lorenzo Stoakes Reviewed-by: Suren Baghdasaryan Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 67 +++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 17 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index 553efe110aac..f5dbaf1d7d65 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -233,6 +233,53 @@ static inline bool binder_alloc_is_mapped(struct binde= r_alloc *alloc) return smp_load_acquire(&alloc->mapped); } =20 +static struct page *binder_page_lookup(struct binder_alloc *alloc, + unsigned long addr) +{ + struct mm_struct *mm =3D alloc->mm; + struct page *page; + long npages =3D 0; + + /* + * Find an existing page in the remote mm. If missing, + * don't attempt to fault-in just propagate an error. + */ + mmap_read_lock(mm); + if (binder_alloc_is_mapped(alloc)) + npages =3D get_user_pages_remote(mm, addr, 1, FOLL_NOFAULT, + &page, NULL); + mmap_read_unlock(mm); + + return npages > 0 ? page : NULL; +} + +static int binder_page_insert(struct binder_alloc *alloc, + unsigned long addr, + struct page *page) +{ + struct mm_struct *mm =3D alloc->mm; + struct vm_area_struct *vma; + int ret =3D -ESRCH; + + /* attempt per-vma lock first */ + vma =3D lock_vma_under_rcu(mm, addr); + if (vma) { + if (binder_alloc_is_mapped(alloc)) + ret =3D vm_insert_page(vma, addr, page); + vma_end_read(vma); + return ret; + } + + /* fall back to mmap_lock */ + mmap_read_lock(mm); + vma =3D vma_lookup(mm, addr); + if (vma && binder_alloc_is_mapped(alloc)) + ret =3D vm_insert_page(vma, addr, page); + mmap_read_unlock(mm); + + return ret; +} + static struct page *binder_page_alloc(struct binder_alloc *alloc, unsigned long index, unsigned long addr) @@ -254,9 +301,7 @@ static int binder_install_single_page(struct binder_all= oc *alloc, unsigned long index, unsigned long addr) { - struct vm_area_struct *vma; struct page *page; - long npages; int ret; =20 if (!mmget_not_zero(alloc->mm)) @@ -268,16 +313,7 @@ static int binder_install_single_page(struct binder_al= loc *alloc, goto out; } =20 - mmap_read_lock(alloc->mm); - vma =3D vma_lookup(alloc->mm, addr); - if (!vma || !binder_alloc_is_mapped(alloc)) { - __free_page(page); - pr_err("%d: %s failed, no vma\n", alloc->pid, __func__); - ret =3D -ESRCH; - goto unlock; - } - - ret =3D vm_insert_page(vma, addr, page); + ret =3D binder_page_insert(alloc, addr, page); switch (ret) { case -EBUSY: /* @@ -287,9 +323,8 @@ static int binder_install_single_page(struct binder_all= oc *alloc, */ ret =3D 0; __free_page(page); - npages =3D get_user_pages_remote(alloc->mm, addr, 1, - FOLL_NOFAULT, &page, NULL); - if (npages <=3D 0) { + page =3D binder_page_lookup(alloc, addr); + if (!page) { pr_err("%d: failed to find page at offset %lx\n", alloc->pid, addr - alloc->vm_start); ret =3D -ESRCH; @@ -307,8 +342,6 @@ static int binder_install_single_page(struct binder_all= oc *alloc, ret =3D -ENOMEM; break; } -unlock: - mmap_read_unlock(alloc->mm); out: mmput_async(alloc->mm); return ret; --=20 2.47.0.338.g60cca15819-goog From nobody Sat Nov 23 23:34:17 2024 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 3F5531D7E35 for ; Tue, 19 Nov 2024 18:33:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732041197; cv=none; b=tC/YLRx7pPJGt0QBU80qNsdgq9Y/4XdoiTaNEPKFe35i7cMmdL9tIZBN5NRhZB1CZRneHQOBm8cbfOBwdyNnYBZlNxB2cFqndJlr0MYITmGKpOUqArUdOugfDGJQmyo4FD7KMUST5z1SwmK/ITXKACu3KmN/8eJdrKaiRMNIeDQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732041197; c=relaxed/simple; bh=wxsmlyP+WToV6V1HulnlNVW1tnL7apXHhJYL687VBOM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oXshl4resYVoLdiP9sWnSOLQOCxXjXPONkZWbAEFeV87vGje/5TZYiGdN20gQERRWK01trbKkNYgsXjwd7H96XIZvNthDZzP0BEL+g2whOxm3Js6S8k2s+Kza5Oi4ke+pBMYXUkgOeCQu6Kv/St/LdCZ0BgJDKmeQXev5g8r9/0= 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=dr+KgiUB; arc=none smtp.client-ip=209.85.216.73 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="dr+KgiUB" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ea45ba6b9aso3852195a91.0 for ; Tue, 19 Nov 2024 10:33:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732041194; x=1732645994; 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=CcyRSlRTjN2gWvMAog98q27NJnjQzmeZzTCDDKlo7k4=; b=dr+KgiUB6qmuj4i862tdq1fqC79lNowU9t0Y9HYLHdMbXmefZu83MFtjy/IFct8ftZ IB+3G881O/TZKlXjZMvulnEpuXL+favz7G96A2iROcMYIsWWMJ6HBCXCRxi8DVAzPbu0 6jQmrrCrjQ5L/pY52cIKr7LIpFqENPLl3Vjb/djUciwXkE27SyrT6a98VH1Ln7PMat+N daTNfWr/nenZ/vBHYbdfNqVg9PLzcpEfh/n+zo9/w/Zu4Zsw96tA9YR/F3145p5RqGv2 MwplHX1cHZHqa7x/MJcAbYbafgUZICT67MURVkVIJPRgWSQMYREsfNOrbsAXHyEvSzzC pTSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732041194; x=1732645994; 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=CcyRSlRTjN2gWvMAog98q27NJnjQzmeZzTCDDKlo7k4=; b=JxrffM5tq0Uqe1Cst2YQ05up4qh6BELhkQ4d/NAU3FPlbvsRL7fWHvo8AVN5b1mFlV EmpjhtOHEgjc75sRyKrcff/t6QYk4WawGR30Hu/tE6xPsWjB8xzwrzBNxJYnoidlnyB3 Ox36rqXqPaDRykqCbd9WR6ZdVEulzxKO5p3VcObccXppQ2v7tq9pZBaRJH2iS5dPk9E4 BBKr02K1rj5c7c4AwxpDQ1T/CY1RLHXLsy03uqq2rMj2Lxhx/nj2pCHFJ5j6dq1kvVPL 5rVW3ah+HTQ2rYY55qihoRS+NbnjYwDyzVDphpD4SCULuQ1/ZvGVaxYQeIkSTTJFpjG6 ubwg== X-Gm-Message-State: AOJu0Yy7XqiczX71upp0KDjSyOj9vX8U7Z0iydoRID0RADjY5d1JWj7i MDG/oA/Ow1aaZQNz3NsbU7dLQoZQmmNfmd2UGSBRqHRqX7WU5rwKIlbLzx2SAzc6TgyFakte5sN SNUlkWS9Atg== X-Google-Smtp-Source: AGHT+IHEMaKeMAlsSZSmKtt5fZMyNgnoazxqAI8o2ZhbdOkrf1pvp5xfQ7xLi5RTDzM3Nl0CnL2XkjmMoMISdQ== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a17:90b:194f:b0:2ea:6b45:a5b6 with SMTP id 98e67ed59e1d1-2ea6b45a6b7mr17904a91.0.1732041194673; Tue, 19 Nov 2024 10:33:14 -0800 (PST) Date: Tue, 19 Nov 2024 18:32:41 +0000 In-Reply-To: <20241119183250.3497547-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: <20241119183250.3497547-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241119183250.3497547-9-cmllamas@google.com> Subject: [PATCH v4 8/9] binder: propagate vm_insert_page() errors 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" Instead of always overriding errors with -ENOMEM, propagate the specific error code returned by vm_insert_page(). This allows for more accurate error logs and handling. Cc: Suren Baghdasaryan Signed-off-by: Carlos Llamas Reviewed-by: Suren Baghdasaryan --- drivers/android/binder_alloc.c | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index f5dbaf1d7d65..aea35d475ee8 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -339,7 +339,6 @@ static int binder_install_single_page(struct binder_all= oc *alloc, __free_page(page); pr_err("%d: %s failed to insert page at offset %lx with %d\n", alloc->pid, __func__, addr - alloc->vm_start, ret); - ret =3D -ENOMEM; break; } out: --=20 2.47.0.338.g60cca15819-goog From nobody Sat Nov 23 23:34:17 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 4A1AB1D86C7 for ; Tue, 19 Nov 2024 18:33:18 +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=1732041199; cv=none; b=tjnADGKqTJOQjOWGMAk/0LuQw/WyHOlpuCjIfYdBWzeK0KDfMJEG4P2ofNNiVjFquws1ltyp1p27eyohDo0ePZr3Wv+4Y0hQDw+mqqPeIsKpKaYc4n9Uq6gyFx6niECXrbgUaN6LAV5FSp9mlcCs39s2JWVQhaElezk90uEtJf4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732041199; c=relaxed/simple; bh=jgGxBpCNEu9KEEniPSZgpzzvAzuwtb3/WNSDo9OiNFA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EOHgizt9AimD7ImrIiYBKcnlTM/R6jro8xtginbVyCy5O5FFEcM/wgi/d9u5NVgZUAgo+8RhcT6T1QlQhnHeexXgUCWm6dAPfTKwD5b2XOMnQ8QOQr8xfj5a6yVCUzCzlW8ukuWI/+15uIL93lvUUsTo66QcyiD1MBrM8xnTgZE= 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=svnFInen; 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="svnFInen" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6ea6aa3b68bso82521317b3.3 for ; Tue, 19 Nov 2024 10:33:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732041197; x=1732645997; 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=i7giPzKmhTug7GC37/yaLfvsOihG5y0Sl3zHLNgstjs=; b=svnFInenhfk26QdHTsGsko6XC6AV7OqZLgT9ji/r85TDH461bGTuMTxUmisHZqOgbz mJ/tOL1s1QbNhR0z1Kwp9xbAiQXAOwBOh97W/DW9lzM1GPuoiyk/HgVIne5l/V6/jaWb X/pgmhrjPSuegkSYmPk8qo2TNOUAlHMdICBoXakiKgw8M5bkkqdhE7mCUiVz3tGfRIf0 1nPdQRbPet1u1+vhnqlkkdOQs7uhJssR6n+mJbqMfR71WVtaDjfAErh4Fy/S11zjD5uY iIxpupoyJKvJ82/yEWFik8vYCTxfRERwJop2sML7jxkF4Mwx4hd1lSZ4r+vee6PTqbh6 a/7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732041197; x=1732645997; 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=i7giPzKmhTug7GC37/yaLfvsOihG5y0Sl3zHLNgstjs=; b=F8VOftCG81sk8b8Cq+p/9kDL7kai6norh3sp9tH1ngrvReUTNYh9BaK35jhbrFldNG fFwc22wHOp9R/oAk7TvoiNudscgOITzDdGVi5eKZ2RnN0/1E9WnzuSPvSJliguqIY0Za csiZ5u4Ae9rTVHQZtd/Eh7LmwIY9vETHEuLvgcyK/ZHikJ3qqKAoJj5oiwnpvU1WWTQk fhLDzQbEilDB1AF2cb8VhB6pgdYDhefcqmT2w6PEvQPZX7bGpK/KunZy/Y1lV/ySltMS xYdGwjthJUxf1M/dSnbdsLIe4VYSzJMG+ps4a7Qse4AVGTN8IQw+PsmwvEd9pqX9KiQe xB4A== X-Gm-Message-State: AOJu0YzQEzm4KtEoLW9bLf6XJrqYDn40q0J8dVAj23qvXxifQk8kcDYK i2woxC25GHAIjE1heeaD2JFv9B7ImIZNPuUclsBcYkg/OWHlZURNI7RrhBxBSG9GlZHurRz2mLY cBTQbW+n/Tw== X-Google-Smtp-Source: AGHT+IEp2WTe/lMBMe6FiOp4xUYGRVhGj1CtKQ9wVyVVN8/MbDg+JCIqObmH9/kmTSyQNKZpRUkWiFDzu9b2/g== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a05:690c:3081:b0:6ee:8453:f427 with SMTP id 00721157ae682-6ee8453f5b5mr756017b3.7.1732041196813; Tue, 19 Nov 2024 10:33:16 -0800 (PST) Date: Tue, 19 Nov 2024 18:32:42 +0000 In-Reply-To: <20241119183250.3497547-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: <20241119183250.3497547-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241119183250.3497547-10-cmllamas@google.com> Subject: [PATCH v4 9/9] binder: use per-vma lock in page reclaiming 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, "Liam R. Howlett" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Suggested-by: Liam R. Howlett Signed-off-by: Carlos Llamas --- 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 aea35d475ee8..85753897efa1 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -1129,19 +1129,28 @@ enum lru_status binder_alloc_free_page(struct list_= head *item, struct mm_struct *mm =3D alloc->mm; struct vm_area_struct *vma; unsigned long page_addr; + int mm_locked =3D 0; size_t index; =20 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; =20 index =3D page->index; page_addr =3D alloc->vm_start + index * PAGE_SIZE; =20 - vma =3D vma_lookup(mm, page_addr); + /* attempt per-vma lock first */ + 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 (!mutex_trylock(&alloc->mutex)) + goto err_get_alloc_mutex_failed; + if (vma && !binder_alloc_is_mapped(alloc)) goto err_invalid_vma; =20 @@ -1163,7 +1172,10 @@ enum lru_status binder_alloc_free_page(struct list_h= ead *item, } =20 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); =20 @@ -1173,7 +1185,10 @@ enum lru_status binder_alloc_free_page(struct list_h= ead *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: --=20 2.47.0.338.g60cca15819-goog