From patchwork Fri Nov 30 19:58:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 10707087 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 609D01057 for ; Fri, 30 Nov 2018 19:58:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 541A430453 for ; Fri, 30 Nov 2018 19:58:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4869C304F5; Fri, 30 Nov 2018 19:58:28 +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=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C205B30453 for ; Fri, 30 Nov 2018 19:58:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 98F796B59F2; Fri, 30 Nov 2018 14:58:22 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 914CB6B59F3; Fri, 30 Nov 2018 14:58:22 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 78D816B59F4; Fri, 30 Nov 2018 14:58:22 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-yw1-f70.google.com (mail-yw1-f70.google.com [209.85.161.70]) by kanga.kvack.org (Postfix) with ESMTP id 48D406B59F2 for ; Fri, 30 Nov 2018 14:58:22 -0500 (EST) Received: by mail-yw1-f70.google.com with SMTP id t17so4380254ywc.23 for ; Fri, 30 Nov 2018 11:58:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:subject:date:message-id :in-reply-to:references; bh=ZiSWalXRo2lAHNoxFcnCBmYqM80BUgBK6TZ//qlBoW0=; b=kzxMiKV6o/I7QrSoqo1GOvFCOpV6DhplE91iSB1Z8yjXxwFXR5v9uRDpeGXXu+vXO0 hlqWKZu4KnPm0B+7CfFysvaF4672vSrL8RR+RS4WnQ6L/QrkBGmjHR/W99jmkCkvTjPK IrsPybisP+YRRrWA5QHDbFnu+lLufZhrQUW/BND/MACu0fLlKFQqUgdY5kDym3X2nq21 wHxuk75FQdeeqyjEFeQLX713hL3mmUyrMYQZgpG4qfBhjtixEcQAw9IzeypwHllTS3ZG xpgZmOlaPPmGVnjTusedIytCS6NaeQ8r/F0tGHArZUyonq9CIlwlQDKOCMu9kN6S1dBB rBIA== X-Gm-Message-State: AA+aEWavqivjJ3ezvZ4CPsyM+sl/QrXTAGI6hGAW2WWR0zOc7jyzU/c9 XKP2e5Om6FZG78SlSiAu0jb8AeIqCek8/6II1lOCebL23GDk+jdJZjq5dWkzyF1VyhGpYSdhedh X23mQ5xyVxHMRwd+VeHRMnlvMt5G5/ktqJJ/IzYU8n29oytGEhrgOmdS64L83emUoLLMNVgaxff hFaOqJ6A6y5R3GKPB67E+WxlfKUtD0BfyWTjrFBUdUU1KQz2dWT0k/+BcLvo5kAyy7B7QJO6Iil Gap6kdyPJhD/xtssA/tNpjxCJdZfiJTZVc6HiImyjMCRzBHQpM6KOq71Rbm4ljGfSaa7FOLyneZ TXT0xvyWj5l7a7HUv6AtLLEb3xxsAK3TPrnGj5w7D9oSTIoYkE+F6T3nKiQDERJyj2SLx4v6rqq A X-Received: by 2002:a25:af0a:: with SMTP id a10-v6mr6613556ybh.279.1543607902037; Fri, 30 Nov 2018 11:58:22 -0800 (PST) X-Received: by 2002:a25:af0a:: with SMTP id a10-v6mr6613527ybh.279.1543607901168; Fri, 30 Nov 2018 11:58:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543607901; cv=none; d=google.com; s=arc-20160816; b=wPVAl7WkYlYGa2kugXQJWadry8W1EPue0cQzJRCinuHbrhlld25u06WuX/tgLbCQyZ +m7vdN7xn4Jp6vFM8tRafYo05KbY+XW0LGsK+Ehx7JMvph42xq81SVM9qKStxiJA3LFt Ro7SKuGPk1B3g7iSummoVpzXc3XMbgFd93Prl7v1+NLvkkt6J9+N/FyNmuNlzoih500Q gCJzuPwTkUobsuSlR0W4vF25zv3dg4NA8PFY47by5TPMB97lZl5rOqSKawI/Pzv8qhWw 5PA9OBgmTtMZmU4pOUupKXuVgJAaMKKS41rEWjOc1/JCyrxBc2RRYP60vdn88MOXhEnA 0Png== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=ZiSWalXRo2lAHNoxFcnCBmYqM80BUgBK6TZ//qlBoW0=; b=S5m2pDGcb6LGaDAHZlxvUmRJaCILnHvF8z/vTs+UINq0MR5m6PdGpFkQ0IwnszzRE2 krzDdFpSTAuCuiKdo0cLmMqr0pD8Yj32rVL8IIR1IlYueUlv6/SnkcWaQo6mgH4swczB gqwyIt466eexSJEdF11I6MpsG4s/pjTHcGqgcaG0vDHFH40EW+alKSP+PJK/iMQGb1yw JU86W7Mi30Ts+yX5Cc71aXy2uUR2SERPZi48nWh8SDBVH+57jBLEUeRAEUR8gj/A2N/f 8OLX0FD7kcdLaMvXWGHedJ/4lt6d53KpTQpSY3UeorDCOSTd4sCtOMNrh2Tntms9Kgzx GKdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@toxicpanda-com.20150623.gappssmtp.com header.s=20150623 header.b=qE17nE2J; spf=neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of josef@toxicpanda.com) smtp.mailfrom=josef@toxicpanda.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id u186-v6sor2671556ybf.135.2018.11.30.11.58.21 for (Google Transport Security); Fri, 30 Nov 2018 11:58:21 -0800 (PST) Received-SPF: neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of josef@toxicpanda.com) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@toxicpanda-com.20150623.gappssmtp.com header.s=20150623 header.b=qE17nE2J; spf=neutral (google.com: 209.85.220.65 is neither permitted nor denied by best guess record for domain of josef@toxicpanda.com) smtp.mailfrom=josef@toxicpanda.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references; bh=ZiSWalXRo2lAHNoxFcnCBmYqM80BUgBK6TZ//qlBoW0=; b=qE17nE2Jo3x4abm13SJxAAIFGmnZyoF8C/6nu8aRpvS4rDGOAT+NB7nFb5qR/q9r0W wyEq3cqlr+cDklN/eobicAnmyPZ5O00B6CLcEWwkMfbaQv2f1e/zarf+qIW/wPERWt9z QGzxh3p+tv3y8zML5KreAJ0J0fdK1gQiiTp1jNjrMW9vgTdx5CDjGysHbm7NtzHtqj2z G48SlfhBIGMBXljirIlfet56WObqccekan4prBch/7ywFpwIc9gEWxYUBnEKPj4xUmnP p7oYI1XwYAfhLaNWOYg0r74CjigClE0qUNLmKqCBjSEdRHgY4p9ac7AqgtmRfyHOfhUM 7DqA== X-Google-Smtp-Source: AFSGD/WUySeblOm5lO6iEZroC7YfnS5osaYyKXSy7/i64tvWyXdByqa0y4VKPk5GYCaEeI3VijK3IQ== X-Received: by 2002:a5b:b09:: with SMTP id z9-v6mr6591976ybp.483.1543607900825; Fri, 30 Nov 2018 11:58:20 -0800 (PST) Received: from localhost ([107.15.81.208]) by smtp.gmail.com with ESMTPSA id z74sm3209536ywz.51.2018.11.30.11.58.19 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 30 Nov 2018 11:58:20 -0800 (PST) From: Josef Bacik To: kernel-team@fb.com, hannes@cmpxchg.org, linux-kernel@vger.kernel.org, tj@kernel.org, david@fromorbit.com, akpm@linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, riel@redhat.com, jack@suse.cz Subject: [PATCH 4/4] mm: use the cached page for filemap_fault Date: Fri, 30 Nov 2018 14:58:12 -0500 Message-Id: <20181130195812.19536-5-josef@toxicpanda.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20181130195812.19536-1-josef@toxicpanda.com> References: <20181130195812.19536-1-josef@toxicpanda.com> 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: X-Virus-Scanned: ClamAV using ClamSMTP If we drop the mmap_sem we have to redo the vma lookup which requires redoing the fault handler. Chances are we will just come back to the same page, so save this page in our vmf->cached_page and reuse it in the next loop through the fault handler. Signed-off-by: Josef Bacik --- mm/filemap.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 5e76b24b2a0f..d4385b704e04 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2392,6 +2392,35 @@ static struct file *do_async_mmap_readahead(struct vm_area_struct *vma, return fpin; } +static int vmf_has_cached_page(struct vm_fault *vmf, struct page **page) +{ + struct page *cached_page = vmf->cached_page; + struct mm_struct *mm = vmf->vma->vm_mm; + struct address_space *mapping = vmf->vma->vm_file->f_mapping; + pgoff_t offset = vmf->pgoff; + + if (!cached_page) + return 0; + + if (vmf->flags & FAULT_FLAG_KILLABLE) { + int ret = lock_page_killable(cached_page); + if (ret) { + up_read(&mm->mmap_sem); + return ret; + } + } else + lock_page(cached_page); + vmf->cached_page = NULL; + if (cached_page->mapping == mapping && + cached_page->index == offset) { + *page = cached_page; + } else { + unlock_page(cached_page); + put_page(cached_page); + } + return 0; +} + /** * filemap_fault - read in file data for page fault handling * @vmf: struct vm_fault containing details of the fault @@ -2425,13 +2454,24 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) struct inode *inode = mapping->host; pgoff_t offset = vmf->pgoff; pgoff_t max_off; - struct page *page; + struct page *page = NULL; vm_fault_t ret = 0; max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); if (unlikely(offset >= max_off)) return VM_FAULT_SIGBUS; + /* + * We may have read in the page already and have a page from an earlier + * loop. If so we need to see if this page is still valid, and if not + * do the whole dance over again. + */ + error = vmf_has_cached_page(vmf, &page); + if (error) + goto out_retry; + if (page) + goto have_cached_page; + /* * Do we have something in the page cache already? */ @@ -2492,6 +2532,7 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) put_page(page); goto retry_find; } +have_cached_page: VM_BUG_ON_PAGE(page->index != offset, page); /* @@ -2558,7 +2599,7 @@ vm_fault_t filemap_fault(struct vm_fault *vmf) * page. */ if (page) - put_page(page); + vmf->cached_page = page; if (fpin) fput(fpin); return ret | VM_FAULT_RETRY;