From patchwork Tue Nov 13 05:49:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 10679565 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 466C413BB for ; Tue, 13 Nov 2018 05:50:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 35DEE2A2CD for ; Tue, 13 Nov 2018 05:50:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2A2892A276; Tue, 13 Nov 2018 05:50:43 +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 6BEA72A19B for ; Tue, 13 Nov 2018 05:50:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6632D6B0010; Tue, 13 Nov 2018 00:50:41 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 613316B0266; Tue, 13 Nov 2018 00:50:41 -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 52A606B0269; Tue, 13 Nov 2018 00:50:41 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) by kanga.kvack.org (Postfix) with ESMTP id 116E06B0010 for ; Tue, 13 Nov 2018 00:50:41 -0500 (EST) Received: by mail-pf1-f200.google.com with SMTP id l15-v6so9570870pff.5 for ; Mon, 12 Nov 2018 21:50: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=lnd7r9boe2fj9KO1gDE/0zibZIbMLjFGfeIFh5zAW8g=; b=lCwptUCyVOwNN9fD5pvXv0gVNJdPk9InQb46fl/CFrW2GcBA8NcbTEYXismcO/yGN3 /UAp0W2R4NwKwA1GRdqf7Le7LjmHDsfTDstap35PxNv+3+tytM0Y0Bo3je0w+a7HcePS vLa3OJ0ngDvmeRYGrXhGtFLCDP7pvDRAYsviWgGPR9ZtjTw1kU4ZUE/TX3miNoNoJwC6 FhhigLG7MgQL7nT6t79x2xXzdFX+WRtIqRrlPPBDvpMi/8MbJe5vA5Fo43EzeZKwQged kSoHQ9yHw/gn70aRy+8Ebx/hoYUFNxSfM3DQ6J1U3MhD5gfAoouJ1w79hxpMaAcQcs1y U6PQ== X-Gm-Message-State: AGRZ1gKQqqhhxwpiRIfeSFxbei3KEYfnhDPlXobI02UiTqdTY2gmdfR4 lS7E3CIsQPYYGlvdBXdfRAhHsoyN9EFxKdqYLWjtUuY0dxxgQvU64J72EZalL9/cjcRAknVwg57 BhRQhbHX709GL8bOL2QAy1+LiC+MpplORqCgK7o5yLs5ZbTVQah7j0xuYYS8suYgPxA== X-Received: by 2002:a65:55ca:: with SMTP id k10mr3485075pgs.448.1542088240725; Mon, 12 Nov 2018 21:50:40 -0800 (PST) X-Google-Smtp-Source: AJdET5fsaT5Y5XqmDH92V2dLPEejW6/O7TRDxac5DemtR0bWwQdfOXrPbfc1pUzqaXJeupuzv42E X-Received: by 2002:a65:55ca:: with SMTP id k10mr3485033pgs.448.1542088239362; Mon, 12 Nov 2018 21:50:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542088239; cv=none; d=google.com; s=arc-20160816; b=aBT8IwJ3c0X9zhD75ja6Ki2tuB/ZIQHkZCeZoEC7CdRCIgdX6zyB75tjTHDHlB/MdP Zdrii45mdc9Gi9Yh31b4hOZfakOHRu0fWoo10AOVfCDbcjtVyTD714SycnrADvd0bFMX 6YQUfi2UJ3Dp95oQWFRdbTaw6C7eONr9aR7FldSmChp4ORB3Wv4n0jAuqnnW5hJ/Tpd7 a84NXRnQQFZa00fL6cZ4s64oXVnVrvpssibTyp8k+y3HSFhrvAs5Lh7tBD5DpE811Z0g n671oEL0E171wj0e4f+N6SDMv86B6mEPOWM9O3+CCRL6pDwkWaW875I5dY+2sfTnoHPq 4G3w== 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=lnd7r9boe2fj9KO1gDE/0zibZIbMLjFGfeIFh5zAW8g=; b=oV6UmytAY0z0OTwrQtZok12RtVUHWCxDfPccmODtZa0yIAfDaUGmsQfB5F2pNPq5Yi KO7kvlwt/OX/KIDoi5wbpvTs4dUBnUX907SjgCiWvfY99S5BhxqW8G2g0Ev3gHkm80WU YiIAcwnJv9iksh6m3VKgNYwnVBqI6YdbKvt0AyOMgjuolUyEGcJRTQ9uWsuTU0rvHJfz oolQqx7S1W7E8mHq4CZQBGoSHWqJX2zsf6+F0JdLHENbBpkPPODrawX26I4SsoFKwRef rzK89mWoxsINf7ekSUi7sgfednzGceA0h+kQ1iYO+JW4KXBCkuUYSPxut6x5TN7mu7Hn 3Imw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=QDbZRHQC; 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 b18si20041795pgj.399.2018.11.12.21.50.39 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Nov 2018 21:50:39 -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=QDbZRHQC; 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 E9D2F22513; Tue, 13 Nov 2018 05:50:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1542088239; bh=6mLMxvWq1b5Wgb1O0XaUhDi1D4EP82leXGTlxVcdTvQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QDbZRHQC2QDEItdXIHN2c+j/94iJx1z2bliP+SqG6WFjcCMdB7DMC1JC2uq2di6Dt NT9gvR0JmwhIPjkFqXDN+y5dLFu74TKAQtuiPkncPAKdPex6qPnTPh+VBRNRYjWU8B 9jWvQbPr4zEB+J8pP2su6mNnY0fl8sxU+d9wc9rU= 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.19 37/44] mm: thp: fix MADV_DONTNEED vs migrate_misplaced_transhuge_page race condition Date: Tue, 13 Nov 2018 00:49:43 -0500 Message-Id: <20181113054950.77898-37-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181113054950.77898-1-sashal@kernel.org> References: <20181113054950.77898-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 d7c3393413fe7e7dc54498ea200ea94742d61e18 ] Patch series "migrate_misplaced_transhuge_page race conditions". Aaron found a new instance of the THP MADV_DONTNEED race against pmdp_clear_flush* variants, that was apparently left unfixed. While looking into the race found by Aaron, I may have found two more issues in migrate_misplaced_transhuge_page. These race conditions would not cause kernel instability, but they'd corrupt userland data or leave data non zero after MADV_DONTNEED. I did only minor testing, and I don't expect to be able to reproduce this (especially the lack of ->invalidate_range before migrate_page_copy, requires the latest iommu hardware or infiniband to reproduce). The last patch is noop for x86 and it needs further review from maintainers of archs that implement flush_cache_range() (not in CC yet). To avoid confusion, it's not the first patch that introduces the bug fixed in the second patch, even before removing the pmdp_huge_clear_flush_notify, that _notify suffix was called after migrate_page_copy already run. This patch (of 3): This is a corollary of ced108037c2aa ("thp: fix MADV_DONTNEED vs. numa balancing race"), 58ceeb6bec8 ("thp: fix MADV_DONTNEED vs. MADV_FREE race") and 5b7abeae3af8c ("thp: fix MADV_DONTNEED vs clear soft dirty race). When the above three fixes where posted Dave asked https://lkml.kernel.org/r/929b3844-aec2-0111-fef7-8002f9d4e2b9@intel.com but apparently this was missed. The pmdp_clear_flush* in migrate_misplaced_transhuge_page() was introduced in a54a407fbf7 ("mm: Close races between THP migration and PMD numa clearing"). The important part of such commit is only the part where the page lock is not released until the first do_huge_pmd_numa_page() finished disarming the pagenuma/protnone. The addition of pmdp_clear_flush() wasn't beneficial to such commit and there's no commentary about such an addition either. I guess the pmdp_clear_flush() in such commit was added just in case for safety, but it ended up introducing the MADV_DONTNEED race condition found by Aaron. At that point in time nobody thought of such kind of MADV_DONTNEED race conditions yet (they were fixed later) so the code may have looked more robust by adding the pmdp_clear_flush(). This specific race condition won't destabilize the kernel, but it can confuse userland because after MADV_DONTNEED the memory won't be zeroed out. This also optimizes the code and removes a superfluous TLB flush. [akpm@linux-foundation.org: reflow comment to 80 cols, fix grammar and typo (beacuse)] Link: http://lkml.kernel.org/r/20181013002430.698-2-aarcange@redhat.com Signed-off-by: Andrea Arcangeli Reported-by: Aaron Tomlin Acked-by: Mel Gorman Acked-by: Kirill A. Shutemov Cc: Jerome Glisse Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/migrate.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/mm/migrate.c b/mm/migrate.c index 84381b55b2bd..1f634b1563b6 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -2029,15 +2029,26 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm, entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); /* - * Clear the old entry under pagetable lock and establish the new PTE. - * Any parallel GUP will either observe the old page blocking on the - * page lock, block on the page table lock or observe the new page. - * The SetPageUptodate on the new page and page_add_new_anon_rmap - * guarantee the copy is visible before the pagetable update. + * Overwrite the old entry under pagetable lock and establish + * the new PTE. Any parallel GUP will either observe the old + * page blocking on the page lock, block on the page table + * lock or observe the new page. The SetPageUptodate on the + * 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); - pmdp_huge_clear_flush_notify(vma, mmun_start, pmd); + /* + * 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 + * caching this non present pmd mapping. There's no need to clear the + * pmd before doing set_pmd_at(), nor to flush the TLB after + * set_pmd_at(). Clearing the pmd here would introduce a race + * condition against MADV_DONTNEED, because MADV_DONTNEED only holds the + * mmap_sem for reading. If the pmd is set to NULL at any given time, + * MADV_DONTNEED won't wait on the pmd lock and it'll skip clearing this + * pmd. + */ set_pmd_at(mm, mmun_start, pmd, entry); update_mmu_cache_pmd(vma, address, &entry); @@ -2051,7 +2062,7 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm, * 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_only_end(mm, mmun_start, mmun_end); + 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);