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 883A6C3DA42 for ; Wed, 17 Jul 2024 14:17:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D7D0A6B0089; Wed, 17 Jul 2024 10:17:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D060A6B008C; Wed, 17 Jul 2024 10:17:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B312B6B0092; Wed, 17 Jul 2024 10:17:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 8CB106B0089 for ; Wed, 17 Jul 2024 10:17:22 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3EFDB1A0AC0 for ; Wed, 17 Jul 2024 14:17:22 +0000 (UTC) X-FDA: 82349447124.03.54FE992 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf04.hostedemail.com (Postfix) with ESMTP id 9428C40024 for ; Wed, 17 Jul 2024 14:17:19 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="Z6QO/AYP"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf04.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721225807; a=rsa-sha256; cv=none; b=uJ9ntjJjFwi7HpHhp0BXO1wigyw7Tx0e3+EXuGUn0TRzLWQsDPH/xcH1nC2rZCNYoEF/Hl 445I6yYIZAz8OB2NVSEYMB/wSc/xRWEKwqKnmyj/tg0FAVmfBUlVTvPYh+3g5UDnbjjXsV mPx2MRI5zkMPZ2SLXAd5J/O7gcCZh+8= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="Z6QO/AYP"; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf04.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721225807; 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:dkim-signature; bh=AybjgpdX+v+9PD235KW36GR5rh2bKZzBZYsfPIsm4Mc=; b=dt5WDoIAfLKP4Tkk+mzmKsyFe95ccqAOeU4nwa91El7gE+A50AQgzDIDDGxt/iFfbnrahR afK3wg6dd3aPKbElnxavtiZoh0rQvP72q1yV2zSh/mAkMSkKBmXBCjbSzfNvPKw1mw1LhI 37o/1gq/LDtQSebw01W1Z+qPF4l1BqI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721225838; h=from:from: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:autocrypt:autocrypt; bh=AybjgpdX+v+9PD235KW36GR5rh2bKZzBZYsfPIsm4Mc=; b=Z6QO/AYP3t23JbyXCSVlVkbCx+CrmErYSt6AhOJlzQ9fxIkAdoGIqy4dFDxCWSaIwyPGSb 6YBfsYs1/E1LGKEHFfxSZkdEbLJ9if9+ABTCNDFZjvu7vAuYtaVKDsv9jb/pOBxnKpdetp 9/wgPJjlqr90gU4ct1pfbJTyZMatGwU= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-490-OGFjqx9UPGCCdAI_sRUoxg-1; Wed, 17 Jul 2024 10:17:16 -0400 X-MC-Unique: OGFjqx9UPGCCdAI_sRUoxg-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-367a058fa21so477127f8f.1 for ; Wed, 17 Jul 2024 07:17:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721225835; x=1721830635; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from:references:cc:to:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=AybjgpdX+v+9PD235KW36GR5rh2bKZzBZYsfPIsm4Mc=; b=AY3NLsaTuovwvvUuzBZcjchEH0D9TG5GDjOCjgk4EffWYPvy8PxzGHG6pjNnatG4Vj XeQaNdW55RkrS8ixOtgOyUijPrny19v21P/rKpruhdC4Ftn75zuUV1FFMWW+m68Q+TIF V+D4EwPDpphTmEAHNaH2ylkLjffJI7HOeRLMccwTcvczPYvOXol9+NUJc5ewAsi1fCiF vHd7LKj+h1h0qV8ldSmF/ZVqE9c34skzpBMzIu9O4/P4KAAEuz7q4g9prlCQJrx2AJko ijgR1NPecSb173pJ5pA4fHEyv+eeoWhZzFFr/3mZZyXdaSWtGkOX7z1Y0zGHylBSVdEw x99A== X-Forwarded-Encrypted: i=1; AJvYcCWwiTA0a60D55H8BX9Saqy8NVCtqlLPkzujrUx8XHVaX5Fm26JkaW60zieIlJYJWkI0CLy5F94Ix8LSIJFLx+jTfPs= X-Gm-Message-State: AOJu0YzLHQ2RqTOlsopNA0eK8d0b0sHpqmb4Yli6Mc6pKEF42rUIzTZf vJSM3EpQkA0svZKSlMMjFZjU88BvumTChqkDmLhI0pewx7NKGebqARqid1fM7M4gTcrWYXdSZ6h k26Lrhf+yd5dLTscg/RsWM+389M5iY03HwxrPBkWm3jGBa1qi X-Received: by 2002:a5d:6a8c:0:b0:368:37aa:50b3 with SMTP id ffacd0b85a97d-36837aa5114mr1278331f8f.24.1721225835022; Wed, 17 Jul 2024 07:17:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHnphFuCU3/s2xvd2+v3W8mDqCA/9Z7vgToEBk46u+RU/PCwUcungc0RBIN8ast5eoND1VwVA== X-Received: by 2002:a5d:6a8c:0:b0:368:37aa:50b3 with SMTP id ffacd0b85a97d-36837aa5114mr1278305f8f.24.1721225834479; Wed, 17 Jul 2024 07:17:14 -0700 (PDT) Received: from ?IPV6:2003:cb:c714:c00:b08b:a871:ce99:dfde? (p200300cbc7140c00b08ba871ce99dfde.dip0.t-ipconnect.de. [2003:cb:c714:c00:b08b:a871:ce99:dfde]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-427a5e8e2ecsm177697035e9.21.2024.07.17.07.17.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 17 Jul 2024 07:17:14 -0700 (PDT) Message-ID: <116ca902-103d-47cb-baf0-905983baf9bb@redhat.com> Date: Wed, 17 Jul 2024 16:17:12 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] mm/x86/pat: Only untrack the pfn range if unmap region To: Peter Xu , Yan Zhao Cc: "linux-kernel@vger.kernel.org" , "linux-mm@kvack.org" , Andrew Morton , Alex Williamson , Jason Gunthorpe , Al Viro , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "Kirill A . Shutemov" , "x86@kernel.org" , "Tian, Kevin" , Pei Li , David Wang <00107082@163.com>, Bert Karwatzki , Sergey Senozhatsky References: <20240712144244.3090089-1-peterx@redhat.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn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csstw2TAkFnMEbo9Zu9w7Kv AxBQXWeXhJI9XQssfrf4Gusdqx8nPEpfOqCtbbwJMATbHyqLt7/oz/5deGuwxgb65pWIzufa N7eop7uh+6bezi+rugUI+w6DABEBAAHCwXwEGAEIACYCGwwWIQQb2cqtc1xMOkYN/MpN3hD3 AP+DWgUCXw7HsgUJEqkpoQAKCRBN3hD3AP+DWrrpD/4qS3dyVRxDcDHIlmguXjC1Q5tZTwNB boaBTPHSy/Nksu0eY7x6HfQJ3xajVH32Ms6t1trDQmPx2iP5+7iDsb7OKAb5eOS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 9428C40024 X-Stat-Signature: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V7sM5kFyrs8Zfhsnpy88OJG9HLeNHbB2AdAjIXaFOnXIxCWjuTt6SvG7ZeZglYA7a1wYrP1eddsQKpXz0aWY5hASBroAiwH/tmIiiyT1JplV2cgP/vMGxAqVRtuhxfwEC2ISNTkUTqenYjl518E6PvI7vCzphXmMNKNOi7B+8P/WrnqRMX7u2thST6fjIRACsf1lVp+Waf5aXmm6TZwD0ED8+km3gptmUb3MzXOyzTMyVhqpQM3xAk9xOWLVrPL2zrF8xd9pH8c4EZ7LRtsA6587nINffx7SOdCZ1JtwkBK5D3Tyjn7y/LvyTkm/D8kOoMu+cI1BqeZUhFB0VxP7edT6mBs7idoD747VgPJ+O7mAPtkhvGyAf7S5xm4EhEWYS9czdjuGKpG7DGQSE6VW8+TqMVjw0nn+vMoK8LEa+uZ6qX3abBlmPdTI9KgQE864BJO/+/2grKd0aJWcXVP5IaP3NBid4UgDQlcqEy7WrOnFg+0GOQW+f8SxxdDbuzWZMcAtX/bzdwygfagZ3xpwNzCGYTrLnslgW9WxV3Nr6JZ/CVdxj+j8NrGaMbI69IFxWQ2jqeN+RApzaflDYlEDoE1mrqczZMqlTXn1EG/M1ICryuB/kOQ0ZZaoq9VLvH4HjBUtvuDtNEjCza2YDB5UXg5BZaWXgfHnDdJCNUkcod9IowpCKo1b1dO1RrIP5KOn1M/FppGuXov3Go59+lFDyAxK2juyWx6M21TMsfkw6p9tFNZpFu/h0ZgCKMwNBsogN8WwfhzgjiVxRer5Qm4K/NnOSWOcSU5vLSWvAwSvo2Km7wHYVjTD3UfJVbNzEFUGE4mBR3AvZTEQ8UcXf6FYxwVgukxvYnbRWjBI84g44Y6lq977cP+kdzvH333/WNT7lvVa2ku5uTirIGSENhZmrDw7+UxWnXEdPSwjXKG25g2CCNnhYo9csAA/Sg== 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 16.07.24 21:01, Peter Xu wrote: > On Tue, Jul 16, 2024 at 05:13:29PM +0800, Yan Zhao wrote: >> On Mon, Jul 15, 2024 at 10:29:59PM +0800, Peter Xu wrote: >>> On Mon, Jul 15, 2024 at 03:08:58PM +0800, Yan Zhao wrote: >>>> On Fri, Jul 12, 2024 at 10:42:44AM -0400, Peter Xu wrote: >>>>> This patch is one patch of an old series [1] that got reposted standalone >>>>> here, with the hope to fix some reported untrack_pfn() issues reported >>>>> recently [2,3], where there used to be other fix [4] but unfortunately >>>>> which looks like to cause other issues. The hope is this patch can fix it >>>>> the right way. >>>>> >>>>> X86 uses pfn tracking to do pfnmaps. AFAICT, the tracking should normally >>>>> start at mmap() of device drivers, then untracked when munmap(). However >>>>> in the current code the untrack is done in unmap_single_vma(). This might >>>>> be problematic. >>>>> >>>>> For example, unmap_single_vma() can be used nowadays even for zapping a >>>>> single page rather than the whole vmas. It's very confusing to do whole >>>>> vma untracking in this function even if a caller would like to zap one >>>>> page. It could simply be wrong. >>>>> >>>>> Such issue won't be exposed by things like MADV_DONTNEED won't ever work >>>>> for pfnmaps and it'll fail the madvise() already before reaching here. >>>>> However looks like it can be triggered like what was reported where invoked >>>>> from an unmap request from a file vma. >>>>> >>>>> There's also work [5] on VFIO (merged now) to allow tearing down MMIO >>>>> pgtables before an munmap(), in which case we may not want to untrack the >>>>> pfns if we're only tearing down the pgtables. IOW, we may want to keep the >>>>> pfn tracking information as those pfn mappings can be restored later with >>>>> the same vma object. Currently it's not an immediate problem for VFIO, as >>>>> VFIO uses UC- by default, but it looks like there's plan to extend that in >>>>> the near future. >>>>> >>>>> IIUC, this was overlooked when zap_page_range_single() was introduced, >>>>> while in the past it was only used in the munmap() path which wants to >>>>> always unmap the region completely. E.g., commit f5cc4eef9987 ("VM: make >>>>> zap_page_range() callers that act on a single VMA use separate helper") is >>>>> the initial commit that introduced unmap_single_vma(), in which the chunk >>>>> of untrack_pfn() was moved over from unmap_vmas(). >>>>> >>>>> Recover that behavior to untrack pfnmap only when unmap regions. >>>>> >>>>> [1] https://lore.kernel.org/r/20240523223745.395337-1-peterx@redhat.com >>>>> [2] https://groups.google.com/g/syzkaller-bugs/c/FeQZvSbqWbQ/m/tHFmoZthAAAJ >>>>> [3] https://lore.kernel.org/r/20240712131931.20207-1-00107082@163.com >>>>> [4] https://lore.kernel.org/all/20240710-bug12-v1-1-0e5440f9b8d3@gmail.com/ >>>>> [5] https://lore.kernel.org/r/20240523195629.218043-1-alex.williamson@redhat.com >>>>> >>>>> Cc: Alex Williamson >>>>> Cc: Jason Gunthorpe >>>>> Cc: Al Viro >>>>> Cc: Dave Hansen >>>>> Cc: Andy Lutomirski >>>>> Cc: Peter Zijlstra >>>>> Cc: Thomas Gleixner >>>>> Cc: Ingo Molnar >>>>> Cc: Borislav Petkov >>>>> Cc: Kirill A. Shutemov >>>>> Cc: x86@kernel.org >>>>> Cc: Yan Zhao >>>>> Cc: Kevin Tian >>>>> Cc: Pei Li >>>>> Cc: David Hildenbrand >>>>> Cc: David Wang <00107082@163.com> >>>>> Cc: Bert Karwatzki >>>>> Cc: Sergey Senozhatsky >>>>> Signed-off-by: Peter Xu >>>>> --- >>>>> >>>>> NOTE: I massaged the commit message comparing to the rfc post [1], the >>>>> patch itself is untouched. Also removed rfc tag, and added more people >>>>> into the loop. Please kindly help test this patch if you have a reproducer, >>>>> as I can't reproduce it myself even with the syzbot reproducer on top of >>>>> mm-unstable. Instead of further check on the reproducer, I decided to send >>>>> this out first as we have a bunch of reproducers on the list now.. >>>>> --- >>>>> mm/memory.c | 5 ++--- >>>>> 1 file changed, 2 insertions(+), 3 deletions(-) >>>>> >>>>> diff --git a/mm/memory.c b/mm/memory.c >>>>> index 4bcd79619574..f57cc304b318 100644 >>>>> --- a/mm/memory.c >>>>> +++ b/mm/memory.c >>>>> @@ -1827,9 +1827,6 @@ static void unmap_single_vma(struct mmu_gather *tlb, >>>>> if (vma->vm_file) >>>>> uprobe_munmap(vma, start, end); >>>>> >>>>> - if (unlikely(vma->vm_flags & VM_PFNMAP)) >>>>> - untrack_pfn(vma, 0, 0, mm_wr_locked); >>>>> - >>>> Specifically to VFIO's case, looks it doesn't matter if untrack_pfn() is >>>> called here, since remap_pfn_range() is not called in mmap() and fault >>>> handler, and therefore (vma->vm_flags & VM_PAT) is always 0. >>> >>> Right when with current repo, but I'm thinking maybe we should have VM_PAT >>> there.. >> Yes, I agree. >> >> But, currently for VFIO, it cannot call io_remap_pfn_range() in the fault >> handler since vm_flags_set() requires mmap lock held for write while >> the fault handler can only hold mmap lock for read. >> So, it relies on ioremap()/iounmap() to reserve/de-reserve memtypes, >> without VM_PAT being set in vma. > > Right, neither vm_flags_set() nor io_remap_pfn_range() should be called in > a fault handler. They should only be called in mmap() phase. > > When you mentioned ioremap(), it's only for the VGA device, right? I don't > see it's used in the vfio-pci's fault handler. > >> >>> >>> See what reserve_pfn_range() does right now: it'll make sure only one owner >>> reserve this area, e.g. memtype_reserve() will fail if it has already been >>> reserved. Then when succeeded as the first one to reserve the region, >>> it'll make sure this mem type to also be synchronized to the kernel map >>> (memtype_kernel_map_sync). >>> >>> So I feel like when MMIO disabled for a VFIO card, we may need to call >>> reserve_pfn_range(), telling the kernel the mem type VFIO uses, even if the >>> pgtable will be empty, and even if currently it's always UC- for now: >>> >>> vfio_pci_core_mmap(): >>> vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); >>> >>> Then the memtype will be reserved even if it cannot be used. Otherwise I >>> don't know whether there's side effects of kernel identity mapping where it >>> mismatch later with what's mapped in the userspace via the vma, when MMIO >>> got enabled again: pgtable started to be injected with a different memtype >>> that specified only in the vma's pgprot. >> Current VFIO relies on pci_iomap() to reserve MMIO pfns as UC-, thus >> no VM_PAT in vmas. >> >> Calling remap_pfn_range() in the mmap() will cause problem to support >> dynamic faulting. Looks there's still a window even with an immediate >> unmap after mmap(). > > It can be conditionally injected. See Alex's commit (not yet posted > anywhere, only used in our internal testing so far): > > https://github.com/xzpeter/linux/commit/f432e0e46c34e493943034be4cb9d6eb638f57d1 > >> >> Also, calling remap_pfn_range() in mmap() may not meet the requirement of >> drivers to dynamic switch backend resources, e.g. as what's in >> cxl_mmap_fault(), since one remap_pfn_range() cannot reserve memtypes for >> all backend resources at once. >> >> So, is there any way for the driver to reserve memtypes and set VM_PAT in >> fault handler? > > I must confess I'm not familiar with memtype stuff, and just started > looking recently. > > Per my reading so far, we have these multiple ways of doing memtype > reservations, and no matter which API we use (ioremap / track_pfn_remap / > pci_iomap), the same memtype needs to be used otherwise the reservation > will fail. Here ioremap / pci_iomap will involve one more vmap so that the > virtual mapping will present already after the API returns. > > Then here IMHO track_pfn_remap() is the one that we should still use in > page-level mmap() controls, because so far we don't want to map any MMIO > range yet, however we want to say "hey this VMA maps something special", by > reserving these memtype and set VM_PAT. We want the virtual mapping only > later during a fault(). > > In short, it looks to me the right thing we should do is to manually invoke > track_pfn_remap() in vfio-pci's mmap() hook. > > if (!vdev->pm_runtime_engaged && __vfio_pci_memory_enabled(vdev)) > ret = remap_pfn_range(vma, vma->vm_start, pfn, > req_len, vma->vm_page_prot); > else > // TODO: manually invoke track_pfn_remap() here > vm_flags_set(vma, VM_IO | VM_PFNMAP | > VM_DONTEXPAND | VM_DONTDUMP); > > Then the vma is registered, and untrack_pfn() should be automatically done > when vma unmaps (and that relies on this patch to not do that too early). > From that POV, I still think this patch does the right thing and should be > merged, even if we probably have one more issue to fix as David Wang > reported.. Especially if it only applies to OOT drivers. Do we have report from in-tree drivers? I'd be curious how a previous truncation on a file can tear down a PFNMAP mapping -- and if there are simple ways to forbid that (if possible). -- Cheers, David / dhildenb