From patchwork Tue Oct 27 09:39:45 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chandan Rajendra X-Patchwork-Id: 7495391 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id E2E6BBEEA4 for ; Tue, 27 Oct 2015 09:41:51 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 10C8D20877 for ; Tue, 27 Oct 2015 09:41:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8D23D20881 for ; Tue, 27 Oct 2015 09:41:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932130AbbJ0Jlo (ORCPT ); Tue, 27 Oct 2015 05:41:44 -0400 Received: from e23smtp03.au.ibm.com ([202.81.31.145]:38006 "EHLO e23smtp03.au.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932093AbbJ0Jln (ORCPT ); Tue, 27 Oct 2015 05:41:43 -0400 Received: from localhost by e23smtp03.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 27 Oct 2015 19:41:41 +1000 Received: from d23dlp01.au.ibm.com (202.81.31.203) by e23smtp03.au.ibm.com (202.81.31.209) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 27 Oct 2015 19:41:39 +1000 X-IBM-Helo: d23dlp01.au.ibm.com X-IBM-MailFrom: chandan@linux.vnet.ibm.com X-IBM-RcptTo: linux-btrfs@vger.kernel.org Received: from d23relay07.au.ibm.com (d23relay07.au.ibm.com [9.190.26.37]) by d23dlp01.au.ibm.com (Postfix) with ESMTP id 230522CE8052 for ; Tue, 27 Oct 2015 20:41:39 +1100 (EST) Received: from d23av04.au.ibm.com (d23av04.au.ibm.com [9.190.235.139]) by d23relay07.au.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id t9R9fWl718350204 for ; Tue, 27 Oct 2015 20:41:40 +1100 Received: from d23av04.au.ibm.com (localhost [127.0.0.1]) by d23av04.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id t9R9f63Z019378 for ; Tue, 27 Oct 2015 20:41:06 +1100 Received: from localhost.in.ibm.com ([9.124.35.170]) by d23av04.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id t9R9eWj7017951; Tue, 27 Oct 2015 20:41:04 +1100 From: Chandan Rajendra To: linux-btrfs@vger.kernel.org Cc: Chandan Rajendra , clm@fb.com, jbacik@fb.com, bo.li.liu@oracle.com, dsterba@suse.cz, quwenruo@cn.fujitsu.com, chandan@mykolab.com Subject: [PATCH V7 12/13] Btrfs: prepare_pages: Retry adding a page to the page cache Date: Tue, 27 Oct 2015 15:09:45 +0530 Message-Id: <1445938786-12488-13-git-send-email-chandan@linux.vnet.ibm.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1445938786-12488-1-git-send-email-chandan@linux.vnet.ibm.com> References: <1445938786-12488-1-git-send-email-chandan@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 15102709-0009-0000-0000-00000247EF52 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When reading the page from the disk, we can race with Direct I/O which can get the page lock (before prepare_uptodate_page() gets it) and can go ahead and invalidate the page. Hence if the page is not found in the inode's address space, retry the operation of getting a page. Reported-by: Jakub Palider Reviewed-by: Josef Bacik Signed-off-by: Chandan Rajendra --- fs/btrfs/file.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c index 5a7551c..0e54a53 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c @@ -1316,6 +1316,7 @@ static noinline int prepare_pages(struct inode *inode, struct page **pages, int faili; for (i = 0; i < num_pages; i++) { +again: pages[i] = find_or_create_page(inode->i_mapping, index + i, mask | __GFP_WRITE); if (!pages[i]) { @@ -1330,6 +1331,21 @@ static noinline int prepare_pages(struct inode *inode, struct page **pages, if (i == num_pages - 1) err = prepare_uptodate_page(pages[i], pos + write_bytes, false); + + /* + * When reading the page from the disk, we can race + * with direct i/o which can get the page lock (before + * prepare_uptodate_page() gets it) and can go ahead + * and invalidate the page. Hence if the page is found + * to be not belonging to the inode's address space, + * retry the operation of getting a page. + */ + if (unlikely(pages[i]->mapping != inode->i_mapping)) { + unlock_page(pages[i]); + page_cache_release(pages[i]); + goto again; + } + if (err) { page_cache_release(pages[i]); faili = i - 1;