Skip to contentSkip to conversation

xen/blkback: safely unmap purge persistent grants

PATCH RESEND In review Bob Liu 5 participants 23 messages Mar 26, 2015 – Apr 1, 2015

Subsystems (3)
lore
Share & tools
lore

Bob Liu

author
#0 lore raw

PATCH RESEND 1/2 xen/blkback: safely unmap purge persistent grants

Commit c43cf3ea8385 ("xen-blkback: safely unmap grants in case they are still in use") use gnttab_unmap_refs_async() to wait until the mapped pages are no longer in use before unmapping them, but that commit missed the persistent case. Purge persistent pages can't be unmapped either unless no longer in use.

---
 drivers/block/xen-blkback/blkback.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/block/xen-blkback/blkback.c +18 -6
index 2a04d34..f59d7c3 100644
@@ -327,8 +327,18 @@ void xen_blkbk_unmap_purged_grants(struct work_struct *work)
 	struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
 	struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
 	struct persistent_gnt *persistent_gnt;
-	int ret, segs_to_unmap = 0;
+	int segs_to_unmap = 0;
 	struct xen_blkif *blkif = container_of(work, typeof(*blkif), persistent_purge_work);
+	struct gntab_unmap_queue_data unmap_data;
+	struct completion unmap_completion;
+
+	init_completion(&unmap_completion);
+
+	unmap_data.data = &unmap_completion;
+	unmap_data.done = &free_persistent_gnts_unmap_callback;
+	unmap_data.pages = pages;
+	unmap_data.unmap_ops = unmap;
+	unmap_data.kunmap_ops = NULL;
 
 	while(!list_empty(&blkif->persistent_purge_list)) {
 		persistent_gnt = list_first_entry(&blkif->persistent_purge_list,
@@ -344,17 +354,19 @@ void xen_blkbk_unmap_purged_grants(struct work_struct *work)
 		pages[segs_to_unmap] = persistent_gnt->page;
 
 		if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
-			ret = gnttab_unmap_refs(unmap, NULL, pages,
-				segs_to_unmap);
-			BUG_ON(ret);
+			unmap_data.count = segs_to_unmap;
+			gnttab_unmap_refs_async(&unmap_data);
+			wait_for_completion(&unmap_completion);
+
 			put_free_pages(blkif, pages, segs_to_unmap);
 			segs_to_unmap = 0;
 		}
 		kfree(persistent_gnt);
 	}
 	if (segs_to_unmap > 0) {
-		ret = gnttab_unmap_refs(unmap, NULL, pages, segs_to_unmap);
-		BUG_ON(ret);
+		unmap_data.count = segs_to_unmap;
+		gnttab_unmap_refs_async(&unmap_data);
+		wait_for_completion(&unmap_completion);
 		put_free_pages(blkif, pages, segs_to_unmap);
 	}
 }
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 2/2 xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

---
 drivers/block/xen-blkback/blkback.c | 31 +++----------------------------
 drivers/xen/gntdev.c                | 28 +++-------------------------
 drivers/xen/grant-table.c           | 29 +++++++++++++++++++++++++++++
 include/xen/grant_table.h           |  1 +
 4 files changed, 36 insertions(+), 53 deletions(-)
drivers/block/xen-blkback/blkback.c +3 -28
index f59d7c3..0c8da82 100644
@@ -263,17 +263,6 @@ static void put_persistent_gnt(struct xen_blkif *blkif,
 	atomic_dec(&blkif->persistent_gnt_in_use);
 }
 
-static void free_persistent_gnts_unmap_callback(int result,
-						struct gntab_unmap_queue_data *data)
-{
-	struct completion *c = data->data;
-
-	/* BUG_ON used to reproduce existing behaviour,
-	   but is this the best way to deal with this? */
-	BUG_ON(result);
-	complete(c);
-}
-
 static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
                                  unsigned int num)
 {
@@ -283,12 +272,7 @@ static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
 	struct rb_node *n;
 	int segs_to_unmap = 0;
 	struct gntab_unmap_queue_data unmap_data;
-	struct completion unmap_completion;
-
-	init_completion(&unmap_completion);
 
-	unmap_data.data = &unmap_completion;
-	unmap_data.done = &free_persistent_gnts_unmap_callback;
 	unmap_data.pages = pages;
 	unmap_data.unmap_ops = unmap;
 	unmap_data.kunmap_ops = NULL;
@@ -308,8 +292,7 @@ static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
 			!rb_next(&persistent_gnt->node)) {
 
 			unmap_data.count = segs_to_unmap;
-			gnttab_unmap_refs_async(&unmap_data);
-			wait_for_completion(&unmap_completion);
+			BUG_ON(gnttab_unmap_refs_async_wait_completion(&unmap_data));
 
 			put_free_pages(blkif, pages, segs_to_unmap);
 			segs_to_unmap = 0;
@@ -330,12 +313,7 @@ void xen_blkbk_unmap_purged_grants(struct work_struct *work)
 	int segs_to_unmap = 0;
 	struct xen_blkif *blkif = container_of(work, typeof(*blkif), persistent_purge_work);
 	struct gntab_unmap_queue_data unmap_data;
-	struct completion unmap_completion;
 
-	init_completion(&unmap_completion);
-
-	unmap_data.data = &unmap_completion;
-	unmap_data.done = &free_persistent_gnts_unmap_callback;
 	unmap_data.pages = pages;
 	unmap_data.unmap_ops = unmap;
 	unmap_data.kunmap_ops = NULL;
@@ -355,9 +333,7 @@ void xen_blkbk_unmap_purged_grants(struct work_struct *work)
 
 		if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
 			unmap_data.count = segs_to_unmap;
-			gnttab_unmap_refs_async(&unmap_data);
-			wait_for_completion(&unmap_completion);
-
+			BUG_ON(gnttab_unmap_refs_async_wait_completion(&unmap_data));
 			put_free_pages(blkif, pages, segs_to_unmap);
 			segs_to_unmap = 0;
 		}
@@ -365,8 +341,7 @@ void xen_blkbk_unmap_purged_grants(struct work_struct *work)
 	}
 	if (segs_to_unmap > 0) {
 		unmap_data.count = segs_to_unmap;
-		gnttab_unmap_refs_async(&unmap_data);
-		wait_for_completion(&unmap_completion);
+		BUG_ON(gnttab_unmap_refs_async_wait_completion(&unmap_data));
 		put_free_pages(blkif, pages, segs_to_unmap);
 	}
 }
drivers/xen/gntdev.c +3 -25
index d5bb1a3..7a88524 100644
@@ -327,30 +327,10 @@ static int map_grant_pages(struct grant_map *map)
 	return err;
 }
 
-struct unmap_grant_pages_callback_data
-{
-	struct completion completion;
-	int result;
-};
-
-static void unmap_grant_callback(int result,
-				 struct gntab_unmap_queue_data *data)
-{
-	struct unmap_grant_pages_callback_data* d = data->data;
-
-	d->result = result;
-	complete(&d->completion);
-}
-
 static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
 {
 	int i, err = 0;
 	struct gntab_unmap_queue_data unmap_data;
-	struct unmap_grant_pages_callback_data data;
-
-	init_completion(&data.completion);
-	unmap_data.data = &data;
-	unmap_data.done= &unmap_grant_callback;
 
 	if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
 		int pgno = (map->notify.addr >> PAGE_SHIFT);
@@ -367,11 +347,9 @@ static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
 	unmap_data.pages = map->pages + offset;
 	unmap_data.count = pages;
 
-	gnttab_unmap_refs_async(&unmap_data);
-
-	wait_for_completion(&data.completion);
-	if (data.result)
-		return data.result;
+	err = gnttab_unmap_refs_async_wait_completion(&unmap_data);
+	if (err)
+		return err;
 
 	for (i = 0; i < pages; i++) {
 		if (map->unmap_ops[offset+i].status)
drivers/xen/grant-table.c +29 -0
index 17972fb..1dce9bc 100644
@@ -123,6 +123,12 @@ struct gnttab_ops {
 	int (*query_foreign_access)(grant_ref_t ref);
 };
 
+struct unmap_refs_async_callback_data
+{
+	struct completion completion;
+	int result;
+};
+
 static struct gnttab_ops *gnttab_interface;
 
 static int grant_table_version;
@@ -863,6 +869,29 @@ void gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item)
 }
 EXPORT_SYMBOL_GPL(gnttab_unmap_refs_async);
 
+static void unmap_refs_async_callback(int result,
+		struct gntab_unmap_queue_data *data)
+{
+	struct unmap_refs_async_callback_data* d = data->data;
+
+	d->result = result;
+	complete(&d->completion);
+}
+
+int gnttab_unmap_refs_async_wait_completion(struct gntab_unmap_queue_data* item)
+{
+	struct unmap_refs_async_callback_data data;
+
+	init_completion(&data.completion);
+	item->data = &data;
+	item->done= &unmap_refs_async_callback;
+	gnttab_unmap_refs_async(item);
+	wait_for_completion(&data.completion);
+
+	return data.result;
+}
+EXPORT_SYMBOL_GPL(gnttab_unmap_refs_async_wait_completion);
+
 static int gnttab_map_frames_v1(xen_pfn_t *frames, unsigned int nr_gframes)
 {
 	int rc;
include/xen/grant_table.h +1 -0
index 143ca5f..46bad05 100644
@@ -191,6 +191,7 @@ int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
 		      struct gnttab_unmap_grant_ref *kunmap_ops,
 		      struct page **pages, unsigned int count);
 void gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item);
+int gnttab_unmap_refs_async_wait_completion(struct gntab_unmap_queue_data* item);
 
 
 /* Perform a batch of grant map/copy operations. Retry every batch slot
Signature · 1 line
1.8.3.1
author
#2 lore raw

PATCH 2/2 xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

In reply to Bob Liu

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

---
 drivers/block/xen-blkback/blkback.c | 31 +++----------------------------
 drivers/xen/gntdev.c                | 28 +++-------------------------
 drivers/xen/grant-table.c           | 29 +++++++++++++++++++++++++++++
 include/xen/grant_table.h           |  1 +
 4 files changed, 36 insertions(+), 53 deletions(-)
drivers/block/xen-blkback/blkback.c +3 -28
index f59d7c3..0c8da82 100644
@@ -263,17 +263,6 @@ static void put_persistent_gnt(struct xen_blkif *blkif,
 	atomic_dec(&blkif->persistent_gnt_in_use);
 }
 
-static void free_persistent_gnts_unmap_callback(int result,
-						struct gntab_unmap_queue_data *data)
-{
-	struct completion *c = data->data;
-
-	/* BUG_ON used to reproduce existing behaviour,
-	   but is this the best way to deal with this? */
-	BUG_ON(result);
-	complete(c);
-}
-
 static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
                                  unsigned int num)
 {
@@ -283,12 +272,7 @@ static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
 	struct rb_node *n;
 	int segs_to_unmap = 0;
 	struct gntab_unmap_queue_data unmap_data;
-	struct completion unmap_completion;
-
-	init_completion(&unmap_completion);
 
-	unmap_data.data = &unmap_completion;
-	unmap_data.done = &free_persistent_gnts_unmap_callback;
 	unmap_data.pages = pages;
 	unmap_data.unmap_ops = unmap;
 	unmap_data.kunmap_ops = NULL;
@@ -308,8 +292,7 @@ static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
 			!rb_next(&persistent_gnt->node)) {
 
 			unmap_data.count = segs_to_unmap;
-			gnttab_unmap_refs_async(&unmap_data);
-			wait_for_completion(&unmap_completion);
+			BUG_ON(gnttab_unmap_refs_async_wait_completion(&unmap_data));
 
 			put_free_pages(blkif, pages, segs_to_unmap);
 			segs_to_unmap = 0;
@@ -330,12 +313,7 @@ void xen_blkbk_unmap_purged_grants(struct work_struct *work)
 	int segs_to_unmap = 0;
 	struct xen_blkif *blkif = container_of(work, typeof(*blkif), persistent_purge_work);
 	struct gntab_unmap_queue_data unmap_data;
-	struct completion unmap_completion;
 
-	init_completion(&unmap_completion);
-
-	unmap_data.data = &unmap_completion;
-	unmap_data.done = &free_persistent_gnts_unmap_callback;
 	unmap_data.pages = pages;
 	unmap_data.unmap_ops = unmap;
 	unmap_data.kunmap_ops = NULL;
@@ -355,9 +333,7 @@ void xen_blkbk_unmap_purged_grants(struct work_struct *work)
 
 		if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
 			unmap_data.count = segs_to_unmap;
-			gnttab_unmap_refs_async(&unmap_data);
-			wait_for_completion(&unmap_completion);
-
+			BUG_ON(gnttab_unmap_refs_async_wait_completion(&unmap_data));
 			put_free_pages(blkif, pages, segs_to_unmap);
 			segs_to_unmap = 0;
 		}
@@ -365,8 +341,7 @@ void xen_blkbk_unmap_purged_grants(struct work_struct *work)
 	}
 	if (segs_to_unmap > 0) {
 		unmap_data.count = segs_to_unmap;
-		gnttab_unmap_refs_async(&unmap_data);
-		wait_for_completion(&unmap_completion);
+		BUG_ON(gnttab_unmap_refs_async_wait_completion(&unmap_data));
 		put_free_pages(blkif, pages, segs_to_unmap);
 	}
 }
drivers/xen/gntdev.c +3 -25
index d5bb1a3..7a88524 100644
@@ -327,30 +327,10 @@ static int map_grant_pages(struct grant_map *map)
 	return err;
 }
 
-struct unmap_grant_pages_callback_data
-{
-	struct completion completion;
-	int result;
-};
-
-static void unmap_grant_callback(int result,
-				 struct gntab_unmap_queue_data *data)
-{
-	struct unmap_grant_pages_callback_data* d = data->data;
-
-	d->result = result;
-	complete(&d->completion);
-}
-
 static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
 {
 	int i, err = 0;
 	struct gntab_unmap_queue_data unmap_data;
-	struct unmap_grant_pages_callback_data data;
-
-	init_completion(&data.completion);
-	unmap_data.data = &data;
-	unmap_data.done= &unmap_grant_callback;
 
 	if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
 		int pgno = (map->notify.addr >> PAGE_SHIFT);
@@ -367,11 +347,9 @@ static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
 	unmap_data.pages = map->pages + offset;
 	unmap_data.count = pages;
 
-	gnttab_unmap_refs_async(&unmap_data);
-
-	wait_for_completion(&data.completion);
-	if (data.result)
-		return data.result;
+	err = gnttab_unmap_refs_async_wait_completion(&unmap_data);
+	if (err)
+		return err;
 
 	for (i = 0; i < pages; i++) {
 		if (map->unmap_ops[offset+i].status)
drivers/xen/grant-table.c +29 -0
index 17972fb..1dce9bc 100644
@@ -123,6 +123,12 @@ struct gnttab_ops {
 	int (*query_foreign_access)(grant_ref_t ref);
 };
 
+struct unmap_refs_async_callback_data
+{
+	struct completion completion;
+	int result;
+};
+
 static struct gnttab_ops *gnttab_interface;
 
 static int grant_table_version;
@@ -863,6 +869,29 @@ void gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item)
 }
 EXPORT_SYMBOL_GPL(gnttab_unmap_refs_async);
 
+static void unmap_refs_async_callback(int result,
+		struct gntab_unmap_queue_data *data)
+{
+	struct unmap_refs_async_callback_data* d = data->data;
+
+	d->result = result;
+	complete(&d->completion);
+}
+
+int gnttab_unmap_refs_async_wait_completion(struct gntab_unmap_queue_data* item)
+{
+	struct unmap_refs_async_callback_data data;
+
+	init_completion(&data.completion);
+	item->data = &data;
+	item->done= &unmap_refs_async_callback;
+	gnttab_unmap_refs_async(item);
+	wait_for_completion(&data.completion);
+
+	return data.result;
+}
+EXPORT_SYMBOL_GPL(gnttab_unmap_refs_async_wait_completion);
+
 static int gnttab_map_frames_v1(xen_pfn_t *frames, unsigned int nr_gframes)
 {
 	int rc;
include/xen/grant_table.h +1 -0
index 143ca5f..46bad05 100644
@@ -191,6 +191,7 @@ int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
 		      struct gnttab_unmap_grant_ref *kunmap_ops,
 		      struct page **pages, unsigned int count);
 void gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item);
+int gnttab_unmap_refs_async_wait_completion(struct gntab_unmap_queue_data* item);
 
 
 /* Perform a batch of grant map/copy operations. Retry every batch slot
Signature · 1 line
1.8.3.1
Acked-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

El 26/03/15 a les 13.16, Bob Liu ha escrit:

Quoted 5 lines

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

For the blkback parts:

Although, as David already said, I think we should do better than BUG_ON on error.

#4 lore raw

Re: [PATCH 2/2] xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

Quoted 5 lines · Roger Pau Monné

El 26/03/15 a les 13.16, Bob Liu ha escrit:

Quoted 4 lines

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

Quoted 8 lines
Quoted 1 line

For the blkback parts:

Although, as David already said, I think we should do better than BUG_ON on error.

Have an 'stuck deferred pages' list and an timer to occasionally flush them out? Something similar to 569ca5b3f94cd0b3295ec5943aa457cf4a4f6a3a "xen/gnttab: add deferred freeing logic" ?

Quoted 1 line
#5 lore raw

Re: [PATCH 2/2] xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

In reply to Roger Pau Monné
Quoted 5 lines · Roger Pau Monné

El 26/03/15 a les 13.16, Bob Liu ha escrit:

Quoted 4 lines

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

Quoted 8 lines
Quoted 1 line

For the blkback parts:

Although, as David already said, I think we should do better than BUG_ON on error.

Have an 'stuck deferred pages' list and an timer to occasionally flush them out? Something similar to 569ca5b3f94cd0b3295ec5943aa457cf4a4f6a3a "xen/gnttab: add deferred freeing logic" ?

Quoted 1 line
author
#6 lore raw

Re: [PATCH 2/2] xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

Quoted 6 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Roger Pau Monné

El 26/03/15 a les 13.16, Bob Liu ha escrit:

Quoted 4 lines

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

Quoted 9 lines
Quoted 8 lines
Quoted 1 line

For the blkback parts:

Although, as David already said, I think we should do better than BUG_ON on error.

This patch only keeps the existing behaviour the same as before. I prefer to make new patches(based on this one) if want to change the behaviour.

Quoted 3 lines

Have an 'stuck deferred pages' list and an timer to occasionally flush them out? Something similar to 569ca5b3f94cd0b3295ec5943aa457cf4a4f6a3a "xen/gnttab: add deferred freeing logic" ?

Sounds like a good idea.

Signature · 2 lines
Regards,
-Bob
author
#7 lore raw

Re: [PATCH 2/2] xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

In reply to Konrad Rzeszutek Wilk
Quoted 6 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Roger Pau Monné

El 26/03/15 a les 13.16, Bob Liu ha escrit:

Quoted 4 lines

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

Quoted 9 lines
Quoted 8 lines
Quoted 1 line

For the blkback parts:

Although, as David already said, I think we should do better than BUG_ON on error.

This patch only keeps the existing behaviour the same as before. I prefer to make new patches(based on this one) if want to change the behaviour.

Quoted 3 lines

Have an 'stuck deferred pages' list and an timer to occasionally flush them out? Something similar to 569ca5b3f94cd0b3295ec5943aa457cf4a4f6a3a "xen/gnttab: add deferred freeing logic" ?

Sounds like a good idea.

Signature · 2 lines
Regards,
-Bob
#8 lore raw

Re: [PATCH 2/2] xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

Quoted 22 lines · Bob Liu
Quoted 6 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Roger Pau Monné

El 26/03/15 a les 13.16, Bob Liu ha escrit:

Quoted 4 lines

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

Quoted 9 lines
Quoted 8 lines
Quoted 1 line

For the blkback parts:

Although, as David already said, I think we should do better than BUG_ON on error.

This patch only keeps the existing behaviour the same as before. I prefer to make new patches(based on this one) if want to change the behaviour.

Quoted 5 lines
Quoted 3 lines

Have an 'stuck deferred pages' list and an timer to occasionally flush them out? Something similar to 569ca5b3f94cd0b3295ec5943aa457cf4a4f6a3a "xen/gnttab: add deferred freeing logic" ?

Sounds like a good idea.

If it failed the first time I don't see how it would succeed later. No one has actually hit this particular BUG_ON() so I would leave it as is.

David

#9 lore raw

Re: [Xen-devel] [PATCH 2/2] xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

In reply to Bob Liu
Quoted 22 lines · Bob Liu
Quoted 6 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Roger Pau Monné

El 26/03/15 a les 13.16, Bob Liu ha escrit:

Quoted 4 lines

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

Quoted 9 lines
Quoted 8 lines
Quoted 1 line

For the blkback parts:

Although, as David already said, I think we should do better than BUG_ON on error.

This patch only keeps the existing behaviour the same as before. I prefer to make new patches(based on this one) if want to change the behaviour.

Quoted 5 lines
Quoted 3 lines

Have an 'stuck deferred pages' list and an timer to occasionally flush them out? Something similar to 569ca5b3f94cd0b3295ec5943aa457cf4a4f6a3a "xen/gnttab: add deferred freeing logic" ?

Sounds like a good idea.

If it failed the first time I don't see how it would succeed later. No one has actually hit this particular BUG_ON() so I would leave it as is.

David

Acked-by on v1 2/2
#10 lore raw

Re: [PATCH 2/2] xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

In reply to Bob Liu

El 26/03/15 a les 13.16, Bob Liu ha escrit:

Quoted 5 lines

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

For the blkback parts:

Although, as David already said, I think we should do better than BUG_ON on error.

#11 lore raw

Re: [PATCH 2/2] xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

In reply to Bob Liu
Quoted 5 lines · Bob Liu

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

Jens, and would you be OK if this patch went through the Xen tree?

Thanks.

Quoted 198 lines
---
 drivers/block/xen-blkback/blkback.c | 31 +++----------------------------
 drivers/xen/gntdev.c                | 28 +++-------------------------
 drivers/xen/grant-table.c           | 29 +++++++++++++++++++++++++++++
 include/xen/grant_table.h           |  1 +
 4 files changed, 36 insertions(+), 53 deletions(-)
drivers/block/xen-blkback/blkback.c +3 -28
index f59d7c3..0c8da82 100644
@@ -263,17 +263,6 @@ static void put_persistent_gnt(struct xen_blkif *blkif,
 	atomic_dec(&blkif->persistent_gnt_in_use);
 }
 
-static void free_persistent_gnts_unmap_callback(int result,
-						struct gntab_unmap_queue_data *data)
-{
-	struct completion *c = data->data;
-
-	/* BUG_ON used to reproduce existing behaviour,
-	   but is this the best way to deal with this? */
-	BUG_ON(result);
-	complete(c);
-}
-
 static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
                                  unsigned int num)
 {
@@ -283,12 +272,7 @@ static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
 	struct rb_node *n;
 	int segs_to_unmap = 0;
 	struct gntab_unmap_queue_data unmap_data;
-	struct completion unmap_completion;
-
-	init_completion(&unmap_completion);
 
-	unmap_data.data = &unmap_completion;
-	unmap_data.done = &free_persistent_gnts_unmap_callback;
 	unmap_data.pages = pages;
 	unmap_data.unmap_ops = unmap;
 	unmap_data.kunmap_ops = NULL;
@@ -308,8 +292,7 @@ static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
 			!rb_next(&persistent_gnt->node)) {
 
 			unmap_data.count = segs_to_unmap;
-			gnttab_unmap_refs_async(&unmap_data);
-			wait_for_completion(&unmap_completion);
+			BUG_ON(gnttab_unmap_refs_async_wait_completion(&unmap_data));
 
 			put_free_pages(blkif, pages, segs_to_unmap);
 			segs_to_unmap = 0;
@@ -330,12 +313,7 @@ void xen_blkbk_unmap_purged_grants(struct work_struct *work)
 	int segs_to_unmap = 0;
 	struct xen_blkif *blkif = container_of(work, typeof(*blkif), persistent_purge_work);
 	struct gntab_unmap_queue_data unmap_data;
-	struct completion unmap_completion;
 
-	init_completion(&unmap_completion);
-
-	unmap_data.data = &unmap_completion;
-	unmap_data.done = &free_persistent_gnts_unmap_callback;
 	unmap_data.pages = pages;
 	unmap_data.unmap_ops = unmap;
 	unmap_data.kunmap_ops = NULL;
@@ -355,9 +333,7 @@ void xen_blkbk_unmap_purged_grants(struct work_struct *work)
 
 		if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
 			unmap_data.count = segs_to_unmap;
-			gnttab_unmap_refs_async(&unmap_data);
-			wait_for_completion(&unmap_completion);
-
+			BUG_ON(gnttab_unmap_refs_async_wait_completion(&unmap_data));
 			put_free_pages(blkif, pages, segs_to_unmap);
 			segs_to_unmap = 0;
 		}
@@ -365,8 +341,7 @@ void xen_blkbk_unmap_purged_grants(struct work_struct *work)
 	}
 	if (segs_to_unmap > 0) {
 		unmap_data.count = segs_to_unmap;
-		gnttab_unmap_refs_async(&unmap_data);
-		wait_for_completion(&unmap_completion);
+		BUG_ON(gnttab_unmap_refs_async_wait_completion(&unmap_data));
 		put_free_pages(blkif, pages, segs_to_unmap);
 	}
 }
drivers/xen/gntdev.c +3 -25
index d5bb1a3..7a88524 100644
@@ -327,30 +327,10 @@ static int map_grant_pages(struct grant_map *map)
 	return err;
 }
 
-struct unmap_grant_pages_callback_data
-{
-	struct completion completion;
-	int result;
-};
-
-static void unmap_grant_callback(int result,
-				 struct gntab_unmap_queue_data *data)
-{
-	struct unmap_grant_pages_callback_data* d = data->data;
-
-	d->result = result;
-	complete(&d->completion);
-}
-
 static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
 {
 	int i, err = 0;
 	struct gntab_unmap_queue_data unmap_data;
-	struct unmap_grant_pages_callback_data data;
-
-	init_completion(&data.completion);
-	unmap_data.data = &data;
-	unmap_data.done= &unmap_grant_callback;
 
 	if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
 		int pgno = (map->notify.addr >> PAGE_SHIFT);
@@ -367,11 +347,9 @@ static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
 	unmap_data.pages = map->pages + offset;
 	unmap_data.count = pages;
 
-	gnttab_unmap_refs_async(&unmap_data);
-
-	wait_for_completion(&data.completion);
-	if (data.result)
-		return data.result;
+	err = gnttab_unmap_refs_async_wait_completion(&unmap_data);
+	if (err)
+		return err;
 
 	for (i = 0; i < pages; i++) {
 		if (map->unmap_ops[offset+i].status)
drivers/xen/grant-table.c +29 -0
index 17972fb..1dce9bc 100644
@@ -123,6 +123,12 @@ struct gnttab_ops {
 	int (*query_foreign_access)(grant_ref_t ref);
 };
 
+struct unmap_refs_async_callback_data
+{
+	struct completion completion;
+	int result;
+};
+
 static struct gnttab_ops *gnttab_interface;
 
 static int grant_table_version;
@@ -863,6 +869,29 @@ void gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item)
 }
 EXPORT_SYMBOL_GPL(gnttab_unmap_refs_async);
 
