Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2790538pxb; Mon, 18 Oct 2021 01:45:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyxJyoUI+RCX3NFz0u/g0grfZ/REGD3oQOLaMNKp8MpGq4Q3Lm7Kj9WbdrXPc6u+NNXqNBo X-Received: by 2002:a17:90b:2382:: with SMTP id mr2mr8759922pjb.186.1634546718778; Mon, 18 Oct 2021 01:45:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634546718; cv=none; d=google.com; s=arc-20160816; b=bUsfnFlrCi83i0hyMU5NP0oncySH5nzUTsOXnqx/5Xa9cdOXLGUMgIwR98yMjpGVSf 8i6O9ItnmX8Q13grGAGfvPT9iPPeF33S6xDed6F2CqhTFRRA9fCpDEoQIrAaWf4W/CAn Oeg0N2fV2lmvZ6UuJsQPD3i8ClshDgqOg8vwSeV48A5EmcqPUPESNzTTV6kBFKBdMBwu 71kYGpFnIYhpH6dMqSsRAk0jlhG8V/EEgUCwcZG9NECi520mLqVDKQgDBcvpWp4Y9Q8F WjySPZgEciFyQIHlAS2Ksc1ciT7jfkv1BilO55o0yL+KBsE2p3hEeJMoPE33D/1HlGhF avbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=9JaIx8VpWvuUdA6UzYtoFnNy3De6CwfZcUSCPIca5YM=; b=JBTh9FzwEO7Wct9AlvXsOWALm8Axmw3vi4bTNxmScYiqUaLj5Z1kSXY7ZCJj5CTY9p xgnD6HiRfdY7LUHqBnyQH+HGtm0kGGAVe3k+vPoJaP+Fma5YU6YjlWgj01j1/xNmRNOr W3KoM5zLSidKDZok4OmGRc9TC+AVLJzwJ2LG77odtvij2SqFytdCPR9KSGl7kRABHxNL SWw8JQGCaBMuOCNvJW0sKokio/fTPjLFcT+tB+e30F7rBT3E4oBLKn3Uw4jwfQ9b+lTA 1bqOrEW9m+AoN4HPYFwSTQ9Yjx7fxogXlPuYgopeglUdiJDgdHgducJtfCJeTpCQ7SnC b/TA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kh157huE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g35si20013475pgl.237.2021.10.18.01.45.06; Mon, 18 Oct 2021 01:45:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kh157huE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230392AbhJRIqH (ORCPT + 99 others); Mon, 18 Oct 2021 04:46:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52514 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230386AbhJRIqG (ORCPT ); Mon, 18 Oct 2021 04:46:06 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08911C061765 for ; Mon, 18 Oct 2021 01:43:56 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id r7so39927931wrc.10 for ; Mon, 18 Oct 2021 01:43:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=9JaIx8VpWvuUdA6UzYtoFnNy3De6CwfZcUSCPIca5YM=; b=kh157huEHS8uKAqERN+t0jBIFXD5TSWZkL/0sV8/zdSKbW+3jx/odYTuVvRWTNgObP qIp882UtDITS84UZMJTv9uCqB5NttEoyn1w189L4jnBZc1VpZOeleSoDVt6lbk4+OqG/ OBQaJYfWg9+AcrkFgdLnbxbKozmMkoM2vySWjcDVhQPwOaPZQnXKZHSDjbd02T9AbV3H qW9eB/6/3Zl+Qi/HUko3NqN/ps5K+dnK6+HJBVWF/6XzvbQ/R/ioQU807ExcN3hqOOac JR0KYfN9kTHJSCV15JoeNEcxn60+8AWVUzcLsfDp4+OLhWyyJLcot22jPpu5o0jewFkw jsgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=9JaIx8VpWvuUdA6UzYtoFnNy3De6CwfZcUSCPIca5YM=; b=wRWgcBjfZnw6yl1JV8ZL5MOY+ixb9gPIOYzpkfAJ8/Q5k8yKmInguC43WjM/A4PmMr 19kTr4pdAjwtvkJPl7ftdMYhYIhbnYqgYkEFFvAnZx0ZgKRwjfqUk6FnQQaBe1zz4rMa qIZhkSrtOxya8r7YyLrUZvdfJkAWcPZLdsMJj5u1JwSq2UJBKmUjuuiSMUtM41Xbu3f6 VpFywRKgzRKTf7nBtt0y5WWScwBoIgxaqVFzlPWzl4I9n0QAyy7+Eeh2h5T8Ibg+Li8s 9KaxivnmWoTGfzxS0WppUwoOvBoufrWSXNvrU0/MT6Ge00ROQYpOfdKorGgNOkOwuzCF t+Tw== X-Gm-Message-State: AOAM531tooQ9+91a/8eoDVqOIDKfMlfFUVqZBXwZAjLiQF+7eTAASVvg tB8yQ452QlU3xSogWEPx1UcBJQ== X-Received: by 2002:a5d:4a0c:: with SMTP id m12mr34451489wrq.27.1634546634268; Mon, 18 Oct 2021 01:43:54 -0700 (PDT) Received: from ?IPv6:2a01:e34:ed2f:f020:b2b3:1133:5bdb:b2f2? ([2a01:e34:ed2f:f020:b2b3:1133:5bdb:b2f2]) by smtp.googlemail.com with ESMTPSA id c3sm10667002wrw.66.2021.10.18.01.43.52 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 18 Oct 2021 01:43:53 -0700 (PDT) Subject: Re: [PATCH] thermal: imx: Fix temperature measurements on i.MX6 after alarm To: Petr Benes Cc: =?UTF-8?B?TWljaGFsIFZva8OhxI0=?= , Andrzej Pietrasiewicz , linux-pm@vger.kernel.org, Shawn Guo , Amit Kucheria , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, =?UTF-8?Q?Petr_Bene=c5=a1?= , stable@vger.kernel.org References: <20211008081137.1948848-1-michal.vokac@ysoft.com> From: Daniel Lezcano Message-ID: Date: Mon, 18 Oct 2021 10:43:52 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 18/10/2021 10:36, Petr Benes wrote: > On Sun, 17 Oct 2021 at 00:23, Daniel Lezcano wrote: >> >> On 08/10/2021 10:11, Michal Vokáč wrote: >>> From: Petr Beneš >>> >>> SoC temperature readout may not work after thermal alarm fires interrupt. >>> This harms userspace as well as CPU cooling device. >>> >>> Two issues with the logic involved. First, there is no protection against >>> concurent measurements, hence one can switch the sensor off while >>> the other one tries to read temperature later. Second, the interrupt path >>> usually fails. At the end the sensor is powered off and thermal IRQ is >>> disabled. One has to reenable the thermal zone by the sysfs interface. >>> >>> Most of troubles come from commit d92ed2c9d3ff ("thermal: imx: Use >>> driver's local data to decide whether to run a measurement") >> >> Are these troubles observed and reproduced ? Or is it your understanding >> from reading the code ? > > Yes, it is observed. We are using: > CONFIG_CPU_THERMAL=y > CONFIG_CPU_FREQ_THERMAL=y > If the SoC temperature oscillates around the passive trip point, it is not > possible to read out /sys/class/thermal/thermal_zone0/temp after > a while (minutes). For reproduction either heat the device up a bit or set > the passive trip point lower. Ok, I have an i.MX7 here to resurrect. I'll give a try. >> get_temp() and tz enable/disable are protected against races in the core >> code via the tz mutex > > imx_get_temp() itself doesn't handle concurrent invocations properly, despite > it may seem it does. The sensor is switched on/off without control. > That is a flaw in imx_get_temp(). > No evidence the core locking is wrong. > > data->irq_enabled is set to false in imx_thermal_alarm_irq() each time > the interrupt arrives. imx_get_temp() does wrong decision based > on the value later. > >> >>> It uses data->irq_enabled as the "local data". Indeed, its value is >>> related to the state of the sensor loosely under normal operation and, >>> frankly, gets unleashed when the thermal interrupt arrives. >>> >>> Current patch adds the "local data" (new member sensor_on in >>> imx_thermal_data) and sets its value in controlled manner.> >>> Fixes: d92ed2c9d3ff ("thermal: imx: Use driver's local data to decide whether to run a measurement") >>> Cc: petrben@gmail.com >>> Cc: stable@vger.kernel.org >>> Signed-off-by: Petr Beneš >>> Signed-off-by: Michal Vokáč >>> --- >>> drivers/thermal/imx_thermal.c | 30 ++++++++++++++++++++++++++---- >>> 1 file changed, 26 insertions(+), 4 deletions(-) >>> >>> diff --git a/drivers/thermal/imx_thermal.c b/drivers/thermal/imx_thermal.c >>> index 2c7473d86a59..df5658e21828 100644 >>> --- a/drivers/thermal/imx_thermal.c >>> +++ b/drivers/thermal/imx_thermal.c >>> @@ -209,6 +209,8 @@ struct imx_thermal_data { >>> struct clk *thermal_clk; >>> const struct thermal_soc_data *socdata; >>> const char *temp_grade; >>> + struct mutex sensor_lock; >>> + bool sensor_on; >>> }; >>> >>> static void imx_set_panic_temp(struct imx_thermal_data *data, >>> @@ -252,11 +254,12 @@ static int imx_get_temp(struct thermal_zone_device *tz, int *temp) >>> const struct thermal_soc_data *soc_data = data->socdata; >>> struct regmap *map = data->tempmon; >>> unsigned int n_meas; >>> - bool wait, run_measurement; >>> + bool wait; >>> u32 val; >>> >>> - run_measurement = !data->irq_enabled; >>> - if (!run_measurement) { >>> + mutex_lock(&data->sensor_lock); >>> + >>> + if (data->sensor_on) { >>> /* Check if a measurement is currently in progress */ >>> regmap_read(map, soc_data->temp_data, &val); >>> wait = !(val & soc_data->temp_valid_mask); >>> @@ -283,13 +286,15 @@ static int imx_get_temp(struct thermal_zone_device *tz, int *temp) >>> >>> regmap_read(map, soc_data->temp_data, &val); >>> >>> - if (run_measurement) { >>> + if (!data->sensor_on) { >>> regmap_write(map, soc_data->sensor_ctrl + REG_CLR, >>> soc_data->measure_temp_mask); >>> regmap_write(map, soc_data->sensor_ctrl + REG_SET, >>> soc_data->power_down_mask); >>> } >>> >>> + mutex_unlock(&data->sensor_lock); >>> + >>> if ((val & soc_data->temp_valid_mask) == 0) { >>> dev_dbg(&tz->device, "temp measurement never finished\n"); >>> return -EAGAIN; >>> @@ -339,20 +344,26 @@ static int imx_change_mode(struct thermal_zone_device *tz, >>> const struct thermal_soc_data *soc_data = data->socdata; >>> >>> if (mode == THERMAL_DEVICE_ENABLED) { >>> + mutex_lock(&data->sensor_lock); >>> regmap_write(map, soc_data->sensor_ctrl + REG_CLR, >>> soc_data->power_down_mask); >>> regmap_write(map, soc_data->sensor_ctrl + REG_SET, >>> soc_data->measure_temp_mask); >>> + data->sensor_on = true; >>> + mutex_unlock(&data->sensor_lock); >>> >>> if (!data->irq_enabled) { >>> data->irq_enabled = true; >>> enable_irq(data->irq); >>> } >>> } else { >>> + mutex_lock(&data->sensor_lock); >>> regmap_write(map, soc_data->sensor_ctrl + REG_CLR, >>> soc_data->measure_temp_mask); >>> regmap_write(map, soc_data->sensor_ctrl + REG_SET, >>> soc_data->power_down_mask); >>> + data->sensor_on = false; >>> + mutex_unlock(&data->sensor_lock); >>> >>> if (data->irq_enabled) { >>> disable_irq(data->irq); >>> @@ -728,6 +739,8 @@ static int imx_thermal_probe(struct platform_device *pdev) >>> } >>> >>> /* Make sure sensor is in known good state for measurements */ >>> + mutex_init(&data->sensor_lock); >>> + mutex_lock(&data->sensor_lock); >>> regmap_write(map, data->socdata->sensor_ctrl + REG_CLR, >>> data->socdata->power_down_mask); >>> regmap_write(map, data->socdata->sensor_ctrl + REG_CLR, >>> @@ -739,6 +752,8 @@ static int imx_thermal_probe(struct platform_device *pdev) >>> IMX6_MISC0_REFTOP_SELBIASOFF); >>> regmap_write(map, data->socdata->sensor_ctrl + REG_SET, >>> data->socdata->power_down_mask); >>> + data->sensor_on = false; >>> + mutex_unlock(&data->sensor_lock); >>> >>> ret = imx_thermal_register_legacy_cooling(data); >>> if (ret) >>> @@ -796,10 +811,13 @@ static int imx_thermal_probe(struct platform_device *pdev) >>> if (data->socdata->version == TEMPMON_IMX6SX) >>> imx_set_panic_temp(data, data->temp_critical); >>> >>> + mutex_lock(&data->sensor_lock); >>> regmap_write(map, data->socdata->sensor_ctrl + REG_CLR, >>> data->socdata->power_down_mask); >>> regmap_write(map, data->socdata->sensor_ctrl + REG_SET, >>> data->socdata->measure_temp_mask); >>> + data->sensor_on = true; >>> + mutex_unlock(&data->sensor_lock); >>> >>> data->irq_enabled = true; >>> ret = thermal_zone_device_enable(data->tz); >>> @@ -832,8 +850,12 @@ static int imx_thermal_remove(struct platform_device *pdev) >>> struct regmap *map = data->tempmon; >>> >>> /* Disable measurements */ >>> + mutex_lock(&data->sensor_lock); >>> regmap_write(map, data->socdata->sensor_ctrl + REG_SET, >>> data->socdata->power_down_mask); >>> + data->sensor_on = false; >>> + mutex_unlock(&data->sensor_lock); >>> + >>> if (!IS_ERR(data->thermal_clk)) >>> clk_disable_unprepare(data->thermal_clk); >>> >>> >> >> >> -- >> Linaro.org │ Open source software for ARM SoCs >> >> Follow Linaro: Facebook | >> Twitter | >> Blog -- Linaro.org │ Open source software for ARM SoCs Follow Linaro: Facebook | Twitter | Blog