Skip to content

Commit 8022ccd

Browse files
bastien-curutchetKernel Patches Daemon
authored andcommitted
selftests/bpf: test_xsk: Fix memory leaks
Some tests introduce memory leaks by not freeing all the pkt_stream objects they're creating. Fix these memory leaks. Signed-off-by: Bastien Curutchet (eBPF Foundation) <[email protected]>
1 parent fe6e9a7 commit 8022ccd

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

tools/testing/selftests/bpf/test_xsk.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,13 @@ static void pkt_stream_receive_half(struct test_spec *test)
546546
struct pkt_stream *pkt_stream = test->ifobj_tx->xsk->pkt_stream;
547547
u32 i;
548548

549+
if (test->ifobj_rx->xsk->pkt_stream != test->rx_pkt_stream_default)
550+
/* Packet stream has already been replaced so we have to release this one.
551+
* The newly created one will be freed by the restore_default() at the
552+
* end of the test
553+
*/
554+
pkt_stream_delete(test->ifobj_rx->xsk->pkt_stream);
555+
549556
test->ifobj_rx->xsk->pkt_stream = pkt_stream_generate(pkt_stream->nb_pkts,
550557
pkt_stream->pkts[0].len);
551558
pkt_stream = test->ifobj_rx->xsk->pkt_stream;
@@ -573,6 +580,22 @@ static void pkt_stream_even_odd_sequence(struct test_spec *test)
573580
}
574581
}
575582

583+
static void release_even_odd_sequence(struct test_spec *test)
584+
{
585+
struct pkt_stream *later_free_tx = test->ifobj_tx->xsk->pkt_stream;
586+
struct pkt_stream *later_free_rx = test->ifobj_rx->xsk->pkt_stream;
587+
int i;
588+
589+
for (i = 0; i < test->nb_sockets; i++) {
590+
/* later_free_{rx/tx} will be freed by restore_default() */
591+
if (test->ifobj_tx->xsk_arr[i].pkt_stream != later_free_tx)
592+
pkt_stream_delete(test->ifobj_tx->xsk_arr[i].pkt_stream);
593+
if (test->ifobj_rx->xsk_arr[i].pkt_stream != later_free_rx)
594+
pkt_stream_delete(test->ifobj_rx->xsk_arr[i].pkt_stream);
595+
}
596+
597+
}
598+
576599
static u64 pkt_get_addr(struct pkt *pkt, struct xsk_umem_info *umem)
577600
{
578601
if (!pkt->valid)
@@ -1874,6 +1897,7 @@ int testapp_stats_tx_invalid_descs(struct test_spec *test)
18741897
int testapp_stats_rx_full(struct test_spec *test)
18751898
{
18761899
pkt_stream_replace(test, DEFAULT_UMEM_BUFFERS + DEFAULT_UMEM_BUFFERS / 2, MIN_PKT_SIZE);
1900+
pkt_stream_delete(test->ifobj_rx->xsk->pkt_stream);
18771901
test->ifobj_rx->xsk->pkt_stream = pkt_stream_generate(DEFAULT_UMEM_BUFFERS, MIN_PKT_SIZE);
18781902

18791903
test->ifobj_rx->xsk->rxqsize = DEFAULT_UMEM_BUFFERS;
@@ -1885,6 +1909,7 @@ int testapp_stats_rx_full(struct test_spec *test)
18851909
int testapp_stats_fill_empty(struct test_spec *test)
18861910
{
18871911
pkt_stream_replace(test, DEFAULT_UMEM_BUFFERS + DEFAULT_UMEM_BUFFERS / 2, MIN_PKT_SIZE);
1912+
pkt_stream_delete(test->ifobj_rx->xsk->pkt_stream);
18881913
test->ifobj_rx->xsk->pkt_stream = pkt_stream_generate(DEFAULT_UMEM_BUFFERS, MIN_PKT_SIZE);
18891914

18901915
test->ifobj_rx->use_fill_ring = false;
@@ -2043,6 +2068,7 @@ int testapp_xdp_shared_umem(struct test_spec *test)
20432068
{
20442069
struct xsk_xdp_progs *skel_rx = test->ifobj_rx->xdp_progs;
20452070
struct xsk_xdp_progs *skel_tx = test->ifobj_tx->xdp_progs;
2071+
int ret;
20462072

20472073
test->total_steps = 1;
20482074
test->nb_sockets = 2;
@@ -2053,7 +2079,11 @@ int testapp_xdp_shared_umem(struct test_spec *test)
20532079

20542080
pkt_stream_even_odd_sequence(test);
20552081

2056-
return testapp_validate_traffic(test);
2082+
ret = testapp_validate_traffic(test);
2083+
2084+
release_even_odd_sequence(test);
2085+
2086+
return ret;
20572087
}
20582088

20592089
int testapp_poll_txq_tmout(struct test_spec *test)

0 commit comments

Comments
 (0)