From 757d26edba7d7de8c564a87a262a0b1321ddf804 Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Wed, 14 Jun 2023 14:58:25 +0200 Subject: all: support swap feature on Linux If the feature is supported on the device, allocate a 128MB swap file after VM boot and activate it. --- executor/common_linux.h | 61 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 9 deletions(-) (limited to 'executor/common_linux.h') diff --git a/executor/common_linux.h b/executor/common_linux.h index 621f0b2d8..d5152e058 100644 --- a/executor/common_linux.h +++ b/executor/common_linux.h @@ -1153,6 +1153,18 @@ static void initialize_wifi_devices(void) } #endif +#if SYZ_EXECUTOR || (SYZ_NET_DEVICES && SYZ_NIC_VF) || SYZ_SWAP +static int runcmdline(char* cmdline) +{ + debug("%s\n", cmdline); + int ret = system(cmdline); + if (ret) { + debug("FAIL: %s\n", cmdline); + } + return ret; +} +#endif + #if SYZ_EXECUTOR || SYZ_NET_DEVICES #include #include @@ -1428,15 +1440,6 @@ error: } #if SYZ_EXECUTOR || SYZ_NIC_VF -static int runcmdline(char* cmdline) -{ - debug("%s\n", cmdline); - int ret = system(cmdline); - if (ret) { - debug("FAIL: %s\n", cmdline); - } - return ret; -} static void netlink_nicvf_setup(void) { @@ -5574,3 +5577,43 @@ static long syz_pkey_set(volatile long pkey, volatile long val) return 0; } #endif + +#if SYZ_EXECUTOR || SYZ_SWAP +#include +#include +#include +#include +#include +#include + +#define SWAP_FILE "./swap-file" +#define SWAP_FILE_SIZE (128 * 1000 * 1000) // 128 MB. + +static void setup_swap() +{ + // The call must be idempotent, so first disable swap and remove the swap file. + swapoff(SWAP_FILE); + unlink(SWAP_FILE); + // Zero-fill the file. + int fd = open(SWAP_FILE, O_CREAT | O_WRONLY | O_CLOEXEC, 0600); + if (fd == -1) { + failmsg("swap file open failed", "file: %s", SWAP_FILE); + return; + } + // We cannot do ftruncate -- swapon complains about this. Do fallocate instead. + fallocate(fd, FALLOC_FL_ZERO_RANGE, 0, SWAP_FILE_SIZE); + close(fd); + // Set up the swap file. + char cmdline[64]; + sprintf(cmdline, "mkswap %s", SWAP_FILE); + if (runcmdline(cmdline)) { + fail("mkswap failed"); + return; + } + if (swapon(SWAP_FILE, SWAP_FLAG_PREFER) == 1) { + failmsg("swapon failed", "file: %s", SWAP_FILE); + return; + } +} + +#endif -- cgit mrf-deployment