Re: [PATCH 1/5] drm/amdgpu: Avoid reclaim fs while eviction lock

2020-01-02 Thread Yong Zhao

One comment inline.

On 2020-01-02 4:11 p.m., Alex Sierra wrote:

[Why]
Avoid reclaim filesystem while eviction lock is held called from
MMU notifier.

[How]
Setting PF_MEMALLOC_NOFS flags while eviction mutex is locked.
Using memalloc_nofs_save / memalloc_nofs_restore API.

Change-Id: I5531c9337836e7d4a430df3f16dcc82888e8018c
Signed-off-by: Alex Sierra 
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 40 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  6 +++-
  2 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index b999b67ff57a..d6aba4f9df74 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -82,6 +82,32 @@ struct amdgpu_prt_cb {
struct dma_fence_cb cb;
  };
  
+/**

+ * vm eviction_lock can be taken in MMU notifiers. Make sure no reclaim-FS
+ * happens while holding this lock anywhere to prevent deadlocks when
+ * an MMU notifier runs in reclaim-FS context.
+ */
+static inline void amdgpu_vm_eviction_lock(struct amdgpu_vm *vm)
+{
+   mutex_lock(>eviction_lock);
+   vm->saved_flags = memalloc_nofs_save();
[yz] I feel memalloc_nofs_save() should be called before mutex_lock(). 
Not too sure though.

+}
+
+static inline int amdgpu_vm_eviction_trylock(struct amdgpu_vm *vm)
+{
+   if (mutex_trylock(>eviction_lock)) {
+   vm->saved_flags = memalloc_nofs_save();
+   return 1;
+   }
+   return 0;
+}
+
+static inline void amdgpu_vm_eviction_unlock(struct amdgpu_vm *vm)
+{
+   memalloc_nofs_restore(vm->saved_flags);
+   mutex_unlock(>eviction_lock);
+}
+
  /**
   * amdgpu_vm_level_shift - return the addr shift for each level
   *
@@ -678,9 +704,9 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, 
struct amdgpu_vm *vm,
}
}
  
-	mutex_lock(>eviction_lock);

+   amdgpu_vm_eviction_lock(vm);
vm->evicting = false;
-   mutex_unlock(>eviction_lock);
+   amdgpu_vm_eviction_unlock(vm);
  
  	return 0;

  }
@@ -1559,7 +1585,7 @@ static int amdgpu_vm_bo_update_mapping(struct 
amdgpu_device *adev,
if (!(flags & AMDGPU_PTE_VALID))
owner = AMDGPU_FENCE_OWNER_KFD;
  
-	mutex_lock(>eviction_lock);

+   amdgpu_vm_eviction_lock(vm);
if (vm->evicting) {
r = -EBUSY;
goto error_unlock;
@@ -1576,7 +1602,7 @@ static int amdgpu_vm_bo_update_mapping(struct 
amdgpu_device *adev,
r = vm->update_funcs->commit(, fence);
  
  error_unlock:

-   mutex_unlock(>eviction_lock);
+   amdgpu_vm_eviction_unlock(vm);
return r;
  }
  
@@ -2537,18 +2563,18 @@ bool amdgpu_vm_evictable(struct amdgpu_bo *bo)

return false;
  
  	/* Try to block ongoing updates */

-   if (!mutex_trylock(_base->vm->eviction_lock))
+   if (!amdgpu_vm_eviction_trylock(bo_base->vm))
return false;
  
  	/* Don't evict VM page tables while they are updated */

if (!dma_fence_is_signaled(bo_base->vm->last_direct) ||
!dma_fence_is_signaled(bo_base->vm->last_delayed)) {
-   mutex_unlock(_base->vm->eviction_lock);
+   amdgpu_vm_eviction_unlock(bo_base->vm);
return false;
}
  
  	bo_base->vm->evicting = true;

-   mutex_unlock(_base->vm->eviction_lock);
+   amdgpu_vm_eviction_unlock(bo_base->vm);
return true;
  }
  
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h

index 100547f094ff..c21a36bebc0c 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
@@ -30,6 +30,7 @@
  #include 
  #include 
  #include 
+#include 
  
  #include "amdgpu_sync.h"

  #include "amdgpu_ring.h"
