From nobody Sun Nov 24 07:49:29 2024 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D530191461 for ; Thu, 7 Nov 2024 04:03:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730952187; cv=none; b=FiRdMCsuBZPcL5nH0Xc+/9Cmw8zyJvbYnGnhf9OGET+YhKx6uo1KEtUDnAuCS9O6KxlDv2i4S4M3/mSkVu0IOjChiWzwtbfV0FAvYKFWow04Eg500MqK23fV3joFBg3TmTFtgdCF8I7pPjeg/YrT3SB4ecgUhoqIaLp0W9zyZV4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730952187; c=relaxed/simple; bh=JrXBqOo2JhoPajzziRHF9ByUvQg4sbbU4dHzwS1P/dU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tkVERrXVTi7qoIn68iaKPuWO61Sv+vUU+nBlQR3oVwai6qZ/5ijyVODeKoqcMqLCA3J4mkMmRQDQhkbEXgfwag2bOZIc3JUmEeTatWM7Q0zzojDpgDfB+0NwGYv/2FoJPXP0lnWPXpPjBp43Pkod1pwXK1LYyiQRL8fWymFBdPM= 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=L6LeaMeF; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--cmllamas.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="L6LeaMeF" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-7ed98536f95so421877a12.1 for ; Wed, 06 Nov 2024 20:03:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730952185; x=1731556985; 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=VR6K66HQm7qElaALR5w1qrjpqMA/hWE59p9IBcioHF4=; b=L6LeaMeFj4vnZRDkCW/wrFJ8/I4mdFYxxwyf8BmIxBDmox7/ANQJUHMO0GnSOXtQp5 4yHR+QkLsxg+NNFEnrQkPpja3hXCVne84koHrhcCo/nsl4tt9HowDnieiFxV+hjjl5LM vIKVhFXUfZLBYvjQLC8k14TZjLfeANn6cR0uMUzBSpCVeh1RxlB6b1LULVqp/wMBa6Xj 2IytdmGeFrREpAajm3xxAk9+C1etK3Hwd7q3klkbZUDE4Sy/BZyl1+YI1dwqcnidctri +Gj1IcvAre+KzZwfRe6wwqLK5hPlTwGqgHEjPkXzhrMqPQJQQqvE8IGe5ZGV8zz/GtBX GJDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730952185; x=1731556985; 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=VR6K66HQm7qElaALR5w1qrjpqMA/hWE59p9IBcioHF4=; b=pqpb3olGx98hgj+n5Lxzc6ZtPp2sogLtyVK84oe+JYy3Vw3tbQX1cPa+8PyoIQrgjw ZExifZwlkc2mVMvgWZ54lIM0vClxFpjs7VU8nFQqyVfvJybqcgqmRoa74jg2b/Jh5F/N MNGTH3sFgd0QcUjCl2R8E9i+jNMKvLLOQ/+Q+81SGND1pbm7acP/pauqhiJf7ZPg19Jm +rGwrQ+M1Y/1rgMlh2r9eYCpfP95MOu4OOjGE5B4MiVp/5YQT1/DN//nU0u4sYJ+2iUo gviYC0AZv59kZCYMSsnK8IpBMjK6c/agECsgcvhUJoiMnarIx0oyg6y8Ii9aV3PbUBsh fAXw== X-Gm-Message-State: AOJu0Yz/8AbBAWWsjhzsHoSWRIzoXH0z7/xPDO/jLjTQKT9uLs6D5PA2 GKagvoTnmtB2Ngwqg0FS+AEWUckqsa1Z5vhhL0pVt0oqdEM7c2Y0ywKA64vhXAVM4OiczSZywz6 FQr3s2Y+rWw== X-Google-Smtp-Source: AGHT+IGzTSkxUrm1MUGUYs5zsF8w+IstRURotU6hnUiYOUqUTJ7+iZFLzQYNfXpOOIrqTc04S4LmfsYOhni34Q== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a63:f50c:0:b0:7ea:f1c2:8a9 with SMTP id 41be03b00d2f7-7f41c42f222mr5737a12.0.1730952184702; Wed, 06 Nov 2024 20:03:04 -0800 (PST) Date: Thu, 7 Nov 2024 04:02:27 +0000 In-Reply-To: <20241107040239.2847143-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: <20241107040239.2847143-1-cmllamas@google.com> X-Mailer: git-send-email 2.47.0.199.ga7371fff76-goog Message-ID: <20241107040239.2847143-6-cmllamas@google.com> Subject: [PATCH v2 5/8] binder: use alloc->mapped to save the vma state From: Carlos Llamas To: Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Carlos Llamas , Suren Baghdasaryan Cc: linux-kernel@vger.kernel.org, kernel-team@android.com, Minchan Kim , "Liam R. Howlett" , Matthew Wilcox Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It is unsafe to use alloc->vma outside of the mmap_sem. Instead, add a new boolean alloc->mapped to save the vma state (mapped or unmmaped) and use this to validate several paths. This change is in preparation for removing the alloc->vma pointer in a subsequent patch. Cc: Minchan Kim Cc: Liam R. Howlett Cc: Matthew Wilcox Cc: Suren Baghdasaryan Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 29 ++++++++++++++++++------- drivers/android/binder_alloc.h | 2 ++ drivers/android/binder_alloc_selftest.c | 2 +- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index 46789fa530a1..7239b92ef20a 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)) { mmap_read_unlock(alloc->mm); __free_page(page); pr_err("%d: %s failed, no vma\n", alloc->pid, __func__); @@ -611,7 +624,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); @@ -893,7 +906,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 @@ -923,7 +936,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); @@ -1029,7 +1042,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) @@ -1069,12 +1082,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 +1124,7 @@ enum lru_status binder_alloc_free_page(struct list_he= ad *item, page_addr =3D alloc->buffer + index * PAGE_SIZE; =20 vma =3D vma_lookup(mm, page_addr); - if (vma && vma !=3D binder_alloc_get_vma(alloc)) + if (vma && !binder_alloc_is_mapped(alloc)) goto err_invalid_vma; =20 trace_binder_unmap_kernel_start(alloc, index); diff --git a/drivers/android/binder_alloc.h b/drivers/android/binder_alloc.h index 5c2473e95494..89f6fa25c670 100644 --- a/drivers/android/binder_alloc.h +++ b/drivers/android/binder_alloc.h @@ -75,6 +75,7 @@ struct binder_buffer { * @buffer_size: size of address space specified via mmap * @pid: pid for associated binder_proc (invariant after in= it) * @pages_high: high watermark of offset in @pages + * @mapped: whether the vm area is mapped * @oneway_spam_detected: %true if oneway spam detection fired, clear that * flag once the async buffer has returned to a healthy state * @@ -96,6 +97,7 @@ struct binder_alloc { size_t buffer_size; int pid; size_t pages_high; + bool mapped; bool oneway_spam_detected; }; =20 diff --git a/drivers/android/binder_alloc_selftest.c b/drivers/android/bind= er_alloc_selftest.c index c6941b9abad9..2dda82d0d5e8 100644 --- a/drivers/android/binder_alloc_selftest.c +++ b/drivers/android/binder_alloc_selftest.c @@ -291,7 +291,7 @@ void binder_selftest_alloc(struct binder_alloc *alloc) if (!binder_selftest_run) return; mutex_lock(&binder_selftest_lock); - if (!binder_selftest_run || !alloc->vma) + if (!binder_selftest_run || !alloc->mapped) goto done; pr_info("STARTED\n"); binder_selftest_alloc_offset(alloc, end_offset, 0); --=20 2.47.0.199.ga7371fff76-goog