Subject: Re: [PATCH bpf-next v3 3/4] bpf: cgroup: properly use bpf_prog_array api

From: Stanislav Fomichev <[email-masked]>
Date: Tue, 28 May 2019 13:16:46 -0700

On 05/28, Roman Gushchin wrote:
> On Tue, May 28, 2019 at 11:29:45AM -0700, Stanislav Fomichev wrote:
> > Now that we don't have __rcu markers on the bpf_prog_array helpers,
> > let's use proper rcu_dereference_protected to obtain array pointer
> > under mutex.
> > 
> > We also don't need __rcu annotations on cgroup_bpf.inactive since
> > it's not read/updated concurrently.
> > 
> > v3:
> > * amend cgroup_rcu_dereference to include percpu_ref_is_dying;
> >   cgroup_bpf is now reference counted and we don't hold cgroup_mutex
> >   anymore in cgroup_bpf_release
> > 
> > v2:
> > * replace xchg with rcu_swap_protected
> > 
> > Cc: Roman Gushchin <[email-masked]>
> > Signed-off-by: Stanislav Fomichev <[email-masked]>
> > ---
> >  include/linux/bpf-cgroup.h |  2 +-
> >  kernel/bpf/cgroup.c        | 32 +++++++++++++++++++++-----------
> >  2 files changed, 22 insertions(+), 12 deletions(-)
> > 
> > diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h
> > index 9f100fc422c3..b631ee75762d 100644
> > --- a/include/linux/bpf-cgroup.h
> > +++ b/include/linux/bpf-cgroup.h
> > @@ -72,7 +72,7 @@ struct cgroup_bpf {
> >  	u32 flags[MAX_BPF_ATTACH_TYPE];
> >  
> >  	/* temp storage for effective prog array used by prog_attach/detach */
> > -	struct bpf_prog_array __rcu *inactive;
> > +	struct bpf_prog_array *inactive;
> >  
> >  	/* reference counter used to detach bpf programs after cgroup removal */
> >  	struct percpu_ref refcnt;
> > diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
> > index d995edbe816d..118b70175dd9 100644
> > --- a/kernel/bpf/cgroup.c
> > +++ b/kernel/bpf/cgroup.c
> > @@ -22,6 +22,13 @@
> >  DEFINE_STATIC_KEY_FALSE(cgroup_bpf_enabled_key);
> >  EXPORT_SYMBOL(cgroup_bpf_enabled_key);
> >  
> > +#define cgroup_rcu_dereference(cgrp, p)					\
> > +	rcu_dereference_protected(p, lockdep_is_held(&cgroup_mutex) ||	\
> > +				  percpu_ref_is_dying(&cgrp->bpf.refcnt))
> 
> Some comments why percpu_ref_is_dying(&cgrp->bpf.refcnt) is enough here will
> be appreciated.
I was actually debating whether to just use raw
rcu_dereference_protected(p, lockdep_is_held()) in __cgroup_bpf_query and
rcu_dereference_protected(p, percpu_ref_is_dying()) in cgroup_bpf_release
instead of having a cgroup_rcu_dereference which covers both cases.

Maybe that should make it more clear (and doesn't require any comment)?