From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bali.collaboradmins.com (bali.collaboradmins.com [148.251.105.195]) (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 14A9D40DB4E for ; Mon, 3 Aug 2026 13:36:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.251.105.195 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785764163; cv=none; b=A8epS0Xho2d4bDlhISh9bAfRTij5tKbu2N0Xl1EjXaA4bK/uzLUjq/zJFg9uf/tZjF5epDD78YKJo2s3ueQV1XJdvY13Eez6+kaJK4HR5eI4ckbXQx5OD8E4REWdGSSwLx7jp5iwzT6XvEV1ty+uUw3j6k3PN1pHTwOw0dxBZl4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785764163; c=relaxed/simple; bh=JTJhPk572PVGJDshQzn2xjCt+P6e2tpxEvXRAPlQVUw=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=u2J92rR4qoqKhGpHcaQ/uzmBCekABHCzLBA/SVwmUs5M8xKtUJ7QKvQxNm26VV1SNYAZQLQbWDCdD08k2IhV0k6RvCm3Vuzd5zIn7hqF1mi0lmOYpWF5J8QTaceTrtdL1M6SzRB5ZBQOoQFRzjgsEFMneXtxri8oKYP6Wlz/RcE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=erELjXkd; arc=none smtp.client-ip=148.251.105.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="erELjXkd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1785764160; bh=JTJhPk572PVGJDshQzn2xjCt+P6e2tpxEvXRAPlQVUw=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=erELjXkdKVMPbweTp3EbOCovzA0xArQZ19x/UO/+S1gczwh6jmPLDHgfz8XPN7SZV qSN1zV2I9D8pLKN0qAH1FtCNWt6oTppSlLg2dz4t1sJQVMx4Ymp5X+cYmN36QMfgDV bG7TfkF1gkSILAe1damcNj8lOuRS4EgGzFEyIW1FrJWtsEMGCMvf3gOOWSQLm0Scki EPNzKrTWFODk/N6kc70bvr3d/rwDPxdWrysj9NJRS95gC1cKWGeJLB1OHq1xJREQ8h 8AE4KgrXi8OmfvJU8+jxsWyG2LC/s6BPEc1GLn94yBo77nG2H3ToFWQkwmmoQgyux+ +NoPKhXwZ8L8g== Received: from fedora1.home (unknown [100.64.0.11]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange secp256r1 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bbrezillon) by bali.collaboradmins.com (Postfix) with ESMTPSA id C4B0817E0908; Mon, 03 Aug 2026 15:35:59 +0200 (CEST) Date: Mon, 3 Aug 2026 15:35:55 +0200 From: Boris Brezillon To: Osama Abdelkader Cc: Steven Price , Liviu Dudau , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] drm/panthor: use local variables for firmware interface counts Message-ID: <20260803153555.70ca46de@fedora1.home> In-Reply-To: <20260803124807.27094-1-osama.abdelkader@gmail.com> References: <2125ea5a-d878-4032-9338-d0cd0970ccfa@arm.com> <20260803124807.27094-1-osama.abdelkader@gmail.com> Organization: Collabora X-Mailer: Claws Mail 4.4.0 (GTK 3.24.52; x86_64-redhat-linux-gnu) 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=US-ASCII Content-Transfer-Encoding: 7bit On Mon, 3 Aug 2026 14:48:06 +0200 Osama Abdelkader wrote: > panthor_fw_init_ifaces() validates the firmware interface group count > before iterating over the CSG interfaces. panthor_init_csg_iface() does > the same for the per-group stream count before iterating over the CS > interfaces. > > Store those validated counts in local variables and use the locals as the > loop bounds. This avoids reading the same control interface fields twice > and makes it explicit that the loops use the values that were just > validated. > > Signed-off-by: Osama Abdelkader > Reviewed-by: Steven Price > --- > v2: > - Reword as an optimization/cleanup instead of a firmware trust-boundary fix. > - Drop the Fixes and stable tags. > > drivers/gpu/drm/panthor/panthor_fw.c | 11 +++++++---- > 1 file changed, 7 insertions(+), 4 deletions(-) > > diff --git a/drivers/gpu/drm/panthor/panthor_fw.c b/drivers/gpu/drm/panthor/panthor_fw.c > index e2fcbd639c3c..6e6da98d795e 100644 > --- a/drivers/gpu/drm/panthor/panthor_fw.c > +++ b/drivers/gpu/drm/panthor/panthor_fw.c > @@ -959,6 +959,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 || > @@ -972,8 +973,8 @@ 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) > + stream_num = READ_ONCE(csg_iface->control->stream_num); We need a comment to explain the READ_ONCE(), otherwise new readers (and given my inability to remember things, I consider myself a new reader after 2 weeks :-)) will keep wondering why we're forcing the compiler to read the memory only once. IIUC, that's here to protect against self-modifying control sections, so maybe say that. Or if we consider that the FW is trusted/sure, drop the READ_ONCE()... > + if (stream_num < MIN_CS_PER_CSG || stream_num > MAX_CS_PER_CSG) > return -EINVAL; > > if (!csg_iface->input || !csg_iface->output) { > @@ -990,7 +991,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) > @@ -1015,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) > @@ -1034,17 +1036,17 @@ 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) { > + 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) > return ret; > } > > drm_info(&ptdev->base, "CSF FW using interface v%d.%d.%d, Features %#x Instrumentation features %#x",