mbox series

[RESEND,v3,0/2] selftests/resctrl: Bug fix and optimization

Message ID cover.1693575451.git.maciej.wieczor-retman@intel.com
Headers show
Series selftests/resctrl: Bug fix and optimization | expand

Message

Maciej Wieczor-Retman Sept. 1, 2023, 1:41 p.m. UTC
Write_schemata() uses fprintf() to write a bitmask into a schemata file
inside resctrl FS. It checks fprintf() return value but it doesn't check
fclose() return value. Error codes from fprintf() such as write errors,
are buffered and flushed back to the user only after fclose() is executed
which means any invalid bitmask can be written into the schemata file.

Rewrite write_schemata() to use syscalls instead of stdio file
operations to avoid the buffering.

The resctrlfs.c file defines functions that interact with the resctrl FS
while resctrl_val.c file defines functions that perform measurements on
the cache. Run_benchmark() fits logically into the second file before
resctrl_val() function that uses it.

Move run_benchmark() from resctrlfs.c to resctrl_val.c and remove
redundant part of the kernel-doc comment. Make run_benchmark() static
and remove it from the header file.

Series is based on kselftest next branch.

Changelog v3:
- Use snprintf() return value instead of strlen() in write_schemata().
  (Ilpo)
- Make run_benchmark() static and remove it from the header file.
  (Reinette)
- Added Ilpo's reviewed-by tag to Patch 2/2.
- Patch messages and cover letter rewording.

Changelog v2:
- Change sprintf() to snprintf() in write_schemata().
- Redo write_schemata() with syscalls instead of stdio functions.
- Fix typos and missing dots in patch messages.
- Branch printf attribute patch to a separate series.

[v1] https://lore.kernel.org/all/cover.1692880423.git.maciej.wieczor-retman@intel.com/
[v2] https://lore.kernel.org/all/cover.1693213468.git.maciej.wieczor-retman@intel.com/

Wieczor-Retman Maciej (2):
  selftests/resctrl: Fix schemata write error check
  selftests/resctrl: Move run_benchmark() to a more fitting file

 tools/testing/selftests/resctrl/resctrl.h     |  1 -
 tools/testing/selftests/resctrl/resctrl_val.c | 50 ++++++++++++
 tools/testing/selftests/resctrl/resctrlfs.c   | 78 ++++---------------
 3 files changed, 64 insertions(+), 65 deletions(-)


base-commit: 9b1db732866bee060b9bca9493e5ebf5e8874c48

Comments

Reinette Chatre Sept. 12, 2023, 4 p.m. UTC | #1
Hi Maciej,

On 9/11/2023 11:32 PM, Maciej Wieczór-Retman wrote:
> On 2023-09-11 at 09:59:06 -0700, Reinette Chatre wrote:
>> Hi Maciej,
>> When I build the tests with this applied I encounter the following:
>>
>> resctrlfs.c: In function ‘write_schemata’:
>> resctrlfs.c:475:14: warning: implicit declaration of function ‘open’; did you mean ‘popen’? [-Wimplicit-function-declaration]
>>  475 |         fd = open(controlgroup, O_WRONLY);
>>      |              ^~~~
>>      |              popen
>> resctrlfs.c:475:33: error: ‘O_WRONLY’ undeclared (first use in this function)
>>  475 |         fd = open(controlgroup, O_WRONLY);
>>      |                                 ^~~~~~~~
>> resctrlfs.c:475:33: note: each undeclared identifier is reported only once for each function it appears in
> 
> Hmm, that's odd. How do you build the tests?

I applied this series on top of kselftest repo's "next" branch.

I use a separate build directory and first ran "make headers". After that,
$ make O=<build dir> -C tools/testing/selftests/resctrl

