Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3411267pxf; Mon, 29 Mar 2021 01:13:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyoAFjjJ1GHIcs3F9JSJHKmS3s8pV9c4MQjYTWjLAmyPW9fPUOIvL71MiVLNv52OxCQM4J7 X-Received: by 2002:a05:6402:2cd:: with SMTP id b13mr27633063edx.55.1617005626566; Mon, 29 Mar 2021 01:13:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617005626; cv=none; d=google.com; s=arc-20160816; b=t/gCT/HsX7O+ZJh+GspF+TAXDvNFqT9b7FvXrkZ7goUK8G5Zgeosi1IFSmXOR+9Jz1 3afRdX1pcSajkf3UZ7BoR51NFXuz3jZ4Llh3AL0rRmNny99vu2CUjW5f/bOgneM7H4LY 4NoR/2VcWlaRcW21f+YCggonnHnXhQZjWB4dP7RHbZG/Vdcng/5Yc3lpKJWtzTaoQhqT BH2Q6OL0gLiD5EtfDD5Sm7m8RIaIaQTjil4jQc6CzEJIywcWnHZ+odhCOGVnAufvs8gj BwqccZ9l7NDAlKTHFVAfRxGc+f7xY3DwwjPWeMJxwYivdWqc5k2mgce/ezaip004ZIiB 9zFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=wziFFMVWdUKgruWfnk3G5fr8TF4tLLkfH5asltPayJ4=; b=bR2s4/ifqetQdyVu2Eg2FVZGcWtHJYs+j3P4ygRpGN9eTIT1K0FH4bh3wHnZ54iIRT bTqqgVbdkovNIRj8q4QrxXT93WfhObNFWtTvQ7jQ6OKbUOS5UGyTWb11O1ZqpGstPGIO TO6shErn+KKf00er+NEd4sVlrFmTaXf4b5XpmoaZ9FD0+5v/3HPY/HYDoOHhE7qDzEuC iRF8y4W/zv5MZzLjsfQY7o0YPtaUN8Ls/J1hRDoQDNChh8RcGL8LTUJm8ZycUFQxfJLr pSRdfmqI+D95/Wdrf17J72GGYef6Fn5M6eMAd+jfukRRyL7qlFb6KCWQgvlxuPcSFQ7h tsiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="YlRcx7/e"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rn25si12950640ejb.250.2021.03.29.01.13.22; Mon, 29 Mar 2021 01:13:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="YlRcx7/e"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232540AbhC2IKR (ORCPT + 99 others); Mon, 29 Mar 2021 04:10:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:48512 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232289AbhC2IGG (ORCPT ); Mon, 29 Mar 2021 04:06:06 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6058961959; Mon, 29 Mar 2021 08:06:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1617005165; bh=m5Wk5tBfABEkkMFBb/GMdpxP32+hQC7kv5XguCzEq58=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YlRcx7/eklIOFbyY5A9+0iFpzk7avF7XIhN9klgo17E0qfADD5/MZh10Bwvh5lXkZ 9i5M6mjQ0R/hD+wjoEHYiLkX7Z+ukDYX+6ExDqMGSAQVXrs+Y+dyqDC1IbRQdNtUo0 61kWIU+vo1N5Pdxqv7uHysTQ/9p+mrkWzIx9IaDc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Rafael J. Wysocki" , Hans de Goede , Sasha Levin Subject: [PATCH 4.14 48/59] ACPI: scan: Rearrange memory allocation in acpi_device_add() Date: Mon, 29 Mar 2021 09:58:28 +0200 Message-Id: <20210329075610.459964138@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210329075608.898173317@linuxfoundation.org> References: <20210329075608.898173317@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rafael J. Wysocki [ Upstream commit c1013ff7a5472db637c56bb6237f8343398c03a7 ] The upfront allocation of new_bus_id is done to avoid allocating memory under acpi_device_lock, but it doesn't really help, because (1) it leads to many unnecessary memory allocations for _ADR devices, (2) kstrdup_const() is run under that lock anyway and (3) it complicates the code. Rearrange acpi_device_add() to allocate memory for a new struct acpi_device_bus_id instance only when necessary, eliminate a redundant local variable from it and reduce the number of labels in there. No intentional functional impact. Signed-off-by: Rafael J. Wysocki Reviewed-by: Hans de Goede Signed-off-by: Sasha Levin --- drivers/acpi/scan.c | 57 +++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index bd58f0743cfc..9ec463da9a50 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -622,12 +622,23 @@ void acpi_bus_put_acpi_device(struct acpi_device *adev) put_device(&adev->dev); } +static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id) +{ + struct acpi_device_bus_id *acpi_device_bus_id; + + /* Find suitable bus_id and instance number in acpi_bus_id_list. */ + list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) { + if (!strcmp(acpi_device_bus_id->bus_id, dev_id)) + return acpi_device_bus_id; + } + return NULL; +} + int acpi_device_add(struct acpi_device *device, void (*release)(struct device *)) { + struct acpi_device_bus_id *acpi_device_bus_id; int result; - struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id; - int found = 0; if (device->handle) { acpi_status status; @@ -653,38 +664,26 @@ int acpi_device_add(struct acpi_device *device, INIT_LIST_HEAD(&device->del_list); mutex_init(&device->physical_node_lock); - new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL); - if (!new_bus_id) { - pr_err(PREFIX "Memory allocation error\n"); - result = -ENOMEM; - goto err_detach; - } - mutex_lock(&acpi_device_lock); - /* - * Find suitable bus_id and instance number in acpi_bus_id_list - * If failed, create one and link it into acpi_bus_id_list - */ - list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) { - if (!strcmp(acpi_device_bus_id->bus_id, - acpi_device_hid(device))) { - acpi_device_bus_id->instance_no++; - found = 1; - kfree(new_bus_id); - break; + + acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device)); + if (acpi_device_bus_id) { + acpi_device_bus_id->instance_no++; + } else { + acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id), + GFP_KERNEL); + if (!acpi_device_bus_id) { + result = -ENOMEM; + goto err_unlock; } - } - if (!found) { - acpi_device_bus_id = new_bus_id; acpi_device_bus_id->bus_id = kstrdup_const(acpi_device_hid(device), GFP_KERNEL); if (!acpi_device_bus_id->bus_id) { - pr_err(PREFIX "Memory allocation error for bus id\n"); + kfree(acpi_device_bus_id); result = -ENOMEM; - goto err_free_new_bus_id; + goto err_unlock; } - acpi_device_bus_id->instance_no = 0; list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list); } dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no); @@ -719,13 +718,9 @@ int acpi_device_add(struct acpi_device *device, list_del(&device->node); list_del(&device->wakeup_list); - err_free_new_bus_id: - if (!found) - kfree(new_bus_id); - + err_unlock: mutex_unlock(&acpi_device_lock); - err_detach: acpi_detach_data(device->handle, acpi_scan_drop_device); return result; } -- 2.30.1