Skip to contentSkip to conversation

drm/ttm: set TTM allocated pages as reserved

PATCH In review Christian König 4 participants 13 messages Mar 29, 2023 – Mar 30, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/ttm: set TTM allocated pages as reserved

KVM tries to grab references to pages in VMAs marked with VM_PFNMAP.

This is illegal and can cause data corruption with TTM pages because only some of them are actually reference counted.

Mark all pages allocated by TTM as reserved, this way KVM handles the PFNs like they would point to MMIO space.

This still results in a warning, but at least no other problem.

---
 drivers/gpu/drm/ttm/ttm_pool.c | 62 ++++++++++++++++++++--------------
 1 file changed, 36 insertions(+), 26 deletions(-)
drivers/gpu/drm/ttm/ttm_pool.c +36 -26
index aa116a7bbae3..c665a8bf366a 100644
@@ -82,6 +82,7 @@ static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,
 	unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
 	struct ttm_pool_dma *dma;
 	struct page *p;
+	unsigned int i;
 	void *vaddr;
 
 	/* Don't set the __GFP_COMP flag for higher order allocations.
@@ -94,38 +95,43 @@ static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,
 
 	if (!pool->use_dma_alloc) {
 		p = alloc_pages(gfp_flags, order);
-		if (p)
-			p->private = order;
-		return p;
-	}
+		if (!p)
+			return NULL;
 
-	dma = kmalloc(sizeof(*dma), GFP_KERNEL);
-	if (!dma)
-		return NULL;
+		p->private = order;
+	} else {
 
-	if (order)
-		attr |= DMA_ATTR_NO_WARN;
+		dma = kmalloc(sizeof(*dma), GFP_KERNEL);
+		if (!dma)
+			return NULL;
 
-	vaddr = dma_alloc_attrs(pool->dev, (1ULL << order) * PAGE_SIZE,
-				&dma->addr, gfp_flags, attr);
-	if (!vaddr)
-		goto error_free;
+		if (order)
+			attr |= DMA_ATTR_NO_WARN;
 
-	/* TODO: This is an illegal abuse of the DMA API, but we need to rework
-	 * TTM page fault handling and extend the DMA API to clean this up.
-	 */
-	if (is_vmalloc_addr(vaddr))
-		p = vmalloc_to_page(vaddr);
-	else
-		p = virt_to_page(vaddr);
+		vaddr = dma_alloc_attrs(pool->dev, (1ULL << order) * PAGE_SIZE,
+					&dma->addr, gfp_flags, attr);
+		if (!vaddr) {
+			kfree(dma);
+			return NULL;
+		}
 
-	dma->vaddr = (unsigned long)vaddr | order;
-	p->private = (unsigned long)dma;
-	return p;
+		/* TODO: This is an illegal abuse of the DMA API, but we need
+		 * to rework TTM page fault handling and extend the DMA API to
+		 * clean this up.
+		 */
+		if (is_vmalloc_addr(vaddr))
+			p = vmalloc_to_page(vaddr);
+		else
+			p = virt_to_page(vaddr);
 
-error_free:
-	kfree(dma);
-	return NULL;
+		dma->vaddr = (unsigned long)vaddr | order;
+		p->private = (unsigned long)dma;
+	}
+
+	for (i = 0; i < (1 << order); ++i)
+		SetPageReserved(&p[i]);
+
+	return p;
 }
 
 /* Reset the caching and pages of size 1 << order */
