Skip to contentSkip to conversation

xen/gntdev.c: Mark pages as dirty

PATCH In review Souptick Joarder 2 participants 8 messages Sep 6, 2020 – Sep 30, 2020

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

PATCH 1/2 xen/gntdev.c: Mark pages as dirty

There seems to be a bug in the original code when gntdev_get_page() is called with writeable=true then the page needs to be marked dirty before being put.

To address this, a bool writeable is added in gnt_dev_copy_batch, set it in gntdev_grant_copy_seg() (and drop writeable argument to gntdev_get_page()) and then, based on batch->writeable, use set_page_dirty_lock().

Fixes a4cdb556cae0 (xen/gntdev: add ioctl for grant copy)
Cc John Hubbard <[email protected]>
Cc Boris Ostrovsky <[email protected]>
Cc Juergen Gross <[email protected]>
Cc David Vrabel <[email protected]>
---
 drivers/xen/gntdev.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/xen/gntdev.c +12 -5
index 64a9025a..5e1411b 100644
@@ -720,17 +720,18 @@ struct gntdev_copy_batch {
 	s16 __user *status[GNTDEV_COPY_BATCH];
 	unsigned int nr_ops;
 	unsigned int nr_pages;
+	bool writeable;
 };
 
 static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
-			   bool writeable, unsigned long *gfn)
+				unsigned long *gfn)
 {
 	unsigned long addr = (unsigned long)virt;
 	struct page *page;
 	unsigned long xen_pfn;
 	int ret;
 
-	ret = get_user_pages_fast(addr, 1, writeable ? FOLL_WRITE : 0, &page);
+	ret = get_user_pages_fast(addr, 1, batch->writeable ? FOLL_WRITE : 0, &page);
 	if (ret < 0)
 		return ret;
 
@@ -746,9 +747,13 @@ static void gntdev_put_pages(struct gntdev_copy_batch *batch)
 {
 	unsigned int i;
 
-	for (i = 0; i < batch->nr_pages; i++)
+	for (i = 0; i < batch->nr_pages; i++) {
+		if (batch->writeable && !PageDirty(batch->pages[i]))
+			set_page_dirty_lock(batch->pages[i]);
 		put_page(batch->pages[i]);
+	}
 	batch->nr_pages = 0;
+	batch->writeable = false;
 }
 
 static int gntdev_copy(struct gntdev_copy_batch *batch)
@@ -837,8 +842,9 @@ static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
 			virt = seg->source.virt + copied;
 			off = (unsigned long)virt & ~XEN_PAGE_MASK;
 			len = min(len, (size_t)XEN_PAGE_SIZE - off);
+			batch->writeable = false;
 
-			ret = gntdev_get_page(batch, virt, false, &gfn);
+			ret = gntdev_get_page(batch, virt, &gfn);
 			if (ret < 0)
 				return ret;
 
@@ -856,8 +862,9 @@ static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
 			virt = seg->dest.virt + copied;
 			off = (unsigned long)virt & ~XEN_PAGE_MASK;
 			len = min(len, (size_t)XEN_PAGE_SIZE - off);
+			batch->writeable = true;
 
-			ret = gntdev_get_page(batch, virt, true, &gfn);
+			ret = gntdev_get_page(batch, virt, &gfn);
 			if (ret < 0)
 				return ret;
 
Signature · 1 line
1.9.1
#1 lore raw

PATCH 2/2 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/
Cc John Hubbard <[email protected]>
Cc Boris Ostrovsky <[email protected]>
Cc Juergen Gross <[email protected]>
Cc David Vrabel <[email protected]>
---
 drivers/xen/gntdev.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
drivers/xen/gntdev.c +2 -8
index 5e1411b..a36b712 100644
@@ -731,7 +731,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, batch->writeable ? FOLL_WRITE : 0, &page);
+	ret = pin_user_pages_fast(addr, 1, batch->writeable ? FOLL_WRITE : 0, &page);
 	if (ret < 0)
 		return ret;
 
@@ -745,13 +745,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++) {
-		if(batch->writeable && !PageDirty(batch->pages[i]))
-			set_page_dirty_lock(batch->pages[i]);
-		put_page(batch->pages[i]);
-	}
+	unpin_user_pages_dirty_lock(batch->pages, batch->nr_pages, batch->writeable);
 	batch->nr_pages = 0;
 	batch->writeable = false;
 }
Signature · 1 line
1.9.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] xen/gntdev.c: Convert get_user_pages*() to pin_user_pages*()

Quoted 6 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

Quoted 8 lines

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

        https://lwn.net/Articles/807108/
