llist_for_each_safe(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

✨ llist_for_each_safe(9) ✨

ok so this is from the Kernel API Manual and bestie it is a whole vibe β€” it's <pink>llist_for_each_safe</pink>, the lock-less list iterator that doesn't freak out when stuff gets yeeted mid-loop πŸ’…

πŸŽ€ NAME πŸŽ€

llist_for_each_safe β€” iterate over some deleted entries of a lock-less list, safe against removal of list entry πŸ¦‹

🌸 SYNOPSIS 🌸

llist_for_each_safe (pos , n , node );

✨ ARGUMENTS ✨

  • pos β€” the struct llist_node you're gonna use as your loop cursor, like your little main character pointer πŸ‘‘
  • n β€” another struct llist_node, this one just chilling as temporary storage so nothing breaks on you πŸ™ˆ
  • node β€” the first entry of the deleted list entries. like, the entry point. the doorway. the beginning of the deleted-era πŸšͺ

πŸ’– DESCRIPTION πŸ’–

ok so here's the tea: in general, some entries of the lock-less list can be traversed safely only after being deleted from the list. so you start with an entry instead of the list head. that's the whole bit, that's the move 🧚

and ngl this part matters: if you're using it on entries deleted from the lock-less list directly, the traverse order is from the newest to the oldest added entry. so it's giving reverse-chronological πŸ”₯

if you want to traverse from the oldest to the newest, you must reverse the order by yourself before traversing. no cap, it will not do that for you, you have to do it yourself 😭

fr fr do not assume it's gonna hand you oldest-first. it's not. flip it yourself first or cry about it later πŸ’€

πŸ’œ SEE ALSO πŸ’œ

Kernel file ./include/linux/llist.h β€” and the whole friend group:


that's the whole page bestie, go be safe out there 🌈🫢

signed off from ./include/linux in October 2026 β€” llist_for_each_safe(9) periodt πŸ’…

πŸ“¦ where this page comes from