@@ -134,6 +140,7 @@ static void ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching,
 {
 	unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
 	struct ttm_pool_dma *dma;
+	unsigned int i;
 	void *vaddr;
 
 #ifdef CONFIG_X86
@@ -144,6 +151,9 @@ static void ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching,
 		set_pages_wb(p, 1 << order);
 #endif
 
+	for (i = 0; i < (1 << order); ++i)
+		ClearPageReserved(&p[i]);
+
 	if (!pool || !pool->use_dma_alloc) {
 		__free_pages(p, order);
 		return;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 3 lines · Christian König

KVM tries to grab references to pages in VMAs marked with VM_PFNMAP. This is illegal and can cause data corruption with TTM pages because only some of them are actually reference counted.

I think that you are referring to this:

        /* Don't set the __GFP_COMP flag for higher order allocations.
         * Mapping pages directly into an userspace process and calling
         * put_page() on a TTM allocated page is illegal.
         */
        if (order)
                gfp_flags |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN |
                        __GFP_KSWAPD_RECLAIM;

By "directly" I guess you mean without going through remap_pfn_range().

Based on our discussion offlist, it should be possible to remove the get_page/put_page from the path that fills in the KVM page table, but it is difficult to remove it altogether (it requires changing everything to use userspace virtual address).

Indeed KVM needs to detect non-reference-counted pages because unfortunately there are cases where people want to map VM_PFNMAP pages into a guest. If it is not enough to have PageReserved set, and there is a better check, KVM can be fixed too.

Quoted 4 lines

Mark all pages allocated by TTM as reserved, this way KVM handles the PFNs like they would point to MMIO space.

This still results in a warning, but at least no other problem.

What warning is it?

Paolo

Quoted 1 line
#2 lore raw
Quoted 4 lines · Paolo Bonzini
Quoted 3 lines · Christian König

KVM tries to grab references to pages in VMAs marked with VM_PFNMAP. This is illegal and can cause data corruption with TTM pages because only some of them are actually reference counted.

After some other offlist discussions, I also would like to understand what you mean by corruption.

First, is it a _host_ corruption or a guest corruption/crash? A guest crash would be KVM doing exactly what it's meant to do: it detects the non-reserved, non-refcounted page and refuses to map it into the guest.

On the other hand, if it is a host crash, my understanding is that an order>0 allocation leaves the tail pages with a zero reference count (and without a compound_head if, as in this case, __GFP_COMP is unset). If that's correct, more analysis is needed to understand why get_page_unless_zero() isn't rejecting the tail pages.

Paolo

Quoted 11 lines
Quoted 4 lines

Mark all pages allocated by TTM as reserved, this way KVM handles the PFNs like they would point to MMIO space.

This still results in a warning, but at least no other problem.

What warning is it?

Paolo

Quoted 1 line
#3 lore raw

Am 29.03.23 um 17:08 schrieb Paolo Bonzini:

Quoted 7 lines · Paolo Bonzini
Quoted 4 lines · Christian König
Quoted 3 lines

KVM tries to grab references to pages in VMAs marked with VM_PFNMAP. This is illegal and can cause data corruption with TTM pages because only some of them are actually reference counted.

After some other offlist discussions, I also would like to understand what you mean by corruption.

I think what was meant here is that only parts of the buffers where updated, but see below.

Quoted 2 lines

First, is it a _host_ corruption or a guest corruption/crash?  A guest crash would be KVM doing exactly what it's meant to do: it detects the non-reserved, non-refcounted page and refuses to map it into the guest.

Yes I think that this is what happens. The use case and mapping is indeed valid as far as I can see, but the handling that KVM does here is really problematic.

When the PFN points to an IO address it just works because that isn't backed by struct pages. And when the PFN points to the first page of a higher order allocation it also works, only when the PFN points to a following page kvm_try_get_pfn() fails and causes the problems.

What needs to happen instead is that when both hva_to_pfn_fast() and hva_to_pfn_slow() fails you don't try to convert the PFN into a page and so also don't get a reference to the page.

This somehow needs to be communicated to the callers of hva_to_pfn() so that kvm_release_pfn() knows what to do.

Regards,
Christian.

Quoted 18 lines

On the other hand, if it is a host crash, my understanding is that an order>0 allocation leaves the tail pages with a zero reference count (and without a compound_head if, as in this case, __GFP_COMP is unset). If that's correct, more analysis is needed to understand why get_page_unless_zero() isn't rejecting the tail pages.

Paolo

Quoted 11 lines
Quoted 4 lines

Mark all pages allocated by TTM as reserved, this way KVM handles the PFNs like they would point to MMIO space.

This still results in a warning, but at least no other problem.

What warning is it?

Paolo

Quoted 1 line
#4 lore raw
Quoted 3 lines · Christian König
Quoted 1 line

First, is it a _host_ corruption or a guest corruption/crash?  A guest crash would be KVM doing exactly what it's meant to do: it detects the non-reserved, non-refcounted page and refuses to map it into the guest.

Yes I think that this is what happens.

Ok I was worried all the time that this was host corruption/crash; which obviously would have been much worse.

Quoted 6 lines

The use case and mapping is indeed valid as far as I can see, but the handling that KVM does here is really problematic.

What needs to happen instead is that when both hva_to_pfn_fast() and hva_to_pfn_slow() fails you don't try to convert the PFN into a page and so also don't get a reference to the page.

This somehow needs to be communicated to the callers of hva_to_pfn() so that kvm_release_pfn() knows what to do.

There's a bit more complication here:

1) in the guest page fault path we can avoid taking the reference altogether

2) in other MMU-notifier-protected paths, we can also avoid taking the reference but we also must stop using kmap() in virt/kvm/pfncache.c.

3) other uses of kmap() must switch to MMU-notifier protection.

If the DRM people are okay with SetPageReserved() as a temporary hack, we can change or remove the WARN in kvm_is_zone_device_page(), since that is what you are referring to in the commit message.

Paolo

#5 lore raw

Am 29.03.23 um 17:51 schrieb Paolo Bonzini:

Quoted 14 lines · Christian König
Quoted 3 lines
Quoted 1 line

First, is it a _host_ corruption or a guest corruption/crash?  A guest crash would be KVM doing exactly what it's meant to do: it detects the non-reserved, non-refcounted page and refuses to map it into the guest.

Yes I think that this is what happens.

Ok I was worried all the time that this was host corruption/crash; which obviously would have been much worse.

Quoted 6 lines

The use case and mapping is indeed valid as far as I can see, but the handling that KVM does here is really problematic.

What needs to happen instead is that when both hva_to_pfn_fast() and hva_to_pfn_slow() fails you don't try to convert the PFN into a page and so also don't get a reference to the page.

This somehow needs to be communicated to the callers of hva_to_pfn() so that kvm_release_pfn() knows what to do.

Quoted 7 lines

There's a bit more complication here:

1) in the guest page fault path we can avoid taking the reference altogether

2) in other MMU-notifier-protected paths, we can also avoid taking the reference but we also must stop using kmap() in virt/kvm/pfncache.c.

3) other uses of kmap() must switch to MMU-notifier protection.

I would rather suggest to return the page additionally to the pfn from hva_to_pfn() when the function was able to grab a reference to it.

When the page is then not available you can't call kmap() on that either.

Quoted 2 lines

If the DRM people are okay with SetPageReserved() as a temporary hack, we can change or remove the WARN in kvm_is_zone_device_page(), since that is what you are referring to in the commit message.

Adding Daniel for additional comments on this, but essentially we have changed quite some infrastructure to make sure that everybody uses VM_PFNMAP to prevent stuff like this from happening.

I would really prefer a proper solution in KVM instead.

Christian.

Quoted 3 lines

Paolo

#6 lore raw
Quoted 6 lines · Christian König
Quoted 3 lines

3) other uses of kmap() must switch to MMU-notifier protection.

I would rather suggest to return the page additionally to the pfn from hva_to_pfn() when the function was able to grab a reference to it.

Quoted 8 lines

When the page is then not available you can't call kmap() on that either.

Quoted 2 lines

If the DRM people are okay with SetPageReserved() as a temporary hack, we can change or remove the WARN in kvm_is_zone_device_page(), since that is what you are referring to in the commit message.

Adding Daniel for additional comments on this, but essentially we have changed quite some infrastructure to make sure that everybody uses VM_PFNMAP to prevent stuff like this from happening.

I would really prefer a proper solution in KVM instead.

Hmm... Now that I think about it that would be

https://lore.kernel.org/kvm/[email protected]/t/

Time to resurrect that work.

Paolo

#7 lore raw

+David

