Skip to content

Commit 26e0e5d

Browse files
milantracygvisor-bot
authored andcommitted
Implement memmap.Mappable and memmap.File for vfioFd.
PiperOrigin-RevId: 615472230
1 parent aff56ec commit 26e0e5d

File tree

6 files changed

+96
-9
lines changed

6 files changed

+96
-9
lines changed

pkg/sentry/devices/tpuproxy/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ go_library(
1212
"tpu.go",
1313
"tpu_mmap.go",
1414
"vfio.go",
15+
"vfio_mmap.go",
1516
],
1617
visibility = [
1718
"//pkg/sentry:internal",

pkg/sentry/devices/tpuproxy/device.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func (dev *vfioDevice) Open(ctx context.Context, mnt *vfs.Mount, d *vfs.Dentry,
117117
unix.Close(hostFd)
118118
return nil, err
119119
}
120+
fd.memmapFile.fd = fd
120121
return &fd.vfsfd, nil
121122
}
122123

pkg/sentry/devices/tpuproxy/tpu.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type tpuFD struct {
4040
hostFD int32
4141
device *tpuDevice
4242
queue waiter.Queue
43-
memmapFile tpuFdMemmapFile
43+
memmapFile tpuFDMemmapFile
4444
}
4545

4646
// Release implements vfs.FileDescriptionImpl.Release.

pkg/sentry/devices/tpuproxy/tpu_mmap.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,25 @@ func (fd *tpuFD) InvalidateUnsavable(ctx context.Context) error {
6060
return nil
6161
}
6262

63-
type tpuFdMemmapFile struct {
63+
type tpuFDMemmapFile struct {
6464
fd *tpuFD
6565
}
6666

6767
// IncRef implements memmap.File.IncRef.
68-
func (mf *tpuFdMemmapFile) IncRef(memmap.FileRange, uint32) {
68+
func (mf *tpuFDMemmapFile) IncRef(memmap.FileRange, uint32) {
6969
}
7070

7171
// DecRef implements memmap.File.DecRef.
72-
func (mf *tpuFdMemmapFile) DecRef(fr memmap.FileRange) {
72+
func (mf *tpuFDMemmapFile) DecRef(fr memmap.FileRange) {
7373
}
7474

7575
// MapInternal implements memmap.File.MapInternal.
76-
func (mf *tpuFdMemmapFile) MapInternal(fr memmap.FileRange, at hostarch.AccessType) (safemem.BlockSeq, error) {
76+
func (mf *tpuFDMemmapFile) MapInternal(fr memmap.FileRange, at hostarch.AccessType) (safemem.BlockSeq, error) {
7777
log.Traceback("tpuproxy: rejecting tpuFdMemmapFile.MapInternal")
7878
return safemem.BlockSeq{}, linuxerr.EINVAL
7979
}
8080

8181
// FD implements memmap.File.FD.
82-
func (mf *tpuFdMemmapFile) FD() int {
82+
func (mf *tpuFDMemmapFile) FD() int {
8383
return int(mf.fd.hostFD)
8484
}

pkg/sentry/devices/tpuproxy/vfio.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ type vfioFd struct {
3434
vfs.DentryMetadataFileDescriptionImpl
3535
vfs.NoLockFD
3636

37-
hostFd int32
38-
device *vfioDevice
39-
queue waiter.Queue
37+
hostFd int32
38+
device *vfioDevice
39+
queue waiter.Queue
40+
memmapFile vfioFDMemmapFile
4041
}
4142

4243
// Release implements vfs.FileDescriptionImpl.Release.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2024 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+
"gvisor.dev/gvisor/pkg/context"
19+
"gvisor.dev/gvisor/pkg/errors/linuxerr"
20+
"gvisor.dev/gvisor/pkg/hostarch"
21+
"gvisor.dev/gvisor/pkg/log"
22+
"gvisor.dev/gvisor/pkg/safemem"
23+
"gvisor.dev/gvisor/pkg/sentry/memmap"
24+
"gvisor.dev/gvisor/pkg/sentry/vfs"
25+
)
26+
27+
// ConfigureMMap implements vfs.FileDescriptionImpl.ConfigureMMap.
28+
func (fd *vfioFd) ConfigureMMap(ctx context.Context, opts *memmap.MMapOpts) error {
29+
return vfs.GenericConfigureMMap(&fd.vfsfd, fd, opts)
30+
}
31+
32+
// AddMapping implements memmap.Mappable.AddMapping.
33+
func (fd *vfioFd) AddMapping(ctx context.Context, ms memmap.MappingSpace, ar hostarch.AddrRange, offset uint64, writable bool) error {
34+
return nil
35+
}
36+
37+
// RemoveMapping implements memmap.Mappable.RemoveMapping.
38+
func (fd *vfioFd) RemoveMapping(ctx context.Context, ms memmap.MappingSpace, ar hostarch.AddrRange, offset uint64, writable bool) {
39+
}
40+
41+
// CopyMapping implements memmap.Mappable.CopyMapping.
42+
func (fd *vfioFd) CopyMapping(ctx context.Context, ms memmap.MappingSpace, srcAR, dstAR hostarch.AddrRange, offset uint64, writable bool) error {
43+
return nil
44+
}
45+
46+
// Translate implements memmap.Mappable.Translate.
47+
func (fd *vfioFd) Translate(ctx context.Context, required, optional memmap.MappableRange, at hostarch.AccessType) ([]memmap.Translation, error) {
48+
return []memmap.Translation{
49+
{
50+
Source: optional,
51+
File: &fd.memmapFile,
52+
Offset: optional.Start,
53+
Perms: at,
54+
},
55+
}, nil
56+
}
57+
58+
// InvalidateUnsavable implements memmap.Mappable.InvalidateUnsavable.
59+
func (fd *vfioFd) InvalidateUnsavable(ctx context.Context) error {
60+
return nil
61+
}
62+
63+
type vfioFDMemmapFile struct {
64+
fd *vfioFd
65+
}
66+
67+
// IncRef implements memmap.File.IncRef.
68+
func (mf *vfioFDMemmapFile) IncRef(memmap.FileRange, uint32) {
69+
}
70+
71+
// DecRef implements memmap.File.DecRef.
72+
func (mf *vfioFDMemmapFile) DecRef(fr memmap.FileRange) {
73+
}
74+
75+
// MapInternal implements memmap.File.MapInternal.
76+
func (mf *vfioFDMemmapFile) MapInternal(fr memmap.FileRange, at hostarch.AccessType) (safemem.BlockSeq, error) {
77+
log.Traceback("tpuproxy: rejecting vfioFdMemmapFile.MapInternal")
78+
return safemem.BlockSeq{}, linuxerr.EINVAL
79+
}
80+
81+
// FD implements memmap.File.FD.
82+
func (mf *vfioFDMemmapFile) FD() int {
83+
return int(mf.fd.hostFd)
84+
}

0 commit comments

Comments
 (0)