public inbox for linux-hardening@vger.kernel.org
 help / color / mirror / Atom feed
From: Rosen Penev <rosenp@gmail.com>
To: linux-media@vger.kernel.org
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>,
	Kees Cook <kees@kernel.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	linux-kernel@vger.kernel.org (open list),
	linux-hardening@vger.kernel.org (open list:KERNEL HARDENING (not
	covered by other areas):Keyword:\b__counted_by(_le|_be)?\b)
Subject: [PATCH] media: dvb_ca_en50221: simplify ca allocation
Date: Mon,  6 Apr 2026 18:01:40 -0700	[thread overview]
Message-ID: <20260407010140.3229-1-rosenp@gmail.com> (raw)

Use a flexible array member to combine allocations and simplify memory
handling slightly.

Add __counted_by for extra runtime analysis. Move counting variable
assignment after allocation as done by kzalloc_flex for GCC >= 15.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/media/dvb-core/dvb_ca_en50221.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/drivers/media/dvb-core/dvb_ca_en50221.c b/drivers/media/dvb-core/dvb_ca_en50221.c
index 1b91ebb8f667..a792feb076ef 100644
--- a/drivers/media/dvb-core/dvb_ca_en50221.c
+++ b/drivers/media/dvb-core/dvb_ca_en50221.c
@@ -125,9 +125,6 @@ struct dvb_ca_private {
 	/* number of slots supported by this CA interface */
 	unsigned int slot_count;
 
-	/* information on each slot */
-	struct dvb_ca_slot *slot_info;
-
 	/* wait queues for read() and write() operations */
 	wait_queue_head_t wait_queue;
 
@@ -157,6 +154,9 @@ struct dvb_ca_private {
 
 	/* Whether the device is disconnected */
 	int exit;
+
+	/* information on each slot */
+	struct dvb_ca_slot slot_info[] __counted_by(slot_count);
 };
 
 static void dvb_ca_private_free(struct dvb_ca_private *ca)
@@ -167,7 +167,6 @@ static void dvb_ca_private_free(struct dvb_ca_private *ca)
 	for (i = 0; i < ca->slot_count; i++)
 		vfree(ca->slot_info[i].rx_buffer.data);
 
-	kfree(ca->slot_info);
 	kfree(ca);
 }
 
@@ -1880,20 +1879,15 @@ int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
 		return -EINVAL;
 
 	/* initialise the system data */
-	ca = kzalloc_obj(*ca);
+	ca = kzalloc_flex(*ca, slot_info, slot_count);
 	if (!ca) {
 		ret = -ENOMEM;
 		goto exit;
 	}
+	ca->slot_count = slot_count;
 	kref_init(&ca->refcount);
 	ca->pub = pubca;
 	ca->flags = flags;
-	ca->slot_count = slot_count;
-	ca->slot_info = kzalloc_objs(struct dvb_ca_slot, slot_count);
-	if (!ca->slot_info) {
-		ret = -ENOMEM;
-		goto free_ca;
-	}
 	init_waitqueue_head(&ca->wait_queue);
 	ca->open = 0;
 	ca->wakeup = 0;
@@ -1904,7 +1898,7 @@ int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
 	ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca,
 				  DVB_DEVICE_CA, 0);
 	if (ret)
-		goto free_slot_info;
+		goto free_ca;
 
 	/* now initialise each slot */
 	for (i = 0; i < slot_count; i++) {
@@ -1939,8 +1933,6 @@ int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
 
 unregister_device:
 	dvb_unregister_device(ca->dvbdev);
-free_slot_info:
-	kfree(ca->slot_info);
 free_ca:
 	kfree(ca);
 exit:
-- 
2.53.0


                 reply	other threads:[~2026-04-07  1:01 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260407010140.3229-1-rosenp@gmail.com \
    --to=rosenp@gmail.com \
    --cc=gustavoars@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox