Skip to contentSkip to conversation

Xen backend support for paged out grant targets.

PATCH In review [email protected] 5 participants 12 messages Aug 27, 2012 – Sep 12, 2012

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

PATCH 1/1 Xen backend support for paged out grant targets.

From: Andres Lagar-Cavilla <[email protected]>

Since Xen-4.2, hvm domains may have portions of their memory paged out. When a foreign domain (such as dom0) attempts to map these frames, the map will initially fail. The hypervisor returns a suitable errno, and kicks an asynchronous page-in operation carried out by a helper. The foreign domain is expected to retry the mapping operation until it eventually succeeds. The foreign domain is not put to sleep because itself could be the one running the pager assist (typical scenario for dom0).

This patch adds support for this mechanism for backend drivers using grant mapping and copying operations. Specifically, this covers the blkback and gntdev drivers (which map foregin grants), and the netback driver (which copies foreign grants).

* Add GNTST_eagain, already exposed by Xen, to the grant interface.
* Add a retry method for grants that fail with GNTST_eagain (i.e. because the target foregin frame is paged out).
* Insert hooks with appropriate macro decorators in the aforementioned drivers.

The retry loop is only invoked if the grant operation status is GNTST_eagain. It guarantees to leave a new status code different from GNTST_eagain. Any other status code results in identical code execution as before.

The retry loop performs 256 attempts with increasing time intervals through a 32 second period. It uses msleep to yield while waiting for the next retry.

---
 drivers/net/xen-netback/netback.c   |   13 +++++++++++++
 drivers/xen/grant-table.c           |   25 +++++++++++++++++++++++++
 drivers/xen/xenbus/xenbus_client.c  |    6 ++----
 include/xen/grant_table.h           |   25 +++++++++++++++++++++++++
 include/xen/interface/grant_table.h |    2 ++
 5 files changed, 67 insertions(+), 4 deletions(-)
drivers/net/xen-netback/netback.c +13 -0
index 682633b..063adf5 100644
@@ -548,6 +548,8 @@ static int netbk_check_gop(struct xenvif *vif, int nr_meta_slots,
 
 	for (i = 0; i < nr_meta_slots; i++) {
 		copy_op = npo->copy + npo->copy_cons++;
+		if (copy_op->status == GNTST_eagain)
+			gnttab_retry_eagain_copy(copy_op);
 		if (copy_op->status != GNTST_okay) {
 			netdev_dbg(vif->dev,
 				   "Bad status %d from copy to DOM%d.\n",
@@ -976,6 +978,11 @@ static int xen_netbk_tx_check_gop(struct xen_netbk *netbk,
 
 	/* Check status of header. */
 	err = gop->status;
+	if (unlikely(err == GNTST_eagain))
+	{
+		gnttab_retry_eagain_copy(gop);
+		err = gop->status;
+	}
 	if (unlikely(err)) {
 		pending_ring_idx_t index;
 		index = pending_index(netbk->pending_prod++);
@@ -1001,6 +1008,12 @@ static int xen_netbk_tx_check_gop(struct xen_netbk *netbk,
 			if (unlikely(err))
 				xen_netbk_idx_release(netbk, pending_idx);
 			continue;
+		} else {
+			if (newerr == GNTST_eagain)
+			{
+				gnttab_retry_eagain_copy(gop);
+				newerr = gop->status;
+			}
 		}
 
 		/* Error on this fragment: respond to client with an error. */
drivers/xen/grant-table.c +25 -0
index eea81cf..2b62a63 100644
@@ -38,6 +38,7 @@
 #include <linux/vmalloc.h>
 #include <linux/uaccess.h>
 #include <linux/io.h>
+#include <linux/delay.h>
 #include <linux/hardirq.h>
 
 #include <xen/xen.h>
@@ -823,6 +824,25 @@ unsigned int gnttab_max_grant_frames(void)
 }
 EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
 
+void
+gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
+						const char *func)
+{
+	u8 delay = 1;
+
+	do {
+		BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
+		if (*status == GNTST_eagain)
+			msleep(delay++);
+	} while ((*status == GNTST_eagain) && delay);
+
+	if (delay == 0) {
+		printk(KERN_ERR "%s: %s eagain grant\n", func, current->comm);
+		*status = GNTST_bad_page;
+	}
+}
+EXPORT_SYMBOL_GPL(gnttab_retry_eagain_gop);
+
 int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
 		    struct gnttab_map_grant_ref *kmap_ops,
 		    struct page **pages, unsigned int count)
@@ -836,6 +856,11 @@ int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
 	if (ret)
 		return ret;
 
+	/* Retry eagain maps */
+	for (i = 0; i < count; i++)
+		if (map_ops[i].status == GNTST_eagain)
+			gnttab_retry_eagain_map(map_ops + i);
+
 	if (xen_feature(XENFEAT_auto_translated_physmap))
 		return ret;
 
drivers/xen/xenbus/xenbus_client.c +2 -4
index b3e146e..749f6a3 100644
@@ -490,8 +490,7 @@ static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev,
 
 	op.host_addr = arbitrary_virt_to_machine(pte).maddr;
 
-	if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
-		BUG();
+	gnttab_map_grant_no_eagain(&op);
 
 	if (op.status != GNTST_okay) {
 		free_vm_area(area);
@@ -572,8 +571,7 @@ int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref,
 	gnttab_set_map_op(&op, (unsigned long)vaddr, GNTMAP_host_map, gnt_ref,
 			  dev->otherend_id);
 
-	if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
-		BUG();
+	gnttab_map_grant_no_eagain(&op);
 
 	if (op.status != GNTST_okay) {
 		xenbus_dev_fatal(dev, op.status,
include/xen/grant_table.h +25 -0
index 11e27c3..0f75184 100644
@@ -183,6 +183,31 @@ unsigned int gnttab_max_grant_frames(void);
 
 #define gnttab_map_vaddr(map) ((void *)(map.host_virt_addr))
 
+/* Retry a grant map/copy operation when the hypervisor returns GNTST_eagain.
+ * This is typically due to paged out target frames.
+ * Generic entry-point, use macro decorators below for specific grant
+ * operations.
+ * Will retry for 1, 2, ... 255 ms, i.e. 256 times during 32 seconds.
+ * Return value in *status guaranteed to no longer be GNTST_eagain. */
+void gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
+                             const char *func);
+
+#define gnttab_retry_eagain_map(_gop)                       \
+    gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, (_gop), \
+                            &(_gop)->status, __func__)
+
+#define gnttab_retry_eagain_copy(_gop)                  \
+    gnttab_retry_eagain_gop(GNTTABOP_copy, (_gop),      \
+                            &(_gop)->status, __func__)
+
+#define gnttab_map_grant_no_eagain(_gop)                                    \
+do {                                                                        \
+    if ( HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, (_gop), 1))      \
+        BUG();                                                              \
+    if ((_gop)->status == GNTST_eagain)                                     \
+        gnttab_retry_eagain_map((_gop));                                    \
+} while(0)
+
 int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
 		    struct gnttab_map_grant_ref *kmap_ops,
 		    struct page **pages, unsigned int count);
include/xen/interface/grant_table.h +2 -0
index 7da811b..66cb734 100644
@@ -520,6 +520,7 @@ DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version);
 #define GNTST_permission_denied (-8) /* Not enough privilege for operation.  */
 #define GNTST_bad_page         (-9) /* Specified page was invalid for op.    */
 #define GNTST_bad_copy_arg    (-10) /* copy arguments cross page boundary */
+#define GNTST_eagain          (-12) /* Retry.                                */
 
 #define GNTTABOP_error_msgs {                   \
     "okay",                                     \
@@ -533,6 +534,7 @@ DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version);
     "permission denied",                        \
     "bad page",                                 \
     "copy arguments cross page boundary"        \
+    "retry"                                     \
 }
 
 #endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */
Signature · 1 line
1.7.5.4
#1 lore raw
Quoted 15 lines · On 27/08/12 17:51, [email protected] wrote:

From: Andres Lagar-Cavilla <[email protected]>

Since Xen-4.2, hvm domains may have portions of their memory paged out. When a foreign domain (such as dom0) attempts to map these frames, the map will initially fail. The hypervisor returns a suitable errno, and kicks an asynchronous page-in operation carried out by a helper. The foreign domain is expected to retry the mapping operation until it eventually succeeds. The foreign domain is not put to sleep because itself could be the one running the pager assist (typical scenario for dom0).

This patch adds support for this mechanism for backend drivers using grant mapping and copying operations. Specifically, this covers the blkback and gntdev drivers (which map foregin grants), and the netback driver (which copies foreign grants).

Quoted 4 lines

* Add GNTST_eagain, already exposed by Xen, to the grant interface.
* Add a retry method for grants that fail with GNTST_eagain (i.e. because the target foregin frame is paged out).
* Insert hooks with appropriate macro decorators in the aforementioned drivers.

I think you should implement wrappers around HYPERVISOR_grant_table_op() have have the wrapper do the retries instead of every backend having to check for EAGAIN and issue the retries itself. Similar to the gnttab_map_grant_no_eagain() function you've already added.

Why do some operations not retry anyway?

drivers/xen/grant-table.c unsigned int gnttab_max_grant_frames(void) · line 837

Quoted 11 lines
drivers/xen/grant-table.c +11 -0
+void
+gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
+						const char *func)
+{
+	u8 delay = 1;
+
+	do {
+		BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
+		if (*status == GNTST_eagain)
+			msleep(delay++);
+	} while ((*status == GNTST_eagain) && delay);

Terminating the loop when delay wraps is a bit subtle. Why not make delay unsigned and check delay <= MAX_DELAY?

Would it be sensible to ramp the delay faster? Perhaps double each iteration with a maximum possible delay of e.g., 256 ms.

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void); · line 209

