From patchwork Sun Mar 14 11:13:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 400085 Delivered-To: patch@linaro.org Received: by 2002:a02:8562:0:0:0:0:0 with SMTP id g89csp2613042jai; Sun, 14 Mar 2021 04:14:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxw6GJPh5lHJ0CQ3hz1eKMuXxnPK3kHlJDKui98zM7hpxTK+1bofh1fl6OFOvR+dUvvf5v7 X-Received: by 2002:aa7:c78e:: with SMTP id n14mr24150618eds.31.1615720487055; Sun, 14 Mar 2021 04:14:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615720487; cv=none; d=google.com; s=arc-20160816; b=vMGjep8R3d8XcAGEJrV/AFukEQ1Fyuj3w8XufMh8BSrq9Gwbu60YdWovO+G/dskdY7 pA2AmTgtY6Io/UiRApQG2/vfN38umwi9cYrGzkruTler43R71sL6jm9OzG4slpmWPHHX zVXfOQynekv8Og3HCRh3bxveBvP6hhJFr5sNwL+B6h5zu6hzKVBu/yFtT9dFI991QLRA QDWK8mLMxszPKvxXLm+mh57LRIW1XBTBs8LQTaihwORXw5jUMzDAu7HVkcKxEzwJqlow TK4wMlS8P/QpXsrNigZckYZXSzPzEUsFOFh7tJH6mUtni+6PkAxxiuO1cShNXJ7ubdXg tlkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=8xPAvMxHmY9pOZ0ZnygmV1N99XR+BZf8XBmni53ESgY=; b=ar/ZyiagJJGEgzofUTs0FTF7mjuqyWl5d/7cTvn76c40QVXOSJml9ngDlLEbAiwpHA OUwxq9l70RR2WSyj8S5K9fF1Wo959AdfUBVULGrDJmwCYAcMtsD5YPgkFjQlzQVwCeFw nbbECUbGS8yPYY05XgdjkgEVNV/gHF/Lj26cKG+iCtoc28THFopTJ/BYaAUz9VbWzuXC 6WjOAHwTCF/1saeDxJmjBMjb5lqUU4W2IDc13tuYsZEei+LkUYVm+j1/Kv6tNN7U9+oN DJfSNsKODrSRcv+3njAJe0LyXuPlA5wPkI6Y5eiTX6fb3AtqZ9AGjrebFCL7GeiAQ/sz JOBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=z6J3QWAU; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z22si8231744edc.427.2021.03.14.04.14.46; Sun, 14 Mar 2021 04:14:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-pm-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=@linaro.org header.s=google header.b=z6J3QWAU; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235248AbhCNLOO (ORCPT + 7 others); Sun, 14 Mar 2021 07:14:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234904AbhCNLN5 (ORCPT ); Sun, 14 Mar 2021 07:13:57 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92516C061762 for ; Sun, 14 Mar 2021 04:13:55 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id k8so4171251wrc.3 for ; Sun, 14 Mar 2021 04:13:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=8xPAvMxHmY9pOZ0ZnygmV1N99XR+BZf8XBmni53ESgY=; b=z6J3QWAUD5nKc3yZCQ4Wg8qGFur7mKvRy0vsI5FRjSEuyhyNqQIBJpbVYu5BNT44dG hYVy3cZLFtt0f/aCvRmDJYchM9tg3u4qmGNJzekJrznushNnni1o/7434/rXMTAw5tYw fGVKjU8u1OPfVb4G+oq98lIeH6fZ2FxBY/Ox4e+fDhNRC2DOFI0yXtsjQAfEaKaekLXx oLdQVlc1HKIts44VvP3L+zYHe55HYCRpKslJV0cVpFL7Z2ShTjgeR5BRV54A4qTJvQjM vDZaJxxohTSGC5WEvgv5I+yPj1WGByZsK1u/os2O2uuE/ZDXVZQSFAIzT+UXpY29URqC Hyrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=8xPAvMxHmY9pOZ0ZnygmV1N99XR+BZf8XBmni53ESgY=; b=eTRji/koID4GJg0cP6H/Si7rh7wUmNPVCtzS9aHgRdU7YUaNqPYxM8aawbyYxSj/To xkPlZENprrjJGQ/EnibR4pIaNyQllUuV18syBGrKOqkri57u5rM/apfxHssNI3Uj0OEr cxhtAULGLxfeVbuclYZcEVA3N+O1RMG0ti8wo/Wl8OEhVOrJLJ5Dar76waiBv17vx8HI MeIrgU8Ng1ANysmvE7voJPz9PhvbGdahS4SlEk3EoH833wsMbNDPR3KOLoKG5pt6d2Kb X98tB2eM75TFqNlZRl2YBFYRo+iKA5EavyhAmaxS5LrSc8ldFBoht9tihX7QC6Fowfkk PDeg== X-Gm-Message-State: AOAM533OFX/ZrPyXPjXAH+IrvpLDjIK+cs2zvaZs/rOE/7UKxDFdi0R3 m2ojs+Kkog8LHuz+gGHttyq0FQ== X-Received: by 2002:adf:e84f:: with SMTP id d15mr22660387wrn.394.1615720434029; Sun, 14 Mar 2021 04:13:54 -0700 (PDT) Received: from localhost.localdomain ([82.142.0.212]) by smtp.gmail.com with ESMTPSA id i8sm16828330wry.90.2021.03.14.04.13.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Mar 2021 04:13:53 -0700 (PDT) From: Daniel Lezcano To: daniel.lezcano@linaro.org, rui.zhang@intel.com Cc: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, lukasz.luba@arm.com, Jiri Pirko , Ido Schimmel , "David S. Miller" , Jakub Kicinski , Amit Kucheria , netdev@vger.kernel.org (open list:MELLANOX ETHERNET SWITCH DRIVERS) Subject: [PATCH v3 1/5] thermal/drivers/core: Use a char pointer for the cooling device name Date: Sun, 14 Mar 2021 12:13:29 +0100 Message-Id: <20210314111333.16551-1-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.17.1 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org We want to have any kind of name for the cooling devices as we do no longer want to rely on auto-numbering. Let's replace the cooling device's fixed array by a char pointer to be allocated dynamically when registering the cooling device, so we don't limit the length of the name. Rework the error path at the same time as we have to rollback the allocations in case of error. Tested with a dummy device having the name: "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch" A village on the island of Anglesey (Wales), known to have the longest name in Europe. Signed-off-by: Daniel Lezcano Reviewed-by: Lukasz Luba --- V3: - Inverted kfree() and put_device() when unregistering the cooling device (Reported by Ido Schimmel) --- .../ethernet/mellanox/mlxsw/core_thermal.c | 2 +- drivers/thermal/thermal_core.c | 38 +++++++++++-------- include/linux/thermal.h | 2 +- 3 files changed, 24 insertions(+), 18 deletions(-) -- 2.17.1 Tested-by: Ido Schimmel diff --git a/drivers/net/ethernet/mellanox/mlxsw/core_thermal.c b/drivers/net/ethernet/mellanox/mlxsw/core_thermal.c index bf85ce9835d7..7447c2a73cbd 100644 --- a/drivers/net/ethernet/mellanox/mlxsw/core_thermal.c +++ b/drivers/net/ethernet/mellanox/mlxsw/core_thermal.c @@ -141,7 +141,7 @@ static int mlxsw_get_cooling_device_idx(struct mlxsw_thermal *thermal, /* Allow mlxsw thermal zone binding to an external cooling device */ for (i = 0; i < ARRAY_SIZE(mlxsw_thermal_external_allowed_cdev); i++) { if (strnstr(cdev->type, mlxsw_thermal_external_allowed_cdev[i], - sizeof(cdev->type))) + strlen(cdev->type))) return 0; } diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 996c038f83a4..c8d4010940ef 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -960,10 +960,7 @@ __thermal_cooling_device_register(struct device_node *np, { struct thermal_cooling_device *cdev; struct thermal_zone_device *pos = NULL; - int result; - - if (type && strlen(type) >= THERMAL_NAME_LENGTH) - return ERR_PTR(-EINVAL); + int ret; if (!ops || !ops->get_max_state || !ops->get_cur_state || !ops->set_cur_state) @@ -973,14 +970,17 @@ __thermal_cooling_device_register(struct device_node *np, if (!cdev) return ERR_PTR(-ENOMEM); - result = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL); - if (result < 0) { - kfree(cdev); - return ERR_PTR(result); + ret = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL); + if (ret < 0) + goto out_kfree_cdev; + cdev->id = ret; + + cdev->type = kstrdup(type ? type : "", GFP_KERNEL); + if (!cdev->type) { + ret = -ENOMEM; + goto out_ida_remove; } - cdev->id = result; - strlcpy(cdev->type, type ? : "", sizeof(cdev->type)); mutex_init(&cdev->lock); INIT_LIST_HEAD(&cdev->thermal_instances); cdev->np = np; @@ -990,12 +990,9 @@ __thermal_cooling_device_register(struct device_node *np, cdev->devdata = devdata; thermal_cooling_device_setup_sysfs(cdev); dev_set_name(&cdev->device, "cooling_device%d", cdev->id); - result = device_register(&cdev->device); - if (result) { - ida_simple_remove(&thermal_cdev_ida, cdev->id); - put_device(&cdev->device); - return ERR_PTR(result); - } + ret = device_register(&cdev->device); + if (ret) + goto out_kfree_type; /* Add 'this' new cdev to the global cdev list */ mutex_lock(&thermal_list_lock); @@ -1013,6 +1010,14 @@ __thermal_cooling_device_register(struct device_node *np, mutex_unlock(&thermal_list_lock); return cdev; + +out_kfree_type: + kfree(cdev->type); + put_device(&cdev->device); +out_ida_remove: + ida_simple_remove(&thermal_cdev_ida, cdev->id); +out_kfree_cdev: + return ERR_PTR(ret); } /** @@ -1171,6 +1176,7 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) ida_simple_remove(&thermal_cdev_ida, cdev->id); device_del(&cdev->device); thermal_cooling_device_destroy_sysfs(cdev); + kfree(cdev->type); put_device(&cdev->device); } EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); diff --git a/include/linux/thermal.h b/include/linux/thermal.h index 6ac7bb1d2b1f..169502164364 100644 --- a/include/linux/thermal.h +++ b/include/linux/thermal.h @@ -91,7 +91,7 @@ struct thermal_cooling_device_ops { struct thermal_cooling_device { int id; - char type[THERMAL_NAME_LENGTH]; + char *type; struct device device; struct device_node *np; void *devdata;