From: Rong Tao <rtoax@foxmail.com>
To: ast@kernel.org, daniel@iogearbox.net
Cc: rtoax@foxmail.com, rongtao@cestc.cn,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
Jiri Olsa <jolsa@kernel.org>, Mykola Lysenko <mykolal@fb.com>,
Shuah Khan <shuah@kernel.org>,
Juntong Deng <juntong.deng@outlook.com>,
Amery Hung <amery.hung@bytedance.com>,
Dave Marchevsky <davemarchevsky@fb.com>,
Hou Tao <houtao1@huawei.com>,
bpf@vger.kernel.org (open list:BPF [GENERAL] (Safe Dynamic
Programs and Tools)), linux-kernel@vger.kernel.org (open list),
linux-kselftest@vger.kernel.org (open list:KERNEL SELFTEST
FRAMEWORK)
Subject: [PATCH bpf-next 1/2] bpf: Add bpf_task_cwd_from_pid() kfunc
Date: Thu, 29 May 2025 11:32:34 +0800 [thread overview]
Message-ID: <tencent_97F8B56B340F51DB604B482FEBF012460505@qq.com> (raw)
In-Reply-To: <cover.1748488784.git.rtoax@foxmail.com>
From: Rong Tao <rongtao@cestc.cn>
It is a bit troublesome to get cwd based on pid in bpf program, such as
bpftrace example [1].
This patch therefore adds a new bpf_task_cwd_from_pid() kfunc which
allows BPF programs to get cwd from a pid.
[1] https://github.com/bpftrace/bpftrace/issues/3314
Signed-off-by: Rong Tao <rongtao@cestc.cn>
---
kernel/bpf/helpers.c | 45 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index b71e428ad936..0f32fbc997bb 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -24,6 +24,10 @@
#include <linux/bpf_mem_alloc.h>
#include <linux/kasan.h>
#include <linux/bpf_verifier.h>
+#include <linux/fs.h>
+#include <linux/fs_struct.h>
+#include <linux/path.h>
+#include <linux/string.h>
#include "../../lib/kstrtox.h"
@@ -2643,6 +2647,46 @@ __bpf_kfunc struct task_struct *bpf_task_from_vpid(s32 vpid)
return p;
}
+/**
+ * bpf_task_cwd_from_pid - Get a task's absolute pathname of the current
+ * working directory from its pid.
+ * @pid: The pid of the task being looked up.
+ * @buf: The array pointed to by buf.
+ * @buf_len: buf length.
+ */
+__bpf_kfunc int bpf_task_cwd_from_pid(s32 pid, char *buf, u32 buf_len)
+{
+ struct path pwd;
+ char kpath[256], *path;
+ struct task_struct *task;
+
+ if (!buf || buf_len == 0)
+ return -EINVAL;
+
+ rcu_read_lock();
+ task = pid_task(find_vpid(pid), PIDTYPE_PID);
+ if (!task) {
+ rcu_read_unlock();
+ return -ESRCH;
+ }
+ task_lock(task);
+ if (!task->fs) {
+ task_unlock(task);
+ return -ENOENT;
+ }
+ get_fs_pwd(task->fs, &pwd);
+ task_unlock(task);
+ rcu_read_unlock();
+
+ path = d_path(&pwd, kpath, sizeof(kpath));
+ path_put(&pwd);
+ if (IS_ERR(path))
+ return PTR_ERR(path);
+
+ strncpy(buf, path, buf_len);
+ return 0;
+}
+
/**
* bpf_dynptr_slice() - Obtain a read-only pointer to the dynptr data.
* @p: The dynptr whose data slice to retrieve
@@ -3314,6 +3358,7 @@ BTF_ID_FLAGS(func, bpf_task_get_cgroup1, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
#endif
BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_task_from_vpid, KF_ACQUIRE | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_task_cwd_from_pid, KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_throw)
#ifdef CONFIG_BPF_EVENTS
BTF_ID_FLAGS(func, bpf_send_signal_task, KF_TRUSTED_ARGS)
--
2.49.0
next parent reply other threads:[~2025-05-29 3:34 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <cover.1748488784.git.rtoax@foxmail.com>
2025-05-29 3:32 ` Rong Tao [this message]
2025-05-29 5:44 ` Alexei Starovoitov
2025-05-30 1:28 ` Rong Tao
2025-05-30 1:55 ` Yonghong Song
2025-05-30 6:34 ` Rong Tao
2025-06-02 6:20 ` Dan Carpenter
2025-05-29 3:32 ` [PATCH bpf-next 2/2] selftests/bpf: Add selftests for bpf_task_cwd_from_pid() Rong Tao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tencent_97F8B56B340F51DB604B482FEBF012460505@qq.com \
--to=rtoax@foxmail.com \
--cc=amery.hung@bytedance.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davemarchevsky@fb.com \
--cc=eddyz87@gmail.com \
--cc=haoluo@google.com \
--cc=houtao1@huawei.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=juntong.deng@outlook.com \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mykolal@fb.com \
--cc=rongtao@cestc.cn \
--cc=sdf@fomichev.me \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®