From nobody Mon Feb 9 01:29:46 2026 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 7AB911DE2A8 for ; Tue, 26 Nov 2024 18:40:36 +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=1732646438; cv=none; b=S1DHSLT4Pw/lEKrFpcqJg8XvSF+qQk4zGGd+1daC3+PByMwA/fR89nOXhTOd5dAOV680EI3icmflHdBH4qsAHWa773FIUYPJCn/9bBCwdB3nqFspF3eiRc9tK7gL+AOH1LlGlB8W63ZQHQ2x5VK5uYrcGAySI19fzcDnkRED/LQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732646438; c=relaxed/simple; bh=+YueLBpXhct/5r9ESZ6mKeySFEVOoXDlJQzfwzcQSmg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qQs1Lw583wPaFyDszSRpmXIJ8aGn7v2zenJ7Ill1lkbzQhsxBZooWmX4t9X5FvqiIRVBJblsGPtt1mYg9BIh/ln3xkyo1o1NE+/0NF8tMzPUBltkEkIFlXB7LmMMv3HrVqXEXJkxVj30a1n2jCmIzsicnlxGut/Az6PJZ0VkyAI= 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=mdtLmVgk; 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="mdtLmVgk" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ea50590a43so6861289a91.2 for ; Tue, 26 Nov 2024 10:40:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732646436; x=1733251236; 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=GUZblWB5BGFZ8fypeg+VMjlX//kBM9YKQ4HZ8RcYsmk=; b=mdtLmVgkw1p1ysoBb0298FvezmbAA3hFiNGB0DAYVCn0QtIm3PO1uovlYGVaHpqmG8 5YElMl09/c7jJKBO1gdA22yT4itf4v/Xmn9jo8d7uphgohkjTLwJIwTtQO4YrU6dYSeL 7HlVS27Q85RX9bPHv7u10gcU7gwT/Df/iTc/dz8THUYnyAP7rMUE7PPmAeIFwFX5Jmfy BQlC9lMOeT+XII/vPZhbXbqGvogkd6eh251KYnmSzetoIcBp5B0clxWsfX8Gd+hBiiql wDXgwnZv9pPhfmHuUW3OcLXX7lzalHabhCO77GAb0IISdHKv7xfVc9cHObiqWnhOPljX 0xmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732646436; x=1733251236; 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=GUZblWB5BGFZ8fypeg+VMjlX//kBM9YKQ4HZ8RcYsmk=; b=bT4iwntqlVbuzXQkhCKrCNiHVr7ezv8GU4HFBs+NQRKQdrmV1xkBhQnFia7ms0s37F 0eOhBt/UuKZ2aCb5G2bDmhfxJ3lcRf3lpRtoQd2j1B/1fGM5GV63V7fmQ31uLu26Fhbl zcqNjSJ8CfMFD/XYI3MzDRoWR4XNLfUQBK/fs4eGXzDZEVA5AyvNBuo7SlofFGmOwsSJ b24r0RfoI5ZB2J11Vl9AX5bZcc3NWstDow/nABqb0bpUmii+h8lwkkJAIysrFC63Mw9E yDbzPdF++OJYzXEN6ZKEk5yppbm+JEmI+Ku0tknKEszkRoCUyLsMlPquB2a0DXMaf8GJ jK3A== X-Gm-Message-State: AOJu0YwUFMrliVMe+fDv2BLOiy3VgBkyRaH9wuQ/ANMOLy2o/YRDq5aa gt9GOOkWQZFs0ansEJ3zfKLmqrA3pDmahBpo7D1h/ujYA7NeNCyfiqROkfVw+b5HmstrlDryDQ3 Bzuk/3ppK0w== X-Google-Smtp-Source: AGHT+IGGU9v0gG3aucNpeuV+kJjhHCGgIknliWLmvGq4MlILCWcmMX78+slqY+1jay/q0sAu+QBml5Kf6WFLTg== X-Received: from pjyd15.prod.google.com ([2002:a17:90a:dfcf:b0:2e0:5d64:e34c]) (user=cmllamas job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:544e:b0:2ea:7a22:5390 with SMTP id 98e67ed59e1d1-2ee08e9d43emr409621a91.5.1732646435778; Tue, 26 Nov 2024 10:40:35 -0800 (PST) Date: Tue, 26 Nov 2024 18:40:08 +0000 In-Reply-To: <20241126184021.45292-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: <20241126184021.45292-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241126184021.45292-6-cmllamas@google.com> Subject: [PATCH v5 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 | 44 ++++++++++++------------- drivers/android/binder_alloc.h | 6 ++-- drivers/android/binder_alloc_selftest.c | 2 +- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index 21edd16bf23d..4c308d860478 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 /** @@ -1110,7 +1109,8 @@ 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)) + /* ensure the vma corresponds to the binder mapping */ + 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 69cce5bac1e1..634bc2e03729 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