Re: [PATCH bpf-next 1/2] bpf: Add bpf_task_cwd_from_pid() kfunc

From: Dan Carpenter
Date: Mon Jun 02 2025 - 02:20:30 EST


Hi Rong,

kernel test robot noticed the following build warnings:

url: https://github.com/intel-lab-lkp/linux/commits/Rong-Tao/selftests-bpf-Add-selftests-for-bpf_task_cwd_from_pid/20250529-113933
base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link: https://lore.kernel.org/r/tencent_97F8B56B340F51DB604B482FEBF012460505%40qq.com
patch subject: [PATCH bpf-next 1/2] bpf: Add bpf_task_cwd_from_pid() kfunc
config: x86_64-randconfig-161-20250529 (https://download.01.org/0day-ci/archive/20250530/202505300432.nZC50gOu-lkp@xxxxxxxxx/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@xxxxxxxxx>
| Reported-by: Dan Carpenter <dan.carpenter@xxxxxxxxxx>
| Closes: https://lore.kernel.org/r/202505300432.nZC50gOu-lkp@xxxxxxxxx/

smatch warnings:
kernel/bpf/helpers.c:2687 bpf_task_cwd_from_pid() warn: inconsistent returns 'rcu_read'.

vim +/rcu_read +2687 kernel/bpf/helpers.c

b24383bde5a454 Rong Tao 2025-05-29 2657 __bpf_kfunc int bpf_task_cwd_from_pid(s32 pid, char *buf, u32 buf_len)
b24383bde5a454 Rong Tao 2025-05-29 2658 {
b24383bde5a454 Rong Tao 2025-05-29 2659 struct path pwd;
b24383bde5a454 Rong Tao 2025-05-29 2660 char kpath[256], *path;
b24383bde5a454 Rong Tao 2025-05-29 2661 struct task_struct *task;
b24383bde5a454 Rong Tao 2025-05-29 2662
b24383bde5a454 Rong Tao 2025-05-29 2663 if (!buf || buf_len == 0)
b24383bde5a454 Rong Tao 2025-05-29 2664 return -EINVAL;
b24383bde5a454 Rong Tao 2025-05-29 2665
b24383bde5a454 Rong Tao 2025-05-29 2666 rcu_read_lock();
b24383bde5a454 Rong Tao 2025-05-29 2667 task = pid_task(find_vpid(pid), PIDTYPE_PID);
b24383bde5a454 Rong Tao 2025-05-29 2668 if (!task) {
b24383bde5a454 Rong Tao 2025-05-29 2669 rcu_read_unlock();
b24383bde5a454 Rong Tao 2025-05-29 2670 return -ESRCH;
b24383bde5a454 Rong Tao 2025-05-29 2671 }
b24383bde5a454 Rong Tao 2025-05-29 2672 task_lock(task);
b24383bde5a454 Rong Tao 2025-05-29 2673 if (!task->fs) {
b24383bde5a454 Rong Tao 2025-05-29 2674 task_unlock(task);
b24383bde5a454 Rong Tao 2025-05-29 2675 return -ENOENT;

rcu_read_unlock();

b24383bde5a454 Rong Tao 2025-05-29 2676 }
b24383bde5a454 Rong Tao 2025-05-29 2677 get_fs_pwd(task->fs, &pwd);
b24383bde5a454 Rong Tao 2025-05-29 2678 task_unlock(task);
b24383bde5a454 Rong Tao 2025-05-29 2679 rcu_read_unlock();
b24383bde5a454 Rong Tao 2025-05-29 2680
b24383bde5a454 Rong Tao 2025-05-29 2681 path = d_path(&pwd, kpath, sizeof(kpath));
b24383bde5a454 Rong Tao 2025-05-29 2682 path_put(&pwd);
b24383bde5a454 Rong Tao 2025-05-29 2683 if (IS_ERR(path))
b24383bde5a454 Rong Tao 2025-05-29 2684 return PTR_ERR(path);
b24383bde5a454 Rong Tao 2025-05-29 2685
b24383bde5a454 Rong Tao 2025-05-29 2686 strncpy(buf, path, buf_len);
b24383bde5a454 Rong Tao 2025-05-29 @2687 return 0;
b24383bde5a454 Rong Tao 2025-05-29 2688 }

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki