Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp2555730rwb; Mon, 3 Oct 2022 02:29:43 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6HE/0TjtePhusIQciPpkCKO4iEQKw6GDmLW3etOt6QcF9tCbuxxOIUDiO0RZMXbQKMyI5N X-Received: by 2002:a17:907:a052:b0:780:c4e4:1715 with SMTP id gz18-20020a170907a05200b00780c4e41715mr14595811ejc.55.1664789383446; Mon, 03 Oct 2022 02:29:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664789383; cv=none; d=google.com; s=arc-20160816; b=J0hdsL0DTf6a7fRyGtt1r6EOQbPqZZmxaupl92VW34E5WVM7S2CTqanm/JTedbWxgi EC2lDi2r+S7VYj/vVsygStUVTbmwyZdDdIxErpllz/MP5G7KFc0TKCGE4EaA+qSBIfSx H1iai6gzY6nzXzmlBUO60ik0RsVfQbdoKy7MKDi6Fa7Z+YGm4nudSRXZ1ArKC19UQGIG aZmPrh+Ow7rza+s93OJIDTMRYY2yPOtJmfIGUP6E8CpOC75ZMmZiAI2BFhIUcK38beYH cWg4YRlXvEk+dHAXDRojO4XdF0Fk1HAxOnOuhOp8R37xhQszqhdJck9/xXut1/b3AgMc 0pIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=tzrfmSr6f7o4cBSCp3l1J9VPTWqtaMP8juIoSfuLjXg=; b=0UjzteKlkzGgVSJSjpvjqWDt/1ANx+ZsJVDiCaVDs5ttqPbwFiI8/noZuMPJKgQ5li wCdy4BtyAHgC1F2AY/CwkvnedAykjQ7RydGb7V1axwToDggMmz18Pq44/fVa5NWyJjUm SJG4ubpFv8swBA+glAx9WJbQKAq9lt/BYeznv5xEENGu59qkQFabDjelWl46s9tKmpC+ rUdOxsBw4ZImzB/SuNEi+iNLeKnY2/Oqgf4eio6A2dnfYxmeP3T5khYfT+Y+SVaNXq7i 76M1lTkMp1/dKyzOViPDjKO/WfDm9huyhQ+2N+RJeGIah9G6G712LBXVWOfw+mS16OqK RoBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=mTAtjb9s; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nd16-20020a170907629000b0073d6f592b12si8761539ejc.485.2022.10.03.02.29.18; Mon, 03 Oct 2022 02:29:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=mTAtjb9s; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229591AbiJCIWu (ORCPT + 99 others); Mon, 3 Oct 2022 04:22:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230286AbiJCIWM (ORCPT ); Mon, 3 Oct 2022 04:22:12 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AEA3B7B1F8; Mon, 3 Oct 2022 00:56:23 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 04232B8058E; Mon, 3 Oct 2022 07:23:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4DC95C433D6; Mon, 3 Oct 2022 07:23:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1664781792; bh=WPgt52ITCZkfwU+zUs3byRL0FeAokXFGdSIgvMaDlBc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mTAtjb9saeeQ7hWruSRSLmdeVZm+0vxhpBsHprz7dNHyHChI0dBVhdiOJOhloeM7I gOoAl1Qwj0M8bOZojkt8s0rOmRTNiNU2Ox4Au9NFOhznXiY9h79vaJU24wDngDegcS 7ipHgAXZfE6XtLgtvGQfG+m/bS0QcOv8rYu9z36s= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alistair Popple , Nadav Amit , "Huang, Ying" , David Hildenbrand , Peter Xu , Alex Sierra , Ben Skeggs , Felix Kuehling , huang ying , Jason Gunthorpe , John Hubbard , Karol Herbst , Logan Gunthorpe , Lyude Paul , Matthew Wilcox , Paul Mackerras , Ralph Campbell , Andrew Morton Subject: [PATCH 5.4 13/30] mm/migrate_device.c: flush TLB while holding PTL Date: Mon, 3 Oct 2022 09:11:55 +0200 Message-Id: <20221003070716.679782473@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221003070716.269502440@linuxfoundation.org> References: <20221003070716.269502440@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alistair Popple commit 60bae73708963de4a17231077285bd9ff2f41c44 upstream. When clearing a PTE the TLB should be flushed whilst still holding the PTL to avoid a potential race with madvise/munmap/etc. For example consider the following sequence: CPU0 CPU1 ---- ---- migrate_vma_collect_pmd() pte_unmap_unlock() madvise(MADV_DONTNEED) -> zap_pte_range() pte_offset_map_lock() [ PTE not present, TLB not flushed ] pte_unmap_unlock() [ page is still accessible via stale TLB ] flush_tlb_range() In this case the page may still be accessed via the stale TLB entry after madvise returns. Fix this by flushing the TLB while holding the PTL. Fixes: 8c3328f1f36a ("mm/migrate: migrate_vma() unmap page from vma while collecting pages") Link: https://lkml.kernel.org/r/9f801e9d8d830408f2ca27821f606e09aa856899.1662078528.git-series.apopple@nvidia.com Signed-off-by: Alistair Popple Reported-by: Nadav Amit Reviewed-by: "Huang, Ying" Acked-by: David Hildenbrand Acked-by: Peter Xu Cc: Alex Sierra Cc: Ben Skeggs Cc: Felix Kuehling Cc: huang ying Cc: Jason Gunthorpe Cc: John Hubbard Cc: Karol Herbst Cc: Logan Gunthorpe Cc: Lyude Paul Cc: Matthew Wilcox Cc: Paul Mackerras Cc: Ralph Campbell Cc: Signed-off-by: Andrew Morton Signed-off-by: Greg Kroah-Hartman --- mm/migrate.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) --- a/mm/migrate.c +++ b/mm/migrate.c @@ -2343,13 +2343,14 @@ next: migrate->dst[migrate->npages] = 0; migrate->src[migrate->npages++] = mpfn; } - arch_leave_lazy_mmu_mode(); - pte_unmap_unlock(ptep - 1, ptl); /* Only flush the TLB if we actually modified any entries */ if (unmapped) flush_tlb_range(walk->vma, start, end); + arch_leave_lazy_mmu_mode(); + pte_unmap_unlock(ptep - 1, ptl); + return 0; }