All of lore.kernel.org
 help / color / mirror / Atom feed
From: Puranjay Mohan <puranjay@kernel.org>
To: Eduard Zingerman <eddyz87@gmail.com>, bpf@vger.kernel.org
Cc: kkd@meta.com, Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <martin.lau@kernel.org>,
	kernel-team@fb.com
Subject: Re: [PATCH bpf-next] bpf: support nested rcu critical sections
Date: Wed, 17 Sep 2025 13:44:58 +0000	[thread overview]
Message-ID: <mb61pcy7pqjl1.fsf@kernel.org> (raw)
In-Reply-To: <ea1536afc399eeda111f6f8e7c45ba81108fef6d.camel@gmail.com>

Eduard Zingerman <eddyz87@gmail.com> writes:

> On Tue, 2025-09-16 at 11:36 +0000, Puranjay Mohan wrote:
>> Currently, nested rcu critical sections are rejected by the verifier and
>> rcu_lock state is managed by a boolean variable. Add support for nested
>> rcu critical sections by make active_rcu_locks a counter similar to
>> active_preempt_locks. bpf_rcu_read_lock() increments this counter and
>> bpf_rcu_read_unlock() decrements it, MEM_RCU -> PTR_UNTRUSTED transition
>> happens when active_rcu_locks drops to 0.
>> 
>> Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
>> ---
>
> [...]
>
>> @@ -13874,22 +13874,22 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
>>  		}
>>  
>>  		if (rcu_lock) {
>> -			verbose(env, "nested rcu read lock (kernel function %s)\n", func_name);
>> -			return -EINVAL;
>> +			env->cur_state->active_rcu_locks++;
>>  		} else if (rcu_unlock) {
>> -			bpf_for_each_reg_in_vstate_mask(env->cur_state, state, reg, clear_mask, ({
>> -				if (reg->type & MEM_RCU) {
>> -					reg->type &= ~(MEM_RCU | PTR_MAYBE_NULL);
>> -					reg->type |= PTR_UNTRUSTED;
>> -				}
>> -			}));
>> -			env->cur_state->active_rcu_lock = false;
>> +			if (--env->cur_state->active_rcu_locks == 0) {
>> +				bpf_for_each_reg_in_vstate_mask(env->cur_state, state, reg, clear_mask, ({
>> +					if (reg->type & MEM_RCU) {
>> +						reg->type &= ~(MEM_RCU | PTR_MAYBE_NULL);
>> +						reg->type |= PTR_UNTRUSTED;
>> +					}
>> +				}));
>> +			}
>>  		} else if (sleepable) {
>>  			verbose(env, "kernel func %s is sleepable within rcu_read_lock region\n", func_name);
>>  			return -EACCES;
>>  		}
>>  	} else if (rcu_lock) {
>> -		env->cur_state->active_rcu_lock = true;
>> +		env->cur_state->active_rcu_locks++;
>>  	} else if (rcu_unlock) {
>>  		verbose(env, "unmatched rcu read unlock (kernel function %s)\n", func_name);
>>  		return -EINVAL;
>
> Nit: active_rcu_locks increment in two places can be avoided e.g. as follows:
>
>         if (rcu_lock) {
>                 env->cur_state->active_rcu_locks++;
>         } else if (rcu_unlock) {
>                 struct bpf_func_state *state;
>                 struct bpf_reg_state *reg;
>                 u32 clear_mask = (1 << STACK_SPILL) | (1 << STACK_ITER);
>
>                 if (env->cur_state->active_rcu_locks == 0) {
>                         verbose(private_data: env, fmt: "unmatched rcu read unlock (kernel function %s)\n", func_name);
>                         return -EINVAL;
>                 }
>                 if (--env->cur_state->active_rcu_locks == 0) {
>                         bpf_for_each_reg_in_vstate_mask(env->cur_state, state, reg, clear_mask, ({
>                                 if (reg->type & MEM_RCU) {
>                                         reg->type &= ~(MEM_RCU | PTR_MAYBE_NULL);
>                                         reg->type |= PTR_UNTRUSTED;
>                                 }
>                         }));
>                 }
>         } else if (sleepable) {
>                 verbose(private_data: env, fmt: "kernel func %s is sleepable within rcu_read_lock region\n", func_name);
>                 return -EACCES;
>         }
>
>         if (in_rbtree_lock_required_cb(env) && (rcu_lock || rcu_unlock)) {
>                 verbose(private_data: env, fmt: "Calling bpf_rcu_read_{lock,unlock} in unnecessary rbtree callback\n");
>                 return -EACCES;
>         }
>
> [...]

I agree, this looks better. Will use it in the next version.

Thanks,
Puranjay

  reply	other threads:[~2025-09-17 13:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-16 11:36 [PATCH bpf-next] bpf: support nested rcu critical sections Puranjay Mohan
2025-09-16 20:34 ` Alexei Starovoitov
2025-09-17 13:43   ` Puranjay Mohan
2025-09-16 23:26 ` Eduard Zingerman
2025-09-17 13:44   ` Puranjay Mohan [this message]
2025-09-17  0:59 ` Kumar Kartikeya Dwivedi
2025-09-17  4:51 ` Leon Hwang
2025-09-17 14:03   ` Puranjay Mohan
2025-09-18  2:25     ` Leon Hwang

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=mb61pcy7pqjl1.fsf@kernel.org \
    --to=puranjay@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=kernel-team@fb.com \
    --cc=kkd@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.