Received: by 2002:ab2:784b:0:b0:1fd:adc2:8405 with SMTP id m11csp352351lqp; Mon, 10 Jun 2024 06:18:07 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXLx1QoXiPan/8d9rP+Nq8zrwh19Nf6VzifmSCnGqztRmaezr/XTd2x3RuEJRdtg72TJdCrwJpeO7MBOwMGsUHScnUU1oEINCIQrBrHmw== X-Google-Smtp-Source: AGHT+IFGzviJQShZWgrCHc/xWLPDZnjbFlrNrf21RCTN5XWXuRqyskXZrmEHk9CrJs/pzCtnKknI X-Received: by 2002:a17:907:5c4:b0:a67:a2e0:9dcc with SMTP id a640c23a62f3a-a6cd56108ebmr791357566b.5.1718025487614; Mon, 10 Jun 2024 06:18:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718025487; cv=pass; d=google.com; s=arc-20160816; b=F3s3CEr+BJhaqoEa29a+mJdufQ4S5pqyzL/JBrpWoPTzNcrgBC1JLU+qmjDB7zRmnf jcutKf9vmfZcM9GZdyV0BAvft712P2qdnBkuXFMLVbhX0PFIOCvq6fFq/Iy0S8FfOTzf HUiO2sXO5aKXTmQ7Nc+pWiTZDFBq3YcGpPFDGw7korgVo6v23F054IJ+PKcp7Hu3ver1 hQl2/FWtYV7phlETg5s0m7x1+di2GuuyP/MTWvNVig36ztL4QiJ9HREJQ5Xy2JHFUt2L PsWvICu5slmCYs9FIYSL8shk5m+0CFCJ6hzQ8e6dJ26Gm5xjG6ux2PGNlM41UebgrwIX ie+g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=b6durJruOx5OBJweZdk8psRvbNH9GjvXvLEl7eKWBUI=; fh=oQCLq+wd++VIofpYOH1JuLytvTrxwG27xlhX6tOdaRI=; b=FZcvXarYsT+9+4kP+1M88x4YGDtWMaK69Kp64Aq2RWVvMJ6oaMqoigWdqmc7lSnRVn XTsuDVX0QYI4/2xIfKhuwdX2QbR+5Ib2ZnxS6/pVLRbAFwHwmCWz6JdxHZ2k6sIE1Q3E jVNZGcwmzJqVpmdmOMpV58Tq0Y0+5Sn0P+AmbMEpqBn0jX95SZd9zHBlkbKL2KIZk28R 54sC9eoWFdbhfH6Vd0dKQKZrqX2t7GGobeNEbj2+3W/Go1xQ4p6qI5MpHPBqtHXzrWNE NT2fF7r3xLFYe4cROfKS1ZBsgsbxpVrzMOSHjRzr45pEGChLKox8uGOkcjst2zWxnIpl admw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b=pW5Ox50I; arc=pass (i=1 spf=pass spfdomain=ideasonboard.com dkim=pass dkdomain=ideasonboard.com); spf=pass (google.com: domain of linux-kernel+bounces-208215-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208215-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a6f15327007si159838066b.827.2024.06.10.06.18.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 06:18:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-208215-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass (test mode) header.i=@ideasonboard.com header.s=mail header.b=pW5Ox50I; arc=pass (i=1 spf=pass spfdomain=ideasonboard.com dkim=pass dkdomain=ideasonboard.com); spf=pass (google.com: domain of linux-kernel+bounces-208215-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208215-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 959371F25293 for ; Mon, 10 Jun 2024 13:09:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CE36F81AD7; Mon, 10 Jun 2024 13:09:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="pW5Ox50I" Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8AC28DDC0; Mon, 10 Jun 2024 13:09:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.167.242.64 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718024974; cv=none; b=ln8B4ihJ23ioEbXMEM3X9yl37ICoKsuLo6arGHdCFa6DrxcT9q1vMA+4SvypLvnh+nc+4Te3JG8yxLrFDHdFolQZhE0hxl1z1MeWwd7v5BdVEoj1KUBMEAVwPHpJvSsuwrZb0kYp7jsLWno8AOtgDi4NP/Qi3qRSxlYkXLdE9iY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718024974; c=relaxed/simple; bh=P3HR2KD0zqsJGS2R7ct1yLvmTw5LrC648wwAWEh3bUg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Fxs7ihslj9bOonxRzpklYUIWfgJUK/bGDth29KqQuBs+Vz+vt1oBw3V6JWvqpSzhkeaPhUezRg2jQJYHNb9eVUj3N05sV/RNCci7/VF21Q3xFlf5n6nx3SCFTzpTwW9w0KakXqUbaIZOjJ6M3H1hB1m+yjUNCrnmwiL2cB6Cs4Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ideasonboard.com; spf=pass smtp.mailfrom=ideasonboard.com; dkim=pass (1024-bit key) header.d=ideasonboard.com header.i=@ideasonboard.com header.b=pW5Ox50I; arc=none smtp.client-ip=213.167.242.64 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ideasonboard.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ideasonboard.com Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id A85A1397; Mon, 10 Jun 2024 15:09:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1718024957; bh=P3HR2KD0zqsJGS2R7ct1yLvmTw5LrC648wwAWEh3bUg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=pW5Ox50IAYhuClLVzAgESOCNu/rD3nRuNXCwV7pNawUfPV3JQPRAxzLLKT9teYaOd 0VNKnzH+GB/Lz0LgrkUon2N/ANy5yBl/nTA8LShcM2ofu05uMjRCNA7dMowBoc3bAz XNhTD2V8481Nc5LOuTjQeFRnaN0JWhivSHPYaA78= Date: Mon, 10 Jun 2024 16:09:10 +0300 From: Laurent Pinchart To: Ricardo Ribalda Cc: Mauro Carvalho Chehab , linux-media@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] media: uvcvideo: Enforce alignment of frame and interval Message-ID: <20240610130910.GA12787@pendragon.ideasonboard.com> References: <20240404-uvc-align-v2-1-9e104b0ecfbd@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20240404-uvc-align-v2-1-9e104b0ecfbd@chromium.org> Hi Ricardo, Thank you for the patch. On Thu, Apr 04, 2024 at 05:56:18PM +0000, Ricardo Ribalda wrote: > Struct uvc_frame and interval (u32*) are packaged together on > streaming->formats on a single contiguous allocation. > > Right now they allocated right after uvc_format, without taking into s/they/they are/ > consideration their required alignment. > > This is working fine because both structures have a field with a > pointer, but it will stop working when the sizeof() of any of those > structs is not a multiple of the sizeof(void*). > > Enforce that alignment during the allocation. > > Signed-off-by: Ricardo Ribalda > --- > This is better than 3 allocations, and do not have any performance > penalty. > > I have tried this patch printing the size and the address of the > pointers in the old and the new mode, and it looks the same. > > [ 2.235223] drivers/media/usb/uvc/uvc_driver.c:694 uvc_parse_streaming 432 > [ 2.235249] drivers/media/usb/uvc/uvc_driver.c:704 uvc_parse_streaming 432 > [ 2.235256] drivers/media/usb/uvc/uvc_driver.c:714 uvc_parse_streaming 00000000d32087cc 00000000d3803788 > [ 2.235265] drivers/media/usb/uvc/uvc_driver.c:720 uvc_parse_streaming 00000000d32087cc 00000000d3803788 > --- > Changes in v2: Thanks Laurent. > - Enforce alignment during allocation instead of using __aligned() > macros. > - Link to v1: https://lore.kernel.org/r/20230501-uvc-align-v1-1-0f713e4b84c3@chromium.org > --- > drivers/media/usb/uvc/uvc_driver.c | 20 +++++++++++++++----- > 1 file changed, 15 insertions(+), 5 deletions(-) > > diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c > index 7aefa76a42b31..7d9844ba3b205 100644 > --- a/drivers/media/usb/uvc/uvc_driver.c > +++ b/drivers/media/usb/uvc/uvc_driver.c > @@ -663,16 +663,26 @@ static int uvc_parse_streaming(struct uvc_device *dev, > goto error; > } > > - size = nformats * sizeof(*format) + nframes * sizeof(*frame) > - + nintervals * sizeof(*interval); > + /* > + * Allocate memory for the formats, the frames and the intervals, > + * plus any required padding to guarantee that everything has the > + * correct alignment. > + */ > + size = nformats * sizeof(*format); > + size = ALIGN(size, __alignof__(*frame)) + nframes * sizeof(*frame); > + size = ALIGN(size, __alignof__(*interval)) > + + nintervals * sizeof(*interval); You have two extra spaces here. I'll fix when applying. Reviewed-by: Laurent Pinchart > + > format = kzalloc(size, GFP_KERNEL); > - if (format == NULL) { > + if (!format) { > ret = -ENOMEM; > goto error; > } > > - frame = (struct uvc_frame *)&format[nformats]; > - interval = (u32 *)&frame[nframes]; > + frame = (void *)format + nformats * sizeof(*format); > + frame = PTR_ALIGN(frame, __alignof__(*frame)); > + interval = (void *)frame + nframes * sizeof(*frame); > + interval = PTR_ALIGN(interval, __alignof__(*interval)); > > streaming->format = format; > streaming->nformats = nformats; > > --- > base-commit: 58390c8ce1bddb6c623f62e7ed36383e7fa5c02f > change-id: 20230501-uvc-align-6ff202b68dab -- Regards, Laurent Pinchart