<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Linux-Swap on K-Life Hack | Systems Architecture &amp; DevOps</title><link>https://klifehack.com/en/tags/linux-swap/</link><description>Recent content in Linux-Swap on K-Life Hack | Systems Architecture &amp; DevOps</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Fri, 12 Jun 2026 10:07:02 +0900</lastBuildDate><atom:link href="https://klifehack.com/en/tags/linux-swap/index.xml" rel="self" type="application/rss+xml"/><item><title>Architecture and Implementation of NBD-VRAM: Utilizing NVIDIA GPU VRAM as Linux Swap Space</title><link>https://klifehack.com/en/p/nvidia-vram-linux-swap-nbd/</link><pubDate>Fri, 12 Jun 2026 10:07:02 +0900</pubDate><guid>https://klifehack.com/en/p/nvidia-vram-linux-swap-nbd/</guid><description>&lt;h1 id="high-speed-linux-swap-tier-using-gpu-vram-architecture-and-implementation-of-nbd-vram"&gt;High-Speed Linux Swap Tier Using GPU VRAM: Architecture and Implementation of NBD-VRAM
&lt;/h1&gt;&lt;p&gt;The proliferation of containers in development environments and the execution of large-scale build processes lead to the exhaustion of physical memory (System RAM), causing system-wide hangs and process termination by the OOM (Out Of Memory) killer. This issue is particularly prominent in laptops with onboard memory or edge nodes where physical memory expansion is difficult. Traditional swap space expansion targeting SSDs or HDDs not only significantly shortens storage write endurance (TBW) but also causes overall system performance degradation due to I/O bottlenecks.&lt;/p&gt;
&lt;p&gt;Against this background, an approach that repurposes the VRAM (Video RAM) of NVIDIA GPUs—which are installed in systems but remain idle during periods when they are not executing graphics processing or machine learning tasks—as a high-speed Linux swap tier has attracted attention. This article provides a technical analysis of the architecture, implementation process, and operational considerations of &amp;ldquo;NBD-VRAM&amp;rdquo;, an open-source project that combines Network Block Device (NBD) and the CUDA API.&lt;/p&gt;
&lt;h2 id="memory-hierarchy-architecture-of-nbd-vram"&gt;Memory Hierarchy Architecture of NBD-VRAM
&lt;/h2&gt;&lt;p&gt;💡 NBD-VRAM does not treat VRAM as a simple direct expansion of physical memory, but positions it as an &amp;ldquo;ultra-high-speed intermediate swap tier&amp;rdquo; within the Linux virtual memory management system. This establishes a multi-tier memory hierarchy.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;b&gt;System RAM&lt;/b&gt;: The lowest-latency and highest-bandwidth primary memory region.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;VRAM Swap (NBD-VRAM)&lt;/b&gt;: A secondary swap region faster than SSDs (securing a bandwidth of approximately 1.3 GB/s under an RTX 3070 environment).&lt;/li&gt;
&lt;li&gt;&lt;b&gt;zRAM&lt;/b&gt;: A RAM-based swap region utilizing memory compression technology.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;SSD/HDD Swap&lt;/b&gt;: The final and slowest swap region configured on persistent storage.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Since data overflowing from physical memory is evacuated to the VRAM swap tier before being written directly to a slow SSD, system-wide I/O wait is minimized, making it possible to maintain operational responsiveness even under high loads.&lt;/p&gt;
&lt;h2 id="operating-principle-and-data-flow"&gt;Operating Principle and Data Flow
&lt;/h2&gt;&lt;p&gt;NBD-VRAM operates without building custom kernel modules by combining the official NVIDIA driver stack, the CUDA API, and the standard Linux NBD (Network Block Device) protocol.&lt;/p&gt;
&lt;h3 id="1-vram-allocation-and-management"&gt;1. VRAM Allocation and Management
&lt;/h3&gt;&lt;p&gt;A daemon process running in user space calls the CUDA API to allocate a VRAM region of the specified size. Because consumer NVIDIA GPUs (such as the GeForce series) have limitations on direct access to BAR1 memory and Peer-to-Peer (P2P) APIs supported by professional GPUs (such as Quadro or Tesla), NBD-VRAM uses standard CUDA memory copy functions (cuMemcpyHtoD / cuMemcpyDtoH) to transfer data between host RAM and GPU device memory.&lt;/p&gt;
&lt;h3 id="2-virtual-block-device-creation"&gt;2. Virtual Block Device Creation
&lt;/h3&gt;&lt;p&gt;The daemon associates the allocated VRAM region with the Linux kernel&amp;rsquo;s NBD subsystem. This allows the kernel to recognize it as a standard block device, such as /dev/nbd0. The structure of the data path is defined as follows.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[Kernel Swap Subsystem] ──&amp;amp;gt; [/dev/nbd0] ──&amp;amp;gt; [NBD Daemon (User Space)] ──&amp;amp;gt; [CUDA API] ──&amp;amp;gt; [GPU VRAM]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="3-enabling-the-swap-space"&gt;3. Enabling the Swap Space
&lt;/h3&gt;&lt;p&gt;The created virtual block device is initialized and enabled as swap using standard Linux utilities.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;mkswap /dev/nbd0
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;swapon -p &lt;span style="color:#ae81ff"&gt;100&lt;/span&gt; /dev/nbd0
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="system-implementation-and-configuration-settings"&gt;System Implementation and Configuration Settings
&lt;/h2&gt;&lt;p&gt;🛠️ To automatically enable NBD-VRAM at system startup, configure it as a Systemd service unit. An example configuration allocating 7GB (7168MB) of VRAM with a swap priority of 100 is shown below.&lt;/p&gt;
&lt;h3 id="1-placing-the-daemon-configuration-file"&gt;1. Placing the Daemon Configuration File
&lt;/h3&gt;&lt;p&gt;Write the service definition in /etc/systemd/system/nbd-vram.service.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-ini" data-lang="ini"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;[Unit]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;Description&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;NBD-VRAM Swap Service&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;After&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;systemd-modules-load.service&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;[Service]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;Type&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;simple&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;ExecStart&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;/usr/local/bin/nbd-vram --size 7168 --device /dev/nbd0 --priority 100&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;ExecStop&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;/usr/bin/nbd-client -d /dev/nbd0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;Restart&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;always&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;[Install]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;WantedBy&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;multi-user.target&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="2-dynamic-resizing-feature"&gt;2. Dynamic Resizing Feature
&lt;/h3&gt;&lt;p&gt;If VRAM allocation of the specified size (e.g., 7168MB) fails at startup, NBD-VRAM features a fallback mechanism that automatically retries while reducing the allocation size by &lt;b&gt;512MB&lt;/b&gt; at a time. This automatically configures the swap with the maximum allocatable capacity even if other processes are already consuming VRAM.&lt;/p&gt;
&lt;h2 id="troubleshooting"&gt;Troubleshooting
&lt;/h2&gt;&lt;p&gt;⚠️ Typical failure factors likely to occur during deployment in real-world environments and their resolution workflows are shown below.&lt;/p&gt;
&lt;h3 id="1-nbd-kernel-module-not-loaded"&gt;1. NBD Kernel Module Not Loaded
&lt;/h3&gt;&lt;p&gt;If errors such as modprobe: FATAL: Module nbd not found occur when starting the service, NBD is either not enabled in the kernel configuration or the module is not loaded.&lt;/p&gt;
&lt;p&gt;As a countermeasure, attempt to reinstall the kernel package or manually load the module.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;sudo modprobe nbd
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="2-cuda-initialization-error-driverlibrary-mismatch"&gt;2. CUDA Initialization Error (Driver/Library Mismatch)
&lt;/h3&gt;&lt;p&gt;If the system has not been rebooted after an NVIDIA driver update, the daemon will fail to initialize libcuda.so.&lt;/p&gt;
&lt;p&gt;As a countermeasure, verify that nvidia-smi responds normally, and if there is a mismatch, reload the driver stack or reboot the host.&lt;/p&gt;
&lt;h3 id="3-verification-steps-for-normal-operation"&gt;3. Verification Steps for Normal Operation
&lt;/h3&gt;&lt;p&gt;After starting the service, run verification commands to check the system status.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;swapon --show
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;free -h
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="operational-notes"&gt;Operational Notes
&lt;/h2&gt;&lt;p&gt;While NBD-VRAM is an extremely effective workaround in memory-constrained environments, the following technical constraints must be accepted during operation.&lt;/p&gt;
&lt;p&gt;・&lt;b&gt;Latency Overhead&lt;/b&gt;: Because multiple abstraction layers (Swap -&amp;gt; NBD -&amp;gt; Unix Socket -&amp;gt; Daemon -&amp;gt; CUDA -&amp;gt; VRAM) intervene in the data transfer path, latency occurs compared to direct access to physical RAM. This feature should not be positioned as a &amp;ldquo;complete replacement for physical RAM,&amp;rdquo; but rather as a &amp;ldquo;high-speed buffer to prevent fallback to SSD swap.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;・&lt;b&gt;Resource Contention&lt;/b&gt;: Since VRAM is occupied as swap, executing 3D rendering or deep learning training processes on the same GPU will cause VRAM capacity contention, leading to performance degradation or out-of-memory errors.&lt;/p&gt;
&lt;p&gt;・&lt;b&gt;Difference from CUDA Memory Expansion&lt;/b&gt;: This tool expands the &amp;ldquo;operating system swap space&amp;rdquo; and does not directly expand the GPU&amp;rsquo;s computation memory to hold weight data for large-scale AI models. Model loading still depends on the native free VRAM capacity of the GPU.&lt;/p&gt;
&lt;p&gt;With an understanding of these characteristics, consider introducing this tool as a means to achieve both SSD lifespan protection and system stabilization in environments such as concurrent development container operations or build servers where temporary memory bursts occur.&lt;/p&gt;</description></item></channel></rss>