Subject: Re: [PATCH v9 bpf-next 13/14] selftests/bpf: Add test for d_path helper

From: Jiri Olsa <[email-masked]>
Date: Wed, 5 Aug 2020 20:00:34 +0200

On Tue, Aug 04, 2020 at 11:40:05PM -0700, Andrii Nakryiko wrote:

SNIP

> > +SEC("fentry/vfs_getattr")
> > +int BPF_PROG(prog_stat, struct path *path, struct kstat *stat,
> > +            __u32 request_mask, unsigned int query_flags)
> > +{
> > +       pid_t pid = bpf_get_current_pid_tgid() >> 32;
> > +       int ret;
> > +
> > +       if (pid != my_pid)
> > +               return 0;
> > +
> > +       if (cnt_stat >= MAX_FILES)
> > +               return 0;
> > +       ret = bpf_d_path(path, paths_stat[cnt_stat], MAX_PATH_LEN);
> > +
> > +       /* We need to recheck cnt_stat for verifier. */
> > +       if (cnt_stat >= MAX_FILES)
> > +               return 0;
> > +       rets_stat[cnt_stat] = ret;
> > +
> > +       cnt_stat++;
> > +       return 0;
> > +}
> > +
> > +SEC("fentry/filp_close")
> > +int BPF_PROG(prog_close, struct file *file, void *id)
> > +{
> > +       pid_t pid = bpf_get_current_pid_tgid() >> 32;
> > +       int ret;
> > +
> > +       if (pid != my_pid)
> > +               return 0;
> > +
> > +       if (cnt_close >= MAX_FILES)
> > +               return 0;
> > +       ret = bpf_d_path(&file->f_path,
> > +                        paths_close[cnt_close], MAX_PATH_LEN);
> > +
> > +       /* We need to recheck cnt_stat for verifier. */
> 
> you need to do it because you are re-reading a global variable; if you
> stored cnt_close in a local variable, then did >= MAX_FILES check
> once, you probably could have avoided this duplication. Same for
> another instance above.

I see, nice.. I'll update both comments

thanks,
jirka