2022-09-30 20:50:37

by David Vernet

[permalink] [raw]
Subject: [PATCH 1/2] bpf: Add kfuncs for storing struct task_struct * as a kptr

Now that BPF supports adding new kernel functions with kfuncs, and
storing kernel objects in maps with kptrs, we can add a set of kfuncs
which allow struct task_struct objects to be stored in maps as
referenced kptrs. The possible use-cases for doing this are plentiful.
During tracing, for example, it would be useful to be able to collect
some tasks that performed a certain operation, and then periodically
summarize who they are, which cgroup they're in, how much CPU time
they've spent, etc.

In order to enable this, this patch adds three new kfuncs:

struct task_struct *bpf_task_acquire(struct task_struct *p);
struct task_struct *bpf_task_kptr_get(struct task_struct **pp);
void bpf_task_release(struct task_struct *p);

A follow-on patch will add selftests validating these kfuncs.

Signed-off-by: David Vernet <[email protected]>
---
kernel/bpf/helpers.c | 75 +++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 74 insertions(+), 1 deletion(-)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index b069517a3da0..eb9950eaec35 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -1711,9 +1711,82 @@ static const struct btf_kfunc_id_set tracing_kfunc_set = {
.set = &tracing_btf_ids,
};

+/**
+ * bpf_task_acquire - Acquire a reference to a task. A task acquired by this
+ * kfunc which is not stored in a map as a kptr, must be released by calling
+ * bpf_task_release().
+ */
+__used noinline
+struct task_struct *bpf_task_acquire(struct task_struct *p)
+{
+ refcount_inc(&p->rcu_users);
+ return p;
+}
+
+/**
+ * bpf_task_kptr_get - Acquire a reference on a struct task_struct * kptr. A
+ * task kptr acquired by this kfunc which is not stored in a map as a kptr,
+ * must be released by calling bpf_task_release().
+ */
+__used noinline
+struct task_struct *bpf_task_kptr_get(struct task_struct **pp)
+{
+ struct task_struct *p;
+
+ rcu_read_lock();
+ p = READ_ONCE(*pp);
+ if (p && !refcount_inc_not_zero(&p->rcu_users))
+ p = NULL;
+ rcu_read_unlock();
+
+ return p;
+}
+
+/**
+ * bpf_task_release - Release the reference acquired on a struct task_struct *.
+ * If this kfunc is invoked in an RCU read region, the task_struct is
+ * guaranteed to not be freed until the current grace period has ended, even if
+ * its refcount drops to 0.
+ */
+__used noinline void bpf_task_release(struct task_struct *p)
+{
+ if (!p)
+ return;
+
+ put_task_struct_rcu_user(p);
+}
+
+BTF_SET8_START(task_kfunc_btf_ids)
+BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE)
+BTF_ID_FLAGS(func, bpf_task_kptr_get, KF_ACQUIRE | KF_KPTR_GET | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_task_release, KF_RELEASE | KF_TRUSTED_ARGS)
+BTF_SET8_END(task_kfunc_btf_ids)
+
+static const struct btf_kfunc_id_set task_kfunc_set = {
+ .owner = THIS_MODULE,
+ .set = &task_kfunc_btf_ids,
+};
+
+BTF_ID_LIST(task_dtor_kfunc_ids)
+BTF_ID(struct, task_struct)
+BTF_ID(func, bpf_task_release)
+
static int __init kfunc_init(void)
{
- return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &tracing_kfunc_set);
+ int ret;
+ const struct btf_id_dtor_kfunc helper_dtor_kfuncs[] = {
+ {
+ .btf_id = task_dtor_kfunc_ids[0],
+ .kfunc_btf_id = task_dtor_kfunc_ids[1]
+ },
+ };
+
+ ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &tracing_kfunc_set);
+ ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &task_kfunc_set);
+ ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &task_kfunc_set);
+ return ret ?: register_btf_id_dtor_kfuncs(helper_dtor_kfuncs,
+ ARRAY_SIZE(helper_dtor_kfuncs),
+ THIS_MODULE);
}

late_initcall(kfunc_init);
--
2.37.3


2022-09-30 23:29:22

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH 1/2] bpf: Add kfuncs for storing struct task_struct * as a kptr

Hi David,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on bpf-next/master]
[also build test WARNING on next-20220930]
[cannot apply to bpf/master linus/master v6.0-rc7]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/David-Vernet/Support-storing-struct-task_struct-objects-as-kptrs/20221001-044848
base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: m68k-allyesconfig
compiler: m68k-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/bcd8fc859e8153905211ff7ac025680507884bb8
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review David-Vernet/Support-storing-struct-task_struct-objects-as-kptrs/20221001-044848
git checkout bcd8fc859e8153905211ff7ac025680507884bb8
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=m68k SHELL=/bin/bash kernel/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>

All warnings (new ones prefixed by >>):

>> kernel/bpf/helpers.c:1720:21: warning: no previous prototype for 'bpf_task_acquire' [-Wmissing-prototypes]
1720 | struct task_struct *bpf_task_acquire(struct task_struct *p)
| ^~~~~~~~~~~~~~~~
>> kernel/bpf/helpers.c:1732:21: warning: no previous prototype for 'bpf_task_kptr_get' [-Wmissing-prototypes]
1732 | struct task_struct *bpf_task_kptr_get(struct task_struct **pp)
| ^~~~~~~~~~~~~~~~~
>> kernel/bpf/helpers.c:1751:22: warning: no previous prototype for 'bpf_task_release' [-Wmissing-prototypes]
1751 | __used noinline void bpf_task_release(struct task_struct *p)
| ^~~~~~~~~~~~~~~~


vim +/bpf_task_acquire +1720 kernel/bpf/helpers.c

1713
1714 /**
1715 * bpf_task_acquire - Acquire a reference to a task. A task acquired by this
1716 * kfunc which is not stored in a map as a kptr, must be released by calling
1717 * bpf_task_release().
1718 */
1719 __used noinline
> 1720 struct task_struct *bpf_task_acquire(struct task_struct *p)
1721 {
1722 refcount_inc(&p->rcu_users);
1723 return p;
1724 }
1725
1726 /**
1727 * bpf_task_kptr_get - Acquire a reference on a struct task_struct * kptr. A
1728 * task kptr acquired by this kfunc which is not stored in a map as a kptr,
1729 * must be released by calling bpf_task_release().
1730 */
1731 __used noinline
> 1732 struct task_struct *bpf_task_kptr_get(struct task_struct **pp)
1733 {
1734 struct task_struct *p;
1735
1736 rcu_read_lock();
1737 p = READ_ONCE(*pp);
1738 if (p && !refcount_inc_not_zero(&p->rcu_users))
1739 p = NULL;
1740 rcu_read_unlock();
1741
1742 return p;
1743 }
1744
1745 /**
1746 * bpf_task_release - Release the reference acquired on a struct task_struct *.
1747 * If this kfunc is invoked in an RCU read region, the task_struct is
1748 * guaranteed to not be freed until the current grace period has ended, even if
1749 * its refcount drops to 0.
1750 */
> 1751 __used noinline void bpf_task_release(struct task_struct *p)
1752 {
1753 if (!p)
1754 return;
1755
1756 put_task_struct_rcu_user(p);
1757 }
1758

--
0-DAY CI Kernel Test Service
https://01.org/lkp


Attachments:
(No filename) (3.86 kB)
config (284.92 kB)
Download all attachments