Received: by 2002:ac8:6216:0:b0:41c:c224:f26f with SMTP id ks22csp77742qtb; Thu, 2 Nov 2023 12:03:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEsoA5J4h7uWHbghe4NjbMCXPEZ2QwHDOj8i1Edo1RtIkI81jJRIxdB/S61r+/6V5d9MP6R X-Received: by 2002:a05:6a21:a58d:b0:16e:ab42:9837 with SMTP id gd13-20020a056a21a58d00b0016eab429837mr22752125pzc.29.1698951784557; Thu, 02 Nov 2023 12:03:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698951784; cv=none; d=google.com; s=arc-20160816; b=Fe8cLcRl3t3uuJQOsPF/c92q/txMIcnHxFPHTliVtbW54GYikskFHvyJ2OlBeneNF2 UHJRc3zABRR7GaeDu6ZJtNVJDyihDg/ZZBlnQG+kAmA5tAhC9wRfnVG9L5J25F0qZaH5 BKkKH3w8TJggz3P/QWJdLp9ZF+AKPosvw8VFuRlhTn6wGq8jY2X3slk6JDycgajjo3Fr R3YwG2LLftpqp58XZuWbTuBYRyHFsr24oO5hqXNDsPceLEnA82+5LX9dk09wZkLw2pSh 6F9DLNtM7KrJh8/QcNXE29ZKQ7cJgOt/bu7AQFm3MpN5bZq4w9WtyvxA+OpdMxK3fqBA 212A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=gSE9dYv++c4k7PvE4esAvLpKmpkaeD7lAM2vlPuDfos=; fh=CZn2LCtibpDsl0Zo9a9tnbwjfnRU9o2A0KEGhAadkDo=; b=RMqODCW81T2GbVcgjdMtOuvOmE0xqRpoaKR1POJZoSca93tc5O84FIdL5DZLTgNwXX blfvMYaRhApxE6OxrP+xeQ8E5bve5A1ST94hIYlrhkelvEuXIprmdYpYlpQx0wx8Dw2d oHsRZw7I8dhTk7K5wj9/xjvYwX+eUGtq+DdW3RmdTd2xSG3AqeT78+EDMUxU83bXxRRS LiZ3mTc654h/jyEULOeur9YtB0Sf3BYGFQ869p82RzZJEcdH1iMju9TwWrtX7W4JyVVT 7jNV3RmyIA8gzyTTWpfmC2mqP7puUMfXN9MNJdrvHikH1AvGsTOC8Q175QVlLFmT20FO h4Sw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=lM4HQNkj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id bx29-20020a056a02051d00b005a99ef909a9si123957pgb.453.2023.11.02.12.03.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 12:03:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=lM4HQNkj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 530468028A7B; Thu, 2 Nov 2023 12:01:45 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377469AbjKBTB0 (ORCPT + 99 others); Thu, 2 Nov 2023 15:01:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377432AbjKBTBF (ORCPT ); Thu, 2 Nov 2023 15:01:05 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2042DD59 for ; Thu, 2 Nov 2023 12:00:54 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-5b95ee4ae94so942607a12.0 for ; Thu, 02 Nov 2023 12:00:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698951653; x=1699556453; 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=gSE9dYv++c4k7PvE4esAvLpKmpkaeD7lAM2vlPuDfos=; b=lM4HQNkjkz/G/wuWD8kfFlkgtfKGdmNTbNibsHVwDNrCYAe+4/tdnRX+jybBF8/dMz hvJMPxNGmFB23I3M2FyjUm/qModJfqyRdOpevFLeBT7u4BZxGNv/pXu/pxLEbC04i0v0 sviHpE0dft7z+URjGjnIA3ZAlr9evT48vaK52jDOlq0w0gBaEJJBNu7A15DHwMwpXccl mXIJn00TdSq9GvXTTCYc2kJyOrvY21wfjO94rngaYrUQvOSuwcqlPXdOES7ATFuiD3cx t0JMmNC61qC+3KZRCP+shUbQrK7C24EbYY3rpR0E++RX0bzj0x6noHve3slz7YNGo7SB XM8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698951653; x=1699556453; 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=gSE9dYv++c4k7PvE4esAvLpKmpkaeD7lAM2vlPuDfos=; b=qhGNbrmmz36quh8wbcpRtHFASd4O58erFFPho0MWE/n4A0vK0CDsERSe2wSDYkZN2N V9PcctJonhtBo36JyKb0QPVF2444UdtYYoPJ/fcY/DcjZ49vRlAIfQmKzoC60r9wUI37 HQKAzT+/bLLbvSSOqTW5c+cVDtWdBP2aFcSSFJN0gWKcYQNpT2lcZONj2peOPQSHvdsA hih69xOgdjthG3SEZ30aNChuV9ifAIV8+t2X+XE4E/h2gEl0azJ0BCmF1vN5/7My55h1 11ktZuQ4Hp1/0o43OakWp496dKca0f4aKZ5IrcC2aLtN6ajlDpVLlaUxUrqxWWz6x2no 1Bng== X-Gm-Message-State: AOJu0YwWXTHegPNMoqZpBxn6DSPAuTgmdrxGBZwEV4MQJRgD1ZUcvEH7 yFcZ++htd2M94ZCIpXEeFiQeG0KDYXHsow== X-Received: from xllamas.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5070]) (user=cmllamas job=sendgmr) by 2002:a63:9316:0:b0:5ae:3966:b620 with SMTP id b22-20020a639316000000b005ae3966b620mr368253pge.1.1698951652810; Thu, 02 Nov 2023 12:00:52 -0700 (PDT) Date: Thu, 2 Nov 2023 18:59:11 +0000 In-Reply-To: <20231102185934.773885-1-cmllamas@google.com> Mime-Version: 1.0 References: <20231102185934.773885-1-cmllamas@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Message-ID: <20231102185934.773885-11-cmllamas@google.com> Subject: [PATCH 10/21] binder: do unlocked work in binder_alloc_new_buf() 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-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 02 Nov 2023 12:01:45 -0700 (PDT) Extract non-critical sections from binder_alloc_new_buf_locked() that don't require holding the alloc->mutex. While we are here, consolidate the multiple checks for size overflow into a single statement. Also add a few touchups to follow the coding guidelines. Signed-off-by: Carlos Llamas --- drivers/android/binder_alloc.c | 85 ++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 41 deletions(-) diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c index 27c7163761c4..ed1f52f98b0d 100644 --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -368,9 +368,7 @@ static bool debug_low_async_space_locked(struct binder_alloc *alloc, int pid) static struct binder_buffer *binder_alloc_new_buf_locked( struct binder_alloc *alloc, - size_t data_size, - size_t offsets_size, - size_t extra_buffers_size, + size_t size, int is_async, int pid) { @@ -378,39 +376,10 @@ static struct binder_buffer *binder_alloc_new_buf_locked( struct binder_buffer *buffer; size_t buffer_size; struct rb_node *best_fit = NULL; - size_t size, data_offsets_size; unsigned long has_page_addr; unsigned long end_page_addr; int ret; - /* Check binder_alloc is fully initialized */ - if (!binder_alloc_get_vma(alloc)) { - binder_alloc_debug(BINDER_DEBUG_USER_ERROR, - "%d: binder_alloc_buf, no vma\n", - alloc->pid); - return ERR_PTR(-ESRCH); - } - - data_offsets_size = ALIGN(data_size, sizeof(void *)) + - ALIGN(offsets_size, sizeof(void *)); - - if (data_offsets_size < data_size || data_offsets_size < offsets_size) { - binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, - "%d: got transaction with invalid size %zd-%zd\n", - alloc->pid, data_size, offsets_size); - return ERR_PTR(-EINVAL); - } - size = data_offsets_size + ALIGN(extra_buffers_size, sizeof(void *)); - if (size < data_offsets_size || size < extra_buffers_size) { - binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, - "%d: got transaction with invalid extra_buffers_size %zd\n", - alloc->pid, extra_buffers_size); - return ERR_PTR(-EINVAL); - } - - /* Pad 0-size buffers so they get assigned unique addresses */ - size = max(size, sizeof(void *)); - if (is_async && alloc->free_async_space < size + sizeof(struct binder_buffer)) { binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, @@ -427,13 +396,14 @@ static struct binder_buffer *binder_alloc_new_buf_locked( if (size < buffer_size) { best_fit = n; n = n->rb_left; - } else if (size > buffer_size) + } else if (size > buffer_size) { n = n->rb_right; - else { + } else { best_fit = n; break; } } + if (best_fit == NULL) { size_t allocated_buffers = 0; size_t largest_alloc_size = 0; @@ -511,11 +481,7 @@ static struct binder_buffer *binder_alloc_new_buf_locked( binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, "%d: binder_alloc_buf size %zd got %pK\n", alloc->pid, size, buffer); - buffer->data_size = data_size; - buffer->offsets_size = offsets_size; - buffer->async_transaction = is_async; - buffer->extra_buffers_size = extra_buffers_size; - buffer->pid = pid; + buffer->oneway_spam_suspect = false; if (is_async) { alloc->free_async_space -= size + sizeof(struct binder_buffer); @@ -533,6 +499,7 @@ static struct binder_buffer *binder_alloc_new_buf_locked( alloc->oneway_spam_detected = false; } } + return buffer; err_alloc_buf_struct_failed: @@ -565,11 +532,47 @@ struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc, int pid) { struct binder_buffer *buffer; + size_t size; + + /* Check binder_alloc is fully initialized */ + if (!binder_alloc_get_vma(alloc)) { + binder_alloc_debug(BINDER_DEBUG_USER_ERROR, + "%d: binder_alloc_buf, no vma\n", + alloc->pid); + return ERR_PTR(-ESRCH); + } + + size = ALIGN(data_size, sizeof(void *)) + + ALIGN(offsets_size, sizeof(void *)) + + ALIGN(extra_buffers_size, sizeof(void *)); + + if (size < data_size || + size < offsets_size || + size < extra_buffers_size) { + binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, + "%d: got transaction with invalid size %zd-%zd-%zd\n", + alloc->pid, data_size, offsets_size, + extra_buffers_size); + return ERR_PTR(-EINVAL); + } + + /* Pad 0-size buffers so they get assigned unique addresses */ + size = max(size, sizeof(void *)); mutex_lock(&alloc->mutex); - buffer = binder_alloc_new_buf_locked(alloc, data_size, offsets_size, - extra_buffers_size, is_async, pid); + buffer = binder_alloc_new_buf_locked(alloc, size, is_async, pid); mutex_unlock(&alloc->mutex); + + if (IS_ERR(buffer)) + goto out; + + buffer->data_size = data_size; + buffer->offsets_size = offsets_size; + buffer->async_transaction = is_async; + buffer->extra_buffers_size = extra_buffers_size; + buffer->pid = pid; + +out: return buffer; } -- 2.42.0.869.gea05f2083d-goog