From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6ACEB313267 for ; Mon, 3 Aug 2026 14:38:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785767887; cv=none; b=WzUQba+xOLZhMR6K7zee8w9x3tASV0ZJF4v9LIiA9nEyNTzfgN7Pu8b7Z8yjXZILjBqWSSK39x12wEUdmIxsQ8kQFWx/kwvqNOFTozcutdobhQkVBU6xdtIk/XwjXWN2xPX+LQ9jhX5Un9MP7PkH0uDI+OoKStPjk5e2WHIlqso= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785767887; c=relaxed/simple; bh=myIJN7qk85z7FqQofK3r/8NHttuSxMUwSKGXvlxKYyc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=cYCAiZKng+65rlJqVFdKVwbf1Bo7ERx4O81BhNi3umWAiymEWOpuNdSeNbagwwIPqOPGV9W2FMoST+D/7X9zoeFZY3FfkR+k7VbKsZ3QVOo3QcwLfGXGBMbzbpAb/37a0ZhE0TvSHjgCLfz2/4OCTQW3Xhtus+L+e66nywKbRAE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=T8mxH5JY; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="T8mxH5JY" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 42F3D143D for ; Mon, 3 Aug 2026 07:38:00 -0700 (PDT) Received: from [10.2.11.34] (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 0BFB23F632 for ; Mon, 3 Aug 2026 07:38:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1785767884; bh=myIJN7qk85z7FqQofK3r/8NHttuSxMUwSKGXvlxKYyc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=T8mxH5JY+AqA5MvTZjjNtsAglYfqVrJ3APkY4U+SLopJbMUBLIiVwU5ExM9xEuNCi +fanaPGlVZje8Zbm6JRUPwsRy2lDHD0aqX7xw/NHm0Nn6CdQlS28M6Lel1tqwXNoYu gW9Q2gMJEav1is7POtN7EUlK42DHNULg5GfznN/s= Date: Mon, 3 Aug 2026 15:37:52 +0100 From: Liviu Dudau To: Osama Abdelkader Cc: Boris Brezillon , Steven Price , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] drm/panthor: use local variables for firmware interface counts Message-ID: References: <20260803153555.70ca46de@fedora1.home> <20260803141149.68182-1-osama.abdelkader@gmail.com> 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 Content-Transfer-Encoding: 8bit In-Reply-To: <20260803141149.68182-1-osama.abdelkader@gmail.com> On Mon, Aug 03, 2026 at 04:11:49PM +0200, Osama Abdelkader wrote: > The firmware exposes the global group count and per-group stream count in > the shared control interface. These values are validated before being used > as loop bounds. > > Read each count once with READ_ONCE() and store it in a local variable. > This avoids reading the same control section field twice and makes it > explicit that the loop uses the same value that passed validation > to protect against self-modifying control sections. > > Signed-off-by: Osama Abdelkader > Reviewed-by: Steven Price Reviewed-by: Liviu Dudau Best regards, Liviu > --- > v3: > - Add comments explaining the READ_ONCE() usage. > > drivers/gpu/drm/panthor/panthor_fw.c | 24 ++++++++++++++++++------ > 1 file changed, 18 insertions(+), 6 deletions(-) > > diff --git a/drivers/gpu/drm/panthor/panthor_fw.c b/drivers/gpu/drm/panthor/panthor_fw.c > index 8411c468bdac..b2ccb81f280c 100644 > --- a/drivers/gpu/drm/panthor/panthor_fw.c > +++ b/drivers/gpu/drm/panthor/panthor_fw.c > @@ -954,6 +954,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev, > u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem); > u64 iface_offset = CSF_GROUP_CONTROL_OFFSET + > ((u64)csg_idx * glb_iface->control->group_stride); > + u32 stream_num; > unsigned int i; > > if (iface_offset > shared_section_sz || > @@ -967,8 +968,13 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev, > csg_iface->output = iface_fw_to_cpu_addr(ptdev, csg_iface->control->output_va, > sizeof(*csg_iface->output)); > > - if (csg_iface->control->stream_num < MIN_CS_PER_CSG || > - csg_iface->control->stream_num > MAX_CS_PER_CSG) > + /* > + * To protect against self-modifying control sections > + * take a single snapshot from the control section so validation and > + * iteration use the same value. > + */ > + stream_num = READ_ONCE(csg_iface->control->stream_num); > + if (stream_num < MIN_CS_PER_CSG || stream_num > MAX_CS_PER_CSG) > return -EINVAL; > > if (!csg_iface->input || !csg_iface->output) { > @@ -986,7 +992,7 @@ static int panthor_init_csg_iface(struct panthor_device *ptdev, > } > } > > - for (i = 0; i < csg_iface->control->stream_num; i++) { > + for (i = 0; i < stream_num; i++) { > int ret = panthor_init_cs_iface(ptdev, csg_idx, i); > > if (ret) > @@ -1010,6 +1016,7 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev) > { > struct panthor_fw_global_iface *glb_iface = &ptdev->fw->iface.global; > u64 shared_section_sz = panthor_kernel_bo_size(ptdev->fw->shared_section->mem); > + u32 group_num; > unsigned int i; > > if (!ptdev->fw->shared_section->mem->kmap) > @@ -1035,13 +1042,18 @@ static int panthor_fw_init_ifaces(struct panthor_device *ptdev) > return -EINVAL; > } > > - if (glb_iface->control->group_num > MAX_CSGS || > - glb_iface->control->group_num < MIN_CSGS) { > + /* > + * To protect against self-modifying control sections > + * take a single snapshot from the control section so validation and > + * iteration use the same value. > + */ > + group_num = READ_ONCE(glb_iface->control->group_num); > + if (group_num > MAX_CSGS || group_num < MIN_CSGS) { > drm_err(&ptdev->base, "Invalid number of control groups"); > return -EINVAL; > } > > - for (i = 0; i < glb_iface->control->group_num; i++) { > + for (i = 0; i < group_num; i++) { > int ret = panthor_init_csg_iface(ptdev, i); > > if (ret) > -- > 2.43.0 > -- ==================== | I would like to | | fix the world, | | but they're not | | giving me the | \ source code! / --------------- ¯\_(ツ)_/¯