From: s.hauer@pengutronix.de (Sascha Hauer)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH v5 4/4] clk: basic clock hardware types
Date: Wed, 7 Mar 2012 22:20:25 +0100 [thread overview]
Message-ID: <20120307212025.GC26882@pengutronix.de> (raw)
In-Reply-To: <1330763341-3437-5-git-send-email-mturquette@linaro.org>
On Sat, Mar 03, 2012 at 12:29:01AM -0800, Mike Turquette wrote:
> +struct clk *clk_register_divider(struct device *dev, const char *name,
> + const char *parent_name, unsigned long flags,
> + void __iomem *reg, u8 shift, u8 width,
> + u8 clk_divider_flags, spinlock_t *lock)
> +{
> + struct clk_divider *div;
> + char **parent_names = NULL;
> + u8 len;
> +
> + div = kmalloc(sizeof(struct clk_divider), GFP_KERNEL);
> +
> + if (!div) {
> + pr_err("%s: could not allocate divider clk\n", __func__);
> + return ERR_PTR(-ENOMEM);
> + }
> +
> + /* struct clk_divider assignments */
> + div->reg = reg;
> + div->shift = shift;
> + div->width = width;
> + div->flags = clk_divider_flags;
> + div->lock = lock;
> +
> + if (parent_name) {
> + parent_names = kmalloc(sizeof(char *), GFP_KERNEL);
> +
> + if (! parent_names)
> + goto out;
> +
> + len = sizeof(char) * strlen(parent_name);
> +
> + parent_names[0] = kmalloc(len, GFP_KERNEL);
> +
> + if (!parent_names[0])
> + goto out;
> +
> + strncpy(parent_names[0], parent_name, len);
> + }
> +
> +out:
> + return clk_register(dev, name,
> + &clk_divider_ops, &div->hw,
> + parent_names,
> + (parent_name ? 1 : 0),
> + flags);
> +}
clk_register_divider and also clk_register_gate have some problems.
First you allocate memory with exactly the string length without
the terminating 0. Then the functions leak memory when clk_register
fails. Could you fold in the following patch to fix this?
Sascha
8<--------------------------------------------------
fix divider/gate registration
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
drivers/clk/clk-divider.c | 34 +++++++++++++++-------------------
drivers/clk/clk-gate.c | 33 ++++++++++++++-------------------
include/linux/clk-provider.h | 2 ++
3 files changed, 31 insertions(+), 38 deletions(-)
diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c
index 8f02930..99b6b55 100644
--- a/drivers/clk/clk-divider.c
+++ b/drivers/clk/clk-divider.c
@@ -156,14 +156,13 @@ struct clk *clk_register_divider(struct device *dev, const char *name,
u8 clk_divider_flags, spinlock_t *lock)
{
struct clk_divider *div;
- char **parent_names = NULL;
- u8 len;
+ struct clk *clk;
- div = kmalloc(sizeof(struct clk_divider), GFP_KERNEL);
+ div = kzalloc(sizeof(struct clk_divider), GFP_KERNEL);
if (!div) {
pr_err("%s: could not allocate divider clk\n", __func__);
- return ERR_PTR(-ENOMEM);
+ return NULL;
}
/* struct clk_divider assignments */
@@ -174,25 +173,22 @@ struct clk *clk_register_divider(struct device *dev, const char *name,
div->lock = lock;
if (parent_name) {
- parent_names = kmalloc(sizeof(char *), GFP_KERNEL);
-
- if (! parent_names)
- goto out;
-
- len = sizeof(char) * strlen(parent_name);
-
- parent_names[0] = kmalloc(len, GFP_KERNEL);
-
- if (!parent_names[0])
+ div->parent[0] = kstrdup(parent_name, GFP_KERNEL);
+ if (!div->parent[0])
goto out;
-
- strncpy(parent_names[0], parent_name, len);
}
-out:
- return clk_register(dev, name,
+ clk = clk_register(dev, name,
&clk_divider_ops, &div->hw,
- parent_names,
+ div->parent,
(parent_name ? 1 : 0),
flags);
+ if (clk)
+ return clk;
+
+out:
+ kfree(div->parent[0]);
+ kfree(div);
+
+ return NULL;
}
diff --git a/drivers/clk/clk-gate.c b/drivers/clk/clk-gate.c
index e831f7b..92c0489 100644
--- a/drivers/clk/clk-gate.c
+++ b/drivers/clk/clk-gate.c
@@ -80,14 +80,13 @@ struct clk *clk_register_gate(struct device *dev, const char *name,
u8 clk_gate_flags, spinlock_t *lock)
{
struct clk_gate *gate;
- char **parent_names = NULL;
- u8 len;
+ struct clk *clk;
- gate = kmalloc(sizeof(struct clk_gate), GFP_KERNEL);
+ gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
if (!gate) {
pr_err("%s: could not allocate gated clk\n", __func__);
- return ERR_PTR(-ENOMEM);
+ return NULL;
}
/* struct clk_gate assignments */
@@ -97,25 +96,21 @@ struct clk *clk_register_gate(struct device *dev, const char *name,
gate->lock = lock;
if (parent_name) {
- parent_names = kmalloc(sizeof(char *), GFP_KERNEL);
-
- if (! parent_names)
- goto out;
-
- len = sizeof(char) * strlen(parent_name);
-
- parent_names[0] = kmalloc(len, GFP_KERNEL);
-
- if (!parent_names[0])
+ gate->parent[0] = kstrdup(parent_name, GFP_KERNEL);
+ if (!gate->parent[0])
goto out;
-
- strncpy(parent_names[0], parent_name, len);
}
-out:
- return clk_register(dev, name,
+ clk = clk_register(dev, name,
&clk_gate_ops, &gate->hw,
- parent_names,
+ gate->parent,
(parent_name ? 1 : 0),
flags);
+ if (clk)
+ return clk;
+out:
+ kfree(gate->parent[0]);
+ kfree(gate);
+
+ return NULL;
}
diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index 9b7dd5e..ed1f918 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -174,6 +174,7 @@ struct clk_gate {
u8 bit_idx;
u8 flags;
spinlock_t *lock;
+ char *parent[1];
};
#define CLK_GATE_SET_TO_DISABLE BIT(0)
@@ -210,6 +211,7 @@ struct clk_divider {
u8 width;
u8 flags;
spinlock_t *lock;
+ char *parent[1];
};
#define CLK_DIVIDER_ONE_BASED BIT(0)
--
1.7.9.1
--
Pengutronix e.K. | |
Industrial Linux Solutions | http://www.pengutronix.de/ |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
next prev parent reply other threads:[~2012-03-07 21:20 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-03-03 8:28 [PATCH v5 0/4] common clk framework Mike Turquette
2012-03-03 8:28 ` [PATCH v5 1/4] Documentation: common clk API Mike Turquette
2012-03-03 8:28 ` [PATCH v5 2/4] clk: Kconfig: add entry for HAVE_CLK_PREPARE Mike Turquette
2012-03-05 2:04 ` Richard Zhao
2012-03-05 19:46 ` Turquette, Mike
2012-03-03 8:29 ` [PATCH v5 3/4] clk: introduce the common clock framework Mike Turquette
2012-03-03 13:31 ` Sascha Hauer
2012-03-03 17:14 ` Turquette, Mike
2012-03-04 11:52 ` Sascha Hauer
2012-03-05 0:12 ` Turquette, Mike
2012-03-05 7:38 ` Sascha Hauer
2012-03-05 20:03 ` Turquette, Mike
2012-03-06 19:00 ` Sascha Hauer
2012-03-07 21:20 ` Turquette, Mike
2012-03-08 6:27 ` Andrew Lunn
2012-03-08 23:25 ` Sascha Hauer
2012-03-09 7:57 ` Andrew Lunn
2012-03-09 18:25 ` Turquette, Mike
2012-03-19 7:01 ` Shawn Guo
2012-03-19 11:22 ` Sascha Hauer
2012-03-09 18:18 ` Turquette, Mike
2012-03-09 0:51 ` Thomas Gleixner
2012-03-17 3:23 ` Saravana Kannan
2012-03-19 5:38 ` Shawn Guo
2012-03-19 7:42 ` Shawn Guo
2012-03-05 9:22 ` Richard Zhao
2012-03-14 2:03 ` Turquette, Mike
2012-03-13 11:24 ` Sascha Hauer
2012-03-13 23:43 ` Turquette, Mike
2012-03-14 8:48 ` Sascha Hauer
2012-03-14 20:47 ` Turquette, Mike
2012-03-14 21:28 ` Thomas Gleixner
2012-03-14 22:13 ` Turquette, Mike
2012-03-14 22:18 ` Thomas Gleixner
2012-03-03 8:29 ` [PATCH v5 4/4] clk: basic clock hardware types Mike Turquette
2012-03-04 14:26 ` Andrew Lunn
2012-03-04 14:35 ` Andrew Lunn
2012-03-05 0:15 ` Turquette, Mike
2012-03-04 17:42 ` Andrew Lunn
2012-03-05 0:30 ` Turquette, Mike
2012-03-05 8:48 ` Andrew Lunn
2012-03-05 9:29 ` Sascha Hauer
2012-03-05 10:17 ` Andrew Lunn
2012-03-09 23:38 ` Turquette, Mike
2012-03-04 20:33 ` [PATCH] clk: Fix compile errors in DEFINE_CLK_GATE Andrew Lunn
2012-03-05 0:31 ` Turquette, Mike
2012-03-07 21:20 ` Sascha Hauer [this message]
2012-03-09 22:50 ` [PATCH v5 4/4] clk: basic clock hardware types Turquette, Mike
2012-03-09 2:34 ` [PATCH v5 0/4] common clk framework Richard Zhao
2012-03-09 9:19 ` Sascha Hauer
2012-03-09 18:35 ` Turquette, Mike
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120307212025.GC26882@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=linux-arm-kernel@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).