llist_for_each_safe(9)
β¨ 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β thestruct llist_nodeyou're gonna use as your loop cursor, like your little main character pointer πnβ anotherstruct 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:
init_llist_head(9)init_llist_node(9)llist_on_list(9)llist_entry(9)member_address_is_nonnull(9)llist_for_each(9)llist_for_each_entry(9)llist_for_each_entry_safe(9)llist_empty(9)llist_add_batch(9)llist_add(9)llist_del_all(9)llist_del_first_init(9)
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
- 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