BPF List
 help / color / mirror / Atom feed
From: Jiri Olsa <olsajiri@gmail.com>
To: Andrii Nakryiko <andrii@kernel.org>
Cc: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net,
	martin.lau@kernel.org, kernel-team@meta.com
Subject: Re: [PATCH v2 bpf-next] bpf: consistently use bpf_rcu_lock_held() everywhere
Date: Wed, 15 Oct 2025 11:27:58 +0200	[thread overview]
Message-ID: <aO9pHt32T8BI3Rlt@krava> (raw)
In-Reply-To: <20251014201403.4104511-1-andrii@kernel.org>

On Tue, Oct 14, 2025 at 01:14:03PM -0700, Andrii Nakryiko wrote:
> We have many places which open-code what's now is bpf_rcu_lock_held()
> macro, so replace all those places with a clean and short macro invocation.
> For that, move bpf_rcu_lock_held() macro into include/linux/bpf.h.
> 
> Signed-off-by: Andrii Nakryiko <andrii@kernel.org>

Acked-by: Jiri Olsa <jolsa@kernel.org>

jirka

> ---
> v1->v2:
>   - move bpf_rcu_lock_held() outside of #ifdef CONFIG_BPF_SYSCALL area (kernel
>     test robot).
> 
>  include/linux/bpf.h               |  3 +++
>  include/linux/bpf_local_storage.h |  3 ---
>  kernel/bpf/hashtab.c              | 21 +++++++--------------
>  kernel/bpf/helpers.c              | 12 ++++--------
>  4 files changed, 14 insertions(+), 25 deletions(-)
> 
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index f87fb203aaae..86afd9ac6848 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
> @@ -2381,6 +2381,9 @@ bpf_prog_run_array_uprobe(const struct bpf_prog_array *array,
>  bool bpf_jit_bypass_spec_v1(void);
>  bool bpf_jit_bypass_spec_v4(void);
>  
> +#define bpf_rcu_lock_held() \
> +	(rcu_read_lock_held() || rcu_read_lock_trace_held() || rcu_read_lock_bh_held())
> +
>  #ifdef CONFIG_BPF_SYSCALL
>  DECLARE_PER_CPU(int, bpf_prog_active);
>  extern struct mutex bpf_stats_enabled_mutex;
> diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h
> index ab7244d8108f..782f58feea35 100644
> --- a/include/linux/bpf_local_storage.h
> +++ b/include/linux/bpf_local_storage.h
> @@ -18,9 +18,6 @@
>  
>  #define BPF_LOCAL_STORAGE_CACHE_SIZE	16
>  
> -#define bpf_rcu_lock_held()                                                    \
> -	(rcu_read_lock_held() || rcu_read_lock_trace_held() ||                 \
> -	 rcu_read_lock_bh_held())
>  struct bpf_local_storage_map_bucket {
>  	struct hlist_head list;
>  	raw_spinlock_t lock;
> diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
> index e7a6ba04dc82..f876f09355f0 100644
> --- a/kernel/bpf/hashtab.c
> +++ b/kernel/bpf/hashtab.c
> @@ -657,8 +657,7 @@ static void *__htab_map_lookup_elem(struct bpf_map *map, void *key)
>  	struct htab_elem *l;
>  	u32 hash, key_size;
>  
> -	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
> -		     !rcu_read_lock_bh_held());
> +	WARN_ON_ONCE(!bpf_rcu_lock_held());
>  
>  	key_size = map->key_size;
>  
> @@ -1086,8 +1085,7 @@ static long htab_map_update_elem(struct bpf_map *map, void *key, void *value,
>  		/* unknown flags */
>  		return -EINVAL;
>  
> -	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
> -		     !rcu_read_lock_bh_held());
> +	WARN_ON_ONCE(!bpf_rcu_lock_held());
>  
>  	key_size = map->key_size;
>  
> @@ -1194,8 +1192,7 @@ static long htab_lru_map_update_elem(struct bpf_map *map, void *key, void *value
>  		/* unknown flags */
>  		return -EINVAL;
>  
> -	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
> -		     !rcu_read_lock_bh_held());
> +	WARN_ON_ONCE(!bpf_rcu_lock_held());
>  
>  	key_size = map->key_size;
>  
> @@ -1263,8 +1260,7 @@ static long htab_map_update_elem_in_place(struct bpf_map *map, void *key,
>  		/* unknown flags */
>  		return -EINVAL;
>  
> -	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
> -		     !rcu_read_lock_bh_held());
> +	WARN_ON_ONCE(!bpf_rcu_lock_held());
>  
>  	key_size = map->key_size;
>  
> @@ -1326,8 +1322,7 @@ static long __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
>  		/* unknown flags */
>  		return -EINVAL;
>  
> -	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
> -		     !rcu_read_lock_bh_held());
> +	WARN_ON_ONCE(!bpf_rcu_lock_held());
>  
>  	key_size = map->key_size;
>  
> @@ -1404,8 +1399,7 @@ static long htab_map_delete_elem(struct bpf_map *map, void *key)
>  	u32 hash, key_size;
>  	int ret;
>  
> -	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
> -		     !rcu_read_lock_bh_held());
> +	WARN_ON_ONCE(!bpf_rcu_lock_held());
>  
>  	key_size = map->key_size;
>  
> @@ -1440,8 +1434,7 @@ static long htab_lru_map_delete_elem(struct bpf_map *map, void *key)
>  	u32 hash, key_size;
>  	int ret;
>  
> -	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
> -		     !rcu_read_lock_bh_held());
> +	WARN_ON_ONCE(!bpf_rcu_lock_held());
>  
>  	key_size = map->key_size;
>  
> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index dea8443f782c..825280c953be 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c
> @@ -42,8 +42,7 @@
>   */
>  BPF_CALL_2(bpf_map_lookup_elem, struct bpf_map *, map, void *, key)
>  {
> -	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
> -		     !rcu_read_lock_bh_held());
> +	WARN_ON_ONCE(!bpf_rcu_lock_held());
>  	return (unsigned long) map->ops->map_lookup_elem(map, key);
>  }
>  
> @@ -59,8 +58,7 @@ const struct bpf_func_proto bpf_map_lookup_elem_proto = {
>  BPF_CALL_4(bpf_map_update_elem, struct bpf_map *, map, void *, key,
>  	   void *, value, u64, flags)
>  {
> -	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
> -		     !rcu_read_lock_bh_held());
> +	WARN_ON_ONCE(!bpf_rcu_lock_held());
>  	return map->ops->map_update_elem(map, key, value, flags);
>  }
>  
> @@ -77,8 +75,7 @@ const struct bpf_func_proto bpf_map_update_elem_proto = {
>  
>  BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key)
>  {
> -	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
> -		     !rcu_read_lock_bh_held());
> +	WARN_ON_ONCE(!bpf_rcu_lock_held());
>  	return map->ops->map_delete_elem(map, key);
>  }
>  
> @@ -134,8 +131,7 @@ const struct bpf_func_proto bpf_map_peek_elem_proto = {
>  
>  BPF_CALL_3(bpf_map_lookup_percpu_elem, struct bpf_map *, map, void *, key, u32, cpu)
>  {
> -	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held() &&
> -		     !rcu_read_lock_bh_held());
> +	WARN_ON_ONCE(!bpf_rcu_lock_held());
>  	return (unsigned long) map->ops->map_lookup_percpu_elem(map, key, cpu);
>  }
>  
> -- 
> 2.47.3
> 
> 

  reply	other threads:[~2025-10-15  9:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-14 20:14 [PATCH v2 bpf-next] bpf: consistently use bpf_rcu_lock_held() everywhere Andrii Nakryiko
2025-10-15  9:27 ` Jiri Olsa [this message]
2025-10-15 10:31 ` patchwork-bot+netdevbpf

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=aO9pHt32T8BI3Rlt@krava \
    --to=olsajiri@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@meta.com \
    --cc=martin.lau@kernel.org \
    /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