* can add a new bpf helper function bpf_map_compare_and_update_elem?
@ 2021-11-25 8:34 Yahui Chen
2021-11-26 20:25 ` Cong Wang
0 siblings, 1 reply; 3+ messages in thread
From: Yahui Chen @ 2021-11-25 8:34 UTC (permalink / raw)
To: bpf@vger.kernel.org; +Cc: zhangwei123171
Suppose we have a map, MAP_A, and the user program does the following:
1. bpf_map_lookup_elem(MAP_A, key, value)
2. change the value
3. bpf_map_update_elem(MAP_A, key, value, FLAG)
At the same time, the kernel's BPF program may also be modifying the value.
Then we have concurrency problems.
Therefore, can we add a helper function like compare and swap?
Let's call it bpf_map_compare_and_update_elem.
So, the map operations will be modified as follows:
for true {
1. bpf_map_lookup_elem(MAP_A, key, old_value)
2. change value
3. ret = bpf_map_compare_and_update_elem(MAP_A, key, old_value,
new_value, FLAG)
4. if ret == 0 { break }
}
thank you
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: can add a new bpf helper function bpf_map_compare_and_update_elem?
2021-11-25 8:34 can add a new bpf helper function bpf_map_compare_and_update_elem? Yahui Chen
@ 2021-11-26 20:25 ` Cong Wang
2021-11-29 6:49 ` Yahui Chen
0 siblings, 1 reply; 3+ messages in thread
From: Cong Wang @ 2021-11-26 20:25 UTC (permalink / raw)
To: Yahui Chen; +Cc: bpf@vger.kernel.org, zhangwei123171
On Thu, Nov 25, 2021 at 04:34:48PM +0800, Yahui Chen wrote:
> Suppose we have a map, MAP_A, and the user program does the following:
>
> 1. bpf_map_lookup_elem(MAP_A, key, value)
> 2. change the value
> 3. bpf_map_update_elem(MAP_A, key, value, FLAG)
>
> At the same time, the kernel's BPF program may also be modifying the value.
>
> Then we have concurrency problems.
>
This is why we have bpf spinlock. ;)
> Therefore, can we add a helper function like compare and swap?
>
I don't think you can atomically compare and swap values larger than
CPU word size.
Thanks.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: can add a new bpf helper function bpf_map_compare_and_update_elem?
2021-11-26 20:25 ` Cong Wang
@ 2021-11-29 6:49 ` Yahui Chen
0 siblings, 0 replies; 3+ messages in thread
From: Yahui Chen @ 2021-11-29 6:49 UTC (permalink / raw)
To: Cong Wang; +Cc: bpf@vger.kernel.org, zhangwei123171, zhaojianxing
Cong Wang <xiyou.wangcong@gmail.com> 于2021年11月27日周六 上午4:25写道:
>
> On Thu, Nov 25, 2021 at 04:34:48PM +0800, Yahui Chen wrote:
> > Suppose we have a map, MAP_A, and the user program does the following:
> >
> > 1. bpf_map_lookup_elem(MAP_A, key, value)
> > 2. change the value
> > 3. bpf_map_update_elem(MAP_A, key, value, FLAG)
> >
> > At the same time, the kernel's BPF program may also be modifying the value.
> >
> > Then we have concurrency problems.
> >
>
> This is why we have bpf spinlock. ;)
>
I get a bpf_spinlock example from
https://patchwork.ozlabs.org/project/netdev/patch/20190124041403.2100609-2-ast@kernel.org/:
```
Example:
struct hash_elem {
int cnt;
struct bpf_spin_lock lock;
};
struct hash_elem * val = bpf_map_lookup_elem(&hash_map, &key);
if (val) {
bpf_spin_lock(&val->lock);
val->cnt++;
bpf_spin_unlock(&val->lock);
}
```
But, I think bpf_spinlock cannot be used in user mode.
>
> > Therefore, can we add a helper function like compare and swap?
> >
>
> I don't think you can atomically compare and swap values larger than
> CPU word size.
>
Yep, you are right.
So I imagined adding something code like `memcmp` to the kernel before
update bpf map element.
If memcmp success then update the element, otherwise return fail.
New helper function looks like:
bpf_map_compare_and_update_elem(MAP_A, key, old_value,
new_value, FLAG)
>
> Thanks.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-11-29 6:54 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-11-25 8:34 can add a new bpf helper function bpf_map_compare_and_update_elem? Yahui Chen
2021-11-26 20:25 ` Cong Wang
2021-11-29 6:49 ` Yahui Chen
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.