index 128028efda64..b6307337a3c7 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -723,10 +723,11 @@ static int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
 	pl->link = NULL;
 
 	err = update_effective_progs(cgrp, atype);
-	if (err)
-		goto cleanup;
-
-	/* now can actually delete it from this cgroup list */
+	/*
+	 * Proceed regardless of error. The link and/or prog will be freed
+	 * just after this function returns so just delete it from this
+	 * cgroup list and retry calling update_effective_progs again later.
+	 */
 	list_del(&pl->node);
 	kfree(pl);
 	if (list_empty(progs))
@@ -735,12 +736,11 @@ static int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
 	if (old_prog)
 		bpf_prog_put(old_prog);
 	static_branch_dec(&cgroup_bpf_enabled_key[atype]);
-	return 0;
 
-cleanup:
-	/* restore back prog or link */
-	pl->prog = old_prog;
-	pl->link = link;
+	/* In case of error call update_effective_progs again */
+	if (err)
+		err = update_effective_progs(cgrp, atype);
+
 	return err;
 }
 
@@ -881,6 +881,7 @@ static void bpf_cgroup_link_release(struct bpf_link *link)
 	struct bpf_cgroup_link *cg_link =
 		container_of(link, struct bpf_cgroup_link, link);
 	struct cgroup *cg;
+	int err;
 
 	/* link might have been auto-detached by dying cgroup already,
 	 * in that case our work is done here
@@ -896,8 +897,10 @@ static void bpf_cgroup_link_release(struct bpf_link *link)
 		return;
 	}
 
-	WARN_ON(__cgroup_bpf_detach(cg_link->cgroup, NULL, cg_link,
-				    cg_link->type));
+	err = __cgroup_bpf_detach(cg_link->cgroup, NULL, cg_link,
+				  cg_link->type);
+	if (err)
+		pr_warn("cgroup_bpf_detach() failed, err %d\n", err);
 
 	cg = cg_link->cgroup;
 	cg_link->cgroup = NULL;