Re: [PATCH v5 02/12] array_idx: sanitize speculative array de-references
From: Thomas Gleixner
Date: Sun Jan 28 2018 - 06:38:13 EST
On Sun, 28 Jan 2018, Ingo Molnar wrote:
> * Dan Williams <dan.j.williams@xxxxxxxxx> wrote:
> > +
> > +#ifndef __NOSPEC_H__
> > +#define __NOSPEC_H__
_LINUX_NOSPEC_H
We have an established practice for those header guards...
> > +/*
> > + * When idx is out of bounds (idx >= sz), the sign bit will be set.
> > + * Extend the sign bit to all bits and invert, giving a result of zero
> > + * for an out of bounds idx, or ~0UL if within bounds [0, sz).
> > + */
> > +#ifndef array_idx_mask
> > +static inline unsigned long array_idx_mask(unsigned long idx, unsigned long sz)
> > +{
> > + /*
> > + * Warn developers about inappropriate array_idx usage.
> > + *
> > + * Even if the cpu speculates past the WARN_ONCE branch, the
>
> s/cpu/CPU
>
> > + * sign bit of idx is taken into account when generating the
> > + * mask.
> > + *
> > + * This warning is compiled out when the compiler can infer that
> > + * idx and sz are less than LONG_MAX.
>
> Please use 'idx' and 'sz' in quotes, to make sure they stand out more in free
> flowing comment text. Also please use '()' to denote functions/methods.
>
> I.e. something like:
>
> * Warn developers about inappropriate array_idx() usage.
> *
> * Even if the CPU speculates past the WARN_ONCE() branch, the
> * sign bit of 'idx' is taken into account when generating the
> * mask.
> *
> * This warning is compiled out when the compiler can infer that
> * 'idx' and 'sz' are less than LONG_MAX.
I rather prefer to have a proper kernel doc instead of the comment above
the function and then use @idx and @sz in the code comments.
Thanks,
tglx