What Is the "Current" in Linux Kernel Source

What is the current in Linux kernel source?

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

current in Linux kernel code

It is a pointer to the current process ie, the process which has issued the system call.

From the docs:

The Current Process

Although kernel modules don't execute sequentially as applications do,
most actions performed by the kernel are related to a specific
process. Kernel code can know the current process driving it by
accessing the global item current, a pointer to struct task_struct,
which as of version 2.4 of the kernel is declared in
<asm/current.h>, included by <linux/sched.h>. 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. An example of this
technique is presented in "Access Control on a Device File", in
Chapter 5, "Enhanced Char Driver Operations".

Actually, current is not properly a global variable any more, like it
was in the first Linux kernels. The developers optimized access to the
structure describing the current process by hiding it in the stack
page. You can look at the details of current in <asm/current.h>. While
the code you'll look at might seem hairy, we must keep in mind that
Linux is an SMP-compliant system, and a global variable simply won't
work when you are dealing with multiple CPUs. The details of the
implementation remain hidden to other kernel subsystems though, and a
device driver can just include and refer to the
current process.

From a module's point of view, current is just like the external
reference printk. A module can refer to current wherever it sees fit.
For example, the following statement prints the process ID and the
command name of the current process by accessing certain fields in
struct task_struct:

 printk("The process is \"%s\" (pid %i)\n",
current->comm, current->pid);

The command name stored in current->comm is the base name of the
program file that is being executed by the current process.

Why do we have to use another kernel source from the Internet when implementing a new system call?

I'm trying to add a system call to my OS and when I read the online tutorials, it always starts with downloading and extracting a kernel source code from the Internet.

Well, that's right. You need to modify the kernel source code in order to implement a new syscall.

why do we have to use another kernel source from the Internet?

It's not "another kernel source", it's just "a kernel source". You don't usually have the source code for your currently installed kernel already at hand.

Normally, most Linux distributions provide a binary package for the kernel itself (which is automatically installed), a package for its headers only (which can be used to compile new modules), and possibly a source package related the binary package.

For example, on Ubuntu or Debian (assuming that you have enabled source packages) you should be able to get the current kernel source:

apt-get source linux-image-$(uname -r)

Since the tutorial author cannot possibly know which kernel version or which Linux distribution you are using, or even if your distribution provides a kernel source package at all, they just tell you to download a kernel source package from that Linux kernel website. This also ensures you use the exact same version that is shown in the tutorial, to avoid any compatibility problem with newer/older kernel versions.

Furthermore, you usually don't want to play around with the kernel of the machine you are using, since if something bad happens, you can end up damaging your system. You usually want to use a virtual machine for experimenting.

Can we add the new system call to the running OS and compile it directly?

Not really, it's not possible to hot-patch a new syscall into a running kernel. Since you need to modify the source code, first of all you need to have the source. Secondly, you'll need to do whatever modification you need and then compile the new kernel. Thirdly, you'll need to properly install the new kernel and reboot the machine.

How does the kernel know what is the current thread?

  1. Attribute const means that the returned pointer will remain the same for the duration of the program. In practice, this is true only in the scope of the one thread, but I can't think of any situation where a compiler would even try to optimize accesses between threads.

  2. Using register and asm("sp") binds a variable to the hardware register called sp, i.e. current stack pointer. This way the code does not have to be written in assembler to access this register directly.

  3. THREAD_SIZE is a constant, which gives the amount of memory allocated for the thread's stack. I assume that it always has to be a power of 2, e.g. 8 kilobytes might be a typical value.

    The expression ~(THREAD_SIZE - 1) then gives a bitmask for getting rid of the actual stack address. For 8 kB stack, it would be 0xffffe000.

    By taking a bitwise and with the stack pointer value, we get the lowest address allocated for the stack. On this architecture, the thread information is stored there. This is simply a design decision, they could have used some other place for storing the information.

    The stack pointer is useful for getting the thread information because each thread will always have its own stack.

Naming a variable `current` in a kernel module leads to function declaration isn’t a prototype error

The kernel has a macro called current which is pointing to users currently executing process. As this book states,

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.

In other words, as @GilHamilton mentioned in the comments, current is #defined to the function get_current() in the kernel. Using current as a variable name will give a compile-time error!

Where do I find the version of a Linux kernel source tree?

You can find the version by running

make kernelversion

In the source tree



Related Topics



Leave a reply



Submit