if (max_encap_size < ipv4_encap_size) {
                mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
                               ipv4_encap_size, max_encap_size);
-               return -EOPNOTSUPP;
+               err = -EOPNOTSUPP;
+               goto out;
        }
 
        encap_header = kzalloc(ipv4_encap_size, GFP_KERNEL);
-       if (!encap_header)
-               return -ENOMEM;
+       if (!encap_header) {
+               err = -ENOMEM;
+               goto out;
+       }
 
        /* used by mlx5e_detach_encap to lookup a neigh hash table
         * entry in the neigh hash table when a user deletes a rule
        if (max_encap_size < ipv6_encap_size) {
                mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
                               ipv6_encap_size, max_encap_size);
-               return -EOPNOTSUPP;
+               err = -EOPNOTSUPP;
+               goto out;
        }
 
        encap_header = kzalloc(ipv6_encap_size, GFP_KERNEL);
-       if (!encap_header)
-               return -ENOMEM;
+       if (!encap_header) {
+               err = -ENOMEM;
+               goto out;
+       }
 
        /* used by mlx5e_detach_encap to lookup a neigh hash table
         * entry in the neigh hash table when a user deletes a rule