From patchwork Tue Jan 7 11:33:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huisong Li X-Patchwork-Id: 13928769 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) (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 DF5021E3DCC; Tue, 7 Jan 2025 12:00:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.191 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736251260; cv=none; b=jUNLV0tW5MWOL5RbT7ewTW4RW2LdrnwdRvJsC8EpieL4GEsjU4lXxVDFYENua3XmQtTOw6KbYWApCX8Qem4AZOa2ZlilxVjHIZfDvPLCJe7Stian55bwpf11GDcFePqDX8vNT0fdqTG2MQWWk+yo/tI7TB8Sm1a8me9vUyY2cb8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736251260; c=relaxed/simple; bh=xCWqeAQHI04mjLJkOPci40xum0sHWZ95xAE5hBndvzs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=FkDOm+u12QeVdAYiLtMwO9WOk/rsi47SBM4udQdQI6IBdx4CNu8kF8LcFHcAksNG6Ky/9gVBjzmtpCcgajaUzjtBQqYIpQV2GEeU03S721nuAiut50RCHe6C0JRcGp9B4C1d8uPcX9xbavwAcjDNG+F8mxWK1JMSGkPTEEYmtp4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.191 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.234]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4YS8Km2JL1z1kxfL; Tue, 7 Jan 2025 19:42:20 +0800 (CST) Received: from dggemv711-chm.china.huawei.com (unknown [10.1.198.66]) by mail.maildlp.com (Postfix) with ESMTPS id 52AB614011B; Tue, 7 Jan 2025 19:45:20 +0800 (CST) Received: from kwepemn100009.china.huawei.com (7.202.194.112) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Tue, 7 Jan 2025 19:45:20 +0800 Received: from localhost.localdomain (10.28.79.22) by kwepemn100009.china.huawei.com (7.202.194.112) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Tue, 7 Jan 2025 19:45:19 +0800 From: Huisong Li To: CC: , , , , , , Subject: [PATCH v2 1/3] hwmon: (acpi_power_meter) Fix uninitialized variables Date: Tue, 7 Jan 2025 19:33:28 +0800 Message-ID: <20250107113330.7970-2-lihuisong@huawei.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20250107113330.7970-1-lihuisong@huawei.com> References: <20250107113330.7970-1-lihuisong@huawei.com> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemn100009.china.huawei.com (7.202.194.112) The 'power1_alarm' attribute uses the 'power' and 'cap' in the acpi_power_meter_resource structure. Currently, these two fields are just updated when user query 'power' and 'cap' attribute. If user directly query the 'power1_alarm' attribute without queryng above two attributes, driver will use uninitialized variables to judge. So this patch adds the setting of alarm state and update 'cap' in the notification callback and update 'power' and 'cap' if needed to show the real alarm state. Signed-off-by: Huisong Li --- drivers/hwmon/acpi_power_meter.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/drivers/hwmon/acpi_power_meter.c b/drivers/hwmon/acpi_power_meter.c index 2f1c9d97ad21..0b337d4fb212 100644 --- a/drivers/hwmon/acpi_power_meter.c +++ b/drivers/hwmon/acpi_power_meter.c @@ -84,6 +84,7 @@ struct acpi_power_meter_resource { u64 power; u64 cap; u64 avg_interval; + bool power_alarm; int sensors_valid; unsigned long sensors_last_updated; struct sensor_device_attribute sensors[NUM_SENSORS]; @@ -396,6 +397,9 @@ static ssize_t show_val(struct device *dev, struct acpi_device *acpi_dev = to_acpi_device(dev); struct acpi_power_meter_resource *resource = acpi_dev->driver_data; u64 val = 0; + int ret; + + guard(mutex)(&resource->lock); switch (attr->index) { case 0: @@ -423,10 +427,17 @@ static ssize_t show_val(struct device *dev, val = 0; break; case 6: - if (resource->power > resource->cap) - val = 1; - else - val = 0; + ret = update_meter(resource); + if (ret) + return ret; + /* need to update cap if not to support the notification. */ + if (!(resource->caps.flags & POWER_METER_CAN_NOTIFY)) { + ret = update_cap(resource); + if (ret) + return ret; + } + val = resource->power_alarm || resource->power > resource->cap; + resource->power_alarm = resource->power > resource->cap ? true : false; break; case 7: case 8: @@ -847,12 +858,20 @@ static void acpi_power_meter_notify(struct acpi_device *device, u32 event) sysfs_notify(&device->dev.kobj, NULL, POWER_AVERAGE_NAME); break; case METER_NOTIFY_CAP: + mutex_lock(&resource->lock); + res = update_cap(resource); + if (res) + dev_err(&device->dev, "update cap failed when capping value is changed.\n"); + mutex_unlock(&resource->lock); sysfs_notify(&device->dev.kobj, NULL, POWER_CAP_NAME); break; case METER_NOTIFY_INTERVAL: sysfs_notify(&device->dev.kobj, NULL, POWER_AVG_INTERVAL_NAME); break; case METER_NOTIFY_CAPPING: + mutex_lock(&resource->lock); + resource->power_alarm = true; + mutex_unlock(&resource->lock); sysfs_notify(&device->dev.kobj, NULL, POWER_ALARM_NAME); dev_info(&device->dev, "Capping in progress.\n"); break; From patchwork Tue Jan 7 11:33:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huisong Li X-Patchwork-Id: 13928746 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (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 E76181EE01F; Tue, 7 Jan 2025 11:45:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736250328; cv=none; b=AHO807E3lwKmN73FHYlfjUX8HBjH0vNd1B0si2GwvAx/P4Bj+Ej7lDf2aWIummf02iMuYqVuRwNyV1DgtWWG5Mf1Ldbhd9PHZ4mcRiIP9jxwydoExHtg+0cnQg8j3G5551ujJmQA2GMcPvqOg8zOLEjYOF5yC1fOBITTX+RVYFs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736250328; c=relaxed/simple; bh=bjSbK1/dcaOyUEPT9A1nk7BF/nXc7j7ppdKyUA2GeWY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=WGqeGIUJ5QDl0SS1oTCLD4VsuJSXsqT+aRBisHyRX0pd4gw53YJNWECJD20y4zf/WYW7GhwONcb7n+bf6hfnf7Zc67z3MsOgpKQ0X1u9N0bIwu3Yvq6uuQK4NpC33boCYH7PmOlZvZ9Rhy0c6IiCd4xNSVrJ7j1s5llACF1jxyQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4YS8K00FCMzxWt9; Tue, 7 Jan 2025 19:41:40 +0800 (CST) Received: from dggemv704-chm.china.huawei.com (unknown [10.3.19.47]) by mail.maildlp.com (Postfix) with ESMTPS id DAB231800CB; Tue, 7 Jan 2025 19:45:20 +0800 (CST) Received: from kwepemn100009.china.huawei.com (7.202.194.112) by dggemv704-chm.china.huawei.com (10.3.19.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Tue, 7 Jan 2025 19:45:20 +0800 Received: from localhost.localdomain (10.28.79.22) by kwepemn100009.china.huawei.com (7.202.194.112) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Tue, 7 Jan 2025 19:45:20 +0800 From: Huisong Li To: CC: , , , , , , Subject: [PATCH v2 2/3] hwmon: (acpi_power_meter) Fix update the power trip points on failure Date: Tue, 7 Jan 2025 19:33:29 +0800 Message-ID: <20250107113330.7970-3-lihuisong@huawei.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20250107113330.7970-1-lihuisong@huawei.com> References: <20250107113330.7970-1-lihuisong@huawei.com> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemn100009.china.huawei.com (7.202.194.112) The power trip points maintained in local should not be updated when '_PTP' method fails to evaluate. Signed-off-by: Huisong Li --- drivers/hwmon/acpi_power_meter.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/drivers/hwmon/acpi_power_meter.c b/drivers/hwmon/acpi_power_meter.c index 0b337d4fb212..594f7681d523 100644 --- a/drivers/hwmon/acpi_power_meter.c +++ b/drivers/hwmon/acpi_power_meter.c @@ -293,8 +293,8 @@ static ssize_t set_trip(struct device *dev, struct device_attribute *devattr, struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); struct acpi_device *acpi_dev = to_acpi_device(dev); struct acpi_power_meter_resource *resource = acpi_dev->driver_data; + unsigned long temp, trip_bk; int res; - unsigned long temp; res = kstrtoul(buf, 10, &temp); if (res) @@ -302,13 +302,15 @@ static ssize_t set_trip(struct device *dev, struct device_attribute *devattr, temp = DIV_ROUND_CLOSEST(temp, 1000); - mutex_lock(&resource->lock); + guard(mutex)(&resource->lock); + + trip_bk = resource->trip[attr->index - 7]; resource->trip[attr->index - 7] = temp; res = set_acpi_trip(resource); - mutex_unlock(&resource->lock); - - if (res) + if (res) { + resource->trip[attr->index - 7] = trip_bk; return res; + } return count; } From patchwork Tue Jan 7 11:33:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huisong Li X-Patchwork-Id: 13928748 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) (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 923981EE00D; Tue, 7 Jan 2025 11:45:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.255 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736250333; cv=none; b=SzEYiEFlEyQ1a91JjAufKcaGzwLHlwtxU+YKJ7DjhcdvvyerOCVf9Qk4Y5sPQbNpo6myiaDV1+DRpUAwWcQxOJ5aME/OeQUd9Nop8MyFyKVA37L/lOe8XSB7+pakE81U3f6FBVZZWRODCqjEM/8vzHfmPSOhQGINIUtyrlw1Ohc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736250333; c=relaxed/simple; bh=uad2b0Tyq+IuQqorDhEM4sUX5uhjGZENHUz87Z12eF0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=K26xFGB5cEbmpG0VBSF6bSCG4PtBVCoXhEW6j44t0OPhuMc5klBEH4SkyYBlLvEt+W/oWXvn+LuDgKgJ5nPgWce4ononkq2/wYqcxLgImllL8BlXlJFAFE/NdyJbLGatOXBcFZm88ZAyOV4N3sQozF1x7S+oHhmqIF/r5CckEnA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.255 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4YS8K05DN5z15NKl; Tue, 7 Jan 2025 19:41:40 +0800 (CST) Received: from dggemv703-chm.china.huawei.com (unknown [10.3.19.46]) by mail.maildlp.com (Postfix) with ESMTPS id 4C2B61800CB; Tue, 7 Jan 2025 19:45:21 +0800 (CST) Received: from kwepemn100009.china.huawei.com (7.202.194.112) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Tue, 7 Jan 2025 19:45:21 +0800 Received: from localhost.localdomain (10.28.79.22) by kwepemn100009.china.huawei.com (7.202.194.112) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Tue, 7 Jan 2025 19:45:20 +0800 From: Huisong Li To: CC: , , , , , , Subject: [PATCH v2 3/3] hwmon: (acpi_power_meter) Remove redundant 'sensors_valid' variable Date: Tue, 7 Jan 2025 19:33:30 +0800 Message-ID: <20250107113330.7970-4-lihuisong@huawei.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20250107113330.7970-1-lihuisong@huawei.com> References: <20250107113330.7970-1-lihuisong@huawei.com> Precedence: bulk X-Mailing-List: linux-hwmon@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemn100009.china.huawei.com (7.202.194.112) The 'sensors_valid' in acpi_power_meter_resource structure is always '1' after querying power once. The default value of this variable is zero which just ensure user can query power successfully without any time requirement at first time. We can get power and fill the 'sensors_last_updated' field at probing phase to make sure that a valid value is returned to user at first query within the sampling interval. Then this redundant variable can be safely removed. Signed-off-by: Huisong Li --- drivers/hwmon/acpi_power_meter.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/drivers/hwmon/acpi_power_meter.c b/drivers/hwmon/acpi_power_meter.c index 594f7681d523..49bef3350439 100644 --- a/drivers/hwmon/acpi_power_meter.c +++ b/drivers/hwmon/acpi_power_meter.c @@ -85,7 +85,6 @@ struct acpi_power_meter_resource { u64 cap; u64 avg_interval; bool power_alarm; - int sensors_valid; unsigned long sensors_last_updated; struct sensor_device_attribute sensors[NUM_SENSORS]; int num_sensors; @@ -316,15 +315,14 @@ static ssize_t set_trip(struct device *dev, struct device_attribute *devattr, } /* Power meter */ -static int update_meter(struct acpi_power_meter_resource *resource) +static int update_meter(struct acpi_power_meter_resource *resource, bool check) { unsigned long long data; acpi_status status; unsigned long local_jiffies = jiffies; - if (time_before(local_jiffies, resource->sensors_last_updated + - msecs_to_jiffies(resource->caps.sampling_time)) && - resource->sensors_valid) + if (check && time_before(local_jiffies, resource->sensors_last_updated + + msecs_to_jiffies(resource->caps.sampling_time))) return 0; status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PMM", @@ -336,7 +334,6 @@ static int update_meter(struct acpi_power_meter_resource *resource) } resource->power = data; - resource->sensors_valid = 1; resource->sensors_last_updated = jiffies; return 0; } @@ -349,7 +346,7 @@ static ssize_t show_power(struct device *dev, struct acpi_power_meter_resource *resource = acpi_dev->driver_data; mutex_lock(&resource->lock); - update_meter(resource); + update_meter(resource, true); mutex_unlock(&resource->lock); if (resource->power == UNKNOWN_POWER) @@ -429,7 +426,7 @@ static ssize_t show_val(struct device *dev, val = 0; break; case 6: - ret = update_meter(resource); + ret = update_meter(resource, true); if (ret) return ret; /* need to update cap if not to support the notification. */ @@ -699,6 +696,10 @@ static int setup_attrs(struct acpi_power_meter_resource *resource) return res; if (resource->caps.flags & POWER_METER_CAN_MEASURE) { + res = update_meter(resource, false); + if (res) + goto error; + res = register_attrs(resource, meter_attrs); if (res) goto error; @@ -898,7 +899,6 @@ static int acpi_power_meter_add(struct acpi_device *device) if (!resource) return -ENOMEM; - resource->sensors_valid = 0; resource->acpi_dev = device; mutex_init(&resource->lock); strcpy(acpi_device_name(device), ACPI_POWER_METER_DEVICE_NAME);