+static void unmap_refs_async_callback(int result,
+		struct gntab_unmap_queue_data *data)
+{
+	struct unmap_refs_async_callback_data* d = data->data;
+
+	d->result = result;
+	complete(&d->completion);
+}
+
+int gnttab_unmap_refs_async_wait_completion(struct gntab_unmap_queue_data* item)
+{
+	struct unmap_refs_async_callback_data data;
+
+	init_completion(&data.completion);
+	item->data = &data;
+	item->done= &unmap_refs_async_callback;
+	gnttab_unmap_refs_async(item);
+	wait_for_completion(&data.completion);
+
+	return data.result;
+}
+EXPORT_SYMBOL_GPL(gnttab_unmap_refs_async_wait_completion);
+
 static int gnttab_map_frames_v1(xen_pfn_t *frames, unsigned int nr_gframes)
 {
 	int rc;
include/xen/grant_table.h +1 -0
index 143ca5f..46bad05 100644
@@ -191,6 +191,7 @@ int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
 		      struct gnttab_unmap_grant_ref *kunmap_ops,
 		      struct page **pages, unsigned int count);
 void gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item);
+int gnttab_unmap_refs_async_wait_completion(struct gntab_unmap_queue_data* item);
 
 
 /* Perform a batch of grant map/copy operations. Retry every batch slot
Signature · 1 line
1.8.3.1
#12 lore raw

Re: [PATCH 2/2] xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

In reply to Bob Liu
Quoted 5 lines · Bob Liu

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

Jens, and would you be OK if this patch went through the Xen tree?

Thanks.

Quoted 198 lines
---
 drivers/block/xen-blkback/blkback.c | 31 +++----------------------------
 drivers/xen/gntdev.c                | 28 +++-------------------------
 drivers/xen/grant-table.c           | 29 +++++++++++++++++++++++++++++
 include/xen/grant_table.h           |  1 +
 4 files changed, 36 insertions(+), 53 deletions(-)
drivers/block/xen-blkback/blkback.c +3 -28
index f59d7c3..0c8da82 100644
@@ -263,17 +263,6 @@ static void put_persistent_gnt(struct xen_blkif *blkif,
 	atomic_dec(&blkif->persistent_gnt_in_use);
 }
 
-static void free_persistent_gnts_unmap_callback(int result,
-						struct gntab_unmap_queue_data *data)
-{
-	struct completion *c = data->data;
-
-	/* BUG_ON used to reproduce existing behaviour,
-	   but is this the best way to deal with this? */
-	BUG_ON(result);
-	complete(c);
-}
-
 static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
                                  unsigned int num)
 {
@@ -283,12 +272,7 @@ static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
 	struct rb_node *n;
 	int segs_to_unmap = 0;
 	struct gntab_unmap_queue_data unmap_data;
-	struct completion unmap_completion;
-
-	init_completion(&unmap_completion);
 
-	unmap_data.data = &unmap_completion;
-	unmap_data.done = &free_persistent_gnts_unmap_callback;
 	unmap_data.pages = pages;
 	unmap_data.unmap_ops = unmap;
 	unmap_data.kunmap_ops = NULL;
@@ -308,8 +292,7 @@ static void free_persistent_gnts(struct xen_blkif *blkif, struct rb_root *root,
 			!rb_next(&persistent_gnt->node)) {
 
 			unmap_data.count = segs_to_unmap;
-			gnttab_unmap_refs_async(&unmap_data);
-			wait_for_completion(&unmap_completion);
+			BUG_ON(gnttab_unmap_refs_async_wait_completion(&unmap_data));
 
 			put_free_pages(blkif, pages, segs_to_unmap);
 			segs_to_unmap = 0;
@@ -330,12 +313,7 @@ void xen_blkbk_unmap_purged_grants(struct work_struct *work)
 	int segs_to_unmap = 0;
 	struct xen_blkif *blkif = container_of(work, typeof(*blkif), persistent_purge_work);
 	struct gntab_unmap_queue_data unmap_data;
-	struct completion unmap_completion;
 
-	init_completion(&unmap_completion);
-
-	unmap_data.data = &unmap_completion;
-	unmap_data.done = &free_persistent_gnts_unmap_callback;
 	unmap_data.pages = pages;
 	unmap_data.unmap_ops = unmap;
 	unmap_data.kunmap_ops = NULL;
@@ -355,9 +333,7 @@ void xen_blkbk_unmap_purged_grants(struct work_struct *work)
 
 		if (++segs_to_unmap == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
 			unmap_data.count = segs_to_unmap;
-			gnttab_unmap_refs_async(&unmap_data);
-			wait_for_completion(&unmap_completion);
-
+			BUG_ON(gnttab_unmap_refs_async_wait_completion(&unmap_data));
 			put_free_pages(blkif, pages, segs_to_unmap);
 			segs_to_unmap = 0;
 		}
@@ -365,8 +341,7 @@ void xen_blkbk_unmap_purged_grants(struct work_struct *work)
 	}
 	if (segs_to_unmap > 0) {
 		unmap_data.count = segs_to_unmap;
-		gnttab_unmap_refs_async(&unmap_data);
-		wait_for_completion(&unmap_completion);
+		BUG_ON(gnttab_unmap_refs_async_wait_completion(&unmap_data));
 		put_free_pages(blkif, pages, segs_to_unmap);
 	}
 }
