Re: [PATCH 1/5] mm/util: add kstrdup_const

Joe Perches
2015-01-12T17:13:55Z

On Mon, 2015-01-12 at 10:18 +0100, Andrzej Hajda wrote:
> The patch adds alternative version of kstrdup which returns pointer
> to constant char array. The function checks if input string is in
> persistent and read-only memory section, if yes it returns the input string,
> otherwise it fallbacks to kstrdup.
> kstrdup_const is accompanied by kfree_const performing conditional memory
> deallocation of the string.

trivia:

> diff --git a/mm/util.c b/mm/util.c
[]
> +void kfree_const(const void *x)
> +{
> + if (!is_kernel_rodata((unsigned long)x))
> + kfree(x);
> +}
> +EXPORT_SYMBOL(kfree_const);
[]
> +const char *kstrdup_const(const char *s, gfp_t gfp)
> +{
> + if (is_kernel_rodata((unsigned long)s))
> + return s;
> +
> + return kstrdup(s, gfp);
> +}
> +EXPORT_SYMBOL(kstrdup_const);

I think it'd be nicer if these used the same form
even if it's a vertical line or 2 longer

void kfree_const(const void *x)
{
if (is_kernel_rodata((unsigned long)x))
return;

kfree(x);
}