bio_alloc_bioset(9)
π 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 beNULL, 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β theGFP_*mask handed to the slab allocator. classic memory-allocator business, iykyk.bsβ thebio_setyou'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 π
NULLon 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
- 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