When trying to enable Mellanox ConnectX3 VF for Hyper-V,
I got the below error without https://reviews.freebsd.org/D8868
mlx4_en: mlx4_core0: Port 1: failed reserving qp for TX ring
mlx4_en: mlx4_core0: Port 1: Failed to allocate NIC resources
and the related error handling has a bug, causing a kernel crash:
Fatal trap 12: page fault while in kernel mode
...
Stopped at sysctl_ctx_free+0x7c: movq 0x8(%rax), %rax
The cause is:
In mlx4_en_init_netdev() -> mlx4_en_alloc_resources(),
if mlx4_en_create_tx_ring() fails, mlx4_en_sysctl_stat() won't be called;
later mlx4_en_init_netdev() -> mlx4_en_destroy_netdev() ->
mlx4_en_free_resources() -> sysctl_ctx_free(&priv->stat_ctx) will
dereference the NULL stat_ctx, because sysctl_ctx_init(&priv->stat_ctx)
hasn't been called.
To fix the issue, let's move the sysctl_ctx_init() to an earlier
place.
BTW, we also move the init for priv->conf_ctx for consistency of
coding style, though it's not a must.