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 1E00EC7EE33 for ; Thu, 29 Aug 2024 07:46:08 +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=ARdYw+ULfprbR8rWo2jBMYSOxo9s06JO2wfsOxBjzAI=; b=1nu+ENU/9EIm8KiO72r4sSuHus z0F7flFuOFU1f7elEpmG1Yxcg15wt2RM25d8YoUfQK1sx1fZ3zaY/sTPD5RGuMMYy4U1fLZh8C8WC 1tkluKaeVuK1snchsWFvB6HZAqeCEMjfK99XSHiXkrQxbakatr5c3mlnFPPb8LOwzkLCr1cfHFQ7t MeYc4gDWQYqe0EuQ9UX1oWltCW1a43UWzZqXLp3Ij1xY03hWLRrhacwvrEI7OSI6U/Udj5lNlpNSs wROQLTf47nsQviOW77Me2LhaU2jp1SpFqrdAXzBdOOVr2dHm8JHS1oUDuXwL6LbmCllwPXTWY+J0m SOKK7gTQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sjZqv-000000010nW-3ZKu; Thu, 29 Aug 2024 07:45:57 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sjYlA-00000000nmU-2rAI for linux-arm-kernel@lists.infradead.org; Thu, 29 Aug 2024 06:35:58 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724913355; 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=ARdYw+ULfprbR8rWo2jBMYSOxo9s06JO2wfsOxBjzAI=; b=FDTEIrDCBhF0Hfs+As0xgNZ7PiycpF1dOaLha9+aLdQ+gNlnvWdC/OFzpp9t7r2C7Y+7o1 zqQMj6EGgwYJyBJ0Yn6p/JOzLDknotAu1WNCgCKxVbBV1pQ1PvsG0+h4ZxEhaB0JA66tsb hjrWZchKJA47OteBcVBm5fBks2aNPzA= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-110-e-ZgBst7NMiwpSh6Pk3vxQ-1; Thu, 29 Aug 2024 02:35:54 -0400 X-MC-Unique: e-ZgBst7NMiwpSh6Pk3vxQ-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-371b187634fso625796f8f.0 for ; Wed, 28 Aug 2024 23:35:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724913353; x=1725518153; 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=ARdYw+ULfprbR8rWo2jBMYSOxo9s06JO2wfsOxBjzAI=; b=mMGmVY5xOx3Iu6XKxBpvxn8R6vvlblLyZFl2YWXgloxKZQImElNTvqHHPnpn1/YLbW Un/+mh7+G7WaZCw/9MFuyND0cI6BDRhZTgl5kvnCk20N97WuJe6t3LJGpQMaxaxuAAnd 28qRD7UH3SLw2Duln5icHs1HxOqvKZMavuZaiH1t8f52xs+q2eoOHKOPz7qhfzXHCFjo CYir32Lbbj1gdp0g0/9JE0Y3Bmdwg8VD8g5JvmUYMvLVRX+OmP1tUSfZw5B1qvvixD7I Ghgrric9Z0lOM/deb9MlW6c4lxAb98RgsjqgM/SJPhFZnFr2bVc8v2Zlh5IWK2oLsaMQ z/gQ== X-Forwarded-Encrypted: i=1; AJvYcCUlzc7ZG11b8veOgTrOjJS9XDe7cXz4B/JhariG+fuGU9PTYnI/3e6E906dKLkMYF+tMJ7Ia+8fVRb0XAdP57Yv@lists.infradead.org X-Gm-Message-State: AOJu0YxxZXAmGkAhlM98YeRZUP43XMr/lfqz6NRRm98PGwzHDlS/QRr4 LD1QzPDVarEMwTq9BxTNFyCCfhW5DdW+z6Atv9nkDUuFx1eE2fXL1riibqiPCDh/AghBIwTZEcn zm8qTHjebEkjvl0kSNWsCPMhNwI0lAGHYiO0RXhweP1+DwlxdIheAJTQnMVvbKRoZgphjzEGX X-Received: by 2002:a5d:6dc9:0:b0:36b:aa96:d1e5 with SMTP id ffacd0b85a97d-374a0228f7fmr388998f8f.18.1724913352813; Wed, 28 Aug 2024 23:35:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE+BYnHXIYm3/KvEc5f71yqUBdVMg4j3LgSlWxLc6pR3RHwNPaLqwMOSo5yMutRBM4U2UC/rA== X-Received: by 2002:a5d:6dc9:0:b0:36b:aa96:d1e5 with SMTP id ffacd0b85a97d-374a0228f7fmr388949f8f.18.1724913351919; Wed, 28 Aug 2024 23:35:51 -0700 (PDT) Received: from ?IPV6:2003:cb:c711:c600:c1d6:7158:f946:f083? (p200300cbc711c600c1d67158f946f083.dip0.t-ipconnect.de. [2003:cb:c711:c600:c1d6:7158:f946:f083]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3749efafb30sm581789f8f.94.2024.08.28.23.35.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 Aug 2024 23:35:51 -0700 (PDT) Message-ID: <2123f339-2487-4b1c-abb1-313e9a012242@redhat.com> Date: Thu, 29 Aug 2024 08:35:49 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 06/19] mm/pagewalk: Check pfnmap for folio_walk_start() To: Jason Gunthorpe , Peter Xu Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Gavin Shan , Catalin Marinas , x86@kernel.org, Ingo Molnar , Andrew Morton , Paolo Bonzini , Dave Hansen , Thomas Gleixner , Alistair Popple , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Sean Christopherson , Oscar Salvador , Borislav Petkov , Zi Yan , Axel Rasmussen , Yan Zhao , Will Deacon , Kefeng Wang , Alex Williamson References: <20240826204353.2228736-1-peterx@redhat.com> <20240826204353.2228736-7-peterx@redhat.com> <9f9d7e96-b135-4830-b528-37418ae7bbfd@redhat.com> <20240828234652.GD3773488@nvidia.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 WhVhdl0q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BL6T9b+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: <20240828234652.GD3773488@nvidia.com> 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-20240828_233556_837394_2BCAF505 X-CRM114-Status: GOOD ( 31.62 ) 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 29.08.24 01:46, Jason Gunthorpe wrote: > On Wed, Aug 28, 2024 at 03:45:49PM -0400, Peter Xu wrote: > >> Meanwhile I'm actually not 100% sure pte_special is only needed in >> gup-fast. See vm_normal_page() and for VM_PFNMAP when pte_special bit is >> not defined: >> >> } else { >> unsigned long off; >> off = (addr - vma->vm_start) >> PAGE_SHIFT; >> if (pfn == vma->vm_pgoff + off) <------------------ [1] >> return NULL; >> if (!is_cow_mapping(vma->vm_flags)) >> return NULL; >> } >> >> I suspect things can go wrong when there's assumption on vm_pgoff [1]. At >> least vfio-pci isn't storing vm_pgoff for the base PFN, so this check will >> go wrong when pte_special is not supported on any arch but when vfio-pci is >> present. I suspect more drivers can break it. Fortunately, we did an excellent job at documenting vm_normal_page(): * There are 2 broad cases. Firstly, an architecture may define a pte_special() * pte bit, in which case this function is trivial. Secondly, an architecture * may not have a spare pte bit, which requires a more complicated scheme, * described below. * * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a * special mapping (even if there are underlying and valid "struct pages"). * COWed pages of a VM_PFNMAP are always normal. * * The way we recognize COWed pages within VM_PFNMAP mappings is through the * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit * set, and the vm_pgoff will point to the first PFN mapped: thus every special * mapping will always honor the rule * * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT) * * And for normal mappings this is false. * remap_pfn_range_notrack() will currently handle that for us: if (is_cow_mapping(vma->vm_flags)) { if (addr != vma->vm_start || end != vma->vm_end) return -EINVAL; } Even if [1] would succeed, the is_cow_mapping() check will return NULL and it will all work as expected, even without pte_special(). Because VM_PFNMAP is easy: in a !COW mapping, everything is special. > > I think that is a very important point. > > IIRC this was done magically in one of the ioremap pfns type calls, > and if VFIO is using fault instead it won't do it. > > This probably needs more hand holding for the driver somehow.. As long as these drivers don't support COW-mappings. It's all good. And IIUC, we cannot support COW mappings if we don't use remap_pfn_range(). For this reason, remap_pfn_range() also bails out if not the whole VMA is covered in a COW mapping. It would be great if we could detect and fail that. Likely when trying to insert PFNs (*not* using remap_pfn_range) manually we would have to WARN if we stumble over a COW mapping. In the meantime, we should really avoid any new VM_PFNMAP COW users ... > >> So I wonder if it's really the case in real life that only gup-fast would >> need the special bit. It could be that we thought it like that, but nobody >> really seriously tried run it without special bit yet to see things broke. > > Indeed. VM_PFNMAP for sure works. VM_MIXEDMAP, I am not so sure. The s390x introduction of pte_special() [again, I posted the commit] raised why they need it: because pfn_valid() could have returned non-refcounted pages. One would have to dig if that is even still the case as of today, and if other architectures have similar constraints. > > What arches even use the whole 'special but not special' system? > > Can we start banning some of this stuff on non-special arches? Again, VM_PFNMAP is not a problem. Only VM_MIXEDMAP, and I would love to see that go. There are some, but not that many users ... but I'm afraid it's not that easy :) -- Cheers, David / dhildenb