From patchwork Thu Aug 12 11:57:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 12433411 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2E16BC4338F for ; Thu, 12 Aug 2021 11:57:58 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BEAAA61058 for ; Thu, 12 Aug 2021 11:57:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org BEAAA61058 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 551758D0008; Thu, 12 Aug 2021 07:57:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 501988D0006; Thu, 12 Aug 2021 07:57:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F07E8D0008; Thu, 12 Aug 2021 07:57:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0026.hostedemail.com [216.40.44.26]) by kanga.kvack.org (Postfix) with ESMTP id 242678D0006 for ; Thu, 12 Aug 2021 07:57:57 -0400 (EDT) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id AF88621948 for ; Thu, 12 Aug 2021 11:57:56 +0000 (UTC) X-FDA: 78466279752.25.A65A5A0 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf06.hostedemail.com (Postfix) with ESMTP id 604F9802445D for ; Thu, 12 Aug 2021 11:57:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628769475; 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=DhaQlQ1V661YXit5hgeRnzn374WIc9eKxhLijAq2GWpG3Nw+xFWU2I/eu/8cOQm/XfDm9r 3ELWJHGGjGmbxol2detJ5wgwWPk2yG2v7Htf4zwOE5KuCQlKWefDKfd+xv+lFMjaB4eEtU UHFa0Qy/DEZFcOEfDaKm7AUbP3xkjbc= 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-76-S88aJCw4N3CuIJwDb8IIEw-1; Thu, 12 Aug 2021 07:57:54 -0400 X-MC-Unique: S88aJCw4N3CuIJwDb8IIEw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2A2A0824F83; Thu, 12 Aug 2021 11:57:53 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.22.32.7]) by smtp.corp.redhat.com (Postfix) with ESMTP id C4B8560657; Thu, 12 Aug 2021 11:57:50 +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: [PATCH 1/2] nfs: Fix write to swapfile failure due to generic_write_checks() From: David Howells 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 Date: Thu, 12 Aug 2021 12:57:50 +0100 Message-ID: <162876946993.3068428.11259546375949085551.stgit@warthog.procyon.org.uk> In-Reply-To: <162876946134.3068428.15475611190876694695.stgit@warthog.procyon.org.uk> References: <162876946134.3068428.15475611190876694695.stgit@warthog.procyon.org.uk> User-Agent: StGit/0.23 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=DhaQlQ1V; spf=none (imf06.hostedemail.com: domain of dhowells@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 604F9802445D X-Stat-Signature: tjr87f4k66op4guzacsu6p3u3batwn9k X-HE-Tag: 1628769476-44528 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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; From patchwork Thu Aug 12 11:57:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 12433413 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5F09BC4338F for ; Thu, 12 Aug 2021 11:58:07 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EFA5A6108C for ; Thu, 12 Aug 2021 11:58:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org EFA5A6108C Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 810778D000A; Thu, 12 Aug 2021 07:58:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C03E8D0006; Thu, 12 Aug 2021 07:58:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6885E8D000A; Thu, 12 Aug 2021 07:58:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0061.hostedemail.com [216.40.44.61]) by kanga.kvack.org (Postfix) with ESMTP id 4BDEA8D0006 for ; Thu, 12 Aug 2021 07:58:06 -0400 (EDT) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id F19598249980 for ; Thu, 12 Aug 2021 11:58:05 +0000 (UTC) X-FDA: 78466280130.25.557479B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf30.hostedemail.com (Postfix) with ESMTP id 5F677E008BE9 for ; Thu, 12 Aug 2021 11:58:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628769484; 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=wdKGvZeZhG6ZhsVwcrNbC/I62CcUzp285FqM7Ey5wvA=; b=ZUvgZ/jlV7wOblyyJL9jgMURsY7EyMmwWqpIFN5KxmpYwsN5lJVEw/aOk4APMWLMkfN/A6 3zLUXx0vTEdsDZ13ZVjgrUGU5QlciHDzcSQl8k4LhvdKjMFd8vPBN/5ENsWAjd0iTTzwkN QFGEGWn12eRm4cO0OuToAGDKOYNiE/I= 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-481-oAbktps4N5qq2jOoTy82ag-1; Thu, 12 Aug 2021 07:58:03 -0400 X-MC-Unique: oAbktps4N5qq2jOoTy82ag-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3F6631853035; Thu, 12 Aug 2021 11:58:01 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.22.32.7]) by smtp.corp.redhat.com (Postfix) with ESMTP id 30B8060657; Thu, 12 Aug 2021 11:57:59 +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: [PATCH 2/2] mm: Make swap_readpage() for SWP_FS_OPS use ->direct_IO() not ->readpage() From: David Howells 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 Date: Thu, 12 Aug 2021 12:57:58 +0100 Message-ID: <162876947840.3068428.12591293664586646085.stgit@warthog.procyon.org.uk> In-Reply-To: <162876946134.3068428.15475611190876694695.stgit@warthog.procyon.org.uk> References: <162876946134.3068428.15475611190876694695.stgit@warthog.procyon.org.uk> User-Agent: StGit/0.23 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 5F677E008BE9 X-Stat-Signature: 6bg95wqy9bid7ycd55zk3n6yuwx7gz6u Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="ZUvgZ/jl"; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf30.hostedemail.com: domain of dhowells@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=dhowells@redhat.com X-HE-Tag: 1628769485-55587 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Make swap_readpage(), when accessing a swap file (SWP_FS_OPS) use the ->direct_IO() method on the filesystem rather then ->readpage(). Suggested-by: Matthew Wilcox (Oracle) Signed-off-by: David Howells --- mm/page_io.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/mm/page_io.c b/mm/page_io.c index edb72bf624d2..108f864cea28 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -354,6 +354,72 @@ int __swap_writepage(struct page *page, struct writeback_control *wbc, return 0; } +struct swapfile_kiocb { + struct kiocb iocb; + struct page *page; + refcount_t ki_refcnt; +}; + +static void swapfile_put_kiocb(struct swapfile_kiocb *ki) +{ + if (refcount_dec_and_test(&ki->ki_refcnt)) { + fput(ki->iocb.ki_filp); + kfree(ki); + } +} + +static void swapfile_read_complete(struct kiocb *iocb, long ret, long ret2) +{ + struct swapfile_kiocb *ki = container_of(iocb, struct swapfile_kiocb, iocb); + struct page *page = ki->page; + + if (ret == PAGE_SIZE) { + count_vm_event(PSWPIN); + SetPageUptodate(page); + } else { + pr_err_ratelimited("Read error (%ld) on dio swapfile (%llu)\n", + ret, page_file_offset(page)); + } + + unlock_page(page); + swapfile_put_kiocb(ki); +} + +static int swapfile_read(struct swap_info_struct *sis, struct page *page, + bool synchronous) +{ + struct swapfile_kiocb *ki; + struct file *swap_file = sis->swap_file; + struct bio_vec bv = { + .bv_page = page, + .bv_len = PAGE_SIZE, + .bv_offset = 0 + }; + struct iov_iter to; + int ret; + + ki = kzalloc(sizeof(*ki), GFP_KERNEL); + if (!ki) + return -ENOMEM; + + refcount_set(&ki->ki_refcnt, 2); + init_sync_kiocb(&ki->iocb, swap_file); + ki->page = page; + ki->iocb.ki_flags = IOCB_DIRECT | IOCB_SWAP; + ki->iocb.ki_pos = page_file_offset(page); + ki->iocb.ki_filp = get_file(swap_file); + if (!synchronous) + ki->iocb.ki_complete = swapfile_read_complete; + + iov_iter_bvec(&to, READ, &bv, 1, PAGE_SIZE); + ret = swap_file->f_mapping->a_ops->direct_IO(&ki->iocb, &to); + + if (ret != -EIOCBQUEUED) + swapfile_read_complete(&ki->iocb, ret, 0); + swapfile_put_kiocb(ki); + return (ret > 0) ? 0 : ret; +} + int swap_readpage(struct page *page, bool synchronous) { struct bio *bio; @@ -381,12 +447,7 @@ int swap_readpage(struct page *page, bool synchronous) } if (data_race(sis->flags & SWP_FS_OPS)) { - struct file *swap_file = sis->swap_file; - struct address_space *mapping = swap_file->f_mapping; - - ret = mapping->a_ops->readpage(swap_file, page); - if (!ret) - count_vm_event(PSWPIN); + ret = swapfile_read(sis, page, synchronous); goto out; }