Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CUDA forward compatibility hook #906

Merged
merged 8 commits into from
Feb 27, 2025
Merged

Conversation

elezar
Copy link
Member

@elezar elezar commented Feb 6, 2025

With #877 the default behaviour of the NVIDIA Container Runtime / NVIDIA Container Runtime Hook was changed to not mount compat libraries from the container into the container. This removed "automatic" support for CUDA Forward compatibility.

This change attempts to address this by adding a createContainerHook that will create a file in /etc/ld.so.conf.d/ in the container to ensure that the /usr/local/cuda/compat libraries are added to the ldcache over the libraries mounted from the host. The provided host diver version is compared to the version of the compat libraries in the container and the config update is only performed if the compat libraries are newer than the host drivers.

Note that the hook only creates a file in the container's file system and does not perform any mount operations. This means that this mechanism is not present the same vulnerabilities causing CVE-2024-0132 and CVE-2025-23359.

In the case of the legacy runtime, this behaviour is only triggered if the allow-cuda-compat-libs-from-container feature flag is not enabled. The CDI spec generation has also been extended to include this hook.

@elezar elezar force-pushed the add-compat-lib-hook branch from f21f7a6 to d617fb5 Compare February 6, 2025 22:24
@elezar elezar force-pushed the add-compat-lib-hook branch 3 times, most recently from 606aed5 to ba30397 Compare February 6, 2025 23:08
@elezar elezar changed the title Add compat lib hook Add CUDA forward compatibility hook Feb 7, 2025
@elezar elezar force-pushed the add-compat-lib-hook branch 3 times, most recently from 0c4dd3d to 0b50458 Compare February 7, 2025 13:07
@elezar elezar marked this pull request as ready for review February 7, 2025 13:10
@elezar elezar added the must-backport The changes in PR need to be backported to at least one stable release branch. label Feb 7, 2025
@elezar elezar force-pushed the add-compat-lib-hook branch 2 times, most recently from d1e97a0 to eeef750 Compare February 7, 2025 21:08
@elezar elezar force-pushed the add-compat-lib-hook branch 2 times, most recently from 88841fa to 8e0c69b Compare February 10, 2025 13:38
compatLibHookDiscoverer := discover.NewCUDACompatHookDiscoverer(logger, cfg.NVIDIACTKConfig.Path, driver)
discoverers = append(discoverers, compatLibHookDiscoverer)
if cfg.NVIDIAContainerRuntimeConfig.Mode == "legacy" {
ldcacheIpdateHookDiscoverer, err := discover.NewLDCacheUpdateHook(
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: Since the nvidia-container-runtime-hook is invoked as a prestart hook, this is done BEFORE the createContainer hook that we insert above. This means that we need to once again run the update ldcache hook.

Copy link
Member Author

Choose a reason for hiding this comment

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

@klueska one option would be to create the file in /etc/ld.so.conf.d when invoking the nvidia-container-runtime-hook instead -- before calling out to the nvidia-container-cli. We don't have as ready access to the driver version, but we could extract it there.

Copy link
Contributor

@cdesiniotis cdesiniotis Feb 10, 2025

Choose a reason for hiding this comment

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

If we create the file in nvidia-container-runtime-hook, then wouldn't that remove the need to add the cuda-compat createContainer hook altogether? And as a result, the forward compatibility support in the legacy stack would not require users to use NVIDIA container Runtime, correct?

Copy link
Member Author

@elezar elezar Feb 11, 2025

Choose a reason for hiding this comment

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

I did try it, and my initial assumption was incorrect. Because we invoke ldconfig as:

        argv = (char * []){cnt->cfg.ldconfig, "-f", "/etc/ld.so.conf", "-C", "/etc/ld.so.cache", cnt->cfg.libs_dir, cnt->cfg.libs32_dir, NULL};

in libnvidia-container, this means that the cnt->cfg.libs_dir and cnt->cfg.libs32_dir folders take precedence over the files in /etc/ld.so.conf.d and the CUDA libraries present there are used.

We could rework the libnvidia-container implementation further, but the intent of this change is also to provide the functionality for #910 so that we can remove the legacy code path by default.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it. So for legacy mode, we are adding two createContainer hooks ( the cuda-compat hook and the update-ldcache) to ensure the compat libs are used, the caveat being that we execute ldconfig twice.

Copy link
Member Author

@elezar elezar Feb 17, 2025

Choose a reason for hiding this comment

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

Yes, that is correct. Note that since #910 disables the legacy mode by default, the intent is that this particular configuration becomes more uncommon.

@elezar elezar force-pushed the add-compat-lib-hook branch 3 times, most recently from 77bfb3c to 1140fcf Compare February 10, 2025 15:08
@elezar elezar force-pushed the add-compat-lib-hook branch from 1140fcf to cb6b4f6 Compare February 10, 2025 18:18
@elezar elezar force-pushed the add-compat-lib-hook branch 2 times, most recently from f39a194 to aaf5ed8 Compare February 11, 2025 14:23
Copy link
Collaborator

@ArangoGutierrez ArangoGutierrez left a comment

Choose a reason for hiding this comment

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

Not blocking comments


driverMajor := strings.SplitN(hostDriverVersion, ".", 2)[0]

if driverMajor >= compatMajor {

Choose a reason for hiding this comment

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

The string comparison shouldn't be done. There should be an Atoi before this.
Example: driverMajor = "11", compatMajor = "101"
The current code will show that driverMajor is greater than compatMajor, which is wrong. Can add a testcase too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's true. This relies on the current state of our driver versions where the branch (major) version is always some three digit number. This is also why string comparison is used in libnvidia-container at present, but is not future proof and will break when we release a 1xxx driver branch. I will update to use Atoi instead.


// Create the 'cuda-compat' command
c := cli.Command{
Name: "cuda-compat",

Choose a reason for hiding this comment

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

Grammar review for fun.
If its a command, then it should be a verb. 'enable-cuda-compat'?

This change adds an nvidia-cdi-hook enable-cuda-compat hook that checks the
container for cuda compat libs and updates /etc/ld.so.conf.d to include their
parent folder if their driver major version is sufficient.

This allows CUDA Forward Compatibility to be used when this is not available
through the libnvidia-container.

Signed-off-by: Evan Lezar <[email protected]>
This change adds the enable-cuda-compat hook to the incomming OCI runtime spec
if the allow-cuda-compat-libs-from-container feature flag is not enabled.

An update-ldcache hook is also injected to ensure that the required folders
are processed.

Signed-off-by: Evan Lezar <[email protected]>
@elezar elezar force-pushed the add-compat-lib-hook branch from 5ee65f9 to aff9301 Compare February 27, 2025 13:58
@elezar elezar merged commit 968e2cc into NVIDIA:main Feb 27, 2025
16 checks passed
//
// Note: Since this mechanism replaces the logic in the `nvidia-container-cli`,
// toggling this feature has no effect if `allow-cuda-compat-libs-from-container` is enabled.
DisableCUDACompatLibHook *feature `toml:"disable-cuda-compat-lib-hook,omitempty"`
Copy link

Choose a reason for hiding this comment

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

This feature flag seems not used in the cdi mode. Is this expected?

@@ -97,6 +97,8 @@ func NewDriverLibraryDiscoverer(logger logger.Interface, driver *root.Driver, nv
libraryPaths,
)

// TODO: The following should use the version directly.
cudaCompatLibHookDiscoverer := discover.NewCUDACompatHookDiscoverer(logger, nvidiaCDIHookPath, driver)
Copy link

Choose a reason for hiding this comment

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

Should the disable-cuda-compat-lib-hook feature flag be used here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
must-backport The changes in PR need to be backported to at least one stable release branch.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants