2

I am trying to test suspend a specific process in Linux-5.8.18.
I wrote modified kernel to export freeze_task() (and also removed basic checking from it) as follows,

spin_lock_irqsave(&freezer_lock, flags);

if (!(p->flags & PF_KTHREAD))
    fake_signal_wake_up(p);
else
    wake_up_state(p, TASK_INTERRUPTIBLE);

spin_unlock_irqrestore(&freezer_lock, flags);
return true;

Then I wrote another kernel module to call this simplified freeze_task to suspend a specific process.
But I tried many times, that process is still running even after freeze_task() is being called.
So what is the internal mechanism to suspend a process in Linux?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
wangt13
  • 959
  • 7
  • 17
  • The function `freeze_task` is part of the mechanism which freezes the **entire system**. (It is used for putting the machine into the sleep, for future restoring). It is not intended to use the function or its parts for other purposes. BTW, as you could deduce from the function's name, `wake_up_state` does not *suspend* the process, it *awakes* it (the opposite action). – Tsyvarev Jun 09 '21 at 07:17
  • You are right, the function is a part of freezing entire system, but it is the core function to freeze each qualified process. For the user space process it will call fake_signal_wake_up(p) which calls signal_wake_up(p, 0) to the "suspend" the process of p. I could NOT find the internal of how does kernel stop that process. – wangt13 Jun 09 '21 at 08:07
  • Change the process state to not be runnable. – stark Jun 09 '21 at 09:51
  • I did try this way, but I hit another issue. For example, I set the running process to UNINTERRUPTIBLE state and need to tell scheduler to de-schedule the process. But unfortunately, I could NOT find a proper scheduler function to schedule out the modified process. Do you know which function should I call to tell scheduler explicitly schedule out a process? – wangt13 Jun 09 '21 at 23:46
  • Normally you wait for the process to yield. If you really want to clobber it right now, the usual method is SIGSTOP. – stark Jun 10 '21 at 13:13
  • Yep, SIGSTOP is one way. But I am trying to figuring out the internal mechanism of suspending (and thawing) a process in Linux kernel. Can I call yield_to() to the signaled process to ask scheduler re-evaluate/re-schedule the process when its state is being changed to TASK_STOPPED? – wangt13 Jun 11 '21 at 08:58
  • @wangt13 The internals of freezing the userspace and kernel threads are explained here: https://www.kernel.org/doc/html/latest/power/freezing-of-tasks.html. As someone has mentioned, this is in the context of the entire system, not single processes. You can experiment with cgroups to freeze individual tasks from userspace, no need to hack kernel modules for that. – AtomHeartFather Feb 04 '22 at 08:50

0 Answers0