|
| 1 | +/** |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +**/ |
| 17 | +#include <err.h> |
| 18 | +#include <libgen.h> |
| 19 | +#undef basename /* Use the GNU version of basename. */ |
| 20 | +#include <stdlib.h> |
| 21 | + |
| 22 | +#include "cli.h" |
| 23 | +#include "compat_mode.h" |
| 24 | + |
| 25 | +static void filter_by_major_version(bool, const struct nvc_driver_info *, char * [], size_t *); |
| 26 | +static int get_compat_library_path(struct error *, const char * [], size_t, char **); |
| 27 | + |
| 28 | +int |
| 29 | +update_compat_libraries(struct nvc_context *ctx, struct nvc_container *cnt, const struct nvc_driver_info *info) { |
| 30 | + if (cnt->flags & OPT_CUDA_COMPAT_MODE_DISABLED) { |
| 31 | + return (0); |
| 32 | + } |
| 33 | + if (cnt->libs == NULL || cnt->nlibs == 0) { |
| 34 | + return (0); |
| 35 | + } |
| 36 | + size_t nlibs = cnt->nlibs; |
| 37 | + char **libs = array_copy(&ctx->err, (const char * const *)cnt->libs, cnt->nlibs); |
| 38 | + if (libs == NULL) { |
| 39 | + return (-1); |
| 40 | + } |
| 41 | + |
| 42 | + /* For cuda-compat-mode=mount, we also allow compat libraries with a LOWER major versions. */ |
| 43 | + bool allow_lower_major_versions = (cnt-> flags & OPT_CUDA_COMPAT_MODE_MOUNT); |
| 44 | + filter_by_major_version(allow_lower_major_versions, info, libs, &nlibs); |
| 45 | + |
| 46 | + /* Use the filtered library list. */ |
| 47 | + free(cnt->libs); |
| 48 | + cnt->libs = libs; |
| 49 | + cnt->nlibs = nlibs; |
| 50 | + |
| 51 | + if (!(cnt->flags & OPT_CUDA_COMPAT_MODE_LDCONFIG)) { |
| 52 | + return (0); |
| 53 | + } |
| 54 | + /* For cuda-compat-mode=ldconfig we also ensure that cuda_compat_dir is set. */ |
| 55 | + if (get_compat_library_path(&ctx->err, (const char **)libs, nlibs, &cnt->cuda_compat_dir) < 0) { |
| 56 | + return (-1); |
| 57 | + } |
| 58 | + return (0); |
| 59 | +} |
| 60 | + |
| 61 | +static void |
| 62 | +filter_by_major_version(bool allow_lower_major_versions, const struct nvc_driver_info *info, char * paths[], size_t *size) |
| 63 | +{ |
| 64 | + char *lib, *maj; |
| 65 | + bool exclude; |
| 66 | + /* |
| 67 | + * XXX Filter out any library that has a lower or equal major version than RM to prevent us from |
| 68 | + * running into an unsupported configurations (e.g. CUDA compat on Geforce or non-LTS drivers). |
| 69 | + */ |
| 70 | + for (size_t i = 0; i < *size; ++i) { |
| 71 | + lib = basename(paths[i]); |
| 72 | + if ((maj = strstr(lib, ".so.")) != NULL) { |
| 73 | + maj += strlen(".so."); |
| 74 | + exclude = false; |
| 75 | + if (allow_lower_major_versions) { |
| 76 | + // Only filter out EQUAL RM versions. |
| 77 | + exclude = (strncmp(info->nvrm_version, maj, strspn(maj, "0123456789")) == 0); |
| 78 | + } else { |
| 79 | + // If the major version of RM is greater than or equal to the major version |
| 80 | + // of the library that we are considering, we remove the library from the |
| 81 | + // list. |
| 82 | + exclude = (strncmp(info->nvrm_version, maj, strspn(maj, "0123456789")) >= 0); |
| 83 | + } |
| 84 | + if (exclude) { |
| 85 | + paths[i] = NULL; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + array_pack(paths, size); |
| 90 | +} |
| 91 | + |
| 92 | +static int |
| 93 | +get_compat_library_path(struct error *err, const char * paths[], size_t size, char **compat_dir_result) |
| 94 | +{ |
| 95 | + char *dir; |
| 96 | + char *compat_dir; |
| 97 | + |
| 98 | + if (size == 0) { |
| 99 | + return 0; |
| 100 | + } |
| 101 | + |
| 102 | + char **dirnames = array_copy(err, (const char * const *)paths, size); |
| 103 | + if (dirnames == NULL) { |
| 104 | + return -1; |
| 105 | + } |
| 106 | + |
| 107 | + for (size_t i = 0; i < size; ++i) { |
| 108 | + dir = dirname(dirnames[i]); |
| 109 | + if (i == 0) { |
| 110 | + compat_dir = strdup(dir); |
| 111 | + if (compat_dir == NULL) { |
| 112 | + return -1; |
| 113 | + } |
| 114 | + continue; |
| 115 | + } |
| 116 | + if (strcmp(dir, compat_dir)) { |
| 117 | + goto fail; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + *compat_dir_result = compat_dir; |
| 122 | + return 0; |
| 123 | +fail: |
| 124 | + free(dirnames); |
| 125 | + free(compat_dir); |
| 126 | + return -1; |
| 127 | +} |
0 commit comments