From patchwork Wed Apr 8 04:19:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhang, Rui" X-Patchwork-Id: 11479309 X-Patchwork-Delegate: rui.zhang@intel.com Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8655192A for ; Wed, 8 Apr 2020 04:19:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6F78A2076D for ; Wed, 8 Apr 2020 04:19:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726477AbgDHET1 (ORCPT ); Wed, 8 Apr 2020 00:19:27 -0400 Received: from mga11.intel.com ([192.55.52.93]:49178 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725763AbgDHET1 (ORCPT ); Wed, 8 Apr 2020 00:19:27 -0400 IronPort-SDR: NiTNxjS5LNo/EJO7p6wMtmndOtuGZPWcjFbiC/TPu9b4m8M7WyvLpvEZ22C3dzABHEiDiMxTLY LrNl137V/13Q== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Apr 2020 21:19:27 -0700 IronPort-SDR: TW3IYBidF6SyvVkoBppYF0QA3DFGkleFh+qylxLU393M/DPVwLLJeT5jVmHaXRK+6e2w5WdlIf Nf24RgGkBgCg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,357,1580803200"; d="scan'208";a="451470370" Received: from tliao1-mobl2.ccr.corp.intel.com (HELO rzhang1-mobile.ccr.corp.intel.com) ([10.249.172.195]) by fmsmga005.fm.intel.com with ESMTP; 07 Apr 2020 21:19:25 -0700 From: Zhang Rui To: linux-pm@vger.kernel.org Cc: rui.zhang@intel.com, daniel.lezcano@linaro.org, tiwai@suse.de, viresh.kumar@linaro.org Subject: [RFC PATCH 1/5] thermal: rename thermal_cooling_device_stats_update() Date: Wed, 8 Apr 2020 12:19:13 +0800 Message-Id: <20200408041917.2329-1-rui.zhang@intel.com> X-Mailer: git-send-email 2.17.1 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Rename thermal_cooling_device_stats_update() to thermal_cdev_stats_update_cur() Signed-off-by: Zhang Rui --- drivers/thermal/thermal_core.h | 4 ++-- drivers/thermal/thermal_helpers.c | 2 +- drivers/thermal/thermal_sysfs.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h index a9bf00e91d64..722902d5724a 100644 --- a/drivers/thermal/thermal_core.h +++ b/drivers/thermal/thermal_core.h @@ -81,11 +81,11 @@ ssize_t weight_store(struct device *, struct device_attribute *, const char *, size_t); #ifdef CONFIG_THERMAL_STATISTICS -void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev, +void thermal_cdev_stats_update_cur(struct thermal_cooling_device *cdev, unsigned long new_state); #else static inline void -thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev, +thermal_cdev_stats_update_cur(struct thermal_cooling_device *cdev, unsigned long new_state) {} #endif /* CONFIG_THERMAL_STATISTICS */ diff --git a/drivers/thermal/thermal_helpers.c b/drivers/thermal/thermal_helpers.c index 2ba756af76b7..3af895e5dfce 100644 --- a/drivers/thermal/thermal_helpers.c +++ b/drivers/thermal/thermal_helpers.c @@ -186,7 +186,7 @@ void thermal_cdev_update(struct thermal_cooling_device *cdev) } if (!cdev->ops->set_cur_state(cdev, target)) - thermal_cooling_device_stats_update(cdev, target); + thermal_cdev_stats_update_cur(cdev, target); cdev->updated = true; mutex_unlock(&cdev->lock); diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c index aa99edb4dff7..00caa7787b71 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -716,7 +716,7 @@ cur_state_store(struct device *dev, struct device_attribute *attr, result = cdev->ops->set_cur_state(cdev, state); if (!result) - thermal_cooling_device_stats_update(cdev, state); + thermal_cdev_stats_update_cur(cdev, state); mutex_unlock(&cdev->lock); return result ? result : count; @@ -765,7 +765,7 @@ static void update_time_in_state(struct cooling_dev_stats *stats) stats->last_time = now; } -void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev, +void thermal_cdev_stats_update_cur(struct thermal_cooling_device *cdev, unsigned long new_state) { struct cooling_dev_stats *stats = cdev->stats; From patchwork Wed Apr 8 04:19:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhang, Rui" X-Patchwork-Id: 11479311 X-Patchwork-Delegate: rui.zhang@intel.com Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7CF1992A for ; Wed, 8 Apr 2020 04:19:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 660632076D for ; Wed, 8 Apr 2020 04:19:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726492AbgDHETc (ORCPT ); Wed, 8 Apr 2020 00:19:32 -0400 Received: from mga11.intel.com ([192.55.52.93]:49178 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725763AbgDHETb (ORCPT ); Wed, 8 Apr 2020 00:19:31 -0400 IronPort-SDR: PzkXhYO9vke/NuhMUw4/sxHbZzOZYyfNv4TwgqBczXugj9ZnvGzEd1Dl8R6IfQYSYWGH9/Kbk8 fCDMTgD/oTMg== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Apr 2020 21:19:31 -0700 IronPort-SDR: o7yHOJVZg29UVPBaxHBmkJS2OQ5FC8ygeHI1a+hirdEpSoNm0dahrtN+zMe3d7X4fWj5tNCi+a QC8vSelTmcfw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,357,1580803200"; d="scan'208";a="451470384" Received: from tliao1-mobl2.ccr.corp.intel.com (HELO rzhang1-mobile.ccr.corp.intel.com) ([10.249.172.195]) by fmsmga005.fm.intel.com with ESMTP; 07 Apr 2020 21:19:30 -0700 From: Zhang Rui To: linux-pm@vger.kernel.org Cc: rui.zhang@intel.com, daniel.lezcano@linaro.org, tiwai@suse.de, viresh.kumar@linaro.org Subject: [RFC PATCH 2/5] thermal: create statistics table in two steps Date: Wed, 8 Apr 2020 12:19:14 +0800 Message-Id: <20200408041917.2329-2-rui.zhang@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200408041917.2329-1-rui.zhang@intel.com> References: <20200408041917.2329-1-rui.zhang@intel.com> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Part of cooling device stats table is created based on the number of cooling states supported, and this may be changed at runtime. Introduce cooling_device_stats_resize() to handle this piece of the statistics table. Plus, check the existence of the statistics table before access because the table may fail to be created during cooling device registration. Signed-off-by: Zhang Rui --- drivers/thermal/thermal_sysfs.c | 56 ++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c index 00caa7787b71..45cfc2746874 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -770,6 +770,9 @@ void thermal_cdev_stats_update_cur(struct thermal_cooling_device *cdev, { struct cooling_dev_stats *stats = cdev->stats; + if (!stats) + return; + spin_lock(&stats->lock); if (stats->state == new_state) @@ -904,33 +907,52 @@ static const struct attribute_group cooling_device_stats_attr_group = { .name = "stats" }; -static void cooling_device_stats_setup(struct thermal_cooling_device *cdev) +static int cooling_device_stats_resize(struct thermal_cooling_device *cdev) { - struct cooling_dev_stats *stats; + struct cooling_dev_stats *stats = cdev->stats; unsigned long states; - int var; + int ret; - if (cdev->ops->get_max_state(cdev, &states)) - return; + ret = cdev->ops->get_max_state(cdev, &states); + if (ret) + return ret; states++; /* Total number of states is highest state + 1 */ - var = sizeof(*stats); - var += sizeof(*stats->time_in_state) * states; - var += sizeof(*stats->trans_table) * states * states; + stats->time_in_state = kcalloc(states, sizeof(*stats->time_in_state), GFP_KERNEL); + if (!stats->time_in_state) + return -ENOMEM; - stats = kzalloc(var, GFP_KERNEL); - if (!stats) - return; + stats->trans_table = kcalloc(states, sizeof(*stats->trans_table) * states, GFP_KERNEL); + if (!stats->trans_table) { + kfree(stats->time_in_state); + return -ENOMEM; + } - stats->time_in_state = (ktime_t *)(stats + 1); - stats->trans_table = (unsigned int *)(stats->time_in_state + states); - cdev->stats = stats; stats->last_time = ktime_get(); stats->max_states = states; + return 0; +} +static void cooling_device_stats_setup(struct thermal_cooling_device *cdev) +{ + struct cooling_dev_stats *stats; + int var, ret; + + stats = kzalloc(sizeof(*stats), GFP_KERNEL); + if (!stats) + return; + + cdev->stats = stats; spin_lock_init(&stats->lock); + ret = cooling_device_stats_resize(cdev); + if (ret) { + kfree(cdev->stats); + cdev->stats = NULL; + return; + } + /* Fill the empty slot left in cooling_device_attr_groups */ var = ARRAY_SIZE(cooling_device_attr_groups) - 2; cooling_device_attr_groups[var] = &cooling_device_stats_attr_group; @@ -938,6 +960,12 @@ static void cooling_device_stats_setup(struct thermal_cooling_device *cdev) static void cooling_device_stats_destroy(struct thermal_cooling_device *cdev) { + struct cooling_dev_stats *stats = cdev->stats; + + if (!stats) + return; + kfree(stats->time_in_state); + kfree(stats->trans_table); kfree(cdev->stats); cdev->stats = NULL; } From patchwork Wed Apr 8 04:19:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhang, Rui" X-Patchwork-Id: 11479313 X-Patchwork-Delegate: rui.zhang@intel.com Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4BF1792A for ; Wed, 8 Apr 2020 04:19:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 34F282074F for ; Wed, 8 Apr 2020 04:19:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726494AbgDHETg (ORCPT ); Wed, 8 Apr 2020 00:19:36 -0400 Received: from mga11.intel.com ([192.55.52.93]:49178 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725763AbgDHETg (ORCPT ); Wed, 8 Apr 2020 00:19:36 -0400 IronPort-SDR: wiMRAWE719wQk/c9NVUVFUe0IkiIxcMAv9blDaEiIwI8pArudZma/1eZLuN6/Qu1zCCzWhoWPW kkPm54I+SaTg== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Apr 2020 21:19:36 -0700 IronPort-SDR: 7QAtOA5qb/lU7NSXiHQNlNsHIwZOQFw/nqdz/mF608nMq6WAQ6TOaSdfOdAtqjkwx56MgsfX3/ nSDKFmisbnXA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,357,1580803200"; d="scan'208";a="451470401" Received: from tliao1-mobl2.ccr.corp.intel.com (HELO rzhang1-mobile.ccr.corp.intel.com) ([10.249.172.195]) by fmsmga005.fm.intel.com with ESMTP; 07 Apr 2020 21:19:34 -0700 From: Zhang Rui To: linux-pm@vger.kernel.org Cc: rui.zhang@intel.com, daniel.lezcano@linaro.org, tiwai@suse.de, viresh.kumar@linaro.org Subject: [RFC PATCH 3/5] thermal: support statistics table resizing at runtime Date: Wed, 8 Apr 2020 12:19:15 +0800 Message-Id: <20200408041917.2329-3-rui.zhang@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200408041917.2329-1-rui.zhang@intel.com> References: <20200408041917.2329-1-rui.zhang@intel.com> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Introduce thermal_cdev_stats_update_max() which can be used to update the cooling device statistics table when maximum cooling state of a cooling device is changed. Signed-off-by: Zhang Rui --- drivers/thermal/thermal_sysfs.c | 65 ++++++++++++++++++++++++++------- include/linux/thermal.h | 7 ++++ 2 files changed, 58 insertions(+), 14 deletions(-) diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c index 45cfc2746874..96e4a445952f 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -745,6 +745,10 @@ static const struct attribute_group *cooling_device_attr_groups[] = { }; #ifdef CONFIG_THERMAL_STATISTICS +static int cooling_device_stats_resize(struct thermal_cooling_device *cdev, + unsigned long cur_state, + unsigned long max_state); + struct cooling_dev_stats { spinlock_t lock; unsigned int total_trans; @@ -787,6 +791,23 @@ void thermal_cdev_stats_update_cur(struct thermal_cooling_device *cdev, spin_unlock(&stats->lock); } +void thermal_cdev_stats_update_max(struct thermal_cooling_device *cdev) +{ + struct cooling_dev_stats *stats = cdev->stats; + unsigned long cur_state, max_state; + + if (!stats) + return; + + if (cdev->ops->get_max_state(cdev, &max_state)) + return; + + if (cdev->ops->get_cur_state(cdev, &cur_state)) + return; + + cooling_device_stats_resize(cdev, cur_state, max_state); +} + static ssize_t total_trans_show(struct device *dev, struct device_attribute *attr, char *buf) { @@ -907,36 +928,44 @@ static const struct attribute_group cooling_device_stats_attr_group = { .name = "stats" }; -static int cooling_device_stats_resize(struct thermal_cooling_device *cdev) +static int cooling_device_stats_resize(struct thermal_cooling_device *cdev, + unsigned long cur_state, + unsigned long max_state) { struct cooling_dev_stats *stats = cdev->stats; - unsigned long states; - int ret; - - ret = cdev->ops->get_max_state(cdev, &states); - if (ret) - return ret; + unsigned long states = max_state + 1; + void *time_in_state, *trans_table; - states++; /* Total number of states is highest state + 1 */ + if (stats->max_states == states) + return 0; - stats->time_in_state = kcalloc(states, sizeof(*stats->time_in_state), GFP_KERNEL); - if (!stats->time_in_state) + time_in_state = kcalloc(states, sizeof(*stats->time_in_state), GFP_KERNEL); + if (!time_in_state) return -ENOMEM; - stats->trans_table = kcalloc(states, sizeof(*stats->trans_table) * states, GFP_KERNEL); - if (!stats->trans_table) { - kfree(stats->time_in_state); + trans_table = kcalloc(states, sizeof(*stats->trans_table) * states, GFP_KERNEL); + if (!trans_table) { + kfree(time_in_state); return -ENOMEM; } + spin_lock(&stats->lock); + kfree(stats->time_in_state); + kfree(stats->trans_table); + stats->time_in_state = time_in_state; + stats->trans_table = trans_table; stats->last_time = ktime_get(); stats->max_states = states; + stats->state = cur_state; + stats->total_trans = 0; + spin_unlock(&stats->lock); return 0; } static void cooling_device_stats_setup(struct thermal_cooling_device *cdev) { struct cooling_dev_stats *stats; + unsigned long max_state; int var, ret; stats = kzalloc(sizeof(*stats), GFP_KERNEL); @@ -946,7 +975,15 @@ static void cooling_device_stats_setup(struct thermal_cooling_device *cdev) cdev->stats = stats; spin_lock_init(&stats->lock); - ret = cooling_device_stats_resize(cdev); + ret = cdev->ops->get_max_state(cdev, &max_state); + if (ret) + return; + + /** + * cooling device current state will be updated soon + * during registration + **/ + ret = cooling_device_stats_resize(cdev, 0, max_state); if (ret) { kfree(cdev->stats); cdev->stats = NULL; diff --git a/include/linux/thermal.h b/include/linux/thermal.h index 126913c6a53b..cf3fad92f473 100644 --- a/include/linux/thermal.h +++ b/include/linux/thermal.h @@ -533,4 +533,11 @@ static inline void thermal_notify_framework(struct thermal_zone_device *tz, { } #endif /* CONFIG_THERMAL */ +#ifdef CONFIG_THERMAL_STATISTICS +void thermal_cdev_stats_update_max(struct thermal_cooling_device *cdev); +#else +static inline void +thermal_cdev_stats_update_max(struct thermal_cooling_device *cdev) {} +#endif /* CONFIG_THERMAL_STATISTICS */ + #endif /* __THERMAL_H__ */ From patchwork Wed Apr 8 04:19:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhang, Rui" X-Patchwork-Id: 11479315 X-Patchwork-Delegate: rui.zhang@intel.com Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CB2591805 for ; Wed, 8 Apr 2020 04:19:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A85672074F for ; Wed, 8 Apr 2020 04:19:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726495AbgDHETi (ORCPT ); Wed, 8 Apr 2020 00:19:38 -0400 Received: from mga11.intel.com ([192.55.52.93]:49178 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725763AbgDHETi (ORCPT ); Wed, 8 Apr 2020 00:19:38 -0400 IronPort-SDR: 1+EXfqA+0AL55gHzWeauR/odXv9k8IMSggGugzRPEetkcipKrd9CbMMY0FdbtY+g6iK5tx42OF Lq12uVMXqvsw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Apr 2020 21:19:38 -0700 IronPort-SDR: Fr1b150FY2EfJ9SJU7sEJCXrDRkdddmUEbfQtDI89Kn/5cX7Qaa1T9DXhMt304HKIr+/0oxyyA Y3DkncAavPyw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,357,1580803200"; d="scan'208";a="451470411" Received: from tliao1-mobl2.ccr.corp.intel.com (HELO rzhang1-mobile.ccr.corp.intel.com) ([10.249.172.195]) by fmsmga005.fm.intel.com with ESMTP; 07 Apr 2020 21:19:36 -0700 From: Zhang Rui To: linux-pm@vger.kernel.org Cc: rui.zhang@intel.com, daniel.lezcano@linaro.org, tiwai@suse.de, viresh.kumar@linaro.org Subject: [RFC PATCH 4/5] thermal: Add a sanity check for invalid state at stats update Date: Wed, 8 Apr 2020 12:19:16 +0800 Message-Id: <20200408041917.2329-4-rui.zhang@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200408041917.2329-1-rui.zhang@intel.com> References: <20200408041917.2329-1-rui.zhang@intel.com> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Takashi Iwai This is from the origin changelog "The thermal sysfs handler keeps the statistics table with the fixed size that was determined from the initial max_states() call, and the table entry is updated at each sysfs cur_state write call. And, when the driver's set_cur_state() ops accepts the value given from user-space, the thermal sysfs core blindly applies it to the statistics table entry, which may overflow and cause an Oops. Although it's rather a bug in the driver's ops implementations, we shouldn't crash but rather give a proper warning instead. This patch adds a sanity check for avoiding such an OOB access and warns with a stack trace to show the suspicious device in question." Part of the problem described above is gone, but I'd like to keep this patch so that we can detect the other max_state changes from other drivers, and fix the drivers by invoking thermal_cdev_stats_update_max() when necessary. Signed-off-by: Takashi Iwai Signed-off-by: Zhang Rui --- drivers/thermal/thermal_sysfs.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c index 96e4a445952f..e0e21c67e78a 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -779,6 +779,11 @@ void thermal_cdev_stats_update_cur(struct thermal_cooling_device *cdev, spin_lock(&stats->lock); + if (dev_WARN_ONCE(&cdev->device, new_state >= stats->max_states, + "new state %ld exceeds max_state %ld", + new_state, stats->max_states)) + goto unlock; + if (stats->state == new_state) goto unlock; From patchwork Wed Apr 8 04:19:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhang, Rui" X-Patchwork-Id: 11479317 X-Patchwork-Delegate: rui.zhang@intel.com Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 754BE1805 for ; Wed, 8 Apr 2020 04:19:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 548752074F for ; Wed, 8 Apr 2020 04:19:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726504AbgDHETj (ORCPT ); Wed, 8 Apr 2020 00:19:39 -0400 Received: from mga11.intel.com ([192.55.52.93]:49178 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725763AbgDHETj (ORCPT ); Wed, 8 Apr 2020 00:19:39 -0400 IronPort-SDR: ih0c5nG8TAkJil4C8DRH7YRGS7rfqk33MXe4Rvdm0R/CucJc5Eo1gzIPpQvhLICJdTd0BimDrx 8I5AI/rcjycA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Apr 2020 21:19:39 -0700 IronPort-SDR: jyg34RvdK0nQjnvjjoNWcJ63L8z5/qTrAxVIFJpVq9K1CT7BDEn7h4ILXpuO9rpjsSRnnDUayd 2XHdp6z6/Ecg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,357,1580803200"; d="scan'208";a="451470419" Received: from tliao1-mobl2.ccr.corp.intel.com (HELO rzhang1-mobile.ccr.corp.intel.com) ([10.249.172.195]) by fmsmga005.fm.intel.com with ESMTP; 07 Apr 2020 21:19:38 -0700 From: Zhang Rui To: linux-pm@vger.kernel.org Cc: rui.zhang@intel.com, daniel.lezcano@linaro.org, tiwai@suse.de, viresh.kumar@linaro.org Subject: [RFC PATCH 5/5] ACPI: processor: do update when maximum cooling state changed Date: Wed, 8 Apr 2020 12:19:17 +0800 Message-Id: <20200408041917.2329-5-rui.zhang@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200408041917.2329-1-rui.zhang@intel.com> References: <20200408041917.2329-1-rui.zhang@intel.com> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org ACPI processor cooling device supports 1 cooling state before cpufreq driver probed, and 4 cooling states after cpufreq driver probed. Thus update the statistics table when the cpufeq driver is probed/unprobed. This fixes an OOB issue when updating the statistics of the processor cooling device. Fixes: 8ea229511e06 ("thermal: Add cooling device's statistics in sysfs") Reported-by: Takashi Iwai Signed-off-by: Zhang Rui --- drivers/acpi/processor_thermal.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/acpi/processor_thermal.c b/drivers/acpi/processor_thermal.c index 41feb88ee92d..179d1b50ee2b 100644 --- a/drivers/acpi/processor_thermal.c +++ b/drivers/acpi/processor_thermal.c @@ -142,6 +142,7 @@ void acpi_thermal_cpufreq_init(struct cpufreq_policy *policy) if (ret < 0) pr_err("Failed to add freq constraint for CPU%d (%d)\n", cpu, ret); + thermal_cdev_stats_update_max(pr->cdev); } } @@ -154,6 +155,7 @@ void acpi_thermal_cpufreq_exit(struct cpufreq_policy *policy) if (pr) freq_qos_remove_request(&pr->thermal_req); + thermal_cdev_stats_update_max(pr->cdev); } } #else /* ! CONFIG_CPU_FREQ */