一般來說,如果我們想在 Linux 內核中使用當前宏,我們應該:
#include <asm/current.h>
但有一個 asm-generic 版本:
#include <asm-generic/current.h>
在ASM版本實作了當前宏觀突破每CPU的變數,但ASM-通用版本實作過的thread_info當前的宏觀,這兩個是完全不同的。Linux kernel headers 組織說我們應該使用asm版本,其中包括asm/current.h,但是很多博客或書籍都說 x86 使用asm-generic版本來實作當前的宏,包括Linux Kernel Development , 3rd, 3 Process Management, Storeing the Process Descriptor。那么,x86 Linux 內核真正使用哪個版本,asm還是asm-generic?我如何確定 Linux 內核真正使用的是哪個版本?
uj5u.com熱心網友回復:
要使用的正確標頭是asm/current.h,不要使用asm-generic. 這asm真的適用于任何事情。asm-generic檔案夾中的頭檔案(顧名思義)作為宏/函式的“通用”默認實作提供,然后每個體系結構/arch/xxx都有自己的asm包含檔案夾,如果需要,它可以以特定于體系結構的方式定義相同的宏/函式.
這樣做是因為它可能是實際需要的(一些拱門可能有一個與通用的實作不兼容的實作)和性能,因為在特定的拱門下可能有更好和更優化的方法來實作相同的結果。
事實上,如果我們查看每個 arch 是如何定義的,get_current()或者get_current_thread_info()我們可以看到其中一些(例如 alpha、spark)在thread_info結構中保留對當前任務的參考,并thread_info在暫存器中保留指向當前任務的指標以提高性能。其他人直接current在暫存器中保存一個指標(例如powerpc 32bit),而其他人定義一個全域per-cpu變數(例如x86)。特別是在 x86 上,該thread_info結構甚至沒有指向當前任務的指標,它是一個非常簡單的 16 位元組結構,用于放入快取線以提高性能。
// example from /arch/powerpc/include/asm/current.h
/*
* We keep `current' in r2 for speed.
*/
register struct task_struct *current asm ("r2");
我如何確定 Linux 內核真正使用的是哪個版本?
好吧,讓我們簡單地看一下:
$ rg '#include. current\.h' | cat
security/landlock/ptrace.c:#include <asm/current.h>
security/landlock/syscalls.c:#include <asm/current.h>
sound/pci/rme9652/hdsp.c:#include <asm/current.h>
sound/pci/rme9652/rme9652.c:#include <asm/current.h>
net/ipv4/raw.c:#include <asm/current.h>
net/core/dev.c:#include <asm/current.h>
ipc/msg.c:#include <asm/current.h>
fs/quota/quota.c:#include <asm/current.h>
drivers/staging/media/atomisp/pci/hmm/hmm_bo.c:#include <asm/current.h>
fs/jfs/ioctl.c:#include <asm/current.h>
fs/hugetlbfs/inode.c:#include <asm/current.h>
drivers/parport/daisy.c:#include <asm/current.h>
...
如您所見,這asm/current.h是實際使用的唯一標頭。
我們還可以看到(至少從 v5.14 開始)似乎只有 arc 使用了“通用”版本:
$ rg '#include. generic. current\.h' | cat
arch/arc/include/asm/current.h:#include <asm-generic/current.h>
許多博客或書籍說 x86 使用 asm-generic 版本來實作當前的宏,包括 Linux Kernel Development, 3rd
我只能推測這些資源是很久以前撰寫的,基于相當舊的內核版本,在撰寫本文時可能使用了不同的包含系統(也許 x86 也曾經使用通用版本)。如果不是,那么這些資源很可能是錯誤的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/361330.html
