All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jay Kamat <jgkamat@fb.com>
To: Shuah Khan <shuah@kernel.org>
Cc: <jgkamat@fb.com>, <linux-kselftest@vger.kernel.org>,
	Roman Gushchin <guro@fb.com>, Tejun Heo <tj@kernel.org>,
	<kernel-team@fb.com>, <linux-kernel@vger.kernel.org>,
	<jaygkamat@gmail.com>
Subject: Re: [PATCH 1/2] Fix cg_read_strcmp()
Date: Fri, 7 Sep 2018 11:28:59 -0700	[thread overview]
Message-ID: <86worxmabx.fsf@fb.com> (raw)
In-Reply-To: <bed84882-b194-7e45-0db6-08b02bc672d5@kernel.org>


Shuah Khan writes:

> On 09/07/2018 10:49 AM, jgkamat@fb.com wrote:
>> From: Jay Kamat <jgkamat@fb.com>
>>
>> Fix a couple issues with cg_read_strcmp(), to improve correctness of
>> cgroup tests
>> - Fix cg_read_strcmp() always returning 0 for empty "needle" strings
>> - Fix a memory leak in cg_read_strcmp()
>>
>> Fixes: 84092dbcf901 ("selftests: cgroup: add memory controller self-tests")
>>
>> Signed-off-by: Jay Kamat <jgkamat@fb.com>
>> ---
>>  tools/testing/selftests/cgroup/cgroup_util.c | 17 ++++++++++++++---
>>  1 file changed, 14 insertions(+), 3 deletions(-)
>>
>> diff --git a/tools/testing/selftests/cgroup/cgroup_util.c b/tools/testing/selftests/cgroup/cgroup_util.c
>> index 1e9e3c470561..8b644ea39725 100644
>> --- a/tools/testing/selftests/cgroup/cgroup_util.c
>> +++ b/tools/testing/selftests/cgroup/cgroup_util.c
>> @@ -89,17 +89,28 @@ int cg_read(const char *cgroup, const char *control, char *buf, size_t len)
>>  int cg_read_strcmp(const char *cgroup, const char *control,
>>  		   const char *expected)
>>  {
>> -	size_t size = strlen(expected) + 1;
>> +	size_t size;
>>  	char *buf;
>> +	int ret;
>> +
>> +	/* Handle the case of comparing against empty string */
>> +	if (!expected)
>> +		size = 32;
>
> This doesn't look right. I would think expected shouldn't be null?
> It gets used below.
>
>> +	else
>> +		size = strlen(expected) + 1;
>>
>>  	buf = malloc(size);
>>  	if (!buf)
>>  		return -1;
>>
>> -	if (cg_read(cgroup, control, buf, size))
>> +	if (cg_read(cgroup, control, buf, size)) {
>> +		free(buf);
>>  		return -1;
>> +	}
>>
>> -	return strcmp(expected, buf);
>> +	ret = strcmp(expected, buf);
>
> If expected is null, what's the point in running the test?
> Is  empty "needle" string a valid test scenario?

There are a couple places where an empty "needle" string is used currently:

- cg_test_proc_killed (newly added in the next patch): Verify cgroup.procs is
  empty (there are no processes running)
- test_memcg_oom_events: Verify cgroup.procs is empty

Previously, when passing in an empty needle string, this function would always
return 0, as the size allocated (1) would not be enough to read any data in
'cg_read', and strcmp would compare two null strings.

>
>> +	free(buf);
>> +	return ret;
>>  }
>>
>>  int cg_read_strstr(const char *cgroup, const char *control, const char *needle)
>>
>
> thanks,
> -- Shuah

I could definitely remove the unneeded strcmp in the null 'expected' case, but
I am worried it would feel a bit too hacky or add too much duplication.

