bio_alloc_bioset(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’– bio_alloc_bioset(9) β€” allocate a bio for I/O πŸ’–

okay so real talk, bestie β€” this is a page straight out of the Kernel API Manual, section 9, and it's about getting yourself a shiny new bio so you can do some I/O. she's lowkey iconic and you're gonna understand her, trust. πŸ¦‹


✨ SYNOPSIS ✨

struct bio * bio_alloc_bioset (struct block_device *bdev , unsigned
short nr_vecs , blk_opf_t opf , gfp_t gfp , struct bio_set *bs );

like, five whole arguments. she's giving "main character with a lot of demands" but honestly every single one of them matters, so let's go through them one by one. πŸ’…


πŸŽ€ ARGUMENTS πŸŽ€

  • bdev β€” the block device you're allocating the bio for. and yes, she can be NULL, no cap, that's genuinely allowed. 🫢
  • nr_vecs β€” how many bvecs you want pre-allocated. like, reserve the seats now so nobody's scrambling later.
  • opf β€” the operation and flags for the bio. this is the "what are we even DOING here" argument.
  • gfp β€” the GFP_* mask handed to the slab allocator. classic memory-allocator business, iykyk.
  • bs β€” the bio_set you're allocating from. your home base, your safe space, your mempool neighborhood. 🏑

🌸 DESCRIPTION 🌸

So the whole gig: you allocate a bio from the mempools in bs. that's it, that's the vibe. ✨

BUT β€” and this is the part where you need to pay attention, bestie β€” if __GFP_DIRECT_REclaim is set, then bio_alloc will always be able to allocate a bio. why? mempool guarantees, babe. that's the whole magic trick.

to make that work, though, there's a rule and it is non-negotiable:

callers must never allocate more than 1 bio at a time from the general pool. one. uno. singular. 🫡

if you genuinely need more than 1 bio, then you have to always submit the previously allocated bio for IO before you attempt to allocate a new one. skip that step and you can cause deadlocks under memory pressure and nobody wants that energy in their kernel. πŸ’€


🧚 the submit_bio_noacct situation

note that when you're running under submit_bio_noacct β€” which, fyi, means literally any block driver β€” bios are not submitted until after you return. check the code in submit_bio_noacct that converts recursion into iteration, specifically to prevent stack overflows. iconic behavior honestly. πŸ‘‘

so normally, you'd think allocating multiple bios under submit_bio_noacct would be susceptible to deadlocks. and like... you're not wrong to worry! but we have deadlock avoidance code that resubmits any blocked bios from a rescuer thread. so there's a whole rescue squad on standby. πŸ¦Έβ€β™€οΈ


πŸ™ˆ the catch (yes there's a catch)

however β€” we do not guarantee forward progress for allocations from other mempools. so doing multiple allocations from the same mempool under submit_bio_noacct? should be avoided. hard avoid. like, don't.

instead, use bio_set's front_pad for per bio allocations. that's the move. that's the whole tip. πŸŽ€


⭐ RETURN ⭐

  • pointer to new bio on success πŸŽ‰
  • NULL on failure 😭

πŸ¦‹ SEE ALSO πŸ¦‹

kernel file ./block/bio.c, plus the whole friend group: bio_reset(9), bio_reuse(9), bio_chain(9), bio_chain_and_submit(9), bio_kmalloc(9), bio_truncate(9), guard_bio_eod(9), bio_put(9), bio_alloc_clone(9), bio_init_clone(9), bio_full(9), __bio_add_page(9), bio_add_virt_nofail(9), bio_add_page(9), bio_add_folio(9), bio_add_vmalloc_chunk(9), bio_add_vmalloc(9), bio_iov_iter_get_pages(9), bio_iov_iter_bounce(9), bio_iov_iter_unbounce(9), bio_await(9), submit_bio_wait(9), bdev_rw_virt(9), bio_copy_data(9), bio_endio(9), bio_split(9), bio_trim(9), bioset_init(9)

like, go read them all. it's giving study group. πŸ“šπŸ’–


✨ and that's the page, bestie ✨ this came from ./block, dated October 2026, in the Kernel API Manual β€” bio_alloc_bioset(9). now go forth and allocate responsibly, 1 bio at a time. mwah. πŸ’…πŸŽ€πŸŒΈ

πŸ“¦ where this page comes from