35

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;
 }
jtepe
  • 3,242
  • 2
  • 24
  • 31
Kahn Cse
  • 397
  • 2
  • 5
  • 10
  • Example? `current` is a really generic variable name. – nneonneo Sep 15 '12 at 05:03
  • 1
    @nneonneo Yes, but also its usage is very generic (at least in the linux kernel): it refers the `struct` describing the currently running process. – peterh Jul 08 '19 at 14:37

3 Answers3

45

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. [...]

jtepe
  • 3,242
  • 2
  • 24
  • 31
Mat
  • 202,337
  • 40
  • 393
  • 406
  • As unfortunately happens a lot in software, this variable name isn't as intuitive as it could be. A name such as curr_process instead of current would make the Linux kernel code much easier to read. – Alan Evangelista Feb 15 '21 at 03:39
  • current.h has been moved to arch/x86/include/asm. Please update the answer – Alan Evangelista Feb 15 '21 at 03:50
3

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

coredump
  • 3,017
  • 6
  • 35
  • 53
  • 3
    `current` is not a variable but a `#define` to a function that returns `struct task_struct *` – Quaker Aug 21 '17 at 09:11
  • @Quaker It might have been true in 2012, as this answer was posted. I think, some check in the kernel git could help, and then upvoting + improving the answer based on the result (or voting it down). – peterh Jul 08 '19 at 14:40
0

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

jtepe
  • 3,242
  • 2
  • 24
  • 31
lizebin
  • 3
  • 2