kmap_atomic(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

✨ kmap_atomic (9) ✨

okay so real talk bestie, this is from the Kernel API Manual and it's giving "vintage, but make it deprecated" πŸ’…


🌸 NAME 🌸

kmap_atomic β€” atomically map a page for temporary usage. and yes. it's Deprecated! 🚩 (the exclamation mark is theirs, not mine, they're being dramatic)


πŸŽ€ SYNOPSIS πŸŽ€

void * kmap_atomic (const struct page *page );

πŸ¦‹ ARGUMENTS πŸ¦‹

  • page β€” pointer to the page you wanna map, that's it, that's the whole vibe 🫢

⭐ RETURN ⭐

it hands you back the virtual address of the mapping β€” that's your golden ticket to the page contents, no cap πŸ”‘


πŸ’– DESCRIPTION πŸ’–

so here's the tea β˜• β€” kmap_atomic is in fact just a wrapper around kmap_local_page, but it also disables pagefaults and, depending on your PREEMPT_RT configuration, also CPU migration and preemption. the way that's two extra side effects for free 😭

heads up: because those last two (CPU migration + preemption) are config-dependent, users should not count on them. don't build your whole personality around them, they might not show up.

mappings should always be released by kunmap_atomic. always always. no leaving the map open like an unread group chat πŸ™ˆ

Do not use in new code. use kmap_local_page instead. periodt. βœ‹


🌈 when do you actually reach for this?

it's used in atomic context when code wants to access the contents of a page that might be allocated from high memory (peep __GFP_HIGHMEM), for example a page in the pagecache. the API has two functions, and they can be used in a manner similar to the following:

// Find the page of interest.
struct page *page = find_get_page(mapping, offset);

// Gain access to the contents of that page.
void *vaddr = kmap_atomic(page);

// Do something to the contents of that page.
memset(vaddr, 0, PAGE_SIZE);

// Unmap that page.
kunmap_atomic(vaddr);
⚠️ iconic little trap: note that the kunmap_atomic call takes the result of the kmap_atomic call, not the argument. yes this has personally victimised people. you're welcome πŸ’€

🧚 the two-page situation

if you need to map two pages because you want to copy from one page to another, you need to keep the kmap_atomic calls strictly nested, like:

vaddr1 = kmap_atomic(page1); vaddr2 = kmap_atomic(page2);

memcpy(vaddr1, vaddr2, PAGE_SIZE);

kunmap_atomic(vaddr2); kunmap_atomic(vaddr1);

strictly nested meaning unmapping goes in the reverse order β€” last in, first out, very stack, very main character energy πŸ‘‘


πŸ‘‘ SEE ALSO πŸ‘‘

kernel file ./include/linux/highmem.h plus the whole friend group:


πŸ’œ sign-off πŸ’œ

this lil page lives in ./include/linux, stamped October 2026, and it's literally titled kmap_atomic(9) β€” go off queen, even if you are deprecated πŸŽ€βœ¨ bye bestie, use kmap_local_page fr fr 🫢

πŸ“¦ where this page comes from