前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >为什么 Linux 默认页大小是 “4KB”?

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

作者头像
酷酷的哀殿
修改2023-09-23 18:11:44
4.2K0
修改2023-09-23 18:11:44
举报
文章被收录于专栏:酷酷的哀殿酷酷的哀殿

经常看到一些博客在讲 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]。

代码语言:javascript
复制
 /* 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

代码语言:javascript
复制
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]

代码语言:javascript
复制
 ARM – 4k
 ARM64 – 4k or 64k
 MIPS – Widely Configurable
 x86 – 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.

参考文章

  1. https://github.com/search?l=C&q=%23define+PAGE_SHIFT+repo%3Atorvalds%2Flinux+path%3Aarch&type=Code
  2. https://github.com/torvalds/linux/blob/5b8b9d0c6d0e0f1993c6c56deaf9646942c49d94/arch/openrisc/include/asm/page.h#L21
  3. https://www.kernel.org/doc/ols/2002/ols2002-pages-573-593.pdf
  4. https://openrisc.io/or1k.html#__RefHeading__504779_595890882
  5. https://elinux.org/images/4/4c/Ott.pdf
  6. https://www.realworldtech.com/forum/?threadid=144991&curpostid=145006
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-09-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 酷酷的哀殿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、Linux 默认页大小不是 4KB
  • 2、Linux 默认页大小是对应架构的 MMU 管理的最小值
  • 3、x86 架构下,Linux 默认页大小是 4Kb
  • 4、Linux 的默认大小会控制在 16K 以内
  • 参考文章
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档