Using Excess Video Memory as SWAP in Linux

English translations of some iportant posts that deserve to be kindly translated
Avatar de Usuario
Naguissa
Administrador del Sitio
Mensajes: 483
Registrado: 04 Jul 2016, 11:17
Agradecido: 102 veces
Agradecimiento recibido: 134 veces

Using Excess Video Memory as SWAP in Linux

Mensaje sin leer por Naguissa »

After my NAS based on an Orange Pi was destroyed when a metal piece (heat sink) fell across the board, I used an old AMD Athlon 64x2 to set up the NAS. It was ideal as it had 2 dual IDE PATA channels and 4 SATA channels.

But in that PC there are only 2GB of RAM, which is quite limited, and an NVIDIA GeForce 210 graphics card with 512MB of video memory, connected via PCI-e.

Considering the low RAM and the unused video RAM (vRAM), I wondered if I could make use of that vRAM. The answer, in Linux, is "obviously yes."

This led me to the Linux MTD (Memory Technology Devices) technology and its ability to create a block device with it. On this device, I could then create a Swap partition with maximum priority.

The idea is to create a block device in the unused video memory of the graphics card. Afterwards, we can use it as we wish, in my case, as a swap partition.

1. Detect the PCI ID of the Graphics Card

Use the `lspci` command to view all PCI devices. Look for the graphics card you want to use and copy its ID:

Código: Seleccionar todo

root@NAS:/home/nas# lspci
00:00.0 Host bridge: Advanced Micro Devices, Inc. [AMD/ATI] RX780/RX790 Host Bridge
00:02.0 PCI bridge: Advanced Micro Devices, Inc. [AMD/ATI] RX780/RD790 PCI to PCI bridge (external gfx0 port A)
00:0a.0 PCI bridge: Advanced Micro Devices, Inc. [AMD/ATI] RD790 PCI to PCI bridge (PCI express gpp port F)
00:11.0 SATA controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 SATA Controller [AHCI mode]
00:12.0 USB controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB OHCI0 Controller
00:12.1 USB controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0 USB OHCI1 Controller
00:12.2 USB controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB EHCI Controller
00:13.0 USB controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB OHCI0 Controller
00:13.1 USB controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0 USB OHCI1 Controller
00:13.2 USB controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB EHCI Controller
00:14.0 SMBus: Advanced Micro Devices, Inc. [AMD/ATI] SBx00 SMBus Controller (rev 3c)
00:14.1 IDE interface: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 IDE Controller
00:14.2 Audio device: Advanced Micro Devices, Inc. [AMD/ATI] SBx00 Azalia (Intel HDA)
00:14.3 ISA bridge: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 LPC host controller
00:14.4 PCI bridge: Advanced Micro Devices, Inc. [AMD/ATI] SBx00 PCI to PCI Bridge
00:14.5 USB controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB OHCI2 Controller
00:18.0 Host bridge: Advanced Micro Devices, Inc. [AMD] K8 [Athlon64/Opteron] HyperTransport Technology Configuration
00:18.1 Host bridge: Advanced Micro Devices, Inc. [AMD] K8 [Athlon64/Opteron] Address Map
00:18.2 Host bridge: Advanced Micro Devices, Inc. [AMD] K8 [Athlon64/Opteron] DRAM Controller
00:18.3 Host bridge: Advanced Micro Devices, Inc. [AMD] K8 [Athlon64/Opteron] Miscellaneous Control
01:00.0 VGA compatible controller: NVIDIA Corporation GT218 [GeForce 210] (rev a2)
01:00.1 Audio device: NVIDIA Corporation High Definition Audio Controller (rev a1)
02:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 03)
2. Note the Memory Blocks

Use `lspci` again with the options `-v` and `-s` along with the PCI ID of the graphics card to extract details about accessible VRAM blocks:

Código: Seleccionar todo

