Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp1178921imm; Tue, 5 Jun 2018 10:14:10 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIcmLeIXyEkH6snwrjQYdz12ocgcCgN7sL2+Icw3By0YG13RALqXUpc7nI1ke6AEP/TT4E4 X-Received: by 2002:a63:bc0a:: with SMTP id q10-v6mr21645597pge.70.1528218850002; Tue, 05 Jun 2018 10:14:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528218849; cv=none; d=google.com; s=arc-20160816; b=Q7q/yPe6OsD3C35tauoQsdoSGDCtocX2+NIAhoQmGEiscY5dm1NKm3a1lTnpMlPiP0 7RuDSvYns9VWrMuEGF2AiZ0a6otpWV4tAJ04BrSmH7NISNjgZr/Xl6LZBPuZtsWt8Zrc QidDkBY/2vG4b8a8Np9R+ugkdpPROrXkgpPDBBwzZqMl50aii/qD0Bvw7n8JUnknCk/W B0sXL9PPZ2fKWXo8CtvAsq0zL11XsNjOUccIG9wiSAJtwue5qiiSQA7Mq7g20mQ4VpJ+ Z0z24Pu5Pg8RR3Id9Rg0Tzr3jAD/A++RPe5sT8UQi5pYWJYSVZprvRp4LXTTx6NEq6or ExTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date :arc-authentication-results; bh=vUKZVy/AcPhBF7nZrBGonORJgkQbkIwlmHD2jA5CA7E=; b=tZ70bb+DI30MB9R9NXHj6q3oqWILFtPq36NL6Jm5aFmWeVAfsXK19xnDvryqxMtZZn rfzxtwOw3YW4/ubICz9diy3ITFFH945o2rY9BCKP6drColZJonvE7Sm2uMDsZJAKn+9j ffsPTnDjh+CRNjOZTjDjoWPAnwg3k7cSfRqdWSjJd4GG7wX1RqW9T3XRHYqHZ9Ak24ZF T0Vxk4xqh6+HNeFocoOkfTYXs4URBDgSnPhEUxUOYYKAZNHicvde8IFuTVvTxVZYYnsm noIV0nftHLTu5nldhQ6iwxn/Y0FYgEtbhMEcVhBplDam2c/fu7AS1hJq0m2ea5wf6Xtv +21Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h192-v6si23743839pgc.540.2018.06.05.10.13.55; Tue, 05 Jun 2018 10:14:09 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753854AbeFERN1 (ORCPT + 99 others); Tue, 5 Jun 2018 13:13:27 -0400 Received: from outbound-smtp04.blacknight.com ([81.17.249.35]:41881 "EHLO outbound-smtp04.blacknight.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753762AbeFERNZ (ORCPT ); Tue, 5 Jun 2018 13:13:25 -0400 Received: from mail.blacknight.com (pemlinmail03.blacknight.ie [81.17.254.16]) by outbound-smtp04.blacknight.com (Postfix) with ESMTPS id B6C1D9891A for ; Tue, 5 Jun 2018 17:13:23 +0000 (UTC) Received: (qmail 759 invoked from network); 5 Jun 2018 17:13:23 -0000 Received: from unknown (HELO techsingularity.net) (mgorman@techsingularity.net@[37.228.237.73]) by 81.17.254.9 with ESMTPSA (DHE-RSA-AES256-SHA encrypted, authenticated); 5 Jun 2018 17:13:23 -0000 Date: Tue, 5 Jun 2018 18:13:19 +0100 From: Mel Gorman To: Andrew Morton Cc: mhocko@kernel.org, vbabka@suse.cz, Aaron Lu , Dave Hansen , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH] mremap: Avoid TLB flushing anonymous pages that are not in swap cache Message-ID: <20180605171319.uc5jxdkxopio6kg3@techsingularity.net> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Disposition: inline User-Agent: NeoMutt/20170912 (1.9.0) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit 5d1904204c99 ("mremap: fix race between mremap() and page cleanning") fixed races between mremap and other operations for both file-backed and anonymous mappings. The file-backed was the most critical as it allowed the possibility that data could be changed on a physical page after page_mkclean returned which could trigger data loss or data integrity issues. A customer reported that the cost of the TLBs for anonymous regressions was excessive and resulting in a 30-50% drop in performance overall since this commit on a microbenchmark. Unfortunately I neither have access to the test-case nor can I describe what it does other than saying that mremap operations dominate heavily. The anonymous page race fix is overkill for two reasons. Pages that are not in the swap cache are not going to be issued for IO and if a stale TLB entry is used, the write still occurs on the same physical page. Any race with mmap replacing the address space is handled by mmap_sem. As anonymous pages are often dirty, it can mean that mremap always has to flush even when it is not necessary. This patch special cases anonymous pages to only flush if the page is in swap cache and can be potentially queued for IO. It uses the page lock to serialise against any potential reclaim. If the page is added to the swap cache on the reclaim side after the page lock is dropped on the mremap side then reclaim will call try_to_unmap_flush_dirty() before issuing any IO so there is no data integrity issue. This means that in the common case where a workload avoids swap entirely that mremap is a much cheaper operation due to the lack of TLB flushes. Using another testcase that simply calls mremap heavily with varying number of threads, it was found that very broadly speaking that TLB shootdowns were reduced by 31% on average throughout the entire test case but your milage will vary. Signed-off-by: Mel Gorman --- mm/mremap.c | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/mm/mremap.c b/mm/mremap.c index 049470aa1e3e..d26c5a00fd9d 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -112,6 +113,41 @@ static pte_t move_soft_dirty_pte(pte_t pte) return pte; } +/* Returns true if a TLB must be flushed before PTL is dropped */ +static bool should_force_flush(pte_t *pte) +{ + bool is_swapcache; + struct page *page; + + if (!pte_present(*pte) || !pte_dirty(*pte)) + return false; + + /* + * If we are remapping a dirty file PTE, make sure to flush TLB + * before we drop the PTL for the old PTE or we may race with + * page_mkclean(). + */ + page = pte_page(*pte); + if (page_is_file_cache(page)) + return true; + + /* + * For anonymous pages, only flush swap cache pages that could + * be unmapped and queued for swap since flush_tlb_batched_pending was + * last called. Reclaim itself takes care that the TLB is flushed + * before IO is queued. If a page is not in swap cache and a stale TLB + * is used before mremap is complete then the write hits the same + * physical page and there is no lost data loss. Check under the + * page lock to avoid any potential race with reclaim. + */ + if (!trylock_page(page)) + return true; + is_swapcache = PageSwapCache(page); + unlock_page(page); + + return is_swapcache; +} + static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd, unsigned long old_addr, unsigned long old_end, struct vm_area_struct *new_vma, pmd_t *new_pmd, @@ -163,15 +199,11 @@ static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd, pte = ptep_get_and_clear(mm, old_addr, old_pte); /* - * If we are remapping a dirty PTE, make sure - * to flush TLB before we drop the PTL for the - * old PTE or we may race with page_mkclean(). - * * This check has to be done after we removed the * old PTE from page tables or another thread may * dirty it after the check and before the removal. */ - if (pte_present(pte) && pte_dirty(pte)) + if (should_force_flush(&pte)) force_flush = true; pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr); pte = move_soft_dirty_pte(pte);