@@ -242,9 +243,12 @@ struct amdgpu_vm {
/* tree of virtual addresses mapped */
struct rb_root_cached   va;
  
-	/* Lock to prevent eviction while we are updating page tables */

+   /* Lock to prevent eviction while we are updating page tables
+* use vm_eviction_lock/unlock(vm)
+*/
struct mutexeviction_lock;
boolevicting;
+   unsigned intsaved_flags;
  
  	/* BOs who needs a validation */

struct list_headevicted;

___
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx


[PATCH 1/5] drm/amdgpu: Avoid reclaim fs while eviction lock

2020-01-02 Thread Alex Sierra
[Why]
Avoid reclaim filesystem while eviction lock is held called from
MMU notifier.

[How]
Setting PF_MEMALLOC_NOFS flags while eviction mutex is locked.
Using memalloc_nofs_save / memalloc_nofs_restore API.

Change-Id: I5531c9337836e7d4a430df3f16dcc82888e8018c
Signed-off-by: Alex Sierra 
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 40 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  6 +++-
 2 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index b999b67ff57a..d6aba4f9df74 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -82,6 +82,32 @@ struct amdgpu_prt_cb {
struct dma_fence_cb cb;
 };
 
+/**
+ * vm eviction_lock can be taken in MMU notifiers. Make sure no reclaim-FS
+ * happens while holding this lock anywhere to prevent deadlocks when
+ * an MMU notifier runs in reclaim-FS context.
+ */
+static inline void amdgpu_vm_eviction_lock(struct amdgpu_vm *vm)
+{
+   mutex_lock(>eviction_lock);
+   vm->saved_flags = memalloc_nofs_save();
+}
+
+static inline int amdgpu_vm_eviction_trylock(struct amdgpu_vm *vm)
+{
+   if (mutex_trylock(>eviction_lock)) {
+   vm->saved_flags = memalloc_nofs_save();
+   return 1;
+   }
+   return 0;
+}
+
+static inline void amdgpu_vm_eviction_unlock(struct amdgpu_vm *vm)
+{
+   memalloc_nofs_restore(vm->saved_flags);
+   mutex_unlock(>eviction_lock);
+}
+
 /**
  * amdgpu_vm_level_shift - return the addr shift for each level
  *
@@ -678,9 +704,9 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, 
struct amdgpu_vm *vm,
}
}
 
-   mutex_lock(>eviction_lock);
+   amdgpu_vm_eviction_lock(vm);
vm->evicting = false;
-   mutex_unlock(>eviction_lock);
+   amdgpu_vm_eviction_unlock(vm);
 
return 0;
 }
@@ -1559,7 +1585,7 @@ static int amdgpu_vm_bo_update_mapping(struct 
amdgpu_device *adev,
if (!(flags & AMDGPU_PTE_VALID))
owner = AMDGPU_FENCE_OWNER_KFD;
 
-   mutex_lock(>eviction_lock);
+   amdgpu_vm_eviction_lock(vm);
if (vm->evicting) {
r = -EBUSY;
goto error_unlock;
@@ -1576,7 +1602,7 @@ static int amdgpu_vm_bo_update_mapping(struct 
amdgpu_device *adev,
r = vm->update_funcs->commit(, fence);
 
 error_unlock:
-   mutex_unlock(>eviction_lock);
+   amdgpu_vm_eviction_unlock(vm);
return r;
 }
 
@@ -2537,18 +2563,18 @@ bool amdgpu_vm_evictable(struct amdgpu_bo *bo)
return false;
 
/* Try to block ongoing updates */
-   if (!mutex_trylock(_base->vm->eviction_lock))
+   if (!amdgpu_vm_eviction_trylock(bo_base->vm))
return false;
 
/* Don't evict VM page tables while they are updated */
if (!dma_fence_is_signaled(bo_base->vm->last_direct) ||
!dma_fence_is_signaled(bo_base->vm->last_delayed)) {
-   mutex_unlock(_base->vm->eviction_lock);
+   amdgpu_vm_eviction_unlock(bo_base->vm);
return false;
}
 
bo_base->vm->evicting = true;
-   mutex_unlock(_base->vm->eviction_lock);
+   amdgpu_vm_eviction_unlock(bo_base->vm);
return true;
 }
 
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
index 100547f094ff..c21a36bebc0c 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
@@ -30,6 +30,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include "amdgpu_sync.h"
 #include "amdgpu_ring.h"
