Skip to content

Commit

Permalink
Merge branch 'upstream-fix-mknod-path' into 'master'
Browse files Browse the repository at this point in the history
Remove path_join() with already chrooted directory

See merge request nvidia/container-toolkit/libnvidia-container!55
  • Loading branch information
klueska committed Feb 3, 2021
2 parents 915f592 + 50fa0d9 commit 3217d38
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions src/nvc.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,10 @@ init_within_userns(struct error *err)
}

static int
mig_nvcaps_mknodes(struct error *err, const char *root, int num_gpus) {
mig_nvcaps_mknodes(struct error *err, int num_gpus) {
FILE *fp;
char line[PATH_MAX];
char rel_path[PATH_MAX];
char abs_path[PATH_MAX];
char path[PATH_MAX];
int gpu = -1, gi = -1, ci = -1, mig_minor = -1;
int rv = -1;

Expand All @@ -166,34 +165,33 @@ mig_nvcaps_mknodes(struct error *err, const char *root, int num_gpus) {

// Walk through each line of NV_CAPS_MIG_MINORS_PATH
memset(line, 0, PATH_MAX);
memset(rel_path, 0, PATH_MAX);
memset(abs_path, 0, PATH_MAX);
memset(path, 0, PATH_MAX);
while (fgets(line, PATH_MAX - 1, fp)) {
// Look for a ci access entry and construct a path into /proc from it
if (sscanf(line, "gpu%d/gi%d/ci%d/access %d", &gpu, &gi, &ci, &mig_minor) == 4) {
if (gpu >= num_gpus)
continue;
if (sprintf(rel_path, NV_COMP_INST_CAPS_PATH "/" NV_MIG_ACCESS_FILE, gpu, gi, ci) < 0) {
if (sprintf(path, NV_COMP_INST_CAPS_PATH "/" NV_MIG_ACCESS_FILE, gpu, gi, ci) < 0) {
error_setx(err, "error constructing path for ci access file");
goto fail;
}
// Look for a gi access entry and construct a path into /proc from it
} else if (sscanf(line, "gpu%d/gi%d/access %d", &gpu, &gi, &mig_minor) == 3) {
if (gpu >= num_gpus)
continue;
if (sprintf(rel_path, NV_GPU_INST_CAPS_PATH "/" NV_MIG_ACCESS_FILE, gpu, gi) < 0) {
if (sprintf(path, NV_GPU_INST_CAPS_PATH "/" NV_MIG_ACCESS_FILE, gpu, gi) < 0) {
error_setx(err, "error constructing path for gi access file");
goto fail;
}
// Look for a mig config entry and construct a path into /proc from it
} else if (sscanf(line, "config %d", &mig_minor) == 1) {
if (sprintf(rel_path, NV_MIG_CAPS_PATH "/" NV_MIG_CONFIG_FILE) < 0) {
if (sprintf(path, NV_MIG_CAPS_PATH "/" NV_MIG_CONFIG_FILE) < 0) {
error_setx(err, "error constructing path for mig config file");
goto fail;
}
// Look for a mig monitor entry and construct a path into /proc from it
} else if (sscanf(line, "monitor %d", &mig_minor) == 1) {
if (sprintf(rel_path, NV_MIG_CAPS_PATH "/" NV_MIG_MONITOR_FILE) < 0) {
if (sprintf(path, NV_MIG_CAPS_PATH "/" NV_MIG_MONITOR_FILE) < 0) {
error_setx(err, "error constructing path for mig monitor file");
goto fail;
}
Expand All @@ -203,25 +201,19 @@ mig_nvcaps_mknodes(struct error *err, const char *root, int num_gpus) {
goto fail;
}

// Join the constructed /proc path with the root of the driver installation
if (path_join(NULL, abs_path, root, rel_path) < 0) {
error_setx(err, "error joining nvcaps path with driver root path");
goto fail;
}

// This file contains entries for all possible MIG nvcaps on up
// to 32 GPUs. If the newly constructed path does not exist,
// then just move on because there are many entries in this
// file that will not be present on the machine.
if (!file_exists(NULL, abs_path))
if (!file_exists(NULL, path))
continue;

// Call into nvidia-modprobe code to perform the mknod() on
// /dev/nvidia-caps/nvidia-cap<mig_minor> from the canonial
// /proc path we constructed.
log_infof("running mknod for " NV_CAPS_DEVICE_PATH " from %s", mig_minor, abs_path);
if (nvidia_cap_mknod(abs_path, &mig_minor) == 0) {
error_setx(err, "error running mknod for nvcap: %s", abs_path);
log_infof("running mknod for " NV_CAPS_DEVICE_PATH " from %s", mig_minor, path);
if (nvidia_cap_mknod(path, &mig_minor) == 0) {
error_setx(err, "error running mknod for nvcap: %s", path);
goto fail;
}
}
Expand Down Expand Up @@ -292,7 +284,7 @@ load_kernel_modules(struct error *err, const char *root)
log_err("could not create kernel module device node");
}
log_info("running mknod for all nvcaps in " NV_CAPS_DEVICE_DIR);
if (mig_nvcaps_mknodes(err, root, devs.num_matches) < 0)
if (mig_nvcaps_mknodes(err, devs.num_matches) < 0)
log_errf("could not create kernel module device nodes: %s", err->msg);
error_reset(err);
}
Expand Down

0 comments on commit 3217d38

Please sign in to comment.