dri-devel.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@ziepe.ca>
To: "Yang, Philip" <Philip.Yang@amd.com>
Cc: "linux-rdma@vger.kernel.org" <linux-rdma@vger.kernel.org>,
	"Kuehling, Felix" <Felix.Kuehling@amd.com>,
	"amd-gfx@lists.freedesktop.org" <amd-gfx@lists.freedesktop.org>,
	"linux-mm@kvack.org" <linux-mm@kvack.org>,
	"dri-devel@lists.freedesktop.org"
	<dri-devel@lists.freedesktop.org>,
	"Deucher, Alexander" <Alexander.Deucher@amd.com>
Subject: Re: [PATCH v2 hmm 00/11] Various revisions from a locking/code review
Date: Thu, 13 Jun 2019 14:50:09 -0300	[thread overview]
Message-ID: <20190613175009.GG22901@ziepe.ca> (raw)
In-Reply-To: <69bb7fe9-98e7-8a49-3e0b-f639010b8991@amd.com>

On Wed, Jun 12, 2019 at 09:49:12PM +0000, Yang, Philip wrote:
> Rebase to https://github.com/jgunthorpe/linux.git hmm branch, need some 
> changes because of interface hmm_range_register change. Then run a quick 
> amdgpu_test. Test is finished, result is ok.

Great! Thanks

I'll add your Tested-by to the series

>  But there is below kernel BUG message, seems hmm_free_rcu calls
> down_write.....
> 
> [ 1171.919921] BUG: sleeping function called from invalid context at 
> /home/yangp/git/compute_staging/kernel/kernel/locking/rwsem.c:65
> [ 1171.919933] in_atomic(): 1, irqs_disabled(): 0, pid: 53, name: 
> kworker/1:1
> [ 1171.919938] 2 locks held by kworker/1:1/53:
> [ 1171.919940]  #0: 000000001c7c19d4 ((wq_completion)rcu_gp){+.+.}, at: 
> process_one_work+0x20e/0x630
> [ 1171.919951]  #1: 00000000923f2cfa 
> ((work_completion)(&sdp->work)){+.+.}, at: process_one_work+0x20e/0x630
> [ 1171.919959] CPU: 1 PID: 53 Comm: kworker/1:1 Tainted: G        W 
>     5.2.0-rc1-kfd-yangp #196
> [ 1171.919961] Hardware name: ASUS All Series/Z97-PRO(Wi-Fi ac)/USB 3.1, 
> BIOS 9001 03/07/2016
> [ 1171.919965] Workqueue: rcu_gp srcu_invoke_callbacks
> [ 1171.919968] Call Trace:
> [ 1171.919974]  dump_stack+0x67/0x9b
> [ 1171.919980]  ___might_sleep+0x149/0x230
> [ 1171.919985]  down_write+0x1c/0x70
> [ 1171.919989]  hmm_free_rcu+0x24/0x80
> [ 1171.919993]  srcu_invoke_callbacks+0xc9/0x150
> [ 1171.920000]  process_one_work+0x28e/0x630
> [ 1171.920008]  worker_thread+0x39/0x3f0
> [ 1171.920014]  ? process_one_work+0x630/0x630
> [ 1171.920017]  kthread+0x11c/0x140
> [ 1171.920021]  ? kthread_park+0x90/0x90
> [ 1171.920026]  ret_from_fork+0x24/0x30

Thank you Phillip, it seems the prior tests were not done with
lockdep..

Sigh, I will keep this with the gross pagetable_lock then. I updated
the patches on the git with this modification. I think we have covered
all the bases so I will send another V of the series to the list and
if no more comments then it will move ahead to hmm.git. Thanks to all.

diff --git a/mm/hmm.c b/mm/hmm.c
index 136c812faa2790..4c64d4c32f4825 100644
--- a/mm/hmm.c
+++ b/mm/hmm.c
@@ -49,16 +49,15 @@ static struct hmm *hmm_get_or_create(struct mm_struct *mm)
 
 	lockdep_assert_held_exclusive(&mm->mmap_sem);
 
+	/* Abuse the page_table_lock to also protect mm->hmm. */
+	spin_lock(&mm->page_table_lock);
 	if (mm->hmm) {
-		if (kref_get_unless_zero(&mm->hmm->kref))
+		if (kref_get_unless_zero(&mm->hmm->kref)) {
+			spin_unlock(&mm->page_table_lock);
 			return mm->hmm;
-		/*
-		 * The hmm is being freed by some other CPU and is pending a
-		 * RCU grace period, but this CPU can NULL now it since we
-		 * have the mmap_sem.
-		 */
-		mm->hmm = NULL;
+		}
 	}
+	spin_unlock(&mm->page_table_lock);
 
 	hmm = kmalloc(sizeof(*hmm), GFP_KERNEL);
 	if (!hmm)
@@ -81,7 +80,14 @@ static struct hmm *hmm_get_or_create(struct mm_struct *mm)
 	}
 
 	mmgrab(hmm->mm);
+
+	/*
+	 * We hold the exclusive mmap_sem here so we know that mm->hmm is
+	 * still NULL or 0 kref, and is safe to update.
+	 */
+	spin_lock(&mm->page_table_lock);
 	mm->hmm = hmm;
+	spin_unlock(&mm->page_table_lock);
 	return hmm;
 }
 
@@ -89,10 +95,14 @@ static void hmm_free_rcu(struct rcu_head *rcu)
 {
 	struct hmm *hmm = container_of(rcu, struct hmm, rcu);
 
-	down_write(&hmm->mm->mmap_sem);
+	/*
+	 * The mm->hmm pointer is kept valid while notifier ops can be running
+	 * so they don't have to deal with a NULL mm->hmm value
+	 */
+	spin_lock(&hmm->mm->page_table_lock);
 	if (hmm->mm->hmm == hmm)
 		hmm->mm->hmm = NULL;
-	up_write(&hmm->mm->mmap_sem);
+	spin_unlock(&hmm->mm->page_table_lock);
 	mmdrop(hmm->mm);
 
 	kfree(hmm);
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

      reply	other threads:[~2019-06-13 17:50 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-06 18:44 [PATCH v2 hmm 00/11] Various revisions from a locking/code review Jason Gunthorpe
     [not found] ` <20190606184438.31646-1-jgg-uk2M96/98Pc@public.gmane.org>
2019-06-06 18:44   ` [PATCH v2 hmm 01/11] mm/hmm: fix use after free with struct hmm in the mmu notifiers Jason Gunthorpe
     [not found]     ` <20190606184438.31646-2-jgg-uk2M96/98Pc@public.gmane.org>
2019-06-07  2:29       ` John Hubbard
     [not found]         ` <9c72d18d-2924-cb90-ea44-7cd4b10b5bc2-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2019-06-07 12:34           ` Jason Gunthorpe
     [not found]             ` <20190607123432.GB14802-uk2M96/98Pc@public.gmane.org>
2019-06-07 13:42               ` Jason Gunthorpe
2019-06-08  1:13             ` John Hubbard
2019-06-08  1:37             ` John Hubbard
2019-06-07 18:12       ` Ralph Campbell
2019-06-08  8:49       ` Christoph Hellwig
2019-06-08 11:33         ` Jason Gunthorpe
2019-06-06 18:44   ` [PATCH v2 hmm 02/11] mm/hmm: Use hmm_mirror not mm as an argument for hmm_range_register Jason Gunthorpe
     [not found]     ` <20190606184438.31646-3-jgg-uk2M96/98Pc@public.gmane.org>
2019-06-07  2:36       ` John Hubbard
2019-06-07 18:24       ` Ralph Campbell
2019-06-07 22:39         ` Ralph Campbell
     [not found]           ` <e460ddf5-9ed3-7f3b-98ce-526c12fdb8b1-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2019-06-10 13:09             ` Jason Gunthorpe
2019-06-08  8:54       ` Christoph Hellwig
     [not found]         ` <20190608085425.GB32185-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org>
2019-06-11 19:44           ` Jason Gunthorpe
     [not found]             ` <20190611194431.GC29375-uk2M96/98Pc@public.gmane.org>
2019-06-12  7:12               ` Christoph Hellwig
     [not found]                 ` <20190612071234.GA20306-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org>
2019-06-12 11:41                   ` Jason Gunthorpe
     [not found]                     ` <20190612114125.GA3876-uk2M96/98Pc@public.gmane.org>
2019-06-12 12:11                       ` Christoph Hellwig
2019-06-07 22:33     ` Ira Weiny
2019-06-06 18:44   ` [PATCH v2 hmm 03/11] mm/hmm: Hold a mmgrab from hmm to mm Jason Gunthorpe
     [not found]     ` <20190606184438.31646-4-jgg-uk2M96/98Pc@public.gmane.org>
2019-06-07  2:44       ` John Hubbard
     [not found]         ` <48fcaa19-6ac3-59d0-cd51-455abeca7cdb-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2019-06-07 12:36           ` Jason Gunthorpe
2019-06-07 18:41       ` Ralph Campbell
     [not found]         ` <605172dc-5c66-123f-61a3-8e6880678aef-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2019-06-07 18:51           ` Jason Gunthorpe
2019-06-07 22:38     ` Ira Weiny
2019-06-06 18:44   ` [PATCH v2 hmm 04/11] mm/hmm: Simplify hmm_get_or_create and make it reliable Jason Gunthorpe
2019-06-07  2:54     ` John Hubbard
     [not found]     ` <20190606184438.31646-5-jgg-uk2M96/98Pc@public.gmane.org>
2019-06-07 18:52       ` Ralph Campbell
2019-06-07 22:44     ` Ira Weiny
2019-06-06 18:44   ` [PATCH v2 hmm 05/11] mm/hmm: Remove duplicate condition test before wait_event_timeout Jason Gunthorpe
2019-06-07  3:06     ` John Hubbard
     [not found]       ` <86962e22-88b1-c1bf-d704-d5a5053fa100-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2019-06-07 12:47         ` Jason Gunthorpe
2019-06-07 13:31         ` [PATCH v3 " Jason Gunthorpe
2019-06-07 22:55           ` Ira Weiny
2019-06-08  1:32           ` John Hubbard
     [not found]     ` <20190606184438.31646-6-jgg-uk2M96/98Pc@public.gmane.org>
2019-06-07 19:01       ` [PATCH v2 " Ralph Campbell
     [not found]         ` <6833be96-12a3-1a1c-1514-c148ba2dd87b-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2019-06-07 19:13           ` Jason Gunthorpe
     [not found]             ` <20190607191302.GR14802-uk2M96/98Pc@public.gmane.org>
2019-06-07 20:21               ` Ralph Campbell
2019-06-07 20:44                 ` Jason Gunthorpe
2019-06-07 22:13                   ` Ralph Campbell
2019-06-08  1:47                     ` Jason Gunthorpe
2019-06-06 18:44   ` [PATCH v2 hmm 06/11] mm/hmm: Hold on to the mmget for the lifetime of the range Jason Gunthorpe
2019-06-07  3:15     ` John Hubbard
     [not found]     ` <20190606184438.31646-7-jgg-uk2M96/98Pc@public.gmane.org>
2019-06-07 20:29       ` Ralph Campbell
2019-06-06 18:44   ` [PATCH v2 hmm 07/11] mm/hmm: Use lockdep instead of comments Jason Gunthorpe
2019-06-07  3:19     ` John Hubbard
     [not found]     ` <20190606184438.31646-8-jgg-uk2M96/98Pc@public.gmane.org>
2019-06-07 20:31       ` Ralph Campbell
2019-06-07 22:16     ` Souptick Joarder
2019-06-06 18:44   ` [PATCH v2 hmm 08/11] mm/hmm: Remove racy protection against double-unregistration Jason Gunthorpe
2019-06-07  3:29     ` John Hubbard
     [not found]       ` <88400de9-e1ae-509b-718f-c6b0f726b14c-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2019-06-07 13:57         ` Jason Gunthorpe
     [not found]     ` <20190606184438.31646-9-jgg-uk2M96/98Pc@public.gmane.org>
2019-06-07 20:33       ` Ralph Campbell
2019-06-06 18:44   ` [PATCH v2 hmm 09/11] mm/hmm: Poison hmm_range during unregister Jason Gunthorpe
2019-06-07  3:37     ` John Hubbard
     [not found]       ` <c00da0f2-b4b8-813b-0441-a50d4de9d8be-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2019-06-07 14:03         ` Jason Gunthorpe
2019-06-07 20:46     ` Ralph Campbell
2019-06-07 20:49       ` Jason Gunthorpe
2019-06-07 23:01     ` Ira Weiny
2019-06-06 18:44   ` [PATCH v2 hmm 10/11] mm/hmm: Do not use list*_rcu() for hmm->ranges Jason Gunthorpe
2019-06-07  3:40     ` John Hubbard
2019-06-07 20:49     ` Ralph Campbell
2019-06-07 22:11     ` Souptick Joarder
2019-06-07 23:02     ` Ira Weiny
2019-06-06 18:44   ` [PATCH v2 hmm 11/11] mm/hmm: Remove confusing comment and logic from hmm_release Jason Gunthorpe
2019-06-07  3:47     ` John Hubbard
     [not found]       ` <3edc47bd-e8f6-0e65-5844-d16901890637-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2019-06-07 12:58         ` Jason Gunthorpe
2019-06-07 21:37     ` Ralph Campbell
2019-06-08  2:12       ` Jason Gunthorpe
     [not found]       ` <61ea869d-43d2-d1e5-dc00-cf5e3e139169-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2019-06-10 16:02         ` Jason Gunthorpe
2019-06-10 22:03           ` Ralph Campbell
2019-06-07 16:05   ` [PATCH v2 12/11] mm/hmm: Fix error flows in hmm_invalidate_range_start Jason Gunthorpe
2019-06-07 23:52     ` Ralph Campbell
2019-06-08  1:35       ` Jason Gunthorpe
2019-06-11 19:48   ` [PATCH v2 hmm 00/11] Various revisions from a locking/code review Jason Gunthorpe
2019-06-12 17:54     ` Kuehling, Felix
     [not found]       ` <5d3b0ae2-3662-cab2-5e6c-82912f32356a-5C7GfCeVMHo@public.gmane.org>
2019-06-12 21:49         ` Yang, Philip
2019-06-13 17:50           ` Jason Gunthorpe [this message]

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=20190613175009.GG22901@ziepe.ca \
    --to=jgg@ziepe.ca \
    --cc=Alexander.Deucher@amd.com \
    --cc=Felix.Kuehling@amd.com \
    --cc=Philip.Yang@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-rdma@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).