selftests/xsk: Destroy BPF resources only when ctx refcount drops to 0
authorMaciej Fijalkowski <maciej.fijalkowski@intel.com>
Wed, 29 Jun 2022 14:34:58 +0000 (16:34 +0200)
committerDaniel Borkmann <daniel@iogearbox.net>
Thu, 30 Jun 2022 20:50:10 +0000 (22:50 +0200)
commit39e940d4abfabb08b6937a315546b24d10be67e3
tree6d29536aab989e9d4d47ed7fc5b3fe235141f828
parent6d4c767c032b165cc290c51f4e82bc54b14b1cf1
selftests/xsk: Destroy BPF resources only when ctx refcount drops to 0

Currently, xsk_socket__delete frees BPF resources regardless of ctx
refcount. Xdpxceiver has a test to verify whether underlying BPF
resources would not be wiped out after closing XSK socket that was
bound to interface with other active sockets. From library's xsk part
perspective it also means that the internal xsk context is shared and
its refcount is bumped accordingly.

After a switch to loading XDP prog based on previously opened XSK
socket, mentioned xdpxceiver test fails with:

  not ok 16 [xdpxceiver.c:swap_xsk_resources:1334]: ERROR: 9/"Bad file descriptor

which means that in swap_xsk_resources(), xsk_socket__delete() released
xskmap which in turn caused a failure of xsk_socket__update_xskmap().

To fix this, when deleting socket, decrement ctx refcount before
releasing BPF resources and do so only when refcount dropped to 0 which
means there are no more active sockets for this ctx so BPF resources can
be freed safely.

Fixes: 2f6324a3937f ("libbpf: Support shared umems between queues and devices")
Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Magnus Karlsson <magnus.karlsson@intel.com>
Link: https://lore.kernel.org/bpf/20220629143458.934337-5-maciej.fijalkowski@intel.com
tools/testing/selftests/bpf/xsk.c