drivers/xen/gntdev.c +3 -25
index d5bb1a3..7a88524 100644
@@ -327,30 +327,10 @@ static int map_grant_pages(struct grant_map *map)
 	return err;
 }
 
-struct unmap_grant_pages_callback_data
-{
-	struct completion completion;
-	int result;
-};
-
-static void unmap_grant_callback(int result,
-				 struct gntab_unmap_queue_data *data)
-{
-	struct unmap_grant_pages_callback_data* d = data->data;
-
-	d->result = result;
-	complete(&d->completion);
-}
-
 static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
 {
 	int i, err = 0;
 	struct gntab_unmap_queue_data unmap_data;
-	struct unmap_grant_pages_callback_data data;
-
-	init_completion(&data.completion);
-	unmap_data.data = &data;
-	unmap_data.done= &unmap_grant_callback;
 
 	if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
 		int pgno = (map->notify.addr >> PAGE_SHIFT);
@@ -367,11 +347,9 @@ static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
 	unmap_data.pages = map->pages + offset;
 	unmap_data.count = pages;
 
-	gnttab_unmap_refs_async(&unmap_data);
-
-	wait_for_completion(&data.completion);
-	if (data.result)
-		return data.result;
+	err = gnttab_unmap_refs_async_wait_completion(&unmap_data);
+	if (err)
+		return err;
 
 	for (i = 0; i < pages; i++) {
 		if (map->unmap_ops[offset+i].status)
drivers/xen/grant-table.c +29 -0
index 17972fb..1dce9bc 100644
@@ -123,6 +123,12 @@ struct gnttab_ops {
 	int (*query_foreign_access)(grant_ref_t ref);
 };
 
+struct unmap_refs_async_callback_data
+{
+	struct completion completion;
+	int result;
+};
+
 static struct gnttab_ops *gnttab_interface;
 
 static int grant_table_version;
@@ -863,6 +869,29 @@ void gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item)
 }
 EXPORT_SYMBOL_GPL(gnttab_unmap_refs_async);
 
+static void unmap_refs_async_callback(int result,
+		struct gntab_unmap_queue_data *data)
+{
+	struct unmap_refs_async_callback_data* d = data->data;
+
+	d->result = result;
+	complete(&d->completion);
+}
+
+int gnttab_unmap_refs_async_wait_completion(struct gntab_unmap_queue_data* item)
+{
+	struct unmap_refs_async_callback_data data;
+
+	init_completion(&data.completion);
+	item->data = &data;
+	item->done= &unmap_refs_async_callback;
+	gnttab_unmap_refs_async(item);
+	wait_for_completion(&data.completion);
+
+	return data.result;
+}
+EXPORT_SYMBOL_GPL(gnttab_unmap_refs_async_wait_completion);
+
 static int gnttab_map_frames_v1(xen_pfn_t *frames, unsigned int nr_gframes)
 {
 	int rc;
include/xen/grant_table.h +1 -0
index 143ca5f..46bad05 100644
@@ -191,6 +191,7 @@ int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
 		      struct gnttab_unmap_grant_ref *kunmap_ops,
 		      struct page **pages, unsigned int count);
 void gnttab_unmap_refs_async(struct gntab_unmap_queue_data* item);
