Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp1863033rwb; Fri, 19 Aug 2022 10:37:57 -0700 (PDT) X-Google-Smtp-Source: AA6agR66+NNX9YnwyDvrz9TFZrQxYtvKMKoGW9yfK1lnEZhtxzUvriz3k0vPUm8R/T2YsatwF9fI X-Received: by 2002:a17:907:7b83:b0:730:8649:9836 with SMTP id ne3-20020a1709077b8300b0073086499836mr5546685ejc.0.1660930677312; Fri, 19 Aug 2022 10:37:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660930677; cv=none; d=google.com; s=arc-20160816; b=i40ArRktjJAcaogO7tghi48ManB2ExiRwW1oHr3w+A58CeUngarimIWGDUhmy0XxvR s3lWnfW0wd6Uvnm3/gyyTGWExiYwTXHsBiK3yG4uMROFscOBDKqbNTQKxIYGZM5C3xVH C19nvIewL4MoftcSXOoekJQEGaIYdwMjHYETew2YyPhxHW6tnt31iSNY8c1AnS4GuhCo 9fxHBkM1tgiyq4wUvu1yv49eOwIp/O9rLQuLPhuZHdpH7as22dIP/zxKBpOqZHSbS1Nx NvqXjhBc1EjAGPFglvZzday665y8sgcmd25fdMX22tG0oJzLYSPeUkPu8gbDq5XE7w2j 7djg== 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=op0OZovKg/S7gMuEQm6i0bQPkK5/t1E365Y5TPKozhI=; b=Neh5Wju6W438w4WcFkfJOZVHM56RUzPxLfZ8cDUB8XVRfMYGHBKylAzS+BsUuTfrMb zkfRLFy/7adKnIL9XPWIfgXNQJVVqu8OAFHDFRiPfY7blFfu8LcAsboe0NNm/kdIvxnX Vygv8u+B5fJ1CbaA48UXXoEjQp7tb8iyd5jvsubA0oqkOVEfh4i4kqghwiDgGk0uCLRY 6XRAzd1X/S0Q1tmZXQP8pA+e1kjQDIU/C4rKWj7EEtsyzfRPOkrlrAumRoIZifiCBKbC jYycopuG6wEUCtu0B41oHW9jCLHvwBEMrH4bIpCubQEYt7QagF2QHYdsy3xWuLbn2UC8 qepg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="z/cCq06X"; 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 hp42-20020a1709073e2a00b007309910699bsi3568093ejc.673.2022.08.19.10.37.30; Fri, 19 Aug 2022 10:37:57 -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="z/cCq06X"; 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 S1350220AbiHSPsP (ORCPT + 99 others); Fri, 19 Aug 2022 11:48:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350216AbiHSPrH (ORCPT ); Fri, 19 Aug 2022 11:47:07 -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 C444472EF0; Fri, 19 Aug 2022 08:46:54 -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 59344B82811; Fri, 19 Aug 2022 15:46:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5D916C433D6; Fri, 19 Aug 2022 15:46:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660924011; bh=8bqd+yYOfyD1mkyKe7DA4R6bQcoB4Fqwxcv8wENN82w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=z/cCq06XqCiGUf/9FJEmRH7yhIjBllaMovVFt7oP8djUhFkYgr1J0FOK75jQk5mUv IQnUy8NDacuNkigFP72OigInZZQvzZZ8D8eCpLngpPluzhL/aNpxsOyl61u0JLmItj 6Ws7/ZuZBym1WkR/PPaA1Go+mu+YeJcuDUYnaVZA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Aneesh Kumar K.V" , Hugh Dickins , "Kirill A. Shutemov" , Christophe Leroy , Joel Fernandes , Kalesh Singh , "Kirill A. Shutemov" , Michael Ellerman , Nicholas Piggin , Stephen Rothwell , Andrew Morton , Linus Torvalds , Jann Horn Subject: [PATCH 5.10 026/545] mm/mremap: hold the rmap lock in write mode when moving page table entries. Date: Fri, 19 Aug 2022 17:36:36 +0200 Message-Id: <20220819153830.371154034@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220819153829.135562864@linuxfoundation.org> References: <20220819153829.135562864@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,T_SCC_BODY_TEXT_LINE 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: Aneesh Kumar K.V commit 97113eb39fa7972722ff490b947d8af023e1f6a2 upstream. To avoid a race between rmap walk and mremap, mremap does take_rmap_locks(). The lock was taken to ensure that rmap walk don't miss a page table entry due to PTE moves via move_pagetables(). The kernel does further optimization of this lock such that if we are going to find the newly added vma after the old vma, the rmap lock is not taken. This is because rmap walk would find the vmas in the same order and if we don't find the page table attached to older vma we would find it with the new vma which we would iterate later. As explained in commit eb66ae030829 ("mremap: properly flush TLB before releasing the page") mremap is special in that it doesn't take ownership of the page. The optimized version for PUD/PMD aligned mremap also doesn't hold the ptl lock. This can result in stale TLB entries as show below. This patch updates the rmap locking requirement in mremap to handle the race condition explained below with optimized mremap:: Optmized PMD move CPU 1 CPU 2 CPU 3 mremap(old_addr, new_addr) page_shrinker/try_to_unmap_one mmap_write_lock_killable() addr = old_addr lock(pte_ptl) lock(pmd_ptl) pmd = *old_pmd pmd_clear(old_pmd) flush_tlb_range(old_addr) *new_pmd = pmd *new_addr = 10; and fills TLB with new addr and old pfn unlock(pmd_ptl) ptep_clear_flush() old pfn is free. Stale TLB entry Optimized PUD move also suffers from a similar race. Both the above race condition can be fixed if we force mremap path to take rmap lock. Link: https://lkml.kernel.org/r/20210616045239.370802-7-aneesh.kumar@linux.ibm.com Fixes: 2c91bd4a4e2e ("mm: speed up mremap by 20x on large regions") Fixes: c49dd3401802 ("mm: speedup mremap on 1GB or larger regions") Link: https://lore.kernel.org/linux-mm/CAHk-=wgXVR04eBNtxQfevontWnP6FDm+oj5vauQXP3S-huwbPw@mail.gmail.com Signed-off-by: Aneesh Kumar K.V Acked-by: Hugh Dickins Acked-by: Kirill A. Shutemov Cc: Christophe Leroy Cc: Joel Fernandes Cc: Kalesh Singh Cc: Kirill A. Shutemov Cc: Michael Ellerman Cc: Nicholas Piggin Cc: Stephen Rothwell Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds [patch rewritten for backport since the code was refactored since] Signed-off-by: Jann Horn Signed-off-by: Greg Kroah-Hartman --- mm/mremap.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) --- a/mm/mremap.c +++ b/mm/mremap.c @@ -310,12 +310,10 @@ unsigned long move_page_tables(struct vm */ bool moved; - if (need_rmap_locks) - take_rmap_locks(vma); + take_rmap_locks(vma); moved = move_normal_pmd(vma, old_addr, new_addr, old_pmd, new_pmd); - if (need_rmap_locks) - drop_rmap_locks(vma); + drop_rmap_locks(vma); if (moved) continue; #endif