Testing swap
All the current xfstests for swap only test setting up a swap partition, not actually doing swap to it. That's why this bug went unnoticed for two years and why Steve French is still under the impression that swap-over-CIFS works (it doesn't). Could somebody adapt Dave's test into the xfstests framework?
----- Forwarded message from David Howells <[email protected]> -----
Date: Thu, 12 Aug 2021 12:57:50 +0100
From: David Howells <[email protected]>
To: [email protected]
Cc: [email protected], [email protected],
[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
Subject: [PATCH 1/2] nfs: Fix write to swapfile failure due to
generic_write_checks()
User-Agent: StGit/0.23
Trying to use a swapfile on NFS results in every DIO write failing with ETXTBSY because generic_write_checks(), as called by nfs_direct_write() from nfs_direct_IO(), forbids writes to swapfiles.
Fix this by introducing a new kiocb flag, IOCB_SWAP, that's set by the swap code to indicate that the swapper is doing this operation and so overrule the check in generic_write_checks().
Without this patch, the following is seen:
Write error on dio swapfile (3800334336)
Altering __swap_writepage() to show the error shows:
Write error (-26) on dio swapfile (3800334336)
Tested by swapping off all swap partitions and then swapping on a prepared NFS file (CONFIG_NFS_SWAP=y is also needed). Enough copies of the following program then need to be run to force swapping to occur (at least one per gigabyte of RAM):
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
int main()
{
unsigned int pid = getpid(), iterations = 0;
size_t i, j, size = 1024 * 1024 * 1024;
char *p;
bool mismatch;
p = malloc(size);
if (!p) {
perror("malloc");
exit(1);
}
srand(pid);
for (i = 0; i < size; i += 4)
*(unsigned int *)(p + i) = rand();
do {
for (j = 0; j < 16; j++) {
for (i = 0; i < size; i += 4096)
*(unsigned int *)(p + i) += 1;
iterations++;
}
mismatch = false;
srand(pid);
for (i = 0; i < size; i += 4) {
unsigned int r = rand();
unsigned int v = *(unsigned int *)(p + i);
if (i % 4096 == 0)
v -= iterations;
if (v != r) {
fprintf(stderr, "mismatch %zx: %x != %x (diff %x)\n",
i, v, r, v - r);
mismatch = true;
}
}
} while (!mismatch);
exit(1);
}----- End forwarded message -----
Quoted 5 lines · Matthew Wilcox
All the current xfstests for swap only test setting up a swap partition, not actually doing swap to it. That's why this bug went unnoticed for two years and why Steve French is still under the impression that swap-over-CIFS works (it doesn't). Could somebody adapt Dave's test into the xfstests framework?
Dave, can you just submit your test to xfstests?
Quoted 77 lines
----- Forwarded message from David Howells <[email protected]> -----
Date: Thu, 12 Aug 2021 12:57:50 +0100
From: David Howells <[email protected]>
To: [email protected]
Cc: [email protected], [email protected],
[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
Subject: [PATCH 1/2] nfs: Fix write to swapfile failure due to
generic_write_checks()
User-Agent: StGit/0.23Trying to use a swapfile on NFS results in every DIO write failing with ETXTBSY because
generic_write_checks(), as called bynfs_direct_write()fromnfs_direct_IO(), forbids writes to swapfiles.Fix this by introducing a new kiocb flag,
IOCB_SWAP, that's set by the swap code to indicate that the swapper is doing this operation and so overrule the check ingeneric_write_checks().Without this patch, the following is seen:
Write error on dio swapfile (3800334336)Altering
__swap_writepage()to show the error shows:Write error (-26) on dio swapfile (3800334336)Tested by swapping off all swap partitions and then swapping on a prepared NFS file (
CONFIG_NFS_SWAP=yis also needed). Enough copies of the following program then need to be run to force swapping to occur (at least one per gigabyte of RAM):#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/mman.h> int main() { unsigned int pid = getpid(), iterations = 0; size_t i, j, size = 1024 * 1024 * 1024; char *p; bool mismatch; p = malloc(size); if (!p) { perror("malloc"); exit(1); } srand(pid); for (i = 0; i < size; i += 4) *(unsigned int *)(p + i) = rand(); do { for (j = 0; j < 16; j++) { for (i = 0; i < size; i += 4096) *(unsigned int *)(p + i) += 1; iterations++; } mismatch = false; srand(pid); for (i = 0; i < size; i += 4) { unsigned int r = rand(); unsigned int v = *(unsigned int *)(p + i); if (i % 4096 == 0) v -= iterations; if (v != r) { fprintf(stderr, "mismatch %zx: %x != %x (diff %x)\n", i, v, r, v - r); mismatch = true; } } } while (!mismatch); exit(1); }
Quoted 1 line
----- End forwarded message -----
---end quoted text---