Skip to contentSkip to conversation

xen/gntdev.c: Convert get_user_pages*() to pin_user_pages*()

RFC PATCH In review Souptick Joarder 2 participants 2 messages Aug 18, 2020 – Aug 20, 2020 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 xen/gntdev.c: Convert get_user_pages*() to pin_user_pages*()

In 2019, we introduced pin_user_pages*() and now we are converting get_user_pages*() to the new API as appropriate. [1] & [2] could be referred for more information. This is case 5 as per document [1].

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

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

        https://lwn.net/Articles/807108/
---
 drivers/xen/gntdev.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/xen/gntdev.c +2 -5
index 64a9025a..e480509 100644
@@ -730,7 +730,7 @@ static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
 	unsigned long xen_pfn;
 	int ret;
 
-	ret = get_user_pages_fast(addr, 1, writeable ? FOLL_WRITE : 0, &page);
+	ret = pin_user_pages_fast(addr, 1, writeable ? FOLL_WRITE : 0, &page);
 	if (ret < 0)
 		return ret;
 
@@ -744,10 +744,7 @@ static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
 
 static void gntdev_put_pages(struct gntdev_copy_batch *batch)
 {
-	unsigned int i;
-
-	for (i = 0; i < batch->nr_pages; i++)
-		put_page(batch->pages[i]);
+	unpin_user_pages(batch->pages, batch->nr_pages);
 	batch->nr_pages = 0;
 }
 
Signature · 1 line
1.9.1
#1 lore raw
Quoted 29 lines · Souptick Joarder

In 2019, we introduced pin_user_pages*() and now we are converting get_user_pages*() to the new API as appropriate. [1] & [2] could be referred for more information. This is case 5 as per document [1].

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

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

        https://lwn.net/Articles/807108/
---
 drivers/xen/gntdev.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/xen/gntdev.c +1 -1
index 64a9025a..e480509 100644
@@ -730,7 +730,7 @@ static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
 	unsigned long xen_pfn;
 	int ret;
 
-	ret = get_user_pages_fast(addr, 1, writeable ? FOLL_WRITE : 0, &page);
+	ret = pin_user_pages_fast(addr, 1, writeable ? FOLL_WRITE : 0, &page);
 	if (ret < 0)
 		return ret;
 

drivers/xen/gntdev.c static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt, · line 747

Quoted 8 lines
drivers/xen/gntdev.c +1 -4
@@ -744,10 +744,7 @@ static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
 
 static void gntdev_put_pages(struct gntdev_copy_batch *batch)
 {
-	unsigned int i;
-
-	for (i = 0; i < batch->nr_pages; i++)
-		put_page(batch->pages[i]);
+	unpin_user_pages(batch->pages, batch->nr_pages);

There seems to be a bug in the original code (just like there was in privcmd.c that you fixed earlier) --- when gntdev_get_page() is called with writeable=true then the page needs to be marked dirty before being put.

You can add a bool writeable to gntdev_copy_batch and set it in gntdev_grant_copy_seg() (and drop writeable argument to gntdev_get_page()) and then, based on batch->writeable, use either unpin_user_pages() or unpin_user_pages_dirty_lock().

(But to fix this in stable branches please make first patch call set_page_dirty_lock() for each page like you did in previous series)

-boris

Quoted 3 lines
drivers/xen/gntdev.c +0 -0
 	batch->nr_pages = 0;
 }