summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/pgalloc
diff options
context:
space:
mode:
authorJamie Liu <jamieliu@google.com>2020-06-09 11:14:24 -0700
committergVisor bot <gvisor-bot@google.com>2020-06-09 11:16:05 -0700
commitecff24930cb2dd5b0910da859d6e712f2f1d32c4 (patch)
treeb43f5cc63e0c141e5e9c624034068049e4e65102 /pkg/sentry/pgalloc
parent20afd66e019bf0aaaf66e854135cd2c0fe0dfd92 (diff)
Ensure pgalloc.MemoryFile.fileSize is always chunk-aligned.
findAvailableLocked() may return a non-aligned FileRange.End after expansion since it may round FileRange.Start down to a hugepage boundary. PiperOrigin-RevId: 315520321
Diffstat (limited to 'pkg/sentry/pgalloc')
-rw-r--r--pkg/sentry/pgalloc/pgalloc.go11
1 files changed, 6 insertions, 5 deletions
diff --git a/pkg/sentry/pgalloc/pgalloc.go b/pkg/sentry/pgalloc/pgalloc.go
index c8d9facc2..46f19d218 100644
--- a/pkg/sentry/pgalloc/pgalloc.go
+++ b/pkg/sentry/pgalloc/pgalloc.go
@@ -393,16 +393,17 @@ func (f *MemoryFile) Allocate(length uint64, kind usage.MemoryKind) (platform.Fi
return platform.FileRange{}, syserror.ENOMEM
}
- // Expand the file if needed. Note that findAvailableRange will
- // appropriately double the fileSize when required.
+ // Expand the file if needed.
if int64(fr.End) > f.fileSize {
- if err := f.file.Truncate(int64(fr.End)); err != nil {
+ // Round the new file size up to be chunk-aligned.
+ newFileSize := (int64(fr.End) + chunkMask) &^ chunkMask
+ if err := f.file.Truncate(newFileSize); err != nil {
return platform.FileRange{}, err
}
- f.fileSize = int64(fr.End)
+ f.fileSize = newFileSize
f.mappingsMu.Lock()
oldMappings := f.mappings.Load().([]uintptr)
- newMappings := make([]uintptr, f.fileSize>>chunkShift)
+ newMappings := make([]uintptr, newFileSize>>chunkShift)
copy(newMappings, oldMappings)
f.mappings.Store(newMappings)
f.mappingsMu.Unlock()