diff mbox

usb: fix uvc_alloc_entity() allocation alignment

Message ID 20180604134713.101064-1-namit@vmware.com (mailing list archive)
State New, archived
Headers show

Commit Message

Nadav Amit June 4, 2018, 1:47 p.m. UTC
The use of ALIGN() in uvc_alloc_entity() is incorrect, since the size of
(entity->pads) is not a power of two. As a stop-gap, until a better
solution is adapted, use roundup() instead.

Found by a static assertion. Compile-tested only.

Fixes: 4ffc2d89f38a ("uvcvideo: Register subdevices for each entity")

Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>
Cc: linux-media@vger.kernel.org
Cc: linux-kernel@vger.kernel.org

Signed-off-by: Nadav Amit <namit@vmware.com>
---
 drivers/media/usb/uvc/uvc_driver.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Laurent Pinchart Aug. 6, 2018, 11:58 p.m. UTC | #1
Hi Nadav,

Thank you for the patch.

On Monday, 4 June 2018 16:47:13 EEST Nadav Amit wrote:
> The use of ALIGN() in uvc_alloc_entity() is incorrect, since the size of
> (entity->pads) is not a power of two. As a stop-gap, until a better
> solution is adapted, use roundup() instead.
> 
> Found by a static assertion. Compile-tested only.
> 
> Fixes: 4ffc2d89f38a ("uvcvideo: Register subdevices for each entity")
> 
> Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> Cc: Mauro Carvalho Chehab <mchehab@kernel.org>
> Cc: linux-media@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> 
> Signed-off-by: Nadav Amit <namit@vmware.com>
> ---
>  drivers/media/usb/uvc/uvc_driver.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/media/usb/uvc/uvc_driver.c
> b/drivers/media/usb/uvc/uvc_driver.c index 2469b49b2b30..6b989d41c034
> 100644
> --- a/drivers/media/usb/uvc/uvc_driver.c
> +++ b/drivers/media/usb/uvc/uvc_driver.c
> @@ -909,7 +909,7 @@ static struct uvc_entity *uvc_alloc_entity(u16 type, u8
> id, unsigned int size;
>  	unsigned int i;
> 
> -	extra_size = ALIGN(extra_size, sizeof(*entity->pads));
> +	extra_size = roundup(extra_size, sizeof(*entity->pads));
>  	num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
>  	size = sizeof(*entity) + extra_size + sizeof(*entity->pads) * num_pads
>  	     + num_inputs;

The purpose of this alignment is to make sure that entity->pads will be 
properly aligned. In theory the size of uvc_entity should be taken into 
account too, but the structure contains pointers, so its size should already 
be properly aligned. This patch thus looks good to me. What made you say it's 
a stop-gap measure ?

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

and applied to my tree.
Nadav Amit Aug. 7, 2018, 12:58 a.m. UTC | #2
at 4:58 PM, Laurent Pinchart <laurent.pinchart@ideasonboard.com> wrote:

> Hi Nadav,
> 
> Thank you for the patch.
> 
> On Monday, 4 June 2018 16:47:13 EEST Nadav Amit wrote:
>> The use of ALIGN() in uvc_alloc_entity() is incorrect, since the size of
>> (entity->pads) is not a power of two. As a stop-gap, until a better
>> solution is adapted, use roundup() instead.
>> 
>> Found by a static assertion. Compile-tested only.
>> 
>> Fixes: 4ffc2d89f38a ("uvcvideo: Register subdevices for each entity")
>> 
>> Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
>> Cc: Mauro Carvalho Chehab <mchehab@kernel.org>
>> Cc: linux-media@vger.kernel.org
>> Cc: linux-kernel@vger.kernel.org
>> 
>> Signed-off-by: Nadav Amit <namit@vmware.com>
>> ---
>> drivers/media/usb/uvc/uvc_driver.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>> 
>> diff --git a/drivers/media/usb/uvc/uvc_driver.c
>> b/drivers/media/usb/uvc/uvc_driver.c index 2469b49b2b30..6b989d41c034
>> 100644
>> --- a/drivers/media/usb/uvc/uvc_driver.c
>> +++ b/drivers/media/usb/uvc/uvc_driver.c
>> @@ -909,7 +909,7 @@ static struct uvc_entity *uvc_alloc_entity(u16 type, u8
>> id, unsigned int size;
>> 	unsigned int i;
>> 
>> -	extra_size = ALIGN(extra_size, sizeof(*entity->pads));
>> +	extra_size = roundup(extra_size, sizeof(*entity->pads));
>> 	num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
>> 	size = sizeof(*entity) + extra_size + sizeof(*entity->pads) * num_pads
>> 	     + num_inputs;
> 
> The purpose of this alignment is to make sure that entity->pads will be 
> properly aligned. In theory the size of uvc_entity should be taken into 
> account too, but the structure contains pointers, so its size should already 
> be properly aligned. This patch thus looks good to me. What made you say it's 
> a stop-gap measure ?

Thanks. It’s been a while. Anyhow, I don’t know how “hot” this code is, but
roundup uses a div operations, so if it is “hot” you may want a different
way to align with lower overhead.

I presume it is not…

Regards,
Nadav
Laurent Pinchart Aug. 7, 2018, 8:59 a.m. UTC | #3
Hi Nadav,

On Tuesday, 7 August 2018 03:58:05 EEST Nadav Amit wrote:
> at 4:58 PM, Laurent Pinchart <laurent.pinchart@ideasonboard.com> wrote:
> > On Monday, 4 June 2018 16:47:13 EEST Nadav Amit wrote:
> > 
> >> The use of ALIGN() in uvc_alloc_entity() is incorrect, since the size of
> >> (entity->pads) is not a power of two. As a stop-gap, until a better
> >> solution is adapted, use roundup() instead.
> >> 
> >> Found by a static assertion. Compile-tested only.
> >> 
> >> Fixes: 4ffc2d89f38a ("uvcvideo: Register subdevices for each entity")
> >> 
> >> Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> >> Cc: Mauro Carvalho Chehab <mchehab@kernel.org>
> >> Cc: linux-media@vger.kernel.org
> >> Cc: linux-kernel@vger.kernel.org
> >> 
> >> Signed-off-by: Nadav Amit <namit@vmware.com>
> >> ---
> >> drivers/media/usb/uvc/uvc_driver.c | 2 +-
> >> 1 file changed, 1 insertion(+), 1 deletion(-)
> >> 
> >> diff --git a/drivers/media/usb/uvc/uvc_driver.c
> >> b/drivers/media/usb/uvc/uvc_driver.c index 2469b49b2b30..6b989d41c034
> >> 100644
> >> --- a/drivers/media/usb/uvc/uvc_driver.c
> >> +++ b/drivers/media/usb/uvc/uvc_driver.c
> >> @@ -909,7 +909,7 @@ static struct uvc_entity *uvc_alloc_entity(u16 type,
> >> u8 id,
> >>  	unsigned int size;
> >> 	unsigned int i;
> >> 
> >> -	extra_size = ALIGN(extra_size, sizeof(*entity->pads));
> >> +	extra_size = roundup(extra_size, sizeof(*entity->pads));
> >> 	num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
> >> 	size = sizeof(*entity) + extra_size + sizeof(*entity->pads) * num_pads
> >> 	     + num_inputs;
> > 
> > The purpose of this alignment is to make sure that entity->pads will be 
> > properly aligned. In theory the size of uvc_entity should be taken into 
> > account too, but the structure contains pointers, so its size should
> > already be properly aligned. This patch thus looks good to me. What
> > made you say it's a stop-gap measure ?
> 
> 
> Thanks. It’s been a while. Anyhow, I don’t know how “hot” this code is, but
> roundup uses a div operations, so if it is “hot” you may want a different
> way to align with lower overhead.
> 
> I presume it is not…

You're right, it isn't. I'll include this patch in my next pull request for 
v4.20.
diff mbox

Patch

diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c
index 2469b49b2b30..6b989d41c034 100644
--- a/drivers/media/usb/uvc/uvc_driver.c
+++ b/drivers/media/usb/uvc/uvc_driver.c
@@ -909,7 +909,7 @@  static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id,
 	unsigned int size;
 	unsigned int i;
 
-	extra_size = ALIGN(extra_size, sizeof(*entity->pads));
+	extra_size = roundup(extra_size, sizeof(*entity->pads));
 	num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1;
 	size = sizeof(*entity) + extra_size + sizeof(*entity->pads) * num_pads
 	     + num_inputs;