Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2271553pxb; Fri, 5 Feb 2021 13:24:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJwEDiBZpvsf0JyjnzZMRD6fFBJl+fIVCDq5FAQCEu9VsZJGADZ6zIcfgn649avspiTAyP6f X-Received: by 2002:a17:907:948d:: with SMTP id dm13mr5656523ejc.545.1612560256836; Fri, 05 Feb 2021 13:24:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612560256; cv=none; d=google.com; s=arc-20160816; b=gB8mfEp+I8svGk5xRmnxrWEOZZEpgiwV9LbsPVhOt68mEKvul7cFuCsEJHTgk1chg/ u5TkkpQRMkFS24NCPiwE6M+yJYyUVuuKitH1sJdipLBI5Srs98NTe9GE1Khf4/RcWCgi mmAHkXjz0MTW0yw0OG4Lrrq68Urc/Wdzg5RC0Mqq3/9dfqzvX6GfhYhmUGMdPx8HJAYH 4o8dlT8og7rcOMMeytJhLlQ04RyNL2B/lCnZlzi+gS4R9qWJnKV5u7EythLYY72oO91d En8+FtxHV6MfD8Kos0X226yE6ZizBDrER2TnMv5uhHnRtors4qjidlZrgxZITtPnP6ax YEkA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ZZVRLFFqiHEqcUjNRZX2O5pxlAxHltgqwhPGEVcd8kY=; b=OLSywgJMWHDjMXUGEFFyrMsDf5zMY7Lg/RSK+cmHZll0dKMypm7fHmaFZKAZSHjRjn 6Ic44DgrFb8TLoHOV+H8H3rsmSrpkFbF9Sgb6gRs5ekeNZgpKVhG2y7eJvmJ4sMObLVg d0wYWoRUZonwSEaPMHikSNrwPGKCiyE7IEJzxv9Yce04JShNHfCuQKovFPtrG3CwG0fO tzMkAq17LzMrhz2djXCoydgv09jt7yPKB7uG/5cH87a67HdZs0Uuf3FXNsiecHa3Id/O wJH6yCXnO/eac8cMzxmi/u66Tvt4BzbhyamWvPrYe98RvfP5GMmsEgcOreneZe8hoEZc w4ow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=LsQehwpp; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l26si3944247eje.14.2021.02.05.13.23.52; Fri, 05 Feb 2021 13:24:16 -0800 (PST) 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=@linuxfoundation.org header.s=korg header.b=LsQehwpp; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233051AbhBEVWp (ORCPT + 99 others); Fri, 5 Feb 2021 16:22:45 -0500 Received: from mail.kernel.org ([198.145.29.99]:45586 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233050AbhBEPCP (ORCPT ); Fri, 5 Feb 2021 10:02:15 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id A115F65057; Fri, 5 Feb 2021 14:12:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1612534347; bh=vPjHsjJDuLcblib7u+kgiItXwZrsEiz5VNHBh4L3QV4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LsQehwpproJgEc+tmY5yJbv3JQHxd2X/u4pjqGodsv71BqN/xejTwU+o/2XENmMVX w6SJoiORrCcOOrXldSRtvzBGFxcnvzDz/8C5tTd7cXfNwo4zq6pNZKNUWW48rmVAYe 1ZnT438gvNJHd5ZJ/ytR7bUxiT9d9AI2dTn5BvKY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Stephen Berman , "Rafael J. Wysocki" , Sebastian Andrzej Siewior Subject: [PATCH 5.4 05/32] ACPI: thermal: Do not call acpi_thermal_check() directly Date: Fri, 5 Feb 2021 15:07:20 +0100 Message-Id: <20210205140652.576200712@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210205140652.348864025@linuxfoundation.org> References: <20210205140652.348864025@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rafael J. Wysocki commit 81b704d3e4674e09781d331df73d76675d5ad8cb upstream. Calling acpi_thermal_check() from acpi_thermal_notify() directly is problematic if _TMP triggers Notify () on the thermal zone for which it has been evaluated (which happens on some systems), because it causes a new acpi_thermal_notify() invocation to be queued up every time and if that takes place too often, an indefinite number of pending work items may accumulate in kacpi_notify_wq over time. Besides, it is not really useful to queue up a new invocation of acpi_thermal_check() if one of them is pending already. For these reasons, rework acpi_thermal_notify() to queue up a thermal check instead of calling acpi_thermal_check() directly and only allow one thermal check to be pending at a time. Moreover, only allow one acpi_thermal_check_fn() instance at a time to run thermal_zone_device_update() for one thermal zone and make it return early if it sees other instances running for the same thermal zone. While at it, fold acpi_thermal_check() into acpi_thermal_check_fn(), as it is only called from there after the other changes made here. [This issue appears to have been exposed by commit 6d25be5782e4 ("sched/core, workqueues: Distangle worker accounting from rq lock"), but it is unclear why it was not visible earlier.] BugLink: https://bugzilla.kernel.org/show_bug.cgi?id=208877 Reported-by: Stephen Berman Diagnosed-by: Sebastian Andrzej Siewior Signed-off-by: Rafael J. Wysocki Reviewed-by: Sebastian Andrzej Siewior Tested-by: Stephen Berman Cc: All applicable [bigeasy: Backported to v5.4.y] Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Greg Kroah-Hartman --- drivers/acpi/thermal.c | 55 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 17 deletions(-) --- a/drivers/acpi/thermal.c +++ b/drivers/acpi/thermal.c @@ -174,6 +174,8 @@ struct acpi_thermal { int tz_enabled; int kelvin_offset; struct work_struct thermal_check_work; + struct mutex thermal_check_lock; + refcount_t thermal_check_count; }; /* -------------------------------------------------------------------------- @@ -494,17 +496,6 @@ static int acpi_thermal_get_trip_points( return 0; } -static void acpi_thermal_check(void *data) -{ - struct acpi_thermal *tz = data; - - if (!tz->tz_enabled) - return; - - thermal_zone_device_update(tz->thermal_zone, - THERMAL_EVENT_UNSPECIFIED); -} - /* sys I/F for generic thermal sysfs support */ static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp) @@ -538,6 +529,8 @@ static int thermal_get_mode(struct therm return 0; } +static void acpi_thermal_check_fn(struct work_struct *work); + static int thermal_set_mode(struct thermal_zone_device *thermal, enum thermal_device_mode mode) { @@ -563,7 +556,7 @@ static int thermal_set_mode(struct therm ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s kernel ACPI thermal control\n", tz->tz_enabled ? "Enable" : "Disable")); - acpi_thermal_check(tz); + acpi_thermal_check_fn(&tz->thermal_check_work); } return 0; } @@ -932,6 +925,12 @@ static void acpi_thermal_unregister_ther Driver Interface -------------------------------------------------------------------------- */ +static void acpi_queue_thermal_check(struct acpi_thermal *tz) +{ + if (!work_pending(&tz->thermal_check_work)) + queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work); +} + static void acpi_thermal_notify(struct acpi_device *device, u32 event) { struct acpi_thermal *tz = acpi_driver_data(device); @@ -942,17 +941,17 @@ static void acpi_thermal_notify(struct a switch (event) { case ACPI_THERMAL_NOTIFY_TEMPERATURE: - acpi_thermal_check(tz); + acpi_queue_thermal_check(tz); break; case ACPI_THERMAL_NOTIFY_THRESHOLDS: acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS); - acpi_thermal_check(tz); + acpi_queue_thermal_check(tz); acpi_bus_generate_netlink_event(device->pnp.device_class, dev_name(&device->dev), event, 0); break; case ACPI_THERMAL_NOTIFY_DEVICES: acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES); - acpi_thermal_check(tz); + acpi_queue_thermal_check(tz); acpi_bus_generate_netlink_event(device->pnp.device_class, dev_name(&device->dev), event, 0); break; @@ -1052,7 +1051,27 @@ static void acpi_thermal_check_fn(struct { struct acpi_thermal *tz = container_of(work, struct acpi_thermal, thermal_check_work); - acpi_thermal_check(tz); + + if (!tz->tz_enabled) + return; + /* + * In general, it is not sufficient to check the pending bit, because + * subsequent instances of this function may be queued after one of them + * has started running (e.g. if _TMP sleeps). Avoid bailing out if just + * one of them is running, though, because it may have done the actual + * check some time ago, so allow at least one of them to block on the + * mutex while another one is running the update. + */ + if (!refcount_dec_not_one(&tz->thermal_check_count)) + return; + + mutex_lock(&tz->thermal_check_lock); + + thermal_zone_device_update(tz->thermal_zone, THERMAL_EVENT_UNSPECIFIED); + + refcount_inc(&tz->thermal_check_count); + + mutex_unlock(&tz->thermal_check_lock); } static int acpi_thermal_add(struct acpi_device *device) @@ -1084,6 +1103,8 @@ static int acpi_thermal_add(struct acpi_ if (result) goto free_memory; + refcount_set(&tz->thermal_check_count, 3); + mutex_init(&tz->thermal_check_lock); INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn); pr_info(PREFIX "%s [%s] (%ld C)\n", acpi_device_name(device), @@ -1149,7 +1170,7 @@ static int acpi_thermal_resume(struct de tz->state.active |= tz->trips.active[i].flags.enabled; } - queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work); + acpi_queue_thermal_check(tz); return AE_OK; }