Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/nvidia-device-plugin/plugin-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/NVIDIA/k8s-device-plugin/internal/cdi"
"github.com/NVIDIA/k8s-device-plugin/internal/imex"
"github.com/NVIDIA/k8s-device-plugin/internal/plugin"
"github.com/NVIDIA/k8s-device-plugin/internal/utils"
)

// GetPlugins returns a set of plugins for the specified configuration.
Expand All @@ -54,7 +55,7 @@ func GetPlugins(ctx context.Context, infolib info.Interface, nvmllib nvml.Interf
cdi.WithNvidiaCTKPath(*config.Flags.Plugin.NvidiaCTKPath),
cdi.WithDeviceIDStrategy(*config.Flags.Plugin.DeviceIDStrategy),
cdi.WithVendor("k8s.device-plugin.nvidia.com"),
cdi.WithGdrcopyEnabled(*config.Flags.GDRCopyEnabled),
cdi.WithGdrcopyEnabled(*config.Flags.GDRCopyEnabled || utils.IsGdrdrvLoaded()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Since these are effectively no-ops if the drivers are not available, is it a simpler change to change the default for these settings instead of adding logic to check whether the modules are loaded?

cdi.WithGdsEnabled(*config.Flags.GDSEnabled),
cdi.WithMofedEnabled(*config.Flags.MOFEDEnabled),
cdi.WithImexChannels(imexChannels),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ spec:
mountPath: /mps
- name: cdi-root
mountPath: /var/run/cdi
- name: proc-modules
mountPath: /host/proc/modules
readOnly: true
{{- if $options.hasConfigMap }}
- name: available-configs
mountPath: /available-configs
Expand Down Expand Up @@ -252,6 +255,10 @@ spec:
hostPath:
path: /var/run/cdi
type: DirectoryOrCreate
- name: proc-modules
hostPath:
path: /proc/modules
type: File
{{- if $options.hasConfigMap }}
- name: available-configs
configMap:
Expand Down
3 changes: 2 additions & 1 deletion internal/plugin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/NVIDIA/k8s-device-plugin/internal/cdi"
"github.com/NVIDIA/k8s-device-plugin/internal/imex"
"github.com/NVIDIA/k8s-device-plugin/internal/rm"
"github.com/NVIDIA/k8s-device-plugin/internal/utils"

"github.com/google/uuid"
"google.golang.org/grpc"
Expand Down Expand Up @@ -352,7 +353,7 @@ func (plugin *nvidiaDevicePlugin) getAllocateResponse(requestIds []string) (*plu
if plugin.config.Flags.Plugin.PassDeviceSpecs != nil && *plugin.config.Flags.Plugin.PassDeviceSpecs {
response.Devices = append(response.Devices, plugin.apiDeviceSpecs(*plugin.config.Flags.NvidiaDevRoot, requestIds)...)
}
if plugin.config.Flags.GDRCopyEnabled != nil && *plugin.config.Flags.GDRCopyEnabled {
if (plugin.config.Flags.GDRCopyEnabled != nil && *plugin.config.Flags.GDRCopyEnabled) || utils.IsGdrdrvLoaded() {
response.Envs["NVIDIA_GDRCOPY"] = "enabled"
}
if plugin.config.Flags.GDSEnabled != nil && *plugin.config.Flags.GDSEnabled {
Expand Down
61 changes: 61 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
#
# 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 utils

import (
"bufio"
"os"
"slices"
"strings"
)

// Supported driver module constants.
const (
GDRCOPY_DRIVER_MODULE = "gdrdrv"
)

// IsGdrdrvLoaded checks if the "gdrdrv" driver module is loaded.
func IsGdrdrvLoaded() bool {
return isDriverLoaded(GDRCOPY_DRIVER_MODULE)
}

// isDriverLoaded checks if the specified driver module is loaded by reading the modules file.
// It first checks "/host/proc/modules" (for containerized environments) and falls back to "/proc/modules" if not found.
Comment on lines +36 to +37
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: let's keep a maximum column length for the docu-comments around 80.

func isDriverLoaded(module string) bool {
paths := []string{"/host/proc/modules", "/proc/modules"}
return slices.ContainsFunc(paths, func(path string) bool {
return isDriverLoadedWithPath(module, path)
})
}

// isDriverLoadedWithPath checks if the specified driver module is loaded by reading the specified modules file.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: let's keep a maximum column length for the docu-comments around 80.

func isDriverLoadedWithPath(module, modulesPath string) bool {
f, err := os.Open(modulesPath)
if err != nil {
return false
}
defer f.Close()

scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, module+" ") {
return true
}
}
return false
}