@@ -242,9 +243,12 @@ struct amdgpu_vm {
/* tree of virtual addresses mapped */
struct rb_root_cached   va;
 
-   /* Lock to prevent eviction while we are updating page tables */
+   /* Lock to prevent eviction while we are updating page tables
+* use vm_eviction_lock/unlock(vm)
+*/
struct mutexeviction_lock;
boolevicting;
+   unsigned intsaved_flags;
 
/* BOs who needs a validation */
struct list_headevicted;
-- 
2.17.1

___
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx


Re: [PATCH 1/5] drm/amdgpu: Avoid reclaim fs while eviction lock

2020-01-02 Thread Christian König

Am 20.12.19 um 07:24 schrieb Alex Sierra:

[Why]
Avoid reclaim filesystem while eviction lock is held called from
MMU notifier.

[How]
Setting PF_MEMALLOC_NOFS flags while eviction mutex is locked.
Using memalloc_nofs_save / memalloc_nofs_restore API.

Change-Id: I5531c9337836e7d4a430df3f16dcc82888e8018c
Signed-off-by: Alex Sierra 
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 14 ++---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 28 +-
  2 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index b999b67ff57a..b36daa6230fb 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -678,9 +678,9 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, 
struct amdgpu_vm *vm,
}
}
  
-	mutex_lock(>eviction_lock);

+   vm_eviction_lock(vm);
vm->evicting = false;
-   mutex_unlock(>eviction_lock);
+   vm_eviction_unlock(vm);
  
  	return 0;

  }
@@ -1559,7 +1559,7 @@ static int amdgpu_vm_bo_update_mapping(struct 
amdgpu_device *adev,
if (!(flags & AMDGPU_PTE_VALID))
owner = AMDGPU_FENCE_OWNER_KFD;
  
-	mutex_lock(>eviction_lock);

+   vm_eviction_lock(vm);
if (vm->evicting) {
r = -EBUSY;
goto error_unlock;
@@ -1576,7 +1576,7 @@ static int amdgpu_vm_bo_update_mapping(struct 
amdgpu_device *adev,
r = vm->update_funcs->commit(, fence);
  
  error_unlock:

-   mutex_unlock(>eviction_lock);
+   vm_eviction_unlock(vm);
return r;
  }
  
@@ -2537,18 +2537,18 @@ bool amdgpu_vm_evictable(struct amdgpu_bo *bo)

return false;
  
  	/* Try to block ongoing updates */

-   if (!mutex_trylock(_base->vm->eviction_lock))
+   if (!vm_eviction_trylock(bo_base->vm))
return false;
  
  	/* Don't evict VM page tables while they are updated */

if (!dma_fence_is_signaled(bo_base->vm->last_direct) ||
!dma_fence_is_signaled(bo_base->vm->last_delayed)) {
-   mutex_unlock(_base->vm->eviction_lock);
+   vm_eviction_unlock(bo_base->vm);
return false;
}
  
  	bo_base->vm->evicting = true;

-   mutex_unlock(_base->vm->eviction_lock);
+   vm_eviction_unlock(bo_base->vm);
return true;
  }
  
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h

index 100547f094ff..d35aa76469ec 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
@@ -30,6 +30,7 @@
  #include 
  #include 
  #include 
+#include 
  
  #include "amdgpu_sync.h"

  #include "amdgpu_ring.h"
@@ -242,9 +243,12 @@ struct amdgpu_vm {
/* tree of virtual addresses mapped */
struct rb_root_cached   va;
  
-	/* Lock to prevent eviction while we are updating page tables */

+   /* Lock to prevent eviction while we are updating page tables
+* use vm_eviction_lock/unlock(vm)
+*/
struct mutexeviction_lock;
boolevicting;
+   unsigned intsaved_flags;
  
  	/* BOs who needs a validation */

struct list_headevicted;
@@ -436,4 +440,26 @@ void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
struct amdgpu_vm *vm);
  void amdgpu_vm_del_from_lru_notify(struct ttm_buffer_object *bo);
  
+/* vm eviction_lock can be taken in MMU notifiers. Make sure no reclaim-FS

+ * happens while holding this lock anywhere to prevent deadlocks when
+ * an MMU notifier runs in reclaim-FS context.
+ */
+static inline void vm_eviction_lock(struct amdgpu_vm *vm)


Please add a proper amdgpu_ prefix to the function names.

Additional to that please don't put local static functions into the 
header, those shouldn't be used outside of the VM code.


Christian.


+{
+   mutex_lock(>eviction_lock);
+   vm->saved_flags = memalloc_nofs_save();
+}
+static inline int vm_eviction_trylock(struct amdgpu_vm *vm)
+{
+   if (mutex_trylock(>eviction_lock)) {
+   vm->saved_flags = memalloc_nofs_save();
+   return 1;
+   }
+   return 0;
+}
+static inline void vm_eviction_unlock(struct amdgpu_vm *vm)
+{
+   memalloc_nofs_restore(vm->saved_flags);
+   mutex_unlock(>eviction_lock);
+}
  #endif


___
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx


Re: [PATCH 1/5] drm/amdgpu: Avoid reclaim fs while eviction lock

2019-12-20 Thread Yong Zhao

One style comment inline.

Yong

On 2019-12-20 1:24 a.m., Alex Sierra wrote:

[Why]
Avoid reclaim filesystem while eviction lock is held called from
MMU notifier.

[How]
Setting PF_MEMALLOC_NOFS flags while eviction mutex is locked.
Using memalloc_nofs_save / memalloc_nofs_restore API.

Change-Id: I5531c9337836e7d4a430df3f16dcc82888e8018c
Signed-off-by: Alex Sierra 
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 14 ++---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 28 +-
  2 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index b999b67ff57a..b36daa6230fb 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -678,9 +678,9 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, 
struct amdgpu_vm *vm,
}
}
  
