diff mbox series

[v4] readahead02.c: Fix check input fsize

Message ID 20230116111749.18865-1-wegao@suse.com
State Superseded
Headers show
Series [v4] readahead02.c: Fix check input fsize | expand

Commit Message

Wei Gao Jan. 16, 2023, 11:17 a.m. UTC
We run the test with a loop device it will fail with ENOSPC if we
pass -s bigger than the loop device, we should at least check if
the device is large enough in the test setup.The test should make
use of use tst_parse_filesize() so that we can pass sizes with
units e.g. -s 128M.

Signed-off-by: Wei Gao <wegao@suse.com>
Suggested-by: Cyril Hrubis <chrubis@suse.cz>
---
 include/tst_device.h                          |  4 +++-
 lib/tst_device.c                              |  2 +-
 .../kernel/syscalls/readahead/readahead02.c   | 21 +++++++++++++++----
 3 files changed, 21 insertions(+), 6 deletions(-)

Comments

Cyril Hrubis Jan. 16, 2023, 1:42 p.m. UTC | #1
Hi!
> diff --git a/include/tst_device.h b/include/tst_device.h
> index 977427f1c..f03f17f7d 100644
> --- a/include/tst_device.h
> +++ b/include/tst_device.h
> @@ -6,6 +6,8 @@
>  #ifndef TST_DEVICE_H__
>  #define TST_DEVICE_H__
>  
> +#define DEV_SIZE_MB 300u
> +
>  #include <unistd.h>
>  #include <stdint.h>
>  #include <sys/stat.h>
> @@ -49,7 +51,7 @@ int tst_clear_device(const char *dev);
>   * free loopdev). If path is non-NULL, it will be filled with free loopdev path.
>   *
>   */
> -int tst_find_free_loopdev(const char *path, size_t path_len);
> +int tst_find_free_loopdev(char *path, size_t path_len);
>  
>  /*
>   * Attaches a file to a loop device.
> diff --git a/lib/tst_device.c b/lib/tst_device.c
> index 48d7e3ab6..b098fc80b 100644
> --- a/lib/tst_device.c
> +++ b/lib/tst_device.c
> @@ -38,6 +38,7 @@
>  #include "lapi/syscalls.h"
>  #include "test.h"
>  #include "safe_macros.h"
> +#include "tst_device.h"
>  
>  #ifndef LOOP_CTL_GET_FREE
>  # define LOOP_CTL_GET_FREE 0x4C82
> @@ -46,7 +47,6 @@
>  #define LOOP_CONTROL_FILE "/dev/loop-control"
>  
>  #define DEV_FILE "test_dev.img"
> -#define DEV_SIZE_MB 300u
>  #define UUID_STR_SZ 37
>  #define UUID_FMT "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"

These changes are useless now.

> diff --git a/testcases/kernel/syscalls/readahead/readahead02.c b/testcases/kernel/syscalls/readahead/readahead02.c
> index 7acf4bb18..5989c7cbf 100644
> --- a/testcases/kernel/syscalls/readahead/readahead02.c
> +++ b/testcases/kernel/syscalls/readahead/readahead02.c
> @@ -33,6 +33,7 @@
>  #include "tst_test.h"
>  #include "tst_timer.h"
>  #include "lapi/syscalls.h"
> +#include "tst_device.h"
>  
>  static char testfile[PATH_MAX] = "testfile";
>  #define DROP_CACHES_FNAME "/proc/sys/vm/drop_caches"
> @@ -366,11 +367,24 @@ static void setup_readahead_length(void)
>  
>  static void setup(void)
>  {
> -	if (opt_fsizestr) {
> -		testfile_size = SAFE_STRTOL(opt_fsizestr, 1, INT_MAX);
> -		tst_set_max_runtime(1 + testfile_size / (DEFAULT_FILESIZE/32));
> +	/*
> +	 * Acutaly dev size will reduced after create filesystem,
> +	 * so use dev_szie * 0.8 as dev real usage size, test case will
> +	 * create two files within dev so we need div 2 get max file size
> +	 */
> +	size_t fsize_max = tst_device->size * 0.8 / 2 * 1024 * 1024;

