Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp7222471rwp; Tue, 18 Jul 2023 11:54:01 -0700 (PDT) X-Google-Smtp-Source: APBJJlGHPBacvY4f4fi9TxsEs4M7gX0ACwb/fOFb2yzLIHSIdaaPE0C/RLYgffAHy5JGpZQ0HUur X-Received: by 2002:a17:902:c211:b0:1b2:2c0c:d400 with SMTP id 17-20020a170902c21100b001b22c0cd400mr10300813pll.52.1689706441486; Tue, 18 Jul 2023 11:54:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689706441; cv=none; d=google.com; s=arc-20160816; b=qt+pej8Zb5vqvYQulQz/q7AL53gkb1ZxfV1+ldppMwu5Yz8ksHk1p7vkaeVTMnLNCt Cy4DQCO4Nv/167u9o4UtCKGaoRUJW3lv2SQyONMnzWFYztSg6fkcGJj+jW/DAxz8sDwF fPeVCLwO8O7BMQcCVLdgQOk6xmOL3H54AiHfK9RwaDA4bRRz762P3F8W1SuL7T2EUpG0 3aqC3ezvct3/yGoo8J6wr2cC68WLixtr+yMp0pz0Gge4krsAflCVHGMqz3+Bc8Ys89nE mJoviVnyxBv+05IgunCytm/w7LdGXaU2XLq/d73OKF2vCLDODd6lrYuZNSNtRevKvyHC pcuQ== 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; bh=P13Kn98fNHrmCu3E6gzuxH75O3VuhOh8Kbv6uX27aGo=; fh=WAm0nkxw3rD71A4QzwJS1JiPPKRsyn91IiWMPsOrzoY=; b=Of6KcDl0GwrjY4W6nx6Rm5bFKKPHoAjk0qsXpUe24iIXdhDVi8bVDSVzYS5CbTb1ek lVcH3y+5a1tNPR7psEqkuKh/TMXTUSR24kix0zwIfExNN+NAdmeJPghKm/Mb8P4+hbJ2 h9gtw8xaOYivj46b7iN1EC1eyMm6qYUqOpo2YSm9ETrTAi0dqm3+9aW2oiUjbfuoETkh YbM8HXD9v0rA2TjwjMl5jjZF2RkfLfCvZM69arNmgs10NINQUoYeN2XaGf57vJItpwUB PeRZSCGCUcv2U2Yo9ydiuDd/2qyuBsbF+UGBexEiVUpEfVPaR2s9qF16YBE9eSiXkZ4p IQNQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d9-20020a170903230900b001b8c6890612si2047011plh.594.2023.07.18.11.53.46; Tue, 18 Jul 2023 11:54:01 -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; 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 S229738AbjGRSVf (ORCPT + 99 others); Tue, 18 Jul 2023 14:21:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229678AbjGRSVe (ORCPT ); Tue, 18 Jul 2023 14:21:34 -0400 Received: from cloudserver094114.home.pl (cloudserver094114.home.pl [79.96.170.134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B905197; Tue, 18 Jul 2023 11:21:26 -0700 (PDT) Received: from localhost (127.0.0.1) (HELO v370.home.net.pl) by /usr/run/smtp (/usr/run/postfix/private/idea_relay_lmtp) via UNIX with SMTP (IdeaSmtpServer 5.2.0) id a6acf452a24c91d8; Tue, 18 Jul 2023 20:21:24 +0200 Received: from kreacher.localnet (unknown [195.136.19.94]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by v370.home.net.pl (Postfix) with ESMTPSA id 3CF626614F7; Tue, 18 Jul 2023 20:21:24 +0200 (CEST) From: "Rafael J. Wysocki" To: Linux ACPI Cc: LKML , Linux PM , Michal Wilczynski , Zhang Rui , Srinivas Pandruvada , Daniel Lezcano Subject: [PATCH v1 6/7] ACPI: thermal: Rework thermal_get_trend() Date: Tue, 18 Jul 2023 20:07:49 +0200 Message-ID: <9147669.CDJkKcVGEf@kreacher> In-Reply-To: <13318886.uLZWGnKmhe@kreacher> References: <13318886.uLZWGnKmhe@kreacher> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="UTF-8" X-CLIENT-IP: 195.136.19.94 X-CLIENT-HOSTNAME: 195.136.19.94 X-VADE-SPAMSTATE: clean X-VADE-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedviedrgeeggdduudelucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecujffqoffgrffnpdggtffipffknecuuegrihhlohhuthemucduhedtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhvfevufffkfgjfhgggfgtsehtufertddttdejnecuhfhrohhmpedftfgrfhgrvghlucflrdcuhgihshhotghkihdfuceorhhjfiesrhhjfiihshhotghkihdrnhgvtheqnecuggftrfgrthhtvghrnhepvdffueeitdfgvddtudegueejtdffteetgeefkeffvdeftddttdeuhfegfedvjefhnecukfhppeduleehrddufeeirdduledrleegnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepudelhedrudefiedrudelrdelgedphhgvlhhopehkrhgvrggthhgvrhdrlhhotggrlhhnvghtpdhmrghilhhfrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqedpnhgspghrtghpthhtohepjedprhgtphhtthhopehlihhnuhigqdgrtghpihesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlihhnuhigqdhpmhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehmihgthhgrlhdrfihilhgtiiihnhhskhhisehinhhtvghlrdgtohhmpdhr tghpthhtoheprhhuihdriihhrghnghesihhnthgvlhdrtghomhdprhgtphhtthhopehsrhhinhhivhgrshdrphgrnhgurhhuvhgruggrsehlihhnuhigrdhinhhtvghlrdgtohhm X-DCC--Metrics: v370.home.net.pl 1024; Body=7 Fuz1=7 Fuz2=7 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 From: Rafael J. Wysocki Rework the ACPI thermal driver's .get_trend() callback function, thermal_get_trend(), to use trip point data stored in the generic trip structures instead of calling thermal_get_trip_type() and thermal_get_trip_temp() and make it hold thermal_check_lock to protect against possible races against trip point updates. Signed-off-by: Rafael J. Wysocki --- drivers/acpi/thermal.c | 107 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 78 insertions(+), 29 deletions(-) Index: linux-pm/drivers/acpi/thermal.c =================================================================== --- linux-pm.orig/drivers/acpi/thermal.c +++ linux-pm/drivers/acpi/thermal.c @@ -572,47 +572,96 @@ static int thermal_get_crit_temp(struct return -EINVAL; } +static struct thermal_trip *acpi_thermal_get_trip(struct acpi_thermal *tz, + int trip_index) +{ + struct thermal_trip *trip; + int i; + + if (!tz || trip_index < 0) + return NULL; + + trip = tz->trips.critical.trip_ref.trip; + if (trip) { + if (!trip_index) + return trip; + + trip_index--; + } + + trip = tz->trips.hot.trip_ref.trip; + if (trip) { + if (!trip_index) + return trip; + + trip_index--; + } + + trip = tz->trips.passive.trip_ref.trip; + if (trip) { + if (!trip_index) + return trip; + + trip_index--; + } + + for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { + trip = tz->trips.active[i].trip_ref.trip; + if (trip) { + if (!trip_index) + return trip; + + trip_index--; + } + } + + return NULL; +} + static int thermal_get_trend(struct thermal_zone_device *thermal, - int trip, enum thermal_trend *trend) + int trip_index, enum thermal_trend *trend) { struct acpi_thermal *tz = thermal_zone_device_priv(thermal); - enum thermal_trip_type type; - int i; + struct thermal_trip *trip; + int ret = 0; - if (thermal_get_trip_type(thermal, trip, &type)) - return -EINVAL; + mutex_lock(&tz->thermal_check_lock); - if (type == THERMAL_TRIP_ACTIVE) { - int trip_temp; + trip = acpi_thermal_get_trip(tz, trip_index); + if (!trip) { + ret = -EINVAL; + goto out; + } + if (trip->type == THERMAL_TRIP_ACTIVE) { int temp = deci_kelvin_to_millicelsius_with_offset( tz->temperature, tz->kelvin_offset); - if (thermal_get_trip_temp(thermal, trip, &trip_temp)) - return -EINVAL; - if (temp > trip_temp) { + if (temp > trip->temperature) *trend = THERMAL_TREND_RAISING; - return 0; - } else { - /* Fall back on default trend */ - return -EINVAL; - } + else /* Fall back on default trend */ + ret = -EINVAL; + } else { + /* + * tz->temperature has already been updated by generic thermal + * layer, before this callback being invoked. + */ + int i = tz->trips.passive.tc1 * (tz->temperature - + tz->last_temperature) + + tz->trips.passive.tc2 * (tz->temperature - + tz->trips.passive.temperature); + + if (i > 0) + *trend = THERMAL_TREND_RAISING; + else if (i < 0) + *trend = THERMAL_TREND_DROPPING; + else + *trend = THERMAL_TREND_STABLE; } - /* - * tz->temperature has already been updated by generic thermal layer, - * before this callback being invoked - */ - i = tz->trips.passive.tc1 * (tz->temperature - tz->last_temperature) + - tz->trips.passive.tc2 * (tz->temperature - tz->trips.passive.temperature); - - if (i > 0) - *trend = THERMAL_TREND_RAISING; - else if (i < 0) - *trend = THERMAL_TREND_DROPPING; - else - *trend = THERMAL_TREND_STABLE; +out: + mutex_unlock(&tz->thermal_check_lock); - return 0; + return ret; } static void acpi_thermal_zone_device_hot(struct thermal_zone_device *thermal)