[PATCH] i2c: keba: simplify allocation

Rosen Penev posted 1 patch 2 weeks, 6 days ago
drivers/i2c/busses/i2c-keba.c | 15 +++++++--------
1 file changed, 7 insertions(+), 8 deletions(-)
[PATCH] i2c: keba: simplify allocation
Posted by Rosen Penev 2 weeks, 6 days ago
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