Re: [PATCH V5 4/4] x86/resctrl: Use appropriate API for strings terminated by newline

From: Andy Shevchenko
Date: Tue May 19 2020 - 04:19:55 EST


On Tue, May 19, 2020 at 2:50 AM Reinette Chatre
<reinette.chatre@xxxxxxxxx> wrote:
>
> The user input to files in the resctrl filesystem are expected to be
> terminated with a newline. Testing the user input includes a test for
> the presence of a newline and then replacing the newline with NUL
> byte followed by comparison using strcmp().
>
> sysfs_streq() exists to test if strings are equal, treating both NUL and
> newline-then-NUL as equivalent string terminations. Even more,
> sysfs_match_string() exists to match a given string in an array using
> sysfs_streq().
>
> Replace existing strcmp() comparisons of strings that are terminated
> with a newline with more appropriate sysfs_streq() via the
> sysfs_match_string() API that can perform the match across the different
> mode strings that are already maintained in an array.

...

> Changes since V4:
> - Remove "mode" local variable from rdtgroup_mode_write(). This variable
> was previously used to create shorter lines with the original strcmp()
> code that was removed in patch 4/4.
> - Andy pointed out that the repeated assignment to rdtgrp->mode could be
> replaced by a single assignment after all the checks. This was initially
> rejected because it would let the "RDT_MODE_PSEUDO_LOCKED" assignment
> slip through. Even so, Andy's feedback revealed that the new changes
> unintentionally let a user's attempt at setting the mode to pseudo-locked
> be silently ignored where it previously reported an error. Restore original
> user space behavior by returning success when user attempts to change any
> mode when it is already the current mode (including pseudo-locked) and
> returning failure when user attempts to set the mode to pseudo-locked.
> After this change it is possible to follow Andy's original suggestion
> of using a single assignment. (Andy)

Glad we can do it!

...

> + ret = sysfs_match_string(rdt_mode_str, buf);
> + if (ret < 0) {
> + rdt_last_cmd_puts("Unknown or unsupported mode\n");

> + ret = -EINVAL;

This is redundant.

> + goto out;
> + }

--
With Best Regards,
Andy Shevchenko