clinuxubuntulinux-device-driversudo

Is task->state deprecated after 5.6 Linux kernel versions?


Consider:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/sched/signal.h>
#include <linux/jiffies.h>

MODULE_LICENSE("GPL");

static int __init process_init(void)
{
    struct task_struct *task;

    printk(KERN_INFO "Listing process CPU time and waiting time:\n");

    for_each_process(task)
    {
        // Convert CPU time from clock ticks to seconds
        unsigned long user_time = task->utime / HZ;
        unsigned long system_time = task->stime / HZ;

        // Context switches
        unsigned long voluntary_switches = task->nvcsw;
        unsigned long involuntary_switches = task->nivcsw;

        // Process state (to determine if it's waiting)
        long state = task->state;

        printk(KERN_INFO "Process: %s | PID: %d | User Time: %lus | System Time: %lus | State: %ld | Voluntary CS: %lu | Involuntary CS: %lu\n",
               task->comm, task->pid, user_time, system_time, state, voluntary_switches, involuntary_switches);
    }

    return 0;
}

static void __exit process_exit(void)
{
    printk(KERN_INFO "Exiting process waiting time module\n");
}

module_init(process_init);
module_exit(process_exit);

I'm learning Linux device driver development, and when I try to check the process state then I get the below error message. When state was removed, the code correctly compiled. What should I do?

Error message:

/root/MJ/waiting_time.c:26:28: error: ‘struct task_struct’ has no member named ‘state’; did you mean ‘stats’?
26 | long state = task->state;


Solution

  • Looking at the commit posted by 0andriy use:

    long state = READ_ONCE(task->__state);
    

    But you might be more interested in task_state_to_char function https://elixir.bootlin.com/linux/v6.13.7/source/include/linux/sched.h#L1663 .