linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] selftests/cpufreq: Fix cpufreq basic read and update testcases
@ 2025-04-30 17:14 Swapnil Sapkal
  2025-05-19  7:58 ` Viresh Kumar
  0 siblings, 1 reply; 7+ messages in thread
From: Swapnil Sapkal @ 2025-04-30 17:14 UTC (permalink / raw)
  To: rafael, viresh.kumar, shuah
  Cc: gautham.shenoy, narasimhan.v, linux-pm, linux-kselftest,
	linux-kernel, Swapnil Sapkal

In cpufreq basic selftests, one of the testcases is to read all cpufreq
sysfs files and print the values. This testcase assumes all the cpufreq
sysfs files have read permissions. However certain cpufreq sysfs files
(eg. stats/reset) are write only files and this testcase errors out
when it is not able to read the file.
Similarily, there is one more testcase which reads the cpufreq sysfs
file data and write it back to same file. This testcase also errors out
for sysfs files without read permission.
Fix these testcases by adding proper read permission checks.

Reported-by: Narasimhan V <narasimhan.v@amd.com>
Signed-off-by: Swapnil Sapkal <swapnil.sapkal@amd.com>
---
 tools/testing/selftests/cpufreq/cpufreq.sh | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/cpufreq/cpufreq.sh b/tools/testing/selftests/cpufreq/cpufreq.sh
index e350c521b467..3484fa34e8d8 100755
--- a/tools/testing/selftests/cpufreq/cpufreq.sh
+++ b/tools/testing/selftests/cpufreq/cpufreq.sh
@@ -52,7 +52,14 @@ read_cpufreq_files_in_dir()
 	for file in $files; do
 		if [ -f $1/$file ]; then
 			printf "$file:"
-			cat $1/$file
+			#file is readable ?
+			local rfile=$(ls -l $1/$file | awk '$1 ~ /^.*r.*/ { print $NF; }')
+
+			if [ ! -z $rfile ]; then
+				cat $1/$file
+			else
+				printf "$file is not readable\n"
+			fi
 		else
 			printf "\n"
 			read_cpufreq_files_in_dir "$1/$file"
@@ -83,10 +90,10 @@ update_cpufreq_files_in_dir()
 
 	for file in $files; do
 		if [ -f $1/$file ]; then
-			# file is writable ?
-			local wfile=$(ls -l $1/$file | awk '$1 ~ /^.*w.*/ { print $NF; }')
+			# file is readable and writable ?
+			local rwfile=$(ls -l $1/$file | awk '$1 ~ /^.*rw.*/ { print $NF; }')
 
-			if [ ! -z $wfile ]; then
+			if [ ! -z $rwfile ]; then
 				# scaling_setspeed is a special file and we
 				# should skip updating it
 				if [ $file != "scaling_setspeed" ]; then
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] selftests/cpufreq: Fix cpufreq basic read and update testcases
  2025-04-30 17:14 [PATCH] selftests/cpufreq: Fix cpufreq basic read and update testcases Swapnil Sapkal
@ 2025-05-19  7:58 ` Viresh Kumar
  2025-05-22  8:37   ` Sapkal, Swapnil
  2025-05-22 15:17   ` Shuah Khan
  0 siblings, 2 replies; 7+ messages in thread
From: Viresh Kumar @ 2025-05-19  7:58 UTC (permalink / raw)
  To: Swapnil Sapkal
  Cc: rafael, shuah, gautham.shenoy, narasimhan.v, linux-pm,
	linux-kselftest, linux-kernel