Quoted 19 lines · Paolo Bonzini
Quoted 18 lines · Christian K�nig
Quoted 3 lines

3) other uses of kmap() must switch to MMU-notifier protection.

I would rather suggest to return the page additionally to the pfn from hva_to_pfn() when the function was able to grab a reference to it.

When the page is then not available you can't call kmap() on that either.

Quoted 4 lines

If the DRM people are okay with SetPageReserved() as a temporary hack, we can change or remove the WARN in kvm_is_zone_device_page(), since that is what you are referring to in the commit message.

Adding Daniel for additional comments on this, but essentially we have changed quite some infrastructure to make sure that everybody uses VM_PFNMAP to prevent stuff like this from happening.

Quoted 7 lines
Quoted 1 line

I would really prefer a proper solution in KVM instead.

Hmm... Now that I think about it that would be

https://lore.kernel.org/kvm/[email protected]/t/

Time to resurrect that work.

Ya. I had previously asked David to first eliminated the usage that isn't protected by mmu_notifiers, but after seeing the resulting complexity, I had a change of heart[2]. Can you weigh in on the latter thread, specifically my proposal of using a module param to let the admin opt-in to "unsafe" kmap usage.

[1] https://lore.kernel.org/kvm/[email protected]
[2] https://lore.kernel.org/all/[email protected]/

#8 lore raw

Am 29.03.23 um 19:22 schrieb Sean Christopherson:

Quoted 21 lines

+David

Quoted 18 lines · Paolo Bonzini
Quoted 7 lines · Christian K�nig
Quoted 2 lines

3) other uses of kmap() must switch to MMU-notifier protection.

I would rather suggest to return the page additionally to the pfn from hva_to_pfn() when the function was able to grab a reference to it.

When the page is then not available you can't call kmap() on that either.

Quoted 8 lines
Quoted 3 lines

If the DRM people are okay with SetPageReserved() as a temporary hack, we can change or remove the WARN in kvm_is_zone_device_page(), since that is what you are referring to in the commit message.

Adding Daniel for additional comments on this, but essentially we have changed quite some infrastructure to make sure that everybody uses VM_PFNMAP to prevent stuff like this from happening.

I would really prefer a proper solution in KVM instead.

Hmm... Now that I think about it that would be

Quoted 7 lines
Quoted 3 lines

https://lore.kernel.org/kvm/[email protected]/t/

Time to resurrect that work.

Ya. I had previously asked David to first eliminated the usage that isn't protected by mmu_notifiers, but after seeing the resulting complexity, I had a change of heart[2]. Can you weigh in on the latter thread, specifically my proposal of using a module param to let the admin opt-in to "unsafe" kmap usage.

I don't think that this is something an admin should be able to decide.

x86 system are rather grateful, but if you kmap() pages accessed by GPUs on ARM the system might just reboot spontaneously.

Robin Murphy <[email protected]> can fill you in on the hw details if needed.

Christian.

Quoted 3 lines

[1] https://lore.kernel.org/kvm/[email protected]
[2] https://lore.kernel.org/all/[email protected]/

#9 lore raw
Quoted 24 lines · Christian K�nig

Am 29.03.23 um 19:22 schrieb Sean Christopherson:

Quoted 23 lines

+David

Quoted 20 lines · Paolo Bonzini
Quoted 7 lines · Christian K�nig
Quoted 2 lines

3) other uses of kmap() must switch to MMU-notifier protection.

I would rather suggest to return the page additionally to the pfn from hva_to_pfn() when the function was able to grab a reference to it.

When the page is then not available you can't call kmap() on that either.

Quoted 8 lines
Quoted 3 lines

If the DRM people are okay with SetPageReserved() as a temporary hack, we can change or remove the WARN in kvm_is_zone_device_page(), since that is what you are referring to in the commit message.

Adding Daniel for additional comments on this, but essentially we have changed quite some infrastructure to make sure that everybody uses VM_PFNMAP to prevent stuff like this from happening.

I would really prefer a proper solution in KVM instead.

Hmm... Now that I think about it that would be

