Skip to content

Commit 4eeb780

Browse files
nixprimegvisor-bot
authored andcommitted
mm: only hugepage-align mappings of hugepage-aligned length
This is consistent with Linux after d4148aeab4124 "mm, mmap: limit THP alignment of anonymous mappings to PMD-aligned sizes". In #12804, when PyTorch / libcuda / the Nvidia userspace driver allocates CPU memory, it relies specifically on Linux's mmap address selection behavior (and the assumption that no other threads are doing mmap() / munmap()) in order to get (in this case) a mapping aligned to one page before a hugepage boundary. After this CL, this looks like: ``` I0401 23:24:16.756773 1 strace.go:576] [ 1: 1] python3 E mmap(0x0, 0x11fff000, 0x0, MAP_PRIVATE|MAP_ANONYMOUS, 0xffffffffffffffff (bad FD), 0x0) I0401 23:24:16.756782 1 strace.go:614] [ 1: 1] python3 X mmap(0x0, 0x11fff000, 0x0, MAP_PRIVATE|MAP_ANONYMOUS, 0xffffffffffffffff (bad FD), 0x0) = 139925074735104 (0x7f42d85ff000) (3.624µs) I0401 23:24:16.756790 1 strace.go:564] [ 1: 1] python3 E munmap(0x7f42d85ff000, 0x1a01000) I0401 23:24:16.756796 1 strace.go:602] [ 1: 1] python3 X munmap(0x7f42d85ff000, 0x1a01000) = 0 (0x0) (1.436µs) I0401 23:24:16.756801 1 strace.go:564] [ 1: 1] python3 E munmap(0x7f42ea000000, 0x5fe000) I0401 23:24:16.756805 1 strace.go:602] [ 1: 1] python3 X munmap(0x7f42ea000000, 0x5fe000) = 0 (0x0) (598ns) <driver ioctls> I0401 23:24:16.758206 1 strace.go:576] [ 1: 1] python3 E mmap(0x0, 0x10001000, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0xffffffffffffffff (bad FD), 0x0) I0401 23:24:16.758213 1 strace.go:614] [ 1: 1] python3 X mmap(0x0, 0x10001000, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0xffffffffffffffff (bad FD), 0x0) = 139924833562624 (0x7f42c9fff000) (1.983µs) ... cpu.data_ptr() = 0x7f42c9fff040 7f42c9fff000-7f42da000000 rw-p 00000000 00:00 0 7f42da000000-7f42ea000000 ---p 00000000 00:00 0 ``` This is consistent across executions in runc and (after this CL) gVisor. Before this CL, this doesn't work in gVisor: ``` I0401 23:06:38.431128 1 strace.go:576] [ 1: 1] python3 E mmap(0x0, 0x10001000, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0xffffffffffffffff (bad FD), 0x0) I0401 23:06:38.431136 1 strace.go:614] [ 1: 1] python3 X mmap(0x0, 0x10001000, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0xffffffffffffffff (bad FD), 0x0) = 140266751131648 (0x7f9265e00000) (2.221µs) ... cpu.data_ptr() = 0x7f9265e00040 7f9265e00000-7f9275e01000 rw-p 00000000 00:00 0 ``` Fixes #12804 PiperOrigin-RevId: 893266800
1 parent 28a0852 commit 4eeb780

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

pkg/sentry/mm/vma.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ func (mm *MemoryManager) findAvailableLocked(length uint64, opts findAvailableOp
195195
return 0, linuxerr.ENOMEM
196196
}
197197

198-
// Prefer hugepage alignment if a hugepage or more is requested and the vma
199-
// will actually be eligible for hugepages.
198+
// Prefer hugepage alignment if length is hugepage-aligned and the vma will
199+
// actually be eligible for hugepages.
200200
alignment := uint64(hostarch.PageSize)
201-
if length >= hostarch.HugePageSize && opts.Private && !opts.GrowsDown && !opts.Stack {
201+
if hostarch.IsHugePageAligned(length) && opts.Private && !opts.GrowsDown && !opts.Stack {
202202
alignment = hostarch.HugePageSize
203203
}
204204

0 commit comments

Comments
 (0)