-	mutex_lock(>eviction_lock);

+   vm_eviction_lock(vm);
vm->evicting = false;
-   mutex_unlock(>eviction_lock);
+   vm_eviction_unlock(vm);
  
  	return 0;

  }
@@ -1559,7 +1559,7 @@ static int amdgpu_vm_bo_update_mapping(struct 
amdgpu_device *adev,
if (!(flags & AMDGPU_PTE_VALID))
owner = AMDGPU_FENCE_OWNER_KFD;
  
-	mutex_lock(>eviction_lock);

+   vm_eviction_lock(vm);
if (vm->evicting) {
r = -EBUSY;
goto error_unlock;
@@ -1576,7 +1576,7 @@ static int amdgpu_vm_bo_update_mapping(struct 
amdgpu_device *adev,
r = vm->update_funcs->commit(, fence);
  
  error_unlock:

-   mutex_unlock(>eviction_lock);
+   vm_eviction_unlock(vm);
return r;
  }
  
@@ -2537,18 +2537,18 @@ bool amdgpu_vm_evictable(struct amdgpu_bo *bo)

return false;
  
  	/* Try to block ongoing updates */

-   if (!mutex_trylock(_base->vm->eviction_lock))
+   if (!vm_eviction_trylock(bo_base->vm))
return false;
  
  	/* Don't evict VM page tables while they are updated */

if (!dma_fence_is_signaled(bo_base->vm->last_direct) ||
!dma_fence_is_signaled(bo_base->vm->last_delayed)) {
-   mutex_unlock(_base->vm->eviction_lock);
+   vm_eviction_unlock(bo_base->vm);
return false;
}
  
  	bo_base->vm->evicting = true;

-   mutex_unlock(_base->vm->eviction_lock);
+   vm_eviction_unlock(bo_base->vm);
return true;
  }
  
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h

index 100547f094ff..d35aa76469ec 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
@@ -30,6 +30,7 @@
  #include 
  #include 
  #include 
+#include 
  
  #include "amdgpu_sync.h"

  #include "amdgpu_ring.h"
@@ -242,9 +243,12 @@ struct amdgpu_vm {
/* tree of virtual addresses mapped */
struct rb_root_cached   va;
  
-	/* Lock to prevent eviction while we are updating page tables */

+   /* Lock to prevent eviction while we are updating page tables
+* use vm_eviction_lock/unlock(vm)
+*/
struct mutexeviction_lock;
boolevicting;
+   unsigned intsaved_flags;

[yz] The tabs should be used here instead of spaces.
  
  	/* BOs who needs a validation */

struct list_headevicted;
@@ -436,4 +440,26 @@ void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
struct amdgpu_vm *vm);
  void amdgpu_vm_del_from_lru_notify(struct ttm_buffer_object *bo);
  
+/* vm eviction_lock can be taken in MMU notifiers. Make sure no reclaim-FS

+ * happens while holding this lock anywhere to prevent deadlocks when
+ * an MMU notifier runs in reclaim-FS context.
+ */
+static inline void vm_eviction_lock(struct amdgpu_vm *vm)
+{
+   mutex_lock(>eviction_lock);
+   vm->saved_flags = memalloc_nofs_save();
+}
+static inline int vm_eviction_trylock(struct amdgpu_vm *vm)
+{
+   if (mutex_trylock(>eviction_lock)) {
+   vm->saved_flags = memalloc_nofs_save();
+   return 1;
+   }
+   return 0;
+}
+static inline void vm_eviction_unlock(struct amdgpu_vm *vm)
+{
+   memalloc_nofs_restore(vm->saved_flags);
+   mutex_unlock(>eviction_lock);
+}
  #endif

___
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx


Re: [PATCH 1/5] drm/amdgpu: Avoid reclaim fs while eviction lock

2019-12-20 Thread Felix Kuehling

On 2019-12-20 1:24, Alex Sierra wrote:

[Why]
Avoid reclaim filesystem while eviction lock is held called from
MMU notifier.

[How]
Setting PF_MEMALLOC_NOFS flags while eviction mutex is locked.
Using memalloc_nofs_save / memalloc_nofs_restore API.

Change-Id: I5531c9337836e7d4a430df3f16dcc82888e8018c
Signed-off-by: Alex Sierra 

This patch is

Reviewed-by: Felix Kuehling 


---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 14 ++---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 28 +-
  2 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index b999b67ff57a..b36daa6230fb 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -678,9 +678,9 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, 
struct amdgpu_vm *vm,
}
}
  
-	mutex_lock(>eviction_lock);

+   vm_eviction_lock(vm);
vm->evicting = false;
-   mutex_unlock(>eviction_lock);
+   vm_eviction_unlock(vm);
  
  	return 0;

  }
@@ -1559,7 +1559,7 @@ static int amdgpu_vm_bo_update_mapping(struct 
amdgpu_device *adev,
if (!(flags & AMDGPU_PTE_VALID))
owner = AMDGPU_FENCE_OWNER_KFD;
  
-	mutex_lock(>eviction_lock);

+   vm_eviction_lock(vm);
if (vm->evicting) {
r = -EBUSY;
goto error_unlock;
@@ -1576,7 +1576,7 @@ static int amdgpu_vm_bo_update_mapping(struct 
amdgpu_device *adev,
r = vm->update_funcs->commit(, fence);
  
  error_unlock:

-   mutex_unlock(>eviction_lock);
+   vm_eviction_unlock(vm);
return r;
  }
  
@@ -2537,18 +2537,18 @@ bool amdgpu_vm_evictable(struct amdgpu_bo *bo)

return false;
  
  	/* Try to block ongoing updates */

-   if (!mutex_trylock(_base->vm->eviction_lock))
+   if (!vm_eviction_trylock(bo_base->vm))
return false;
  
  	/* Don't evict VM page tables while they are updated */

if (!dma_fence_is_signaled(bo_base->vm->last_direct) ||
!dma_fence_is_signaled(bo_base->vm->last_delayed)) {
-   mutex_unlock(_base->vm->eviction_lock);
+   vm_eviction_unlock(bo_base->vm);
return false;
}
  
  	bo_base->vm->evicting = true;

-   mutex_unlock(_base->vm->eviction_lock);
+   vm_eviction_unlock(bo_base->vm);
return true;
  }
  
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h

index 100547f094ff..d35aa76469ec 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
@@ -30,6 +30,7 @@
  #include 
  #include 
  #include 
+#include 
  
  #include "amdgpu_sync.h"

  #include "amdgpu_ring.h"
@@ -242,9 +243,12 @@ struct amdgpu_vm {
/* tree of virtual addresses mapped */
struct rb_root_cached   va;
  
-	/* Lock to prevent eviction while we are updating page tables */

+   /* Lock to prevent eviction while we are updating page tables
+* use vm_eviction_lock/unlock(vm)
+*/
struct mutexeviction_lock;
boolevicting;
+   unsigned intsaved_flags;
  
  	/* BOs who needs a validation */

struct list_headevicted;
@@ -436,4 +440,26 @@ void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
struct amdgpu_vm *vm);
  void amdgpu_vm_del_from_lru_notify(struct ttm_buffer_object *bo);
  
+/* vm eviction_lock can be taken in MMU notifiers. Make sure no reclaim-FS

+ * happens while holding this lock anywhere to prevent deadlocks when
+ * an MMU notifier runs in reclaim-FS context.
+ */
+static inline void vm_eviction_lock(struct amdgpu_vm *vm)
+{
+   mutex_lock(>eviction_lock);
+   vm->saved_flags = memalloc_nofs_save();
+}
+static inline int vm_eviction_trylock(struct amdgpu_vm *vm)
+{
+   if (mutex_trylock(>eviction_lock)) {
+   vm->saved_flags = memalloc_nofs_save();
+   return 1;
+   }
+   return 0;
+}
+static inline void vm_eviction_unlock(struct amdgpu_vm *vm)
+{
+   memalloc_nofs_restore(vm->saved_flags);
+   mutex_unlock(>eviction_lock);
+}
  #endif

___
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx


[PATCH 1/5] drm/amdgpu: Avoid reclaim fs while eviction lock

2019-12-19 Thread Alex Sierra
[Why]
Avoid reclaim filesystem while eviction lock is held called from
MMU notifier.

[How]
Setting PF_MEMALLOC_NOFS flags while eviction mutex is locked.
Using memalloc_nofs_save / memalloc_nofs_restore API.

Change-Id: I5531c9337836e7d4a430df3f16dcc82888e8018c
Signed-off-by: Alex Sierra 
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 14 ++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 28 +-
 2 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index b999b67ff57a..b36daa6230fb 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -678,9 +678,9 @@ int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, 
struct amdgpu_vm *vm,
}
}
 
