sg_split(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

🌸 sg_split(9) β€” the Kernel API Manual moment

okay so this is sg_split, from the kernel API manual, and yes bestie the (9) means it's a kernel-internal function, we're in the deep deep. no cap, this one is genuinely useful and also genuinely a little fiddly. let's get into it πŸ’…


✨ NAME ✨

sg_split β€” split a scatterlist into several scatterlists. that's it, that's the vibe. one sg list goes in, several sg lists come out. πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

int sg_split (struct scatterlist *in , const int in_mapped_nents , const
       off_t skip , const int nb_splits , const size_t *split_sizes , struct
       scatterlist **out , int *out_mapped_nents , gfp_t gfp_mask );

like, look at all those params. main character energy honestly. 🌈


πŸ’– ARGUMENTS πŸ’–

okay bestie, let's meet the cast:

  • in β€” the input sg list. the thing we're chopping up. βœ‚οΈ
  • in_mapped_nents β€” the result of a dma_map_sg(in, ...), or 0 if it's not mapped. so: the count you got back from mapping, or a big fat zero if no mapping happened.
  • skip β€” the number of bytes to skip in the input sg list. like scrolling past the intro, but in bytes. πŸ™ˆ
  • nb_splits β€” the number of desired sg outputs. how many pieces we're making, bestie.
  • split_sizes β€” the respective size of each output sg list in bytes. one size per output, in order. πŸ“
  • out β€” an array where to store the allocated output sg lists. this is where the magic lands. ✨
  • out_mapped_nents β€” the resulting sg lists mapped number of sg entries. Might be NULL if sglist not already mapped (in_mapped_nents = 0). ⚠️ so like, if you didn't map it, pass NULL here, fr fr.
  • gfp_mask β€” the allocation flag. the classic gfp_t situation, you know the drill. 🫢

πŸ”₯ DESCRIPTION πŸ”₯

This function splits the input sg list into nb_splits sg lists, which are allocated and stored into out. The in is split into:

  • out[0], which covers bytes [skip .. skip + split_sizes[0] - 1] of in
  • out[1], which covers bytes [skip + split_sizes[0] .. skip + split_sizes[0] + split_sizes[1] -1] etc ...

so yeah, each output picks up exactly where the last one left off, offset by skip at the start. mathematically tidy, we love to see it. 🧚

⚠️ heads up bestie: It will be the caller's duty to kfree out array members. not mine, not the function's β€” yours. the allocated output sg lists in out are your responsibility now. don't be the person who leaks them, i'm begging. 😭

Returns 0 upon success, or error code. so, 0 = slay, anything else = read the error code and cry a little. πŸ’€


πŸ‘‘ SEE ALSO πŸ‘‘

Kernel file ./lib/sg_split.c β€” go read the source, iykyk. that's where the real lore lives. ⭐


okay that's the whole page, bestie! written from the ./lib corner of the kernel, October 2026 edition. go split some scatterlists and remember to kfree πŸ’œπŸ«ΆπŸŒ·

πŸ“¦ where this page comes from