vmalloc返回的地址无法转换为物理地址的原因
我试图理解Linux中的内存管理。在vmalloc的情况下,我发现了这个 返回的地址无法转换为物理地址或 总线地址,因为您不能断言内存在物理上是可用的 相邻的 当我调用virt_to_phys()时,我会得到一个地址 你是说virt_to_phys的返回值不正确吗vmalloc返回的地址无法转换为物理地址的原因,c,linux,linux-kernel,linux-device-driver,mmu,C,Linux,Linux Kernel,Linux Device Driver,Mmu,我试图理解Linux中的内存管理。在vmalloc的情况下,我发现了这个 返回的地址无法转换为物理地址或 总线地址,因为您不能断言内存在物理上是可用的 相邻的 当我调用virt_to_phys()时,我会得到一个地址 你是说virt_to_phys的返回值不正确吗 #include <linux/kernel.h> #include <linux/module.h> #include <linux/vmalloc.h> #include <linux/m
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/vmalloc.h>
#include <linux/moduleparam.h>
MODULE_LICENSE("GPL");
static char *ptr;
int alloc_size = 4096*1234;
static int test_hello_init(void)
{
ptr = vmalloc(alloc_size);
if(!ptr) {
/* handle error */
pr_err("memory allocation failed\n");
return -ENOMEM;
} else {
int i;
for (i = 0; i < 1234; i++) {
pr_info("Page Index:%d\tPhysical address:%px\t Virtual Address:%llx\n",
i, ptr+(4096*i), virt_to_phys(ptr+(4096*i)));
}
}
return 0;
}
static void test_hello_exit(void)
{
vfree(ptr);
pr_info("Memory freed\n");
}
module_init(test_hello_init);
module_exit(test_hello_exit);
物理内存被分成许多页,每个页可能是一个4kib的区域;物理页面被映射到虚拟内存中,只要内核喜欢 对于
vmalloc()
;内核分配“随机”(非连续)物理页,并将它们映射到连续的虚拟地址。您可以将vmalloc()
返回的地址转换为物理地址,但它将仅是第一页中的物理地址,与下一页或任何其他页的物理地址无关
您可以将每个单独页面的虚拟地址转换为物理地址。例如,如果您使用
vmalloc()
分配1234页;然后(使用类似“的循环(virtual_address=start;virtual_address
)您可以找到1234个完全不相关的物理地址。物理内存被分成许多页,每个页可能是一个4kib的区域;物理页被映射到内核喜欢的任何虚拟内存中
对于vmalloc()
;内核分配“随机”(非连续)物理页,并将它们映射到连续的虚拟地址。您可以转换vmalloc()返回的地址
转换为物理地址,但它将仅是第一页中的物理地址,与下一页或任何其他页的物理地址无关
您可以将每个单独页面的虚拟地址转换为物理地址。例如,如果使用
vmalloc()
分配1234个页面;然后(使用类似“的循环(虚拟地址=开始;虚拟地址<结束;虚拟地址+=页面大小){
)您可以找到1234个完全不相关的物理地址。您知道什么是PMMU(分页内存管理单元)吗?您是否希望virt_to_phys(X+1)==virt_to_phys(X)+1
?不仅直接,例如X[1]
。为了澄清您的问题,您能举一个与您所问内容相关的代码示例吗?我这样问是因为“我只是想理解。”问题过于宽泛,很有可能被关闭。我将添加代码和输出以澄清问题。您似乎问了很多关于MMU的问题。我建议您先找一本好书读一读。您是否知道PMMU(分页内存管理单元)是什么?您是否希望virt_to_phys(X+1)==virt_to_phys(X)+1
?不仅是直接的,在例如X[1]
的情况下也是如此。为了澄清你的问题,你能举一个你所问的与之相关的代码的例子吗?我这样问是因为“我只是想理解。”问题过于宽泛,很有可能被关闭。我将添加代码和输出以澄清问题。您似乎问了很多关于MMU的问题。我建议您先找一本好书阅读。附加代码和代码的dmesg输出。Iam运行在64位上machine@md.jamal部件不能断言memory在物理上是连续的
并不意味着它不会在物理上是连续的。地址可能是连续的,也可能不是。您可以找到具有不同物理地址的连续虚拟地址,但您也可以找到两个连续虚拟地址具有连续的物理地址。您刚刚找到了一个物理地址连续的特定状态并不意味着每次都是如此。附加的代码和代码的dmesg输出。Iam在64位上运行machine@md.jamal部分不能断言内存是物理连续的
并不意味着它将不会\uuuuu.是物理连续的
。地址E可能是连续的。它们可能不是。您可以找到具有不同物理地址的连续虚拟地址,但也可以找到两个连续虚拟地址具有连续物理地址。您只是找到了物理地址连续的特定状态,并不意味着每次都是如此。
[26472.440426] Page Index:1217 Physical address:ffffa1f544336000 Virtual Address:12ce44336000
[26472.440427] Page Index:1218 Physical address:ffffa1f544337000 Virtual Address:12ce44337000
[26472.440427] Page Index:1219 Physical address:ffffa1f544338000 Virtual Address:12ce44338000
[26472.440428] Page Index:1220 Physical address:ffffa1f544339000 Virtual Address:12ce44339000
[26472.440428] Page Index:1221 Physical address:ffffa1f54433a000 Virtual Address:12ce4433a000
[26472.440428] Page Index:1222 Physical address:ffffa1f54433b000 Virtual Address:12ce4433b000
[26472.440429] Page Index:1223 Physical address:ffffa1f54433c000 Virtual Address:12ce4433c000
[26472.440429] Page Index:1224 Physical address:ffffa1f54433d000 Virtual Address:12ce4433d000
[26472.440429] Page Index:1225 Physical address:ffffa1f54433e000 Virtual Address:12ce4433e000
[26472.440430] Page Index:1226 Physical address:ffffa1f54433f000 Virtual Address:12ce4433f000
[26472.440430] Page Index:1227 Physical address:ffffa1f544340000 Virtual Address:12ce44340000
[26472.440430] Page Index:1228 Physical address:ffffa1f544341000 Virtual Address:12ce44341000
[26472.440431] Page Index:1229 Physical address:ffffa1f544342000 Virtual Address:12ce44342000
[26472.440431] Page Index:1230 Physical address:ffffa1f544343000 Virtual Address:12ce44343000
[26472.440431] Page Index:1231 Physical address:ffffa1f544344000 Virtual Address:12ce44344000
[26472.440432] Page Index:1232 Physical address:ffffa1f544345000 Virtual Address:12ce44345000
[26472.440432] Page Index:1233 Physical address:ffffa1f544346000 Virtual Address:12ce44346000