I'm not entirely sure about the * 0.8 here. I suppose that we need some
space for metadata and some manipulation space but 20% is probably way
to much. What motivation is behind the exact number here?

> +	/* At least two pagesize for test case */
> +	pagesize = getpagesize();
> +	size_t fsize_min = pagesize * 2;
> +
> +	if (tst_parse_filesize(opt_fsizestr, (long long *)&testfile_size, fsize_min, fsize_max)) {
                                              ^
					      This is wrong, as long as
					      size of size_t and long
					      long is different it will
					      either corrupt memory or
					      crash.

The testfile_size has to be defined long long there is no way around it.

> +		tst_set_max_runtime(1 + DEFAULT_FILESIZE / (DEFAULT_FILESIZE/32));

Why do we even bother with setting runtime if we are doing
tst_brk(TBROK, ...) on the next line?

> +		tst_brk(TBROK, "invalid initial filesize '%s'", opt_fsizestr);
                                             ^
					I do not understand what
					'initial' means in this context.
>  	}
>  
> +	tst_set_max_runtime(1 + testfile_size / (DEFAULT_FILESIZE/32));
> +
>  	if (access(PROC_IO_FNAME, F_OK))
>  		tst_brk(TCONF, "Requires " PROC_IO_FNAME);
>  
> @@ -380,7 +394,6 @@ static void setup(void)
>  	/* check if readahead is supported */
>  	tst_syscall(__NR_readahead, 0, 0, 0);
>  
> -	pagesize = getpagesize();
>  
>  	setup_readahead_length();
>  	tst_res(TINFO, "readahead length: %d", readahead_length);
> -- 
> 2.35.3
>
Wei Gao Jan. 18, 2023, 3:36 a.m. UTC | #2
On Mon, Jan 16, 2023 at 02:42:49PM +0100, Cyril Hrubis wrote:
> Hi!
> > diff --git a/include/tst_device.h b/include/tst_device.h
> > index 977427f1c..f03f17f7d 100644
> > --- a/include/tst_device.h
> > +++ b/include/tst_device.h
> > @@ -6,6 +6,8 @@
> >  #ifndef TST_DEVICE_H__
> >  #define TST_DEVICE_H__
> >  
> > +#define DEV_SIZE_MB 300u
> > +
> >  #include <unistd.h>
> >  #include <stdint.h>
> >  #include <sys/stat.h>
> > @@ -49,7 +51,7 @@ int tst_clear_device(const char *dev);
> >   * free loopdev). If path is non-NULL, it will be filled with free loopdev path.
> >   *
> >   */
> > -int tst_find_free_loopdev(const char *path, size_t path_len);
> > +int tst_find_free_loopdev(char *path, size_t path_len);
> >  
> >  /*
> >   * Attaches a file to a loop device.
> > diff --git a/lib/tst_device.c b/lib/tst_device.c
> > index 48d7e3ab6..b098fc80b 100644
> > --- a/lib/tst_device.c
> > +++ b/lib/tst_device.c
> > @@ -38,6 +38,7 @@
> >  #include "lapi/syscalls.h"
> >  #include "test.h"
> >  #include "safe_macros.h"
> > +#include "tst_device.h"
> >  
> >  #ifndef LOOP_CTL_GET_FREE
> >  # define LOOP_CTL_GET_FREE 0x4C82
> > @@ -46,7 +47,6 @@
> >  #define LOOP_CONTROL_FILE "/dev/loop-control"
> >  
> >  #define DEV_FILE "test_dev.img"
> > -#define DEV_SIZE_MB 300u
> >  #define UUID_STR_SZ 37
> >  #define UUID_FMT "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"
> 
> These changes are useless now.
ack
> 
> > diff --git a/testcases/kernel/syscalls/readahead/readahead02.c b/testcases/kernel/syscalls/readahead/readahead02.c
> > index 7acf4bb18..5989c7cbf 100644
> > --- a/testcases/kernel/syscalls/readahead/readahead02.c
> > +++ b/testcases/kernel/syscalls/readahead/readahead02.c
> > @@ -33,6 +33,7 @@
> >  #include "tst_test.h"
> >  #include "tst_timer.h"
> >  #include "lapi/syscalls.h"
> > +#include "tst_device.h"
> >  
> >  static char testfile[PATH_MAX] = "testfile";
> >  #define DROP_CACHES_FNAME "/proc/sys/vm/drop_caches"
> > @@ -366,11 +367,24 @@ static void setup_readahead_length(void)
> >  
> >  static void setup(void)
> >  {
> > -	if (opt_fsizestr) {
> > -		testfile_size = SAFE_STRTOL(opt_fsizestr, 1, INT_MAX);
> > -		tst_set_max_runtime(1 + testfile_size / (DEFAULT_FILESIZE/32));
> > +	/*
> > +	 * Acutaly dev size will reduced after create filesystem,
> > +	 * so use dev_szie * 0.8 as dev real usage size, test case will
> > +	 * create two files within dev so we need div 2 get max file size
> > +	 */
> > +	size_t fsize_max = tst_device->size * 0.8 / 2 * 1024 * 1024;
> 
> I'm not entirely sure about the * 0.8 here. I suppose that we need some
> space for metadata and some manipulation space but 20% is probably way
> to much. What motivation is behind the exact number here?
> 

