From 77eff46290cfea219d0be985848c4fdfccf856d9 Mon Sep 17 00:00:00 2001 From: Jing Chen Date: Tue, 28 Nov 2023 11:21:26 -0800 Subject: [PATCH] Add an initial implementation for vfioDevice Open, which implements vfs.Device interface. PiperOrigin-RevId: 586047954 --- pkg/sentry/devices/tpuproxy/BUILD | 9 +++- pkg/sentry/devices/tpuproxy/device.go | 64 +++++++++++++++++++++++++++ pkg/sentry/devices/tpuproxy/tpu.go | 3 ++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 pkg/sentry/devices/tpuproxy/device.go diff --git a/pkg/sentry/devices/tpuproxy/BUILD b/pkg/sentry/devices/tpuproxy/BUILD index 7dbcb03df..feb362719 100644 --- a/pkg/sentry/devices/tpuproxy/BUILD +++ b/pkg/sentry/devices/tpuproxy/BUILD @@ -6,13 +6,20 @@ licenses(["notice"]) go_library( name = "tpuproxy", - srcs = ["tpu.go"], + srcs = [ + "device.go", + "tpu.go", + ], deps = [ "//pkg/context", + "//pkg/devutil", "//pkg/errors/linuxerr", + "//pkg/log", "//pkg/sentry/arch", "//pkg/sentry/vfs", + "//pkg/sync", "//pkg/usermem", "//pkg/waiter", + "@org_golang_x_sys//unix:go_default_library", ], ) diff --git a/pkg/sentry/devices/tpuproxy/device.go b/pkg/sentry/devices/tpuproxy/device.go new file mode 100644 index 000000000..ba2420b3a --- /dev/null +++ b/pkg/sentry/devices/tpuproxy/device.go @@ -0,0 +1,64 @@ +// Copyright 2023 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tpuproxy + +import ( + "fmt" + + "golang.org/x/sys/unix" + "gvisor.dev/gvisor/pkg/context" + "gvisor.dev/gvisor/pkg/devutil" + "gvisor.dev/gvisor/pkg/errors/linuxerr" + "gvisor.dev/gvisor/pkg/log" + "gvisor.dev/gvisor/pkg/sentry/vfs" + "gvisor.dev/gvisor/pkg/sync" +) + +// vfioDevice implements vfs.Device for /dev/vfio/[0-9]+ +// +// +stateify savable +type vfioDevice struct { + mu sync.Mutex + + minor uint32 +} + +// Open implememnts vfs.Device.Open. +func (dev *vfioDevice) Open(ctx context.Context, mnt *vfs.Mount, d *vfs.Dentry, opts vfs.OpenOptions) (*vfs.FileDescription, error) { + devClient := devutil.GoferClientFromContext(ctx) + if devClient == nil { + log.Warningf("devutil.CtxDevGoferClient is not set") + return nil, linuxerr.ENOENT + } + dev.mu.Lock() + defer dev.mu.Unlock() + devName := fmt.Sprintf("/dev/vfio/%d", dev.minor) + hostFD, err := devClient.OpenAt(ctx, devName, opts.Flags) + if err != nil { + ctx.Warningf("vfioDevice: failed to open host %s: %v", devName, err) + return nil, err + } + fd := &vfioFD{ + hostFD: int32(hostFD), + device: dev, + } + if err := fd.vfsfd.Init(fd, opts.Flags, mnt, d, &vfs.FileDescriptionOptions{ + UseDentryMetadata: true, + }); err != nil { + unix.Close(hostFD) + return nil, err + } + return &fd.vfsfd, nil +} diff --git a/pkg/sentry/devices/tpuproxy/tpu.go b/pkg/sentry/devices/tpuproxy/tpu.go index d7ac0539a..c91c76d52 100644 --- a/pkg/sentry/devices/tpuproxy/tpu.go +++ b/pkg/sentry/devices/tpuproxy/tpu.go @@ -32,6 +32,9 @@ type vfioFD struct { vfs.FileDescriptionDefaultImpl vfs.DentryMetadataFileDescriptionImpl vfs.NoLockFD + + hostFD int32 + device *vfioDevice } // Release implements vfs.FileDescriptionImpl.Release.