Quoted 7 lines
include/xen/grant_table.h +7 -0
+#define gnttab_map_grant_no_eagain(_gop)                                    \
+do {                                                                        \
+    if ( HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, (_gop), 1))      \
+        BUG();                                                              \
+    if ((_gop)->status == GNTST_eagain)                                     \
+        gnttab_retry_eagain_map((_gop));                                    \
+} while(0)

Inline functions, please.

David

#2 lore raw
Quoted 21 lines · David Vrabel
Quoted 15 lines · On 27/08/12 17:51, [email protected] wrote:

From: Andres Lagar-Cavilla <[email protected]>

Since Xen-4.2, hvm domains may have portions of their memory paged out. When a foreign domain (such as dom0) attempts to map these frames, the map will initially fail. The hypervisor returns a suitable errno, and kicks an asynchronous page-in operation carried out by a helper. The foreign domain is expected to retry the mapping operation until it eventually succeeds. The foreign domain is not put to sleep because itself could be the one running the pager assist (typical scenario for dom0).

This patch adds support for this mechanism for backend drivers using grant mapping and copying operations. Specifically, this covers the blkback and gntdev drivers (which map foregin grants), and the netback driver (which copies foreign grants).

Quoted 4 lines

* Add GNTST_eagain, already exposed by Xen, to the grant interface.
* Add a retry method for grants that fail with GNTST_eagain (i.e. because the target foregin frame is paged out).
* Insert hooks with appropriate macro decorators in the aforementioned drivers.

Quoted 6 lines

I think you should implement wrappers around HYPERVISOR_grant_table_op() have have the wrapper do the retries instead of every backend having to check for EAGAIN and issue the retries itself. Similar to the gnttab_map_grant_no_eagain() function you've already added.

Why do some operations not retry anyway?

All operations retry. The reason why I could not make it as elegant as you suggest is because grant operations are submitted in batches and their status(es?) later checked individually elsewhere. This is the case for netback. Note that both blkback and gntdev use a more linear structure with the gnttab_map_refs helper, which allows me to hide all the retry gore from those drivers into grant table code. Likewise for xenbus ring mapping.

In summary, outside of core grant table code, only the netback driver needs to check explicitly for retries, due to its batch-copy-delayed-per-slot-check structure.

Quoted 15 lines

drivers/xen/grant-table.c unsigned int gnttab_max_grant_frames(void) · line 837

Quoted 12 lines
drivers/xen/grant-table.c +11 -0
+void
+gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
+						const char *func)
+{
+	u8 delay = 1;
+
+	do {
+		BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
+		if (*status == GNTST_eagain)
+			msleep(delay++);
+	} while ((*status == GNTST_eagain) && delay);

Terminating the loop when delay wraps is a bit subtle. Why not make delay unsigned and check delay <= MAX_DELAY?

Good idea (MAX_DELAY == 256). I'd like to get Konrad's feedback before a re-spin.

Quoted 3 lines

Would it be sensible to ramp the delay faster? Perhaps double each iteration with a maximum possible delay of e.g., 256 ms.

Generally speaking we've never seen past three retries. I am open to changing the algorithm but there is a significant possibility it won't matter at all.

Quoted 10 lines

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void); · line 209

Quoted 8 lines
include/xen/grant_table.h +7 -0
+#define gnttab_map_grant_no_eagain(_gop)                                    \
+do {                                                                        \
+    if ( HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, (_gop), 1))      \
+        BUG();                                                              \
+    if ((_gop)->status == GNTST_eagain)                                     \
+        gnttab_retry_eagain_map((_gop));                                    \
+} while(0)

Inline functions, please.

I want to retain the original context for debugging. Eventually we print __func__ if things go wrong.

Thanks, great feedback
Andres

Quoted 2 lines

David

#3 lore raw

Actually acted upon your feedback ipso facto:

commit d5fab912caa1f0cf6be0a6773f502d3417a207b6

Author: Andres Lagar-Cavilla <[email protected]>
Date:   Sun Aug 26 09:45:57 2012 -0400

Xen backend support for paged out grant targets.

Since Xen-4.2, hvm domains may have portions of their memory paged out. When a foreign domain (such as dom0) attempts to map these frames, the map will initially fail. The hypervisor returns a suitable errno, and kicks an asynchronous page-in operation carried out by a helper. The foreign domain is expected to retry the mapping operation until it eventually succeeds. The foreign domain is not put to sleep because itself could be the one running the pager assist (typical scenario for dom0).

This patch adds support for this mechanism for backend drivers using grant mapping and copying operations. Specifically, this covers the blkback and gntdev drivers (which map foregin grants), and the netback driver (which copies foreign grants).

* Add GNTST_eagain, already exposed by Xen, to the grant interface.
* Add a retry method for grants that fail with GNTST_eagain (i.e. because the target foregin frame is paged out).
* Insert hooks with appropriate macro decorators in the aforementioned drivers.

The retry loop is only invoked if the grant operation status is GNTST_eagain. It guarantees to leave a new status code different from GNTST_eagain. Any other status code results in identical code execution as before.

The retry loop performs 256 attempts with increasing time intervals through a 32 second period. It uses msleep to yield while waiting for the next retry.

V2 after feedback from David Vrabel:
* Explicit MAX_DELAY instead of wrap-around delay into zero
* Abstract GNTST_eagain check into core grant table code for netback module.

drivers/net/xen-netback/netback.c +3 -8
index 682633b..5610fd8 100644
@@ -635,9 +635,7 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 		return;
 
 	BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
-	ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, &netbk->grant_copy_op,
-					npo.copy_prod);
-	BUG_ON(ret != 0);
+	gnttab_batch_copy_no_eagain(netbk->grant_copy_op, npo.copy_prod);
 
 	while ((skb = __skb_dequeue(&rxq)) != NULL) {
 		sco = (struct skb_cb_overlay *)skb->cb;
@@ -1460,18 +1458,15 @@ static void xen_netbk_tx_submit(struct xen_netbk *netbk)
 static void xen_netbk_tx_action(struct xen_netbk *netbk)
 {
 	unsigned nr_gops;
-	int ret;
 
 	nr_gops = xen_netbk_tx_build_gops(netbk);
 
 	if (nr_gops == 0)
 		return;
-	ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
-					netbk->tx_copy_ops, nr_gops);
-	BUG_ON(ret);
 
-	xen_netbk_tx_submit(netbk);
+	gnttab_batch_copy_no_eagain(netbk->tx_copy_ops, nr_gops);
 
+	xen_netbk_tx_submit(netbk);
 }
 
 static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx)
drivers/xen/grant-table.c +26 -0
index eea81cf..96543b2 100644
@@ -38,6 +38,7 @@
 #include <linux/vmalloc.h>
 #include <linux/uaccess.h>
 #include <linux/io.h>
+#include <linux/delay.h>
 #include <linux/hardirq.h>
 
 #include <xen/xen.h>
@@ -823,6 +824,26 @@ unsigned int gnttab_max_grant_frames(void)
 }
 EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
 
+#define MAX_DELAY 256
+void
+gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
+						const char *func)
+{
+	unsigned delay = 1;
+
+	do {
+		BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
+		if (*status == GNTST_eagain)
+			msleep(delay++);
+	} while ((*status == GNTST_eagain) && (delay < MAX_DELAY));
+
+	if (delay >= MAX_DELAY) {
+		printk(KERN_ERR "%s: %s eagain grant\n", func, current->comm);
+		*status = GNTST_bad_page;
+	}
+}
+EXPORT_SYMBOL_GPL(gnttab_retry_eagain_gop);
+
 int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
 		    struct gnttab_map_grant_ref *kmap_ops,
 		    struct page **pages, unsigned int count)
@@ -836,6 +857,11 @@ int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
 	if (ret)
 		return ret;
 
+	/* Retry eagain maps */
+	for (i = 0; i < count; i++)
+		if (map_ops[i].status == GNTST_eagain)
+			gnttab_retry_eagain_map(map_ops + i);
+
 	if (xen_feature(XENFEAT_auto_translated_physmap))
 		return ret;
 
drivers/xen/xenbus/xenbus_client.c +2 -4
index b3e146e..749f6a3 100644
@@ -490,8 +490,7 @@ static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev,
 
 	op.host_addr = arbitrary_virt_to_machine(pte).maddr;
 
-	if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
-		BUG();
+	gnttab_map_grant_no_eagain(&op);
 
 	if (op.status != GNTST_okay) {
 		free_vm_area(area);
@@ -572,8 +571,7 @@ int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref,
 	gnttab_set_map_op(&op, (unsigned long)vaddr, GNTMAP_host_map, gnt_ref,
 			  dev->otherend_id);
 
-	if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
-		BUG();
+	gnttab_map_grant_no_eagain(&op);
 
 	if (op.status != GNTST_okay) {
 		xenbus_dev_fatal(dev, op.status,
include/xen/grant_table.h +38 -0
index 11e27c3..2fecfab 100644
@@ -43,6 +43,7 @@
 #include <xen/interface/grant_table.h>
 
 #include <asm/xen/hypervisor.h>
+#include <asm/xen/hypercall.h>
 
 #include <xen/features.h>
 
@@ -183,6 +184,43 @@ unsigned int gnttab_max_grant_frames(void);
 
 #define gnttab_map_vaddr(map) ((void *)(map.host_virt_addr))
 
+/* Retry a grant map/copy operation when the hypervisor returns GNTST_eagain.
+ * This is typically due to paged out target frames.
+ * Generic entry-point, use macro decorators below for specific grant
+ * operations.
+ * Will retry for 1, 2, ... 255 ms, i.e. 256 times during 32 seconds.
+ * Return value in *status guaranteed to no longer be GNTST_eagain. */
+void gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
+                             const char *func);
+
+#define gnttab_retry_eagain_map(_gop)                       \
+    gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, (_gop), \
+                            &(_gop)->status, __func__)
+
+#define gnttab_retry_eagain_copy(_gop)                  \
+    gnttab_retry_eagain_gop(GNTTABOP_copy, (_gop),      \
+                            &(_gop)->status, __func__)
+
+#define gnttab_map_grant_no_eagain(_gop)                                    \
+do {                                                                        \
+    if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, (_gop), 1))       \
+        BUG();                                                              \
+    if ((_gop)->status == GNTST_eagain)                                     \
+        gnttab_retry_eagain_map((_gop));                                    \
+} while(0)
+
+static inline void
+gnttab_batch_copy_no_eagain(struct gnttab_copy *batch, unsigned count)
+{
+    unsigned i;
+    struct gnttab_copy *op;
+
+    BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_copy, batch, count));
+    for (i = 0, op = batch; i < count; i++, op++)
+        if (op->status == GNTST_eagain)
+            gnttab_retry_eagain_copy(op);
+}
+
 int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
 		    struct gnttab_map_grant_ref *kmap_ops,
 		    struct page **pages, unsigned int count);
include/xen/interface/grant_table.h +2 -0
index 7da811b..66cb734 100644
@@ -520,6 +520,7 @@ DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version);
 #define GNTST_permission_denied (-8) /* Not enough privilege for operation.  */
 #define GNTST_bad_page         (-9) /* Specified page was invalid for op.    */
 #define GNTST_bad_copy_arg    (-10) /* copy arguments cross page boundary */
+#define GNTST_eagain          (-12) /* Retry.                                */
 
 #define GNTTABOP_error_msgs {                   \
     "okay",                                     \
@@ -533,6 +534,7 @@ DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version);
     "permission denied",                        \
     "bad page",                                 \
     "copy arguments cross page boundary"        \
+    "retry"                                     \
 }
 
 #endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */
Quoted 76 lines · Andres Lagar-Cavilla
Quoted 21 lines · David Vrabel
Quoted 15 lines · On 27/08/12 17:51, [email protected] wrote:

From: Andres Lagar-Cavilla <[email protected]>

Since Xen-4.2, hvm domains may have portions of their memory paged out. When a foreign domain (such as dom0) attempts to map these frames, the map will initially fail. The hypervisor returns a suitable errno, and kicks an asynchronous page-in operation carried out by a helper. The foreign domain is expected to retry the mapping operation until it eventually succeeds. The foreign domain is not put to sleep because itself could be the one running the pager assist (typical scenario for dom0).

This patch adds support for this mechanism for backend drivers using grant mapping and copying operations. Specifically, this covers the blkback and gntdev drivers (which map foregin grants), and the netback driver (which copies foreign grants).

Quoted 4 lines

* Add GNTST_eagain, already exposed by Xen, to the grant interface.
* Add a retry method for grants that fail with GNTST_eagain (i.e. because the target foregin frame is paged out).
* Insert hooks with appropriate macro decorators in the aforementioned drivers.

Quoted 6 lines

I think you should implement wrappers around HYPERVISOR_grant_table_op() have have the wrapper do the retries instead of every backend having to check for EAGAIN and issue the retries itself. Similar to the gnttab_map_grant_no_eagain() function you've already added.

Why do some operations not retry anyway?

All operations retry. The reason why I could not make it as elegant as you suggest is because grant operations are submitted in batches and their status(es?) later checked individually elsewhere. This is the case for netback. Note that both blkback and gntdev use a more linear structure with the gnttab_map_refs helper, which allows me to hide all the retry gore from those drivers into grant table code. Likewise for xenbus ring mapping.

In summary, outside of core grant table code, only the netback driver needs to check explicitly for retries, due to its batch-copy-delayed-per-slot-check structure.

Quoted 15 lines

Review

Quoted 12 lines
(quoted diff) +11 -0
+void
+gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
+						const char *func)
+{
+	u8 delay = 1;
+
+	do {
+		BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
+		if (*status == GNTST_eagain)
+			msleep(delay++);
+	} while ((*status == GNTST_eagain) && delay);

Terminating the loop when delay wraps is a bit subtle. Why not make delay unsigned and check delay <= MAX_DELAY?

Good idea (MAX_DELAY == 256). I'd like to get Konrad's feedback before a re-spin.

Quoted 3 lines

Would it be sensible to ramp the delay faster? Perhaps double each iteration with a maximum possible delay of e.g., 256 ms.

Generally speaking we've never seen past three retries. I am open to changing the algorithm but there is a significant possibility it won't matter at all.

Quoted 10 lines

Review

Quoted 8 lines
(quoted diff) +7 -0
+#define gnttab_map_grant_no_eagain(_gop)                                    \
+do {                                                                        \
+    if ( HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, (_gop), 1))      \
+        BUG();                                                              \
+    if ((_gop)->status == GNTST_eagain)                                     \
+        gnttab_retry_eagain_map((_gop));                                    \
+} while(0)

Inline functions, please.

I want to retain the original context for debugging. Eventually we print __func__ if things go wrong.

Thanks, great feedback
Andres

Quoted 2 lines

David

#4 lore raw
Quoted 7 lines · Andres Lagar-Cavilla

Actually acted upon your feedback ipso facto:

commit d5fab912caa1f0cf6be0a6773f502d3417a207b6

Author: Andres Lagar-Cavilla <[email protected]>
Date:   Sun Aug 26 09:45:57 2012 -0400

Xen backend support for paged out grant targets.

This looks mostly fine expect for the #define instead of inline functions.

Quoted 1 line

+#define gnttab_map_grant_no_eagain(_gop) \

This name tripped me up previously. As I read this as:

gnttab_map_grant_no_[retries_for]_eagain().

Perhaps gnttab_map_grant_with_retries() ? Or similar?

David

#5 lore raw
Quoted 11 lines · David Vrabel
Quoted 7 lines · Andres Lagar-Cavilla

Actually acted upon your feedback ipso facto:

commit d5fab912caa1f0cf6be0a6773f502d3417a207b6

Author: Andres Lagar-Cavilla <[email protected]>
Date:   Sun Aug 26 09:45:57 2012 -0400

Xen backend support for paged out grant targets.

This looks mostly fine expect for the #define instead of inline functions.

Quoted 7 lines
Quoted 1 line

+#define gnttab_map_grant_no_eagain(_gop) \

This name tripped me up previously. As I read this as:

gnttab_map_grant_no_[retries_for]_eagain().

Perhaps gnttab_map_grant_with_retries() ? Or similar?

gnttab_map_grant_retry ?

Besides that, it looks good to me. Ian Campbell needs to Ack so that David Miller (the network maintainer) can pick it up. Please CC them both and also LKML.

Quoted 2 lines

David

#6 lore raw
Quoted 18 lines · Konrad Rzeszutek Wilk
Quoted 17 lines · David Vrabel
Quoted 7 lines · Andres Lagar-Cavilla

Actually acted upon your feedback ipso facto:

commit d5fab912caa1f0cf6be0a6773f502d3417a207b6

Author: Andres Lagar-Cavilla <[email protected]>
Date:   Sun Aug 26 09:45:57 2012 -0400

Xen backend support for paged out grant targets.

This looks mostly fine expect for the #define instead of inline functions.

Quoted 1 line

+#define gnttab_map_grant_no_eagain(_gop) \

This name tripped me up previously. As I read this as:

gnttab_map_grant_no_[retries_for]_eagain().

Quoted 7 lines
Quoted 1 line

Perhaps gnttab_map_grant_with_retries() ? Or similar?

gnttab_map_grant_retry ?

Besides that, it looks good to me. Ian Campbell needs to Ack so that David Miller (the network maintainer) can pick it up. Please CC them both and also LKML.

Sure will do. However, I need to bring attention to the following hunk:

drivers/net/xen-netback/netback.c +0 -0
index 682633b..5610fd8 100644
@@ -635,9 +635,7 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
		return;

	BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));

- ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, &netbk->grant_copy_op,
- npo.copy_prod);
- BUG_ON(ret != 0);
+ gnttab_batch_copy_no_eagain(netbk->grant_copy_op, npo.copy_prod);

It seems like the (extant) code passes a struct gnttab_copy ** to the grant table hypercall (&netbk->grant_copy_op, defined as struct gnttab_copy [])

I "fixed" it to pass a struct gnttab_copy * (netbk->grant_copy_op, no '&'). This matches the other invocation of the grant copy hyper call (in netbk_tx_action). Did I fix it, though? I am confused as to how this could have ever worked.

