From nobody Fri Oct 3 11:26:45 2025 Received: from lx20.hoststar.hosting (lx20.hoststar.hosting [168.119.41.54]) (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 2EDB22E6CC5; Mon, 1 Sep 2025 15:39:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=168.119.41.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756741156; cv=none; b=B2nZeB9t1qyRxgIX5aJ3bvc6FP6T/xbSJg3VCj2ny1LR09bs2rScKGcs39b5kKkzSDnQflyXQGoPEdQ8U2RSChZpIKsaxuhZ3OYUsyn2LcCtL69fsnac1Hww33o9jZv7TdVlGSH1fZgWTl5d0yelNJXqVM2bnoZJHM/rOjp3tfI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756741156; c=relaxed/simple; bh=pzCAYpcZ6TRuPnvynWB0NnbgVoLcpBe58CdI+qoi7L8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FVDQZLB593epD1Mb29ff4GqjZBG0AbIvZr+o3i+AtqT4c1J8jNS1qSFoZoaBIaAItgzamt22glwLPC8Tpeh0vNk0qMbE6CZeZUbSt6M1SwXnegAqqtTIemBmydis9ny3AnIpcb4QoaTW3bGqvjP1UdXkBmhiXEPnqg+BHKWZYC4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=emfend.at; spf=pass smtp.mailfrom=emfend.at; dkim=pass (1024-bit key) header.d=emfend.at header.i=@emfend.at header.b=nE+8j4aU; arc=none smtp.client-ip=168.119.41.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=emfend.at Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=emfend.at Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=emfend.at header.i=@emfend.at header.b="nE+8j4aU" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=emfend.at; s=mail; h=Cc:To:In-Reply-To:References:Message-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:Subject:Date:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=lAEGMtMrINsBBz6MI0IX+Lwhx6wj87W77gHBsxw581A=; b=nE+8j4aUzAse7w9JricNCKIZpX DT1grMqUsWWx3jPTq+H9cEqEOdSetK+8m1Y89w9IoTmaFvch+YOhVueqcW1bDhAaFWOQ6hhAa61mB iMqDlgQ8xdKnhzEdR9Wds+sTiNXPCvajxh+h2BWeDVrmyIapGdur5RiAVsKVHy7/7+O8=; Received: from 194-208-208-245.tele.net ([194.208.208.245]:61156 helo=[127.0.1.1]) by lx20.hoststar.hosting with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1ut6E3-009YyN-3e; Mon, 01 Sep 2025 17:13:43 +0200 From: Matthias Fend Date: Mon, 01 Sep 2025 17:13:36 +0200 Subject: [PATCH 1/3] media: allegro: print warning if channel creation timeout occurs 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-Transfer-Encoding: quoted-printable Message-Id: <20250901-allegro-dvt-fixes-v1-1-4e4d493836ef@emfend.at> References: <20250901-allegro-dvt-fixes-v1-0-4e4d493836ef@emfend.at> In-Reply-To: <20250901-allegro-dvt-fixes-v1-0-4e4d493836ef@emfend.at> To: Michael Tretter , Pengutronix Kernel Team , Mauro Carvalho Chehab Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Matthias Fend X-Mailer: b4 0.14.2 X-Spam-Score: -1.0 X-Spam-Bar: - X-Spam-Report: Spam detection software, running on the system "lx20.hoststar.hosting", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Several fixes and improvements for the Allegro DVT video IP encoder. These relate to race conditions that occur when multiple streams are used simultaneously. The problems could be reproduced on a ZCU [...] Content analysis details: (-1.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP 0.0 TVD_RCVD_IP Message was received from an IP address 0.0 Local_hs1_NotHoststar Sender is not from hoststar.ch|de|com 0.0 KAM_DMARC_STATUS Test Rule for DKIM or SPF Failure with Strict Alignment This message can be helpful for troubleshooting and there is already a corresponding message in case of a channel destroy timeout. Add a similar message for channel creation. Signed-off-by: Matthias Fend --- drivers/media/platform/allegro-dvt/allegro-core.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/media/platform/allegro-dvt/allegro-core.c b/drivers/me= dia/platform/allegro-dvt/allegro-core.c index eb03df0d8652745ef533b9b7aa6c105a140ad022..4c5c2a7fe5426aa81ba341aba04= 6ef166c51f664 100644 --- a/drivers/media/platform/allegro-dvt/allegro-core.c +++ b/drivers/media/platform/allegro-dvt/allegro-core.c @@ -2599,8 +2599,14 @@ static int allegro_create_channel(struct allegro_cha= nnel *channel) allegro_mcu_send_create_channel(dev, channel); time_left =3D wait_for_completion_timeout(&channel->completion, msecs_to_jiffies(5000)); - if (time_left =3D=3D 0) + if (time_left =3D=3D 0) { + v4l2_warn(&dev->v4l2_dev, + "user %d: timeout while creating channel\n", + channel->user_id); + channel->error =3D -ETIMEDOUT; + } + if (channel->error) goto err; =20 --=20 2.25.1 From nobody Fri Oct 3 11:26:45 2025 Received: from lx20.hoststar.hosting (lx20.hoststar.hosting [168.119.41.54]) (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 18F8C32F772; Mon, 1 Sep 2025 15:43:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=168.119.41.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756741431; cv=none; b=Jyr0h9TgShOYjx177AUs+wwkd09l1kwdDrjkRB7Oflih0kj1kZmD+tUQvMroRqiZlLLqWlrZ9cCFDF/g60EIl2+fXFV87GO3JAEGmyN2UPYYQ97+CwJyJzQ0IPG9m88TSUJEcsPerzoeXIj4VaDQTSYIZ6tV4QDlug5SBuqBUu4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756741431; c=relaxed/simple; bh=inglLfU5gjsaZCpx+FkdLCa8PO3vi3J62HKV+6JkDAk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=odCRAUnAffDv/7abAq5ifGKpit3VIKXCEBNMFJKYn38gT4hvtPJoNdPG72uen9EtYEP2kvxjlwJZRGnWRIAnaNuiVbdnQSFu9ZyXymeYXO/vqG0OLYRJNr3usjbg9aaZ2YQ8IeprfVX489vXUAsLPrh3sgsHLExkPDEgcxzcItU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=emfend.at; spf=pass smtp.mailfrom=emfend.at; dkim=pass (1024-bit key) header.d=emfend.at header.i=@emfend.at header.b=Ms48I5P+; arc=none smtp.client-ip=168.119.41.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=emfend.at Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=emfend.at Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=emfend.at header.i=@emfend.at header.b="Ms48I5P+" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=emfend.at; s=mail; h=Cc:To:In-Reply-To:References:Message-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:Subject:Date:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=ywncx89NF7pFbZJ1GzTfJ9q/DxkEWttcK9TGbAeufeA=; b=Ms48I5P+r9Oa7LE4AIKdyg+6HF DFWTI2HA3sC4nTjqHl6PPXwAcRhipRjcYNGcBj5BU21MITR5IVaY0/ZDT75N/B0FJn9u7h9ZW2pFT waDRgVdUZpSixLM58f1Kwi/QMiLOU6SOhoiZSharq4iJ7CuglPiAcEnJot2M/Y61Swc8=; Received: from 194-208-208-245.tele.net ([194.208.208.245]:61156 helo=[127.0.1.1]) by lx20.hoststar.hosting with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1ut6E3-009YyN-Mb; Mon, 01 Sep 2025 17:13:44 +0200 From: Matthias Fend Date: Mon, 01 Sep 2025 17:13:37 +0200 Subject: [PATCH 2/3] media: allegro: process all pending status mbox messages 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-Transfer-Encoding: quoted-printable Message-Id: <20250901-allegro-dvt-fixes-v1-2-4e4d493836ef@emfend.at> References: <20250901-allegro-dvt-fixes-v1-0-4e4d493836ef@emfend.at> In-Reply-To: <20250901-allegro-dvt-fixes-v1-0-4e4d493836ef@emfend.at> To: Michael Tretter , Pengutronix Kernel Team , Mauro Carvalho Chehab Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Matthias Fend X-Mailer: b4 0.14.2 X-Spam-Score: -1.0 X-Spam-Bar: - X-Spam-Report: Spam detection software, running on the system "lx20.hoststar.hosting", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: This message can be helpful for troubleshooting and there is already a corresponding message in case of a channel destroy timeout. Add a similar message for channel creation. Signed-off-by: Matthias Fend --- drivers/media/platform/allegro-dvt/allegro-core.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) Content analysis details: (-1.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP 0.0 TVD_RCVD_IP Message was received from an IP address 0.0 Local_hs1_NotHoststar Sender is not from hoststar.ch|de|com 0.0 KAM_DMARC_STATUS Test Rule for DKIM or SPF Failure with Strict Alignment Under certain circumstances, not every message written by the MCU to the status mailbox may trigger a corresponding interrupt. This is likely when multiple messages are generated in a very short period of time. Since the current implementation only processes one message per interrupt, even if multiple messages are already available in the mailbox, expected messages are either not received or are processed late. This leads to various subsequent problems and causes the driver to no longer function properly. The behavior has been adjusted so that after an interrupt, all messages available in the mailbox are processed. Signed-off-by: Matthias Fend --- drivers/media/platform/allegro-dvt/allegro-core.c | 42 ++++++++++++++++++-= ---- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/drivers/media/platform/allegro-dvt/allegro-core.c b/drivers/me= dia/platform/allegro-dvt/allegro-core.c index 4c5c2a7fe5426aa81ba341aba046ef166c51f664..d3aea46b7d1d9854307d61d2f16= 47eaa340d504d 100644 --- a/drivers/media/platform/allegro-dvt/allegro-core.c +++ b/drivers/media/platform/allegro-dvt/allegro-core.c @@ -828,6 +828,20 @@ static int allegro_mbox_write(struct allegro_mbox *mbo= x, return err; } =20 +static unsigned int allegro_mbox_get_available(struct allegro_mbox *mbox) +{ + struct regmap *sram =3D mbox->dev->sram; + unsigned int head, tail; + + regmap_read(sram, mbox->head, &head); + regmap_read(sram, mbox->tail, &tail); + + if (tail >=3D head) + return tail - head; + else + return mbox->size - (head - tail); +} + static ssize_t allegro_mbox_read(struct allegro_mbox *mbox, u32 *dst, size_t nbyte) { @@ -836,11 +850,15 @@ static ssize_t allegro_mbox_read(struct allegro_mbox = *mbox, u16 type; } __attribute__ ((__packed__)) *header; struct regmap *sram =3D mbox->dev->sram; - unsigned int head; + unsigned int available, head; ssize_t size; size_t body_no_wrap; int stride =3D regmap_get_reg_stride(sram); =20 + available =3D allegro_mbox_get_available(mbox); + if (available < sizeof(*header)) + return -EAGAIN; + regmap_read(sram, mbox->head, &head); if (head > mbox->size) return -EIO; @@ -854,6 +872,8 @@ static ssize_t allegro_mbox_read(struct allegro_mbox *m= box, return -EIO; if (size > nbyte) return -EINVAL; + if (size > available) + return -EAGAIN; =20 /* * The message might wrap within the mailbox. If the message does not @@ -913,26 +933,27 @@ static int allegro_mbox_send(struct allegro_mbox *mbo= x, void *msg) * allegro_mbox_notify() - Notify the mailbox about a new message * @mbox: The allegro_mbox to notify */ -static void allegro_mbox_notify(struct allegro_mbox *mbox) +static int allegro_mbox_notify(struct allegro_mbox *mbox) { struct allegro_dev *dev =3D mbox->dev; union mcu_msg_response *msg; - ssize_t size; u32 *tmp; int err; =20 msg =3D kmalloc(sizeof(*msg), GFP_KERNEL); if (!msg) - return; + return -ENOMEM; =20 msg->header.version =3D dev->fw_info->mailbox_version; =20 tmp =3D kmalloc(mbox->size, GFP_KERNEL); - if (!tmp) + if (!tmp) { + err =3D -ENOMEM; goto out; + } =20 - size =3D allegro_mbox_read(mbox, tmp, mbox->size); - if (size < 0) + err =3D allegro_mbox_read(mbox, tmp, mbox->size); + if (err < 0) goto out; =20 err =3D allegro_decode_mail(msg, tmp); @@ -944,6 +965,8 @@ static void allegro_mbox_notify(struct allegro_mbox *mb= ox) out: kfree(tmp); kfree(msg); + + return err; } =20 static int allegro_encoder_buffer_init(struct allegro_dev *dev, @@ -2326,7 +2349,10 @@ static irqreturn_t allegro_irq_thread(int irq, void = *data) if (!dev->mbox_status) return IRQ_NONE; =20 - allegro_mbox_notify(dev->mbox_status); + while (allegro_mbox_get_available(dev->mbox_status) > 0) { + if (allegro_mbox_notify(dev->mbox_status)) + break; + } =20 return IRQ_HANDLED; } --=20 2.25.1 From nobody Fri Oct 3 11:26:45 2025 Received: from lx20.hoststar.hosting (lx20.hoststar.hosting [168.119.41.54]) (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 251F419CC37; Mon, 1 Sep 2025 15:43:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=168.119.41.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756741436; cv=none; b=ijcPdSRZj63qev5OlgLSWIbrdJ55QgFRUJlEdtkzal0d4OULCRgtRjxR+i9dy0nTutofH1lWhLNhKRGCMReZBPXWBtOMW85oD7z+UVzapejpxuijmHcYIbEmZ59AS4JkNUpISgCJHolvSDyZBpLj0K+PNbSZLatquG8zFA+jH3M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756741436; c=relaxed/simple; bh=am4bL3m9AU3nNCPWaO6cdfHt55wwlOiuU308mgqRkQ4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rNTMoWBilnFMk6oiaCCEsP763G86Fo3RHjT1k8+qtnk1QvMH4EE1lgMsrTeUIL4CjKFvkKspKLXaxdkUClxmoaPSEdMQJa+Ue2zkOQYnOxzGoiaCa9KwgD1lNpXC/Va0bjZYSn4nk6ZYsdGj05+ZFVF221OQzvU7BLxW6gHlzdE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=emfend.at; spf=pass smtp.mailfrom=emfend.at; dkim=pass (1024-bit key) header.d=emfend.at header.i=@emfend.at header.b=lE5xbrB2; arc=none smtp.client-ip=168.119.41.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=emfend.at Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=emfend.at Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=emfend.at header.i=@emfend.at header.b="lE5xbrB2" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=emfend.at; s=mail; h=Cc:To:In-Reply-To:References:Message-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:Subject:Date:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=wzd0Xe2NufD57luvKvcTjx6X7hryM1+600ih+eNfwKA=; b=lE5xbrB2FQFeNfasHfBCjFAi1B J3n+QDAzSIPOGNCHmxx8ENwSWmkPCvP2IZSjwBukeS9MofF+OvYEV62QeLwA9fbj/NNuHhLDDCDXw sHyGHY0n+Wa6tgwhztjodbluKura3BddKtduJzsqc6NC59bT/C3N61i1dSXGKm3R0ucU=; Received: from 194-208-208-245.tele.net ([194.208.208.245]:61156 helo=[127.0.1.1]) by lx20.hoststar.hosting with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1ut6E4-009YyN-Kz; Mon, 01 Sep 2025 17:13:45 +0200 From: Matthias Fend Date: Mon, 01 Sep 2025 17:13:38 +0200 Subject: [PATCH 3/3] media: allegro: fix race conditions in channel handling 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-Transfer-Encoding: quoted-printable Message-Id: <20250901-allegro-dvt-fixes-v1-3-4e4d493836ef@emfend.at> References: <20250901-allegro-dvt-fixes-v1-0-4e4d493836ef@emfend.at> In-Reply-To: <20250901-allegro-dvt-fixes-v1-0-4e4d493836ef@emfend.at> To: Michael Tretter , Pengutronix Kernel Team , Mauro Carvalho Chehab Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, Matthias Fend X-Mailer: b4 0.14.2 X-Spam-Score: -1.0 X-Spam-Bar: - X-Spam-Report: Spam detection software, running on the system "lx20.hoststar.hosting", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Under certain circumstances, not every message written by the MCU to the status mailbox may trigger a corresponding interrupt. This is likely when multiple messages are generated in a very short perio [...] Content analysis details: (-1.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP 0.0 TVD_RCVD_IP Message was received from an IP address 0.0 Local_hs1_NotHoststar Sender is not from hoststar.ch|de|com 0.0 KAM_DMARC_STATUS Test Rule for DKIM or SPF Failure with Strict Alignment Since the channel list is used in different contexts, it must be ensured that it is always consistent. Also, the channels contained in the list may only be released when they are no longer needed in any context. Add a lock to protect the list and reference handling for the channels. Signed-off-by: Matthias Fend --- drivers/media/platform/allegro-dvt/allegro-core.c | 64 ++++++++++++++++++-= ---- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/drivers/media/platform/allegro-dvt/allegro-core.c b/drivers/me= dia/platform/allegro-dvt/allegro-core.c index d3aea46b7d1d9854307d61d2f1647eaa340d504d..adcaa4f877df1c58807d62ddf3e= b21848fb08520 100644 --- a/drivers/media/platform/allegro-dvt/allegro-core.c +++ b/drivers/media/platform/allegro-dvt/allegro-core.c @@ -177,6 +177,7 @@ struct allegro_dev { */ unsigned long channel_user_ids; struct list_head channels; + struct mutex channels_lock; }; =20 static const struct regmap_config allegro_regmap_config =3D { @@ -200,6 +201,7 @@ static const struct regmap_config allegro_sram_config = =3D { #define fh_to_channel(__fh) container_of(__fh, struct allegro_channel, fh) =20 struct allegro_channel { + struct kref ref; struct allegro_dev *dev; struct v4l2_fh fh; struct v4l2_ctrl_handler ctrl_handler; @@ -427,33 +429,55 @@ static unsigned long allegro_next_user_id(struct alle= gro_dev *dev) } =20 static struct allegro_channel * -allegro_find_channel_by_user_id(struct allegro_dev *dev, - unsigned int user_id) +allegro_ref_get_channel_by_user_id(struct allegro_dev *dev, + unsigned int user_id) { struct allegro_channel *channel; =20 + guard(mutex)(&dev->channels_lock); + list_for_each_entry(channel, &dev->channels, list) { - if (channel->user_id =3D=3D user_id) - return channel; + if (channel->user_id =3D=3D user_id) { + if (kref_get_unless_zero(&channel->ref)) + return channel; + break; + } } =20 return ERR_PTR(-EINVAL); } =20 static struct allegro_channel * -allegro_find_channel_by_channel_id(struct allegro_dev *dev, - unsigned int channel_id) +allegro_ref_get_channel_by_channel_id(struct allegro_dev *dev, + unsigned int channel_id) { struct allegro_channel *channel; =20 + guard(mutex)(&dev->channels_lock); + list_for_each_entry(channel, &dev->channels, list) { - if (channel->mcu_channel_id =3D=3D channel_id) - return channel; + if (channel->mcu_channel_id =3D=3D channel_id) { + if (kref_get_unless_zero(&channel->ref)) + return channel; + break; + } } =20 return ERR_PTR(-EINVAL); } =20 +static void allegro_free_channel(struct kref *ref) +{ + struct allegro_channel *channel =3D container_of(ref, struct allegro_chan= nel, ref); + + kfree(channel); +} + +static int allegro_ref_put_channel(struct allegro_channel *channel) +{ + return kref_put(&channel->ref, allegro_free_channel); +} + static inline bool channel_exists(struct allegro_channel *channel) { return channel->mcu_channel_id !=3D -1; @@ -2183,7 +2207,7 @@ allegro_handle_create_channel(struct allegro_dev *dev, int err =3D 0; struct create_channel_param param; =20 - channel =3D allegro_find_channel_by_user_id(dev, msg->user_id); + channel =3D allegro_ref_get_channel_by_user_id(dev, msg->user_id); if (IS_ERR(channel)) { v4l2_warn(&dev->v4l2_dev, "received %s for unknown user %d\n", @@ -2250,6 +2274,7 @@ allegro_handle_create_channel(struct allegro_dev *dev, out: channel->error =3D err; complete(&channel->completion); + allegro_ref_put_channel(channel); =20 /* Handled successfully, error is passed via channel->error */ return 0; @@ -2261,7 +2286,7 @@ allegro_handle_destroy_channel(struct allegro_dev *de= v, { struct allegro_channel *channel; =20 - channel =3D allegro_find_channel_by_channel_id(dev, msg->channel_id); + channel =3D allegro_ref_get_channel_by_channel_id(dev, msg->channel_id); if (IS_ERR(channel)) { v4l2_err(&dev->v4l2_dev, "received %s for unknown channel %d\n", @@ -2274,6 +2299,7 @@ allegro_handle_destroy_channel(struct allegro_dev *de= v, "user %d: vcu destroyed channel %d\n", channel->user_id, channel->mcu_channel_id); complete(&channel->completion); + allegro_ref_put_channel(channel); =20 return 0; } @@ -2284,7 +2310,7 @@ allegro_handle_encode_frame(struct allegro_dev *dev, { struct allegro_channel *channel; =20 - channel =3D allegro_find_channel_by_channel_id(dev, msg->channel_id); + channel =3D allegro_ref_get_channel_by_channel_id(dev, msg->channel_id); if (IS_ERR(channel)) { v4l2_err(&dev->v4l2_dev, "received %s for unknown channel %d\n", @@ -2294,6 +2320,7 @@ allegro_handle_encode_frame(struct allegro_dev *dev, } =20 allegro_channel_finish_frame(channel, msg); + allegro_ref_put_channel(channel); =20 return 0; } @@ -3079,6 +3106,8 @@ static int allegro_open(struct file *file) if (!channel) return -ENOMEM; =20 + kref_init(&channel->ref); + v4l2_fh_init(&channel->fh, vdev); =20 init_completion(&channel->completion); @@ -3245,7 +3274,10 @@ static int allegro_open(struct file *file) goto error; } =20 - list_add(&channel->list, &dev->channels); + scoped_guard(mutex, &dev->channels_lock) { + list_add(&channel->list, &dev->channels); + } + file->private_data =3D &channel->fh; v4l2_fh_add(&channel->fh); =20 @@ -3262,17 +3294,20 @@ static int allegro_open(struct file *file) static int allegro_release(struct file *file) { struct allegro_channel *channel =3D fh_to_channel(file->private_data); + struct allegro_dev *dev =3D channel->dev; =20 v4l2_m2m_ctx_release(channel->fh.m2m_ctx); =20 - list_del(&channel->list); + scoped_guard(mutex, &dev->channels_lock) { + list_del(&channel->list); + } =20 v4l2_ctrl_handler_free(&channel->ctrl_handler); =20 v4l2_fh_del(&channel->fh); v4l2_fh_exit(&channel->fh); =20 - kfree(channel); + allegro_ref_put_channel(channel); =20 return 0; } @@ -3867,6 +3902,7 @@ static int allegro_probe(struct platform_device *pdev) dev->plat_dev =3D pdev; init_completion(&dev->init_complete); INIT_LIST_HEAD(&dev->channels); + mutex_init(&dev->channels_lock); =20 mutex_init(&dev->lock); =20 --=20 2.25.1