All of lore.kernel.org
 help / color / mirror / Atom feed
* Testing swap
@ 2021-08-12 13:20 Matthew Wilcox
  2021-08-13  6:47 ` Christoph Hellwig
  0 siblings, 1 reply; 2+ messages in thread
From: Matthew Wilcox @ 2021-08-12 13:20 UTC (permalink / raw)
  To: fstests; +Cc: sfrench, linux-fsdevel

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 <dhowells@redhat.com> -----

Date: Thu, 12 Aug 2021 12:57:50 +0100
From: David Howells <dhowells@redhat.com>
To: willy@infradead.org
Cc: dhowells@redhat.com, trond.myklebust@primarydata.com,
	darrick.wong@oracle.com, hch@lst.de, jlayton@kernel.org,
	sfrench@samba.org, torvalds@linux-foundation.org,
	linux-nfs@vger.kernel.org, linux-mm@kvack.org,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
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 -----

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Testing swap
  2021-08-12 13:20 Testing swap Matthew Wilcox
@ 2021-08-13  6:47 ` Christoph Hellwig
  0 siblings, 0 replies; 2+ messages in thread
From: Christoph Hellwig @ 2021-08-13  6:47 UTC (permalink / raw)
  To: Matthew Wilcox, David Howells; +Cc: fstests, sfrench, linux-fsdevel

On Thu, Aug 12, 2021 at 02:20:13PM +0100, Matthew Wilcox wrote:
> 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?

> ----- Forwarded message from David Howells <dhowells@redhat.com> -----
> 
> Date: Thu, 12 Aug 2021 12:57:50 +0100
> From: David Howells <dhowells@redhat.com>
> To: willy@infradead.org
> Cc: dhowells@redhat.com, trond.myklebust@primarydata.com,
> 	darrick.wong@oracle.com, hch@lst.de, jlayton@kernel.org,
> 	sfrench@samba.org, torvalds@linux-foundation.org,
> 	linux-nfs@vger.kernel.org, linux-mm@kvack.org,
> 	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
> 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 -----
---end quoted text---

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-13  6:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-12 13:20 Testing swap Matthew Wilcox
2021-08-13  6:47 ` Christoph Hellwig

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.