(cc'ing Ian Campbell for more insight)
Andres

Quoted 2 lines
Quoted 2 lines

David

#7 lore raw
Quoted 21 lines · Andres Lagar-Cavilla
Quoted 18 lines · Konrad Rzeszutek Wilk
Quoted 17 lines · David Vrabel
Quoted 7 lines · Andres Lagar-Cavilla

Actually acted upon your feedback ipso facto:

commit d5fab912caa1f0cf6be0a6773f502d3417a207b6

Author: Andres Lagar-Cavilla <[email protected]>
Date:   Sun Aug 26 09:45:57 2012 -0400

Xen backend support for paged out grant targets.

This looks mostly fine expect for the #define instead of inline functions.

Quoted 1 line

+#define gnttab_map_grant_no_eagain(_gop) \

This name tripped me up previously. As I read this as:

gnttab_map_grant_no_[retries_for]_eagain().

Quoted 7 lines
Quoted 7 lines
Quoted 1 line

Perhaps gnttab_map_grant_with_retries() ? Or similar?

gnttab_map_grant_retry ?

Besides that, it looks good to me. Ian Campbell needs to Ack so that David Miller (the network maintainer) can pick it up. Please CC them both and also LKML

Konrad, Ian, can I get some insight on the question I pose below. I have otherwise cleaned up the patch for 3.7.

Thanks
Andres

Quoted 25 lines
Quoted 1 line

.

Sure will do. However, I need to bring attention to the following hunk:

drivers/net/xen-netback/netback.c +1 -3
index 682633b..5610fd8 100644
@@ -635,9 +635,7 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 		return;
 
 	BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
-	ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, &netbk->grant_copy_op,
-					npo.copy_prod);
-	BUG_ON(ret != 0);
+	gnttab_batch_copy_no_eagain(netbk->grant_copy_op, npo.copy_prod);
 
 It seems like the (extant) code passes a struct gnttab_copy ** to the grant table hypercall (&netbk->grant_copy_op, defined as struct gnttab_copy [])
 

I "fixed" it to pass a struct gnttab_copy * (netbk->grant_copy_op, no '&'). This matches the other invocation of the grant copy hyper call (in netbk_tx_action). Did I fix it, though? I am confused as to how this could have ever worked.

(cc'ing Ian Campbell for more insight)
Andres

Quoted 2 lines
Quoted 2 lines

David

#8 lore raw
Quoted 23 lines · Andres Lagar-Cavilla
Quoted 21 lines · Andres Lagar-Cavilla
Quoted 18 lines · Konrad Rzeszutek Wilk
Quoted 17 lines · David Vrabel
Quoted 7 lines · Andres Lagar-Cavilla

Actually acted upon your feedback ipso facto:

commit d5fab912caa1f0cf6be0a6773f502d3417a207b6

Author: Andres Lagar-Cavilla <[email protected]>
Date:   Sun Aug 26 09:45:57 2012 -0400

Xen backend support for paged out grant targets.

This looks mostly fine expect for the #define instead of inline functions.

Quoted 1 line

+#define gnttab_map_grant_no_eagain(_gop) \

This name tripped me up previously. As I read this as:

gnttab_map_grant_no_[retries_for]_eagain().

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

Perhaps gnttab_map_grant_with_retries() ? Or similar?

gnttab_map_grant_retry ?

Besides that, it looks good to me. Ian Campbell needs to Ack so that David Miller (the network maintainer) can pick it up. Please CC them both and also LKML

Konrad, Ian, can I get some insight on the question I pose below. I have otherwise cleaned up the patch for 3.7.

Can you re-send a cleanup patch and CC the relevant folks pls.

Quoted 28 lines

Thanks
Andres

Quoted 25 lines
Quoted 1 line

.

Sure will do. However, I need to bring attention to the following hunk:

drivers/net/xen-netback/netback.c +1 -3
index 682633b..5610fd8 100644
@@ -635,9 +635,7 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 		return;
 
 	BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
-	ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, &netbk->grant_copy_op,
-					npo.copy_prod);
-	BUG_ON(ret != 0);
+	gnttab_batch_copy_no_eagain(netbk->grant_copy_op, npo.copy_prod);
 
 It seems like the (extant) code passes a struct gnttab_copy ** to the grant table hypercall (&netbk->grant_copy_op, defined as struct gnttab_copy [])
 

I "fixed" it to pass a struct gnttab_copy * (netbk->grant_copy_op, no '&'). This matches the other invocation of the grant copy hyper call (in netbk_tx_action). Did I fix it, though? I am confused as to how this could have ever worked.

(cc'ing Ian Campbell for more insight)
Andres

Quoted 2 lines
Quoted 2 lines

David

#9 lore raw
In reply to Andres Lagar-Cavilla
Quoted 26 lines · Andres Lagar-Cavilla

Actually acted upon your feedback ipso facto:

commit d5fab912caa1f0cf6be0a6773f502d3417a207b6

Author: Andres Lagar-Cavilla <[email protected]>
Date:   Sun Aug 26 09:45:57 2012 -0400

Xen backend support for paged out grant targets.

Since Xen-4.2, hvm domains may have portions of their memory paged out. When a foreign domain (such as dom0) attempts to map these frames, the map will initially fail. The hypervisor returns a suitable errno, and kicks an asynchronous page-in operation carried out by a helper. The foreign domain is expected to retry the mapping operation until it eventually succeeds. The foreign domain is not put to sleep because itself could be the one running the pager assist (typical scenario for dom0).

This patch adds support for this mechanism for backend drivers using grant mapping and copying operations. Specifically, this covers the blkback and gntdev drivers (which map foregin grants), and the netback driver (which copies foreign grants).

* Add GNTST_eagain, already exposed by Xen, to the grant interface.
* Add a retry method for grants that fail with GNTST_eagain (i.e. because the target foregin frame is paged out).
* Insert hooks with appropriate macro decorators in the aforementioned drivers.

Quoted 7 lines

The retry loop is only invoked if the grant operation status is GNTST_eagain. It guarantees to leave a new status code different from GNTST_eagain. Any other status code results in identical code execution as before.

The retry loop performs 256 attempts with increasing time intervals through a 32 second period. It uses msleep to yield while waiting for the next retry.

Would it make sense to yield to other processes (so call schedule)? Or perhaps have this in a workqueue ?

I mean the 'msleep' just looks like a hack. .. 32 seconds of doing 'msleep' on 1VCPU dom0 could trigger the watchdog I think?

Quoted 274 lines

V2 after feedback from David Vrabel:
* Explicit MAX_DELAY instead of wrap-around delay into zero
* Abstract GNTST_eagain check into core grant table code for netback module.

drivers/net/xen-netback/netback.c +3 -8
index 682633b..5610fd8 100644
@@ -635,9 +635,7 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 		return;
 
 	BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
-	ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, &netbk->grant_copy_op,
-					npo.copy_prod);
-	BUG_ON(ret != 0);
+	gnttab_batch_copy_no_eagain(netbk->grant_copy_op, npo.copy_prod);
 
 	while ((skb = __skb_dequeue(&rxq)) != NULL) {
 		sco = (struct skb_cb_overlay *)skb->cb;
@@ -1460,18 +1458,15 @@ static void xen_netbk_tx_submit(struct xen_netbk *netbk)
 static void xen_netbk_tx_action(struct xen_netbk *netbk)
 {
 	unsigned nr_gops;
-	int ret;
 
 	nr_gops = xen_netbk_tx_build_gops(netbk);
 
 	if (nr_gops == 0)
 		return;
-	ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
-					netbk->tx_copy_ops, nr_gops);
-	BUG_ON(ret);
 
-	xen_netbk_tx_submit(netbk);
+	gnttab_batch_copy_no_eagain(netbk->tx_copy_ops, nr_gops);
 
+	xen_netbk_tx_submit(netbk);
 }
 
 static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx)
drivers/xen/grant-table.c +26 -0
index eea81cf..96543b2 100644
@@ -38,6 +38,7 @@
 #include <linux/vmalloc.h>
 #include <linux/uaccess.h>
 #include <linux/io.h>
+#include <linux/delay.h>
 #include <linux/hardirq.h>
 
 #include <xen/xen.h>
@@ -823,6 +824,26 @@ unsigned int gnttab_max_grant_frames(void)
 }
 EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
 
+#define MAX_DELAY 256
+void
+gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
+						const char *func)
+{
+	unsigned delay = 1;
+
+	do {
+		BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
+		if (*status == GNTST_eagain)
+			msleep(delay++);
+	} while ((*status == GNTST_eagain) && (delay < MAX_DELAY));
+
+	if (delay >= MAX_DELAY) {
+		printk(KERN_ERR "%s: %s eagain grant\n", func, current->comm);
+		*status = GNTST_bad_page;
+	}
+}
+EXPORT_SYMBOL_GPL(gnttab_retry_eagain_gop);
+
 int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
 		    struct gnttab_map_grant_ref *kmap_ops,
 		    struct page **pages, unsigned int count)
@@ -836,6 +857,11 @@ int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
 	if (ret)
 		return ret;
 
+	/* Retry eagain maps */
+	for (i = 0; i < count; i++)
+		if (map_ops[i].status == GNTST_eagain)
+			gnttab_retry_eagain_map(map_ops + i);
+
 	if (xen_feature(XENFEAT_auto_translated_physmap))
 		return ret;
 
drivers/xen/xenbus/xenbus_client.c +2 -4
index b3e146e..749f6a3 100644
@@ -490,8 +490,7 @@ static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev,
 
 	op.host_addr = arbitrary_virt_to_machine(pte).maddr;
 
-	if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
-		BUG();
+	gnttab_map_grant_no_eagain(&op);
 
 	if (op.status != GNTST_okay) {
 		free_vm_area(area);
@@ -572,8 +571,7 @@ int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref,
 	gnttab_set_map_op(&op, (unsigned long)vaddr, GNTMAP_host_map, gnt_ref,
 			  dev->otherend_id);
 
-	if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
-		BUG();
+	gnttab_map_grant_no_eagain(&op);
 
 	if (op.status != GNTST_okay) {
 		xenbus_dev_fatal(dev, op.status,
include/xen/grant_table.h +38 -0
index 11e27c3..2fecfab 100644
@@ -43,6 +43,7 @@
 #include <xen/interface/grant_table.h>
 
 #include <asm/xen/hypervisor.h>
+#include <asm/xen/hypercall.h>
 
 #include <xen/features.h>
 
@@ -183,6 +184,43 @@ unsigned int gnttab_max_grant_frames(void);
 
 #define gnttab_map_vaddr(map) ((void *)(map.host_virt_addr))
 
+/* Retry a grant map/copy operation when the hypervisor returns GNTST_eagain.
+ * This is typically due to paged out target frames.
+ * Generic entry-point, use macro decorators below for specific grant
+ * operations.
+ * Will retry for 1, 2, ... 255 ms, i.e. 256 times during 32 seconds.
+ * Return value in *status guaranteed to no longer be GNTST_eagain. */
+void gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
+                             const char *func);
+
+#define gnttab_retry_eagain_map(_gop)                       \
+    gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, (_gop), \
+                            &(_gop)->status, __func__)
+
+#define gnttab_retry_eagain_copy(_gop)                  \
+    gnttab_retry_eagain_gop(GNTTABOP_copy, (_gop),      \
+                            &(_gop)->status, __func__)
+
+#define gnttab_map_grant_no_eagain(_gop)                                    \
+do {                                                                        \
+    if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, (_gop), 1))       \
+        BUG();                                                              \
+    if ((_gop)->status == GNTST_eagain)                                     \
+        gnttab_retry_eagain_map((_gop));                                    \
+} while(0)
+
+static inline void
+gnttab_batch_copy_no_eagain(struct gnttab_copy *batch, unsigned count)
+{
+    unsigned i;
+    struct gnttab_copy *op;
+
+    BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_copy, batch, count));
+    for (i = 0, op = batch; i < count; i++, op++)
+        if (op->status == GNTST_eagain)
+            gnttab_retry_eagain_copy(op);
+}
+
 int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
 		    struct gnttab_map_grant_ref *kmap_ops,
 		    struct page **pages, unsigned int count);
include/xen/interface/grant_table.h +2 -0
index 7da811b..66cb734 100644
@@ -520,6 +520,7 @@ DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version);
 #define GNTST_permission_denied (-8) /* Not enough privilege for operation.  */
 #define GNTST_bad_page         (-9) /* Specified page was invalid for op.    */
 #define GNTST_bad_copy_arg    (-10) /* copy arguments cross page boundary */
+#define GNTST_eagain          (-12) /* Retry.                                */
 
 #define GNTTABOP_error_msgs {                   \
     "okay",                                     \
@@ -533,6 +534,7 @@ DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version);
     "permission denied",                        \
     "bad page",                                 \
     "copy arguments cross page boundary"        \
+    "retry"                                     \
 }
 
 #endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */
Quoted 76 lines · Andres Lagar-Cavilla
Quoted 21 lines · David Vrabel
Quoted 15 lines · On 27/08/12 17:51, [email protected] wrote:

From: Andres Lagar-Cavilla <[email protected]>

Since Xen-4.2, hvm domains may have portions of their memory paged out. When a foreign domain (such as dom0) attempts to map these frames, the map will initially fail. The hypervisor returns a suitable errno, and kicks an asynchronous page-in operation carried out by a helper. The foreign domain is expected to retry the mapping operation until it eventually succeeds. The foreign domain is not put to sleep because itself could be the one running the pager assist (typical scenario for dom0).

This patch adds support for this mechanism for backend drivers using grant mapping and copying operations. Specifically, this covers the blkback and gntdev drivers (which map foregin grants), and the netback driver (which copies foreign grants).

Quoted 4 lines

* Add GNTST_eagain, already exposed by Xen, to the grant interface.
* Add a retry method for grants that fail with GNTST_eagain (i.e. because the target foregin frame is paged out).
* Insert hooks with appropriate macro decorators in the aforementioned drivers.

Quoted 6 lines

I think you should implement wrappers around HYPERVISOR_grant_table_op() have have the wrapper do the retries instead of every backend having to check for EAGAIN and issue the retries itself. Similar to the gnttab_map_grant_no_eagain() function you've already added.

Why do some operations not retry anyway?

All operations retry. The reason why I could not make it as elegant as you suggest is because grant operations are submitted in batches and their status(es?) later checked individually elsewhere. This is the case for netback. Note that both blkback and gntdev use a more linear structure with the gnttab_map_refs helper, which allows me to hide all the retry gore from those drivers into grant table code. Likewise for xenbus ring mapping.

In summary, outside of core grant table code, only the netback driver needs to check explicitly for retries, due to its batch-copy-delayed-per-slot-check structure.

Quoted 15 lines

drivers/xen/grant-table.c unsigned int gnttab_max_grant_frames(void)

Quoted 12 lines
drivers/xen/grant-table.c +11 -0
+void
+gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
+						const char *func)
+{
+	u8 delay = 1;
+
+	do {
+		BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
+		if (*status == GNTST_eagain)
+			msleep(delay++);
+	} while ((*status == GNTST_eagain) && delay);

Terminating the loop when delay wraps is a bit subtle. Why not make delay unsigned and check delay <= MAX_DELAY?

Good idea (MAX_DELAY == 256). I'd like to get Konrad's feedback before a re-spin.

Quoted 3 lines

Would it be sensible to ramp the delay faster? Perhaps double each iteration with a maximum possible delay of e.g., 256 ms.

Generally speaking we've never seen past three retries. I am open to changing the algorithm but there is a significant possibility it won't matter at all.

Quoted 10 lines

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void); · line 210

Quoted 8 lines
include/xen/grant_table.h +7 -0
+#define gnttab_map_grant_no_eagain(_gop)                                    \
+do {                                                                        \
+    if ( HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, (_gop), 1))      \
+        BUG();                                                              \
+    if ((_gop)->status == GNTST_eagain)                                     \
+        gnttab_retry_eagain_map((_gop));                                    \
+} while(0)

Inline functions, please.

I want to retain the original context for debugging. Eventually we print __func__ if things go wrong.

Thanks, great feedback
Andres

Quoted 2 lines

David

#10 lore raw

But msleep will wind up calling schedule(). We definitely cannot afford to pin down a dom0 vcpu when the pager itself is in dom0.

Iiuc....

Andres

Quoted 369 lines · Konrad Rzeszutek Wilk
Quoted 9 lines · Andres Lagar-Cavilla

Actually acted upon your feedback ipso facto:

commit d5fab912caa1f0cf6be0a6773f502d3417a207b6

Author: Andres Lagar-Cavilla <[email protected]>
Date:   Sun Aug 26 09:45:57 2012 -0400

Xen backend support for paged out grant targets.

Since Xen-4.2, hvm domains may have portions of their memory paged

out. When a

Quoted 1 line

foreign domain (such as dom0) attempts to map these frames, the map

will

Quoted 2 lines

initially fail. The hypervisor returns a suitable errno, and kicks an asynchronous page-in operation carried out by a helper. The foreign

domain is

Quoted 1 line

expected to retry the mapping operation until it eventually

succeeds. The

Quoted 1 line

foreign domain is not put to sleep because itself could be the one

running the

Quoted 3 lines

pager assist (typical scenario for dom0).

This patch adds support for this mechanism for backend drivers using

grant

Quoted 1 line

mapping and copying operations. Specifically, this covers the

blkback and

Quoted 1 line

gntdev drivers (which map foregin grants), and the netback driver

(which copies

Quoted 4 lines

foreign grants).

* Add GNTST_eagain, already exposed by Xen, to the grant interface.
* Add a retry method for grants that fail with GNTST_eagain (i.e.

because the

Quoted 2 lines

target foregin frame is paged out).
* Insert hooks with appropriate macro decorators in the

aforementioned drivers.

Quoted 2 lines

The retry loop is only invoked if the grant operation status is

GNTST_eagain.

Quoted 1 line

It guarantees to leave a new status code different from

GNTST_eagain. Any other

Quoted 3 lines

status code results in identical code execution as before.

The retry loop performs 256 attempts with increasing time intervals

through a

Quoted 1 line

32 second period. It uses msleep to yield while waiting for the next

retry.

Quoted 1 line

Would it make sense to yield to other processes (so call schedule)? Or perhaps have this in a workqueue ?

I mean the 'msleep' just looks like a hack. .. 32 seconds of doing 'msleep' on 1VCPU dom0 could trigger the watchdog I think?

Quoted 3 lines

V2 after feedback from David Vrabel:
* Explicit MAX_DELAY instead of wrap-around delay into zero
* Abstract GNTST_eagain check into core grant table code for netback

module.

Review

Quoted 4 lines
(quoted diff) +0 -0

b/drivers/net/xen-netback/netback.c