Cc John Hubbard <[email protected]>
Cc Boris Ostrovsky <[email protected]>
Cc Juergen Gross <[email protected]>
Cc David Vrabel <[email protected]>
#3 lore raw

Re: [PATCH 2/2] xen/gntdev.c: Convert get_user_pages*() to pin_user_pages*()

Quoted 12 lines · On Fri, Sep 11, 2020 at 8:12 PM <[email protected]> wrote:
Quoted 9 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/
Quoted 8 lines
Quoted 5 lines
Cc John Hubbard <[email protected]>
Cc Boris Ostrovsky <[email protected]>
Cc Juergen Gross <[email protected]>
Cc David Vrabel <[email protected]>

Are these 2 patches queued for 5.10-rc1 ?

Quoted 2 lines
#4 lore raw

Re: [PATCH 2/2] xen/gntdev.c: Convert get_user_pages*() to pin_user_pages*()

Quoted 12 lines · Souptick Joarder
Quoted 11 lines · On Fri, Sep 11, 2020 at 8:12 PM <[email protected]> wrote:
Quoted 9 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/
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Cc John Hubbard <[email protected]>
Cc Boris Ostrovsky <[email protected]>
Cc Juergen Gross <[email protected]>
Cc David Vrabel <[email protected]>

Are these 2 patches queued for 5.10-rc1 ?

Yes, I am preparing the branch. (BTW, your second patch appears to have been either manually edited or not generated on top of the first patch. Please don't do this next time)

-boris

#5 lore raw

Re: [PATCH 2/2] xen/gntdev.c: Convert get_user_pages*() to pin_user_pages*()

Quoted 21 lines · On Tue, Sep 29, 2020 at 6:00 PM <[email protected]> wrote:
Quoted 18 lines · Souptick Joarder
Quoted 17 lines · On Fri, Sep 11, 2020 at 8:12 PM <[email protected]> wrote:
Quoted 14 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/
Cc John Hubbard <[email protected]>
Cc Boris Ostrovsky <[email protected]>
Cc Juergen Gross <[email protected]>
Cc David Vrabel <[email protected]>
Quoted 5 lines
Quoted 2 lines
Quoted 1 line

Are these 2 patches queued for 5.10-rc1 ?

Yes, I am preparing the branch. (BTW, your second patch appears to have been either manually edited or not generated on top of the first patch. Please don't do this next time)

I created it on top of the first one and didn't edit manually. I was able to apply it in my local repository.
What was the error ?

#6 lore raw

Re: [PATCH 2/2] xen/gntdev.c: Convert get_user_pages*() to pin_user_pages*()

Quoted 23 lines · Souptick Joarder
Quoted 22 lines · On Tue, Sep 29, 2020 at 6:00 PM <[email protected]> wrote:
Quoted 19 lines · Souptick Joarder
Quoted 18 lines · On Fri, Sep 11, 2020 at 8:12 PM <[email protected]> wrote:
Quoted 6 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

Quoted 8 lines

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

        https://lwn.net/Articles/807108/
Cc John Hubbard <[email protected]>
Cc Boris Ostrovsky <[email protected]>
Cc Juergen Gross <[email protected]>
Cc David Vrabel <[email protected]>
Quoted 9 lines
Quoted 4 lines
Quoted 1 line

Are these 2 patches queued for 5.10-rc1 ?

Yes, I am preparing the branch. (BTW, your second patch appears to have been either manually edited or not generated on top of the first patch. Please don't do this next time)

I created it on top of the first one and didn't edit manually. I was able to apply it in my local repository.
What was the error ?

Patch 1:

+ if (batch->writeable && !PageDirty(batch->pages[i]))

Patch 2:

- if(batch->writeable && !PageDirty(batch->pages[i]))

This doesn't look to me like usual whitespace damage in-flight. Anyway, this has been applied to for-linus-5.10

-boris

Reviewed-by on v1 1/2
#7 lore raw
In reply to Souptick Joarder
Quoted 9 lines · Souptick Joarder

There seems to be a bug in the original code when gntdev_get_page() is called with writeable=true then the page needs to be marked dirty before being put.

To address this, a bool writeable is added in gnt_dev_copy_batch, set it in gntdev_grant_copy_seg() (and drop writeable argument to gntdev_get_page()) and then, based on batch->writeable, use set_page_dirty_lock().

Quoted 7 lines
Fixes a4cdb556cae0 (xen/gntdev: add ioctl for grant copy)
Cc John Hubbard <[email protected]>
Cc Boris Ostrovsky <[email protected]>
Cc Juergen Gross <[email protected]>
Cc David Vrabel <[email protected]>

(can be added at commit time)