https://lore.kernel.org/kvm/[email protected]/t/

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Time to resurrect that work.

Ya. I had previously asked David to first eliminated the usage that isn't protected by mmu_notifiers, but after seeing the resulting complexity, I had a change of heart[2]. Can you weigh in on the latter thread, specifically my proposal of using a module param to let the admin opt-in to "unsafe" kmap usage.

I don't think that this is something an admin should be able to decide.

Why not? Assuming the admin has CAP_SYS_ADMIN, they can reboot the host in a myriad of ways. The idea with the KVM module param is to allow curated setups where the userspace VMM is trusted to a large extent, e.g. AWS' Nitro, to opt-in to the unsafe behavior. I.e. by enabling the flag, the admin is acknowledging that bad things can happen if untrusted/compromised userspace gets ahold of /dev/kvm.

Quoted 2 lines

x86 system are rather grateful, but if you kmap() pages accessed by GPUs on ARM the system might just reboot spontaneously.

FWIW, the dangers of an unsafe kmap() are arguably far worse than spontaneous reboots, e.g. there's potential for use-after-free and possibly even write access to arbitrary memory.

#10 lore raw

Am 29.03.23 um 19:39 schrieb Sean Christopherson:

Quoted 29 lines · Christian K�nig
Quoted 28 lines

Am 29.03.23 um 19:22 schrieb Sean Christopherson:

Quoted 27 lines

+David

Quoted 14 lines · Paolo Bonzini
Quoted 13 lines · Christian K�nig
Quoted 1 line

3) other uses of kmap() must switch to MMU-notifier protection.

I would rather suggest to return the page additionally to the pfn from hva_to_pfn() when the function was able to grab a reference to it.

When the page is then not available you can't call kmap() on that either.

Quoted 3 lines

If the DRM people are okay with SetPageReserved() as a temporary hack, we can change or remove the WARN in kvm_is_zone_device_page(), since that is what you are referring to in the commit message.

Adding Daniel for additional comments on this, but essentially we have changed quite some infrastructure to make sure that everybody uses VM_PFNMAP to prevent stuff like this from happening.

Quoted 6 lines
Quoted 1 line

I would really prefer a proper solution in KVM instead.

Hmm... Now that I think about it that would be

https://lore.kernel.org/kvm/[email protected]/t/

Time to resurrect that work.

Ya. I had previously asked David to first eliminated the usage that isn't protected by mmu_notifiers, but after seeing the resulting complexity, I had a change of heart[2]. Can you weigh in on the latter thread, specifically my proposal of using a module param to let the admin opt-in to "unsafe" kmap usage.

Quoted 7 lines
Quoted 1 line

I don't think that this is something an admin should be able to decide.

Why not? Assuming the admin has CAP_SYS_ADMIN, they can reboot the host in a myriad of ways. The idea with the KVM module param is to allow curated setups where the userspace VMM is trusted to a large extent, e.g. AWS' Nitro, to opt-in to the unsafe behavior. I.e. by enabling the flag, the admin is acknowledging that bad things can happen if untrusted/compromised userspace gets ahold of /dev/kvm.

Well exactly that's the point, you don't need untrusted/compromised userspace the system could just go spontaneously into nirvana during normal operation.

This would result in very very hard to debug problems since the issues only happen rather rarely.

On the other hand why do you need the kmap() in the first place?

Regards,
Christian.

Quoted 6 lines
Quoted 3 lines

x86 system are rather grateful, but if you kmap() pages accessed by GPUs on ARM the system might just reboot spontaneously.

FWIW, the dangers of an unsafe kmap() are arguably far worse than spontaneous reboots, e.g. there's potential for use-after-free and possibly even write access to arbitrary memory.

#11 lore raw
Quoted 38 lines · Christian K�nig

Am 29.03.23 um 19:39 schrieb Sean Christopherson:

Quoted 29 lines · Christian K�nig
Quoted 28 lines

Am 29.03.23 um 19:22 schrieb Sean Christopherson:

Quoted 27 lines

+David

Quoted 14 lines · Paolo Bonzini
Quoted 13 lines · Christian K�nig
Quoted 1 line

