diff mbox series

[v4,16/22] cpupower: add the function to get the sysfs value from specific table

Message ID 20211119103102.88124-17-ray.huang@amd.com (mailing list archive)
State Superseded, archived
Headers show
Series cpufreq: introduce a new AMD CPU frequency control mechanism | expand

Commit Message

Huang Rui Nov. 19, 2021, 10:30 a.m. UTC
Expose the helper into cpufreq header, then cpufreq driver can use this
function to get the sysfs value if it has any specific sysfs interfaces.

Signed-off-by: Huang Rui <ray.huang@amd.com>
---
 tools/power/cpupower/lib/cpufreq.c | 21 +++++++++++++++------
 tools/power/cpupower/lib/cpufreq.h | 12 ++++++++++++
 2 files changed, 27 insertions(+), 6 deletions(-)

Comments

Fontenot, Nathan Nov. 19, 2021, 8:38 p.m. UTC | #1
On 11/19/21 4:30 AM, Huang Rui wrote:
> Expose the helper into cpufreq header, then cpufreq driver can use this
> function to get the sysfs value if it has any specific sysfs interfaces.
> 
> Signed-off-by: Huang Rui <ray.huang@amd.com>
> ---
>  tools/power/cpupower/lib/cpufreq.c | 21 +++++++++++++++------
>  tools/power/cpupower/lib/cpufreq.h | 12 ++++++++++++
>  2 files changed, 27 insertions(+), 6 deletions(-)
> 
> diff --git a/tools/power/cpupower/lib/cpufreq.c b/tools/power/cpupower/lib/cpufreq.c
> index c3b56db8b921..02719cc400a1 100644
> --- a/tools/power/cpupower/lib/cpufreq.c
> +++ b/tools/power/cpupower/lib/cpufreq.c
> @@ -83,20 +83,21 @@ static const char *cpufreq_value_files[MAX_CPUFREQ_VALUE_READ_FILES] = {
>  	[STATS_NUM_TRANSITIONS] = "stats/total_trans"
>  };
>  
> -
> -static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu,
> -						 enum cpufreq_value which)
> +unsigned long cpufreq_get_sysfs_value_from_table(unsigned int cpu,
> +						 const char **table,
> +						 unsigned index,
> +						 unsigned size)
>  {
>  	unsigned long value;
>  	unsigned int len;
>  	char linebuf[MAX_LINE_LEN];
>  	char *endp;
>  
> -	if (which >= MAX_CPUFREQ_VALUE_READ_FILES)
> +	if (!table && !table[index] && index >= size)

Should you validate the index before accessing table[index]

        if (!table && index >= size && !table[index])

-Nathan

>  		return 0;
>  
> -	len = sysfs_cpufreq_read_file(cpu, cpufreq_value_files[which],
> -				linebuf, sizeof(linebuf));
> +	len = sysfs_cpufreq_read_file(cpu, table[index], linebuf,
> +				      sizeof(linebuf));
>  
>  	if (len == 0)
>  		return 0;
> @@ -109,6 +110,14 @@ static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu,
>  	return value;
>  }
>  
> +static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu,
> +						 enum cpufreq_value which)
> +{
> +	return cpufreq_get_sysfs_value_from_table(cpu, cpufreq_value_files,
> +						  which,
> +						  MAX_CPUFREQ_VALUE_READ_FILES);
> +}
> +
>  /* read access to files which contain one string */
>  
>  enum cpufreq_string {
> diff --git a/tools/power/cpupower/lib/cpufreq.h b/tools/power/cpupower/lib/cpufreq.h
> index 95f4fd9e2656..107668c0c454 100644
> --- a/tools/power/cpupower/lib/cpufreq.h
> +++ b/tools/power/cpupower/lib/cpufreq.h
> @@ -203,6 +203,18 @@ int cpufreq_modify_policy_governor(unsigned int cpu, char *governor);
>  int cpufreq_set_frequency(unsigned int cpu,
>  				unsigned long target_frequency);
>  
> +/*
> + * get the sysfs value from specific table
> + *
> + * Read the value with the sysfs file name from specific table. Does
> + * only work if the cpufreq driver has the specific sysfs interfaces.
> + */
> +
> +unsigned long cpufreq_get_sysfs_value_from_table(unsigned int cpu,
> +						 const char **table,
> +						 unsigned index,
> +						 unsigned size);
> +
>  #ifdef __cplusplus
>  }
>  #endif
>
Juuso Alasuutari Nov. 19, 2021, 10:19 p.m. UTC | #2
On Fri, 19 Nov 2021 14:38:58 -0600, Nathan Fontenot wrote:
> On 11/19/21 4:30 AM, Huang Rui wrote:
> > Expose the helper into cpufreq header, then cpufreq driver can use this
> > function to get the sysfs value if it has any specific sysfs interfaces.
> > 
> > Signed-off-by: Huang Rui <ray.huang@amd.com>
> > ---
> >  tools/power/cpupower/lib/cpufreq.c | 21 +++++++++++++++------
> >  tools/power/cpupower/lib/cpufreq.h | 12 ++++++++++++
> >  2 files changed, 27 insertions(+), 6 deletions(-)
> > 
> > diff --git a/tools/power/cpupower/lib/cpufreq.c b/tools/power/cpupower/lib/cpufreq.c
> > index c3b56db8b921..02719cc400a1 100644
> > --- a/tools/power/cpupower/lib/cpufreq.c
> > +++ b/tools/power/cpupower/lib/cpufreq.c
> > @@ -83,20 +83,21 @@ static const char *cpufreq_value_files[MAX_CPUFREQ_VALUE_READ_FILES] = {
> >  	[STATS_NUM_TRANSITIONS] = "stats/total_trans"
> >  };
> >  
> > -
> > -static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu,
> > -						 enum cpufreq_value which)
> > +unsigned long cpufreq_get_sysfs_value_from_table(unsigned int cpu,
> > +						 const char **table,
> > +						 unsigned index,
> > +						 unsigned size)
> >  {
> >  	unsigned long value;
> >  	unsigned int len;
> >  	char linebuf[MAX_LINE_LEN];
> >  	char *endp;
> >  
> > -	if (which >= MAX_CPUFREQ_VALUE_READ_FILES)
> > +	if (!table && !table[index] && index >= size)
> 
> Should you validate the index before accessing table[index]
> 
>         if (!table && index >= size && !table[index])

The operator is wrong as well. I believe this is what was originally intended:

	if (!table || index >= size || !table[index])

- Juuso
Huang Rui Nov. 22, 2021, 2:41 a.m. UTC | #3
On Sat, Nov 20, 2021 at 06:19:43AM +0800, Juuso Alasuutari wrote:
> On Fri, 19 Nov 2021 14:38:58 -0600, Nathan Fontenot wrote:
> > On 11/19/21 4:30 AM, Huang Rui wrote:
> > > Expose the helper into cpufreq header, then cpufreq driver can use this
> > > function to get the sysfs value if it has any specific sysfs interfaces.
> > > 
> > > Signed-off-by: Huang Rui <ray.huang@amd.com>
> > > ---
> > >  tools/power/cpupower/lib/cpufreq.c | 21 +++++++++++++++------
> > >  tools/power/cpupower/lib/cpufreq.h | 12 ++++++++++++
> > >  2 files changed, 27 insertions(+), 6 deletions(-)
> > > 
> > > diff --git a/tools/power/cpupower/lib/cpufreq.c b/tools/power/cpupower/lib/cpufreq.c
> > > index c3b56db8b921..02719cc400a1 100644
> > > --- a/tools/power/cpupower/lib/cpufreq.c
> > > +++ b/tools/power/cpupower/lib/cpufreq.c
> > > @@ -83,20 +83,21 @@ static const char *cpufreq_value_files[MAX_CPUFREQ_VALUE_READ_FILES] = {
> > >  	[STATS_NUM_TRANSITIONS] = "stats/total_trans"
> > >  };
> > >  
> > > -
> > > -static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu,
> > > -						 enum cpufreq_value which)
> > > +unsigned long cpufreq_get_sysfs_value_from_table(unsigned int cpu,
> > > +						 const char **table,
> > > +						 unsigned index,
> > > +						 unsigned size)
> > >  {
> > >  	unsigned long value;
> > >  	unsigned int len;
> > >  	char linebuf[MAX_LINE_LEN];
> > >  	char *endp;
> > >  
> > > -	if (which >= MAX_CPUFREQ_VALUE_READ_FILES)
> > > +	if (!table && !table[index] && index >= size)
> > 
> > Should you validate the index before accessing table[index]
> > 
> >         if (!table && index >= size && !table[index])
> 
> The operator is wrong as well. I believe this is what was originally intended:
> 
> 	if (!table || index >= size || !table[index])

Yes, nice catch. Thanks. Updated it.

Thanks,
Ray
diff mbox series

Patch

diff --git a/tools/power/cpupower/lib/cpufreq.c b/tools/power/cpupower/lib/cpufreq.c
index c3b56db8b921..02719cc400a1 100644
--- a/tools/power/cpupower/lib/cpufreq.c
+++ b/tools/power/cpupower/lib/cpufreq.c
@@ -83,20 +83,21 @@  static const char *cpufreq_value_files[MAX_CPUFREQ_VALUE_READ_FILES] = {
 	[STATS_NUM_TRANSITIONS] = "stats/total_trans"
 };
 
-
-static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu,
-						 enum cpufreq_value which)
+unsigned long cpufreq_get_sysfs_value_from_table(unsigned int cpu,
+						 const char **table,
+						 unsigned index,
+						 unsigned size)
 {
 	unsigned long value;
 	unsigned int len;
 	char linebuf[MAX_LINE_LEN];
 	char *endp;
 
-	if (which >= MAX_CPUFREQ_VALUE_READ_FILES)
+	if (!table && !table[index] && index >= size)
 		return 0;
 
-	len = sysfs_cpufreq_read_file(cpu, cpufreq_value_files[which],
-				linebuf, sizeof(linebuf));
+	len = sysfs_cpufreq_read_file(cpu, table[index], linebuf,
+				      sizeof(linebuf));
 
 	if (len == 0)
 		return 0;
@@ -109,6 +110,14 @@  static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu,
 	return value;
 }
 
+static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu,
+						 enum cpufreq_value which)
+{
+	return cpufreq_get_sysfs_value_from_table(cpu, cpufreq_value_files,
+						  which,
+						  MAX_CPUFREQ_VALUE_READ_FILES);
+}
+
 /* read access to files which contain one string */
 
 enum cpufreq_string {
diff --git a/tools/power/cpupower/lib/cpufreq.h b/tools/power/cpupower/lib/cpufreq.h
index 95f4fd9e2656..107668c0c454 100644
--- a/tools/power/cpupower/lib/cpufreq.h
+++ b/tools/power/cpupower/lib/cpufreq.h
@@ -203,6 +203,18 @@  int cpufreq_modify_policy_governor(unsigned int cpu, char *governor);
 int cpufreq_set_frequency(unsigned int cpu,
 				unsigned long target_frequency);
 
+/*
+ * get the sysfs value from specific table
+ *
+ * Read the value with the sysfs file name from specific table. Does
+ * only work if the cpufreq driver has the specific sysfs interfaces.
+ */
+
+unsigned long cpufreq_get_sysfs_value_from_table(unsigned int cpu,
+						 const char **table,
+						 unsigned index,
+						 unsigned size);
+
 #ifdef __cplusplus
 }
 #endif