devres: provide and use devm_kstrdup_const()
Subsystems (5)
v2 cover devres: provide and use devm_kstrdup_const()
This series implements devm_kstrdup_const() together with some prerequisite changes and uses it in pmc-atom driver.
v1 -> v2:
- fixed the changelog in the patch implementing devm_kstrdup_const()
- fixed the kernel doc
- moved is_kernel_rodata() to asm-generic/sections.h
- fixed constness
Bartosz Golaszewski (4):
devres: constify p in devm_kfree()
mm: move is_kernel_rodata() to asm-generic/sections.h
devres: provide devm_kstrdup_const()
clk: pmc-atom: use devm_kstrdup_const()
drivers/base/devres.c | 43 ++++++++++++++++++++++++++++++++-- drivers/clk/x86/clk-pmc-atom.c | 19 ++++----------- include/asm-generic/sections.h | 14 +++++++++++ include/linux/device.h | 5 +++- mm/util.c | 7 ------ 5 files changed, 63 insertions(+), 25 deletions(-)
Signature · 1 line
2.18.0
PATCH v2 1/4 devres: constify p in devm_kfree()
Make devm_kfree() signature uniform with that of kfree(). To avoid compiler warnings: cast p to (void *) when calling devres_destroy().
--- drivers/base/devres.c | 5 +++-- include/linux/device.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)
drivers/base/devres.c +3 -2
@@ -885,11 +885,12 @@ EXPORT_SYMBOL_GPL(devm_kasprintf);* * Free memory allocated with devm_kmalloc(). */ -void devm_kfree (struct device *dev,void *p) +void devm_kfree (struct device *dev,const void *p) {int rc; - rc =devres_destroy (dev, devm_kmalloc_release, devm_kmalloc_match, p); + rc =devres_destroy (dev, devm_kmalloc_release, + devm_kmalloc_match, (void *)p);WARN_ON (rc); }EXPORT_SYMBOL_GPL (devm_kfree);
include/linux/device.h +1 -1
@@ -692,7 +692,7 @@ static inline void *devm_kcalloc(struct device *dev, {return devm_kmalloc_array (dev, n, size, flags | __GFP_ZERO); } -extern void devm_kfree (struct device *dev,void *p); +extern void devm_kfree (struct device *dev,const void *p);extern char *devm_kstrdup (struct device *dev,const char *s,gfp_t gfp) __malloc;extern void *devm_kmemdup (struct device *dev,const void *src,size_t len,gfp_t gfp);
Signature · 1 line
2.18.0
PATCH v2 2/4 mm: move is_kernel_rodata() to asm-generic/sections.h
In reply to Bartosz GolaszewskiExport this routine so that we can use it later in devm_kstrdup_const() and devm_kfree_const().
--- include/asm-generic/sections.h | 14 ++++++++++++++ mm/util.c | 7 ------- 2 files changed, 14 insertions(+), 7 deletions(-)
include/asm-generic/sections.h +14 -0
@@ -141,4 +141,18 @@ static inline bool init_section_intersects(void *virt, size_t size) return memory_intersects(__init_begin, __init_end, virt, size); } +/** +* is_kernel_rodata - checks if the pointer address is located in the +* .rodata section +* +* @addr: address to check +* +* Returns: true if the address is located in .rodata, false otherwise. +*/ +static inline bool is_kernel_rodata (unsigned long addr) +{ +return addr >= (unsigned long )__start_rodata && + addr < (unsigned long )__end_rodata; +} +#endif /* _ASM_GENERIC_SECTIONS_H_ */
mm/util.c +0 -7
@@ -15,17 +15,10 @@#include <linux/vmalloc.h> #include <linux/userfaultfd_k.h> -#include <asm/sections.h> #include <linux/uaccess.h> #include "internal.h" -static inline int is_kernel_rodata (unsigned long addr) -{ -return addr >= (unsigned long )__start_rodata && - addr < (unsigned long )__end_rodata; -} -/** * kfree_const - conditionally free memory * @x: pointer to the memory
Signature · 1 line
2.18.0
PATCH v2 3/4 devres: provide devm_kstrdup_const()
In reply to Bartosz GolaszewskiProvide a resource managed version of kstrdup_const(). This variant internally calls devm_kstrdup() on pointers that are outside of .rodata section and returns the string as is otherwise.
Also provide a corresponding version of devm_kfree().
--- drivers/base/devres.c | 38 ++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 3 +++ 2 files changed, 41 insertions(+)
drivers/base/devres.c +38 -0
@@ -11,6 +11,8 @@#include <linux/slab.h> #include <linux/percpu.h> +#include <asm/sections.h> +#include "base.h" struct devres_node {
@@ -822,6 +824,28 @@ char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) }EXPORT_SYMBOL_GPL (devm_kstrdup); +/** +* devm_kstrdup_const - resource managed conditional string duplication +* @dev: device for which to duplicate the string +* @s: the string to duplicate +* @gfp: the GFP mask used in the kmalloc() call when allocating memory +* +* Strings allocated by devm_kstrdup_const will be automatically freed when +* the associated device is detached. +* +* RETURNS: +* Source string if it is in .rodata section otherwise it falls back to +* devm_kstrdup. +*/ +const char *devm_kstrdup_const (struct device *dev,const char *s,gfp_t gfp) +{ +if (is_kernel_rodata ((unsigned long )s)) +return s; + +return devm_kstrdup (dev, s, gfp); +} +EXPORT_SYMBOL (devm_kstrdup_const); +/** * devm_kvasprintf - Allocate resource managed space and format a string * into that.
@@ -895,6 +919,20 @@ void devm_kfree(struct device *dev, const void *p) }EXPORT_SYMBOL_GPL (devm_kfree); +/** +* devm_kfree_const - Resource managed conditional kfree +* @dev: device this memory belongs to +* @p: memory to free +* +* Function calls devm_kfree only if @p is not in .rodata section. +*/ +void devm_kfree_const (struct device *dev,const void *p) +{ +if (!is_kernel_rodata ((unsigned long )p)) +devm_kfree (dev, p); +} +EXPORT_SYMBOL (devm_kfree_const); +/** * devm_kmemdup - Resource-managed kmemdup * @dev: Device this memory belongs to
include/linux/device.h +3 -0
@@ -693,7 +693,10 @@ static inline void *devm_kcalloc(struct device *dev,return devm_kmalloc_array (dev, n, size, flags | __GFP_ZERO); }extern void devm_kfree (struct device *dev,const void *p); +extern void devm_kfree_const (struct device *dev,const void *p);extern char *devm_kstrdup (struct device *dev,const char *s,gfp_t gfp) __malloc; +extern const char *devm_kstrdup_const (struct device *dev, +const char *s,gfp_t gfp);extern void *devm_kmemdup (struct device *dev,const void *src,size_t len,gfp_t gfp);
Signature · 1 line
2.18.0
PATCH v2 4/4 clk: pmc-atom: use devm_kstrdup_const()
In reply to Bartosz GolaszewskiUse devm_kstrdup_const() in the pmc-atom driver. This mostly serves as an example of how to use this new routine to shrink driver code.
While we're at it: replace a call to kcalloc() with devm_kcalloc().
--- drivers/clk/x86/clk-pmc-atom.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-)
drivers/clk/x86/clk-pmc-atom.c +4 -15
@@ -253,14 +253,6 @@ static void plt_clk_unregister_fixed_rate_loop(struct clk_plt_data *data,plt_clk_unregister_fixed_rate (data->parents[i]); } -static void plt_clk_free_parent_names_loop (const char **parent_names, -unsigned int i) -{ -while (i--) -kfree_const (parent_names[i]); -kfree (parent_names); -} -static void plt_clk_unregister_loop (struct clk_plt_data *data,unsigned int i) {
@@ -286,8 +278,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,if (!data->parents)return ERR_PTR (-ENOMEM ); - parent_names =kcalloc (nparents,sizeof (*parent_names), -GFP_KERNEL ); + parent_names =devm_kcalloc (&pdev->dev, nparents, +sizeof (*parent_names),GFP_KERNEL );if (!parent_names)return ERR_PTR (-ENOMEM );
@@ -300,7 +292,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev, err =PTR_ERR (data->parents[i]); goto err_unreg; } - parent_names[i] =kstrdup_const (clks[i].name,GFP_KERNEL ); + parent_names[i] =devm_kstrdup_const (&pdev->dev, + clks[i].name,GFP_KERNEL ); } data->nparents = nparents;
@@ -308,7 +301,6 @@ static const char **plt_clk_register_parents(struct platform_device *pdev, err_unreg:plt_clk_unregister_fixed_rate_loop (data, i); -plt_clk_free_parent_names_loop (parent_names, i);return ERR_PTR (err); }
@@ -351,15 +343,12 @@ static int plt_clk_probe(struct platform_device *pdev) goto err_unreg_clk_plt; } -plt_clk_free_parent_names_loop (parent_names, data->nparents); -platform_set_drvdata (pdev, data);return 0 ; err_unreg_clk_plt:plt_clk_unregister_loop (data, i);plt_clk_unregister_parents (data); -plt_clk_free_parent_names_loop (parent_names, data->nparents);return err; }
Signature · 1 line
2.18.0
Re: [PATCH v2 4/4] clk: pmc-atom: use devm_kstrdup_const()
Quoting Bartosz Golaszewski (2018-08-28 02:33:32)
Quoted 9 lines
Use
devm_kstrdup_const()in the pmc-atom driver. This mostly serves as an example of how to use this new routine to shrink driver code. =While we're at it: replace a call to
kcalloc()withdevm_kcalloc(). =Signed-off-by Bartosz Golaszewski <[email protected]>---
If you want this example to be merged through the clk tree please resend after the other patches merge.
2018-08-28 11:33 GMT+02:00 Bartosz Golaszewski <[email protected]>:
Quoted 22 lines
This series implements
devm_kstrdup_const()together with some prerequisite changes and uses it in pmc-atom driver.v1 -> v2:
- fixed the changelog in the patch implementingdevm_kstrdup_const()
- fixed the kernel doc
- movedis_kernel_rodata()toasm-generic/sections.h
- fixed constnessBartosz Golaszewski (4):
devres: constify p indevm_kfree()
mm: moveis_kernel_rodata()toasm-generic/sections.h
devres: providedevm_kstrdup_const()
clk: pmc-atom: usedevm_kstrdup_const()drivers/base/devres.c | 43 ++++++++++++++++++++++++++++++++-- drivers/clk/x86/clk-pmc-atom.c | 19 ++++----------- include/asm-generic/sections.h | 14 +++++++++++ include/linux/device.h | 5 +++- mm/util.c | 7 ------ 5 files changed, 63 insertions(+), 25 deletions(-)
Quoted 3 lines
--
2.18.0
If there are no objections - can this be picked up for 4.20?
Thanks,
Bartosz
Re: [PATCH v2 1/4] devres: constify p in devm_kfree()
In reply to Bartosz GolaszewskiQuoted 3 lines · Bartosz Golaszewski
Make
devm_kfree()signature uniform with that ofkfree(). To avoid compiler warnings: cast p to (void *) when callingdevres_destroy().
Regards,
Bjorn
Quoted 41 lines
Signed-off-by Bartosz Golaszewski <[email protected]>--- drivers/base/devres.c | 5 +++-- include/linux/device.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)drivers/base/devres.c +3
-2@@ -885,11 +885,12 @@ EXPORT_SYMBOL_GPL(devm_kasprintf);* * Free memory allocated with devm_kmalloc(). */ -void devm_kfree (struct device *dev,void *p) +void devm_kfree (struct device *dev,const void *p) {int rc; - rc =devres_destroy (dev, devm_kmalloc_release, devm_kmalloc_match, p); + rc =devres_destroy (dev, devm_kmalloc_release, + devm_kmalloc_match, (void *)p);WARN_ON (rc); }EXPORT_SYMBOL_GPL (devm_kfree);include/linux/device.h +1
-1@@ -692,7 +692,7 @@ static inline void *devm_kcalloc(struct device *dev, {return devm_kmalloc_array (dev, n, size, flags | __GFP_ZERO); } -extern void devm_kfree (struct device *dev,void *p); +extern void devm_kfree (struct device *dev,const void *p);extern char *devm_kstrdup (struct device *dev,const char *s,gfp_t gfp) __malloc;extern void *devm_kmemdup (struct device *dev,const void *src,size_t len,gfp_t gfp);Signature · 1 line
2.18.0
Re: [PATCH v2 2/4] mm: move is_kernel_rodata() to asm-generic/sections.h
In reply to Bartosz GolaszewskiQuoted 3 lines · Bartosz Golaszewski
Export this routine so that we can use it later in
devm_kstrdup_const()anddevm_kfree_const().
Regards,
Bjorn
Quoted 54 lines
Signed-off-by Bartosz Golaszewski <[email protected]>--- include/asm-generic/sections.h | 14 ++++++++++++++ mm/util.c | 7 ------- 2 files changed, 14 insertions(+), 7 deletions(-)include/asm-generic/sections.h +14
-0@@ -141,4 +141,18 @@ static inline bool init_section_intersects(void *virt, size_t size) return memory_intersects(__init_begin, __init_end, virt, size); } +/** +* is_kernel_rodata - checks if the pointer address is located in the +* .rodata section +* +* @addr: address to check +* +* Returns: true if the address is located in .rodata, false otherwise. +*/ +static inline bool is_kernel_rodata (unsigned long addr) +{ +return addr >= (unsigned long )__start_rodata && + addr < (unsigned long )__end_rodata; +} +#endif /* _ASM_GENERIC_SECTIONS_H_ */ mm/util.c +0
-7@@ -15,17 +15,10 @@#include <linux/vmalloc.h> #include <linux/userfaultfd_k.h> -#include <asm/sections.h> #include <linux/uaccess.h> #include "internal.h" -static inline int is_kernel_rodata (unsigned long addr) -{ -return addr >= (unsigned long )__start_rodata && - addr < (unsigned long )__end_rodata; -} -/** * kfree_const - conditionally free memory * @x: pointer to the memory Signature · 1 line
2.18.0
Re: [PATCH v2 3/4] devres: provide devm_kstrdup_const()
In reply to Bartosz GolaszewskiQuoted 7 lines · Bartosz Golaszewski
Provide a resource managed version of
kstrdup_const(). This variant internally callsdevm_kstrdup()on pointers that are outside of .rodata section and returns the string as is otherwise.Also provide a corresponding version of
devm_kfree().Signed-off-by Bartosz Golaszewski <[email protected]>
Regards,
Bjorn
Quoted 86 lines
--- drivers/base/devres.c | 38 ++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 3 +++ 2 files changed, 41 insertions(+)drivers/base/devres.c +38
-0@@ -11,6 +11,8 @@#include <linux/slab.h> #include <linux/percpu.h> +#include <asm/sections.h> +#include "base.h" struct devres_node {@@ -822,6 +824,28 @@ char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) }EXPORT_SYMBOL_GPL (devm_kstrdup); +/** +* devm_kstrdup_const - resource managed conditional string duplication +* @dev: device for which to duplicate the string +* @s: the string to duplicate +* @gfp: the GFP mask used in the kmalloc() call when allocating memory +* +* Strings allocated by devm_kstrdup_const will be automatically freed when +* the associated device is detached. +* +* RETURNS: +* Source string if it is in .rodata section otherwise it falls back to +* devm_kstrdup. +*/ +const char *devm_kstrdup_const (struct device *dev,const char *s,gfp_t gfp) +{ +if (is_kernel_rodata ((unsigned long )s)) +return s; + +return devm_kstrdup (dev, s, gfp); +} +EXPORT_SYMBOL (devm_kstrdup_const); +/** * devm_kvasprintf - Allocate resource managed space and format a string * into that. @@ -895,6 +919,20 @@ void devm_kfree(struct device *dev, const void *p) }EXPORT_SYMBOL_GPL (devm_kfree); +/** +* devm_kfree_const - Resource managed conditional kfree +* @dev: device this memory belongs to +* @p: memory to free +* +* Function calls devm_kfree only if @p is not in .rodata section. +*/ +void devm_kfree_const (struct device *dev,const void *p) +{ +if (!is_kernel_rodata ((unsigned long )p)) +devm_kfree (dev, p); +} +EXPORT_SYMBOL (devm_kfree_const); +/** * devm_kmemdup - Resource-managed kmemdup * @dev: Device this memory belongs to include/linux/device.h +3
-0@@ -693,7 +693,10 @@ static inline void *devm_kcalloc(struct device *dev,return devm_kmalloc_array (dev, n, size, flags | __GFP_ZERO); }extern void devm_kfree (struct device *dev,const void *p); +extern void devm_kfree_const (struct device *dev,const void *p);extern char *devm_kstrdup (struct device *dev,const char *s,gfp_t gfp) __malloc; +extern const char *devm_kstrdup_const (struct device *dev, +const char *s,gfp_t gfp);extern void *devm_kmemdup (struct device *dev,const void *src,size_t len,gfp_t gfp);Signature · 1 line
2.18.0
Re: [PATCH v2 4/4] clk: pmc-atom: use devm_kstrdup_const()
In reply to Bartosz GolaszewskiQuoted 6 lines · Bartosz Golaszewski
Use
devm_kstrdup_const()in the pmc-atom driver. This mostly serves as an example of how to use this new routine to shrink driver code.While we're at it: replace a call to
kcalloc()withdevm_kcalloc().Signed-off-by Bartosz Golaszewski <[email protected]>
Regards,
Bjorn
Quoted 71 lines
--- drivers/clk/x86/clk-pmc-atom.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-)drivers/clk/x86/clk-pmc-atom.c +4
-15@@ -253,14 +253,6 @@ static void plt_clk_unregister_fixed_rate_loop(struct clk_plt_data *data,plt_clk_unregister_fixed_rate (data->parents[i]); } -static void plt_clk_free_parent_names_loop (const char **parent_names, -unsigned int i) -{ -while (i--) -kfree_const (parent_names[i]); -kfree (parent_names); -} -static void plt_clk_unregister_loop (struct clk_plt_data *data,unsigned int i) {@@ -286,8 +278,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev,if (!data->parents)return ERR_PTR (-ENOMEM ); - parent_names =kcalloc (nparents,sizeof (*parent_names), -GFP_KERNEL ); + parent_names =devm_kcalloc (&pdev->dev, nparents, +sizeof (*parent_names),GFP_KERNEL );if (!parent_names)return ERR_PTR (-ENOMEM );@@ -300,7 +292,8 @@ static const char **plt_clk_register_parents(struct platform_device *pdev, err =PTR_ERR (data->parents[i]); goto err_unreg; } - parent_names[i] =kstrdup_const (clks[i].name,GFP_KERNEL ); + parent_names[i] =devm_kstrdup_const (&pdev->dev, + clks[i].name,GFP_KERNEL ); } data->nparents = nparents;@@ -308,7 +301,6 @@ static const char **plt_clk_register_parents(struct platform_device *pdev, err_unreg:plt_clk_unregister_fixed_rate_loop (data, i); -plt_clk_free_parent_names_loop (parent_names, i);return ERR_PTR (err); }@@ -351,15 +343,12 @@ static int plt_clk_probe(struct platform_device *pdev) goto err_unreg_clk_plt; } -plt_clk_free_parent_names_loop (parent_names, data->nparents); -platform_set_drvdata (pdev, data);return 0 ; err_unreg_clk_plt:plt_clk_unregister_loop (data, i);plt_clk_unregister_parents (data); -plt_clk_free_parent_names_loop (parent_names, data->nparents);return err; }Signature · 1 line
2.18.0
Stephen Boyd
maintainerQuoting Bartosz Golaszewski (2018-09-20 05:59:54)
Quoted 23 lines
2018-08-28 11:33 GMT+02:00 Bartosz Golaszewski <[email protected]>:
Quoted 22 lines
This series implements
devm_kstrdup_const()together with some prerequisite changes and uses it in pmc-atom driver.v1 -> v2:
- fixed the changelog in the patch implementingdevm_kstrdup_const()
- fixed the kernel doc
- movedis_kernel_rodata()toasm-generic/sections.h
- fixed constnessBartosz Golaszewski (4):
devres: constify p indevm_kfree()
mm: moveis_kernel_rodata()toasm-generic/sections.h
devres: providedevm_kstrdup_const()
clk: pmc-atom: usedevm_kstrdup_const()drivers/base/devres.c | 43 ++++++++++++++++++++++++++++++++-- drivers/clk/x86/clk-pmc-atom.c | 19 ++++----------- include/asm-generic/sections.h | 14 +++++++++++ include/linux/device.h | 5 +++- mm/util.c | 7 ------ 5 files changed, 63 insertions(+), 25 deletions(-)
Quoted 6 lines
Quoted 3 lines
--
2.18.0If there are no objections - can this be picked up for 4.20?
There are so many people on To: line who do you want to pick this up? Maybe you can send a pull request to Greg directly.