kni: fix possible alloc_q starvation when mbufs are exhausted
PATCH 1/1 kni: fix possible alloc_q starvation when mbufs are exhausted
In some scenarios, mbufs returned by rte_kni_rx_burst are not freed immediately. So kni_allocate_mbufs may be failed, but we don't know.
Even worse, when alloc_q is completely exhausted, kni_net_tx in rte_kni.ko will drop all tx packets. kni_allocate_mbufs is never called again, even if the mbufs are eventually freed.
In this patch, we always try to allocate mbufs for alloc_q.
Don't worry about alloc_q being allocated too many mbufs, in fact, the old logic will gradually fill up alloc_q.
Also, the cost of more calls to kni_allocate_mbufs should be acceptable.
--- lib/kni/rte_kni.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
lib/kni/rte_kni.c +3 -3
@@ -634,9 +634,9 @@ rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned int num) {unsigned int ret =kni_fifo_get (kni->tx_q, (void **)mbufs, num); -/* If buffers removed, allocate mbufs and then put them into alloc_q */ -if (ret) -kni_allocate_mbufs (kni); +/* Always try to allocate mbufs for alloc_q to avoid starvation when +* kni->pktmbuf_pool is exhausted. */ +kni_allocate_mbufs (kni);return ret; }
Signature · 1 line
2.25.1
PATCH v2 1/1 kni: fix possible alloc_q starvation when mbufs are exhausted
In some scenarios, mbufs returned by rte_kni_rx_burst are not freed immediately. So kni_allocate_mbufs may be failed, but we don't know.
Even worse, when alloc_q is completely exhausted, kni_net_tx in rte_kni.ko will drop all tx packets. kni_allocate_mbufs is never called again, even if the mbufs are eventually freed.
In this patch, we always try to allocate mbufs for alloc_q.
Don't worry about alloc_q being allocated too many mbufs, in fact, the old logic will gradually fill up alloc_q.
Also, the cost of more calls to kni_allocate_mbufs should be acceptable.
--- lib/kni/rte_kni.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
lib/kni/rte_kni.c +5 -3
@@ -634,9 +634,11 @@ rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned int num) {unsigned int ret =kni_fifo_get (kni->tx_q, (void **)mbufs, num); -/* If buffers removed, allocate mbufs and then put them into alloc_q */ -if (ret) -kni_allocate_mbufs (kni); +/* +* Always try to allocate mbufs for alloc_q to avoid starvation when +* kni->pktmbuf_pool is exhausted. +*/ +kni_allocate_mbufs (kni);return ret; }
Signature · 1 line
2.25.1
Quoted 13 lines · On Wed, 9 Nov 2022 14:04:34 +0800 Yangchao Zhou <[email protected]> wrote:
In some scenarios, mbufs returned by
rte_kni_rx_burstare not freed immediately. Sokni_allocate_mbufsmay be failed, but we don't know.Even worse, when
alloc_qis completely exhausted,kni_net_txin rte_kni.ko will drop all tx packets.kni_allocate_mbufsis never called again, even if the mbufs are eventually freed.In this patch, we always try to allocate mbufs for
alloc_q.Don't worry about
alloc_qbeing allocated too many mbufs, in fact, the old logic will gradually fill upalloc_q.
Also, the cost of more calls tokni_allocate_mbufsshould be acceptable.
Quoted 5 lines
Signed-off-by Yangchao Zhou <[email protected]>
Since fifo_get returning 0 (no buffers) is very common would this change impact performance.
If the problem is pool draining might be better to make the pool bigger.
Quoted 16 lines · Stephen Hemminger
Quoted 13 lines · On Wed, 9 Nov 2022 14:04:34 +0800 Yangchao Zhou <[email protected]> wrote:
In some scenarios, mbufs returned by
rte_kni_rx_burstare not freed immediately. Sokni_allocate_mbufsmay be failed, but we don't know.Even worse, when
alloc_qis completely exhausted,kni_net_txin rte_kni.ko will drop all tx packets.kni_allocate_mbufsis never called again, even if the mbufs are eventually freed.In this patch, we always try to allocate mbufs for
alloc_q.Don't worry about
alloc_qbeing allocated too many mbufs, in fact, the old logic will gradually fill upalloc_q.
Also, the cost of more calls tokni_allocate_mbufsshould be acceptable.
Quoted 9 lines
Quoted 5 lines
Signed-off-by Yangchao Zhou <[email protected]>Since
fifo_getreturning 0 (no buffers) is very common would this change impact performance.
It does add a little cost, but there is no extra mbuf allocation and deallocation.
Quoted 4 lines
If the problem is pool draining might be better to make the pool bigger.
Yes, using a larger pool can avoid this problem. But this may lead to resource wastage and full resource calculation is a challenge for developers as it involves to mempool caching mechanism, IP fragment cache, ARP cache, NIC txq, other transit queue, etc.
The mbuf allocation failure may also occur on many NIC drivers, but if the mbuf allocation fails, the mbuf is not taken out so that it can be recovered after a retry later.
KNI currently does not have such a takedown and recovery mechanism. It is also possible to consider implementing something similar to the NIC driver, but with more changes and other overheads.
Quoted 26 lines · Matt
On Thu, Nov 10, 2022 at 12:39 AM Stephen Hemminger <[email protected] <mailto:[email protected]>> wrote:
On Wed, 9 Nov 2022 14:04:34 +0800
Yangchao Zhou <[email protected] <mailto:[email protected]>> wrote:> In some scenarios, mbufs returned by
rte_kni_rx_burstare not freed > immediately. Sokni_allocate_mbufsmay be failed, but we don't know. >
> Even worse, whenalloc_qis completely exhausted,kni_net_txin > rte_kni.ko will drop all tx packets.kni_allocate_mbufsis never > called again, even if the mbufs are eventually freed.
>
> In this patch, we always try to allocate mbufs foralloc_q. >
> Don't worry aboutalloc_qbeing allocated too many mbufs, in fact, > the old logic will gradually fill upalloc_q.
> Also, the cost of more calls tokni_allocate_mbufsshould be acceptable.
>
> Fixes:3e12a98fe397("kni: optimize Rx burst")
> Cc: [email protected] <mailto:[email protected]>
> Cc: [email protected] <mailto:[email protected]>
>
Quoted 9 lines
> Signed-off-by: Yangchao Zhou <[email protected] <mailto:[email protected]>>
Since
fifo_getreturning 0 (no buffers) is very common would this change impact performance.It does add a little cost, but there is no extra mbuf allocation and deallocation.
It is common that 'rte_kni_rx_burst()' called in continuous loop, I expect to try to fill alloc_q in each call impacts performance, even no mbuf allocated at all.
Quoted 9 lines
If the problem is pool draining might be better to make the pool bigger.
Yes, using a larger pool can avoid this problem. But this may lead to resource wastage and full resource calculation is a challenge for developers as it involves to mempool caching mechanism, IP fragment cache, ARP cache, NIC txq, other transit queue, etc.
Quoted 6 lines
The mbuf allocation failure may also occur on many NIC drivers, but if the mbuf allocation fails, the mbuf is not taken out so that it can be recovered after a retry later.
KNI currently does not have such a takedown and recovery mechanism. It is also possible to consider implementing something similar to the NIC driver, but with more changes and other overheads.
I agree this can cause starvation and recovery mechanism is missing,
Following may be an option, but still concerned about performance impact and not sure about upstreaming it. Other solution can be to use larger pool as Stephen suggested.
rte_kni_rx_burst()
ret = kni_fifo_get() if (ret & kni_fifo_count(alloc_q) == 0) kni_allocate_mbufs()
PATCH v3 1/1 kni: fix possible alloc_q starvation when mbufs are exhausted
In reply to Yangchao ZhouIn some scenarios, mbufs returned by rte_kni_rx_burst are not freed immediately. So kni_allocate_mbufs may be failed, but we don't know.
Even worse, when alloc_q is completely exhausted, kni_net_tx in rte_kni.ko will drop all tx packets. kni_allocate_mbufs is never called again, even if the mbufs are eventually freed.
In this patch, we try to allocate mbufs for alloc_q when it is empty.
According to historical experience, the performance bottleneck of KNI is offen the usleep_range of kni thread in rte_kni.ko.
The check of kni_fifo_count is trivial and the cost should be acceptable.
--- lib/kni/rte_kni.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/kni/rte_kni.c +2 -2
@@ -634,8 +634,8 @@ rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned int num) {unsigned int ret =kni_fifo_get (kni->tx_q, (void **)mbufs, num); -/* If buffers removed, allocate mbufs and then put them into alloc_q */ -if (ret) +/* If buffers removed or alloc_q is empty, allocate mbufs and then put them into alloc_q */ +if (ret || (kni_fifo_count (kni->alloc_q) ==0 ))kni_allocate_mbufs (kni);return ret;
Signature · 1 line
2.25.1
Quoted 7 lines · Yangchao Zhou
In some scenarios, mbufs returned by
rte_kni_rx_burstare not freed immediately. Sokni_allocate_mbufsmay be failed, but we don't know.Even worse, when
alloc_qis completely exhausted,kni_net_txin rte_kni.ko will drop all tx packets.kni_allocate_mbufsis never called again, even if the mbufs are eventually freed.
Quoted 6 lines
In this patch, we try to allocate mbufs for
alloc_qwhen it is empty.According to historical experience, the performance bottleneck of KNI is offen the
usleep_rangeof kni thread in rte_kni.ko.
The check ofkni_fifo_countis trivial and the cost should be acceptable.
Hi Yangchao,
Are you observing any performance impact with this change in you use case?
Quoted 23 lines
Fixes 3e12a98fe397 ("kni: optimize Rx burst")Signed-off-by Yangchao Zhou <[email protected]>--- lib/kni/rte_kni.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)lib/kni/rte_kni.c +2
-2@@ -634,8 +634,8 @@ rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned int num) {unsigned int ret =kni_fifo_get (kni->tx_q, (void **)mbufs, num); -/* If buffers removed, allocate mbufs and then put them into alloc_q */ -if (ret) +/* If buffers removed or alloc_q is empty, allocate mbufs and then put them into alloc_q */ +if (ret || (kni_fifo_count (kni->alloc_q) ==0 ))kni_allocate_mbufs (kni);return ret;
Hi Ferruh,
In my case, the traffic is not large, so I can't see the impact. I also tested under high load(>2Mpps with 2 DPDK cores and 2 kernel threads) and found no significant difference in performance either.
I think the reason should be that it will not
run to 'kni_fifo_count(kni->alloc_q) == 0' under high load.
Quoted 48 lines · Ferruh Yigit
Quoted 7 lines · Yangchao Zhou
In some scenarios, mbufs returned by
rte_kni_rx_burstare not freed immediately. Sokni_allocate_mbufsmay be failed, but we don't know.Even worse, when
alloc_qis completely exhausted,kni_net_txin rte_kni.ko will drop all tx packets.kni_allocate_mbufsis never called again, even if the mbufs are eventually freed.Quoted 6 lines
In this patch, we try to allocate mbufs for
alloc_qwhen it is empty.According to historical experience, the performance bottleneck of KNI is offen the
usleep_rangeof kni thread in rte_kni.ko.
The check ofkni_fifo_countis trivial and the cost should be acceptable.Hi Yangchao,
Are you observing any performance impact with this change in you use case?
lib/kni/rte_kni.c rte_kni_rx_burst(struct rte_kni *kni, struct
Quoted 13 lines
Fixes 3e12a98fe397 ("kni: optimize Rx burst")Signed-off-by Yangchao Zhou <[email protected]>--- lib/kni/rte_kni.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)lib/kni/rte_kni.c +0
-0@@ -634,8 +634,8 @@ rte_kni_rx_burst(struct rte_kni *kni, struct
rte_mbuf**mbufs, unsigned int num)lib/kni/rte_kni.c rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned int num)
Quoted 4 lines
lib/kni/rte_kni.c +0
-1{unsigned int ret =kni_fifo_get (kni->tx_q, (void **)mbufs, num); -/* If buffers removed, allocate mbufs and then put them into
alloc_q*/lib/kni/rte_kni.c rte_kni_rx_burst(struct rte_kni *kni, struct · line 637
Quoted 2 lines
lib/kni/rte_kni.c +1
-1-if (ret) +/* If buffers removed or alloc_q is empty, allocate mbufs and then put them into
alloc_q*/Quoted 4 lines
lib/kni/rte_kni.c +1
-0+if (ret || (kni_fifo_count (kni->alloc_q) ==0 ))kni_allocate_mbufs (kni);return ret;
Ferruh Yigit
Acked-byQuoted 8 lines · Matt
Hi Ferruh,
In my case, the traffic is not large, so I can't see the impact. I also tested under high load(>2Mpps with 2 DPDK cores and 2 kernel threads) and found no significant difference in performance either.
I think the reason should be that it will not
run to 'kni_fifo_count(kni->alloc_q)== 0' under high load.
I agree, additional check most likely hit on the low bandwidth, thanks for checking for performance impact.
Quoted 25 lines
On Tue, Jan 3, 2023 at 8:47 PM Ferruh Yigit <[email protected] <mailto:[email protected]>> wrote:
On 12/30/2022 4:23 AM, Yangchao Zhou wrote:
> In some scenarios, mbufs returned byrte_kni_rx_burstare not freed > immediately. Sokni_allocate_mbufsmay be failed, but we don't know. >
> Even worse, whenalloc_qis completely exhausted,kni_net_txin > rte_kni.ko will drop all tx packets.kni_allocate_mbufsis never > called again, even if the mbufs are eventually freed.
>
> In this patch, we try to allocate mbufs foralloc_qwhen it is empty. >
> According to historical experience, the performance bottleneck of KNI > is offen theusleep_rangeof kni thread in rte_kni.ko.
> The check ofkni_fifo_countis trivial and the cost should be acceptable.
>Hi Yangchao,
Are you observing any performance impact with this change in you use case?
Quoted 5 lines
> Fixes:
3e12a98fe397("kni: optimize Rx burst") > Cc: [email protected] <mailto:[email protected]> >
> Signed-off-by: Yangchao Zhou <[email protected] <mailto:[email protected]>>
Thomas Monjalon
Applied04/01/2023 15:34, Ferruh Yigit:
Quoted 38 lines · Matt
Quoted 8 lines
Hi Ferruh,
In my case, the traffic is not large, so I can't see the impact. I also tested under high load(>2Mpps with 2 DPDK cores and 2 kernel threads) and found no significant difference in performance either.
I think the reason should be that it will not
run to 'kni_fifo_count(kni->alloc_q)== 0' under high load.I agree, additional check most likely hit on the low bandwidth, thanks for checking for performance impact.
Quoted 25 lines
On Tue, Jan 3, 2023 at 8:47 PM Ferruh Yigit <[email protected] <mailto:[email protected]>> wrote:
On 12/30/2022 4:23 AM, Yangchao Zhou wrote:
> In some scenarios, mbufs returned byrte_kni_rx_burstare not freed > immediately. Sokni_allocate_mbufsmay be failed, but we don't know. >
> Even worse, whenalloc_qis completely exhausted,kni_net_txin > rte_kni.ko will drop all tx packets.kni_allocate_mbufsis never > called again, even if the mbufs are eventually freed.
>
> In this patch, we try to allocate mbufs foralloc_qwhen it is empty. >
> According to historical experience, the performance bottleneck of KNI > is offen theusleep_rangeof kni thread in rte_kni.ko.
> The check ofkni_fifo_countis trivial and the cost should be acceptable.
>Hi Yangchao,
Are you observing any performance impact with this change in you use case?
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Fixes 3e12a98fe397 ("kni: optimize Rx burst")Cc [email protected] <mailto:[email protected]>Signed-off-by Yangchao Zhou <[email protected]<mailto:[email protected]>>
Acked-by Ferruh Yigit <[email protected]>
Applied, thanks.