From patchwork Fri Apr 5 16:29:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13619261 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9A98716F27B; Fri, 5 Apr 2024 16:29:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712334578; cv=none; b=VsuLjSVaefDCdy64okjlGf5haBI01BzK9sn3TZcoiZswvos5ZEalqF2OVgZV1btBr3n1FLu5YDKug6EaGin4xWpH+kj6QCJQRXsveTdpuuxAAdPHjW3Fz+8evfOV2n4rGdji7nl1Rf3V1lVMa+UBxMd1p3nLjQgQLmuTdJZqotg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712334578; c=relaxed/simple; bh=KihqcYDrq7c/A9UdGLxFUePTlzZ5bk4ZzDlVQv1u4hQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UXeJeJb2O+UHzlhH6UuvX7xXLQ1kOC9cKN1L/whzL+qPLBGec5O/2p0o7Cz1g4X9snyV33ABcYUHlRbJX2N/3H7vXfO9KnWiS6GoqMdeOdEoyeyi12/aSabRg2+H0YrngnRaeJy6BTfaYNbke1cUOEWGBUakw+GgvQsCErVZxjg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ET+tglv6; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ET+tglv6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 156CFC433F1; Fri, 5 Apr 2024 16:29:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712334578; bh=KihqcYDrq7c/A9UdGLxFUePTlzZ5bk4ZzDlVQv1u4hQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ET+tglv6ZpdQ20prrQhOxrNnaJbTbGKeRFtRh54QbnofpKo3eG8TuHp73MG4ONZvt XfV85v294NkWnwGeqiEKo3985kxNziWG//Lo5YSMs1BWzm7kHimxFfQSGSXfn4R/+j vi1SXDDD9C+AwsMTqiCv6HD2LrCZEdpgm2odcDKKkN2CNVUKb17nsWsXPxEUCUr8GV JvK3osnf3J/Miytcb3cDEqVZ5LVCgwib9oM6RxeOK5xTeVL2jSpBtHxCMIwqlGMPeS EDQkw71X9/9GE6Zs2aTqlYvojtja5AksLfiuzbNmY7mJfgJDc6MvHXRdbwhvgO+xhy jP97t7/Kpc8EA== From: Andrii Nakryiko To: stable@vger.kernel.org Cc: bpf@vger.kernel.org, Andrii Nakryiko , Alexei Starovoitov Subject: [PATCH 6.8.y 1/2] bpf: put uprobe link's path and task in release callback Date: Fri, 5 Apr 2024 09:29:31 -0700 Message-ID: <20240405162932.4047185-1-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <2024040527-propeller-immovably-a6d8@gregkh> References: <2024040527-propeller-immovably-a6d8@gregkh> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net There is no need to delay putting either path or task to deallocation step. It can be done right after bpf_uprobe_unregister. Between release and dealloc, there could be still some running BPF programs, but they don't access either task or path, only data in link->uprobes, so it is safe to do. On the other hand, doing path_put() in dealloc callback makes this dealloc sleepable because path_put() itself might sleep. Which is problematic due to the need to call uprobe's dealloc through call_rcu(), which is what is done in the next bug fix patch. So solve the problem by releasing these resources early. Signed-off-by: Andrii Nakryiko Link: https://lore.kernel.org/r/20240328052426.3042617-1-andrii@kernel.org Signed-off-by: Alexei Starovoitov (cherry picked from commit e9c856cabefb71d47b2eeb197f72c9c88e9b45b0) --- kernel/trace/bpf_trace.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 7ac6c52b25eb..45de8a4923e2 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3142,6 +3142,9 @@ static void bpf_uprobe_multi_link_release(struct bpf_link *link) umulti_link = container_of(link, struct bpf_uprobe_multi_link, link); bpf_uprobe_unregister(&umulti_link->path, umulti_link->uprobes, umulti_link->cnt); + if (umulti_link->task) + put_task_struct(umulti_link->task); + path_put(&umulti_link->path); } static void bpf_uprobe_multi_link_dealloc(struct bpf_link *link) @@ -3149,9 +3152,6 @@ static void bpf_uprobe_multi_link_dealloc(struct bpf_link *link) struct bpf_uprobe_multi_link *umulti_link; umulti_link = container_of(link, struct bpf_uprobe_multi_link, link); - if (umulti_link->task) - put_task_struct(umulti_link->task); - path_put(&umulti_link->path); kvfree(umulti_link->uprobes); kfree(umulti_link); } From patchwork Fri Apr 5 16:29:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13619262 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D6C7B16F90A; Fri, 5 Apr 2024 16:29:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712334581; cv=none; b=YNXxwbg/hxiiWyRtC6acQE6BL7FKsEjE17kskAmzHOI0z3rUNVFlRjNMePzwdrki/yvLL/QzFGv2GLzOtyWaPOYC5d9jfJ4J0mbZga0h0fagVq68GOnyoV+xbcBPZhhJ1DSUNJfZd9ChB1q0Gci5p2/uNwNHpnOk7/IthLwnIUY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712334581; c=relaxed/simple; bh=3UHMly7IXkqF/Ezrys5TDn66gkvfFw7IQ3luVUe8fgU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D709uQXOLa7XQxcniEwb/HvmbuHwYNdxm5T+It90DH8LUBzfGkQKg63uUGci5V99PExIM4GgcIMbIPmcTTrIbt++/RRwrYJudfSqtJWvbCF3ti4Xtz2x0pCERoh5k2UkkBoeSCRIZjIbvurSHECNP9CnxC2IVXYS5ml3DjzRNC0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kNMd6Psj; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kNMd6Psj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 438C0C433C7; Fri, 5 Apr 2024 16:29:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712334581; bh=3UHMly7IXkqF/Ezrys5TDn66gkvfFw7IQ3luVUe8fgU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kNMd6Psj6nqehRIjC1eFHEcvsyBwW3suYtakW5Mik6zeJEBgLCztplXo68OtCUAeH V4aR1cI4ewwN/RZYU7G7JfpNzpteTM4h9BkMyAodYxHCwbWvhydEsnt/gwRA2vkHkn ANQnDTmKOcIdVof4yFf5XwjrM/q7k6BWg2ibONyExXdfZwKikRkN3YgFEuBEK5CBKV 0O31BdGVxA1XnhXpxSGo3gF3bhK7ggoI82CRRhpZ4IqN/qFLe94UFexETRNcE3AEc0 44DWeXPDaBWOvFFAK+5ubyh/swiIXVtDEMVoHZXXGcWuBb9REZ6gM/F9E/NdnlW9Uu kHiTfdBg+072A== From: Andrii Nakryiko To: stable@vger.kernel.org Cc: bpf@vger.kernel.org, Andrii Nakryiko , Alexei Starovoitov , syzbot+981935d9485a560bfbcb@syzkaller.appspotmail.com, syzbot+2cb5a6c573e98db598cc@syzkaller.appspotmail.com, syzbot+62d8b26793e8a2bd0516@syzkaller.appspotmail.com, Jiri Olsa Subject: [PATCH 6.8.y 2/2] bpf: support deferring bpf_link dealloc to after RCU grace period Date: Fri, 5 Apr 2024 09:29:32 -0700 Message-ID: <20240405162932.4047185-2-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240405162932.4047185-1-andrii@kernel.org> References: <2024040527-propeller-immovably-a6d8@gregkh> <20240405162932.4047185-1-andrii@kernel.org> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net BPF link for some program types is passed as a "context" which can be used by those BPF programs to look up additional information. E.g., for multi-kprobes and multi-uprobes, link is used to fetch BPF cookie values. Because of this runtime dependency, when bpf_link refcnt drops to zero there could still be active BPF programs running accessing link data. This patch adds generic support to defer bpf_link dealloc callback to after RCU GP, if requested. This is done by exposing two different deallocation callbacks, one synchronous and one deferred. If deferred one is provided, bpf_link_free() will schedule dealloc_deferred() callback to happen after RCU GP. BPF is using two flavors of RCU: "classic" non-sleepable one and RCU tasks trace one. The latter is used when sleepable BPF programs are used. bpf_link_free() accommodates that by checking underlying BPF program's sleepable flag, and goes either through normal RCU GP only for non-sleepable, or through RCU tasks trace GP *and* then normal RCU GP (taking into account rcu_trace_implies_rcu_gp() optimization), if BPF program is sleepable. We use this for multi-kprobe and multi-uprobe links, which dereference link during program run. We also preventively switch raw_tp link to use deferred dealloc callback, as upcoming changes in bpf-next tree expose raw_tp link data (specifically, cookie value) to BPF program at runtime as well. Fixes: 0dcac2725406 ("bpf: Add multi kprobe link") Fixes: 89ae89f53d20 ("bpf: Add multi uprobe link") Reported-by: syzbot+981935d9485a560bfbcb@syzkaller.appspotmail.com Reported-by: syzbot+2cb5a6c573e98db598cc@syzkaller.appspotmail.com Reported-by: syzbot+62d8b26793e8a2bd0516@syzkaller.appspotmail.com Signed-off-by: Andrii Nakryiko Acked-by: Jiri Olsa Link: https://lore.kernel.org/r/20240328052426.3042617-2-andrii@kernel.org Signed-off-by: Alexei Starovoitov (cherry picked from commit 1a80dbcb2dbaf6e4c216e62e30fa7d3daa8001ce) --- include/linux/bpf.h | 16 +++++++++++++++- kernel/bpf/syscall.c | 35 ++++++++++++++++++++++++++++++++--- kernel/trace/bpf_trace.c | 4 ++-- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index e30100597d0a..7d719976b564 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1568,12 +1568,26 @@ struct bpf_link { enum bpf_link_type type; const struct bpf_link_ops *ops; struct bpf_prog *prog; - struct work_struct work; + /* rcu is used before freeing, work can be used to schedule that + * RCU-based freeing before that, so they never overlap + */ + union { + struct rcu_head rcu; + struct work_struct work; + }; }; struct bpf_link_ops { void (*release)(struct bpf_link *link); + /* deallocate link resources callback, called without RCU grace period + * waiting + */ void (*dealloc)(struct bpf_link *link); + /* deallocate link resources callback, called after RCU grace period; + * if underlying BPF program is sleepable we go through tasks trace + * RCU GP and then "classic" RCU GP + */ + void (*dealloc_deferred)(struct bpf_link *link); int (*detach)(struct bpf_link *link); int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog, struct bpf_prog *old_prog); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index a1f18681721c..0f90b6b27430 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2895,17 +2895,46 @@ void bpf_link_inc(struct bpf_link *link) atomic64_inc(&link->refcnt); } +static void bpf_link_defer_dealloc_rcu_gp(struct rcu_head *rcu) +{ + struct bpf_link *link = container_of(rcu, struct bpf_link, rcu); + + /* free bpf_link and its containing memory */ + link->ops->dealloc_deferred(link); +} + +static void bpf_link_defer_dealloc_mult_rcu_gp(struct rcu_head *rcu) +{ + if (rcu_trace_implies_rcu_gp()) + bpf_link_defer_dealloc_rcu_gp(rcu); + else + call_rcu(rcu, bpf_link_defer_dealloc_rcu_gp); +} + /* bpf_link_free is guaranteed to be called from process context */ static void bpf_link_free(struct bpf_link *link) { + bool sleepable = false; + bpf_link_free_id(link->id); if (link->prog) { + sleepable = link->prog->aux->sleepable; /* detach BPF program, clean up used resources */ link->ops->release(link); bpf_prog_put(link->prog); } - /* free bpf_link and its containing memory */ - link->ops->dealloc(link); + if (link->ops->dealloc_deferred) { + /* schedule BPF link deallocation; if underlying BPF program + * is sleepable, we need to first wait for RCU tasks trace + * sync, then go through "classic" RCU grace period + */ + if (sleepable) + call_rcu_tasks_trace(&link->rcu, bpf_link_defer_dealloc_mult_rcu_gp); + else + call_rcu(&link->rcu, bpf_link_defer_dealloc_rcu_gp); + } + if (link->ops->dealloc) + link->ops->dealloc(link); } static void bpf_link_put_deferred(struct work_struct *work) @@ -3415,7 +3444,7 @@ static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link, static const struct bpf_link_ops bpf_raw_tp_link_lops = { .release = bpf_raw_tp_link_release, - .dealloc = bpf_raw_tp_link_dealloc, + .dealloc_deferred = bpf_raw_tp_link_dealloc, .show_fdinfo = bpf_raw_tp_link_show_fdinfo, .fill_link_info = bpf_raw_tp_link_fill_link_info, }; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 45de8a4923e2..c8d1ebc43838 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2713,7 +2713,7 @@ static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link, static const struct bpf_link_ops bpf_kprobe_multi_link_lops = { .release = bpf_kprobe_multi_link_release, - .dealloc = bpf_kprobe_multi_link_dealloc, + .dealloc_deferred = bpf_kprobe_multi_link_dealloc, .fill_link_info = bpf_kprobe_multi_link_fill_link_info, }; @@ -3227,7 +3227,7 @@ static int bpf_uprobe_multi_link_fill_link_info(const struct bpf_link *link, static const struct bpf_link_ops bpf_uprobe_multi_link_lops = { .release = bpf_uprobe_multi_link_release, - .dealloc = bpf_uprobe_multi_link_dealloc, + .dealloc_deferred = bpf_uprobe_multi_link_dealloc, .fill_link_info = bpf_uprobe_multi_link_fill_link_info, };