xa_for_each_range(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… xa_for_each_range β€” from the Kernel API Manual, section 9 πŸŽ€

okay bestie, so this one is literally called xa_for_each_range(9) and it lives in the Kernel API Manual. let's get into it ✨


🌸 NAME 🌸

xa_for_each_range β€” bestie, it iterates over a portion of an XArray. not the whole thing. a portion. we're being picky and we're being efficient πŸ’…


✨ SYNOPSIS ✨

xa_for_each_range (xa , index , entry , start , last );

that's the whole vibe. five things go in, magic comes out πŸͺ„


🫢 ARGUMENTS 🫢

  • xa β€” the XArray itself. the container. the main character of this whole operation πŸ‘‘
  • index β€” the index of an entry. it's your lil position tracker, and yes it moves 🎯
  • entry β€” the entry retrieved from the array. this is the prize you get each loop 🎁
  • start β€” the first index to retrieve from the array. where the party begins 🌈
  • last β€” the last index to retrieve from the array. where the party ends, sadly 😭

πŸ¦‹ DESCRIPTION πŸ¦‹

so during the iteration, entry will have the value of the entry stored in xa at index. literally what it says on the tin, no cap ✨

and here's the cute part: you may modify index during the iteration if you want to skip or reprocess indices. iconic behaviour. you're driving, not the loop πŸš—πŸ’¨ and yes β€” it is safe to modify the array during the iteration. go off, bestie.

at the end of the iteration, entry will be set to NULL and index will have a value less than or equal to max. so no weird leftovers, no zombie pointers. clean exit 🧼

now the performance tea, and this part actually matters so listen up β˜•οΈ:

xa_for_each_range is O(n.log(n)) while xas_for_each is O(n). okay so the other one is technically faster, BUT πŸ‘€ β€” you have to handle your own locking with xas_for_each, and if you have to unlock after each iteration, it will also end up being O(n.log(n)). so the classic "it depends" situation, we love that for us πŸ™ˆ

⚠️ also important: xa_for_each_range will spin if it hits a retry entry. like, spin. forever-ish. if you intend to see retry entries, you should use the xas_for_each iterator instead. don't say i didn't warn you πŸ’€

and one last trade-off for the spreadsheet girlies: the xas_for_each iterator will expand into more inline code than xa_for_each_range. so it's a whole size-vs-flexibility moment βœ¨πŸ“Š


πŸ’œ CONTEXT πŸ’œ

any context. literally any. it takes and releases the RCU lock, so it handles its own biz 🫢 no preconditions, no drama.


πŸ”₯ SEE ALSO πŸ”₯

Kernel file ./include/linux/xarray.h β€” plus this whole friend group πŸ’–:


and that's the page, bestie πŸ§šπŸ’– this one ships out of ./include/linux, October 2026, and it's filed under xa_for_each_range(9) β€” go iterate responsibly, mwah πŸ’‹πŸŽ€

πŸ“¦ where this page comes from