kmemdup(9)
β¨ kmemdup β the kernel's very own copy machine π
okay so picture this: you've got a chunk of memory and you literally just need another one exactly like it. same bytes, same energy, brand new home. that's kmemdup, bestie. it's in the Kernel API Manual (section 9, the cool one) and it's giving "duplicate region of memory" π«Ά
πΈ SYNOPSIS πΈ
void * kmemdup (const void *src , size_t len , gfp_t gfp );
three arguments, one very cute little pointer coming back at you. no cap β¨
π ARGUMENTS π
srcβ the memory region you wanna duplicate. your original, your blueprint, your main character π¦lenβ how long that region is. gotta tell her the size, she's not a mind reader πgfpβ the GFP mask to use. that's the allocation vibes you're handing over to the memory gods π
π RETURN π
you get back a shiny newly allocated copy of src β or NULL if something went wrong (error case, we don't love her but she exists π).
and here's the tea: the result is physically contiguous. fr fr. contiguous memory, no scattering, all snuggled together π§
β important bestie note: when you're done, free it with kfree. don't just leave her hanging, that's rude π
π SEE ALSO π
all the siblings and cousins, in the kernel file ./mm/util.c β go say hi π«Ά
kfree_const(9), __kmemdup_nul(9), kstrdup(9), kstrdup_const(9), kstrndup(9), kmemdup_array(9), kvmemdup(9), kmemdup_nul(9), memdup_user(9), vmemdup_user(9), strndup_user(9), memdup_user_nul(9), randomize_page(9), __account_locked_vm(9), account_locked_vm(9), __vmalloc_array(9), vmalloc_array(9), __vcalloc(9), vcalloc(9), folio_mapping(9), folio_copy(9), get_cmdline(9), mem_dump_obj(9), compat_set_desc_from_vma(9), __compat_vma_mmap(9), compat_vma_mmap(9), snapshot_page(9), mmap_action_prepare(9), mmap_action_complete(9), folio_pte_batch(9), page_range_contiguous(9)
and that's the whole vibe, bestie π this lil page lives in ./mm, dated October 2026, and it's part of the Kernel API Manual, section 9. slay, go duplicate some memory responsibly β¨ππ₯
π¦ 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