sys_brk的资格要求是什么 [英] What is the aligment requirements for sys_brk

查看:102
本文介绍了sys_brk的资格要求是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sys_brk syscall在堆中动态分配内存.我注意到,在获取当前的休息位置时,我通常会获得类似于以下的值:

I'm using sys_brk syscall to dynamically allocate memory in the heap. I noticed that when acquiring the current break location I usually get value similar to this:

mov rax, 0x0C
mov rdi, 0x00
syscall

产生

rax   0x401000

该值通常对齐512个字节.因此,我想问一下中断值是否有对齐要求?还是我们可以按照自己的方式调整它的位置?

The value usually 512 bytes aligned. So I would like to ask is there some alignment requirements on the break value? Or we can misalign it the way we want?

推荐答案

内核确实以字节粒度跟踪中断.但是,如果您完全关心性能,请不要将其直接用于少量分配.

The kernel does track the break with byte granularity. But don't use it directly for small allocations if you care at all about performance.

评论中有一些讨论,关于内核将中断四舍五入到页面边界,但事实并非如此. sys_brk 的实现使用此(添加了我的评论,因此在上下文之外也很有意义

There was some discussion in comments about the kernel rounding the break to a page boundary, but that's not the case. The implementation of sys_brk uses this (with my comments added so it makes sense out of context)

newbrk = PAGE_ALIGN(brk);     // the syscall arg
oldbrk = PAGE_ALIGN(mm->brk); // the current break
if (oldbrk == newbrk)
    goto set_brk;      // no need to map / unmap any pages, just update mm->brk

这将检查中断是否移至另一页,但最终mm->brk = brk;会将当前中断设置为传递给系统调用的确切arg(如果有效).如果当前中断始终是页面对齐的,则内核将不需要PAGE_ALIGN().

This checks if the break moved to a different page, but eventually mm->brk = brk; sets the current break to the exact arg passed to the system call (if it's valid). If the current break was always page aligned, the kernel wouldn't need PAGE_ALIGN() on it.

当然,内存保护至少具有页面粒度(如果内核选择对此映射使用匿名超大页面,则可能具有超大页面).因此 ,您可以访问包含中断的页面末尾的内存没错 .这就是为什么内核代码仅检查中断是否移至其他页面以跳过map/unmap逻辑,但仍会更新实际brk的原因.

Of course, memory protection has at least page granularity (and maybe hugepage, if the kernel chooses to use anonymous hugepages for this mapping). So you can access memory out to the end of the page containing the break without faulting. This is why the kernel code is just checking if the break moved to a different page to skip the map / unmap logic, but still updates the actual brk.

AFAIK,没有任何东西可以将中断上方的映射内存用作暂存空间,因此不像堆栈指针下方的内存可以被异步破坏.

AFAIK, nothing will ever use that mapped memory above the break as scratch space, so it's not like memory below the stack pointer that can be clobbered asynchronously.

brk只是内核内置的简单内存管理系统.系统调用非常昂贵,因此如果您关心性能,则应该跟踪用户空间中的内容,并且仅在需要新页面时才进行系统调用. 直接使用sys_brk进行微小分配对于性能而言是很糟糕的,尤其是在启用了Meltdown + Spectre缓解功能的内核中(使系统调用更加昂贵,例如成千上万个时钟周期+ TLB和分支预测无效) ,而不是数百个时钟周期.

brk is just a simple memory-management system built-in to the kernel. System calls are expensive, so if you care about performance you should keep track of things in user-space and only make a system call at all when you need a new page. Using sys_brk directly for tiny allocations is terrible for performance, especially in kernels with Meltdown + Spectre mitigation enabled (making system calls much more expensive, like tens of thousands of clock cycles + TLB and branch prediction invalidation, instead of hundreds of clock cycles).

这篇关于sys_brk的资格要求是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