Skip to contentSkip to conversation

drm/radeon: Convert get_user_pages() --> pin_user_pages()

PATCH In review Souptick Joarder 3 participants 8 messages May 26, 2020 – Jun 8, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/radeon: Convert get_user_pages() --> pin_user_pages()

This code was using get_user_pages(), in a "Case 2" scenario (DMA/RDMA), using the categorization from [1]. That means that it's time to convert the get_user_pages() + release_pages() calls to pin_user_pages() + unpin_user_pages() calls.

There is some helpful background in [2]: basically, this is a small part of fixing a long-standing disconnect between pinning pages, and file systems' use of those pages.

[1] Documentation/core-api/pin_user_pages.rst

[2] "Explicit pinning of user-space pages":

    https://lwn.net/Articles/807108/

Hi,

I'm compile tested this, but unable to run-time test, so any testing help is much appriciated.

---
 drivers/gpu/drm/radeon/radeon_ttm.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/radeon/radeon_ttm.c +3 -3
index 5d50c9e..e927de2 100644
@@ -506,7 +506,7 @@ static int radeon_ttm_tt_pin_userptr(struct ttm_tt *ttm)
 		uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
 		struct page **pages = ttm->pages + pinned;
 
-		r = get_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
+		r = pin_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
 				   pages, NULL);
 		if (r < 0)
 			goto release_pages;
@@ -535,7 +535,7 @@ static int radeon_ttm_tt_pin_userptr(struct ttm_tt *ttm)
 	kfree(ttm->sg);
 
 release_pages:
-	release_pages(ttm->pages, pinned);
+	unpin_user_pages(ttm->pages, pinned);
 	return r;
 }
 
@@ -562,7 +562,7 @@ static void radeon_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
 			set_page_dirty(page);
 
 		mark_page_accessed(page);
-		put_page(page);
+		unpin_user_page(page);
 	}
 
 	sg_free_table(ttm->sg);
Signature · 1 line
1.9.1
Acked-by on v1 1/1
#1 lore raw
Quoted 48 lines · Souptick Joarder

This code was using get_user_pages(), in a "Case 2" scenario (DMA/RDMA), using the categorization from [1]. That means that it's time to convert the get_user_pages() + release_pages() calls to pin_user_pages() + unpin_user_pages() calls.

There is some helpful background in [2]: basically, this is a small part of fixing a long-standing disconnect between pinning pages, and file systems' use of those pages.

[1] Documentation/core-api/pin_user_pages.rst

[2] "Explicit pinning of user-space pages":

     https://lwn.net/Articles/807108/

Hi,

I'm compile tested this, but unable to run-time test, so any testing help is much appriciated.

---
  drivers/gpu/drm/radeon/radeon_ttm.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/radeon/radeon_ttm.c +2 -2
index 5d50c9e..e927de2 100644
@@ -506,7 +506,7 @@ static int radeon_ttm_tt_pin_userptr(struct ttm_tt *ttm)
  		uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
  		struct page **pages = ttm->pages + pinned;
 
-		r = get_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
+		r = pin_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
  				   pages, NULL);
  		if (r < 0)
  			goto release_pages;
@@ -535,7 +535,7 @@ static int radeon_ttm_tt_pin_userptr(struct ttm_tt *ttm)
  	kfree(ttm->sg);
 
  release_pages:
-	release_pages(ttm->pages, pinned);
+	unpin_user_pages(ttm->pages, pinned);
  	return r;
  }
 

drivers/gpu/drm/radeon/radeon_ttm.c static void radeon_ttm_tt_unpin_userptr(struct ttm_tt *ttm) · line 562

Quoted 1 line
drivers/gpu/drm/radeon/radeon_ttm.c +0 -0
@@ -562,7 +562,7 @@ static void radeon_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
  			set_page_dirty(page);