> I use "make -C tools/testing/selftests/resctrl" while in the root kernel
> source directory. I tried to get the same error you experienced by
> compiling some dummy test program with "open" and "O_WRONLY". From the
> experiment I found that the "resctrl.h" header provides the declarations
> that are causing your errors.
Maciej Wieczor-Retman Sept. 13, 2023, 5:59 a.m. UTC | #2
On 2023-09-12 at 09:00:28 -0700, Reinette Chatre wrote:
>Hi Maciej,
>
>On 9/11/2023 11:32 PM, Maciej Wieczór-Retman wrote:
>> On 2023-09-11 at 09:59:06 -0700, Reinette Chatre wrote:
>>> Hi Maciej,
>>> When I build the tests with this applied I encounter the following:
>>>
>>> resctrlfs.c: In function ‘write_schemata’:
>>> resctrlfs.c:475:14: warning: implicit declaration of function ‘open’; did you mean ‘popen’? [-Wimplicit-function-declaration]
>>>  475 |         fd = open(controlgroup, O_WRONLY);
>>>      |              ^~~~
>>>      |              popen
>>> resctrlfs.c:475:33: error: ‘O_WRONLY’ undeclared (first use in this function)
>>>  475 |         fd = open(controlgroup, O_WRONLY);
>>>      |                                 ^~~~~~~~
>>> resctrlfs.c:475:33: note: each undeclared identifier is reported only once for each function it appears in
>> 
>> Hmm, that's odd. How do you build the tests?
>
>I applied this series on top of kselftest repo's "next" branch.
>
>I use a separate build directory and first ran "make headers". After that,
>$ make O=<build dir> -C tools/testing/selftests/resctrl

I do the same, just without the build directory, but that shouldn't
matter here I guess.

>> I use "make -C tools/testing/selftests/resctrl" while in the root kernel
>> source directory. I tried to get the same error you experienced by
>> compiling some dummy test program with "open" and "O_WRONLY". From the
>> experiment I found that the "resctrl.h" header provides the declarations
>> that are causing your errors.
>
>From what I can tell resctrl.h does not include fcntl.h that provides
>what is needed.

I found out you can run "gcc -M <file>" and it will recursively tell you
what headers are including other headers.

Using this I found that "resctrl.h" includes <sys/mount.h> which in turn
includes <fcntl.h> out of /usr/include/sys directory. Is that also the
case on your system?
Reinette Chatre Sept. 13, 2023, 6:49 p.m. UTC | #3
Hi Maciej,

On 9/12/2023 10:59 PM, Maciej Wieczór-Retman wrote:
> On 2023-09-12 at 09:00:28 -0700, Reinette Chatre wrote:
>> Hi Maciej,
>>
>> On 9/11/2023 11:32 PM, Maciej Wieczór-Retman wrote:
>>> On 2023-09-11 at 09:59:06 -0700, Reinette Chatre wrote:
>>>> Hi Maciej,
>>>> When I build the tests with this applied I encounter the following:
>>>>
>>>> resctrlfs.c: In function ‘write_schemata’:
>>>> resctrlfs.c:475:14: warning: implicit declaration of function ‘open’; did you mean ‘popen’? [-Wimplicit-function-declaration]
>>>>  475 |         fd = open(controlgroup, O_WRONLY);
>>>>      |              ^~~~
>>>>      |              popen
>>>> resctrlfs.c:475:33: error: ‘O_WRONLY’ undeclared (first use in this function)
>>>>  475 |         fd = open(controlgroup, O_WRONLY);
>>>>      |                                 ^~~~~~~~
>>>> resctrlfs.c:475:33: note: each undeclared identifier is reported only once for each function it appears in
>>>
>>> Hmm, that's odd. How do you build the tests?
>>
>> I applied this series on top of kselftest repo's "next" branch.
>>
>> I use a separate build directory and first ran "make headers". After that,
>> $ make O=<build dir> -C tools/testing/selftests/resctrl
> 
> I do the same, just without the build directory, but that shouldn't
> matter here I guess.
> 
>>> I use "make -C tools/testing/selftests/resctrl" while in the root kernel
>>> source directory. I tried to get the same error you experienced by
>>> compiling some dummy test program with "open" and "O_WRONLY". From the
>>> experiment I found that the "resctrl.h" header provides the declarations
>>> that are causing your errors.
>>
>>From what I can tell resctrl.h does not include fcntl.h that provides
>> what is needed.
> 
> I found out you can run "gcc -M <file>" and it will recursively tell you
> what headers are including other headers.
> 
> Using this I found that "resctrl.h" includes <sys/mount.h> which in turn
> includes <fcntl.h> out of /usr/include/sys directory. Is that also the
> case on your system?
> 