+int gnttab_unmap_refs_async_wait_completion(struct gntab_unmap_queue_data* item);
 
 
 /* Perform a batch of grant map/copy operations. Retry every batch slot
Signature · 1 line
1.8.3.1
#13 lore raw

Re: [Xen-devel] [PATCH 2/2] xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

In reply to Bob Liu
Quoted 4 lines · Bob Liu

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

[...]

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+int gnttab_unmap_refs_async_wait_completion(struct gntab_unmap_queue_data* item)

This name is a bit wordy. Can you rename it to:
gnttab_unmap_refs_sync() gnttab_unmap_refs_wait(), or gnttab_unmap_refs_safe().

(I can't decide which is the best name.).

David

author
#14 lore raw

Re: [PATCH 2/2] xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

Quoted 11 lines · David Vrabel
Quoted 4 lines · Bob Liu

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

[...]

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+int gnttab_unmap_refs_async_wait_completion(struct gntab_unmap_queue_data* item)

This name is a bit wordy. Can you rename it to:
gnttab_unmap_refs_sync() gnttab_unmap_refs_wait(), or

Sure, I think gnttab_unmap_refs_sync() is fine.
Will be updated.

Quoted 6 lines

gnttab_unmap_refs_safe().

(I can't decide which is the best name.).

David

Signature · 2 lines
Regards,
-Bob
author
#15 lore raw

Re: [Xen-devel] [PATCH 2/2] xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

In reply to David Vrabel
Quoted 11 lines · David Vrabel
Quoted 4 lines · Bob Liu

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

[...]

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+int gnttab_unmap_refs_async_wait_completion(struct gntab_unmap_queue_data* item)

This name is a bit wordy. Can you rename it to:
gnttab_unmap_refs_sync() gnttab_unmap_refs_wait(), or

Sure, I think gnttab_unmap_refs_sync() is fine.
Will be updated.

Quoted 6 lines

gnttab_unmap_refs_safe().

(I can't decide which is the best name.).

David

Signature · 2 lines
Regards,
-Bob
#16 lore raw

Re: [PATCH 2/2] xen/grant: introduce func gnttab_unmap_refs_async_wait_completion()

In reply to Bob Liu
Quoted 4 lines · Bob Liu

There are several place using gnttab async unmap and wait for completion, so move the common code to a function gnttab_unmap_refs_async_wait_completion().

[...]

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+int gnttab_unmap_refs_async_wait_completion(struct gntab_unmap_queue_data* item)

This name is a bit wordy. Can you rename it to:
gnttab_unmap_refs_sync() gnttab_unmap_refs_wait(), or gnttab_unmap_refs_safe().

(I can't decide which is the best name.).

David

Acked-by on v1 1/2
#17 lore raw
In reply to Bob Liu

El 26/03/15 a les 13.16, Bob Liu ha escrit:

Quoted 6 lines

Commit c43cf3ea8385 ("xen-blkback: safely unmap grants in case they are still in use") use gnttab_unmap_refs_async() to wait until the mapped pages are no longer in use before unmapping them, but that commit missed the persistent case. Purge persistent pages can't be unmapped either unless no longer in use.

AFAICT since this is basically the same patch as what I've sent:

#18 lore raw
Quoted 6 lines · Roger Pau Monné

El 26/03/15 a les 13.16, Bob Liu ha escrit:

Quoted 5 lines

Commit c43cf3ea8385 ("xen-blkback: safely unmap grants in case they are still in use") use gnttab_unmap_refs_async() to wait until the mapped pages are no longer in use before unmapping them, but that commit missed the persistent case. Purge persistent pages can't be unmapped either unless no longer in use.

Quoted 6 lines
Quoted 1 line

AFAICT since this is basically the same patch as what I've sent:

Hey Jens,

These patches are on top of patches that add this framework in the Xen tree. Would you be OK with this patch (and the other which I will CC shortly) to go through the Xen tree?

Thanks.

#19 lore raw
In reply to Roger Pau Monné
Quoted 6 lines · Roger Pau Monné

El 26/03/15 a les 13.16, Bob Liu ha escrit:

Quoted 5 lines

Commit c43cf3ea8385 ("xen-blkback: safely unmap grants in case they are still in use") use gnttab_unmap_refs_async() to wait until the mapped pages are no longer in use before unmapping them, but that commit missed the persistent case. Purge persistent pages can't be unmapped either unless no longer in use.

Quoted 6 lines
Quoted 1 line

AFAICT since this is basically the same patch as what I've sent:

Hey Jens,

These patches are on top of patches that add this framework in the Xen tree. Would you be OK with this patch (and the other which I will CC shortly) to go through the Xen tree?

Thanks.

#20 lore raw
Quoted 11 lines · Konrad Rzeszutek Wilk
Quoted 10 lines · Roger Pau Monné

El 26/03/15 a les 13.16, Bob Liu ha escrit:

Quoted 6 lines

Commit c43cf3ea8385 ("xen-blkback: safely unmap grants in case they are still in use") use gnttab_unmap_refs_async() to wait until the mapped pages are no longer in use before unmapping them, but that commit missed the persistent case. Purge persistent pages can't be unmapped either unless no longer in use.

AFAICT since this is basically the same patch as what I've sent:

Quoted 8 lines
Quoted 2 lines

Hey Jens,

These patches are on top of patches that add this framework in the Xen tree. Would you be OK with this patch (and the other which I will CC shortly) to go through the Xen tree?

Sure that's fine.

Signature · 1 line
Jens Axboe
#21 lore raw
In reply to Konrad Rzeszutek Wilk
Quoted 11 lines · Konrad Rzeszutek Wilk
Quoted 10 lines · Roger Pau Monné

El 26/03/15 a les 13.16, Bob Liu ha escrit:

Quoted 6 lines

Commit c43cf3ea8385 ("xen-blkback: safely unmap grants in case they are still in use") use gnttab_unmap_refs_async() to wait until the mapped pages are no longer in use before unmapping them, but that commit missed the persistent case. Purge persistent pages can't be unmapped either unless no longer in use.

AFAICT since this is basically the same patch as what I've sent:

Quoted 8 lines
Quoted 2 lines

Hey Jens,

These patches are on top of patches that add this framework in the Xen tree. Would you be OK with this patch (and the other which I will CC shortly) to go through the Xen tree?

Sure that's fine.

Signature · 1 line
Jens Axboe
Acked-by on v1 1/2
#22 lore raw
In reply to Bob Liu

El 26/03/15 a les 13.16, Bob Liu ha escrit:

Quoted 6 lines

Commit c43cf3ea8385 ("xen-blkback: safely unmap grants in case they are still in use") use gnttab_unmap_refs_async() to wait until the mapped pages are no longer in use before unmapping them, but that commit missed the persistent case. Purge persistent pages can't be unmapped either unless no longer in use.

AFAICT since this is basically the same patch as what I've sent: