devm_kstrdup_const(9)
π devm_kstrdup_const(9) β from the Kernel API Manual, bestie
okay so we're in the Kernel API Manual today and the star of the show is devm_kstrdup_const β¨ it's that girl who duplicates your string, but only when she has to, and then cleans up after herself. very demure, very mindful.
β¨ NAME β¨
devm_kstrdup_const β resource managed conditional string duplication π
like, it duplicates a string, but conditionally, and it's managed by the device resource system. the whole package fr fr.
πΈ SYNOPSIS πΈ
const char * devm_kstrdup_const (struct device *dev , const char *s ,
gfp_t gfp );
that's the whole signature, no notes π
π¦ ARGUMENTS π¦
devβ the device you're duplicating the string for. it's the main character here, everything gets tied to it π«Άsβ the string to duplicate. the actual content, the moment, the vibe πgfpβ the GFP mask used in thekmalloccall when allocating memory. yes she does actually callkmalloc, we'll get to that π
π DESCRIPTION π
so here's the tea β β anything allocated by devm_kstrdup_const gets automatically freed when the associated device is detached. no manual kfree, no leak anxiety, no 3am debugging session. the devres system handles it. periodt π
β RETURN β
you get the source string itself back if it lives in the .rodata section β no allocation, she just points at it, iconic β otherwise it falls back to devm_kstrdup. so it's giving "I'll only do work if I genuinely have to" ππ₯
π SEE ALSO π
the kernel file ./drivers/base/devres.c and honestly the whole devres friend group:
__devres_alloc_node(9)devres_for_each_res(9)devres_free(9)devres_add(9)devres_find(9)devres_get(9)devres_remove(9)devres_destroy(9)devres_release(9)devres_release_all(9)devres_open_group(9)devres_close_group(9)devres_remove_group(9)devres_release_group(9)__devm_add_action(9)devm_remove_action_nowarn(9)devm_release_action(9)devm_kmalloc(9)devm_krealloc(9)devm_kstrdup(9)devm_kvasprintf(9)devm_kasprintf(9)devm_kfree(9)devm_kmemdup(9)devm_kmemdup_const(9)devm_get_free_pages(9)devm_free_pages(9)__devm_alloc_percpu(9)
go read them, iykyk ππ«
ngl bestie,.rodatameans your string is literally constant forever, so duplicating it would be a whole waste of akmalloc. that's exactly why this function exists. no cap π
okk that's the whole page!! brought to you from ./drivers/base, dated October 2026, and the page itself is devm_kstrdup_const(9) β slay, go free your strings (or don't, she's already handled π
πΈπ)
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms