remoteproc: Refactor function rproc_alloc()
Mathieu Poirier
maintainerv3 cover remoteproc: Refactor function rproc_alloc()
These are the remaining patches from the second version [1] that have not been applied yet.
Applies cleanly on rproc-next (3898fc99d199)
Thanks,
Mathieu
New for V3:
- Freeing firmware name allocation with kfree_const() in patch 1.
- Get the comment out of the conditional block in patch 2.
- A cleaner implementation of function rproc_alloc_ops() in patch 3.
New for V2:
- Reworked title for patch 01.
- Added "Fixes" tag to patch 01.
- Using kasprintf() instead of complex memory allocation.
- Using kstrdup_const() instead of kstrdup().
- Reworked rproc_alloc_firmware() to use non-negative form.
[1]. https://patchwork.kernel.org/project/linux-remoteproc/list/?series=271809
Mathieu Poirier (4):
remoteproc: Use kstrdup_const() rather than kstrdup()
remoteproc: Restructure firmware name allocation
remoteproc: Split rproc_ops allocation from rproc_alloc()
remoteproc: Get rid of tedious error path
drivers/remoteproc/remoteproc_core.c | 70 +++++++++++++++------------- include/linux/remoteproc.h | 2 +- 2 files changed, 39 insertions(+), 33 deletions(-)
Signature · 1 line
2.20.1
Mathieu Poirier
maintainerPATCH v3 1/4 remoteproc: Use kstrdup_const() rather than kstrdup()
For cases where @firmware is declared "const char *", use function kstrdup_const() to avoid needlessly creating another copy on the heap.
--- drivers/remoteproc/remoteproc_core.c | 8 ++++---- include/linux/remoteproc.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/remoteproc/remoteproc_core.c +4 -4
@@ -1996,7 +1996,7 @@ static void rproc_type_release(struct device *dev)if (rproc->index >=0 )ida_simple_remove (&rproc_dev_index, rproc->index); -kfree (rproc->firmware); +kfree_const (rproc->firmware);kfree (rproc->ops);kfree (rproc); }
@@ -2009,7 +2009,7 @@ static const struct device_type rproc_type = {static int rproc_alloc_firmware (struct rproc *rproc,const char *name,const char *firmware) { -char *p; +const char *p;if (!firmware)/*
@@ -2018,7 +2018,7 @@ static int rproc_alloc_firmware(struct rproc *rproc,*/ p =kasprintf (GFP_KERNEL ,"rproc-%s-fw" , name); else - p =kstrdup (firmware,GFP_KERNEL ); + p =kstrdup_const (firmware,GFP_KERNEL );if (!p)return -ENOMEM ;
@@ -2122,7 +2122,7 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,return rproc; free_firmware: -kfree (rproc->firmware); +kfree_const (rproc->firmware); free_rproc:kfree (rproc);return NULL ;
include/linux/remoteproc.h +1 -1
@@ -489,7 +489,7 @@ struct rproc {struct list_head node;struct iommu_domain *domain;const char *name; -char *firmware; +const char *firmware;void *priv;struct rproc_ops *ops;struct device dev;
Signature · 1 line
2.20.1
Re: [PATCH v3 1/4] remoteproc: Use kstrdup_const() rather than kstrdup()
Quoted 7 lines · Mathieu Poirier
For cases where @firmware is declared "const char *", use function
kstrdup_const()to avoid needlessly creating another copy on the heap.Suggested-by Bjorn Andersson <[email protected]>Signed-off-by Mathieu Poirier <[email protected]>Reviewed-by Alex Elder <[email protected]>
Quoted 61 lines
--- drivers/remoteproc/remoteproc_core.c | 8 ++++---- include/linux/remoteproc.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-)drivers/remoteproc/remoteproc_core.c +4
-4@@ -1996,7 +1996,7 @@ static void rproc_type_release(struct device *dev)if (rproc->index >=0 )ida_simple_remove (&rproc_dev_index, rproc->index); -kfree (rproc->firmware); +kfree_const (rproc->firmware);kfree (rproc->ops);kfree (rproc); }@@ -2009,7 +2009,7 @@ static const struct device_type rproc_type = {static int rproc_alloc_firmware (struct rproc *rproc,const char *name,const char *firmware) { -char *p; +const char *p;if (!firmware)/* @@ -2018,7 +2018,7 @@ static int rproc_alloc_firmware(struct rproc *rproc,*/ p =kasprintf (GFP_KERNEL ,"rproc-%s-fw" , name); else - p =kstrdup (firmware,GFP_KERNEL ); + p =kstrdup_const (firmware,GFP_KERNEL );if (!p)return -ENOMEM ;@@ -2122,7 +2122,7 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,return rproc; free_firmware: -kfree (rproc->firmware); +kfree_const (rproc->firmware); free_rproc:kfree (rproc);return NULL ;include/linux/remoteproc.h +1
-1@@ -489,7 +489,7 @@ struct rproc {struct list_head node;struct iommu_domain *domain;const char *name; -char *firmware; +const char *firmware;void *priv;struct rproc_ops *ops;struct device dev;Signature · 1 line
2.20.1
Mathieu Poirier
maintainerPATCH v3 2/4 remoteproc: Restructure firmware name allocation
In reply to Mathieu PoirierImprove the readability of function rproc_alloc_firmware() by using a non-negated condition and moving the comment out of the conditional block
--- drivers/remoteproc/remoteproc_core.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/remoteproc/remoteproc_core.c +7 -7
@@ -2011,14 +2011,14 @@ static int rproc_alloc_firmware(struct rproc *rproc, {const char *p; -if (!firmware) -/* -* If the caller didn't pass in a firmware name then -* construct a default name. -*/ - p =kasprintf (GFP_KERNEL ,"rproc-%s-fw" , name); -else +/* +* Allocate a firmware name if the caller gave us one to work +* with. Otherwise construct a new one using a default pattern. +*/ +if (firmware) p =kstrdup_const (firmware,GFP_KERNEL ); +else + p =kasprintf (GFP_KERNEL ,"rproc-%s-fw" , name);if (!p)return -ENOMEM ;
Signature · 1 line
2.20.1
Re: [PATCH v3 2/4] remoteproc: Restructure firmware name allocation
Quoted 7 lines · Mathieu Poirier
Improve the readability of function
rproc_alloc_firmware()by using a non-negated condition and moving the comment out of the conditional blockSuggested-by Alex Elder <[email protected]>Signed-off-by Mathieu Poirier <[email protected]>Reviewed-by Alex Elder <[email protected]>
Looks better, thanks for respinning.
Regards,
Bjorn
Quoted 33 lines
--- drivers/remoteproc/remoteproc_core.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)drivers/remoteproc/remoteproc_core.c +7
-7@@ -2011,14 +2011,14 @@ static int rproc_alloc_firmware(struct rproc *rproc, {const char *p; -if (!firmware) -/* -* If the caller didn't pass in a firmware name then -* construct a default name. -*/ - p =kasprintf (GFP_KERNEL ,"rproc-%s-fw" , name); -else +/* +* Allocate a firmware name if the caller gave us one to work +* with. Otherwise construct a new one using a default pattern. +*/ +if (firmware) p =kstrdup_const (firmware,GFP_KERNEL ); +else + p =kasprintf (GFP_KERNEL ,"rproc-%s-fw" , name);if (!p)return -ENOMEM ;Signature · 1 line
2.20.1
Mathieu Poirier
maintainerPATCH v3 3/4 remoteproc: Split rproc_ops allocation from rproc_alloc()
In reply to Mathieu PoirierMake the rproc_ops allocation a function on its own in an effort to clean up function rproc_alloc().
--- drivers/remoteproc/remoteproc_core.c | 33 ++++++++++++++++++---------- 1 file changed, 21 insertions(+), 12 deletions(-)
drivers/remoteproc/remoteproc_core.c +21 -12
@@ -2028,6 +2028,26 @@ static int rproc_alloc_firmware(struct rproc *rproc,return 0 ; } +static int rproc_alloc_ops (struct rproc *rproc,const struct rproc_ops *ops) +{ + rproc->ops =kmemdup (ops,sizeof (*ops),GFP_KERNEL ); +if (!rproc->ops) +return -ENOMEM ; + +if (rproc->ops->load) +return 0 ; + +/* Default to ELF loader if no load function is specified */ + rproc->ops->load = rproc_elf_load_segments; + rproc->ops->parse_fw = rproc_elf_load_rsc_table; + rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table; +if (!rproc->ops->sanity_check) + rproc->ops->sanity_check = rproc_elf32_sanity_check; + rproc->ops->get_boot_addr = rproc_elf_get_boot_addr; + +return 0 ; +} +/** * rproc_alloc() - allocate a remote processor handle * @dev: the underlying device
@@ -2067,8 +2087,7 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,if (rproc_alloc_firmware (rproc, name, firmware)) goto free_rproc; - rproc->ops =kmemdup (ops,sizeof (*ops),GFP_KERNEL ); -if (!rproc->ops) +if (rproc_alloc_ops (rproc, ops)) goto free_firmware; rproc->name = name;
@@ -2096,16 +2115,6 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,atomic_set (&rproc->power,0 ); -/* Default to ELF loader if no load function is specified */ -if (!rproc->ops->load) { - rproc->ops->load = rproc_elf_load_segments; - rproc->ops->parse_fw = rproc_elf_load_rsc_table; - rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table; -if (!rproc->ops->sanity_check) - rproc->ops->sanity_check = rproc_elf32_sanity_check; - rproc->ops->get_boot_addr = rproc_elf_get_boot_addr; - } -mutex_init (&rproc->lock);INIT_LIST_HEAD (&rproc->carveouts);
Signature · 1 line
2.20.1
Re: [PATCH v3 3/4] remoteproc: Split rproc_ops allocation from rproc_alloc()
Quoted 5 lines · Mathieu Poirier
Make the
rproc_opsallocation a function on its own in an effort to clean up functionrproc_alloc().Signed-off-by Mathieu Poirier <[email protected]>Reviewed-by Alex Elder <[email protected]>
Quoted 65 lines
--- drivers/remoteproc/remoteproc_core.c | 33 ++++++++++++++++++---------- 1 file changed, 21 insertions(+), 12 deletions(-)drivers/remoteproc/remoteproc_core.c +21
-12@@ -2028,6 +2028,26 @@ static int rproc_alloc_firmware(struct rproc *rproc,return 0 ; } +static int rproc_alloc_ops (struct rproc *rproc,const struct rproc_ops *ops) +{ + rproc->ops =kmemdup (ops,sizeof (*ops),GFP_KERNEL ); +if (!rproc->ops) +return -ENOMEM ; + +if (rproc->ops->load) +return 0 ; + +/* Default to ELF loader if no load function is specified */ + rproc->ops->load = rproc_elf_load_segments; + rproc->ops->parse_fw = rproc_elf_load_rsc_table; + rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table; +if (!rproc->ops->sanity_check) + rproc->ops->sanity_check = rproc_elf32_sanity_check; + rproc->ops->get_boot_addr = rproc_elf_get_boot_addr; + +return 0 ; +} +/** * rproc_alloc() - allocate a remote processor handle * @dev: the underlying device @@ -2067,8 +2087,7 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,if (rproc_alloc_firmware (rproc, name, firmware)) goto free_rproc; - rproc->ops =kmemdup (ops,sizeof (*ops),GFP_KERNEL ); -if (!rproc->ops) +if (rproc_alloc_ops (rproc, ops)) goto free_firmware; rproc->name = name;@@ -2096,16 +2115,6 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,atomic_set (&rproc->power,0 ); -/* Default to ELF loader if no load function is specified */ -if (!rproc->ops->load) { - rproc->ops->load = rproc_elf_load_segments; - rproc->ops->parse_fw = rproc_elf_load_rsc_table; - rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table; -if (!rproc->ops->sanity_check) - rproc->ops->sanity_check = rproc_elf32_sanity_check; - rproc->ops->get_boot_addr = rproc_elf_get_boot_addr; - } -mutex_init (&rproc->lock);INIT_LIST_HEAD (&rproc->carveouts);Signature · 1 line
2.20.1
Mathieu Poirier
maintainerPATCH v3 4/4 remoteproc: Get rid of tedious error path
In reply to Mathieu PoirierGet rid of tedious error management by moving firmware and operation allocation after calling device_initialize(). That way we take advantage of the automatic call to rproc_type_release() to cleanup after ourselves when put_device() is called.
--- drivers/remoteproc/remoteproc_core.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-)
drivers/remoteproc/remoteproc_core.c +9 -12
@@ -2084,12 +2084,6 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,if (!rproc)return NULL ; -if (rproc_alloc_firmware (rproc, name, firmware)) - goto free_rproc; - -if (rproc_alloc_ops (rproc, ops)) - goto free_firmware; - rproc->name = name; rproc->priv = &rproc[1 ]; rproc->auto_boot = true;
@@ -2103,12 +2097,17 @@ struct rproc *rproc_alloc(struct device *dev, const char *name, rproc->dev.driver_data = rproc;idr_init (&rproc->notifyids); +if (rproc_alloc_firmware (rproc, name, firmware)) + goto put_device; + +if (rproc_alloc_ops (rproc, ops)) + goto put_device; +/* Assign a unique device index and name */ rproc->index =ida_simple_get (&rproc_dev_index,0 ,0 ,GFP_KERNEL );if (rproc->index <0 ) {dev_err (dev,"ida_simple_get failed: %d\n" , rproc->index); -put_device (&rproc->dev); -return NULL ; + goto put_device; }dev_set_name (&rproc->dev,"remoteproc%d" , rproc->index);
@@ -2130,10 +2129,8 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,return rproc; -free_firmware: -kfree_const (rproc->firmware); -free_rproc: -kfree (rproc); +put_device: +put_device (&rproc->dev);return NULL ; }EXPORT_SYMBOL (rproc_alloc);
Signature · 1 line
2.20.1
Re: [PATCH v3 4/4] remoteproc: Get rid of tedious error path
Quoted 8 lines · Mathieu Poirier
Get rid of tedious error management by moving firmware and operation allocation after calling
device_initialize(). That way we take advantage of the automatic call torproc_type_release()to cleanup after ourselves whenput_device()is called.Signed-off-by Mathieu Poirier <[email protected]>Reviewed-by Alex Elder <[email protected]>Acked-by Suman Anna <[email protected]>
Quoted 57 lines
--- drivers/remoteproc/remoteproc_core.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-)drivers/remoteproc/remoteproc_core.c +9
-12@@ -2084,12 +2084,6 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,if (!rproc)return NULL ; -if (rproc_alloc_firmware (rproc, name, firmware)) - goto free_rproc; - -if (rproc_alloc_ops (rproc, ops)) - goto free_firmware; - rproc->name = name; rproc->priv = &rproc[1 ]; rproc->auto_boot = true;@@ -2103,12 +2097,17 @@ struct rproc *rproc_alloc(struct device *dev, const char *name, rproc->dev.driver_data = rproc;idr_init (&rproc->notifyids); +if (rproc_alloc_firmware (rproc, name, firmware)) + goto put_device; + +if (rproc_alloc_ops (rproc, ops)) + goto put_device; +/* Assign a unique device index and name */ rproc->index =ida_simple_get (&rproc_dev_index,0 ,0 ,GFP_KERNEL );if (rproc->index <0 ) {dev_err (dev,"ida_simple_get failed: %d\n" , rproc->index); -put_device (&rproc->dev); -return NULL ; + goto put_device; }dev_set_name (&rproc->dev,"remoteproc%d" , rproc->index);@@ -2130,10 +2129,8 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,return rproc; -free_firmware: -kfree_const (rproc->firmware); -free_rproc: -kfree (rproc); +put_device: +put_device (&rproc->dev);return NULL ; }EXPORT_SYMBOL (rproc_alloc);Signature · 1 line
2.20.1
This series was applied to andersson/remoteproc.git (refs/heads/for-next).
Hello:
This series was applied to andersson/remoteproc.git (refs/heads/for-next).
Quoted 9 lines · On Mon, 20 Apr 2020 17:15:57 -0600 you wrote:
These are the remaining patches from the second version [1] that have not been applied yet.
Applies cleanly on rproc-next (
3898fc99d199)Thanks,
Mathieu[...]
Here is a summary with links:
- [v3,1/4] remoteproc: Use kstrdup_const() rather than kstrdup()
https://git.kernel.org/andersson/remoteproc/c/1487deda19c82d30d1867277e89bc2d515b9d2d4 - [v3,2/4] remoteproc: Restructure firmware name allocation https://git.kernel.org/andersson/remoteproc/c/9d5f82c8ba2471e34150a0e750ef54089e2a3740 - [v3,3/4] remoteproc: Split rproc_ops allocation from rproc_alloc() https://git.kernel.org/andersson/remoteproc/c/bf860aa176d0104cfbaf863acbadf5548f1172c2 - [v3,4/4] remoteproc: Get rid of tedious error path https://git.kernel.org/andersson/remoteproc/c/226f5db4212438cdfe1a94652d74c6c01788a837
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.wiki.kernel.org/userdoc/pwbot