No. The test system I used is running glibc 2.35 and it seems that including
fcntl.h was added to sys/mount.h in 2.36. See glibc commit
78a408ee7ba0 ("linux: Add open_tree")

Generally we should avoid indirect inclusions and here I think certainly so
since it cannot be guaranteed that fcntl.h would be available via 
sys/mount.h.

Reinette
Maciej Wieczor-Retman Sept. 14, 2023, 6:01 a.m. UTC | #4
Hi,

On 2023-09-13 at 11:49:19 -0700, Reinette Chatre wrote:
>Hi Maciej,
>
>On 9/12/2023 10:59 PM, Maciej Wieczór-Retman wrote:
>> On 2023-09-12 at 09:00:28 -0700, Reinette Chatre wrote:
>>> Hi Maciej,
>>>
>>> On 9/11/2023 11:32 PM, Maciej Wieczór-Retman wrote:
>>>> On 2023-09-11 at 09:59:06 -0700, Reinette Chatre wrote:
>>>>> Hi Maciej,
>>>>> When I build the tests with this applied I encounter the following:
>>>>>
>>>>> resctrlfs.c: In function ‘write_schemata’:
>>>>> resctrlfs.c:475:14: warning: implicit declaration of function ‘open’; did you mean ‘popen’? [-Wimplicit-function-declaration]
>>>>>  475 |         fd = open(controlgroup, O_WRONLY);
>>>>>      |              ^~~~
>>>>>      |              popen
>>>>> resctrlfs.c:475:33: error: ‘O_WRONLY’ undeclared (first use in this function)
>>>>>  475 |         fd = open(controlgroup, O_WRONLY);
>>>>>      |                                 ^~~~~~~~
>>>>> resctrlfs.c:475:33: note: each undeclared identifier is reported only once for each function it appears in
>>>>
>>>> Hmm, that's odd. How do you build the tests?
>>>
>>> I applied this series on top of kselftest repo's "next" branch.
>>>
>>> I use a separate build directory and first ran "make headers". After that,
>>> $ make O=<build dir> -C tools/testing/selftests/resctrl
>> 
>> I do the same, just without the build directory, but that shouldn't
>> matter here I guess.
>> 
>>>> I use "make -C tools/testing/selftests/resctrl" while in the root kernel
>>>> source directory. I tried to get the same error you experienced by
>>>> compiling some dummy test program with "open" and "O_WRONLY". From the
>>>> experiment I found that the "resctrl.h" header provides the declarations
>>>> that are causing your errors.
>>>
>>>From what I can tell resctrl.h does not include fcntl.h that provides
>>> what is needed.
>> 
>> I found out you can run "gcc -M <file>" and it will recursively tell you
>> what headers are including other headers.
>> 
>> Using this I found that "resctrl.h" includes <sys/mount.h> which in turn
>> includes <fcntl.h> out of /usr/include/sys directory. Is that also the
>> case on your system?
>> 
>
>No. The test system I used is running glibc 2.35 and it seems that including
>fcntl.h was added to sys/mount.h in 2.36. See glibc commit
>78a408ee7ba0 ("linux: Add open_tree")
>
>Generally we should avoid indirect inclusions and here I think certainly so
>since it cannot be guaranteed that fcntl.h would be available via 
>sys/mount.h.

