libaio,io_uring: make it possible to cleanup cmdprio malloced data
authorNiklas Cassel <niklas.cassel@wdc.com>
Fri, 12 Nov 2021 09:54:44 +0000 (09:54 +0000)
committerJens Axboe <axboe@kernel.dk>
Fri, 12 Nov 2021 15:49:55 +0000 (08:49 -0700)
commitd6cbeab4b33194688f4e96fe3df1feaacdc08d2b
treeefbf49ca60b52d2a6eb5cc0652b7780ba7395e8f
parent97f2d4840d5dedae0d4baa79c78784bd80d82a5d
libaio,io_uring: make it possible to cleanup cmdprio malloced data

The way that fio currently handles engine options:
options_free() will call free() only for options that have the type
FIO_OPT_STR_STORE. This means that any option that has a pointer in
either td->o or td->eo, which is not of type FIO_OPT_STR_STORE will
leak memory. This is true even for numjobs == 1.

When running with numjobs > 1, fio_options_mem_dupe() will memcpy
td->eo into the new td. Since off1 of the pointers in the first td
has already been set, the pointers in the new td will point to the
same data. (Regardless, options_free() will never try to free the
memory, for neither td.) Neither can we manually free the memory in
cleanup(), since the other td will still point to the same memory,
so this would lead to a double free.

These memory leaks are reported by e.g. valgrind.

The most obvious way to solve this is to put dynamically allocated
memory in {ioring,libaio}_data instead of {ioring,libaio}_options.

This solves the problem since {ioring,libaio}_data is dynamically
allocated by each td during the ioengine init callback, and is freed
when the ioengine cleanup callback for that td is called.

The downside of this is that the parsing has to be done in
fio_cmdprio_init() instead of in the option .cb callback, since the
.cb callback is called before {ioring,libaio}_data is available.

This patch keeps the static cmdprio options in
{ioring,libaio}_options, but moves the dynamically allocated memory
needed by cmdprio to {ioring,libaio}_data.

No cmdprio related memory leaks are reported after this patch.

Signed-off-by: Niklas Cassel <niklas.cassel@wdc.com>
Link: https://lore.kernel.org/r/20211112095428.158300-9-Niklas.Cassel@wdc.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
engines/cmdprio.c
engines/cmdprio.h
engines/io_uring.c
engines/libaio.c