From nobody Thu Apr 2 02:40:51 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 2E98E30F547 for ; Mon, 30 Mar 2026 11:51:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774871473; cv=none; b=gV1PCRMC7RkWhc7Nxuey4jyfHriYakHgK0JhcEqx2ysIT4arjEq8IpL+y9lkMRCVL1Y+kgwR7fb7+/G+SjSOMm8SQ6Y6URRwW42i8IBz1EC4R8OkoJiPRKFduAd9TKencqscRJfLRuLsUd3Wvp9MU4DdedV9tU8tN6MvcKIm5Q8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774871473; c=relaxed/simple; bh=G31nE7zI4SzUheJLunq7rAzy+Jf06biujkagTr4CBH8=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=tXzBTCBEcJaHK19HGtd3HoMgR4AMZrBRmtSXOCC9QUuVW3qR6o31b9M6S6ZflYC+7MSw/QgguaBfyZVrkg+qu5GBjoeaO00H5H1/S9DLXQ2SIo0fkDEH950wTdvg/dWYM6EuhLUJbw2nP7bww5brRD3DUMITzVQm72Jk90iW25M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qQEbrSaQ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qQEbrSaQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 517F2C4CEF7; Mon, 30 Mar 2026 11:51:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774871472; bh=G31nE7zI4SzUheJLunq7rAzy+Jf06biujkagTr4CBH8=; h=From:To:Cc:Subject:Date:From; b=qQEbrSaQEzxPYTX60d+fHk5IzOT7oIAso53EGgWoqmn4hlVUhylywqanQV7T5NpvX A/7i4GmsAJLEVb6ezAeeS1mmJ7FO3h6vwsvn9iyAj0+Qn7Q2j8RFd2kUVdMRv+vAJj lp9GmltCYz6L2CsphgR0E/gnTOG4IDpRUExrddMX8yTXfV6WeHoGFCfiT0jwwutl1n 9kpicw0ZgUPAZTMpkNf/U77uQKzxKY9z7mh2Ryyf7WZEnHmDOanIRAelZ3UrdEeL3X MDeXO1DdI8QW7XkQbkFxB+Aegw4G9BR/QLoWOUByfXeYXSjH54253lSQgA68hsrke2 CI5CgFX4nA0Mw== From: Dinh Nguyen To: gregkh@linuxfoundation.org Cc: dinguyen@kernel.org, linux-kernel@vger.kernel.org, Rosen Penev Subject: [PATCH] firmware: stratix10-svc: kmalloc_array + kzalloc to flex Date: Mon, 30 Mar 2026 06:51:01 -0500 Message-ID: <20260330115101.241784-1-dinguyen@kernel.org> X-Mailer: git-send-email 2.42.0.411.g813d9a9188 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Rosen Penev Use a flexible array member to combine allocations. Simplifies memory management. Add __counted_by for extra runtime analysis. Also move counting variable assignment up as required by __counted_by. Signed-off-by: Rosen Penev Signed-off-by: Dinh Nguyen --- drivers/firmware/stratix10-svc.c | 82 ++++++++++++++------------------ 1 file changed, 37 insertions(+), 45 deletions(-) diff --git a/drivers/firmware/stratix10-svc.c b/drivers/firmware/stratix10-= svc.c index e9e35d67ef96..5a76cf3fc83a 100644 --- a/drivers/firmware/stratix10-svc.c +++ b/drivers/firmware/stratix10-svc.c @@ -240,10 +240,34 @@ struct stratix10_async_ctrl { DECLARE_HASHTABLE(trx_list, ASYNC_TRX_HASH_BITS); }; =20 +/** + * struct stratix10_svc_chan - service communication channel + * @ctrl: pointer to service controller which is the provider of this chan= nel + * @scl: pointer to service client which owns the channel + * @name: service client name associated with the channel + * @task: pointer to the thread task which handles SMC or HVC call + * @svc_fifo: a queue for storing service message data (separate fifo for = every channel) + * @svc_fifo_lock: protect access to service message data queue (locking p= ending fifo) + * @lock: protect access to the channel + * @async_chan: reference to asynchronous channel object for this channel + * + * This struct is used by service client to communicate with service layer. + * Each service client has its own channel created by service controller. + */ +struct stratix10_svc_chan { + struct stratix10_svc_controller *ctrl; + struct stratix10_svc_client *scl; + char *name; + struct task_struct *task; + struct kfifo svc_fifo; + spinlock_t svc_fifo_lock; + spinlock_t lock; + struct stratix10_async_chan *async_chan; +}; + /** * struct stratix10_svc_controller - service controller * @dev: device - * @chans: array of service channels * @num_chans: number of channels in 'chans' array * @num_active_client: number of active service client * @node: list management @@ -253,13 +277,13 @@ struct stratix10_async_ctrl { * @svc: manages the list of client svc drivers * @sdm_lock: only allows a single command single response to SDM * @actrl: async control structure + * @chans: array of service channels * * This struct is used to create communication channels for service client= s, to * handle secure monitor or hypervisor call. */ struct stratix10_svc_controller { struct device *dev; - struct stratix10_svc_chan *chans; int num_chans; int num_active_client; struct list_head node; @@ -269,31 +293,7 @@ struct stratix10_svc_controller { struct stratix10_svc *svc; struct mutex sdm_lock; struct stratix10_async_ctrl actrl; -}; - -/** - * struct stratix10_svc_chan - service communication channel - * @ctrl: pointer to service controller which is the provider of this chan= nel - * @scl: pointer to service client which owns the channel - * @name: service client name associated with the channel - * @task: pointer to the thread task which handles SMC or HVC call - * @svc_fifo: a queue for storing service message data (separate fifo for = every channel) - * @svc_fifo_lock: protect access to service message data queue (locking p= ending fifo) - * @lock: protect access to the channel - * @async_chan: reference to asynchronous channel object for this channel - * - * This struct is used by service client to communicate with service layer. - * Each service client has its own channel created by service controller. - */ -struct stratix10_svc_chan { - struct stratix10_svc_controller *ctrl; - struct stratix10_svc_client *scl; - char *name; - struct task_struct *task; - struct kfifo svc_fifo; - spinlock_t svc_fifo_lock; - spinlock_t lock; - struct stratix10_async_chan *async_chan; + struct stratix10_svc_chan chans[] __counted_by(num_chans); }; =20 static LIST_HEAD(svc_ctrl); @@ -1901,7 +1901,6 @@ static int stratix10_svc_drv_probe(struct platform_de= vice *pdev) { struct device *dev =3D &pdev->dev; struct stratix10_svc_controller *controller; - struct stratix10_svc_chan *chans; struct gen_pool *genpool; struct stratix10_svc_sh_memory *sh_memory; struct stratix10_svc *svc =3D NULL; @@ -1929,23 +1928,16 @@ static int stratix10_svc_drv_probe(struct platform_= device *pdev) return PTR_ERR(genpool); =20 /* allocate service controller and supporting channel */ - controller =3D devm_kzalloc(dev, sizeof(*controller), GFP_KERNEL); + controller =3D devm_kzalloc(dev, struct_size(controller, chans, SVC_NUM_C= HANNEL), + GFP_KERNEL); if (!controller) { ret =3D -ENOMEM; goto err_destroy_pool; } =20 - chans =3D devm_kmalloc_array(dev, SVC_NUM_CHANNEL, - sizeof(*chans), GFP_KERNEL | __GFP_ZERO); - if (!chans) { - ret =3D -ENOMEM; - goto err_destroy_pool; - } - - controller->dev =3D dev; controller->num_chans =3D SVC_NUM_CHANNEL; + controller->dev =3D dev; controller->num_active_client =3D 0; - controller->chans =3D chans; controller->genpool =3D genpool; controller->invoke_fn =3D invoke_fn; INIT_LIST_HEAD(&controller->node); @@ -1962,16 +1954,16 @@ static int stratix10_svc_drv_probe(struct platform_= device *pdev) mutex_init(&controller->sdm_lock); =20 for (i =3D 0; i < SVC_NUM_CHANNEL; i++) { - chans[i].scl =3D NULL; - chans[i].ctrl =3D controller; - chans[i].name =3D (char *)chan_names[i]; - spin_lock_init(&chans[i].lock); - ret =3D kfifo_alloc(&chans[i].svc_fifo, fifo_size, GFP_KERNEL); + controller->chans[i].scl =3D NULL; + controller->chans[i].ctrl =3D controller; + controller->chans[i].name =3D (char *)chan_names[i]; + spin_lock_init(&controller->chans[i].lock); + ret =3D kfifo_alloc(&controller->chans[i].svc_fifo, fifo_size, GFP_KERNE= L); if (ret) { dev_err(dev, "failed to allocate FIFO %d\n", i); goto err_free_fifos; } - spin_lock_init(&chans[i].svc_fifo_lock); + spin_lock_init(&controller->chans[i].svc_fifo_lock); } =20 list_add_tail(&controller->node, &svc_ctrl); @@ -2015,7 +2007,7 @@ static int stratix10_svc_drv_probe(struct platform_de= vice *pdev) list_del(&controller->node); /* free only the FIFOs that were successfully allocated */ while (i--) - kfifo_free(&chans[i].svc_fifo); + kfifo_free(&controller->chans[i].svc_fifo); stratix10_svc_async_exit(controller); err_destroy_pool: gen_pool_destroy(genpool); --=20 2.42.0.411.g813d9a9188