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