Provide a helper that replaces the kzalloc with devm_kzalloc so error
handling gets easier.
Signed-off-by: Markus Schneider-Pargmann <msp@baylibre.com>
---
Notes:
Changes in v3:
- New patch
drivers/clk/mediatek/clk-mtk.c | 33 ++++++++++++++++++++++++++++-----
drivers/clk/mediatek/clk-mtk.h | 2 ++
2 files changed, 30 insertions(+), 5 deletions(-)
diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
index 05a188c62119..ef4c29422bbb 100644
--- a/drivers/clk/mediatek/clk-mtk.c
+++ b/drivers/clk/mediatek/clk-mtk.c
@@ -18,19 +18,42 @@
#include "clk-mtk.h"
#include "clk-gate.h"
-struct clk_hw_onecell_data *mtk_alloc_clk_data(unsigned int clk_num)
+static void mtk_init_clk_data(struct clk_hw_onecell_data *clk_data,
+ unsigned int clk_num)
{
int i;
+
+ clk_data->num = clk_num;
+
+ for (i = 0; i < clk_num; i++)
+ clk_data->hws[i] = ERR_PTR(-ENOENT);
+}
+
+struct clk_hw_onecell_data *mtk_devm_alloc_clk_data(struct device *dev,
+ unsigned int clk_num)
+{
struct clk_hw_onecell_data *clk_data;
- clk_data = kzalloc(struct_size(clk_data, hws, clk_num), GFP_KERNEL);
+ clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, clk_num),
+ GFP_KERNEL);
if (!clk_data)
return NULL;
- clk_data->num = clk_num;
+ mtk_init_clk_data(clk_data, clk_num);
- for (i = 0; i < clk_num; i++)
- clk_data->hws[i] = ERR_PTR(-ENOENT);
+ return clk_data;
+}
+EXPORT_SYMBOL_GPL(mtk_devm_alloc_clk_data);
+
+struct clk_hw_onecell_data *mtk_alloc_clk_data(unsigned int clk_num)
+{
+ struct clk_hw_onecell_data *clk_data;
+
+ clk_data = kzalloc(struct_size(clk_data, hws, clk_num), GFP_KERNEL);
+ if (!clk_data)
+ return NULL;
+
+ mtk_init_clk_data(clk_data, clk_num);
return clk_data;
}
diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
index 1b95c484d5aa..190fe66ae79f 100644
--- a/drivers/clk/mediatek/clk-mtk.h
+++ b/drivers/clk/mediatek/clk-mtk.h
@@ -184,6 +184,8 @@ void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
struct clk_hw_onecell_data *clk_data);
struct clk_hw_onecell_data *mtk_alloc_clk_data(unsigned int clk_num);
+struct clk_hw_onecell_data *mtk_devm_alloc_clk_data(struct device *dev,
+ unsigned int clk_num);
void mtk_free_clk_data(struct clk_hw_onecell_data *clk_data);
struct clk_hw *mtk_clk_register_ref2usb_tx(const char *name,
--
2.37.2
Quoting Markus Schneider-Pargmann (2022-08-22 08:26:50) > Provide a helper that replaces the kzalloc with devm_kzalloc so error > handling gets easier. > > Signed-off-by: Markus Schneider-Pargmann <msp@baylibre.com> > --- Applied to clk-next
Hi Markus, Thank you for the patch! Perhaps something to improve: [auto build test WARNING on clk/clk-next] [also build test WARNING on robh/for-next linus/master v6.0-rc2 next-20220822] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Markus-Schneider-Pargmann/clk-mediatek-Add-mt8365-support/20220822-233030 base: https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git clk-next config: loongarch-randconfig-c004-20220821 (https://download.01.org/0day-ci/archive/20220823/202208230714.8DNW6JjZ-lkp@intel.com/config) compiler: loongarch64-linux-gcc (GCC) 12.1.0 If you fix the issue, kindly add following tag where applicable Reported-by: kernel test robot <lkp@intel.com> cocci warnings: (new ones prefixed by >>) >> drivers/clk/mediatek/clk-mtk.c:64:1-6: WARNING: invalid free of devm_ allocated data vim +64 drivers/clk/mediatek/clk-mtk.c 9741b1a68035b54 James Liao 2015-04-23 61 609cc5e1a82394e Chen-Yu Tsai 2022-05-19 62 void mtk_free_clk_data(struct clk_hw_onecell_data *clk_data) 300796cad22153f Chun-Jie Chen 2021-09-14 63 { 300796cad22153f Chun-Jie Chen 2021-09-14 @64 kfree(clk_data); 300796cad22153f Chun-Jie Chen 2021-09-14 65 } 609cc5e1a82394e Chen-Yu Tsai 2022-05-19 66 EXPORT_SYMBOL_GPL(mtk_free_clk_data); 300796cad22153f Chun-Jie Chen 2021-09-14 67 -- 0-DAY CI Kernel Test Service https://01.org/lkp
Hi, On Tue, Aug 23, 2022 at 07:44:26AM +0800, kernel test robot wrote: > Hi Markus, > > Thank you for the patch! Perhaps something to improve: > > [auto build test WARNING on clk/clk-next] > [also build test WARNING on robh/for-next linus/master v6.0-rc2 next-20220822] > [If your patch is applied to the wrong git tree, kindly drop us a note. > And when submitting patch, we suggest to use '--base' as documented in > https://git-scm.com/docs/git-format-patch#_base_tree_information] > > url: https://github.com/intel-lab-lkp/linux/commits/Markus-Schneider-Pargmann/clk-mediatek-Add-mt8365-support/20220822-233030 > base: https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git clk-next > config: loongarch-randconfig-c004-20220821 (https://download.01.org/0day-ci/archive/20220823/202208230714.8DNW6JjZ-lkp@intel.com/config) > compiler: loongarch64-linux-gcc (GCC) 12.1.0 > > If you fix the issue, kindly add following tag where applicable > Reported-by: kernel test robot <lkp@intel.com> > > cocci warnings: (new ones prefixed by >>) > >> drivers/clk/mediatek/clk-mtk.c:64:1-6: WARNING: invalid free of devm_ allocated data > > vim +64 drivers/clk/mediatek/clk-mtk.c > > 9741b1a68035b54 James Liao 2015-04-23 61 > 609cc5e1a82394e Chen-Yu Tsai 2022-05-19 62 void mtk_free_clk_data(struct clk_hw_onecell_data *clk_data) > 300796cad22153f Chun-Jie Chen 2021-09-14 63 { > 300796cad22153f Chun-Jie Chen 2021-09-14 @64 kfree(clk_data); > 300796cad22153f Chun-Jie Chen 2021-09-14 65 } > 609cc5e1a82394e Chen-Yu Tsai 2022-05-19 66 EXPORT_SYMBOL_GPL(mtk_free_clk_data); I don't see how this should be called with clk_data being initialized by devm_*. Maybe I am bit code blind. Do you have an actual call stack how this is supposed to happen? Also I wasn't able to get the same warning with coccinelle (different compiler). Best, Markus
© 2016 - 2024 Red Hat, Inc.