0%

legacy_va_layout设置大堆


作者: 耗子007


设置/proc/sys/vm/legacy_va_layout为0,调整进程地址空间的heap增长方向为从上往下。
这样可以使得heap的上限超过2G。

1
2
0 ----------------------------------------------------------------------------------------- 3G -------- 4G
保留 | 代码段 | 数据段 | BSS段 | 堆-增长方向向上 | 内存映射 | 栈-增长方向向下 | 命令行参数 | 环境变量 内核段
1
2
3
4
/proc/sys/vm/legacy_va_layout (since Linux 2.6.9)
If nonzero, this disables the new 32-bit memory-mapping
layout; the kernel will use the legacy (2.4) layout for all
processes.

修改heap增长方向代码(以linux4.1的arm为例)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void arch_pick_mmap_layout(struct mm_struct *mm)
{
unsigned long random_factor = 0UL;

if (current->flags & PF_RANDOMIZE)
random_factor = arch_mmap_rnd();

if (mmap_is_legacy()) { //获取legacy_va_layout的值,非零使用正常模式
mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
mm->get_unmapped_area = arch_get_unmapped_area;
} else { //为零,使用自上向下的模式
mm->mmap_base = mmap_base(random_factor);
mm->get_unmapped_area = arch_get_unmapped_area_topdown;
}
}