Skip to content

Commit 09d1ea1

Browse files
brglgregkh
authored andcommitted
devres: provide devm_kstrdup_const()
Provide 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. Make devm_kfree() check if the passed pointer doesn't point to .rodata and if so - don't actually destroy the resource. Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl> Reviewed-by: Bjorn Andersson <bjorn.andersson@linaro.org> Acked-by: Mike Rapoport <rppt@linux.vnet.ibm.com> Acked-by: Rasmus Villemoes <linux@rasmusvillemoes.dk> Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 59c3f82 commit 09d1ea1

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎drivers/base/devres.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <linux/slab.h>
1212
#include <linux/percpu.h>
1313

14+
#include <asm/sections.h>
15+
1416
#include "base.h"
1517

1618
struct devres_node {
@@ -822,6 +824,28 @@ char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp)
822824
}
823825
EXPORT_SYMBOL_GPL(devm_kstrdup);
824826

827+
/**
828+
* devm_kstrdup_const - resource managed conditional string duplication
829+
* @dev: device for which to duplicate the string
830+
* @s: the string to duplicate
831+
* @gfp: the GFP mask used in the kmalloc() call when allocating memory
832+
*
833+
* Strings allocated by devm_kstrdup_const will be automatically freed when
834+
* the associated device is detached.
835+
*
836+
* RETURNS:
837+
* Source string if it is in .rodata section otherwise it falls back to
838+
* devm_kstrdup.
839+
*/
840+
const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp)
841+
{
842+
if (is_kernel_rodata((unsigned long)s))
843+
return s;
844+
845+
return devm_kstrdup(dev, s, gfp);
846+
}
847+
EXPORT_SYMBOL_GPL(devm_kstrdup_const);
848+
825849
/**
826850
* devm_kvasprintf - Allocate resource managed space and format a string
827851
* into that.
@@ -889,6 +913,13 @@ void devm_kfree(struct device *dev, const void *p)
889913
{
890914
int rc;
891915

916+
/*
917+
* Special case: pointer to a string in .rodata returned by
918+
* devm_kstrdup_const().
919+
*/
920+
if (unlikely(is_kernel_rodata((unsigned long)p)))
921+
return;
922+
892923
rc = devres_destroy(dev, devm_kmalloc_release,
893924
devm_kmalloc_match, (void *)p);
894925
WARN_ON(rc);

‎include/linux/device.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,8 @@ static inline void *devm_kcalloc(struct device *dev,
696696
}
697697
extern void devm_kfree(struct device *dev, const void *p);
698698
extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
699+
extern const char *devm_kstrdup_const(struct device *dev,
700+
const char *s, gfp_t gfp);
699701
extern void *devm_kmemdup(struct device *dev, const void *src, size_t len,
700702
gfp_t gfp);
701703

0 commit comments

Comments
 (0)