gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/devices/tpuproxy/device.go (about) 1 // Copyright 2023 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tpuproxy 16 17 import ( 18 "fmt" 19 "path/filepath" 20 21 "golang.org/x/sys/unix" 22 "gvisor.dev/gvisor/pkg/abi/linux" 23 "gvisor.dev/gvisor/pkg/context" 24 "gvisor.dev/gvisor/pkg/devutil" 25 "gvisor.dev/gvisor/pkg/errors/linuxerr" 26 "gvisor.dev/gvisor/pkg/fdnotifier" 27 "gvisor.dev/gvisor/pkg/log" 28 "gvisor.dev/gvisor/pkg/sentry/vfs" 29 "gvisor.dev/gvisor/pkg/sync" 30 ) 31 32 const ( 33 // VFIO_MINOR is the VFIO minor number from include/linux/miscdevice.h. 34 VFIO_MINOR = 196 35 // VFIOPath is the path to a VFIO device, it is usually used to 36 // construct a VFIO container. 37 VFIOPath = "/dev/vfio/vfio" 38 39 tpuDeviceGroupName = "vfio" 40 vfioDeviceGroupName = "vfio" 41 ) 42 43 // device implements TPU's vfs.Device for /dev/vfio/[0-9]+ 44 // 45 // +stateify savable 46 type tpuDevice struct { 47 mu sync.Mutex 48 49 minor uint32 50 } 51 52 // Open implements vfs.Device.Open. 53 func (dev *tpuDevice) Open(ctx context.Context, mnt *vfs.Mount, d *vfs.Dentry, opts vfs.OpenOptions) (*vfs.FileDescription, error) { 54 devClient := devutil.GoferClientFromContext(ctx) 55 if devClient == nil { 56 log.Warningf("devutil.CtxDevGoferClient is not set") 57 return nil, linuxerr.ENOENT 58 } 59 dev.mu.Lock() 60 defer dev.mu.Unlock() 61 devName := fmt.Sprintf("vfio/%d", dev.minor) 62 hostFD, err := devClient.OpenAt(ctx, devName, opts.Flags) 63 if err != nil { 64 ctx.Warningf("vfioDevice: failed to open host %s: %v", devName, err) 65 return nil, err 66 } 67 fd := &tpuFD{ 68 hostFD: int32(hostFD), 69 device: dev, 70 } 71 if err := fd.vfsfd.Init(fd, opts.Flags, mnt, d, &vfs.FileDescriptionOptions{ 72 UseDentryMetadata: true, 73 }); err != nil { 74 unix.Close(hostFD) 75 return nil, err 76 } 77 if err := fdnotifier.AddFD(int32(hostFD), &fd.queue); err != nil { 78 unix.Close(hostFD) 79 return nil, err 80 } 81 fd.memmapFile.fd = fd 82 return &fd.vfsfd, nil 83 } 84 85 // device implements vfs.Device for /dev/vfio/vfio. 86 type vfioDevice struct { 87 mu sync.Mutex 88 89 // +checklocks:mu 90 devAddrSet DevAddrSet 91 } 92 93 // Open implements vfs.Device.Open. 94 func (dev *vfioDevice) Open(ctx context.Context, mnt *vfs.Mount, d *vfs.Dentry, opts vfs.OpenOptions) (*vfs.FileDescription, error) { 95 client := devutil.GoferClientFromContext(ctx) 96 if client == nil { 97 log.Warningf("devutil.CtxDevGoferClient is not set") 98 return nil, linuxerr.ENOENT 99 } 100 101 dev.mu.Lock() 102 defer dev.mu.Unlock() 103 name := fmt.Sprintf("vfio/%s", filepath.Base(VFIOPath)) 104 hostFD, err := client.OpenAt(ctx, name, opts.Flags) 105 if err != nil { 106 ctx.Warningf("failed to open host file %s: %v", name, err) 107 return nil, err 108 } 109 fd := &vfioFD{ 110 hostFD: int32(hostFD), 111 device: dev, 112 } 113 if err := fd.vfsfd.Init(fd, opts.Flags, mnt, d, &vfs.FileDescriptionOptions{ 114 UseDentryMetadata: true, 115 }); err != nil { 116 unix.Close(hostFD) 117 return nil, err 118 } 119 if err := fdnotifier.AddFD(int32(hostFD), &fd.queue); err != nil { 120 unix.Close(hostFD) 121 return nil, err 122 } 123 fd.memmapFile.fd = fd 124 return &fd.vfsfd, nil 125 } 126 127 // RegisterTPUDevice registers devices implemented by this package in vfsObj. 128 func RegisterTPUDevice(vfsObj *vfs.VirtualFilesystem, minor uint32) error { 129 return vfsObj.RegisterDevice(vfs.CharDevice, linux.VFIO_MAJOR, minor, &tpuDevice{ 130 minor: minor, 131 }, &vfs.RegisterDeviceOptions{ 132 GroupName: tpuDeviceGroupName, 133 }) 134 } 135 136 // RegisterVfioDevice registers VFIO devices that are implemented by this package in vfsObj. 137 func RegisterVfioDevice(vfsObj *vfs.VirtualFilesystem) error { 138 return vfsObj.RegisterDevice(vfs.CharDevice, linux.MISC_MAJOR, VFIO_MINOR, &vfioDevice{}, &vfs.RegisterDeviceOptions{ 139 GroupName: vfioDeviceGroupName, 140 }) 141 }