selftests: memcg: uninitialized variable in test_memcg_reclaim()
Subsystems (5)
PATCH 1/1 selftests: memcg: uninitialized variable in test_memcg_reclaim()
The "fd" is used on the clean up path without ever being initialized.
---
I kind of went over kill on fixing this as if it were real code which matters. :P
.../selftests/cgroup/test_memcontrol.c | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-)
tools/testing/selftests/cgroup/test_memcontrol.c +13 -10
@@ -658,18 +658,18 @@ static int test_memcg_reclaim(const char *root) memcg =cg_name (root,"memcg_test" );if (!memcg) - goto cleanup; +return KSFT_FAIL ;if (cg_create (memcg)) - goto cleanup; + goto free_memcg; current =cg_read_long (memcg,"memory.current" );if (current !=0 ) - goto cleanup; + goto destroy_memcg; fd =get_temp_fd ();if (fd <0 ) - goto cleanup; + goto destroy_memcg;cg_run_nowait (memcg, alloc_pagecache_50M_noexit, (void *)(long )fd);
@@ -697,7 +697,7 @@ static int test_memcg_reclaim(const char *root)fprintf (stderr,"failed to allocate %ld for memcg reclaim test\n" , expected_usage); - goto cleanup; + goto close; } }
@@ -717,7 +717,7 @@ static int test_memcg_reclaim(const char *root)* not reclaim the full amount. */ if (to_reclaim <=0 ) - goto cleanup; + goto close;snprintf (buf,sizeof (buf),"%ld" , to_reclaim);
@@ -729,7 +729,7 @@ static int test_memcg_reclaim(const char *root)*/ current =cg_read_long (memcg,"memory.current" );if (!values_close (current,MB (30 ),3 ) && current >MB (30 )) - goto cleanup; + goto close;break ; }
@@ -738,14 +738,17 @@ static int test_memcg_reclaim(const char *root)continue ;/* We got an unexpected error or ran out of retries. */ - goto cleanup; + goto close; } ret =KSFT_PASS ; -cleanup: + +close: +close (fd); +destroy_memcg:cg_destroy (memcg); +free_memcg:free (memcg); -close (fd);return ret; }
Signature · 1 line
2.35.1
Hi Dan!
Quoted 4 lines · Dan Carpenter
The "fd" is used on the clean up path without ever being initialized.
Fixes eae3cb2e87ff ("selftests: cgroup: add a selftest for memory.reclaim")
Thanks for fixing this :)
Quoted 13 lines
Signed-off-by Dan Carpenter <[email protected]>---
I kind of went over kill on fixing this as if it were real code which matters. :P.../selftests/cgroup/test_memcontrol.c | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-)tools/testing/selftests/cgroup/test_memcontrol.c +0
-0
tools/testing/selftests/cgroup/test_memcontrol.c static int test_memcg_reclaim(const char *root) · line 661
Quoted 5 lines
tools/testing/selftests/cgroup/test_memcontrol.c +1
-1@@ -658,18 +658,18 @@ static int test_memcg_reclaim(const char *root) memcg =cg_name (root,"memcg_test" );if (!memcg) - goto cleanup; +return KSFT_FAIL ;
Nit: Just goto free here as well, free ignores NULLs anyway. It's easier to have fewer return paths and more consistent with other tests.
Quoted 55 lines
tools/testing/selftests/cgroup/test_memcontrol.c +8
-8if (cg_create (memcg)) - goto cleanup; + goto free_memcg; current =cg_read_long (memcg,"memory.current" );if (current !=0 ) - goto cleanup; + goto destroy_memcg; fd =get_temp_fd ();if (fd <0 ) - goto cleanup; + goto destroy_memcg;cg_run_nowait (memcg, alloc_pagecache_50M_noexit, (void *)(long )fd);@@ -697,7 +697,7 @@ static int test_memcg_reclaim(const char *root)fprintf (stderr,"failed to allocate %ld for memcg reclaim test\n" , expected_usage); - goto cleanup; + goto close; } }@@ -717,7 +717,7 @@ static int test_memcg_reclaim(const char *root)* not reclaim the full amount. */ if (to_reclaim <=0 ) - goto cleanup; + goto close;snprintf (buf,sizeof (buf),"%ld" , to_reclaim);@@ -729,7 +729,7 @@ static int test_memcg_reclaim(const char *root)*/ current =cg_read_long (memcg,"memory.current" );if (!values_close (current,MB (30 ),3 ) && current >MB (30 )) - goto cleanup; + goto close;break ; }@@ -738,14 +738,17 @@ static int test_memcg_reclaim(const char *root)continue ;/* We got an unexpected error or ran out of retries. */ - goto cleanup; + goto close; } ret =KSFT_PASS ; -cleanup: +
tools/testing/selftests/cgroup/test_memcontrol.c static int test_memcg_reclaim(const char *root) · line 754
Quoted 10 lines
tools/testing/selftests/cgroup/test_memcontrol.c +4
-1+close: +close (fd); +destroy_memcg:cg_destroy (memcg); +free_memcg:free (memcg); -close (fd);return ret; }
Nit: keep the cleanup_* naming for labels to make it obvious and to be consistent with the rest of the file (e.g. cleanup_free, cleanup_memcg, cleanup_file/cleanup_all). See test_memcg_subtree_control().
I would honestly have one label to cleanup the memcg. Calling cg_destroy() on a non-existent memcg should be fine. rmdir() will just fail silently. All other tests do this and it's easier to read when we have fewer return paths. My advice would be cleanup_file and cleanup_memcg labels.
Thanks!
With these nits:
Quoted 3 lines
--
2.35.1
Quoted 11 lines · Yosry Ahmed
Nit: keep the cleanup_* naming for labels to make it obvious and to be consistent with the rest of the file (e.g.
cleanup_free,cleanup_memcg, cleanup_file/cleanup_all). Seetest_memcg_subtree_control().I would honestly have one label to cleanup the memcg. Calling
cg_destroy()on a non-existent memcg should be fine.rmdir()will just fail silently. All other tests do this and it's easier to read when we have fewer return paths. My advice would becleanup_fileandcleanup_memcglabels.
One error label handling is very bug prone. You always end up freeing things which have not been initialized/allocated. Or dereferencing pointers which are NULL. Or, since most kernel functions clean up after themselves, you end up double freeing things.
regards,
dan carpenter
Quoted 14 lines · Dan Carpenter
Quoted 11 lines · Yosry Ahmed
Nit: keep the cleanup_* naming for labels to make it obvious and to be consistent with the rest of the file (e.g.
cleanup_free,cleanup_memcg, cleanup_file/cleanup_all). Seetest_memcg_subtree_control().I would honestly have one label to cleanup the memcg. Calling
cg_destroy()on a non-existent memcg should be fine.rmdir()will just fail silently. All other tests do this and it's easier to read when we have fewer return paths. My advice would becleanup_fileandcleanup_memcglabels.
Quoted 4 lines
One error label handling is very bug prone. You always end up freeing things which have not been initialized/allocated. Or dereferencing pointers which are NULL. Or, since most kernel functions clean up after themselves, you end up double freeing things.
I am not suggesting a single cleanup label, I said "one label to cleanup the memcg", which is separate from cleaning up the file. Basically just merging the destroy_memcg and free_memcg labels to be consistent with other tests. I don't feel strongly about this anyway :)
Quoted 3 lines
regards,
dan carpenter