diff --git a/pkg/sentry/devices/tpuproxy/BUILD b/pkg/sentry/devices/tpuproxy/BUILD index 7cad336e2..39e2b5f1e 100644 --- a/pkg/sentry/devices/tpuproxy/BUILD +++ b/pkg/sentry/devices/tpuproxy/BUILD @@ -31,6 +31,7 @@ go_library( "//pkg/seccomp", "//pkg/sentry/arch", "//pkg/sentry/fsimpl/eventfd", + "//pkg/sentry/fsimpl/kernfs", "//pkg/sentry/kernel", "//pkg/sentry/memmap", "//pkg/sentry/vfs", diff --git a/pkg/sentry/devices/tpuproxy/tpu.go b/pkg/sentry/devices/tpuproxy/tpu.go index faef39be5..9d9e2c1cd 100644 --- a/pkg/sentry/devices/tpuproxy/tpu.go +++ b/pkg/sentry/devices/tpuproxy/tpu.go @@ -27,6 +27,7 @@ import ( "gvisor.dev/gvisor/pkg/marshal/primitive" "gvisor.dev/gvisor/pkg/sentry/arch" "gvisor.dev/gvisor/pkg/sentry/fsimpl/eventfd" + "gvisor.dev/gvisor/pkg/sentry/fsimpl/kernfs" "gvisor.dev/gvisor/pkg/sentry/kernel" "gvisor.dev/gvisor/pkg/sentry/vfs" "gvisor.dev/gvisor/pkg/usermem" @@ -174,6 +175,8 @@ func (fd *tpuFD) getPciDeviceFd(t *kernel.Task, arg hostarch.Addr) (uintptr, fun if err != nil { return 0, cleanup, err } + // Initialize a mapping that is backed by a host FD. + pciDevFD.CachedMappable.Init(int(hostFD)) return uintptr(newFD), func() {}, nil } @@ -183,10 +186,10 @@ type pciDeviceFD struct { vfs.FileDescriptionDefaultImpl vfs.DentryMetadataFileDescriptionImpl vfs.NoLockFD + kernfs.CachedMappable - hostFD int32 - queue waiter.Queue - memmapFile tpuFDMemmapFile + hostFD int32 + queue waiter.Queue } // Release implements vfs.FileDescriptionImpl.Release. diff --git a/pkg/sentry/devices/tpuproxy/tpu_mmap.go b/pkg/sentry/devices/tpuproxy/tpu_mmap.go index 553b9c09a..ad672947c 100644 --- a/pkg/sentry/devices/tpuproxy/tpu_mmap.go +++ b/pkg/sentry/devices/tpuproxy/tpu_mmap.go @@ -85,59 +85,6 @@ func (mf *tpuFDMemmapFile) FD() int { // ConfigureMMap implements vfs.FileDescriptionImpl.ConfigureMMap. func (fd *pciDeviceFD) ConfigureMMap(ctx context.Context, opts *memmap.MMapOpts) error { + fd.CachedMappable.InitFileMapperOnce() return vfs.GenericConfigureMMap(&fd.vfsfd, fd, opts) } - -// AddMapping implements memmap.Mappable.AddMapping. -func (fd *pciDeviceFD) AddMapping(ctx context.Context, ms memmap.MappingSpace, ar hostarch.AddrRange, offset uint64, writable bool) error { - return nil -} - -// RemoveMapping implements memmap.Mappable.RemoveMapping. -func (fd *pciDeviceFD) RemoveMapping(ctx context.Context, ms memmap.MappingSpace, ar hostarch.AddrRange, offset uint64, writable bool) { -} - -// CopyMapping implements memmap.Mappable.CopyMapping. -func (fd *pciDeviceFD) CopyMapping(ctx context.Context, ms memmap.MappingSpace, srcAR, dstAR hostarch.AddrRange, offset uint64, writable bool) error { - return nil -} - -// Translate implements memmap.Mappable.Translate. -func (fd *pciDeviceFD) Translate(ctx context.Context, required, optional memmap.MappableRange, at hostarch.AccessType) ([]memmap.Translation, error) { - return []memmap.Translation{ - { - Source: optional, - File: &fd.memmapFile, - Offset: optional.Start, - Perms: at, - }, - }, nil -} - -// InvalidateUnsavable implements memmap.Mappable.InvalidateUnsavable. -func (fd *pciDeviceFD) InvalidateUnsavable(ctx context.Context) error { - return nil -} - -type pciDeviceFdMemmapFile struct { - fd *pciDeviceFD -} - -// IncRef implements memmap.File.IncRef. -func (mf *pciDeviceFdMemmapFile) IncRef(memmap.FileRange, uint32) { -} - -// DecRef implements memmap.File.DecRef. -func (mf *pciDeviceFdMemmapFile) DecRef(fr memmap.FileRange) { -} - -// MapInternal implements memmap.File.MapInternal. -func (mf *pciDeviceFdMemmapFile) MapInternal(fr memmap.FileRange, at hostarch.AccessType) (safemem.BlockSeq, error) { - log.Traceback("tpuproxy: rejecting pciDeviceFdMemmapFile.MapInternal") - return safemem.BlockSeq{}, linuxerr.EINVAL -} - -// FD implements memmap.File.FD. -func (mf *pciDeviceFdMemmapFile) FD() int { - return int(mf.fd.hostFD) -}