Skip to contentSkip to conversation

kni: fix possible rx_q mbuf leak and speed up alloc_q release

PATCH New Yangchao Zhou 3 participants 6 messages Apr 17, 2018 – Apr 19, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 kni: fix possible rx_q mbuf leak and speed up alloc_q release

Quoted 1 line

From 3293d014123150bcd0650ce1d09b19bbb9eae9c4 Mon Sep 17 00:00:00 2001

From: zhouyangchao <[email protected]>
Date: Tue, 10 Apr 2018 18:26:35 +0800
Subject: [PATCH] kni: fix possible rx_q mbuf leak and speed up alloc_q release

rx_q fifo can only be released by kernel thread. There may be mbuf leaks in rx_q because kernel threads are randomly stopped.

When the kni is released and netdev is unregisterd, convert the physical address mbufs in rx_q to the virtual address in free_q. By the way, alloc_q can be processed together to speed up the release rate in userspace.

---
 kernel/linux/kni/kni_dev.h  |  1 +
 kernel/linux/kni/kni_misc.c |  2 ++
 kernel/linux/kni/kni_net.c  | 40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+)
kernel/linux/kni/kni_dev.h +1 -0
index c9393d8..6275ef2 100644
@@ -92,6 +92,7 @@ struct kni_dev {
 	void *alloc_va[MBUF_BURST_SZ];
 };
 
+void kni_net_release_fifo_phy(struct kni_dev *kni);
 void kni_net_rx(struct kni_dev *kni);
 void kni_net_init(struct net_device *dev);
 void kni_net_config_lo_mode(char *lo_str);
kernel/linux/kni/kni_misc.c +2 -0
index 01574ec..fa69f8e 100644
@@ -192,6 +192,8 @@ kni_dev_remove(struct kni_dev *dev)
 		free_netdev(dev->net_dev);
 	}
 
+	kni_net_release_fifo_phy(dev);
+
 	return 0;
 }
 
kernel/linux/kni/kni_net.c +40 -0
index 9f9b798..1d64d78 100644
@@ -163,6 +163,46 @@ kni_net_release(struct net_device *dev)
 	return (ret == 0) ? req.result : ret;
 }
 
+static void
+kni_fifo_trans_pa2va(struct kni_dev *kni,
+	struct rte_kni_fifo *src_pa, struct rte_kni_fifo *dst_va)
+{
+	uint32_t ret, i, num_fq, num_rx;
+	void *kva;
+	do {
+		num_fq = kni_fifo_free_count(kni->free_q);
+		if (num_fq == 0)
+			return;
+
+		num_rx = min_t(uint32_t, num_fq, MBUF_BURST_SZ);
+
+		num_rx = kni_fifo_get(src_pa, kni->pa, num_rx);
+		if (num_rx == 0)
+			return;
+
+		for (i = 0; i < num_rx; i++) {
+			kva = pa2kva(kni->pa[i]);
+			kni->va[i] = pa2va(kni->pa[i], kva);
+		}
+
+		ret = kni_fifo_put(dst_va, kni->va, num_rx);
+		if (ret != num_rx) {
+			/* Failing should not happen */
+			pr_err("Fail to enqueue entries into dst_va\n");
+			return;
+		}
+	} while (1);
+}
+
+/* Try to release mbufs when kni release */
+void kni_net_release_fifo_phy(struct kni_dev *kni)
+{
+	/* release rx_q first, because it can't release in userspace */
+	kni_fifo_trans_pa2va(kni, kni->rx_q, kni->free_q);
+	/* release alloc_q for speeding up kni release in userspace */
+	kni_fifo_trans_pa2va(kni, kni->alloc_q, kni->free_q);
+}
+
 /*
  * Configuration changes (passed on by ifconfig)
  */
Signature · 1 line
2.9.0.windows.1
#1 lore raw

PATCH v3 1/1 kni: fix possible rx_q mbuf leaks and speed up alloc_q release

