Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sentry: use read(2) host syscall to perform read on disk-backed MemoryFiles #11163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions pkg/sentry/fsimpl/tmpfs/regular_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,14 +655,8 @@ func (rw *regularFileReadWriter) ReadToBlocks(dsts safemem.BlockSeq) (uint64, er
mr := memmap.MappableRange{uint64(rw.off), uint64(end)}
switch {
case seg.Ok():
// Get internal mappings.
ims, err := rw.file.inode.fs.mf.MapInternal(seg.FileRangeOf(seg.Range().Intersect(mr)), hostarch.Read)
if err != nil {
return done, err
}
n, err := rw.readFromMF(seg.FileRangeOf(seg.Range().Intersect(mr)), dsts)

// Copy from internal mappings.
n, err := safemem.CopySeq(dsts, ims)
done += n
rw.off += uint64(n)
dsts = dsts.DropFirst64(n)
Expand Down Expand Up @@ -812,6 +806,29 @@ exitLoop:
return done, retErr
}

func (rw *regularFileReadWriter) readFromMF(fr memmap.FileRange, dsts safemem.BlockSeq) (uint64, error) {
if rw.file.inode.fs.mf.IsDiskBacked() {
// Disk-backed files are not prepopulated. The safemem.CopySeq() approach
// used below incurs a lot of page faults without page prepopulation, which
// causes a lot of context switching. Use read(2) host syscall instead,
// which makes one context switch and faults all the pages that are touched
// during the read.
return hostfd.Preadv2(
int32(rw.file.inode.fs.mf.FD()), // fd
dsts.TakeFirst64(fr.Length()), // dsts
int64(fr.Start), // offset
0, // flags
)
}
// Get internal mappings.
ims, err := rw.file.inode.fs.mf.MapInternal(fr, hostarch.Read)
if err != nil {
return 0, err
}
// Copy from internal mappings.
return safemem.CopySeq(dsts, ims)
}

func (rw *regularFileReadWriter) writeToMF(fr memmap.FileRange, srcs safemem.BlockSeq) (uint64, error) {
if rw.file.inode.fs.mf.IsDiskBacked() {
// Disk-backed files are not prepopulated. The safemem.CopySeq() approach
Expand Down