VLA removal, device_handler and COMMAND_SIZE

From: Stephen Kitt
Date: Fri Mar 09 2018 - 19:55:01 EST


Hi,

Iâve been looking into removing some VLAs from device_handler drivers,
prompted by https://lkml.org/lkml/2018/3/7/621

The uses in question here are quite straightforward, e.g. in
drivers/scsi/device_handler/scsi_dh_alua.c:

u8 cdb[COMMAND_SIZE(MAINTENANCE_IN)];

Thereâs no trivial way of keeping the compiler happy with -Wvla though here,
at least not while keeping the behaviour strictly identical. Iâve come up
with two approaches, and Iâm curious whether theyâre appropriate or if
thereâs a better way...

The first approach is to use MAX_COMMAND_SIZE instead; this wastes a few
bytes on the stack here and there, and stays reasonably maintainable.

The second approach might be symptomatic of a twisted mind, and involves
replacing COMMAND_SIZE so that it can be calculated at compile time when the
opcode is known:

/*
* SCSI command sizes are as follows, in bytes, for fixed size commands, per
* group: 6, 10, 10, 12, 16, 12, 10, 10. The top three bits of an opcode
* determine its group.
* The size table is encoded into a 32-bit value by subtracting each value
* from 16, resulting in a value of 1715488362
* (6 << 28 + 6 << 24 + 4 << 20 + 0 << 16 + 4 << 12 + 6 << 8 + 6 << 4 + 10).
* Command group 3 is reserved and should never be used.
*/
#define COMMAND_SIZE(opcode) \
(16 - (15 & (1715488362 >> (4 * (((opcode) >> 5) & 7)))))


This has the side-effect of making some of the call sites more complex, and
the macro itself isnât the most maintainer-friendly. It does mean we can drop
BLK_SCSI_REQUEST from drivers/target/Kconfig so itâs not all bad...

Both patches will follow in reply to this email, Iâll let more familiar
developers judge which is appropriate (if any).

Regards,

Stephen

Attachment: pgptJ5sHnXyBn.pgp
Description: OpenPGP digital signature