Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp4014138ioo; Wed, 25 May 2022 12:52:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyiq3Be+iZgNQ7DM/Q/e1lPqYWgrcuq/edb4BvRZP/Zf7k2FRkEd4KFM6sQ1WLpycfT1lb7 X-Received: by 2002:a05:6402:4020:b0:42a:d19f:88df with SMTP id d32-20020a056402402000b0042ad19f88dfmr36177385eda.56.1653508357206; Wed, 25 May 2022 12:52:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653508357; cv=none; d=google.com; s=arc-20160816; b=DVlwY/gkRRYTzpnmIjMnMp3WNAH1AIE2K9IB195sWzGhfr2qh2rDgRtQ0EmIb7u2J3 44DmZP3go0MJCARRMH1IKPgTC+eXzmUNbJ5GmBVGg941tSE2l7lp0d07JZuHaPe/5aPT lsihaiWjAbBxHcbrAj0pN/wp1w2cKyht/+tk1qLd/uycfiQxnrqE5im9bMmzWOtOd9ba pgqM4EBmS+bYFYOjiZySHN+uYDB98CVl+9qQtfsUaqacnE/pREEvB2hU8LwMxbje3OBW fUDi19pfLa5i+ShTeURzA0kwEU2Mq3LFbHw4TuLhgfa0QsF/U0pOQG9cZqzmNUFFjwC2 bmTw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=djZ8XmF/BN7RDuUvTtWT4tV4yPNLXInCBVYncLZI08A=; b=svA6ZrV6O6u5CXJfWJuHILFoagE4Jy7MrFTvlW46/jWsTXgyEDDYcZqv9rbF+NW3ZL g7s0V4kU2CeLSurNIXG5kvj/XhkzUxt42unOvspvRxzT9XpP98vjfXGQtvztDkwfrabS NYNy855JukKLXarH612uhosv7URjFFvU80MlZ5qgZODJwjtuLmVksUFbbtteCRsWnYHH N3DftSW2lJjebrZgrhScC0zAtE/X4C+E7oACAoxMc/2P5DddDFVy+c17JGJ7W+RVcYKx qdU0zlUsz8YfN+OG78mUNhNzrJDJcPwHq8CySKXYiRy9rqzsFZzFqO5rQnJs3qMAVt+t fR4g== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i11-20020a0564020f0b00b00427e36ae868si2827544eda.130.2022.05.25.12.52.10; Wed, 25 May 2022 12:52:37 -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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236962AbiEYISp (ORCPT + 99 others); Wed, 25 May 2022 04:18:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232083AbiEYIST (ORCPT ); Wed, 25 May 2022 04:18:19 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A061D7B9C1 for ; Wed, 25 May 2022 01:18:17 -0700 (PDT) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4L7P4z4CQzzQkNy; Wed, 25 May 2022 16:15:15 +0800 (CST) Received: from huawei.com (10.175.124.27) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Wed, 25 May 2022 16:18:15 +0800 From: Miaohe Lin To: , , CC: , , , , , , , , , , Subject: [PATCH v3 4/4] mm/migration: fix potential pte_unmap on an not mapped pte Date: Wed, 25 May 2022 16:18:22 +0800 Message-ID: <20220525081822.53547-5-linmiaohe@huawei.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20220525081822.53547-1-linmiaohe@huawei.com> References: <20220525081822.53547-1-linmiaohe@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.124.27] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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 __migration_entry_wait and migration_entry_wait_on_locked assume pte is always mapped from caller. But this is not the case when it's called from migration_entry_wait_huge and follow_huge_pmd. Add a hugetlbfs variant that calls hugetlb_migration_entry_wait(ptep == NULL) to fix this issue. Fixes: 30dad30922cc ("mm: migration: add migrate_entry_wait_huge()") Suggested-by: David Hildenbrand Signed-off-by: Miaohe Lin Reviewed-by: David Hildenbrand --- include/linux/swapops.h | 12 ++++++++---- mm/hugetlb.c | 4 ++-- mm/migrate.c | 23 +++++++++++++++++++---- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/include/linux/swapops.h b/include/linux/swapops.h index f24775b41880..bb7afd03a324 100644 --- a/include/linux/swapops.h +++ b/include/linux/swapops.h @@ -244,8 +244,10 @@ extern void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep, spinlock_t *ptl); extern void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, unsigned long address); -extern void migration_entry_wait_huge(struct vm_area_struct *vma, - struct mm_struct *mm, pte_t *pte); +#ifdef CONFIG_HUGETLB_PAGE +extern void __migration_entry_wait_huge(pte_t *ptep, spinlock_t *ptl); +extern void migration_entry_wait_huge(struct vm_area_struct *vma, pte_t *pte); +#endif #else static inline swp_entry_t make_readable_migration_entry(pgoff_t offset) { @@ -271,8 +273,10 @@ static inline void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep, spinlock_t *ptl) { } static inline void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, unsigned long address) { } -static inline void migration_entry_wait_huge(struct vm_area_struct *vma, - struct mm_struct *mm, pte_t *pte) { } +#ifdef CONFIG_HUGETLB_PAGE +static inline void __migration_entry_wait_huge(pte_t *ptep, spinlock_t *ptl) { } +static inline void migration_entry_wait_huge(struct vm_area_struct *vma, pte_t *pte) { } +#endif static inline int is_writable_migration_entry(swp_entry_t entry) { return 0; diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 2026fcfc8886..5015270cca12 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5694,7 +5694,7 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, */ entry = huge_ptep_get(ptep); if (unlikely(is_hugetlb_entry_migration(entry))) { - migration_entry_wait_huge(vma, mm, ptep); + migration_entry_wait_huge(vma, ptep); return 0; } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) return VM_FAULT_HWPOISON_LARGE | @@ -6912,7 +6912,7 @@ follow_huge_pmd(struct mm_struct *mm, unsigned long address, } else { if (is_hugetlb_entry_migration(pte)) { spin_unlock(ptl); - __migration_entry_wait(mm, (pte_t *)pmd, ptl); + __migration_entry_wait_huge((pte_t *)pmd, ptl); goto retry; } /* diff --git a/mm/migrate.c b/mm/migrate.c index 97c31b87d1a2..581dd2b4dcbb 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -315,13 +315,28 @@ void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd, __migration_entry_wait(mm, ptep, ptl); } -void migration_entry_wait_huge(struct vm_area_struct *vma, - struct mm_struct *mm, pte_t *pte) +#ifdef CONFIG_HUGETLB_PAGE +void __migration_entry_wait_huge(pte_t *ptep, spinlock_t *ptl) { - spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte); - __migration_entry_wait(mm, pte, ptl); + pte_t pte; + + spin_lock(ptl); + pte = huge_ptep_get(ptep); + + if (unlikely(!is_hugetlb_entry_migration(pte))) + spin_unlock(ptl); + else + migration_entry_wait_on_locked(pte_to_swp_entry(pte), NULL, ptl); } +void migration_entry_wait_huge(struct vm_area_struct *vma, pte_t *pte) +{ + spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), vma->vm_mm, pte); + + __migration_entry_wait_huge(pte, ptl); +} +#endif + #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd) { -- 2.23.0