Kahn Cse
Kahn Cse

Reputation: 437

What is the "current" in Linux kernel source?

I'm studying about Linux kernel and I have a problem.

I see many Linux kernel source files have current->files. So what is the current?

struct file *fget(unsigned int fd)
{
     struct file *file;
     struct files_struct *files = current->files;

     rcu_read_lock();
     file = fcheck_files(files, fd);
     if (file) {
             /* File object ref couldn't be taken */
             if (file->f_mode & FMODE_PATH ||
                 !atomic_long_inc_not_zero(&file->f_count))
                     file = NULL;
     }
     rcu_read_unlock();

     return file;
 }

Upvotes: 37

Views: 43307

Answers (3)

lizebin
lizebin

Reputation: 3

this is ARM64 definition. in arch/arm64/include/asm/current.h, https://elixir.bootlin.com/linux/latest/source/arch/arm64/include/asm/current.h

struct task_struct;

/*
 * We don't use read_sysreg() as we want the compiler to cache the value where
 * possible.
 */
static __always_inline struct task_struct *get_current(void)
{
    unsigned long sp_el0;

    asm ("mrs %0, sp_el0" : "=r" (sp_el0));

    return (struct task_struct *)sp_el0;
}

#define current get_current()

which just use the sp_el0 register. As the pointer to current process's task_struct

Upvotes: 0

Mat
Mat

Reputation: 206727

It's a pointer to the current process (i.e. the process that issued the system call).

On x86, it's defined in arch/x86/include/asm/current.h (similar files for other archs).

#ifndef _ASM_X86_CURRENT_H
#define _ASM_X86_CURRENT_H

#include <linux/compiler.h>
#include <asm/percpu.h>

#ifndef __ASSEMBLY__
struct task_struct;

DECLARE_PER_CPU(struct task_struct *, current_task);

static __always_inline struct task_struct *get_current(void)
{
    return percpu_read_stable(current_task);
}

#define current get_current()

#endif /* __ASSEMBLY__ */

#endif /* _ASM_X86_CURRENT_H */

More information in Linux Device Drivers chapter 2:

The current pointer refers to the user process currently executing. During the execution of a system call, such as open or read, the current process is the one that invoked the call. Kernel code can use process-specific information by using current, if it needs to do so. [...]

Upvotes: 46

coredump
coredump

Reputation: 3107

Current is a global variable of type struct task_struct. You can find it's definition at [1].

Files is a struct files_struct and it contains information of the files used by the current process.

[1] http://students.mimuw.edu.pl/SO/LabLinux/PROCESY/ZRODLA/sched.h.html

Upvotes: 3

Related Questions