From patchwork Fri Jul 21 14:51:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Rafael J. Wysocki" X-Patchwork-Id: 123921 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp282312vqg; Fri, 21 Jul 2023 08:25:07 -0700 (PDT) X-Google-Smtp-Source: APBJJlG5eQ44uBcLCH0+bPIGBO/zksA8I34jFgdyrqwjpZL1/vo55Qjil8v1lRbhgTObHvi1QCjF X-Received: by 2002:a05:6a20:12cf:b0:137:e0dc:b10e with SMTP id v15-20020a056a2012cf00b00137e0dcb10emr2810989pzg.0.1689953107407; Fri, 21 Jul 2023 08:25:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689953107; cv=none; d=google.com; s=arc-20160816; b=iUl2cXNGc8ke41NFArCs30CWLcl1bFm5yD5ZadHq+Year+VkU/Fp47xyApfVrIuRyT XtUabSq314dvlUrS/6DIzMBpny4XMyB8HD45Hkis3O2iOpWm60xn+nI3OiP6TS6Be+Km gyjvK3qfw4digaLkW30OsEa4EeI5bMRU8jyO+ONKBServ0ByT1Z+jqgKXNtNoOjI8mN5 uKsYN1Wg7hVtN6tdwjDdA48NcHeVHrnKvbvqo/YMeP2fnCYIQ11AagDJO0Fsp3h2stl9 qcRTb3r/5FolOQbEfeh7W0Fi1rRvMdqzg5tbHb9jM5+86AdKES3aAIrCr46rE0Xetyvg 4kfw== 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=93R+AFky1UWa1j0rJdCnBvLEecsPST/VBiQg9OOfphc=; fh=WAm0nkxw3rD71A4QzwJS1JiPPKRsyn91IiWMPsOrzoY=; b=nLIZacx1W1BzB8tCtFLuCfUbiSH3/YBJolaIQGaunxtQvp4L//kJDHvwfFeX27NoII 4aeuFdwHpB8WMI78+9N8+KQty/DOPVAK5SxEM61upG7J6CJ3Aeq5/8N4sRTiC0E7felH SnxvkHXtD2r38cbVrI/EcLREMJPBoTni2NYJAYN6sIGziKLSDv2u6bqDWHV3vSavpvtF xzIjlryGu/W2G8sBFyCCBN8bb66uXRcx11atj97l3QYA3hwch7jNppC8mtrK1L8PiJRi mcUz9zpIfuplniTE9RLbevQJ/7R5XTUaGK2dciiQ7UGNs8i1YQanFhwvWA6z9HxmeLiH U6dQ== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x10-20020a056a00188a00b0067dd7254cb0si3352389pfh.39.2023.07.21.08.24.54; Fri, 21 Jul 2023 08:25: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; 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 S231910AbjGUPS0 (ORCPT + 99 others); Fri, 21 Jul 2023 11:18:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231516AbjGUPSQ (ORCPT ); Fri, 21 Jul 2023 11:18:16 -0400 Received: from cloudserver094114.home.pl (cloudserver094114.home.pl [79.96.170.134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A18830DB; Fri, 21 Jul 2023 08:18:14 -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 31b8b80791d346d9; Fri, 21 Jul 2023 17:18:13 +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 9C449661901; Fri, 21 Jul 2023 17:18:12 +0200 (CEST) From: "Rafael J. Wysocki" To: Linux ACPI Cc: LKML , Linux PM , Michal Wilczynski , Zhang Rui , Srinivas Pandruvada , Daniel Lezcano Subject: [PATCH v2 5/8] ACPI: thermal: Hold thermal zone lock around trip updates Date: Fri, 21 Jul 2023 16:51:23 +0200 Message-ID: <3448044.QJadu78ljV@kreacher> In-Reply-To: <5710197.DvuYhMxLoT@kreacher> References: <13318886.uLZWGnKmhe@kreacher> <5710197.DvuYhMxLoT@kreacher> MIME-Version: 1.0 X-CLIENT-IP: 195.136.19.94 X-CLIENT-HOSTNAME: 195.136.19.94 X-VADE-SPAMSTATE: clean X-VADE-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedviedrhedvgdekvdcutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfjqffogffrnfdpggftiffpkfenuceurghilhhouhhtmecuudehtdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephffvvefufffkjghfggfgtgesthfuredttddtjeenucfhrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqeenucggtffrrghtthgvrhhnpedvffeuiedtgfdvtddugeeujedtffetteegfeekffdvfedttddtuefhgeefvdejhfenucfkphepudelhedrudefiedrudelrdelgeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpeduleehrddufeeirdduledrleegpdhhvghlohepkhhrvggrtghhvghrrdhlohgtrghlnhgvthdpmhgrihhlfhhrohhmpedftfgrfhgrvghlucflrdcuhgihshhotghkihdfuceorhhjfiesrhhjfiihshhotghkihdrnhgvtheqpdhnsggprhgtphhtthhopeejpdhrtghpthhtoheplhhinhhugidqrggtphhisehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhinhhugidqphhmsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepmhhitghhrghlrdifihhltgiihihnshhkihesihhnthgvlhdrtghomhdprhgt phhtthhopehruhhirdiihhgrnhhgsehinhhtvghlrdgtohhmpdhrtghpthhtohepshhrihhnihhvrghsrdhprghnughruhhvrggurgeslhhinhhugidrihhnthgvlhdrtghomh 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772044269368069758 X-GMAIL-MSGID: 1772044269368069758 From: Rafael J. Wysocki There is a race condition between acpi_thermal_trips_update() and acpi_thermal_check_fn(), because the trip points may get updated while the latter is running which in theory may lead to inconsistent results. For example, if two trips are updated together, using the temperature value of one of them from before the update and the temperature value of the other one from after the update may not lead to the expected outcome. To address this, make acpi_thermal_trips_update() hold the thermal zone lock across the entire update of trip points. While at it, change the acpi_thermal_trips_update() return data type to void as that function always returns 0 anyway. Signed-off-by: Rafael J. Wysocki --- v1 -> v2: * Hold the thermal zone lock instead of thermal_check_lock around trip point updates (this also helps to protect thermal_get_trend() from using stale trip temperatures). * Add a comment documenting the purpose of the locking. * Make acpi_thermal_trips_update() void. --- drivers/acpi/thermal.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) Index: linux-pm/drivers/acpi/thermal.c =================================================================== --- linux-pm.orig/drivers/acpi/thermal.c +++ linux-pm/drivers/acpi/thermal.c @@ -190,7 +190,7 @@ static int acpi_thermal_get_polling_freq return 0; } -static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag) +static void __acpi_thermal_trips_update(struct acpi_thermal *tz, int flag) { acpi_status status; unsigned long long tmp; @@ -398,17 +398,28 @@ static int acpi_thermal_trips_update(str ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device"); } } +} - return 0; +static void acpi_thermal_trips_update(struct acpi_thermal *tz, int flag) +{ + /* + * The locking is needed here to protect thermal_get_trend() from using + * a stale passive trip temperature and to synchronize with the trip + * temperature updates in acpi_thermal_check_fn(). + */ + thermal_zone_device_lock(tz->thermal_zone); + + __acpi_thermal_trips_update(tz, flag); + + thermal_zone_device_unlock(tz->thermal_zone); } static int acpi_thermal_get_trip_points(struct acpi_thermal *tz) { - int i, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT); bool valid; + int i; - if (ret) - return ret; + __acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT); valid = tz->trips.critical.valid | tz->trips.hot.valid |