Received: by 2002:ab2:7a55:0:b0:1f4:4a7d:290d with SMTP id u21csp357708lqp; Thu, 4 Apr 2024 15:55:33 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW8H9RFBi91jy9436pHm6EN05QBUnR1CkNoDoww2aPZLPXz0DleyBIP0i9dY+/1bObOhYEjGKkqf3lVmY3v93lsspCS2LGJBhmo6C2hXQ== X-Google-Smtp-Source: AGHT+IFkRv2qI7gDpXSSqiSiQt5U1sGHKvsdCUmms7+QLDJWmNFR28svwzE3tbpCN+YXQV+li6iW X-Received: by 2002:a50:8d1d:0:b0:56d:c721:d0b2 with SMTP id s29-20020a508d1d000000b0056dc721d0b2mr2989082eds.33.1712271333099; Thu, 04 Apr 2024 15:55:33 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712271333; cv=pass; d=google.com; s=arc-20160816; b=aUkWxGr8atonhTL4fQKSs+XnAIniyF0h1D+IKutf56Z8XOyXvyy99ScyHTlnnYL73h 4A6VGZ33sWbTGP2oX6aah1N9v2+3I+qN5pHVh9NpouGo6lsCtu+oKak66+M72gLTJctz IbSzPaCbOqkQEKieyQGIDTLxDyGZxQuUcRzegb1XJ8HwC+lwu5agNucDV233ZBUvXhLr 8NGI8e2yYqYC1VZS3YGMh2PG2FTHbaPzd1PqHbbmKLjCscKZKXS88Tg1BVKHrN7FICFx z+c+pDR6FPeNKMPMdvtHW1bfXlcAAI44j5hBPBscZYunViDs8X3nhVZMBnwbBl7fRra2 93Sg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=LzvwGvQOKkAflrjQ9FfbK7fEcx5ZL9u8p5PhJ/loDQY=; fh=OnjlES3ltT1rfj1xqWlPH5gHLUiiHEk+YdAUQDlLl9M=; b=nbwzxYnjofvVkK1d4m2lP7VhljZZ8/dk71scpdFTmUIzrjKpqVyY9Wqi2963Fj1Rhb feA9KPCbCMrXlPGpfNdI8yzxOnXBFDvZfT/o23y3T1mh2IC7qEGFk4lNENCBVh3wRtHt iFPg/barEXzeH+GrBIBqeB1FEtB1Mwm2zoMmE/WoqIZK8yxDxwiggABhXq74FzeVPGd5 wQ2dO8bWmvm6kOflBYv/TXicp8Sp7p+9RkY60Hg5KK6rdnNOAcAtbbnTWPit0PHSFs4q DdC3MIZRNr6kkLFj69PONbJzotQKq02kjJBmXjVemLCTPIjUskccslVQf2IdZqSFsy0U 9T5g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-132224-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-132224-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id b95-20020a509f68000000b0056c498c9881si112704edf.471.2024.04.04.15.55.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Apr 2024 15:55:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-132224-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-132224-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-132224-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 50EB71F29127 for ; Thu, 4 Apr 2024 22:54:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8334A13C3D8; Thu, 4 Apr 2024 22:53:57 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5012070CC9; Thu, 4 Apr 2024 22:53:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712271237; cv=none; b=S9XxWLukRk2vy/Vrujp72usSEvKMjQ0+3gPwtxbrzawnwKmdcWcGw9tu3hWG91HnZM43I19eCrkt3YMtYBmz7wffgGv7rIK0EJXyRUWez+F/nyLknp/HeT76lMcQFu3Pxo0AMLZ15lfBfQy5hL/lfANw9tbUujnXbAR0ur46+EE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712271237; c=relaxed/simple; bh=MfJgJvrlVu8oF0lBKSSOF9ccKC/eBt1ErWmb46ryNkY=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=h8IAwiTcSo8SASLzK2l2Wz4hh37qn2sSzgz1tJqYCM/kZoVQSAiUlX/LLZQZhS/srS0cwf0yblvageeTVobtEaoox6PfGRVfef6PJTTT+pRWyo0eYto852IBItHZOWl7bc+AZDUvHmC5xE5p+S+sec9+/tcCWPFDGU1WHbwyI1E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 44BF5FEC; Thu, 4 Apr 2024 15:54:24 -0700 (PDT) Received: from [10.57.73.43] (unknown [10.57.73.43]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 8B2053F64C; Thu, 4 Apr 2024 15:53:52 -0700 (PDT) Message-ID: <731eddd6-a3ea-4c4d-b7c0-51319de8b09a@arm.com> Date: Thu, 4 Apr 2024 23:53:53 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 5/6] thermal: core: Sort trip point crossing notifications by temperature Content-Language: en-US To: "Rafael J. Wysocki" Cc: LKML , Linux PM , Srinivas Pandruvada , Daniel Lezcano , AngeloGioacchino Del Regno References: <4558251.LvFx2qVVIh@kreacher> <7648070.EvYhyI6sBW@kreacher> From: Lukasz Luba In-Reply-To: <7648070.EvYhyI6sBW@kreacher> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 4/2/24 20:03, Rafael J. Wysocki wrote: > 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; > }; > > > > Reviewed-by: Lukasz Luba