From patchwork Tue Nov 13 05:50:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 10679595 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 9576F13B5 for ; Tue, 13 Nov 2018 05:51:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 83ACE2A2B7 for ; Tue, 13 Nov 2018 05:51:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 81F3F2A33F; Tue, 13 Nov 2018 05:51:45 +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=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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 ACB422A2B7 for ; Tue, 13 Nov 2018 05:51:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 52F2A6B027D; Tue, 13 Nov 2018 00:51:42 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4B3846B027E; Tue, 13 Nov 2018 00:51:42 -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 2E0906B027F; Tue, 13 Nov 2018 00:51:42 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) by kanga.kvack.org (Postfix) with ESMTP id D56E06B027D for ; Tue, 13 Nov 2018 00:51:41 -0500 (EST) Received: by mail-pl1-f197.google.com with SMTP id 34-v6so8775761plf.6 for ; Mon, 12 Nov 2018 21:51:41 -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:cc:subject:date :message-id:in-reply-to:references; bh=9SVzJUwv2InvqBXn2gVLt+voypV4GC2eUO8m8FuGdA8=; b=OmqdxLwyehBhcK3kZgesXyKsHhjhWXmzGuQjSM+N0rMNWQWqgOtfAtPP7dTHrRLD2Q bh/uUYZrmKguSlAoigrvgc+lcqvLS9HS8EvQsdiR1SJpm0AxPissSeckWS7qVwiy3e8w UIpfQTkCXx94Tl1lQ/d/KgQH2CxZ6YK7bSuEgDyFF1VqReFkP79Aa2QXReg6Q7O2j2Tl 1G4ho2E4IWljJX164kq1fz4fReH6uosr9tNLvgUQQs66H3cVEFuryPWrWKBCmVtdZ/D4 93P80Z8fjWEA7bRSbJUoXBEf8R90i5PqB0KqDpZQxi/bvwQTzZE3cynDnDu/qgZRATK3 UzNA== X-Gm-Message-State: AGRZ1gIhL0QEUxMLR5Ss8j53qrV7AOZcKH1aH0X8+woQknXktZSOzuey /TaVki5z+ugvf/HI1qaT/4OaLDa54Dp8LzNiavnTEEjnpwMaBMIrH4dz3ObvmEDJa5aUXh40JX6 IIiwKpiDGbTlUV79NobR/GptSzWmxL0TJrgvaizitV5G9tnvmVWXv7qwmSlWAUaGvHA== X-Received: by 2002:a63:e545:: with SMTP id z5mr3548341pgj.195.1542088301507; Mon, 12 Nov 2018 21:51:41 -0800 (PST) X-Google-Smtp-Source: AJdET5da0EcvNGyraEyJlqr07ziVd8jm90zGoyYakaNVZCxAhkoQV95JxpF9H3VagDJSD3UGF5P5 X-Received: by 2002:a63:e545:: with SMTP id z5mr3548303pgj.195.1542088300369; Mon, 12 Nov 2018 21:51:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542088300; cv=none; d=google.com; s=arc-20160816; b=hCIiFRc+g7Sz6LssIuNvLvWhVDO4aCW/70AFTSUOyk6g3vmdXoTwTqfkne6oG+vAM5 ctzZKG80j0Ys6qNRo7mO5RwVLNLhyKQWfTkJWtKDN72JoNW+45BLpkPlP+qQbMYAoUNf miLGKISZY/rKQvR2+7Kw/tDQehb/DmR6LbK7WHt1nWm7KFxmv7FZxfSq5mhMLIIAMtIS 5uY9psnB1aew1ovCVxNdEWYIVftIiYpQJUsKo60d91LzmW4qOZL0NW9f0UCGIXc4kY8q +xYq4IN/5ObDz6s5Kd9ulB+lOPpAJVjhPKV6nljyHYLTnf++4yxwbehJ1Ik8VqibsbkY BOkA== 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:cc:to:from :dkim-signature; bh=9SVzJUwv2InvqBXn2gVLt+voypV4GC2eUO8m8FuGdA8=; b=gN5tgZWKRqSuj1bqwL6scmTkLX7FMrOKuQdOoQuI9t5PyaFPPQwQ4v6vZ3DH5Z7FWf u+g2s1iYGiSpK+XSMA2w40732Y7Kua4J9vWwkhJOJ6c2+S3+Y1b0K7pjU8cqQauugewu 7d8Bo6I982NUsKDwhkcJf/ASsY/ELVCN2LrOZbhGiSP+xAqaLp+Ify6UE60dcn6seg7L vEhihEGsPpRoLHIyDkn344VSa/ZjCJ3uqtwUrIt03ody5NmvE/dGZE1SrurIPyAieFCb U9IMJfXHKDvbJPjdcbNkI79c5J5Ssb3AeXhYZh/uGnRrmrVCOfG96mklYMMWP2gs2cmC OR4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2gp4l0aD; spf=pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=sashal@kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from mail.kernel.org (mail.kernel.org. [198.145.29.99]) by mx.google.com with ESMTPS id h80-v6si17091133pfj.112.2018.11.12.21.51.40 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Nov 2018 21:51:40 -0800 (PST) Received-SPF: pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) client-ip=198.145.29.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2gp4l0aD; spf=pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=sashal@kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sasha-vm.mshome.net (unknown [64.114.255.114]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id EA8A42251C; Tue, 13 Nov 2018 05:51:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1542088300; bh=mKnrCYdD2BT619GhtKJguAHMUTkzdAjXuz7ndxIz5CQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2gp4l0aD/oUvqhD6CxdhV33rWILMENHK5Hk85a291glk0AAAcHCK7DQGDxVkEaKC2 csOBsukQSrVXQ0NpBfOaKRtDkPRxLY9Lv+8j4hvOIBNvEinV1rnGh/HmOv8zazb34+ bHyp+1z9Z/yhM98PcTJr6Nj1E80MmG2lrIOicg58= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Andrea Arcangeli , Jerome Glisse , Andrew Morton , Linus Torvalds , Sasha Levin , linux-mm@kvack.org Subject: [PATCH AUTOSEL 4.18 34/39] mm: thp: fix mmu_notifier in migrate_misplaced_transhuge_page() Date: Tue, 13 Nov 2018 00:50:48 -0500 Message-Id: <20181113055053.78352-34-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181113055053.78352-1-sashal@kernel.org> References: <20181113055053.78352-1-sashal@kernel.org> 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 From: Andrea Arcangeli [ Upstream commit 7066f0f933a1fd707bb38781866657769cff7efc ] change_huge_pmd() after arming the numa/protnone pmd doesn't flush the TLB right away. do_huge_pmd_numa_page() flushes the TLB before calling migrate_misplaced_transhuge_page(). By the time do_huge_pmd_numa_page() runs some CPU could still access the page through the TLB. change_huge_pmd() before arming the numa/protnone transhuge pmd calls mmu_notifier_invalidate_range_start(). So there's no need of mmu_notifier_invalidate_range_start()/mmu_notifier_invalidate_range_only_end() sequence in migrate_misplaced_transhuge_page() too, because by the time migrate_misplaced_transhuge_page() runs, the pmd mapping has already been invalidated in the secondary MMUs. It has to or if a secondary MMU can still write to the page, the migrate_page_copy() would lose data. However an explicit mmu_notifier_invalidate_range() is needed before migrate_misplaced_transhuge_page() starts copying the data of the transhuge page or the below can happen for MMU notifier users sharing the primary MMU pagetables and only implementing ->invalidate_range: CPU0 CPU1 GPU sharing linux pagetables using only ->invalidate_range ----------- ------------ --------- GPU secondary MMU writes to the page mapped by the transhuge pmd change_pmd_range() mmu..._range_start() ->invalidate_range_start() noop change_huge_pmd() set_pmd_at(numa/protnone) pmd_unlock() do_huge_pmd_numa_page() CPU TLB flush globally (1) CPU cannot write to page migrate_misplaced_transhuge_page() GPU writes to the page... migrate_page_copy() ...GPU stops writing to the page CPU TLB flush (2) mmu..._range_end() (3) ->invalidate_range_stop() noop ->invalidate_range() GPU secondary MMU is invalidated and cannot write to the page anymore (too late) Just like we need a CPU TLB flush (1) because the TLB flush (2) arrives too late, we also need a mmu_notifier_invalidate_range() before calling migrate_misplaced_transhuge_page(), because the ->invalidate_range() in (3) also arrives too late. This requirement is the result of the lazy optimization in change_huge_pmd() that releases the pmd_lock without first flushing the TLB and without first calling mmu_notifier_invalidate_range(). Even converting the removed mmu_notifier_invalidate_range_only_end() into a mmu_notifier_invalidate_range_end() would not have been enough to fix this, because it run after migrate_page_copy(). After the hugepage data copy is done migrate_misplaced_transhuge_page() can proceed and call set_pmd_at without having to flush the TLB nor any secondary MMUs because the secondary MMU invalidate, just like the CPU TLB flush, has to happen before the migrate_page_copy() is called or it would be a bug in the first place (and it was for drivers using ->invalidate_range()). KVM is unaffected because it doesn't implement ->invalidate_range(). The standard PAGE_SIZEd migrate_misplaced_page is less accelerated and uses the generic migrate_pages which transitions the pte from numa/protnone to a migration entry in try_to_unmap_one() and flushes TLBs and all mmu notifiers there before copying the page. Link: http://lkml.kernel.org/r/20181013002430.698-3-aarcange@redhat.com Signed-off-by: Andrea Arcangeli Acked-by: Mel Gorman Acked-by: Kirill A. Shutemov Reviewed-by: Aaron Tomlin Cc: Jerome Glisse Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/huge_memory.c | 14 +++++++++++++- mm/migrate.c | 19 ++++++------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 3238bb2d0c93..d8d46936e39e 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1560,8 +1560,20 @@ int do_huge_pmd_numa_page(struct vm_fault *vmf, pmd_t pmd) * We are not sure a pending tlb flush here is for a huge page * mapping or not. Hence use the tlb range variant */ - if (mm_tlb_flush_pending(vma->vm_mm)) + if (mm_tlb_flush_pending(vma->vm_mm)) { flush_tlb_range(vma, haddr, haddr + HPAGE_PMD_SIZE); + /* + * change_huge_pmd() released the pmd lock before + * invalidating the secondary MMUs sharing the primary + * MMU pagetables (with ->invalidate_range()). The + * mmu_notifier_invalidate_range_end() (which + * internally calls ->invalidate_range()) in + * change_pmd_range() will run after us, so we can't + * rely on it here and we need an explicit invalidate. + */ + mmu_notifier_invalidate_range(vma->vm_mm, haddr, + haddr + HPAGE_PMD_SIZE); + } /* * Migrate the THP to the requested node, returns with page unlocked diff --git a/mm/migrate.c b/mm/migrate.c index 38ad6365ed10..1d189fc0d206 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -2022,8 +2022,8 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm, int isolated = 0; struct page *new_page = NULL; int page_lru = page_is_file_cache(page); - unsigned long mmun_start = address & HPAGE_PMD_MASK; - unsigned long mmun_end = mmun_start + HPAGE_PMD_SIZE; + unsigned long start = address & HPAGE_PMD_MASK; + unsigned long end = start + HPAGE_PMD_SIZE; /* * Rate-limit the amount of data that is being migrated to a node. @@ -2058,11 +2058,9 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm, WARN_ON(PageLRU(new_page)); /* Recheck the target PMD */ - mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); ptl = pmd_lock(mm, pmd); if (unlikely(!pmd_same(*pmd, entry) || !page_ref_freeze(page, 2))) { spin_unlock(ptl); - mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); /* Reverse changes made by migrate_page_copy() */ if (TestClearPageActive(new_page)) @@ -2093,8 +2091,8 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm, * new page and page_add_new_anon_rmap guarantee the copy is * visible before the pagetable update. */ - flush_cache_range(vma, mmun_start, mmun_end); - page_add_anon_rmap(new_page, vma, mmun_start, true); + flush_cache_range(vma, start, end); + page_add_anon_rmap(new_page, vma, start, true); /* * At this point the pmd is numa/protnone (i.e. non present) and the TLB * has already been flushed globally. So no TLB can be currently @@ -2106,7 +2104,7 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm, * MADV_DONTNEED won't wait on the pmd lock and it'll skip clearing this * pmd. */ - set_pmd_at(mm, mmun_start, pmd, entry); + set_pmd_at(mm, start, pmd, entry); update_mmu_cache_pmd(vma, address, &entry); page_ref_unfreeze(page, 2); @@ -2115,11 +2113,6 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm, set_page_owner_migrate_reason(new_page, MR_NUMA_MISPLACED); spin_unlock(ptl); - /* - * No need to double call mmu_notifier->invalidate_range() callback as - * the above pmdp_huge_clear_flush_notify() did already call it. - */ - mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); /* Take an "isolate" reference and put new page on the LRU. */ get_page(new_page); @@ -2144,7 +2137,7 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm, ptl = pmd_lock(mm, pmd); if (pmd_same(*pmd, entry)) { entry = pmd_modify(entry, vma->vm_page_prot); - set_pmd_at(mm, mmun_start, pmd, entry); + set_pmd_at(mm, start, pmd, entry); update_mmu_cache_pmd(vma, address, &entry); } spin_unlock(ptl);