Maybe we also need a preceding patch, to fix the above? It should be set_page_dirty_lock(), rather than set_page_dirty(), unless I'm overlooking something (which is very possible!).

Either way, from a tunnel vision perspective of changing gup to pup, this looks good to me, so

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Quoted 8 lines
drivers/gpu/drm/radeon/radeon_ttm.c +1 -1
  		mark_page_accessed(page);
-		put_page(page);
+		unpin_user_page(page);
  	}
 
  	sg_free_table(ttm->sg);
 
#2 lore raw
Quoted 6 lines · John Hubbard
Quoted 5 lines · Souptick Joarder

This code was using get_user_pages(), in a "Case 2" scenario (DMA/RDMA), using the categorization from [1]. That means that it's time to convert the get_user_pages() + release_pages() calls to pin_user_pages() + unpin_user_pages() calls.

Quoted 8 lines
Quoted 8 lines

There is some helpful background in [2]: basically, this is a small part of fixing a long-standing disconnect between pinning pages, and file systems' use of those pages.

[1] Documentation/core-api/pin_user_pages.rst

[2] "Explicit pinning of user-space pages":

     https://lwn.net/Articles/807108/

I don't think this is a case 2 here, nor is it any of the others. Feels like not covered at all by the doc.

radeon has a mmu notifier (might be a bit broken, but hey whatever there's other drivers which have the same concept, but less broken). So when you do an munmap, radeon will release the page refcount.

Which case it that?

Note that currently only amdgpu doesn't work like that for gpu dma directly to userspace ranges, it uses hmm and afaiui doens't hold a full page pin refcount.

Cheers, Daniel

Quoted 57 lines
Quoted 32 lines

Hi,

I'm compile tested this, but unable to run-time test, so any testing help is much appriciated.

---
  drivers/gpu/drm/radeon/radeon_ttm.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/radeon/radeon_ttm.c +2 -2
index 5d50c9e..e927de2 100644
@@ -506,7 +506,7 @@ static int radeon_ttm_tt_pin_userptr(struct ttm_tt *ttm)
  		uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
  		struct page **pages = ttm->pages + pinned;
-		r = get_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
+		r = pin_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
  				   pages, NULL);
  		if (r < 0)
  			goto release_pages;
@@ -535,7 +535,7 @@ static int radeon_ttm_tt_pin_userptr(struct ttm_tt *ttm)
  	kfree(ttm->sg);
  release_pages:
-	release_pages(ttm->pages, pinned);
+	unpin_user_pages(ttm->pages, pinned);
  	return r;
  }

drivers/gpu/drm/radeon/radeon_ttm.c static void radeon_ttm_tt_unpin_userptr(struct ttm_tt *ttm) · line 562

Quoted 1 line
drivers/gpu/drm/radeon/radeon_ttm.c +0 -0
@@ -562,7 +562,7 @@ static void radeon_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
  			set_page_dirty(page);

Maybe we also need a preceding patch, to fix the above? It should be set_page_dirty_lock(), rather than set_page_dirty(), unless I'm overlooking something (which is very possible!).

Either way, from a tunnel vision perspective of changing gup to pup, this looks good to me, so

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Quoted 6 lines
drivers/gpu/drm/radeon/radeon_ttm.c +1 -1
  		mark_page_accessed(page);
-		put_page(page);
+		unpin_user_page(page);
  	}
  	sg_free_table(ttm->sg);
 
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#3 lore raw
Quoted 16 lines · Daniel Vetter
Quoted 6 lines · John Hubbard
Quoted 5 lines · Souptick Joarder

This code was using get_user_pages(), in a "Case 2" scenario (DMA/RDMA), using the categorization from [1]. That means that it's time to convert the get_user_pages() + release_pages() calls to pin_user_pages() + unpin_user_pages() calls.

Quoted 8 lines
Quoted 8 lines

There is some helpful background in [2]: basically, this is a small part of fixing a long-standing disconnect between pinning pages, and file systems' use of those pages.

