xen/gntdev: Avoid blocking in unmap_grant_pages()
Subsystems (2)
PATCH v2 1/1 xen/gntdev: Avoid blocking in unmap_grant_pages()
unmap_grant_pages() currently waits for the pages to no longer be used. In https://github.com/QubesOS/qubes-issues/issues/7481, this lead to a deadlock against i915: i915 was waiting for gntdev's MMU notifier to finish, while gntdev was waiting for i915 to free its pages. I also believe this is responsible for various deadlocks I have experienced in the past.
Avoid these problems by making unmap_grant_pages async. This requires making it return void, as any errors will not be available when the function returns. Fortunately, the only use of the return value is a WARN_ON(), which can be replaced by a WARN_ON when the error is detected. Additionally, a failed call will not prevent further calls from being made, but this is harmless.
Because unmap_grant_pages is now async, the grant handle will be sent to INVALID_GRANT_HANDLE too late to prevent multiple unmaps of the same handle. Instead, a separate bool array is allocated for this purpose. This wastes memory, but stuffing this information in padding bytes is too fragile. Furthermore, it is necessary to grab a reference to the map before making the asynchronous call, and release the reference when the call returns.
--- drivers/xen/gntdev-common.h | 5 ++ drivers/xen/gntdev.c | 100 +++++++++++++++++++----------------- 2 files changed, 59 insertions(+), 46 deletions(-)
drivers/xen/gntdev-common.h +5 -0
@@ -16,6 +16,7 @@#include <linux/mmu_notifier.h> #include <linux/types.h> #include <xen/interface/event_channel.h> +#include <xen/grant_table.h> struct gntdev_dmabuf_priv ;
@@ -56,6 +57,7 @@ struct gntdev_grant_map {struct gnttab_unmap_grant_ref *unmap_ops;struct gnttab_map_grant_ref *kmap_ops;struct gnttab_unmap_grant_ref *kunmap_ops; +bool *being_removed;struct page **pages;unsigned long pages_vm_start;
@@ -73,6 +75,9 @@ struct gntdev_grant_map {/* Needed to avoid allocation in gnttab_dma_free_pages(). */ xen_pfn_t *frames;#endif + +/* Needed to avoid allocation in __unmap_grant_pages */ +struct gntab_unmap_queue_data unmap_data; }; struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count,
drivers/xen/gntdev.c +54 -46
180 lines not on this page · raw message
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
Juergen Gross
maintainerQuoted 14 lines · Demi Marie Obenour
unmap_grant_pages()currently waits for the pages to no longer be used. In https://github.com/QubesOS/qubes-issues/issues/7481, this lead to a deadlock against i915: i915 was waiting for gntdev's MMU notifier to finish, while gntdev was waiting for i915 to free its pages. I also believe this is responsible for various deadlocks I have experienced in the past.Avoid these problems by making
unmap_grant_pagesasync. This requires making it return void, as any errors will not be available when the function returns. Fortunately, the only use of the return value is aWARN_ON(), which can be replaced by aWARN_ONwhen the error is detected. Additionally, a failed call will not prevent further calls from being made, but this is harmless.
Quoted 7 lines
Because
unmap_grant_pagesis now async, the grant handle will be sent toINVALID_GRANT_HANDLEtoo late to prevent multiple unmaps of the same handle. Instead, a separate bool array is allocated for this purpose. This wastes memory, but stuffing this information in padding bytes is too fragile. Furthermore, it is necessary to grab a reference to the map before making the asynchronous call, and release the reference when the call returns.
I think there is even more syncing needed:
- In the error path of gntdev_mmap() unmap_grant_pages() is being called and it is assumed, map is available afterwards again. This should be rather easy to avoid by adding a counter of active mappings to struct gntdev_grant_map (number of pages not being unmapped yet). In case this counter is not zero gntdev_mmap() should bail out early.
- gntdev_put_map() is calling unmap_grant_pages() in case the refcount has dropped to zero. This call can set the refcount to 1 again, so there is another delay needed before freeing map. I think unmap_grant_pages() should return in case the count of mapped pages is zero (see above), thus avoiding to increment the refcount of map if nothing is to be done. This would enable gntdev_put_map() to just return after the call of unmap_grant_pages() in case the refcount has been incremented again.
Juergen
Quoted 23 lines · Juergen Gross
Quoted 14 lines · Demi Marie Obenour
unmap_grant_pages()currently waits for the pages to no longer be used. In https://github.com/QubesOS/qubes-issues/issues/7481, this lead to a deadlock against i915: i915 was waiting for gntdev's MMU notifier to finish, while gntdev was waiting for i915 to free its pages. I also believe this is responsible for various deadlocks I have experienced in the past.Avoid these problems by making
unmap_grant_pagesasync. This requires making it return void, as any errors will not be available when the function returns. Fortunately, the only use of the return value is aWARN_ON(), which can be replaced by aWARN_ONwhen the error is detected. Additionally, a failed call will not prevent further calls from being made, but this is harmless.Quoted 7 lines
Because
unmap_grant_pagesis now async, the grant handle will be sent toINVALID_GRANT_HANDLEtoo late to prevent multiple unmaps of the same handle. Instead, a separate bool array is allocated for this purpose. This wastes memory, but stuffing this information in padding bytes is too fragile. Furthermore, it is necessary to grab a reference to the map before making the asynchronous call, and release the reference when the call returns.
Quoted 7 lines
I think there is even more syncing needed:
- In the error path of
gntdev_mmap()unmap_grant_pages()is being called and it is assumed, map is available afterwards again. This should be rather easy to avoid by adding a counter of active mappings to structgntdev_grant_map(number of pages not being unmapped yet). In case this counter is not zerogntdev_mmap()should bail out early.
Is it possible to just unmap the pages directly here? I don’t think there can be any other users of these pages yet. Userspace could race against the unmap and cause a page fault, but that should just cause userspace to get SIGSEGV or SIGBUS. In any case this code can use the sync version; if it gets blocked that’s userspace’s problem.
Quoted 7 lines
-
gntdev_put_map()is callingunmap_grant_pages()in case the refcount has dropped to zero. This call can set the refcount to 1 again, so there is another delay needed before freeing map. I thinkunmap_grant_pages()should return in case the count of mapped pages is zero (see above), thus avoiding to increment the refcount of map if nothing is to be done. This would enablegntdev_put_map()to just return after the call ofunmap_grant_pages()in case the refcount has been incremented again.
I will change this in v3, but I do wonder if gntdev is using the wrong MMU notifier callback. It seems that the appropriate callback is actually release(): if I understand correctly, release() is called precisely when the refcount on the physical page is about to drop to 0, and that is what we want.
Signature · 3 lines
Sincerely, Demi Marie Obenour (she/her/hers) Invisible Things Lab
Juergen Gross
maintainerQuoted 46 lines · Demi Marie Obenour
Quoted 23 lines · Juergen Gross
Quoted 14 lines · Demi Marie Obenour
unmap_grant_pages()currently waits for the pages to no longer be used. In https://github.com/QubesOS/qubes-issues/issues/7481, this lead to a deadlock against i915: i915 was waiting for gntdev's MMU notifier to finish, while gntdev was waiting for i915 to free its pages. I also believe this is responsible for various deadlocks I have experienced in the past.Avoid these problems by making
unmap_grant_pagesasync. This requires making it return void, as any errors will not be available when the function returns. Fortunately, the only use of the return value is aWARN_ON(), which can be replaced by aWARN_ONwhen the error is detected. Additionally, a failed call will not prevent further calls from being made, but this is harmless.Quoted 7 lines
Because
unmap_grant_pagesis now async, the grant handle will be sent toINVALID_GRANT_HANDLEtoo late to prevent multiple unmaps of the same handle. Instead, a separate bool array is allocated for this purpose. This wastes memory, but stuffing this information in padding bytes is too fragile. Furthermore, it is necessary to grab a reference to the map before making the asynchronous call, and release the reference when the call returns.Quoted 7 lines
I think there is even more syncing needed:
- In the error path of
gntdev_mmap()unmap_grant_pages()is being called and it is assumed, map is available afterwards again. This should be rather easy to avoid by adding a counter of active mappings to structgntdev_grant_map(number of pages not being unmapped yet). In case this counter is not zerogntdev_mmap()should bail out early.Is it possible to just unmap the pages directly here? I don’t think there can be any other users of these pages yet. Userspace could race against the unmap and cause a page fault, but that should just cause userspace to get SIGSEGV or SIGBUS. In any case this code can use the sync version; if it gets blocked that’s userspace’s problem.
Quoted 7 lines
-
gntdev_put_map()is callingunmap_grant_pages()in case the refcount has dropped to zero. This call can set the refcount to 1 again, so there is another delay needed before freeing map. I thinkunmap_grant_pages()should return in case the count of mapped pages is zero (see above), thus avoiding to increment the refcount of map if nothing is to be done. This would enablegntdev_put_map()to just return after the call ofunmap_grant_pages()in case the refcount has been incremented again.
Quoted 5 lines
I will change this in v3, but I do wonder if gntdev is using the wrong MMU notifier callback. It seems that the appropriate callback is actually
release(): if I understand correctly,release()is called precisely when the refcount on the physical page is about to drop to 0, and that is what we want.
No, I don't think this is correct.
release() is being called when the refcount of the address space is about to drop to 0. It has no page granularity.
Juergen