On 30-04-25, 17:14, Swapnil Sapkal wrote:
> In cpufreq basic selftests, one of the testcases is to read all cpufreq
> sysfs files and print the values. This testcase assumes all the cpufreq
> sysfs files have read permissions. However certain cpufreq sysfs files
> (eg. stats/reset) are write only files and this testcase errors out
> when it is not able to read the file.
> Similarily, there is one more testcase which reads the cpufreq sysfs
> file data and write it back to same file. This testcase also errors out
> for sysfs files without read permission.
> Fix these testcases by adding proper read permission checks.
> 
> Reported-by: Narasimhan V <narasimhan.v@amd.com>
> Signed-off-by: Swapnil Sapkal <swapnil.sapkal@amd.com>
> ---
>  tools/testing/selftests/cpufreq/cpufreq.sh | 15 +++++++++++----
>  1 file changed, 11 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/testing/selftests/cpufreq/cpufreq.sh b/tools/testing/selftests/cpufreq/cpufreq.sh
> index e350c521b467..3484fa34e8d8 100755
> --- a/tools/testing/selftests/cpufreq/cpufreq.sh
> +++ b/tools/testing/selftests/cpufreq/cpufreq.sh
> @@ -52,7 +52,14 @@ read_cpufreq_files_in_dir()
>  	for file in $files; do
>  		if [ -f $1/$file ]; then
>  			printf "$file:"
> -			cat $1/$file
> +			#file is readable ?
> +			local rfile=$(ls -l $1/$file | awk '$1 ~ /^.*r.*/ { print $NF; }')
> +
> +			if [ ! -z $rfile ]; then
> +				cat $1/$file
> +			else
> +				printf "$file is not readable\n"
> +			fi

What about:

if [ -r $1/$file ]; then
    cat $1/$file
else
    printf "$file is not readable\n"
fi


-- 
viresh

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] selftests/cpufreq: Fix cpufreq basic read and update testcases
  2025-05-19  7:58 ` Viresh Kumar
@ 2025-05-22  8:37   ` Sapkal, Swapnil
  2025-05-22  9:45     ` Viresh Kumar
  2025-05-22 15:17   ` Shuah Khan
  1 sibling, 1 reply; 7+ messages in thread
From: Sapkal, Swapnil @ 2025-05-22  8:37 UTC (permalink / raw)
  To: Viresh Kumar
  Cc: rafael, shuah, gautham.shenoy, narasimhan.v, linux-pm,
	linux-kselftest, linux-kernel

Hi Viresh,

On 5/19/2025 1:28 PM, Viresh Kumar wrote:
> On 30-04-25, 17:14, Swapnil Sapkal wrote:
>> In cpufreq basic selftests, one of the testcases is to read all cpufreq
>> sysfs files and print the values. This testcase assumes all the cpufreq
>> sysfs files have read permissions. However certain cpufreq sysfs files
>> (eg. stats/reset) are write only files and this testcase errors out
>> when it is not able to read the file.
>> Similarily, there is one more testcase which reads the cpufreq sysfs
>> file data and write it back to same file. This testcase also errors out
>> for sysfs files without read permission.
>> Fix these testcases by adding proper read permission checks.
>>
>> Reported-by: Narasimhan V <narasimhan.v@amd.com>
>> Signed-off-by: Swapnil Sapkal <swapnil.sapkal@amd.com>
>> ---
>>   tools/testing/selftests/cpufreq/cpufreq.sh | 15 +++++++++++----
>>   1 file changed, 11 insertions(+), 4 deletions(-)
>>
>> diff --git a/tools/testing/selftests/cpufreq/cpufreq.sh b/tools/testing/selftests/cpufreq/cpufreq.sh
>> index e350c521b467..3484fa34e8d8 100755
>> --- a/tools/testing/selftests/cpufreq/cpufreq.sh
>> +++ b/tools/testing/selftests/cpufreq/cpufreq.sh
>> @@ -52,7 +52,14 @@ read_cpufreq_files_in_dir()
>>   	for file in $files; do
>>   		if [ -f $1/$file ]; then
>>   			printf "$file:"
>> -			cat $1/$file
>> +			#file is readable ?
>> +			local rfile=$(ls -l $1/$file | awk '$1 ~ /^.*r.*/ { print $NF; }')
>> +
>> +			if [ ! -z $rfile ]; then
>> +				cat $1/$file
>> +			else
>> +				printf "$file is not readable\n"
>> +			fi
> 
> What about:
> 
> if [ -r $1/$file ]; then
>      cat $1/$file
> else
>      printf "$file is not readable\n"
> fi
> 
> 

Initially I tried the same, but it does not work properly with the root user.

