linux下默认大小是什么,为什么 Linux 默认页大小是 “4KB”?

经常看到一些博客在讲 Linux 内存的 PAGE SIZE时,都会提到 Linux 默认页大小是 4KB。

笔者通过搜索找到了一些与 PAGE SIZE相关的资料,希望对读者有所帮助。

因为微信不支持外链,建议点击文章底部的“阅读原文”进行阅读。

1、Linux 默认页大小不是 4KB

首先,我们先看看 Linux 默认页大小是 4KB是否能够成立?

通过 github 以 #define PAGE_SHIFT为关键字在 linux 仓库进行搜索,可以得到一个 GitHub 搜索结果页 [1]。

很遗憾, GitHub 搜索结果页的第一个结果就不满足题目中的 4KB要求。

现将第一个结果 linux/arch/openrisc/include/asm/page.h 的部分代码摘录如下 [2]。

/* PAGE_SHIFT determines the page size */#define PAGE_SHIFT 13#ifdef __ASSEMBLY__#define PAGE_SIZE (1 << PAGE_SHIFT)#else#define PAGE_SIZE (1UL << PAGE_SHIFT)

在 openrisc架构下, PAGE SIZE 是 8 Kbyte (2^13)。

2、Linux 默认页大小是对应架构的 MMU 管理的最小值

本结论来自一篇2002年的文章 Multiple Page Size Support in the Linux Kernel [3]。

The Linux kernel currently supports a single user space page size, usually the minimum dictated by the architecture. This paper describes the ongoing modifications to the Linux kernel to allow applications to vary the size of pages used to map their address spaces and to reap the performance benefits associated with the use of large pages.

我们仍然以 openrisc架构为例,在OpenRISC 1000Architecture Manual 的  8.1 MMU FEATURES [4] 提到 page size存在3种,最小是 8 Kbyte。

Three different page sizes:Level 0 pages (32 Gbyte; only with 64-bit EA) translated with D/I Area Translation Buffer (ATB)Level 1 pages (16 MByte) translated with D/I Area Translation Buffer (ATB)Level 2 pages (8 Kbyte) translated with D/I Translation Lookaside Buffer (TLB)

这份数据可以与上一节的 #define PAGE_SHIFT 13相互印证。

3、x86 架构下,Linux 默认页大小是 4Kb

x86 架构下,Linux 默认页大小是 4Kb的原因很简单,x86 的 MMU 管理的最小值就是 4k。

数据来源: Virtual Memory and Linux [5]

ARM – 4kARM64 – 4k or 64kMIPS – Widely Configurablex86 – 4k

4、Linux 的默认大小会控制在 16K 以内

本结论来自一篇 Linus Torvalds 的吐槽文章 Some alternatives for alias handling [6]。

4kB is good. 8kB is borderline ok. 16kB or more is simply not acceptable.

参考文章

https://github.com/search?l=C&q=%23define+PAGE_SHIFT+repo%3Atorvalds%2Flinux+path%3Aarch&type=Code

https://github.com/torvalds/linux/blob/5b8b9d0c6d0e0f1993c6c56deaf9646942c49d94/arch/openrisc/include/asm/page.h#L21

https://www.kernel.org/doc/ols/2002/ols2002-pages-573-593.pdf

https://openrisc.io/or1k.html#__RefHeading__504779_595890882

https://elinux.org/images/4/4c/Ott.pdf

https://www.realworldtech.com/forum/?threadid=144991&curpostid=145006

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值