clk: fix possible null pointer dereference
[linux-drm-fsl-dcu.git] / drivers / clk / clk.c
index 5307225684eb351c49463b1332fdf9eb6e4f9755..d48ac71c6c8b173793a31e95ebaa93749e883a76 100644 (file)
@@ -240,7 +240,6 @@ static const struct file_operations clk_dump_fops = {
        .release        = single_release,
 };
 
-/* caller must hold prepare_lock */
 static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry)
 {
        struct dentry *d;
@@ -354,13 +353,13 @@ out:
        mutex_unlock(&clk_debug_lock);
 }
 
-struct dentry *clk_debugfs_add_file(struct clk *clk, char *name, umode_t mode,
+struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
                                void *data, const struct file_operations *fops)
 {
        struct dentry *d = NULL;
 
-       if (clk->dentry)
-               d = debugfs_create_file(name, mode, clk->dentry, data, fops);
+       if (hw->clk->dentry)
+               d = debugfs_create_file(name, mode, hw->clk->dentry, data, fops);
 
        return d;
 }
@@ -574,11 +573,6 @@ unsigned int __clk_get_enable_count(struct clk *clk)
        return !clk ? 0 : clk->enable_count;
 }
 
-unsigned int __clk_get_prepare_count(struct clk *clk)
-{
-       return !clk ? 0 : clk->prepare_count;
-}
-
 unsigned long __clk_get_rate(struct clk *clk)
 {
        unsigned long ret;
@@ -601,7 +595,7 @@ out:
 }
 EXPORT_SYMBOL_GPL(__clk_get_rate);
 
-unsigned long __clk_get_accuracy(struct clk *clk)
+static unsigned long __clk_get_accuracy(struct clk *clk)
 {
        if (!clk)
                return 0;
@@ -707,7 +701,7 @@ struct clk *__clk_lookup(const char *name)
  */
 long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
                              unsigned long *best_parent_rate,
-                             struct clk **best_parent_p)
+                             struct clk_hw **best_parent_p)
 {
        struct clk *clk = hw->clk, *parent, *best_parent = NULL;
        int i, num_parents;
@@ -743,7 +737,7 @@ long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
 
 out:
        if (best_parent)
-               *best_parent_p = best_parent;
+               *best_parent_p = best_parent->hw;
        *best_parent_rate = best;
 
        return best;
@@ -951,6 +945,7 @@ unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
 {
        unsigned long parent_rate = 0;
        struct clk *parent;
+       struct clk_hw *parent_hw;
 
        if (!clk)
                return 0;
@@ -959,10 +954,11 @@ unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
        if (parent)
                parent_rate = parent->rate;
 
-       if (clk->ops->determine_rate)
+       if (clk->ops->determine_rate) {
+               parent_hw = parent ? parent->hw : NULL;
                return clk->ops->determine_rate(clk->hw, rate, &parent_rate,
-                                               &parent);
-       else if (clk->ops->round_rate)
+                                               &parent_hw);
+       else if (clk->ops->round_rate)
                return clk->ops->round_rate(clk->hw, rate, &parent_rate);
        else if (clk->flags & CLK_SET_RATE_PARENT)
                return __clk_round_rate(clk->parent, rate);
@@ -1350,6 +1346,7 @@ static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
 {
        struct clk *top = clk;
        struct clk *old_parent, *parent;
+       struct clk_hw *parent_hw;
        unsigned long best_parent_rate = 0;
        unsigned long new_rate;
        int p_index = 0;
@@ -1365,9 +1362,11 @@ static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
 
        /* find the closest rate and parent clk/rate */
        if (clk->ops->determine_rate) {
+               parent_hw = parent ? parent->hw : NULL;
                new_rate = clk->ops->determine_rate(clk->hw, rate,
                                                    &best_parent_rate,
-                                                   &parent);
+                                                   &parent_hw);
+               parent = parent_hw ? parent_hw->clk : NULL;
        } else if (clk->ops->round_rate) {
                new_rate = clk->ops->round_rate(clk->hw, rate,
                                                &best_parent_rate);
@@ -1944,7 +1943,6 @@ int __clk_init(struct device *dev, struct clk *clk)
        else
                clk->rate = 0;
 
-       clk_debug_register(clk);
        /*
         * walk the list of orphan clocks and reparent any that are children of
         * this clock
@@ -1979,6 +1977,9 @@ int __clk_init(struct device *dev, struct clk *clk)
 out:
        clk_prepare_unlock();
 
+       if (!ret)
+               clk_debug_register(clk);
+
        return ret;
 }
 
@@ -2273,14 +2274,17 @@ int __clk_get(struct clk *clk)
 
 void __clk_put(struct clk *clk)
 {
+       struct module *owner;
+
        if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
                return;
 
        clk_prepare_lock();
+       owner = clk->owner;
        kref_put(&clk->ref, __clk_release);
        clk_prepare_unlock();
 
-       module_put(clk->owner);
+       module_put(owner);
 }
 
 /***        clk rate change notifiers        ***/