rx_q fifo can only be released by kernel thread. There may be mbuf leaks in rx_q because kernel threads are randomly stopped.

When the kni is released and netdev is unregisterd, convert the physical address mbufs in rx_q to the virtual address in free_q. By the way, alloc_q can be processed together to speed up the release rate in userspace.

---
 kernel/linux/kni/kni_dev.h  |    1 +
 kernel/linux/kni/kni_misc.c |    2 ++
 kernel/linux/kni/kni_net.c  |   40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 0 deletions(-)
kernel/linux/kni/kni_dev.h +1 -0
index c9393d8..6275ef2 100644
@@ -92,6 +92,7 @@ struct kni_dev {
 	void *alloc_va[MBUF_BURST_SZ];
 };
 
+void kni_net_release_fifo_phy(struct kni_dev *kni);
 void kni_net_rx(struct kni_dev *kni);
 void kni_net_init(struct net_device *dev);
 void kni_net_config_lo_mode(char *lo_str);
kernel/linux/kni/kni_misc.c +2 -0
index 01574ec..fa69f8e 100644
@@ -192,6 +192,8 @@ struct kni_net {
 		free_netdev(dev->net_dev);
 	}
 
+	kni_net_release_fifo_phy(dev);
+
 	return 0;
 }
 
kernel/linux/kni/kni_net.c +40 -0
index 9f9b798..1d64d78 100644
@@ -163,6 +163,46 @@
 	return (ret == 0) ? req.result : ret;
 }
 
+static void
+kni_fifo_trans_pa2va(struct kni_dev *kni,
+	struct rte_kni_fifo *src_pa, struct rte_kni_fifo *dst_va)
+{
+	uint32_t ret, i, num_fq, num_rx;
+	void *kva;
+	do {
+		num_fq = kni_fifo_free_count(kni->free_q);
+		if (num_fq == 0)
+			return;
+
+		num_rx = min_t(uint32_t, num_fq, MBUF_BURST_SZ);
+
+		num_rx = kni_fifo_get(src_pa, kni->pa, num_rx);
+		if (num_rx == 0)
+			return;
+
+		for (i = 0; i < num_rx; i++) {
+			kva = pa2kva(kni->pa[i]);
+			kni->va[i] = pa2va(kni->pa[i], kva);
+		}
+
+		ret = kni_fifo_put(dst_va, kni->va, num_rx);
+		if (ret != num_rx) {
+			/* Failing should not happen */
+			pr_err("Fail to enqueue entries into dst_va\n");
+			return;
+		}
+	} while (1);
+}
+
+/* Try to release mbufs when kni release */
+void kni_net_release_fifo_phy(struct kni_dev *kni)
+{
+	/* release rx_q first, because it can't release in userspace */
+	kni_fifo_trans_pa2va(kni, kni->rx_q, kni->free_q);
+	/* release alloc_q for speeding up kni release in userspace */
+	kni_fifo_trans_pa2va(kni, kni->alloc_q, kni->free_q);
+}
+
 /*
  * Configuration changes (passed on by ifconfig)
  */
Signature · 1 line
1.7.1
#2 lore raw

Re: [PATCH v3] kni: fix possible rx_q mbuf leaks and speed up alloc_q release

Quoted 7 lines · Yangchao Zhou

rx_q fifo can only be released by kernel thread. There may be mbuf leaks in rx_q because kernel threads are randomly stopped.

When the kni is released and netdev is unregisterd, convert the physical address mbufs in rx_q to the virtual address in free_q. By the way, alloc_q can be processed together to speed up the release rate in userspace.

Overall looks good to me. A few comments below.

Do you observe a speed up in userspace related alloc_q free?

Quoted 40 lines
---
 kernel/linux/kni/kni_dev.h  |    1 +
 kernel/linux/kni/kni_misc.c |    2 ++
 kernel/linux/kni/kni_net.c  |   40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 0 deletions(-)
