Message ID | 20230118211123.111493-3-daniel.lezcano@linaro.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2566368wrn; Wed, 18 Jan 2023 13:14:35 -0800 (PST) X-Google-Smtp-Source: AMrXdXuU0soLKCcczJbh7+WkTMkUKEPo8pJ+AAnE77zFA+zIwVmq0csxqULJvigm4g49K28LnlEt X-Received: by 2002:a17:906:a28c:b0:7c1:540c:e214 with SMTP id i12-20020a170906a28c00b007c1540ce214mr4499617ejz.47.1674076475124; Wed, 18 Jan 2023 13:14:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674076475; cv=none; d=google.com; s=arc-20160816; b=fmNiY1wr3wOJdIqjYXT1cQjEZS8GzxKNUEMzbVTDaqETASzP7JEmqiz3rkM9CCpO49 8DPbZWscb4LOdKx/ThibOYwdpFCJfpCjhbbF7W6d3P4BI0U3LCS9YcMPCxJ8x7lq22Sg WMwrAfyzGJaNC62ZrjOA+Xtmih+KVBggK70wujn7g7u6Y/UkzrjU/0KOKUUmehWG58tf biv+j/KtvJCseHHeNfMKUDT9eAf/mZEMgzb2LWw+ZqPqQcqe8Y1TpC0wtVTuPMiW9W6f yCTl7Y5dJyeJNQe2webyp5veaBXpP2SFBOUD+L2ZjdaoAF+2KoY0HDQ3BC4VxDP4bnFv aS3g== 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 :dkim-signature; bh=mv1QMfQ8+R0tx+YAwMOp8de+sCeOb8qyTorZfg+2pN8=; b=U3TfS/ywOTFa4cEeq18greIHZyetrbR4WONE6GUCk5i7QXo3poMlk5wjHms6zzwDUM 5NrLLYeiAgDHfuJWac7YoWy1A9Oru0UHr/EACFiJnN2oEeZuzkqOsfSW0lroDPY5i/RT PmYPz5KHS7TvbK9hpDVsisRBEtGSE8TvjpyyWCSCfHgY9eVDZrJxPlzRdJxPpZgEElU/ AuwNWXHJBiumRGNAV1MkafA7vPZvDTkHjy/AiJPEN78fJ0OdeYZ266hrs0e2MDNcr9PS WRfkegeCz0qBxcb8FSf8d4TGWr+fWkVvrBidA2V9RAVSjmcs1nw740TqBOOkko6gb5nt KpBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=k3WuuZ1F; 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=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id wu2-20020a170906eec200b0078e1d213831si36257463ejb.122.2023.01.18.13.14.09; Wed, 18 Jan 2023 13:14:35 -0800 (PST) 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=@linaro.org header.s=google header.b=k3WuuZ1F; 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=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230477AbjARVMq (ORCPT <rfc822;pfffrao@gmail.com> + 99 others); Wed, 18 Jan 2023 16:12:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230443AbjARVMU (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 18 Jan 2023 16:12:20 -0500 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF76E611EA for <linux-kernel@vger.kernel.org>; Wed, 18 Jan 2023 13:11:34 -0800 (PST) Received: by mail-wm1-x330.google.com with SMTP id g10so130171wmo.1 for <linux-kernel@vger.kernel.org>; Wed, 18 Jan 2023 13:11:34 -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=mv1QMfQ8+R0tx+YAwMOp8de+sCeOb8qyTorZfg+2pN8=; b=k3WuuZ1FdgzBGZGuHPHyrzGbvPMvuhO1xbq16DV12VumlZUanpXElBkLHbk1HgCpb4 aFrLqQWwlzFyRm+M+Qx/60pZpfyzcU0Y4FXOUMT69WH+Zyfmy7JzETS8/6KQjLjMjya4 XZAIpvHFciPB4c64lEGldGgwAGcTX+/fOYWm/dljxxi85sIixRnIOqPsG3WrcOOoVR8+ b1kZyGVe0UQszOg9JNtyyJl3bjU0T9gvIlH8mHfrQxrbVtqUvo9/n8AbRRFS2AM7VrIG SvDBw4S1ziev2Ikm8/45YhUS3pVxs/mjU/97pm22BoPfUgpOqpbEq4t/Dct6t3aM5ncu i0IQ== 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=mv1QMfQ8+R0tx+YAwMOp8de+sCeOb8qyTorZfg+2pN8=; b=SzBSY5GCtDPIQ0knD6geDsgYA+up+H/p0bIItXg6PI46pG00MNzn4b2mmSad4qEs6U k6qBj4VX+kU3Atohnk9UxsRrPv/Ietfc0/tH1EC6KKDxDAeneZI84H3MuujyqNbTM0YA LYkOtgU/owHV5w/Q0cBDBNYcJFIrCmkz2I/8MBXB+0oDN9BxqBfhGjoIMGM+RdBBi1YO v2NI+cSWRs7FsHmZDF72Flels+cZ7AknQe8h8dzptGTNWoBdysQB8RAyhx2ZsiM98EK8 feryTk7aOgGkXLFBpYpt+CUoKxQ/mNdlISj5BwIqHK9EI3/YFfJmLp7slcAx/a4qtOJz nEEg== X-Gm-Message-State: AFqh2krHEQ+JIcgWHJ7oK0mp9d08omRo6iNYPSqypaR9WY3BKOyCm3SL BnSmziP4CmXcbjdZ5+t9rUDqOA== X-Received: by 2002:a05:600c:4f8d:b0:3d3:3d1b:6354 with SMTP id n13-20020a05600c4f8d00b003d33d1b6354mr4129660wmq.3.1674076293181; Wed, 18 Jan 2023 13:11:33 -0800 (PST) Received: from mai.. (146725694.box.freepro.com. [130.180.211.218]) by smtp.gmail.com with ESMTPSA id l27-20020a05600c2cdb00b003d99da8d30asm3198835wmc.46.2023.01.18.13.11.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Jan 2023 13:11:32 -0800 (PST) From: Daniel Lezcano <daniel.lezcano@linaro.org> To: daniel.lezcano@linaro.org, rafael@kernel.org Cc: linux-pm@vger.kernel.org, Amit Kucheria <amitk@kernel.org>, Zhang Rui <rui.zhang@intel.com>, linux-kernel@vger.kernel.org (open list) Subject: [PATCH 3/5] thermal/core: Remove unneeded mutex_destroy() Date: Wed, 18 Jan 2023 22:11:21 +0100 Message-Id: <20230118211123.111493-3-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230118211123.111493-1-daniel.lezcano@linaro.org> References: <20230118211123.111493-1-daniel.lezcano@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=1.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: * X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755396414215086331?= X-GMAIL-MSGID: =?utf-8?q?1755396414215086331?= |
Series |
[1/5] thermal/core: Fix unregistering netlink at thermal init time
|
|
Commit Message
Daniel Lezcano
Jan. 18, 2023, 9:11 p.m. UTC
If the thermal framework fails to initialize, the mutex can be used by
the different functions registering a thermal zone anyway. We should
not destroy the mutexes as other components may use them.
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
---
drivers/thermal/thermal_core.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
Comments
On Wed, 2023-01-18 at 22:11 +0100, Daniel Lezcano wrote: > If the thermal framework fails to initialize, the mutex can be used > by > the different functions registering a thermal zone anyway. Hmm, even with no governors and unregistered thermal sysfs class? IMO, thermal APIs for registering a thermal_zone/cooling_device should yield early if thermal_init fails. For other APIs that relies on a valid thermal_zone_device/thermal_cooling_device pointer, nothing needs to be changed. what do you think? thanks, rui > We should > not destroy the mutexes as other components may use them. > > Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org> > --- > drivers/thermal/thermal_core.c | 6 ++---- > 1 file changed, 2 insertions(+), 4 deletions(-) > > diff --git a/drivers/thermal/thermal_core.c > b/drivers/thermal/thermal_core.c > index fad0c4a07d16..ea78c93277be 100644 > --- a/drivers/thermal/thermal_core.c > +++ b/drivers/thermal/thermal_core.c > @@ -1602,7 +1602,7 @@ static int __init thermal_init(void) > > result = thermal_netlink_init(); > if (result) > - goto error; > + return result; > > result = thermal_register_governors(); > if (result) > @@ -1623,9 +1623,7 @@ static int __init thermal_init(void) > thermal_unregister_governors(); > unregister_netlink: > thermal_netlink_exit(); > -error: > - mutex_destroy(&thermal_list_lock); > - mutex_destroy(&thermal_governor_lock); > + > return result; > } > postcore_initcall(thermal_init);
On 19/01/2023 08:41, Zhang, Rui wrote: > On Wed, 2023-01-18 at 22:11 +0100, Daniel Lezcano wrote: >> If the thermal framework fails to initialize, the mutex can be used >> by >> the different functions registering a thermal zone anyway. > > Hmm, even with no governors and unregistered thermal sysfs class? > > IMO, thermal APIs for registering a thermal_zone/cooling_device should > yield early if thermal_init fails. > For other APIs that relies on a valid > thermal_zone_device/thermal_cooling_device pointer, nothing needs to > be changed. > > what do you think? I think you are right. It would be nice if we can check if the thermal class is registered and bail out if not. But there is no function to check that AFAICS. Alternatively we can convert the thermal class static structure to a pointer and set it to NULL in case of error in thermal_init() ?
On Thu, Jan 19, 2023 at 10:30 AM Daniel Lezcano <daniel.lezcano@linaro.org> wrote: > > On 19/01/2023 08:41, Zhang, Rui wrote: > > On Wed, 2023-01-18 at 22:11 +0100, Daniel Lezcano wrote: > >> If the thermal framework fails to initialize, the mutex can be used > >> by > >> the different functions registering a thermal zone anyway. > > > > Hmm, even with no governors and unregistered thermal sysfs class? > > > > IMO, thermal APIs for registering a thermal_zone/cooling_device should > > yield early if thermal_init fails. > > For other APIs that relies on a valid > > thermal_zone_device/thermal_cooling_device pointer, nothing needs to > > be changed. > > > > what do you think? > > I think you are right. > > It would be nice if we can check if the thermal class is registered and > bail out if not. But there is no function to check that AFAICS. > > Alternatively we can convert the thermal class static structure to a > pointer and set it to NULL in case of error in thermal_init() ? It doesn't matter if this is a NULL pointer or a static object that's clearly marked as unused.
On 19/01/2023 13:11, Rafael J. Wysocki wrote: > On Thu, Jan 19, 2023 at 10:30 AM Daniel Lezcano > <daniel.lezcano@linaro.org> wrote: >> >> On 19/01/2023 08:41, Zhang, Rui wrote: >>> On Wed, 2023-01-18 at 22:11 +0100, Daniel Lezcano wrote: >>>> If the thermal framework fails to initialize, the mutex can be used >>>> by >>>> the different functions registering a thermal zone anyway. >>> >>> Hmm, even with no governors and unregistered thermal sysfs class? >>> >>> IMO, thermal APIs for registering a thermal_zone/cooling_device should >>> yield early if thermal_init fails. >>> For other APIs that relies on a valid >>> thermal_zone_device/thermal_cooling_device pointer, nothing needs to >>> be changed. >>> >>> what do you think? >> >> I think you are right. >> >> It would be nice if we can check if the thermal class is registered and >> bail out if not. But there is no function to check that AFAICS. >> >> Alternatively we can convert the thermal class static structure to a >> pointer and set it to NULL in case of error in thermal_init() ? > > It doesn't matter if this is a NULL pointer or a static object that's > clearly marked as unused. Without introducing another global variable, is it possible to know if the class is used or not ?
On Thu, Jan 19, 2023 at 1:48 PM Daniel Lezcano <daniel.lezcano@linaro.org> wrote: > > On 19/01/2023 13:11, Rafael J. Wysocki wrote: > > On Thu, Jan 19, 2023 at 10:30 AM Daniel Lezcano > > <daniel.lezcano@linaro.org> wrote: > >> > >> On 19/01/2023 08:41, Zhang, Rui wrote: > >>> On Wed, 2023-01-18 at 22:11 +0100, Daniel Lezcano wrote: > >>>> If the thermal framework fails to initialize, the mutex can be used > >>>> by > >>>> the different functions registering a thermal zone anyway. > >>> > >>> Hmm, even with no governors and unregistered thermal sysfs class? > >>> > >>> IMO, thermal APIs for registering a thermal_zone/cooling_device should > >>> yield early if thermal_init fails. > >>> For other APIs that relies on a valid > >>> thermal_zone_device/thermal_cooling_device pointer, nothing needs to > >>> be changed. > >>> > >>> what do you think? > >> > >> I think you are right. > >> > >> It would be nice if we can check if the thermal class is registered and > >> bail out if not. But there is no function to check that AFAICS. > >> > >> Alternatively we can convert the thermal class static structure to a > >> pointer and set it to NULL in case of error in thermal_init() ? > > > > It doesn't matter if this is a NULL pointer or a static object that's > > clearly marked as unused. > > Without introducing another global variable, is it possible to know if > the class is used or not ? If thermal_class.p is cleared to NULL on class_register() failures in thermal_init() (unfortunately, the driver core doesn't do that, but maybe it should - let me cut a patch for that), then it can be used for that.
On 19/01/2023 14:24, Rafael J. Wysocki wrote: > On Thu, Jan 19, 2023 at 1:48 PM Daniel Lezcano > <daniel.lezcano@linaro.org> wrote: >> >> On 19/01/2023 13:11, Rafael J. Wysocki wrote: >>> On Thu, Jan 19, 2023 at 10:30 AM Daniel Lezcano >>> <daniel.lezcano@linaro.org> wrote: >>>> >>>> On 19/01/2023 08:41, Zhang, Rui wrote: >>>>> On Wed, 2023-01-18 at 22:11 +0100, Daniel Lezcano wrote: >>>>>> If the thermal framework fails to initialize, the mutex can be used >>>>>> by >>>>>> the different functions registering a thermal zone anyway. >>>>> >>>>> Hmm, even with no governors and unregistered thermal sysfs class? >>>>> >>>>> IMO, thermal APIs for registering a thermal_zone/cooling_device should >>>>> yield early if thermal_init fails. >>>>> For other APIs that relies on a valid >>>>> thermal_zone_device/thermal_cooling_device pointer, nothing needs to >>>>> be changed. >>>>> >>>>> what do you think? >>>> >>>> I think you are right. >>>> >>>> It would be nice if we can check if the thermal class is registered and >>>> bail out if not. But there is no function to check that AFAICS. >>>> >>>> Alternatively we can convert the thermal class static structure to a >>>> pointer and set it to NULL in case of error in thermal_init() ? >>> >>> It doesn't matter if this is a NULL pointer or a static object that's >>> clearly marked as unused. >> >> Without introducing another global variable, is it possible to know if >> the class is used or not ? > > If thermal_class.p is cleared to NULL on class_register() failures in > thermal_init() (unfortunately, the driver core doesn't do that, but > maybe it should - let me cut a patch for that), then it can be used > for that. It should be in class_unregister() too, right ? And is it possible to add a class_is_registered() ? in order to prevent accessing class structure internals ?
On Thu, Jan 19, 2023 at 3:13 PM Daniel Lezcano <daniel.lezcano@linaro.org> wrote: > > On 19/01/2023 14:24, Rafael J. Wysocki wrote: > > On Thu, Jan 19, 2023 at 1:48 PM Daniel Lezcano > > <daniel.lezcano@linaro.org> wrote: > >> > >> On 19/01/2023 13:11, Rafael J. Wysocki wrote: > >>> On Thu, Jan 19, 2023 at 10:30 AM Daniel Lezcano > >>> <daniel.lezcano@linaro.org> wrote: > >>>> > >>>> On 19/01/2023 08:41, Zhang, Rui wrote: > >>>>> On Wed, 2023-01-18 at 22:11 +0100, Daniel Lezcano wrote: > >>>>>> If the thermal framework fails to initialize, the mutex can be used > >>>>>> by > >>>>>> the different functions registering a thermal zone anyway. > >>>>> > >>>>> Hmm, even with no governors and unregistered thermal sysfs class? > >>>>> > >>>>> IMO, thermal APIs for registering a thermal_zone/cooling_device should > >>>>> yield early if thermal_init fails. > >>>>> For other APIs that relies on a valid > >>>>> thermal_zone_device/thermal_cooling_device pointer, nothing needs to > >>>>> be changed. > >>>>> > >>>>> what do you think? > >>>> > >>>> I think you are right. > >>>> > >>>> It would be nice if we can check if the thermal class is registered and > >>>> bail out if not. But there is no function to check that AFAICS. > >>>> > >>>> Alternatively we can convert the thermal class static structure to a > >>>> pointer and set it to NULL in case of error in thermal_init() ? > >>> > >>> It doesn't matter if this is a NULL pointer or a static object that's > >>> clearly marked as unused. > >> > >> Without introducing another global variable, is it possible to know if > >> the class is used or not ? > > > > If thermal_class.p is cleared to NULL on class_register() failures in > > thermal_init() (unfortunately, the driver core doesn't do that, but > > maybe it should - let me cut a patch for that), then it can be used > > for that. > > It should be in class_unregister() too, right ? > > And is it possible to add a class_is_registered() ? in order to prevent > accessing class structure internals ? I suppose so. And we'd like it to be used some places like thermal_zone_device_register_with_trips(), wouldn't we?
On 19/01/2023 16:05, Rafael J. Wysocki wrote: > On Thu, Jan 19, 2023 at 3:13 PM Daniel Lezcano > <daniel.lezcano@linaro.org> wrote: >> >> On 19/01/2023 14:24, Rafael J. Wysocki wrote: >>> On Thu, Jan 19, 2023 at 1:48 PM Daniel Lezcano >>> <daniel.lezcano@linaro.org> wrote: >>>> >>>> On 19/01/2023 13:11, Rafael J. Wysocki wrote: >>>>> On Thu, Jan 19, 2023 at 10:30 AM Daniel Lezcano >>>>> <daniel.lezcano@linaro.org> wrote: >>>>>> >>>>>> On 19/01/2023 08:41, Zhang, Rui wrote: >>>>>>> On Wed, 2023-01-18 at 22:11 +0100, Daniel Lezcano wrote: >>>>>>>> If the thermal framework fails to initialize, the mutex can be used >>>>>>>> by >>>>>>>> the different functions registering a thermal zone anyway. >>>>>>> >>>>>>> Hmm, even with no governors and unregistered thermal sysfs class? >>>>>>> >>>>>>> IMO, thermal APIs for registering a thermal_zone/cooling_device should >>>>>>> yield early if thermal_init fails. >>>>>>> For other APIs that relies on a valid >>>>>>> thermal_zone_device/thermal_cooling_device pointer, nothing needs to >>>>>>> be changed. >>>>>>> >>>>>>> what do you think? >>>>>> >>>>>> I think you are right. >>>>>> >>>>>> It would be nice if we can check if the thermal class is registered and >>>>>> bail out if not. But there is no function to check that AFAICS. >>>>>> >>>>>> Alternatively we can convert the thermal class static structure to a >>>>>> pointer and set it to NULL in case of error in thermal_init() ? >>>>> >>>>> It doesn't matter if this is a NULL pointer or a static object that's >>>>> clearly marked as unused. >>>> >>>> Without introducing another global variable, is it possible to know if >>>> the class is used or not ? >>> >>> If thermal_class.p is cleared to NULL on class_register() failures in >>> thermal_init() (unfortunately, the driver core doesn't do that, but >>> maybe it should - let me cut a patch for that), then it can be used >>> for that. >> >> It should be in class_unregister() too, right ? >> >> And is it possible to add a class_is_registered() ? in order to prevent >> accessing class structure internals ? > > I suppose so. > > And we'd like it to be used some places like > thermal_zone_device_register_with_trips(), wouldn't we? Yes, in thermal_zone_device_register_with_trips() and thermal_cooling_device_register().
On Thursday, January 19, 2023 5:39:29 PM CET Daniel Lezcano wrote: > On 19/01/2023 16:05, Rafael J. Wysocki wrote: > > On Thu, Jan 19, 2023 at 3:13 PM Daniel Lezcano > > <daniel.lezcano@linaro.org> wrote: > >> > >> On 19/01/2023 14:24, Rafael J. Wysocki wrote: > >>> On Thu, Jan 19, 2023 at 1:48 PM Daniel Lezcano > >>> <daniel.lezcano@linaro.org> wrote: > >>>> > >>>> On 19/01/2023 13:11, Rafael J. Wysocki wrote: > >>>>> On Thu, Jan 19, 2023 at 10:30 AM Daniel Lezcano > >>>>> <daniel.lezcano@linaro.org> wrote: > >>>>>> > >>>>>> On 19/01/2023 08:41, Zhang, Rui wrote: > >>>>>>> On Wed, 2023-01-18 at 22:11 +0100, Daniel Lezcano wrote: > >>>>>>>> If the thermal framework fails to initialize, the mutex can be used > >>>>>>>> by > >>>>>>>> the different functions registering a thermal zone anyway. > >>>>>>> > >>>>>>> Hmm, even with no governors and unregistered thermal sysfs class? > >>>>>>> > >>>>>>> IMO, thermal APIs for registering a thermal_zone/cooling_device should > >>>>>>> yield early if thermal_init fails. > >>>>>>> For other APIs that relies on a valid > >>>>>>> thermal_zone_device/thermal_cooling_device pointer, nothing needs to > >>>>>>> be changed. > >>>>>>> > >>>>>>> what do you think? > >>>>>> > >>>>>> I think you are right. > >>>>>> > >>>>>> It would be nice if we can check if the thermal class is registered and > >>>>>> bail out if not. But there is no function to check that AFAICS. > >>>>>> > >>>>>> Alternatively we can convert the thermal class static structure to a > >>>>>> pointer and set it to NULL in case of error in thermal_init() ? > >>>>> > >>>>> It doesn't matter if this is a NULL pointer or a static object that's > >>>>> clearly marked as unused. > >>>> > >>>> Without introducing another global variable, is it possible to know if > >>>> the class is used or not ? > >>> > >>> If thermal_class.p is cleared to NULL on class_register() failures in > >>> thermal_init() (unfortunately, the driver core doesn't do that, but > >>> maybe it should - let me cut a patch for that), then it can be used > >>> for that. > >> > >> It should be in class_unregister() too, right ? > >> > >> And is it possible to add a class_is_registered() ? in order to prevent > >> accessing class structure internals ? > > > > I suppose so. > > > > And we'd like it to be used some places like > > thermal_zone_device_register_with_trips(), wouldn't we? > > Yes, in thermal_zone_device_register_with_trips() and > thermal_cooling_device_register(). Something like the patch below I think, because thermal_cooling_device_register() is a wrapper around thermal_zone_device_register_with_trips(). It needs to be split into 2 individual patches. --- drivers/base/class.c | 16 +++++++++++----- drivers/thermal/thermal_core.c | 3 +++ include/linux/device/class.h | 5 +++++ 3 files changed, 19 insertions(+), 5 deletions(-) Index: linux-pm/include/linux/device/class.h =================================================================== --- linux-pm.orig/include/linux/device/class.h +++ linux-pm/include/linux/device/class.h @@ -82,6 +82,11 @@ struct class_dev_iter { const struct device_type *type; }; +static inline bool class_is_registered(struct class *class) +{ + return !!class->p; +} + extern struct kobject *sysfs_dev_block_kobj; extern struct kobject *sysfs_dev_char_kobj; extern int __must_check __class_register(struct class *class, Index: linux-pm/drivers/base/class.c =================================================================== --- linux-pm.orig/drivers/base/class.c +++ linux-pm/drivers/base/class.c @@ -53,6 +53,8 @@ static void class_release(struct kobject pr_debug("class '%s': release.\n", class->name); + class->p = NULL; + if (class->class_release) class->class_release(class); else @@ -186,17 +188,21 @@ int __class_register(struct class *cls, cls->p = cp; error = kset_register(&cp->subsys); - if (error) { - kfree(cp); - return error; - } + if (error) + goto err_out; + error = class_add_groups(class_get(cls), cls->class_groups); class_put(cls); if (error) { kobject_del(&cp->subsys.kobj); kfree_const(cp->subsys.kobj.name); - kfree(cp); + goto err_out; } + return 0; + +err_out: + cls->p = NULL; + kfree(cp); return error; } EXPORT_SYMBOL_GPL(__class_register); Index: linux-pm/drivers/thermal/thermal_core.c =================================================================== --- linux-pm.orig/drivers/thermal/thermal_core.c +++ linux-pm/drivers/thermal/thermal_core.c @@ -1342,6 +1342,9 @@ thermal_zone_device_register_with_trips( if (num_trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp) && !trips) return ERR_PTR(-EINVAL); + if (!class_is_registered(&thermal_class)) + return ERR_PTR(-ENODEV); + tz = kzalloc(sizeof(*tz), GFP_KERNEL); if (!tz) return ERR_PTR(-ENOMEM);
On Thu, 2023-01-19 at 18:21 +0100, Rafael J. Wysocki wrote: > On Thursday, January 19, 2023 5:39:29 PM CET Daniel Lezcano wrote: > > On 19/01/2023 16:05, Rafael J. Wysocki wrote: > > > On Thu, Jan 19, 2023 at 3:13 PM Daniel Lezcano > > > <daniel.lezcano@linaro.org> wrote: > > > > On 19/01/2023 14:24, Rafael J. Wysocki wrote: > > > > > On Thu, Jan 19, 2023 at 1:48 PM Daniel Lezcano > > > > > <daniel.lezcano@linaro.org> wrote: > > > > > > On 19/01/2023 13:11, Rafael J. Wysocki wrote: > > > > > > > On Thu, Jan 19, 2023 at 10:30 AM Daniel Lezcano > > > > > > > <daniel.lezcano@linaro.org> wrote: > > > > > > > > On 19/01/2023 08:41, Zhang, Rui wrote: > > > > > > > > > On Wed, 2023-01-18 at 22:11 +0100, Daniel Lezcano > > > > > > > > > wrote: > > > > > > > > > > If the thermal framework fails to initialize, the > > > > > > > > > > mutex can be used > > > > > > > > > > by > > > > > > > > > > the different functions registering a thermal zone > > > > > > > > > > anyway. > > > > > > > > > > > > > > > > > > Hmm, even with no governors and unregistered thermal > > > > > > > > > sysfs class? > > > > > > > > > > > > > > > > > > IMO, thermal APIs for registering a > > > > > > > > > thermal_zone/cooling_device should > > > > > > > > > yield early if thermal_init fails. > > > > > > > > > For other APIs that relies on a valid > > > > > > > > > thermal_zone_device/thermal_cooling_device pointer, > > > > > > > > > nothing needs to > > > > > > > > > be changed. > > > > > > > > > > > > > > > > > > what do you think? > > > > > > > > > > > > > > > > I think you are right. > > > > > > > > > > > > > > > > It would be nice if we can check if the thermal class > > > > > > > > is registered and > > > > > > > > bail out if not. But there is no function to check that > > > > > > > > AFAICS. > > > > > > > > > > > > > > > > Alternatively we can convert the thermal class static > > > > > > > > structure to a > > > > > > > > pointer and set it to NULL in case of error in > > > > > > > > thermal_init() ? > > > > > > > > > > > > > > It doesn't matter if this is a NULL pointer or a static > > > > > > > object that's > > > > > > > clearly marked as unused. > > > > > > > > > > > > Without introducing another global variable, is it possible > > > > > > to know if > > > > > > the class is used or not ? > > > > > > > > > > If thermal_class.p is cleared to NULL on class_register() > > > > > failures in > > > > > thermal_init() (unfortunately, the driver core doesn't do > > > > > that, but > > > > > maybe it should - let me cut a patch for that), then it can > > > > > be used > > > > > for that. > > > > > > > > It should be in class_unregister() too, right ? > > > > > > > > And is it possible to add a class_is_registered() ? in order to > > > > prevent > > > > accessing class structure internals ? > > > > > > I suppose so. > > > > > > And we'd like it to be used some places like > > > thermal_zone_device_register_with_trips(), wouldn't we? > > > > Yes, in thermal_zone_device_register_with_trips() and > > thermal_cooling_device_register(). > > Something like the patch below I think, because > thermal_cooling_device_register() > is a wrapper around thermal_zone_device_register_with_trips(). > thermal_zone_device_register() is a wrapper around thermal_zone_device_register_with_trips(), but thermal_cooling_device_register() is not. :) thermal_cooling_device_register() registers a cooling device to thermal class so the class_is_registered() check is still needed. thanks, rui > It needs to be split into 2 individual patches. > > --- > drivers/base/class.c | 16 +++++++++++----- > drivers/thermal/thermal_core.c | 3 +++ > include/linux/device/class.h | 5 +++++ > 3 files changed, 19 insertions(+), 5 deletions(-) > > Index: linux-pm/include/linux/device/class.h > =================================================================== > --- linux-pm.orig/include/linux/device/class.h > +++ linux-pm/include/linux/device/class.h > @@ -82,6 +82,11 @@ struct class_dev_iter { > const struct device_type *type; > }; > > +static inline bool class_is_registered(struct class *class) > +{ > + return !!class->p; > +} > + > extern struct kobject *sysfs_dev_block_kobj; > extern struct kobject *sysfs_dev_char_kobj; > extern int __must_check __class_register(struct class *class, > Index: linux-pm/drivers/base/class.c > =================================================================== > --- linux-pm.orig/drivers/base/class.c > +++ linux-pm/drivers/base/class.c > @@ -53,6 +53,8 @@ static void class_release(struct kobject > > pr_debug("class '%s': release.\n", class->name); > > + class->p = NULL; > + > if (class->class_release) > class->class_release(class); > else > @@ -186,17 +188,21 @@ int __class_register(struct class *cls, > cls->p = cp; > > error = kset_register(&cp->subsys); > - if (error) { > - kfree(cp); > - return error; > - } > + if (error) > + goto err_out; > + > error = class_add_groups(class_get(cls), cls->class_groups); > class_put(cls); > if (error) { > kobject_del(&cp->subsys.kobj); > kfree_const(cp->subsys.kobj.name); > - kfree(cp); > + goto err_out; > } > + return 0; > + > +err_out: > + cls->p = NULL; > + kfree(cp); > return error; > } > EXPORT_SYMBOL_GPL(__class_register); > Index: linux-pm/drivers/thermal/thermal_core.c > =================================================================== > --- linux-pm.orig/drivers/thermal/thermal_core.c > +++ linux-pm/drivers/thermal/thermal_core.c > @@ -1342,6 +1342,9 @@ thermal_zone_device_register_with_trips( > if (num_trips > 0 && (!ops->get_trip_type || !ops- > >get_trip_temp) && !trips) > return ERR_PTR(-EINVAL); > > + if (!class_is_registered(&thermal_class)) > + return ERR_PTR(-ENODEV); > + > tz = kzalloc(sizeof(*tz), GFP_KERNEL); > if (!tz) > return ERR_PTR(-ENOMEM); > > >
On Fri, Jan 20, 2023 at 3:10 PM Zhang, Rui <rui.zhang@intel.com> wrote: > > On Thu, 2023-01-19 at 18:21 +0100, Rafael J. Wysocki wrote: > > On Thursday, January 19, 2023 5:39:29 PM CET Daniel Lezcano wrote: > > > On 19/01/2023 16:05, Rafael J. Wysocki wrote: > > > > On Thu, Jan 19, 2023 at 3:13 PM Daniel Lezcano > > > > <daniel.lezcano@linaro.org> wrote: > > > > > On 19/01/2023 14:24, Rafael J. Wysocki wrote: > > > > > > On Thu, Jan 19, 2023 at 1:48 PM Daniel Lezcano > > > > > > <daniel.lezcano@linaro.org> wrote: > > > > > > > On 19/01/2023 13:11, Rafael J. Wysocki wrote: > > > > > > > > On Thu, Jan 19, 2023 at 10:30 AM Daniel Lezcano > > > > > > > > <daniel.lezcano@linaro.org> wrote: > > > > > > > > > On 19/01/2023 08:41, Zhang, Rui wrote: > > > > > > > > > > On Wed, 2023-01-18 at 22:11 +0100, Daniel Lezcano > > > > > > > > > > wrote: > > > > > > > > > > > If the thermal framework fails to initialize, the > > > > > > > > > > > mutex can be used > > > > > > > > > > > by > > > > > > > > > > > the different functions registering a thermal zone > > > > > > > > > > > anyway. > > > > > > > > > > > > > > > > > > > > Hmm, even with no governors and unregistered thermal > > > > > > > > > > sysfs class? > > > > > > > > > > > > > > > > > > > > IMO, thermal APIs for registering a > > > > > > > > > > thermal_zone/cooling_device should > > > > > > > > > > yield early if thermal_init fails. > > > > > > > > > > For other APIs that relies on a valid > > > > > > > > > > thermal_zone_device/thermal_cooling_device pointer, > > > > > > > > > > nothing needs to > > > > > > > > > > be changed. > > > > > > > > > > > > > > > > > > > > what do you think? > > > > > > > > > > > > > > > > > > I think you are right. > > > > > > > > > > > > > > > > > > It would be nice if we can check if the thermal class > > > > > > > > > is registered and > > > > > > > > > bail out if not. But there is no function to check that > > > > > > > > > AFAICS. > > > > > > > > > > > > > > > > > > Alternatively we can convert the thermal class static > > > > > > > > > structure to a > > > > > > > > > pointer and set it to NULL in case of error in > > > > > > > > > thermal_init() ? > > > > > > > > > > > > > > > > It doesn't matter if this is a NULL pointer or a static > > > > > > > > object that's > > > > > > > > clearly marked as unused. > > > > > > > > > > > > > > Without introducing another global variable, is it possible > > > > > > > to know if > > > > > > > the class is used or not ? > > > > > > > > > > > > If thermal_class.p is cleared to NULL on class_register() > > > > > > failures in > > > > > > thermal_init() (unfortunately, the driver core doesn't do > > > > > > that, but > > > > > > maybe it should - let me cut a patch for that), then it can > > > > > > be used > > > > > > for that. > > > > > > > > > > It should be in class_unregister() too, right ? > > > > > > > > > > And is it possible to add a class_is_registered() ? in order to > > > > > prevent > > > > > accessing class structure internals ? > > > > > > > > I suppose so. > > > > > > > > And we'd like it to be used some places like > > > > thermal_zone_device_register_with_trips(), wouldn't we? > > > > > > Yes, in thermal_zone_device_register_with_trips() and > > > thermal_cooling_device_register(). > > > > Something like the patch below I think, because > > thermal_cooling_device_register() > > is a wrapper around thermal_zone_device_register_with_trips(). > > > > thermal_zone_device_register() is a wrapper around > thermal_zone_device_register_with_trips(), but > thermal_cooling_device_register() is not. :) > > thermal_cooling_device_register() registers a cooling device to thermal > class so the class_is_registered() check is still needed. OK, thanks!
diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index fad0c4a07d16..ea78c93277be 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -1602,7 +1602,7 @@ static int __init thermal_init(void) result = thermal_netlink_init(); if (result) - goto error; + return result; result = thermal_register_governors(); if (result) @@ -1623,9 +1623,7 @@ static int __init thermal_init(void) thermal_unregister_governors(); unregister_netlink: thermal_netlink_exit(); -error: - mutex_destroy(&thermal_list_lock); - mutex_destroy(&thermal_governor_lock); + return result; } postcore_initcall(thermal_init);