--
Thanks and Regards,
Swapnil

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] selftests/cpufreq: Fix cpufreq basic read and update testcases
  2025-05-22  8:37   ` Sapkal, Swapnil
@ 2025-05-22  9:45     ` Viresh Kumar
  2025-05-26  8:52       ` Sapkal, Swapnil
  0 siblings, 1 reply; 7+ messages in thread
From: Viresh Kumar @ 2025-05-22  9:45 UTC (permalink / raw)
  To: Sapkal, Swapnil
  Cc: rafael, shuah, gautham.shenoy, narasimhan.v, linux-pm,
	linux-kselftest, linux-kernel

On 22-05-25, 14:07, Sapkal, Swapnil wrote:
> Initially I tried the same, but it does not work properly with the root user.

Hmm,

Tried chatgpt now and it says this should work:

if ! cat "$1/$file" 2>/dev/null; then
    printf "$file is not readable\n"
fi

- This attempts to read the file.
- If it fails, the cat command returns non-zero, and you print a message.
- 2>/dev/null suppresses error messages (Permission denied, etc.)
- This works reliably for both root and non-root users, because it actually tests the read action, not just permission bits.

-- 
viresh

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] selftests/cpufreq: Fix cpufreq basic read and update testcases
  2025-05-19  7:58 ` Viresh Kumar
  2025-05-22  8:37   ` Sapkal, Swapnil
@ 2025-05-22 15:17   ` Shuah Khan
  2025-05-26  8:46     ` Sapkal, Swapnil
  1 sibling, 1 reply; 7+ messages in thread
From: Shuah Khan @ 2025-05-22 15:17 UTC (permalink / raw)
  To: Viresh Kumar, Swapnil Sapkal
  Cc: rafael, shuah, gautham.shenoy, narasimhan.v, linux-pm,
	linux-kselftest, linux-kernel, Shuah Khan

On 5/19/25 01:58, Viresh Kumar wrote:
> On 30-04-25, 17:14, Swapnil Sapkal wrote:
>> In cpufreq basic selftests, one of the testcases is to read all cpufreq
>> sysfs files and print the values. This testcase assumes all the cpufreq
>> sysfs files have read permissions. However certain cpufreq sysfs files
>> (eg. stats/reset) are write only files and this testcase errors out
>> when it is not able to read the file.
>> Similarily, there is one more testcase which reads the cpufreq sysfs
>> file data and write it back to same file. This testcase also errors out
>> for sysfs files without read permission.
>> Fix these testcases by adding proper read permission checks.

Can you share how you ran the test?

>>
>> Reported-by: Narasimhan V <narasimhan.v@amd.com>
>> Signed-off-by: Swapnil Sapkal <swapnil.sapkal@amd.com>
>> ---
>>   tools/testing/selftests/cpufreq/cpufreq.sh | 15 +++++++++++----
>>   1 file changed, 11 insertions(+), 4 deletions(-)
>>
>> diff --git a/tools/testing/selftests/cpufreq/cpufreq.sh b/tools/testing/selftests/cpufreq/cpufreq.sh
>> index e350c521b467..3484fa34e8d8 100755
>> --- a/tools/testing/selftests/cpufreq/cpufreq.sh
>> +++ b/tools/testing/selftests/cpufreq/cpufreq.sh
>> @@ -52,7 +52,14 @@ read_cpufreq_files_in_dir()
>>   	for file in $files; do
>>   		if [ -f $1/$file ]; then
>>   			printf "$file:"
>> -			cat $1/$file
>> +			#file is readable ?
>> +			local rfile=$(ls -l $1/$file | awk '$1 ~ /^.*r.*/ { print $NF; }')
>> +
>> +			if [ ! -z $rfile ]; then
>> +				cat $1/$file
>> +			else
>> +				printf "$file is not readable\n"
>> +			fi
> 
> What about:
> 
> if [ -r $1/$file ]; then
>      cat $1/$file
> else
>      printf "$file is not readable\n"
> fi
> 
> 

thanks,
-- Shuah

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] selftests/cpufreq: Fix cpufreq basic read and update testcases
  2025-05-22 15:17   ` Shuah Khan
