From patchwork Mon Mar 28 17:50:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 12793994 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C06B1C433EF for ; Mon, 28 Mar 2022 17:51:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244737AbiC1RxO (ORCPT ); Mon, 28 Mar 2022 13:53:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244770AbiC1Rwz (ORCPT ); Mon, 28 Mar 2022 13:52:55 -0400 Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C3B03CA5F; Mon, 28 Mar 2022 10:51:14 -0700 (PDT) Received: from fraeml714-chm.china.huawei.com (unknown [172.18.147.226]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4KS0YQ3KRMz67Q7R; Tue, 29 Mar 2022 01:48:42 +0800 (CST) Received: from roberto-ThinkStation-P620.huawei.com (10.204.63.22) by fraeml714-chm.china.huawei.com (10.206.15.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 28 Mar 2022 19:51:11 +0200 From: Roberto Sassu To: , , , , , , , , , CC: , , , , , , , , , , Roberto Sassu Subject: [PATCH 03/18] bpf-preload: Generalize object pinning from the kernel Date: Mon, 28 Mar 2022 19:50:18 +0200 Message-ID: <20220328175033.2437312-4-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220328175033.2437312-1-roberto.sassu@huawei.com> References: <20220328175033.2437312-1-roberto.sassu@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.204.63.22] X-ClientProxiedBy: lhreml754-chm.china.huawei.com (10.201.108.204) To fraeml714-chm.china.huawei.com (10.206.15.33) X-CFilter-Loop: Reflected Precedence: bulk List-ID: Rename bpf_iter_link_pin_kernel() to bpf_obj_do_pin_kernel(), to match the user space counterpart bpf_obj_do_pin() and similarly to the latter, accept a generic object pointer and its type, so that the preload method can pin not only links but also maps and progs. Signed-off-by: Roberto Sassu --- kernel/bpf/inode.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c index 1f2d468abf58..a9d725db4cf4 100644 --- a/kernel/bpf/inode.c +++ b/kernel/bpf/inode.c @@ -414,9 +414,10 @@ static const struct inode_operations bpf_dir_iops = { .unlink = simple_unlink, }; -/* pin iterator link into bpffs */ -static int bpf_iter_link_pin_kernel(struct dentry *parent, - const char *name, struct bpf_link *link) +/* pin object into bpffs */ +static int bpf_obj_do_pin_kernel(struct dentry *parent, + const char *name, void *raw, + enum bpf_type type) { umode_t mode = S_IFREG | S_IRUSR; struct dentry *dentry; @@ -428,8 +429,22 @@ static int bpf_iter_link_pin_kernel(struct dentry *parent, inode_unlock(parent->d_inode); return PTR_ERR(dentry); } - ret = bpf_mkobj_ops(dentry, mode, link, &bpf_link_iops, - &bpf_iter_fops); + + switch (type) { + case BPF_TYPE_PROG: + ret = bpf_mkprog(dentry, mode, raw); + break; + case BPF_TYPE_MAP: + ret = bpf_mkmap(dentry, mode, raw); + break; + case BPF_TYPE_LINK: + ret = bpf_mklink(dentry, mode, raw); + break; + default: + ret = -EOPNOTSUPP; + break; + } + dput(dentry); inode_unlock(parent->d_inode); return ret; @@ -726,8 +741,8 @@ static int populate_bpffs(struct dentry *parent) goto out_put; for (i = 0; i < BPF_PRELOAD_LINKS; i++) { bpf_link_inc(objs[i].link); - err = bpf_iter_link_pin_kernel(parent, - objs[i].link_name, objs[i].link); + err = bpf_obj_do_pin_kernel(parent, objs[i].link_name, + objs[i].link, BPF_TYPE_LINK); if (err) { bpf_link_put(objs[i].link); goto out_put;