Would something like this be the best solution? If you had something else in
mind (or if I'm misunderstanding something), please let me know, and I'll
update the patchset!

	size_t size;
	char *buf;
	int ret;

	/* Handle the case of comparing against empty string */
	if (!expected)
		size = 32;
	else
		size = strlen(expected) + 1;

	buf = malloc(size);
	if (!buf)
		return -1;

	if (cg_read(cgroup, control, buf, size)) {
		free(buf);
		return -1;
	}

	if (!expected)
		ret = !buf;
	else
		ret = strcmp(expected, buf);
	free(buf);
	return ret;

Thanks,
-Jay

WARNING: multiple messages have this Message-ID (diff)
From: jgkamat at fb.com (Jay Kamat)
Subject: [PATCH 1/2] Fix cg_read_strcmp()
Date: Fri, 7 Sep 2018 11:28:59 -0700	[thread overview]
Message-ID: <86worxmabx.fsf@fb.com> (raw)
In-Reply-To: <bed84882-b194-7e45-0db6-08b02bc672d5@kernel.org>


Shuah Khan writes:

> On 09/07/2018 10:49 AM, jgkamat at fb.com wrote:
>> From: Jay Kamat <jgkamat at fb.com>
>>
>> Fix a couple issues with cg_read_strcmp(), to improve correctness of
>> cgroup tests
>> - Fix cg_read_strcmp() always returning 0 for empty "needle" strings
>> - Fix a memory leak in cg_read_strcmp()
>>
>> Fixes: 84092dbcf901 ("selftests: cgroup: add memory controller self-tests")
>>
>> Signed-off-by: Jay Kamat <jgkamat at fb.com>
>> ---
>>  tools/testing/selftests/cgroup/cgroup_util.c | 17 ++++++++++++++---
>>  1 file changed, 14 insertions(+), 3 deletions(-)
>>
>> diff --git a/tools/testing/selftests/cgroup/cgroup_util.c b/tools/testing/selftests/cgroup/cgroup_util.c
>> index 1e9e3c470561..8b644ea39725 100644
>> --- a/tools/testing/selftests/cgroup/cgroup_util.c
>> +++ b/tools/testing/selftests/cgroup/cgroup_util.c
>> @@ -89,17 +89,28 @@ int cg_read(const char *cgroup, const char *control, char *buf, size_t len)
>>  int cg_read_strcmp(const char *cgroup, const char *control,
>>  		   const char *expected)
>>  {
>> -	size_t size = strlen(expected) + 1;
>> +	size_t size;
>>  	char *buf;
>> +	int ret;
>> +
>> +	/* Handle the case of comparing against empty string */
>> +	if (!expected)
>> +		size = 32;
>
> This doesn't look right. I would think expected shouldn't be null?
> It gets used below.
>
>> +	else
>> +		size = strlen(expected) + 1;
>>
>>  	buf = malloc(size);
>>  	if (!buf)
>>  		return -1;
>>
>> -	if (cg_read(cgroup, control, buf, size))
>> +	if (cg_read(cgroup, control, buf, size)) {
>> +		free(buf);
>>  		return -1;
>> +	}
>>
>> -	return strcmp(expected, buf);
>> +	ret = strcmp(expected, buf);
>
> If expected is null, what's the point in running the test?
> Is  empty "needle" string a valid test scenario?

There are a couple places where an empty "needle" string is used currently:

- cg_test_proc_killed (newly added in the next patch): Verify cgroup.procs is
  empty (there are no processes running)
- test_memcg_oom_events: Verify cgroup.procs is empty

Previously, when passing in an empty needle string, this function would always
return 0, as the size allocated (1) would not be enough to read any data in
'cg_read', and strcmp would compare two null strings.

>
>> +	free(buf);
>> +	return ret;
>>  }
>>
>>  int cg_read_strstr(const char *cgroup, const char *control, const char *needle)
>>
>
> thanks,
> -- Shuah

I could definitely remove the unneeded strcmp in the null 'expected' case, but
I am worried it would feel a bit too hacky or add too much duplication.

Would something like this be the best solution? If you had something else in
mind (or if I'm misunderstanding something), please let me know, and I'll
update the patchset!

	size_t size;
	char *buf;
	int ret;

	/* Handle the case of comparing against empty string */
	if (!expected)
		size = 32;
	else
		size = strlen(expected) + 1;

	buf = malloc(size);
	if (!buf)
		return -1;

	if (cg_read(cgroup, control, buf, size)) {
		free(buf);
		return -1;
	}

	if (!expected)
		ret = !buf;
	else
		ret = strcmp(expected, buf);
	free(buf);
	return ret;

Thanks,
-Jay

WARNING: multiple messages have this Message-ID (diff)
From: jgkamat@fb.com (Jay Kamat)
Subject: [PATCH 1/2] Fix cg_read_strcmp()
Date: Fri, 7 Sep 2018 11:28:59 -0700	[thread overview]
Message-ID: <86worxmabx.fsf@fb.com> (raw)
Message-ID: <20180907182859.ht5K3JQ5hDN1b-FDjZC1BM6AI62-NLEG1Dp8AEphx78@z> (raw)
In-Reply-To: <bed84882-b194-7e45-0db6-08b02bc672d5@kernel.org>


Shuah Khan writes:

> On 09/07/2018 10:49 AM, jgkamat@fb.com wrote:
>> From: Jay Kamat <jgkamat at fb.com>
>>
>> Fix a couple issues with cg_read_strcmp(), to improve correctness of
>> cgroup tests
>> - Fix cg_read_strcmp() always returning 0 for empty "needle" strings
>> - Fix a memory leak in cg_read_strcmp()
>>
>> Fixes: 84092dbcf901 ("selftests: cgroup: add memory controller self-tests")
>>
>> Signed-off-by: Jay Kamat <jgkamat at fb.com>
>> ---
>>  tools/testing/selftests/cgroup/cgroup_util.c | 17 ++++++++++++++---
>>  1 file changed, 14 insertions(+), 3 deletions(-)
>>
>> diff --git a/tools/testing/selftests/cgroup/cgroup_util.c b/tools/testing/selftests/cgroup/cgroup_util.c
>> index 1e9e3c470561..8b644ea39725 100644
>> --- a/tools/testing/selftests/cgroup/cgroup_util.c
>> +++ b/tools/testing/selftests/cgroup/cgroup_util.c
>> @@ -89,17 +89,28 @@ int cg_read(const char *cgroup, const char *control, char *buf, size_t len)
>>  int cg_read_strcmp(const char *cgroup, const char *control,
>>  		   const char *expected)
>>  {
>> -	size_t size = strlen(expected) + 1;
>> +	size_t size;
>>  	char *buf;
>> +	int ret;
>> +
>> +	/* Handle the case of comparing against empty string */
>> +	if (!expected)
>> +		size = 32;
>
> This doesn't look right. I would think expected shouldn't be null?
> It gets used below.
>
>> +	else
>> +		size = strlen(expected) + 1;
>>
>>  	buf = malloc(size);
>>  	if (!buf)
>>  		return -1;
>>
>> -	if (cg_read(cgroup, control, buf, size))
>> +	if (cg_read(cgroup, control, buf, size)) {
>> +		free(buf);
>>  		return -1;
>> +	}
>>
>> -	return strcmp(expected, buf);
>> +	ret = strcmp(expected, buf);
>
> If expected is null, what's the point in running the test?
> Is  empty "needle" string a valid test scenario?

There are a couple places where an empty "needle" string is used currently:

- cg_test_proc_killed (newly added in the next patch): Verify cgroup.procs is
  empty (there are no processes running)
- test_memcg_oom_events: Verify cgroup.procs is empty

Previously, when passing in an empty needle string, this function would always
return 0, as the size allocated (1) would not be enough to read any data in
'cg_read', and strcmp would compare two null strings.

>
>> +	free(buf);
>> +	return ret;
>>  }
>>
>>  int cg_read_strstr(const char *cgroup, const char *control, const char *needle)
>>
>
> thanks,
> -- Shuah

I could definitely remove the unneeded strcmp in the null 'expected' case, but
I am worried it would feel a bit too hacky or add too much duplication.

Would something like this be the best solution? If you had something else in
mind (or if I'm misunderstanding something), please let me know, and I'll
update the patchset!

	size_t size;
	char *buf;
	int ret;

	/* Handle the case of comparing against empty string */
	if (!expected)
		size = 32;
	else
		size = strlen(expected) + 1;

	buf = malloc(size);
	if (!buf)
		return -1;

	if (cg_read(cgroup, control, buf, size)) {
		free(buf);
		return -1;
	}

	if (!expected)
		ret = !buf;
	else
		ret = strcmp(expected, buf);
	free(buf);
	return ret;

Thanks,
-Jay

  reply	other threads:[~2018-09-07 18:29 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-05  1:08 kselftests for memory.oom.group jgkamat
2018-09-05  1:08 ` jgkamat
2018-09-05  1:08 ` jgkamat
2018-09-05  1:08 ` [PATCH 1/2] Fix cg_read_strcmp() jgkamat
2018-09-05  1:08   ` jgkamat
2018-09-05  1:08   ` jgkamat
2018-09-05 14:43   ` Shuah Khan
2018-09-05 14:43     ` Shuah Khan
2018-09-05 14:43     ` shuah
2018-09-05  1:08 ` [PATCH 2/2] Add tests for memory.oom.group jgkamat
2018-09-05  1:08   ` jgkamat
2018-09-05  1:08   ` jgkamat
2018-09-05 15:21   ` Shuah Khan
2018-09-05 15:21     ` Shuah Khan
2018-09-05 15:21     ` shuah
2018-09-07 16:49 ` kselftests " jgkamat
2018-09-07 16:49   ` jgkamat
2018-09-07 16:49   ` jgkamat
2018-09-07 16:49   ` [PATCH 1/2] Fix cg_read_strcmp() jgkamat
2018-09-07 16:49     ` jgkamat
2018-09-07 16:49     ` jgkamat
2018-09-07 16:56     ` Roman Gushchin
2018-09-07 16:56       ` Roman Gushchin
2018-09-07 16:56       ` guro
2018-09-07 17:06     ` Shuah Khan
2018-09-07 17:06       ` Shuah Khan
2018-09-07 17:06       ` shuah
2018-09-07 18:28       ` Jay Kamat [this message]
2018-09-07 18:28         ` Jay Kamat
2018-09-07 18:28         ` jgkamat
2018-09-07 18:53         ` Shuah Khan
2018-09-07 18:53           ` Shuah Khan
2018-09-07 18:53           ` shuah
2018-09-07 16:49   ` [PATCH 2/2] Add tests for memory.oom.group jgkamat
2018-09-07 16:49     ` jgkamat
2018-09-07 16:49     ` jgkamat
2018-09-07 16:57     ` Roman Gushchin
2018-09-07 16:57       ` Roman Gushchin
2018-09-07 16:57       ` guro
2018-09-07 21:34   ` kselftests " jgkamat
2018-09-07 21:34     ` jgkamat
2018-09-07 21:34     ` jgkamat
2018-09-07 21:34     ` [PATCH v3 1/2] Fix cg_read_strcmp() jgkamat
2018-09-07 21:34       ` jgkamat
2018-09-07 21:34       ` jgkamat
2018-09-07 21:34     ` [PATCH v3 2/2] Add tests for memory.oom.group jgkamat
2018-09-07 21:34       ` jgkamat
2018-09-07 21:34       ` jgkamat
2018-09-07 22:53     ` kselftests " Shuah Khan
2018-09-07 22:53       ` Shuah Khan
2018-09-07 22:53       ` shuah

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86worxmabx.fsf@fb.com \
    --to=jgkamat@fb.com \
    --cc=guro@fb.com \
    --cc=jaygkamat@gmail.com \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.