Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp497238pxb; Thu, 14 Jan 2021 10:53:04 -0800 (PST) X-Google-Smtp-Source: ABdhPJzqWiVrnrTXNDa4UyYcEhiuDQR3zgrlRfmV7ITfTOPcP8RUYiQLeqTtJxcT4bjLn55EA1Ah X-Received: by 2002:a17:907:7295:: with SMTP id dt21mr6438818ejc.518.1610650383785; Thu, 14 Jan 2021 10:53:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610650383; cv=none; d=google.com; s=arc-20160816; b=awIH8Sr0JiTUaQmq3eMKe7Nofclf3GT7eH6Q6DM9TV/ZLK4ym41+BkCJNyZbcXmu3k 6/OJ9qIHXwnxQYoJ5FafHqq6V/njauH9FDIdHvMpS5ierV//PrTRXvt2QhRh/DzFDWUH unMpm57HaVvsFBvVLue0s8VMP4SEQyRzC2YJ7hVg9luTl9uzMoge3qxKnKMFKAc2i6xe kYF31DclNXmKtGTCx+HBjmcdyO/TwxO14dqxtsOO8f8nMv5q7XCDTGFGd00694oi+FLf EL20f07xI+3v+Zf5bCBbckDUTjuBGOegr3X10Zjmqv5nvxl1xODTSK4wRk0lkKspeXc7 8UrQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=uUEX/2lPALU4EJwyE4cXPGLDTzBIJ4q3POqYHpYiwSY=; b=h//fq+ooR6PU0hJlP3AYVfFuC/X4FD+Y9mq0whWICymH8doinwFzWS61s8ZwutHubX uHf0lSHYapFCkTbAWcc7P0tyZv4xLSvpMNklyKxDqj5HEKhZ51Uc51ZVh68jqAzsYZ4y fCEFHhSg8UHcFem1IF5RmfGXO25z/5pgfRLO1vPdV0jboZGvEQ9HgqzQXkRzHmM6LcB9 halxdXeyDqK9lxe3UAto16WCpYr7NoN+Nv4wMykD4KQ3FCr2Xea2c8VwepsFBKlB44bE xjh7mGNWVmmxI5dEM/mnV5wj8tgzhCKfq0zahxpzkVbNTfohs/xSn8KO+dhPJ9n/NklO 9p5A== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l19si604276ejx.163.2021.01.14.10.52.40; Thu, 14 Jan 2021 10:53:03 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729524AbhANSss (ORCPT + 99 others); Thu, 14 Jan 2021 13:48:48 -0500 Received: from cloudserver094114.home.pl ([79.96.170.134]:58580 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726778AbhANSsr (ORCPT ); Thu, 14 Jan 2021 13:48:47 -0500 Received: from 89-64-81-33.dynamic.chello.pl (89.64.81.33) (HELO kreacher.localnet) by serwer1319399.home.pl (79.96.170.134) with SMTP (IdeaSmtpServer 0.83.537) id 4b0a89bcdcba0787; Thu, 14 Jan 2021 19:48:04 +0100 From: "Rafael J. Wysocki" To: Linux ACPI Cc: LKML , Hans De Goede , Andy Shevchenko Subject: [PATCH v1 1/2] ACPI: scan: Rearrange memory allocation in acpi_device_add() Date: Thu, 14 Jan 2021 19:46:47 +0100 Message-ID: <2999734.9HhbEeWEHR@kreacher> In-Reply-To: <3494203.VBaj39JGmp@kreacher> References: <3494203.VBaj39JGmp@kreacher> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rafael J. Wysocki 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 --- drivers/acpi/scan.c | 57 +++++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 31 deletions(-) Index: linux-pm/drivers/acpi/scan.c =================================================================== --- linux-pm.orig/drivers/acpi/scan.c +++ linux-pm/drivers/acpi/scan.c @@ -621,12 +621,23 @@ void acpi_bus_put_acpi_device(struct acp 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; @@ -652,38 +663,26 @@ int acpi_device_add(struct acpi_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); @@ -718,13 +717,9 @@ int acpi_device_add(struct acpi_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; }