Received: by 2002:a05:7208:3003:b0:81:def:69cd with SMTP id f3csp4276353rba; Tue, 2 Apr 2024 12:05:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXOkZTrBHWuT8hr7w0c4R//v64oBe/iv4kHHud77YJbSupjtCWaHg8xp+UClXc3iUe6A4QEYbC51lj490dAp7SeZjmxsjnB98X038pV0w== X-Google-Smtp-Source: AGHT+IHMUWNl4orzec5N9sGowOi2PWSASvRaWBHLgk0THhZ4j7IrmpzXo25X2TmxFT6lsGl1BGmx X-Received: by 2002:a05:6122:200a:b0:4d4:2176:6b6a with SMTP id l10-20020a056122200a00b004d421766b6amr702706vkd.8.1712084722563; Tue, 02 Apr 2024 12:05:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712084722; cv=pass; d=google.com; s=arc-20160816; b=PpqQiNxrDJcKGkFXNf/jXVv5KtG/OoqYo9gcecE63MxmdxLKuVa6Q355IB0KEvy629 ehqQENtd9ShuFNeDfryhHoMamXoN7LhVftrBm+q6wiun85eRA6tIK+SiJ5MdkDzz3qMB KeDWv2FQLW6ws7s3EQrYb5fR3YKCuPMOMwjYw3qH5VxZIQdNvaMckw+qZyC0Wq/oFJme UCe0mdD5t7C8mZVHHnD74zrbyrrOau9LxjsXZkmCeYA+QDfHAppP2pgPr+WMr/CIU+Q6 hmnlhgILalLFqzkqBukBcuuZeDxhgWpE4sID1giMavb7cUWyeI5u4LAjWu16A+kRtdqO +95A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=mwDB0lcQVP0CiDvdzcORAzB12sZwY+CbFhrfoDl54DU=; fh=cogxfxmfuty1INqTmQqsiEMMrqp+W8B7xyoJyzbm4Sw=; b=kaFrzqXEUSka9DlWh/PfC5Y42DgiQQcrBRW2uJykc54aMYMwLtz0vEAEoCj5g8iM4n sLlsmjoHBPtro+IWWSslNB+u0qTkarOPrIVCzkQD37ce+YaBtvPFJuxzZu9kXyWx1ylM Yii8YuWUIPwoEs5QHBIAvKRVBG8Vz0Jje91VgPRW2rBDSvcImh+Ji/A3ll5zPsqujGJV zRqUI2qJvOx6TirXN76MjiCv0yg9RkT4Lv4q9a5PGWG4zDfoCuJxKLa1fyqyxe9Pml3C 1Y9/y1sNpkucxNdVkrn8QOuHJ9Wiu/lH1sKYnoCXDWdn1WEOQ16fTIHOG45L09e1GCNz L8FA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@rjwysocki.net header.s=dkim header.b=s4yXbo4M; arc=pass (i=1 spf=pass spfdomain=rjwysocki.net); spf=pass (google.com: domain of linux-kernel+bounces-128536-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-128536-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id cy14-20020a056122444e00b004d41cc1af41si1775105vkb.264.2024.04.02.12.05.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Apr 2024 12:05:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-128536-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=fail header.i=@rjwysocki.net header.s=dkim header.b=s4yXbo4M; arc=pass (i=1 spf=pass spfdomain=rjwysocki.net); spf=pass (google.com: domain of linux-kernel+bounces-128536-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-128536-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 1D8D61C224AA for ; Tue, 2 Apr 2024 19:05:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3572615B97D; Tue, 2 Apr 2024 19:04:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=rjwysocki.net header.i=@rjwysocki.net header.b="s4yXbo4M" Received: from cloudserver094114.home.pl (cloudserver094114.home.pl [79.96.170.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC64215B543; Tue, 2 Apr 2024 19:04:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=79.96.170.134 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712084691; cv=none; b=pt7jpILIUBVj2ugFrO/enhktfYjO2g1K57Z+DcpEN/gvOpTAqB96MjwinN5wS5yN5muH+rBpgH65syCYxYNDcSb/+HSBy0OFsRjckmtyQ4E+uj4Ml0hylw2w7LSOU7HdQZ+Pt5Spcq/cQJ8Y5P2cO75RL1d4btznjFwlhVJNCYs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712084691; c=relaxed/simple; bh=75dlDrbyavZG+XAjXcnusq3zKXNyrtB0AJ6UujvYzN4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Kx2CSMsOH3KaAQ1wgXNtFuk3kA/hp9xUZiPp9tVrk+9TRGjROkT5WafGbifqX+K7/vacvGO0cngmbDhdgZXN96J3sYDSMKxJ5lrzM61udFGAoBZtFTPIyOND9XM999XOwRJnH17m2SMipEOR6VvLMVUx1+PnSftmEGzQjiC9Gbc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rjwysocki.net; spf=pass smtp.mailfrom=rjwysocki.net; dkim=fail (2048-bit key) header.d=rjwysocki.net header.i=@rjwysocki.net header.b=s4yXbo4M reason="signature verification failed"; arc=none smtp.client-ip=79.96.170.134 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rjwysocki.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rjwysocki.net 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 6.0.0) id b40a14353fd5a461; Tue, 2 Apr 2024 21:04:41 +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 cloudserver094114.home.pl (Postfix) with ESMTPSA id B279466C5C5; Tue, 2 Apr 2024 21:04:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=rjwysocki.net; s=dkim; t=1712084681; bh=75dlDrbyavZG+XAjXcnusq3zKXNyrtB0AJ6UujvYzN4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=s4yXbo4MdONd10jTVLdqokKe8zTSw8uVSNyWSTN6za9vvGTcdi/AOBgdUz/VU2R/L pX952mlhesGrk29FNYQoawwRNfyXhuyflBzj/OHnUQpH9h+B39TfXg480NrfM0p0/N KaFXHSvgtOdq1ttbYeRv2bEpv9dNlT+wnGIPgsH7GpiG8Ou/VxVKjNrANOW/pq9TNF nPG4bXPx+OqalkILAAihl/KSw40VepUsQyhA9UVinhIMEdOCRJPBsiuKAerPvuwMQJ nUzZ4bUzElDyKEfmNpj2mN6M3FQeJe9LU8BLwuEx1NrpdDwGj3hzghwFcQFReRx0Ik aqVe1VbMJnnfw== From: "Rafael J. Wysocki" To: Linux PM Cc: LKML , Srinivas Pandruvada , Daniel Lezcano , Lukasz Luba , AngeloGioacchino Del Regno Subject: [PATCH v3 5/6] thermal: core: Sort trip point crossing notifications by temperature Date: Tue, 02 Apr 2024 21:03:36 +0200 Message-ID: <7648070.EvYhyI6sBW@kreacher> In-Reply-To: <4558251.LvFx2qVVIh@kreacher> References: <4558251.LvFx2qVVIh@kreacher> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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: gggruggvucftvghtrhhoucdtuddrgedvledrudefvddgudefvdcutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfjqffogffrnfdpggftiffpkfenuceurghilhhouhhtmecuudehtdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephffvvefufffkjghfggfgtgesthfuredttddtjeenucfhrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqeenucggtffrrghtthgvrhhnpeefudduuedtuefgleffudeigeeitdeufeelvdejgefftdethffhhfethfeljefgteenucffohhmrghinhepkhgvrhhnvghlrdhorhhgnecukfhppeduleehrddufeeirdduledrleegnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepudelhedrudefiedrudelrdelgedphhgvlhhopehkrhgvrggthhgvrhdrlhhotggrlhhnvghtpdhmrghilhhfrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqedpnhgspghrtghpthhtohepiedprhgtphhtthhopehlihhnuhigqdhpmhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehsrhhinhhivhgrshdrphgrnhgurhhuvhgruggrsehlihhnuhigrdhinhhtvghlrdgtohhmpdhrtghpthhtohep uggrnhhivghlrdhlvgiitggrnhhosehlihhnrghrohdrohhrghdprhgtphhtthhopehluhhkrghsiidrlhhusggrsegrrhhmrdgtohhmpdhrtghpthhtoheprghnghgvlhhoghhiohgrtggthhhinhhordguvghlrhgvghhnohestgholhhlrggsohhrrgdrtghomh X-DCC--Metrics: v370.home.net.pl 1024; Body=6 Fuz1=6 Fuz2=6 From: Rafael J. Wysocki If multiple trip points are crossed in one go and the trips table in the thermal zone device object is not sorted, the corresponding trip point crossing notifications sent to user space will not be ordered either. Moreover, if the trips table is sorted by trip temperature in ascending order, the trip crossing notifications on the way up will be sent in that order too, but the trip crossing notifications on the way down will be sent in the reverse order. This is generally confusing and it is better to make the kernel send the notifications in the order of growing (on the way up) or falling (on the way down) trip temperature. To achieve that, instead of sending a trip crossing notification and recording a trip crossing event in the statistics right away from handle_thermal_trip(), put the trip in question on a list that will be sorted by __thermal_zone_device_update() after processing all of the trips and before sending the notifications and recording trip crossing events. Link: https://lore.kernel.org/linux-pm/20240306085428.88011-1-daniel.lezcano@linaro.org/ Reported-by: Daniel Lezcano Signed-off-by: Rafael J. Wysocki --- v2 -> v3: Rebase, update changelog, add notify_temp to struct thermal_trip_desc v1 -> v2: New patch --- drivers/thermal/thermal_core.c | 41 +++++++++++++++++++++++++++++++++++------ drivers/thermal/thermal_core.h | 2 ++ 2 files changed, 37 insertions(+), 6 deletions(-) Index: linux-pm/drivers/thermal/thermal_core.c =================================================================== --- linux-pm.orig/drivers/thermal/thermal_core.c +++ linux-pm/drivers/thermal/thermal_core.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -361,7 +362,9 @@ static void handle_critical_trips(struct } static void handle_thermal_trip(struct thermal_zone_device *tz, - struct thermal_trip_desc *td) + struct thermal_trip_desc *td, + struct list_head *way_up_list, + struct list_head *way_down_list) { const struct thermal_trip *trip = &td->trip; int old_threshold; @@ -387,8 +390,8 @@ static void handle_thermal_trip(struct t * In that case, the trip temperature becomes the new threshold. */ if (tz->temperature < trip->temperature - trip->hysteresis) { - thermal_notify_tz_trip_down(tz, trip); - thermal_debug_tz_trip_down(tz, trip); + list_add(&td->notify_list_node, way_down_list); + td->notify_temp = trip->temperature - trip->hysteresis; } else { td->threshold -= trip->hysteresis; } @@ -398,8 +401,8 @@ static void handle_thermal_trip(struct t * if the zone temperature exceeds the trip one. The new * threshold is then set to the low temperature of the trip. */ - thermal_notify_tz_trip_up(tz, trip); - thermal_debug_tz_trip_up(tz, trip); + list_add_tail(&td->notify_list_node, way_up_list); + td->notify_temp = trip->temperature; td->threshold -= trip->hysteresis; } @@ -452,10 +455,24 @@ static void thermal_zone_device_init(str pos->initialized = false; } +static int thermal_trip_notify_cmp(void *ascending, const struct list_head *a, + const struct list_head *b) +{ + struct thermal_trip_desc *tda = container_of(a, struct thermal_trip_desc, + notify_list_node); + struct thermal_trip_desc *tdb = container_of(b, struct thermal_trip_desc, + notify_list_node); + int ret = tdb->notify_temp - tda->notify_temp; + + return ascending ? ret : -ret; +} + void __thermal_zone_device_update(struct thermal_zone_device *tz, enum thermal_notify_event event) { struct thermal_trip_desc *td; + LIST_HEAD(way_down_list); + LIST_HEAD(way_up_list); if (tz->suspended) return; @@ -470,7 +487,19 @@ void __thermal_zone_device_update(struct tz->notify_event = event; for_each_trip_desc(tz, td) - handle_thermal_trip(tz, td); + handle_thermal_trip(tz, td, &way_up_list, &way_down_list); + + list_sort(&way_up_list, &way_up_list, thermal_trip_notify_cmp); + list_for_each_entry(td, &way_up_list, notify_list_node) { + thermal_notify_tz_trip_up(tz, &td->trip); + thermal_debug_tz_trip_up(tz, &td->trip); + } + + list_sort(NULL, &way_down_list, thermal_trip_notify_cmp); + list_for_each_entry(td, &way_down_list, notify_list_node) { + thermal_notify_tz_trip_down(tz, &td->trip); + thermal_debug_tz_trip_down(tz, &td->trip); + } monitor_thermal_zone(tz); } Index: linux-pm/drivers/thermal/thermal_core.h =================================================================== --- linux-pm.orig/drivers/thermal/thermal_core.h +++ linux-pm/drivers/thermal/thermal_core.h @@ -17,6 +17,8 @@ struct thermal_trip_desc { struct thermal_trip trip; + struct list_head notify_list_node; + int notify_temp; int threshold; };