static int mlx5_mdev_init(struct mlx5_core_dev *dev, int profile_idx, const char *name)
 {
        struct mlx5_priv *priv = &dev->priv;
+       int err;
 
        strncpy(priv->name, name, MLX5_MAX_NAME_LEN);
        priv->name[MLX5_MAX_NAME_LEN - 1] = 0;
                return -ENOMEM;
        }
 
+       err = mlx5_health_init(dev);
+       if (err)
+               goto err_health_init;
+
+       err = mlx5_pagealloc_init(dev);
+       if (err)
+               goto err_pagealloc_init;
+
        return 0;
+
+err_pagealloc_init:
+       mlx5_health_cleanup(dev);
+err_health_init:
+       debugfs_remove(dev->priv.dbg_root);
+
+       return err;
 }
 
 static void mlx5_mdev_uninit(struct mlx5_core_dev *dev)
 {
+       mlx5_pagealloc_cleanup(dev);
+       mlx5_health_cleanup(dev);
        debugfs_remove_recursive(dev->priv.dbg_root);
 }
 
                goto pci_init_err;
        }
 
-       err = mlx5_health_init(dev);
-       if (err) {
-               dev_err(&pdev->dev, "mlx5_health_init failed with error code %d\n", err);
-               goto close_pci;
-       }
-
-       err = mlx5_pagealloc_init(dev);
-       if (err)
-               goto err_pagealloc_init;
-
        err = mlx5_load_one(dev, true);
        if (err) {
                dev_err(&pdev->dev, "mlx5_load_one failed with error code %d\n", err);
 
 clean_load:
        mlx5_unload_one(dev, true);
+
 err_load_one:
-       mlx5_pagealloc_cleanup(dev);
-err_pagealloc_init:
-       mlx5_health_cleanup(dev);
-close_pci:
        mlx5_pci_close(dev);
 pci_init_err:
        mlx5_mdev_uninit(dev);
 
        if (mlx5_unload_one(dev, true)) {
                dev_err(&dev->pdev->dev, "mlx5_unload_one failed\n");
-               mlx5_health_cleanup(dev);
+               mlx5_health_flush(dev);
                return;
        }
 
-       mlx5_pagealloc_cleanup(dev);
-       mlx5_health_cleanup(dev);
        mlx5_pci_close(dev);
        mlx5_mdev_uninit(dev);
        devlink_free(devlink);
 
 int mlx5_core_get_caps(struct mlx5_core_dev *dev, enum mlx5_cap_type cap_type);
 int mlx5_cmd_alloc_uar(struct mlx5_core_dev *dev, u32 *uarn);
 int mlx5_cmd_free_uar(struct mlx5_core_dev *dev, u32 uarn);
+void mlx5_health_flush(struct mlx5_core_dev *dev);
 void mlx5_health_cleanup(struct mlx5_core_dev *dev);
 int mlx5_health_init(struct mlx5_core_dev *dev);
 void mlx5_start_health_poll(struct mlx5_core_dev *dev);