Emulated coherent graphics memory
Subsystems (6)
v4 cover Emulated coherent graphics memory
Planning to merge this through the drm/vmwgfx tree soon, so if there are any objections, please speak up.
Graphics APIs like OpenGL 4.4 and Vulkan require the graphics driver to provide coherent graphics memory, meaning that the GPU sees any content written to the coherent memory on the next GPU operation that touches that memory, and the CPU sees any content written by the GPU to that memory immediately after any fence object trailing the GPU operation has signaled.
Paravirtual drivers that otherwise require explicit synchronization needs to do this by hooking up dirty tracking to pagefault handlers and buffer object validation. This is a first attempt to do that for the vmwgfx driver.
The mm patches has been out for RFC. I think I have addressed all the feedback I got, except a possible softdirty breakage. But although the dirty-tracking and softdirty may write-protect PTEs both care about, that shouldn't really cause any operation interference. In particular since we use the hardware dirty PTE bits and softdirty uses other PTE bits.
For the TTM changes they are hopefully in line with the long-term strategy of making helpers out of what's left of TTM.
The code has been tested and excercised by a tailored version of mesa where we disable all explicit synchronization and assume graphics memory is coherent. The performance loss varies of course; a typical number is around 5%.
Changes v1-v2:
- Addressed a number of typos and formatting issues.
- Added a usage warning for apply_to_pfn_range() and apply_to_page_range()
- Re-evaluated the decision to use apply_to_pfn_range() rather than modifying the pagewalk.c. It still looks like generically handling the transparent huge page cases requires the mmap_sem to be held at least in read mode, so sticking with apply_to_pfn_range() for now.
- The TTM page-fault helper vma copy argument was scratched in favour of a pageprot_t argument.
Changes v3:
- Adapted to upstream API changes.
Changes v4:
- Adapted to upstream mmu_notifier changes. (Jerome?)
- Fixed a couple of warnings on 32-bit x86
- Fixed image offset computation on multisample images.
PATCH v4 1/9 mm: Allow the [page|pfn]_mkwrite callbacks to drop the mmap_sem
From: Thomas Hellstrom <[email protected]>
Driver fault callbacks are allowed to drop the mmap_sem when expecting long hardware waits to avoid blocking other mm users. Allow the mkwrite callbacks to do the same by returning early on VM_FAULT_RETRY.
In particular we want to be able to drop the mmap_sem when waiting for a reservation object lock on a GPU buffer object. These locks may be held while waiting for the GPU.
--- mm/memory.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
mm/memory.c +6 -4
@@ -2238,7 +2238,7 @@ static vm_fault_t do_page_mkwrite(struct vm_fault *vmf) ret = vmf->vma->vm_ops->page_mkwrite (vmf);/* Restore original flags so that caller is not surprised */ vmf->flags = old_flags; -if (unlikely (ret & (VM_FAULT_ERROR |VM_FAULT_NOPAGE ))) +if (unlikely (ret & (VM_FAULT_ERROR |VM_FAULT_NOPAGE |VM_FAULT_RETRY )))return ret;if (unlikely (!(ret &VM_FAULT_LOCKED ))) {lock_page (page);
@@ -2515,7 +2515,7 @@ static vm_fault_t wp_pfn_shared(struct vm_fault *vmf)pte_unmap_unlock (vmf->pte, vmf->ptl); vmf->flags |=FAULT_FLAG_MKWRITE ; ret = vma->vm_ops->pfn_mkwrite (vmf); -if (ret & (VM_FAULT_ERROR |VM_FAULT_NOPAGE )) +if (ret & (VM_FAULT_ERROR |VM_FAULT_NOPAGE |VM_FAULT_RETRY ))return ret;return finish_mkwrite_fault (vmf); }
@@ -2536,7 +2536,8 @@ static vm_fault_t wp_page_shared(struct vm_fault *vmf)pte_unmap_unlock (vmf->pte, vmf->ptl); tmp =do_page_mkwrite (vmf);if (unlikely (!tmp || (tmp & - (VM_FAULT_ERROR |VM_FAULT_NOPAGE )))) { + (VM_FAULT_ERROR |VM_FAULT_NOPAGE | +VM_FAULT_RETRY )))) {put_page (vmf->page);return tmp; }
@@ -3601,7 +3602,8 @@ static vm_fault_t do_shared_fault(struct vm_fault *vmf)unlock_page (vmf->page); tmp =do_page_mkwrite (vmf);if (unlikely (!tmp || - (tmp & (VM_FAULT_ERROR |VM_FAULT_NOPAGE )))) { + (tmp & (VM_FAULT_ERROR |VM_FAULT_NOPAGE | +VM_FAULT_RETRY )))) {put_page (vmf->page);return tmp; }
Signature · 1 line
2.20.1
PATCH v4 2/9 mm: Add an apply_to_pfn_range interface
In reply to Thomas Hellström (VMware)From: Thomas Hellstrom <[email protected]>
This is basically apply_to_page_range with added functionality:
Allocating missing parts of the page table becomes optional, which means that the function can be guaranteed not to error if allocation is disabled. Also passing of the closure struct and callback function becomes different and more in line with how things are done elsewhere.
Finally we keep apply_to_page_range as a wrapper around apply_to_pfn_range
The reason for not using the page-walk code is that we want to perform the page-walk on vmas pointing to an address space without requiring the mmap_sem to be held rather than on vmas belonging to a process with the mmap_sem held.
Notable changes since RFC:
Don't export apply_to_pfn range.
--- include/linux/mm.h | 10 ++++ mm/memory.c | 135 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 113 insertions(+), 32 deletions(-)
include/linux/mm.h +10 -0
@@ -2675,6 +2675,16 @@ typedef int (*pte_fn_t)(pte_t *pte, pgtable_t token, unsigned long addr,extern int apply_to_page_range (struct mm_struct *mm,unsigned long address,unsigned long size,pte_fn_t fn,void *data); +struct pfn_range_apply ; +typedef int (*pter_fn_t )(pte_t *pte,pgtable_t token,unsigned long addr, +struct pfn_range_apply *closure); +struct pfn_range_apply { +struct mm_struct *mm; +pter_fn_t ptefn; +unsigned int alloc; +}; +extern int apply_to_pfn_range (struct pfn_range_apply *closure, +unsigned long address,unsigned long size);#ifdef CONFIG_PAGE_POISONING extern bool page_poisoning_enabled (void );
mm/memory.c +103 -32
223 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v4 3/9 mm: Add write-protect and clean utilities for address space ranges
In reply to Thomas Hellström (VMware)From: Thomas Hellstrom <[email protected]>
Add two utilities to a) write-protect and b) clean all ptes pointing into a range of an address space.
The utilities are intended to aid in tracking dirty pages (either driver-allocated system memory or pci device memory).
The write-protect utility should be used in conjunction with page_mkwrite() and pfn_mkwrite() to trigger write page-faults on page accesses. Typically one would want to use this on sparse accesses into large memory regions. The clean utility should be used to utilize hardware dirtying functionality and avoid the overhead of page-faults, typically on large accesses into small memory regions.
The added file "as_dirty_helpers.c" is initially listed as maintained by VMware under our DRM driver. If somebody would like it elsewhere, that's of course no problem.
Notable changes since RFC:
- Added comments to help avoid the usage of these function for VMAs it's not intended for. We also do advisory checks on the vm_flags and warn on illegal usage.
- Perform the pte modifications the same way softdirty does.
- Add mmu_notifier range invalidation calls.
- Add a config option so that this code is not unconditionally included.
- Tell the mmu_gather code about pending tlb flushes.
--- MAINTAINERS | 1 + include/linux/mm.h | 9 +- mm/Kconfig | 3 + mm/Makefile | 1 + mm/as_dirty_helpers.c | 298 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 311 insertions(+), 1 deletion(-) create mode 100644 mm/as_dirty_helpers.c
MAINTAINERS +1 -0
@@ -5179,6 +5179,7 @@ T: git git://people.freedesktop.org/~thomash/linux S: Supported F: drivers/gpu/drm/vmwgfx/ F: include/uapi/drm/vmwgfx_drm.h +F: mm/as_dirty_helpers.c DRM DRIVERS M: David Airlie <[email protected]>
include/linux/mm.h +8 -1
@@ -2685,7 +2685,14 @@ struct pfn_range_apply { };extern int apply_to_pfn_range (struct pfn_range_apply *closure,unsigned long address,unsigned long size); - +unsigned long apply_as_wrprotect (struct address_space *mapping, +pgoff_t first_index,pgoff_t nr); +unsigned long apply_as_clean (struct address_space *mapping, +pgoff_t first_index,pgoff_t nr, +pgoff_t bitmap_pgoff, +unsigned long *bitmap, +pgoff_t *start, +pgoff_t *end);#ifdef CONFIG_PAGE_POISONING extern bool page_poisoning_enabled (void );extern void kernel_poison_pages (struct page *page,int numpages,int enable);
mm/Kconfig +3 -0
@@ -765,4 +765,7 @@ config GUP_BENCHMARK config ARCH_HAS_PTE_SPECIAL bool +config AS_DIRTY_HELPERS + bool + endmenu
mm/Makefile +1 -0
@@ -104,3 +104,4 @@ obj-$(CONFIG_HARDENED_USERCOPY) += usercopy.o obj-$(CONFIG_PERCPU_STATS) += percpu-stats.o obj-$(CONFIG_HMM) += hmm.o obj-$(CONFIG_MEMFD_CREATE) += memfd.o +obj-$(CONFIG_AS_DIRTY_HELPERS) += as_dirty_helpers.o
mm/as_dirty_helpers.c new file +298 -0
298 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v4 4/9 drm/ttm: Allow the driver to provide the ttm struct vm_operations_struct
In reply to Thomas Hellström (VMware)From: Thomas Hellstrom <[email protected]>
Add a pointer to the struct vm_operations_struct in the bo_device, and assign that pointer to the default value currently used.
The driver can then optionally modify that pointer and the new value can be used for each new vma created.
--- drivers/gpu/drm/ttm/ttm_bo.c | 1 + drivers/gpu/drm/ttm/ttm_bo_vm.c | 6 +++--- include/drm/ttm/ttm_bo_driver.h | 6 ++++++ 3 files changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +1 -0
@@ -1739,6 +1739,7 @@ int ttm_bo_device_init(struct ttm_bo_device *bdev,mutex_lock (&ttm_global_mutex);list_add_tail (&bdev->device_list, &glob->device_list);mutex_unlock (&ttm_global_mutex); + bdev->vm_ops = &ttm_bo_vm_ops;return 0 ; out_no_sys:
drivers/gpu/drm/ttm/ttm_bo_vm.c +3 -3
@@ -395,7 +395,7 @@ static int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,return ret; } -static const struct vm_operations_struct ttm_bo_vm_ops = { +const struct vm_operations_struct ttm_bo_vm_ops = { .fault = ttm_bo_vm_fault, .open = ttm_bo_vm_open, .close = ttm_bo_vm_close,
@@ -448,7 +448,7 @@ int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,if (unlikely (ret !=0 )) goto out_unref; - vma->vm_ops = &ttm_bo_vm_ops; + vma->vm_ops = bdev->vm_ops;/* * Note: We're transferring the bo reference to
@@ -480,7 +480,7 @@ int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)ttm_bo_get (bo); - vma->vm_ops = &ttm_bo_vm_ops; + vma->vm_ops = bo->bdev->vm_ops; vma->vm_private_data = bo; vma->vm_flags |=VM_MIXEDMAP ; vma->vm_flags |=VM_IO |VM_DONTEXPAND ;
include/drm/ttm/ttm_bo_driver.h +6 -0
@@ -442,6 +442,9 @@ extern struct ttm_bo_global {* @driver: Pointer to a struct ttm_bo_driver struct setup by the driver. * @man: An array of mem_type_managers. * @vma_manager: Address space manager +* @vm_ops: Pointer to the struct vm_operations_struct used for this +* device's VM operations. The driver may override this before the first +* mmap() call. * lru_lock: Spinlock that protects the buffer+device lru lists and * ddestroy lists. * @dev_mapping: A pointer to the struct address_space representing the
@@ -460,6 +463,7 @@ struct ttm_bo_device {struct ttm_bo_global *glob;struct ttm_bo_driver *driver;struct ttm_mem_type_manager man[TTM_NUM_MEM_TYPES ]; +const struct vm_operations_struct *vm_ops;/* * Protected by internal locks.
@@ -488,6 +492,8 @@ struct ttm_bo_device {bool no_retry; }; +extern const struct vm_operations_struct ttm_bo_vm_ops; +/** * struct ttm_lru_bulk_move_pos *
Signature · 1 line
2.20.1
PATCH v4 5/9 drm/ttm: TTM fault handler helpers
In reply to Thomas Hellström (VMware)From: Thomas Hellstrom <[email protected]>
With the vmwgfx dirty tracking, the default TTM fault handler is not completely sufficient (vmwgfx need to modify the vma->vm_flags member, and also needs to restrict the number of prefaults).
We also want to replicate the new ttm_bo_vm_reserve() functionality
So start turning the TTM vm code into helpers: ttm_bo_vm_fault_reserved() and ttm_bo_vm_reserve(), and provide a default TTM fault handler for other drivers to use.
--- drivers/gpu/drm/ttm/ttm_bo_vm.c | 175 +++++++++++++++++++------------- include/drm/ttm/ttm_bo_api.h | 10 ++ 2 files changed, 113 insertions(+), 72 deletions(-)
drivers/gpu/drm/ttm/ttm_bo_vm.c +103 -72
@@ -42,8 +42,6 @@#include <linux/uaccess.h> #include <linux/mem_encrypt.h> -#define TTM_BO_VM_NUM_PREFAULT 16 -static vm_fault_t ttm_bo_vm_fault_idle (struct ttm_buffer_object *bo,struct vm_fault *vmf) {
@@ -106,31 +104,30 @@ static unsigned long ttm_bo_io_mem_pfn(struct ttm_buffer_object *bo, + page_offset; } -static vm_fault_t ttm_bo_vm_fault (struct vm_fault *vmf) +/** +* ttm_bo_vm_reserve - Reserve a buffer object in a retryable vm callback +* @bo: The buffer object +* @vmf: The fault structure handed to the callback +* +* vm callbacks like fault() and *_mkwrite() allow for the mm_sem to be dropped +* during long waits, and after the wait the callback will be restarted. This +* is to allow other threads using the same virtual memory space concurrent +* access to map(), unmap() completely unrelated buffer objects. TTM buffer +* object reservations sometimes wait for GPU and should therefore be +* considered long waits. This function reserves the buffer object interruptibly +* taking this into account. Starvation is avoided by the vm system not +* allowing too many repeated restarts. +* This function is intended to be used in customized fault() and _mkwrite() +* handlers. +* +* Return: +* 0 on success and the bo was reserved. +* VM_FAULT_RETRY if blocking wait. +* VM_FAULT_NOPAGE if blocking wait and retrying was not allowed. +*/ +vm_fault_t ttm_bo_vm_reserve (struct ttm_buffer_object *bo, +struct vm_fault *vmf) { -struct vm_area_struct *vma = vmf->vma; -struct ttm_buffer_object *bo = (struct ttm_buffer_object *) - vma->vm_private_data; -struct ttm_bo_device *bdev = bo->bdev; -unsigned long page_offset; -unsigned long page_last; -unsigned long pfn; -struct ttm_tt *ttm =NULL ; -struct page *page; -int err; -int i; -vm_fault_t ret =VM_FAULT_NOPAGE ; -unsigned long address = vmf->address; -struct ttm_mem_type_manager *man = - &bdev->man[bo->mem.mem_type]; -struct vm_area_struct cvma; - -/* -* Work around locking order reversal in fault / nopfn -* between mmap_sem and bo_reserve: Perform a trylock operation -* for reserve, and if it fails, retry the fault after waiting -* for the buffer to become unreserved. -*/ if (unlikely (!reservation_object_trylock (bo->resv))) {if (vmf->flags &FAULT_FLAG_ALLOW_RETRY ) {if (!(vmf->flags &FAULT_FLAG_RETRY_NOWAIT )) {
@@ -151,14 +148,55 @@ static vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf)return VM_FAULT_NOPAGE ; } +return 0 ; +} +EXPORT_SYMBOL (ttm_bo_vm_reserve); + +/** +* ttm_bo_vm_fault_reserved - TTM fault helper +* @vmf: The struct vm_fault given as argument to the fault callback +* @prot: The page protection to be used for this memory area. +* @num_prefault: Maximum number of prefault pages. The caller may want to +* specify this based on madvice settings and the size of the GPU object +* backed by the memory. +* +* This function inserts one or more page table entries pointing to the +* memory backing the buffer object, and then returns a return code +* instructing the caller to retry the page access. +* +* Return: +* VM_FAULT_NOPAGE on success or pending signal +* VM_FAULT_SIGBUS on unspecified error +* VM_FAULT_OOM on out-of-memory +* VM_FAULT_RETRY if retryable wait +*/ +vm_fault_t ttm_bo_vm_fault_reserved (struct vm_fault *vmf, +pgprot_t prot, +pgoff_t num_prefault) +{ +struct vm_area_struct *vma = vmf->vma; +struct vm_area_struct cvma = *vma; +struct ttm_buffer_object *bo = (struct ttm_buffer_object *) + vma->vm_private_data; +struct ttm_bo_device *bdev = bo->bdev; +unsigned long page_offset; +unsigned long page_last; +unsigned long pfn; +struct ttm_tt *ttm =NULL ; +struct page *page; +int err; +pgoff_t i; +vm_fault_t ret =VM_FAULT_NOPAGE ; +unsigned long address = vmf->address; +struct ttm_mem_type_manager *man = + &bdev->man[bo->mem.mem_type]; +/* * Refuse to fault imported pages. This should be handled * (if at all) by redirecting mmap to the exporter. */ -if (bo->ttm && (bo->ttm->page_flags &TTM_PAGE_FLAG_SG )) { - ret =VM_FAULT_SIGBUS ; - goto out_unlock; - } +if (bo->ttm && (bo->ttm->page_flags &TTM_PAGE_FLAG_SG )) +return VM_FAULT_SIGBUS ;if (bdev->driver->fault_reserve_notify) {struct dma_fence *moving =dma_fence_get (bo->moving);
@@ -169,11 +207,9 @@ static vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf)break ;case -EBUSY :case -ERESTARTSYS : - ret =VM_FAULT_NOPAGE ; - goto out_unlock; +return VM_FAULT_NOPAGE ;default : - ret =VM_FAULT_SIGBUS ; - goto out_unlock; +return VM_FAULT_SIGBUS ; }if (bo->moving != moving) {
@@ -189,26 +225,15 @@ static vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf)* move. */ ret =ttm_bo_vm_fault_idle (bo, vmf); -if (unlikely (ret !=0 )) { -if (ret ==VM_FAULT_RETRY && - !(vmf->flags &FAULT_FLAG_RETRY_NOWAIT )) { -/* The BO has already been unreserved. */ -return ret; - } - - goto out_unlock; - } +if (unlikely (ret !=0 )) +return ret; err =ttm_mem_io_lock (man, true); -if (unlikely (err !=0 )) { - ret =VM_FAULT_NOPAGE ; - goto out_unlock; - } +if (unlikely (err !=0 )) +return VM_FAULT_NOPAGE ; err =ttm_mem_io_reserve_vm (bo); -if (unlikely (err !=0 )) { - ret =VM_FAULT_SIGBUS ; - goto out_io_unlock; - } +if (unlikely (err !=0 )) +return VM_FAULT_SIGBUS ; page_offset = ((address - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff -drm_vma_node_start (&bo->vma_node);
@@ -220,18 +245,8 @@ static vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf) goto out_io_unlock; } -/* -* Make a local vma copy to modify the page_prot member -* and vm_flags if necessary. The vma parameter is protected -* by mmap_sem in write mode. -*/ - cvma = *vma; - cvma.vm_page_prot =vm_get_page_prot (cvma.vm_flags); - -if (bo->mem.bus.is_iomem) { - cvma.vm_page_prot =ttm_io_prot (bo->mem.placement, - cvma.vm_page_prot); - }else { + cvma.vm_page_prot =ttm_io_prot (bo->mem.placement, prot); +if (!bo->mem.bus.is_iomem) {struct ttm_operation_ctx ctx = { .interruptible = false, .no_wait_gpu = false,
@@ -240,24 +255,21 @@ static vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf) }; ttm = bo->ttm; - cvma.vm_page_prot =ttm_io_prot (bo->mem.placement, - cvma.vm_page_prot); - -/* Allocate all page at once, most common usage */ -if (ttm_tt_populate (ttm, &ctx)) { +if (ttm_tt_populate (bo->ttm, &ctx)) { ret =VM_FAULT_OOM ; goto out_io_unlock; } + }else { +/* Iomem should not be marked encrypted */ + cvma.vm_page_prot =pgprot_decrypted (cvma.vm_page_prot); }/* * Speculatively prefault a number of pages. Only error on * first page. */ -for (i =0 ; i <TTM_BO_VM_NUM_PREFAULT ; ++i) { +for (i =0 ; i < num_prefault; ++i) {if (bo->mem.bus.is_iomem) { -/* Iomem should not be marked encrypted */ - cvma.vm_page_prot =pgprot_decrypted (cvma.vm_page_prot); pfn =ttm_bo_io_mem_pfn (bo, page_offset); }else { page = ttm->pages[page_offset];
@@ -295,7 +307,26 @@ static vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf) ret =VM_FAULT_NOPAGE ; out_io_unlock:ttm_mem_io_unlock (man); -out_unlock: +return ret; +} +EXPORT_SYMBOL (ttm_bo_vm_fault_reserved); + +static vm_fault_t ttm_bo_vm_fault (struct vm_fault *vmf) +{ +struct vm_area_struct *vma = vmf->vma; +pgprot_t prot; +struct ttm_buffer_object *bo = vma->vm_private_data; +vm_fault_t ret; + + ret =ttm_bo_vm_reserve (bo, vmf); +if (ret) +return ret; + + prot =vm_get_page_prot (vma->vm_flags); + ret =ttm_bo_vm_fault_reserved (vmf, prot,TTM_BO_VM_NUM_PREFAULT ); +if (ret ==VM_FAULT_RETRY && !(vmf->flags &FAULT_FLAG_RETRY_NOWAIT )) +return ret; +reservation_object_unlock (bo->resv);return ret; }
include/drm/ttm/ttm_bo_api.h +10 -0
@@ -768,4 +768,14 @@ int ttm_bo_swapout(struct ttm_bo_global *glob, struct ttm_operation_ctx *ctx);void ttm_bo_swapout_all (struct ttm_bo_device *bdev);int ttm_bo_wait_unreserved (struct ttm_buffer_object *bo); + +/* Default number of pre-faulted pages in the TTM fault handler */ +#define TTM_BO_VM_NUM_PREFAULT 16 + +vm_fault_t ttm_bo_vm_reserve (struct ttm_buffer_object *bo, +struct vm_fault *vmf); + +vm_fault_t ttm_bo_vm_fault_reserved (struct vm_fault *vmf, +pgprot_t prot, +pgoff_t num_prefault);#endif
Signature · 1 line
2.20.1
PATCH v4 6/9 drm/vmwgfx: Implement an infrastructure for write-coherent resources
In reply to Thomas Hellström (VMware)From: Thomas Hellstrom <[email protected]>
This infrastructure will, for coherent resources, make sure that from the user-space point of view, data written by the CPU is immediately automatically available to the GPU at resource validation time.
--- drivers/gpu/drm/vmwgfx/Kconfig | 1 + drivers/gpu/drm/vmwgfx/Makefile | 2 +- drivers/gpu/drm/vmwgfx/vmwgfx_bo.c | 5 +- drivers/gpu/drm/vmwgfx/vmwgfx_drv.c | 5 + drivers/gpu/drm/vmwgfx/vmwgfx_drv.h | 26 +- drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c | 1 - drivers/gpu/drm/vmwgfx/vmwgfx_page_dirty.c | 409 ++++++++++++++++++ drivers/gpu/drm/vmwgfx/vmwgfx_resource.c | 57 +++ drivers/gpu/drm/vmwgfx/vmwgfx_resource_priv.h | 11 + drivers/gpu/drm/vmwgfx/vmwgfx_validation.c | 71 +++ drivers/gpu/drm/vmwgfx/vmwgfx_validation.h | 16 +- 11 files changed, 584 insertions(+), 20 deletions(-) create mode 100644 drivers/gpu/drm/vmwgfx/vmwgfx_page_dirty.c
drivers/gpu/drm/vmwgfx/Kconfig +1 -0
@@ -8,6 +8,7 @@ config DRM_VMWGFX select FB_CFB_IMAGEBLIT select DRM_TTM select FB + select AS_DIRTY_HELPERS # Only needed for the transitional use of drm_crtc_init - can be removed # again once vmwgfx sets up the primary plane itself. select DRM_KMS_HELPER
drivers/gpu/drm/vmwgfx/Makefile +1 -1
@@ -8,7 +8,7 @@ vmwgfx-y := vmwgfx_execbuf.o vmwgfx_gmr.o vmwgfx_kms.o vmwgfx_drv.o \ vmwgfx_cmdbuf_res.o vmwgfx_cmdbuf.o vmwgfx_stdu.o \ vmwgfx_cotable.o vmwgfx_so.o vmwgfx_binding.o vmwgfx_msg.o \ vmwgfx_simple_resource.o vmwgfx_va.o vmwgfx_blit.o \ - vmwgfx_validation.o \ + vmwgfx_validation.o vmwgfx_page_dirty.o \ ttm_object.o ttm_lock.o obj-$(CONFIG_DRM_VMWGFX) := vmwgfx.o
drivers/gpu/drm/vmwgfx/vmwgfx_bo.c +4 -1
@@ -463,6 +463,7 @@ void vmw_bo_bo_free(struct ttm_buffer_object *bo) {struct vmw_buffer_object *vmw_bo =vmw_buffer_object (bo); +WARN_ON (vmw_bo->dirty);vmw_bo_unmap (vmw_bo);kfree (vmw_bo); }
@@ -476,8 +477,10 @@ void vmw_bo_bo_free(struct ttm_buffer_object *bo)static void vmw_user_bo_destroy (struct ttm_buffer_object *bo) {struct vmw_user_buffer_object *vmw_user_bo =vmw_user_buffer_object (bo); +struct vmw_buffer_object *vbo = &vmw_user_bo->vbo; -vmw_bo_unmap (&vmw_user_bo->vbo); +WARN_ON (vbo->dirty); +vmw_bo_unmap (vbo);ttm_prime_object_kfree (vmw_user_bo, prime); }
drivers/gpu/drm/vmwgfx/vmwgfx_drv.c +5 -0
@@ -833,6 +833,11 @@ static int vmw_driver_load(struct drm_device *dev, unsigned long chipset)DRM_ERROR ("Failed initializing TTM buffer object driver.\n" ); goto out_no_bdev; } + dev_priv->vm_ops = *dev_priv->bdev.vm_ops; + dev_priv->vm_ops.fault = vmw_bo_vm_fault; + dev_priv->vm_ops.pfn_mkwrite = vmw_bo_vm_mkwrite; + dev_priv->vm_ops.page_mkwrite = vmw_bo_vm_mkwrite; + dev_priv->bdev.vm_ops = &dev_priv->vm_ops;/* * Enable VRAM, but initially don't use it until SVGA is enabled and
drivers/gpu/drm/vmwgfx/vmwgfx_drv.h +23 -3
@@ -94,6 +94,7 @@ struct vmw_fpriv {* @dx_query_ctx: DX context if this buffer object is used as a DX query MOB * @map: Kmap object for semi-persistent mappings * @res_prios: Eviction priority counts for attached resources +* @dirty: structure for user-space dirty-tracking */ struct vmw_buffer_object {struct ttm_buffer_object base;
@@ -104,6 +105,7 @@ struct vmw_buffer_object {/* Protected by reservation */ struct ttm_bo_kmap_obj map;u32 res_prios[TTM_MAX_BO_PRIORITY ]; +struct vmw_bo_dirty *dirty; };/**
@@ -134,7 +136,8 @@ struct vmw_res_func;* @res_dirty: Resource contains data not yet in the backup buffer. Protected * by resource reserved. * @backup_dirty: Backup buffer contains data not yet in the HW resource. -* Protecte by resource reserved. +* Protected by resource reserved. +* @coherent: Emulate coherency by tracking vm accesses. * @backup: The backup buffer if any. Protected by resource reserved. * @backup_offset: Offset into the backup buffer if any. Protected by resource * reserved. Note that only a few resource types can have a @backup_offset
@@ -151,14 +154,16 @@ struct vmw_res_func;* @hw_destroy: Callback to destroy the resource on the device, as part of * resource destruction. */ +struct vmw_resource_dirty ;struct vmw_resource {struct kref kref;struct vmw_private *dev_priv;int id;u32 used_prio;unsigned long backup_size; -bool res_dirty; -bool backup_dirty; +u32 res_dirty :1 ; +u32 backup_dirty :1 ; +u32 coherent :1 ;struct vmw_buffer_object *backup;unsigned long backup_offset;unsigned long pin_count;
@@ -166,6 +171,7 @@ struct vmw_resource {struct list_head lru_head;struct list_head mob_head;struct list_head binding_head; +struct vmw_resource_dirty *dirty;void (*res_free) (struct vmw_resource *res);void (*hw_destroy) (struct vmw_resource *res); };
@@ -606,6 +612,9 @@ struct vmw_private {/* Validation memory reservation */ struct vmw_validation_mem vvm; + +/* VM operations */ +struct vm_operations_struct vm_ops; };static inline struct vmw_surface *vmw_res_to_srf (struct vmw_resource *res)
@@ -722,6 +731,8 @@ extern void vmw_resource_evict_all(struct vmw_private *dev_priv);extern void vmw_resource_unbind_list (struct vmw_buffer_object *vbo);void vmw_resource_mob_attach (struct vmw_resource *res);void vmw_resource_mob_detach (struct vmw_resource *res); +void vmw_resource_dirty_update (struct vmw_resource *res,pgoff_t start, +pgoff_t end);/** * vmw_resource_mob_attached - Whether a resource currently has a mob attached
@@ -1410,6 +1421,15 @@ int vmw_host_log(const char *log);#define VMW_DEBUG_USER (fmt, ...) \ DRM_DEBUG_DRIVER(fmt, ##__VA_ARGS__) +/* Resource dirtying - vmwgfx_page_dirty.c */ +void vmw_bo_dirty_scan (struct vmw_buffer_object *vbo); +int vmw_bo_dirty_add (struct vmw_buffer_object *vbo); +void vmw_bo_dirty_transfer_to_res (struct vmw_resource *res); +void vmw_bo_dirty_clear_res (struct vmw_resource *res); +void vmw_bo_dirty_release (struct vmw_buffer_object *vbo); +vm_fault_t vmw_bo_vm_fault (struct vm_fault *vmf); +vm_fault_t vmw_bo_vm_mkwrite (struct vm_fault *vmf); +/** * Inline helper functions */
drivers/gpu/drm/vmwgfx/vmwgfx_execbuf.c +0 -1
@@ -2560,7 +2560,6 @@ static int vmw_cmd_dx_check_subresource(struct vmw_private *dev_priv, offsetof(typeof(*cmd), sid)); cmd =container_of (header,typeof (*cmd), header); -return vmw_cmd_res_check (dev_priv, sw_context, vmw_res_surface,VMW_RES_DIRTY_NONE , user_surface_converter, &cmd->sid,NULL );
drivers/gpu/drm/vmwgfx/vmwgfx_page_dirty.c new file +409 -0
409 lines not on this page · raw message
drivers/gpu/drm/vmwgfx/vmwgfx_resource.c +57 -0
95 lines not on this page · raw message
drivers/gpu/drm/vmwgfx/vmwgfx_resource_priv.h +11 -0
@@ -71,6 +71,12 @@ struct vmw_user_resource_conv {* @commit_notify: If the resource is a command buffer managed resource, * callback to notify that a define or remove command * has been committed to the device. +* @dirty_alloc: Allocate a dirty tracker. NULL if dirty-tracking is not +* supported. +* @dirty_free: Free the dirty tracker. +* @dirty_sync: Upload the dirty mob contents to the resource. +* @dirty_add_range: Add a sequential dirty range to the resource +* dirty tracker. */ struct vmw_res_func {enum vmw_res_type res_type;
@@ -90,6 +96,11 @@ struct vmw_res_func { struct ttm_validate_buffer *val_buf);void (*commit_notify)(struct vmw_resource *res,enum vmw_cmdbuf_res_state state); +int (*dirty_alloc)(struct vmw_resource *res); +void (*dirty_free)(struct vmw_resource *res); +int (*dirty_sync)(struct vmw_resource *res); +void (*dirty_range_add)(struct vmw_resource *res,size_t start, +size_t end); };/**
drivers/gpu/drm/vmwgfx/vmwgfx_validation.c +71 -0
104 lines not on this page · raw message
drivers/gpu/drm/vmwgfx/vmwgfx_validation.h +2 -14
@@ -172,20 +172,6 @@ vmw_validation_bo_reserve(struct vmw_validation_context *ctx, NULL, true); } -/** -* vmw_validation_bo_backoff - Unreserve buffer objects registered with a -* validation context -* @ctx: The validation context -* -* This function unreserves the buffer objects previously reserved using -* vmw_validation_bo_reserve. It's typically used as part of an error path -*/ -static inline void -vmw_validation_bo_backoff (struct vmw_validation_context *ctx) -{ -ttm_eu_backoff_reservation (&ctx->ticket, &ctx->bo_list); -} -/** * vmw_validation_bo_fence - Unreserve and fence buffer objects registered * with a validation context
@@ -268,4 +254,6 @@ int vmw_validation_preload_res(struct vmw_validation_context *ctx, unsigned int size);void vmw_validation_res_set_dirty (struct vmw_validation_context *ctx,void *val_private,u32 dirty); +void vmw_validation_bo_backoff (struct vmw_validation_context *ctx); +#endif
Signature · 1 line
2.20.1
PATCH v4 7/9 drm/vmwgfx: Use an RBtree instead of linked list for MOB resources
In reply to Thomas Hellström (VMware)From: Thomas Hellstrom <[email protected]>
With emulated coherent memory we need to be able to quickly look up a resource from the MOB offset. Instead of traversing a linked list with O(n) worst case, use an RBtree with O(log n) worst case complexity.
--- drivers/gpu/drm/vmwgfx/vmwgfx_bo.c | 5 ++-- drivers/gpu/drm/vmwgfx/vmwgfx_drv.h | 10 +++---- drivers/gpu/drm/vmwgfx/vmwgfx_resource.c | 33 +++++++++++++++++------- 3 files changed, 32 insertions(+), 16 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_bo.c +3 -2
@@ -464,6 +464,7 @@ void vmw_bo_bo_free(struct ttm_buffer_object *bo)struct vmw_buffer_object *vmw_bo =vmw_buffer_object (bo);WARN_ON (vmw_bo->dirty); +WARN_ON (!RB_EMPTY_ROOT (&vmw_bo->res_tree));vmw_bo_unmap (vmw_bo);kfree (vmw_bo); }
@@ -480,6 +481,7 @@ static void vmw_user_bo_destroy(struct ttm_buffer_object *bo)struct vmw_buffer_object *vbo = &vmw_user_bo->vbo;WARN_ON (vbo->dirty); +WARN_ON (!RB_EMPTY_ROOT (&vbo->res_tree));vmw_bo_unmap (vbo);ttm_prime_object_kfree (vmw_user_bo, prime); }
@@ -515,8 +517,7 @@ int vmw_bo_init(struct vmw_private *dev_priv,memset (vmw_bo,0 ,sizeof (*vmw_bo));BUILD_BUG_ON (TTM_MAX_BO_PRIORITY <=3 ); vmw_bo->base.priority =3 ; - -INIT_LIST_HEAD (&vmw_bo->res_list); + vmw_bo->res_tree =RB_ROOT ; ret = ttm_bo_init(bdev, &vmw_bo->base, size, ttm_bo_type_device, placement,
drivers/gpu/drm/vmwgfx/vmwgfx_drv.h +5 -5
@@ -89,7 +89,7 @@ struct vmw_fpriv {/** * struct vmw_buffer_object - TTM buffer object with vmwgfx additions * @base: The TTM buffer object -* @res_list: List of resources using this buffer object as a backing MOB +* @res_tree: RB tree of resources using this buffer object as a backing MOB * @pin_count: pin depth * @dx_query_ctx: DX context if this buffer object is used as a DX query MOB * @map: Kmap object for semi-persistent mappings
@@ -98,7 +98,7 @@ struct vmw_fpriv {*/ struct vmw_buffer_object {struct ttm_buffer_object base; -struct list_head res_list; +struct rb_root res_tree;s32 pin_count;/* Not ref-counted. Protected by binding_mutex */ struct vmw_resource *dx_query_ctx;
@@ -146,8 +146,8 @@ struct vmw_res_func;* pin-count greater than zero. It is not on the resource LRU lists and its * backup buffer is pinned. Hence it can't be evicted. * @func: Method vtable for this resource. Immutable. +* @mob_node; Node for the MOB backup rbtree. Protected by @backup reserved. * @lru_head: List head for the LRU list. Protected by @dev_priv::resource_lock. -* @mob_head: List head for the MOB backup list. Protected by @backup reserved. * @binding_head: List head for the context binding list. Protected by * the @dev_priv::binding_mutex * @res_free: The resource destructor.
@@ -168,8 +168,8 @@ struct vmw_resource {unsigned long backup_offset;unsigned long pin_count;const struct vmw_res_func *func; +struct rb_node mob_node;struct list_head lru_head; -struct list_head mob_head;struct list_head binding_head;struct vmw_resource_dirty *dirty;void (*res_free) (struct vmw_resource *res);
@@ -742,7 +742,7 @@ void vmw_resource_dirty_update(struct vmw_resource *res, pgoff_t start,*/ static inline bool vmw_resource_mob_attached (const struct vmw_resource *res) { -return !list_empty (&res->mob_head); +return !RB_EMPTY_NODE (&res->mob_node); }/**
drivers/gpu/drm/vmwgfx/vmwgfx_resource.c +24 -9
@@ -41,11 +41,24 @@void vmw_resource_mob_attach (struct vmw_resource *res) {struct vmw_buffer_object *backup = res->backup; +struct rb_node **new = &backup->res_tree.rb_node, *parent =NULL ;lockdep_assert_held (&backup->base.resv->lock.base); res->used_prio = (res->res_dirty) ? res->func->dirty_prio : res->func->prio; -list_add_tail (&res->mob_head, &backup->res_list); + +while (*new) { +struct vmw_resource *this = + container_of(*new, struct vmw_resource, mob_node); + + parent = *new; + new = (res->backup_offset < this->backup_offset) ? + &((*new)->rb_left) : &((*new)->rb_right); + } + +rb_link_node (&res->mob_node, parent, new); +rb_insert_color (&res->mob_node, &backup->res_tree); +vmw_bo_prio_add (backup, res->used_prio); }
@@ -59,7 +72,8 @@ void vmw_resource_mob_detach(struct vmw_resource *res)lockdep_assert_held (&backup->base.resv->lock.base);if (vmw_resource_mob_attached (res)) { -list_del_init (&res->mob_head); +rb_erase (&res->mob_node, &backup->res_tree); +RB_CLEAR_NODE (&res->mob_node);vmw_bo_prio_del (backup, res->used_prio); } }
@@ -206,8 +220,8 @@ int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res, res->res_free = res_free; res->dev_priv = dev_priv; res->func = func; +RB_CLEAR_NODE (&res->mob_node);INIT_LIST_HEAD (&res->lru_head); -INIT_LIST_HEAD (&res->mob_head);INIT_LIST_HEAD (&res->binding_head); res->id =-1 ; res->backup =NULL ;
@@ -756,19 +770,20 @@ int vmw_resource_validate(struct vmw_resource *res, bool intr)*/ void vmw_resource_unbind_list (struct vmw_buffer_object *vbo) { - -struct vmw_resource *res, *next;struct ttm_validate_buffer val_buf = { .bo = &vbo->base, .num_shared =0 };lockdep_assert_held (&vbo->base.resv->lock.base); -list_for_each_entry_safe (res, next, &vbo->res_list, mob_head) { -if (!res->func->unbind) -continue ; +while (!RB_EMPTY_ROOT (&vbo->res_tree)) { +struct rb_node *node = vbo->res_tree.rb_node; +struct vmw_resource *res = + container_of(node, struct vmw_resource, mob_node); + +if (!WARN_ON_ONCE (!res->func->unbind)) + (void ) res->func->unbind (res, res->res_dirty, &val_buf); - (void ) res->func->unbind (res, res->res_dirty, &val_buf); res->backup_dirty = true; res->res_dirty = false;vmw_resource_mob_detach (res);
Signature · 1 line
2.20.1
PATCH v4 8/9 drm/vmwgfx: Implement an infrastructure for read-coherent resources
In reply to Thomas Hellström (VMware)From: Thomas Hellstrom <[email protected]>
Similar to write-coherent resources, make sure that from the user-space point of view, GPU rendered contents is automatically available for reading by the CPU.
--- drivers/gpu/drm/vmwgfx/vmwgfx_drv.h | 7 +- drivers/gpu/drm/vmwgfx/vmwgfx_page_dirty.c | 73 ++++++++++++- drivers/gpu/drm/vmwgfx/vmwgfx_resource.c | 103 +++++++++++++++++- drivers/gpu/drm/vmwgfx/vmwgfx_resource_priv.h | 2 + drivers/gpu/drm/vmwgfx/vmwgfx_validation.c | 3 +- 5 files changed, 177 insertions(+), 11 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_drv.h +6 -1
@@ -689,7 +689,8 @@ extern void vmw_resource_unreference(struct vmw_resource **p_res);extern struct vmw_resource *vmw_resource_reference (struct vmw_resource *res);extern struct vmw_resource *vmw_resource_reference_unless_doomed (struct vmw_resource *res); -extern int vmw_resource_validate (struct vmw_resource *res,bool intr); +extern int vmw_resource_validate (struct vmw_resource *res,bool intr, +bool dirtying);extern int vmw_resource_reserve (struct vmw_resource *res,bool interruptible,bool no_backup);extern bool vmw_resource_needs_backup (const struct vmw_resource *res);
@@ -733,6 +734,8 @@ void vmw_resource_mob_attach(struct vmw_resource *res);void vmw_resource_mob_detach (struct vmw_resource *res);void vmw_resource_dirty_update (struct vmw_resource *res,pgoff_t start,pgoff_t end); +int vmw_resources_clean (struct vmw_buffer_object *vbo,pgoff_t start, +pgoff_t end,pgoff_t *num_prefault);/** * vmw_resource_mob_attached - Whether a resource currently has a mob attached
@@ -1427,6 +1430,8 @@ int vmw_bo_dirty_add(struct vmw_buffer_object *vbo);void vmw_bo_dirty_transfer_to_res (struct vmw_resource *res);void vmw_bo_dirty_clear_res (struct vmw_resource *res);void vmw_bo_dirty_release (struct vmw_buffer_object *vbo); +void vmw_bo_dirty_unmap (struct vmw_buffer_object *vbo, +pgoff_t start,pgoff_t end);vm_fault_t vmw_bo_vm_fault (struct vm_fault *vmf);vm_fault_t vmw_bo_vm_mkwrite (struct vm_fault *vmf);
drivers/gpu/drm/vmwgfx/vmwgfx_page_dirty.c +68 -5
102 lines not on this page · raw message
drivers/gpu/drm/vmwgfx/vmwgfx_resource.c +99 -4
142 lines not on this page · raw message
drivers/gpu/drm/vmwgfx/vmwgfx_resource_priv.h +2 -0
@@ -77,6 +77,7 @@ struct vmw_user_resource_conv {* @dirty_sync: Upload the dirty mob contents to the resource. * @dirty_add_range: Add a sequential dirty range to the resource * dirty tracker. +* @clean: Clean the resource. */ struct vmw_res_func {enum vmw_res_type res_type;
@@ -101,6 +102,7 @@ struct vmw_res_func {int (*dirty_sync)(struct vmw_resource *res);void (*dirty_range_add)(struct vmw_resource *res,size_t start,size_t end); +int (*clean)(struct vmw_resource *res); };/**
drivers/gpu/drm/vmwgfx/vmwgfx_validation.c +2 -1
@@ -641,7 +641,8 @@ int vmw_validation_res_validate(struct vmw_validation_context *ctx, bool intr)struct vmw_resource *res = val->res;struct vmw_buffer_object *backup = res->backup; - ret =vmw_resource_validate (res, intr); + ret =vmw_resource_validate (res, intr, val->dirty_set && + val->dirty);if (ret) {if (ret != -ERESTARTSYS )DRM_ERROR ("Failed to validate resource.\n" );
Signature · 1 line
2.20.1
PATCH v4 9/9 drm/vmwgfx: Add surface dirty-tracking callbacks
In reply to Thomas Hellström (VMware)From: Thomas Hellstrom <[email protected]>
Add the callbacks necessary to implement emulated coherent memory for surfaces. Add a flag to the gb_surface_create ioctl to indicate that surface memory should be coherent.
Also bump the drm minor version to signal the availability of coherent surfaces.
--- .../device_include/svga3d_surfacedefs.h | 233 ++++++++++- drivers/gpu/drm/vmwgfx/vmwgfx_drv.h | 4 +- drivers/gpu/drm/vmwgfx/vmwgfx_surface.c | 395 +++++++++++++++++- include/uapi/drm/vmwgfx_drm.h | 4 +- 4 files changed, 629 insertions(+), 7 deletions(-)
drivers/gpu/drm/vmwgfx/device_include/svga3d_surfacedefs.h +232 -1
@@ -1280,7 +1280,6 @@ svga3dsurface_get_pixel_offset(SVGA3dSurfaceFormat format,return offset; } - static inline u32 svga3dsurface_get_image_offset(SVGA3dSurfaceFormat format, surf_size_struct baseLevelSize,
@@ -1375,4 +1374,236 @@ svga3dsurface_is_screen_target_format(SVGA3dSurfaceFormat format) return svga3dsurface_is_dx_screen_target_format(format); } +/** +* struct svga3dsurface_mip - Mimpmap level information +* @bytes: Bytes required in the backing store of this mipmap level. +* @img_stride: Byte stride per image. +* @row_stride: Byte stride per block row. +* @size: The size of the mipmap. +*/ +struct svga3dsurface_mip { +size_t bytes; +size_t img_stride; +size_t row_stride; +struct drm_vmw_size size; + +}; + +/** +* struct svga3dsurface_cache - Cached surface information +* @desc: Pointer to the surface descriptor +* @mip: Array of mipmap level information. Valid size is @num_mip_levels. +* @mip_chain_bytes: Bytes required in the backing store for the whole chain +* of mip levels. +* @sheet_bytes: Bytes required in the backing store for a sheet +* representing a single sample. +* @num_mip_levels: Valid size of the @mip array. Number of mipmap levels in +* a chain. +* @num_layers: Number of slices in an array texture or number of faces in +* a cubemap texture. +*/ +struct svga3dsurface_cache { +const struct svga3d_surface_desc *desc; +struct svga3dsurface_mip mip[DRM_VMW_MAX_MIP_LEVELS ]; +size_t mip_chain_bytes; +size_t sheet_bytes; +u32 num_mip_levels; +u32 num_layers; +}; + +/** +* struct svga3dsurface_loc - Surface location +* @sub_resource: Surface subresource. Defined as layer * num_mip_levels + +* mip_level. +* @x: X coordinate. +* @y: Y coordinate. +* @z: Z coordinate. +*/ +struct svga3dsurface_loc { +u32 sub_resource; +u32 x, y, z; +}; + +/** +* svga3dsurface_subres - Compute the subresource from layer and mipmap. +* @cache: Surface layout data. +* @mip_level: The mipmap level. +* @layer: The surface layer (face or array slice). +* +* Return: The subresource. +*/ +static inline u32 svga3dsurface_subres (const struct svga3dsurface_cache *cache, +u32 mip_level,u32 layer) +{ +return cache->num_mip_levels * layer + mip_level; +} + +/** +* svga3dsurface_setup_cache - Build a surface cache entry +* @size: The surface base level dimensions. +* @format: The surface format. +* @num_mip_levels: Number of mipmap levels. +* @num_layers: Number of layers. +* @cache: Pointer to a struct svga3dsurface_cach object to be filled in. +* +* Return: Zero on success, -EINVAL on invalid surface layout. +*/ +static inline int svga3dsurface_setup_cache (const struct drm_vmw_size *size, +SVGA3dSurfaceFormat format, +u32 num_mip_levels, +u32 num_layers, +u32 num_samples, +struct svga3dsurface_cache *cache) +{ +const struct svga3d_surface_desc *desc; +u32 i; + +memset (cache,0 ,sizeof (*cache)); + cache->desc = desc =svga3dsurface_get_desc (format); + cache->num_mip_levels = num_mip_levels; + cache->num_layers = num_layers; +for (i =0 ; i < cache->num_mip_levels; i++) { +struct svga3dsurface_mip *mip = &cache->mip[i]; + + mip->size =svga3dsurface_get_mip_size (*size, i); + mip->bytes =svga3dsurface_get_image_buffer_size + (desc, &mip->size,0 ); + mip->row_stride = +__KERNEL_DIV_ROUND_UP (mip->size.width, + desc->block_size.width) * + desc->bytes_per_block * num_samples; +if (!mip->row_stride) + goto invalid_dim; + + mip->img_stride = +__KERNEL_DIV_ROUND_UP (mip->size.height, + desc->block_size.height) * + mip->row_stride; +if (!mip->img_stride) + goto invalid_dim; + + cache->mip_chain_bytes += mip->bytes; + } + cache->sheet_bytes = cache->mip_chain_bytes * num_layers; +if (!cache->sheet_bytes) + goto invalid_dim; + +return 0 ; + +invalid_dim: +VMW_DEBUG_USER ("Invalid surface layout for dirty tracking.\n" ); +return -EINVAL ; +} + +/** +* svga3dsurface_get_loc - Get a surface location from an offset into the +* backing store +* @cache: Surface layout data. +* @loc: Pointer to a struct svga3dsurface_loc to be filled in. +* @offset: Offset into the surface backing store. +*/ +static inline void +svga3dsurface_get_loc (const struct svga3dsurface_cache *cache, +struct svga3dsurface_loc *loc, +size_t offset) +{ +const struct svga3dsurface_mip *mip = &cache->mip[0 ]; +const struct svga3d_surface_desc *desc = cache->desc; +u32 layer; +int i; + +if (offset >= cache->sheet_bytes) + offset %= cache->sheet_bytes; + + layer = offset / cache->mip_chain_bytes; + offset -= layer * cache->mip_chain_bytes; +for (i =0 ; i < cache->num_mip_levels; ++i, ++mip) { +if (mip->bytes > offset) +break ; + offset -= mip->bytes; + } + + loc->sub_resource =svga3dsurface_subres (cache, i, layer); + loc->z = offset / mip->img_stride; + offset -= loc->z * mip->img_stride; + loc->z *= desc->block_size.depth; + loc->y = offset / mip->row_stride; + offset -= loc->y * mip->row_stride; + loc->y *= desc->block_size.height; + loc->x = offset / desc->bytes_per_block; + loc->x *= desc->block_size.width; +} + +/** +* svga3dsurface_inc_loc - Clamp increment a surface location with one block +* size +* in each dimension. +* @loc: Pointer to a struct svga3dsurface_loc to be incremented. +* +* When computing the size of a range as size = end - start, the range does not +* include the end element. However a location representing the last byte +* of a touched region in the backing store *is* included in the range. +* This function modifies such a location to match the end definition +* given as start + size which is the one used in a SVGA3dBox. +*/ +static inline void +svga3dsurface_inc_loc (const struct svga3dsurface_cache *cache, +struct svga3dsurface_loc *loc) +{ +const struct svga3d_surface_desc *desc = cache->desc; +u32 mip = loc->sub_resource % cache->num_mip_levels; +const struct drm_vmw_size *size = &cache->mip[mip].size; + + loc->sub_resource++; + loc->x += desc->block_size.width; +if (loc->x > size->width) + loc->x = size->width; + loc->y += desc->block_size.height; +if (loc->y > size->height) + loc->y = size->height; + loc->z += desc->block_size.depth; +if (loc->z > size->depth) + loc->z = size->depth; +} + +/** +* svga3dsurface_min_loc - The start location in a subresource +* @cache: Surface layout data. +* @sub_resource: The subresource. +* @loc: Pointer to a struct svga3dsurface_loc to be filled in. +*/ +static inline void +svga3dsurface_min_loc (const struct svga3dsurface_cache *cache, +u32 sub_resource, +struct svga3dsurface_loc *loc) +{ + loc->sub_resource = sub_resource; + loc->x = loc->y = loc->z =0 ; +} + +/** +* svga3dsurface_min_loc - The end location in a subresource +* @cache: Surface layout data. +* @sub_resource: The subresource. +* @loc: Pointer to a struct svga3dsurface_loc to be filled in. +* +* Following the end definition given in svga3dsurface_inc_loc(), +* Compute the end location of a surface subresource. +*/ +static inline void +svga3dsurface_max_loc (const struct svga3dsurface_cache *cache, +u32 sub_resource, +struct svga3dsurface_loc *loc) +{ +const struct drm_vmw_size *size; +u32 mip; + + loc->sub_resource = sub_resource +1 ; + mip = sub_resource % cache->num_mip_levels; + size = &cache->mip[mip].size; + loc->x = size->width; + loc->y = size->height; + loc->z = size->depth; +} +#endif /* _SVGA3D_SURFACEDEFS_H_ */
drivers/gpu/drm/vmwgfx/vmwgfx_drv.h +2 -2
@@ -44,9 +44,9 @@#include <linux/sync_file.h> #define VMWGFX_DRIVER_NAME "vmwgfx" -#define VMWGFX_DRIVER_DATE "20180704" +#define VMWGFX_DRIVER_DATE "20190328"#define VMWGFX_DRIVER_MAJOR 2 -#define VMWGFX_DRIVER_MINOR 15 +#define VMWGFX_DRIVER_MINOR 16#define VMWGFX_DRIVER_PATCHLEVEL 0#define VMWGFX_FIFO_STATIC_SIZE (1024*1024)#define VMWGFX_MAX_RELOCATIONS 2048
drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +392 -3
443 lines not on this page · raw message
include/uapi/drm/vmwgfx_drm.h +3 -1
@@ -891,11 +891,13 @@ struct drm_vmw_shader_arg {* surface. * @drm_vmw_surface_flag_create_buffer: Create a backup buffer if none is * given. +* @drm_vmw_surface_flag_coherent: Back surface with coherent memory. */ enum drm_vmw_surface_flags { drm_vmw_surface_flag_shareable = (1 <<0 ), drm_vmw_surface_flag_scanout = (1 <<1 ), - drm_vmw_surface_flag_create_buffer = (1 <<2 ) + drm_vmw_surface_flag_create_buffer = (1 <<2 ), + drm_vmw_surface_flag_coherent = (1 <<3 ), };/**
Signature · 1 line
2.20.1
Re: [PATCH v4 3/9] mm: Add write-protect and clean utilities for address space ranges
In reply to Thomas Hellström (VMware)Quoted 4 lines · Thomas Hellström (VMware)
From: Thomas Hellstrom <[email protected]>
[ snip ]
Quoted 84 lines
mm/as_dirty_helpers.c new file +84
-0+/** +* apply_pt_wrprotect - Leaf pte callback to write-protect a pte +* @pte: Pointer to the pte +* @token: Page table token, see apply_to_pfn_range() +* @addr: The virtual page address +* @closure: Pointer to a struct pfn_range_apply embedded in a +* struct apply_as +* +* The function write-protects a pte and records the range in +* virtual address space of touched ptes for efficient range TLB flushes. +* +* Return: Always zero. +*/ +static int apply_pt_wrprotect (pte_t *pte,pgtable_t token, +unsigned long addr, +struct pfn_range_apply *closure) +{ +struct apply_as *aas =container_of (closure,typeof (*aas), base); +pte_t ptent = *pte; + +if (pte_write (ptent)) { +pte_t old_pte =ptep_modify_prot_start (aas->vma, addr, pte); + + ptent =pte_wrprotect (old_pte); +ptep_modify_prot_commit (aas->vma, addr, pte, old_pte, ptent); + aas->total++; + aas->start =min (aas->start, addr); + aas->end =max (aas->end, addr +PAGE_SIZE ); + } + +return 0 ; +} + +/** +* struct apply_as_clean - Closure structure for apply_as_clean +* @base: struct apply_as we derive from +* @bitmap_pgoff: Address_space Page offset of the first bit in @bitmap +* @bitmap: Bitmap with one bit for each page offset in the address_space range +* covered. +* @start: Address_space page offset of first modified pte relative +* to @bitmap_pgoff +* @end: Address_space page offset of last modified pte relative +* to @bitmap_pgoff +*/ +struct apply_as_clean { +struct apply_as base; +pgoff_t bitmap_pgoff; +unsigned long *bitmap; +pgoff_t start; +pgoff_t end; +}; + +/** +* apply_pt_clean - Leaf pte callback to clean a pte +* @pte: Pointer to the pte +* @token: Page table token, see apply_to_pfn_range() +* @addr: The virtual page address +* @closure: Pointer to a struct pfn_range_apply embedded in a +* struct apply_as_clean +* +* The function cleans a pte and records the range in +* virtual address space of touched ptes for efficient TLB flushes. +* It also records dirty ptes in a bitmap representing page offsets +* in the address_space, as well as the first and last of the bits +* touched. +* +* Return: Always zero. +*/ +static int apply_pt_clean (pte_t *pte,pgtable_t token, +unsigned long addr, +struct pfn_range_apply *closure) +{ +struct apply_as *aas =container_of (closure,typeof (*aas), base); +struct apply_as_clean *clean =container_of (aas,typeof (*clean), base); +pte_t ptent = *pte; + +if (pte_dirty (ptent)) { +pgoff_t pgoff = ((addr - aas->vma->vm_start) >>PAGE_SHIFT ) + + aas->vma->vm_pgoff - clean->bitmap_pgoff; +pte_t old_pte =ptep_modify_prot_start (aas->vma, addr, pte); + + ptent =pte_mkclean (old_pte); +ptep_modify_prot_commit (aas->vma, addr, pte, old_pte, ptent); +
mm/as_dirty_helpers.c line 119
Quoted 10 lines
mm/as_dirty_helpers.c new file +10
-0+ aas->total++; + aas->start =min (aas->start, addr); + aas->end =max (aas->end, addr +PAGE_SIZE ); + +__set_bit (pgoff, clean->bitmap); + clean->start =min (clean->start, pgoff); + clean->end =max (clean->end, pgoff +1 ); + } + +return 0 ;
Usually, when a PTE is write-protected, or when a dirty-bit is cleared, the TLB flush must be done while the page-table lock for that specific table is taken (i.e., within apply_pt_clean() and apply_pt_wrprotect() in this case).
Otherwise, in the case of apply_pt_clean() for example, another core might shortly after (before the TLB flush) write to the same page whose PTE was changed. The dirty-bit in such case might not be set, and the change get lost.
Does this function regards a certain use-case in which deferring the TLB flushes is fine? If so, assertions and documentation of the related assumption would be useful.
Re: [PATCH v4 3/9] mm: Add write-protect and clean utilities for address space ranges
Hi, Nadav,
Quoted 104 lines · Nadav Amit
Quoted 4 lines · Thomas Hellström (VMware)
From: Thomas Hellstrom <[email protected]>
[ snip ]
Quoted 84 lines
mm/as_dirty_helpers.c new file +84
-0+/** +* apply_pt_wrprotect - Leaf pte callback to write-protect a pte +* @pte: Pointer to the pte +* @token: Page table token, see apply_to_pfn_range() +* @addr: The virtual page address +* @closure: Pointer to a struct pfn_range_apply embedded in a +* struct apply_as +* +* The function write-protects a pte and records the range in +* virtual address space of touched ptes for efficient range TLB flushes. +* +* Return: Always zero. +*/ +static int apply_pt_wrprotect (pte_t *pte,pgtable_t token, +unsigned long addr, +struct pfn_range_apply *closure) +{ +struct apply_as *aas =container_of (closure,typeof (*aas), base); +pte_t ptent = *pte; + +if (pte_write (ptent)) { +pte_t old_pte =ptep_modify_prot_start (aas->vma, addr, pte); + + ptent =pte_wrprotect (old_pte); +ptep_modify_prot_commit (aas->vma, addr, pte, old_pte, ptent); + aas->total++; + aas->start =min (aas->start, addr); + aas->end =max (aas->end, addr +PAGE_SIZE ); + } + +return 0 ; +} + +/** +* struct apply_as_clean - Closure structure for apply_as_clean +* @base: struct apply_as we derive from +* @bitmap_pgoff: Address_space Page offset of the first bit in @bitmap +* @bitmap: Bitmap with one bit for each page offset in the address_space range +* covered. +* @start: Address_space page offset of first modified pte relative +* to @bitmap_pgoff +* @end: Address_space page offset of last modified pte relative +* to @bitmap_pgoff +*/ +struct apply_as_clean { +struct apply_as base; +pgoff_t bitmap_pgoff; +unsigned long *bitmap; +pgoff_t start; +pgoff_t end; +}; + +/** +* apply_pt_clean - Leaf pte callback to clean a pte +* @pte: Pointer to the pte +* @token: Page table token, see apply_to_pfn_range() +* @addr: The virtual page address +* @closure: Pointer to a struct pfn_range_apply embedded in a +* struct apply_as_clean +* +* The function cleans a pte and records the range in +* virtual address space of touched ptes for efficient TLB flushes. +* It also records dirty ptes in a bitmap representing page offsets +* in the address_space, as well as the first and last of the bits +* touched. +* +* Return: Always zero. +*/ +static int apply_pt_clean (pte_t *pte,pgtable_t token, +unsigned long addr, +struct pfn_range_apply *closure) +{ +struct apply_as *aas =container_of (closure,typeof (*aas), base); +struct apply_as_clean *clean =container_of (aas,typeof (*clean), base); +pte_t ptent = *pte; + +if (pte_dirty (ptent)) { +pgoff_t pgoff = ((addr - aas->vma->vm_start) >>PAGE_SHIFT ) + + aas->vma->vm_pgoff - clean->bitmap_pgoff; +pte_t old_pte =ptep_modify_prot_start (aas->vma, addr, pte); + + ptent =pte_mkclean (old_pte); +ptep_modify_prot_commit (aas->vma, addr, pte, old_pte, ptent); +mm/as_dirty_helpers.c line 119
Quoted 10 lines
mm/as_dirty_helpers.c new file +10
-0+ aas->total++; + aas->start =min (aas->start, addr); + aas->end =max (aas->end, addr +PAGE_SIZE ); + +__set_bit (pgoff, clean->bitmap); + clean->start =min (clean->start, pgoff); + clean->end =max (clean->end, pgoff +1 ); + } + +return 0 ;Usually, when a PTE is write-protected, or when a dirty-bit is cleared, the TLB flush must be done while the page-table lock for that specific table is taken (i.e., within
apply_pt_clean()andapply_pt_wrprotect()in this case).
Quoted 4 lines
Otherwise, in the case of
apply_pt_clean()for example, another core might shortly after (before the TLB flush) write to the same page whose PTE was changed. The dirty-bit in such case might not be set, and the change get lost.
Hmm. Let's assume that was the case, we have two possible situations:
A: pt_clean
1. That core's TLB entry is invalid. It will set the PTE dirty bit and continue. The dirty bit will probably remain set after the TLB flush.
2. That core's TLB entry is valid. It will just continue. The dirty bit will remain clear after the TLB flush.
But I fail to see how having the TLB flush within the page table lock would help in this case. Since the writing core will never attempt to take it? In any case, if such a race occurs, the corresponding bit in the bitmap would have been set and we've recorded that the page is dirty.
B: wrprotect situation, the situation is a bit different:
1. That core's TLB entry is invalid. It will read the PTE, cause a fault and block in mkwrite() on an external address space lock which is held over this operation. (Is it this situation that is your main concern?)
2. That core's TLB entry is valid. It will just continue regardless of any locks.
For both mkwrite() and dirty() if we act on the recorded pages *after* the TLB flush, we're OK. The difference is that just after the TLB flush there should be no write-enabled PTEs in the write-protect case, but there may be dirty PTEs in the pt_clean case. Something that is mentioned in the docs already.
Quoted 4 lines
Does this function regards a certain use-case in which deferring the TLB flushes is fine? If so, assertions and documentation of the related assumption would be useful.
If I understand your comment correctly, the page table lock is sometimes used as the lock in B1, blocking a possible software fault until the TLB flush has happened. Here we assume an external address space lock taken both around the wrprotect operation and in mkwrite(). Would it be OK if I add comments about the necessity of an external lock to the doc? Ok with a follow-up patch?
Thanks,
Thomas
Re: [PATCH v4 3/9] mm: Add write-protect and clean utilities for address space ranges
Quoted 113 lines · Thomas Hellström (VMware)
Hi, Nadav,
Quoted 103 lines · Nadav Amit
Quoted 3 lines · Thomas Hellström (VMware)
From: Thomas Hellstrom <[email protected]>
[ snip ]
Quoted 84 lines
mm/as_dirty_helpers.c new file +84
-0+/** +* apply_pt_wrprotect - Leaf pte callback to write-protect a pte +* @pte: Pointer to the pte +* @token: Page table token, see apply_to_pfn_range() +* @addr: The virtual page address +* @closure: Pointer to a struct pfn_range_apply embedded in a +* struct apply_as +* +* The function write-protects a pte and records the range in +* virtual address space of touched ptes for efficient range TLB flushes. +* +* Return: Always zero. +*/ +static int apply_pt_wrprotect (pte_t *pte,pgtable_t token, +unsigned long addr, +struct pfn_range_apply *closure) +{ +struct apply_as *aas =container_of (closure,typeof (*aas), base); +pte_t ptent = *pte; + +if (pte_write (ptent)) { +pte_t old_pte =ptep_modify_prot_start (aas->vma, addr, pte); + + ptent =pte_wrprotect (old_pte); +ptep_modify_prot_commit (aas->vma, addr, pte, old_pte, ptent); + aas->total++; + aas->start =min (aas->start, addr); + aas->end =max (aas->end, addr +PAGE_SIZE ); + } + +return 0 ; +} + +/** +* struct apply_as_clean - Closure structure for apply_as_clean +* @base: struct apply_as we derive from +* @bitmap_pgoff: Address_space Page offset of the first bit in @bitmap +* @bitmap: Bitmap with one bit for each page offset in the address_space range +* covered. +* @start: Address_space page offset of first modified pte relative +* to @bitmap_pgoff +* @end: Address_space page offset of last modified pte relative +* to @bitmap_pgoff +*/ +struct apply_as_clean { +struct apply_as base; +pgoff_t bitmap_pgoff; +unsigned long *bitmap; +pgoff_t start; +pgoff_t end; +}; + +/** +* apply_pt_clean - Leaf pte callback to clean a pte +* @pte: Pointer to the pte +* @token: Page table token, see apply_to_pfn_range() +* @addr: The virtual page address +* @closure: Pointer to a struct pfn_range_apply embedded in a +* struct apply_as_clean +* +* The function cleans a pte and records the range in +* virtual address space of touched ptes for efficient TLB flushes. +* It also records dirty ptes in a bitmap representing page offsets +* in the address_space, as well as the first and last of the bits +* touched. +* +* Return: Always zero. +*/ +static int apply_pt_clean (pte_t *pte,pgtable_t token, +unsigned long addr, +struct pfn_range_apply *closure) +{ +struct apply_as *aas =container_of (closure,typeof (*aas), base); +struct apply_as_clean *clean =container_of (aas,typeof (*clean), base); +pte_t ptent = *pte; + +if (pte_dirty (ptent)) { +pgoff_t pgoff = ((addr - aas->vma->vm_start) >>PAGE_SHIFT ) + + aas->vma->vm_pgoff - clean->bitmap_pgoff; +pte_t old_pte =ptep_modify_prot_start (aas->vma, addr, pte); + + ptent =pte_mkclean (old_pte); +ptep_modify_prot_commit (aas->vma, addr, pte, old_pte, ptent); +mm/as_dirty_helpers.c line 119
Quoted 10 lines
mm/as_dirty_helpers.c new file +10
-0+ aas->total++; + aas->start =min (aas->start, addr); + aas->end =max (aas->end, addr +PAGE_SIZE ); + +__set_bit (pgoff, clean->bitmap); + clean->start =min (clean->start, pgoff); + clean->end =max (clean->end, pgoff +1 ); + } + +return 0 ;Usually, when a PTE is write-protected, or when a dirty-bit is cleared, the TLB flush must be done while the page-table lock for that specific table is taken (i.e., within
apply_pt_clean()andapply_pt_wrprotect()in this case).Quoted 4 lines
Otherwise, in the case of
apply_pt_clean()for example, another core might shortly after (before the TLB flush) write to the same page whose PTE was changed. The dirty-bit in such case might not be set, and the change get lost.
Quoted 5 lines
Hmm. Let's assume that was the case, we have two possible situations:
A:
pt_clean1. That core's TLB entry is invalid. It will set the PTE dirty bit and continue. The dirty bit will probably remain set after the TLB flush.
I guess you mean the PTE is not cached in the TLB.
Quoted 3 lines
2. That core's TLB entry is valid. It will just continue. The dirty bit will remain clear after the TLB flush.
But I fail to see how having the TLB flush within the page table lock would help in this case. Since the writing core will never attempt to take it? In any case, if such a race occurs, the corresponding bit in the bitmap would have been set and we've recorded that the page is dirty.
I don’t understand. What do you mean “recorded that the page is dirty”? IIUC, the PTE is clear in this case - you mean PG_dirty is set?
To clarify, this code actually may work correctly on Intel CPUs, based on a recent discussion with Dave Hansen. Apparently, most Intel CPUs set the dirty bit in memory atomically when a page is first written.
But this is a generic code and not arch-specific. My concern is that a certain page might be written to, but would not be marked as dirty in either the bitmap or the PTE.
The practice of flushing cleaned/write-protected PTEs while hold the page-table lock related (sorry for my confusion).
Quoted 6 lines
B: wrprotect situation, the situation is a bit different:
1. That core's TLB entry is invalid. It will read the PTE, cause a fault and block in
mkwrite()on an external address space lock which is held over this operation. (Is it this situation that is your main concern?)
2. That core's TLB entry is valid. It will just continue regardless of any locks.For both
mkwrite()anddirty()if we act on the recorded pages *after* the TLB flush, we're OK. The difference is that just after the TLB flush there should be no write-enabled PTEs in the write-protect case, but there may be dirty PTEs in thept_cleancase. Something that is mentioned in the docs already.
The wrprotect might work correctly, I guess. It does work to mprotect() (again, sorry for confusing).
Quoted 5 lines
Quoted 3 lines
Does this function regards a certain use-case in which deferring the TLB flushes is fine? If so, assertions and documentation of the related assumption would be useful.
If I understand your comment correctly, the page table lock is sometimes used as the lock in B1, blocking a possible software fault until the TLB flush has happened. Here we assume an external address space lock taken both around the wrprotect operation and in
mkwrite(). Would it be OK if I add comments about the necessity of an external lock to the doc? Ok with a follow-up patch?
I think the patch should explain itself. I think the comment:
Quoted 3 lines
+ * WARNING: This function should only be used for address spaces that
+ * completely own the pages / memory the page table points to. Typically a
+ * device file.
... should be more concrete (define address spaces that completely own memory), and possibly backed by an (debug) assertion to ensure that it is only used correctly.
Re: [PATCH v4 3/9] mm: Add write-protect and clean utilities for address space ranges
Quoted 112 lines · Nadav Amit
Quoted 112 lines · Thomas Hellström (VMware)
Hi, Nadav,
Quoted 107 lines · Nadav Amit
Quoted 3 lines · Thomas Hellström (VMware)
From: Thomas Hellstrom <[email protected]>
[ snip ]
Quoted 84 lines
mm/as_dirty_helpers.c new file +84
-0+/** +* apply_pt_wrprotect - Leaf pte callback to write-protect a pte +* @pte: Pointer to the pte +* @token: Page table token, see apply_to_pfn_range() +* @addr: The virtual page address +* @closure: Pointer to a struct pfn_range_apply embedded in a +* struct apply_as +* +* The function write-protects a pte and records the range in +* virtual address space of touched ptes for efficient range TLB flushes. +* +* Return: Always zero. +*/ +static int apply_pt_wrprotect (pte_t *pte,pgtable_t token, +unsigned long addr, +struct pfn_range_apply *closure) +{ +struct apply_as *aas =container_of (closure,typeof (*aas), base); +pte_t ptent = *pte; + +if (pte_write (ptent)) { +pte_t old_pte =ptep_modify_prot_start (aas->vma, addr, pte); + + ptent =pte_wrprotect (old_pte); +ptep_modify_prot_commit (aas->vma, addr, pte, old_pte, ptent); + aas->total++; + aas->start =min (aas->start, addr); + aas->end =max (aas->end, addr +PAGE_SIZE ); + } + +return 0 ; +} + +/** +* struct apply_as_clean - Closure structure for apply_as_clean +* @base: struct apply_as we derive from +* @bitmap_pgoff: Address_space Page offset of the first bit in @bitmap +* @bitmap: Bitmap with one bit for each page offset in the address_space range +* covered. +* @start: Address_space page offset of first modified pte relative +* to @bitmap_pgoff +* @end: Address_space page offset of last modified pte relative +* to @bitmap_pgoff +*/ +struct apply_as_clean { +struct apply_as base; +pgoff_t bitmap_pgoff; +unsigned long *bitmap; +pgoff_t start; +pgoff_t end; +}; + +/** +* apply_pt_clean - Leaf pte callback to clean a pte +* @pte: Pointer to the pte +* @token: Page table token, see apply_to_pfn_range() +* @addr: The virtual page address +* @closure: Pointer to a struct pfn_range_apply embedded in a +* struct apply_as_clean +* +* The function cleans a pte and records the range in +* virtual address space of touched ptes for efficient TLB flushes. +* It also records dirty ptes in a bitmap representing page offsets +* in the address_space, as well as the first and last of the bits +* touched. +* +* Return: Always zero. +*/ +static int apply_pt_clean (pte_t *pte,pgtable_t token, +unsigned long addr, +struct pfn_range_apply *closure) +{ +struct apply_as *aas =container_of (closure,typeof (*aas), base); +struct apply_as_clean *clean =container_of (aas,typeof (*clean), base); +pte_t ptent = *pte; + +if (pte_dirty (ptent)) { +pgoff_t pgoff = ((addr - aas->vma->vm_start) >>PAGE_SHIFT ) + + aas->vma->vm_pgoff - clean->bitmap_pgoff; +pte_t old_pte =ptep_modify_prot_start (aas->vma, addr, pte); + + ptent =pte_mkclean (old_pte); +ptep_modify_prot_commit (aas->vma, addr, pte, old_pte, ptent); +mm/as_dirty_helpers.c line 119
Quoted 10 lines
mm/as_dirty_helpers.c new file +10
-0+ aas->total++; + aas->start =min (aas->start, addr); + aas->end =max (aas->end, addr +PAGE_SIZE ); + +__set_bit (pgoff, clean->bitmap); + clean->start =min (clean->start, pgoff); + clean->end =max (clean->end, pgoff +1 ); + } + +return 0 ;Usually, when a PTE is write-protected, or when a dirty-bit is cleared, the TLB flush must be done while the page-table lock for that specific table is taken (i.e., within
apply_pt_clean()andapply_pt_wrprotect()in this case).Otherwise, in the case of
apply_pt_clean()for example, another core might shortly after (before the TLB flush) write to the same page whose PTE was changed. The dirty-bit in such case might not be set, and the change get lost.
Quoted 6 lines
Quoted 5 lines
Hmm. Let's assume that was the case, we have two possible situations:
A:
pt_clean1. That core's TLB entry is invalid. It will set the PTE dirty bit and continue. The dirty bit will probably remain set after the TLB flush.
I guess you mean the PTE is not cached in the TLB.
Yes.
Quoted 6 lines
Quoted 4 lines
2. That core's TLB entry is valid. It will just continue. The dirty bit will remain clear after the TLB flush.
But I fail to see how having the TLB flush within the page table lock would help in this case. Since the writing core will never attempt to take it? In any case, if such a race occurs, the corresponding bit in the bitmap would have been set and we've recorded that the page is dirty.
I don’t understand. What do you mean “recorded that the page is dirty”? IIUC, the PTE is clear in this case - you mean
PG_dirtyis set?
All PTEs we touch and clean are noted in the bitmap.
Quoted 8 lines
To clarify, this code actually may work correctly on Intel CPUs, based on a recent discussion with Dave Hansen. Apparently, most Intel CPUs set the dirty bit in memory atomically when a page is first written.
But this is a generic code and not arch-specific. My concern is that a certain page might be written to, but would not be marked as dirty in either the bitmap or the PTE.
Regardless of arch, we have four cases:
1. Writes occuring before we scan (and possibly modify) the PTE. Should be handled correctly.
2. Writes occurning after the TLB flush. Should be handled correctly, unless after a TLB flush the TLB cached entry and the PTE differs on the dirty bit. Then we could in theory go on writing without marking the PTE dirty. But that would probably be an arch code bug: I mean anything using tlb_gather_mmu() would flush TLB outside of the page table lock, and if, for example, unmap_mapping_range() left the TLB entries and the PTES in an inconsistent state, that wouldn't be good.
3. Writes occuring after the PTE scan, but before the TLB flush without us modifying the PTE: That would be the same as a spurious TLB flush. It should be harmless. The write will not be picked up in the bitmap, but the PTE dirty bit will be set.
4. Writes occuring after us clearing the dirty bit and before the TLB flush: We will detect the write, since the bitmap bit is already set. If the write continues after the TLB flush, we go to 2.
Note, for archs doing software PTE_DIRTY, that would be very similar to softdirty, which is also doing batched TLB flushes...
Quoted 17 lines
The practice of flushing cleaned/write-protected PTEs while hold the page-table lock related (sorry for my confusion).
Quoted 6 lines
B: wrprotect situation, the situation is a bit different:
1. That core's TLB entry is invalid. It will read the PTE, cause a fault and block in
mkwrite()on an external address space lock which is held over this operation. (Is it this situation that is your main concern?)
2. That core's TLB entry is valid. It will just continue regardless of any locks.For both
mkwrite()anddirty()if we act on the recorded pages *after* the TLB flush, we're OK. The difference is that just after the TLB flush there should be no write-enabled PTEs in the write-protect case, but there may be dirty PTEs in thept_cleancase. Something that is mentioned in the docs already.The wrprotect might work correctly, I guess. It does work to
mprotect()(again, sorry for confusing).Quoted 4 lines
Quoted 3 lines
Does this function regards a certain use-case in which deferring the TLB flushes is fine? If so, assertions and documentation of the related assumption would be useful.
If I understand your comment correctly, the page table lock is sometimes used as the lock in B1, blocking a possible software fault until the TLB flush has happened. Here we assume an external address space lock taken both around the wrprotect operation and in
mkwrite(). Would it be OK if I add comments about the necessity of an external lock to the doc? Ok with a follow-up patch?
Quoted 8 lines
I think the patch should explain itself. I think the comment:
Quoted 3 lines
+ * WARNING: This function should only be used for address spaces that
+ * completely own the pages / memory the page table points to. Typically a
+ * device file.... should be more concrete (define address spaces that completely own memory), and possibly backed by an (debug) assertion to ensure that it is only used correctly.
Agreed. To clarify we should only run this on VM_IO vmas without (trans)huge pages, for which there are already checks. I'll update the doc.
/Thomas
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [PATCH v4 3/9] mm: Add write-protect and clean utilities for address space ranges
Quoted 138 lines · Thomas Hellström (VMware)
Quoted 112 lines · Nadav Amit
Quoted 112 lines · Thomas Hellström (VMware)
Hi, Nadav,
Quoted 107 lines · Nadav Amit
Quoted 3 lines · Thomas Hellström (VMware)
From: Thomas Hellstrom <[email protected]>
[ snip ]
Quoted 84 lines
mm/as_dirty_helpers.c new file +84
-0+/** +* apply_pt_wrprotect - Leaf pte callback to write-protect a pte +* @pte: Pointer to the pte +* @token: Page table token, see apply_to_pfn_range() +* @addr: The virtual page address +* @closure: Pointer to a struct pfn_range_apply embedded in a +* struct apply_as +* +* The function write-protects a pte and records the range in +* virtual address space of touched ptes for efficient range TLB flushes. +* +* Return: Always zero. +*/ +static int apply_pt_wrprotect (pte_t *pte,pgtable_t token, +unsigned long addr, +struct pfn_range_apply *closure) +{ +struct apply_as *aas =container_of (closure,typeof (*aas), base); +pte_t ptent = *pte; + +if (pte_write (ptent)) { +pte_t old_pte =ptep_modify_prot_start (aas->vma, addr, pte); + + ptent =pte_wrprotect (old_pte); +ptep_modify_prot_commit (aas->vma, addr, pte, old_pte, ptent); + aas->total++; + aas->start =min (aas->start, addr); + aas->end =max (aas->end, addr +PAGE_SIZE ); + } + +return 0 ; +} + +/** +* struct apply_as_clean - Closure structure for apply_as_clean +* @base: struct apply_as we derive from +* @bitmap_pgoff: Address_space Page offset of the first bit in @bitmap +* @bitmap: Bitmap with one bit for each page offset in the address_space range +* covered. +* @start: Address_space page offset of first modified pte relative +* to @bitmap_pgoff +* @end: Address_space page offset of last modified pte relative +* to @bitmap_pgoff +*/ +struct apply_as_clean { +struct apply_as base; +pgoff_t bitmap_pgoff; +unsigned long *bitmap; +pgoff_t start; +pgoff_t end; +}; + +/** +* apply_pt_clean - Leaf pte callback to clean a pte +* @pte: Pointer to the pte +* @token: Page table token, see apply_to_pfn_range() +* @addr: The virtual page address +* @closure: Pointer to a struct pfn_range_apply embedded in a +* struct apply_as_clean +* +* The function cleans a pte and records the range in +* virtual address space of touched ptes for efficient TLB flushes. +* It also records dirty ptes in a bitmap representing page offsets +* in the address_space, as well as the first and last of the bits +* touched. +* +* Return: Always zero. +*/ +static int apply_pt_clean (pte_t *pte,pgtable_t token, +unsigned long addr, +struct pfn_range_apply *closure) +{ +struct apply_as *aas =container_of (closure,typeof (*aas), base); +struct apply_as_clean *clean =container_of (aas,typeof (*clean), base); +pte_t ptent = *pte; + +if (pte_dirty (ptent)) { +pgoff_t pgoff = ((addr - aas->vma->vm_start) >>PAGE_SHIFT ) + + aas->vma->vm_pgoff - clean->bitmap_pgoff; +pte_t old_pte =ptep_modify_prot_start (aas->vma, addr, pte); + + ptent =pte_mkclean (old_pte); +ptep_modify_prot_commit (aas->vma, addr, pte, old_pte, ptent); +mm/as_dirty_helpers.c line 119
Quoted 10 lines
mm/as_dirty_helpers.c new file +10
-0+ aas->total++; + aas->start =min (aas->start, addr); + aas->end =max (aas->end, addr +PAGE_SIZE ); + +__set_bit (pgoff, clean->bitmap); + clean->start =min (clean->start, pgoff); + clean->end =max (clean->end, pgoff +1 ); + } + +return 0 ;Usually, when a PTE is write-protected, or when a dirty-bit is cleared, the TLB flush must be done while the page-table lock for that specific table is taken (i.e., within
apply_pt_clean()andapply_pt_wrprotect()in this case).Otherwise, in the case of
apply_pt_clean()for example, another core might shortly after (before the TLB flush) write to the same page whose PTE was changed. The dirty-bit in such case might not be set, and the change get lost.Quoted 6 lines
Quoted 5 lines
Hmm. Let's assume that was the case, we have two possible situations:
A:
pt_clean1. That core's TLB entry is invalid. It will set the PTE dirty bit and continue. The dirty bit will probably remain set after the TLB flush.
I guess you mean the PTE is not cached in the TLB.
Yes.
Quoted 5 lines
Quoted 3 lines
2. That core's TLB entry is valid. It will just continue. The dirty bit will remain clear after the TLB flush.
But I fail to see how having the TLB flush within the page table lock would help in this case. Since the writing core will never attempt to take it? In any case, if such a race occurs, the corresponding bit in the bitmap would have been set and we've recorded that the page is dirty.
I don’t understand. What do you mean “recorded that the page is dirty”? IIUC, the PTE is clear in this case - you mean
PG_dirtyis set?All PTEs we touch and clean are noted in the bitmap.
Quoted 7 lines
To clarify, this code actually may work correctly on Intel CPUs, based on a recent discussion with Dave Hansen. Apparently, most Intel CPUs set the dirty bit in memory atomically when a page is first written.
But this is a generic code and not arch-specific. My concern is that a certain page might be written to, but would not be marked as dirty in either the bitmap or the PTE.
Quoted 5 lines
Regardless of arch, we have four cases:
1. Writes occuring before we scan (and possibly modify) the PTE. Should be handled correctly.
2. Writes occurning after the TLB flush. Should be handled correctly, unless after a TLB flush the TLB cached entry and the PTE differs on the dirty bit. Then we could in theory go on writing without marking the PTE dirty. But that would probably be an arch code bug: I mean anything usingtlb_gather_mmu()would flush TLB outside of the page table lock, and if, for example,unmap_mapping_range()left the TLB entries and the PTES in an inconsistent state, that wouldn't be good.
3. Writes occuring after the PTE scan, but before the TLB flush without us modifying the PTE: That would be the same as a spurious TLB flush. It should be harmless. The write will not be picked up in the bitmap, but the PTE dirty bit will be set.
4. Writes occuring after us clearing the dirty bit and before the TLB flush: We will detect the write, since the bitmap bit is already set. If the write continues after the TLB flush, we go to 2.
Thanks for the detailed explanation. It does sound reasonable.
Quoted 1 line
Note, for archs doing software
PTE_DIRTY, that would be very similar to softdirty, which is also doing batched TLB flushes…
Somewhat similar. But AFAIK, soft-dirty allows you to do only one of two things:
- Clear the refs ( using /proc/[pid]/clear_refs ); or
- Read the refs (using /proc/[pid]/pagemap )
This interface does not provide any atomicity like the one you try to obtain.