drivers/net/xen-netback/netback.c static void xen_netbk_rx_action(struct xen_netbk

Quoted 4 lines

index 682633b..5610fd8 100644

drivers/net/xen-netback/netback.c +0 -0
@@ -635,9 +635,7 @@ static void xen_netbk_rx_action(struct xen_netbk

*netbk)

drivers/net/xen-netback/netback.c static void xen_netbk_rx_action(struct xen_netbk *netbk)

Quoted 4 lines
drivers/net/xen-netback/netback.c +0 -1
              return;
 
      BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
-     ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,

&netbk->grant_copy_op,

drivers/net/xen-netback/netback.c static void xen_netbk_tx_submit(struct xen_netbk

Quoted 7 lines
drivers/net/xen-netback/netback.c +1 -2
-                                     npo.copy_prod);
-     BUG_ON(ret != 0);
+     gnttab_batch_copy_no_eagain(netbk->grant_copy_op, npo.copy_prod);
 
      while ((skb = __skb_dequeue(&rxq)) != NULL) {
              sco = (struct skb_cb_overlay *)skb->cb;
@@ -1460,18 +1458,15 @@ static void xen_netbk_tx_submit(struct xen_netbk

*netbk)

Quoted 10 lines
drivers/net/xen-netback/netback.c +0 -2
 static void xen_netbk_tx_action(struct xen_netbk *netbk)
 {
      unsigned nr_gops;
-     int ret;
 
      nr_gops = xen_netbk_tx_build_gops(netbk);
 
      if (nr_gops == 0)
              return;
-     ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,

drivers/net/xen-netback/netback.c static void xen_netbk_tx_submit(struct xen_netbk *netbk)

Quoted 10 lines
drivers/net/xen-netback/netback.c +2 -3
-                                     netbk->tx_copy_ops, nr_gops);
-     BUG_ON(ret);
 
-     xen_netbk_tx_submit(netbk);
+     gnttab_batch_copy_no_eagain(netbk->tx_copy_ops, nr_gops);
 
+     xen_netbk_tx_submit(netbk);
 }
 
 static void xen_netbk_idx_release(struct xen_netbk *netbk, u16

pending_idx)

Quoted 21 lines
drivers/xen/grant-table.c +6 -0
index eea81cf..96543b2 100644
@@ -38,6 +38,7 @@
 #include <linux/vmalloc.h>
 #include <linux/uaccess.h>
 #include <linux/io.h>
+#include <linux/delay.h>
 #include <linux/hardirq.h>
 
 #include <xen/xen.h>
@@ -823,6 +824,26 @@ unsigned int gnttab_max_grant_frames(void)
 }
 EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
 
+#define MAX_DELAY 256
+void
+gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
+                                             const char *func)
+{

drivers/xen/grant-table.c unsigned int gnttab_max_grant_frames(void) · line 841

Quoted 10 lines
drivers/xen/grant-table.c +10 -0
+     unsigned delay = 1;
+
+     do {
+             BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
+             if (*status == GNTST_eagain)
+                     msleep(delay++);
+     } while ((*status == GNTST_eagain) && (delay < MAX_DELAY));
+
+     if (delay >= MAX_DELAY) {
+             printk(KERN_ERR "%s: %s eagain grant\n", func,

current->comm);

drivers/xen/grant-table.c int gnttab_map_refs(struct gnttab_map_grant_ref

Quoted 9 lines
drivers/xen/grant-table.c +5 -0
+             *status = GNTST_bad_page;
+     }
+}
+EXPORT_SYMBOL_GPL(gnttab_retry_eagain_gop);
+
 int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
                  struct gnttab_map_grant_ref *kmap_ops,
                  struct page **pages, unsigned int count)
@@ -836,6 +857,11 @@ int gnttab_map_refs(struct gnttab_map_grant_ref

*map_ops,

Review

Quoted 12 lines
drivers/xen/grant-table.c +5 -0
      if (ret)
              return ret;
 
+     /* Retry eagain maps */
+     for (i = 0; i < count; i++)
+             if (map_ops[i].status == GNTST_eagain)
+                     gnttab_retry_eagain_map(map_ops + i);
+
      if (xen_feature(XENFEAT_auto_translated_physmap))
              return ret;
 
(quoted diff) +0 -0

b/drivers/xen/xenbus/xenbus_client.c

drivers/xen/xenbus/xenbus_client.c static int xenbus_map_ring_valloc_pv(struct

Quoted 4 lines

index b3e146e..749f6a3 100644

drivers/xen/xenbus/xenbus_client.c +0 -0
@@ -490,8 +490,7 @@ static int xenbus_map_ring_valloc_pv(struct

xenbus_device *dev,

drivers/xen/xenbus/xenbus_client.c int xenbus_map_ring(struct xenbus_device *dev, int

Quoted 10 lines
drivers/xen/xenbus/xenbus_client.c +1 -2
      op.host_addr = arbitrary_virt_to_machine(pte).maddr;
 
-     if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
-             BUG();
+     gnttab_map_grant_no_eagain(&op);
 
      if (op.status != GNTST_okay) {
              free_vm_area(area);
@@ -572,8 +571,7 @@ int xenbus_map_ring(struct xenbus_device *dev, int

gnt_ref,

drivers/xen/xenbus/xenbus_client.c int xenbus_map_ring(struct xenbus_device *dev, int · line 571

Quoted 1 line
drivers/xen/xenbus/xenbus_client.c +0 -0
      gnttab_set_map_op(&op, (unsigned long)vaddr, GNTMAP_host_map,

gnt_ref,

Quoted 21 lines
drivers/xen/xenbus/xenbus_client.c +1 -2
                        dev->otherend_id);
 
-     if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
-             BUG();
+     gnttab_map_grant_no_eagain(&op);
 
      if (op.status != GNTST_okay) {
              xenbus_dev_fatal(dev, op.status,
include/xen/grant_table.h +1 -0
index 11e27c3..2fecfab 100644
@@ -43,6 +43,7 @@
 #include <xen/interface/grant_table.h>
 
 #include <asm/xen/hypervisor.h>
+#include <asm/xen/hypercall.h>
 
 #include <xen/features.h>
 

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void); · line 187

Quoted 4 lines
include/xen/grant_table.h +1 -0
@@ -183,6 +184,43 @@ unsigned int gnttab_max_grant_frames(void);
 
 #define gnttab_map_vaddr(map) ((void *)(map.host_virt_addr))
 
+/* Retry a grant map/copy operation when the hypervisor returns

GNTST_eagain.

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void);

Quoted 6 lines
include/xen/grant_table.h +6 -0
+ * This is typically due to paged out target frames.
+ * Generic entry-point, use macro decorators below for specific grant
+ * operations.
+ * Will retry for 1, 2, ... 255 ms, i.e. 256 times during 32 seconds.
+ * Return value in *status guaranteed to no longer be GNTST_eagain. */
+void gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t

*status,

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void);

Quoted 11 lines
include/xen/grant_table.h +11 -0
+                             const char *func);
+
+#define gnttab_retry_eagain_map(_gop)                       \
+    gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, (_gop), \
+                            &(_gop)->status, __func__)
+
+#define gnttab_retry_eagain_copy(_gop)                  \
+    gnttab_retry_eagain_gop(GNTTABOP_copy, (_gop),      \
+                            &(_gop)->status, __func__)
+
+#define gnttab_map_grant_no_eagain(_gop)
     \

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void); · line 205

Quoted 1 line
include/xen/grant_table.h +1 -0
+do {
     \

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void); · line 206

Quoted 1 line
include/xen/grant_table.h +1 -0
+    if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, (_gop), 1))
    \

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void); · line 207

Quoted 1 line
include/xen/grant_table.h +1 -0
+        BUG();
     \

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void); · line 208

Quoted 1 line
include/xen/grant_table.h +1 -0
+    if ((_gop)->status == GNTST_eagain)
    \

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void); · line 209

Quoted 1 line
include/xen/grant_table.h +1 -0
+        gnttab_retry_eagain_map((_gop));
     \

Review

Quoted 18 lines
include/xen/grant_table.h +14 -0
+} while(0)
+
+static inline void
+gnttab_batch_copy_no_eagain(struct gnttab_copy *batch, unsigned count)
+{
+    unsigned i;
+    struct gnttab_copy *op;
+
+    BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_copy, batch, count));
+    for (i = 0, op = batch; i < count; i++, op++)
+        if (op->status == GNTST_eagain)
+            gnttab_retry_eagain_copy(op);
+}
+
 int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
                  struct gnttab_map_grant_ref *kmap_ops,
                  struct page **pages, unsigned int count);
(quoted diff) +0 -0

b/include/xen/interface/grant_table.h

include/xen/interface/grant_table.h DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version); · line 520

Quoted 5 lines

index 7da811b..66cb734 100644

include/xen/interface/grant_table.h +0 -0
@@ -520,6 +520,7 @@ DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version);
 #define GNTST_permission_denied (-8) /* Not enough privilege for

operation. */

include/xen/interface/grant_table.h DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version); · line 521

Quoted 1 line
include/xen/interface/grant_table.h +0 -0
 #define GNTST_bad_page         (-9) /* Specified page was invalid for

op. */

include/xen/interface/grant_table.h DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version); · line 522

Quoted 1 line
include/xen/interface/grant_table.h +0 -0
 #define GNTST_bad_copy_arg    (-10) /* copy arguments cross page

boundary */

include/xen/interface/grant_table.h DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version); · line 523

Quoted 1 line
include/xen/interface/grant_table.h +1 -0
+#define GNTST_eagain          (-12) /* Retry.
     */
Quoted 16 lines
include/xen/interface/grant_table.h +1 -0
 #define GNTTABOP_error_msgs {                   \
     "okay",                                     \
@@ -533,6 +534,7 @@ DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version);
     "permission denied",                        \
     "bad page",                                 \
     "copy arguments cross page boundary"        \
+    "retry"                                     \
 }
 
 #endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */
Quoted 1 line · Andres Lagar-Cavilla
Quoted 6 lines
Quoted 6 lines · David Vrabel
Quoted 5 lines · On 27/08/12 17:51, [email protected] wrote:
Quoted 3 lines

From: Andres Lagar-Cavilla <[email protected]>

Since Xen-4.2, hvm domains may have portions of their memory paged

out. When a

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

foreign domain (such as dom0) attempts to map these frames, the map

will

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

initially fail. The hypervisor returns a suitable errno, and kicks an asynchronous page-in operation carried out by a helper. The foreign

domain is

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

expected to retry the mapping operation until it eventually

succeeds. The

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

foreign domain is not put to sleep because itself could be the one

running the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

pager assist (typical scenario for dom0).

This patch adds support for this mechanism for backend drivers using

grant

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

mapping and copying operations. Specifically, this covers the

