From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A86278F2E for ; Tue, 14 Apr 2026 07:36:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776152211; cv=none; b=qa4XoQV4XnkOT5wa16vVSwUkIupI9aB75kW8kWT1ZIyTO3u7yIh6Qb4CHdy6yhDXgC+G1q1Y6DH58exvSdOEwVqEeHluVq9jPzIgfdVt0xhw494spuUlEVTBz9CimlmNoC+QJmZ2ys8/Tjvk/FaZCamInhXzto2TPFhNEcPbRMI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776152211; c=relaxed/simple; bh=rSNBpuok1a3fiCy60Hvt+1KClM7ZWttTGpKJeYuoCoU=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=e2Omxbc9WdlxXYN59wcn19BC44nrtO4vZbIqKiS0rVTQRAyNTDtsmHZX3Zd1FJj34NX+OeOFitgwLU15HY08lnD0svrWZhysDpLOcaWLDvlyt88GAQFED9PGPfZZOB26RNRX28iPDbtbhr8mH09zjsVCFfOKCHjmy+Bxta4y7ts= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=AlHOb1U1; arc=none smtp.client-ip=209.85.215.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AlHOb1U1" Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-c70e27e2b74so1996389a12.0 for ; Tue, 14 Apr 2026 00:36:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776152210; x=1776757010; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=/0ogmPgdwUipHOmKcnr8RlOytrha5m2as46j8FW3cr8=; b=AlHOb1U1Ri5GDDlAfNYguFxsXqUbopwsbTlCWhPFyqxVB4hlvjdn+5UMfsSNq6Ref6 O/TbXeX+al0celP20n5a8gBwcvZw2kBfQImuXTnjPUrW6lHBzvlxNL2i1wmQXYINU+DW dgJHaWqAf1+tuhRnHydkygESikOpEhbgzBRZvPeQEyN1EZUupt4HlFg6dANM6bAlf2pC Xe/rDV+f4i/3E/aHXXHTxz8Z5PixhUCI6vk1WIwaAXka93K6ij7fkSrkjvc921pqc1h9 SxmW9+uPggq4rzxd+SwxGJTQmEgLzdgWIk1MLNRu3ZTjmjhnFjlw4UPOOSkZ4s8Srz+W UuOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776152210; x=1776757010; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/0ogmPgdwUipHOmKcnr8RlOytrha5m2as46j8FW3cr8=; b=kRQ9/alnBOA7bgW2x56Yyy9PdY/pwy7bnqhCY8y1RX2vi4F65hQzTWZCUkTLq0tU7f 8OE8F+EAhuRBe9/w5MbS6Nannq4ibMUOKznHqA4tAff2lM/QYBobbsC7ad+qBbSawTyG yN8Bb1m0e3AJVXNtoRqU/Vb/8GNW3jfVTn6xCgj815bEWUApxcOIVMRx76YcYasYnZ6Q oeaczNqVilclS/DDuzEqWWyMafBB3f1t6FrGyKjp0MeAhtM7atsRf34pau4RaCt6HKE8 dfhEBYYcI3pzPv/kl1oDijrNImog+7JnTdf9goD119yyL3KzGM7p8q3Lbt7URpBdGFB8 6ZIQ== X-Forwarded-Encrypted: i=1; AFNElJ93sbtfSMk5kdGBSUp8ImiCogXzekNBQQokTsdlvmhN9PRXwm9KzZVMU8kxrAl2nJRPGk3i2+kR9lC7I4E=@vger.kernel.org X-Gm-Message-State: AOJu0YzlwhHDUrDZcgXfToWgSfNin9C9Q3zjA8feOIs/akwsVmeJIhWQ 3ekQUOuAMm3zLW9CBkBROORgnd9UJnB4NyAL5wOFXxoUrX0ZOlZEVXnE X-Gm-Gg: AeBDieu0KaxyPAA/MelscQeZ1lazXRgzxbwdYA6PajZRUqoKRNS2fYMg1Sz058tCZcF 4LMH2AKAlWXbYSFLzaNAFwcLsUhSA6zCjWk1AvgHCNtvy7mCOH53uh4gKkpMjgm2vwqfsZ0vhYE d8rHY02MWmPDoOBUH/u7ieDl6r9cCRLIjrmPQVyQTwLb/gw5vg4FmAkBtTBOPug8XMJ3fXxQSU5 qEYQIWHJYUnB79o7reJhMr7wvfBOmCeRI7CcNDR5ph+5hs91noUQtXEv+mIPrlx8btChtS8ZyyN gJCEduZPZg/QEIF6thYRh3LE8lMyOn8iGsSgOO+aPUyx1Hoy5yZHAnHy4LRlXjK1WiVC2VR1AhW ewYODFNYHGn6Uim3Rnv45UmUgYiy2oARjV8KQrhi4qfQ/cc7EtqN+HIrdC7VGkYzmkuzv6xZvVY iqWOlgznTOcQr24QbuIxBNQo9oVR5AyhF0+MAfNnW48uG/l/kQXq/W9BL5D7NfcXPp0Z7ND7jd3 W/i+r7j42ouYMAhHCj6beAG5vdxka2HcJwA9ZbIjlC1mM9es/wJtw== X-Received: by 2002:a17:903:2785:b0:2b2:45b7:306e with SMTP id d9443c01a7336-2b2d593683cmr115063175ad.3.1776152209649; Tue, 14 Apr 2026 00:36:49 -0700 (PDT) Received: from [127.0.0.1] (211-76-176-101.dynamic-ip.pni.tw. [211.76.176.101]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b2d4f3b299sm169494335ad.73.2026.04.14.00.36.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 14 Apr 2026 00:36:49 -0700 (PDT) Message-ID: <2f29f66b-46db-4925-b922-4add61b633bf@gmail.com> Date: Tue, 14 Apr 2026 15:36:39 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 13/13] mm/huge_memory: add and use has_deposited_pgtable() To: Lorenzo Stoakes , David Hildenbrand Cc: Andrew Morton , Zi Yan , Baolin Wang , "Liam R . Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Kiryl Shutsemau , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <41b1ff54-c120-42ae-8b74-54767abf3554@gmail.com> Content-Language: en-US From: Yin Tirui In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi Lorenzo and David, Sorry for the late reply. On 4/7/26 18:48, Lorenzo Stoakes wrote: > On Thu, Apr 02, 2026 at 03:49:35PM +0800, Yin Tirui wrote: >> >> >> On 4/2/26 14:46, Lorenzo Stoakes (Oracle) wrote: >>> >>> I mean you would have needed to handle this case in any event, since this change >>> is strictly an equivalent reworking of zap_huge_pmd(). >>> >>> But it seems that doing so has clarified the requirements somewhat here :) >>> >>> I haven't had a look at that series yet (please cc this email if you weren't >>> already, I do filter a lot of stuff due to how much mail I get daily) >> >> Hi Lorenzo, >> >> Thanks for the quick reply. I will definitely CC you on the v4 series. > > Thanks. > >> >>> >>> So if this is a PMD leaf entry it will be present and PFN map, so I'd have >>> thought simply adding: >>> >>> /* Huge PFN map must deposit, as cannot refault. */ >>> if (vma_test(vma, VMA_PFNMAP_BIT)) >>> return true; >>> >>> Would suffice? >> >> Here is the dilemma: >> >> Currently, VFIO uses vmf_insert_pfn_pmd() to create huge pfnmaps on page >> faults. This sets VM_PFNMAP in vfio_pci_core_mmap(), but it does not >> deposit a pgtable (unless arch_needs_pgtable_deposit() is true). > > Hmmm... it's only the VFIO and hyperv drivers using this. > > Wouldn't we generally want a deposited huge page here now we're allowing huge > PFN maps? > > Or are this _special cases_ where we have a PMD-sized entry but are not > necessarily wanting to treat it as THP? > > This is a real wrinkle in this whole series no? > > David - any thoughts? > >> >> To resolve this, >> >> Option A: Force VFIO (vmf_insert_pfn_pmd) to also deposit pgtables. This >> unifies the VM_PFNMAP lifecycle. However, since VFIO can refault, >> depositing pgtables here incurs unnecessary memory overhead. > > How can VFIO refault as a PFN mapping? Does it intentionally sometimes > clear PTE entries to effect a refault, and implement a custom fault > handler? > > I guess having a fault handler makes it refaultable... > > I mean obviously that then contradicts the suggested comment above :) > > That seems to me to cast a bit of a question over the whole series - having > PMD mappings that are _sometimes_ THP and _sometimes_ not is weird (TM). > > And it'd suck to add - yet another very specific check - to determine if we > do, in fact, assume THP for a PMD sized PFN map. Yes, exactly. VFIO and Hyper-V rely on their custom `.fault` handlers to dynamically build mappings. In contrast, `remap_pfn_range()` establishes static pre-mappings. > >> >> Option B: Introduce a new VMA flag set during remap_pfn_range(), which >> we can explicitly check in has_deposited_pgtable(). > > Yeah would rather not, that feels like a hack. Agreed. > >> >> Option C: Check vma->vm_ops->fault (and huge_fault). We would only >> deposit pgtables for mappings without fault handlers. However, this is >> fragile because a driver might still register a .fault() handler that >> simply returns VM_FAULT_SIGBUS. > > I mean again this is yet another check (TM). But probably the most preferable I > think. > > Wouldn't a driver doing that be being somewhat redundant? E.g. in do_fault(); > > if (!vma->vm_ops->fault) { > vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd, > vmf->address, &vmf->ptl); > if (unlikely(!vmf->pte)) > ret = VM_FAULT_SIGBUS; > > And so can expect maybe some more redundancy if they also happen to map > PMD-sized ranges? :) > > And the only two callers of vmf_insert_pfn_pmd() - hyperv and VFIO both > implement actual fault handlers anyway. > > So I think this is fine? > I agree. David, since Lorenzo also asked for your thoughts on the overall design aspect ("sometimes THP and sometimes not"), what is your opinion on this? Should we proceed with checking `!vma->vm_ops->fault` to differentiate the deposit behavior for huge PFNMAPs? >> >> Do you have a preference among these, or perhaps another idea? >> >>> >>> By the way, I am wondering if the prot bits are correctly preserved on page >>> table deposit, as this is key for pfn map (e.g. if the range is uncached, for >>> instance). That's something to check and ensure is correct. >>> >>> I _suspect_ they will be, as we have pretty well established mechanisms for that >>> (propagate vma->vm_page_prot etc.) but definitely worth making sure. >>> >> >> Yes, they are correctly preserved! >> >> During a PMD split in __split_huge_pmd_locked(), we populate the >> deposited pgtable like this: >> >> entry = pfn_pte(pmd_pfn(old_pmd), pmd_pgprot(old_pmd)); >> set_ptes(mm, haddr, pte, entry, HPAGE_PMD_NR); >> >> The newly refactored pmd_pgprot() correctly extracts the exact >> protection bits (including crucial cache modes like UC/WC for device >> memory) from the huge PMD, strips the hardware-specific huge bit, and >> returns a pure PTE-level pgprot_t. > > OK good :) > >> >>>> >>>> [1] >>>> https://lore.kernel.org/linux-mm/20260228070906.1418911-5-yintirui@huawei.com/ >> >> -- >> Yin Tirui >> > > Cheers, Lorenzo -- Yin Tirui