github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sentry/devices/nvproxy/uvm_mmap.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 nvproxy 16 17 import ( 18 "github.com/metacubex/gvisor/pkg/context" 19 "github.com/metacubex/gvisor/pkg/errors/linuxerr" 20 "github.com/metacubex/gvisor/pkg/hostarch" 21 "github.com/metacubex/gvisor/pkg/log" 22 "github.com/metacubex/gvisor/pkg/safemem" 23 "github.com/metacubex/gvisor/pkg/sentry/memmap" 24 "github.com/metacubex/gvisor/pkg/sentry/vfs" 25 ) 26 27 // ConfigureMMap implements vfs.FileDescriptionImpl.ConfigureMMap. 28 func (fd *uvmFD) ConfigureMMap(ctx context.Context, opts *memmap.MMapOpts) error { 29 // UVM_VALIDATE_VA_RANGE, and probably other ioctls, expect that 30 // application mmaps of /dev/nvidia-uvm are immediately visible to the 31 // driver. 32 if opts.PlatformEffect < memmap.PlatformEffectPopulate { 33 opts.PlatformEffect = memmap.PlatformEffectPopulate 34 } 35 return vfs.GenericConfigureMMap(&fd.vfsfd, fd, opts) 36 } 37 38 // AddMapping implements memmap.Mappable.AddMapping. 39 func (fd *uvmFD) AddMapping(ctx context.Context, ms memmap.MappingSpace, ar hostarch.AddrRange, offset uint64, writable bool) error { 40 return nil 41 } 42 43 // RemoveMapping implements memmap.Mappable.RemoveMapping. 44 func (fd *uvmFD) RemoveMapping(ctx context.Context, ms memmap.MappingSpace, ar hostarch.AddrRange, offset uint64, writable bool) { 45 } 46 47 // CopyMapping implements memmap.Mappable.CopyMapping. 48 func (fd *uvmFD) CopyMapping(ctx context.Context, ms memmap.MappingSpace, srcAR, dstAR hostarch.AddrRange, offset uint64, writable bool) error { 49 return nil 50 } 51 52 // Translate implements memmap.Mappable.Translate. 53 func (fd *uvmFD) Translate(ctx context.Context, required, optional memmap.MappableRange, at hostarch.AccessType) ([]memmap.Translation, error) { 54 return []memmap.Translation{ 55 { 56 Source: optional, 57 File: &fd.memmapFile, 58 Offset: optional.Start, 59 // kernel-open/nvidia-uvm/uvm.c:uvm_mmap() requires mappings to be 60 // PROT_READ|PROT_WRITE. 61 Perms: hostarch.ReadWrite, 62 }, 63 }, nil 64 } 65 66 // InvalidateUnsavable implements memmap.Mappable.InvalidateUnsavable. 67 func (fd *uvmFD) InvalidateUnsavable(ctx context.Context) error { 68 return nil 69 } 70 71 type uvmFDMemmapFile struct { 72 fd *uvmFD 73 } 74 75 // IncRef implements memmap.File.IncRef. 76 func (mf *uvmFDMemmapFile) IncRef(fr memmap.FileRange, memCgID uint32) { 77 } 78 79 // DecRef implements memmap.File.DecRef. 80 func (mf *uvmFDMemmapFile) DecRef(fr memmap.FileRange) { 81 } 82 83 // MapInternal implements memmap.File.MapInternal. 84 func (mf *uvmFDMemmapFile) MapInternal(fr memmap.FileRange, at hostarch.AccessType) (safemem.BlockSeq, error) { 85 // TODO(jamieliu): make an attempt with MAP_FIXED_NOREPLACE? 86 log.Traceback("nvproxy: rejecting uvmFDMemmapFile.MapInternal") 87 return safemem.BlockSeq{}, linuxerr.EINVAL 88 } 89 90 // FD implements memmap.File.FD. 91 func (mf *uvmFDMemmapFile) FD() int { 92 return int(mf.fd.hostFD) 93 }