blkback and

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

gntdev drivers (which map foregin grants), and the netback driver

(which copies

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

foreign grants).

* Add GNTST_eagain, already exposed by Xen, to the grant interface.
* Add a retry method for grants that fail with GNTST_eagain (i.e.

because the

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

target foregin frame is paged out).
* Insert hooks with appropriate macro decorators in the

aforementioned drivers.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I think you should implement wrappers around

HYPERVISOR_grant_table_op()

Quoted 1 line
Quoted 1 line
Quoted 1 line

have have the wrapper do the retries instead of every backend having

to

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

check for EAGAIN and issue the retries itself. Similar to the gnttab_map_grant_no_eagain() function you've already added.

Why do some operations not retry anyway?

All operations retry. The reason why I could not make it as elegant as

you suggest is because grant operations are submitted in batches and their status(es?) later checked individually elsewhere. This is the case for netback. Note that both blkback and gntdev use a more linear structure with the gnttab_map_refs helper, which allows me to hide all the retry gore from those drivers into grant table code. Likewise for xenbus ring mapping.

Quoted 2 lines
Quoted 2 lines

In summary, outside of core grant table code, only the netback driver

needs to check explicitly for retries, due to its
batch-copy-delayed-per-slot-check structure.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +2 -0
+void
+gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t

*status,

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

drivers/xen/grant-table.c unsigned int gnttab_max_grant_frames(void)

Quoted 9 lines
drivers/xen/grant-table.c +9 -0
+                                         const char *func)
+{
+ u8 delay = 1;
+
+ do {
+         BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
+         if (*status == GNTST_eagain)
+                 msleep(delay++);
+ } while ((*status == GNTST_eagain) && delay);

Terminating the loop when delay wraps is a bit subtle. Why not make delay unsigned and check delay <= MAX_DELAY?

Quoted 1 line
Quoted 1 line

Good idea (MAX_DELAY == 256). I'd like to get Konrad's feedback before

a re-spin.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Would it be sensible to ramp the delay faster? Perhaps double each iteration with a maximum possible delay of e.g., 256 ms.

Generally speaking we've never seen past three retries. I am open to

changing the algorithm but there is a significant possibility it won't matter at all.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

+#define gnttab_map_grant_no_eagain(_gop)

         \
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+do {

         \
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ if ( HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, (_gop),

1)) \

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ BUG();

         \
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ if ((_gop)->status == GNTST_eagain)

        \
Quoted 17 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ gnttab_retry_eagain_map((_gop));

         \
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+} while(0)

Inline functions, please.

I want to retain the original context for debugging. Eventually we

print __func__ if things go wrong.

Quoted 8 lines
Quoted 7 lines

Thanks, great feedback
Andres

Quoted 2 lines

David

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#11 lore raw
Quoted 2 lines · Andres Lagar-Cavilla

But msleep will wind up calling schedule(). We definitely cannot afford to pin down a dom0 vcpu when the pager itself is in dom0.

Duh! Somehow I was thinking it just do a dumb loop. Maybe I am thinking about a different type of early XYZsleep variant.

Quoted 393 lines

Iiuc....

Andres

Quoted 386 lines · Konrad Rzeszutek Wilk
Quoted 9 lines · Andres Lagar-Cavilla

Actually acted upon your feedback ipso facto:

commit d5fab912caa1f0cf6be0a6773f502d3417a207b6

Author: Andres Lagar-Cavilla <[email protected]>
Date:   Sun Aug 26 09:45:57 2012 -0400

Xen backend support for paged out grant targets.

Since Xen-4.2, hvm domains may have portions of their memory paged

out. When a

Quoted 1 line

foreign domain (such as dom0) attempts to map these frames, the map

will

Quoted 2 lines

initially fail. The hypervisor returns a suitable errno, and kicks an asynchronous page-in operation carried out by a helper. The foreign

domain is

Quoted 1 line

expected to retry the mapping operation until it eventually

succeeds. The

Quoted 1 line

foreign domain is not put to sleep because itself could be the one

running the

Quoted 3 lines

pager assist (typical scenario for dom0).

This patch adds support for this mechanism for backend drivers using

grant

Quoted 1 line

mapping and copying operations. Specifically, this covers the

blkback and

Quoted 1 line

gntdev drivers (which map foregin grants), and the netback driver

(which copies

Quoted 4 lines

foreign grants).

* Add GNTST_eagain, already exposed by Xen, to the grant interface.
* Add a retry method for grants that fail with GNTST_eagain (i.e.

because the

Quoted 2 lines

target foregin frame is paged out).
* Insert hooks with appropriate macro decorators in the

aforementioned drivers.

Quoted 2 lines

The retry loop is only invoked if the grant operation status is

GNTST_eagain.

Quoted 1 line

It guarantees to leave a new status code different from

GNTST_eagain. Any other

Quoted 3 lines

status code results in identical code execution as before.

The retry loop performs 256 attempts with increasing time intervals

through a

Quoted 1 line

32 second period. It uses msleep to yield while waiting for the next

retry.

Quoted 1 line

Would it make sense to yield to other processes (so call schedule)? Or perhaps have this in a workqueue ?

I mean the 'msleep' just looks like a hack. .. 32 seconds of doing 'msleep' on 1VCPU dom0 could trigger the watchdog I think?

Quoted 3 lines

V2 after feedback from David Vrabel:
* Explicit MAX_DELAY instead of wrap-around delay into zero
* Abstract GNTST_eagain check into core grant table code for netback

module.

Review

Quoted 4 lines
(quoted diff) +0 -0

b/drivers/net/xen-netback/netback.c

drivers/net/xen-netback/netback.c static void xen_netbk_rx_action(struct xen_netbk

Quoted 4 lines

index 682633b..5610fd8 100644

drivers/net/xen-netback/netback.c +0 -0
@@ -635,9 +635,7 @@ static void xen_netbk_rx_action(struct xen_netbk

*netbk)

drivers/net/xen-netback/netback.c static void xen_netbk_rx_action(struct xen_netbk *netbk)

Quoted 4 lines
drivers/net/xen-netback/netback.c +0 -1
              return;
 
      BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
-     ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,

&netbk->grant_copy_op,

drivers/net/xen-netback/netback.c static void xen_netbk_tx_submit(struct xen_netbk

Quoted 7 lines
drivers/net/xen-netback/netback.c +1 -2
-                                     npo.copy_prod);
-     BUG_ON(ret != 0);
+     gnttab_batch_copy_no_eagain(netbk->grant_copy_op, npo.copy_prod);
 
      while ((skb = __skb_dequeue(&rxq)) != NULL) {
              sco = (struct skb_cb_overlay *)skb->cb;
@@ -1460,18 +1458,15 @@ static void xen_netbk_tx_submit(struct xen_netbk

*netbk)

Quoted 10 lines
drivers/net/xen-netback/netback.c +0 -2
 static void xen_netbk_tx_action(struct xen_netbk *netbk)
 {
      unsigned nr_gops;
-     int ret;
 
      nr_gops = xen_netbk_tx_build_gops(netbk);
 
      if (nr_gops == 0)
              return;
-     ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,

drivers/net/xen-netback/netback.c static void xen_netbk_tx_submit(struct xen_netbk *netbk)

Quoted 10 lines
drivers/net/xen-netback/netback.c +2 -3
-                                     netbk->tx_copy_ops, nr_gops);
-     BUG_ON(ret);
 
-     xen_netbk_tx_submit(netbk);
+     gnttab_batch_copy_no_eagain(netbk->tx_copy_ops, nr_gops);
 
+     xen_netbk_tx_submit(netbk);
 }
 
 static void xen_netbk_idx_release(struct xen_netbk *netbk, u16

pending_idx)

Quoted 21 lines
drivers/xen/grant-table.c +6 -0
index eea81cf..96543b2 100644
@@ -38,6 +38,7 @@
 #include <linux/vmalloc.h>
 #include <linux/uaccess.h>
 #include <linux/io.h>
+#include <linux/delay.h>
 #include <linux/hardirq.h>
 
 #include <xen/xen.h>
@@ -823,6 +824,26 @@ unsigned int gnttab_max_grant_frames(void)
 }
 EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
 
+#define MAX_DELAY 256
+void
+gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
+                                             const char *func)
+{

drivers/xen/grant-table.c unsigned int gnttab_max_grant_frames(void) · line 841

Quoted 10 lines
drivers/xen/grant-table.c +10 -0
+     unsigned delay = 1;
+
+     do {
+             BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
+             if (*status == GNTST_eagain)
+                     msleep(delay++);
+     } while ((*status == GNTST_eagain) && (delay < MAX_DELAY));
+
+     if (delay >= MAX_DELAY) {
+             printk(KERN_ERR "%s: %s eagain grant\n", func,

current->comm);

drivers/xen/grant-table.c int gnttab_map_refs(struct gnttab_map_grant_ref

Quoted 9 lines
drivers/xen/grant-table.c +5 -0
+             *status = GNTST_bad_page;
+     }
+}
+EXPORT_SYMBOL_GPL(gnttab_retry_eagain_gop);
+
 int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
                  struct gnttab_map_grant_ref *kmap_ops,
                  struct page **pages, unsigned int count)
