Received: by 2002:a05:6a10:8a4d:0:0:0:0 with SMTP id dn13csp179798pxb; Thu, 12 Aug 2021 13:45:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysb7d33/U79kCQe057KNjSEBN5zhVgcgm2MIJM4cohK6oIuhpc7DPqkqVh9T0vWHmVcRbu X-Received: by 2002:a17:906:368e:: with SMTP id a14mr5596616ejc.60.1628801133099; Thu, 12 Aug 2021 13:45:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628801133; cv=none; d=google.com; s=arc-20160816; b=muomt/wj9XO76F/mM7kngkl3RAxyki4fCay4x6CF1LdbsPYzdZr5y2qRrsu0kSYbQH j2KoZvYLBYT44DF1uZHwp5TqD/H95zHpjj2nQD7++AaTwkmuP1Kk+S08PkF6msisVVc+ VOFWm1jbtyAM3ZtGi0JhOSK4rwG+EIdFsvuK/OB4exN3z5/dE5EatU641koqWR5+bihQ QXqKT51obPqfo9GytlvMPRK5YgCEYq1aJISF66qxlilSOHTNUfWGQd/J0Igs1HArJMUq ImG/PEoyylBdK0UyYTUxf1TjqU1ZizBs0Iig+BsRIVDA0iBqgh78lxKmUvleLRkt39Wv dsBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject:organization:dkim-signature; bh=qTmU29yZIgB7SK96cJsT0SpdzVNLSTZbvkXxHkWMKmU=; b=gKhQEExdnd65rNFPZEeD1IBOnpc+1qlIIU80k2/K/Z5ym9g+DAVChyWD34N92iEDHs 5BY3wgmRR1PxIzylo+W/x0+JClG+Ufma6kMebU1uGkox+/Ib8UMjSanlflUWqJBYzCjN e9JF01uezTE8ZQw2NLHAA8ngXQYpvkAxHe3pyrbohOQWa9ToGNvkfMgzPikYWb65jG4q 0B34w6nhnwnyauQGmXlbBQawUfrU+yTrrdC5CYCnsqmGM79bXdjjtZcbIS9HxK5fFoDc JqhxV1T4DIJEZSNI+x2ioB+i2W/QNZf28V8LASr7kdoRUukHUwrm+jSeYW12fy88VQM0 s0Yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UIt0rWoR; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j7si3541620eja.129.2021.08.12.13.45.09; Thu, 12 Aug 2021 13:45:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UIt0rWoR; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237252AbhHLUWs (ORCPT + 99 others); Thu, 12 Aug 2021 16:22:48 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:60963 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237320AbhHLUWl (ORCPT ); Thu, 12 Aug 2021 16:22:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628799735; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qTmU29yZIgB7SK96cJsT0SpdzVNLSTZbvkXxHkWMKmU=; b=UIt0rWoR9u2HOZoxD5CpbWskcqYqw0e2+0AL8/MWtcQHGfCr/p8VUGzz8/1c00vtnnJ37W IQY5AQjSwgqftniEibQWkuAQSAmiodBOxcCBZSrVZ9FVmx77xxR3O9PHPPn07RHoTocLDZ XrlLXeu8bzfEok+wX5ckPlZEhvA3HSk= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-561-OOb8V-K0PFKzUe5mosFGXw-1; Thu, 12 Aug 2021 16:22:12 -0400 X-MC-Unique: OOb8V-K0PFKzUe5mosFGXw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 43A641853029; Thu, 12 Aug 2021 20:22:10 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.22.32.7]) by smtp.corp.redhat.com (Postfix) with ESMTP id 96A9C669ED; Thu, 12 Aug 2021 20:22:07 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [RFC PATCH v2 1/5] nfs: Fix write to swapfile failure due to generic_write_checks() From: David Howells To: willy@infradead.org Cc: "Darrick J. Wong" , Christoph Hellwig , Trond Myklebust , linux-nfs@vger.kernel.org, dhowells@redhat.com, dhowells@redhat.com, trond.myklebust@primarydata.com, darrick.wong@oracle.com, hch@lst.de, viro@zeniv.linux.org.uk, 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 Date: Thu, 12 Aug 2021 21:22:06 +0100 Message-ID: <162879972678.3306668.10709543333474121000.stgit@warthog.procyon.org.uk> In-Reply-To: <162879971699.3306668.8977537647318498651.stgit@warthog.procyon.org.uk> References: <162879971699.3306668.8977537647318498651.stgit@warthog.procyon.org.uk> User-Agent: StGit/0.23 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org 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 #include #include #include #include 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); } Fixes: dc617f29dbe5 ("vfs: don't allow writes to swap files") Signed-off-by: David Howells cc: Darrick J. Wong cc: Christoph Hellwig cc: Trond Myklebust cc: linux-nfs@vger.kernel.org --- fs/read_write.c | 2 +- include/linux/fs.h | 1 + mm/page_io.c | 7 ++++--- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/fs/read_write.c b/fs/read_write.c index 9db7adf160d2..daef721ca67e 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -1646,7 +1646,7 @@ ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from) loff_t count; int ret; - if (IS_SWAPFILE(inode)) + if (IS_SWAPFILE(inode) && !(iocb->ki_flags & IOCB_SWAP)) return -ETXTBSY; if (!iov_iter_count(from)) diff --git a/include/linux/fs.h b/include/linux/fs.h index 640574294216..b3e6a20f28ef 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -319,6 +319,7 @@ enum rw_hint { /* iocb->ki_waitq is valid */ #define IOCB_WAITQ (1 << 19) #define IOCB_NOIO (1 << 20) +#define IOCB_SWAP (1 << 21) /* This is a swap request */ struct kiocb { struct file *ki_filp; diff --git a/mm/page_io.c b/mm/page_io.c index d597bc6e6e45..edb72bf624d2 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -303,7 +303,8 @@ int __swap_writepage(struct page *page, struct writeback_control *wbc, iov_iter_bvec(&from, WRITE, &bv, 1, PAGE_SIZE); init_sync_kiocb(&kiocb, swap_file); - kiocb.ki_pos = page_file_offset(page); + kiocb.ki_pos = page_file_offset(page); + kiocb.ki_flags = IOCB_DIRECT | IOCB_WRITE | IOCB_SWAP; set_page_writeback(page); unlock_page(page); @@ -324,8 +325,8 @@ int __swap_writepage(struct page *page, struct writeback_control *wbc, */ set_page_dirty(page); ClearPageReclaim(page); - pr_err_ratelimited("Write error on dio swapfile (%llu)\n", - page_file_offset(page)); + pr_err_ratelimited("Write error (%d) on dio swapfile (%llu)\n", + ret, page_file_offset(page)); } end_page_writeback(page); return ret;