kernel/linux/kni/kni_dev.h +1 -0
index c9393d8..6275ef2 100644
@@ -92,6 +92,7 @@ struct kni_dev {
 	void *alloc_va[MBUF_BURST_SZ];
 };
 
+void kni_net_release_fifo_phy(struct kni_dev *kni);
 void kni_net_rx(struct kni_dev *kni);
 void kni_net_init(struct net_device *dev);
 void kni_net_config_lo_mode(char *lo_str);
kernel/linux/kni/kni_misc.c +2 -0
index 01574ec..fa69f8e 100644
@@ -192,6 +192,8 @@ struct kni_net {
 		free_netdev(dev->net_dev);
 	}
 
+	kni_net_release_fifo_phy(dev);
+
 	return 0;
 }
 
kernel/linux/kni/kni_net.c +0 -0
index 9f9b798..1d64d78 100644
@@ -163,6 +163,46 @@
 	return (ret == 0) ? req.result : ret;

kernel/linux/kni/kni_net.c line 173

Quoted 10 lines
kernel/linux/kni/kni_net.c +8 -0
 }
 
+static void
+kni_fifo_trans_pa2va(struct kni_dev *kni,
+	struct rte_kni_fifo *src_pa, struct rte_kni_fifo *dst_va)
+{
+	uint32_t ret, i, num_fq, num_rx;
+	void *kva;
+	do {
+		num_fq = kni_fifo_free_count(kni->free_q);

Since this function made more generic with src, dst arguments, it is better to use dst_va here instead of kni->free_q

kernel/linux/kni/kni_net.c line 174

Quoted 1 line
kernel/linux/kni/kni_net.c +1 -0
+		if (num_fq == 0)

And this variable name can be updated from free queue to dst.

Quoted 35 lines
kernel/linux/kni/kni_net.c +31 -0
+			return;
+
+		num_rx = min_t(uint32_t, num_fq, MBUF_BURST_SZ);
+
+		num_rx = kni_fifo_get(src_pa, kni->pa, num_rx);
+		if (num_rx == 0)
+			return;
+
+		for (i = 0; i < num_rx; i++) {
+			kva = pa2kva(kni->pa[i]);
+			kni->va[i] = pa2va(kni->pa[i], kva);
+		}
+
+		ret = kni_fifo_put(dst_va, kni->va, num_rx);
+		if (ret != num_rx) {
+			/* Failing should not happen */
+			pr_err("Fail to enqueue entries into dst_va\n");
+			return;
+		}
+	} while (1);
+}
+
+/* Try to release mbufs when kni release */
+void kni_net_release_fifo_phy(struct kni_dev *kni)
+{
+	/* release rx_q first, because it can't release in userspace */
+	kni_fifo_trans_pa2va(kni, kni->rx_q, kni->free_q);
+	/* release alloc_q for speeding up kni release in userspace */
+	kni_fifo_trans_pa2va(kni, kni->alloc_q, kni->free_q);
+}
+
 /*
  * Configuration changes (passed on by ifconfig)
  */
#3 lore raw

PATCH v4 1/1 kni: fix possible rx_q mbuf leaks and speed up alloc_q release

rx_q fifo can only be released by kernel thread. There may be mbuf leaks in rx_q because kernel threads are randomly stopped.

When the kni is released and netdev is unregisterd, convert the physical address mbufs in rx_q to the virtual address in free_q. By the way, alloc_q can be processed together to speed up the release rate in userspace.

In my test, it is improved from 300-500ms with a mempool that has
131072 mbufs to 10ms(regardless of the specifications).

---
v4:
* Add improve performance description.

---
 kernel/linux/kni/kni_dev.h  |    1 +
 kernel/linux/kni/kni_misc.c |    2 ++
 kernel/linux/kni/kni_net.c  |   40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 0 deletions(-)
kernel/linux/kni/kni_dev.h +1 -0
index c9393d8..6275ef2 100644
@@ -92,6 +92,7 @@ struct kni_dev {
 	void *alloc_va[MBUF_BURST_SZ];
 };
 
