AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Philip Yang <Philip.Yang@amd.com>
To: <amd-gfx@lists.freedesktop.org>
Cc: Philip Yang <Philip.Yang@amd.com>, felix.kuehling@amd.com
Subject: [PATCH 1/1] drm/amdkfd: Use mmap_write_trylock to avoid deadlock
Date: Thu, 24 Feb 2022 18:07:02 -0500	[thread overview]
Message-ID: <20220224230702.1624-1-Philip.Yang@amd.com> (raw)

If one thread takes read lock, one thread to acquire write lock, then
other thread can not acquire read lock while the writer is stalled. This
causes below deadlock case:

thread 1: prefetch range migrate to VRAM, take mmap read lock
thread 2: svm_range_evict_svm_bo_worker, migrate to RAM, take mmap read
lock
thread 3: svm_range_restore_work, for xnack off case only, take mmap
write lock to flush deferred list

To avoid deadlock, use write_trylock, go to sleep if lock contention,
then trylock again.

Signed-off-by: Philip Yang <Philip.Yang@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
index b71d47afd243..1983849c4070 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
@@ -1608,22 +1608,25 @@ static int svm_range_validate_and_map(struct mm_struct *mm,
  * @svms: the svm range list
  * @mm: the mm structure
  *
- * Context: Returns with mmap write lock held, pending deferred work flushed
+ * Context: Non-atomic context, may sleep
  *
+ * Returns with mmap write lock held, pending deferred work flushed
  */
 void
 svm_range_list_lock_and_flush_work(struct svm_range_list *svms,
 				   struct mm_struct *mm)
 {
-retry_flush_work:
-	flush_work(&svms->deferred_list_work);
-	mmap_write_lock(mm);
-
-	if (list_empty(&svms->deferred_range_list))
-		return;
-	mmap_write_unlock(mm);
-	pr_debug("retry flush\n");
-	goto retry_flush_work;
+	while (true) {
+		flush_work(&svms->deferred_list_work);
+		if (!mmap_write_trylock(mm)) {
+			usleep_range(1000, 2000);
+			continue;
+		}
+		if (list_empty(&svms->deferred_range_list))
+			return;
+		mmap_write_unlock(mm);
+		pr_debug("retry flush\n");
+	}
 }
 
 static void svm_range_restore_work(struct work_struct *work)
-- 
2.17.1


                 reply	other threads:[~2022-02-24 23:07 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220224230702.1624-1-Philip.Yang@amd.com \
    --to=philip.yang@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=felix.kuehling@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox