Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp552854rdb; Thu, 30 Nov 2023 11:33:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IGfNUjwYYKwXj6uKpVMP/E/DBDoEWPgej2foRQKHN1vo5h+1MAm91ub6r67Fq/KZfy2yZ4m X-Received: by 2002:a17:902:848a:b0:1cc:6cc3:d9ba with SMTP id c10-20020a170902848a00b001cc6cc3d9bamr19707494plo.4.1701372820702; Thu, 30 Nov 2023 11:33:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701372820; cv=none; d=google.com; s=arc-20160816; b=eekwG/nWVWtz7yQmFMGumCokmnTLmeFK+XG8Euq8W0c70Y1icawR2g52K2EqqmtfVz SQxvNIRmkDWs02oTzUiwUV67yCZOiG0jVC3E20E5m+Gl2cqUrRw2XGw1FjEETO+NJJNq zKQgMMcaSzuIjDiDN2YofNjLRH5a5eZNEl10S+tk2p0TU+r42HmSPH35j+otn0wHYIZL 0KK379XAM09EVAHHhOOIFXg8V2Epmx9StBhp9J7Z5kXAtBBLG416SrYyewGhBp1rrFLB b1ww+/4zQEQDgt9D2Fex625gdToxrvVscAZtP+o1I9odIUm7Qf0G2WYJQYAstsH6jVg+ WIdQ== 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=X1gM3JICfWdmSj6l7+G3+uL+DyT/2JjNTbGk1crWePI=; fh=hQqXPxg301G2vy5Z4webupsJLc0/EEbcY0z7ZSklO2g=; b=EpFcKSOYuLmmeGIAYZXC01P0PceAFW33dUD7RluUf9QYI7K7EsAehqu238vp7C8kWd /DFn3WZRiUuqjAKQQQ+IAC/9lASWJnzufeh2cwykNjz9l37PJJYeeG80Bn6h3jOyoGNB ewq03f0o3Jz23CtMV3xRY2Gm4Cr48GwwRMhBpcfyaDFnlNcDt5IetTitYfOBclrgupCk GIG5Iqp0lFCnui3Vkdy5/K+sHMfRhwl61GXO0eK+ddf0GOrGEpoF3Wwy6YDZ5wAACjPv uk8YwSzMEI5TbHu+c5/uPUP3aEsy2V0f8FyAnB9J1Pk0DFQNLmL79gaYwkn6HTmuMtMl 6q8w== 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 t15-20020a17090340cf00b001cfbd982757si1875279pld.88.2023.11.30.11.33.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 11:33:40 -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 D2CC080B9509; Thu, 30 Nov 2023 11:33:37 -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 S1376437AbjK3TdV convert rfc822-to-8bit (ORCPT + 99 others); Thu, 30 Nov 2023 14:33:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376422AbjK3TdU (ORCPT ); Thu, 30 Nov 2023 14:33:20 -0500 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 274C893; Thu, 30 Nov 2023 11:33:27 -0800 (PST) Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6cb90b33c1dso291023b3a.0; Thu, 30 Nov 2023 11:33:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701372806; x=1701977606; 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=Mg9NX8SmvZzhqz4CJUk+NYEDY+XieW5G5uysa9/nGSM=; b=IBuBgLG4fm9VyYldyehYdT2F2xy4jovaVfyailPycy42bWwiBbhd8qno7/2xxIACNB Lzoz4julf6aAQljSwV8NVLTUYINa0rJKsDoSUMxxtKnaknskjUq69x/Q8ThHiKGgJPsX Kg2UyEQd4WIM3qHPFdUZdys87Nv4bsLuiRYed8UwvzAK0+KrYGb0ji8BHvT++wf47R2h vbrqoOMiyVWKLmhxUH21mtv8FPTqo04Lk4UI0N1xYgwA/m602r8J3Pr55z6DloE2lCOs qoRYX3422x20bBwc0EPu8K32S1unZ5ixZdsAT2KGtGvRBFzWyMRBu2p1jNMpcOI0VhKX aQ5A== X-Gm-Message-State: AOJu0Yy7jQgEghpAamPrgh9wFL2Wh0VR/zAqFrPHmPEddwh57cUfabsF 5wPh1YJe6Xa+MFwRwTaa1NXZvfEL9BpfCvJXC2s= X-Received: by 2002:a05:6a00:3a19:b0:6cb:8347:c8b1 with SMTP id fj25-20020a056a003a1900b006cb8347c8b1mr27778157pfb.1.1701372806544; Thu, 30 Nov 2023 11:33:26 -0800 (PST) MIME-Version: 1.0 References: <5754079.DvuYhMxLoT@kreacher> <2933888.e9J7NaK4W3@kreacher> In-Reply-To: <2933888.e9J7NaK4W3@kreacher> From: "Rafael J. Wysocki" Date: Thu, 30 Nov 2023 20:33:14 +0100 Message-ID: Subject: Re: [PATCH v1 2/2] thermal: sysfs: Eliminate unnecessary zone locking To: "Rafael J. Wysocki" Cc: Daniel Lezcano , Lukasz Luba , Linux PM , LKML , 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]); Thu, 30 Nov 2023 11:33:38 -0800 (PST) On Thu, Nov 30, 2023 at 7:38 PM Rafael J. Wysocki wrote: > > From: Rafael J. Wysocki > > The _show() callback functions of the trip point sysfs attributes, > temperature, hysteresis and type, need not use thermal zone locking, > because the layout of the data structures they access does not change > after the thermal zone registration. > > Namely, they all need to access a specific entry in the thermal > zone's trips[] table that is always present for non-tripless thermal > zones and its size cannot change after the thermal zone has been > registered. Thus it is always safe to access the trips[] table of a > registered thermal zone from each of the sysfs attributes in question. > > Moreover, the type of a trip point does not change after registering its > thermal zone, and while its temperature and hysteresis can change, for > example due to a firmware-induced thermal zone update, holding the zone > lock around reading them is pointless, because it does not prevent stale > values from being returned to user space. For example, a trip point > temperature can always change ater trip_point_temp_show() has read it > and before the function's return statement is executed, regardless of > whether or not zone locking is used. > > For this reason, drop the zone locking from trip_point_type_show(), > trip_point_temp_show(), and trip_point_hyst_show(). > > Signed-off-by: Rafael J. Wysocki > --- > drivers/thermal/thermal_sysfs.c | 60 ++++++++++++++-------------------------- > 1 file changed, 21 insertions(+), 39 deletions(-) > > Index: linux-pm/drivers/thermal/thermal_sysfs.c > =================================================================== > --- linux-pm.orig/drivers/thermal/thermal_sysfs.c > +++ linux-pm/drivers/thermal/thermal_sysfs.c > @@ -83,25 +83,18 @@ trip_point_type_show(struct device *dev, > char *buf) > { > struct thermal_zone_device *tz = to_thermal_zone(dev); > - struct thermal_trip trip; > - int trip_id, result; > + int trip_id; > + > + if (!device_is_registered(dev)) > + return -ENODEV; > > if (sscanf(attr->attr.name, "trip_point_%d_type", &trip_id) != 1) > return -EINVAL; > > - mutex_lock(&tz->lock); > - > - if (device_is_registered(dev)) > - result = __thermal_zone_get_trip(tz, trip_id, &trip); > - else > - result = -ENODEV; > - > - mutex_unlock(&tz->lock); > - > - if (result) > - return result; > + if (trip_id < 0 || trip_id > tz->num_trips) An off-by-one here, it should be trip_id >= tz->num_trips (and analogously below). I'll send an update of this shortly. > + return -EINVAL; > > - switch (trip.type) { > + switch (tz->trips[trip_id].type) { > case THERMAL_TRIP_CRITICAL: > return sprintf(buf, "critical\n"); > case THERMAL_TRIP_HOT: > @@ -164,25 +157,18 @@ trip_point_temp_show(struct device *dev, > char *buf) > { > struct thermal_zone_device *tz = to_thermal_zone(dev); > - struct thermal_trip trip; > - int trip_id, ret; > + int trip_id; > + > + 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 = __thermal_zone_get_trip(tz, trip_id, &trip); > - else > - ret = -ENODEV; > - > - mutex_unlock(&tz->lock); > - > - if (ret) > - return ret; > + if (trip_id < 0 || trip_id > tz->num_trips) > + return -EINVAL; > > - return sprintf(buf, "%d\n", trip.temperature); > + return sprintf(buf, "%d\n", tz->trips[trip_id].temperature); > } > > static ssize_t > @@ -234,22 +220,18 @@ trip_point_hyst_show(struct device *dev, > char *buf) > { > struct thermal_zone_device *tz = to_thermal_zone(dev); > - struct thermal_trip trip; > - int trip_id, ret; > + int trip_id; > + > + 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 = __thermal_zone_get_trip(tz, trip_id, &trip); > - else > - ret = -ENODEV; > - > - mutex_unlock(&tz->lock); > + if (trip_id < 0 || trip_id > tz->num_trips) > + return -EINVAL; > > - return ret ? ret : sprintf(buf, "%d\n", trip.hysteresis); > + return sprintf(buf, "%d\n", tz->trips[trip_id].hysteresis); > } > > static ssize_t > > > >