@ 2025-05-26  8:46     ` Sapkal, Swapnil
  0 siblings, 0 replies; 7+ messages in thread
From: Sapkal, Swapnil @ 2025-05-26  8:46 UTC (permalink / raw)
  To: Shuah Khan, Viresh Kumar
  Cc: rafael, shuah, gautham.shenoy, narasimhan.v, linux-pm,
	linux-kselftest, linux-kernel

Hi Shuah,

On 5/22/2025 8:47 PM, Shuah Khan wrote:
> On 5/19/25 01:58, Viresh Kumar wrote:
>> On 30-04-25, 17:14, Swapnil Sapkal wrote:
>>> In cpufreq basic selftests, one of the testcases is to read all cpufreq
>>> sysfs files and print the values. This testcase assumes all the cpufreq
>>> sysfs files have read permissions. However certain cpufreq sysfs files
>>> (eg. stats/reset) are write only files and this testcase errors out
>>> when it is not able to read the file.
>>> Similarily, there is one more testcase which reads the cpufreq sysfs
>>> file data and write it back to same file. This testcase also errors out
>>> for sysfs files without read permission.
>>> Fix these testcases by adding proper read permission checks.
> 
> Can you share how you ran the test?
> 

I ran the basic tests with the following command:

./main.sh -t basic

>>>
>>> Reported-by: Narasimhan V <narasimhan.v@amd.com>
>>> Signed-off-by: Swapnil Sapkal <swapnil.sapkal@amd.com>
>>> ---
>>>   tools/testing/selftests/cpufreq/cpufreq.sh | 15 +++++++++++----
>>>   1 file changed, 11 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/tools/testing/selftests/cpufreq/cpufreq.sh b/tools/testing/selftests/cpufreq/cpufreq.sh
>>> index e350c521b467..3484fa34e8d8 100755
>>> --- a/tools/testing/selftests/cpufreq/cpufreq.sh
>>> +++ b/tools/testing/selftests/cpufreq/cpufreq.sh
>>> @@ -52,7 +52,14 @@ read_cpufreq_files_in_dir()
>>>       for file in $files; do
>>>           if [ -f $1/$file ]; then
>>>               printf "$file:"
>>> -            cat $1/$file
>>> +            #file is readable ?
>>> +            local rfile=$(ls -l $1/$file | awk '$1 ~ /^.*r.*/ { print $NF; }')
>>> +
>>> +            if [ ! -z $rfile ]; then
>>> +                cat $1/$file
>>> +            else
>>> +                printf "$file is not readable\n"
>>> +            fi
>>
>> What about:
>>
>> if [ -r $1/$file ]; then
>>      cat $1/$file
>> else
>>      printf "$file is not readable\n"
>> fi
>>
>>
> 
> thanks,
> -- Shuah
--
Thanks and Regards,
Swapnil

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] selftests/cpufreq: Fix cpufreq basic read and update testcases
  2025-05-22  9:45     ` Viresh Kumar
@ 2025-05-26  8:52       ` Sapkal, Swapnil
  0 siblings, 0 replies; 7+ messages in thread
From: Sapkal, Swapnil @ 2025-05-26  8:52 UTC (permalink / raw)
  To: Viresh Kumar
  Cc: rafael, shuah, gautham.shenoy, narasimhan.v, linux-pm,
	linux-kselftest, linux-kernel

Hi Viresh,

On 5/22/2025 3:15 PM, Viresh Kumar wrote:
> On 22-05-25, 14:07, Sapkal, Swapnil wrote:
>> Initially I tried the same, but it does not work properly with the root user.
> 
> Hmm,
> 
> Tried chatgpt now and it says this should work:
> 
> if ! cat "$1/$file" 2>/dev/null; then
>      printf "$file is not readable\n"
> fi
> 
> - This attempts to read the file.
> - If it fails, the cat command returns non-zero, and you print a message.
> - 2>/dev/null suppresses error messages (Permission denied, etc.)
> - This works reliably for both root and non-root users, because it actually tests the read action, not just permission bits.
> 

This looks clean. I will send v2 with this change.

--
Thanks and Regards,
Swapnil

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-05-26  8:52 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-30 17:14 [PATCH] selftests/cpufreq: Fix cpufreq basic read and update testcases Swapnil Sapkal
2025-05-19  7:58 ` Viresh Kumar
2025-05-22  8:37   ` Sapkal, Swapnil
2025-05-22  9:45     ` Viresh Kumar
2025-05-26  8:52       ` Sapkal, Swapnil
2025-05-22 15:17   ` Shuah Khan
2025-05-26  8:46     ` Sapkal, Swapnil

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).