From nobody Mon Apr 6 06:40:05 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 072ACC32793 for ; Wed, 18 Jan 2023 09:24:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229535AbjARJXw (ORCPT ); Wed, 18 Jan 2023 04:23:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230281AbjARJUl (ORCPT ); Wed, 18 Jan 2023 04:20:41 -0500 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 859BD75A1C for ; Wed, 18 Jan 2023 00:38:35 -0800 (PST) Received: by mail-pj1-x1030.google.com with SMTP id y3-20020a17090a390300b00229add7bb36so1434332pjb.4 for ; Wed, 18 Jan 2023 00:38:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Rk4Yuj9GI6UIE2hgNfLDbnDh1l8wksnTp158bl3zXSc=; b=nnVisWFzyIwOrVLjxPSve3qNP6rlOovgA08f/iPGaiQzYCJDpfc9cgeCKtC9MHW+kK ZvW4Sc2nntdflPaYnuFaYre3Sr/kiKblRrhJrUBzpbcTDzugbaRDddIcCPhAgDHsI4/f hKCu8uLh5cFo7Yacgt4UNDEi09FMXoq3UDlpNEqhVxwwG7wmtqynXrXBknEEn3OmGNdf /RcVCJWhb8a4Z708WmmoxrSr0rLAqYbyBQ++KoBoX3PLTxPDZX/ATNB+eDk9d6RI+xOt 7ITaEYKwnx3xHuTvLjZS+5/3we3/mCg5wMhqy6jMzkeSk0VENs10bG48V+oySz5I4par 9bYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Rk4Yuj9GI6UIE2hgNfLDbnDh1l8wksnTp158bl3zXSc=; b=yLYghzma3JsjVkLiz7HP22BTks7vY8RkBejA+8fqP+mlyzORFr/J2zjPpPY3yUIcrZ oq6ib8+OLNhlNwJfSMoFYRk096B5Ji8fEwwUu1WjD78iTQ5racvfbWmRTCXIZhdH5V74 lj7pRPZyLn853d0xxGDLBr5L6i6IythThMN0VYsDsE6x5ocxR+NVL05BlmMTyXqMVyB5 E7/G8OSK+3RMobXGsw3RNbg7R2QzpUVubzFpk9ocmO0TysAfL1hxAkAOFvhQbl3LX0tP ZV1u3Gu3wfxm/mXqNOXMBItepIfMWNB09tK9i9QRHLTh++ADc7l5uAqlG3b411sXc8Ru 0IYA== X-Gm-Message-State: AFqh2kqcP+O1QbE8zYDXuetQDT6gx4G6McX/BC1j6g5UYRdpzJNttRTy 0tveYs+ys1eNa34UbZlGrLnCOg== X-Google-Smtp-Source: AMrXdXsFjoz00AgH9TIWqwkZuGatKxLiS7uOo9FgY+fRilu9KfZYXmhBSNT9y0HW0aYsx03+Ir8NXg== X-Received: by 2002:a17:902:d512:b0:192:fd1e:a968 with SMTP id b18-20020a170902d51200b00192fd1ea968mr9510211plg.46.1674031114966; Wed, 18 Jan 2023 00:38:34 -0800 (PST) Received: from localhost ([122.172.81.45]) by smtp.gmail.com with ESMTPSA id y13-20020a17090322cd00b001896af10ca7sm350395plg.134.2023.01.18.00.38.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Jan 2023 00:38:34 -0800 (PST) From: Viresh Kumar To: "Rafael J. Wysocki" , Daniel Lezcano , Amit Kucheria , Zhang Rui , Yang Yingliang , Viresh Kumar Cc: linux-pm@vger.kernel.org, Vincent Guittot , Caleb Connolly , "Rafael J. Wysocki" , linux-kernel@vger.kernel.org Subject: [PATCH V4 1/3] thermal: core: call put_device() only after device_register() fails Date: Wed, 18 Jan 2023 14:08:24 +0530 Message-Id: X-Mailer: git-send-email 2.31.1.272.g89b43f80a514 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" put_device() shouldn't be called before a prior call to device_register(). __thermal_cooling_device_register() doesn't follow that properly and needs fixing. Also thermal_cooling_device_destroy_sysfs() is getting called unnecessarily on few error paths. Fix all this by placing the calls at the right place. Based on initial work done by Caleb Connolly. Fixes: 4748f9687caa ("thermal: core: fix some possible name leaks in error = paths") Fixes: c408b3d1d9bb ("thermal: Validate new state in cur_state_store()") Reported-by: Caleb Connolly Signed-off-by: Viresh Kumar Reviewed-by: Yang Yingliang Tested-by: Caleb Connolly Tested-by: Frank Rowand --- For v6.2-rc. V3->V4: - The first three versions were sent by Caleb. - The new version fixes the current bugs, without looking to optimize the code any further, which is done separately in the next two patches. drivers/thermal/thermal_core.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index f17ab2316dbd..77bd47d976a2 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -909,15 +909,20 @@ __thermal_cooling_device_register(struct device_node = *np, cdev->devdata =3D devdata; =20 ret =3D cdev->ops->get_max_state(cdev, &cdev->max_state); - if (ret) - goto out_kfree_type; + if (ret) { + kfree(cdev->type); + goto out_ida_remove; + } =20 thermal_cooling_device_setup_sysfs(cdev); + ret =3D dev_set_name(&cdev->device, "cooling_device%d", cdev->id); if (ret) { + kfree(cdev->type); thermal_cooling_device_destroy_sysfs(cdev); - goto out_kfree_type; + goto out_ida_remove; } + ret =3D device_register(&cdev->device); if (ret) goto out_kfree_type; @@ -943,6 +948,8 @@ __thermal_cooling_device_register(struct device_node *n= p, thermal_cooling_device_destroy_sysfs(cdev); kfree(cdev->type); put_device(&cdev->device); + + /* thermal_release() takes care of the rest */ cdev =3D NULL; out_ida_remove: ida_free(&thermal_cdev_ida, id); --=20 2.31.1.272.g89b43f80a514 From nobody Mon Apr 6 06:40:05 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D493BC004D4 for ; Wed, 18 Jan 2023 09:24:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229476AbjARJYE (ORCPT ); Wed, 18 Jan 2023 04:24:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230286AbjARJUl (ORCPT ); Wed, 18 Jan 2023 04:20:41 -0500 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C10FC7572A for ; Wed, 18 Jan 2023 00:38:38 -0800 (PST) Received: by mail-pf1-x435.google.com with SMTP id x4so21452041pfj.1 for ; Wed, 18 Jan 2023 00:38:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=id0JR6OCkU6vLjgrNgRIuw5EcxaUhOjb9e6uHVbO4ng=; b=w6Obwzd3CGGBvdmAWvcE8LFRen+uO0H92gJZLVS6ZqKIa06i2L4mg/83y+oTpIP9sV woX+pSIKfzgtRr2jj2eDz/bTS6yVX1HxRVvVnfbUZHHpoTxmTAWoucMRg7h9SuIg/hv8 An2ZMUJ7HC0Ha1SHW1/tKC5nwI1BEVfDNNfQU1mK+JYQIUp1Xv8I9dFuKgYjOvxN06Jq w9tWMN3Uzd8Eq0aW6Iq854BYR12pMSDZgAO28gFPgZrdI+XwetDFnLDVlfel6We/UR1S NiUPPa0YHthXx5UlaPzu5CnlXhBM/P8RK3Zdse6gB0ltxlvZdxvMfavGxDhX3/K0vvjj +Rcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=id0JR6OCkU6vLjgrNgRIuw5EcxaUhOjb9e6uHVbO4ng=; b=Rh+hHgh7NHZ/DwjzE8Js5zj8/Nu4oqzqibakmq2BiQ+jkoLZIgFKgc7eHWdxMXD6BB /1oi9CVMhrvf7LfJTftcdHSEg89oyDs7DSGHVW7whgKTGmhMnDcmBEZ8grVMx29mrVyE GxkL0bm9XvB+tUhG+WD8kU/LMqPov0WZ62VO+xZGGhvV52uyyMsQoNJQAnwCHH/rixT7 laB5kSnhRAKfLG92a33jcoVIX6LSKVZWcuT0uY40kDs7jZqfLTwOHXcROMSf2bK5BGKi LcXcB/U3WYg0jjuj5/Qnw8lFstHY/ztlzW/V23yeCW179giFSQc9eFad/z8KMo46nbST wsJg== X-Gm-Message-State: AFqh2kpUs63Wn9JEvzaZ7H2CaomyOwTh7EbCZeV5yQlU9Qzj9o8ywedG kgmIg/tUPQltdnCUAGN3dZGeBg== X-Google-Smtp-Source: AMrXdXsaR0p9XFpJdxoiAqHT2u53w+g7DgHhCblPR0lWQS5kewzQSvMuA5WNmvvwWNF6y845T0pSlg== X-Received: by 2002:a05:6a00:26cc:b0:589:62bd:14d with SMTP id p12-20020a056a0026cc00b0058962bd014dmr5988623pfw.1.1674031118270; Wed, 18 Jan 2023 00:38:38 -0800 (PST) Received: from localhost ([122.172.81.45]) by smtp.gmail.com with ESMTPSA id i70-20020a628749000000b005815837164fsm6044666pfe.59.2023.01.18.00.38.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Jan 2023 00:38:37 -0800 (PST) From: Viresh Kumar To: "Rafael J. Wysocki" , Daniel Lezcano , Amit Kucheria , Zhang Rui Cc: Viresh Kumar , linux-pm@vger.kernel.org, Vincent Guittot , Caleb Connolly , Yang Yingliang , linux-kernel@vger.kernel.org Subject: [PATCH 2/3] thermal: core: Move cdev cleanup to thermal_release() Date: Wed, 18 Jan 2023 14:08:25 +0530 Message-Id: X-Mailer: git-send-email 2.31.1.272.g89b43f80a514 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" thermal_release() already frees cdev, let it do rest of the cleanup as well in order to simplify the error paths in __thermal_cooling_device_register(). Signed-off-by: Viresh Kumar Reviewed-by: Yang Yingliang --- drivers/thermal/thermal_core.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 77bd47d976a2..1fb109a97ff6 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -774,6 +774,9 @@ static void thermal_release(struct device *dev) } else if (!strncmp(dev_name(dev), "cooling_device", sizeof("cooling_device") - 1)) { cdev =3D to_cooling_device(dev); + thermal_cooling_device_destroy_sysfs(cdev); + kfree(cdev->type); + ida_free(&thermal_cdev_ida, cdev->id); kfree(cdev); } } @@ -909,23 +912,21 @@ __thermal_cooling_device_register(struct device_node = *np, cdev->devdata =3D devdata; =20 ret =3D cdev->ops->get_max_state(cdev, &cdev->max_state); - if (ret) { - kfree(cdev->type); - goto out_ida_remove; - } + if (ret) + goto out_cdev_type; =20 thermal_cooling_device_setup_sysfs(cdev); =20 ret =3D dev_set_name(&cdev->device, "cooling_device%d", cdev->id); - if (ret) { - kfree(cdev->type); - thermal_cooling_device_destroy_sysfs(cdev); - goto out_ida_remove; - } + if (ret) + goto out_cooling_dev; =20 ret =3D device_register(&cdev->device); - if (ret) - goto out_kfree_type; + if (ret) { + /* thermal_release() handles rest of the cleanup */ + put_device(&cdev->device); + return ERR_PTR(ret); + } =20 /* Add 'this' new cdev to the global cdev list */ mutex_lock(&thermal_list_lock); @@ -944,13 +945,10 @@ __thermal_cooling_device_register(struct device_node = *np, =20 return cdev; =20 -out_kfree_type: +out_cooling_dev: thermal_cooling_device_destroy_sysfs(cdev); +out_cdev_type: kfree(cdev->type); - put_device(&cdev->device); - - /* thermal_release() takes care of the rest */ - cdev =3D NULL; out_ida_remove: ida_free(&thermal_cdev_ida, id); out_kfree_cdev: @@ -1111,10 +1109,7 @@ void thermal_cooling_device_unregister(struct therma= l_cooling_device *cdev) =20 mutex_unlock(&thermal_list_lock); =20 - ida_free(&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); --=20 2.31.1.272.g89b43f80a514 From nobody Mon Apr 6 06:40:05 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0051C004D4 for ; Wed, 18 Jan 2023 09:24:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230143AbjARJYr (ORCPT ); Wed, 18 Jan 2023 04:24:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230294AbjARJUm (ORCPT ); Wed, 18 Jan 2023 04:20:42 -0500 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD45775A39 for ; Wed, 18 Jan 2023 00:38:41 -0800 (PST) Received: by mail-pj1-x102f.google.com with SMTP id x2-20020a17090a46c200b002295ca9855aso1616322pjg.2 for ; Wed, 18 Jan 2023 00:38:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=F8AmeTIEVUx58Wbd0penu0pGx5Sd8zIQKSjU9tMK1s0=; b=fcxxGLXsEhMz1dXR+sW8vGv8hQhFw2nTQq3bG8GW3oPm2WHJOcpNSJCfv6N/Pd5EfF gQ7D6TVNnPHDZy2ZAFJBUkDYUA4OV3g2boYlEUnb7XmEpc4oLQb8+I2u3tCJwcyxdskl LFYSh8C9+77hbu/5JRCd78RbnRRkWNWLsYefs5nv4hvgIEW/BDKra5AhbQjCuaG4aFLe NusRmWiND9VbcJsssJtE/p6ieuXjGVCNEyZNFuZzGcGYFZLKZgD9INuvtCeK3M0ZBQRQ zL2nx0F2yUhh5Ek4w13rYMU2yBKI3aoK/d2v/f9wvCsYlhZw/iWHpkiyYuzXbrZrNVYS n6Dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=F8AmeTIEVUx58Wbd0penu0pGx5Sd8zIQKSjU9tMK1s0=; b=ZKSwUoWw38w7z+R1pj81p/EU7mF/XwMJjo+hTZCiOaU8tOj37jStpqToACHX9FpgqU GePzfszs7JrAlIxHvFtvL2ESo1f+A/zIwDdQUCFLMuavgZiAVwvT5mqFpmZ8fCuQP+KU 7ajnhKs4HpC3e+6nbJmGjdATsBDc/j5bTpz7E20xWiQYwciQymCocm0KXVuWDyvtylur sN8+WpZekiFOrwI7kA2uFYdRGXo7gHiUNj93xYAgl/HpNiwZAwVw9CeQWEYZmVYKWOSr SwTCLbcJUVA0O37Tcj/l2jcVLS47d2ork5b0HpNchK/K2+jF5zKxw2zfgXCON9dOAvQn Qu+w== X-Gm-Message-State: AFqh2ko2UdnQGlJC/b/YZUA4r+jn2zG7S01Xy3s3b3SGdmB3FwDR9ek0 zlM/tJzFPytBDSjM8sAgl2P37Q== X-Google-Smtp-Source: AMrXdXtPjHYpN5DQbvFwLPlpgG7s1EXXuufzxx093G1OB5KnHQTwUfhTgapAMC1qsnVH4q63TuGbfQ== X-Received: by 2002:a05:6a20:ba90:b0:a6:7529:7c99 with SMTP id fb16-20020a056a20ba9000b000a675297c99mr6357612pzb.5.1674031121455; Wed, 18 Jan 2023 00:38:41 -0800 (PST) Received: from localhost ([122.172.81.45]) by smtp.gmail.com with ESMTPSA id a20-20020aa794b4000000b0058bb0b8750csm9713730pfl.88.2023.01.18.00.38.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Jan 2023 00:38:40 -0800 (PST) From: Viresh Kumar To: "Rafael J. Wysocki" , Daniel Lezcano , Amit Kucheria , Zhang Rui Cc: Viresh Kumar , linux-pm@vger.kernel.org, Vincent Guittot , Caleb Connolly , Yang Yingliang , linux-kernel@vger.kernel.org Subject: [PATCH 3/3] thermal: core: Use device_unregister() instead of device_del/put() Date: Wed, 18 Jan 2023 14:08:26 +0530 Message-Id: X-Mailer: git-send-email 2.31.1.272.g89b43f80a514 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Lets not open code device_unregister() unnecessarily. Signed-off-by: Viresh Kumar Reviewed-by: Yang Yingliang --- drivers/thermal/thermal_core.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 1fb109a97ff6..9fb37c5d9c4f 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -1109,8 +1109,7 @@ void thermal_cooling_device_unregister(struct thermal= _cooling_device *cdev) =20 mutex_unlock(&thermal_list_lock); =20 - device_del(&cdev->device); - put_device(&cdev->device); + device_unregister(&cdev->device); } EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); =20 --=20 2.31.1.272.g89b43f80a514