From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id ADE31D5AE79 for ; Thu, 7 Nov 2024 09:30:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3EEA6B0083; Thu, 7 Nov 2024 04:30:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EEF6E6B0085; Thu, 7 Nov 2024 04:30:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB6856B0088; Thu, 7 Nov 2024 04:30:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BF6416B0083 for ; Thu, 7 Nov 2024 04:30:52 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 3C71080559 for ; Thu, 7 Nov 2024 09:30:52 +0000 (UTC) X-FDA: 82758779166.02.F4E2698 Received: from szxga07-in.huawei.com (szxga07-in.huawei.com [45.249.212.35]) by imf06.hostedemail.com (Postfix) with ESMTP id F3E05180005 for ; Thu, 7 Nov 2024 09:30:23 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=none; spf=pass (imf06.hostedemail.com: domain of mawupeng1@huawei.com designates 45.249.212.35 as permitted sender) smtp.mailfrom=mawupeng1@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730971765; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7qa40Xcw43Xnub062gFKjW0nMoOvsKiQf8LgHTSGQgU=; b=j6qviU8R3lbLDasSBXnLXwWBXTDxuvBAcYGGwajFEzqZT+OprLWvDdSx6zbpr0XVYwjWZd aU+16AB5argxnDdN4dJ/WedLj/9OEIzyYELSTIsIDBtHHqOpugid5co6/14jeQefkZsWGA wuAxXBqBDTvEju1yBV8kIl995ByeCaY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730971765; a=rsa-sha256; cv=none; b=oIyIXaJ1y9nx2CNIUi/mJYrJvF8yCtDHkyhUfvRsB7wigoGLc7tZUnzgeRJfaEGDa92rSL t95llXqd2pNWTdQMV3WA3VGz+3S6kNSrbSGhmSgrE24uvwDaqJ3GmUysNo2seLtxDBOMvz LWO+uvszBRi63ugwNk86JSTCuJ7uu0A= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none; spf=pass (imf06.hostedemail.com: domain of mawupeng1@huawei.com designates 45.249.212.35 as permitted sender) smtp.mailfrom=mawupeng1@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.234]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4XkcG55QMNz1SDql; Thu, 7 Nov 2024 17:29:01 +0800 (CST) Received: from kwepemg100017.china.huawei.com (unknown [7.202.181.58]) by mail.maildlp.com (Postfix) with ESMTPS id 93DC61401F4; Thu, 7 Nov 2024 17:30:44 +0800 (CST) Received: from [10.174.178.120] (10.174.178.120) by kwepemg100017.china.huawei.com (7.202.181.58) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Thu, 7 Nov 2024 17:30:43 +0800 Message-ID: <673991ef-90d0-456f-ae75-8d70bff3ea4d@huawei.com> Date: Thu, 7 Nov 2024 17:30:42 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird CC: , , , , , , , , , , , , , , Subject: Re: [PATCH v1] x86/mm/pat: fix VM_PAT handling when fork() fails in copy_page_range() To: , References: <20241029210331.1339581-1-david@redhat.com> <8c494db6-1def-44ea-84ef-51d0140bddf3@redhat.com> <7e860861-c7cb-401f-ac92-61db92fa4d8b@huawei.com> <262aa19c-59fe-420a-aeae-0b1866a3e36b@redhat.com> From: mawupeng In-Reply-To: <262aa19c-59fe-420a-aeae-0b1866a3e36b@redhat.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.178.120] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemg100017.china.huawei.com (7.202.181.58) X-Rspamd-Server: rspam10 X-Stat-Signature: yns6qkp3b1nxb58njh7njknet1zyb53u X-Rspamd-Queue-Id: F3E05180005 X-Rspam-User: X-HE-Tag: 1730971823-948486 X-HE-Meta: U2FsdGVkX19eyq5HLXpGB+VKvOCAJ5IYUwr1HN2ruV3RQ0T0umkSwScogFPuyonMCjzkBY5kVfCFXyC/UmB64BMJqDD6fTEEqLcW9FmlLE15dYP4zYBgrjqY4cNdRyZloWiCUxaH5+fKjpsi8IEiZKvgmv3OSV5m1EoiLwbpDGgvtcCkfeXk0j/gn1paNLaLjW4ZTOq3xTHImkVcH3fZG1+tD0vrob3PeC4zYdZycR0+IXwHSgmr2osQ65h56/Rxav5cIFlBlvsZEHHcqOp+b52TIUR1ftP9yoDdYtu9/KB3/mdok7cYkMBG6ik/nXUzWXYaft53kcHAZkn9nXmtO1VhhfClwMwVReNS5sp/1QR/9Q7ZsIGbLDR7jKVN0Z9+LN4/NEoYbCpBr9VIOTmQ9ixDmmbFqIH+esNV3Xr/hetv37qY6n5spPpKnm3gVZZbFAk2U5BVaLQLqKB0SlSSlzQfDVHruVsyarp6/8ajcOxTS6SQBqLnMt+Q+Iii+m5RCHl9McqOvzRqENALky0GXnJYFtASSMijeKcdviVC6g+fS2ZGibf360uMpcTei8eoGDyxh7Tz/TzfsVVmRlIsQzhE8rrg/3y2OxeGlK4tFZSMB2KlGIqf+IYABgxFldqawCgFyyjslEogQS/SX6xLG8laZ1ZhCw2Kp1NV8mU0PGbwuDunnSRy879yPu0SDpaJ2rF690QZQu0GQSrrPvdLOA7nmWSkwmHlx+CzYW7EUzQeIiAanuXunyeTezyDsdPssnpYSqEJppaZEjmnlBEPtraXifaBUdz2/8r8rjjKwt3ZMT7QGtIOXtP7NnflBpjBi+jwWuVWXSPAEMBSvJN/j6Veii0lwBCXhB41gTS5+PrnaXLAmHb+7zpP5+FQNxSaHjIE+HND6dME0kyt65NivBzG8AHTD06n+tVRL/wcQPfN9AWyXUD/rnLJs4Il2LVg6m/G/yFPVsezGhx0AcR Jt0ai0cP xI5Si28XkhbvFq2BqkQH7mG0q2YJUzKjO5EVzax3NV9x6B13Zlr4wt/AlChWwc7rOk6H1k2aoYskyqz3Jxs0+/Pl7g9Hb4XbZMoWnuFGJE2afAYSGVICLeOhWavKlyoouiaWM2gYk356YXAPl8ODFQV/84gELjBNNRXJxMtpCD0VI+uzhKXy48RsdUQcOK++kRIvo6D84TjlK04pfQu6KL9yAXXmrRTb6/fep0NVROm3uezHb0Nc+OF2ie+Q+PmUC31sWe5SKTm4xs9FfsES+1wpJ1f7l3//ylMLD 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: List-Subscribe: List-Unsubscribe: On 2024/11/7 17:08, David Hildenbrand wrote: > On 07.11.24 09:43, mawupeng wrote: >> >> >> On 2024/10/31 17:47, David Hildenbrand wrote: >>> On 30.10.24 22:32, Peter Xu wrote: >>>> On Tue, Oct 29, 2024 at 10:03:31PM +0100, David Hildenbrand wrote: >>>>> If track_pfn_copy() fails, we already added the dst VMA to the maple >>>>> tree. As fork() fails, we'll cleanup the maple tree, and stumble over >>>>> the dst VMA for which we neither performed any reservation nor copied >>>>> any page tables. >>>>> >>>>> Consequently untrack_pfn() will see VM_PAT and try obtaining the >>>>> PAT information from the page table -- which fails because the page >>>>> table was not copied. >>>>> >>>>> The easiest fix would be to simply clear the VM_PAT flag of the dst VMA >>>>> if track_pfn_copy() fails. However, the whole thing is about "simply" >>>>> clearing the VM_PAT flag is shaky as well: if we passed track_pfn_copy() >>>>> and performed a reservation, but copying the page tables fails, we'll >>>>> simply clear the VM_PAT flag, not properly undoing the reservation ... >>>>> which is also wrong. >>>> >>>> David, >>>> >>> >>> Hi Peter, >>> >>>> Sorry to not have chance yet reply to your other email.. >>>> >>>> The only concern I have with the current fix to fork() is.. we started to >>>> have device drivers providing fault() on PFNMAPs as vfio-pci does, then I >>>> think it means we could potentially start to hit the same issue even >>>> without fork(), but as long as the 1st pgtable entry of the PFNMAP range is >>>> not mapped when the process with VM_PAT vma exit()s, or munmap() the vma. >>> >>> As these drivers are not using remap_pfn_range, there is no way they could currently get VM_PAT set. >>> >>> So what you describe is independent of the current state we are fixing here, and this fix should sort out the issues with current VM_PAT handling. >>> >>> It indeed is an interesting question how to handle reservations when *not* using remap_pfn_range() to cover the whole area. >>> >>> remap_pfn_range() handles VM_PAT automatically because it can do it: it knows that the whole range will map consecutive PFNs with the same protection, and we expect not parts of the range suddenly getting unmapped (and any driver that does that is buggy). >>> >>> This behavior is, however, not guaranteed to be the case when remap_pfn_range() is *not* called on the whole range. >>> >>> For that case (i.e., vfio-pci) I still wonder if the driver shouldn't do the reservation and leave VM_PAT alone. >>> >>> In the driver, we'd do the reservation once and not worry about fork() etc ... and we'd undo the reservation once the last relevant VM_PFNMAP VMA is gone or the driver let's go of the device. I assume there are already mechanisms in place to deal with that to some degree, because the driver cannot go away while any VMA still has the VM_PFNMAP mapping -- otherwise something would be seriously messed up. >>> >>> Long story short: let's look into not using VM_PAT for that use case. >>> >>> Looking at the VM_PAT issues we had over time, not making it more complicated sounds like a very reasonable thing to me :) >> >> Hi David, >> >> The VM_PAT reservation do seems complicated. It can trigger the same warning in get_pat_info if remap_p4d_range fails: >> >> remap_pfn_range >>    remap_pfn_range_notrack >>      remap_pfn_range_internal >>        remap_p4d_range    // page allocation can failed here >>      zap_page_range_single >>        unmap_single_vma >>          untrack_pfn >>            get_pat_info >>              WARN_ON_ONCE(1); >> >> Any idea on this problem? > > In remap_pfn_range(), if remap_pfn_range_notrack() fails, we call untrack_pfn(), to undo the tracking. > > The problem is that zap_page_range_single() shouldn't do that untrack_pfn() call. > > That should be fixed by Peter's patch: > > https://lore.kernel.org/all/20240712144244.3090089-1-peterx@redhat.com/T/#u Thank you for your prompt reply. This do fix this issue. >