-   mutex_lock(>eviction_lock);
+   vm_eviction_lock(vm);
vm->evicting = false;
-   mutex_unlock(>eviction_lock);
+   vm_eviction_unlock(vm);
 
return 0;
 }
@@ -1559,7 +1559,7 @@ static int amdgpu_vm_bo_update_mapping(struct 
amdgpu_device *adev,
if (!(flags & AMDGPU_PTE_VALID))
owner = AMDGPU_FENCE_OWNER_KFD;
 
-   mutex_lock(>eviction_lock);
+   vm_eviction_lock(vm);
if (vm->evicting) {
r = -EBUSY;
goto error_unlock;
@@ -1576,7 +1576,7 @@ static int amdgpu_vm_bo_update_mapping(struct 
amdgpu_device *adev,
r = vm->update_funcs->commit(, fence);
 
 error_unlock:
-   mutex_unlock(>eviction_lock);
+   vm_eviction_unlock(vm);
return r;
 }
 
@@ -2537,18 +2537,18 @@ bool amdgpu_vm_evictable(struct amdgpu_bo *bo)
return false;
 
/* Try to block ongoing updates */
-   if (!mutex_trylock(_base->vm->eviction_lock))
+   if (!vm_eviction_trylock(bo_base->vm))
return false;
 
/* Don't evict VM page tables while they are updated */
if (!dma_fence_is_signaled(bo_base->vm->last_direct) ||
!dma_fence_is_signaled(bo_base->vm->last_delayed)) {
-   mutex_unlock(_base->vm->eviction_lock);
+   vm_eviction_unlock(bo_base->vm);
return false;
}
 
bo_base->vm->evicting = true;
-   mutex_unlock(_base->vm->eviction_lock);
+   vm_eviction_unlock(bo_base->vm);
return true;
 }
 
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
index 100547f094ff..d35aa76469ec 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
@@ -30,6 +30,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include "amdgpu_sync.h"
 #include "amdgpu_ring.h"
@@ -242,9 +243,12 @@ struct amdgpu_vm {
/* tree of virtual addresses mapped */
struct rb_root_cached   va;
 
-   /* Lock to prevent eviction while we are updating page tables */
+   /* Lock to prevent eviction while we are updating page tables
+* use vm_eviction_lock/unlock(vm)
+*/
struct mutexeviction_lock;
boolevicting;
+   unsigned intsaved_flags;
 
/* BOs who needs a validation */
struct list_headevicted;
@@ -436,4 +440,26 @@ void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
struct amdgpu_vm *vm);
 void amdgpu_vm_del_from_lru_notify(struct ttm_buffer_object *bo);
 
+/* vm eviction_lock can be taken in MMU notifiers. Make sure no reclaim-FS
+ * happens while holding this lock anywhere to prevent deadlocks when
+ * an MMU notifier runs in reclaim-FS context.
+ */
+static inline void vm_eviction_lock(struct amdgpu_vm *vm)
+{
+   mutex_lock(>eviction_lock);
+   vm->saved_flags = memalloc_nofs_save();
+}
+static inline int vm_eviction_trylock(struct amdgpu_vm *vm)
+{
+   if (mutex_trylock(>eviction_lock)) {
+   vm->saved_flags = memalloc_nofs_save();
+   return 1;
+   }
+   return 0;
+}
+static inline void vm_eviction_unlock(struct amdgpu_vm *vm)
+{
+   memalloc_nofs_restore(vm->saved_flags);
+   mutex_unlock(>eviction_lock);
+}
 #endif
-- 
2.17.1

___
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx