powerpc/pseries: extract of_helpers module
PATCH 1/5 powerpc/pseries: extract of_helpers module
Extract a new module to share the code between other modules.
There is no functional change.
--- arch/powerpc/platforms/pseries/Makefile | 1 + arch/powerpc/platforms/pseries/of_helpers.c | 38 +++++++++++++++++++++++++++++ arch/powerpc/platforms/pseries/of_helpers.h | 8 ++++++ arch/powerpc/platforms/pseries/reconfig.c | 34 ++------------------------ 4 files changed, 49 insertions(+), 32 deletions(-) create mode 100644 arch/powerpc/platforms/pseries/of_helpers.c create mode 100644 arch/powerpc/platforms/pseries/of_helpers.h
arch/powerpc/platforms/pseries/Makefile +1 -0
@@ -2,6 +2,7 @@ ccflags-$(CONFIG_PPC64) := $(NO_MINIMAL_TOC) ccflags-$(CONFIG_PPC_PSERIES_DEBUG) += -DDEBUG obj-y := lpar.o hvCall.o nvram.o reconfig.o \ + of_helpers.o \ setup.o iommu.o event_sources.o ras.o \ firmware.o power.o dlpar.o mobility.o rng.o obj-$(CONFIG_SMP) += smp.o
arch/powerpc/platforms/pseries/of_helpers.c new file +38 -0
@@ -0,0 +1,38 @@ +#include <linux/string.h> +#include <linux/err.h> +#include <linux/slab.h> +#include <linux/of.h> + +#include "of_helpers.h" + +/** +* pseries_of_derive_parent - basically like dirname(1) +* @path: the full_name of a node to be added to the tree +* +* Returns the node which should be the parent of the node +* described by path. E.g., for path = "/foo/bar", returns +* the node with full_name = "/foo". +*/ +struct device_node *pseries_of_derive_parent (const char *path) +{ +struct device_node *parent =NULL ; +char *parent_path ="/" ; +size_t parent_path_len =strrchr (path,'/' ) - path +1 ; + +/* reject if path is "/" */ +if (!strcmp (path,"/" )) +return ERR_PTR (-EINVAL ); + +if (strrchr (path,'/' ) != path) { + parent_path =kmalloc (parent_path_len,GFP_KERNEL ); +if (!parent_path) +return ERR_PTR (-ENOMEM ); +strlcpy (parent_path, path, parent_path_len); + } + parent =of_find_node_by_path (parent_path); +if (!parent) +return ERR_PTR (-EINVAL ); +if (strcmp (parent_path,"/" )) +kfree (parent_path); +return parent; +}
arch/powerpc/platforms/pseries/of_helpers.h new file +8 -0
@@ -0,0 +1,8 @@ +#ifndef _PSERIES_OF_HELPERS_H +#define _PSERIES_OF_HELPERS_H + +#include <linux/of.h> + +struct device_node *pseries_of_derive_parent (const char *path); + +#endif /* _PSERIES_OF_HELPERS_H */
arch/powerpc/platforms/pseries/reconfig.c +2 -32
@@ -22,37 +22,7 @@#include <asm/uaccess.h> #include <asm/mmu.h> -/** -* derive_parent - basically like dirname(1) -* @path: the full_name of a node to be added to the tree -* -* Returns the node which should be the parent of the node -* described by path. E.g., for path = "/foo/bar", returns -* the node with full_name = "/foo". -*/ -static struct device_node *derive_parent (const char *path) -{ -struct device_node *parent =NULL ; -char *parent_path ="/" ; -size_t parent_path_len =strrchr (path,'/' ) - path +1 ; - -/* reject if path is "/" */ -if (!strcmp (path,"/" )) -return ERR_PTR (-EINVAL ); - -if (strrchr (path,'/' ) != path) { - parent_path =kmalloc (parent_path_len,GFP_KERNEL ); -if (!parent_path) -return ERR_PTR (-ENOMEM ); -strlcpy (parent_path, path, parent_path_len); - } - parent =of_find_node_by_path (parent_path); -if (!parent) -return ERR_PTR (-EINVAL ); -if (strcmp (parent_path,"/" )) -kfree (parent_path); -return parent; -} +#include "of_helpers.h" static int pSeries_reconfig_add_node (const char *path,struct property *proplist) {
@@ -71,7 +41,7 @@ static int pSeries_reconfig_add_node(const char *path, struct property *proplistof_node_set_flag (np,OF_DYNAMIC );of_node_init (np); - np->parent =derive_parent (path); + np->parent =pseries_of_derive_parent (path);if (IS_ERR (np->parent)) { err =PTR_ERR (np->parent); goto out_err;
Signature · 1 line
2.4.6
PATCH 2/5 powerpc/pseries: fix a potential memory leak
In case we have a full node name like /foo/bar and /foo is not found the parent_path left unfreed. So, free a memory before return to a caller.
--- arch/powerpc/platforms/pseries/of_helpers.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
arch/powerpc/platforms/pseries/of_helpers.c +2 -4
@@ -15,7 +15,7 @@*/ struct device_node *pseries_of_derive_parent (const char *path) { -struct device_node *parent =NULL ; +struct device_node *parent;char *parent_path ="/" ;size_t parent_path_len =strrchr (path,'/' ) - path +1 ;
@@ -30,9 +30,7 @@ struct device_node *pseries_of_derive_parent(const char *path)strlcpy (parent_path, path, parent_path_len); } parent =of_find_node_by_path (parent_path); -if (!parent) -return ERR_PTR (-EINVAL );if (strcmp (parent_path,"/" ))kfree (parent_path); -return parent; +return parent ? parent :ERR_PTR (-EINVAL ); }
Signature · 1 line
2.4.6
PATCH 3/5 powerpc/pseries: don't call strrchr() twice
In reply to Andy ShevchenkoThere is no need to call strrchr() second time. We already know that in that case parent_path_len either 1 ("/foo") or bigger ("/foo/bar").
--- arch/powerpc/platforms/pseries/of_helpers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/platforms/pseries/of_helpers.c +1 -1
@@ -23,7 +23,7 @@ struct device_node *pseries_of_derive_parent(const char *path)if (!strcmp (path,"/" ))return ERR_PTR (-EINVAL ); -if (strrchr (path,'/' ) != path) { +if (parent_path_len >1 ) { parent_path =kmalloc (parent_path_len,GFP_KERNEL );if (!parent_path)return ERR_PTR (-ENOMEM );
Signature · 1 line
2.4.6
PATCH 4/5 powerpc/pseries: replace kmalloc + strlcpy
In reply to Andy ShevchenkoThe helper kstrndup() will do the same in one line.
--- arch/powerpc/platforms/pseries/of_helpers.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/powerpc/platforms/pseries/of_helpers.c +1 -2
@@ -24,10 +24,9 @@ struct device_node *pseries_of_derive_parent(const char *path)return ERR_PTR (-EINVAL );if (parent_path_len >1 ) { - parent_path =kmalloc (parent_path_len,GFP_KERNEL ); + parent_path =kstrndup (path, parent_path_len,GFP_KERNEL );if (!parent_path)return ERR_PTR (-ENOMEM ); -strlcpy (parent_path, path, parent_path_len); } parent =of_find_node_by_path (parent_path);if (strcmp (parent_path,"/" ))
Signature · 1 line
2.4.6
PATCH 5/5 powerpc/pseries: re-use code from of_helpers module
In reply to Andy ShevchenkoThe derive_parent() has similar semantics to what we have in newly introduced of_helpers module. The replacement reduces code base and propagates the actual error code to the caller.
--- arch/powerpc/platforms/pseries/dlpar.c | 31 +++++-------------------------- 1 file changed, 5 insertions(+), 26 deletions(-)
arch/powerpc/platforms/pseries/dlpar.c +5 -26
@@ -18,6 +18,8 @@#include <linux/cpu.h> #include <linux/slab.h> #include <linux/of.h> + +#include "of_helpers.h" #include "offline_states.h" #include "pseries.h"
@@ -244,36 +246,13 @@ cc_error:return first_dn; } -static struct device_node *derive_parent (const char *path) -{ -struct device_node *parent; -char *last_slash; - - last_slash =strrchr (path,'/' ); -if (last_slash == path) { - parent =of_find_node_by_path ("/" ); - }else { -char *parent_path; -int parent_path_len = last_slash - path +1 ; - parent_path =kmalloc (parent_path_len,GFP_KERNEL ); -if (!parent_path) -return NULL ; - -strlcpy (parent_path, path, parent_path_len); - parent =of_find_node_by_path (parent_path); -kfree (parent_path); - } - -return parent; -} -int dlpar_attach_node (struct device_node *dn) {int rc; - dn->parent =derive_parent (dn->full_name); -if (!dn->parent) -return -ENOMEM ; + dn->parent =pseries_of_derive_parent (dn->full_name); +if (IS_ERR (dn->parent)) +return PTR_ERR (dn_parent); rc =of_attach_node (dn);if (rc) {
Signature · 1 line
2.4.6
Hi Andy,
arch/powerpc/platforms/pseries/of_helpers.c line 27
Quoted 12 lines · Andy Shevchenko
arch/powerpc/platforms/pseries/of_helpers.c new file +12
-0+struct device_node *pseries_of_derive_parent (const char *path) +{ +struct device_node *parent =NULL ; +char *parent_path ="/" ; +size_t parent_path_len =strrchr (path,'/' ) - path +1 ; + +/* reject if path is "/" */ +if (!strcmp (path,"/" )) +return ERR_PTR (-EINVAL ); + +if (strrchr (path,'/' ) != path) { + parent_path =kmalloc (parent_path_len,GFP_KERNEL );
If path doesn't contain any slash this will do interesting things; you might want to fix that too while you're at it :-)
Segher
Quoted 19 lines · Segher Boessenkool
Hi Andy,
Quoted 12 lines · Andy Shevchenko
arch/powerpc/platforms/pseries/of_helpers.c new file +12
-0+struct device_node *pseries_of_derive_parent (const char *path) +{ +struct device_node *parent =NULL ; +char *parent_path ="/" ; +size_t parent_path_len =strrchr (path,'/' ) - path +1 ; + +/* reject if path is "/" */ +if (!strcmp (path,"/" )) +return ERR_PTR (-EINVAL ); + +if (strrchr (path,'/' ) != path) { + parent_path = kmalloc(parent_path_len,Quoted 1 line
GFP_KERNEL);If path doesn't contain any slash this will do interesting things; you might want to fix that too while you're at it :-)
No problem, though it is in the original code. I would do as a separate patch on top of the series. Will be okay for you?
Quoted 3 lines
Segher
Signature · 2 lines
Andy Shevchenko <[email protected]> Intel Finland Oy
Quoted 5 lines · Andy Shevchenko
Quoted 2 lines
If path doesn't contain any slash this will do interesting things; you might want to fix that too while you're at it :-)
No problem, though it is in the original code. I would do as a separate patch on top of the series. Will be okay for you?
Of course -- you're fixing other problems here as well (memory leak or what was it), that's why I asked.
Segher