Okay, would including the fcntl.h header to resctrl.h be okay in this
case? Or is there some other more sophisticated way of doing that (some
include guard or checking glibc version for example)?
Reinette Chatre Sept. 14, 2023, 3:14 p.m. UTC | #5
Hi Maciej,

On 9/13/2023 11:01 PM, Maciej Wieczór-Retman wrote:
> On 2023-09-13 at 11:49:19 -0700, Reinette Chatre wrote:
>> On 9/12/2023 10:59 PM, Maciej Wieczór-Retman wrote:
>>> On 2023-09-12 at 09:00:28 -0700, Reinette Chatre wrote:
>>>> On 9/11/2023 11:32 PM, Maciej Wieczór-Retman wrote:
>>>>> On 2023-09-11 at 09:59:06 -0700, Reinette Chatre wrote:
>>>>>> Hi Maciej,
>>>>>> When I build the tests with this applied I encounter the following:
>>>>>>
>>>>>> resctrlfs.c: In function ‘write_schemata’:
>>>>>> resctrlfs.c:475:14: warning: implicit declaration of function ‘open’; did you mean ‘popen’? [-Wimplicit-function-declaration]
>>>>>>  475 |         fd = open(controlgroup, O_WRONLY);
>>>>>>      |              ^~~~
>>>>>>      |              popen
>>>>>> resctrlfs.c:475:33: error: ‘O_WRONLY’ undeclared (first use in this function)
>>>>>>  475 |         fd = open(controlgroup, O_WRONLY);
>>>>>>      |                                 ^~~~~~~~
>>>>>> resctrlfs.c:475:33: note: each undeclared identifier is reported only once for each function it appears in
>>>>>
>>>>> Hmm, that's odd. How do you build the tests?
>>>>
>>>> I applied this series on top of kselftest repo's "next" branch.
>>>>
>>>> I use a separate build directory and first ran "make headers". After that,
>>>> $ make O=<build dir> -C tools/testing/selftests/resctrl
>>>
>>> I do the same, just without the build directory, but that shouldn't
>>> matter here I guess.
>>>
>>>>> I use "make -C tools/testing/selftests/resctrl" while in the root kernel
>>>>> source directory. I tried to get the same error you experienced by
>>>>> compiling some dummy test program with "open" and "O_WRONLY". From the
>>>>> experiment I found that the "resctrl.h" header provides the declarations
>>>>> that are causing your errors.
>>>>
>>> >From what I can tell resctrl.h does not include fcntl.h that provides
>>>> what is needed.
>>>
>>> I found out you can run "gcc -M <file>" and it will recursively tell you
>>> what headers are including other headers.
>>>
>>> Using this I found that "resctrl.h" includes <sys/mount.h> which in turn
>>> includes <fcntl.h> out of /usr/include/sys directory. Is that also the
>>> case on your system?
>>>
>>
>> No. The test system I used is running glibc 2.35 and it seems that including
>> fcntl.h was added to sys/mount.h in 2.36. See glibc commit
>> 78a408ee7ba0 ("linux: Add open_tree")
>>
>> Generally we should avoid indirect inclusions and here I think certainly so
>> since it cannot be guaranteed that fcntl.h would be available via 
>> sys/mount.h.
> 
> Okay, would including the fcntl.h header to resctrl.h be okay in this
> case? Or is there some other more sophisticated way of doing that (some
> include guard or checking glibc version for example)?

Ideally fcntl.h would be included in the file it is used. Doing so you may
encounter the same problems as Ilpo in [1]. If that is the case and that fix works
for you then you may want to have this series depend on Ilpo's work.

Reinette

