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: 18185 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp213083wru; Thu, 10 Nov 2022 07:30:51 -0800 (PST) X-Google-Smtp-Source: AMsMyM74hK3EJ6D7UHmvQFvgI/OGDVDaLMfAifK7bLrLQWu3GK744gW0BPKbonWNujtWrV+UqMUL X-Received: by 2002:a17:902:ee52:b0:186:a808:5374 with SMTP id 18-20020a170902ee5200b00186a8085374mr63395412plo.158.1668094251233; Thu, 10 Nov 2022 07:30:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668094251; cv=none; d=google.com; s=arc-20160816; b=MMEiU6HYzS72P0a/HFXRXLNJNEIGGw0q/4nUiLIV84B8qu2iLQE9OI0pj/T0BZuysK xr2xOGCZk+xTq9hVUGzOT2ZOjdWiBC5GkHaWhgcjEEZKNzcNmF6jaCmp8p5ztuEnVUR7 mqN90xfkrTZzwvyygQOuEhjPoPFF4r1bJgu/i1JtlPWj1tec/26Y+cbgAjVxtqv893qq fE0HJ0pZnRgRDxMmvY6bNhoPoCFi9WczyWzZRUc5TecWPSbvtP8c/8DBHVzaXMbP6QWu ZTYMwkn0dhJsyvJWZyEo2skqK6K6e/UytxoMDsjUGcsOfNju1m41fjCMnCg7SpI8Um1o VkIA== 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:sender :dkim-signature; bh=bOYNmW9OZFQBi/Rb1c2Zeo5a8y5kFjUwBvSI7SdtFFM=; b=OlLbdVZRsy6hjgZuTmYVCkl9BKOdFzf00pizCCZB72xdGbPq8Ar4CzZVLJODh5lDi4 NwEy5jfTaYwuqm87sFO/cXRkqbi0a8J3uXD66OMIQCg5n30j4+i+VR4ACWfmcYLRe2SH 7Ud21gaJK23Rz6pkNMHUFTQ9C5tGyOcBed806+fzcHGjUNKh1VUjyysxuy+NWsVEe1pi QfU8PFphGgFvGQ7eGAZqoiXkRItQsgVeolW1ceolASP6LscrPQZq4Mft0bubHMaiJwSs FAcB+C6Rb/m0hkQCOMG0nSZLo6Y9QGFEcUbT6xYAp/FFDt7zdnwMk8335B606try/6V7 X0+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Ktcssiua; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m17-20020a170902db1100b001869e76bbe3si24260245plx.487.2022.11.10.07.30.34; Thu, 10 Nov 2022 07:30:51 -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=@gmail.com header.s=20210112 header.b=Ktcssiua; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231421AbiKJPZK (ORCPT + 99 others); Thu, 10 Nov 2022 10:25:10 -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-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 X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749123597857013582?= X-GMAIL-MSGID: =?utf-8?q?1749123597857013582?= 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: 18179 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp212298wru; Thu, 10 Nov 2022 07:29:26 -0800 (PST) X-Google-Smtp-Source: AMsMyM66CN4r2cKhabo/m62CCbECz537FUVhPZTHsup1h/++LL2UIYyC+PtC9YxOJuI/xpJRVuu3 X-Received: by 2002:a17:906:9c83:b0:779:c14c:55e4 with SMTP id fj3-20020a1709069c8300b00779c14c55e4mr58557804ejc.619.1668094165981; Thu, 10 Nov 2022 07:29:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668094165; cv=none; d=google.com; s=arc-20160816; b=n+KZhilTEfO+aHUQlvoOptUFuoIv15ReFUMTZspsYZw5zJDc5YS3L6yuEtIJBSF2kq Uq9h6HDcgduKeZunkhzOU7ri07c0u7MpMDL/H2hGyL1H+d/4uEbrw2cBTFoy8TyaFpIH OReUUjU7ZpsapXYzpv3gDmokke3Mzy4x9h+1AaoVgovoyLv6EnS/ad3ORwWcDKFhyHSy L9hiQRqgZ1Sd47OlCQYl5XqY2pRrplsjGmnr+ykR6oHgaLiAXfELM17Y5Zy3Z1m9NAxy nNl41O0dSwjFO5CDXo58PfzytvhRI03YmUZlebuLahdYWGi4qyAdF3UezvzPvBovrgHi 9vSw== 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:sender :dkim-signature; bh=iSO/JGqHw5m1/Y8PZVH1KMpl9TGQLAVgPtR2Ov1K83Q=; b=TUTjvTvsIw2fYFdmVtv+tvkDgQ1kI3oc2o4XNIwyDyn6YuERTD5NB7VQdPGQY1T4ji krzv9VhR2RSok1LhQb4D3km3B6kJQHDkIULVfmRmGrJMX9TOT9M5m7skJVadOTOtXof+ +1p07XK2S1AxIfqhg/xbxdE0gJLtEdiTmxi7v6lfc8T0CaTsKG2HEX9/puprVtG5hSxe Gx4BwqVKFDfjx3UZGeDIkp09V8CS2/tnr1SfNyZ2i8qC9VqH0f/kxPYI31hEhafcbTdI DGBZqd/d2qPxRp4f52tGUl+ButBRkHLvDMC3CyHGqVVlzuf5POBz8R18yAwqAD4llE0X FW8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=hL0xDKbd; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y20-20020a056402135400b00458c5c47070si14410538edw.234.2022.11.10.07.29.02; Thu, 10 Nov 2022 07:29:25 -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=@gmail.com header.s=20210112 header.b=hL0xDKbd; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231431AbiKJPZN (ORCPT + 99 others); Thu, 10 Nov 2022 10:25:13 -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-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 X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749123508089715882?= X-GMAIL-MSGID: =?utf-8?q?1749123508089715882?= 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:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 18183 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp213046wru; Thu, 10 Nov 2022 07:30:47 -0800 (PST) X-Google-Smtp-Source: AMsMyM7XtIoyggDTxVF4fSVKQF4AYL+kNiOU9KKcfPDQ5OyeF4ZmUNadHt9+/W2+9mHuvBTf+0om X-Received: by 2002:a17:907:c016:b0:78d:8dfb:10ad with SMTP id ss22-20020a170907c01600b0078d8dfb10admr3160130ejc.289.1668094246777; Thu, 10 Nov 2022 07:30:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668094246; cv=none; d=google.com; s=arc-20160816; b=GJVC8R98YrsN29vh3j0GuiF44/0yiVnLIGyrddGqCPYZdHp38CTzmo5qBNnvLIU4sJ wTam9c+3oZ1ozGDQcbXrV+bxZxl59YiIlmTP20e83BXtJ1SASAMHskw2fgSGab+Pjg7U Lx4liTJY07r03GLzs6RJJDdAzHyS3sogxY3txM7vc5JDA1ZGRhvQ/0A96BiLb69FsbZh cQbGQlIAdco3xYrVdEuAT3bmF68QGu4v7Xp5etbiBBa72ZdKhxd9VWRQ46Dzs0MDKTkO MYLrc3gx8d54silMdyJJvvG3elqJUO5xrLerPkM1cZofgVBrfWOhunNHodAbAoyDO85O Lrqg== 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:sender :dkim-signature; bh=UJE4o8UGPSLDbHBH3td/ssdYbDar1LnD9EEm4jyPN1Y=; b=mk+nAcqE3pPnlu/1lv79cpM5NuuMkulTKM9tz5YAPRZ+O/ROKX6CTzERHZNFJdAiPw 2W1oqcJSp513aGgAUQvt5XJz94691pPf4tWPXTc8ojQU6ptiL4QW+4cnC5VU9swfPqGH KMHWxs2cy1SZAhkSywsuemAJsLO0oo0ISyH4kQyWBDlgfU05QIt06CChYwHkOazF4iAy Wvuw8VSK0O2NeYiq3yADDVQQR0HL9MlWXEOvEDxCbybATgV62/1rUd9TdLik2sg6KrH9 //nRkp7jV/fbI2+nHUADB00wJVO+r8w2i5TJM4oJYYpGBUvFWE20sOZFoG7ZSXMv0orK jrLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=auhldo3q; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gt19-20020a1709072d9300b0078d450cbb02si21588977ejc.452.2022.11.10.07.30.22; Thu, 10 Nov 2022 07:30:46 -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=@gmail.com header.s=20210112 header.b=auhldo3q; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230097AbiKJPZS (ORCPT + 99 others); Thu, 10 Nov 2022 10:25:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231419AbiKJPZJ (ORCPT ); Thu, 10 Nov 2022 10:25:09 -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 4F0D931FB8; Thu, 10 Nov 2022 07:25:09 -0800 (PST) Received: by mail-oa1-x36.google.com with SMTP id 586e51a60fabf-13ba86b5ac0so2525364fac.1; Thu, 10 Nov 2022 07:25:09 -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=UJE4o8UGPSLDbHBH3td/ssdYbDar1LnD9EEm4jyPN1Y=; b=auhldo3qG9yeMDSYl7jgYhWvJwAS1ewzMiLMMJe1yNQwYk9ftiJMrbzICYhzVUhlHm QSMyXHOuVivMtF3I8iYVWXoxOlVuJrW/lLuVvbSNzpAmoWcF22uLVl4MkvbIbo9XrcRh MhMIC7eb5K+yLTHJJ4huFB0PdcSOrKz/hoT6qR3VCUUGbGrZPDjH/6pO45NQKaX1Nc6H fL0sSOozSR/upyvuMNn29quJrkoOHCxq+HN37N6uEZkXMraYQRlW6+wMBtXVWHh+SiLr ga53fXSrIUZJ6bTcLqN2Jsil0ML1BLzY4G7ii6Q8RQEd0+HPHm6jvPuOs7Ld/SMTayLQ uqvA== 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=UJE4o8UGPSLDbHBH3td/ssdYbDar1LnD9EEm4jyPN1Y=; b=RmtPspfy/x02MgMMF8w5Vx4H5sKY7sz0kKU+L4oRln+Uk2n13sSxJjUyU1AL83q9XB e74Togctot+JhzP5LCirwNo5pXpKbmsDEI4TLYCNQ6wWcz/Npjd0+GJCQcE41Ry2cRp5 4ld9hGaltJ99kfBagpmFSKgXEZI68z30+jkFgr5R+SnnP6uomP74nJ5LDVtTZOytqtu3 DZIfTRxq9npJNueMm1/qSY3kQRljIvXJqE/gK2iNWC54iUAWcjrF8Uspx/bknT0xsNlB et/IkRIL/sDrk25b1oz3OKBpx0BSrWOFUCU18LCmyH6jPk1c0M1KWz3vJujFUDzbfwiz 6NnA== X-Gm-Message-State: ACrzQf3sHZNyPTQeV6z4llW1OGbLwqrQE/prKmJJIQv+q1h/6y9/D4d5 5773Y8bZ0XSwA5WogCh58Rg= X-Received: by 2002:a05:6870:4687:b0:13e:bdf2:3e6c with SMTP id a7-20020a056870468700b0013ebdf23e6cmr1441935oap.45.1668093908659; Thu, 10 Nov 2022 07:25:08 -0800 (PST) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id ej13-20020a056870f70d00b0011f400edb17sm7649233oab.4.2022.11.10.07.25.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 07:25:08 -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 3/9] thermal/core: Ensure that thermal device is registered in thermal_zone_get_temp Date: Thu, 10 Nov 2022 07:24:54 -0800 Message-Id: <20221110152500.3032655-4-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 X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749123592675093222?= X-GMAIL-MSGID: =?utf-8?q?1749123592675093222?= Calls to thermal_zone_get_temp() are not protected against thermal zone device removal. As result, it is possible that the thermal zone operations callbacks are no longer valid when thermal_zone_get_temp() is called. This may result in crashes such as BUG: unable to handle page fault for address: ffffffffc04ef420 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 5d60e067 P4D 5d60e067 PUD 5d610067 PMD 110197067 PTE 0 Oops: 0000 [#1] PREEMPT SMP NOPTI CPU: 1 PID: 3209 Comm: cat Tainted: G W 5.10.136-19389-g615abc6eb807 #1 02df41ac0b12f3a64f4b34245188d8875bb3bce1 Hardware name: Google Coral/Coral, BIOS Google_Coral.10068.92.0 11/27/2018 RIP: 0010:thermal_zone_get_temp+0x26/0x73 Code: 89 c3 eb d3 0f 1f 44 00 00 55 48 89 e5 41 57 41 56 53 48 85 ff 74 50 48 89 fb 48 81 ff 00 f0 ff ff 77 44 48 8b 83 98 03 00 00 <48> 83 78 10 00 74 36 49 89 f6 4c 8d bb d8 03 00 00 4c 89 ff e8 9f RSP: 0018:ffffb3758138fd38 EFLAGS: 00010287 RAX: ffffffffc04ef410 RBX: ffff98f14d7fb000 RCX: 0000000000000000 RDX: ffff98f17cf90000 RSI: ffffb3758138fd64 RDI: ffff98f14d7fb000 RBP: ffffb3758138fd50 R08: 0000000000001000 R09: ffff98f17cf90000 R10: 0000000000000000 R11: ffffffff8dacad28 R12: 0000000000001000 R13: ffff98f1793a7d80 R14: ffff98f143231708 R15: ffff98f14d7fb018 FS: 00007ec166097800(0000) GS:ffff98f1bbd00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: ffffffffc04ef420 CR3: 000000010ee9a000 CR4: 00000000003506e0 Call Trace: temp_show+0x31/0x68 dev_attr_show+0x1d/0x4f sysfs_kf_seq_show+0x92/0x107 seq_read_iter+0xf5/0x3f2 vfs_read+0x205/0x379 __x64_sys_read+0x7c/0xe2 do_syscall_64+0x43/0x55 entry_SYSCALL_64_after_hwframe+0x61/0xc6 if a thermal device is removed while accesses to its device attributes are ongoing. The problem is exposed by code in iwl_op_mode_mvm_start(), which registers a thermal zone device only to unregister it shortly afterwards if an unrelated failure is encountered while accessing the hardware. Check if the thermal zone device is registered after acquiring the thermal zone device mutex to ensure this does not happen. The code was tested by triggering the failure in iwl_op_mode_mvm_start() on purpose. Without this patch, the kernel crashes reliably. The crash is no longer observed after applying this and the preceding patches. Signed-off-by: Guenter Roeck --- v2: Simplify error handling drivers/thermal/thermal_helpers.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/thermal/thermal_helpers.c b/drivers/thermal/thermal_helpers.c index c65cdce8f856..fca0b23570f9 100644 --- a/drivers/thermal/thermal_helpers.c +++ b/drivers/thermal/thermal_helpers.c @@ -115,7 +115,12 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) int ret; mutex_lock(&tz->lock); - ret = __thermal_zone_get_temp(tz, temp); + + if (device_is_registered(&tz->device)) + ret = __thermal_zone_get_temp(tz, temp); + else + ret = -ENODEV; + mutex_unlock(&tz->lock); return ret; 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: 18184 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp213060wru; Thu, 10 Nov 2022 07:30:47 -0800 (PST) X-Google-Smtp-Source: AMsMyM6Dq1Y+BSCnLlzX3Gmrt89o9vgWMd/btv/AMp+PrW9LCk22qtPp7WdX0oF4fW1PfvYMEPVi X-Received: by 2002:a17:906:858a:b0:7ad:d164:bc45 with SMTP id v10-20020a170906858a00b007add164bc45mr52667135ejx.113.1668094247537; Thu, 10 Nov 2022 07:30:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668094247; cv=none; d=google.com; s=arc-20160816; b=wLp3lFXsVNf9V4owA5cLs/r5WHIKBvwZwpLxWhZ+ndGbuRf+/grUcm17cqbkTISh6w kLCGRnn2assyuTe4I1FcPAPGydrKwwKz5BV0J+lS8oKpp24UN1M++tgbPsoHgAYcTUGw GJYPhfURvs79oURt7cOWWbxfWtaezy5jJWqsowcTW2AY3q7qDve+eB+bJ83yfpDY8w6z hdAAII6iL8bqyxQX1CDYnxdTdu6blAh3EX/9KZ2XkmJD/DfRjQ6y8WuTO0bgEQ5/z1pw tYbIaRUgdwWOgXbyALbsETseN7HnPa8xsRO0qxQPKaYyV12PLKGyxEsSZNuzKqaC3ocQ vULg== 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:sender :dkim-signature; bh=nPRDmNkrm1vn+V+1JEnLY9+b9B506P/EqjPsjoC9grA=; b=jZQ2rm2luwbjWHTmYa6XCaGIRbSgxlJYrK8guL1i4QRcxPE6931NaJQ83FopN4e5E1 Vghv7cU5WMTRS9hOhHeKjwny2NnMOL/qLBpwL48LHHsg+nHQFprz+pG2c7UwUr1Lt4ju F/zDkzv4PTBsAPzubqIf4UnxJPuea1wUhxlGoQ4pY6iqRly0aHkUicZBFqK2aXK3tyEJ fNAKfZSYNMxcdRSbBKFW4U5f4toXNxtFiw5scZsiUdvzI5BHVPYvfEq9sOM49fH7dwE4 h8G9ncKxnXFyVhgs8+2CKemgXTwH7KnagfkC9lZBfukCThA37io7PwUWgShKeVAXO/1V Gmrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=KKlnEPm2; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qm7-20020a170907674700b0078dcdb1c9dfsi16620132ejc.553.2022.11.10.07.30.22; Thu, 10 Nov 2022 07:30:47 -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=@gmail.com header.s=20210112 header.b=KKlnEPm2; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230343AbiKJPZV (ORCPT + 99 others); Thu, 10 Nov 2022 10:25:21 -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-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 X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749123594015545558?= X-GMAIL-MSGID: =?utf-8?q?1749123594015545558?= 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:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 18181 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp212542wru; Thu, 10 Nov 2022 07:29:57 -0800 (PST) X-Google-Smtp-Source: AMsMyM5GcEZB88z31S/aTe5t+KJW3Jm+SG1QimCelXg0ZJoNGiB5hwXDdMSrcw0Gp51VSMHfqR2m X-Received: by 2002:a17:907:d685:b0:78d:3e56:52c with SMTP id wf5-20020a170907d68500b0078d3e56052cmr2882554ejc.732.1668094197039; Thu, 10 Nov 2022 07:29:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668094197; cv=none; d=google.com; s=arc-20160816; b=Qy4az8Kh9oEwWElu0pHbc54l6xBnYZQVuVnBHMirw0IvKpp/mQRAM6o0diD2LyTsxE JpiPPYSOlKUMgZ29Qt1Z1fW0OZBz1IOqBMSGXPwIDDgD+vkSHujNbBBk1iz0vZSTy2gk lc6RwjurPY/KDjQZ0Ur85V9iUfw2ijs/5EBEC59AGkde7mwSX8+ATsvpnb31GjOMLQmW JvRw3kTSwpOLNkDKAhBsqfQzR/wAEGUmgUBhrR5fEZR/emJYlTUNRCTTNcaaZwBQ4z77 ryN26XzhOCrpnLv6cU7m15N21pH/jzMiwx0DnNbUHJh3plls1iO8j8IRTHAN9Qa5ntR4 JTng== 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:sender :dkim-signature; bh=LM2EPhN1nhlDPDS4PEgysbO7FCLJZz7jFfUt9dT+ukM=; b=uYZksT76GJYJ71mudk+oacF2NEdbJYsTnqyzwtejTZMKGIlDeeYRhdQShnsu8D1E1o W10izENuMW/Q6p07Iu4uJQv2UBqBC0HeYYuxeedp0FYe8c53b5EVILSWPLged0BQ3zRE nOh64GAAVCkQJ+LY9+Lq+6KbeIUPm7nSJKcvfjUasAYm/ev1AsWBAUd5Ct6+boQw1H1z KWYp3JlaO1RBrXziYHcIqvZuehFFs+ZPOu5AeE9XsKx0YUcNqUory71hBvsLmTOHmqCV j74EOIFqbQ3nutiWr3VidtDdTge7ZeNXi2KdPCt7gDFd5PYnCV3AIZIFvvcXl8FyUoqM f+DQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=PgDgYUHn; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bo21-20020a0564020b3500b0045d0660a039si17270252edb.316.2022.11.10.07.29.31; Thu, 10 Nov 2022 07:29:57 -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=@gmail.com header.s=20210112 header.b=PgDgYUHn; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231447AbiKJPZY (ORCPT + 99 others); Thu, 10 Nov 2022 10:25:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231423AbiKJPZN (ORCPT ); Thu, 10 Nov 2022 10:25:13 -0500 Received: from mail-ot1-x335.google.com (mail-ot1-x335.google.com [IPv6:2607:f8b0:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A53AA326E8; Thu, 10 Nov 2022 07:25:12 -0800 (PST) Received: by mail-ot1-x335.google.com with SMTP id d26-20020a05683018fa00b0066ab705617aso1268805otf.13; Thu, 10 Nov 2022 07:25:12 -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=LM2EPhN1nhlDPDS4PEgysbO7FCLJZz7jFfUt9dT+ukM=; b=PgDgYUHnijUqOXnXpT2/400UFKTQ2AaTUgtzXeMT3PswiE8/2oTCtsKorY6+xea5ES 5Uhmi/pR6TKGj8ZtAD91itVcvoYRc3WWfOcAHHXkYwmgeMmx/4fZ17LOU0L5/H1iGE+O KaSDA9qDafA6F9FYpPzXsCQnczCFoTHZvRW20YEWR1NIXkN6fUKO2bkPB9ReWOenhS7m AkvjxkFjNr+Z3ZMLoZ6aQraaqZTaHGEibAGSsGe21W1apY51SKhj9D4SpP3fJa/OxQBX Z14AZyMoTSpcBKGSj7iU9f0qh5CdnbJjF+4/inRcQEP7x+MXQdNPm1YHL4u2RkZyD2yj AtpA== 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=LM2EPhN1nhlDPDS4PEgysbO7FCLJZz7jFfUt9dT+ukM=; b=LllgMUoppPo6HqRqdAADXiGQSi2Vj1uFy7MtkdXWj/yoAjBhvt3x/LOoH8UnQT1txP 1JEkKxDJmQK76s6TS3C8Z8rXxcNP5PS1W5cY5l1V7pShqweymUhPW7lqTlEXB8fnsGiu FjU7T7bGV2Ic9JMnk4+Xm8KA7o453bvBJAbsMUsSlqFaGXihn/lMT2H9eFQHFVzCDWAP owh+99sBFzfVBTiJqN4Y6e0fCGR1ufcWKKx0CuCj7dDhtCP74jvd/lU7WUYA57wtc/VM s7kwi0n3hz/lnYZnRefbhbeRdgJPu8ckz/uG4egtAiVI0jOUudAC7WhvJ0S3nRU/UxeJ eJ9Q== X-Gm-Message-State: ACrzQf30ALp1NX6u58W3nL7uXHrBwU6vN53ueWLN3oC8dr3bcxF5qT20 cC0i4SAgNnFaHeNsAvHM8MA= X-Received: by 2002:a05:6830:832:b0:66c:a704:f987 with SMTP id t18-20020a056830083200b0066ca704f987mr11957357ots.89.1668093911953; Thu, 10 Nov 2022 07:25:11 -0800 (PST) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id x34-20020a056870b42200b0013d7fffbc3csm7651214oap.58.2022.11.10.07.25.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 07:25:11 -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 5/9] thermal/core: Introduce locked version of thermal_zone_device_update Date: Thu, 10 Nov 2022 07:24:56 -0800 Message-Id: <20221110152500.3032655-6-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 X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749123540730262455?= X-GMAIL-MSGID: =?utf-8?q?1749123540730262455?= In thermal_zone_device_set_mode(), the thermal zone mutex is released only to be reacquired in the subsequent call to thermal_zone_device_update(). Introduce __thermal_zone_device_update(), which is similar to thermal_zone_device_update() but has to be called with the thermal device mutex held. Call the new function from thermal_zone_device_set_mode() to avoid the extra thermal device mutex release/acquire sequence in that function. With the new function in place, re-implement thermal_zone_device_update() as wrapper around __thermal_zone_device_update() to acquire and release the thermal device mutex. Signed-off-by: Guenter Roeck --- v2: Reword description to clarify that the new function must be called with thermal device mutex held. drivers/thermal/thermal_core.c | 57 ++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 562ece8d16aa..9facd9c5b70f 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -403,6 +403,34 @@ static void thermal_zone_device_init(struct thermal_zone_device *tz) pos->initialized = false; } +static void __thermal_zone_device_update(struct thermal_zone_device *tz, + enum thermal_notify_event event) +{ + int count; + + if (atomic_read(&in_suspend)) + return; + + if (WARN_ONCE(!tz->ops->get_temp, + "'%s' must not be called without 'get_temp' ops set\n", + __func__)) + return; + + if (!thermal_zone_device_is_enabled(tz)) + return; + + update_temperature(tz); + + __thermal_zone_set_trips(tz); + + tz->notify_event = event; + + for (count = 0; count < tz->num_trips; count++) + handle_thermal_trip(tz, count); + + monitor_thermal_zone(tz); +} + static int thermal_zone_device_set_mode(struct thermal_zone_device *tz, enum thermal_device_mode mode) { @@ -423,9 +451,9 @@ static int thermal_zone_device_set_mode(struct thermal_zone_device *tz, if (!ret) tz->mode = mode; - mutex_unlock(&tz->lock); + __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); - thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); + mutex_unlock(&tz->lock); if (mode == THERMAL_DEVICE_ENABLED) thermal_notify_tz_enable(tz->id); @@ -457,31 +485,8 @@ int thermal_zone_device_is_enabled(struct thermal_zone_device *tz) void thermal_zone_device_update(struct thermal_zone_device *tz, enum thermal_notify_event event) { - int count; - - if (atomic_read(&in_suspend)) - return; - - if (WARN_ONCE(!tz->ops->get_temp, "'%s' must not be called without " - "'get_temp' ops set\n", __func__)) - return; - mutex_lock(&tz->lock); - - if (!thermal_zone_device_is_enabled(tz)) - goto out; - - update_temperature(tz); - - __thermal_zone_set_trips(tz); - - tz->notify_event = event; - - for (count = 0; count < tz->num_trips; count++) - handle_thermal_trip(tz, count); - - monitor_thermal_zone(tz); -out: + __thermal_zone_device_update(tz, event); mutex_unlock(&tz->lock); } EXPORT_SYMBOL_GPL(thermal_zone_device_update); 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: 18186 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp213187wru; Thu, 10 Nov 2022 07:31:02 -0800 (PST) X-Google-Smtp-Source: AMsMyM6U34kyLm/18esHf9JXTS+Jv6v2WfXX0U9SzUGiKHrYCA7fPZv/2BAoFsGZJjnLsS3L0am4 X-Received: by 2002:a17:906:3811:b0:7ae:129b:2ce7 with SMTP id v17-20020a170906381100b007ae129b2ce7mr2744597ejc.92.1668094262059; Thu, 10 Nov 2022 07:31:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668094262; cv=none; d=google.com; s=arc-20160816; b=g7eG2pJdq+Bp1gNlLX4fenbsHFihzzQDy9vOIr/eX8lUQGo+K9ofaZ1Stxxme1Rbqx jz8NxKjPx7uNZKgmzVBbqOaHyrfn6BcpcFwUtAlW3ytXFFXjO/bMBAt3F0isiwQ6Qz/M ZEwr421K9SmkYnBMrSeZZyeZOTvu5mvaqmCl7CprK2yq81arSMunPJgf8xEghBYN/CG/ OZfUw+0IbJqe1koXO1oF/hJH/DYcjmhrpim6opFr3rc9ViC4L4/2idFUQqoxD+qWM4IN U+caCW57k6a7JCECq0hden7U8DlrWeXTQNcF5mV6Z9vxOlV18JinrGfPgYf7gACtdeli QiyQ== 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:sender :dkim-signature; bh=TQ10nOb48x6GWqAJdDBvsYg0lrgcue29G6thFXet4SM=; b=W8HgFDTUb4CmrArwctMMWvX4S+AcAeERUhCUSYockmPKKWINGVfT6BXCMAOpUciCYJ REipv5y6y81RZ9q4RVjONCfbg0UKcF+05O+cW4zRKErwtbFIAOHREerYz3qVW9tGvwZL Ib9wDX6QIS5xiLDexH8R2dNyYnKjlObpQZqub31SjaMUDWIcPf/2lncGwhY3jHbAj5JZ 1P5A4mW7QBBnk2a1fGA3VkoInW7BBI1/mVdgp0za6CHHg4wQaL9Qpr/G2fEkamBp2ijR QzTt8N/jrCdQzZ8P1xAHktSoCv4ok1QxqIyuMmDD1i91t2NzCiJrh4fAYks/lhpfvhqH QxKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=n2DGCbHt; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mp14-20020a1709071b0e00b0078de4629958si18611771ejc.248.2022.11.10.07.30.35; Thu, 10 Nov 2022 07:31:02 -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=@gmail.com header.s=20210112 header.b=n2DGCbHt; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231490AbiKJPZ1 (ORCPT + 99 others); Thu, 10 Nov 2022 10:25:27 -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-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 X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749123608690324304?= X-GMAIL-MSGID: =?utf-8?q?1749123608690324304?= 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:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 18164 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp211968wru; Thu, 10 Nov 2022 07:28:48 -0800 (PST) X-Google-Smtp-Source: AMsMyM6d5H5zTQHZ4S3QEEH5+o6YZIumYsiMLnVZpiGWjenr2oePLYxuyCgURtor+gmhq7i4ZmcD X-Received: by 2002:a17:906:f4e:b0:7a2:335e:90e2 with SMTP id h14-20020a1709060f4e00b007a2335e90e2mr2927078ejj.712.1668094128494; Thu, 10 Nov 2022 07:28:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668094128; cv=none; d=google.com; s=arc-20160816; b=Z/ftgqIzVHzSpHfgFy82XiN2ZDE6OtwYmpFmYkVH7GYz7dzx/qEVFrVA8AxC9W6VcQ qxaWsYgG5OhxI4T3tyQ+GkwuNGxuBqn1O7/FZWdSFsJbis1NlxBVF+3DfOpXW7HLS26V 1XVlHjuIbs4JUWaWpBwHbdVrxsPp2RYFDHTx/3/HgqqrisPyU9la0oi3P3AwBKD0F/TK UiNqpm1PZKLttesVeTxS9S2qAISYzWPDXRWoqAZhxB9WAjE+Io3ft+RT4c9EK+BjaEnK 1ePrSUzaIesiDftBwaykkU1Ep+JuSTebjTu5XC9I+UqKSUq4YGiC12NiilUBvcpiFOkm VeTA== 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:sender :dkim-signature; bh=wERUdQdjWjh/WLWGcq6kXq7Y3fhhIRGrVPA3P4TVF/k=; b=jwIRjWxUqxU3ZwQXeCrDsLd98V7m/CATGR2VDMG39O5tTmydH0DSzBQ8h0aiv5ocHb IyowDnODCtbedm1NBWZVG5F26zzS8bF+AryULTHNRMMrImJYORfYAnhWikT0qWKjdii5 LbglHp4T2LIQnXgKg8yrDi6SakwlXrKxJAbgUm28wrC/YO5x5avoYUQheIdnvXtjeada vBopSBBc9t9xatNC1AlCxvo2qkAIotGwbktoTmuZm6DvBavjOT22UNFcEfDsZEfo07Ui uuiwE6Ynsc4EOd+earFKLkStfYAe1EXQ3VW7voTFSOjFm9qtIP7kvIdp/ycjRUKB6rVg 8PvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=XlcvyvGd; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hh18-20020a170906a95200b007ae4717bf18si14818938ejb.156.2022.11.10.07.28.23; Thu, 10 Nov 2022 07:28:48 -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=@gmail.com header.s=20210112 header.b=XlcvyvGd; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230426AbiKJPZf (ORCPT + 99 others); Thu, 10 Nov 2022 10:25:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231464AbiKJPZV (ORCPT ); Thu, 10 Nov 2022 10:25:21 -0500 Received: from mail-oi1-x232.google.com (mail-oi1-x232.google.com [IPv6:2607:f8b0:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 096883E086; Thu, 10 Nov 2022 07:25:16 -0800 (PST) Received: by mail-oi1-x232.google.com with SMTP id n205so2162506oib.1; Thu, 10 Nov 2022 07:25:16 -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=wERUdQdjWjh/WLWGcq6kXq7Y3fhhIRGrVPA3P4TVF/k=; b=XlcvyvGdDoL7j/ld25ik8DutzpkV/ZhQe5SsKtayc7k0cH7McLdnpH7Rz7pb4os8oD niAH3q9Vj29F3ImpOFH0sGgKWEE6/eIMu/okSj7HeY/3uoELTaanIlljr4Y5Eb/ct+29 yxxawZ7k9JCQvLD/X5LwkUsnrCt2xe0upzKAanbY3wUSqUyjcGc5VrMG80MiakGCxuro 8KVizuzoKSFN60Zns+Hp72DXGX0hS92YVnuhy6geIC/VupZQrDEZzW5a5k4ZnqoNaQs3 Q7hRrMtHLE3bz23jU24DnuURApm+NpuOA39YAji3BEj8bOsNR4SMWToYqk9fo4dKldjF Q2Uw== 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=wERUdQdjWjh/WLWGcq6kXq7Y3fhhIRGrVPA3P4TVF/k=; b=dqVwZfHa/TptIOLp20WGPc1/aRpAJ17eb8ro9KE1cLNpk9B0HR8u0VMfLflj7wkO4X W5RDmV+J+vw75cELf4FqaPxU7vB4BElsxyW/ypzxOqbmw51T4x1VKVeLti0pODCGFit2 ixkiPdNL/l2GjWKDSuaL8TTaLSx7OEsB0a9Ronn+/GWFtHSAqQ986Eo/nTXAizgOK5di bym9RUsDRvJGfM4sL3R5/FPzuvFUkd736xTAkm77Sz6thSXEIUm4rdeC5IHGnaphkoPP s4r4CkKHxVijVv8LBgcimqSUO6jWLqYcoVVqommLutSlvN4TruZHl0kAf6YxAn8xzJ9z hA8g== X-Gm-Message-State: ACrzQf3MqNMNUitJaovDRcnsKQa81HgETDfsAJSdzxr8v68CDxcryPLG SPAqfZZQ6Yw31W26CYdYskE= X-Received: by 2002:a05:6808:658:b0:343:4df6:c5d with SMTP id z24-20020a056808065800b003434df60c5dmr1574790oih.279.1668093915356; Thu, 10 Nov 2022 07:25:15 -0800 (PST) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id s16-20020a4adb90000000b0047f72b6988fsm5221123oou.45.2022.11.10.07.25.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 07:25:14 -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 7/9] thermal/core: Protect sysfs accesses to thermal operations with thermal zone mutex Date: Thu, 10 Nov 2022 07:24:58 -0800 Message-Id: <20221110152500.3032655-8-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 X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749123468574778556?= X-GMAIL-MSGID: =?utf-8?q?1749123468574778556?= Protect access to thermal operations against thermal zone removal by acquiring the thermal zone device mutex. After acquiring the mutex, check if the thermal zone device is registered and abort the operation if not. With this change, we can call __thermal_zone_device_update() instead of thermal_zone_device_update() from trip_point_temp_store() and from emul_temp_store(). Similar, we can call __thermal_zone_set_trips() instead of thermal_zone_set_trips() from trip_point_hyst_store(). Signed-off-by: Guenter Roeck --- v2: Simplify error handling to avoid additional goto. Do not use ternary operator in modified code. Remove now unnecessary { } in emul_temp_store(). drivers/thermal/thermal_core.c | 4 +- drivers/thermal/thermal_core.h | 2 + drivers/thermal/thermal_sysfs.c | 79 +++++++++++++++++++++++++++------ 3 files changed, 69 insertions(+), 16 deletions(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 9facd9c5b70f..b8e3b262b2bd 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -403,8 +403,8 @@ static void thermal_zone_device_init(struct thermal_zone_device *tz) pos->initialized = false; } -static void __thermal_zone_device_update(struct thermal_zone_device *tz, - enum thermal_notify_event event) +void __thermal_zone_device_update(struct thermal_zone_device *tz, + enum thermal_notify_event event) { int count; diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h index 1571917bd3c8..7b51b9a22e7e 100644 --- a/drivers/thermal/thermal_core.h +++ b/drivers/thermal/thermal_core.h @@ -109,6 +109,8 @@ int thermal_register_governor(struct thermal_governor *); void thermal_unregister_governor(struct thermal_governor *); int thermal_zone_device_set_policy(struct thermal_zone_device *, char *); int thermal_build_list_of_policies(char *buf); +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); diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c index ec495c7dff03..e2e94294fbb2 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -92,7 +92,14 @@ trip_point_type_show(struct device *dev, struct device_attribute *attr, if (sscanf(attr->attr.name, "trip_point_%d_type", &trip) != 1) return -EINVAL; - result = tz->ops->get_trip_type(tz, trip, &type); + mutex_lock(&tz->lock); + + if (device_is_registered(dev)) + result = tz->ops->get_trip_type(tz, trip, &type); + else + result = -ENODEV; + + mutex_unlock(&tz->lock); if (result) return result; @@ -128,10 +135,17 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr, if (kstrtoint(buf, 10, &temperature)) return -EINVAL; + mutex_lock(&tz->lock); + + if (!device_is_registered(dev)) { + ret = -ENODEV; + goto unlock; + } + if (tz->ops->set_trip_temp) { ret = tz->ops->set_trip_temp(tz, trip, temperature); if (ret) - return ret; + goto unlock; } if (tz->trips) @@ -140,16 +154,22 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr, if (tz->ops->get_trip_hyst) { ret = tz->ops->get_trip_hyst(tz, trip, &hyst); if (ret) - return ret; + goto unlock; } ret = tz->ops->get_trip_type(tz, trip, &type); if (ret) - return ret; + goto unlock; thermal_notify_tz_trip_change(tz->id, trip, type, temperature, hyst); - thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); + __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); + +unlock: + mutex_unlock(&tz->lock); + + if (ret) + return ret; return count; } @@ -168,7 +188,14 @@ trip_point_temp_show(struct device *dev, struct device_attribute *attr, if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1) return -EINVAL; - ret = tz->ops->get_trip_temp(tz, trip, &temperature); + mutex_lock(&tz->lock); + + if (device_is_registered(dev)) + ret = tz->ops->get_trip_temp(tz, trip, &temperature); + else + ret = -ENODEV; + + mutex_unlock(&tz->lock); if (ret) return ret; @@ -193,6 +220,13 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr, if (kstrtoint(buf, 10, &temperature)) return -EINVAL; + mutex_lock(&tz->lock); + + if (!device_is_registered(dev)) { + ret = -ENODEV; + goto unlock; + } + /* * We are not doing any check on the 'temperature' value * here. The driver implementing 'set_trip_hyst' has to @@ -201,7 +235,10 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr, ret = tz->ops->set_trip_hyst(tz, trip, temperature); if (!ret) - thermal_zone_set_trips(tz); + __thermal_zone_set_trips(tz); + +unlock: + mutex_unlock(&tz->lock); return ret ? ret : count; } @@ -220,7 +257,14 @@ trip_point_hyst_show(struct device *dev, struct device_attribute *attr, if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1) return -EINVAL; - ret = tz->ops->get_trip_hyst(tz, trip, &temperature); + mutex_lock(&tz->lock); + + if (device_is_registered(dev)) + ret = tz->ops->get_trip_hyst(tz, trip, &temperature); + else + ret = -ENODEV; + + mutex_unlock(&tz->lock); return ret ? ret : sprintf(buf, "%d\n", temperature); } @@ -269,16 +313,23 @@ emul_temp_store(struct device *dev, struct device_attribute *attr, if (kstrtoint(buf, 10, &temperature)) return -EINVAL; - if (!tz->ops->set_emul_temp) { - mutex_lock(&tz->lock); + mutex_lock(&tz->lock); + + if (!device_is_registered(dev)) { + ret = -ENODEV; + goto unlock; + } + + if (!tz->ops->set_emul_temp) tz->emul_temperature = temperature; - mutex_unlock(&tz->lock); - } else { + else ret = tz->ops->set_emul_temp(tz, temperature); - } if (!ret) - thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); + __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); + +unlock: + mutex_unlock(&tz->lock); return ret ? ret : count; } 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: 18165 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp211978wru; Thu, 10 Nov 2022 07:28:49 -0800 (PST) X-Google-Smtp-Source: AMsMyM4yiXmpBYDzGMe+tvyFoS8ycesICUF/9qqnYOjmsel5NLsFZoiC5snK6I9W0CCz8vmrj6HZ X-Received: by 2002:a05:6402:d59:b0:458:f012:bacb with SMTP id ec25-20020a0564020d5900b00458f012bacbmr2508041edb.345.1668094129641; Thu, 10 Nov 2022 07:28:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668094129; cv=none; d=google.com; s=arc-20160816; b=z5zXpeSDHBtNwHVJI5OEgolsfd6KsJFto4BoJEtGbPaU0tjPl/dXxea1R3pt8qBH6U bTcqYSYuVSNPBs4UCyefijQ0W8bSuNu7oKb4anH585XrTuvrN3TtkRh47oUsxl2KyQOw D7RpYeFI8zffPMBZNLeKD7F91bo+sd5Iz7MArsZKP6zCcr4x+gN2sb/4A9wyxVnStZ9j MV9wtD31yitlR++2IX11wDspMBnbWHt7Vfm4kttGaNX0mRRo4dbRRpP7F09piTx7iUv2 ybfiipvgXctVTlc+vMEK0ATNWBSqlTgGgLHYjZCZTIlQaaoyfl3i7yzrIDuSoLxbGofS a0SA== 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:sender :dkim-signature; bh=GmjCJ069G5ZA0ck/PetkxKT25fldp2oH5LB83pWLxGg=; b=sphK/KH56H38GLGBd8QgQkc8321XKScmFo93b5msz+0TISc7nQMV0ts5rVHDsUgBs7 eduyHKgrI28+Nv0BKJp0ZcmZVfFI3uWjGd666HwguQKbrnptIfukorfYj3wVDWSma0uh nI595tq/rIQfb9qSRreJVT0ikh+zvoUJMkH7LQXFqDkxiQcnLfgaSSpvf0qaeRnqBPrd treNRBa+J4WzOStcII+9Qs4Zfll1yvRrh4ZRTGmOO20kQE5TNk6Ib8Ebje7hB9+dLcji PuEtK/71p9CTElTgZCSnfWQVQVTsXkbG5X+l67UdZkKkVxZNinEjzGh0/xhD0y3NqCOR N7AA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=pq817FLf; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w18-20020a05640234d200b004619867344csi23223534edc.0.2022.11.10.07.28.24; Thu, 10 Nov 2022 07:28:49 -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=@gmail.com header.s=20210112 header.b=pq817FLf; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231358AbiKJPZo (ORCPT + 99 others); Thu, 10 Nov 2022 10:25:44 -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-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 X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749123469866685767?= X-GMAIL-MSGID: =?utf-8?q?1749123469866685767?= 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) { From patchwork Thu Nov 10 15:25:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 18166 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp211992wru; Thu, 10 Nov 2022 07:28:52 -0800 (PST) X-Google-Smtp-Source: AMsMyM7BFVigR7ueR8g3HBzcWwlKbjflforaL1L67nNk6uEpiFSdKFWJW6WV7/1XDWZmZy9/xvlY X-Received: by 2002:a05:6402:40d0:b0:462:7b99:d424 with SMTP id z16-20020a05640240d000b004627b99d424mr62747024edb.62.1668094131624; Thu, 10 Nov 2022 07:28:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668094131; cv=none; d=google.com; s=arc-20160816; b=FY/gVyPkHXDvG+Yr5uTS42J1AyHhjVHj2iqNaAUuIjTiYfO68f2W7l53ARYQfb9npw rJxkbvSfxQeYE9mYmq69Op3itnRMN4GwciRRvLAMnUvE8fq2MG9Lgf98sRauCLRXVavX dAbfvHRGeTGmeHNmCWxjZH8GCq3KVbZnbdgG8+zUm8syg2ttHrnCK8oSeC+qCt3PVCYp mozaBjhyb/JQssNxChM4gw2EGE/nlzJ/eVwLxxzBVcrgYzeF6hk0nIlZuvw/VEaVvY/4 cA6hmoQ32UytLUBlL4P51qFLosR9RkaGWh7nldsAZVUDZ20A0chnARRLNM30hgCb7gId lgrw== 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:sender :dkim-signature; bh=BP0JS35IGfSl0FVl/KECGclfAmAetA4hJ3SBTMSOqEY=; b=ogIKUrhhNBQLGMZH/3DBpi2Wsvp4n/58piWnku9w+kvNrvQAePVNzMpB5kAWIT9pey 2TVCluem/1kfPTuB98N09ZSWSs4xs8nWCNu37hEwDQmo0T/jFsfRZbheVGiGbZR2II07 5dVlNCG+wkwx2MKhXOV6rDNbOtv+zg+kMO8vRddD0JELUq1Q5diJCMDpXduGiJUYzqdB NtIkwajrUfI08wFpiethlFYKYw179PEiqh9QWk9SZSS816dhR3q4ardtP1kA/Nerjb9G 2cSYmOPfMMUEzuOWTgoyael9IlWK17exgUcAmfY3pU2x7hVfEfIimOM+hqAgxlDFggN2 WP+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="K8P/qg9j"; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s4-20020a056402520400b004523826ed6dsi21390631edd.554.2022.11.10.07.28.26; Thu, 10 Nov 2022 07:28:51 -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=@gmail.com header.s=20210112 header.b="K8P/qg9j"; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230076AbiKJPZx (ORCPT + 99 others); Thu, 10 Nov 2022 10:25:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231511AbiKJPZg (ORCPT ); Thu, 10 Nov 2022 10:25:36 -0500 Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18A863F047; Thu, 10 Nov 2022 07:25:19 -0800 (PST) Received: by mail-oi1-x22c.google.com with SMTP id v81so2138495oie.5; Thu, 10 Nov 2022 07:25:19 -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=BP0JS35IGfSl0FVl/KECGclfAmAetA4hJ3SBTMSOqEY=; b=K8P/qg9jSQx3oCgYAtr8dQEh/PdPjWLXH7I9sz7vHYpsmm6tPvZjZ2GZodU1BXAga/ iPWHc8tCLgQpFL9Rq34MMot+WoWC3whB/GF3/bzQWpih5sjjSpD90h7SAplIdkKD50L9 Lm8EzhYJ8djTH5zjsmGaU5TsUajgq0AeNeoT9kkrfFyLi/NcW9kmCRBVMlfpibdTdEyW ZA/2lA5ebJ7pyOjrVbmTsGIVLJ18E6i/KnwudIuE2jbmMZb5PE7pIpK+KCTpDYdlsBr6 ljlbKpenBWOVclXzyovtd3/cB4NDEH7/e+2akCzXAmnKNLEQWxM6yxrki73TZbBlK2YM kDWw== 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=BP0JS35IGfSl0FVl/KECGclfAmAetA4hJ3SBTMSOqEY=; b=2bfCWdYV9/2726VoiciW1ddSH1TUEvJwXuCMFNVVuHlMWrmt3/T+jIxHr56lXNTAmW kLKUIe78+Qaidl241avJUz5BVppIk/ecE1DVFmBdJAVmfFw7Ri9Z2zGkkvE+fBfbvEBI uVBlfPhBiMNxBcAak4x7n5hQMXDiYkijjK+EgYIwFctqWw5o1aPqDoo/TwN6UIEVsfd+ A/DUb0xWrSt1OkEaMG6Yiab8WWH1e495gzzXTeFEi9OdMxri96DQKSk1jhkcXGFhnyEZ 7U+BqXH2NmqzQ594crXRnxUy/Sge15Nn2TyZin+1v1LVDSQWOY4U3e+ua0/T5sedMMXL cHgw== X-Gm-Message-State: ACrzQf1FXsnS3oNTmxNYniB1Dg6l4JS+Ycd8LLHLndxY4bSzPR0s8DOU 5at+FRpVrErC11TbM560wwM= X-Received: by 2002:a54:410e:0:b0:35a:5a4b:a836 with SMTP id l14-20020a54410e000000b0035a5a4ba836mr17076466oic.95.1668093918430; Thu, 10 Nov 2022 07:25:18 -0800 (PST) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id x34-20020a056870b42200b0013d7fffbc3csm7651353oap.58.2022.11.10.07.25.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 07:25:18 -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 9/9] thermal/core: Protect thermal device operations against thermal device removal Date: Thu, 10 Nov 2022 07:25:00 -0800 Message-Id: <20221110152500.3032655-10-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 X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749123471860517632?= X-GMAIL-MSGID: =?utf-8?q?1749123471860517632?= Thermal device operations may be called after thermal zone device removal. After thermal zone device removal, thermal zone device operations must no longer be called. To prevent such calls from happening, ensure that the thermal device is registered before executing any thermal device operations. Signed-off-by: Guenter Roeck --- v2: Fixed spelling error in description drivers/thermal/thermal_core.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index b8e3b262b2bd..aa0107f11c98 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -203,6 +203,9 @@ int thermal_zone_device_set_policy(struct thermal_zone_device *tz, mutex_lock(&thermal_governor_lock); mutex_lock(&tz->lock); + if (!device_is_registered(&tz->device)) + goto exit; + gov = __find_governor(strim(policy)); if (!gov) goto exit; @@ -445,6 +448,12 @@ static int thermal_zone_device_set_mode(struct thermal_zone_device *tz, return ret; } + if (!device_is_registered(&tz->device)) { + mutex_unlock(&tz->lock); + + return -ENODEV; + } + if (tz->ops->change_mode) ret = tz->ops->change_mode(tz, mode); @@ -486,7 +495,8 @@ void thermal_zone_device_update(struct thermal_zone_device *tz, enum thermal_notify_event event) { mutex_lock(&tz->lock); - __thermal_zone_device_update(tz, event); + if (device_is_registered(&tz->device)) + __thermal_zone_device_update(tz, event); mutex_unlock(&tz->lock); } EXPORT_SYMBOL_GPL(thermal_zone_device_update);