Change return type of of_clk_get_parent_count() to unsigned
Subsystems (9)
cover Change return type of of_clk_get_parent_count() to unsigned
Russell King recently pointed out that of_clk_get_parent_count() can return a negative number if the "clocks" property isn't present in a node. Upon investigating all the callers of this API, none of the them seem to care about that case, and furthermore some callers are assigning the return value to an unsigned int which could cause very large numbers of parents to be used.
This series changes the API to return an unsigned int from 0 (no parents) to N, thereby avoiding any problems with negative return values.
Stephen Boyd (9):
clk: Make of_clk_get_parent_count() return unsigned ints
clk: at91: Remove impossible checks for of_clk_get_parent_count()
clk: gpio: Remove impossible check for of_clk_get_parent_count() < 0
clk: h8300: Remove impossible check for of_clk_get_parent_count()
clk: st: Remove impossible check for of_clk_get_parent_count() < 0
clk: sunxi: Use proper type for of_clk_get_parent_count() return value
clk: ti: Update for of_clk_get_parent_count() returning unsigned int
usb: dwc3: Remove impossible check for of_clk_get_parent_count() < 0
simplefb: Remove impossible check for of_clk_get_parent_count() < 0
drivers/clk/at91/clk-generated.c | 4 ++-- drivers/clk/at91/clk-main.c | 4 ++-- drivers/clk/at91/clk-master.c | 4 ++-- drivers/clk/at91/clk-programmable.c | 4 ++-- drivers/clk/at91/clk-slow.c | 6 +++--- drivers/clk/at91/clk-smd.c | 4 ++-- drivers/clk/at91/clk-usb.c | 4 ++-- drivers/clk/clk-gpio.c | 6 ++---- drivers/clk/clk.c | 16 ++++++++++++++-- drivers/clk/h8300/clk-div.c | 4 ++-- drivers/clk/h8300/clk-h8s2678.c | 4 ++-- drivers/clk/st/clk-flexgen.c | 4 ++-- drivers/clk/st/clkgen-mux.c | 4 ++-- drivers/clk/sunxi/clk-sun6i-ar100.c | 2 +- drivers/clk/ti/clockdomain.c | 2 +- drivers/clk/ti/composite.c | 8 ++++---- drivers/clk/ti/dpll.c | 2 +- drivers/clk/ti/mux.c | 4 ++-- drivers/usb/dwc3/dwc3-of-simple.c | 9 +++++---- drivers/video/fbdev/simplefb.c | 4 ++-- include/linux/clk-provider.h | 2 +- 21 files changed, 56 insertions(+), 45 deletions(-)
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
PATCH 1/9 clk: Make of_clk_get_parent_count() return unsigned ints
Russell King recently pointed out a bug in the clk-gpio code where it fails to register the clk if of_clk_get_parent_count() returns an error because the "clocks" property isn't present in the DT node. If we're trying to count parents from DT we'd like to know the count, not if there is a "clocks" property or not. Furthermore, some drivers are assigning the return value to their clk_init_data::num_parents member which is unsigned, leading to potentially large numbers of parents when the property isn't present.
Let's change the API to return an unsigned int instead of an int. All the callers just want to know the count anyway, and this avoids the bug that was in the clk-gpio driver.
--- drivers/clk/clk.c | 16 ++++++++++++++-- include/linux/clk-provider.h | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-)
drivers/clk/clk.c +14 -2
@@ -3021,9 +3021,21 @@ struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec) }EXPORT_SYMBOL_GPL (of_clk_get_from_provider); -int of_clk_get_parent_count (struct device_node *np) +/** +* of_clk_get_parent_count() - Count the number of clocks a device node has +* @np: device node to count +* +* Returns: The number of clocks that are possible parents of this node +*/ +unsigned int of_clk_get_parent_count (struct device_node *np) { -return of_count_phandle_with_args (np,"clocks" ,"#clock-cells" ); +int count; + + count =of_count_phandle_with_args (np,"clocks" ,"#clock-cells" ); +if (count <0 ) +return 0 ; + +return count; }EXPORT_SYMBOL_GPL (of_clk_get_parent_count);
include/linux/clk-provider.h +1 -1
@@ -716,7 +716,7 @@ void of_clk_del_provider(struct device_node *np);struct clk *of_clk_src_simple_get (struct of_phandle_args *clkspec,void *data);struct clk *of_clk_src_onecell_get (struct of_phandle_args *clkspec,void *data); -int of_clk_get_parent_count (struct device_node *np); +unsigned int of_clk_get_parent_count (struct device_node *np);int of_clk_parent_fill (struct device_node *np,const char **parents,unsigned int size);const char *of_clk_get_parent_name (struct device_node *np,int index);
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
PATCH 2/9 clk: at91: Remove impossible checks for of_clk_get_parent_count()
In reply to Stephen BoydThese checks for < 0 are impossible now that of_clk_get_parent_count() returns an unsigned int. Change the checks for == 0 and update the type.
--- drivers/clk/at91/clk-generated.c | 4 ++-- drivers/clk/at91/clk-main.c | 4 ++-- drivers/clk/at91/clk-master.c | 4 ++-- drivers/clk/at91/clk-programmable.c | 4 ++-- drivers/clk/at91/clk-slow.c | 6 +++--- drivers/clk/at91/clk-smd.c | 4 ++-- drivers/clk/at91/clk-usb.c | 4 ++-- 7 files changed, 15 insertions(+), 15 deletions(-)
drivers/clk/at91/clk-generated.c +2 -2
@@ -268,13 +268,13 @@ void __init of_sama5d2_clk_generated_setup(struct device_node *np,u32 id;const char *name;struct clk *clk; -int num_parents; +unsigned int num_parents;const char *parent_names[GENERATED_SOURCE_MAX ];struct device_node *gcknp;struct clk_range range =CLK_RANGE (0 ,0 ); num_parents =of_clk_get_parent_count (np); -if (num_parents <=0 || num_parents >GENERATED_SOURCE_MAX ) +if (num_parents ==0 || num_parents >GENERATED_SOURCE_MAX )return ;of_clk_parent_fill (np, parent_names, num_parents);
drivers/clk/at91/clk-main.c +2 -2
@@ -611,12 +611,12 @@ void __init of_at91sam9x5_clk_main_setup(struct device_node *np, {struct clk *clk;const char *parent_names[2 ]; -int num_parents; +unsigned int num_parents;unsigned int irq;const char *name = np->name; num_parents =of_clk_get_parent_count (np); -if (num_parents <=0 || num_parents >2 ) +if (num_parents ==0 || num_parents >2 )return ;of_clk_parent_fill (np, parent_names, num_parents);
drivers/clk/at91/clk-master.c +2 -2
@@ -221,14 +221,14 @@ of_at91_clk_master_setup(struct device_node *np, struct at91_pmc *pmc, const struct clk_master_layout *layout) {struct clk *clk; -int num_parents; +unsigned int num_parents;unsigned int irq;const char *parent_names[MASTER_SOURCE_MAX ];const char *name = np->name;struct clk_master_characteristics *characteristics; num_parents =of_clk_get_parent_count (np); -if (num_parents <=0 || num_parents >MASTER_SOURCE_MAX ) +if (num_parents ==0 || num_parents >MASTER_SOURCE_MAX )return ;of_clk_parent_fill (np, parent_names, num_parents);
drivers/clk/at91/clk-programmable.c +2 -2
@@ -232,13 +232,13 @@ of_at91_clk_prog_setup(struct device_node *np, struct at91_pmc *pmc,int num;u32 id;struct clk *clk; -int num_parents; +unsigned int num_parents;const char *parent_names[PROG_SOURCE_MAX ];const char *name;struct device_node *progclknp; num_parents =of_clk_get_parent_count (np); -if (num_parents <=0 || num_parents >PROG_SOURCE_MAX ) +if (num_parents ==0 || num_parents >PROG_SOURCE_MAX )return ;of_clk_parent_fill (np, parent_names, num_parents);
drivers/clk/at91/clk-slow.c +3 -3
@@ -366,11 +366,11 @@ void __init of_at91sam9x5_clk_slow_setup(struct device_node *np, {struct clk *clk;const char *parent_names[2 ]; -int num_parents; +unsigned int num_parents;const char *name = np->name; num_parents =of_clk_get_parent_count (np); -if (num_parents <=0 || num_parents >2 ) +if (num_parents ==0 || num_parents >2 )return ;of_clk_parent_fill (np, parent_names, num_parents);
@@ -437,7 +437,7 @@ void __init of_at91sam9260_clk_slow_setup(struct device_node *np, {struct clk *clk;const char *parent_names[2 ]; -int num_parents; +unsigned int num_parents;const char *name = np->name; num_parents =of_clk_get_parent_count (np);
drivers/clk/at91/clk-smd.c +2 -2
@@ -145,12 +145,12 @@ void __init of_at91sam9x5_clk_smd_setup(struct device_node *np, struct at91_pmc *pmc) {struct clk *clk; -int num_parents; +unsigned int num_parents;const char *parent_names[SMD_SOURCE_MAX ];const char *name = np->name; num_parents =of_clk_get_parent_count (np); -if (num_parents <=0 || num_parents >SMD_SOURCE_MAX ) +if (num_parents ==0 || num_parents >SMD_SOURCE_MAX )return ;of_clk_parent_fill (np, parent_names, num_parents);
drivers/clk/at91/clk-usb.c +2 -2
@@ -373,12 +373,12 @@ void __init of_at91sam9x5_clk_usb_setup(struct device_node *np, struct at91_pmc *pmc) {struct clk *clk; -int num_parents; +unsigned int num_parents;const char *parent_names[USB_SOURCE_MAX ];const char *name = np->name; num_parents =of_clk_get_parent_count (np); -if (num_parents <=0 || num_parents >USB_SOURCE_MAX ) +if (num_parents ==0 || num_parents >USB_SOURCE_MAX )return ;of_clk_parent_fill (np, parent_names, num_parents);
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
PATCH 3/9 clk: gpio: Remove impossible check for of_clk_get_parent_count() < 0
In reply to Stephen BoydThe check for < 0 is impossible now that of_clk_get_parent_count() returns an unsigned int. Simplify the code and update the type here.
--- drivers/clk/clk-gpio.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/clk/clk-gpio.c +2 -4
@@ -205,15 +205,13 @@ static int gpio_clk_driver_probe(struct platform_device *pdev) {struct device_node *node = pdev->dev.of_node;const char **parent_names, *gpio_name; -int num_parents, gpio; +unsigned int num_parents; +int gpio;enum of_gpio_flags of_flags;struct clk *clk;bool active_low, is_mux; num_parents =of_clk_get_parent_count (node); -if (num_parents <0 ) - num_parents =0 ; -if (num_parents) { parent_names =devm_kcalloc (&pdev->dev, num_parents,sizeof (char *),GFP_KERNEL );
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
PATCH 4/9 clk: h8300: Remove impossible check for of_clk_get_parent_count()
In reply to Stephen BoydThe checks for < 1 can be simplified now that of_clk_get_parent_count() returns an unsigned int. Update the code to reflect the int to unsigned int change.
--- drivers/clk/h8300/clk-div.c | 4 ++-- drivers/clk/h8300/clk-h8s2678.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/clk/h8300/clk-div.c +2 -2
@@ -13,7 +13,7 @@ static DEFINE_SPINLOCK(clklock);static void __init h8300_div_clk_setup (struct device_node *node) { -int num_parents; +unsigned int num_parents;struct clk *clk;const char *clk_name = node->name;const char *parent_name;
@@ -22,7 +22,7 @@ static void __init h8300_div_clk_setup(struct device_node *node)int offset; num_parents =of_clk_get_parent_count (node); -if (num_parents <1 ) { +if (!num_parents) {pr_err ("%s: no parent found" , clk_name);return ; }
drivers/clk/h8300/clk-h8s2678.c +2 -2
@@ -83,7 +83,7 @@ static const struct clk_ops pll_ops = {static void __init h8s2678_pll_clk_setup (struct device_node *node) { -int num_parents; +unsigned int num_parents;struct clk *clk;const char *clk_name = node->name;const char *parent_name;
@@ -91,7 +91,7 @@ static void __init h8s2678_pll_clk_setup(struct device_node *node)struct clk_init_data init; num_parents =of_clk_get_parent_count (node); -if (num_parents <1 ) { +if (!num_parents) {pr_err ("%s: no parent found" , clk_name);return ; }
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
PATCH 5/9 clk: st: Remove impossible check for of_clk_get_parent_count() < 0
In reply to Stephen BoydThe checks for < 0 are impossible now that of_clk_get_parent_count() returns an unsigned int. Simplify the code and update the types.
--- drivers/clk/st/clk-flexgen.c | 4 ++-- drivers/clk/st/clkgen-mux.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/clk/st/clk-flexgen.c +2 -2
@@ -244,10 +244,10 @@ static const char ** __init flexgen_get_parents(struct device_node *np, int *num_parents) {const char **parents; -int nparents; +unsigned int nparents; nparents =of_clk_get_parent_count (np); -if (WARN_ON (nparents <=0 )) +if (WARN_ON (!nparents))return NULL ; parents =kcalloc (nparents,sizeof (const char *),GFP_KERNEL );
drivers/clk/st/clkgen-mux.c +2 -2
@@ -26,10 +26,10 @@ static const char ** __init clkgen_mux_get_parents(struct device_node *np, int *num_parents) {const char **parents; -int nparents; +unsigned int nparents; nparents =of_clk_get_parent_count (np); -if (WARN_ON (nparents <=0 )) +if (WARN_ON (!nparents))return ERR_PTR (-EINVAL ); parents =kcalloc (nparents,sizeof (const char *),GFP_KERNEL );
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
PATCH 6/9 clk: sunxi: Use proper type for of_clk_get_parent_count() return value
In reply to Stephen BoydThe return type of of_clk_get_parent_count() is an unsigned int now, so let's update the code here to be more explicit about the range of values we can test for.
--- drivers/clk/sunxi/clk-sun6i-ar100.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/sunxi/clk-sun6i-ar100.c +1 -1
@@ -181,7 +181,7 @@ static int sun6i_a31_ar100_clk_probe(struct platform_device *pdev)struct ar100_clk *ar100;struct resource *r;struct clk *clk; -int nparents; +unsigned int nparents; ar100 =devm_kzalloc (&pdev->dev,sizeof (*ar100),GFP_KERNEL );if (!ar100)
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
PATCH 7/9 clk: ti: Update for of_clk_get_parent_count() returning unsigned int
In reply to Stephen BoydChange the types here to unsigned int instead of int and update the checks for == 0 instead < 1 to be more explicit about what's going on now that of_clk_get_parent_count() has changed return types.
--- drivers/clk/ti/clockdomain.c | 2 +- drivers/clk/ti/composite.c | 8 ++++---- drivers/clk/ti/dpll.c | 2 +- drivers/clk/ti/mux.c | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-)
drivers/clk/ti/clockdomain.c +1 -1
@@ -109,7 +109,7 @@ static void __init of_ti_clockdomain_setup(struct device_node *node)struct clk_hw *clk_hw;const char *clkdm_name = node->name;int i; -int num_clks; +unsigned int num_clks; num_clks =of_clk_get_parent_count (node);
drivers/clk/ti/composite.c +4 -4
@@ -234,14 +234,14 @@ cleanup:static void __init of_ti_composite_clk_setup (struct device_node *node) { -int num_clks; +unsigned int num_clks;int i;struct clk_hw_omap_comp *cclk;/* Number of component clocks to be put inside this clock */ num_clks =of_clk_get_parent_count (node); -if (num_clks <1 ) { +if (!num_clks) {pr_err ("composite clk %s must have component(s)\n" , node->name);return ; }
@@ -271,13 +271,13 @@ CLK_OF_DECLARE(ti_composite_clock, "ti,composite-clock",int __init ti_clk_add_component (struct device_node *node,struct clk_hw *hw,int type) { -int num_parents; +unsigned int num_parents;const char **parent_names;struct component_clk *clk; num_parents =of_clk_get_parent_count (node); -if (num_parents <1 ) { +if (!num_parents) {pr_err ("component-clock %s must have parent(s)\n" , node->name);return -EINVAL ; }
drivers/clk/ti/dpll.c +1 -1
@@ -361,7 +361,7 @@ static void __init of_ti_dpll_setup(struct device_node *node, init->ops = ops; init->num_parents =of_clk_get_parent_count (node); -if (init->num_parents <1 ) { +if (!init->num_parents) {pr_err ("%s must have parent(s)\n" , node->name); goto cleanup; }
drivers/clk/ti/mux.c +2 -2
@@ -178,7 +178,7 @@ static void of_mux_clk_setup(struct device_node *node) {struct clk *clk;void __iomem *reg; -int num_parents; +unsigned int num_parents;const char **parent_names;u8 clk_mux_flags =0 ;u32 mask =0 ;
@@ -261,7 +261,7 @@ struct clk_hw *ti_clk_build_component_mux(struct ti_clk_mux *setup)static void __init of_ti_composite_mux_clk_setup (struct device_node *node) {struct clk_mux *mux; -int num_parents; +unsigned int num_parents;u32 val; mux =kzalloc (sizeof (*mux),GFP_KERNEL );
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
PATCH 8/9 usb: dwc3: Remove impossible check for of_clk_get_parent_count() < 0
In reply to Stephen BoydThe check for < 0 is impossible now that of_clk_get_parent_count() returns an unsigned int. Simplify the code and update the types.
---
Please ack so this can go through clk tree along with patch 1.
drivers/usb/dwc3/dwc3-of-simple.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/usb/dwc3/dwc3-of-simple.c +5 -4
@@ -42,6 +42,7 @@ static int dwc3_of_simple_probe(struct platform_device *pdev)struct device *dev = &pdev->dev;struct device_node *np = dev->of_node; +unsigned int count;int ret;int i;
@@ -49,11 +50,11 @@ static int dwc3_of_simple_probe(struct platform_device *pdev)if (!simple)return -ENOMEM ; - ret =of_clk_get_parent_count (np); -if (ret <0 ) -return ret; + count =of_clk_get_parent_count (np); +if (!count) +return -ENOENT ; - simple->num_clocks = ret; + simple->num_clocks = count; simple->clks =devm_kcalloc (dev, simple->num_clocks,sizeof (struct clk *),GFP_KERNEL );
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
Re: [PATCH 8/9] usb: dwc3: Remove impossible check for of_clk_get_parent_count() < 0
Hi,
Quoted 10 lines · Stephen Boyd
The check for < 0 is impossible now that
of_clk_get_parent_count()returns an unsigned int. Simplify the code and update the types.Cc Felipe Balbi <[email protected]>Cc <[email protected]>Signed-off-by Stephen Boyd <[email protected]>---
Please ack so this can go through clk tree along with patch 1.
sure, no problem ;-) patch looks okay
Signature · 1 line
balbi
PATCH 9/9 simplefb: Remove impossible check for of_clk_get_parent_count() < 0
In reply to Stephen BoydThe check for < 0 is impossible now that of_clk_get_parent_count() returns an unsigned int. Simplify the code and update the types.
---
Please ack so this can go through clk tree along with patch 1.
drivers/video/fbdev/simplefb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/video/fbdev/simplefb.c +2 -2
@@ -174,7 +174,7 @@ static int simplefb_parse_pd(struct platform_device *pdev,struct simplefb_par {u32 palette[PSEUDO_PALETTE_SIZE ];#if definedCONFIG_OF && definedCONFIG_COMMON_CLK -int clk_count; +unsigned int clk_count;struct clk **clks;#endif #if definedCONFIG_OF && definedCONFIG_REGULATOR
@@ -213,7 +213,7 @@ static int simplefb_clocks_init(struct simplefb_par *par,return 0 ; par->clk_count =of_clk_get_parent_count (np); -if (par->clk_count <=0 ) +if (!par->clk_count)return 0 ; par->clks =kcalloc (par->clk_count,sizeof (struct clk *),GFP_KERNEL );
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
Re: [PATCH 9/9] simplefb: Remove impossible check for of_clk_get_parent_count() < 0
Quoted 9 lines · Stephen Boyd
The check for < 0 is impossible now that
of_clk_get_parent_count()returns an unsigned int. Simplify the code and update the types.Cc Hans de Goede <[email protected]>Cc Jean-Christophe Plagniol-Villard <[email protected]>Cc Tomi Valkeinen <[email protected]>Cc <[email protected]>Signed-off-by Stephen Boyd <[email protected]>
Quoted 6 lines
---
Please ack so this can go through clk tree along with patch 1.
drivers/video/fbdev/simplefb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Tomi