什么是“当前"?在 Linux 内核源代码中? [英] What is the "current" in Linux kernel source?

查看:15
本文介绍了什么是“当前"?在 Linux 内核源代码中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Linux 内核,但遇到了一个问题.

我看到许多 Linux 内核源文件都有 current->files.那么什么是current?

struct file *fget(unsigned int fd){结构文件*文件;struct files_struct *files = current->files;rcu_read_lock();文件 = fcheck_files(files, fd);如果(文件){/* 无法获取文件对象引用 */if (file->f_mode & FMODE_PATH ||!atomic_long_inc_not_zero(&file->f_count))文件 = NULL;}rcu_read_unlock();返回文件;}

解决方案

它是指向当前进程(即发出系统调用的进程)的指针.

在 x86 上,它在 arch/x86/include/asm/current.h 中定义(其他 arch 的类似文件).

#ifndef _ASM_X86_CURRENT_H#define _ASM_X86_CURRENT_H#include #include #ifndef __组装__结构体任务结构体;DECLARE_PER_CPU(struct task_struct *, current_task);静态 __always_inline struct task_struct *get_current(void){返回 percpu_read_stable(current_task);}#定义当前 get_current()#endif/* __汇编__ */#endif/* _ASM_X86_CURRENT_H */

Linux 设备驱动程序第 2 章中的更多信息:<块引用>

当前指针指向当前正在执行的用户进程.在执行系统调用(例如 open 或 read)期间,当前进程是调用该调用的进程.如果需要,内核代码可以通过使用 current 来使用特定于进程的信息.[...]

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;
 }

解决方案

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

这篇关于什么是“当前"?在 Linux 内核源代码中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