From nobody Thu Apr 9 10:29:04 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B1B5FC4332F for ; Mon, 17 Oct 2022 10:03:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231421AbiJQKDR (ORCPT ); Mon, 17 Oct 2022 06:03:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230473AbiJQKDO (ORCPT ); Mon, 17 Oct 2022 06:03:14 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF8F35E654 for ; Mon, 17 Oct 2022 03:03:12 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id d7-20020a17090a2a4700b0020d268b1f02so13756861pjg.1 for ; Mon, 17 Oct 2022 03:03:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=X4ECXKPxuSi9FlL0YX5UZrOOqChEolMljlUJMvD0OCY=; b=gZ22LJbRBZC4XEHPVSBT0gTQZjF75VJEv8GWn/tPZ9gJLtkTAS93CnERDsy63KhCpO fqBlcTAGClPK4PI38DBjKM2ZdTRD6g/ara7Joxr8IHwX2ayDURN0t8ozoiZLWgrDWYVu Cd+gjDR+XnxcqFrLfLYaIcwNvPhOlixq0wtBd2N+3HZVg8P5jVz3h3jnw8gHkJMhSp0h 5ZwiR6H1tgEULsQKRFkvvQhcaJNaPI01LsAfrfNn/eIZooJauY3MDG4e9nJON5Yu8TpU ztE6G/mggqW0FOOLMwCD2pHmaXEwmzk+ZNdxXryzeQYzWVWAmWmFsNQOhvPfJm7sD637 xTOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=X4ECXKPxuSi9FlL0YX5UZrOOqChEolMljlUJMvD0OCY=; b=d6ocuRW5f5wKMEeEHgakjAsTBeB3gfjGhh+DE7xR0qwi8CsQ5CUXBl0A+zOc3FKXEN NTOBvHDK9XYd0in8uJnTKaqNRiKcKI/GMOOUvEN6PkY8xyBeC5YeWPx300PIpyFwmm32 2D/zf8xF6SrDjGD/HMQAwKF8Z6IJJ1KR7m5tqC/SWyYSNNblB5xtMckrFGbP6dLzRich SU+Vb7kqw4hZahuhK1inFvdFtIkKnXTIu1dXwP1k/6xQcm0YhhjNtrtXMkNFJW/K+OfQ vQwMzAllsCpkBM2m2lVM6HMxCIcvN4rUC4Ia8RR6/cDJmEYz6BTgOoehCd6zgsXCyRzU M0dg== X-Gm-Message-State: ACrzQf2xTs339m2PLOrJa1/L2UUpFpaTCfkli5CilU5Pe0jLMYqXAhgv b2GpfdjqXDvYLG/7Urt3PgFECA== X-Google-Smtp-Source: AMsMyM4NhfHDJD2hLKC5bLKj3iKelofKvfP4UvUFgTg/zAYNeFiXVx3/ooXoYnq1Agb9vtYfh1gJuw== X-Received: by 2002:a17:90b:4c0b:b0:20d:8572:ab27 with SMTP id na11-20020a17090b4c0b00b0020d8572ab27mr12633543pjb.193.1666000992367; Mon, 17 Oct 2022 03:03:12 -0700 (PDT) Received: from localhost ([122.172.86.128]) by smtp.gmail.com with ESMTPSA id g8-20020a631108000000b00462612c2699sm5864667pgl.86.2022.10.17.03.03.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 03:03:11 -0700 (PDT) From: Viresh Kumar To: "Rafael J. Wysocki" , Daniel Lezcano , Amit Kucheria , Zhang Rui Cc: Viresh Kumar , linux-pm@vger.kernel.org, Vincent Guittot , Dan Carpenter , linux-kernel@vger.kernel.org Subject: [PATCH 1/2] thermal: Validate new state in cur_state_store() Date: Mon, 17 Oct 2022 15:33:01 +0530 Message-Id: <100e1f814d0d3e20a291e4bfdddabc2c0a4a12f0.1666000867.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.31.1.272.g89b43f80a514 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" In cur_state_store(), the new state of the cooling device is received from user-space and is not validated by the thermal core but the same is left for the individual drivers to take care of. Apart from duplicating the code it leaves possibility for introducing bugs where a driver may not do it right. Lets make the thermal core check the new state itself and store the max value in the cooling device structure. Link: https://lore.kernel.org/all/Y0ltRJRjO7AkawvE@kili/ Reported-by: Dan Carpenter Signed-off-by: Viresh Kumar --- drivers/thermal/gov_fair_share.c | 6 +----- drivers/thermal/thermal_core.c | 15 +++++++-------- drivers/thermal/thermal_sysfs.c | 11 +++++------ include/linux/thermal.h | 1 + 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/drivers/thermal/gov_fair_share.c b/drivers/thermal/gov_fair_sh= are.c index a4ee4661e9cc..1cfeac16e7ac 100644 --- a/drivers/thermal/gov_fair_share.c +++ b/drivers/thermal/gov_fair_share.c @@ -49,11 +49,7 @@ static int get_trip_level(struct thermal_zone_device *tz) static long get_target_state(struct thermal_zone_device *tz, struct thermal_cooling_device *cdev, int percentage, int level) { - unsigned long max_state; - - cdev->ops->get_max_state(cdev, &max_state); - - return (long)(percentage * level * max_state) / (100 * tz->num_trips); + return (long)(percentage * level * cdev->max_state) / (100 * tz->num_trip= s); } =20 /** diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 117eeaf7dd24..08de59369e94 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -603,8 +603,7 @@ int thermal_zone_bind_cooling_device(struct thermal_zon= e_device *tz, struct thermal_instance *pos; struct thermal_zone_device *pos1; struct thermal_cooling_device *pos2; - unsigned long max_state; - int result, ret; + int result; =20 if (trip >=3D tz->num_trips || trip < 0) return -EINVAL; @@ -621,15 +620,11 @@ int thermal_zone_bind_cooling_device(struct thermal_z= one_device *tz, if (tz !=3D pos1 || cdev !=3D pos2) return -EINVAL; =20 - ret =3D cdev->ops->get_max_state(cdev, &max_state); - if (ret) - return ret; - /* lower default 0, upper default max_state */ lower =3D lower =3D=3D THERMAL_NO_LIMIT ? 0 : lower; - upper =3D upper =3D=3D THERMAL_NO_LIMIT ? max_state : upper; + upper =3D upper =3D=3D THERMAL_NO_LIMIT ? cdev->max_state : upper; =20 - if (lower > upper || upper > max_state) + if (lower > upper || upper > cdev->max_state) return -EINVAL; =20 dev =3D kzalloc(sizeof(*dev), GFP_KERNEL); @@ -900,6 +895,10 @@ __thermal_cooling_device_register(struct device_node *= np, cdev->updated =3D false; cdev->device.class =3D &thermal_class; cdev->devdata =3D devdata; + + if (cdev->ops->get_max_state(cdev, &cdev->max_state)) + goto out_kfree_type; + thermal_cooling_device_setup_sysfs(cdev); ret =3D device_register(&cdev->device); if (ret) diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysf= s.c index ec495c7dff03..bd7596125461 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -589,13 +589,8 @@ static ssize_t max_state_show(struct device *dev, stru= ct device_attribute *attr, char *buf) { struct thermal_cooling_device *cdev =3D to_cooling_device(dev); - unsigned long state; - int ret; =20 - ret =3D cdev->ops->get_max_state(cdev, &state); - if (ret) - return ret; - return sprintf(buf, "%ld\n", state); + return sprintf(buf, "%ld\n", cdev->max_state); } =20 static ssize_t cur_state_show(struct device *dev, struct device_attribute = *attr, @@ -625,6 +620,10 @@ cur_state_store(struct device *dev, struct device_attr= ibute *attr, if ((long)state < 0) return -EINVAL; =20 + /* Requested state should be less than max_state + 1 */ + if (state > cdev->max_state) + return -EINVAL; + mutex_lock(&cdev->lock); =20 result =3D cdev->ops->set_cur_state(cdev, state); diff --git a/include/linux/thermal.h b/include/linux/thermal.h index 9ecc128944a1..5e093602e8fc 100644 --- a/include/linux/thermal.h +++ b/include/linux/thermal.h @@ -100,6 +100,7 @@ struct thermal_cooling_device_ops { struct thermal_cooling_device { int id; char *type; + unsigned long max_state; struct device device; struct device_node *np; void *devdata; --=20 2.31.1.272.g89b43f80a514 From nobody Thu Apr 9 10:29:04 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BE54CC4332F for ; Mon, 17 Oct 2022 10:03:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231532AbiJQKD1 (ORCPT ); Mon, 17 Oct 2022 06:03:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231443AbiJQKDR (ORCPT ); Mon, 17 Oct 2022 06:03:17 -0400 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E68105E662 for ; Mon, 17 Oct 2022 03:03:15 -0700 (PDT) Received: by mail-pf1-x42e.google.com with SMTP id g28so10607017pfk.8 for ; Mon, 17 Oct 2022 03:03:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8UaAzKyNq9nnRse/+cCAjXKMYZQLGLVwCydbfKuvY54=; b=VrvnK+3ZAIN1ufgfpHItfJNANC6LgdnWBLQHxtEuLb3d/QY/jQsOlmkAnaHrqZQFdp T01CIpbq34QT1DYa9+H26ykD1hvXuToNZiWwhoPn2R6uffGCuCmxeNNQ2NieeJeTgFnG W9J6HeuQjI8eLQFBZ5S+JewdaeEBnrIi4Fh7B4G3eOgSVp1bnWq5656oMR6gfXKTJ7+U eaBGZMoQbTPvfFTvLQWaP0RxBrfZ5W0Gjv0kdgGa7iC+3Lav++wgaHN0qYbzvrpLz1Os 3vfvzVbfLwsg5qiZh50Mo3cG9qL6MtaFFTctAoq16PlUNgVnDw2e/QvnTSMnvefhwYsn R7xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8UaAzKyNq9nnRse/+cCAjXKMYZQLGLVwCydbfKuvY54=; b=Hr10H+a8Xkl/YuVjioAHfuULnyhj9qnfZDBPV+Ir713fksfokM417tufuyOBriZKu/ sNr6kPxtfBNnaM0UGL36qAy8gwEyDQaxRl+918dtHgbrDcdHPL6WPn2uMvyW4v2yv8Rx 8nZXF5GTOsMQvm4xPFahyH9WAFDfLtQrjMJDu6WXeCYm4z7m2eO5TCZIdQXTLRVRjAKX 5VQGneepFs4CBnLMilTKyKGoYI2SRbxaY+yMQa9MOxXzD669AHoXtwh/4YmGwb9IMol9 pcqHMBmPjC+mUOqv5yxTRLVohOoWggDdsiJf1QaI/a9pNKLU2zaDxyI0Zv6R/O7fqSjA XPHQ== X-Gm-Message-State: ACrzQf1SUJ5MdZ91GxqggjC4dPOR4G418xYqV7vSU6fsGshlrKcSDxgN R5QTI1DkqsH0DuCxo7eiaFXnukspkvunDw== X-Google-Smtp-Source: AMsMyM6qZa2HnPhyKKOx8pZXzS74QOIoLa7aYXY+beudzq7U2papGMTBBB3rJGRMsd+XaW/aDQBZ/Q== X-Received: by 2002:a05:6a00:15c2:b0:565:bc96:1c75 with SMTP id o2-20020a056a0015c200b00565bc961c75mr11715950pfu.23.1666000995277; Mon, 17 Oct 2022 03:03:15 -0700 (PDT) Received: from localhost ([122.172.86.128]) by smtp.gmail.com with ESMTPSA id i18-20020a056a00005200b00561b02e3118sm6662744pfk.106.2022.10.17.03.03.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 03:03:14 -0700 (PDT) From: Viresh Kumar To: "Rafael J. Wysocki" , Daniel Lezcano , Amit Kucheria , Zhang Rui Cc: Viresh Kumar , linux-pm@vger.kernel.org, Vincent Guittot , Dan Carpenter , linux-kernel@vger.kernel.org Subject: [PATCH 2/2] thermal: sysfs: Reuse cdev->max_state Date: Mon, 17 Oct 2022 15:33:02 +0530 Message-Id: <56d3dcfd92206eea6164ed26e40ae307c5c5219d.1666000867.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.31.1.272.g89b43f80a514 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Now that the cooling device structure stores the max_state value, reuse it and drop max_states from struct cooling_dev_stats. Signed-off-by: Viresh Kumar --- drivers/thermal/thermal_sysfs.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysf= s.c index bd7596125461..febf9e76c440 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -661,7 +661,6 @@ struct cooling_dev_stats { spinlock_t lock; unsigned int total_trans; unsigned long state; - unsigned long max_states; ktime_t last_time; ktime_t *time_in_state; unsigned int *trans_table; @@ -691,7 +690,7 @@ void thermal_cooling_device_stats_update(struct thermal= _cooling_device *cdev, goto unlock; =20 update_time_in_state(stats); - stats->trans_table[stats->state * stats->max_states + new_state]++; + stats->trans_table[stats->state * (cdev->max_state + 1) + new_state]++; stats->state =3D new_state; stats->total_trans++; =20 @@ -725,7 +724,7 @@ time_in_state_ms_show(struct device *dev, struct device= _attribute *attr, spin_lock(&stats->lock); update_time_in_state(stats); =20 - for (i =3D 0; i < stats->max_states; i++) { + for (i =3D 0; i <=3D cdev->max_state; i++) { len +=3D sprintf(buf + len, "state%u\t%llu\n", i, ktime_to_ms(stats->time_in_state[i])); } @@ -740,7 +739,7 @@ reset_store(struct device *dev, struct device_attribute= *attr, const char *buf, { struct thermal_cooling_device *cdev =3D to_cooling_device(dev); struct cooling_dev_stats *stats =3D cdev->stats; - int i, states =3D stats->max_states; + int i, states =3D cdev->max_state + 1; =20 spin_lock(&stats->lock); =20 @@ -749,7 +748,7 @@ reset_store(struct device *dev, struct device_attribute= *attr, const char *buf, memset(stats->trans_table, 0, states * states * sizeof(*stats->trans_table)); =20 - for (i =3D 0; i < stats->max_states; i++) + for (i =3D 0; i < states; i++) stats->time_in_state[i] =3D ktime_set(0, 0); =20 spin_unlock(&stats->lock); @@ -767,7 +766,7 @@ static ssize_t trans_table_show(struct device *dev, =20 len +=3D snprintf(buf + len, PAGE_SIZE - len, " From : To\n"); len +=3D snprintf(buf + len, PAGE_SIZE - len, " : "); - for (i =3D 0; i < stats->max_states; i++) { + for (i =3D 0; i <=3D cdev->max_state; i++) { if (len >=3D PAGE_SIZE) break; len +=3D snprintf(buf + len, PAGE_SIZE - len, "state%2u ", i); @@ -777,17 +776,17 @@ static ssize_t trans_table_show(struct device *dev, =20 len +=3D snprintf(buf + len, PAGE_SIZE - len, "\n"); =20 - for (i =3D 0; i < stats->max_states; i++) { + for (i =3D 0; i <=3D cdev->max_state; i++) { if (len >=3D PAGE_SIZE) break; =20 len +=3D snprintf(buf + len, PAGE_SIZE - len, "state%2u:", i); =20 - for (j =3D 0; j < stats->max_states; j++) { + for (j =3D 0; j <=3D cdev->max_state; j++) { if (len >=3D PAGE_SIZE) break; len +=3D snprintf(buf + len, PAGE_SIZE - len, "%8u ", - stats->trans_table[i * stats->max_states + j]); + stats->trans_table[i * (cdev->max_state + 1) + j]); } if (len >=3D PAGE_SIZE) break; @@ -823,14 +822,10 @@ static void cooling_device_stats_setup(struct thermal= _cooling_device *cdev) { const struct attribute_group *stats_attr_group =3D NULL; struct cooling_dev_stats *stats; - unsigned long states; + /* Total number of states is highest state + 1 */ + unsigned long states =3D cdev->max_state + 1; int var; =20 - if (cdev->ops->get_max_state(cdev, &states)) - goto out; - - states++; /* Total number of states is highest state + 1 */ - var =3D sizeof(*stats); var +=3D sizeof(*stats->time_in_state) * states; var +=3D sizeof(*stats->trans_table) * states * states; @@ -843,7 +838,6 @@ static void cooling_device_stats_setup(struct thermal_c= ooling_device *cdev) stats->trans_table =3D (unsigned int *)(stats->time_in_state + states); cdev->stats =3D stats; stats->last_time =3D ktime_get(); - stats->max_states =3D states; =20 spin_lock_init(&stats->lock); =20 --=20 2.31.1.272.g89b43f80a514