+void kni_net_release_fifo_phy(struct kni_dev *kni);
 void kni_net_rx(struct kni_dev *kni);
 void kni_net_init(struct net_device *dev);
 void kni_net_config_lo_mode(char *lo_str);
kernel/linux/kni/kni_misc.c +2 -0
index 01574ec..fa69f8e 100644
@@ -192,6 +192,8 @@ struct kni_net {
 		free_netdev(dev->net_dev);
 	}
 
+	kni_net_release_fifo_phy(dev);
+
 	return 0;
 }
 
kernel/linux/kni/kni_net.c +40 -0
index 9f9b798..7fcfa10 100644
@@ -163,6 +163,46 @@
 	return (ret == 0) ? req.result : ret;
 }
 
+static void
+kni_fifo_trans_pa2va(struct kni_dev *kni,
+	struct rte_kni_fifo *src_pa, struct rte_kni_fifo *dst_va)
+{
+	uint32_t ret, i, num_dst, num_rx;
+	void *kva;
+	do {
+		num_dst = kni_fifo_free_count(dst_va);
+		if (num_dst == 0)
+			return;
+
+		num_rx = min_t(uint32_t, num_dst, MBUF_BURST_SZ);
+
+		num_rx = kni_fifo_get(src_pa, kni->pa, num_rx);
+		if (num_rx == 0)
+			return;
+
+		for (i = 0; i < num_rx; i++) {
+			kva = pa2kva(kni->pa[i]);
+			kni->va[i] = pa2va(kni->pa[i], kva);
+		}
+
+		ret = kni_fifo_put(dst_va, kni->va, num_rx);
+		if (ret != num_rx) {
+			/* Failing should not happen */
+			pr_err("Fail to enqueue entries into dst_va\n");
+			return;
+		}
+	} while (1);
+}
+
+/* Try to release mbufs when kni release */
+void kni_net_release_fifo_phy(struct kni_dev *kni)
+{
+	/* release rx_q first, because it can't release in userspace */
+	kni_fifo_trans_pa2va(kni, kni->rx_q, kni->free_q);
+	/* release alloc_q for speeding up kni release in userspace */
+	kni_fifo_trans_pa2va(kni, kni->alloc_q, kni->free_q);
+}
+
 /*
  * Configuration changes (passed on by ifconfig)
  */
Signature · 1 line
1.7.1
Acked-by on v4 1/1
#4 lore raw

Re: [PATCH v4] kni: fix possible rx_q mbuf leaks and speed up alloc_q release

Quoted 8 lines · Yangchao Zhou

rx_q fifo can only be released by kernel thread. There may be mbuf leaks in rx_q because kernel threads are randomly stopped.

When the kni is released and netdev is unregisterd, convert the physical address mbufs in rx_q to the virtual address in free_q. By the way, alloc_q can be processed together to speed up the release rate in userspace.

Quoted 8 lines

In my test, it is improved from 300-500ms with a mempool that has
131072 mbufs to 10ms(regardless of the specifications).

---
v4:
* Add improve performance description.

#5 lore raw

Re: [dpdk-stable] [PATCH v4] kni: fix possible rx_q mbuf leaks and speed up alloc_q release

04/05/2018 14:46, Ferruh Yigit:

Quoted 12 lines · Yangchao Zhou
Quoted 11 lines

rx_q fifo can only be released by kernel thread. There may be mbuf leaks in rx_q because kernel threads are randomly stopped.

When the kni is released and netdev is unregisterd, convert the physical address mbufs in rx_q to the virtual address in free_q. By the way, alloc_q can be processed together to speed up the release rate in userspace.

In my test, it is improved from 300-500ms with a mempool that has
131072 mbufs to 10ms(regardless of the specifications).

Quoted 7 lines
Quoted 5 lines

---
v4:
* Add improve performance description.

Applied, thanks