Acutaly dev size will reduced after create filesystem, for example
system set default dev size is 300M but acutal dev size is 280M

"df -h" example resulst in case dev size = 300M:
/dev/loop1      280M  272M     0 100% /tmp/LTP_reaNP2ElW/mntpoint

Around 7% space loss so we use dev_szie * 0.9 as dev real usage size
Test case will create two files within dev so we need div 2 to get
max file size

> > +	/* At least two pagesize for test case */
> > +	pagesize = getpagesize();
> > +	size_t fsize_min = pagesize * 2;
> > +
> > +	if (tst_parse_filesize(opt_fsizestr, (long long *)&testfile_size, fsize_min, fsize_max)) {
>                                               ^
> 					      This is wrong, as long as
> 					      size of size_t and long
> 					      long is different it will
> 					      either corrupt memory or
> 					      crash.
> 
> The testfile_size has to be defined long long there is no way around it.
ack
> 
> > +		tst_set_max_runtime(1 + DEFAULT_FILESIZE / (DEFAULT_FILESIZE/32));
> 
> Why do we even bother with setting runtime if we are doing
> tst_brk(TBROK, ...) on the next line?
ack
> 
> > +		tst_brk(TBROK, "invalid initial filesize '%s'", opt_fsizestr);
>                                              ^
> 					I do not understand what
> 					'initial' means in this context.
> >  	}
> >  
> > +	tst_set_max_runtime(1 + testfile_size / (DEFAULT_FILESIZE/32));
> > +
> >  	if (access(PROC_IO_FNAME, F_OK))
> >  		tst_brk(TCONF, "Requires " PROC_IO_FNAME);
> >  
> > @@ -380,7 +394,6 @@ static void setup(void)
> >  	/* check if readahead is supported */
> >  	tst_syscall(__NR_readahead, 0, 0, 0);
> >  
> > -	pagesize = getpagesize();
> >  
> >  	setup_readahead_length();
> >  	tst_res(TINFO, "readahead length: %d", readahead_length);
> > -- 
> > 2.35.3
> > 
> 
> -- 
> Cyril Hrubis
> chrubis@suse.cz
diff mbox series

Patch

