From patchwork Thu Nov 10 15:24:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 624381 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 48BFFC433FE for ; Thu, 10 Nov 2022 15:25:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231406AbiKJPZI (ORCPT ); Thu, 10 Nov 2022 10:25:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229746AbiKJPZG (ORCPT ); Thu, 10 Nov 2022 10:25:06 -0500 Received: from mail-ot1-x332.google.com (mail-ot1-x332.google.com [IPv6:2607:f8b0:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE08A317ED; Thu, 10 Nov 2022 07:25:05 -0800 (PST) Received: by mail-ot1-x332.google.com with SMTP id l42-20020a9d1b2d000000b0066c6366fbc3so1297887otl.3; Thu, 10 Nov 2022 07:25:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=bOYNmW9OZFQBi/Rb1c2Zeo5a8y5kFjUwBvSI7SdtFFM=; b=Ktcssiuad0WVHvYrFj71Mdm/+gqzgFmLM+S5ZMFNAMhEbOq9DTTjbg92CJ//Bxb26/ 2dCVdDLNycfy49CWd31iL0SdbhaNfjJAx2pEZrH/MSl1pmMdmKwKFJiUkqGILDUdD1Np edPC66QEWnmbkoUddHs1No70gFjly1xGoqHUxrHcPAV0/tTJ4EGkIl+X32wGLsJ1hVwJ bHNfdQLA9j4Be/HXQKlAaFzL++qG/18lngDfzXkftfm+ACw3sWRGHjMsUhu9QOVrNSgp R3K1unpLHQFN/vmFI1japm86hVx5GtoX2TVg2SkCYnR2Xj6Km6iTvi41YKq750vsNGco yh5A== 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:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=bOYNmW9OZFQBi/Rb1c2Zeo5a8y5kFjUwBvSI7SdtFFM=; b=cfrC79FpUIasQdTkyB/OKaQWbuvzbqdwm0G0vkLhHNjSGipUFsAVbRXkfX9IMBPbhf 7n2CAkKJ+APnzOztemnZ1zZ1v2RBSUye4Po+GW2iY/2IOFhqo0jsYp8KDHiukG1Te5uK pzOzcWzOESvozwxRb7TnUFB590Qvnx1JLxF9d8Ra/tdpHBnJvE/IkkL5/qGgxsbC0YcF AxK4A6GMjBH2QCBudaZXTQZDIUPKU3d1ll599hkZmaHmFI5IDhJtRZ2BYVslzW63RZqX UL6COloDPZcTjIOgt0XQsqdWI81NRyTQDxFaliXd7b4vq2nlWXfBaSZbJEqFH5Qr6EbY s8bw== X-Gm-Message-State: ACrzQf1udxPQGcVlh46VTVjCOP35I8/J4rwNQF665ketLwl+CjupZXUe C/VeYBabuFenSIqa0upGi/Q= X-Google-Smtp-Source: AMsMyM6WyHBjYYGa9Bjkvurtyyqqh3EZNeooWreZveV1eZpmyuNbmbpZbI5QVNm2QBEfmW7PTzS+yg== X-Received: by 2002:a05:6830:3982:b0:65c:46b7:bb63 with SMTP id bs2-20020a056830398200b0065c46b7bb63mr30738255otb.101.1668093905310; Thu, 10 Nov 2022 07:25:05 -0800 (PST) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id j24-20020a056808057800b00342ded07a75sm5793038oig.18.2022.11.10.07.25.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 07:25:05 -0800 (PST) Sender: Guenter Roeck From: Guenter Roeck To: "Rafael J . Wysocki" , Daniel Lezcano Cc: Amit Kucheria , Zhang Rui , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH v2 1/9] thermal/core: Destroy thermal zone device mutex in release function Date: Thu, 10 Nov 2022 07:24:52 -0800 Message-Id: <20221110152500.3032655-2-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221110152500.3032655-1-linux@roeck-us.net> References: <20221110152500.3032655-1-linux@roeck-us.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Accesses to thermal zones, and with it the thermal zone device mutex, are still possible after the thermal zone device has been unregistered. For example, thermal_zone_get_temp() can be called from temp_show() in thermal_sysfs.c if the sysfs attribute was opened before the thermal device was unregistered. Move the call to mutex_destroy from thermal_zone_device_unregister() to thermal_release() to ensure that it is only destroyed after it is guaranteed to be no longer accessed. Signed-off-by: Guenter Roeck --- v2: No change drivers/thermal/thermal_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 117eeaf7dd24..f548875a016d 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -759,6 +759,7 @@ static void thermal_release(struct device *dev) sizeof("thermal_zone") - 1)) { tz = to_thermal_zone(dev); thermal_zone_destroy_device_groups(tz); + mutex_destroy(&tz->lock); kfree(tz); } else if (!strncmp(dev_name(dev), "cooling_device", sizeof("cooling_device") - 1)) { @@ -1390,7 +1391,6 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz) thermal_remove_hwmon_sysfs(tz); ida_free(&thermal_tz_ida, tz->id); ida_destroy(&tz->ida); - mutex_destroy(&tz->lock); device_unregister(&tz->device); thermal_notify_tz_delete(tz_id); From patchwork Thu Nov 10 15:24:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 624380 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 65FABC43219 for ; Thu, 10 Nov 2022 15:25:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229622AbiKJPZJ (ORCPT ); Thu, 10 Nov 2022 10:25:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231414AbiKJPZI (ORCPT ); Thu, 10 Nov 2022 10:25:08 -0500 Received: from mail-oa1-x31.google.com (mail-oa1-x31.google.com [IPv6:2001:4860:4864:20::31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9ACD010B56; Thu, 10 Nov 2022 07:25:07 -0800 (PST) Received: by mail-oa1-x31.google.com with SMTP id 586e51a60fabf-13bd19c3b68so2491077fac.7; Thu, 10 Nov 2022 07:25:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=iSO/JGqHw5m1/Y8PZVH1KMpl9TGQLAVgPtR2Ov1K83Q=; b=hL0xDKbdkurOTH+ZJ8RqK9mu/W5/Nn3jy/R5bg31F2r2Yv6izlO+yxjcypm//piNHi f9LkoIlq67C9YYVHzxzyr/HaRR6a4DAFefa4AWDzp45bhkB31ZtUQjDmWDxlwcF9/L5j +hq38MWZl3/KDsuIpW+z3Uqi+3iMM8NTdi29RWP6AqRlWBJU6J+mqKySjblFlRBPCcte ZTPT1mUYPk+eH1CQS/cQOgZtqPXUTC/gy/o5mjcixvzh3T/TkMB0rbaBILu6xyYw3tsn IRUHErAeG2vubG07+oReAXhnRjRj5ROgVSTuRYqTXfF+mLkOCx0m/vta5DPwN0KApJJP aVUA== 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:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=iSO/JGqHw5m1/Y8PZVH1KMpl9TGQLAVgPtR2Ov1K83Q=; b=pQDPhyx5jb+EsTbvcx7Mt9OkWtkMUsQgtGeOTLZxrXWJpIh5DuWo0mbGdbkBfMEzA5 sORJscEDtHQQ6qmhptAiltd1OgabpmD4+rFTYwmJS3hc27X0AuK3qNA2Fpm92IEfDqCz 7+9lNXQW8bjSLr+uhq/wf6nF51aCHLhaG2aY/FJ9BqbXAUERqr3KF5wKaYQ0opKdFS4V gjKYYP2mCwktgG1BbfApjyZavtnZSBl5VhGY6V4FtG4xw3YdRsAPhHhWPOYdUwJkKWg2 TenM354OgVAqcApypy+OANNRPEPFxMXPaJbKSygmnLmoJ8HQ+eIHRGBUygBP6Evk5FHt WZtA== X-Gm-Message-State: ACrzQf2bzJrCzNkARkjtwajoHnSTevTlxiN+sTpvM3W/heHbsA/UHyai 2d6Oh7p2eTBB1vbohvYPGtU= X-Google-Smtp-Source: AMsMyM4qRuoL9gOLv0mThbhHZMTEUzxEYiaHkQY9MCDHpNdAj7jMESvjOo5Xeyn2epCVZsHSmm12Lg== X-Received: by 2002:a05:6870:aa0d:b0:13b:b765:4791 with SMTP id gv13-20020a056870aa0d00b0013bb7654791mr1528823oab.274.1668093906906; Thu, 10 Nov 2022 07:25:06 -0800 (PST) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id z6-20020a4a2246000000b0049eb2793516sm5271977ooe.44.2022.11.10.07.25.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 07:25:06 -0800 (PST) Sender: Guenter Roeck From: Guenter Roeck To: "Rafael J . Wysocki" , Daniel Lezcano Cc: Amit Kucheria , Zhang Rui , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH v2 2/9] thermal/core: Delete device under thermal device zone lock Date: Thu, 10 Nov 2022 07:24:53 -0800 Message-Id: <20221110152500.3032655-3-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221110152500.3032655-1-linux@roeck-us.net> References: <20221110152500.3032655-1-linux@roeck-us.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Thermal device attributes may still be opened after unregistering the thermal zone and deleting the thermal device. Currently there is no protection against accessing thermal device operations after unregistering a thermal zone. To enable adding such protection, protect the device delete operation with the thermal zone device mutex. This requires splitting the call to device_unregister() into its components, device_del() and put_device(). Only the first call can be executed under mutex protection, since put_device() may result in releasing the thermal zone device memory. Signed-off-by: Guenter Roeck --- v2: No change drivers/thermal/thermal_core.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index f548875a016d..562ece8d16aa 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -1391,7 +1391,12 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz) thermal_remove_hwmon_sysfs(tz); ida_free(&thermal_tz_ida, tz->id); ida_destroy(&tz->ida); - device_unregister(&tz->device); + + mutex_lock(&tz->lock); + device_del(&tz->device); + mutex_unlock(&tz->lock); + + put_device(&tz->device); thermal_notify_tz_delete(tz_id); } From patchwork Thu Nov 10 15:24:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 624379 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 A2545C4332F for ; Thu, 10 Nov 2022 15:25:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231439AbiKJPZO (ORCPT ); Thu, 10 Nov 2022 10:25:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229746AbiKJPZL (ORCPT ); Thu, 10 Nov 2022 10:25:11 -0500 Received: from mail-oa1-x36.google.com (mail-oa1-x36.google.com [IPv6:2001:4860:4864:20::36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F07CF2CCB2; Thu, 10 Nov 2022 07:25:10 -0800 (PST) Received: by mail-oa1-x36.google.com with SMTP id 586e51a60fabf-13b23e29e36so2484168fac.8; Thu, 10 Nov 2022 07:25:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=nPRDmNkrm1vn+V+1JEnLY9+b9B506P/EqjPsjoC9grA=; b=KKlnEPm2PbaBOKwE1hreqVjPeEksFU9/M3Q+oOqwhTimgksD4vs0scvu1vOcPsSSaV GrWRxwkb/nKRMPhjMl8YdlQaVZsPe5c6AAcfBZwi6Yg38A/qDMtYNQ0lRUZ3s0nuE/Sm lcx0UdiW4tJJNh9YhZns0/iSu2DNr59c5VWVXF+Gr1IElswx/TLXKJ/gdrcBqMVFKFDE ejDizxQHa9DY2P3LI8ywPLjOp6Q9TSVG7mY0pMohSFQbZJ/sHm9kIY0K9K7N0Gh8rNM9 YdcVmvQPen05DhLcjrR0fRadV0Q7rbzm5vBYlTO2E7plgaEJA4nilboEN35MD9E2ViPL tK+w== 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:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=nPRDmNkrm1vn+V+1JEnLY9+b9B506P/EqjPsjoC9grA=; b=1YlPk021bUnG0CwecgOs8RF50zHWOF3YQhwVEESpd0B2RLPMI2mr6W3VCw/Sh2K1xG 5yUx5eRRdtkp3FOfBzdO6J6cKcmeJlSEAE5A+ktv2k4zdARqtxvqczUij2ziWM1Mqyy0 M7CyLFu8EnFv2KSrClv/yAcfAs4cFfq2A9t3UMARQq0jyXb1PQilPol85gIrZA2H9pta G/Dh49EavRmln+oSQ/WjCJjtc1VGTDV/pifR2ipOZ5I2fb7DC5M3IQpEGc+/WQvgBcs9 djWoVu00XahjTDwRbjbpCpLViQorkAlkeJZlJdim4P4H9OwUivIp/bOAeeontsAVaBff MqAw== X-Gm-Message-State: ACrzQf2uIAlpQTnWlEJy6K8cUkazrsvzO0US8N7Rnro7SS4HW4ezTlz5 P9gqdBLE0fSdXd3n/XChD5k= X-Google-Smtp-Source: AMsMyM7c77/F8/pD3IV573ddlDZXkCtszUj2iM4SZiq4hbS00G38a5zhJVQ7IvR+nMQTMC1MAb/I4w== X-Received: by 2002:a05:6870:525:b0:130:9e35:137a with SMTP id j37-20020a056870052500b001309e35137amr37067612oao.88.1668093910340; Thu, 10 Nov 2022 07:25:10 -0800 (PST) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id r7-20020a056830418700b006618586b850sm6582808otu.46.2022.11.10.07.25.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 07:25:09 -0800 (PST) Sender: Guenter Roeck From: Guenter Roeck To: "Rafael J . Wysocki" , Daniel Lezcano Cc: Amit Kucheria , Zhang Rui , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH v2 4/9] thermal/core: Move parameter validation from __thermal_zone_get_temp to thermal_zone_get_temp Date: Thu, 10 Nov 2022 07:24:55 -0800 Message-Id: <20221110152500.3032655-5-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221110152500.3032655-1-linux@roeck-us.net> References: <20221110152500.3032655-1-linux@roeck-us.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org All callers of __thermal_zone_get_temp() already validated the thermal zone parameters. Move validation to thermal_zone_get_temp() where it is actually needed. Also add kernel documentation for __thermal_zone_get_temp(), listing the requirement that the function must be called with validated parameters and with thermal device mutex held. Signed-off-by: Guenter Roeck --- v2: Add note about added kernel documentation to description . Use IS_ERR_OR_NULL instead of manually coding it. drivers/thermal/thermal_helpers.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/drivers/thermal/thermal_helpers.c b/drivers/thermal/thermal_helpers.c index fca0b23570f9..321f8020082d 100644 --- a/drivers/thermal/thermal_helpers.c +++ b/drivers/thermal/thermal_helpers.c @@ -64,6 +64,20 @@ get_thermal_instance(struct thermal_zone_device *tz, } EXPORT_SYMBOL(get_thermal_instance); +/** + * __thermal_zone_get_temp() - returns the temperature of a thermal zone + * @tz: a valid pointer to a struct thermal_zone_device + * @temp: a valid pointer to where to store the resulting temperature. + * + * When a valid thermal zone reference is passed, it will fetch its + * temperature and fill @temp. + * + * Both tz and tz->ops must be valid pointers when calling this function, + * and the tz->ops->get_temp callback must be provided. + * The function must be called under tz->lock. + * + * Return: On success returns 0, an error code otherwise + */ int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) { int ret = -EINVAL; @@ -73,9 +87,6 @@ int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) lockdep_assert_held(&tz->lock); - if (!tz || IS_ERR(tz) || !tz->ops->get_temp) - return -EINVAL; - ret = tz->ops->get_temp(tz, temp); if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) { @@ -114,13 +125,22 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) { int ret; + if (IS_ERR_OR_NULL(tz)) + return -EINVAL; + mutex_lock(&tz->lock); + if (!tz->ops->get_temp) { + ret = -EINVAL; + goto unlock; + } + if (device_is_registered(&tz->device)) ret = __thermal_zone_get_temp(tz, temp); else ret = -ENODEV; +unlock: mutex_unlock(&tz->lock); return ret; From patchwork Thu Nov 10 15:24:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 624378 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 C6720C433FE for ; Thu, 10 Nov 2022 15:25:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231414AbiKJPZQ (ORCPT ); Thu, 10 Nov 2022 10:25:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231440AbiKJPZO (ORCPT ); Thu, 10 Nov 2022 10:25:14 -0500 Received: from mail-ot1-x32d.google.com (mail-ot1-x32d.google.com [IPv6:2607:f8b0:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2682D32BB4; Thu, 10 Nov 2022 07:25:14 -0800 (PST) Received: by mail-ot1-x32d.google.com with SMTP id a7-20020a056830008700b0066c82848060so1293505oto.4; Thu, 10 Nov 2022 07:25:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=TQ10nOb48x6GWqAJdDBvsYg0lrgcue29G6thFXet4SM=; b=n2DGCbHt+mR7onXVMjOXSsZgggtd7o3Re8HqCI0ocAutSYfuU2eFDDf8Jai77X/hxn teTHZFGKoRuKrAiSt0NUQembHFQBIwwvamDuu+cz/BmdGeJIckbnb8TA/6DingXhTTlM MhE2MDyqCdFrs4hDOkb28rceWfIbIvjrAkVUO8cVelv6jBI7IodWaYYai4AsD6DgTj6G a3uI5UmjlVdFNOTMf6evSEVYMzj/JVIUhqousDWQgSAyVNjVYH1+ULpMsgv+ypvDYUfs cO015nJazP/FFcsYojL8gECxMXlkrtvrf18vhySVJo08vqHvuLliF0dhS5Vb4M0H+9m3 T9MQ== 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:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=TQ10nOb48x6GWqAJdDBvsYg0lrgcue29G6thFXet4SM=; b=UBLqwP49ELiV7gOOrMmno05+OEFEfDEwdrw1m5BisgVAEuLoE/VcrhU9WX8oPjsW0a h7/JUeA+6yWHV4kaiLTjJdcBubfmMa2KI1VQ7pxzdw2cmdbhKkOJtcQO+kk2Ggt/2e/Y gtfWVfb07fxJvrptvUbeT+SsMAToPKxXDkPaj4EszgdqQmOwbqDXsqhcxNhcI/2mcomo Uqu7SnEdItvYEjdqXw2FZy16rbLgTwLIMtFzGWs0uXLIZ5PaTZZjYdZQiDDeso6whO9V Md9V+W9OclafYMi8A2UQOixP05fiXKlfu0ssYJhh48+6y5DGt8krpCQq5uDQ2FN6BYCs LxQA== X-Gm-Message-State: ACrzQf1yJADJ0PS0pIIxKFTS6O/GcK312dhcSKGnpKSdNGtw5HzbyttW t7yBj6RFShK/AqGM1kNI24xAc2+hmM8= X-Google-Smtp-Source: AMsMyM442yTxnWULQ5cboOlghbbZka7nh7aWZUlcO6geeTbFqr8HJBU/EsPbpHQylWpk+QrVCCIWNQ== X-Received: by 2002:a9d:4b19:0:b0:66c:55d2:c58a with SMTP id q25-20020a9d4b19000000b0066c55d2c58amr26740262otf.273.1668093913460; Thu, 10 Nov 2022 07:25:13 -0800 (PST) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id b20-20020a05687061d400b001324315bb6asm7626122oah.29.2022.11.10.07.25.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 07:25:13 -0800 (PST) Sender: Guenter Roeck From: Guenter Roeck To: "Rafael J . Wysocki" , Daniel Lezcano Cc: Amit Kucheria , Zhang Rui , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH v2 6/9] thermal/core: Protect hwmon accesses to thermal operations with thermal zone mutex Date: Thu, 10 Nov 2022 07:24:57 -0800 Message-Id: <20221110152500.3032655-7-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221110152500.3032655-1-linux@roeck-us.net> References: <20221110152500.3032655-1-linux@roeck-us.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org In preparation to protecting access to thermal operations against thermal zone device removal, protect hwmon accesses to thermal zone operations with the thermal zone mutex. After acquiring the mutex, ensure that the thermal zone device is registered before proceeding. Signed-off-by: Guenter Roeck --- v2: Simplify error handling to avoid additional goto. Do not use ternary operator in modified code. drivers/thermal/thermal_hwmon.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/thermal/thermal_hwmon.c b/drivers/thermal/thermal_hwmon.c index f53f4ceb6a5d..c594c42bea6d 100644 --- a/drivers/thermal/thermal_hwmon.c +++ b/drivers/thermal/thermal_hwmon.c @@ -77,7 +77,15 @@ temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf) int temperature; int ret; - ret = tz->ops->get_crit_temp(tz, &temperature); + mutex_lock(&tz->lock); + + if (device_is_registered(&tz->device)) + ret = tz->ops->get_crit_temp(tz, &temperature); + else + ret = -ENODEV; + + mutex_unlock(&tz->lock); + if (ret) return ret; From patchwork Thu Nov 10 15:24:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 624377 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 3F3B8C433FE for ; Thu, 10 Nov 2022 15:25:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231470AbiKJPZk (ORCPT ); Thu, 10 Nov 2022 10:25:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231504AbiKJPZd (ORCPT ); Thu, 10 Nov 2022 10:25:33 -0500 Received: from mail-ot1-x336.google.com (mail-ot1-x336.google.com [IPv6:2607:f8b0:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE8FD3E0B5; Thu, 10 Nov 2022 07:25:17 -0800 (PST) Received: by mail-ot1-x336.google.com with SMTP id 46-20020a9d0631000000b00666823da25fso1324917otn.0; Thu, 10 Nov 2022 07:25:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=GmjCJ069G5ZA0ck/PetkxKT25fldp2oH5LB83pWLxGg=; b=pq817FLfAn/Vc+Gbr+dri0aLEbm7xOXgkMHfcYwLZWDsOkLu6cXfGVRcxqTpN/DjPd kPgHYrUrrkMbgofm0bOpmslcKQjl27DfROxRDRPLmqszVVDoFXS5/9vlzwROSVEPFgOi T+XnIY8JxHd1NTEMuao9sVo4VwLBUw54N6YJ1UV7FQa9YkJuMYoX+O9bQBm1GwMPyCWi l4/YccSnq8KydeqH+40CIICdYCQQKfo7YWbTjpT/cM9mMRtKU7DHeSjuxTi9TUKwSjlj wp95dLPLLXDtv/b9uOKVyu5EZS2ZYhwP1z4zoCfApIBN7DwhbHYLgBDgnVG+hK0UjOJp XzZg== 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:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=GmjCJ069G5ZA0ck/PetkxKT25fldp2oH5LB83pWLxGg=; b=bAzYPUA5lXbeHvziD9Z3ACiEt9mkMkht+u+AV38HsN1++ofaSU7R8k3ICSqfWouLn9 UjHIiy300zCF3kFGZj6Igrv3bTced4eDPdhGQa9cI3wKviQjc0Dv6PaatAJF36HUgW3h pKDrspaT0hyn9QjBzjABo0xMno7PgSxxEXHXdZENIHkp11xHarBYNK22jg2LdSwXGcbc p4/dP5xYGiOvE2CO+W9u1jrRaF8ZYaohJrTzbXGQi294sCsQg7KAFM3Cg+uXjfxabDna m+SQsy1RGDFCvWMY7lqcFtd25zsm4Nij5+NY0iruXCKsD4Ag1CBxnZaDry6NBq0pfM2P Zsbw== X-Gm-Message-State: ACrzQf0ukJUhbB6SlsHUW2GE/rVw5Ituz6+wAvgwSEhJNIKePSMVEqM5 pJR9x+P7xxewnKvASDjkyto= X-Google-Smtp-Source: AMsMyM7Cpy8eu01F6zW9zK8OBu5c/uNTzlGJaHmX7bjgdoQWsygkrOlz0VJ6lDBiKrdfTuJmcR/vcA== X-Received: by 2002:a05:6830:12:b0:66c:74a0:beeb with SMTP id c18-20020a056830001200b0066c74a0beebmr20826692otp.70.1668093916976; Thu, 10 Nov 2022 07:25:16 -0800 (PST) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id f1-20020a056830204100b006619483182csm6454623otp.18.2022.11.10.07.25.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 07:25:16 -0800 (PST) Sender: Guenter Roeck From: Guenter Roeck To: "Rafael J . Wysocki" , Daniel Lezcano Cc: Amit Kucheria , Zhang Rui , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH v2 8/9] thermal/core: Remove thermal_zone_set_trips() Date: Thu, 10 Nov 2022 07:24:59 -0800 Message-Id: <20221110152500.3032655-9-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221110152500.3032655-1-linux@roeck-us.net> References: <20221110152500.3032655-1-linux@roeck-us.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Since no callers of thermal_zone_set_trips() are left, remove the function. Document __thermal_zone_set_trips() instead. Explicitly state that the thermal zone lock must be held when calling the function, and that the pointer to the thermal zone must be valid. Signed-off-by: Guenter Roeck --- v2: No change drivers/thermal/thermal_core.h | 1 - drivers/thermal/thermal_helpers.c | 34 ++++++++++++++----------------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h index 7b51b9a22e7e..b834cb273429 100644 --- a/drivers/thermal/thermal_core.h +++ b/drivers/thermal/thermal_core.h @@ -113,7 +113,6 @@ void __thermal_zone_device_update(struct thermal_zone_device *tz, enum thermal_notify_event event); /* Helpers */ -void thermal_zone_set_trips(struct thermal_zone_device *tz); void __thermal_zone_set_trips(struct thermal_zone_device *tz); int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp); diff --git a/drivers/thermal/thermal_helpers.c b/drivers/thermal/thermal_helpers.c index 321f8020082d..56aa2e88f34f 100644 --- a/drivers/thermal/thermal_helpers.c +++ b/drivers/thermal/thermal_helpers.c @@ -147,6 +147,21 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) } EXPORT_SYMBOL_GPL(thermal_zone_get_temp); +/** + * __thermal_zone_set_trips - Computes the next trip points for the driver + * @tz: a pointer to a thermal zone device structure + * + * The function computes the next temperature boundaries by browsing + * the trip points. The result is the closer low and high trip points + * to the current temperature. These values are passed to the backend + * driver to let it set its own notification mechanism (usually an + * interrupt). + * + * This function must be called with tz->lock held. Both tz and tz->ops + * must be valid pointers. + * + * It does not return a value + */ void __thermal_zone_set_trips(struct thermal_zone_device *tz) { int low = -INT_MAX; @@ -193,25 +208,6 @@ void __thermal_zone_set_trips(struct thermal_zone_device *tz) dev_err(&tz->device, "Failed to set trips: %d\n", ret); } -/** - * thermal_zone_set_trips - Computes the next trip points for the driver - * @tz: a pointer to a thermal zone device structure - * - * The function computes the next temperature boundaries by browsing - * the trip points. The result is the closer low and high trip points - * to the current temperature. These values are passed to the backend - * driver to let it set its own notification mechanism (usually an - * interrupt). - * - * It does not return a value - */ -void thermal_zone_set_trips(struct thermal_zone_device *tz) -{ - mutex_lock(&tz->lock); - __thermal_zone_set_trips(tz); - mutex_unlock(&tz->lock); -} - static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, int target) {