Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3816175rdh; Tue, 28 Nov 2023 04:54:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IG7+HOh//aQhoRpFeFH/QpqfMDqT+NYB0HgHFrlMxPnmTmn9STlSQKbGOF9YSuf++sDxE57 X-Received: by 2002:a17:902:dad0:b0:1cf:da41:7294 with SMTP id q16-20020a170902dad000b001cfda417294mr4682873plx.25.1701176063640; Tue, 28 Nov 2023 04:54:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701176063; cv=none; d=google.com; s=arc-20160816; b=BmgIDs6xRzsxByNMBM6B9xWytRgfP5TgEs/Y6Qf2NebAGFcStpGvPRJ6TQ0lpXpFVM pSf5AnOd/P8YuVKRl9DAig7TrBAGs2w53SpqlUjuZxzbVj+TqJznwtUkAHOJvATJh+0Q Xc6GBvhSWjZY+UDs0zEztdzG3E9QpZKOmpFSdhJBOYdOT66BXPP91X9YxCAos5Ih5GS3 jpVq9pj0kxf/7FF5LUkojyQteU0TbqK1TFeXoMlUjVmrP3QdxHbVcx89kVEKFTMw+whe R8Umkdw8h/JcjUX5kVTd7oNHnaFxOIEM/H+g+aJkyIvw8tFeBVvopOJK80dbz7vCu/Ul cuSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=8daaTB/uuEndP+5Z978pRqTcRh1DT+YrxN8RgIgfKD8=; fh=0cpBOKuWf+FfnPJZG/zhkSBJdPvgX7kxtRw/of8L22s=; b=NvrgQZ99OppKVn6/ij16dANYdKOp+X3Px9et9ZMrSVSFFHvWj1W5ngPc+91yZEBvHb NUWgcl/hLG4hgL0i8aBacsq2djCwDgyxiJOtGGtnWr5oxbfZ6csWkZocsxhZeblO9wEx wSJu0ma/4THb4AdrvDWynojAadt6HgBRDRhA1wxCxZqlGiobCcBq12MBIq/JCrOB9OEG tJKGM7peDeJa7RbztcieNvROGpbuC8D4j7wt4c5YMTmP4nb9pQsdiEtQC9wNaUK8uZiI PG4RM5eknTokVPhkEGekNj+P182f3CUhMNYvrykr3U+uXe/uM5Jf0drSFW0sV9Q0Jrl9 Dm6g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id l6-20020a170903120600b001cfb1dae607si8488713plh.146.2023.11.28.04.54.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 04:54:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 58AE58225C9A; Tue, 28 Nov 2023 04:54:20 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344737AbjK1MyD convert rfc822-to-8bit (ORCPT + 99 others); Tue, 28 Nov 2023 07:54:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344655AbjK1MyB (ORCPT ); Tue, 28 Nov 2023 07:54:01 -0500 Received: from mail-ot1-f48.google.com (mail-ot1-f48.google.com [209.85.210.48]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9FE010D4; Tue, 28 Nov 2023 04:54:07 -0800 (PST) Received: by mail-ot1-f48.google.com with SMTP id 46e09a7af769-6d815062598so871708a34.0; Tue, 28 Nov 2023 04:54:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701176045; x=1701780845; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KansKB/I6JMABnep2eO/OwfMWyW5lRyhE+ZO5W+A/ws=; b=cHVMkKQjb2/3hAfY2clot2YmI6PaEOiLfqgoFZAfmWL3XjZJRzf9mwyeWOqh467EwY sORBJgHhJdg7yZRzvhXkj68t4Su97edQZt5kk0UxUGt26ZdYIymzPNEYfSaYV/0SpKm0 T5TawRQUpher0tjniLpKLKksc2YgRJuxSDqPVA+EnlqRRUHx6dC+zTnVzYxEY38EMI8u 4XQs+T+/s69CKTnQcxYGtNWblp63AiD4xE62hXyxI8S9wNYVqOi3mDQUMkL23WP8GRR8 oKTaaXD/rjFqci7pDZ48mw7Y8s7jQSezEE3iZwJG6IeNVsFJE8fjrV3XtC8Yc7ysr2CC 4Bug== X-Gm-Message-State: AOJu0YzGYoCVrMpuXJAQCRzclSqwgCju/WRDyJ1wvvYWASIJ1mTvKmk9 sILwuOwmvp2qIvh7sUSCZSMMFYG/Qf5v8JXQsr4= X-Received: by 2002:a05:6820:b0b:b0:58a:7cff:2406 with SMTP id df11-20020a0568200b0b00b0058a7cff2406mr13172699oob.0.1701176045412; Tue, 28 Nov 2023 04:54:05 -0800 (PST) MIME-Version: 1.0 References: <4892163.31r3eYUQgx@kreacher> In-Reply-To: From: "Rafael J. Wysocki" Date: Tue, 28 Nov 2023 13:53:54 +0100 Message-ID: Subject: Re: [PATCH v1] thermal: trip: Rework thermal_zone_set_trip() and its callers To: Lukasz Luba Cc: "Rafael J. Wysocki" , LKML , Linux PM , Daniel Lezcano , Srinivas Pandruvada , Zhang Rui Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 28 Nov 2023 04:54:20 -0800 (PST) Hi Lukasz, On Tue, Nov 28, 2023 at 9:16 AM Lukasz Luba wrote: > > Hi Rafael, > > On 11/27/23 19:59, Rafael J. Wysocki wrote: > > From: Rafael J. Wysocki > > > > Both trip_point_temp_store() and trip_point_hyst_store() use > > thermal_zone_set_trip() to update a given trip point, but none of them > > actually needs to change more than one field in struct thermal_trip > > representing it. However, each of them effectively calls > > __thermal_zone_get_trip() twice in a row for the same trip index value, > > once directly and once via thermal_zone_set_trip(), which is not > > particularly efficient, and the way in which thermal_zone_set_trip() > > carries out the update is not particularly straightforward. > > > > Moreover, some checks done by them both need not go under the thermal > > zone lock and code duplication between them can be reduced quilte a bit > > s/quilte/quite/ > > > by moving the majority of logic into thermal_zone_set_trip(). > > > > Rework all of the above funtcions to address the above. > > s/funtcions/functions/ Thanks for spotting the typos! > > > > No intentional functional impact. > > > > Signed-off-by: Rafael J. Wysocki > > --- > > drivers/thermal/thermal_core.h | 9 +++++ > > drivers/thermal/thermal_sysfs.c | 52 +++++++--------------------------- > > drivers/thermal/thermal_trip.c | 61 ++++++++++++++++++++++++++-------------- > > include/linux/thermal.h | 3 - > > 4 files changed, 61 insertions(+), 64 deletions(-) > > > > Index: linux-pm/drivers/thermal/thermal_core.h > > =================================================================== > > --- linux-pm.orig/drivers/thermal/thermal_core.h > > +++ linux-pm/drivers/thermal/thermal_core.h > > @@ -122,6 +122,15 @@ void __thermal_zone_device_update(struct > > void __thermal_zone_set_trips(struct thermal_zone_device *tz); > > int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id, > > struct thermal_trip *trip); > > + > > +enum thermal_set_trip_target { > > + THERMAL_TRIP_SET_TEMP, > > + THERMAL_TRIP_SET_HYST, > > +}; > > + > > +int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id, > > + enum thermal_set_trip_target what, const char *buf); > > + > > int thermal_zone_trip_id(struct thermal_zone_device *tz, > > const struct thermal_trip *trip); > > int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp); > > Index: linux-pm/drivers/thermal/thermal_sysfs.c > > =================================================================== > > --- linux-pm.orig/drivers/thermal/thermal_sysfs.c > > +++ linux-pm/drivers/thermal/thermal_sysfs.c > > @@ -120,31 +120,17 @@ trip_point_temp_store(struct device *dev > > const char *buf, size_t count) > > { > > struct thermal_zone_device *tz = to_thermal_zone(dev); > > - struct thermal_trip trip; > > - int trip_id, ret; > > + int trip_id; > > + int ret; > > + > > + if (!device_is_registered(dev)) > > + return -ENODEV; > > > > if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip_id) != 1) > > return -EINVAL; > > > > - mutex_lock(&tz->lock); > > - > > - if (!device_is_registered(dev)) { > > - ret = -ENODEV; > > - goto unlock; > > - } > > - > > - ret = __thermal_zone_get_trip(tz, trip_id, &trip); > > - if (ret) > > - goto unlock; > > - > > - ret = kstrtoint(buf, 10, &trip.temperature); > > - if (ret) > > - goto unlock; > > + ret = thermal_zone_set_trip(tz, trip_id, THERMAL_TRIP_SET_TEMP, buf); > > > > - ret = thermal_zone_set_trip(tz, trip_id, &trip); > > -unlock: > > - mutex_unlock(&tz->lock); > > - > > return ret ? ret : count; > > } > > > > @@ -179,30 +165,16 @@ trip_point_hyst_store(struct device *dev > > const char *buf, size_t count) > > { > > struct thermal_zone_device *tz = to_thermal_zone(dev); > > - struct thermal_trip trip; > > - int trip_id, ret; > > + int trip_id; > > + int ret; > > + > > + if (!device_is_registered(dev)) > > + return -ENODEV; > > > > if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip_id) != 1) > > return -EINVAL; > > > > - mutex_lock(&tz->lock); > > - > > - if (!device_is_registered(dev)) { > > - ret = -ENODEV; > > - goto unlock; > > - } > > - > > - ret = __thermal_zone_get_trip(tz, trip_id, &trip); > > - if (ret) > > - goto unlock; > > - > > - ret = kstrtoint(buf, 10, &trip.hysteresis); > > - if (ret) > > - goto unlock; > > - > > - ret = thermal_zone_set_trip(tz, trip_id, &trip); > > -unlock: > > - mutex_unlock(&tz->lock); > > + ret = thermal_zone_set_trip(tz, trip_id, THERMAL_TRIP_SET_HYST, buf); > > > > return ret ? ret : count; > > } > > Index: linux-pm/drivers/thermal/thermal_trip.c > > =================================================================== > > --- linux-pm.orig/drivers/thermal/thermal_trip.c > > +++ linux-pm/drivers/thermal/thermal_trip.c > > @@ -148,42 +148,61 @@ int thermal_zone_get_trip(struct thermal > > EXPORT_SYMBOL_GPL(thermal_zone_get_trip); > > > > int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id, > > - const struct thermal_trip *trip) > > + enum thermal_set_trip_target what, const char *buf) > > { > > - struct thermal_trip t; > > - int ret; > > + struct thermal_trip *trip; > > + int val, ret = 0; > > > > - if (!tz->ops->set_trip_temp && !tz->ops->set_trip_hyst && !tz->trips) > > - return -EINVAL; > > Here we could bail out when there are no callbacks. Not really, because the trip is updated regardless. > > > + if (trip_id < 0 || trip_id >= tz->num_trips) > > + ret = -EINVAL; > > > > - ret = __thermal_zone_get_trip(tz, trip_id, &t); > > + ret = kstrtoint(buf, 10, &val); > > if (ret) > > return ret; > > > > - if (t.type != trip->type) > > - return -EINVAL; > > + mutex_lock(&tz->lock); > > > > - if (t.temperature != trip->temperature && tz->ops->set_trip_temp) { > > - ret = tz->ops->set_trip_temp(tz, trip_id, trip->temperature); > > - if (ret) > > - return ret; > > - } > > + trip = &tz->trips[trip_id]; > > > > - if (t.hysteresis != trip->hysteresis && tz->ops->set_trip_hyst) { > > - ret = tz->ops->set_trip_hyst(tz, trip_id, trip->hysteresis); > > - if (ret) > > - return ret; > > + switch (what) { > > + case THERMAL_TRIP_SET_TEMP: > > + if (val == trip->temperature) > > + goto unlock; > > + > > + if (tz->ops->set_trip_temp) { > > + ret = tz->ops->set_trip_temp(tz, trip_id, val); > > + if (ret) > > + goto unlock; > > + } > > But here we don't bail out and go line below > > > + trip->temperature = val; > > where we modify the actual '&tz->trips[trip_id]'. Right, the trip is updated regardless unless the callback invocation fails, in which case it is better to retain the existing configuration for consistency. The current logic is exactly this AFAICS except that it is hard to untangle. > Shouldn't be something like the code flow below? > --------------------------------------------8<----------------------------- > if (!tz->ops->set_trip_temp) > goto unlock; > > ret = tz->ops->set_trip_temp(tz, trip_id, val); > if (ret) > goto unlock; > > trip->temperature = val; > break > ----------------------------------->8-------------------------------------- Not really. > > > > > > + break; > > + > > + case THERMAL_TRIP_SET_HYST: > > + if (val == trip->hysteresis) > > + goto unlock; > > + > > + if (tz->ops->set_trip_hyst) { > > + ret = tz->ops->set_trip_hyst(tz, trip_id, val); > > + if (ret) > > + goto unlock; > > + } > > + trip->hysteresis = val; > > Similar question here. > > > + break; > > + > > + default: > > + ret = -EINVAL; > > + goto unlock; > > } > > > > - if (tz->trips && (t.temperature != trip->temperature || t.hysteresis != trip->hysteresis)) > > - tz->trips[trip_id] = *trip; > > - > > thermal_notify_tz_trip_change(tz->id, trip_id, trip->type, > > trip->temperature, trip->hysteresis); > > > > __thermal_zone_device_update(tz, THERMAL_TRIP_CHANGED); > > > > - return 0; > > +unlock: > > + mutex_unlock(&tz->lock); > > + > > + return ret; > > } > > > > int thermal_zone_trip_id(struct thermal_zone_device *tz, > > Index: linux-pm/include/linux/thermal.h > > =================================================================== > > --- linux-pm.orig/include/linux/thermal.h > > +++ linux-pm/include/linux/thermal.h > > @@ -283,9 +283,6 @@ int __thermal_zone_get_trip(struct therm > > int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id, > > struct thermal_trip *trip); > > > > -int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id, > > - const struct thermal_trip *trip); > > - > > Surprisingly, nothing outside thermal fwk uses it... > Maybe it's worth to check other functions there. Fair enough, but that's outside this patch IMO. > Other than that, it looks like a good idea. Thanks for the review!