Re: [PATCH v2 03/24] virtio: allow __virtioXX, __leXX in config space

From: Cornelia Huck
Date: Tue Aug 04 2020 - 10:24:06 EST


On Mon, 3 Aug 2020 16:58:46 -0400
"Michael S. Tsirkin" <mst@xxxxxxxxxx> wrote:

> Currently all config space fields are of the type __uXX.
> This confuses people and some drivers (notably vdpa)
> access them using CPU endian-ness - which only
> works well for legacy or LE platforms.
>
> Update virtio_cread/virtio_cwrite macros to allow __virtioXX
> and __leXX field types. Follow-up patches will convert
> config space to use these types.
>
> Signed-off-by: Michael S. Tsirkin <mst@xxxxxxxxxx>
> ---
> include/linux/virtio_config.h | 50 +++++++++++++++++++++++++++++++++--
> 1 file changed, 48 insertions(+), 2 deletions(-)

(...)

> @@ -287,12 +288,57 @@ static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
> return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
> }
>
> +/*
> + * Only the checker differentiates between __virtioXX and __uXX types. But we
> + * try to share as much code as we can with the regular GCC build.
> + */
> +#if !defined(CONFIG_CC_IS_GCC) && !defined(__CHECKER__)
> +
> +/* Not a checker - we can keep things simple */
> +#define __virtio_native_typeof(x) typeof(x)
> +
> +#else
> +
> +/*
> + * We build this out of a couple of helper macros in a vain attempt to
> + * help you keep your lunch down while reading it.
> + */

It might help with the lunch, but it still gives a slight queasiness.
No ideas for a better version, though.

> +#define __virtio_pick_value(x, type, then, otherwise) \
> + __builtin_choose_expr(__same_type(x, type), then, otherwise)
> +
> +#define __virtio_pick_type(x, type, then, otherwise) \
> + __virtio_pick_value(x, type, (then)0, otherwise)
> +
> +#define __virtio_pick_endian(x, x16, x32, x64, otherwise) \
> + __virtio_pick_type(x, x16, __u16, \
> + __virtio_pick_type(x, x32, __u32, \
> + __virtio_pick_type(x, x64, __u64, \
> + otherwise)))
> +
> +#define __virtio_native_typeof(x) typeof( \
> + __virtio_pick_type(x, __u8, __u8, \
> + __virtio_pick_endian(x, __virtio16, __virtio32, __virtio64, \
> + __virtio_pick_endian(x, __le16, __le32, __le64, \
> + __virtio_pick_endian(x, __u16, __u32, __u64, \
> + /* No other type allowed */ \
> + (void)0)))))
> +
> +#endif
> +
> +#define __virtio_native_type(structname, member) \
> + __virtio_native_typeof(((structname*)0)->member)
> +
> +#define __virtio_typecheck(structname, member, val) \
> + /* Must match the member's type, and be integer */ \
> + typecheck(__virtio_native_type(structname, member), (val))
> +
> +

Acked-by: Cornelia Huck <cohuck@xxxxxxxxxx>