Skip to content

Commit c74385d

Browse files
committed
[nvpassthrough] do not load kernel modules unless user opts-in
Signed-off-by: Christopher Desiniotis <cdesiniotis@nvidia.com>
1 parent 97d3fe9 commit c74385d

1 file changed

Lines changed: 32 additions & 12 deletions

File tree

pkg/nvpassthrough/nvpassthrough.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ type Interface interface {
4444
}
4545

4646
type nvpassthrough struct {
47-
logger basicLogger
48-
libModulesRoot string
49-
nvpciLib nvpci.Interface
47+
logger basicLogger
48+
libModulesRoot string
49+
nvpciLib nvpci.Interface
50+
loadKernelModules bool
5051
}
5152

5253
var _ Interface = (*nvpassthrough)(nil)
@@ -92,6 +93,15 @@ func WithLibModulesRoot(libModulesRoot string) Option {
9293
}
9394
}
9495

96+
// WithLoadKernelModules provides an Option for opting-in to loading
97+
// kernel modules before binding NVIDIA PCI devices to them. By default,
98+
// this behavior is disabled.
99+
func WithLoadKernelModules(loadKernelModules bool) Option {
100+
return func(w *nvpassthrough) {
101+
w.loadKernelModules = loadKernelModules
102+
}
103+
}
104+
95105
// WithNvpciLib provides an Option to set the nvpci lib used.
96106
func WithNvpciLib(lib nvpci.Interface) Option {
97107
return func(w *nvpassthrough) {
@@ -168,15 +178,10 @@ func (n *nvpassthrough) BindToVFIODriver(address string) error {
168178
return fmt.Errorf("failed to find best vfio variant driver: %w", err)
169179
}
170180

171-
k, err := kmod.New(
172-
kmod.SetInitFunc(modInitFunc),
173-
kmod.SetRootDir(n.libModulesRoot),
174-
)
175-
if err != nil {
176-
return fmt.Errorf("failed to initialize kmod library: %w", err)
177-
}
178-
if err := k.Load(vfioDriverName, "", 0); err != nil {
179-
return fmt.Errorf("failed to load %q driver: %w", vfioDriverName, err)
181+
if n.loadKernelModules {
182+
if err := n.loadKernelModule(vfioDriverName); err != nil {
183+
return fmt.Errorf("failed to load %q driver: %w", vfioDriverName, err)
184+
}
180185
}
181186

182187
// (cdesiniotis) Module names in the modules.alias file will only ever contain
@@ -239,6 +244,21 @@ func (n *nvpassthrough) BindToVFIODriver(address string) error {
239244
return nil
240245
}
241246

247+
func (n *nvpassthrough) loadKernelModule(moduleName string) error {
248+
k, err := kmod.New(
249+
kmod.SetInitFunc(modInitFunc),
250+
kmod.SetRootDir(n.libModulesRoot),
251+
)
252+
if err != nil {
253+
return fmt.Errorf("failed to initialize kmod library: %w", err)
254+
}
255+
if err := k.Load(moduleName, "", 0); err != nil {
256+
return err
257+
}
258+
259+
return nil
260+
}
261+
242262
// BindToDriver binds an NVIDIA PCI device to the driver supplied as input.
243263
func (n *nvpassthrough) BindToDriver(address string, driver string) error {
244264
device, err := n.nvpciLib.GetNvidiaDeviceByPciBusID(address)

0 commit comments

Comments
 (0)