Skip to contentSkip to conversation

drm/ttm: Fix COW check

PATCH Superseded Felix Kuehling 2 participants 2 messages Jul 10, 2021 – Jul 12, 2021

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/ttm: Fix COW check

KFD Thunk maps invisible VRAM BOs with PROT_NONE, MAP_PRIVATE. is_cow_mapping returns true for these mappings. Add a check for vm_flags & VM_WRITE to avoid mmap failures on private read-only or PROT_NONE mappings.

v2: protect against mprotect making a mapping writable after the fact

Fixes f91142c62161 ("drm/ttm: nuke VM_MIXEDMAP on BO mappings v3")
---
 drivers/gpu/drm/ttm/ttm_bo_vm.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_bo_vm.c +12 -1
index f56be5bc0861..9ad211ede611 100644
@@ -542,17 +542,28 @@ int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
 }
 EXPORT_SYMBOL(ttm_bo_vm_access);
 
+static int ttm_bo_vm_mprotect(struct vm_area_struct *vma, unsigned long start,
+			      unsigned long end, unsigned long newflags)
+{
+	/* Enforce no COW since would have really strange behavior with it. */
+	if (is_cow_mapping(newflags) && (newflags & VM_WRITE))
+		return -EINVAL;
+
+	return 0;
+}
+
 static const struct vm_operations_struct ttm_bo_vm_ops = {
 	.fault = ttm_bo_vm_fault,
 	.open = ttm_bo_vm_open,
 	.close = ttm_bo_vm_close,
 	.access = ttm_bo_vm_access,
+	.mprotect = ttm_bo_vm_mprotect,
 };
 
 int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
 {
 	/* Enforce no COW since would have really strange behavior with it. */
-	if (is_cow_mapping(vma->vm_flags))
+	if (is_cow_mapping(vma->vm_flags) && (vma->vm_flags & VM_WRITE))
 		return -EINVAL;
 
 	ttm_bo_get(bo);
Signature · 6 lines
2.32.0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#1 lore raw

Am 10.07.21 um 02:28 schrieb Felix Kuehling:

Quoted 28 lines

KFD Thunk maps invisible VRAM BOs with PROT_NONE, MAP_PRIVATE. is_cow_mapping returns true for these mappings. Add a check for vm_flags & VM_WRITE to avoid mmap failures on private read-only or PROT_NONE mappings.

v2: protect against mprotect making a mapping writable after the fact

Fixes f91142c62161 ("drm/ttm: nuke VM_MIXEDMAP on BO mappings v3")
---
  drivers/gpu/drm/ttm/ttm_bo_vm.c | 13 ++++++++++++-
  1 file changed, 12 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_bo_vm.c +6 -0
index f56be5bc0861..9ad211ede611 100644
@@ -542,17 +542,28 @@ int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
  }
  EXPORT_SYMBOL(ttm_bo_vm_access);
 
+static int ttm_bo_vm_mprotect(struct vm_area_struct *vma, unsigned long start,
+			      unsigned long end, unsigned long newflags)
+{
+	/* Enforce no COW since would have really strange behavior with it. */
+	if (is_cow_mapping(newflags) && (newflags & VM_WRITE))
+		return -EINVAL;

drivers/gpu/drm/ttm/ttm_bo_vm.c int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr, · line 560

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +5 -0
+
+	return 0;
+}
+
  static const struct vm_operations_struct ttm_bo_vm_ops = {
  	.fault = ttm_bo_vm_fault,
  	.open = ttm_bo_vm_open,
  	.close = ttm_bo_vm_close,
  	.access = ttm_bo_vm_access,
+	.mprotect = ttm_bo_vm_mprotect,

The problem is most drivers implement their own copy of this structure.

You need to add this to those occasions as well, just search for references to ttm_bo_vm_open.

Regards,
Christian.

drivers/gpu/drm/ttm/ttm_bo_vm.c int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr, · line 569

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +1 -1
  };
 
  int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
  {
  	/* Enforce no COW since would have really strange behavior with it. */
-	if (is_cow_mapping(vma->vm_flags))
+	if (is_cow_mapping(vma->vm_flags) && (vma->vm_flags & VM_WRITE))
  		return -EINVAL;
 
  	ttm_bo_get(bo);

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx