Skip to contentSkip to conversation

mailbox: hi6220: kzalloc + kcalloc to kzalloc

PATCH Applied Rosen Penev 2 participants 2 messages Mar 11 – Mar 29 Mailbox API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mailbox: hi6220: kzalloc + kcalloc to kzalloc

Reduce allocations to a single one by using a flexible array member.

Allows using __counted_by for extra runtime analysis.

---
 drivers/mailbox/hi6220-mailbox.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/mailbox/hi6220-mailbox.c +5 -9
index f77741ce42e7..69d15b6283e9 100644
@@ -79,12 +79,12 @@ struct hi6220_mbox {
 	/* region for mailbox */
 	void __iomem *base;
 
-	unsigned int chan_num;
-	struct hi6220_mbox_chan *mchan;
-
 	void *irq_map_chan[MBOX_CHAN_MAX];
 	struct mbox_chan *chan;
 	struct mbox_controller controller;
+
+	unsigned int chan_num;
+	struct hi6220_mbox_chan mchan[] __counted_by(chan_num);
 };
 
 static void mbox_set_state(struct hi6220_mbox *mbox,
@@ -267,16 +267,12 @@ static int hi6220_mbox_probe(struct platform_device *pdev)
 	struct hi6220_mbox *mbox;
 	int i, err;
 
-	mbox = devm_kzalloc(dev, sizeof(*mbox), GFP_KERNEL);
+	mbox = devm_kzalloc(dev, struct_size(mbox, mchan, MBOX_CHAN_MAX), GFP_KERNEL);
 	if (!mbox)
 		return -ENOMEM;
 
-	mbox->dev = dev;
 	mbox->chan_num = MBOX_CHAN_MAX;
-	mbox->mchan = devm_kcalloc(dev,
-		mbox->chan_num, sizeof(*mbox->mchan), GFP_KERNEL);
-	if (!mbox->mchan)
-		return -ENOMEM;
+	mbox->dev = dev;
 
 	mbox->chan = devm_kcalloc(dev,
 		mbox->chan_num, sizeof(*mbox->chan), GFP_KERNEL);

--
2.53.0

maintainer Applied
#1 lore raw
Quoted 50 lines · Rosen Penev

Reduce allocations to a single one by using a flexible array member.

Allows using __counted_by for extra runtime analysis.

---
 drivers/mailbox/hi6220-mailbox.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/mailbox/hi6220-mailbox.c +5 -10
index f77741ce42e7..69d15b6283e9 100644
@@ -79,12 +79,12 @@ struct hi6220_mbox {
        /* region for mailbox */
        void __iomem *base;
 
-       unsigned int chan_num;
-       struct hi6220_mbox_chan *mchan;
-
        void *irq_map_chan[MBOX_CHAN_MAX];
        struct mbox_chan *chan;
        struct mbox_controller controller;
+
+       unsigned int chan_num;
+       struct hi6220_mbox_chan mchan[] __counted_by(chan_num);
 };
 
 static void mbox_set_state(struct hi6220_mbox *mbox,
@@ -267,16 +267,12 @@ static int hi6220_mbox_probe(struct platform_device *pdev)
        struct hi6220_mbox *mbox;
        int i, err;
 
-       mbox = devm_kzalloc(dev, sizeof(*mbox), GFP_KERNEL);
+       mbox = devm_kzalloc(dev, struct_size(mbox, mchan, MBOX_CHAN_MAX), GFP_KERNEL);
        if (!mbox)
                return -ENOMEM;
 
-       mbox->dev = dev;
        mbox->chan_num = MBOX_CHAN_MAX;
-       mbox->mchan = devm_kcalloc(dev,
-               mbox->chan_num, sizeof(*mbox->mchan), GFP_KERNEL);
-       if (!mbox->mchan)
-               return -ENOMEM;
+       mbox->dev = dev;
 
        mbox->chan = devm_kcalloc(dev,
                mbox->chan_num, sizeof(*mbox->chan), GFP_KERNEL);
--
Quoted 2 lines

2.53.0

Applied to mailbox/for-next
Thanks
Jassi