Skip to content

Commit c571898

Browse files
andreslcghkonradwilk
authored andcommitted
xen/gndev: Xen backend support for paged out grant targets V4.
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 foreign grants), and the netback driver (which copies foreign grants). * Add a retry method for grants that fail with GNTST_eagain (i.e. because the target foreign frame is paged out). * Insert hooks with appropriate wrappers 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. V3 after feedback from Ian Campbell: * Add placeholder in array of grant table error descriptions for unrelated error code we jump over. * Eliminate single map and retry macro in favor of a generic batch flavor. * Some renaming. * Bury most implementation in grant_table.c, cleaner interface. V4 rebased on top of sync of Xen grant table interface headers. Signed-off-by: Andres Lagar-Cavilla <andres@lagarcavilla.org> Acked-by: Ian Campbell <ian.campbell@citrix.com> [v5: Fixed whitespace issues] Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
1 parent c3cb470 commit c571898

4 files changed

Lines changed: 70 additions & 12 deletions

File tree

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,7 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
635635
return;
636636

637637
BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
638-
ret = HYPERVISOR_grant_table_op(GNTTABOP_copy, &netbk->grant_copy_op,
639-
npo.copy_prod);
640-
BUG_ON(ret != 0);
638+
gnttab_batch_copy(netbk->grant_copy_op, npo.copy_prod);
641639

642640
while ((skb = __skb_dequeue(&rxq)) != NULL) {
643641
sco = (struct skb_cb_overlay *)skb->cb;
@@ -1460,18 +1458,15 @@ static void xen_netbk_tx_submit(struct xen_netbk *netbk)
14601458
static void xen_netbk_tx_action(struct xen_netbk *netbk)
14611459
{
14621460
unsigned nr_gops;
1463-
int ret;
14641461

14651462
nr_gops = xen_netbk_tx_build_gops(netbk);
14661463

14671464
if (nr_gops == 0)
14681465
return;
1469-
ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
1470-
netbk->tx_copy_ops, nr_gops);
1471-
BUG_ON(ret);
14721466

1473-
xen_netbk_tx_submit(netbk);
1467+
gnttab_batch_copy(netbk->tx_copy_ops, nr_gops);
14741468

1469+
xen_netbk_tx_submit(netbk);
14751470
}
14761471

14771472
static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx)

‎drivers/xen/grant-table.c‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <linux/vmalloc.h>
3939
#include <linux/uaccess.h>
4040
#include <linux/io.h>
41+
#include <linux/delay.h>
4142
#include <linux/hardirq.h>
4243

4344
#include <xen/xen.h>
@@ -823,6 +824,52 @@ unsigned int gnttab_max_grant_frames(void)
823824
}
824825
EXPORT_SYMBOL_GPL(gnttab_max_grant_frames);
825826

827+
/* Handling of paged out grant targets (GNTST_eagain) */
828+
#define MAX_DELAY 256
829+
static inline void
830+
gnttab_retry_eagain_gop(unsigned int cmd, void *gop, int16_t *status,
831+
const char *func)
832+
{
833+
unsigned delay = 1;
834+
835+
do {
836+
BUG_ON(HYPERVISOR_grant_table_op(cmd, gop, 1));
837+
if (*status == GNTST_eagain)
838+
msleep(delay++);
839+
} while ((*status == GNTST_eagain) && (delay < MAX_DELAY));
840+
841+
if (delay >= MAX_DELAY) {
842+
printk(KERN_ERR "%s: %s eagain grant\n", func, current->comm);
843+
*status = GNTST_bad_page;
844+
}
845+
}
846+
847+
void gnttab_batch_map(struct gnttab_map_grant_ref *batch, unsigned count)
848+
{
849+
struct gnttab_map_grant_ref *op;
850+
851+
if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, batch, count))
852+
BUG();
853+
for (op = batch; op < batch + count; op++)
854+
if (op->status == GNTST_eagain)
855+
gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, op,
856+
&op->status, __func__);
857+
}
858+
EXPORT_SYMBOL_GPL(gnttab_batch_map);
859+
860+
void gnttab_batch_copy(struct gnttab_copy *batch, unsigned count)
861+
{
862+
struct gnttab_copy *op;
863+
864+
if (HYPERVISOR_grant_table_op(GNTTABOP_copy, batch, count))
865+
BUG();
866+
for (op = batch; op < batch + count; op++)
867+
if (op->status == GNTST_eagain)
868+
gnttab_retry_eagain_gop(GNTTABOP_copy, op,
869+
&op->status, __func__);
870+
}
871+
EXPORT_SYMBOL_GPL(gnttab_batch_copy);
872+
826873
int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
827874
struct gnttab_map_grant_ref *kmap_ops,
828875
struct page **pages, unsigned int count)
@@ -836,6 +883,12 @@ int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
836883
if (ret)
837884
return ret;
838885

886+
/* Retry eagain maps */
887+
for (i = 0; i < count; i++)
888+
if (map_ops[i].status == GNTST_eagain)
889+
gnttab_retry_eagain_gop(GNTTABOP_map_grant_ref, map_ops + i,
890+
&map_ops[i].status, __func__);
891+
839892
if (xen_feature(XENFEAT_auto_translated_physmap))
840893
return ret;
841894

‎drivers/xen/xenbus/xenbus_client.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,7 @@ static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev,
490490

491491
op.host_addr = arbitrary_virt_to_machine(pte).maddr;
492492

493-
if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
494-
BUG();
493+
gnttab_batch_map(&op, 1);
495494

496495
if (op.status != GNTST_okay) {
497496
free_vm_area(area);
@@ -572,8 +571,7 @@ int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref,
572571
gnttab_set_map_op(&op, (unsigned long)vaddr, GNTMAP_host_map, gnt_ref,
573572
dev->otherend_id);
574573

575-
if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
576-
BUG();
574+
gnttab_batch_map(&op, 1);
577575

578576
if (op.status != GNTST_okay) {
579577
xenbus_dev_fatal(dev, op.status,

‎include/xen/grant_table.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,16 @@ int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
189189
int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
190190
struct page **pages, unsigned int count, bool clear_pte);
191191

192+
/* Perform a batch of grant map/copy operations. Retry every batch slot
193+
* for which the hypervisor returns GNTST_eagain. This is typically due
194+
* to paged out target frames.
195+
*
196+
* Will retry for 1, 2, ... 255 ms, i.e. 256 times during 32 seconds.
197+
*
198+
* Return value in each iand every status field of the batch guaranteed
199+
* to not be GNTST_eagain.
200+
*/
201+
void gnttab_batch_map(struct gnttab_map_grant_ref *batch, unsigned count);
202+
void gnttab_batch_copy(struct gnttab_copy *batch, unsigned count);
203+
192204
#endif /* __ASM_GNTTAB_H__ */

0 commit comments

Comments
 (0)