From patchwork Wed Feb 7 14:07:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13548562 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 8C7857C09D; Wed, 7 Feb 2024 14:08:16 +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=1707314896; cv=none; b=tTowaZYU+WlZusDY5xh43SHTJo2I5pqIhsycNtZnczI1w3/TjFQAIDnoNh2etAyd8WZrAmRH9WJkuhN+fVbXcWmpz65xnM6R9ahY3Td+WrIvBcH6zKeZyqi2f4R8sKmw+X4yA6T7KOrJz+cCmk5C14g2GDht+kytqAZbTvn/Sf8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707314896; c=relaxed/simple; bh=xBKjgPQS/Jpq9Sq/sAc3h8Qc9T8zShFzRqNCb+1mBNc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CLEQDak0DTRPbmtHJbohzdA8TpEXo/l1tDXBaZyyh27uzUQmG1r3GFEIZlhQl7/cG0d+hINVwTW5qS/oDFGjugA5ByWgfqniWUUodctQILy1kfXg7OEbb532GW6GAVhYP/euqL9TL/tPic6hxIv/TIP60F5QXW8xfPbn1SkzY8I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cFA6YVq3; 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="cFA6YVq3" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A1A8EC433C7; Wed, 7 Feb 2024 14:08:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707314896; bh=xBKjgPQS/Jpq9Sq/sAc3h8Qc9T8zShFzRqNCb+1mBNc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cFA6YVq3AUgsVn/VayYMg4PNvmlD2XVzAZWFwfFmg+Gu707jLaBL10vLVFAi+BKdR /yg0oxKBldV2aZop2MqgfS6+qyKu2RkbKl53dLE+wrjioa3wtmZeDnjo8xW7NYzfbO C9qU6ixSgIbYcSpDrSkUYGsizAHUIr3cakGqZahq+EuzubnqW5Ygh62bGSsxnHC9S9 kSykVcLSZsaL2Jwxxdf42yuZ2T56/qaTWjdEvIYQskrjc/uYfLYCrI5oICwUbiU1I0 ClXboQQWPJtBORv5u+439jZETVb1GuncgSev+KZIBrSVYoyX62tosAgQmxMyGvmvQI 3eAXbW6qdLHsQ== From: Geliang Tang To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , Matthieu Baerts , Eduard Zingerman , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: Geliang Tang , bpf@vger.kernel.org, mptcp@lists.linux.dev Subject: [PATCH bpf-next v4 1/3] bpf, btf: Fix error checks for btf_get_module_btf Date: Wed, 7 Feb 2024 22:07:54 +0800 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: 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 From: Geliang Tang To let the modules loaded, commit 3de4d22cc9ac ("bpf, btf: Warn but return no error for NULL btf from __register_btf_kfunc_id_set()") changes the return value of __register_btf_kfunc_id_set() from -ENOENT to 0 when btf is NULL. A better way to do this is to enable CONFIG_MODULE_ALLOW_BTF_MISMATCH. An error code -ENOENT should indeed be returned when kernel module BTF mismatch detected except CONFIG_MODULE_ALLOW_BTF_MISMATCH is enabled in __register_btf_kfunc_id_set(). The same in register_btf_id_dtor_kfuncs(), give the modules a chance to be loaded when CONFIG_MODULE_ALLOW_BTF_MISMATCH is enabled. Fixes: 3de4d22cc9ac ("bpf, btf: Warn but return no error for NULL btf from __register_btf_kfunc_id_set()") Signed-off-by: Geliang Tang --- kernel/bpf/btf.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index f7725cb6e564..203391e61d93 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -8103,8 +8103,11 @@ static int __register_btf_kfunc_id_set(enum btf_kfunc_hook hook, pr_err("missing vmlinux BTF, cannot register kfuncs\n"); return -ENOENT; } - if (kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) + if (kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) && + !IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) { pr_warn("missing module BTF, cannot register kfuncs\n"); + return -ENOENT; + } return 0; } if (IS_ERR(btf)) @@ -8219,7 +8222,8 @@ int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_c pr_err("missing vmlinux BTF, cannot register dtor kfuncs\n"); return -ENOENT; } - if (owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) { + if (owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) && + !IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) { pr_err("missing module BTF, cannot register dtor kfuncs\n"); return -ENOENT; } From patchwork Wed Feb 7 14:07:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13548563 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 7928C7A723; Wed, 7 Feb 2024 14:08:21 +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=1707314901; cv=none; b=fuuz2dHJwdpjVGSjDWy+NGdYS0vFbAtPrtz6KjCVF1XbpDWVOrs8Ii70nIjKUO/+tc/pnLf+8p5RDy1zlzQYI1WSHCGekJCCi1gaQD2OJHYNR77oYlybDRZ1SPR6agq6M4Zz7x0X/u4vHAPpN7aFts9ZQeCJNwMCxE4MqmEaan8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707314901; c=relaxed/simple; bh=JmKdnwDvh8if28fTJVSrKG/pAJ4TElCzJ+iBTT3w+14=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=M7ybi5R0Elt5cVhz6fSpU6qYq6laTZCp62nzq+4eE18LJdSwEKqbpztLAgzeyuHGO51zMc0tAILUgehLG0rCDyWi6DF+g48AIb1ow6DIWrdkLkHhUCPPDD5yAKDHWLAbgne2ojpr2DAjPAznArfROSpqc7E/2inq6mxecAP0+jA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=R7cYiql/; 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="R7cYiql/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2EC9CC43390; Wed, 7 Feb 2024 14:08:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707314901; bh=JmKdnwDvh8if28fTJVSrKG/pAJ4TElCzJ+iBTT3w+14=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R7cYiql/2I+DNe74Y+AyD/JUbB4B8QRx4uyPaTZ5mugnU+6X+8/1AR6az3k317psr KCU3IhAp4hRrVxL3f64MUvMsgPby17+SjdqwmcZ0eRPajIjrW0S7TGbLCRw0dAmRy4 D1Ce9XE11lX09HdFyp7dyKZ3JbXHMxah/3Zc9/f6M8zNbjGSHUrPhwp44mtWf1ErDu YjQGloZs3WdYO02neB25VW36tukE5ZKYDT4oO5tNJQvwU+xb9ewK+ZK/Bpnl2Qs8jT G+Ja1ZqtGR0HHt7PUn+qpypXEutXhXkj1i/TSchK2+aiT5V5MFyLaPhamNIw1cBH2T zhmKdhM9Gpq6A== From: Geliang Tang To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , Matthieu Baerts , Eduard Zingerman , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: Geliang Tang , bpf@vger.kernel.org, mptcp@lists.linux.dev Subject: [PATCH bpf-next v4 2/3] bpf, btf: Add check_btf_kconfigs helper Date: Wed, 7 Feb 2024 22:07:55 +0800 Message-Id: X-Mailer: git-send-email 2.40.1 In-Reply-To: References: 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 From: Geliang Tang This patch extracts duplicate code on error path when btf_get_module_btf() returns NULL from the functions __register_btf_kfunc_id_set() and register_btf_id_dtor_kfuncs() into a new helper named check_btf_kconfigs() to check CONFIG_DEBUG_INFO_BTF, CONFIG_DEBUG_INFO_BTF_MODULES and CONFIG_MODULE_ALLOW_BTF_MISMATCH in it. Signed-off-by: Geliang Tang --- kernel/bpf/btf.c | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 203391e61d93..eedbee04de89 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -7738,6 +7738,20 @@ static struct btf *btf_get_module_btf(const struct module *module) return btf; } +static int check_btf_kconfigs(const struct module *module) +{ + if (!module && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { + pr_err("missing vmlinux BTF, cannot register kfuncs\n"); + return -ENOENT; + } + if (module && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) && + !IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) { + pr_err("missing module BTF, cannot register kfuncs\n"); + return -ENOENT; + } + return 0; +} + BPF_CALL_4(bpf_btf_find_by_name_kind, char *, name, int, name_sz, u32, kind, int, flags) { struct btf *btf = NULL; @@ -8098,18 +8112,8 @@ static int __register_btf_kfunc_id_set(enum btf_kfunc_hook hook, int ret, i; btf = btf_get_module_btf(kset->owner); - if (!btf) { - if (!kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { - pr_err("missing vmlinux BTF, cannot register kfuncs\n"); - return -ENOENT; - } - if (kset->owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) && - !IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) { - pr_warn("missing module BTF, cannot register kfuncs\n"); - return -ENOENT; - } - return 0; - } + if (!btf) + return check_btf_kconfigs(kset->owner); if (IS_ERR(btf)) return PTR_ERR(btf); @@ -8217,18 +8221,8 @@ int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_c int ret; btf = btf_get_module_btf(owner); - if (!btf) { - if (!owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { - pr_err("missing vmlinux BTF, cannot register dtor kfuncs\n"); - return -ENOENT; - } - if (owner && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES) && - !IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH)) { - pr_err("missing module BTF, cannot register dtor kfuncs\n"); - return -ENOENT; - } - return 0; - } + if (!btf) + return check_btf_kconfigs(owner); if (IS_ERR(btf)) return PTR_ERR(btf); From patchwork Wed Feb 7 14:07:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13548564 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 A883B7A726; Wed, 7 Feb 2024 14:08:28 +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=1707314908; cv=none; b=dGTNJhlUoL7jFOih5m/1tLxbJD3JKbtRj+4IsVEYlHUj5aWOgZ4ed7trklEt+dVOCg1QtSYe6M3bJDcUtCVI5Y7khdM9Ei8m1RYMMNDpDkbnHMhquxkOb+vVCL6Ew3bZrBVU7pDn3e5BFX+Ni8k0OWGfcg5HFoyDExnjxgGpIRU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707314908; c=relaxed/simple; bh=J1Il0ufBa31zXemEA+7pfMIoFvnKy7gs4oH7qu5WSe0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SWhBj6sOoPFP5S2tdMiebK6eIdLosHNJydtCJh81819z9XeF4UHTmvTSlfYb1C77/x1i8z1ZX8JVaJzWFSpCo5+r1HpNzcxcAn7daE1dF0YPUH3YNKsDaR2nAo101Es/+nKAg/xfkoBzuqiP/TyZG47Gv3RT5sXVKx6ctVpBOtY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QRbbWV0A; 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="QRbbWV0A" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C4768C43390; Wed, 7 Feb 2024 14:08:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707314908; bh=J1Il0ufBa31zXemEA+7pfMIoFvnKy7gs4oH7qu5WSe0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QRbbWV0AihK5q5+PMymDQHeZqrPHjbEYVxcpn9vDsHQWePLC8vpU2v+SLG8nKlc03 3y24BjM7EbGQT/lXgITe6p6uTGjSK6131fZwAYlLmI4SdIcJ1A5sxdRC1njH0gxFsC y7qFb8Z/VI1JHV0oLtV0EOfd+3sPUYe4mIUKTVJOHfXGvlD/3sARhXuXLgTXglmFVO tjGtJhAsaMeq1Wf6X1L8qcBeDnXdoavdi3rxzSWQveg2lP0ixmmkT5WUwYXxf4H+yU k1AVNS4zWIzTlP/lBenRkU6N9s7bNMM6rEsfh34Jwct4bcRzwWw1zCnZ0YQu1kzbwi nW0f9Nqzbh9Qg== From: Geliang Tang To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , Matthieu Baerts , Eduard Zingerman , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: Geliang Tang , bpf@vger.kernel.org, mptcp@lists.linux.dev Subject: [PATCH bpf-next v4 3/3] bpf, btf: Check btf for register_bpf_struct_ops Date: Wed, 7 Feb 2024 22:07:56 +0800 Message-Id: <836391d2a7f44fffaba4ddf664354ecb8eba66cf.1707314646.git.tanggeliang@kylinos.cn> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: 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 From: Geliang Tang Similar to the handling in the functions __register_btf_kfunc_id_set() and register_btf_id_dtor_kfuncs(), this patch uses the newly added helper check_btf_kconfigs() and IS_ERR() to check the return value of btf_get_module_btf(). Signed-off-by: Geliang Tang --- kernel/bpf/btf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index eedbee04de89..9aae5a4bba24 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -8885,7 +8885,9 @@ int __register_bpf_struct_ops(struct bpf_struct_ops *st_ops) btf = btf_get_module_btf(st_ops->owner); if (!btf) - return -EINVAL; + return check_btf_kconfigs(st_ops->owner); + if (IS_ERR(btf)) + return PTR_ERR(btf); log = kzalloc(sizeof(*log), GFP_KERNEL | __GFP_NOWARN); if (!log) {