root@NAS:/home/nas# lspci -v -s 01:00.0
01:00.0 VGA compatible controller: NVIDIA Corporation GT218 [GeForce 210] (rev a2) (prog-if 00 [VGA controller])
Subsystem: ASUSTeK Computer Inc. GT218 [GeForce 210]
Flags: bus master, fast devsel, latency 0, IRQ 18, NUMA node 0
Memory at fb000000 (32-bit, non-prefetchable) [size=16M]
Memory at c0000000 (64-bit, prefetchable) [size=256M]
Memory at de000000 (64-bit, prefetchable) [size=32M]
I/O ports at ef00 [size=128]
Expansion ROM at 000c0000 [virtual] [disabled] [size=128K]
Capabilities: [60] Power Management version 3
Capabilities: [68] MSI: Enable- Count=1/1 Maskable- 64bit+
Capabilities: [78] Express Endpoint, MSI 00
Capabilities: [b4] Vendor Specific Information: Len=14 <?>
Capabilities: [100] Virtual Channel
Capabilities: [128] Power Budgeting <?>
Capabilities: [600] Vendor Specific Information: ID=0001 Rev=1 Len=024 <?>
Kernel driver in use: nvidia
Kernel modules: nvidia
In this case, I'm going to use the block:

Código: Seleccionar todo

Memory at c0000000 (64-bit, prefetchable) [size=256M]
Also, leave some MBs free for the card's operation; in my case, I'm leaving 32MB, but a few MBs may be sufficient for text mode.

3. Configure the phram Module

Use the values from the previous step to configure the phram module according to your needs:

- Block Address: Initial address of the graphics card's VRAM block (e.g., c0000000).
- Block size, in Mbytes: Size of the block in MB (e.g., 256).
- Reserved size, in Mbytes: Leave some MB for video memory, even for text mode (e.g., 32).

Use this calculator to extract the values and save them in a new file named `/etc/modprobe.d/vram-swap.conf`.




It should look something like:

Código: Seleccionar todo

options phram phram=swap,0xc2000000,224Mi
If you want to add more than one block, repeat the calculation and add the new block behind, changing its name. For example:

Código: Seleccionar todo

options phram phram=swap,0xc2000000,224Mi phram=swapB,0xff000000,64Mi
4. Configure X

Especially important if you use X, create the file `/etc/X11/xorg.conf.d/vramswap.conf` and add the VRAM limitation that you reserved in the previous step, in my example, 32MB:

Código: Seleccionar todo

Section "Device"
    Driver "nvidia" # or whichever other driver you use
    VideoRam 32768
EndSection
5. Test Everything

Now load the modules manually to test everything:

Código: Seleccionar todo

root@NAS:/home/nas# modprobe phram
root@NAS:/home/nas# modprobe mtdblock
root@NAS:/home/nas# mkswap /dev/mtdblock0
root@NAS:/home/nas# swapon /dev/mtdblock0 -p 10
(Use `mkswap` and `swapon` with other mtdblock devices if you have created more than one.)

Now it's ideal to stress the system or use it heavily for a while to check for stability. If there is instability, you can increase the reserved memory from step 3.

6. Configure for System Startup

Edit the file `/etc/modules-load.d/vramswap.conf` and add the modules to load automatically at system startup:

Código: Seleccionar todo

phram
mtdblock
Then create the systemd service to start everything automatically:

Edit the file `/usr/lib/systemd/system/vramswap.service`

Código: Seleccionar todo

[Unit]
Description=Swap on Video RAM

[Service]
Type=oneshot
ExecStart=/usr/sbin/mkswap /dev/mtdblock0
ExecStart=/usr/sbin/swapon /dev/mtdblock0 -p 10
ExecStop=/usr/sbin/swapoff /dev/mtdblock0
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
Here, you need to add all the blocks you created, for example:

Código: Seleccionar todo

[Unit]
Description=Swap on Video RAM

[Service]
Type=oneshot
ExecStart=/usr/sbin/mkswap /dev/mtdblock0
ExecStart=/usr/sbin/swapon /dev/mtdblock0 -p 10
ExecStart=/usr/sbin/mkswap /dev/mtdblock1
ExecStart=/usr/sbin/swapon /dev/mtdblock1 -p 10
ExecStop=/usr/sbin/swapoff /dev/mtdblock0
ExecStop=/usr/sbin/swapoff /dev/mtdblock1
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
And enable it on boot:

Código: Seleccionar todo

root@NAS:/home/nas# systemctl enable vramswap

With this, the system is configured to use that unused video memory as SWAP with each restart.



Source: https://wiki.archlinux.org/title/Swap_on_video_RAM

Tags:
  • Similar Topics
    Respuestas
    Vistas
    Último mensaje