From patchwork Fri Feb 17 02:56:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: mawupeng X-Patchwork-Id: 58327 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp665036wrn; Thu, 16 Feb 2023 19:09:32 -0800 (PST) X-Google-Smtp-Source: AK7set/VrCGF1JCOMg0CeUTh0YtlpFbI/QnCoBHC3QqO9FGdZqIPMZtIM9eWFurlMbqalDz5QjfY X-Received: by 2002:a17:902:d4ce:b0:19a:6ec0:50c2 with SMTP id o14-20020a170902d4ce00b0019a6ec050c2mr9325800plg.26.1676603372244; Thu, 16 Feb 2023 19:09:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676603372; cv=none; d=google.com; s=arc-20160816; b=n+uYXMT70uL3QR6braYAGm1YJO7JctcQ9rfC7kCNWjt8KBSd4iihqdbT8UVVsYRlyO oxOII0G6cnw0wsfP8qeEwNDWGAGPDrYqaz9nhyBRC4TMmXlmbQik2tCaW4L+FbC/scCQ WRTR1bE2dNzblY/WgbOblL50HSQkiU9XGh1y4jmJkevFysUmd6gingwKaaXYyH7KajPJ msCwpJN8lfQR3y/opbvxeRJvlqphdSgCnsEqLyJ8l3rt/SXWQ+hBEkX9AgevExTvTYHH EkfNPpZq90QCd3KOoEDBK7b+uM0S3s0Fd4VIkFwo9pVUq6PqyIhteNQzSOtVvaSt+l7Y P+YA== 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 :message-id:date:subject:cc:to:from; bh=Nr+prP1JGUyGXtaNaNZhgTOV0dPrAIyCavLasHuCb6I=; b=DIYVWTCuaY5+wOix8DZ8p+i5eGCiAIvWCcM6DxVYYrSfpctmnpfjDgVJRSIkVmnaMw 04looBeob6nF2IqTNE5aaDefRdkgoEKkeZy4/WYuiJ0lIP+epK/3J+l3qBs+b9NGDQDM RdzXN8s5gwr2Ec8VuB2K/c6mVbxk4QAxh5gclY4fxX70MdTW29bWGmQ45w1VSOgE6d5p vkbA7Ycc8oYekcQz2ioi4N3aQ3AjZarFaekqOS6lJnUSQeU8qomN7UARRDkCn6zl0ngh +Oo0Q2e+gHh0sBxOeWwUuGFLaGRCAyn+MlEMGnh98wq1GUacQGihayX1EDYTBAtrAVd4 JeYQ== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id je9-20020a170903264900b0019b0e2473e2si1603103plb.416.2023.02.16.19.09.18; Thu, 16 Feb 2023 19:09:32 -0800 (PST) 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 S230139AbjBQC4Z (ORCPT + 99 others); Thu, 16 Feb 2023 21:56:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229489AbjBQC4X (ORCPT ); Thu, 16 Feb 2023 21:56:23 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C6EA5382B for ; Thu, 16 Feb 2023 18:56:21 -0800 (PST) Received: from dggpemm500014.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4PHxJn4D1LzrRsv; Fri, 17 Feb 2023 10:55:53 +0800 (CST) Received: from localhost.localdomain (10.175.112.125) by dggpemm500014.china.huawei.com (7.185.36.153) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.17; Fri, 17 Feb 2023 10:56:19 +0800 From: Wupeng Ma To: , , , CC: , , , , , , Subject: [RFC PATCH 1/1] x86/mm/pat: Clear VM_PAT if copy_p4d_range failed Date: Fri, 17 Feb 2023 10:56:15 +0800 Message-ID: <20230217025615.1595558-1-mawupeng1@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500014.china.huawei.com (7.185.36.153) 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758046057930732237?= X-GMAIL-MSGID: =?utf-8?q?1758046057930732237?= From: Ma Wupeng x86/mm/pat: Clear VM_PAT if copy_p4d_range failed. Syzbot reports a warning in untrack_pfn(). Digging into the root we found that this is due to memory allocation failure in pmd_alloc_one. And this failure is produced due to failslab. In copy_page_range(), memory alloaction for pmd failed. During the error handling process in copy_page_range(), mmput() is called to remove all vmas. While untrack_pfn this empty pfn, warning happens. Here's a simplified flow: dup_mm dup_mmap copy_page_range copy_p4d_range copy_pud_range copy_pmd_range pmd_alloc __pmd_alloc pmd_alloc_one page = alloc_pages(gfp, 0); if (!page) return NULL; mmput exit_mmap unmap_vmas unmap_single_vma untrack_pfn follow_phys WARN_ON_ONCE(1); Since this vma is not generate successfully, we can clear flag VM_PAT. In this case, untrack_pfn() will not be called while cleaning this vma. Function untrack_pfn_moved() has also been renamed to fit the new logic. Reported-by: syzbot+5f488e922d047d8f00cc@syzkaller.appspotmail.com Signed-off-by: Ma Wupeng --- arch/x86/mm/pat/memtype.c | 12 ++++++++---- include/linux/pgtable.h | 7 ++++--- mm/memory.c | 1 + mm/mremap.c | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/arch/x86/mm/pat/memtype.c b/arch/x86/mm/pat/memtype.c index fb4b1b5e0dea..558bb71ff350 100644 --- a/arch/x86/mm/pat/memtype.c +++ b/arch/x86/mm/pat/memtype.c @@ -1070,11 +1070,15 @@ void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn, } /* - * untrack_pfn_moved is called, while mremapping a pfnmap for a new region, - * with the old vma after its pfnmap page table has been removed. The new - * vma has a new pfnmap to the same pfn & cache type with VM_PAT set. + * untrack_pfn_clear is called if the following situation fits: + * + * 1) while mremapping a pfnmap for a new region, with the old vma after + * its pfnmap page table has been removed. The new vma has a new pfnmap + * to the same pfn & cache type with VM_PAT set. + * 2) while duplicating vm area, the new vma fails to copy the pgtable from + * old vma. */ -void untrack_pfn_moved(struct vm_area_struct *vma) +void untrack_pfn_clear(struct vm_area_struct *vma) { vma->vm_flags &= ~VM_PAT; } diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 1159b25b0542..e211124f4330 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -1219,9 +1219,10 @@ static inline void untrack_pfn(struct vm_area_struct *vma, } /* - * untrack_pfn_moved is called while mremapping a pfnmap for a new region. + * untrack_pfn_clear is called while mremapping a pfnmap for a new region + * or fails to copy pgtable during duplicate vm area. */ -static inline void untrack_pfn_moved(struct vm_area_struct *vma) +static inline void untrack_pfn_clear(struct vm_area_struct *vma) { } #else @@ -1233,7 +1234,7 @@ extern void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot, extern int track_pfn_copy(struct vm_area_struct *vma); extern void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn, unsigned long size); -extern void untrack_pfn_moved(struct vm_area_struct *vma); +extern void untrack_pfn_clear(struct vm_area_struct *vma); #endif #ifdef CONFIG_MMU diff --git a/mm/memory.c b/mm/memory.c index f526b9152bef..846d4136d1de 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1278,6 +1278,7 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) continue; if (unlikely(copy_p4d_range(dst_vma, src_vma, dst_pgd, src_pgd, addr, next))) { + untrack_pfn_clear(dst_vma); ret = -ENOMEM; break; } diff --git a/mm/mremap.c b/mm/mremap.c index 930f65c315c0..6ed28eeae5a8 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -682,7 +682,7 @@ static unsigned long move_vma(struct vm_area_struct *vma, /* Tell pfnmap has moved from this vma */ if (unlikely(vma->vm_flags & VM_PFNMAP)) - untrack_pfn_moved(vma); + untrack_pfn_clear(vma); if (unlikely(!err && (flags & MREMAP_DONTUNMAP))) { /* We always clear VM_LOCKED[ONFAULT] on the old vma */