public inbox for linux-i2c@vger.kernel.org
 help / color / mirror / Atom feed
From: Rosen Penev <rosenp@gmail.com>
To: linux-i2c@vger.kernel.org
Cc: Andi Shyti <andi.shyti@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] i2c: keba: simplify allocation
Date: Mon, 16 Mar 2026 17:27:17 -0700	[thread overview]
Message-ID: <20260317002717.70829-1-rosenp@gmail.com> (raw)

Use a flexible array member to combine kzalloc and kcalloc into one
allocation.

Use __counted_by for extra runtime analysis. Move the counting variable
assignment to right after allocation as required by __counted_by.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/i2c/busses/i2c-keba.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/drivers/i2c/busses/i2c-keba.c b/drivers/i2c/busses/i2c-keba.c
index 9420c8b342b5..23ae8a609008 100644
--- a/drivers/i2c/busses/i2c-keba.c
+++ b/drivers/i2c/busses/i2c-keba.c
@@ -51,8 +51,8 @@ struct ki2c {
 	void __iomem *base;
 	struct i2c_adapter adapter;
 
-	struct i2c_client **client;
 	int client_size;
+	struct i2c_client *client[] __counted_by(client_size);
 };
 
 static int ki2c_inuse_lock(struct ki2c *ki2c)
@@ -508,19 +508,18 @@ static int ki2c_probe(struct auxiliary_device *auxdev,
 		      const struct auxiliary_device_id *id)
 {
 	struct device *dev = &auxdev->dev;
+	struct keba_i2c_auxdev *adev;
 	struct i2c_adapter *adap;
 	struct ki2c *ki2c;
 	int ret;
 
-	ki2c = devm_kzalloc(dev, sizeof(*ki2c), GFP_KERNEL);
+	adev = container_of(auxdev, struct keba_i2c_auxdev, auxdev);
+	ki2c = devm_kzalloc(dev, struct_size(ki2c, client, adev->info_size), GFP_KERNEL);
 	if (!ki2c)
 		return -ENOMEM;
-	ki2c->auxdev = container_of(auxdev, struct keba_i2c_auxdev, auxdev);
-	ki2c->client = devm_kcalloc(dev, ki2c->auxdev->info_size,
-				    sizeof(*ki2c->client), GFP_KERNEL);
-	if (!ki2c->client)
-		return -ENOMEM;
-	ki2c->client_size = ki2c->auxdev->info_size;
+
+	ki2c->client_size = adev->info_size;
+	ki2c->auxdev = adev;
 	auxiliary_set_drvdata(auxdev, ki2c);
 
 	ki2c->base = devm_ioremap_resource(dev, &ki2c->auxdev->io);
-- 
2.53.0


                 reply	other threads:[~2026-03-17  0:27 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=20260317002717.70829-1-rosenp@gmail.com \
    --to=rosenp@gmail.com \
    --cc=andi.shyti@kernel.org \
    --cc=gustavoars@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.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