|
Message-ID: <20180311160135.66af901e@heffalump.sk2.org>
Date: Sun, 11 Mar 2018 16:01:35 +0100
From: Stephen Kitt <steve@....org>
To: James Bottomley <jejb@...ux.vnet.ibm.com>
Cc: Bart Van Assche <Bart.VanAssche@....com>, "hare@...e.com"
<hare@...e.com>, "martin.petersen@...cle.com\"
<martin.petersen@...cle.com>, "axboe@...nel.dk" <axboe@...nel.dk>,
"linux-scsi@...r.kernel.org\" <linux-scsi@...r.kernel.org>,
"linux-kernel@...r.kernel.org\" <linux-kernel@...r.kernel.org>,
"linux-block@...r.kernel.org\" <linux-block@...r.kernel.org>,
"kernel-hardening@...ts.openwall.com\"
<kernel-hardening@...ts.openwall.com>
Subject: Re: [PATCH] scsi: resolve COMMAND_SIZE at compile time
On Sat, 10 Mar 2018 12:49:17 -0800, James Bottomley <jejb@...ux.vnet.ibm.com>
wrote:
> On Sat, 2018-03-10 at 14:29 +0100, Stephen Kitt wrote:
> > On Fri, 9 Mar 2018 22:47:12 +0000, Bart Van Assche <Bart.VanAssche@wd
> > c.com>
> > wrote:
> > > On Fri, 2018-03-09 at 23:33 +0100, Stephen Kitt wrote:
> > > > +/*
> > > > + * 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)))))
> > >
> > > To me this seems hard to read and hard to verify. Could this have
> > > been
> > > written as a combination of ternary expressions, e.g. using a gcc
> > > statement
> > > expression to ensure that opcode is evaluated once?
> >
> > That’s what I’d tried initially, e.g.
> >
> > #define COMMAND_SIZE(opcode) ({ \
> > int index = ((opcode) >> 5) & 7; \
> > index == 0 ? 6 : (index == 4 ? 16 : index == 3 || index == 5 ? 12 :
> > 10); \
> > })
> >
> > But gcc still reckons that results in a VLA, defeating the initial
> > purpose of
> > the exercise.
> >
> > Does it help if I make the magic value construction clearer?
> >
> > #define SCSI_COMMAND_SIZE_TBL ( \
> > (16 - 6) \
> > + ((16 - 10) << 4) \
> > + ((16 - 10) << 8) \
> > + ((16 - 12) << 12) \
> > + ((16 - 16) << 16) \
> > + ((16 - 12) << 20) \
> > + ((16 - 10) << 24) \
> > + ((16 - 10) << 28))
> >
> > #define
> > COMMAND_SIZE(opcode) \
> > (16 - (15 & (SCSI_COMMAND_SIZE_TBL >> (4 * (((opcode) >> 5) &
> > 7)))))
>
> Couldn't we do the less clever thing of making the array a static const
> and moving it to a header? That way the compiler should be able to
> work it out at compile time.
I hoped so too, but const-ifying a variable doesn’t make it a compile-time
constant (even though the optimiser can resolve it at build-time), so GCC
still considers uses such as
u8 cdb[COMMAND_SIZE(MAINTENANCE_IN)];
as variable-length arrays, which C90 forbids (and which we’re trying to
eliminate here).
Regards,
Stephen
Content of type "application/pgp-signature" skipped
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.