Skip to content

Commit e777cb4

Browse files
committed
aio-stress: fix memory leak
setup_shared_mem() currently runs as part of run() function, and if it runs for too many iterations it can consume so much memory that OOM kills it. Move setup_shared_mem() along with the 2 checks for io_iter and num_threads to setup(). This way it runs only once and gets freed on test exit. As consequence setup_ious() also needs to be modified to no longer modify global variable 'aligned_buffer', because we don't re-initialize it on every iteration. And finally, at the end of run() function free also memory allocated by setup_ious(). Signed-off-by: Jan Stancek <jstancek@redhat.com> Reviewed-by: Li Wang <liwang@redhat.com> Acked-by: Petr Vorel <pvorel@suse.cz> Acked-by: Andrea Cervesato <andrea.cervesato@suse.com>
1 parent a92aedf commit e777cb4

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

testcases/kernel/io/ltp-aiodio/aio-stress.c

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -914,14 +914,15 @@ static void setup_ious(struct thread_info *t, int num_files, int depth, int recl
914914
{
915915
int i;
916916
size_t bytes = num_files * depth * sizeof(*t->ios);
917+
char *buffer = aligned_buffer;
917918

918919
t->ios = SAFE_MALLOC(bytes);
919920

920921
memset(t->ios, 0, bytes);
921922

922923
for (i = 0; i < depth * num_files; i++) {
923-
t->ios[i].buf = aligned_buffer;
924-
aligned_buffer += padded_reclen;
924+
t->ios[i].buf = buffer;
925+
buffer += padded_reclen;
925926
t->ios[i].buf_size = reclen;
926927
if (verify)
927928
memset(t->ios[i].buf, 'b', reclen);
@@ -932,7 +933,7 @@ static void setup_ious(struct thread_info *t, int num_files, int depth, int recl
932933
}
933934

934935
if (verify) {
935-
verify_buf = aligned_buffer;
936+
verify_buf = buffer;
936937
memset(verify_buf, 'b', reclen);
937938
}
938939

@@ -1228,19 +1229,6 @@ static void setup(void)
12281229
tst_brk(TBROK, "Invalid shm option '%s'", str_use_shm);
12291230
}
12301231
}
1231-
}
1232-
1233-
static void run(void)
1234-
{
1235-
char files[num_files][265];
1236-
int first_stage = WRITE;
1237-
struct io_oper *oper;
1238-
int status = 0;
1239-
int open_fds = 0;
1240-
struct thread_info *t;
1241-
int rwfd;
1242-
int i;
1243-
int j;
12441232

12451233
/*
12461234
* make sure we don't try to submit more I/O than we have allocated
@@ -1256,6 +1244,22 @@ static void run(void)
12561244
tst_res(TINFO, "Dropping thread count to the number of contexts %d", num_threads);
12571245
}
12581246

1247+
if (setup_shared_mem(num_threads, num_files * num_contexts, depth, rec_len))
1248+
tst_brk(TBROK, "error in setup_shared_mem");
1249+
}
1250+
1251+
static void run(void)
1252+
{
1253+
char files[num_files][265];
1254+
int first_stage = WRITE;
1255+
struct io_oper *oper;
1256+
int status = 0;
1257+
int open_fds = 0;
1258+
struct thread_info *t;
1259+
int rwfd;
1260+
int i;
1261+
int j;
1262+
12591263
t = SAFE_MALLOC(num_threads * sizeof(*t));
12601264
memset(t, 0, num_threads * sizeof(*t));
12611265
global_thread_info = t;
@@ -1322,8 +1326,6 @@ static void run(void)
13221326
}
13231327
}
13241328

1325-
if (setup_shared_mem(num_threads, num_files * num_contexts, depth, rec_len))
1326-
tst_brk(TBROK, "error in setup_shared_mem");
13271329

13281330
for (i = 0; i < num_threads; i++)
13291331
setup_ious(&t[i], t[i].num_files, depth, rec_len, max_io_submit);
@@ -1339,6 +1341,13 @@ static void run(void)
13391341
for (i = 0; i < num_files; i++)
13401342
SAFE_UNLINK(files[i]);
13411343

1344+
for (i = 0; i < num_threads; i++) {
1345+
free(t[i].ios);
1346+
free(t[i].iocbs);
1347+
free(t[i].events);
1348+
}
1349+
free(t);
1350+
13421351
if (status)
13431352
tst_res(TFAIL, "Test did not pass");
13441353
else

0 commit comments

Comments
 (0)