Re: [PATCH v3 1/3] scsi: 3w-9xxx: Use flexible array members to avoid struct padding

From: Joe Perches
Date: Sat Aug 08 2020 - 22:38:06 EST


On Sat, 2020-08-08 at 19:47 -0500, Samuel Holland wrote:
> In preparation for removing the "#pragma pack(1)" from the driver, fix
> all instances where a trailing array member could be replaced by a
> flexible array member. Since a flexible array member has zero size, it
> introduces no padding, whether or not the struct is packed.
[]
> diff --git a/drivers/scsi/3w-9xxx.c b/drivers/scsi/3w-9xxx.c
[]
> @@ -676,7 +676,7 @@ static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long
> data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
>
> /* Now allocate ioctl buf memory */
> - cpu_addr = dma_alloc_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, &dma_handle, GFP_KERNEL);
> + cpu_addr = dma_alloc_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted + sizeof(TW_Ioctl_Buf_Apache), &dma_handle, GFP_KERNEL);

Trivia:

It's perhaps more sensible to order the arguments with
the size of the struct first then the data length so
the argument is in the order of the expected content.

cpu_addr = dma_alloc_coherent(&tw_dev->tw_pci_dev->dev,
sizeof(TW_Ioctl_Buf_Apache) + data_buffer_length_adjusted,
&dma_handle, GFP_KERNEL);

> @@ -685,7 +685,7 @@ static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long
> tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
>
> /* Now copy down the entire ioctl */
> - if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
> + if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache)))

if (copy_from_user(tw_ioctl, argp, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length))

etc...