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 BE4D6C87FD3 for ; Thu, 29 Aug 2024 18:46:26 +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-Type:In-Reply-To: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=SNxKnt1pOeKeNbmB5uGX+KLSDH8VwXu6pBqcboAJCGg=; b=ZdkehOXg9b7EQqhyxTWFWRzx8w sEQUa3MjpwNl5IYm48h5gA3l4Wv5jaJ6jaiZvbpwEPE0qkgpHaKo9XzQ/K7MzVf+lo7KSSthuqx06 Iqpf+ty67kTfhCn0OdD5uUdlEiKHj3o40j6NLSjtIiHDB/iEm13/3UfBn9E2X6Lme7lnCWSkDtHvo mQ4XmJL6Clo6bXU5l+rn6uVCo+HwOG5IfcgWf4mpjcIzCHtrIhMwxI2IniNHXIXmvdE07Zy07e2+l uqiXo6DlhixWsbqIC4zMmRIhRawcxPbJrbf8L8bEPtsEuotvhj4recb2DIMTwF4I/LoRrmiOBs5nR XDjpD+Rg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sjk9w-00000003FAK-3aHH; Thu, 29 Aug 2024 18:46:16 +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 1sjk95-00000003EzG-24qh for linux-arm-kernel@lists.infradead.org; Thu, 29 Aug 2024 18:45:24 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724957122; 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: in-reply-to:in-reply-to:references:references; bh=SNxKnt1pOeKeNbmB5uGX+KLSDH8VwXu6pBqcboAJCGg=; b=WlYJD/IBCFfuI2L4ON1TkXF/ETWwhjlMXM+jKgRVi3rwdieP6NAzXgS+MhOFUN61qeh4qE TCf77tY4MUmd6CipbRclmX2oYWuAZH7zvqN/glfrYFe+NRtLCrmQQJZVlKhLBkH7ar++jk Vugr3q/utYPPZdGmEb/FlYxdHo+CHwY= Received: from mail-ot1-f69.google.com (mail-ot1-f69.google.com [209.85.210.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-331-vuwKHfwVMF-nYYQ3bjSZAQ-1; Thu, 29 Aug 2024 14:45:21 -0400 X-MC-Unique: vuwKHfwVMF-nYYQ3bjSZAQ-1 Received: by mail-ot1-f69.google.com with SMTP id 46e09a7af769-709334cc7f1so1380226a34.0 for ; Thu, 29 Aug 2024 11:45:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724957120; x=1725561920; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=SNxKnt1pOeKeNbmB5uGX+KLSDH8VwXu6pBqcboAJCGg=; b=WVJ/mt7NqUDeDNrrGe7+CDxAUxBa/jtl/mu/J+k5JdlSxtUqlWusEzUzHLoO+XZcJa IJh1W71BmkCI1noviRN1WMAk76b1+eWS/jj/fk/p4IImlVicbliU8Db4yGqHFzRNa4Hc CLKWQojgPS0vyqbc+vuRIXGIg+D/AcBQkAl7io18o4ijzCsZatRWR/ewM7/54B1/vQ4q 9MBRqw0HqpDYKIAIa52QXDziSNxgX0ay4My/2nwx3YHq+uM+HWluouokunWj6wFBGsdm KlRyHbZV88QGpE1lyB1qFFoVqaKs8vHYIP5v8rnwfG1Zq+anAvxjELiKdnubpb6NbEU0 hDZw== X-Forwarded-Encrypted: i=1; AJvYcCU62fFLqdtF7JRhtSdZTl3GWblJH75M8GehXsvsjf/cc/wZ1DbFzN7OSYbypiyRA9Efppocb/9veQFNK3EbilRL@lists.infradead.org X-Gm-Message-State: AOJu0YwBr6kGpDbWYw4Wh4t7tfK3P5XtOO0Ab7miloTXpikkDJR1x9S3 q61g66+PCuNpkD7T5wu5Lk/TjLCab8JSn0Hw3RVoqndisfD3TD6rx7LrQjFz7+6EpoQ6m6VkZqS 7lzXMDWUfObAiExeL6i2BGyUXk/f2DB1PZIzut6jMRMH/kLj/eQwCYyt0TDdfgqxx8dmbwP3H X-Received: by 2002:a05:6830:4393:b0:70d:f23d:97fe with SMTP id 46e09a7af769-70f5c4b40admr3551773a34.33.1724957120526; Thu, 29 Aug 2024 11:45:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHzQ8QvrwiOwTqzCT7fw2y797mlGiwpfpRY2dlwgwi44n8PyvYSGtPb7e0yosXO2yQoNYYadA== X-Received: by 2002:a05:6830:4393:b0:70d:f23d:97fe with SMTP id 46e09a7af769-70f5c4b40admr3551752a34.33.1724957120217; Thu, 29 Aug 2024 11:45:20 -0700 (PDT) Received: from x1n (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c340bfa6d0sm7797286d6.1.2024.08.29.11.45.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Aug 2024 11:45:19 -0700 (PDT) Date: Thu, 29 Aug 2024 14:45:16 -0400 From: Peter Xu To: David Hildenbrand Cc: Jason Gunthorpe , 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 Subject: Re: [PATCH v2 06/19] mm/pagewalk: Check pfnmap for folio_walk_start() Message-ID: References: <20240826204353.2228736-1-peterx@redhat.com> <20240826204353.2228736-7-peterx@redhat.com> <9f9d7e96-b135-4830-b528-37418ae7bbfd@redhat.com> <20240828234652.GD3773488@nvidia.com> <2123f339-2487-4b1c-abb1-313e9a012242@redhat.com> MIME-Version: 1.0 In-Reply-To: <2123f339-2487-4b1c-abb1-313e9a012242@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240829_114523_630391_84E289AD X-CRM114-Status: GOOD ( 24.70 ) 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 Thu, Aug 29, 2024 at 08:35:49AM +0200, David Hildenbrand wrote: > 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(). IMHO referencing vm_pgoff is ambiguous, and could be wrong, if without a clear contract. For example, consider when the driver setup a MAP_PRIVATE + VM_PFNMAP vma, vm_pgoff to be not the "base PFN" but some random value, then for a COWed page it's possible the calculation accidentally satisfies "pfn == vma->vm_pgoff + off". Then it could wrongly return NULL rather than the COWed anonymous page here. This is extremely unlikely, but just to show why it's wrong to reference it at all. > > Because VM_PFNMAP is easy: in a !COW mapping, everything is special. Yes it's safe for vfio-pci, as vfio-pci doesn't have private mappings. But still, I don't think it's clear enough now on how VM_PFNMAP should be mapped. -- Peter Xu