Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2126073pxb; Mon, 18 Jan 2021 08:53:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJxHc/u4Wqk0ALqdehZlV7W+UT5duZqjO6UqLLX8QrpiKwIb9j/KZuOcXrSXL92D+dAU2edW X-Received: by 2002:a17:907:da9:: with SMTP id go41mr403851ejc.326.1610988785075; Mon, 18 Jan 2021 08:53:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610988785; cv=none; d=google.com; s=arc-20160816; b=T+9NtStOSri+nhyWjjrLibRhQ6h9S44NXzmCP93qZ2DX7e7nQrkty4rW7KmOUN3ODA C4F51u5Bt+xeT+1qkwXsvAqUksBouxnFzT/w+txGiEjH+EqbnYnxmfHDUdZ5SrRYrklF 96Q/wHpAmMI6XarU5Dez1XlTBKDH14wE4Gq8kZQicP/cJnQtL8s0F5J828MeUf+g3+zp 3cPXdJA3QJJEom2KfaFXC+jgW6F4q4HzcL8DkqdZkUiWX/GPV1K8K7TqlqUxnhwLiy9t +VtrRKv7tqLqiMEFb9xsNbqPm/oj2/hfH3yQNijcCg2xgvAQurzjtpvTPpRdOn1CGooq Icbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version; bh=O9KoYS9I3JZhQXoL3Y7lBSI29uwk71QCwYJQjM0zds4=; b=yEMwaTJ2/PzjqI1HPUSR85GK+i6W3r5Pbupiqh4q0UtCaFphx9qROrTZ3LjLkmPdrU ygMTt94W8nqmMrG2yJU0d2wBj2G+3GwIBD4ury0Q1jdXMqoMaB7t6Rq5qwbrcjMAjA8Y DAzfwtmWHb4hcMlXd9RRrpGebYHucyC5nYJB+407X/UKF1VHS6hjFWVmJ6ObWBvKqp2l x5B/oXslNL6UVErc0Mjnz1fwJ9RcoPNl3+waA8XcCDk/2u8jn4fZS7aVlkmU/ibiDWEj 9pIzhyohryKMEMyYvO6kGGeKpOL8W5DBC+DSIMzjrf4uhuFwxqQXXuDMwt7zr97YgCC1 kAbQ== 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f27si7791596ejf.555.2021.01.18.08.52.29; Mon, 18 Jan 2021 08:53:05 -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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2393356AbhARQte (ORCPT + 99 others); Mon, 18 Jan 2021 11:49:34 -0500 Received: from mail-oo1-f43.google.com ([209.85.161.43]:39064 "EHLO mail-oo1-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2405630AbhARPRO (ORCPT ); Mon, 18 Jan 2021 10:17:14 -0500 Received: by mail-oo1-f43.google.com with SMTP id k9so4167751oop.6; Mon, 18 Jan 2021 07:16:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=O9KoYS9I3JZhQXoL3Y7lBSI29uwk71QCwYJQjM0zds4=; b=Gv+dZAQCtfqscYkbSfIT1D5WiQeiFcJV7zOxJkUQkG2skN0hhjDq9wZd6sC9UsPeC5 Brh8F2O9hsH2Pc7Y+QA5JHD2gu6kjQYO6Xp8+p+HT9e++lunIGqeS7bpT/JIxmHVIj5L 4bwaigxSYOEeXrreFL+nLf6fLihy2pesExpAohbAs5mIJ453comnXjmWbbLghoB/A+s8 3ewd/e4SAZXc1Nx4ufMRPmg8FYcfcJSs3V8zcV1TjpCmF4xlziY0ltHP7iZdF8a3r0GT C7wl2L5N6+N5YMaDm1iWnz7c10rQdvQ/KktLYNCPaS3W6DI4WzvjoHJSrlNrCCnTGiCx 2jtQ== X-Gm-Message-State: AOAM530wODb+Z/vN9iGyn4E378x4uVvAojYmsHqgPPZLNGPECIwIlBnY aSG/6omz4R/0N5wbT7cby0F3VKqGcdLQ0Vm0q3g= X-Received: by 2002:a4a:bf14:: with SMTP id r20mr17407117oop.2.1610982992723; Mon, 18 Jan 2021 07:16:32 -0800 (PST) MIME-Version: 1.0 References: <3494203.VBaj39JGmp@kreacher> <2999734.9HhbEeWEHR@kreacher> <8218eff4-6629-ac20-ec3f-a66aad445bb6@redhat.com> In-Reply-To: <8218eff4-6629-ac20-ec3f-a66aad445bb6@redhat.com> From: "Rafael J. Wysocki" Date: Mon, 18 Jan 2021 16:16:16 +0100 Message-ID: Subject: Re: [PATCH v1 1/2] ACPI: scan: Rearrange memory allocation in acpi_device_add() To: Hans de Goede Cc: "Rafael J. Wysocki" , Linux ACPI , LKML , Andy Shevchenko Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jan 16, 2021 at 1:37 PM Hans de Goede wrote: > > Hi, > > On 1/14/21 7:46 PM, Rafael J. Wysocki wrote: > > 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; > > } > > When I have cases like this, where 2 mallocs are necessary I typically do it like this: > > const char *bus_id; > > ... > > } else { > acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id), > GFP_KERNEL); > bus_id = kstrdup_const(acpi_device_hid(device), GFP_KERNEL); > if (!acpi_device_bus_id || !bus_id) { > kfree(acpi_device_bus_id); > kfree(bus_id); > result = -ENOMEM; > goto err_unlock; > } > acpi_device_bus_id->bus_id = bus_id; > list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list); > } > > ... > > So that there is only one if / 1 error-handling path for both mallocs. > I personally find this a bit cleaner. Yes, that looks better. Let me do it this way, but I won't resend the patch if you don't mind. > Either way, with or without this change, the patch looks good to me: > > Reviewed-by: Hans de Goede Thanks!