From patchwork Fri Aug 5 15:38:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 12937408 X-Patchwork-Delegate: daniel.lezcano@linaro.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4E268C25B08 for ; Fri, 5 Aug 2022 15:38:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238666AbiHEPit (ORCPT ); Fri, 5 Aug 2022 11:38:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238477AbiHEPit (ORCPT ); Fri, 5 Aug 2022 11:38:49 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D91AD22B3B for ; Fri, 5 Aug 2022 08:38:47 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id j1so3701667wrw.1 for ; Fri, 05 Aug 2022 08:38:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=mGocdfdbRBVtbkhwoUtGYTP1ry+mhM2CsW1ZOWw9fd8=; b=rq0571EFJcbGGDYlW9TQ8EZZDyhlajpGv3mOEkzqe+kQWWSVw3I17MWMwXrOIyu+O9 V52QbEOodPAUpbZVsO/UsiJF2EnWa57pr8o791l3aC0NypY+xcXzSVdSCVn9XSH6K4C8 KC6Bzofgdc1QdqhqwZDKhQIeHYpXIJPIhuHrlgmXNUglaqNoh/XpANtOlRmqmFpuI0O8 Lic2WGznFUQoUGm75w7IsoMZj7u9illNFZJebK4Gp7H6IWV7EP9hlvF3fCxigk9dtgRu JPcp6SlmPkOQl5ePHR+xonSK8jjZVJuopqi6MG4YGJpkukV6P/6Ds9/h4mnDcnZ1xoy7 UnlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=mGocdfdbRBVtbkhwoUtGYTP1ry+mhM2CsW1ZOWw9fd8=; b=M15OHzi0AJ/Xcois/9HtySFvmIQ3U/Uh5UWDdUFYoLB58p7XxksTVGX3sHVKTH2+kP QzPO02jLX2J4f1FJw1VO7DKY/MD7ogkje8+vloVLM7XcxS8e1B0HAbDybGFzHyHUb+e1 u3tswojEAus8+B1rUC/m6legfYzURadAqkLz0ZyQ74MQ5qkkROuFTjx/WL2PapdHsEln EDIqF1gsKjML8HKwAPu2GEl4UOB6l4LvcMTnm+xxKmDzfAOdPg86oadzgQ0z+q3ZK2Gf oOIlkNZJqI6bNsIdyaYJE5toW5HKvsr3o1+eETBwebw6VGOr8GM2ClpcrqcugAG6vRrR q0oQ== X-Gm-Message-State: ACgBeo2ERKa/ahMsmgzB80wxqY972pZ0zbPvQSQP79+IfpzJ1SR6JhqN ISgL6+kjKajEfEbR999FLaLUeA== X-Google-Smtp-Source: AA6agR45MGXfqxXZUDoPUFyl35baFe+mTwJWDsZftCp0Te3+wJEVblJAaY6htWWgRfHYKdfStDaCMg== X-Received: by 2002:a05:6000:2a9:b0:21e:8f9b:74b9 with SMTP id l9-20020a05600002a900b0021e8f9b74b9mr4603653wry.232.1659713926310; Fri, 05 Aug 2022 08:38:46 -0700 (PDT) Received: from mai.box.freepro.com ([2a05:6e02:1041:c10:aef0:8606:da6b:79ef]) by smtp.gmail.com with ESMTPSA id h14-20020a5d430e000000b002217540b222sm1721025wrq.58.2022.08.05.08.38.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Aug 2022 08:38:45 -0700 (PDT) From: Daniel Lezcano To: daniel.lezcano@linaro.org, rafael@kernel.org Cc: rui.zhang@intel.com, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Amit Kucheria Subject: [PATCH 2/5] thermal/core: Rework the monitoring a bit Date: Fri, 5 Aug 2022 17:38:31 +0200 Message-Id: <20220805153834.2510142-2-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220805153834.2510142-1-daniel.lezcano@linaro.org> References: <20220805153834.2510142-1-daniel.lezcano@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The should_stop_polling() function wraps the function thermal_zone_device_is_enabled(). The monitor_thermal_zone() function checks if the thermal zone is enabled via the should_stop_polling() function. However, the instant after checking the thermal zone is enabled, this one can be disabled, so even if that reduces the race window, it does not prevent that and the monitoring can be set again with the thermal zone disabled. For this reason, the function should_stop_polling() is replaced by a direct check of the thermal zone mode with the mutex locks held, that prevents the situation described above. As the semantic is clear with the thermal_zone_is_enabled() function, we can remove the should_stop_polling() function and replace the check with the former function. While at it, reorder the checks to improve the readability of the monitor_thermal_zone() function. In the future, the thermal_zone_device_disable() and the thermal_zone_device_enable() functions should unset / set the polling timer directly instead of relying on the next thermal_zone_device_update() call to do that. That will make a synchronous thermal zone mode change but the locking scheme should be double checked for that which out of the scope of this change. Signed-off-by: Daniel Lezcano --- drivers/thermal/thermal_core.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 4e1a83987b99..d7029fd1c112 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -295,25 +295,16 @@ static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, cancel_delayed_work(&tz->poll_queue); } -static inline bool should_stop_polling(struct thermal_zone_device *tz) -{ - return !thermal_zone_device_is_enabled(tz); -} - static void monitor_thermal_zone(struct thermal_zone_device *tz) { - bool stop; - - stop = should_stop_polling(tz); - mutex_lock(&tz->lock); - if (!stop && tz->passive) + if (tz->mode != THERMAL_DEVICE_ENABLED) + thermal_zone_device_set_polling(tz, 0); + else if (tz->passive) thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies); - else if (!stop && tz->polling_delay_jiffies) + else if (tz->polling_delay_jiffies) thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies); - else - thermal_zone_device_set_polling(tz, 0); mutex_unlock(&tz->lock); } @@ -480,7 +471,7 @@ void thermal_zone_device_update(struct thermal_zone_device *tz, { int count; - if (should_stop_polling(tz)) + if (!thermal_zone_device_is_enabled(tz)) return; if (atomic_read(&in_suspend))