#!/bin/sh
# DF-2879 consequence: force the pager to allocate the phantom tail.
# With the primary swap device removed, the only allocatable swap is
# vn1's stripe [64,128) device-relative -- 1 real page + 63 phantom.
# A memory hog larger than RAM forces anon pageout onto those blocks;
# every pageout that lands on a phantom block fails with EINVAL at the
# disk layer and the pageout daemon wedges in an error-retry loop.
#
# WARNING: on the stock kernel this wedges the guest (by design of the
# bug).  Recovery is a VM reset.  Run as root.
set -e
cc -O2 -o /tmp/hog /tmp/hog.c

dmesg > /tmp/d0
swapctl -d /dev/vbd0s1b        # leave only the phantom vn1 swap
/tmp/hog 5200 >/tmp/hog.log 2>&1 &
sleep 45
echo "== dmesg delta =="
dmesg | diff /tmp/d0 - | head -20 || true
echo "== swap state =="
sysctl vm.swap_size vm.swap_free vm.stats.vm.v_swappgsout
pkill -f /tmp/hog || true
swapctl -a /dev/vbd0s1b || true
