Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp603694rwi; Wed, 26 Oct 2022 05:09:23 -0700 (PDT) X-Google-Smtp-Source: AMsMyM63IAsCz7/iPo8zuQZPjqHlZKzOWJdPwd+gVVrzasB5BNK+FqAFpjLqpoQzlxmSrPlq0YrR X-Received: by 2002:a05:6402:c45:b0:442:c549:8e6b with SMTP id cs5-20020a0564020c4500b00442c5498e6bmr40753406edb.123.1666786163580; Wed, 26 Oct 2022 05:09:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666786163; cv=none; d=google.com; s=arc-20160816; b=HarumBHOLAuigsdfSkBwLw/TwEX3cYBEqd7HQOtIr3S6nvZUJCvd+p6LmqG+qemvLB t1zOURF5yhV0jYuTXXSQV69O4VQ7sS9GHsD3zBl04EZdmWNa6xhTQbAQ4B9gIK2hWS72 XxIVo0mtsVp8LDO5QDkO880fiQTTVx7tX4jlcf0zmOJ++jqrFz4h20CnQ/uBc8tquGrO rbMSDw77eTIcEBhL+e6aJUeHdjix6ZI1d/gzaa/XJDrs5wCvdIr0xaKHBoWwqrzZprKw mlUOKBHasI9nXOSOQeSMKp6OgLl2DHEWULu0U5qpcnLA1ahlr34yItMkwL8p2DZHfKg/ JZAg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=eeyWPYOfNLO5HnpCrV5iKgVUweZC5smkX35UuDur0+M=; b=E0GxbNTiQxpTLiKm/oMkAbQASAF2OF43/dy2kf9uTPnlCwuz46qABwtUG09/3ZQgoR AaNcprA6bZ0JSDY583rULuAAjs7d/Oq4q7HOJZVm6LvnBh2X5Eyzjyv3jvtTlYPKgMsB c+ELPfeKbYnfvEimRGy2WQ4sqHb8Bg3FR+fG32LJ+I8nhFZDV0OK/zRoaozc1bQ7jRYf UdlSN6mrjprBPMZNpEsIUSJBJXvNncWiJ7PsSUJnJg7wb994WQKh88Y1XsURlTCj1StG qmXr7uvoKaYJ+YgtBVq+4IAjyO6W8ClTWtEY0sZmlsoBTA47lPdHpak4cswJJJiNtuF4 IZkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=M3jMZCz2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sd31-20020a1709076e1f00b0078e0e850902si5968149ejc.868.2022.10.26.05.08.57; Wed, 26 Oct 2022 05:09:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=M3jMZCz2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233619AbiJZMDi (ORCPT + 99 others); Wed, 26 Oct 2022 08:03:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233485AbiJZMDg (ORCPT ); Wed, 26 Oct 2022 08:03:36 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6FB14B4BC; Wed, 26 Oct 2022 05:03:34 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id p3so12878277pld.10; Wed, 26 Oct 2022 05:03:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=eeyWPYOfNLO5HnpCrV5iKgVUweZC5smkX35UuDur0+M=; b=M3jMZCz2f9I//6dZFQixFueDv6DvYYSZAPHVtKl+MyKAgJt6T38Gb6GzCvJeBqFFWe 0IPEe0eefPjToxWp/R7HaW03L9zOfBHUukgzhX6wH2VLoqOP1LiSPhUW8qdlzKVa1fyn ynUjy1O7nL0X2uG4J6xqfMZCAWMRa/K8N2Ttl14QZGYaym4CzdqkSkNDznT6XxNUhg1A DGnknrKGEEZOpISRKsAuZ9K3FbMmReDddGUuhOTenZ7QTT1bXck7JGO0cAeMWvlZO7SK peq006gMhlFVtmMBqC0WmcoxoqEhL/vgut4uQHRWurhrmUrceuEwqtg6fro2VEvfGI8h c81Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=eeyWPYOfNLO5HnpCrV5iKgVUweZC5smkX35UuDur0+M=; b=JZY6CnB6aNqfH6j9QSrDkbMmHYnWx7kWhRGfC0x8hNEacGQWmLQsIVeQNxQ7XLY3NZ sWXg013DPPIa2QrFQhmu36sptr6g3B1Sawzp1B/b4ih0GqCf3eob37s1xaREco+LnC43 6dQE509Anqu3w1gFFw5ycA0vQ5Vqp7QyX63H5QzCX9QFh2qEjS3dZfbNmsmzVaaEkwWF lcTlSJSXhjdUPkz/nk81oB22O19eKz2JQhfsK2soQrhXF/A4rRmEPErHfJRNEkjhfaYf yEA3ADFV9QRcNAgRt5m1GOPEbGW0YDHs19dP0qNeeAdIpo1x9tA0/3YJPZB2PUXoAE38 zjoA== X-Gm-Message-State: ACrzQf1K+QKNAN6JOPHQVovOSHa+Ye3snKZ3XiscatQpwwVW5DVTHc3X MqVyuaf3vv8Rk/CfrpySFRj/Z/QqeGk= X-Received: by 2002:a17:902:ed82:b0:178:5653:ecfb with SMTP id e2-20020a170902ed8200b001785653ecfbmr43634366plj.58.1666785814222; Wed, 26 Oct 2022 05:03:34 -0700 (PDT) Received: from localhost.localdomain ([43.224.245.180]) by smtp.gmail.com with ESMTPSA id f27-20020aa7969b000000b0053e4296e1d3sm2863289pfk.198.2022.10.26.05.03.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Oct 2022 05:03:33 -0700 (PDT) From: Qibo Huang To: rafael@kernel.org, daniel.lezcano@linaro.org, amitk@kernel.org, rui.zhang@intel.com Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, huangqibo Subject: [PATCH] thermal/core: cooling device duplicate creation check Date: Wed, 26 Oct 2022 20:03:21 +0800 Message-Id: <20221026120321.735-1-huangqibo.tech@gmail.com> X-Mailer: git-send-email 2.37.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 From: huangqibo Because creating a cooling device may have duplicate names. When creating, first check thermal_cdev_list whether there is a device with the same name. If it has the same name, it returns a reference to the cooling device. Signed-off-by: huangqibo --- drivers/thermal/thermal_core.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 117eeaf7dd24..092c7732a294 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -873,6 +873,12 @@ __thermal_cooling_device_register(struct device_node *np, !ops->set_cur_state) return ERR_PTR(-EINVAL); + if (type) + cdev = thermal_cdev_get_zone_by_name(type); + + if (!IS_ERR_OR_NULL(cdev)) + return cdev; + cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); if (!cdev) return ERR_PTR(-ENOMEM); @@ -1435,6 +1441,34 @@ struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name) } EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name); +struct thermal_cooling_device *thermal_cdev_get_zone_by_name(const char *name) +{ + struct thermal_cooling_device *pos = NULL, *ref = ERR_PTR(-EINVAL); + unsigned int found = 0; + + if (!name) + goto exit; + + mutex_lock(&thermal_list_lock); + list_for_each_entry(pos, &thermal_cdev_list, node) + if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) { + found++; + ref = pos; + } + mutex_unlock(&thermal_list_lock); + + /* nothing has been found, thus an error code for it */ + if (found == 0) + ref = ERR_PTR(-ENODEV); + else if (found > 1) + /* Success only when an unique zone is found */ + ref = ERR_PTR(-EEXIST); + +exit: + return ref; +} +EXPORT_SYMBOL_GPL(thermal_cdev_get_zone_by_name); + static int thermal_pm_notify(struct notifier_block *nb, unsigned long mode, void *_unused) { -- 2.36.1