From patchwork Mon Oct 17 13:09:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 3446 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1438474wrs; Mon, 17 Oct 2022 06:10:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4qOkiMGkA8L/BuadoMDaw9Ren+wF2N9GERdoVlK+XPeWswBj434z4rPR3OQQTBUaGHvT3w X-Received: by 2002:a17:90b:3141:b0:20d:49d6:30b with SMTP id ip1-20020a17090b314100b0020d49d6030bmr31795300pjb.223.1666012219742; Mon, 17 Oct 2022 06:10:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666012219; cv=none; d=google.com; s=arc-20160816; b=dkiUYZXzvLPUDtmGfTRgkVZhgSmR4FekYg1LLSy88L/42v1DDQe0xTRBHsgdOKd0vb VKvEGtZJSgCoyaxSSo2OfMLD7RPRaaKEZNfR20oGN1OOHNwRMDg0XARPOz7y9lOjd/vX JOJ1+Kk64INViGP/de+BeSDJZ2GCiXOHX/QG/pQrxJynL+UNBEOi39nROVaJd/qKOCq3 YnLuT9NTeJBGAYaO12TPdkkKxeaDp2Ky+ylCAZD+2xfbK7PlyiJFCn8uq2cm7ijszaq/ 29AVAyACu/eMxnQtG72NUGLfPFYsIQy+Y0uGAe0AIbqjVIEFl8h7xLWFcZ+zC9oI4k8z AiSA== 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=oENftCtS6Re+rYEuXigvS3nBvzokSvAdm/CPq6AiHb0=; b=TA1r4XikIqcb1C1XxfPu4PAX9K6Jl43wQbnhIAbRv5rWRS7m0vT93NRVZPhOR6lN87 WShumsi2cvnivUKSg3PFSmBHZtu2SK0ERg7sprjJMvAWosBoQHN2IJeBRnRIl0Kt2i/3 elelf8mWE8eyWaqk1MLwIeqWHktanVgp1QpFOjsZ1Xi9j2FuMaVsmMuqV7hfwQJFdZRV 8328qGV7q307Ik53a66hfzVXRQM8dS+scb1XG8oDTkQTDORiVYLi8JmEiac5pWG3XGhm 3Ow85Nefwu/QwHN8e/jJxEtMuuEL3+TGTPlFrhJEjz00VvB9nDhL+H0+tE92PohYOyxi +DFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=U8z35qvg; 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 f2-20020a170902ff0200b0017a00403dffsi11032217plj.339.2022.10.17.06.10.00; Mon, 17 Oct 2022 06:10:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=U8z35qvg; 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 S229815AbiJQNJT (ORCPT + 99 others); Mon, 17 Oct 2022 09:09:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229786AbiJQNJQ (ORCPT ); Mon, 17 Oct 2022 09:09:16 -0400 Received: from mail-oa1-x29.google.com (mail-oa1-x29.google.com [IPv6:2001:4860:4864:20::29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 213DC14D2A; Mon, 17 Oct 2022 06:09:16 -0700 (PDT) Received: by mail-oa1-x29.google.com with SMTP id 586e51a60fabf-1324e7a1284so13146164fac.10; Mon, 17 Oct 2022 06:09:16 -0700 (PDT) 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=oENftCtS6Re+rYEuXigvS3nBvzokSvAdm/CPq6AiHb0=; b=U8z35qvgBJtdcS83au5XocR3KoRYrE+LQwEvaGgG7fzPuPN/6UZPRzeBs6bBqR7AE7 oNTfGTKNXmYQ0pbLX1Ttd5Bx5AHNEahqz/Dip1Jbw8TjzU9qgI9oP+xb73/F52KunOur c0pwE1+S7so0tE1aoJTSKFCqfGHGNv9nj6EJWXGRbDpKPNcjIBWk/VPiAJsByVUYJ1Is tVdV6Ap9hT6s9q96EE4KbJ1RTxQ4Vbadlu8CwEsEL9egZ3iVnfp2NO58f6tTmGQy32nZ cc+wGot9TTpvgfZ7Hj8AXVmxcPYUcn76JrjIe1p7CWT/U1LkVLuhk8aqtr2GGAADRzTz 84ig== 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=oENftCtS6Re+rYEuXigvS3nBvzokSvAdm/CPq6AiHb0=; b=eT7AleaABMP53iI2058gGQwLCwT5mFlDkiFcOxkvtvZTYK8a2Aw/tctuGtKz263sjR Jn6iXKSPLvF22nexxZ48r5NBkwf4KbOKpJ8l/6IM9RLPm6+hvaq+BvYURQvnOSsAo9LW 593Z1A3UmycK0V+pRoAeu6hAGX3K+YfzqX1uCS5lNNV/f3g4UNdowFRkFBEHW6vWfdJV gq6KH+/4YiRIlCbY6paEUd/Ioh5sCvofjbqfudLKY5STw6E0qod00YMy5qMLc8DLj/g2 nlK7QoUDhaS6ObH2NPc1lGAoQmBBgcYBqWc7CtfY/e5koJzJWU+OBRjNhRS5v3uLH1IX DFmQ== X-Gm-Message-State: ACrzQf3d+h/9fRY6Gj1zkl+XJV0eSF/o4M2XQv1BZpsjUcmLwv+lKOBD +QNRL3EIhlCOAviPSVUkbEc= X-Received: by 2002:a05:6870:4729:b0:136:71ed:9989 with SMTP id b41-20020a056870472900b0013671ed9989mr15815928oaq.73.1666012155503; Mon, 17 Oct 2022 06:09:15 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id 23-20020aca2117000000b00354efb5be11sm4264343oiz.15.2022.10.17.06.09.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 06:09:15 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: "Rafael J . Wysocki" Cc: Daniel Lezcano , Amit Kucheria , Zhang Rui , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH 1/9] thermal/core: Destroy thermal zone device mutex in release function Date: Mon, 17 Oct 2022 06:09:02 -0700 Message-Id: <20221017130910.2307118-2-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221017130910.2307118-1-linux@roeck-us.net> References: <20221017130910.2307118-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?1746940429403194182?= X-GMAIL-MSGID: =?utf-8?q?1746940429403194182?= 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 --- 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 Mon Oct 17 13:09:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 3445 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1438363wrs; Mon, 17 Oct 2022 06:10:08 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4eFUwQnCanp/L7LTRExoSgk8H/IGSC2qgexCdKEzPQk91oL1uO+Kx1C1TBHbtN2lUbHjKE X-Received: by 2002:a17:902:e841:b0:180:49a2:8e6c with SMTP id t1-20020a170902e84100b0018049a28e6cmr11941566plg.143.1666012207801; Mon, 17 Oct 2022 06:10:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666012207; cv=none; d=google.com; s=arc-20160816; b=N/1bBol6psYsHBz21Uhwp0MrGxT8rw+qPJoQ4orAxWjayCreTN9vNg7eQtK2m/MD10 XaldRIDfhVYZlSfcCFfauGc/a3hRo0c+JnHZzpwkGSL1cGN+1nPyJwq4JyRkkRWESCVq XSIxFX7FPhVhauVLDiezRb5HUZ5db8PBKZCLfAdEiZocVCKdRpIjrdbkrN9TYCPdJhsY L/tsNH1NcfgmOqrx+9LaB/c48Bh/It9S2hmILjQ4pFARJOOb2ecQq81Ne7g8OmaTp6nV XpDyrzQcpPetavfTsJkkTqiRsIKyvSdsLl4EISdnoxrfp/anmdDMru4pojAAuZ+myTPl KfNg== 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=QKfBoF1vAy9J7XDh1mrV+p3UkSxmJG+dWUnnE4QiU30=; b=mlyBU9bGPnrGly3ADcJw6Yu/iH7sGX97bGOT9AeF39M+q/HNSAj9W6ZGYmoRTvieq1 C9RdnUHECRbHLicM7Bg3ylXmBHRigAzXIv4WdGEDJQIKWafionM432m/j9LWSW6pXG1I ByR1bx69TCKtRzo/GzvB9uger+PpTlnco7l0npXB2YF0A9+kGhCvnuAnoYEela8IWDQk g8DiyIBHZoOx96hQiJ9/LPReDoku7MFneC3CRESHg9VhSDW4x+N5VjSwRLFYZwD/3FF7 O3JCYiXPifEILTcme4+WSXEvXMwzg6T5bzNazXtMsQvDHfOhinYLkm1QEHoXmu3W9LlQ Ov4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=JBTRePEC; 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 pi10-20020a17090b1e4a00b00202ae67f30asi13432241pjb.163.2022.10.17.06.09.48; Mon, 17 Oct 2022 06:10:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=JBTRePEC; 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 S229910AbiJQNJV (ORCPT + 99 others); Mon, 17 Oct 2022 09:09:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229627AbiJQNJS (ORCPT ); Mon, 17 Oct 2022 09:09:18 -0400 Received: from mail-oi1-x236.google.com (mail-oi1-x236.google.com [IPv6:2607:f8b0:4864:20::236]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76CACD12D; Mon, 17 Oct 2022 06:09:17 -0700 (PDT) Received: by mail-oi1-x236.google.com with SMTP id p127so11357514oih.9; Mon, 17 Oct 2022 06:09:17 -0700 (PDT) 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=QKfBoF1vAy9J7XDh1mrV+p3UkSxmJG+dWUnnE4QiU30=; b=JBTRePECk0TrlmszbwZ/Qq8e4ibi7Q3gZqBLVv8QbyCizjJ/jwCPNqWND9vi1Tg7O2 X5SRrAEH6fnVSouHXKClyxTb8q7H7NuPxyCvjGS9xAkFDdgxo0GkuurYGi0Ldb9baZ0W fTf+cPVNiYUBN3uVxj3HV2U4d8BzlLh17jeWcEeFMO+FqL1vgtUogthVFeVElPuRj06S 1jXPdHlpXIOOqYp8nNvUK1s5sbYjmy0pde6JELjguyv+sCpDIb1urTz+YjeMOeKLGzQH J/E4iAYL9IIVMcewoT+IBCwD2dex8sGOoh3ADCVwuOj44Y9MiSgJ9fv2MVF5XCJlA9NI GbHw== 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=QKfBoF1vAy9J7XDh1mrV+p3UkSxmJG+dWUnnE4QiU30=; b=2eJMoPScNwMbTawExXKC3gfsgDxrDvWA2Pokh8bzycxWszjb0XOoF0CaFhlCe/I+yq Od8HVTnEW5UrzHuoBB8gTEby8T9n1/n07gYS7Cen0RAby1rxZhk367Nu66kzbB7RLDYj mRllXs6jtYhOwLoydnTSGjvZGaVWSmQsiyMcc1c5n+NcPlIhfLcLMg9UnbVrhZFgkfSe UHmQKHIjkcMbI48fATHYQt/88pV8tL2fyWP6LULRXBzN4raPgwfn0aol0Fgl37Y1UXA6 GDfBUitMV64ciQCAO5SiUiFoQfK+Z11PHww8wZN/1t6UROqdDfp7Uqt0MuWHDcw6kdt9 lHyw== X-Gm-Message-State: ACrzQf3tDJGYFtnlGlAuZgj3OmPc48x4N1vfuyFyQYK0orjnPPCjlP4u vhkM39x0+EbhNYFTgeav1wU= X-Received: by 2002:a05:6808:114c:b0:355:989:7ce7 with SMTP id u12-20020a056808114c00b0035509897ce7mr9607598oiu.153.1666012156849; Mon, 17 Oct 2022 06:09:16 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id x12-20020a4aca8c000000b004767a4863basm4245858ooq.14.2022.10.17.06.09.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 06:09:16 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: "Rafael J . Wysocki" Cc: Daniel Lezcano , Amit Kucheria , Zhang Rui , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH 2/9] thermal/core: Delete device under thermal device zone lock Date: Mon, 17 Oct 2022 06:09:03 -0700 Message-Id: <20221017130910.2307118-3-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221017130910.2307118-1-linux@roeck-us.net> References: <20221017130910.2307118-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?1746940416658748301?= X-GMAIL-MSGID: =?utf-8?q?1746940416658748301?= 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 --- 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 Mon Oct 17 13:09:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 3449 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1438759wrs; Mon, 17 Oct 2022 06:10:48 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4HZMU9gmN0Fnb8jA2mDOCgNbWk1OT8sngSw4ToMEvr9ZOdvow7gA5nX0lpoFc23WEqtdbV X-Received: by 2002:a17:902:70c7:b0:183:3e66:fe48 with SMTP id l7-20020a17090270c700b001833e66fe48mr11662782plt.165.1666012247751; Mon, 17 Oct 2022 06:10:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666012247; cv=none; d=google.com; s=arc-20160816; b=ye2CnX+1kdpMCvTwzJ+RiWgfrm3nl3/cEvKizywdTI8YASbkGoS+WjYimvxyOycsT9 AQBekbSPSESSUq1pQTJVH9zwBEDUZkv0/CpYXFuO1oRW2TwrCksnL8sqZK7DPpDTLqrw bmdPbY/v8V2/0TEi0gkIQ87/vpW6DQ7Ec/sAlKRD0kB5+dEQTLTXO6dvRJlqi7sCbUpy /CEGUoW6fFYX3abiWrV1lfngtiKIJSRd2JNtnlYCXxbE2POxcHg/2/6g3fb5ThTKdY0B 8NBnpbuPaxsFXo9C51GMGiIj+Ep3RYvR7VSzlzR1vXBd7bCCwc/iMMmfrHSW5FSuUeaC PHVw== 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=pFT2bMEFQh2yF9QQTbmTtz9HkQZeU5AhJSQdSV4KX2k=; b=Cx0cs9on6F+TrOgICJcgrbM9XPkgT8/CR1uzSKQDr7GvfjL0cmeIGWREG79iIZym2e yHqeRuXRBJbnxfYbO7Syeyj5mnT8RntWUO8O/KoSQefJqA/c+GW3QPjslkzWaJx7zPMK 4i/tdHf025W+Q9MLLyF6yu+hnq3aI9dC8duHzv1iwrM82MDlTtXSNo829Nln4mSMCdVw ooJ5UAUXZXRvTv4SVXUKeUZo5SG9W2H5M+WhLVdLYvpz+am4PqU8aiWeTE0+AFqFJZ1Y YDx5hzOmhtpzLpP2LOSgDE6tBlYkZeAL3Rm3cn1kBdYHKGwNaN3Wi8EQWQdOxecQtOq6 tp5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=IFPC22K6; 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 j14-20020a636e0e000000b0045d60c88afasi12124338pgc.440.2022.10.17.06.10.10; Mon, 17 Oct 2022 06:10:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=IFPC22K6; 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 S230122AbiJQNJd (ORCPT + 99 others); Mon, 17 Oct 2022 09:09:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229901AbiJQNJV (ORCPT ); Mon, 17 Oct 2022 09:09:21 -0400 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 ED97AF5AC; Mon, 17 Oct 2022 06:09:18 -0700 (PDT) Received: by mail-oi1-x22c.google.com with SMTP id g10so12001857oif.10; Mon, 17 Oct 2022 06:09:18 -0700 (PDT) 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=pFT2bMEFQh2yF9QQTbmTtz9HkQZeU5AhJSQdSV4KX2k=; b=IFPC22K6m4tJHLjYDOFeFSue5qG2Yhz81ayq2GsU1/rLnZ0K6mu+Ngjx0JleNXoxf/ 5ugvYTPouTSq+cOgDmKAVVOiDS+TGmoLX7vLgsQjVMXgSn9rKwp+FbMb/TX/ILrA9SJG ZeN5NbzZTMFRzCCT514W+R9Jw8MnVmJhMi7ZSDyAJQueptsBSPW9Y9n76qXOVNP5DL9k nTRYI36ym/TIqv9YXDK1Y5Nay420OoNzpXpg3vjcSGSaLn5oE8pee9xKFlxYcCrbRMsW 84kCerpqlCYQorji22Md1Eo2z46zJZcozgeExFFFaNHc91+3fn4SyGvAgVF5aMzasLw5 2Scg== 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=pFT2bMEFQh2yF9QQTbmTtz9HkQZeU5AhJSQdSV4KX2k=; b=eml99viomPyjXUl2l9cZRgRDB9IY1F6WtfyQOgcErierguBuGzQh5QmkzPPPWpoqR9 FBgc2l3Z1BBa/LTXMb16PFMvd+ivrxJNNdtvg0LlGSPjzxI14QN1VBrbyPqxx4rRvZa8 F3Cd4x/DkJ6GYphMS91aRIs7fV01dtCMZDVlmbb03k32p7llu5aeNYrjeyc8bdgn7Ww4 sRQ60tWnXQXyLfSn76DpMhpFKLpoLHebAt5LkdrzUdFSH6YESIuodxAzTqfm9BKlWJTe 6zIj3BLA/3iKipoJeETf+2iTqD1sQI5akRN46kMC9SVIr80Yitma1gw9UF1meFYtPg8L GhCg== X-Gm-Message-State: ACrzQf3QUCQTuV8e4pHpYpOVJOtHzV6ER1oVIcqaDyi8myY0mLJT9oeL LBEfJQ3a/MH9+hHaMZWfCCY= X-Received: by 2002:a05:6808:238c:b0:355:1b0b:6cf8 with SMTP id bp12-20020a056808238c00b003551b0b6cf8mr7691993oib.99.1666012158303; Mon, 17 Oct 2022 06:09:18 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id q28-20020a056808201c00b003544db0ebfdsm4284689oiw.4.2022.10.17.06.09.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 06:09:18 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: "Rafael J . Wysocki" Cc: Daniel Lezcano , Amit Kucheria , Zhang Rui , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH 3/9] thermal/core: Ensure that thermal device is registered in thermal_zone_get_temp Date: Mon, 17 Oct 2022 06:09:04 -0700 Message-Id: <20221017130910.2307118-4-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221017130910.2307118-1-linux@roeck-us.net> References: <20221017130910.2307118-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?1746940458850326994?= X-GMAIL-MSGID: =?utf-8?q?1746940458850326994?= 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 --- drivers/thermal/thermal_helpers.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/thermal/thermal_helpers.c b/drivers/thermal/thermal_helpers.c index c65cdce8f856..3bac0b7a4c62 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); + if (!device_is_registered(&tz->device)) { + ret = -ENODEV; + goto unlock; + } ret = __thermal_zone_get_temp(tz, temp); +unlock: mutex_unlock(&tz->lock); return ret; From patchwork Mon Oct 17 13:09:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 3448 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1438589wrs; Mon, 17 Oct 2022 06:10:31 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6FJ6OmuigIoFR9vLmD6HKx+EcqBXEt8wul2Q2zDzRFz3tnAbJ9vGOUrd139L+4q3qRkJub X-Received: by 2002:a17:902:d4cc:b0:185:4b4e:b432 with SMTP id o12-20020a170902d4cc00b001854b4eb432mr10759634plg.130.1666012231529; Mon, 17 Oct 2022 06:10:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666012231; cv=none; d=google.com; s=arc-20160816; b=Z1LzcQJNnzsnBU6+dfZZgR1O+d5xX29KT1OP3zpPazMDRfbljVedDPvrK2fl5EbSfr yX4CvBfSCkn84BLH5s7ZS+XkH5pYhlSdy4J53Ko8XrSBAGeU2yfrxksAm6UlkAde31+p PZBMegrRtIlsQI1Lp0+xyB13zDyvMSqnHX2ZIfw/vexeDu9s11fPejCJfMj0KKFcV/fG fCm+ZOCDIwPEatR9HE5QEIwhkaRNoZeujUfOJfIKCwwY/Lb8P6fIdBV6Afg9/l6fUtKV WQJn8ChHmNlXLdh6uP77MOLHlEgAU7sk7cPsdTN3p7awhBgAVJbE2NvcrdkXLdTAsOka 062Q== 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=3tEB+/8vVKq9rF95ePeiHY4k9BKsvRlONK/3JFhRY2Y=; b=ej9xIS/CSoTRAhTjvRiCw0gClpkdyhVNhW7QOgsXgNn7JcE84CBXkbquBEuTDH+nqk hkmsF/IR1BS/1xW7YO+NSxrgU4LjqxjOC/byvbL0LPmAxutlQkYz2iQ/z9thxP5d66/q IRTeVaTDAk9K2bwT67Z1taYB4KzRk8OqBNBEiDE1CQVmJtPF1iMcwtFsjoRRqzKVCwXh /fp5MSSxmsFR+VesVAWXi9pQ1J/Pe8d6sotsUaa2Oiw0e44ae7vi3F1R0ipq3Qqa4ofr K1On4cTo9bofhD6TdiJ0/lGpMM4FVvf0HvUcGA2Mw1Si+SHUTO9wO2WoB/emB/jTptUo jtbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Xt5fGBbp; 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 j13-20020a65430d000000b0046040a85ea6si12112600pgq.120.2022.10.17.06.10.17; Mon, 17 Oct 2022 06:10:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Xt5fGBbp; 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 S230070AbiJQNJi (ORCPT + 99 others); Mon, 17 Oct 2022 09:09:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229924AbiJQNJW (ORCPT ); Mon, 17 Oct 2022 09:09:22 -0400 Received: from mail-oi1-x22e.google.com (mail-oi1-x22e.google.com [IPv6:2607:f8b0:4864:20::22e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D38B307; Mon, 17 Oct 2022 06:09:20 -0700 (PDT) Received: by mail-oi1-x22e.google.com with SMTP id o64so11996042oib.12; Mon, 17 Oct 2022 06:09:20 -0700 (PDT) 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=3tEB+/8vVKq9rF95ePeiHY4k9BKsvRlONK/3JFhRY2Y=; b=Xt5fGBbphOq+fnSLeYp6ANZDd7pi9UYGXhf30GPpD+K04mnbcPPCfmnGlsP/kmn+UQ ZwfoeVeIXQbU6lFZmXA5JyRn05OkSTxXwqXlLe0/ZUSMuvBWgmghwQAZEqcF7QPTkoH7 vn5fWYWInUa5NfwZshnts9ozD9YYwI/ytfSRWtB8lcslVHDu++fcueNe41SChRbpibU0 wjPOHsDSC1jELzPZRn0i5h8jiLcZixKoOQuXpt8PINxnifCDknEkTm9M6/XsEkABipXw 4d+k/uBbgFpEeM+9RSPH+sp8HWrxUFRrqXx2a7IFDZ3ls45m3CSrS6JxbJ8Tolu81v07 ugkA== 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=3tEB+/8vVKq9rF95ePeiHY4k9BKsvRlONK/3JFhRY2Y=; b=BtM45oUeorhniVaMmmcOpcpM66u6eYPX2c+SZs+Wd1ZBkFf8vn/jXzYUYghUOosOB5 CBZjzB2itMbqp9lYeQN62h+M5DcZmM1+6c8VPCPGgqs6p8w7LWuXMJmG4+Kfo2dSvMfm 5NvcTLXOohDRGco/VRMNFtiOU9cM6vbUTNo1prInD6R6BvAQgbPreaY0EHpGHVn2ZQb/ ZVgegctQoo8gKEHvJgsif5MeYvnfkGqdpf4cm0k47SempiE4ZhlqtOdiT510abERl9Yc QLaNGYG2G1zR562NE+cwwmZNlZDzk4hiqGDqp0dMVmDQ1kwfU6hJMQ2CUlGj5ASOT49H Csfw== X-Gm-Message-State: ACrzQf19gCG4Gwbu/aW9YcRR7VBh0NNMqn1SZEVPHSMJNUnoOpxadXUu ywcB+pVglW/ayM/z0FOoyLY= X-Received: by 2002:a05:6808:17a7:b0:355:3cba:7d41 with SMTP id bg39-20020a05680817a700b003553cba7d41mr1371077oib.244.1666012159671; Mon, 17 Oct 2022 06:09:19 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id s4-20020a056870248400b00136cfb02a94sm4593172oaq.7.2022.10.17.06.09.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 06:09:19 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: "Rafael J . Wysocki" Cc: Daniel Lezcano , Amit Kucheria , Zhang Rui , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH 4/9] thermal/core: Move parameter validation from __thermal_zone_get_temp to thermal_zone_get_temp Date: Mon, 17 Oct 2022 06:09:05 -0700 Message-Id: <20221017130910.2307118-5-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221017130910.2307118-1-linux@roeck-us.net> References: <20221017130910.2307118-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?1746940441383508463?= X-GMAIL-MSGID: =?utf-8?q?1746940441383508463?= 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. Signed-off-by: Guenter Roeck --- 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 3bac0b7a4c62..0161d5fb1cf2 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,7 +125,16 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) { int ret; + if (!tz || IS_ERR(tz)) + return -EINVAL; + mutex_lock(&tz->lock); + + if (!tz->ops->get_temp) { + ret = -EINVAL; + goto unlock; + } + if (!device_is_registered(&tz->device)) { ret = -ENODEV; goto unlock; From patchwork Mon Oct 17 13:09:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 3452 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1439372wrs; Mon, 17 Oct 2022 06:12:01 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4v8JK+q0iqTvmDFL1bdWGD3as74mytfOaDsILS8oYtw9u0n8/OBabCaT8r/iNRD3spo2jH X-Received: by 2002:a17:90b:1956:b0:20a:9ab0:6f9e with SMTP id nk22-20020a17090b195600b0020a9ab06f9emr13648968pjb.231.1666012320764; Mon, 17 Oct 2022 06:12:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666012320; cv=none; d=google.com; s=arc-20160816; b=SiugKTPNtyMu08JuYVB6+yDU58WzSYKTCZqotzlUZpowivBjSnq5aLgPsSR7cywgpz j5SXqexP7Il+8sLxV0VcCyFsIYIiWRogUCL3K7359rNHa7tosQYtm53AnA56ZaJAySi9 8n0MrFMRJxDzD4FyH5/9Rbbl8d8bMGTiYv4nH/Bax7LyIFeJwjNyBZfyTv0o0gfOFiO8 3bC+D6jQgJVsEIdEzTnpammIvmtqEjQP8HJkVOmJHtsxS8I0CFZlNw3c2m9cCLEs9Ofj ToYhFVosghXEcZ+prwzv8ZyvLPpsmyBNw46J4OgAPhb5x58TN2KsgweE5ohAh7DrooOU AN0g== 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=X7gIDtWIHqMTwWV4LzsKahvW+VVqbbSVkDc0k/BhZ2M=; b=LgCUg9jOT6Zaa+YOFdJiiqKDR/3XwYHzHwjCbpPtUi2Mfjoz+yfmnfvNomxW/o9yug VvUx4w0MsnRg+aOILtPxpU4GG19I0MVh3kq52jfr+Ll2worRhsO2BBWjXb+8ystvE6Dp 61lgPWIlSUYBINswoZPyjfwMoeD4NqXoHB5YJiWlBbmUdMdiqPhhRCe7WPLT4HvFxHAz ElLA4Km7svb6nBbioMDpd73KmS2ohXaLwl4jGAqi2ZwGBX6IYxbpLn7RaVrLHARcM8Py +4fHVH/pEgLQsE9pwJZq8leb0btmP6PPMIZBEiXFV1m0x2YYX6xJ9oCLItD4Cqg++5SS 85MA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=czrn5SBz; 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 k15-20020a170902d58f00b00176806795b7si11693440plh.351.2022.10.17.06.10.58; Mon, 17 Oct 2022 06:12:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=czrn5SBz; 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 S230370AbiJQNJn (ORCPT + 99 others); Mon, 17 Oct 2022 09:09:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229970AbiJQNJ1 (ORCPT ); Mon, 17 Oct 2022 09:09:27 -0400 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 DFB48CC3; Mon, 17 Oct 2022 06:09:21 -0700 (PDT) Received: by mail-ot1-x335.google.com with SMTP id z11-20020a05683020cb00b00661a95cf920so5725294otq.5; Mon, 17 Oct 2022 06:09:21 -0700 (PDT) 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=X7gIDtWIHqMTwWV4LzsKahvW+VVqbbSVkDc0k/BhZ2M=; b=czrn5SBzra+2vmL0/mlbIauObwnFQTqCJaEQpupceqA7PsF3Z3DY+Gg+RLYPvfgtqy yZlMnvj2YttPutzMJiywMITS3MORb1lQmTmnr/SFE2WSANfXP89Hubaq/LxllgxzDvJy /9Mse0S+bSSXRVqi5ByMyKIXFpjlMLqiWavHiVyugJJMj8SM8YytKo8VtQu8dJLwtluw aVxznfyR3xBptDMTA9vOSEw+rTAqK+Bno+Md/pe1igq9a48CQH0347EXb2W+dK7w6RO3 H4LBp4nLqJqQXOsrEA41b0aLBrUlNSCgEE6ojLlvLot/UiDQGrEmUmvkx7yAf8RKk1Bb Hvyw== 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=X7gIDtWIHqMTwWV4LzsKahvW+VVqbbSVkDc0k/BhZ2M=; b=1VK8EHso5WimoeHAmV2tfpRxsR/cV8FdRD6d4ulCfPBmAM0Fj503s92iRajQAM1sx9 Y0BYK8DXrePB+ce5B7tCMdsmW6BAlzFLyviatvs3nNw9Ce+HF6piPYUiEI5p2y3xYl6z wmvxADgCMA9U8d9MCeETRo3b2rfhMh8HZENXPkq4GbNecoKgzvjcOnG+DT3tQzW2PL70 YleP4NGFSAVLkSoUtS4cTXV9kCalOXSc9rwfIincZktA59j4+w5usPonQy0ZEn8EuSJj Qd2UGdjDFnQdsLkMyNMb056pcZH1SvcSuDKiPH2FboTK1uEGAZuwB9wtCYsXD95Hv4uY ARvg== X-Gm-Message-State: ACrzQf3qggd06li9vKvizKdyC1oogcygYFGI4lb3ATxSNIHtl3/lo37I zn5VodP0U/bDkcGj3eymoutp6ZKUFQo= X-Received: by 2002:a05:6830:314f:b0:661:ba49:d842 with SMTP id c15-20020a056830314f00b00661ba49d842mr5075426ots.129.1666012161013; Mon, 17 Oct 2022 06:09:21 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id f4-20020a05680814c400b003458d346a60sm4241890oiw.25.2022.10.17.06.09.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 06:09:20 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: "Rafael J . Wysocki" Cc: Daniel Lezcano , Amit Kucheria , Zhang Rui , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH 5/9] thermal/core: Introduce locked version of thermal_zone_device_update Date: Mon, 17 Oct 2022 06:09:06 -0700 Message-Id: <20221017130910.2307118-6-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221017130910.2307118-1-linux@roeck-us.net> References: <20221017130910.2307118-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?1746940534953138313?= X-GMAIL-MSGID: =?utf-8?q?1746940534953138313?= 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() as locked version of thermal_zone_device_update() and call it from thermal_zone_device_set_mode() without releasing the lock to avoid the extra release/acuire sequence. Signed-off-by: Guenter Roeck --- 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 Mon Oct 17 13:09:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 3451 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1439367wrs; Mon, 17 Oct 2022 06:12:00 -0700 (PDT) X-Google-Smtp-Source: AMsMyM48DjEdx1AT43zheCbja6/t5MIwcrciGmohNA7vYZX6/P54fLjyvRCIMjvPB0Vtz5xYTrgc X-Received: by 2002:a63:fd41:0:b0:438:7670:589a with SMTP id m1-20020a63fd41000000b004387670589amr10622229pgj.148.1666012320242; Mon, 17 Oct 2022 06:12:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666012320; cv=none; d=google.com; s=arc-20160816; b=JJup7WxoBHi4XbL5Z8t4hj0JHRg8EXi1ij2JTadw8dmWSzZyBmsVpGzXw+8Knj8me9 Nl+v7ZswuXvoo4E2/olacvukRomzcpx8iFAco+Qf03kIQkw8kGG6sYw4SnFlVX+3dw1U orcn4YrPhRfEcBvX7QiW+kWDA7AehhOvpnZ/4CQaNDDFUPo8CX7snlhyJQ/PVsN/orF8 b76Fgh5+p79S5khhBGVjiJO4ZO0AGcQdvtnQ44Id7jqfoOmbBQUKgk2yx3Ityc5sGx6w EwyjOomiESeE32aJxPLifBaZTJAnYBR01tmKUc/7OUITsqlvHm3IJFoLn8QgIbVCqDGk KOmA== 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=XxBO71YTPZQhAtFbi9nnTkRa58Zyh2hdgkHI+eeVMDA=; b=Nbu8qfwLyBjbRkTrCPABTS/nMYPjRFCYblf2ZSNU5O7yC67SGLHnLQwHUROuNTKjHc zZIY3QqMhWg+9HrpzyCRWUAR02m1IzgJ2YGId1fuZsaoVYoQZwoD2NhBHHt7+DSEwhgc NWvE+c2ow3Qrurt8VGAqHxZb+iWf+OfoIzkHzVGqLkGM08Fc/LiEhhtRVCWgQrHmOk86 INPTeaTonY0E4qbOpiMKE/K1XEI8PFbh5Drv16Hnp6T3C8AK1f/bgVh8mNUwP1wm82NT GYRwELhtB3dWTNUlq31hMSYWsrsVoLFUSDZMG74ujd359EzyUeSH9B3p4AwJfTu+wW9m snrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Nv1MpuRk; 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 k8-20020a6568c8000000b0044e74d0e841si12203977pgt.201.2022.10.17.06.11.17; Mon, 17 Oct 2022 06:12:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Nv1MpuRk; 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 S230418AbiJQNJq (ORCPT + 99 others); Mon, 17 Oct 2022 09:09:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229993AbiJQNJ1 (ORCPT ); Mon, 17 Oct 2022 09:09:27 -0400 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 E03A0D9F; Mon, 17 Oct 2022 06:09:22 -0700 (PDT) Received: by mail-oi1-x22c.google.com with SMTP id g10so12002030oif.10; Mon, 17 Oct 2022 06:09:22 -0700 (PDT) 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=XxBO71YTPZQhAtFbi9nnTkRa58Zyh2hdgkHI+eeVMDA=; b=Nv1MpuRk8XWnN8W4oZzjLoBkpJ5NX7m02MZydTw5nLViCz9tGpw2b2AYNoMVJliJbn XEdocACuUheqF4g6XSJitDgrNFN3NV6KvWMp5Wf0VILlzmSoVgZfJK0QzizXNKE/VXYi EbYMuS3RGIYfhtYlMc2ffrBBPfcfM//IwKMhYAZtEVh6BToqsouD/BzaRdVHACxoqiYu Cs6CbFRCepQFdWkUOB4CsjjBuKmpaJjpLROH4FSlfth9mkmheVXgsInb89wiwfpvpsFD ZDUH+2RAsgxkvfXtM+HlGxQLkLx00R5hUNF5reGf1GNXNE6EHA3CQSixUHiHimbrKVXF sUkw== 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=XxBO71YTPZQhAtFbi9nnTkRa58Zyh2hdgkHI+eeVMDA=; b=TrBsOt9pe5XWnkvmLK5FbSnbRvTubIqdE8nikDhbDOJiDZr/Mh/XFUTN7aBDwuj1zy 2o/B1QBvqCtjupL6xLcRP+9DvHVJsaazQkRZjCRRaTLpP1fP5GXdtZ+2KBsqlW1q6VYh YA0+7iQvVfmqky92a6a67lqhuuJzY3h92+7oH0r96NeCg1cMcep5+7P3cC64+50ZeJ2o 1IzmLPV9bIGJZqi9wGDx/ii60GKMBKcxwiRqYkOOSV44VYvpoGOTvRXaCrwoLB4BFKnk lU5hmr8IrSiWOkIwYcVnwaXm8CwHm7PlghIF4Ez/RDazFvNBlr3F/2A0F3KXujOddMs3 CqHA== X-Gm-Message-State: ACrzQf0csF0PUlrl3/LF4z5hqmRqvgV6ofY7xaxsziLroaKfCUDIXr/x UwS6GObMgT4i+YbWZsDwGXAEVai8lJw= X-Received: by 2002:a05:6808:16ac:b0:353:b7d7:5fef with SMTP id bb44-20020a05680816ac00b00353b7d75fefmr4952009oib.293.1666012162337; Mon, 17 Oct 2022 06:09:22 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id d10-20020a4aaa8a000000b0047644a9b49asm4207865oon.19.2022.10.17.06.09.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 06:09:22 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: "Rafael J . Wysocki" Cc: Daniel Lezcano , Amit Kucheria , Zhang Rui , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH 6/9] thermal/core: Protect hwmon accesses to thermal operations with thermal zone mutex Date: Mon, 17 Oct 2022 06:09:07 -0700 Message-Id: <20221017130910.2307118-7-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221017130910.2307118-1-linux@roeck-us.net> References: <20221017130910.2307118-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?1746940534456662883?= X-GMAIL-MSGID: =?utf-8?q?1746940534456662883?= 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 --- drivers/thermal/thermal_hwmon.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/drivers/thermal/thermal_hwmon.c b/drivers/thermal/thermal_hwmon.c index f53f4ceb6a5d..33bfbaed4236 100644 --- a/drivers/thermal/thermal_hwmon.c +++ b/drivers/thermal/thermal_hwmon.c @@ -77,11 +77,19 @@ temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf) int temperature; int ret; + mutex_lock(&tz->lock); + + if (!device_is_registered(&tz->device)) { + ret = -ENODEV; + goto unlock; + } + ret = tz->ops->get_crit_temp(tz, &temperature); - if (ret) - return ret; - return sprintf(buf, "%d\n", temperature); +unlock: + mutex_unlock(&tz->lock); + + return ret ? ret : sprintf(buf, "%d\n", temperature); } From patchwork Mon Oct 17 13:09:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 3454 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1439565wrs; Mon, 17 Oct 2022 06:12:24 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7Qz2UrdP/RqauyU3D5HZKWGV17OTaZk+jksbjP2mCxowrGonOfYS/gkieDo7BCfBnxOh6A X-Received: by 2002:a65:6c11:0:b0:46a:eec8:19f7 with SMTP id y17-20020a656c11000000b0046aeec819f7mr10818545pgu.478.1666012344212; Mon, 17 Oct 2022 06:12:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666012344; cv=none; d=google.com; s=arc-20160816; b=i8qjHNV85VUxbR6nQS6U+ny/ppoV5zBQdHcbJJM5xsnzjsMbe3XFpU1ZkKe38HzCJn LVUfl7qgOZ4gFHz4L+gGHPCRl1jbQUbMD8MrAcEV9YGJ/+gINtJ58tUT446B+muNIDd5 nd2FJY2cizAd9PNYLdYjQlLondyZ8QVhb0d8c/B7dzNMl3Y1BfLwy+Kojm494nUhtwbs u84iLT12JEVW9lm7d63T6w1BpoGPvt+U5Hxxe4CTQc1WakQc+MBTY7RTN+bc68OUMpeT oZMwfbSF9zHSfHt93GfY3+ylDf+qdd48lQzFoon9SH80Pte/W1zqg0/S7SzcheD17B4l ZpFw== 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=GSjMfysxR25bu72lEn5VGMegOh4cZTT77Ai7vgMJhXA=; b=MoYMplppp9jrzPtAFn/MU4pWtotVdQW/9gJq0CmMd/xJCH8wl1xAPFdG/kGAenHx42 RTNoH3ONtJ8GSdCylz2b951PtnZP8W/RXVx16NQJGb+rsZVsNbE/h7e/KOb3f/qhP856 Y+y9/9oZBOxFithyz20cAkvoYBmtUJXgtO/0PTfaqoyjjErhjmia/CrTmDgWAe6jZZOE VI+5X0dfr4gkPWNt5QE3s6ky0WHMe3trrlfbxOIoqbYfoUie8aTyuLyL6joHAannEb+L L5aC4UXbsbBdurpQt0NqHFT4rnWRR0RA50Ax98zoft+LaEQyEu1jtCUejvgnYqZpPQP7 w+yg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=K+luvcqK; 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 b20-20020a170902b61400b0017a0e8713cesi11213730pls.452.2022.10.17.06.12.06; Mon, 17 Oct 2022 06:12:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=K+luvcqK; 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 S231206AbiJQNKR (ORCPT + 99 others); Mon, 17 Oct 2022 09:10:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230175AbiJQNJk (ORCPT ); Mon, 17 Oct 2022 09:09:40 -0400 Received: from mail-oi1-x22d.google.com (mail-oi1-x22d.google.com [IPv6:2607:f8b0:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C931062D6; Mon, 17 Oct 2022 06:09:24 -0700 (PDT) Received: by mail-oi1-x22d.google.com with SMTP id g130so11999277oia.13; Mon, 17 Oct 2022 06:09:24 -0700 (PDT) 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=GSjMfysxR25bu72lEn5VGMegOh4cZTT77Ai7vgMJhXA=; b=K+luvcqKpHjgwGaRSNK7+zlrPL8Qig9YXDLLgCw5iAr52QtI1/QFoWRpea4XdYrQbI HpVlDJUvjfNCzxg2tiVwgdtic1DNmsyomEDE7oJGW+gMnNV8/uLWEuLvVE1WYNnOzQMa xW54ceicn4Sc1KcrFW9eMJ+sD4JXerS0n66Lejj+M8HC72OIapioOcSNbTq/6Al2AdDh 4WFBwHLrG55ONo+TyFd5QCqIw7FG4rSs9HktHoq7DbTp1Sh/+oWzGwDqHkbI9MZskcYv gWuWEI14XY4VqA1OLnDOlmCX8t3uaSLYczgCUjSYmvNz/44PfNtgaNFo15cKpIhPMwlH 9ZAw== 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=GSjMfysxR25bu72lEn5VGMegOh4cZTT77Ai7vgMJhXA=; b=X/JCiEi8OvHIH74joXnYCpkVtmdHE2u1SNdvqBoC+QftcFKdsFwQTHiUqlZ6TIhGaL iNsNPVlD3V78IOLmVIh88QrJpBfTO4AhnpPnivEXFO8AG4xMkB3Buny9jlinPv7ZSVIa i+NmQ0bFwO61f7j1HYfCAjlnU/oPn/mFAabknT1jGBRMBirvpGEn0ydo/wC34nuDuG/g Pz/KhaFVCWnh1ldLKYgQbs0E6f316YWLmXNBK8eAh3Mman/4y4qfMYwEShgo8t16IUqK fMwzPGFducRHOwgGx1RmuvL5/uufu7T6oi7/LFQ1IfdTza33MTLavpUfu/wx9pFkDv81 mSbA== X-Gm-Message-State: ACrzQf2bZW9Dg8r5lw7rjlQtY4hNX+3Dqo41P+nMotUHcG2ANGj2WOCF pPTZK7Iv4l4yE65J8ESe7zY= X-Received: by 2002:a05:6808:1247:b0:351:9b63:b065 with SMTP id o7-20020a056808124700b003519b63b065mr12571516oiv.261.1666012163767; Mon, 17 Oct 2022 06:09:23 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id d32-20020a056870d2a000b00132218cb7afsm4788402oae.42.2022.10.17.06.09.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 06:09:23 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: "Rafael J . Wysocki" Cc: Daniel Lezcano , Amit Kucheria , Zhang Rui , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH 7/9] thermal/core: Protect sysfs accesses to thermal operations with thermal zone mutex Date: Mon, 17 Oct 2022 06:09:08 -0700 Message-Id: <20221017130910.2307118-8-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221017130910.2307118-1-linux@roeck-us.net> References: <20221017130910.2307118-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?1746940559676342328?= X-GMAIL-MSGID: =?utf-8?q?1746940559676342328?= 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 --- drivers/thermal/thermal_core.c | 4 +- drivers/thermal/thermal_core.h | 2 + drivers/thermal/thermal_sysfs.c | 77 ++++++++++++++++++++++++++++----- 3 files changed, 69 insertions(+), 14 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..68607e6070e8 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -92,7 +92,15 @@ trip_point_type_show(struct device *dev, struct device_attribute *attr, if (sscanf(attr->attr.name, "trip_point_%d_type", &trip) != 1) return -EINVAL; + mutex_lock(&tz->lock); + + if (!device_is_registered(dev)) { + mutex_unlock(&tz->lock); + return -ENODEV; + } + result = tz->ops->get_trip_type(tz, trip, &type); + mutex_unlock(&tz->lock); if (result) return result; @@ -128,10 +136,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,18 +155,21 @@ 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); - return count; +unlock: + mutex_unlock(&tz->lock); + + return ret ? ret : count; } static ssize_t @@ -168,12 +186,19 @@ trip_point_temp_show(struct device *dev, struct device_attribute *attr, if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1) return -EINVAL; + mutex_lock(&tz->lock); + + if (!device_is_registered(dev)) { + ret = -ENODEV; + goto unlock; + } + ret = tz->ops->get_trip_temp(tz, trip, &temperature); - if (ret) - return ret; +unlock: + mutex_unlock(&tz->lock); - return sprintf(buf, "%d\n", temperature); + return ret ? ret : sprintf(buf, "%d\n", temperature); } static ssize_t @@ -193,6 +218,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 +233,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,8 +255,18 @@ trip_point_hyst_show(struct device *dev, struct device_attribute *attr, if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1) return -EINVAL; + mutex_lock(&tz->lock); + + if (!device_is_registered(dev)) { + ret = -ENODEV; + goto unlock; + } + ret = tz->ops->get_trip_hyst(tz, trip, &temperature); +unlock: + mutex_unlock(&tz->lock); + return ret ? ret : sprintf(buf, "%d\n", temperature); } @@ -269,16 +314,24 @@ emul_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_emul_temp) { - mutex_lock(&tz->lock); tz->emul_temperature = temperature; - mutex_unlock(&tz->lock); } 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 Mon Oct 17 13:09:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 3453 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1439408wrs; Mon, 17 Oct 2022 06:12:05 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6SlLgrHub4oQkUf5uQZj3SgpPQUElwcIN/4sXilbIcddZJuJ3BhBHQYbCKuJY2gevv3dqq X-Received: by 2002:a17:902:dad2:b0:179:ee21:22a8 with SMTP id q18-20020a170902dad200b00179ee2122a8mr11870124plx.70.1666012324570; Mon, 17 Oct 2022 06:12:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666012324; cv=none; d=google.com; s=arc-20160816; b=0RN89bOnaj9VQ068kd6UIs+NVS7EV9YmuP4Ve7Y4UizmOnds/H3YFWHDmHVhpfPNH0 vj/FrJZf1Y1lZEBCkYlao0g9GLWwRiG02kn0GDM3RbOfXrT12+JGXrMu7gmN97A5pnOg zJqquiUQrY+cimtx0OUvnHv4lAbgFAevDcG6nZqVcdM4dYmVA+Pqjsr7FG9Qun5IPa1P 6Hl88reUYwjTqxmfKZARbTPm/dZOonh1Tx3qeVi2DtOaON36gtwi6h7+ptDGic+o7jfz Ruh2eipR6hr0szY3m/Cfno437lW75lthv/zh251e+CXD7MOQUAE/FeOzbt2lpSHu+ODl Op6A== 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=4d3iRvEV+6jWxwqm/UWU+ztirujQ1MGfWO9XCWvtifk=; b=VsSPz7UDd/HXbdQIK08yyICCBB2n+TG7CipujLyikYhrxoEc0QV4eIvGRiWCYAo8Dw GmQ/HuO8ioqZQ9Cg7levVcz3VDFqtZC8nkrlM+I3ie48cpYEiEVpLvi59iyNFEvx9sb4 78eTERLU6r5hXBbn/WG5hUf8eswRcQ/RkLKUhRu8Hl2jhSvBmPzkM3vizFdvH5WfzgM5 siyGjaXHryv0buQQBDMFqd0Q89uy0++EdSk2R8HgM8zmKP0G9Kt8OpyhXf+jvtPU9hAV jae2+l7G1VQm+JFuiUY5CVRI6FDF7IGZvGIlJ7NIn9Qer6RyMU6mqecxN+bUbzdCXLws kuXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=hm6rxDSS; 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 m24-20020a635818000000b0041cd5c0295esi10963002pgb.852.2022.10.17.06.11.27; Mon, 17 Oct 2022 06:12:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=hm6rxDSS; 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 S230047AbiJQNJ5 (ORCPT + 99 others); Mon, 17 Oct 2022 09:09:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230055AbiJQNJi (ORCPT ); Mon, 17 Oct 2022 09:09:38 -0400 Received: from mail-oa1-x2d.google.com (mail-oa1-x2d.google.com [IPv6:2001:4860:4864:20::2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C911E62C4; Mon, 17 Oct 2022 06:09:26 -0700 (PDT) Received: by mail-oa1-x2d.google.com with SMTP id 586e51a60fabf-132af5e5543so13141342fac.8; Mon, 17 Oct 2022 06:09:26 -0700 (PDT) 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=4d3iRvEV+6jWxwqm/UWU+ztirujQ1MGfWO9XCWvtifk=; b=hm6rxDSSYxzO0a7hrOC7v4Ok5SEm+QWP+XI1IsRXjCV6wH1ynzDJqgg0TpW9LptWMP 80ZHFQjmIi8oYZFFf0IDAWmo6T1ABd4DC0Dz9npefYtgS1Q46VQnFWZr8DxaoSUWKH4G JCxDystIiiUltocG1kYOoazhiz7tnJGIQkcA3S+ML9cDts9BFMl6VtsNfA0iMLLey3jB jA7IhIQhMLMQiRgrLZedVCRr/92xzTbWbWDc1ln3lKlMh0pMG0bJpmksuduvBYblu2NV JPyC2A7Nmkly3dzRXdYGcRUOobLPnbGyGRjM0UAPGkOJbU6z1hK+yY9y1u0jUaD1tsB9 iqoA== 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=4d3iRvEV+6jWxwqm/UWU+ztirujQ1MGfWO9XCWvtifk=; b=pCcWd5i8HZPVUPYnElrO8rtFZRoiQo4r/+s9dXIaGbQPfDZ8xOAGjNnfsqwoU+1bKA TVaF1HgyWuZhFTxZspTPw93Mqj77tDbBkLOMR7txILX7SWPFSDpHY9Sbg/3KPePjKSw2 MxOBt/mm25SLHLWs9SbSOk0FJs8yz1HGRRwZH/903qIv7J2s/6MnFg+J1NHHFdlC3GP5 FxWYuinu3QCbnPkdmql0/Npyr8Vulc/L9laoN6pnvqE9RxwcdvKDIMKlcUjgMIgFyYYk z9qYFQ2ZUcRLS5a6GbAJjgFkssiSppOAJGbL0GjD3BgdIvZoSzSQLP+gJy90NJnEakpb 9+xw== X-Gm-Message-State: ACrzQf0Ry+KsicCie8TP4/YYN6v9oEAugeWrIqvqhhI4xfjjUQW56pc/ SnG1Q7S84zr090oNDaOFDSQ= X-Received: by 2002:a05:6870:f704:b0:132:9901:df2 with SMTP id ej4-20020a056870f70400b0013299010df2mr5451032oab.278.1666012165087; Mon, 17 Oct 2022 06:09:25 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id u7-20020a4aa347000000b00480646ecd45sm4128567ool.48.2022.10.17.06.09.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 06:09:24 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: "Rafael J . Wysocki" Cc: Daniel Lezcano , Amit Kucheria , Zhang Rui , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH 8/9] thermal/core: Remove thermal_zone_set_trips() Date: Mon, 17 Oct 2022 06:09:09 -0700 Message-Id: <20221017130910.2307118-9-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221017130910.2307118-1-linux@roeck-us.net> References: <20221017130910.2307118-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?1746940539526066810?= X-GMAIL-MSGID: =?utf-8?q?1746940539526066810?= 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 --- 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 0161d5fb1cf2..02b353c8d9aa 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 Mon Oct 17 13:09:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 3450 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1439283wrs; Mon, 17 Oct 2022 06:11:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5Ug4Q+D9R17bd7yUqKOmbUh687Wem392yy3k53fW0kZ6mkcKEY6Dk77h+6rs6ScBU19FI2 X-Received: by 2002:a17:902:eecc:b0:17f:624e:8af2 with SMTP id h12-20020a170902eecc00b0017f624e8af2mr12033886plb.152.1666012310881; Mon, 17 Oct 2022 06:11:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666012310; cv=none; d=google.com; s=arc-20160816; b=Z8Qq3tIky6KDfxFXBO8x53xs2WNAWQm3ULUwjudhMlGgwrAXHVUYbdKHk0Qf3kQBKR 8eEJScSCWWExvTjl042k1o05QVDcFvrfgLfxpXQ3HbTpwjGUhgDk+oRUQByCg7VbJleP aghHliNnm4meNLB8A0oQDmcoSiJ0W2c2pWHfidC1377tKU84hcxXsl22CZj4WGbiM2Lb rfAoMBe98DiG5lKFRwlJqEduGUHuOkpKSL76d3rM46B8zJq2TdEeaGORgjVUEMRQbqy8 jXskT/Mdt6QGjjfkxmwLeLcanqCNBJnBX5VWrm4hvo8u7SJxtC9lT+I+39O2UGTxpfv7 0/QQ== 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=BwRejXer5Ur/UoCRRVie9rgEWzLwsDyLLE/R9VX6Lfg=; b=PSIq0ogJxY7WksOJYecId8b19qeBxxljI2uk0UE9PBjrTGRmRwcrzSyvvXVpaxhx/I avHrW6jC4Gg7muQzUJHssqIz9uFj07Tj2KXkBLj3xnimZYdUYEm6xKU3/yIadkCSz0pA Wtr8lgryeYZ7g9hD05pEH4xzt6TNjaGblmn0duw1BIR4hPgLpgwn/jzjR1b1Np0OmtpR FizxSijyRfcso29Um4LZKoaY+/vmPAHAmYfzq1XIHF4wA+m1RpNdNZ7T+Wn3XqEPedIV Scjt+P9Y4yaBFuKJZbeO5YCCcepgycfQsSdHRrf1uDK8cvus45zqx9uS2SLuO+cKjdfG IAUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=NqU8PvVs; 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 k190-20020a6384c7000000b0045fd1d35d83si11478154pgd.795.2022.10.17.06.11.35; Mon, 17 Oct 2022 06:11:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=NqU8PvVs; 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 S231153AbiJQNKL (ORCPT + 99 others); Mon, 17 Oct 2022 09:10:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230313AbiJQNJj (ORCPT ); Mon, 17 Oct 2022 09:09:39 -0400 Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21EDE9FDC; Mon, 17 Oct 2022 06:09:27 -0700 (PDT) Received: by mail-ot1-x32e.google.com with SMTP id cb2-20020a056830618200b00661b6e5dcd8so5739070otb.8; Mon, 17 Oct 2022 06:09:27 -0700 (PDT) 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=BwRejXer5Ur/UoCRRVie9rgEWzLwsDyLLE/R9VX6Lfg=; b=NqU8PvVseFrjUduslOpG55OpX156OcwaqAIKEcPUs7QDawFmImH0oWY2PhNbZb8+ym imH1qx6uQSX7oLKheirQpKNa5H46dFnFvGBw7YbQXRdo5x0+vcVH131mIj1HgF1OFQHS ACxH1PHJNRxdAZ/z5k1vB1BRTGPbzxz2mDSUz77gXnxVKPuep722RZ76DnHn5Rs8E+BZ 1tr+SxiZtChqgALmebo++LZiPa9ep5DJ2Ax9Yifomt+ey55K0rrkbid7exZ3Em1pfvDY E82DX76B6DnL+wbkIVgmO3iEP1rTz7Z6XJ3UO6Bsd8lDR5mN9liiGzsVWoX13rKvkwZb Vc1Q== 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=BwRejXer5Ur/UoCRRVie9rgEWzLwsDyLLE/R9VX6Lfg=; b=dk9c6iWIuDLEYP7MVx+wOSBX0JxsZJs2RFBv7fqZGwCyDxnWe98H8Rs0K7hS58lgkk kWXInTg+0LKvydu+vbGef0GOVfuWyA6c71iGI7JY9k6zaK0eRkqLkjC9kP1R7qvjiVJ8 vWjI93TtazESoTj1/6/YnJNJ+unW0bw1F6wiaeDHiTC7GCQ/V4js3E+tL9H6e+s47ckA so3zYryCy2/nUIH/a+iGW3M3uOMNdDsfrnjeMcvwsyAwUX8RMyKyctRyqr2JheMpNuZ5 T4wnTX2yhhfTAwCBKARm7QkdeRdDziiijyMTih5EnZi/H0HkbnMjV3rCizX7v/vxopbC 2NkQ== X-Gm-Message-State: ACrzQf1pKl1ynmjJhDvosiU9Qh6ZJEEKfNJCBllXAZQO15tNA4XD+qg3 2h9mluhlYP0UbHaQcra/vnU= X-Received: by 2002:a05:6830:660c:b0:661:a38c:a1f9 with SMTP id cp12-20020a056830660c00b00661a38ca1f9mr4819013otb.238.1666012166409; Mon, 17 Oct 2022 06:09:26 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id l20-20020a0568301d7400b00661a3f4113bsm4649136oti.64.2022.10.17.06.09.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 06:09:26 -0700 (PDT) Sender: Guenter Roeck From: Guenter Roeck To: "Rafael J . Wysocki" Cc: Daniel Lezcano , Amit Kucheria , Zhang Rui , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH 9/9] thermal/core: Protect thermal device operations against thermal device removal Date: Mon, 17 Oct 2022 06:09:10 -0700 Message-Id: <20221017130910.2307118-10-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221017130910.2307118-1-linux@roeck-us.net> References: <20221017130910.2307118-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?1746940524549289232?= X-GMAIL-MSGID: =?utf-8?q?1746940524549289232?= Thermal device operations may be called after thermal zone device removal. After thermal zone device removal, thermal zond 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 --- 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);