Skip to contentSkip to conversation

xen-netback: switch to NAPI + kthread 1:1 model

PATCH Superseded Wei Liu 6 participants 24 messages May 27, 2013 – Jul 3, 2013

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

v2 cover xen-netback: switch to NAPI + kthread 1:1 model

* This is a xen-devel only post, since we have not reached concesus on what to add / remove in this new model. This series tries to be conservative about adding in new feature compared to V1.

This series implements NAPI + kthread 1:1 model for Xen netback.

This model
- provides better scheduling fairness among vifs
- is prerequisite for implementing multiqueue for Xen network driver

The first two patches are ground work for the third patch. First one simplifies code in netback, second one can reduce memory footprint if we switch to 1:1 model.

The third patch has the real meat:
- make use of NAPI to mitigate interrupt
- kthreads are not bound to CPUs any more, so that we can take advantage of backend scheduler and trust it to do the right thing

Change since V1:
- No page pool in this version. Instead page tracking facility is removed.

Wei Liu (3):
xen-netback: remove page tracking facility
xen-netback: switch to per-cpu scratch space
xen-netback: switch to NAPI + kthread 1:1 model

 drivers/net/xen-netback/common.h    |   92 ++--
 drivers/net/xen-netback/interface.c |  122 +++--
 drivers/net/xen-netback/netback.c   |  959 +++++++++++++++--------------------
 3 files changed, 537 insertions(+), 636 deletions(-)
Signature · 1 line
1.7.10.4
author
#1 lore raw

PATCH v2 1/3 xen-netback: remove page tracking facility

The data flow from DomU to DomU on the same host:

With tracking facility:

copy
DomU --------> Dom0 DomU

 |                            ^
 |____________________________|

copy

In other words, we can always copy page from Dom0, thus removing the need for a tracking facility.

copy copy
DomU --------> Dom0 -------> DomU