3) other uses of kmap() must switch to MMU-notifier protection.

I would rather suggest to return the page additionally to the pfn from hva_to_pfn() when the function was able to grab a reference to it.

When the page is then not available you can't call kmap() on that either.

Quoted 3 lines

If the DRM people are okay with SetPageReserved() as a temporary hack, we can change or remove the WARN in kvm_is_zone_device_page(), since that is what you are referring to in the commit message.

Adding Daniel for additional comments on this, but essentially we have changed quite some infrastructure to make sure that everybody uses VM_PFNMAP to prevent stuff like this from happening.

Quoted 6 lines
Quoted 1 line

I would really prefer a proper solution in KVM instead.

Hmm... Now that I think about it that would be

https://lore.kernel.org/kvm/[email protected]/t/

Time to resurrect that work.

Ya. I had previously asked David to first eliminated the usage that isn't protected by mmu_notifiers, but after seeing the resulting complexity, I had a change of heart[2]. Can you weigh in on the latter thread, specifically my proposal of using a module param to let the admin opt-in to "unsafe" kmap usage.

Quoted 7 lines
Quoted 1 line

I don't think that this is something an admin should be able to decide.

Why not? Assuming the admin has CAP_SYS_ADMIN, they can reboot the host in a myriad of ways. The idea with the KVM module param is to allow curated setups where the userspace VMM is trusted to a large extent, e.g. AWS' Nitro, to opt-in to the unsafe behavior. I.e. by enabling the flag, the admin is acknowledging that bad things can happen if untrusted/compromised userspace gets ahold of /dev/kvm.

Quoted 3 lines

Well exactly that's the point, you don't need untrusted/compromised userspace the system could just go spontaneously into nirvana during normal operation.

The proposal is that the module param is off by default, and the expectation is that it would be turned on only by very specific setups. I would not object to making it even more difficult to enable, burying it behind a Kconfig, but IMO that is unnecessary.

Quoted 4 lines

This would result in very very hard to debug problems since the issues only happen rather rarely.

On the other hand why do you need the kmap() in the first place?

The Nitro setup manages guest memory in userspace, and hides that memory from the kernel, e.g. to avoid struct page overhead.. To enable KVM to access guest memory in select flows, e.g. for paravirtualization, without taking on too much complexity, KVM supports kmap() of that memory.

For some uses, e.g. nested virtualization, switching to uaccess might be feasible, but the paravirt stuff, especially Xen support, would likely be insanely complex to do without kmap().

#12 lore raw
In reply to Sean Christopherson
Quoted 31 lines · Sean Christopherson

+David

Quoted 19 lines · Paolo Bonzini
Quoted 18 lines · Christian K�nig
Quoted 3 lines

3) other uses of kmap() must switch to MMU-notifier protection.

I would rather suggest to return the page additionally to the pfn from hva_to_pfn() when the function was able to grab a reference to it.

When the page is then not available you can't call kmap() on that either.

Quoted 4 lines

If the DRM people are okay with SetPageReserved() as a temporary hack, we can change or remove the WARN in kvm_is_zone_device_page(), since that is what you are referring to in the commit message.

Adding Daniel for additional comments on this, but essentially we have changed quite some infrastructure to make sure that everybody uses VM_PFNMAP to prevent stuff like this from happening.

Quoted 7 lines
Quoted 1 line

I would really prefer a proper solution in KVM instead.

Hmm... Now that I think about it that would be

https://lore.kernel.org/kvm/[email protected]/t/

Time to resurrect that work.

Quoted 7 lines

Ya. I had previously asked David to first eliminated the usage that isn't protected by mmu_notifiers, but after seeing the resulting complexity, I had a change of heart[2]. Can you weigh in on the latter thread, specifically my proposal of using a module param to let the admin opt-in to "unsafe" kmap usage.

[1] https://lore.kernel.org/kvm/[email protected]
[2] https://lore.kernel.org/all/[email protected]/

I just finished rebasing my patch series and sent out v6:

https://lore.kernel.org/all/[email protected]/

-David