From nobody Sat Nov 23 11:41:13 2024 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 BD7A25680; Wed, 13 Nov 2024 00:54:22 +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=1731459262; cv=none; b=Ki8v6goPlJINkUvpdpFTEWH/sgPTVA+WKSLsKIv96rRmMB0X+eC6iCi+UQUbnGnOZKfga1n2aAvQZSC0s7AQTEUooNPHiTlNHbbVOp7bx+8KQRZstrZ9Vx8CbHk7zMqDNL1AJnzkx8sPRU/Y9aZEl14nQkFJQkeGBx9i7nGElYs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731459262; c=relaxed/simple; bh=6wcgg1hEfQa6Nf8Cxbudvhg88dmxNzVd4PTSML8QbyY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ufSq/jdr5E1Yf2tYOpnBVoMgo2/xKUw8RB/x6VlQrEO75W0cqfQ9yrOV+KEZlXxQGRZZA7+w+//otPFu5Vy6tUDxvD4UjtwrC7uC18ZZtfktAtrotXJCA5eOV+rl7eIgWluypJ6cDgUtgR1zfDMmFjodSxeL3QNkm98u6AKtSC8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HznpWHRx; 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="HznpWHRx" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B74CC4CECD; Wed, 13 Nov 2024 00:54:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731459262; bh=6wcgg1hEfQa6Nf8Cxbudvhg88dmxNzVd4PTSML8QbyY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HznpWHRxmsCUyUgm3OeJYFQ4kW1C2psaD/P9cVx+RZ6U8//HtBt58/rtAezHGdtjz lqYQsS9Qh+mSJSzymdQfUqoAacOMsV6YdxwPrFUHmcaeVOu3lC69PtPsDIaejMp2lZ 0ZFDTb7AWR+0VeCzIk8r9xwjQ8c6mkx6Ru66yI7oncQq78wMfbqSQ4DrZDXQbwdvHy 6hWBDHEJb2V5FeDbjRE10Vc9E2p7sRdgm4+AcvR24WMVqjfQyyWQ0IwvFOpTZv2Ogm zPepZz3HKhaYL5vn1tSLI/5tIjlH4G2U0u3/EOZlteImuNd/OBt494N45W0jZpu6OG u3R0T9A3OvA9w== From: Song Liu To: bpf@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Cc: kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com, ast@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, kpsingh@kernel.org, mattbobrowski@google.com, amir73il@gmail.com, repnop@google.com, jlayton@kernel.org, josef@toxicpanda.com, mic@digikod.net, gnoack@google.com, Song Liu Subject: [PATCH v3 bpf-next 1/4] bpf: lsm: Remove hook to bpf_task_storage_free Date: Tue, 12 Nov 2024 16:53:48 -0800 Message-ID: <20241113005351.2197340-2-song@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241113005351.2197340-1-song@kernel.org> References: <20241113005351.2197340-1-song@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" free_task() already calls bpf_task_storage_free(). It is not necessary to call bpf_task_storage_free() again on security_task_free(). Remove the hook to bpf_task_storage_free. Signed-off-by: Song Liu --- security/bpf/hooks.c | 1 - 1 file changed, 1 deletion(-) diff --git a/security/bpf/hooks.c b/security/bpf/hooks.c index 3663aec7bcbd..db759025abe1 100644 --- a/security/bpf/hooks.c +++ b/security/bpf/hooks.c @@ -13,7 +13,6 @@ static struct security_hook_list bpf_lsm_hooks[] __ro_aft= er_init =3D { #include #undef LSM_HOOK LSM_HOOK_INIT(inode_free_security, bpf_inode_storage_free), - LSM_HOOK_INIT(task_free, bpf_task_storage_free), }; =20 static const struct lsm_id bpf_lsmid =3D { --=20 2.43.5 From nobody Sat Nov 23 11:41:13 2024 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 963892C9A; Wed, 13 Nov 2024 00:54:30 +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=1731459270; cv=none; b=T6eZsZdoWMBBylkVmI09fnAPgpnpNyp266Wvi+TWNFFCBF/1nV+b468h34VQHyOjxkJM7Yu+3CMtONFya9CohDjv30GbRWEWnNLi0fbrYjMoe0ErP30UhfHlWB9uRRGvTfochxqJtAvltIT0c/rvi689ECweiImtab8emQCZTyI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731459270; c=relaxed/simple; bh=1zk7zUySkABxV8ZMTkh8Eb7XvT0NtTRLUmkBd3HtUX0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WC6iNmyrEQe+lOhf2DyHMA/FerG+H99TnI0rLJwWBt55TelrxYatuWUvgLIKdHMzdhn3L8iiSNpL+bnbjzAWtsEx9AuEIpGbyh0fF1a3eioDGFHqOI7gn3F96hza8wtoRw63Kss/l2GZJV68oJLljAL9CMMWBqBJI8R4QADFAQc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fLkqb6cZ; 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="fLkqb6cZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C1A82C4CECD; Wed, 13 Nov 2024 00:54:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731459270; bh=1zk7zUySkABxV8ZMTkh8Eb7XvT0NtTRLUmkBd3HtUX0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fLkqb6cZf1s9bXji/pdSv6A59w/Y5qdwk3QC/3LP6LlMNReeDcYcnnE9ZNiA82niM CxwOPO9sW834QQAE41fygQg3HDiv0GBKJlEWTmLoWqO8qe++66ao1HCAzD+75hTl3J 5IZaDZe3XWvZppsy/7eIUG+F9IaKdT60ibXV+HEeuKE2GMvAnZ7MqRIfV7PmHYzEEo T45lbnFi+2FRfDGQqnDx04jbJQ8KOdDjoyOyvlPIr7gg+Zgnal1WS727znHecojShf TatWs9Lrxyu+2u2y8TxoDKObuc7V80xdR+OmYL1FoDZeRCWcKXKAptev2FQAgmKtgF H1wzNPUxluw6A== From: Song Liu To: bpf@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Cc: kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com, ast@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, kpsingh@kernel.org, mattbobrowski@google.com, amir73il@gmail.com, repnop@google.com, jlayton@kernel.org, josef@toxicpanda.com, mic@digikod.net, gnoack@google.com, Song Liu Subject: [PATCH v3 bpf-next 2/4] bpf: Make bpf inode storage available to tracing program Date: Tue, 12 Nov 2024 16:53:49 -0800 Message-ID: <20241113005351.2197340-3-song@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241113005351.2197340-1-song@kernel.org> References: <20241113005351.2197340-1-song@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" inode storage can be useful for non-LSM program. For example, file* tools from bcc/libbpf-tools can use inode storage instead of hash map; fanotify fastpath [1] can also use inode storage to store useful data. Make inode storage available for tracing program. Move bpf inode storage from a security blob to inode->i_bpf_storage, and adjust related code accordingly. [1] https://lore.kernel.org/linux-fsdevel/20241029231244.2834368-1-song@ker= nel.org/ Signed-off-by: Song Liu --- fs/inode.c | 2 ++ include/linux/bpf.h | 9 +++++++++ include/linux/bpf_lsm.h | 29 ----------------------------- include/linux/fs.h | 4 ++++ kernel/bpf/Makefile | 3 +-- kernel/bpf/bpf_inode_storage.c | 32 +++++--------------------------- kernel/bpf/bpf_lsm.c | 4 ---- kernel/trace/bpf_trace.c | 4 ++++ security/bpf/hooks.c | 6 ------ 9 files changed, 25 insertions(+), 68 deletions(-) diff --git a/fs/inode.c b/fs/inode.c index 8dabb224f941..c196a62bd48f 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -250,6 +250,8 @@ EXPORT_SYMBOL(free_inode_nonrcu); static void i_callback(struct rcu_head *head) { struct inode *inode =3D container_of(head, struct inode, i_rcu); + + bpf_inode_storage_free(inode); if (inode->free_inode) inode->free_inode(inode); else diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 7da41ae2eac8..9da120140327 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2672,6 +2672,7 @@ struct bpf_link *bpf_link_by_id(u32 id); const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog); void bpf_task_storage_free(struct task_struct *task); +void bpf_inode_storage_free(struct inode *inode); void bpf_cgrp_storage_free(struct cgroup *cgroup); bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog); const struct btf_func_model * @@ -2942,6 +2943,10 @@ static inline void bpf_task_storage_free(struct task= _struct *task) { } =20 +static inline void bpf_inode_storage_free(struct inode *inode) +{ +} + static inline bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog) { return false; @@ -3305,6 +3310,10 @@ extern const struct bpf_func_proto bpf_task_storage_= get_recur_proto; extern const struct bpf_func_proto bpf_task_storage_get_proto; extern const struct bpf_func_proto bpf_task_storage_delete_recur_proto; extern const struct bpf_func_proto bpf_task_storage_delete_proto; +extern const struct bpf_func_proto bpf_inode_storage_get_proto; +extern const struct bpf_func_proto bpf_inode_storage_get_recur_proto; +extern const struct bpf_func_proto bpf_inode_storage_delete_proto; +extern const struct bpf_func_proto bpf_inode_storage_delete_recur_proto; extern const struct bpf_func_proto bpf_for_each_map_elem_proto; extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto; extern const struct bpf_func_proto bpf_sk_setsockopt_proto; diff --git a/include/linux/bpf_lsm.h b/include/linux/bpf_lsm.h index aefcd6564251..a819c2f0a062 100644 --- a/include/linux/bpf_lsm.h +++ b/include/linux/bpf_lsm.h @@ -19,31 +19,12 @@ #include #undef LSM_HOOK =20 -struct bpf_storage_blob { - struct bpf_local_storage __rcu *storage; -}; - -extern struct lsm_blob_sizes bpf_lsm_blob_sizes; - int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog, const struct bpf_prog *prog); =20 bool bpf_lsm_is_sleepable_hook(u32 btf_id); bool bpf_lsm_is_trusted(const struct bpf_prog *prog); =20 -static inline struct bpf_storage_blob *bpf_inode( - const struct inode *inode) -{ - if (unlikely(!inode->i_security)) - return NULL; - - return inode->i_security + bpf_lsm_blob_sizes.lbs_inode; -} - -extern const struct bpf_func_proto bpf_inode_storage_get_proto; -extern const struct bpf_func_proto bpf_inode_storage_delete_proto; -void bpf_inode_storage_free(struct inode *inode); - void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog, bpf_func_t *bpf= _func); =20 int bpf_lsm_get_retval_range(const struct bpf_prog *prog, @@ -66,16 +47,6 @@ static inline int bpf_lsm_verify_prog(struct bpf_verifie= r_log *vlog, return -EOPNOTSUPP; } =20 -static inline struct bpf_storage_blob *bpf_inode( - const struct inode *inode) -{ - return NULL; -} - -static inline void bpf_inode_storage_free(struct inode *inode) -{ -} - static inline void bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog, bpf_func_t *bpf_func) { diff --git a/include/linux/fs.h b/include/linux/fs.h index 3559446279c1..479097e4dd5b 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -79,6 +79,7 @@ struct fs_context; struct fs_parameter_spec; struct fileattr; struct iomap_ops; +struct bpf_local_storage; =20 extern void __init inode_init(void); extern void __init inode_init_early(void); @@ -648,6 +649,9 @@ struct inode { #ifdef CONFIG_SECURITY void *i_security; #endif +#ifdef CONFIG_BPF_SYSCALL + struct bpf_local_storage __rcu *i_bpf_storage; +#endif =20 /* Stat data, not accessed from path walking */ unsigned long i_ino; diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile index 105328f0b9c0..73604c7130f1 100644 --- a/kernel/bpf/Makefile +++ b/kernel/bpf/Makefile @@ -10,8 +10,7 @@ obj-$(CONFIG_BPF_SYSCALL) +=3D syscall.o verifier.o inode= .o helpers.o tnum.o log.o obj-$(CONFIG_BPF_SYSCALL) +=3D bpf_iter.o map_iter.o task_iter.o prog_iter= .o link_iter.o obj-$(CONFIG_BPF_SYSCALL) +=3D hashtab.o arraymap.o percpu_freelist.o bpf_= lru_list.o lpm_trie.o map_in_map.o bloom_filter.o obj-$(CONFIG_BPF_SYSCALL) +=3D local_storage.o queue_stack_maps.o ringbuf.o -obj-$(CONFIG_BPF_SYSCALL) +=3D bpf_local_storage.o bpf_task_storage.o -obj-${CONFIG_BPF_LSM} +=3D bpf_inode_storage.o +obj-$(CONFIG_BPF_SYSCALL) +=3D bpf_local_storage.o bpf_task_storage.o bpf_= inode_storage.o obj-$(CONFIG_BPF_SYSCALL) +=3D disasm.o mprog.o obj-$(CONFIG_BPF_JIT) +=3D trampoline.o obj-$(CONFIG_BPF_SYSCALL) +=3D btf.o memalloc.o diff --git a/kernel/bpf/bpf_inode_storage.c b/kernel/bpf/bpf_inode_storage.c index 44ccebc745e5..8d5a9bfe6643 100644 --- a/kernel/bpf/bpf_inode_storage.c +++ b/kernel/bpf/bpf_inode_storage.c @@ -21,16 +21,11 @@ =20 DEFINE_BPF_STORAGE_CACHE(inode_cache); =20 -static struct bpf_local_storage __rcu ** -inode_storage_ptr(void *owner) +static struct bpf_local_storage __rcu **inode_storage_ptr(void *owner) { struct inode *inode =3D owner; - struct bpf_storage_blob *bsb; =20 - bsb =3D bpf_inode(inode); - if (!bsb) - return NULL; - return &bsb->storage; + return &inode->i_bpf_storage; } =20 static struct bpf_local_storage_data *inode_storage_lookup(struct inode *i= node, @@ -39,14 +34,9 @@ static struct bpf_local_storage_data *inode_storage_look= up(struct inode *inode, { struct bpf_local_storage *inode_storage; struct bpf_local_storage_map *smap; - struct bpf_storage_blob *bsb; - - bsb =3D bpf_inode(inode); - if (!bsb) - return NULL; =20 inode_storage =3D - rcu_dereference_check(bsb->storage, bpf_rcu_lock_held()); + rcu_dereference_check(inode->i_bpf_storage, bpf_rcu_lock_held()); if (!inode_storage) return NULL; =20 @@ -57,15 +47,10 @@ static struct bpf_local_storage_data *inode_storage_loo= kup(struct inode *inode, void bpf_inode_storage_free(struct inode *inode) { struct bpf_local_storage *local_storage; - struct bpf_storage_blob *bsb; - - bsb =3D bpf_inode(inode); - if (!bsb) - return; =20 rcu_read_lock(); =20 - local_storage =3D rcu_dereference(bsb->storage); + local_storage =3D rcu_dereference(inode->i_bpf_storage); if (!local_storage) { rcu_read_unlock(); return; @@ -95,8 +80,6 @@ static long bpf_fd_inode_storage_update_elem(struct bpf_m= ap *map, void *key, =20 if (fd_empty(f)) return -EBADF; - if (!inode_storage_ptr(file_inode(fd_file(f)))) - return -EBADF; =20 sdata =3D bpf_local_storage_update(file_inode(fd_file(f)), (struct bpf_local_storage_map *)map, @@ -136,12 +119,7 @@ BPF_CALL_5(bpf_inode_storage_get, struct bpf_map *, ma= p, struct inode *, inode, if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE)) return (unsigned long)NULL; =20 - /* explicitly check that the inode_storage_ptr is not - * NULL as inode_storage_lookup returns NULL in this case and - * bpf_local_storage_update expects the owner to have a - * valid storage pointer. - */ - if (!inode || !inode_storage_ptr(inode)) + if (!inode) return (unsigned long)NULL; =20 sdata =3D inode_storage_lookup(inode, map, true); diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c index 3bc61628ab25..6b6e0730e515 100644 --- a/kernel/bpf/bpf_lsm.c +++ b/kernel/bpf/bpf_lsm.c @@ -231,10 +231,6 @@ bpf_lsm_func_proto(enum bpf_func_id func_id, const str= uct bpf_prog *prog) } =20 switch (func_id) { - case BPF_FUNC_inode_storage_get: - return &bpf_inode_storage_get_proto; - case BPF_FUNC_inode_storage_delete: - return &bpf_inode_storage_delete_proto; #ifdef CONFIG_NET case BPF_FUNC_sk_storage_get: return &bpf_sk_storage_get_proto; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 949a3870946c..262bd101ea0b 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1553,6 +1553,10 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, con= st struct bpf_prog *prog) if (bpf_prog_check_recur(prog)) return &bpf_task_storage_delete_recur_proto; return &bpf_task_storage_delete_proto; + case BPF_FUNC_inode_storage_get: + return &bpf_inode_storage_get_proto; + case BPF_FUNC_inode_storage_delete: + return &bpf_inode_storage_delete_proto; case BPF_FUNC_for_each_map_elem: return &bpf_for_each_map_elem_proto; case BPF_FUNC_snprintf: diff --git a/security/bpf/hooks.c b/security/bpf/hooks.c index db759025abe1..67719a04bb0b 100644 --- a/security/bpf/hooks.c +++ b/security/bpf/hooks.c @@ -12,7 +12,6 @@ static struct security_hook_list bpf_lsm_hooks[] __ro_aft= er_init =3D { LSM_HOOK_INIT(NAME, bpf_lsm_##NAME), #include #undef LSM_HOOK - LSM_HOOK_INIT(inode_free_security, bpf_inode_storage_free), }; =20 static const struct lsm_id bpf_lsmid =3D { @@ -28,12 +27,7 @@ static int __init bpf_lsm_init(void) return 0; } =20 -struct lsm_blob_sizes bpf_lsm_blob_sizes __ro_after_init =3D { - .lbs_inode =3D sizeof(struct bpf_storage_blob), -}; - DEFINE_LSM(bpf) =3D { .name =3D "bpf", .init =3D bpf_lsm_init, - .blobs =3D &bpf_lsm_blob_sizes }; --=20 2.43.5 From nobody Sat Nov 23 11:41:13 2024 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 2663D18651; Wed, 13 Nov 2024 00:54: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=1731459278; cv=none; b=LvL3PU7v2dUydCxCoJMZ/ZTFY1x/FJ6n+S4jGsOXDD+/daCpTM6iQR3fcg+k3f03RZwOvv3Dp07EUCEY7+ui33ZjXvMyOlUpBA3b9sB7MEZukh1RpG87tSz7kh3mdb/8Oi5D6RZSG6fAO09SHgQILOSi4Q6h1K6/736XIecPAms= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731459278; c=relaxed/simple; bh=7GtgdyCbei+7d3E/OgOIZPfXvpFQvUunLLOxDsqS4Gs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KRAfdNcZOL4H9dU8pA+vuzBcYEZfX8bIBz/SWYbItT7XJxcGh/yGKYWaKAom4vfPCfZg1PBAHzGCV8H8BzwN/PEzzPYTt79oRcAZhJIQPFPjzcQ0s23/Z4yTN7h7CJhzPp+OSOi2HMGp0VJ2S2v4tD2PqbWsGQRn6P+x2GDw9IU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qVtThniw; 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="qVtThniw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8FE1EC4CECD; Wed, 13 Nov 2024 00:54:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731459278; bh=7GtgdyCbei+7d3E/OgOIZPfXvpFQvUunLLOxDsqS4Gs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qVtThniwCJxmsmxGxeMntyu5ZbHcdm4PTY1t5HP4Jyb+bygi7GgymDVYlnWEpJ+W6 VNg+w0mctjWJvG3Wu6364Bo0GrRIK3QGvYaK3oLIIVJ1l9JSviGeMYcAf0RkKp3GJO x+ShNfRUgk9gKdq1ZPTPkHej67gh301bOKdGhlTFi7BJD83gynbh2+WOL+CiOrfl10 EUy2tcRkdQuJ88Iv4Jw/qqORzZAmr9AUPFzY5z3nIDHnQl03cdZX14wnovYD5oBDf9 qZD6iC/Hs21fz8s8CrFUPq6lRA3vvGD0jCEWKLSYBHPwCtV9wLfqaocQ+kOwUVL24k 3ROt3g7WtOkmQ== From: Song Liu To: bpf@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Cc: kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com, ast@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, kpsingh@kernel.org, mattbobrowski@google.com, amir73il@gmail.com, repnop@google.com, jlayton@kernel.org, josef@toxicpanda.com, mic@digikod.net, gnoack@google.com, Song Liu Subject: [PATCH v3 bpf-next 3/4] bpf: Add recursion prevention logic for inode storage Date: Tue, 12 Nov 2024 16:53:50 -0800 Message-ID: <20241113005351.2197340-4-song@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241113005351.2197340-1-song@kernel.org> References: <20241113005351.2197340-1-song@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This logic is similar to the recursion prevention logic for task local storage: bpf programs on LSM hooks lock bpf_inode_storage_busy; bpf tracing program will try to lock bpf_inode_storage_busy, and may return -EBUSY if something else already lock bpf_inode_storage_busy on the same CPU. Signed-off-by: Song Liu --- kernel/bpf/bpf_inode_storage.c | 155 +++++++++++++++++++++++++++------ kernel/trace/bpf_trace.c | 4 + 2 files changed, 134 insertions(+), 25 deletions(-) diff --git a/kernel/bpf/bpf_inode_storage.c b/kernel/bpf/bpf_inode_storage.c index 8d5a9bfe6643..e98a0fc4fb70 100644 --- a/kernel/bpf/bpf_inode_storage.c +++ b/kernel/bpf/bpf_inode_storage.c @@ -21,6 +21,31 @@ =20 DEFINE_BPF_STORAGE_CACHE(inode_cache); =20 +static DEFINE_PER_CPU(int, bpf_inode_storage_busy); + +static void bpf_inode_storage_lock(void) +{ + migrate_disable(); + this_cpu_inc(bpf_inode_storage_busy); +} + +static void bpf_inode_storage_unlock(void) +{ + this_cpu_dec(bpf_inode_storage_busy); + migrate_enable(); +} + +static bool bpf_inode_storage_trylock(void) +{ + migrate_disable(); + if (unlikely(this_cpu_inc_return(bpf_inode_storage_busy) !=3D 1)) { + this_cpu_dec(bpf_inode_storage_busy); + migrate_enable(); + return false; + } + return true; +} + static struct bpf_local_storage __rcu **inode_storage_ptr(void *owner) { struct inode *inode =3D owner; @@ -56,7 +81,9 @@ void bpf_inode_storage_free(struct inode *inode) return; } =20 + bpf_inode_storage_lock(); bpf_local_storage_destroy(local_storage); + bpf_inode_storage_unlock(); rcu_read_unlock(); } =20 @@ -68,7 +95,9 @@ static void *bpf_fd_inode_storage_lookup_elem(struct bpf_= map *map, void *key) if (fd_empty(f)) return ERR_PTR(-EBADF); =20 + bpf_inode_storage_lock(); sdata =3D inode_storage_lookup(file_inode(fd_file(f)), map, true); + bpf_inode_storage_unlock(); return sdata ? sdata->data : NULL; } =20 @@ -81,13 +110,16 @@ static long bpf_fd_inode_storage_update_elem(struct bp= f_map *map, void *key, if (fd_empty(f)) return -EBADF; =20 + bpf_inode_storage_lock(); sdata =3D bpf_local_storage_update(file_inode(fd_file(f)), (struct bpf_local_storage_map *)map, value, map_flags, false, GFP_ATOMIC); + bpf_inode_storage_unlock(); return PTR_ERR_OR_ZERO(sdata); } =20 -static int inode_storage_delete(struct inode *inode, struct bpf_map *map) +static int inode_storage_delete(struct inode *inode, struct bpf_map *map, + bool nobusy) { struct bpf_local_storage_data *sdata; =20 @@ -95,6 +127,9 @@ static int inode_storage_delete(struct inode *inode, str= uct bpf_map *map) if (!sdata) return -ENOENT; =20 + if (!nobusy) + return -EBUSY; + bpf_selem_unlink(SELEM(sdata), false); =20 return 0; @@ -102,55 +137,105 @@ static int inode_storage_delete(struct inode *inode,= struct bpf_map *map) =20 static long bpf_fd_inode_storage_delete_elem(struct bpf_map *map, void *ke= y) { + int err; + CLASS(fd_raw, f)(*(int *)key); =20 if (fd_empty(f)) return -EBADF; - return inode_storage_delete(file_inode(fd_file(f)), map); + bpf_inode_storage_lock(); + err =3D inode_storage_delete(file_inode(fd_file(f)), map, true); + bpf_inode_storage_unlock(); + return err; } =20 -/* *gfp_flags* is a hidden argument provided by the verifier */ -BPF_CALL_5(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, i= node, - void *, value, u64, flags, gfp_t, gfp_flags) +static void *__bpf_inode_storage_get(struct bpf_map *map, struct inode *in= ode, + void *value, u64 flags, gfp_t gfp_flags, bool nobusy) { struct bpf_local_storage_data *sdata; =20 - WARN_ON_ONCE(!bpf_rcu_lock_held()); - if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE)) - return (unsigned long)NULL; - + /* explicitly check that the inode not NULL */ if (!inode) - return (unsigned long)NULL; + return NULL; =20 - sdata =3D inode_storage_lookup(inode, map, true); + sdata =3D inode_storage_lookup(inode, map, nobusy); if (sdata) - return (unsigned long)sdata->data; + return sdata->data; =20 - /* This helper must only called from where the inode is guaranteed - * to have a refcount and cannot be freed. - */ - if (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) { + /* only allocate new storage, when the inode is refcounted */ + if (atomic_read(&inode->i_count) && + flags & BPF_LOCAL_STORAGE_GET_F_CREATE && nobusy) { sdata =3D bpf_local_storage_update( inode, (struct bpf_local_storage_map *)map, value, BPF_NOEXIST, false, gfp_flags); - return IS_ERR(sdata) ? (unsigned long)NULL : - (unsigned long)sdata->data; + return IS_ERR(sdata) ? NULL : sdata->data; } =20 - return (unsigned long)NULL; + return NULL; +} + +/* *gfp_flags* is a hidden argument provided by the verifier */ +BPF_CALL_5(bpf_inode_storage_get_recur, struct bpf_map *, map, struct inod= e *, inode, + void *, value, u64, flags, gfp_t, gfp_flags) +{ + bool nobusy; + void *data; + + WARN_ON_ONCE(!bpf_rcu_lock_held()); + if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE)) + return (unsigned long)NULL; + + nobusy =3D bpf_inode_storage_trylock(); + data =3D __bpf_inode_storage_get(map, inode, value, flags, gfp_flags, nob= usy); + if (nobusy) + bpf_inode_storage_unlock(); + return (unsigned long)data; +} + +/* *gfp_flags* is a hidden argument provided by the verifier */ +BPF_CALL_5(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, i= node, + void *, value, u64, flags, gfp_t, gfp_flags) +{ + void *data; + + WARN_ON_ONCE(!bpf_rcu_lock_held()); + if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE)) + return (unsigned long)NULL; + + bpf_inode_storage_lock(); + data =3D __bpf_inode_storage_get(map, inode, value, flags, gfp_flags, tru= e); + bpf_inode_storage_unlock(); + return (unsigned long)data; +} + +BPF_CALL_2(bpf_inode_storage_delete_recur, struct bpf_map *, map, struct i= node *, inode) +{ + bool nobusy; + int ret; + + WARN_ON_ONCE(!bpf_rcu_lock_held()); + if (!inode) + return -EINVAL; + + nobusy =3D bpf_inode_storage_trylock(); + ret =3D inode_storage_delete(inode, map, nobusy); + if (nobusy) + bpf_inode_storage_unlock(); + return ret; } =20 -BPF_CALL_2(bpf_inode_storage_delete, - struct bpf_map *, map, struct inode *, inode) +BPF_CALL_2(bpf_inode_storage_delete, struct bpf_map *, map, struct inode *= , inode) { + int ret; + WARN_ON_ONCE(!bpf_rcu_lock_held()); if (!inode) return -EINVAL; =20 - /* This helper must only called from where the inode is guaranteed - * to have a refcount and cannot be freed. - */ - return inode_storage_delete(inode, map); + bpf_inode_storage_lock(); + ret =3D inode_storage_delete(inode, map, true); + bpf_inode_storage_unlock(); + return ret; } =20 static int notsupp_get_next_key(struct bpf_map *map, void *key, @@ -186,6 +271,17 @@ const struct bpf_map_ops inode_storage_map_ops =3D { =20 BTF_ID_LIST_SINGLE(bpf_inode_storage_btf_ids, struct, inode) =20 +const struct bpf_func_proto bpf_inode_storage_get_recur_proto =3D { + .func =3D bpf_inode_storage_get_recur, + .gpl_only =3D false, + .ret_type =3D RET_PTR_TO_MAP_VALUE_OR_NULL, + .arg1_type =3D ARG_CONST_MAP_PTR, + .arg2_type =3D ARG_PTR_TO_BTF_ID_OR_NULL, + .arg2_btf_id =3D &bpf_inode_storage_btf_ids[0], + .arg3_type =3D ARG_PTR_TO_MAP_VALUE_OR_NULL, + .arg4_type =3D ARG_ANYTHING, +}; + const struct bpf_func_proto bpf_inode_storage_get_proto =3D { .func =3D bpf_inode_storage_get, .gpl_only =3D false, @@ -197,6 +293,15 @@ const struct bpf_func_proto bpf_inode_storage_get_prot= o =3D { .arg4_type =3D ARG_ANYTHING, }; =20 +const struct bpf_func_proto bpf_inode_storage_delete_recur_proto =3D { + .func =3D bpf_inode_storage_delete_recur, + .gpl_only =3D false, + .ret_type =3D RET_INTEGER, + .arg1_type =3D ARG_CONST_MAP_PTR, + .arg2_type =3D ARG_PTR_TO_BTF_ID_OR_NULL, + .arg2_btf_id =3D &bpf_inode_storage_btf_ids[0], +}; + const struct bpf_func_proto bpf_inode_storage_delete_proto =3D { .func =3D bpf_inode_storage_delete, .gpl_only =3D false, diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 262bd101ea0b..4616f5430a5e 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1554,8 +1554,12 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, con= st struct bpf_prog *prog) return &bpf_task_storage_delete_recur_proto; return &bpf_task_storage_delete_proto; case BPF_FUNC_inode_storage_get: + if (bpf_prog_check_recur(prog)) + return &bpf_inode_storage_get_recur_proto; return &bpf_inode_storage_get_proto; case BPF_FUNC_inode_storage_delete: + if (bpf_prog_check_recur(prog)) + return &bpf_inode_storage_delete_recur_proto; return &bpf_inode_storage_delete_proto; case BPF_FUNC_for_each_map_elem: return &bpf_for_each_map_elem_proto; --=20 2.43.5 From nobody Sat Nov 23 11:41:13 2024 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 3215A7405A; Wed, 13 Nov 2024 00:54:45 +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=1731459286; cv=none; b=oQzH/w/bk0pjdtyonOXpnobt93KSlZewL9tSHKOLVkLi8BTLRlOlZJeh+AjN0wb2HULtls1VYk8VYVEv8j0cjiIxKdp0ADbAnMp2W2twhAv4mRkTDr/p4mo2+yEvFzO/QYOQrZ474+Bt4sY8QCAllszP9v5GQeQGovB5SPwlaDo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731459286; c=relaxed/simple; bh=yGbQ5LZb3K4ouo5/IVAj2bIoOta5TOuCDkkqCTX1MBY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Eo5tOFFHn6fDY2tuJSFHdF0urpK1X7RIX5xYTNF/4jez0yLAFxG7Qb5gVSVHyIiAF3/xuG87UDbJmZvj2ZMCcXAoCIKixp5/xAoXcdqbQWsi7r4+XM7nyNVz5+5rvr2ZjBwqdLDuhlBjv5HJPcRSjUQf3GZonhOrcQ0jzre7VvI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ER2h3VW6; 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="ER2h3VW6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E8A3C4CECD; Wed, 13 Nov 2024 00:54:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731459285; bh=yGbQ5LZb3K4ouo5/IVAj2bIoOta5TOuCDkkqCTX1MBY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ER2h3VW6HNC7vuONmOB8aR+yhSE4dYzPQ2jRkTkHBW2UB7Am2uv2ZCZJnTS2cSpJd 3yHWWKGlTgZOaQ94ams3eW8m5AAaQm/YCJBl/1mtaxNiD9pvi3hqBLvPHxb45ENs8W ah+DCax95ZI3i29FuYpCoR1IQyaOaQXSYFGGzRBEvf8hQ0ozSo8PAzeYOS4WsyIovs sEki+YUO+tGF+PqBhvGDfb72JYSosZddYty2/6cA1+sYJ5xmBXqfFzBuxh7opYQLAE ZVWepEqtMJLe5TSR6dLmPTnc5PfAnvp2Akjaxo77/ceVoar5/HlHKT88YlJGnOPlur ETc120MkwSuMg== From: Song Liu To: bpf@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org Cc: kernel-team@meta.com, andrii@kernel.org, eddyz87@gmail.com, ast@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, kpsingh@kernel.org, mattbobrowski@google.com, amir73il@gmail.com, repnop@google.com, jlayton@kernel.org, josef@toxicpanda.com, mic@digikod.net, gnoack@google.com, Song Liu Subject: [PATCH v3 bpf-next 4/4] selftest/bpf: Test inode local storage recursion prevention Date: Tue, 12 Nov 2024 16:53:51 -0800 Message-ID: <20241113005351.2197340-5-song@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241113005351.2197340-1-song@kernel.org> References: <20241113005351.2197340-1-song@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add selftest for recursion prevention logic of bpf local storage. When inode local storage function is traced, helpers that access inode local storage should return -EBUSY. The recurring program is attached to inode_storage_lookup(). This is not an ideal target for recursion tests. However, given that the target function have to take "struct inode *" argument, there isn't a better target function for the tests. Test results showed that inode_storage_lookup() is inlined in s390x. Work around this by adding this test to DENYLIST.s390x. Signed-off-by: Song Liu --- tools/testing/selftests/bpf/DENYLIST.s390x | 1 + .../bpf/prog_tests/inode_local_storage.c | 72 +++++++++++++++ .../bpf/progs/inode_storage_recursion.c | 90 +++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/inode_local_stor= age.c create mode 100644 tools/testing/selftests/bpf/progs/inode_storage_recursi= on.c diff --git a/tools/testing/selftests/bpf/DENYLIST.s390x b/tools/testing/sel= ftests/bpf/DENYLIST.s390x index 3ebd77206f98..6b8c9c9ec754 100644 --- a/tools/testing/selftests/bpf/DENYLIST.s390x +++ b/tools/testing/selftests/bpf/DENYLIST.s390x @@ -1,5 +1,6 @@ # TEMPORARY # Alphabetical order get_stack_raw_tp # user_stack corrupted user stack= (no backchain userspace) +inode_localstorage/recursion # target function (inode_storage_= lookup) is inlined on s390) stacktrace_build_id # compare_map_keys stackid_hmap v= s. stackmap err -2 errno 2 (?) verifier_iterating_callbacks diff --git a/tools/testing/selftests/bpf/prog_tests/inode_local_storage.c b= /tools/testing/selftests/bpf/prog_tests/inode_local_storage.c new file mode 100644 index 000000000000..a9d9f77216f4 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/inode_local_storage.c @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ + +#include +#include +#include +#include "inode_storage_recursion.skel.h" + +#define TDIR "/tmp/inode_local_storage" +#define TDIR_PARENT "/tmp" + +static void test_recursion(void) +{ + struct inode_storage_recursion *skel; + struct bpf_prog_info info; + __u32 info_len =3D sizeof(info); + int err, prog_fd, map_fd, inode_fd =3D -1; + long value; + + skel =3D inode_storage_recursion__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open_and_load")) + return; + + skel->bss->test_pid =3D getpid(); + + err =3D inode_storage_recursion__attach(skel); + if (!ASSERT_OK(err, "skel_attach")) + goto out; + + err =3D mkdir(TDIR, 0755); + if (!ASSERT_OK(err, "mkdir " TDIR)) + goto out; + + inode_fd =3D open(TDIR_PARENT, O_RDONLY | O_CLOEXEC); + if (!ASSERT_OK_FD(inode_fd, "open inode_fd")) + goto out; + + /* Detach so that the following lookup won't trigger + * trace_inode_storage_lookup and further change the values. + */ + inode_storage_recursion__detach(skel); + map_fd =3D bpf_map__fd(skel->maps.inode_map); + err =3D bpf_map_lookup_elem(map_fd, &inode_fd, &value); + ASSERT_OK(err, "lookup inode_map"); + + /* Check trace_inode_mkdir for the reason that value =3D=3D 201 */ + ASSERT_EQ(value, 201, "inode_map value"); + ASSERT_EQ(skel->bss->nr_del_errs, 1, "bpf_task_storage_delete busy"); + + prog_fd =3D bpf_program__fd(skel->progs.trace_inode_mkdir); + memset(&info, 0, sizeof(info)); + err =3D bpf_prog_get_info_by_fd(prog_fd, &info, &info_len); + ASSERT_OK(err, "get prog info"); + ASSERT_EQ(info.recursion_misses, 0, "trace_inode_mkdir prog recursion"); + + prog_fd =3D bpf_program__fd(skel->progs.trace_inode_storage_lookup); + memset(&info, 0, sizeof(info)); + err =3D bpf_prog_get_info_by_fd(prog_fd, &info, &info_len); + ASSERT_OK(err, "get prog info"); + ASSERT_EQ(info.recursion_misses, 3, "trace_inode_storage_lookup prog recu= rsion"); + +out: + rmdir(TDIR); + close(inode_fd); + inode_storage_recursion__destroy(skel); +} + +void test_inode_localstorage(void) +{ + if (test__start_subtest("recursion")) + test_recursion(); +} diff --git a/tools/testing/selftests/bpf/progs/inode_storage_recursion.c b/= tools/testing/selftests/bpf/progs/inode_storage_recursion.c new file mode 100644 index 000000000000..0ad36f8c6e04 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/inode_storage_recursion.c @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ + +#include "vmlinux.h" +#include +#include + +#ifndef EBUSY +#define EBUSY 16 +#endif + +char _license[] SEC("license") =3D "GPL"; +int nr_del_errs; +int test_pid; + +struct { + __uint(type, BPF_MAP_TYPE_INODE_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, long); +} inode_map SEC(".maps"); + +/* inode_storage_lookup is not an ideal hook for recursion tests, as it + * is static and more likely to get inlined. However, there isn't a + * better function for the test. This is because we need to call + * bpf_inode_storage_* helpers with an inode intput. Unlike task local + * storage, for which we can use bpf_get_current_task_btf() to get task + * pointer with BTF, for inode local storage, we need the get the inode + * pointer from function arguments. Other functions, such as, + * bpf_local_storage_get() does not take inode as input. + */ +SEC("fentry/inode_storage_lookup") +int BPF_PROG(trace_inode_storage_lookup, struct inode *inode) +{ + struct task_struct *task =3D bpf_get_current_task_btf(); + long *ptr; + int err; + + if (!test_pid || task->pid !=3D test_pid) + return 0; + + /* This doesn't have BPF_LOCAL_STORAGE_GET_F_CREATE, so it will + * not trigger on the first call of bpf_inode_storage_get() below. + * + * This is called twice, recursion_misses +=3D 2. + */ + ptr =3D bpf_inode_storage_get(&inode_map, inode, 0, 0); + if (ptr) { + *ptr +=3D 1; + + /* This is called once, recursion_misses +=3D 1. */ + err =3D bpf_inode_storage_delete(&inode_map, inode); + if (err =3D=3D -EBUSY) + nr_del_errs++; + } + + return 0; +} + +SEC("fentry/security_inode_mkdir") +int BPF_PROG(trace_inode_mkdir, struct inode *dir, + struct dentry *dentry, + int mode) +{ + struct task_struct *task =3D bpf_get_current_task_btf(); + long *ptr; + + if (!test_pid || task->pid !=3D test_pid) + return 0; + + /* Trigger trace_inode_storage_lookup, the first time */ + ptr =3D bpf_inode_storage_get(&inode_map, dir, 0, + BPF_LOCAL_STORAGE_GET_F_CREATE); + + /* trace_inode_storage_lookup cannot get ptr, so *ptr is 0. + * Set ptr to 200. + */ + if (ptr && !*ptr) + *ptr =3D 200; + + /* Trigger trace_inode_storage_lookup, the second time. + * trace_inode_storage_lookup can now get ptr and increase the + * value. Now the value is 201. + */ + bpf_inode_storage_get(&inode_map, dir, 0, + BPF_LOCAL_STORAGE_GET_F_CREATE); + + return 0; + +} --=20 2.43.5