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
CUDApp / tests / ampere / memory_refusal.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* Safe device proof: set reserve ABOVE observed free VRAM, then request one small page. |
| 3 | * Never consume VRAM to induce pressure. Link --wrap=ioctl to count real RM allocation |
| 4 | * calls independently of the allocation result; heap-info control ioctls are allowed. */ |
| 5 | #define _POSIX_C_SOURCE 200809L |
| 6 | #include "omega_gpu_session.h" |
| 7 | #include "omega_gpu_platform_linux.h" |
| 8 | #include "nv-ioctl-numbers.h" |
| 9 | #include "nv_escape.h" |
| 10 | #include <inttypes.h> |
| 11 | #include <stdarg.h> |
| 12 | #include <stdint.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <sys/ioctl.h> |
| 17 | |
| 18 | static uint64_t allocation_ioctls; |
| 19 | int __real_ioctl(int fd,unsigned long request,...); |
| 20 | int __wrap_ioctl(int fd,unsigned long request,...) { |
| 21 | va_list ap; va_start(ap,request);void *argument=va_arg(ap,void *);va_end(ap); |
| 22 | if(_IOC_TYPE(request)==NV_IOCTL_MAGIC && |
| 23 | (_IOC_NR(request)==NV_ESC_RM_ALLOC||_IOC_NR(request)==NV_ESC_RM_ALLOC_MEMORY|| |
| 24 | _IOC_NR(request)==NV_ESC_RM_ALLOC_OBJECT||_IOC_NR(request)==NV_ESC_RM_ALLOC_CONTEXT_DMA2)) |
| 25 | allocation_ioctls++; |
| 26 | return __real_ioctl(fd,request,argument); |
| 27 | } |
| 28 | int main(int argc,char **argv) { |
| 29 | if(argc!=2||strcmp(argv[1],"--device")) { puts("AMPERE MEMORY REFUSAL NOT_RUN (explicit --device required)");return 2; } |
| 30 | int rc=1;uint64_t before=0,total=0,after=0,total_after=0; |
| 31 | OmegaGpuMem small={0}; |
| 32 | omega_gpu_session_lock(); |
| 33 | if(!omega_gpu_session_open()||omega_gpu_session_target_sm()!=86) { |
| 34 | fprintf(stderr,"AMPERE MEMORY REFUSAL NOT_RUN: open/target: %s\n",omega_gpu_session_last_error());rc=2;goto done; |
| 35 | } |
| 36 | if(omega_gpu_platform_linux_vram_info(&before,&total)||before>total||total==UINT64_MAX)goto done; |
| 37 | /* Above total is also above free, even if another process frees VRAM |
| 38 | * between these queries. This can never become a real pressure allocation. */ |
| 39 | char reserve[32];snprintf(reserve,sizeof reserve,"%"PRIu64,total+1); |
| 40 | if(setenv("CUDAPP_GPU_MIN_FREE_BYTES",reserve,1))goto done; |
| 41 | uint64_t calls_before=allocation_ioctls; |
| 42 | int allocated=omega_gpu_session_alloc(4096,&small); |
| 43 | uint64_t calls_after=allocation_ioctls; |
| 44 | if(omega_gpu_platform_linux_vram_info(&after,&total_after))goto done; |
| 45 | printf("reserve_bytes=%s requested_bytes=4096 free_before_bytes=%"PRIu64" free_after_bytes=%"PRIu64" total_bytes=%"PRIu64" rm_alloc_calls_before=%"PRIu64" rm_alloc_calls_after=%"PRIu64"\n", |
| 46 | reserve,before,after,total,calls_before,calls_after); |
| 47 | if(!allocated||small.cpu||small.va||calls_before!=calls_after||before!=after||total!=total_after) { |
| 48 | fprintf(stderr,"AMPERE MEMORY REFUSAL FAIL (allocation, driver call, or VRAM delta observed)\n");goto done; |
| 49 | } |
| 50 | if(!strstr(omega_gpu_session_last_error(),"reserve")) { |
| 51 | fprintf(stderr,"AMPERE MEMORY REFUSAL FAIL: refusal was not the reserve gate: %s\n",omega_gpu_session_last_error());goto done; |
| 52 | } |
| 53 | puts("AMPERE MEMORY REFUSAL PASS (small allocation refused before RM allocation; free VRAM unchanged)");rc=0; |
| 54 | done: |
| 55 | if(small.cpu)omega_gpu_session_free(&small); |
| 56 | if(rc==1)fprintf(stderr,"AMPERE MEMORY REFUSAL FAIL stage=%s\n",omega_gpu_session_last_error()); |
| 57 | omega_gpu_session_unlock();omega_gpu_session_close();return rc; |
| 58 | } |