Simple iperf test shows no performance regression (obviously we do two copy's anyway):

W/ tracking: ~5.3Gb/s
W/o tracking: ~5.4Gb/s

---
 drivers/net/xen-netback/netback.c |   77 +------------------------------------
 1 file changed, 2 insertions(+), 75 deletions(-)
drivers/net/xen-netback/netback.c +2 -75
index 82576ff..54853be 100644
@@ -95,21 +95,6 @@ struct netbk_rx_meta {
 
 #define MAX_BUFFER_OFFSET PAGE_SIZE
 
-/* extra field used in struct page */
-union page_ext {
-	struct {
-#if BITS_PER_LONG < 64
-#define IDX_WIDTH   8
-#define GROUP_WIDTH (BITS_PER_LONG - IDX_WIDTH)
-		unsigned int group:GROUP_WIDTH;
-		unsigned int idx:IDX_WIDTH;
-#else
-		unsigned int group, idx;
-#endif
-	} e;
-	void *mapping;
-};
-
 struct xen_netbk {
 	wait_queue_head_t wq;
 	struct task_struct *task;
@@ -214,45 +199,6 @@ static inline unsigned long idx_to_kaddr(struct xen_netbk *netbk,
 	return (unsigned long)pfn_to_kaddr(idx_to_pfn(netbk, idx));
 }
 
-/* extra field used in struct page */
-static inline void set_page_ext(struct page *pg, struct xen_netbk *netbk,
-				unsigned int idx)
-{
-	unsigned int group = netbk - xen_netbk;
-	union page_ext ext = { .e = { .group = group + 1, .idx = idx } };
-
-	BUILD_BUG_ON(sizeof(ext) > sizeof(ext.mapping));
-	pg->mapping = ext.mapping;
-}
-
-static int get_page_ext(struct page *pg,
-			unsigned int *pgroup, unsigned int *pidx)
-{
-	union page_ext ext = { .mapping = pg->mapping };
-	struct xen_netbk *netbk;
-	unsigned int group, idx;
-
-	group = ext.e.group - 1;
-
-	if (group < 0 || group >= xen_netbk_group_nr)
-		return 0;
-
-	netbk = &xen_netbk[group];
-
-	idx = ext.e.idx;
-
-	if ((idx < 0) || (idx >= MAX_PENDING_REQS))
-		return 0;
-
-	if (netbk->mmap_pages[idx] != pg)
-		return 0;
-
-	*pgroup = group;
-	*pidx = idx;
-
-	return 1;
-}
-
 /*
  * This is the amount of packet we copy rather than map, so that the
  * guest can't fiddle with the contents of the headers while we do
@@ -453,12 +399,6 @@ static void netbk_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
 {
 	struct gnttab_copy *copy_gop;
 	struct netbk_rx_meta *meta;
-	/*
-	 * These variables are used iff get_page_ext returns true,
-	 * in which case they are guaranteed to be initialized.
-	 */
-	unsigned int uninitialized_var(group), uninitialized_var(idx);
-	int foreign = get_page_ext(page, &group, &idx);
 	unsigned long bytes;
 
 	/* Data must not cross a page boundary. */
@@ -494,20 +434,9 @@ static void netbk_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
 
 		copy_gop = npo->copy + npo->copy_prod++;
 		copy_gop->flags = GNTCOPY_dest_gref;
-		if (foreign) {
-			struct xen_netbk *netbk = &xen_netbk[group];
-			struct pending_tx_info *src_pend;
+		copy_gop->source.domid = DOMID_SELF;
+		copy_gop->source.u.gmfn = virt_to_mfn(page_address(page));
 
-			src_pend = &netbk->pending_tx_info[idx];
-
-			copy_gop->source.domid = src_pend->vif->domid;
-			copy_gop->source.u.ref = src_pend->req.gref;
-			copy_gop->flags |= GNTCOPY_source_gref;
-		} else {
-			void *vaddr = page_address(page);
-			copy_gop->source.domid = DOMID_SELF;
-			copy_gop->source.u.gmfn = virt_to_mfn(vaddr);
-		}
 		copy_gop->source.offset = offset;
 		copy_gop->dest.domid = vif->domid;
 
@@ -1045,7 +974,6 @@ static struct page *xen_netbk_alloc_page(struct xen_netbk *netbk,
 	page = alloc_page(GFP_KERNEL|__GFP_COLD);
 	if (!page)
 		return NULL;
-	set_page_ext(page, netbk, pending_idx);
 	netbk->mmap_pages[pending_idx] = page;
 	return page;
 }
@@ -1153,7 +1081,6 @@ static struct gnttab_copy *xen_netbk_get_requests(struct xen_netbk *netbk,
 		first->req.offset = 0;
 		first->req.size = dst_offset;
 		first->head = start_idx;
-		set_page_ext(page, netbk, head_idx);
 		netbk->mmap_pages[head_idx] = page;
 		frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
 	}
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH 1/3] xen-netback: remove page tracking facility

Quoted 19 lines · Wei Liu

The data flow from DomU to DomU on the same host:

With tracking facility:

copy
DomU --------> Dom0 DomU

 |                            ^
 |____________________________|

copy

In other words, we can always copy page from Dom0, thus removing the need for a tracking facility.

copy copy
DomU --------> Dom0 -------> DomU

Simple iperf test shows no performance regression (obviously we do two copy's anyway):

Quoted 7 lines

W/ tracking: ~5.3Gb/s
W/o tracking: ~5.4Gb/s

---
 drivers/net/xen-netback/netback.c |   77 +------------------------------------
 1 file changed, 2 insertions(+), 75 deletions(-)

Nice! This is the sort of patch I like.

David

#3 lore raw

Re: [PATCH 1/3] xen-netback: remove page tracking facility

Quoted 1 line · David Vrabel

On 27/05/13 12:29, Wei Liu wrote:

[...]

Quoted 6 lines
Quoted 6 lines

Simple iperf test shows no performance regression (obviously we do two copy's anyway):

W/ tracking: ~5.3Gb/s
W/o tracking: ~5.4Gb/s

Quoted 6 lines
Quoted 4 lines
---
 drivers/net/xen-netback/netback.c |   77 +------------------------------------
 1 file changed, 2 insertions(+), 75 deletions(-)

Nice! This is the sort of patch I like.

Me too. :-) Was there any change in CPU utilization?

--msw

author
#4 lore raw

Re: [PATCH 1/3] xen-netback: remove page tracking facility

Quoted 11 lines · Matt Wilson
Quoted 1 line · David Vrabel

On 27/05/13 12:29, Wei Liu wrote:

[...]

Quoted 6 lines
Quoted 6 lines

Simple iperf test shows no performance regression (obviously we do two copy's anyway):

W/ tracking: ~5.3Gb/s
W/o tracking: ~5.4Gb/s

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
---
 drivers/net/xen-netback/netback.c |   77 +------------------------------------
 1 file changed, 2 insertions(+), 75 deletions(-)

Nice! This is the sort of patch I like.

Me too. :-) Was there any change in CPU utilization?

Didn't measure. But there should not be any change with this patch AFAICT.

Wei.

Quoted 1 line

--msw

author
#5 lore raw

PATCH v2 2/3 xen-netback: switch to per-cpu scratch space

In reply to Wei Liu

There are maximum nr_onlie_cpus netback threads running. We can make use of per-cpu scratch space to reduce the size of buffer space when we move to 1:1 model.

In the unlikely event when per-cpu scratch space is not available, processing routines will refuse to run on that CPU.

---
 drivers/net/xen-netback/netback.c |  247 ++++++++++++++++++++++++++++++-------
 1 file changed, 204 insertions(+), 43 deletions(-)
drivers/net/xen-netback/netback.c +204 -43
index 54853be..0f69eda 100644
@@ -37,6 +37,7 @@
 #include <linux/kthread.h>
 #include <linux/if_vlan.h>
 #include <linux/udp.h>
+#include <linux/cpu.h>
 
 #include <net/tcp.h>
 
@@ -95,6 +96,24 @@ struct netbk_rx_meta {
 
 #define MAX_BUFFER_OFFSET PAGE_SIZE
 
+/* Coalescing tx requests before copying makes number of grant
+ * copy ops greater or equal to number of slots required. In
+ * worst case a tx request consumes 2 gnttab_copy. So the size
+ * of tx_copy_ops array should be 2*MAX_PENDING_REQS.
+ */
+#define TX_COPY_OPS_SIZE (2*MAX_PENDING_REQS)
+DEFINE_PER_CPU(struct gnttab_copy *, tx_copy_ops);
+
+/* Given MAX_BUFFER_OFFSET of 4096 the worst case is that each
+ * head/fragment page uses 2 copy operations because it
+ * straddles two buffers in the frontend. So the size of following
+ * arrays should be 2*XEN_NETIF_RX_RING_SIZE.
+ */
+#define GRANT_COPY_OP_SIZE (2*XEN_NETIF_RX_RING_SIZE)
+#define META_SIZE (2*XEN_NETIF_RX_RING_SIZE)
+DEFINE_PER_CPU(struct gnttab_copy *, grant_copy_op);
+DEFINE_PER_CPU(struct netbk_rx_meta *, meta);
+
 struct xen_netbk {
 	wait_queue_head_t wq;
 	struct task_struct *task;
@@ -116,21 +135,7 @@ struct xen_netbk {
 	atomic_t netfront_count;
 
 	struct pending_tx_info pending_tx_info[MAX_PENDING_REQS];
-	/* Coalescing tx requests before copying makes number of grant
-	 * copy ops greater or equal to number of slots required. In
-	 * worst case a tx request consumes 2 gnttab_copy.
-	 */
-	struct gnttab_copy tx_copy_ops[2*MAX_PENDING_REQS];
-
 	u16 pending_ring[MAX_PENDING_REQS];
-
-	/*
-	 * Given MAX_BUFFER_OFFSET of 4096 the worst case is that each
-	 * head/fragment page uses 2 copy operations because it
-	 * straddles two buffers in the frontend.
-	 */
-	struct gnttab_copy grant_copy_op[2*XEN_NETIF_RX_RING_SIZE];
-	struct netbk_rx_meta meta[2*XEN_NETIF_RX_RING_SIZE];
 };
 
 static struct xen_netbk *xen_netbk;
@@ -608,12 +613,31 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 	int count;
 	unsigned long offset;
 	struct skb_cb_overlay *sco;
+	struct gnttab_copy *gco = get_cpu_var(grant_copy_op);
+	struct netbk_rx_meta *m = get_cpu_var(meta);
+	static int unusable_count;
 
 	struct netrx_pending_operations npo = {
-		.copy  = netbk->grant_copy_op,
-		.meta  = netbk->meta,
+		.copy = gco,
+		.meta = m,
 	};
 
+	if (gco == NULL || m == NULL) {
+		put_cpu_var(grant_copy_op);
+		put_cpu_var(meta);
+		if (unusable_count == 1000) {
+			printk(KERN_ALERT
+			       "xen-netback: "
+			       "CPU %d scratch space is not available,"
+			       " not doing any TX work for netback/%d\n",
+			       smp_processor_id(),
+			       (int)(netbk - xen_netbk));
+			unusable_count = 0;
+		} else
+			unusable_count++;
+		return;
+	}
+
 	skb_queue_head_init(&rxq);
 
 	count = 0;
@@ -635,27 +659,30 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 			break;
 	}
 
-	BUG_ON(npo.meta_prod > ARRAY_SIZE(netbk->meta));
+	BUG_ON(npo.meta_prod > META_SIZE);
 
-	if (!npo.copy_prod)
+	if (!npo.copy_prod) {
+		put_cpu_var(grant_copy_op);
+		put_cpu_var(meta);
 		return;
+	}
 
-	BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
-	gnttab_batch_copy(netbk->grant_copy_op, npo.copy_prod);
+	BUG_ON(npo.copy_prod > GRANT_COPY_OP_SIZE);
+	gnttab_batch_copy(gco, npo.copy_prod);
 
 	while ((skb = __skb_dequeue(&rxq)) != NULL) {
 		sco = (struct skb_cb_overlay *)skb->cb;
 
 		vif = netdev_priv(skb->dev);
 
-		if (netbk->meta[npo.meta_cons].gso_size && vif->gso_prefix) {
+		if (m[npo.meta_cons].gso_size && vif->gso_prefix) {
 			resp = RING_GET_RESPONSE(&vif->rx,
 						vif->rx.rsp_prod_pvt++);
 
 			resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
 
-			resp->offset = netbk->meta[npo.meta_cons].gso_size;
-			resp->id = netbk->meta[npo.meta_cons].id;
+			resp->offset = m[npo.meta_cons].gso_size;
+			resp->id = m[npo.meta_cons].id;
 			resp->status = sco->meta_slots_used;
 
 			npo.meta_cons++;
@@ -680,12 +707,12 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 			flags |= XEN_NETRXF_data_validated;
 
 		offset = 0;
-		resp = make_rx_response(vif, netbk->meta[npo.meta_cons].id,
+		resp = make_rx_response(vif, m[npo.meta_cons].id,
 					status, offset,
-					netbk->meta[npo.meta_cons].size,
+					m[npo.meta_cons].size,
 					flags);
 
-		if (netbk->meta[npo.meta_cons].gso_size && !vif->gso_prefix) {
+		if (m[npo.meta_cons].gso_size && !vif->gso_prefix) {
 			struct xen_netif_extra_info *gso =
 				(struct xen_netif_extra_info *)
 				RING_GET_RESPONSE(&vif->rx,
@@ -693,7 +720,7 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 
 			resp->flags |= XEN_NETRXF_extra_info;
 
-			gso->u.gso.size = netbk->meta[npo.meta_cons].gso_size;
+			gso->u.gso.size = m[npo.meta_cons].gso_size;
 			gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
 			gso->u.gso.pad = 0;
 			gso->u.gso.features = 0;
@@ -703,7 +730,7 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 		}
 
 		netbk_add_frag_responses(vif, status,
-					 netbk->meta + npo.meta_cons + 1,
+					 m + npo.meta_cons + 1,
 					 sco->meta_slots_used);
 
 		RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
@@ -726,6 +753,9 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 	if (!skb_queue_empty(&netbk->rx_queue) &&
 			!timer_pending(&netbk->net_timer))
 		xen_netbk_kick_thread(netbk);
+
+	put_cpu_var(grant_copy_op);
+	put_cpu_var(meta);
 }
 
 void xen_netbk_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb)
@@ -1351,9 +1381,10 @@ static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
 	return false;
 }
 
-static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
+static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk,
+					struct gnttab_copy *tco)
 {
-	struct gnttab_copy *gop = netbk->tx_copy_ops, *request_gop;
+	struct gnttab_copy *gop = tco, *request_gop;
 	struct sk_buff *skb;
 	int ret;
 
@@ -1531,16 +1562,17 @@ static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
 		vif->tx.req_cons = idx;
 		xen_netbk_check_rx_xenvif(vif);
 
-		if ((gop-netbk->tx_copy_ops) >= ARRAY_SIZE(netbk->tx_copy_ops))
+		if ((gop-tco) >= TX_COPY_OPS_SIZE)
 			break;
 	}
 
-	return gop - netbk->tx_copy_ops;
+	return gop - tco;
 }
 
-static void xen_netbk_tx_submit(struct xen_netbk *netbk)
+static void xen_netbk_tx_submit(struct xen_netbk *netbk,
+				struct gnttab_copy *tco)
 {
-	struct gnttab_copy *gop = netbk->tx_copy_ops;
+	struct gnttab_copy *gop = tco;
 	struct sk_buff *skb;
 
 	while ((skb = __skb_dequeue(&netbk->tx_queue)) != NULL) {
@@ -1615,15 +1647,37 @@ static void xen_netbk_tx_submit(struct xen_netbk *netbk)
 static void xen_netbk_tx_action(struct xen_netbk *netbk)
 {
 	unsigned nr_gops;
+	struct gnttab_copy *tco;
+	static int unusable_count;
+
+	tco = get_cpu_var(tx_copy_ops);
+
+	if (tco == NULL) {
+		put_cpu_var(tx_copy_ops);
+		if (unusable_count == 1000) {
+			printk(KERN_ALERT
+			       "xen-netback: "
+			       "CPU %d scratch space is not available,"
+			       " not doing any RX work for netback/%d\n",
+			       smp_processor_id(),
+			       (int)(netbk - xen_netbk));
+		} else
+			unusable_count++;
+		return;
+	}
 
-	nr_gops = xen_netbk_tx_build_gops(netbk);
+	nr_gops = xen_netbk_tx_build_gops(netbk, tco);
 
-	if (nr_gops == 0)
+	if (nr_gops == 0) {
+		put_cpu_var(tx_copy_ops);
 		return;
+	}
+
+	gnttab_batch_copy(tco, nr_gops);
 
-	gnttab_batch_copy(netbk->tx_copy_ops, nr_gops);
+	xen_netbk_tx_submit(netbk, tco);
 
-	xen_netbk_tx_submit(netbk);
+	put_cpu_var(tx_copy_ops);
 }
 
 static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx,
@@ -1760,6 +1814,93 @@ static int xen_netbk_kthread(void *data)
 	return 0;
 }
 
+static int __create_percpu_scratch_space(unsigned int cpu)
+{
+	if (per_cpu(tx_copy_ops, cpu) ||
+	    per_cpu(grant_copy_op, cpu) ||
+	    per_cpu(meta, cpu))
+		return 0;
+
+	per_cpu(tx_copy_ops, cpu) =
+		vzalloc_node(sizeof(struct gnttab_copy) * TX_COPY_OPS_SIZE,
+			     cpu_to_node(cpu));
+
+	per_cpu(grant_copy_op, cpu) =
+		vzalloc_node(sizeof(struct gnttab_copy) * GRANT_COPY_OP_SIZE,
+			     cpu_to_node(cpu));
+
+	per_cpu(meta, cpu) =
+		vzalloc_node(sizeof(struct netbk_rx_meta) * META_SIZE,
+			     cpu_to_node(cpu));
+
+	if (!per_cpu(tx_copy_ops, cpu) ||
+	    !per_cpu(grant_copy_op, cpu) ||
+	    !per_cpu(meta, cpu))
+		return -ENOMEM;
+
+	return 0;
+}
+
+static void __free_percpu_scratch_space(unsigned int cpu)
+{
+	void *tmp;
+
+	tmp = per_cpu(tx_copy_ops, cpu);
+	per_cpu(tx_copy_ops, cpu) = NULL;
+	vfree(tmp);
+
+	tmp = per_cpu(grant_copy_op, cpu);
+	per_cpu(grant_copy_op, cpu) = NULL;
+	vfree(tmp);
+
+	tmp = per_cpu(meta, cpu);
+	per_cpu(meta, cpu) = NULL;
+	vfree(tmp);
+}
+
+static int __netback_percpu_callback(struct notifier_block *nfb,
+				     unsigned long action, void *hcpu)
+{
+	unsigned int cpu = (unsigned long)hcpu;
+	int rc = NOTIFY_DONE;
+
+	switch (action) {
+	case CPU_ONLINE:
+	case CPU_ONLINE_FROZEN:
+		printk(KERN_INFO "xen-netback: CPU %d online, creating scratch space\n",
+		       cpu);
+		rc = __create_percpu_scratch_space(cpu);
+		if (rc) {
+			printk(KERN_ALERT "xen-netback: failed to create scratch space for CPU %d\n",
+			       cpu);
+			/* There is really nothing more we can do. Free any
+			 * partially allocated scratch space. When processing
+			 * routines get to run they will just print warning
+			 * message and stop processing.
+			 */
+			__free_percpu_scratch_space(cpu);
+			rc = NOTIFY_BAD;
+		} else
+			rc = NOTIFY_OK;
+		break;
+	case CPU_DEAD:
+	case CPU_DEAD_FROZEN:
+		printk(KERN_INFO "xen-netback: CPU %d offline, destroying scratch space\n",
+		       cpu);
+		__free_percpu_scratch_space(cpu);
+		rc = NOTIFY_OK;
+		break;
+	default:
+		break;
+	}
+
+	return rc;
+}
+
+static struct notifier_block netback_notifier_block = {
+	.notifier_call = __netback_percpu_callback,
+};
+
 void xen_netbk_unmap_frontend_rings(struct xenvif *vif)
 {
 	if (vif->tx.sring)
@@ -1810,6 +1951,7 @@ static int __init netback_init(void)
 	int i;
 	int rc = 0;
 	int group;
+	int cpu;
 
 	if (!xen_domain())
 		return -ENODEV;
@@ -1821,10 +1963,21 @@ static int __init netback_init(void)
 		fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
 	}
 
+	for_each_online_cpu(cpu) {
+		rc = __create_percpu_scratch_space(cpu);
+		if (rc) {
+			rc = -ENOMEM;
+			goto failed_init;
+		}
+	}
+	register_hotcpu_notifier(&netback_notifier_block);
+
 	xen_netbk_group_nr = num_online_cpus();
 	xen_netbk = vzalloc(sizeof(struct xen_netbk) * xen_netbk_group_nr);
-	if (!xen_netbk)
-		return -ENOMEM;
+	if (!xen_netbk) {
+		goto failed_init;
+		rc = -ENOMEM;
+	}
 
 	for (group = 0; group < xen_netbk_group_nr; group++) {
 		struct xen_netbk *netbk = &xen_netbk[group];
@@ -1849,7 +2002,7 @@ static int __init netback_init(void)
 			printk(KERN_ALERT "kthread_create() fails at netback\n");
 			del_timer(&netbk->net_timer);
 			rc = PTR_ERR(netbk->task);
-			goto failed_init;
+			goto failed_init_destroy_kthreads;
 		}
 
 		kthread_bind(netbk->task, group);
@@ -1865,17 +2018,20 @@ static int __init netback_init(void)
 
 	rc = xenvif_xenbus_init();
 	if (rc)
-		goto failed_init;
+		goto failed_init_destroy_kthreads;
 
 	return 0;
 
-failed_init:
+failed_init_destroy_kthreads:
 	while (--group >= 0) {
 		struct xen_netbk *netbk = &xen_netbk[group];
 		del_timer(&netbk->net_timer);
 		kthread_stop(netbk->task);
 	}
 	vfree(xen_netbk);
+failed_init:
+	for_each_online_cpu(cpu)
+		__free_percpu_scratch_space(cpu);
 	return rc;
 
 }
@@ -1899,6 +2055,11 @@ static void __exit netback_fini(void)
 	}
 
 	vfree(xen_netbk);
+
+	unregister_hotcpu_notifier(&netback_notifier_block);
+
+	for_each_online_cpu(i)
+		__free_percpu_scratch_space(i);
 }
 module_exit(netback_fini);
 
Signature · 1 line
1.7.10.4
#6 lore raw

Re: [PATCH 2/3] xen-netback: switch to per-cpu scratch space

Quoted 1 line · Wei Liu

There are maximum nr_onlie_cpus netback threads running.

nr_onlie_cpus --> nr_online_cpus

Quoted 72 lines

We can make use
of per-cpu scratch space to reduce the size of buffer space when we move to 1:1 model.

In the unlikely event when per-cpu scratch space is not available, processing routines will refuse to run on that CPU.

---
  drivers/net/xen-netback/netback.c |  247 ++++++++++++++++++++++++++++++-------
  1 file changed, 204 insertions(+), 43 deletions(-)
drivers/net/xen-netback/netback.c +19 -14
index 54853be..0f69eda 100644
@@ -37,6 +37,7 @@
  #include <linux/kthread.h>
  #include <linux/if_vlan.h>
  #include <linux/udp.h>
+#include <linux/cpu.h>
 
  #include <net/tcp.h>
 
@@ -95,6 +96,24 @@ struct netbk_rx_meta {
 
  #define MAX_BUFFER_OFFSET PAGE_SIZE
 
+/* Coalescing tx requests before copying makes number of grant
+ * copy ops greater or equal to number of slots required. In
+ * worst case a tx request consumes 2 gnttab_copy. So the size
+ * of tx_copy_ops array should be 2*MAX_PENDING_REQS.
+ */
+#define TX_COPY_OPS_SIZE (2*MAX_PENDING_REQS)
+DEFINE_PER_CPU(struct gnttab_copy *, tx_copy_ops);
+
+/* Given MAX_BUFFER_OFFSET of 4096 the worst case is that each
+ * head/fragment page uses 2 copy operations because it
+ * straddles two buffers in the frontend. So the size of following
+ * arrays should be 2*XEN_NETIF_RX_RING_SIZE.
+ */
+#define GRANT_COPY_OP_SIZE (2*XEN_NETIF_RX_RING_SIZE)
+#define META_SIZE (2*XEN_NETIF_RX_RING_SIZE)
+DEFINE_PER_CPU(struct gnttab_copy *, grant_copy_op);
+DEFINE_PER_CPU(struct netbk_rx_meta *, meta);
+
  struct xen_netbk {
  	wait_queue_head_t wq;
  	struct task_struct *task;
@@ -116,21 +135,7 @@ struct xen_netbk {
  	atomic_t netfront_count;
 
  	struct pending_tx_info pending_tx_info[MAX_PENDING_REQS];
-	/* Coalescing tx requests before copying makes number of grant
-	 * copy ops greater or equal to number of slots required. In
-	 * worst case a tx request consumes 2 gnttab_copy.
-	 */
-	struct gnttab_copy tx_copy_ops[2*MAX_PENDING_REQS];
-
  	u16 pending_ring[MAX_PENDING_REQS];
-
-	/*
-	 * Given MAX_BUFFER_OFFSET of 4096 the worst case is that each
-	 * head/fragment page uses 2 copy operations because it
-	 * straddles two buffers in the frontend.
-	 */
-	struct gnttab_copy grant_copy_op[2*XEN_NETIF_RX_RING_SIZE];
-	struct netbk_rx_meta meta[2*XEN_NETIF_RX_RING_SIZE];
  };
 
  static struct xen_netbk *xen_netbk;

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

Quoted 5 lines
drivers/net/xen-netback/netback.c +2 -0
@@ -608,12 +613,31 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
  	int count;
  	unsigned long offset;
  	struct skb_cb_overlay *sco;
+	struct gnttab_copy *gco = get_cpu_var(grant_copy_op);
+	struct netbk_rx_meta *m = get_cpu_var(meta);

Change m to a friendly name?

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

Quoted 13 lines
drivers/net/xen-netback/netback.c +7 -2
+	static int unusable_count;
 
  	struct netrx_pending_operations npo = {
-		.copy  = netbk->grant_copy_op,
-		.meta  = netbk->meta,
+		.copy = gco,
+		.meta = m,
  	};
 
+	if (gco == NULL || m == NULL) {
+		put_cpu_var(grant_copy_op);
+		put_cpu_var(meta);
+		if (unusable_count == 1000) {

It is better to use a macro to replace this number here. BTW, can you explain why using 1000 here?

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

Quoted 6 lines
drivers/net/xen-netback/netback.c +6 -0
+			printk(KERN_ALERT
+			       "xen-netback: "
+			       "CPU %d scratch space is not available,"
+			       " not doing any TX work for netback/%d\n",
+			       smp_processor_id(),
+			       (int)(netbk - xen_netbk));

unusable_count is not a value based on netbk here. I assume you use unusable_count to judge whether scratch space is available for specific netbk, if so, then unusable_count needs to be counter for specific netbk, not for all netbk.

Quoted 128 lines
drivers/net/xen-netback/netback.c +32 -18
+			unusable_count = 0;
+		} else
+			unusable_count++;
+		return;
+	}
+
  	skb_queue_head_init(&rxq);
 
  	count = 0;
@@ -635,27 +659,30 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
  			break;
  	}
 
-	BUG_ON(npo.meta_prod > ARRAY_SIZE(netbk->meta));
+	BUG_ON(npo.meta_prod > META_SIZE);
 
-	if (!npo.copy_prod)
+	if (!npo.copy_prod) {
+		put_cpu_var(grant_copy_op);
+		put_cpu_var(meta);
  		return;
+	}
 
-	BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
-	gnttab_batch_copy(netbk->grant_copy_op, npo.copy_prod);
+	BUG_ON(npo.copy_prod > GRANT_COPY_OP_SIZE);
+	gnttab_batch_copy(gco, npo.copy_prod);
 
  	while ((skb = __skb_dequeue(&rxq)) != NULL) {
  		sco = (struct skb_cb_overlay *)skb->cb;
 
  		vif = netdev_priv(skb->dev);
 
-		if (netbk->meta[npo.meta_cons].gso_size && vif->gso_prefix) {
+		if (m[npo.meta_cons].gso_size && vif->gso_prefix) {
  			resp = RING_GET_RESPONSE(&vif->rx,
  						vif->rx.rsp_prod_pvt++);
 
  			resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
 
-			resp->offset = netbk->meta[npo.meta_cons].gso_size;
-			resp->id = netbk->meta[npo.meta_cons].id;
+			resp->offset = m[npo.meta_cons].gso_size;
+			resp->id = m[npo.meta_cons].id;
  			resp->status = sco->meta_slots_used;
 
  			npo.meta_cons++;
@@ -680,12 +707,12 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
  			flags |= XEN_NETRXF_data_validated;
 
  		offset = 0;
-		resp = make_rx_response(vif, netbk->meta[npo.meta_cons].id,
+		resp = make_rx_response(vif, m[npo.meta_cons].id,
  					status, offset,
-					netbk->meta[npo.meta_cons].size,
+					m[npo.meta_cons].size,
  					flags);
 
-		if (netbk->meta[npo.meta_cons].gso_size && !vif->gso_prefix) {
+		if (m[npo.meta_cons].gso_size && !vif->gso_prefix) {
  			struct xen_netif_extra_info *gso =
  				(struct xen_netif_extra_info *)
  				RING_GET_RESPONSE(&vif->rx,
@@ -693,7 +720,7 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 
  			resp->flags |= XEN_NETRXF_extra_info;
 
-			gso->u.gso.size = netbk->meta[npo.meta_cons].gso_size;
+			gso->u.gso.size = m[npo.meta_cons].gso_size;
  			gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
  			gso->u.gso.pad = 0;
  			gso->u.gso.features = 0;
@@ -703,7 +730,7 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
  		}
 
  		netbk_add_frag_responses(vif, status,
-					 netbk->meta + npo.meta_cons + 1,
+					 m + npo.meta_cons + 1,
  					 sco->meta_slots_used);
 
  		RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
@@ -726,6 +753,9 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
  	if (!skb_queue_empty(&netbk->rx_queue) &&
  			!timer_pending(&netbk->net_timer))
  		xen_netbk_kick_thread(netbk);
+
+	put_cpu_var(grant_copy_op);
+	put_cpu_var(meta);
  }
 
  void xen_netbk_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb)
@@ -1351,9 +1381,10 @@ static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
  	return false;
  }
 
-static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
+static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk,
+					struct gnttab_copy *tco)
  {
-	struct gnttab_copy *gop = netbk->tx_copy_ops, *request_gop;
+	struct gnttab_copy *gop = tco, *request_gop;
  	struct sk_buff *skb;
  	int ret;
 
@@ -1531,16 +1562,17 @@ static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
  		vif->tx.req_cons = idx;
  		xen_netbk_check_rx_xenvif(vif);
 
-		if ((gop-netbk->tx_copy_ops) >= ARRAY_SIZE(netbk->tx_copy_ops))
+		if ((gop-tco) >= TX_COPY_OPS_SIZE)
  			break;
  	}
 
-	return gop - netbk->tx_copy_ops;
+	return gop - tco;
  }
 
-static void xen_netbk_tx_submit(struct xen_netbk *netbk)
+static void xen_netbk_tx_submit(struct xen_netbk *netbk,
+				struct gnttab_copy *tco)
  {
-	struct gnttab_copy *gop = netbk->tx_copy_ops;
+	struct gnttab_copy *gop = tco;
  	struct sk_buff *skb;
 
  	while ((skb = __skb_dequeue(&netbk->tx_queue)) != NULL) {
@@ -1615,15 +1647,37 @@ static void xen_netbk_tx_submit(struct xen_netbk *netbk)
  static void xen_netbk_tx_action(struct xen_netbk *netbk)

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

Quoted 10 lines
drivers/net/xen-netback/netback.c +8 -0
  {
  	unsigned nr_gops;
+	struct gnttab_copy *tco;
+	static int unusable_count;
+
+	tco = get_cpu_var(tx_copy_ops);
+
+	if (tco == NULL) {
+		put_cpu_var(tx_copy_ops);
+		if (unusable_count == 1000) {

Same as above

Quoted 56 lines
drivers/net/xen-netback/netback.c +40 -4
+			printk(KERN_ALERT
+			       "xen-netback: "
+			       "CPU %d scratch space is not available,"
+			       " not doing any RX work for netback/%d\n",
+			       smp_processor_id(),
+			       (int)(netbk - xen_netbk));
+		} else
+			unusable_count++;
+		return;
+	}
 
-	nr_gops = xen_netbk_tx_build_gops(netbk);
+	nr_gops = xen_netbk_tx_build_gops(netbk, tco);
 
-	if (nr_gops == 0)
+	if (nr_gops == 0) {
+		put_cpu_var(tx_copy_ops);
  		return;
+	}
+
+	gnttab_batch_copy(tco, nr_gops);
 
-	gnttab_batch_copy(netbk->tx_copy_ops, nr_gops);
+	xen_netbk_tx_submit(netbk, tco);
 
-	xen_netbk_tx_submit(netbk);
+	put_cpu_var(tx_copy_ops);
  }
 
  static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx,
@@ -1760,6 +1814,93 @@ static int xen_netbk_kthread(void *data)
  	return 0;
  }
 
+static int __create_percpu_scratch_space(unsigned int cpu)
+{
+	if (per_cpu(tx_copy_ops, cpu) ||
+	    per_cpu(grant_copy_op, cpu) ||
+	    per_cpu(meta, cpu))
+		return 0;
+
+	per_cpu(tx_copy_ops, cpu) =
+		vzalloc_node(sizeof(struct gnttab_copy) * TX_COPY_OPS_SIZE,
+			     cpu_to_node(cpu));
+
+	per_cpu(grant_copy_op, cpu) =
+		vzalloc_node(sizeof(struct gnttab_copy) * GRANT_COPY_OP_SIZE,
+			     cpu_to_node(cpu));
+
+	per_cpu(meta, cpu) =
+		vzalloc_node(sizeof(struct netbk_rx_meta) * META_SIZE,
+			     cpu_to_node(cpu));
+
+	if (!per_cpu(tx_copy_ops, cpu) ||
+	    !per_cpu(grant_copy_op, cpu) ||
+	    !per_cpu(meta, cpu))

drivers/net/xen-netback/netback.c static int xen_netbk_kthread(void *data) · line 1848

Quoted 10 lines
drivers/net/xen-netback/netback.c +10 -0
+		return -ENOMEM;
+
+	return 0;
+}
+
+static void __free_percpu_scratch_space(unsigned int cpu)
+{
+	void *tmp;
+
+	tmp = per_cpu(tx_copy_ops, cpu);

It is better to verify whether tmp is available before freeing it, for example: if (tmp)

drivers/net/xen-netback/netback.c static int xen_netbk_kthread(void *data) · line 1852

Quoted 4 lines
drivers/net/xen-netback/netback.c +4 -0
+	per_cpu(tx_copy_ops, cpu) = NULL;
+	vfree(tmp);
+
+	tmp = per_cpu(grant_copy_op, cpu);

same

drivers/net/xen-netback/netback.c static int xen_netbk_kthread(void *data) · line 1856

Quoted 4 lines
drivers/net/xen-netback/netback.c +4 -0
+	per_cpu(grant_copy_op, cpu) = NULL;
+	vfree(tmp);
+
+	tmp = per_cpu(meta, cpu);

same

Quoted 36 lines
drivers/net/xen-netback/netback.c +36 -0
+	per_cpu(meta, cpu) = NULL;
+	vfree(tmp);
+}
+
+static int __netback_percpu_callback(struct notifier_block *nfb,
+				     unsigned long action, void *hcpu)
+{
+	unsigned int cpu = (unsigned long)hcpu;
+	int rc = NOTIFY_DONE;
+
+	switch (action) {
+	case CPU_ONLINE:
+	case CPU_ONLINE_FROZEN:
+		printk(KERN_INFO "xen-netback: CPU %d online, creating scratch space\n",
+		       cpu);
+		rc = __create_percpu_scratch_space(cpu);
+		if (rc) {
+			printk(KERN_ALERT "xen-netback: failed to create scratch space for CPU %d\n",
+			       cpu);
+			/* There is really nothing more we can do. Free any
+			 * partially allocated scratch space. When processing
+			 * routines get to run they will just print warning
+			 * message and stop processing.
+			 */
+			__free_percpu_scratch_space(cpu);
+			rc = NOTIFY_BAD;
+		} else
+			rc = NOTIFY_OK;
+		break;
+	case CPU_DEAD:
+	case CPU_DEAD_FROZEN:
+		printk(KERN_INFO "xen-netback: CPU %d offline, destroying scratch space\n",
+		       cpu);
+		__free_percpu_scratch_space(cpu);
+		rc = NOTIFY_OK;
+		break;

drivers/net/xen-netback/netback.c static int xen_netbk_kthread(void *data) · line 1902

Quoted 10 lines
drivers/net/xen-netback/netback.c +10 -0
+	default:
+		break;
+	}
+
+	return rc;
+}
+
+static struct notifier_block netback_notifier_block = {
+	.notifier_call = __netback_percpu_callback,
+};

Moving this to the top of this file?

Quoted 22 lines
drivers/net/xen-netback/netback.c +8 -0
+
  void xen_netbk_unmap_frontend_rings(struct xenvif *vif)
  {
  	if (vif->tx.sring)
@@ -1810,6 +1951,7 @@ static int __init netback_init(void)
  	int i;
  	int rc = 0;
  	int group;
+	int cpu;
 
  	if (!xen_domain())
  		return -ENODEV;
@@ -1821,10 +1963,21 @@ static int __init netback_init(void)
  		fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
  	}
 
+	for_each_online_cpu(cpu) {
+		rc = __create_percpu_scratch_space(cpu);
+		if (rc) {
+			rc = -ENOMEM;
+			goto failed_init;
+		}

drivers/net/xen-netback/netback.c static int __init netback_init(void) · line 1979

Quoted 10 lines
drivers/net/xen-netback/netback.c +6 -2
+	}
+	register_hotcpu_notifier(&netback_notifier_block);
+
  	xen_netbk_group_nr = num_online_cpus();
  	xen_netbk = vzalloc(sizeof(struct xen_netbk) * xen_netbk_group_nr);
-	if (!xen_netbk)
-		return -ENOMEM;
+	if (!xen_netbk) {
+		goto failed_init;
+		rc = -ENOMEM;

rc = -ENOMEM is never called.

Thanks
Annie

author
#7 lore raw

Re: [PATCH 2/3] xen-netback: switch to per-cpu scratch space

On Tue, May 28, 2013 at 05:47:25PM +0800, annie li wrote:
[...]

Quoted 4 lines
Quoted 4 lines
drivers/net/xen-netback/netback.c +0 -2
-	struct gnttab_copy grant_copy_op[2*XEN_NETIF_RX_RING_SIZE];
-	struct netbk_rx_meta meta[2*XEN_NETIF_RX_RING_SIZE];
 };
 static struct xen_netbk *xen_netbk;
Quoted 8 lines

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

Quoted 6 lines
drivers/net/xen-netback/netback.c +2 -0
@@ -608,12 +613,31 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 	int count;
 	unsigned long offset;
 	struct skb_cb_overlay *sco;
+	struct gnttab_copy *gco = get_cpu_var(grant_copy_op);
+	struct netbk_rx_meta *m = get_cpu_var(meta);

Change m to a friendly name?

Changed to "meta".

Quoted 16 lines

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

Quoted 12 lines
drivers/net/xen-netback/netback.c +7 -2
+	static int unusable_count;
 	struct netrx_pending_operations npo = {
-		.copy  = netbk->grant_copy_op,
-		.meta  = netbk->meta,
+		.copy = gco,
+		.meta = m,
 	};
+	if (gco == NULL || m == NULL) {
+		put_cpu_var(grant_copy_op);
+		put_cpu_var(meta);
+		if (unusable_count == 1000) {

It is better to use a macro to replace this number here. BTW, can you explain why using 1000 here?

This is just a random number chosen to avoid flooding dmesg log. :-)

Re macro, this is the only place that use this test, so no need for a macro. Following test in TX path look similar, but the put_cpu_var() in that branch is different, I doubt we can get much from defining a macro here.

Quoted 12 lines

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

Quoted 6 lines
drivers/net/xen-netback/netback.c +6 -0
+			printk(KERN_ALERT
+			       "xen-netback: "
+			       "CPU %d scratch space is not available,"
+			       " not doing any TX work for netback/%d\n",
+			       smp_processor_id(),
+			       (int)(netbk - xen_netbk));

unusable_count is not a value based on netbk here. I assume you use unusable_count to judge whether scratch space is available for specific netbk, if so, then unusable_count needs to be counter for specific netbk, not for all netbk.

No, it is not based on netbk. It is for a particular CPU. Per-cpu scratch is for CPUs not netbks. Netback thread can always be scheduled on other CPU in 1:1 model, so in practice it will almost never print out this warning if unusable_count is for netbk.

[...]

Quoted 5 lines

Review static void xen_netbk_rx_action(struct xen_netbk *netbk) · line 634

Quoted 2 lines
(quoted diff) +2 -0
+
+		if (unusable_count == 1000) {

Same as above

[...]

Quoted 12 lines

drivers/net/xen-netback/netback.c static int xen_netbk_kthread(void *data) · line 1848

Quoted 8 lines
drivers/net/xen-netback/netback.c +8 -0
+	return 0;
+}
+
+static void __free_percpu_scratch_space(unsigned int cpu)
+{
+	void *tmp;
+
+	tmp = per_cpu(tx_copy_ops, cpu);

It is better to verify whether tmp is available before freeing it, for example: if (tmp)

No need to do this, as it is legit to free() a NULL pointer.

Quoted 1 line

[...]

Quoted 7 lines

drivers/net/xen-netback/netback.c static int xen_netbk_kthread(void *data) · line 1902

Quoted 4 lines
drivers/net/xen-netback/netback.c +4 -0
+
+static struct notifier_block netback_notifier_block = {
+	.notifier_call = __netback_percpu_callback,
+};

Moving this to the top of this file?

It is sort of convention to put this sort of things in the back rather in the front.

Quoted 1 line
Quoted 1 line

+

[...]

Wei.

Quoted 4 lines

Thanks
Annie

#8 lore raw

Re: [PATCH 2/3] xen-netback: switch to per-cpu scratch space

In reply to Wei Liu
Quoted 25 lines · Wei Liu

There are maximum nr_onlie_cpus netback threads running. We can make use of per-cpu scratch space to reduce the size of buffer space when we move to 1:1 model.

In the unlikely event when per-cpu scratch space is not available, processing routines will refuse to run on that CPU.

---
 drivers/net/xen-netback/netback.c |  247 ++++++++++++++++++++++++++++++-------
 1 file changed, 204 insertions(+), 43 deletions(-)
drivers/net/xen-netback/netback.c +1 -0
index 54853be..0f69eda 100644
@@ -37,6 +37,7 @@
 #include <linux/kthread.h>
 #include <linux/if_vlan.h>
 #include <linux/udp.h>
+#include <linux/cpu.h>
 
 #include <net/tcp.h>
 

drivers/net/xen-netback/netback.c struct netbk_rx_meta { · line 105

Quoted 10 lines
drivers/net/xen-netback/netback.c +7 -0
@@ -95,6 +96,24 @@ struct netbk_rx_meta {
 
 #define MAX_BUFFER_OFFSET PAGE_SIZE
 
+/* Coalescing tx requests before copying makes number of grant
+ * copy ops greater or equal to number of slots required. In
+ * worst case a tx request consumes 2 gnttab_copy. So the size
+ * of tx_copy_ops array should be 2*MAX_PENDING_REQS.
+ */
+#define TX_COPY_OPS_SIZE (2*MAX_PENDING_REQS)
+DEFINE_PER_CPU(struct gnttab_copy *, tx_copy_ops);

static

drivers/net/xen-netback/netback.c struct netbk_rx_meta { · line 115

Quoted 10 lines
drivers/net/xen-netback/netback.c +10 -0
+
+/* Given MAX_BUFFER_OFFSET of 4096 the worst case is that each
+ * head/fragment page uses 2 copy operations because it
+ * straddles two buffers in the frontend. So the size of following
+ * arrays should be 2*XEN_NETIF_RX_RING_SIZE.
+ */
+#define GRANT_COPY_OP_SIZE (2*XEN_NETIF_RX_RING_SIZE)
+#define META_SIZE (2*XEN_NETIF_RX_RING_SIZE)
+DEFINE_PER_CPU(struct gnttab_copy *, grant_copy_op);
+DEFINE_PER_CPU(struct netbk_rx_meta *, meta);

static for both of them.

Quoted 35 lines
drivers/net/xen-netback/netback.c +4 -14
+
 struct xen_netbk {
 	wait_queue_head_t wq;
 	struct task_struct *task;
@@ -116,21 +135,7 @@ struct xen_netbk {
 	atomic_t netfront_count;
 
 	struct pending_tx_info pending_tx_info[MAX_PENDING_REQS];
-	/* Coalescing tx requests before copying makes number of grant
-	 * copy ops greater or equal to number of slots required. In
-	 * worst case a tx request consumes 2 gnttab_copy.
-	 */
-	struct gnttab_copy tx_copy_ops[2*MAX_PENDING_REQS];
-
 	u16 pending_ring[MAX_PENDING_REQS];
-
-	/*
-	 * Given MAX_BUFFER_OFFSET of 4096 the worst case is that each
-	 * head/fragment page uses 2 copy operations because it
-	 * straddles two buffers in the frontend.
-	 */
-	struct gnttab_copy grant_copy_op[2*XEN_NETIF_RX_RING_SIZE];
-	struct netbk_rx_meta meta[2*XEN_NETIF_RX_RING_SIZE];
 };
 
 static struct xen_netbk *xen_netbk;
@@ -608,12 +613,31 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 	int count;
 	unsigned long offset;
 	struct skb_cb_overlay *sco;
+	struct gnttab_copy *gco = get_cpu_var(grant_copy_op);
+	struct netbk_rx_meta *m = get_cpu_var(meta);
+	static int unusable_count;
 
 	struct netrx_pending_operations npo = {

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

Quoted 10 lines
drivers/net/xen-netback/netback.c +6 -2
-		.copy  = netbk->grant_copy_op,
-		.meta  = netbk->meta,
+		.copy = gco,
+		.meta = m,
 	};
 
+	if (gco == NULL || m == NULL) {
+		put_cpu_var(grant_copy_op);
+		put_cpu_var(meta);
+		if (unusable_count == 1000) {

printk_ratelimited ?

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

Quoted 6 lines
drivers/net/xen-netback/netback.c +6 -0
+			printk(KERN_ALERT
+			       "xen-netback: "
+			       "CPU %d scratch space is not available,"
+			       " not doing any TX work for netback/%d\n",
+			       smp_processor_id(),
+			       (int)(netbk - xen_netbk));

So ... are you going to retry it? Drop it? Can you include in the message the the mechanism by which you are going to recover?

Quoted 129 lines
drivers/net/xen-netback/netback.c +32 -18
+			unusable_count = 0;
+		} else
+			unusable_count++;
+		return;
+	}
+
 	skb_queue_head_init(&rxq);
 
 	count = 0;
@@ -635,27 +659,30 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 			break;
 	}
 
-	BUG_ON(npo.meta_prod > ARRAY_SIZE(netbk->meta));
+	BUG_ON(npo.meta_prod > META_SIZE);
 
-	if (!npo.copy_prod)
+	if (!npo.copy_prod) {
+		put_cpu_var(grant_copy_op);
+		put_cpu_var(meta);
 		return;
+	}
 
-	BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
-	gnttab_batch_copy(netbk->grant_copy_op, npo.copy_prod);
+	BUG_ON(npo.copy_prod > GRANT_COPY_OP_SIZE);
+	gnttab_batch_copy(gco, npo.copy_prod);
 
 	while ((skb = __skb_dequeue(&rxq)) != NULL) {
 		sco = (struct skb_cb_overlay *)skb->cb;
 
 		vif = netdev_priv(skb->dev);
 
-		if (netbk->meta[npo.meta_cons].gso_size && vif->gso_prefix) {
+		if (m[npo.meta_cons].gso_size && vif->gso_prefix) {
 			resp = RING_GET_RESPONSE(&vif->rx,
 						vif->rx.rsp_prod_pvt++);
 
 			resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
 
-			resp->offset = netbk->meta[npo.meta_cons].gso_size;
-			resp->id = netbk->meta[npo.meta_cons].id;
+			resp->offset = m[npo.meta_cons].gso_size;
+			resp->id = m[npo.meta_cons].id;
 			resp->status = sco->meta_slots_used;
 
 			npo.meta_cons++;
@@ -680,12 +707,12 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 			flags |= XEN_NETRXF_data_validated;
 
 		offset = 0;
-		resp = make_rx_response(vif, netbk->meta[npo.meta_cons].id,
+		resp = make_rx_response(vif, m[npo.meta_cons].id,
 					status, offset,
-					netbk->meta[npo.meta_cons].size,
+					m[npo.meta_cons].size,
 					flags);
 
-		if (netbk->meta[npo.meta_cons].gso_size && !vif->gso_prefix) {
+		if (m[npo.meta_cons].gso_size && !vif->gso_prefix) {
 			struct xen_netif_extra_info *gso =
 				(struct xen_netif_extra_info *)
 				RING_GET_RESPONSE(&vif->rx,
@@ -693,7 +720,7 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 
 			resp->flags |= XEN_NETRXF_extra_info;
 
-			gso->u.gso.size = netbk->meta[npo.meta_cons].gso_size;
+			gso->u.gso.size = m[npo.meta_cons].gso_size;
 			gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
 			gso->u.gso.pad = 0;
 			gso->u.gso.features = 0;
@@ -703,7 +730,7 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 		}
 
 		netbk_add_frag_responses(vif, status,
-					 netbk->meta + npo.meta_cons + 1,
+					 m + npo.meta_cons + 1,
 					 sco->meta_slots_used);
 
 		RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
@@ -726,6 +753,9 @@ static void xen_netbk_rx_action(struct xen_netbk *netbk)
 	if (!skb_queue_empty(&netbk->rx_queue) &&
 			!timer_pending(&netbk->net_timer))
 		xen_netbk_kick_thread(netbk);
+
+	put_cpu_var(grant_copy_op);
+	put_cpu_var(meta);
 }
 
 void xen_netbk_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb)
@@ -1351,9 +1381,10 @@ static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
 	return false;
 }
 
-static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
+static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk,
+					struct gnttab_copy *tco)
 {
-	struct gnttab_copy *gop = netbk->tx_copy_ops, *request_gop;
+	struct gnttab_copy *gop = tco, *request_gop;
 	struct sk_buff *skb;
 	int ret;
 
@@ -1531,16 +1562,17 @@ static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
 		vif->tx.req_cons = idx;
 		xen_netbk_check_rx_xenvif(vif);
 
-		if ((gop-netbk->tx_copy_ops) >= ARRAY_SIZE(netbk->tx_copy_ops))
+		if ((gop-tco) >= TX_COPY_OPS_SIZE)
 			break;
 	}
 
-	return gop - netbk->tx_copy_ops;
+	return gop - tco;
 }
 
-static void xen_netbk_tx_submit(struct xen_netbk *netbk)
+static void xen_netbk_tx_submit(struct xen_netbk *netbk,
+				struct gnttab_copy *tco)
 {
-	struct gnttab_copy *gop = netbk->tx_copy_ops;
+	struct gnttab_copy *gop = tco;
 	struct sk_buff *skb;
 
 	while ((skb = __skb_dequeue(&netbk->tx_queue)) != NULL) {
@@ -1615,15 +1647,37 @@ static void xen_netbk_tx_submit(struct xen_netbk *netbk)
 static void xen_netbk_tx_action(struct xen_netbk *netbk)
 {

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

Quoted 10 lines
drivers/net/xen-netback/netback.c +9 -0
 	unsigned nr_gops;
+	struct gnttab_copy *tco;
+	static int unusable_count;
+
+	tco = get_cpu_var(tx_copy_ops);
+
+	if (tco == NULL) {
+		put_cpu_var(tx_copy_ops);
+		if (unusable_count == 1000) {
+			printk(KERN_ALERT

Ditto. printk_ratelimited.

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

Quoted 5 lines
drivers/net/xen-netback/netback.c +5 -0
+			       "xen-netback: "
+			       "CPU %d scratch space is not available,"
+			       " not doing any RX work for netback/%d\n",
+			       smp_processor_id(),
+			       (int)(netbk - xen_netbk));

And can you explain what the recovery mechanism is?

Quoted 41 lines
drivers/net/xen-netback/netback.c +25 -4
+		} else
+			unusable_count++;
+		return;
+	}
 
-	nr_gops = xen_netbk_tx_build_gops(netbk);
+	nr_gops = xen_netbk_tx_build_gops(netbk, tco);
 
-	if (nr_gops == 0)
+	if (nr_gops == 0) {
+		put_cpu_var(tx_copy_ops);
 		return;
+	}
+
+	gnttab_batch_copy(tco, nr_gops);
 
-	gnttab_batch_copy(netbk->tx_copy_ops, nr_gops);
+	xen_netbk_tx_submit(netbk, tco);
 
-	xen_netbk_tx_submit(netbk);
+	put_cpu_var(tx_copy_ops);
 }
 
 static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx,
@@ -1760,6 +1814,93 @@ static int xen_netbk_kthread(void *data)
 	return 0;
 }
 
+static int __create_percpu_scratch_space(unsigned int cpu)
+{
+	if (per_cpu(tx_copy_ops, cpu) ||
+	    per_cpu(grant_copy_op, cpu) ||
+	    per_cpu(meta, cpu))
+		return 0;
+
+	per_cpu(tx_copy_ops, cpu) =
+		vzalloc_node(sizeof(struct gnttab_copy) * TX_COPY_OPS_SIZE,
+			     cpu_to_node(cpu));
+
+	per_cpu(grant_copy_op, cpu) =
+		vzalloc_node(sizeof(struct gnttab_copy) * GRANT_COPY_OP_SIZE,

drivers/net/xen-netback/netback.c static int xen_netbk_kthread(void *data) · line 1839

Quoted 10 lines
drivers/net/xen-netback/netback.c +10 -0
+			     cpu_to_node(cpu));
+
+	per_cpu(meta, cpu) =
+		vzalloc_node(sizeof(struct netbk_rx_meta) * META_SIZE,
+			     cpu_to_node(cpu));
+
+	if (!per_cpu(tx_copy_ops, cpu) ||
+	    !per_cpu(grant_copy_op, cpu) ||
+	    !per_cpu(meta, cpu))
+		return -ENOMEM;

And no freeing? Ah you require the __free_percpu_scratch_space to do the job for you. Um, why not do it here instead of depending on the calleer to clean up the mess?

Say:

		{
			__free_percpu_scratch_space(cpu);
			return -ENOMEM;
		}

?

Quoted 22 lines
drivers/net/xen-netback/netback.c +22 -0
+
+	return 0;
+}
+
+static void __free_percpu_scratch_space(unsigned( int cpu)
+{
+	void *tmp;
+
+	tmp = per_cpu(tx_copy_ops, cpu);
+	per_cpu(tx_copy_ops, cpu) = NULL;
+	vfree(tmp);
+
+	tmp = per_cpu(grant_copy_op, cpu);
+	per_cpu(grant_copy_op, cpu) = NULL;
+	vfree(tmp);
+
+	tmp = per_cpu(meta, cpu);
+	per_cpu(meta, cpu) = NULL;
+	vfree(tmp);
+}
+
+static int __netback_percpu_callback(struct notifier_block *nfb,

drivers/net/xen-netback/netback.c static int xen_netbk_kthread(void *data) · line 1871

Quoted 10 lines
drivers/net/xen-netback/netback.c +10 -0
+				     unsigned long action, void *hcpu)
+{
+	unsigned int cpu = (unsigned long)hcpu;
+	int rc = NOTIFY_DONE;
+
+	switch (action) {
+	case CPU_ONLINE:
+	case CPU_ONLINE_FROZEN:
+		printk(KERN_INFO "xen-netback: CPU %d online, creating scratch space\n",
+		       cpu);

I think this is more of pr_debug type.

drivers/net/xen-netback/netback.c static int xen_netbk_kthread(void *data) · line 1881

Quoted 10 lines
drivers/net/xen-netback/netback.c +10 -0
+		rc = __create_percpu_scratch_space(cpu);
+		if (rc) {
+			printk(KERN_ALERT "xen-netback: failed to create scratch space for CPU %d\n",
+			       cpu);
+			/* There is really nothing more we can do. Free any
+			 * partially allocated scratch space. When processing
+			 * routines get to run they will just print warning
+			 * message and stop processing.
+			 */
+			__free_percpu_scratch_space(cpu);

Ugh. Could the code skip creating a kthread on a CPU for which the per_cpu(meta, cpu) == NULL?

Quoted 104 lines
drivers/net/xen-netback/netback.c +47 -5
+			rc = NOTIFY_BAD;
+		} else
+			rc = NOTIFY_OK;
+		break;
+	case CPU_DEAD:
+	case CPU_DEAD_FROZEN:
+		printk(KERN_INFO "xen-netback: CPU %d offline, destroying scratch space\n",
+		       cpu);
+		__free_percpu_scratch_space(cpu);
+		rc = NOTIFY_OK;
+		break;
+	default:
+		break;
+	}
+
+	return rc;
+}
+
+static struct notifier_block netback_notifier_block = {
+	.notifier_call = __netback_percpu_callback,
+};
+
 void xen_netbk_unmap_frontend_rings(struct xenvif *vif)
 {
 	if (vif->tx.sring)
@@ -1810,6 +1951,7 @@ static int __init netback_init(void)
 	int i;
 	int rc = 0;
 	int group;
+	int cpu;
 
 	if (!xen_domain())
 		return -ENODEV;
@@ -1821,10 +1963,21 @@ static int __init netback_init(void)
 		fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
 	}
 
+	for_each_online_cpu(cpu) {
+		rc = __create_percpu_scratch_space(cpu);
+		if (rc) {
+			rc = -ENOMEM;
+			goto failed_init;
+		}
+	}
+	register_hotcpu_notifier(&netback_notifier_block);
+
 	xen_netbk_group_nr = num_online_cpus();
 	xen_netbk = vzalloc(sizeof(struct xen_netbk) * xen_netbk_group_nr);
-	if (!xen_netbk)
-		return -ENOMEM;
+	if (!xen_netbk) {
+		goto failed_init;
+		rc = -ENOMEM;
+	}
 
 	for (group = 0; group < xen_netbk_group_nr; group++) {
 		struct xen_netbk *netbk = &xen_netbk[group];
@@ -1849,7 +2002,7 @@ static int __init netback_init(void)
 			printk(KERN_ALERT "kthread_create() fails at netback\n");
 			del_timer(&netbk->net_timer);
 			rc = PTR_ERR(netbk->task);
-			goto failed_init;
+			goto failed_init_destroy_kthreads;
 		}
 
 		kthread_bind(netbk->task, group);
@@ -1865,17 +2018,20 @@ static int __init netback_init(void)
 
 	rc = xenvif_xenbus_init();
 	if (rc)
-		goto failed_init;
+		goto failed_init_destroy_kthreads;
 
 	return 0;
 
-failed_init:
+failed_init_destroy_kthreads:
 	while (--group >= 0) {
 		struct xen_netbk *netbk = &xen_netbk[group];
 		del_timer(&netbk->net_timer);
 		kthread_stop(netbk->task);
 	}
 	vfree(xen_netbk);
+failed_init:
+	for_each_online_cpu(cpu)
+		__free_percpu_scratch_space(cpu);
 	return rc;
 
 }
@@ -1899,6 +2055,11 @@ static void __exit netback_fini(void)
 	}
 
 	vfree(xen_netbk);
+
+	unregister_hotcpu_notifier(&netback_notifier_block);
+
+	for_each_online_cpu(i)
+		__free_percpu_scratch_space(i);
 }
 module_exit(netback_fini);
 
Signature · 1 line
1.7.10.4
#9 lore raw

Re: [PATCH 2/3] xen-netback: switch to per-cpu scratch space

Quoted 7 lines · Konrad Rzeszutek Wilk
Quoted 6 lines · Wei Liu

There are maximum nr_onlie_cpus netback threads running. We can make use of per-cpu scratch space to reduce the size of buffer space when we move to 1:1 model.

In the unlikely event when per-cpu scratch space is not available, processing routines will refuse to run on that CPU.

[...]

Quoted 2 lines
Quoted 2 lines
drivers/net/xen-netback/netback.c +0 -0

[...]

Quoted 10 lines

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

Quoted 6 lines
drivers/net/xen-netback/netback.c +6 -0
+			printk(KERN_ALERT
+			       "xen-netback: "
+			       "CPU %d scratch space is not available,"
+			       " not doing any TX work for netback/%d\n",
+			       smp_processor_id(),
+			       (int)(netbk - xen_netbk));

So ... are you going to retry it? Drop it? Can you include in the message the the mechanism by which you are going to recover?

[...]

Quoted 7 lines

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

Quoted 5 lines
drivers/net/xen-netback/netback.c +5 -0
+			       "xen-netback: "
+			       "CPU %d scratch space is not available,"
+			       " not doing any RX work for netback/%d\n",
+			       smp_processor_id(),
+			       (int)(netbk - xen_netbk));

And can you explain what the recovery mechanism is?

There isn't any recovery mechanism at the moment. If the scratch space was not allocated then any netback thread may end up being unable to do any work indefinitely (if the scheduler repeatedly schedules them on the VCPU with no scratch space).

This is an appalling failure mode.

I also don't think there is a sensible way to recover. We do not want hotplugging of a VCPU to break or degrade the behaviour of existing VIFs.

The meta data is 12 * 512 = 6144 and the grant table ops is 24 * 512 = 12288. This works out to 6 pages total. I think we can spare 6 pages per VIF and just have per-thread scratch space.

You may also want to consider a smaller batch size instead of allowing for 2x ring size. How often do you need this many entries?

David

author
#10 lore raw

Re: [PATCH 2/3] xen-netback: switch to per-cpu scratch space

Quoted 31 lines · David Vrabel
Quoted 7 lines · Konrad Rzeszutek Wilk
Quoted 6 lines · Wei Liu

There are maximum nr_onlie_cpus netback threads running. We can make use of per-cpu scratch space to reduce the size of buffer space when we move to 1:1 model.

In the unlikely event when per-cpu scratch space is not available, processing routines will refuse to run on that CPU.

[...]

Quoted 2 lines
Quoted 2 lines
drivers/net/xen-netback/netback.c +0 -0

[...]

Quoted 10 lines

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

Quoted 6 lines
drivers/net/xen-netback/netback.c +6 -0
+			printk(KERN_ALERT
+			       "xen-netback: "
+			       "CPU %d scratch space is not available,"
+			       " not doing any TX work for netback/%d\n",
+			       smp_processor_id(),
+			       (int)(netbk - xen_netbk));

So ... are you going to retry it? Drop it? Can you include in the message the the mechanism by which you are going to recover?

[...]

Quoted 7 lines

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

Quoted 5 lines
drivers/net/xen-netback/netback.c +5 -0
+			       "xen-netback: "
+			       "CPU %d scratch space is not available,"
+			       " not doing any RX work for netback/%d\n",
+			       smp_processor_id(),
+			       (int)(netbk - xen_netbk));

And can you explain what the recovery mechanism is?

Quoted 7 lines

There isn't any recovery mechanism at the moment. If the scratch space was not allocated then any netback thread may end up being unable to do any work indefinitely (if the scheduler repeatedly schedules them on the VCPU with no scratch space).

This is an appalling failure mode.

This looks appalling at first glance but I doubt that people would pick this patch without picking the later one.

With later patch vifs can be scheduled on different CPUs so that it always gets a chance to work.

This patch is proposed before that one to reduce meaningless code movement.

Quoted 7 lines

I also don't think there is a sensible way to recover. We do not want hotplugging of a VCPU to break or degrade the behaviour of existing VIFs.

The meta data is 12 * 512 = 6144 and the grant table ops is 24 * 512 = 12288. This works out to 6 pages total. I think we can spare 6 pages per VIF and just have per-thread scratch space.

Sure, we can always worry about shrinking space usage later. :-)

I don't really mind using extra space. I only want a new working baseline.

Quoted 2 lines

You may also want to consider a smaller batch size instead of allowing for 2x ring size. How often do you need this many entries?

Not often, but we ought to prepare for the worst case, right?

Wei.

Quoted 2 lines

David

author
#11 lore raw

PATCH v2 3/3 xen-netback: switch to NAPI + kthread 1:1 model

In reply to Wei Liu

This patch implements 1:1 model netback. NAPI and kthread are utilized to do the weight-lifting job:

- NAPI is used for guest side TX (host side RX)
- kthread is used for guest side RX (host side TX)

Xenvif and xen_netbk are made into one structure to reduce code size.

This model provides better scheduling fairness among vifs. It is also prerequisite for implementing multiqueue for Xen netback.

---
 drivers/net/xen-netback/common.h    |   92 +++--
 drivers/net/xen-netback/interface.c |  122 +++---
 drivers/net/xen-netback/netback.c   |  719 ++++++++++++-----------------------
 3 files changed, 373 insertions(+), 560 deletions(-)
drivers/net/xen-netback/common.h +57 -35
index 8a4d77e..5920d6c 100644
@@ -45,15 +45,43 @@
 #include <xen/grant_table.h>
 #include <xen/xenbus.h>
 
-struct xen_netbk;
+typedef unsigned int pending_ring_idx_t;
+#define INVALID_PENDING_RING_IDX (~0U)
+
+struct pending_tx_info {
+	struct xen_netif_tx_request req; /* coalesced tx request */
+	pending_ring_idx_t head; /* head != INVALID_PENDING_RING_IDX
+				  * if it is head of one or more tx
+				  * reqs
+				  */
+};
+
+#define XEN_NETIF_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, PAGE_SIZE)
+#define XEN_NETIF_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, PAGE_SIZE)
+
+struct xenvif_rx_meta {
+	int id;
+	int size;
+	int gso_size;
+};
+
+/* Discriminate from any valid pending_idx value. */
+#define INVALID_PENDING_IDX 0xFFFF
+
+#define MAX_BUFFER_OFFSET PAGE_SIZE
+
+#define MAX_PENDING_REQS 256
 
 struct xenvif {
 	/* Unique identifier for this interface. */
 	domid_t          domid;
 	unsigned int     handle;
 
-	/* Reference to netback processing backend. */
-	struct xen_netbk *netbk;
+	/* Use NAPI for guest TX */
+	struct napi_struct napi;
+	/* Use kthread for guest RX */
+	struct task_struct *task;
+	wait_queue_head_t wq;
 
 	u8               fe_dev_addr[6];
 
@@ -64,9 +92,6 @@ struct xenvif {
 	char tx_irq_name[IFNAMSIZ+4]; /* DEVNAME-tx */
 	char rx_irq_name[IFNAMSIZ+4]; /* DEVNAME-rx */
 
-	/* List of frontends to notify after a batch of frames sent. */
-	struct list_head notify_list;
-
 	/* The shared rings and indexes. */
 	struct xen_netif_tx_back_ring tx;
 	struct xen_netif_rx_back_ring rx;
@@ -96,12 +121,20 @@ struct xenvif {
 	/* Statistics */
 	unsigned long rx_gso_checksum_fixup;
 
+	struct sk_buff_head rx_queue;
+	struct sk_buff_head tx_queue;
+
+	struct page *mmap_pages[MAX_PENDING_REQS];
+
+	pending_ring_idx_t pending_prod;
+	pending_ring_idx_t pending_cons;
+
+	struct pending_tx_info pending_tx_info[MAX_PENDING_REQS];
+
+	u16 pending_ring[MAX_PENDING_REQS];
+
 	/* Miscellaneous private stuff. */
-	struct list_head schedule_list;
-	atomic_t         refcnt;
 	struct net_device *dev;
-
-	wait_queue_head_t waiting_to_free;
 };
 
 static inline struct xenbus_device *xenvif_to_xenbus_device(struct xenvif *vif)
@@ -109,9 +142,6 @@ static inline struct xenbus_device *xenvif_to_xenbus_device(struct xenvif *vif)
 	return to_xenbus_device(vif->dev->dev.parent);
 }
 
-#define XEN_NETIF_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, PAGE_SIZE)
-#define XEN_NETIF_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, PAGE_SIZE)
-
 struct xenvif *xenvif_alloc(struct device *parent,
 			    domid_t domid,
 			    unsigned int handle);
@@ -121,39 +151,26 @@ int xenvif_connect(struct xenvif *vif, unsigned long tx_ring_ref,
 		   unsigned int rx_evtchn);
 void xenvif_disconnect(struct xenvif *vif);
 
-void xenvif_get(struct xenvif *vif);
-void xenvif_put(struct xenvif *vif);
-
 int xenvif_xenbus_init(void);
 void xenvif_xenbus_fini(void);
 
 int xenvif_schedulable(struct xenvif *vif);
 
-int xen_netbk_rx_ring_full(struct xenvif *vif);
+int xenvif_rx_ring_full(struct xenvif *vif);
 
-int xen_netbk_must_stop_queue(struct xenvif *vif);
+int xenvif_must_stop_queue(struct xenvif *vif);
 
 /* (Un)Map communication rings. */
-void xen_netbk_unmap_frontend_rings(struct xenvif *vif);
-int xen_netbk_map_frontend_rings(struct xenvif *vif,
-				 grant_ref_t tx_ring_ref,
-				 grant_ref_t rx_ring_ref);
-
-/* (De)Register a xenvif with the netback backend. */
-void xen_netbk_add_xenvif(struct xenvif *vif);
-void xen_netbk_remove_xenvif(struct xenvif *vif);
-
-/* (De)Schedule backend processing for a xenvif */
-void xen_netbk_schedule_xenvif(struct xenvif *vif);
-void xen_netbk_deschedule_xenvif(struct xenvif *vif);
+void xenvif_unmap_frontend_rings(struct xenvif *vif);
+int xenvif_map_frontend_rings(struct xenvif *vif,
+			      grant_ref_t tx_ring_ref,
+			      grant_ref_t rx_ring_ref);
 
 /* Check for SKBs from frontend and schedule backend processing */
-void xen_netbk_check_rx_xenvif(struct xenvif *vif);
-/* Receive an SKB from the frontend */
-void xenvif_receive_skb(struct xenvif *vif, struct sk_buff *skb);
+void xenvif_check_rx_xenvif(struct xenvif *vif);
 
 /* Queue an SKB for transmission to the frontend */
-void xen_netbk_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb);
+void xenvif_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb);
 /* Notify xenvif that ring now has space to send an skb to the frontend */
 void xenvif_notify_tx_completion(struct xenvif *vif);
 
@@ -161,7 +178,12 @@ void xenvif_notify_tx_completion(struct xenvif *vif);
 void xenvif_carrier_off(struct xenvif *vif);
 
 /* Returns number of ring slots required to send an skb to the frontend */
-unsigned int xen_netbk_count_skb_slots(struct xenvif *vif, struct sk_buff *skb);
+unsigned int xenvif_count_skb_slots(struct xenvif *vif, struct sk_buff *skb);
+
+int xenvif_tx_action(struct xenvif *vif, int budget);
+void xenvif_rx_action(struct xenvif *vif);
+
+int xenvif_kthread(void *data);
 
 extern bool separate_tx_rx_irq;
 
drivers/net/xen-netback/interface.c +75 -47

266 lines not on this page · raw message

drivers/net/xen-netback/netback.c +241 -478

1,370 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#12 lore raw
Quoted 5 lines · Wei Liu

This patch implements 1:1 model netback. NAPI and kthread are utilized to do the weight-lifting job:

- NAPI is used for guest side TX (host side RX)
- kthread is used for guest side RX (host side TX)

Should this be split into two patches?

David

author
#13 lore raw
Quoted 9 lines · David Vrabel
Quoted 5 lines · Wei Liu

This patch implements 1:1 model netback. NAPI and kthread are utilized to do the weight-lifting job:

- NAPI is used for guest side TX (host side RX)
- kthread is used for guest side RX (host side TX)

Should this be split into two patches?

In fact the original model uses kthread for both TX and RX. This patch just splits some functionality to NAPI. So I don't think this needs to be two patches.

Wei.

Quoted 1 line

David

#14 lore raw
In reply to Wei Liu
Quoted 14 lines · Wei Liu

* This is a xen-devel only post, since we have not reached concesus on what to add / remove in this new model. This series tries to be conservative about adding in new feature compared to V1.

This series implements NAPI + kthread 1:1 model for Xen netback.

This model
- provides better scheduling fairness among vifs
- is prerequisite for implementing multiqueue for Xen network driver

The first two patches are ground work for the third patch. First one simplifies code in netback, second one can reduce memory footprint if we switch to 1:1 model.

Quoted 8 lines

The third patch has the real meat:
- make use of NAPI to mitigate interrupt
- kthreads are not bound to CPUs any more, so that we can take advantage of backend scheduler and trust it to do the right thing

Change since V1:
- No page pool in this version. Instead page tracking facility is removed.

What is your thought about page pool in V1? will you re-post it later on?

Thanks
Annie

Quoted 11 lines

Wei Liu (3):
xen-netback: remove page tracking facility
xen-netback: switch to per-cpu scratch space
xen-netback: switch to NAPI + kthread 1:1 model

  drivers/net/xen-netback/common.h    |   92 ++--
  drivers/net/xen-netback/interface.c |  122 +++--
  drivers/net/xen-netback/netback.c   |  959 +++++++++++++++--------------------
  3 files changed, 537 insertions(+), 636 deletions(-)
author
#15 lore raw
Quoted 21 lines · annie li
Quoted 19 lines · Wei Liu

* This is a xen-devel only post, since we have not reached concesus on what to add / remove in this new model. This series tries to be conservative about adding in new feature compared to V1.

This series implements NAPI + kthread 1:1 model for Xen netback.

This model
- provides better scheduling fairness among vifs
- is prerequisite for implementing multiqueue for Xen network driver

The first two patches are ground work for the third patch. First one simplifies code in netback, second one can reduce memory footprint if we switch to 1:1 model.

The third patch has the real meat:
- make use of NAPI to mitigate interrupt
- kthreads are not bound to CPUs any more, so that we can take advantage of backend scheduler and trust it to do the right thing

Quoted 6 lines
Quoted 3 lines

Change since V1:
- No page pool in this version. Instead page tracking facility is removed.

What is your thought about page pool in V1? will you re-post it later on?

That would be necessary if we introduce mapping in the future. It's sort of redundant at the moment with the copying scheme.

Wei.

#16 lore raw
In reply to Wei Liu
Quoted 14 lines · Wei Liu

* This is a xen-devel only post, since we have not reached concesus on what to add / remove in this new model. This series tries to be conservative about adding in new feature compared to V1.

This series implements NAPI + kthread 1:1 model for Xen netback.

This model
- provides better scheduling fairness among vifs
- is prerequisite for implementing multiqueue for Xen network driver

The first two patches are ground work for the third patch. First one simplifies code in netback, second one can reduce memory footprint if we switch to 1:1 model.

Quoted 8 lines

The third patch has the real meat:
- make use of NAPI to mitigate interrupt
- kthreads are not bound to CPUs any more, so that we can take advantage of backend scheduler and trust it to do the right thing

Change since V1:
- No page pool in this version. Instead page tracking facility is removed.

Andrew Bennieston has done some performance measurements with (I think) the V1 series and it shows a significant decrease in performance of from-guest traffic even with only two VIFs.

Andrew will be able to comment more on this.

Andrew, can you also make available your results for others to review?

David

author
#17 lore raw
Quoted 24 lines · David Vrabel
Quoted 14 lines · Wei Liu

* This is a xen-devel only post, since we have not reached concesus on what to add / remove in this new model. This series tries to be conservative about adding in new feature compared to V1.

This series implements NAPI + kthread 1:1 model for Xen netback.

This model
- provides better scheduling fairness among vifs
- is prerequisite for implementing multiqueue for Xen network driver

The first two patches are ground work for the third patch. First one simplifies code in netback, second one can reduce memory footprint if we switch to 1:1 model.

Quoted 8 lines

The third patch has the real meat:
- make use of NAPI to mitigate interrupt
- kthreads are not bound to CPUs any more, so that we can take advantage of backend scheduler and trust it to do the right thing

Change since V1:
- No page pool in this version. Instead page tracking facility is removed.

Quoted 8 lines

Andrew Bennieston has done some performance measurements with (I think) the V1 series and it shows a significant decrease in performance of from-guest traffic even with only two VIFs.

Andrew will be able to comment more on this.

Andrew, can you also make available your results for others to review?

In my third series there is also simple performance figures attached. Andrew could you please have a look at that as well?

If you have time, could you try my third series? In the third series, the only possible performance impact is the new model, which should narrow the problem down.

Wei.

Quoted 1 line

David

#18 lore raw
Quoted 25 lines · Wei Liu
Quoted 24 lines · David Vrabel
Quoted 22 lines · Wei Liu

* This is a xen-devel only post, since we have not reached concesus on what to add / remove in this new model. This series tries to be conservative about adding in new feature compared to V1.

This series implements NAPI + kthread 1:1 model for Xen netback.

This model
- provides better scheduling fairness among vifs
- is prerequisite for implementing multiqueue for Xen network driver

The first two patches are ground work for the third patch. First one simplifies code in netback, second one can reduce memory footprint if we switch to 1:1 model.

The third patch has the real meat:
- make use of NAPI to mitigate interrupt
- kthreads are not bound to CPUs any more, so that we can take advantage of backend scheduler and trust it to do the right thing

Change since V1:
- No page pool in this version. Instead page tracking facility is removed.

Quoted 7 lines
Quoted 7 lines

Andrew Bennieston has done some performance measurements with (I think) the V1 series and it shows a significant decrease in performance of from-guest traffic even with only two VIFs.

Andrew will be able to comment more on this.

Andrew, can you also make available your results for others to review?

Absolutely; there is now a page at http://wiki.xenproject.org/wiki/Xen-netback_NAPI_%2B_kThread_V1_performance_testing detailing the tests I performed and the results I saw, along with some summary text from my analysis.

Note that I also performed these tests without manually distributing IRQs across cores, and the performance was, as expected, rather poor. I didn't include those plots on the Wiki page since they don't really provide any new information.

Quoted 2 lines

In my third series there is also simple performance figures attached. Andrew could you please have a look at that as well?

I had a look at those; I think they agree with my tests where there is overlap. The tests I performed were repeated a number of times and covered a broader range of scenarios and have associated error bars which provide a measure of variability between tests (as well as indicating the statistical significance of differences between tests).

The error bars can also be interpreted in terms of fairness; smaller error bars mean that all TCP streams across all VIFs attain similar throughput to each other. Larger error bars mean that there is quite a lot of variation from one stream to another, e.g. as a stream or VIF may be starved of resources.

Quoted 4 lines

If you have time, could you try my third series? In the third series, the only possible performance impact is the new model, which should narrow the problem down.
Wei.

I am going to test the V3 patches as soon as I get the time; hopefully later this week, or early next week. I'll post the results once I have them.

Andrew.

author
#19 lore raw
Quoted 8 lines · Andrew Bennieston
Quoted 3 lines
Quoted 3 lines

Andrew will be able to comment more on this.

Andrew, can you also make available your results for others to review?

Absolutely; there is now a page at http://wiki.xenproject.org/wiki/Xen-netback_NAPI_%2B_kThread_V1_performance_testing
detailing the tests I performed and the results I saw, along with
some summary text from my analysis.

Thanks Andrew! Nice plot and nice analysis.

Just one nit, the CPU curves are not very distinguishable. Would you mind not using dotted lines for your next graph? ;-)

Wei.

#20 lore raw
Quoted 10 lines · Wei Liu
Quoted 8 lines · Andrew Bennieston
Quoted 3 lines
Quoted 3 lines

Andrew will be able to comment more on this.

Andrew, can you also make available your results for others to review?

Absolutely; there is now a page at http://wiki.xenproject.org/wiki/Xen-netback_NAPI_%2B_kThread_V1_performance_testing
detailing the tests I performed and the results I saw, along with
some summary text from my analysis.

Quoted 8 lines

Thanks Andrew! Nice plot and nice analysis.

Just one nit, the CPU curves are not very distinguishable. Would you mind not using dotted lines for your next graph? ;-)

Wei.

The CPU curves are pretty much identical. Using solid lines obscures the interesting data somewhat... I'll see what I can do, though :)

Andrew

author
#21 lore raw
Quoted 13 lines · Andrew Bennieston
Quoted 12 lines · Wei Liu
Quoted 8 lines · Andrew Bennieston
Quoted 3 lines
Quoted 3 lines

Andrew will be able to comment more on this.

Andrew, can you also make available your results for others to review?

Absolutely; there is now a page at http://wiki.xenproject.org/wiki/Xen-netback_NAPI_%2B_kThread_V1_performance_testing
detailing the tests I performed and the results I saw, along with
some summary text from my analysis.

Thanks Andrew! Nice plot and nice analysis.

Quoted 9 lines
Quoted 6 lines

Just one nit, the CPU curves are not very distinguishable. Would you mind not using dotted lines for your next graph? ;-)

Wei.

The CPU curves are pretty much identical. Using solid lines obscures the interesting data somewhat... I'll see what I can do, though :)

Oh I see. That's why I can only see one CPU curve. :-)

Wei.

Quoted 1 line

Andrew

#22 lore raw
In reply to Wei Liu
Quoted 34 lines · Wei Liu
Quoted 24 lines · David Vrabel
Quoted 14 lines · Wei Liu

* This is a xen-devel only post, since we have not reached concesus on what to add / remove in this new model. This series tries to be conservative about adding in new feature compared to V1.

This series implements NAPI + kthread 1:1 model for Xen netback.

This model
- provides better scheduling fairness among vifs
- is prerequisite for implementing multiqueue for Xen network driver

The first two patches are ground work for the third patch. First one simplifies code in netback, second one can reduce memory footprint if we switch to 1:1 model.

Quoted 8 lines

The third patch has the real meat:
- make use of NAPI to mitigate interrupt
- kthreads are not bound to CPUs any more, so that we can take advantage of backend scheduler and trust it to do the right thing

Change since V1:
- No page pool in this version. Instead page tracking facility is removed.

Quoted 8 lines

Andrew Bennieston has done some performance measurements with (I think) the V1 series and it shows a significant decrease in performance of from-guest traffic even with only two VIFs.

Andrew will be able to comment more on this.

Andrew, can you also make available your results for others to review?

Quoted 6 lines

In my third series there is also simple performance figures attached. Andrew could you please have a look at that as well?

If you have time, could you try my third series? In the third series, the only possible performance impact is the new model, which should narrow the problem down.

Wei, I finally have the results from testing your V3 patches. They are available at:

http://wiki.xenproject.org/wiki/Xen-netback_NAPI_%2B_kThread_V3_performance_testing

This time, the base for the tests was linux-next, rather than v3.6.11 (mostly to reduce the effort in backporting patches) so the results can't be directly compared to the V1, but I still ran tests without, then with, your patches, so you should be able to see the direct effect of those patches.

The summary is that there is (as expected) no impact on the dom0 -> VM measurements, and the VM -> dom0 measurements are identical with and without the patches up to 4 concurrently transmitting VMs or so, after which the original version outperforms the patched version. The difference becomes less pronounced as the number of TCP streams is increased, though.

My conclusion from these results would be that your V3 patches have fairly minimal performance impact, although they should improve _fairness_ (due to the kthread per VIF) on the transmit (into VM) pathway, and simplify the handling of the receive (out of VM) scenario too.

In other news, it looks like the throughput in general has improved between 3.6 and -next :)

Cheers,
Andrew

author
#23 lore raw

On Wed, Jul 03, 2013 at 01:45:13PM +0100, Andrew Bennieston wrote:
[...]

Quoted 9 lines
Quoted 3 lines

If you have time, could you try my third series? In the third series, the only possible performance impact is the new model, which should narrow the problem down.

Wei, I finally have the results from testing your V3 patches. They are available at:

http://wiki.xenproject.org/wiki/Xen-netback_NAPI_%2B_kThread_V3_performance_testing

Thanks, Andrew.

Quoted 13 lines

This time, the base for the tests was linux-next, rather than v3.6.11 (mostly to reduce the effort in backporting patches) so the results can't be directly compared to the V1, but I still ran tests without, then with, your patches, so you should be able to see the direct effect of those patches.

The summary is that there is (as expected) no impact on the dom0 -> VM measurements, and the VM -> dom0 measurements are identical with and without the patches up to 4 concurrently transmitting VMs or so, after which the original version outperforms the patched version. The difference becomes less pronounced as the number of TCP streams is increased, though.

Quoted 6 lines

My conclusion from these results would be that your V3 patches have fairly minimal performance impact, although they should improve _fairness_ (due to the kthread per VIF) on the transmit (into VM) pathway, and simplify the handling of the receive (out of VM) scenario too.

I'm happy to know at least my patches don't have significant negative impact. :-)

Quoted 3 lines

In other news, it looks like the throughput in general has improved between 3.6 and -next :)

Agreed.

Wei.

Quoted 2 lines

Cheers,
Andrew