improve tcmu-runner behavior under valgrind
improve tcmu-runner behavior under valgrind

Running tcmu-runner under valgrind was complaining of "uninitialized
memory" reading the ring buffer between the kernel and tcmu-runner.
Valgrind validates the ring buffer memory as "initialized" at mmap()
time, so valgrind should think it sees good data in the mmap region.

The valgrind complaint showed the "origin" of the uninitialized data as
memory allocated by tcmu-runner for a command, using malloc().  Clearing
that memory immediately on allocation would make the complaint go away.

What was happening was that the sense data field was not being initialized
(which is OK as long as no one looks at it until after it has been set).
But when an error did occur, tcmu_set_sense_data only cleared out the
first 18 sense data bytes of the 96 available, before filling in a few
bytes in the 0-17 range (still OK as long as no one looks at bytes 18-95).

At the end of the command, all 96 bytes of the sense buffer get copied
back into the ring.  This is still OK because valgrind allows you to
copy uninitialized bytes around, as long as you don’t try to use them in
any computation or comparison.  Hopefully when the kernel receives the
reply, it will be smart enough not to look at bytes 18-95 except in
situations when the protocol says it has been set.

But valgrind doesn’t know anything about all that -- all it sees is this
mmap area, which now has bytes marked "undefined" in it, copied from the
*outgoing* sense data!

When the kernel recycles that part of the ring and puts another command
into the area valgrind thinks is "undefined", the data is fine -- it’s
just that the last thing valgrind saw on the usermode side was the
uninitialized data from malloc being copied into there.  So valgrind
issues the complaint when tcmu-runner attempts to read the new command.

The fix is easy and cheap -- only in the (relatively infrequent) command
instances that return sense data, clear out the whole 96-byte buffer
first, instead of only the first 18 bytes.

Signed-off-by: David Butterfield <a9fc080b52063dba1f42b7aaa6b29e259d2b7ea9@wdc.com>
Reviewed-by: Damien Le Moal <f9b68e53a89359fcdc4af0a87b5143767d923656@wdc.com>

agrover/tcmu-runner,open-iscsi/tcmu-runner,open-iscsi/tcmu-runner