[1] https://lore.kernel.org/lkml/dfc53e-3f92-82e4-6af-d1a28e8c199a@linux.intel.com/
Maciej Wieczor-Retman Sept. 15, 2023, 8:16 a.m. UTC | #6
On 2023-09-14 at 08:14:25 -0700, Reinette Chatre wrote:
>Hi Maciej,
>
>On 9/13/2023 11:01 PM, Maciej Wieczór-Retman wrote:
>> On 2023-09-13 at 11:49:19 -0700, Reinette Chatre wrote:
>>> On 9/12/2023 10:59 PM, Maciej Wieczór-Retman wrote:
>>>> On 2023-09-12 at 09:00:28 -0700, Reinette Chatre wrote:
>>>>> On 9/11/2023 11:32 PM, Maciej Wieczór-Retman wrote:
>>>>>> On 2023-09-11 at 09:59:06 -0700, Reinette Chatre wrote:
>>>>>>> Hi Maciej,
>>>>>>> When I build the tests with this applied I encounter the following:
>>>>>>>
>>>>>>> resctrlfs.c: In function ‘write_schemata’:
>>>>>>> resctrlfs.c:475:14: warning: implicit declaration of function ‘open’; did you mean ‘popen’? [-Wimplicit-function-declaration]
>>>>>>>  475 |         fd = open(controlgroup, O_WRONLY);
>>>>>>>      |              ^~~~
>>>>>>>      |              popen
>>>>>>> resctrlfs.c:475:33: error: ‘O_WRONLY’ undeclared (first use in this function)
>>>>>>>  475 |         fd = open(controlgroup, O_WRONLY);
>>>>>>>      |                                 ^~~~~~~~
>>>>>>> resctrlfs.c:475:33: note: each undeclared identifier is reported only once for each function it appears in
>>>>>>
>>>>>> Hmm, that's odd. How do you build the tests?
>>>>>
>>>>> I applied this series on top of kselftest repo's "next" branch.
>>>>>
>>>>> I use a separate build directory and first ran "make headers". After that,
>>>>> $ make O=<build dir> -C tools/testing/selftests/resctrl
>>>>
>>>> I do the same, just without the build directory, but that shouldn't
>>>> matter here I guess.
>>>>
>>>>>> I use "make -C tools/testing/selftests/resctrl" while in the root kernel
>>>>>> source directory. I tried to get the same error you experienced by
>>>>>> compiling some dummy test program with "open" and "O_WRONLY". From the
>>>>>> experiment I found that the "resctrl.h" header provides the declarations
>>>>>> that are causing your errors.
>>>>>
>>>> >From what I can tell resctrl.h does not include fcntl.h that provides
>>>>> what is needed.
>>>>
>>>> I found out you can run "gcc -M <file>" and it will recursively tell you
>>>> what headers are including other headers.
>>>>
>>>> Using this I found that "resctrl.h" includes <sys/mount.h> which in turn
>>>> includes <fcntl.h> out of /usr/include/sys directory. Is that also the
>>>> case on your system?
>>>>
>>>
>>> No. The test system I used is running glibc 2.35 and it seems that including
>>> fcntl.h was added to sys/mount.h in 2.36. See glibc commit
>>> 78a408ee7ba0 ("linux: Add open_tree")
>>>
>>> Generally we should avoid indirect inclusions and here I think certainly so
>>> since it cannot be guaranteed that fcntl.h would be available via 
>>> sys/mount.h.
>> 
>> Okay, would including the fcntl.h header to resctrl.h be okay in this
>> case? Or is there some other more sophisticated way of doing that (some
>> include guard or checking glibc version for example)?
>
>Ideally fcntl.h would be included in the file it is used. Doing so you may
>encounter the same problems as Ilpo in [1]. If that is the case and that fix works
>for you then you may want to have this series depend on Ilpo's work.

Thanks a lot for finding this, and yes, I get the same errors by adding the
header. I'll send the next version of this series with the added header
rebased on top of Ilpo's series you mentioned.

>Reinette
>
>[1] https://lore.kernel.org/lkml/dfc53e-3f92-82e4-6af-d1a28e8c199a@linux.intel.com/
>