diff --git a/include/tst_device.h b/include/tst_device.h
index 977427f1c..f03f17f7d 100644
--- a/include/tst_device.h
+++ b/include/tst_device.h
@@ -6,6 +6,8 @@ 
 #ifndef TST_DEVICE_H__
 #define TST_DEVICE_H__
 
+#define DEV_SIZE_MB 300u
+
 #include <unistd.h>
 #include <stdint.h>
 #include <sys/stat.h>
@@ -49,7 +51,7 @@  int tst_clear_device(const char *dev);
  * free loopdev). If path is non-NULL, it will be filled with free loopdev path.
  *
  */
-int tst_find_free_loopdev(const char *path, size_t path_len);
+int tst_find_free_loopdev(char *path, size_t path_len);
 
 /*
  * Attaches a file to a loop device.
diff --git a/lib/tst_device.c b/lib/tst_device.c
index 48d7e3ab6..b098fc80b 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -38,6 +38,7 @@ 
 #include "lapi/syscalls.h"
 #include "test.h"
 #include "safe_macros.h"
+#include "tst_device.h"
 
 #ifndef LOOP_CTL_GET_FREE
 # define LOOP_CTL_GET_FREE 0x4C82
@@ -46,7 +47,6 @@ 
 #define LOOP_CONTROL_FILE "/dev/loop-control"
 
 #define DEV_FILE "test_dev.img"
-#define DEV_SIZE_MB 300u
 #define UUID_STR_SZ 37
 #define UUID_FMT "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"
 
diff --git a/testcases/kernel/syscalls/readahead/readahead02.c b/testcases/kernel/syscalls/readahead/readahead02.c
index 7acf4bb18..5989c7cbf 100644
--- a/testcases/kernel/syscalls/readahead/readahead02.c
+++ b/testcases/kernel/syscalls/readahead/readahead02.c
@@ -33,6 +33,7 @@ 
 #include "tst_test.h"
 #include "tst_timer.h"
 #include "lapi/syscalls.h"
+#include "tst_device.h"
 
 static char testfile[PATH_MAX] = "testfile";
 #define DROP_CACHES_FNAME "/proc/sys/vm/drop_caches"
@@ -366,11 +367,24 @@  static void setup_readahead_length(void)
 
 static void setup(void)
 {
-	if (opt_fsizestr) {
-		testfile_size = SAFE_STRTOL(opt_fsizestr, 1, INT_MAX);
-		tst_set_max_runtime(1 + testfile_size / (DEFAULT_FILESIZE/32));
+	/*
+	 * Acutaly dev size will reduced after create filesystem,
+	 * so use dev_szie * 0.8 as dev real usage size, test case will
+	 * create two files within dev so we need div 2 get max file size
+	 */
+	size_t fsize_max = tst_device->size * 0.8 / 2 * 1024 * 1024;
+
+	/* At least two pagesize for test case */
+	pagesize = getpagesize();
+	size_t fsize_min = pagesize * 2;
+
+	if (tst_parse_filesize(opt_fsizestr, (long long *)&testfile_size, fsize_min, fsize_max)) {
+		tst_set_max_runtime(1 + DEFAULT_FILESIZE / (DEFAULT_FILESIZE/32));
+		tst_brk(TBROK, "invalid initial filesize '%s'", opt_fsizestr);
 	}
 
+	tst_set_max_runtime(1 + testfile_size / (DEFAULT_FILESIZE/32));
+
 	if (access(PROC_IO_FNAME, F_OK))
 		tst_brk(TCONF, "Requires " PROC_IO_FNAME);
 
@@ -380,7 +394,6 @@  static void setup(void)
 	/* check if readahead is supported */
 	tst_syscall(__NR_readahead, 0, 0, 0);
 
-	pagesize = getpagesize();
 
 	setup_readahead_length();
 	tst_res(TINFO, "readahead length: %d", readahead_length);