Skip to content

Commit 3a9ba17

Browse files
manninglucasgvisor-bot
authored andcommitted
Fix device FD reference leaks and add support for VFIO_GROUP_UNSET_CONTAINER.
Fixes #11545 PiperOrigin-RevId: 739274186
1 parent 225a7bc commit 3a9ba17

File tree

5 files changed

+19
-3
lines changed

5 files changed

+19
-3
lines changed

pkg/abi/linux/vfio.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ var (
115115
VFIO_CHECK_EXTENSION = IO(VFIO_TYPE, VFIO_BASE+1)
116116
VFIO_SET_IOMMU = IO(VFIO_TYPE, VFIO_BASE+2)
117117
VFIO_GROUP_SET_CONTAINER = IO(VFIO_TYPE, VFIO_BASE+4)
118+
VFIO_GROUP_UNSET_CONTAINER = IO(VFIO_TYPE, VFIO_BASE+5)
118119
VFIO_GROUP_GET_DEVICE_FD = IO(VFIO_TYPE, VFIO_BASE+6)
119120
VFIO_DEVICE_GET_INFO = IO(VFIO_TYPE, VFIO_BASE+7)
120121
VFIO_DEVICE_GET_REGION_INFO = IO(VFIO_TYPE, VFIO_BASE+8)

pkg/sentry/devices/tpuproxy/seccomp_filter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ func Filters() seccomp.SyscallRules {
9292
seccomp.NonNegativeFD{},
9393
seccomp.EqualTo(linux.VFIO_GROUP_SET_CONTAINER),
9494
},
95+
seccomp.PerArg{
96+
seccomp.NonNegativeFD{},
97+
seccomp.EqualTo(linux.VFIO_GROUP_UNSET_CONTAINER),
98+
},
9599
seccomp.PerArg{
96100
seccomp.NonNegativeFD{},
97101
seccomp.EqualTo(linux.VFIO_IOMMU_MAP_DMA),

pkg/sentry/devices/tpuproxy/vfio/pci_device_fd.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"gvisor.dev/gvisor/pkg/errors/linuxerr"
2424
"gvisor.dev/gvisor/pkg/fdnotifier"
2525
"gvisor.dev/gvisor/pkg/hostarch"
26+
"gvisor.dev/gvisor/pkg/log"
2627
"gvisor.dev/gvisor/pkg/marshal/primitive"
2728
"gvisor.dev/gvisor/pkg/sentry/arch"
2829
"gvisor.dev/gvisor/pkg/sentry/devices/tpuproxy/util"
@@ -67,7 +68,9 @@ func (fd *pciDeviceFD) Release(context.Context) {
6768
}
6869
fdnotifier.RemoveFD(fd.hostFD)
6970
fd.queue.Notify(waiter.EventHUp)
70-
unix.Close(int(fd.hostFD))
71+
if err := unix.Close(int(fd.hostFD)); err != nil {
72+
log.Warningf("close(%d) pciDeviceFD failed: %v", fd.hostFD, err)
73+
}
7174
}
7275

7376
// EventRegister implements waiter.Waitable.EventRegister.

pkg/sentry/devices/tpuproxy/vfio/tpu_fd.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"gvisor.dev/gvisor/pkg/errors/linuxerr"
2424
"gvisor.dev/gvisor/pkg/fdnotifier"
2525
"gvisor.dev/gvisor/pkg/hostarch"
26+
"gvisor.dev/gvisor/pkg/log"
2627
"gvisor.dev/gvisor/pkg/marshal/primitive"
2728
"gvisor.dev/gvisor/pkg/sentry/arch"
2829
"gvisor.dev/gvisor/pkg/sentry/devices/tpuproxy/util"
@@ -80,7 +81,9 @@ func (fd *tpuFD) Release(context.Context) {
8081
}
8182
fdnotifier.RemoveFD(fd.hostFD)
8283
fd.queue.Notify(waiter.EventHUp)
83-
unix.Close(int(fd.hostFD))
84+
if err := unix.Close(int(fd.hostFD)); err != nil {
85+
log.Warningf("close(%d) tpuFD failed: %v", fd.hostFD, err)
86+
}
8487
}
8588

8689
// EventRegister implements waiter.Waitable.EventRegister.
@@ -134,6 +137,8 @@ func (fd *tpuFD) Ioctl(ctx context.Context, uio usermem.IO, sysno uintptr, args
134137
switch cmd {
135138
case linux.VFIO_GROUP_SET_CONTAINER:
136139
return fd.setContainer(ctx, t, args[2].Pointer())
140+
case linux.VFIO_GROUP_UNSET_CONTAINER:
141+
return util.IOCTLInvoke[uint32, uintptr](fd.hostFD, linux.VFIO_GROUP_UNSET_CONTAINER, 0)
137142
case linux.VFIO_GROUP_GET_DEVICE_FD:
138143
ret, cleanup, err := fd.getPciDeviceFd(t, args[2].Pointer())
139144
defer cleanup()
@@ -194,6 +199,7 @@ func (fd *tpuFD) getPciDeviceFd(t *kernel.Task, arg hostarch.Addr) (uintptr, fun
194199
if err := fdnotifier.AddFD(int32(hostFD), &fd.queue); err != nil {
195200
return 0, cleanup, err
196201
}
202+
defer pciDevFD.vfsfd.DecRef(t)
197203
newFD, err := t.NewFDFrom(0, &pciDevFD.vfsfd, kernel.FDFlags{})
198204
if err != nil {
199205
return 0, cleanup, err

pkg/sentry/devices/tpuproxy/vfio/vfio_fd.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ func (fd *vfioFD) Release(context.Context) {
7272
fd.unpinRange(DevAddrRange{0, ^uint64(0)})
7373
fdnotifier.RemoveFD(fd.hostFD)
7474
fd.queue.Notify(waiter.EventHUp)
75-
unix.Close(int(fd.hostFD))
75+
if err := unix.Close(int(fd.hostFD)); err != nil {
76+
log.Warningf("close(%d) vfioFD failed: %v", fd.hostFD, err)
77+
}
7678
}
7779

7880
// EventRegister implements waiter.Waitable.EventRegister.

0 commit comments

Comments
 (0)