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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1C182C531DC for ; Fri, 16 Aug 2024 17:58:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=N887NcL0MqOj2hQysbs1Gth/iAPZml/fUaJ1WOFD+aw=; b=E7s0I661FspTAPv73RaaxZwftk UsIAsUdl0RZdfPFed8r+1IU8RxQ0ZVOF6ymh8Vjrmn6IioC46GZFKcFIWFGjllU5z3Gt5bJN7SgNV p1gliBcQxWDnlbVkUCVw7ciZ2NFSVaIUf6p2TMdHIeA5xZmUvxYBUqYvzlzqUDjisFQQOYf/IHbVq YMCVXLwZyKnMm9Cf0f1Hil6LFVXImV+0udqislLoIS6ZCY7Zpx/h0Mk0CT1QTFS7tFHZjRepM7ENe Pu1okrEwfmhT8VRH2DMEppKt85R6kaUYRg1QmAjlfknXMVQg23E/ejUcul+Gy1Bc1XIJO4OngdjkO XL3ExKUw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sf1DF-0000000DmKj-3CcB; Fri, 16 Aug 2024 17:58:09 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sf1Bm-0000000Dlu5-1nQP for linux-arm-kernel@lists.infradead.org; Fri, 16 Aug 2024 17:56:40 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723830996; 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=N887NcL0MqOj2hQysbs1Gth/iAPZml/fUaJ1WOFD+aw=; b=PFnGLFLXRn9t0R3VRXnMt9gIf0JUYnmRuyJ3vZUW7dW3hjQZQNREm7oUYxK0gYunvV8WWr a4gg8ypavkGtKleZlfDh4nBbwDWyIbJSj5kJXCOTJOMY9n+rC9K5D7+0NoApJ2uLd1+Dzq q/P5O9+BQZuG2yfI0PA6ncKSsXqzZ9o= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-437-Xf_CJV4OPey21FNofHXncQ-1; Fri, 16 Aug 2024 13:56:34 -0400 X-MC-Unique: Xf_CJV4OPey21FNofHXncQ-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-428ea5b1479so16205895e9.0 for ; Fri, 16 Aug 2024 10:56:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723830993; x=1724435793; 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=N887NcL0MqOj2hQysbs1Gth/iAPZml/fUaJ1WOFD+aw=; b=NLpkHr+pPjNkHOnTbeJnUdijoBgmhAS2qhyKt/vKArq96Z3Pq4YHTs6+fMCIV1lYmj VqlhxeYefdcG7AYFCZETApQJVj1vRLmI8WNAtEGrDoNSpibLHQnd3RmWJGKyCHZb05SE cJHgECxJqudLkraI6RUTLv1qK/Bp8zqIDFYblit/esyIJ/umnX69T+8AwcfoWIu0Duyo oxWL2obWlahCnimidqkOes/YhCTHD3kMGaddHj1ST3PMyXj4hJilNoAxl67HeGKZbbUl 0kFBgVdOl1xLB+/RFa88oZjioEib477/6YuDBCuDoizZqksnM+WWKhgOCZtHWD85jmbk Elnw== X-Forwarded-Encrypted: i=1; AJvYcCXo0SIIRQdeU2jKgzLLp/bltrhOoJVItTYkGwk3tb9ADFGaFsbHCTRJCQUfY7lN9eu3/MD/Hct+LXnpk3YWHBGkrBijQxQSLwi2BY+EafxnHzaa/jY= X-Gm-Message-State: AOJu0YwVknNTWiJSxzNfP9bTOAmhbLXw99NCOXChwmdRAeoviCRTnzap NjFsuPVuKtZVsKEDkfiaad+yh8/QTMrniGkLirfyN4TlHAFidy1CEPFMjQn4yjAGaS22Qw+tiRP y3wXPnqFgMWFBHEftDKDrGmFVX0RIWCJ1spNkfC8ZDYda4+wJGe4vKKMnx5fgaYnkNG7EROda X-Received: by 2002:a05:600c:3b04:b0:429:994:41a2 with SMTP id 5b1f17b1804b1-429ed785b80mr21318935e9.7.1723830993400; Fri, 16 Aug 2024 10:56:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHD7YlRhLCoJ0q/8oapuwa6a/Pxhf3h8xRE1yVEHeD5piFXRKbP506fq8bpnjsX6vqve4Bqdg== X-Received: by 2002:a05:600c:3b04:b0:429:994:41a2 with SMTP id 5b1f17b1804b1-429ed785b80mr21318815e9.7.1723830992802; Fri, 16 Aug 2024 10:56:32 -0700 (PDT) Received: from ?IPV6:2003:cb:c721:b900:4f34:b2b7:739d:a650? (p200300cbc721b9004f34b2b7739da650.dip0.t-ipconnect.de. [2003:cb:c721:b900:4f34:b2b7:739d:a650]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-429eea70115sm24015035e9.36.2024.08.16.10.56.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 16 Aug 2024 10:56:32 -0700 (PDT) Message-ID: Date: Fri, 16 Aug 2024 19:56:30 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 06/19] mm/pagewalk: Check pfnmap early for folio_walk_start() To: Peter Xu Cc: Jason Gunthorpe , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Sean Christopherson , Oscar Salvador , Axel Rasmussen , linux-arm-kernel@lists.infradead.org, x86@kernel.org, Will Deacon , Gavin Shan , Paolo Bonzini , Zi Yan , Andrew Morton , Catalin Marinas , Ingo Molnar , Alistair Popple , Borislav Petkov , Thomas Gleixner , kvm@vger.kernel.org, Dave Hansen , Alex Williamson , Yan Zhao References: <20240809160909.1023470-1-peterx@redhat.com> <20240809160909.1023470-7-peterx@redhat.com> <20240814130525.GH2032816@nvidia.com> <81080764-7c94-463f-80d3-e3b2968ddf5f@redhat.com> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI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zwy/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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240816_105638_589504_E51FEDFD X-CRM114-Status: GOOD ( 25.46 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 16.08.24 16:21, Peter Xu wrote: > On Fri, Aug 16, 2024 at 11:30:31AM +0200, David Hildenbrand wrote: >> On 14.08.24 15:05, Jason Gunthorpe wrote: >>> On Fri, Aug 09, 2024 at 07:25:36PM +0200, David Hildenbrand wrote: >>> >>>>>> That is in general not what we want, and we still have some places that >>>>>> wrongly hard-code that behavior. >>>>>> >>>>>> In a MAP_PRIVATE mapping you might have anon pages that we can happily walk. >>>>>> >>>>>> vm_normal_page() / vm_normal_page_pmd() [and as commented as a TODO, >>>>>> vm_normal_page_pud()] should be able to identify PFN maps and reject them, >>>>>> no? >>>>> >>>>> Yep, I think we can also rely on special bit. >>> >>> It is more than just relying on the special bit.. >>> >>> VM_PFNMAP/VM_MIXEDMAP should really only be used inside >>> vm_normal_page() because thay are, effectively, support for a limited >>> emulation of the special bit on arches that don't have them. There are >>> a bunch of weird rules that are used to try and make that work >>> properly that have to be followed. >>> >>> On arches with the sepcial bit they should possibly never be checked >>> since the special bit does everything you need. >>> >>> Arguably any place reading those flags out side of vm_normal_page/etc >>> is suspect. >> >> IIUC, your opinion matches mine: VM_PFNMAP/VM_MIXEDMAP and pte_special()/... >> usage should be limited to vm_normal_page/vm_normal_page_pmd/ ... of course, >> GUP-fast is special (one of the reason for "pte_special()" and friends after >> all). > > The issue is at least GUP currently doesn't work with pfnmaps, while > there're potentially users who wants to be able to work on both page + > !page use cases. Besides access_process_vm(), KVM also uses similar thing, > and maybe more; these all seem to be valid use case of reference the vma > flags for PFNMAP and such, so they can identify "it's pfnmap" or more > generic issues like "permission check error on pgtable". What at least VFIO does is first try GUP, and if that fails, try follow_fault_pfn()->follow_pte(). There is a VM_PFNMAP check in there, yes. Ideally, follow_pte() would never return refcounted/normal pages, then the PFNMAP check might only be a performance improvement (maybe). > > The whole private mapping thing definitely made it complicated. Yes, and follow_pte() for now could even return ordinary anon pages. I spotted that when I was working on that VM_PAT stuff, but I was to unsure what to do (see below that KVM with MAP_PRIVATE /dev/mem might just work, no idea if there are use cases?). Fortunately, vfio calls is_invalid_reserved_pfn() and refuses anything that has a struct page. I think KVM does something nasty: if it something with a "struct page", and it's not PageReserved, it would take a reference (if I get kvm_pfn_to_refcounted_page()) independent if it's a "normal" or "not normal" page -- it essentially ignores the vm_normal_page() information in the page tables ... So anon pages in pivate mappings from follow_pte() might currently work with KVM ... because of the way KVM uses follow_pte(). I did not play with it, so I'm not sure if I am missing some detail. -- Cheers, David / dhildenb