vm/vm_page.c: add vm_page_getfake_nowait() vm/vm_page.c: add vm_page_freefake() vm/vm_mmap.c: kern_mincore(): do not spin on a page without object A managed page might stay mapped without belonging to any object, e.g. the real page mapped through a fake page owned by a device object. Then with vm.mincore_mapped=0, kern_mincore() loops forever. If the page has no object, retry only when the pte changed. Otherwise, report the state of the current pmap only, skipping the MINCORE_*_OTHER bits that require the object lock. vm_page: add a VPO flag to mark the fake page owned by the object Owned fake pages can't use the normal vm_page_drop() path because the base ref_count of 1 set by vm_page_initfake() keeps them alive. The vm_fault() and consumers of pmap_remove_all() redirect to the real page serving the fake page's physical address. linuxkpi: lkpi_vmf_insert_special_pte_locked() Add a helper to map a pfn into a vma backed by an OBJT_MGTDEVICE object without taking the real page away from its owner, similar to Linux special ptes for VM_PFNMAP mappings. The existing lkpi_vmf_insert_pfn_prot_locked() renames the real page into the device object, which fails if the page is mapped elsewhere, and worse, removes the page from its original object. The function inserts a fake page owned by the object (VPO_FAKE_OWNED), with the physical address of the requested pfn. vm_fault() maps the real page backing that address instead, so the mapping is managed with the PV entry on the real page, and unmap_mapping_range() and zap_vma_ptes() revoke it. The fake page is freed when it is removed from the object. This mechanism de facto implements page aliasing. To complete it, page hooks that automatically destroy the alias on real page reclamation, and reinstantiate the alias on re-insertion, would be needed.
Details
- Reviewers
markj
Diff Detail
- Repository
- rG FreeBSD src repository
- Lint
Lint Skipped - Unit
Tests Skipped
Event Timeline
Then the following is needed for drm-kmod:
diff --git a/drivers/gpu/drm/i915/i915_mm.c b/drivers/gpu/drm/i915/i915_mm.c index d1867ddd73..20406fba3e 100644 --- a/drivers/gpu/drm/i915/i915_mm.c +++ b/drivers/gpu/drm/i915/i915_mm.c @@ -126,7 +126,7 @@ static int remap_pfn(pte_t *pte, unsigned long addr, void *data) set_pte_at(r->mm, addr, pte, pte_mkspecial(pfn_pte(r->pfn, r->prot))); #elif defined(__FreeBSD__) vm_fault_t ret; - ret = lkpi_vmf_insert_pfn_prot_locked(r->vma, addr, r->pfn, r->prot); + ret = lkpi_vmf_insert_special_pte_locked(r->vma, addr, r->pfn, r->prot); if ((ret & VM_FAULT_OOM) != 0) return -ENOMEM; if ((ret & VM_FAULT_ERROR) != 0)
I will look at this soon, I just need to resurrect a laptop so that I can easily test i915 changes without taking down my main workstation.
So let me ask: is this related to the "fictitious pages" mentioned in D58320? If so, how is the LinuxKPI part to be written/work in the future world order with a real 'struct page'?
In some sense, yes, it is related. So far I do not know how that proposal about 'real struct page' would work at all. But I do not think that this change (in whatever form after debugging and reviewing it) would have any additional effect on the 'real struct page'.
(Still working on testing this. If I test drm-6.12 on my laptop, I get a corrupted display with or without this patch. After retesting drm-6.6 it seems there is some recent regression in main, as I now see similar symptoms there too, so I am bisecting that instead, hopefully will figure it out soon.)
Free allocated fake pages.
Add some comments explaining the intent.
Avoid going through the vm_page_array[] at all, the page physical address is all that is needed for a fake page construction.
In its current form (the linuxkpi/vm/drm-kmod changes), https://reviews.freebsd.org/D59883 can't resolve the ownership/reclamation bug, but it's possible you already know it.
The LLM told me, "I found a concrete omission in the current published combination: it leaves the affected CPU-mapping path using the old, ownership-changing helper. I checked the live review’s latest revision, diff 190340, including the accompanying drm-kmod change. That driver change switches remap_pfn() to lkpi_vmf_insert_special_pte_locked(). D59883 (https://reviews.freebsd.org/D59883?id=190340). However, the graphics-buffer mapping path involved in our ownership problem is:
vm_fault_cpu()
→ remap_io_sg()
→ remap_sg()
→ lkpi_vmf_insert_pfn_prot_locked()remap_sg() is a separate function, and the posted patch leaves it unchanged. I verified this by applying the accompanying patch to a scratch copy of upstream drm-kmod. This path never passes through the modified remap_pfn(). CPU fault handler (https://github.com/freebsd/drm-kmod/blob/c16de0f9fa84f70b9551f69fde837a7f3a73c0e9/drivers/gpu/drm/i915/gem/i915_gem_mman.c#L329), mapping callbacks (https://github.com/freebsd/drm-kmod/blob/c16de0f9fa84f70b9551f69fde837a7f3a73c0e9/drivers/gpu/drm/i915/i915_mm.c#L89). Consequently, the old helper can still discard a page’s swap backing, remove it from its original shmem object, and insert it into the mapping’s object. Later reclamation can therefore still lose contents that the graphics buffer needs to retain. D59883 adds the new helper without removing that behavior from the old one. Existing LinuxKPI implementation (https://github.com/freebsd/freebsd-src/blob/97d0c19e6af78dc7613563805653d13f03861737/sys/compat/linuxkpi/common/src/linux_page.c#L539). This is a definite reason the published combination leaves the ownership bug reachable."
Yes. Replace lkpi_vmf_insert_pfn_prot_locked() with lkpi_vmf_insert_special_pte_locked() in remap_io_sg() too. It appeared with GTT MMAP protocol v4 and was a main reason of backing ownership changing. It is in drivers/gpu/drm/i915/i915_mm.c line 102.
Well why did not you patched the other place too, then?
I think the following would do it, not even compiled.
diff --git a/drivers/gpu/drm/i915/i915_mm.c b/drivers/gpu/drm/i915/i915_mm.c index 711779c868..78fc3597d6 100644 --- a/drivers/gpu/drm/i915/i915_mm.c +++ b/drivers/gpu/drm/i915/i915_mm.c @@ -98,8 +98,8 @@ static int remap_sg(pte_t *pte, unsigned long addr, void *data) set_pte_at(r->mm, addr, pte, pte_mkspecial(pfn_pte(sgt_pfn(r), r->prot))); #elif defined(__FreeBSD__) - vm_fault_t ret = - lkpi_vmf_insert_pfn_prot_locked(r->vma, addr, sgt_pfn(r), r->prot); + vm_fault_t ret = lkpi_vmf_insert_special_pte_locked(r->vma, addr, + sgt_pfn(r), r->prot); if ((ret & VM_FAULT_OOM) != 0) return -ENOMEM; if ((ret & VM_FAULT_ERROR) != 0) @@ -126,7 +126,7 @@ static int remap_pfn(pte_t *pte, unsigned long addr, void *data) set_pte_at(r->mm, addr, pte, pte_mkspecial(pfn_pte(r->pfn, r->prot))); #elif defined(__FreeBSD__) vm_fault_t ret; - ret = lkpi_vmf_insert_pfn_prot_locked(r->vma, addr, r->pfn, r->prot); + ret = lkpi_vmf_insert_special_pte_locked(r->vma, addr, r->pfn, r->prot); if ((ret & VM_FAULT_OOM) != 0) return -ENOMEM; if ((ret & VM_FAULT_ERROR) != 0)
Just in case, there are really two patches, one for src, another for drm. You have them both, right?
Hmm, I think now that if the problem is _GPU_ hanging then this patch cannot have any influence on it GPU does not share page tables with CPU, and the patch only modifies how the aliased mappings are established. There is something else going on.
This does not mean that the patch is not needed, but it is not about GPU seeing wrong memory.
Yes I have patched drm-kmod: https://github.com/emaste/drm-kmod/commit/b686b798555c4faf4d20673f167b6559ef841c6e
This is in my WIP tree which has many other changes; I can try to reproduce on just main + these patches later on.
I think more productive would be to try to analyze the state of the GPU when it hangs. I suppose the current drm driver has some way to dump the state? Since you are saying that only GPU hangs, it should be possible to access machine though a network.
Since you are saying that only GPU hangs, it should be possible to access machine though a network.
I'm not sure how drm attempts recovery after a GPU hang, but in fact I can Ctrl-Alt-F1 and the console will come back after some time, then I can ^C the X server and start it again.
There should be two 'files' created by the detected hang:
debugfs_create_file("i915_error_state", 0644, minor->debugfs_root, i915,
&i915_error_state_fops);
debugfs_create_file("i915_gpu_info", 0644, minor->debugfs_root, i915,
&i915_gpu_info_fops);Is debugfs converted to sysctls on FreeBSD? Anyway, these are the things to look at, they were the useful source of data when I debugged i915 in drm2 times.
No you mount debugfs as a FS. @emaste in case you need the coredump bits I updated the review recently as some things in sys/kern had changed.
The LLM told me, "I examined the minidump from the Meteor Lake test on amd64. The kernel was FreeBSD main at 3ccfcdacd6ae with diff 190340 and INVARIANTS enabled. The driver was drm-kmod 98be24dc9994 with emaste’s companion change b686b798 applied to both remap_sg() and remap_pfn().
During low-memory reclamation, the i915 GEM shrinker tried to revoke a write-combined CPU mapping of a 2 MiB buffer. The stack went from __i915_gem_object_put_pages() through i915_gem_object_release_mmap_offset(), drm_vma_node_unmap(), and lkpi_unmap_mapping_range() into the managed-device pager. The panic was pmap_remove_all: page … is not managed.
The descriptor passed to pmap_remove_all() was the placeholder that lkpi_vmf_insert_special_pte_locked() creates for the mapping. At page index 0 in the OBJT_MGTDEVICE object, it had PG_FICTITIOUS set and oflags = 0x14 (VPO_UNMANAGED | VPO_FAKE_OWNED). In sys/vm/device_pager.c, cdev_mgtdev_pager_free_page() calls pmap_remove_all(m) unconditionally, before it checks VPO_FAKE_OWNED and reaches vm_page_putfake(m). The amd64 implementation of pmap_remove_all() asserts that its argument is managed. Passing the unmanaged placeholder to it accounts for the panic.
I checked the real backing pages separately. The placeholder and the first real page described the same physical frame through distinct descriptors. All 512 real pages still belonged to this buffer’s own filp->f_shmem swap object, at their expected page indices, with all VM validity bits set. The swap counters showed zero pages swapped in or out during this boot. This establishes a cleanup failure while backing-page ownership was intact for this buffer. Whether the same defect explains the recoverable GPU hangs reported earlier in the review remains open."
The full panic log would be more useful. But I think I see what is the problem.
So really just the 'fake' pte cannot work with our vm, it requires the full aliasing support. Most interesting pmap operations like pmap_remove_all() (which was the reason for the managed device pager) need working PHYS_TO_VM_PAGE().
| sys/vm/vm_object.c | ||
|---|---|---|
| 2662 | this one did not apply | |
| sys/vm/vm_object.c | ||
|---|---|---|
| 2662 | Indeed, sorry. Ignoring this chunk is the right thing to do there. | |