MIPS: lantiq: check the return value of kzalloc()
authorXiaoke Wang <xkernel.wang@foxmail.com>
Fri, 25 Mar 2022 11:49:41 +0000 (19:49 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 25 May 2022 06:41:18 +0000 (08:41 +0200)
[ Upstream commit 34123208bbcc8c884a0489f543a23fe9eebb5514 ]

kzalloc() is a memory allocation function which can return NULL when
some internal memory errors happen. So it is better to check the
return value of it to prevent potential wrong memory access or
memory leak.

Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
arch/mips/lantiq/falcon/sysctrl.c
arch/mips/lantiq/xway/gptu.c
arch/mips/lantiq/xway/sysctrl.c

index 82bbd0e2e298fa504f3f1995cf83aee8b7746569..714d9265948973ea312a2629812a7d2b9006d4c0 100644 (file)
@@ -169,6 +169,8 @@ static inline void clkdev_add_sys(const char *dev, unsigned int module,
 {
        struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
 
+       if (!clk)
+               return;
        clk->cl.dev_id = dev;
        clk->cl.con_id = NULL;
        clk->cl.clk = clk;
index e304aabd6678af46ab94dac67bcec2bf85f1a5ae..7d4081d67d61cf6ff9e85ba012ea04287f83a9b1 100644 (file)
@@ -124,6 +124,8 @@ static inline void clkdev_add_gptu(struct device *dev, const char *con,
 {
        struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
 
+       if (!clk)
+               return;
        clk->cl.dev_id = dev_name(dev);
        clk->cl.con_id = con;
        clk->cl.clk = clk;
index c05bed624075444d595a2bfc69eade545e946aba..1b1142c7bb859cdc3094e1220c401bca7a2f9107 100644 (file)
@@ -313,6 +313,8 @@ static void clkdev_add_pmu(const char *dev, const char *con, bool deactivate,
 {
        struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
 
+       if (!clk)
+               return;
        clk->cl.dev_id = dev;
        clk->cl.con_id = con;
        clk->cl.clk = clk;
@@ -336,6 +338,8 @@ static void clkdev_add_cgu(const char *dev, const char *con,
 {
        struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
 
+       if (!clk)
+               return;
        clk->cl.dev_id = dev;
        clk->cl.con_id = con;
        clk->cl.clk = clk;
@@ -354,24 +358,28 @@ static void clkdev_add_pci(void)
        struct clk *clk_ext = kzalloc(sizeof(struct clk), GFP_KERNEL);
 
        /* main pci clock */
-       clk->cl.dev_id = "17000000.pci";
-       clk->cl.con_id = NULL;
-       clk->cl.clk = clk;
-       clk->rate = CLOCK_33M;
-       clk->rates = valid_pci_rates;
-       clk->enable = pci_enable;
-       clk->disable = pmu_disable;
-       clk->module = 0;
-       clk->bits = PMU_PCI;
-       clkdev_add(&clk->cl);
+       if (clk) {
+               clk->cl.dev_id = "17000000.pci";
+               clk->cl.con_id = NULL;
+               clk->cl.clk = clk;
+               clk->rate = CLOCK_33M;
+               clk->rates = valid_pci_rates;
+               clk->enable = pci_enable;
+               clk->disable = pmu_disable;
+               clk->module = 0;
+               clk->bits = PMU_PCI;
+               clkdev_add(&clk->cl);
+       }
 
        /* use internal/external bus clock */
-       clk_ext->cl.dev_id = "17000000.pci";
-       clk_ext->cl.con_id = "external";
-       clk_ext->cl.clk = clk_ext;
-       clk_ext->enable = pci_ext_enable;
-       clk_ext->disable = pci_ext_disable;
-       clkdev_add(&clk_ext->cl);
+       if (clk_ext) {
+               clk_ext->cl.dev_id = "17000000.pci";
+               clk_ext->cl.con_id = "external";
+               clk_ext->cl.clk = clk_ext;
+               clk_ext->enable = pci_ext_enable;
+               clk_ext->disable = pci_ext_disable;
+               clkdev_add(&clk_ext->cl);
+       }
 }
 
 /* xway socs can generate clocks on gpio pins */
@@ -391,9 +399,15 @@ static void clkdev_add_clkout(void)
                char *name;
 
                name = kzalloc(sizeof("clkout0"), GFP_KERNEL);
+               if (!name)
+                       continue;
                sprintf(name, "clkout%d", i);
 
                clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
+               if (!clk) {
+                       kfree(name);
+                       continue;
+               }
                clk->cl.dev_id = "1f103000.cgu";
                clk->cl.con_id = name;
                clk->cl.clk = clk;