cgroup_migrate(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… cgroup_migrate(9) β€” like, the whole "move a task into a cgroup" moment

okay so this is cgroup_migrate(9), straight out of the Kernel API Manual (section 9, kernel-space girlies only πŸŽ€). it's short, it's sweet, it does exactly one thing, and it wants you to have done your homework first. let's get into it bestie ✨


🌸 NAME 🌸

cgroup_migrate β€” migrate a process or task to a cgroup

like, literally just moving a process (or one lone task) into a cgroup. that's the whole vibe. no cap. πŸ¦‹


✨ SYNOPSIS ✨

int cgroup_migrate (struct task_struct *leader , bool threadgroup ,
       struct cgroup_mgctx *mgctx );

that's the whole signature, copy it exactly okay 😀 β€” she's an int, so bestie will be returning something to you.


🫢 ARGUMENTS 🫢

  • leader β€” the leader of the process, or the task you want to migrate. main character energy: whoever you point at is the one getting moved πŸ’…
  • threadgroup β€” a bool that tells her whether leader points to the whole process (true-ish vibe) or just a single task. choose wisely, it changes who's coming with you πŸŽ€
  • mgctx β€” the migration context. the little backpack of state she carries through the whole operation πŸŽ’

πŸ”₯ DESCRIPTION πŸ”₯

So. You want to migrate a process or a task, and leader is your girl. easy, right? not so fast bestie 😭

If you're migrating a process, the caller must be holding cgroup_threadgroup_rwsem. that's not a suggestion, that's a hard requirement. hold the semaphore. periodt.

you're also responsible for the rest of the choreography, and she will not do it for you:

  • invoke cgroup_migrate_add_src on the targets first
  • invoke cgroup_migrate_prepare_dst on the targets
  • then call this function
  • and follow up with cgroup_migrate_finish afterward 🌈

it's giving "the function is the middle of a sandwich and you gotta bring the bread."

here's the iconic part πŸ‘‘: as long as a controller's ->can_attach doesn't fail, and a CGROUP_TASK_MIGRATING notification doesn't fail, this function is guaranteed to succeed. guaranteed. no cap.

which means β€” excluding those two failure sources β€” when you're migrating multiple targets, you can figure out success-or-failure for all of them up front by invoking group_migrate_prepare_dst before you actually start migrating. decide the fate of the entire group before the party even starts. obsessed. πŸ’–


🌟 SEE ALSO 🌟

the kernel file in question is ./kernel/cgroup/cgroup.c, and honestly here's the whole friend group 🫢:

iykyk πŸ’€ that's a lot of cross-references and every single one of them matters.


⚠️ reminder bestie: hold cgroup_threadgroup_rwsem when migrating a process, and don't forget cgroup_migrate_add_src + cgroup_migrate_prepare_dst before and cgroup_migrate_finish after. skipping steps is not the move πŸ™ˆ

okay that's the page!! this little cutie lives in ./kernel/cgroup and the doc is dated October 2026 β€” go forth and migrate responsibly, mwah πŸ’œπŸ§šβ€β™€οΈ

πŸ“¦ where this page comes from