interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / src / omega_gpu_mem_reserve.c

162 lines · 7326 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* omega_gpu_mem_reserve.c -- developer bar D1: refuse an allocation that would leave less than the reserve free
3 * (src/omega_gpu_mem_reserve.h). Hosted only: reads /proc/meminfo, or a regular file a test names by code with
4 * omega_gpu_platform_linux_test_set_meminfo. */
5#if defined(CUDAPP_MIGRATION_PENDING_C_MEMRESV)
6#define ENG_C_PART 1
7#endif
8#include "omega_eng_dec.h"
9#include "omega_gpu_mem_reserve.h"
10#include "cudapp_native.h"
11
12#include "omega_gpu_platform.h"
13#include "omega_gpu_platform_linux.h"
14
15#include <fcntl.h>
16#include <pthread.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <sys/stat.h>
21#include <unistd.h>
22
23/* The meminfo file is chosen by code, never by the environment: the Linux platform is the default session platform
24 * (omega_gpu_session.c install_default) of every hosted program linked with libomega_gpu.a, serving processes included,
25 * so a stale variable left in an environment must not be able to switch the D1 guard off (CodeQL cpp/path-injection
26 * #40 was the former CUDAPP_MEMINFO variable). Every open is O_NOFOLLOW|O_NONBLOCK and must reach a regular file, since
27 * the check runs under the platform lock: open() on a FIFO blocks, and a device file may act on open. A test file that
28 * cannot be used that way falls back to the real /proc/meminfo. */
29static pthread_mutex_t g_meminfo_mu = PTHREAD_MUTEX_INITIALIZER;
30static char g_meminfo_test_path[4096]; /* guarded by g_meminfo_mu; "" = /proc/meminfo */
31
32int omega_gpu_platform_linux_test_set_meminfo(const char *path) {
33 size_t n = path ? strlen(path) : 0;
34 if (n >= sizeof g_meminfo_test_path) return -1;
35 pthread_mutex_lock(&g_meminfo_mu);
36 memcpy(g_meminfo_test_path, n ? path : "", n);
37 g_meminfo_test_path[n] = '\0';
38 pthread_mutex_unlock(&g_meminfo_mu);
39 return 0;
40}
41
42static FILE *meminfo_open_regular(const char *path) {
43 int fd = open(path, O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC);
44 if (fd < 0) return NULL;
45 struct stat st;
46 if (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) { close(fd); return NULL; }
47 FILE *f = fdopen(fd, "r");
48 if (!f) close(fd);
49 return f;
50}
51
52static FILE *meminfo_open(void) {
53 pthread_mutex_lock(&g_meminfo_mu);
54 FILE *f = g_meminfo_test_path[0] ? meminfo_open_regular(g_meminfo_test_path) : NULL;
55 pthread_mutex_unlock(&g_meminfo_mu);
56 return f ? f : meminfo_open_regular("/proc/meminfo");
57}
58
59static void human_bytes(char *out, size_t n, uint64_t b) {
60 if (b < (1ull << 30)) snprintf(out, n, "%.1f MiB", (double)b / (double)(1ull << 20));
61 else snprintf(out, n, "%.1f GiB", (double)b / (double)(1ull << 30));
62}
63
64/* Scalar parsing and saturation live in om/session/reserve.om. The default has no
65 * strtoull/sscanf fallback. A binding failure retains the maximum reserve or
66 * refuses the allocation; it must not turn a broken parser into a disabled guard. */
67static unsigned long long reserve_mib_env(int *valid) {
68 *valid = 1;
69 const char *e = getenv("CUDAPP_MEM_RESERVE_MIB");
70#if defined(CUDAPP_MIGRATION_PENDING_C_MEMRESV)
71 unsigned long long reserve_mib = CUDAPP_MEM_RESERVE_DEFAULT_MIB;
72 if (e && *e) { char *end = NULL; unsigned long long v = strtoull(e, &end, 10); if (end && end != e && *end == 0) reserve_mib = v; }
73 return reserve_mib;
74#else
75 uint64_t result = UINT64_MAX, out[2] = {0};
76 if (omega_memreserve(&result, 0, e, e ? strlen(e) : 0, 0, out, 2) != 0) { *valid = 0; return UINT64_MAX; }
77 return result;
78#endif
79}
80
81/* Saturate rather than wrap: 2^44 MiB shifted by 20 is 2^64 (RtMem F9). */
82static uint64_t mib_bytes(unsigned long long mib) {
83#if defined(CUDAPP_MIGRATION_PENDING_C_MEMRESV)
84 return mib > (UINT64_MAX >> 20) ? UINT64_MAX : (uint64_t)mib << 20;
85#else
86 uint64_t result = UINT64_MAX, out[2] = {0};
87 if (omega_memreserve(&result, 2, NULL, 0, mib, out, 2) != 0) return UINT64_MAX;
88 return result;
89#endif
90}
91
92static int reserve_available(const char *line, unsigned long long *kb) {
93#if defined(CUDAPP_MIGRATION_PENDING_C_MEMRESV)
94 return sscanf(line, "MemAvailable: %llu kB", kb) == 1;
95#else
96 uint64_t result = 0, out[2] = {0};
97 if (omega_memreserve(&result, 1, line, strlen(line), 0, out, 2) != 0) return -1;
98 if (result) *kb = out[0];
99 return result ? 1 : 0;
100#endif
101}
102
103uint64_t omega_gpu_mem_reserve_bytes(void) { int valid; return mib_bytes(reserve_mib_env(&valid)); }
104
105int omega_gpu_mem_reserve_check(uint64_t bytes, char *diag, size_t diag_len) {
106 int valid = 0;
107 const unsigned long long reserve_mib = reserve_mib_env(&valid);
108 if (!valid) {
109 snprintf(diag, diag_len, "dma_alloc: reserve parser binding refused");
110 return OMEGA_GPU_PLAT_REFUSED_LIMIT;
111 }
112 if (eng_on()) {
113 unsigned long long avail_kb = 0;
114 int have = 0;
115 uint64_t in[4], w[16];
116 if (reserve_mib != 0) {
117 FILE *f = meminfo_open();
118 if (f) {
119 char line[256];
120 while (!have && fgets(line, sizeof line, f)) have = reserve_available(line, &avail_kb);
121 fclose(f);
122 if (have < 0) {
123 snprintf(diag, diag_len, "dma_alloc: MemAvailable parser binding refused");
124 return OMEGA_GPU_PLAT_REFUSED_LIMIT;
125 }
126 }
127 }
128 in[0] = bytes;
129 in[1] = avail_kb;
130 in[2] = reserve_mib;
131 in[3] = (reserve_mib != 0 && have) ? 1u : 0u;
132 if (eng_ask(ENG_OP_RESV, in, 4, w) != 0) return OMEGA_GPU_PLAT_REFUSED_LIMIT;
133 if (w[0] == 0) return OMEGA_GPU_PLAT_OK;
134 char want[32], free_[32], keep[32];
135 human_bytes(want, sizeof want, bytes);
136 human_bytes(free_, sizeof free_, w[1]);
137 human_bytes(keep, sizeof keep, w[2]);
138 snprintf(diag, diag_len,
139 "dma_alloc: refused %s: %s of memory is free and CUDA++ keeps %s free (CUDAPP_MEM_RESERVE_MIB=%llu), because the CPU and "
140 "GPU share it and running it out freezes the machine. Free buffers or use a smaller batch.",
141 want, free_, keep, reserve_mib);
142 return (int)w[0];
143 }
144 if (reserve_mib == 0) return OMEGA_GPU_PLAT_OK;
145 FILE *f = meminfo_open();
146 if (!f) return OMEGA_GPU_PLAT_OK;
147 unsigned long long avail_kb = 0; int have = 0; char line[256];
148 while (!have && fgets(line, sizeof line, f)) have = reserve_available(line, &avail_kb);
149 fclose(f);
150 if (!have) return OMEGA_GPU_PLAT_OK;
151 /* Saturate rather than wrap: 2^54 kB times 1024 is 2^64 (RtMem F9). */
152 const uint64_t avail = avail_kb > UINT64_MAX / 1024ull ? UINT64_MAX : (uint64_t)avail_kb * 1024ull;
153 const uint64_t reserve = mib_bytes(reserve_mib);
154 if (bytes <= avail && avail - bytes >= reserve) return OMEGA_GPU_PLAT_OK;
155 char want[32], free_[32], keep[32];
156 human_bytes(want, sizeof want, bytes); human_bytes(free_, sizeof free_, avail); human_bytes(keep, sizeof keep, reserve);
157 snprintf(diag, diag_len,
158 "dma_alloc: refused %s: %s of memory is free and CUDA++ keeps %s free (CUDAPP_MEM_RESERVE_MIB=%llu), because the CPU and "
159 "GPU share it and running it out freezes the machine. Free buffers or use a smaller batch.",
160 want, free_, keep, reserve_mib);
161 return OMEGA_GPU_PLAT_REFUSED_LIMIT;
162}