From nobody Sat Apr 4 01:47:40 2026 Received: from mail-pj1-f54.google.com (mail-pj1-f54.google.com [209.85.216.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA1452E11C7 for ; Sat, 21 Mar 2026 03:52:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774065157; cv=none; b=R84CkchtvyJEktu3sw1miQh3pgjQ19c6INeHf10mmSlI9uGgJrF8r0rQVq9h1/Fe+2UOLtheeQBF9s1oaOFF5w9ZWmFEw8YDTn5+K+Nby2Okh9hMk4xS/MtG/TiAEPLJKA9ZOZ6dT0X40Ph4XtSZZSvfvOkr3Td33k1IwOxR994= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774065157; c=relaxed/simple; bh=+WUaYnyz29v3mRwazoJ/Ikq6XTg5VutX8hbCpJmgTVs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=r4NiOFN0w9NEU+OD8cyR8TwKtP/vqneK24+1/Wtuq8ZcRxEBFnmU+vLO3RknPWD7wlrzfV9YnIDwBkL3tiRRe0ja9+3ySTQqliiBKWwemn6/463ll2DgnU0j81t/rds9yqo8jvKPV6OItMo/3hQtR7Zk7anHb0G4iQ6um5rP5yY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=K3hkGdRn; arc=none smtp.client-ip=209.85.216.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="K3hkGdRn" Received: by mail-pj1-f54.google.com with SMTP id 98e67ed59e1d1-35a02f3b8feso1147745a91.3 for ; Fri, 20 Mar 2026 20:52:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1774065155; x=1774669955; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=HcJnW6kg+/NIdI6Vl3b6KcDbliX1oc7SiSAekESdtOc=; b=K3hkGdRnJIz1U1SLueHKesg9b+IYY7A/chI9q2bh4he9vAOcOPRxNk6keznA9S4edh 4YAiMTaUHkqbiS7M4SJvOk8fThsaVFH2erA8+DAli6vUBJT9ug5+ZgSpAyb7Z+35wLIr DvSDRqOrV8nsVB2TH5BKaGHSNKcZ8y6V7PoGYCSqgzYrrZbcqW9Y0GDwM482r7lwBqcu 14fhx0ovebAFVRxqWJ6eqyJ9+hBtxFPbHlU1OE59AfBodU0Ly2J7QOzP3lRIF3TkCXnC xmsIA754IHmKtguavv5lYrWdPaG6+08UZclH96/MGzB+awoCEhyh1337PS+0LZuH6zPG g5FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774065155; x=1774669955; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=HcJnW6kg+/NIdI6Vl3b6KcDbliX1oc7SiSAekESdtOc=; b=Ru0N9ymtdJ16E82gTcVpyrlUdmYzNvtHnqGuCCddVsTJUIezH+oHLPoLuSnPN2HTv9 srgbCqIRVXHdHOtFrWDxhltB9Dt3w+CjN8F9eswvloiJBATa7yPdmYHtok0t7+fueGzw joRm1RfI4nOsIPr0QZuD/zOpyIHCABW6jLWiyle4WqqXOc8bh6qTqrbiGuvLZXLOMEKT usYQa0SkmC3/BpGOg/xBqLyNOBpNT6Kqy9bzBvnPRKH5QuYrOSHV8dY+KWDWcuvtNVzZ 1mhDiwsdzajJB1yDv6PwrKvJxj1Qk2V30yccXFPs4L9srdK5q/2WHob1FAZ2W+dGZ0Qa 0spg== X-Gm-Message-State: AOJu0YzzpTCW1Nh6/K5CTeL9Ih/S5rIdQcwk6Z4gI0LXjMhuL42h3kPY EFIWwiH9d7wC1AXvUKQCOCcyPtedDzASDpqTT82ckbtx7rwIJMCcUeq7U1U+L8sh X-Gm-Gg: ATEYQzz84ajgklhWx1xfQ868YSLdmwPFtE3fpi5UWqgQvmFTDi26vQ6DXwLlkyX2IPB Px8zMO6lTS44xsJ1BsEgT/vgmVUDAnjew7Pcx0GSlWGGX7OXyMueAzC9VdZCo5jNjxPW3fq3fMw lrvipvRA/GABUls9oCbSXu4zoZqnkha41iLE0PmhDcjLqL5ViXc7UG8Nn3GRDReE2tMJ49Vpfh0 hkuh6sZrpejsVpqL+3JjdWQ6k2tJfcRO+TK/4JcDDgkwf4ouDEnZcfdPyT3KajcoKBaGUx9kTR/ oO0Y2tNZxiNaRGaU3xjUxFTSDSZlMlBfd29OITLuirVqxrAoElaeZKO+idUt7srZPklbA4kUNVG W+Xl9ucxeCC76NwwNhyjuQNPKR6t6fkzn+JysE8oxshClyMjGa3s3oCRmJRPpjkt7fhPeQDeSrw vxO7PqyYVEws38e5n0LtD2RldG3OHAFZoZwN+7Oh1czXGbtJm6JWgiFW6nPCqLHrS5Dg== X-Received: by 2002:a17:90b:3c48:b0:359:855f:ff96 with SMTP id 98e67ed59e1d1-35bd2cb73f1mr3873362a91.17.1774065154813; Fri, 20 Mar 2026 20:52:34 -0700 (PDT) Received: from ryzen ([2601:644:8000:5b5d::8bd]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-35bd3eb4bf5sm3363083a91.4.2026.03.20.20.52.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Mar 2026 20:52:34 -0700 (PDT) From: Rosen Penev To: linux-kernel@vger.kernel.org Cc: Dinh Nguyen , Kees Cook , "Gustavo A. R. Silva" , linux-hardening@vger.kernel.org (open list:KERNEL HARDENING (not covered by other areas):Keyword:\b__counted_by(_le|_be)?\b) Subject: [PATCHv2] firmware: stratix10-svc: kmalloc_array + kzalloc to flex Date: Fri, 20 Mar 2026 20:52:17 -0700 Message-ID: <20260321035217.10180-1-rosenp@gmail.com> X-Mailer: git-send-email 2.53.0 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" 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 --- v2: move stratix10_svc_chan to provide a full definition as needed by flexible array members. 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); }; +/** + * 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); }; 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); /* 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; } - 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); 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); } 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); -- 2.53.0