From patchwork Fri Aug 5 15:38:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 595756 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 6248EC00140 for ; Fri, 5 Aug 2022 15:38:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231585AbiHEPis (ORCPT ); Fri, 5 Aug 2022 11:38:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238385AbiHEPir (ORCPT ); Fri, 5 Aug 2022 11:38:47 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A13D237F4 for ; Fri, 5 Aug 2022 08:38:46 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id v3so3739710wrp.0 for ; Fri, 05 Aug 2022 08:38:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc; bh=aBvzMW7h3alEgW99ErQv4+ny+IZdZS0VnELxUm//1vE=; b=bJploTn1ZE5XHYS0BVPysP7rl5RlcHPt9cxUX1IVE6+RhYPxMWgrfU5DpKNg/cqYTz 84njdSjxidKNs4jPiLtI8tJ2SLTx/N43CjhvJCshuTVqF2R3rsO2q8yUzklN33kq/0jP sXPng9EMCrg7FIRTD0yjlie9OSPyIhN1jNpTIfC8dllrvJIBoWiADfVbhGV+dWlg4FQT rMiU4hnxyenVGwzGqoZdfYUtnhBfXeE4Sc8ohFCncwcBnzEHkXvbhgeWQS2vZClG43eL nIPAe+aLeX5Fkvf9CeIxFdKyNx325MDaT4g2h6J7X8Scxfe/+PlkwJ46fc9EXAV2oC2l N3tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc; bh=aBvzMW7h3alEgW99ErQv4+ny+IZdZS0VnELxUm//1vE=; b=yQAiG6uVbnb0s4eStERLIuDZ5YX9ukvkwVD02KWSov8MwnZ1zQkoikuqaC2RBpr850 tGQxcnvcCQs1GrcyPmA9UpYDMDZdvatH3JHMnorfo8mUF9PPIoDpz2BIfN2KnZ+Ed/Zc BosKRb1FvMuTxcg385HaeEhIRGcQLLm0qGHePRmIyERFkuIbpj340Df6I6w6Q2ij9XKv sKjr7sT9EnIVy2gsZVPEQyFOju0z4BTcX5+249PdYw7sUa8SSA8l+Xl8tw9endcDFisU 8sWiWss/RKfBYn6nn0eDs1ddoRQfM/B4bv6ZbwQsfBDgYptoOYP7t1oTS9XTiNoT/8iY SIeQ== X-Gm-Message-State: ACgBeo3FVQIH2pR3NvobV0e5e/TSU+qDcghzzm3HKx/4rM/Lvv9FsbZC ae0FsDDsp8GXSTyI9s4SAzweGw== X-Google-Smtp-Source: AA6agR4x5cXN7HuFjwyEH0hOFnaSWF2uTDVVYX3KO+S0TO9GQiiR8aa2fLy45UwY+57NlElSOh7xUQ== X-Received: by 2002:adf:e282:0:b0:21e:5546:5a26 with SMTP id v2-20020adfe282000000b0021e55465a26mr4901672wri.77.1659713924862; Fri, 05 Aug 2022 08:38:44 -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.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Aug 2022 08:38:44 -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 1/5] thermal/core: Rearm the monitoring only one time Date: Fri, 5 Aug 2022 17:38:30 +0200 Message-Id: <20220805153834.2510142-1-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The current code calls monitor_thermal_zone() inside the handle_thermal_trip() function. But this one is called in a loop for each trip point which means the monitoring is rearmed several times for nothing (assuming there could be several passive and active trip points). Move the monitor_thermal_zone() function out of the handle_thermal_trip() function and after the thermal trip loop, so the timer will be disabled or rearmed one time. Signed-off-by: Daniel Lezcano --- drivers/thermal/thermal_core.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index b4c68410c158..4e1a83987b99 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -383,11 +383,6 @@ static void handle_thermal_trip(struct thermal_zone_device *tz, int trip) handle_critical_trips(tz, trip, trip_temp, type); else handle_non_critical_trips(tz, trip); - /* - * Alright, we handled this trip successfully. - * So, start monitoring again. - */ - monitor_thermal_zone(tz); } static void update_temperature(struct thermal_zone_device *tz) @@ -503,6 +498,8 @@ void thermal_zone_device_update(struct thermal_zone_device *tz, for (count = 0; count < tz->num_trips; count++) handle_thermal_trip(tz, count); + + monitor_thermal_zone(tz); } EXPORT_SYMBOL_GPL(thermal_zone_device_update); 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: 596372 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)) From patchwork Fri Aug 5 15:38:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 595755 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 48DB1C00140 for ; Fri, 5 Aug 2022 15:38:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240504AbiHEPiw (ORCPT ); Fri, 5 Aug 2022 11:38:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232065AbiHEPiu (ORCPT ); Fri, 5 Aug 2022 11:38:50 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FCC5237F7 for ; Fri, 5 Aug 2022 08:38:49 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id v5so1608651wmj.0 for ; Fri, 05 Aug 2022 08:38:49 -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=N6F1KWOnquRUouMVh2G6/70txn8l+RtXgOaeM52+cMU=; b=V2XfY7982nfVB9EPf9DcG6vgZp5KQoMpU91mPaqcS+EgJmGv7BXdpdU7PvOWsrQMZu Duf0uuqVQpOprvC+RUl+/z9hvlsZty5RyaOL+gQjIynYhJqrArH/Hb3DF+Jh1BhkR72I 5dvJtxs0Xxo74w9qAB2iymM1uf/j5cB6xTLMziat5Ms+WEezdOamVqro1v3dT4Zgtxei qBUBVXxbZ1uY6o4/ONmlO8nEp9aVnhKmLDAebff3K2gswoALA6+HihaBF85aTkehxU0y rRufOHKpU/p8LN23eFpbxVeJcMpAQiHLGFdfTKRN6ohtjHsaxfWJEtyoP6jiVxbaeKPg EqEA== 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=N6F1KWOnquRUouMVh2G6/70txn8l+RtXgOaeM52+cMU=; b=amwNK2fn6WUUtt/I+wjwWNEMXg6HLSjIT9Pw0Ga8c0kijc/JCcgSUsJMjnSDvjzJgo bYAVv88k7T28iDhdU0I0iFvRpDTdayFGy1DFPkkSD+GmzX43te3otkkBfXkIpIDdizY3 9PUR316n47PMP+Yw/tDv9dJKc7Kjts/0PSdS4UZSY0hLA0fs8KtvjQH7WFJGlGXMZ55V Xt2VuxYEFZ58MjDhn8i7y4yk2hjTa3S0aKFVUzFHQgNuJwl+UfUbSggo+GU6zLMdjJv2 dPAXgeN/79hkrZtKV79p7TIHBZw8YemTdVizf6i2OGWkjlqBP/s7cCfCZNTKogFWzPG6 BM6w== X-Gm-Message-State: ACgBeo0RKWdlRJr9x5TRWrieJlMsAYVfNYnZgRzt2f+5Bd4wfT0HUCxd 7e+RRuRmrrDu1LRgVIntJyrEHQ== X-Google-Smtp-Source: AA6agR7UD70typjSKOyMwB0tdF6biWcDynYwwuQQGpaeIGKp9oaEzgOTLqU7sBujDYIh1O48hFSdxA== X-Received: by 2002:a05:600c:a146:b0:3a3:1e79:4446 with SMTP id ib6-20020a05600ca14600b003a31e794446mr5193441wmb.158.1659713927788; Fri, 05 Aug 2022 08:38:47 -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.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Aug 2022 08:38:47 -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 , Lukasz Luba Subject: [PATCH 3/5] thermal/governors: Group the thermal zone lock inside the throttle function Date: Fri, 5 Aug 2022 17:38:32 +0200 Message-Id: <20220805153834.2510142-3-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 thermal zone lock is taken in the different places in the throttling path. At the first glance it does not hurt to move them at the beginning and the end of the 'throttle' function. That will allow a consolidation of the lock in the next following changes. Signed-off-by: Daniel Lezcano --- drivers/thermal/gov_bang_bang.c | 8 ++----- drivers/thermal/gov_fair_share.c | 1 + drivers/thermal/gov_power_allocator.c | 34 ++++++++++++--------------- drivers/thermal/gov_step_wise.c | 8 ++----- 4 files changed, 20 insertions(+), 31 deletions(-) diff --git a/drivers/thermal/gov_bang_bang.c b/drivers/thermal/gov_bang_bang.c index 991a1c54296d..f0bff2e0475b 100644 --- a/drivers/thermal/gov_bang_bang.c +++ b/drivers/thermal/gov_bang_bang.c @@ -31,8 +31,6 @@ static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip) trip, trip_temp, tz->temperature, trip_hyst); - mutex_lock(&tz->lock); - list_for_each_entry(instance, &tz->thermal_instances, tz_node) { if (instance->trip != trip) continue; @@ -65,8 +63,6 @@ static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip) instance->cdev->updated = false; /* cdev needs update */ mutex_unlock(&instance->cdev->lock); } - - mutex_unlock(&tz->lock); } /** @@ -100,10 +96,10 @@ static int bang_bang_control(struct thermal_zone_device *tz, int trip) { struct thermal_instance *instance; - thermal_zone_trip_update(tz, trip); - mutex_lock(&tz->lock); + thermal_zone_trip_update(tz, trip); + list_for_each_entry(instance, &tz->thermal_instances, tz_node) thermal_cdev_update(instance->cdev); diff --git a/drivers/thermal/gov_fair_share.c b/drivers/thermal/gov_fair_share.c index 6a2abcfc648f..5d5ddd648cd2 100644 --- a/drivers/thermal/gov_fair_share.c +++ b/drivers/thermal/gov_fair_share.c @@ -113,6 +113,7 @@ static int fair_share_throttle(struct thermal_zone_device *tz, int trip) } mutex_unlock(&tz->lock); + return 0; } diff --git a/drivers/thermal/gov_power_allocator.c b/drivers/thermal/gov_power_allocator.c index 1d5052470967..d3aca236e274 100644 --- a/drivers/thermal/gov_power_allocator.c +++ b/drivers/thermal/gov_power_allocator.c @@ -392,8 +392,6 @@ static int allocate_power(struct thermal_zone_device *tz, int i, num_actors, total_weight, ret = 0; int trip_max_desired_temperature = params->trip_max_desired_temperature; - mutex_lock(&tz->lock); - num_actors = 0; total_weight = 0; list_for_each_entry(instance, &tz->thermal_instances, tz_node) { @@ -404,10 +402,8 @@ static int allocate_power(struct thermal_zone_device *tz, } } - if (!num_actors) { - ret = -ENODEV; - goto unlock; - } + if (!num_actors) + return -ENODEV; /* * We need to allocate five arrays of the same size: @@ -421,10 +417,8 @@ static int allocate_power(struct thermal_zone_device *tz, BUILD_BUG_ON(sizeof(*req_power) != sizeof(*extra_actor_power)); BUILD_BUG_ON(sizeof(*req_power) != sizeof(*weighted_req_power)); req_power = kcalloc(num_actors * 5, sizeof(*req_power), GFP_KERNEL); - if (!req_power) { - ret = -ENOMEM; - goto unlock; - } + if (!req_power) + return -ENOMEM; max_power = &req_power[num_actors]; granted_power = &req_power[2 * num_actors]; @@ -496,8 +490,6 @@ static int allocate_power(struct thermal_zone_device *tz, control_temp - tz->temperature); kfree(req_power); -unlock: - mutex_unlock(&tz->lock); return ret; } @@ -576,7 +568,6 @@ static void allow_maximum_power(struct thermal_zone_device *tz, bool update) struct power_allocator_params *params = tz->governor_data; u32 req_power; - mutex_lock(&tz->lock); list_for_each_entry(instance, &tz->thermal_instances, tz_node) { struct thermal_cooling_device *cdev = instance->cdev; @@ -598,7 +589,6 @@ static void allow_maximum_power(struct thermal_zone_device *tz, bool update) mutex_unlock(&instance->cdev->lock); } - mutex_unlock(&tz->lock); } /** @@ -707,17 +697,19 @@ static void power_allocator_unbind(struct thermal_zone_device *tz) static int power_allocator_throttle(struct thermal_zone_device *tz, int trip) { - int ret; + int ret = 0; int switch_on_temp, control_temp; struct power_allocator_params *params = tz->governor_data; bool update; + mutex_lock(&tz->lock); + /* * We get called for every trip point but we only need to do * our calculations once */ if (trip != params->trip_max_desired_temperature) - return 0; + goto out; ret = tz->ops->get_trip_temp(tz, params->trip_switch_on, &switch_on_temp); @@ -726,7 +718,7 @@ static int power_allocator_throttle(struct thermal_zone_device *tz, int trip) tz->passive = 0; reset_pid_controller(params); allow_maximum_power(tz, update); - return 0; + goto out; } tz->passive = 1; @@ -737,10 +729,14 @@ static int power_allocator_throttle(struct thermal_zone_device *tz, int trip) dev_warn(&tz->device, "Failed to get the maximum desired temperature: %d\n", ret); - return ret; + goto out; } - return allocate_power(tz, control_temp); + ret = allocate_power(tz, control_temp); + + mutex_unlock(&tz->lock); +out: + return ret; } static struct thermal_governor thermal_gov_power_allocator = { diff --git a/drivers/thermal/gov_step_wise.c b/drivers/thermal/gov_step_wise.c index 9729b46d0258..597a0ebec7a4 100644 --- a/drivers/thermal/gov_step_wise.c +++ b/drivers/thermal/gov_step_wise.c @@ -117,8 +117,6 @@ static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip) dev_dbg(&tz->device, "Trip%d[type=%d,temp=%d]:trend=%d,throttle=%d\n", trip, trip_type, trip_temp, trend, throttle); - mutex_lock(&tz->lock); - list_for_each_entry(instance, &tz->thermal_instances, tz_node) { if (instance->trip != trip) continue; @@ -145,8 +143,6 @@ static void thermal_zone_trip_update(struct thermal_zone_device *tz, int trip) instance->cdev->updated = false; /* cdev needs update */ mutex_unlock(&instance->cdev->lock); } - - mutex_unlock(&tz->lock); } /** @@ -164,10 +160,10 @@ static int step_wise_throttle(struct thermal_zone_device *tz, int trip) { struct thermal_instance *instance; - thermal_zone_trip_update(tz, trip); - mutex_lock(&tz->lock); + thermal_zone_trip_update(tz, trip); + list_for_each_entry(instance, &tz->thermal_instances, tz_node) thermal_cdev_update(instance->cdev); From patchwork Fri Aug 5 15:38:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 596371 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 065DAC00140 for ; Fri, 5 Aug 2022 15:38:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240748AbiHEPiz (ORCPT ); Fri, 5 Aug 2022 11:38:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240575AbiHEPiw (ORCPT ); Fri, 5 Aug 2022 11:38:52 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6F102496B for ; Fri, 5 Aug 2022 08:38:50 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id m41-20020a05600c3b2900b003a4e094256eso1469273wms.0 for ; Fri, 05 Aug 2022 08:38:50 -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=QbP1CQ2OLF0bQuXpLH2jFFQL9/YSjb7PHhFwyob8Fo4=; b=z36qiBVG4vFuawViw3ZQATfX0PpGW7/s4nfkCOT7qhETFDOpdfU71DBijBjEJRIi3n M6Bcme2R0/ooDP8z63GV/snYpXuX+cswNMqo9vuPKrit4f/IxQACiF44pXiACYfPbROu UbN++L87vHLuJQLovqwjlql8Z0weA4fWT+5EoNu6dH2cvVJp9QGWBKn/wV8+LtlxNVDG a7IWzAP1i761s7RgfAs5S0zQSYHLXHsUB/3l8rEGtJ9oCRXvyfSmvC8WjoPrgIJKlRIF CCYBTigvHSAtCSDcF914WS1I1GT3VREiKCmGCRpY99kgGrYyKD1gaBeRgKgLuxSe0GRc eimg== 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=QbP1CQ2OLF0bQuXpLH2jFFQL9/YSjb7PHhFwyob8Fo4=; b=f2A+9jwqeg/oqoKyQ70U+K4B1+YMr+KYFvlOEL0XWn1GHYHXfD8YUgaCzr1oVFfoi3 oR1XfwTtBzWZ3GFscfkKOn39o2VE9tnlsnONhO9NNR67o8+xteNgOf5/7G2RmyTDlmlG Z+N8D5X57Rcb5xOY+wJxXuC0y10Z6oQy5HYOMfaZ2gWAa1hyjpJHUD4qiuRoHeS+FEiJ NqRsK/qfFwwMVTnBuzspTylOdbaM5B/nUIVVCQyk1NYBnar+kFzuCwbfTz3lenl0awHy dKkZTdJt+eeTDftSRHzedcmnFF3BktJ54w4l/hROHMCvf6ntLOADHdhrt1/ha5zYptC+ fxMw== X-Gm-Message-State: ACgBeo199MKTX7nz6nRoEtOmk0ncEds07atLw0n9UUPMIAXve+gWWh8g WwtGhwHmuZnJjpv3ledfLiMSJQ== X-Google-Smtp-Source: AA6agR7pGdNaLkbadQF7fzFpgQUmEhgMiAcG1S/YAt5J2ERgYi5eijiRPzHa+LtPcc2uUvziv7EXEw== X-Received: by 2002:a05:600c:58e:b0:3a5:2c3:5e17 with SMTP id o14-20020a05600c058e00b003a502c35e17mr8078047wmd.200.1659713929361; Fri, 05 Aug 2022 08:38:49 -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.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Aug 2022 08:38:48 -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 , Lukasz Luba Subject: [PATCH 4/5] thermal/core: Move the thermal zone lock out of the governors Date: Fri, 5 Aug 2022 17:38:33 +0200 Message-Id: <20220805153834.2510142-4-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 All the governors throttling ops are taking/releasing the lock at the beginning and the end of the function. We can move the mutex to the throttling call site instead. Signed-off-by: Daniel Lezcano --- drivers/thermal/gov_bang_bang.c | 4 +--- drivers/thermal/gov_fair_share.c | 4 +--- drivers/thermal/gov_power_allocator.c | 16 ++++++---------- drivers/thermal/gov_step_wise.c | 4 +--- drivers/thermal/thermal_core.c | 2 ++ 5 files changed, 11 insertions(+), 19 deletions(-) diff --git a/drivers/thermal/gov_bang_bang.c b/drivers/thermal/gov_bang_bang.c index f0bff2e0475b..a08bbe33be96 100644 --- a/drivers/thermal/gov_bang_bang.c +++ b/drivers/thermal/gov_bang_bang.c @@ -96,15 +96,13 @@ static int bang_bang_control(struct thermal_zone_device *tz, int trip) { struct thermal_instance *instance; - mutex_lock(&tz->lock); + lockdep_assert_held(&tz->lock); thermal_zone_trip_update(tz, trip); list_for_each_entry(instance, &tz->thermal_instances, tz_node) thermal_cdev_update(instance->cdev); - mutex_unlock(&tz->lock); - return 0; } diff --git a/drivers/thermal/gov_fair_share.c b/drivers/thermal/gov_fair_share.c index 5d5ddd648cd2..a4ee4661e9cc 100644 --- a/drivers/thermal/gov_fair_share.c +++ b/drivers/thermal/gov_fair_share.c @@ -82,7 +82,7 @@ static int fair_share_throttle(struct thermal_zone_device *tz, int trip) int total_instance = 0; int cur_trip_level = get_trip_level(tz); - mutex_lock(&tz->lock); + lockdep_assert_held(&tz->lock); list_for_each_entry(instance, &tz->thermal_instances, tz_node) { if (instance->trip != trip) @@ -112,8 +112,6 @@ static int fair_share_throttle(struct thermal_zone_device *tz, int trip) mutex_unlock(&cdev->lock); } - mutex_unlock(&tz->lock); - return 0; } diff --git a/drivers/thermal/gov_power_allocator.c b/drivers/thermal/gov_power_allocator.c index d3aca236e274..2d1aeaba38a8 100644 --- a/drivers/thermal/gov_power_allocator.c +++ b/drivers/thermal/gov_power_allocator.c @@ -697,19 +697,19 @@ static void power_allocator_unbind(struct thermal_zone_device *tz) static int power_allocator_throttle(struct thermal_zone_device *tz, int trip) { - int ret = 0; + int ret; int switch_on_temp, control_temp; struct power_allocator_params *params = tz->governor_data; bool update; - mutex_lock(&tz->lock); + lockdep_assert_held(&tz->lock); /* * We get called for every trip point but we only need to do * our calculations once */ if (trip != params->trip_max_desired_temperature) - goto out; + return 0; ret = tz->ops->get_trip_temp(tz, params->trip_switch_on, &switch_on_temp); @@ -718,7 +718,7 @@ static int power_allocator_throttle(struct thermal_zone_device *tz, int trip) tz->passive = 0; reset_pid_controller(params); allow_maximum_power(tz, update); - goto out; + return 0; } tz->passive = 1; @@ -729,14 +729,10 @@ static int power_allocator_throttle(struct thermal_zone_device *tz, int trip) dev_warn(&tz->device, "Failed to get the maximum desired temperature: %d\n", ret); - goto out; + return ret; } - ret = allocate_power(tz, control_temp); - - mutex_unlock(&tz->lock); -out: - return ret; + return allocate_power(tz, control_temp); } static struct thermal_governor thermal_gov_power_allocator = { diff --git a/drivers/thermal/gov_step_wise.c b/drivers/thermal/gov_step_wise.c index 597a0ebec7a4..cdd3354bc27f 100644 --- a/drivers/thermal/gov_step_wise.c +++ b/drivers/thermal/gov_step_wise.c @@ -160,15 +160,13 @@ static int step_wise_throttle(struct thermal_zone_device *tz, int trip) { struct thermal_instance *instance; - mutex_lock(&tz->lock); + lockdep_assert_held(&tz->lock); thermal_zone_trip_update(tz, trip); list_for_each_entry(instance, &tz->thermal_instances, tz_node) thermal_cdev_update(instance->cdev); - mutex_unlock(&tz->lock); - return 0; } diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index d7029fd1c112..9d554f97e081 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -311,8 +311,10 @@ static void monitor_thermal_zone(struct thermal_zone_device *tz) static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip) { + mutex_lock(&tz->lock); tz->governor ? tz->governor->throttle(tz, trip) : def_governor->throttle(tz, trip); + mutex_unlock(&tz->lock); } void thermal_zone_device_critical(struct thermal_zone_device *tz) From patchwork Fri Aug 5 15:38:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 595754 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 AA889C25B07 for ; Fri, 5 Aug 2022 15:38:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240682AbiHEPi4 (ORCPT ); Fri, 5 Aug 2022 11:38:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240713AbiHEPiy (ORCPT ); Fri, 5 Aug 2022 11:38:54 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A91D025597 for ; Fri, 5 Aug 2022 08:38:52 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id ay36-20020a05600c1e2400b003a4e30d7995so4139446wmb.5 for ; Fri, 05 Aug 2022 08:38:52 -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=lgimsPqam7+Sa77+tu4iuFhMijtMea69pSD3eiGHXzE=; b=O9zYQR032NwJdRPutt8I2o96IHDN7NW8EARKVm/xBo6VvUvxIGe4vbbH0OZMGv7klp pzI38Q/iQdEN0D+Tu4kSoYBUHk1gzk8ZKCywkoVxJ8+uDWNhvfWOfWbOS6Z+BFCf28B7 1DVMHIG+i8sh5/LT3hiSmW//KQF11NXGkqApWRheUOWf7vWJ7BzYoaY1VHmgC81SYgZL eSTagqc39Yz2nX6Dgi1kgdiqG3WCw6t9o0FNeJQxjQADlPbrRCw3r85TIAAfHUDLpfZZ 6X9+9NYquDQvnB4truodAn2anclJtrLZmGGj1eOJVaUQfjsAoWvwTcPD6XO/NejaRpZU AqEA== 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=lgimsPqam7+Sa77+tu4iuFhMijtMea69pSD3eiGHXzE=; b=CeEMXFZfH+AMfTUcRoHrT5gXm33wbo4KCwcNrB7SXRL/aPbohwdiU4xTlYkrBJCAng LykuDonu/8N/wJvOAjKDfOXGJurduqvjcickShVizth9Xir1bL4q0WwJ7bw7nkhRvHlm hTb1BizNr83nawR5FTmBWPsfNAoXHOiOEQC9qA40N3pW/nfoujoXfKU+i+UlWf9Hh9SN 6djyb85jx2JMIMdpe2QrdS8Ahnh+XnPw8WW7Lyyxgnja6cALxtN+ieEakxvqbEGZUcwj pO8ZRnOsM9BkDHkPQsmYLRt1biYBrzbFV3BzuheYPWJ0Jozg5zj2DKNjg+bXvtt5aTc8 jW6Q== X-Gm-Message-State: ACgBeo08a/WsIM5eWDb37ecPEoFg9TWVP3xxjC/lcpfHGWYgh0jFESP1 cL8QNAwNmVF9R9R8fDZlFnDwEA== X-Google-Smtp-Source: AA6agR5Nv53KDQOvqh+4wURfJkqk6vMRuAiEMyXb8st+9yL05Ub0E4OSAi+g/6Ygzi6i40RoQC5w2A== X-Received: by 2002:a05:600c:4153:b0:3a5:1ea9:2824 with SMTP id h19-20020a05600c415300b003a51ea92824mr2329740wmm.80.1659713931082; Fri, 05 Aug 2022 08:38:51 -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.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Aug 2022 08:38:50 -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 5/5] thermal/core: Move the mutex inside the thermal_zone_device_update() function Date: Fri, 5 Aug 2022 17:38:34 +0200 Message-Id: <20220805153834.2510142-5-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 All the different calls inside the thermal_zone_device_update() function take the mutex. The previous changes move the mutex out of the different functions, like the throttling ops. Now that the mutexes are all at the same level in the call stack for the thermal_zone_device_update() function, they can be moved inside this one. That has the benefit of: 1. Simplify the code by not having a plethora of places where the lock is taken 2. Probably closes more race windows because releasing the lock from one line to another can give the opportunity to the thermal zone to change its state in the meantime. For example, the thermal zone can be enabled right after checking it is disabled. Signed-off-by: Daniel Lezcano --- drivers/thermal/thermal_core.c | 32 +++++-------- drivers/thermal/thermal_core.h | 2 + drivers/thermal/thermal_helpers.c | 75 ++++++++++++++++++------------- drivers/thermal/thermal_sysfs.c | 6 ++- 4 files changed, 62 insertions(+), 53 deletions(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 9d554f97e081..60110ac53e23 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -297,24 +297,18 @@ static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, static void monitor_thermal_zone(struct thermal_zone_device *tz) { - mutex_lock(&tz->lock); - 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 (tz->polling_delay_jiffies) thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies); - - mutex_unlock(&tz->lock); } static void handle_non_critical_trips(struct thermal_zone_device *tz, int trip) { - mutex_lock(&tz->lock); tz->governor ? tz->governor->throttle(tz, trip) : def_governor->throttle(tz, trip); - mutex_unlock(&tz->lock); } void thermal_zone_device_critical(struct thermal_zone_device *tz) @@ -382,7 +376,7 @@ static void update_temperature(struct thermal_zone_device *tz) { int temp, ret; - ret = thermal_zone_get_temp(tz, &temp); + ret = __thermal_zone_get_temp(tz, &temp); if (ret) { if (ret != -EAGAIN) dev_warn(&tz->device, @@ -391,10 +385,8 @@ static void update_temperature(struct thermal_zone_device *tz) return; } - mutex_lock(&tz->lock); tz->last_temperature = tz->temperature; tz->temperature = temp; - mutex_unlock(&tz->lock); trace_thermal_temperature(tz); @@ -457,15 +449,9 @@ EXPORT_SYMBOL_GPL(thermal_zone_device_disable); int thermal_zone_device_is_enabled(struct thermal_zone_device *tz) { - enum thermal_device_mode mode; - - mutex_lock(&tz->lock); - - mode = tz->mode; + lockdep_assert_held(&tz->lock); - mutex_unlock(&tz->lock); - - return mode == THERMAL_DEVICE_ENABLED; + return tz->mode == THERMAL_DEVICE_ENABLED; } void thermal_zone_device_update(struct thermal_zone_device *tz, @@ -473,9 +459,6 @@ void thermal_zone_device_update(struct thermal_zone_device *tz, { int count; - if (!thermal_zone_device_is_enabled(tz)) - return; - if (atomic_read(&in_suspend)) return; @@ -483,9 +466,14 @@ void thermal_zone_device_update(struct thermal_zone_device *tz, "'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); + __thermal_zone_set_trips(tz); tz->notify_event = event; @@ -493,6 +481,8 @@ void thermal_zone_device_update(struct thermal_zone_device *tz, handle_thermal_trip(tz, count); monitor_thermal_zone(tz); +out: + mutex_unlock(&tz->lock); } EXPORT_SYMBOL_GPL(thermal_zone_device_update); diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h index 2241d2dce017..1571917bd3c8 100644 --- a/drivers/thermal/thermal_core.h +++ b/drivers/thermal/thermal_core.h @@ -112,6 +112,8 @@ int thermal_build_list_of_policies(char *buf); /* 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); /* sysfs I/F */ int thermal_zone_create_device_groups(struct thermal_zone_device *, int); diff --git a/drivers/thermal/thermal_helpers.c b/drivers/thermal/thermal_helpers.c index 690890f054a3..702c70bdca48 100644 --- a/drivers/thermal/thermal_helpers.c +++ b/drivers/thermal/thermal_helpers.c @@ -64,27 +64,17 @@ 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. - * - * Return: On success returns 0, an error code otherwise - */ -int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) +int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) { int ret = -EINVAL; int count; int crit_temp = INT_MAX; enum thermal_trip_type type; + lockdep_assert_held(&tz->lock); + if (!tz || IS_ERR(tz) || !tz->ops->get_temp) - goto exit; - - mutex_lock(&tz->lock); + return -EINVAL; ret = tz->ops->get_temp(tz, temp); @@ -107,35 +97,42 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) *temp = tz->emul_temperature; } - mutex_unlock(&tz->lock); -exit: return ret; } -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 + * 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. * - * 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). + * When a valid thermal zone reference is passed, it will fetch its + * temperature and fill @temp. * - * It does not return a value + * Return: On success returns 0, an error code otherwise */ -void thermal_zone_set_trips(struct thermal_zone_device *tz) +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); + mutex_unlock(&tz->lock); + + return ret; +} +EXPORT_SYMBOL_GPL(thermal_zone_get_temp); + +void __thermal_zone_set_trips(struct thermal_zone_device *tz) { int low = -INT_MAX; int high = INT_MAX; int trip_temp, hysteresis; int i, ret; - mutex_lock(&tz->lock); - + lockdep_assert_held(&tz->lock); + if (!tz->ops->set_trips || !tz->ops->get_trip_hyst) - goto exit; + return; for (i = 0; i < tz->num_trips; i++) { int trip_low; @@ -154,7 +151,7 @@ void thermal_zone_set_trips(struct thermal_zone_device *tz) /* No need to change trip points */ if (tz->prev_low_trip == low && tz->prev_high_trip == high) - goto exit; + return; tz->prev_low_trip = low; tz->prev_high_trip = high; @@ -169,8 +166,24 @@ void thermal_zone_set_trips(struct thermal_zone_device *tz) ret = tz->ops->set_trips(tz, low, high); if (ret) dev_err(&tz->device, "Failed to set trips: %d\n", ret); +} -exit: +/** + * 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); } diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c index 3c513561d346..f094f7cbc455 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -49,7 +49,11 @@ static ssize_t mode_show(struct device *dev, struct device_attribute *attr, char *buf) { struct thermal_zone_device *tz = to_thermal_zone(dev); - int enabled = thermal_zone_device_is_enabled(tz); + int enabled; + + mutex_lock(&tz->lock); + enabled = thermal_zone_device_is_enabled(tz); + mutex_unlock(&tz->lock); return sprintf(buf, "%s\n", enabled ? "enabled" : "disabled"); }