From patchwork Mon Nov 26 09:46:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kirill Tkhai X-Patchwork-Id: 10697911 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 17CFF13BB for ; Mon, 26 Nov 2018 09:46:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 05AA82979D for ; Mon, 26 Nov 2018 09:46:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EE1F92979F; Mon, 26 Nov 2018 09:46:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI,SUBJ_OBFU_PUNCT_MANY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 36F232979D for ; Mon, 26 Nov 2018 09:46:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726372AbeKZUju (ORCPT ); Mon, 26 Nov 2018 15:39:50 -0500 Received: from relay.sw.ru ([185.231.240.75]:49788 "EHLO relay.sw.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726203AbeKZUju (ORCPT ); Mon, 26 Nov 2018 15:39:50 -0500 Received: from [172.16.25.169] (helo=localhost.localdomain) by relay.sw.ru with esmtp (Exim 4.91) (envelope-from ) id 1gRDT2-0007Bu-DM; Mon, 26 Nov 2018 12:46:12 +0300 Subject: [PATCH 1/2] fuse: Fix race in fuse_writepage_in_flight() From: Kirill Tkhai To: miklos@szeredi.hu, ktkhai@virtuozzo.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Date: Mon, 26 Nov 2018 12:46:10 +0300 Message-ID: <154322517208.18737.3297786654135648324.stgit@localhost.localdomain> User-Agent: StGit/0.18 MIME-Version: 1.0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Checking for FR_PENDING in fuse_writepage_in_flight() is racy. It does not guarantee the first request in misc.write.next list is not in userspace, since there we take fc->lock, while fuse_dev_do_read() takes fiq->waitq.lock: fuse_dev_read() fuse_writepage_in_flight() test_bit(FR_PENDING) clear_bit(FR_PENDING) handle old_req->pages[0] in userspace copy_highpage(old_req->pages[0], page) ^^^^^ userspace never sees this pages The only reliable way to determ, whether we are able to replace old_req's page, is to completely skip the first request in the list. This patch makes the function to do that. Signed-off-by: Kirill Tkhai --- fs/fuse/file.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/fuse/file.c b/fs/fuse/file.c index b52f9baaa3e7..c6650c68b31a 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -1740,6 +1740,7 @@ static bool fuse_writepage_in_flight(struct fuse_req *new_req, { struct fuse_conn *fc = get_fuse_conn(new_req->inode); struct fuse_inode *fi = get_fuse_inode(new_req->inode); + struct fuse_req *first_req; struct fuse_req *tmp; struct fuse_req *old_req; bool found = false; @@ -1764,6 +1765,7 @@ static bool fuse_writepage_in_flight(struct fuse_req *new_req, } new_req->num_pages = 1; + first_req = old_req; for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) { BUG_ON(tmp->inode != new_req->inode); curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT; @@ -1773,7 +1775,7 @@ static bool fuse_writepage_in_flight(struct fuse_req *new_req, } } - if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) { + if (old_req->num_pages == 1 && old_req != first_req) { struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host); copy_highpage(old_req->pages[0], page); From patchwork Mon Nov 26 09:46:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kirill Tkhai X-Patchwork-Id: 10697913 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0DD355A4 for ; Mon, 26 Nov 2018 09:46:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F17332979D for ; Mon, 26 Nov 2018 09:46:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E5CF42979F; Mon, 26 Nov 2018 09:46:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A223C2979D for ; Mon, 26 Nov 2018 09:46:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726441AbeKZUj5 (ORCPT ); Mon, 26 Nov 2018 15:39:57 -0500 Received: from relay.sw.ru ([185.231.240.75]:49798 "EHLO relay.sw.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726203AbeKZUj4 (ORCPT ); Mon, 26 Nov 2018 15:39:56 -0500 Received: from [172.16.25.169] (helo=localhost.localdomain) by relay.sw.ru with esmtp (Exim 4.91) (envelope-from ) id 1gRDTA-0007C0-Rn; Mon, 26 Nov 2018 12:46:21 +0300 Subject: [PATCH 2/2] fuse: Replace page without copying in fuse_writepage_in_flight() From: Kirill Tkhai To: miklos@szeredi.hu, ktkhai@virtuozzo.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Date: Mon, 26 Nov 2018 12:46:20 +0300 Message-ID: <154322557765.18737.14337090699283695815.stgit@localhost.localdomain> In-Reply-To: <154322517208.18737.3297786654135648324.stgit@localhost.localdomain> References: <154322517208.18737.3297786654135648324.stgit@localhost.localdomain> User-Agent: StGit/0.18 MIME-Version: 1.0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP It looks like we can optimize old_req page replacement and avoid copying by simple updating the request's page. Signed-off-by: Kirill Tkhai --- fs/fuse/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/fuse/file.c b/fs/fuse/file.c index c6650c68b31a..83b54b082c86 100644 --- a/fs/fuse/file.c +++ b/fs/fuse/file.c @@ -1778,7 +1778,7 @@ static bool fuse_writepage_in_flight(struct fuse_req *new_req, if (old_req->num_pages == 1 && old_req != first_req) { struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host); - copy_highpage(old_req->pages[0], page); + swap(old_req->pages[0], page); spin_unlock(&fc->lock); dec_wb_stat(&bdi->wb, WB_WRITEBACK);