@@ -836,6 +857,11 @@ int gnttab_map_refs(struct gnttab_map_grant_ref

*map_ops,

Review

Quoted 12 lines
drivers/xen/grant-table.c +5 -0
      if (ret)
              return ret;
 
+     /* Retry eagain maps */
+     for (i = 0; i < count; i++)
+             if (map_ops[i].status == GNTST_eagain)
+                     gnttab_retry_eagain_map(map_ops + i);
+
      if (xen_feature(XENFEAT_auto_translated_physmap))
              return ret;
 
(quoted diff) +0 -0

b/drivers/xen/xenbus/xenbus_client.c

drivers/xen/xenbus/xenbus_client.c static int xenbus_map_ring_valloc_pv(struct

Quoted 4 lines

index b3e146e..749f6a3 100644

drivers/xen/xenbus/xenbus_client.c +0 -0
@@ -490,8 +490,7 @@ static int xenbus_map_ring_valloc_pv(struct

xenbus_device *dev,

drivers/xen/xenbus/xenbus_client.c int xenbus_map_ring(struct xenbus_device *dev, int

Quoted 10 lines
drivers/xen/xenbus/xenbus_client.c +1 -2
      op.host_addr = arbitrary_virt_to_machine(pte).maddr;
 
-     if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
-             BUG();
+     gnttab_map_grant_no_eagain(&op);
 
      if (op.status != GNTST_okay) {
              free_vm_area(area);
@@ -572,8 +571,7 @@ int xenbus_map_ring(struct xenbus_device *dev, int

gnt_ref,

drivers/xen/xenbus/xenbus_client.c int xenbus_map_ring(struct xenbus_device *dev, int · line 571

Quoted 1 line
drivers/xen/xenbus/xenbus_client.c +0 -0
      gnttab_set_map_op(&op, (unsigned long)vaddr, GNTMAP_host_map,

gnt_ref,

Quoted 21 lines
drivers/xen/xenbus/xenbus_client.c +1 -2
                        dev->otherend_id);
 
-     if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
-             BUG();
+     gnttab_map_grant_no_eagain(&op);
 
      if (op.status != GNTST_okay) {
              xenbus_dev_fatal(dev, op.status,
include/xen/grant_table.h +1 -0
index 11e27c3..2fecfab 100644
@@ -43,6 +43,7 @@
 #include <xen/interface/grant_table.h>
 
 #include <asm/xen/hypervisor.h>
+#include <asm/xen/hypercall.h>
 
 #include <xen/features.h>
 

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void); · line 187

Quoted 4 lines
include/xen/grant_table.h +1 -0
@@ -183,6 +184,43 @@ unsigned int gnttab_max_grant_frames(void);
 
 #define gnttab_map_vaddr(map) ((void *)(map.host_virt_addr))
 
+/* Retry a grant map/copy operation when the hypervisor returns

GNTST_eagain.

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void);

Quoted 6 lines
include/xen/grant_table.h +6 -0
+ * This is typically due to paged out target frames.
+ * Generic entry-point, use macro decorators below for specific grant
+ * operations.
+ * Will retry for 1, 2, ... 255 ms, i.e. 256 times during 32 seconds.
+ * Return value in *status guaranteed to no longer be GNTST_eagain. */
+void gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t

*status,

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void);

Quoted 11 lines
include/xen/grant_table.h +11 -0
+                             const char *func);
+
+#define gnttab_retry_eagain_map(_gop)                       \
+    gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, (_gop), \
+                            &(_gop)->status, __func__)
+
+#define gnttab_retry_eagain_copy(_gop)                  \
+    gnttab_retry_eagain_gop(GNTTABOP_copy, (_gop),      \
+                            &(_gop)->status, __func__)
+
+#define gnttab_map_grant_no_eagain(_gop)
     \

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void); · line 205

Quoted 1 line
include/xen/grant_table.h +1 -0
+do {
     \

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void); · line 206

Quoted 1 line
include/xen/grant_table.h +1 -0
+    if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, (_gop), 1))
    \

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void); · line 207

Quoted 1 line
include/xen/grant_table.h +1 -0
+        BUG();
     \

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void); · line 208

Quoted 1 line
include/xen/grant_table.h +1 -0
+    if ((_gop)->status == GNTST_eagain)
    \

include/xen/grant_table.h unsigned int gnttab_max_grant_frames(void); · line 209

Quoted 1 line
include/xen/grant_table.h +1 -0
+        gnttab_retry_eagain_map((_gop));
     \

Review

Quoted 18 lines
include/xen/grant_table.h +14 -0
+} while(0)
+
+static inline void
+gnttab_batch_copy_no_eagain(struct gnttab_copy *batch, unsigned count)
+{
+    unsigned i;
+    struct gnttab_copy *op;
+
+    BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_copy, batch, count));
+    for (i = 0, op = batch; i < count; i++, op++)
+        if (op->status == GNTST_eagain)
+            gnttab_retry_eagain_copy(op);
+}
+
 int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
                  struct gnttab_map_grant_ref *kmap_ops,
                  struct page **pages, unsigned int count);
(quoted diff) +0 -0

b/include/xen/interface/grant_table.h

include/xen/interface/grant_table.h DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version); · line 520

Quoted 5 lines

index 7da811b..66cb734 100644

include/xen/interface/grant_table.h +0 -0
@@ -520,6 +520,7 @@ DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version);
 #define GNTST_permission_denied (-8) /* Not enough privilege for

operation. */

include/xen/interface/grant_table.h DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version); · line 521

Quoted 1 line
include/xen/interface/grant_table.h +0 -0
 #define GNTST_bad_page         (-9) /* Specified page was invalid for

op. */

include/xen/interface/grant_table.h DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version); · line 522

Quoted 1 line
include/xen/interface/grant_table.h +0 -0
 #define GNTST_bad_copy_arg    (-10) /* copy arguments cross page

boundary */

include/xen/interface/grant_table.h DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version); · line 523

Quoted 1 line
include/xen/interface/grant_table.h +1 -0
+#define GNTST_eagain          (-12) /* Retry.
     */
Quoted 16 lines
include/xen/interface/grant_table.h +1 -0
 #define GNTTABOP_error_msgs {                   \
     "okay",                                     \
@@ -533,6 +534,7 @@ DEFINE_GUEST_HANDLE_STRUCT(gnttab_get_version);
     "permission denied",                        \
     "bad page",                                 \
     "copy arguments cross page boundary"        \
+    "retry"                                     \
 }
 
 #endif /* __XEN_PUBLIC_GRANT_TABLE_H__ */
Quoted 1 line · Andres Lagar-Cavilla
Quoted 6 lines
Quoted 6 lines · David Vrabel
Quoted 5 lines · On 27/08/12 17:51, [email protected] wrote:
Quoted 3 lines

From: Andres Lagar-Cavilla <[email protected]>

Since Xen-4.2, hvm domains may have portions of their memory paged

out. When a

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

foreign domain (such as dom0) attempts to map these frames, the map

will

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

initially fail. The hypervisor returns a suitable errno, and kicks an asynchronous page-in operation carried out by a helper. The foreign

domain is

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

expected to retry the mapping operation until it eventually

succeeds. The

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

foreign domain is not put to sleep because itself could be the one

running the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

pager assist (typical scenario for dom0).

This patch adds support for this mechanism for backend drivers using

grant

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

mapping and copying operations. Specifically, this covers the

blkback and

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

gntdev drivers (which map foregin grants), and the netback driver

(which copies

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

foreign grants).

* Add GNTST_eagain, already exposed by Xen, to the grant interface.
* Add a retry method for grants that fail with GNTST_eagain (i.e.

because the

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

target foregin frame is paged out).
* Insert hooks with appropriate macro decorators in the

aforementioned drivers.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I think you should implement wrappers around

HYPERVISOR_grant_table_op()

Quoted 1 line
Quoted 1 line
Quoted 1 line

have have the wrapper do the retries instead of every backend having

to

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

check for EAGAIN and issue the retries itself. Similar to the gnttab_map_grant_no_eagain() function you've already added.

Why do some operations not retry anyway?

All operations retry. The reason why I could not make it as elegant as

you suggest is because grant operations are submitted in batches and their status(es?) later checked individually elsewhere. This is the case for netback. Note that both blkback and gntdev use a more linear structure with the gnttab_map_refs helper, which allows me to hide all the retry gore from those drivers into grant table code. Likewise for xenbus ring mapping.

Quoted 2 lines
Quoted 2 lines

In summary, outside of core grant table code, only the netback driver

needs to check explicitly for retries, due to its
batch-copy-delayed-per-slot-check structure.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Review

Quoted 4 lines
(quoted diff) +2 -0
+void
+gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t

*status,

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

drivers/xen/grant-table.c unsigned int gnttab_max_grant_frames(void)

Quoted 9 lines
drivers/xen/grant-table.c +9 -0
+                                         const char *func)
+{
+ u8 delay = 1;
+
+ do {
+         BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
+         if (*status == GNTST_eagain)
+                 msleep(delay++);
+ } while ((*status == GNTST_eagain) && delay);

Terminating the loop when delay wraps is a bit subtle. Why not make delay unsigned and check delay <= MAX_DELAY?

Quoted 1 line
Quoted 1 line

Good idea (MAX_DELAY == 256). I'd like to get Konrad's feedback before

a re-spin.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Would it be sensible to ramp the delay faster? Perhaps double each iteration with a maximum possible delay of e.g., 256 ms.

Generally speaking we've never seen past three retries. I am open to

changing the algorithm but there is a significant possibility it won't matter at all.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

+#define gnttab_map_grant_no_eagain(_gop)

         \
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+do {

         \
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ if ( HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, (_gop),

1)) \

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ BUG();

         \
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ if ((_gop)->status == GNTST_eagain)

        \
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ gnttab_retry_eagain_map((_gop));

         \
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+} while(0)

Inline functions, please.

I want to retain the original context for debugging. Eventually we

print __func__ if things go wrong.

Quoted 8 lines
Quoted 7 lines

Thanks, great feedback
Andres

Quoted 2 lines

David