[1] Documentation/core-api/pin_user_pages.rst

[2] "Explicit pinning of user-space pages":

     https://lwn.net/Articles/807108/
Quoted 6 lines

I don't think this is a case 2 here, nor is it any of the others. Feels like not covered at all by the doc.

radeon has a mmu notifier (might be a bit broken, but hey whatever there's other drivers which have the same concept, but less broken). So when you do an munmap, radeon will release the page refcount.

I forgot to add: It's also not case 3, since there's no hw page fault support. It's all faked in software, and explicitly synchronizes against pending io (or preempts it, that depends a bit upon the jobs running).

Quoted 71 lines

Which case it that?

Note that currently only amdgpu doesn't work like that for gpu dma directly to userspace ranges, it uses hmm and afaiui doens't hold a full page pin refcount.

Cheers, Daniel

Quoted 57 lines
Quoted 32 lines

Hi,

I'm compile tested this, but unable to run-time test, so any testing help is much appriciated.

---
  drivers/gpu/drm/radeon/radeon_ttm.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/radeon/radeon_ttm.c +2 -2
index 5d50c9e..e927de2 100644
@@ -506,7 +506,7 @@ static int radeon_ttm_tt_pin_userptr(struct ttm_tt *ttm)
  		uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
  		struct page **pages = ttm->pages + pinned;
-		r = get_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
+		r = pin_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
  				   pages, NULL);
  		if (r < 0)
  			goto release_pages;
@@ -535,7 +535,7 @@ static int radeon_ttm_tt_pin_userptr(struct ttm_tt *ttm)
  	kfree(ttm->sg);
  release_pages:
-	release_pages(ttm->pages, pinned);
+	unpin_user_pages(ttm->pages, pinned);
  	return r;
  }

drivers/gpu/drm/radeon/radeon_ttm.c static void radeon_ttm_tt_unpin_userptr(struct ttm_tt *ttm) · line 562

Quoted 1 line
drivers/gpu/drm/radeon/radeon_ttm.c +0 -0
@@ -562,7 +562,7 @@ static void radeon_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
  			set_page_dirty(page);

Maybe we also need a preceding patch, to fix the above? It should be set_page_dirty_lock(), rather than set_page_dirty(), unless I'm overlooking something (which is very possible!).

Either way, from a tunnel vision perspective of changing gup to pup, this looks good to me, so

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Quoted 6 lines
drivers/gpu/drm/radeon/radeon_ttm.c +1 -1
  		mark_page_accessed(page);
-		put_page(page);
+		unpin_user_page(page);
  	}
  	sg_free_table(ttm->sg);
 
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#4 lore raw
Quoted 17 lines · Daniel Vetter
Quoted 16 lines · Daniel Vetter
Quoted 14 lines · John Hubbard
Quoted 13 lines · Souptick Joarder

This code was using get_user_pages(), in a "Case 2" scenario (DMA/RDMA), using the categorization from [1]. That means that it's time to convert the get_user_pages() + release_pages() calls to pin_user_pages() + unpin_user_pages() calls.

There is some helpful background in [2]: basically, this is a small part of fixing a long-standing disconnect between pinning pages, and file systems' use of those pages.

[1] Documentation/core-api/pin_user_pages.rst

[2] "Explicit pinning of user-space pages":

      https://lwn.net/Articles/807108/
Quoted 7 lines
Quoted 6 lines

I don't think this is a case 2 here, nor is it any of the others. Feels like not covered at all by the doc.

radeon has a mmu notifier (might be a bit broken, but hey whatever there's other drivers which have the same concept, but less broken). So when you do an munmap, radeon will release the page refcount.

Aha, thanks Daniel. I withdraw my misinformed ACK, then.

Quoted 4 lines

I forgot to add: It's also not case 3, since there's no hw page fault support. It's all faked in software, and explicitly synchronizes against pending io (or preempts it, that depends a bit upon the jobs running).

This is what case 3 was *intended* to cover, but it looks like case 3 needs to be written a little better. I'll attempt that, and Cc you on the actual patch to -mm. (I think we also need a case 5 for an unrelated scenario, too, so it's time.)

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Quoted 67 lines
Quoted 67 lines

Which case it that?

Note that currently only amdgpu doesn't work like that for gpu dma directly to userspace ranges, it uses hmm and afaiui doens't hold a full page pin refcount.

Cheers, Daniel

Quoted 57 lines
Quoted 32 lines

Hi,

I'm compile tested this, but unable to run-time test, so any testing help is much appriciated.

---
   drivers/gpu/drm/radeon/radeon_ttm.c | 6 +++---
   1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/radeon/radeon_ttm.c +2 -2
index 5d50c9e..e927de2 100644
@@ -506,7 +506,7 @@ static int radeon_ttm_tt_pin_userptr(struct ttm_tt *ttm)
   		uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
   		struct page **pages = ttm->pages + pinned;
-		r = get_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
+		r = pin_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
   				   pages, NULL);
   		if (r < 0)
   			goto release_pages;
@@ -535,7 +535,7 @@ static int radeon_ttm_tt_pin_userptr(struct ttm_tt *ttm)
   	kfree(ttm->sg);
   release_pages:
-	release_pages(ttm->pages, pinned);
+	unpin_user_pages(ttm->pages, pinned);
   	return r;
   }

drivers/gpu/drm/radeon/radeon_ttm.c static void radeon_ttm_tt_unpin_userptr(struct ttm_tt *ttm) · line 562

Quoted 1 line
drivers/gpu/drm/radeon/radeon_ttm.c +0 -0
@@ -562,7 +562,7 @@ static void radeon_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
   			set_page_dirty(page);

Maybe we also need a preceding patch, to fix the above? It should be set_page_dirty_lock(), rather than set_page_dirty(), unless I'm overlooking something (which is very possible!).

Either way, from a tunnel vision perspective of changing gup to pup, this looks good to me, so

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Quoted 6 lines
drivers/gpu/drm/radeon/radeon_ttm.c +1 -1
   		mark_page_accessed(page);
-		put_page(page);
+		unpin_user_page(page);
   	}
   	sg_free_table(ttm->sg);
 
Quoted 5 lines
Quoted 4 lines
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

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

#5 lore raw
Quoted 34 lines · John Hubbard
Quoted 17 lines · Daniel Vetter
Quoted 16 lines · Daniel Vetter
Quoted 14 lines · John Hubbard
Quoted 13 lines · Souptick Joarder

This code was using get_user_pages(), in a "Case 2" scenario (DMA/RDMA), using the categorization from [1]. That means that it's time to convert the get_user_pages() + release_pages() calls to pin_user_pages() + unpin_user_pages() calls.

There is some helpful background in [2]: basically, this is a small part of fixing a long-standing disconnect between pinning pages, and file systems' use of those pages.

[1] Documentation/core-api/pin_user_pages.rst

[2] "Explicit pinning of user-space pages":

      https://lwn.net/Articles/807108/
Quoted 7 lines
Quoted 6 lines

I don't think this is a case 2 here, nor is it any of the others. Feels like not covered at all by the doc.

radeon has a mmu notifier (might be a bit broken, but hey whatever there's other drivers which have the same concept, but less broken). So when you do an munmap, radeon will release the page refcount.

Aha, thanks Daniel. I withdraw my misinformed ACK, then.

Quoted 4 lines

I forgot to add: It's also not case 3, since there's no hw page fault support. It's all faked in software, and explicitly synchronizes against pending io (or preempts it, that depends a bit upon the jobs running).

Quoted 4 lines

This is what case 3 was *intended* to cover, but it looks like case 3 needs to be written a little better. I'll attempt that, and Cc you on the actual patch to -mm. (I think we also need a case 5 for an unrelated scenario, too, so it's time.)

There were no *case 5* in the other patch posted in -mm. Do we need to add it ?

Quoted 81 lines

thanks,
--
John Hubbard
NVIDIA

Quoted 72 lines
Quoted 71 lines

Which case it that?

Note that currently only amdgpu doesn't work like that for gpu dma directly to userspace ranges, it uses hmm and afaiui doens't hold a full page pin refcount.

Cheers, Daniel

Quoted 42 lines
Quoted 32 lines

Hi,

I'm compile tested this, but unable to run-time test, so any testing help is much appriciated.

---
   drivers/gpu/drm/radeon/radeon_ttm.c | 6 +++---
   1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/radeon/radeon_ttm.c +2 -2
index 5d50c9e..e927de2 100644
@@ -506,7 +506,7 @@ static int radeon_ttm_tt_pin_userptr(struct ttm_tt *ttm)
                   uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
                   struct page **pages = ttm->pages + pinned;
-          r = get_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
+          r = pin_user_pages(userptr, num_pages, write ? FOLL_WRITE : 0,
                                      pages, NULL);
                   if (r < 0)
                           goto release_pages;
@@ -535,7 +535,7 @@ static int radeon_ttm_tt_pin_userptr(struct ttm_tt *ttm)
           kfree(ttm->sg);
   release_pages:
-  release_pages(ttm->pages, pinned);
+  unpin_user_pages(ttm->pages, pinned);
           return r;
   }

drivers/gpu/drm/radeon/radeon_ttm.c static void radeon_ttm_tt_unpin_userptr(struct ttm_tt *ttm) · line 562

Quoted 1 line
drivers/gpu/drm/radeon/radeon_ttm.c +0 -0
@@ -562,7 +562,7 @@ static void radeon_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
                           set_page_dirty(page);

Maybe we also need a preceding patch, to fix the above? It should be set_page_dirty_lock(), rather than set_page_dirty(), unless I'm overlooking something (which is very possible!).

Either way, from a tunnel vision perspective of changing gup to pup, this looks good to me, so

Quoted 15 lines

thanks,
--
John Hubbard
NVIDIA

Quoted 6 lines
drivers/gpu/drm/radeon/radeon_ttm.c +1 -1
                   mark_page_accessed(page);
-          put_page(page);
+          unpin_user_page(page);
           }
           sg_free_table(ttm->sg);
 

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

#6 lore raw

On 2020-05-28 23:49, Souptick Joarder wrote:
...

Quoted 7 lines
Quoted 4 lines

This is what case 3 was *intended* to cover, but it looks like case 3 needs to be written a little better. I'll attempt that, and Cc you on the actual patch to -mm. (I think we also need a case 5 for an unrelated scenario, too, so it's time.)

There were no *case 5* in the other patch posted in -mm. Do we need to add it ?

Working on figuring that out [1], but it's not directly relevant to this thread. Maybe I shouldn't have brought it up here. :)

[1] https://lore.kernel.org/r/[email protected]

thanks,
John Hubbard
NVIDIA

#7 lore raw
Quoted 15 lines · John Hubbard

On 2020-05-28 23:49, Souptick Joarder wrote:
...

Quoted 7 lines
Quoted 4 lines

This is what case 3 was *intended* to cover, but it looks like case 3 needs to be written a little better. I'll attempt that, and Cc you on the actual patch to -mm. (I think we also need a case 5 for an unrelated scenario, too, so it's time.)

There were no *case 5* in the other patch posted in -mm. Do we need to add it ?

Working on figuring that out [1], but it's not directly relevant to this thread. Maybe I shouldn't have brought it up here. :)

Quoted 8 lines

[1] https://lore.kernel.org/r/[email protected]

thanks,
John Hubbard
NVIDIA

As this conversion is not relevant ( mentioned above), I have dropped this patch.