On 3/26/26 07:26, Martin KaFai Lau wrote:
> On 3/15/26 4:58 PM, Michal Luczaj wrote:
>>> Beside, from looking at the may_update_sockmap(), I don't know if it is
>>> even doable (or useful) to bpf_map_update_elem(unix_sk) in
>>> tc/flow_dissector/xdp. One possible path is the SOCK_FILTER when looking
>>> at unix_dgram_sendmsg() => sk_filter(). It was not the original use case
>>> when the bpf_map_update_elem(sockmap) support was added iirc.
>>
>> What about a situation when unix_sk is stored in a sockmap, then tc prog
>> looks it up and invokes bpf_map_update_elem(unix_sk)? I'm not sure it's
>> useful, but seems doable.
> 
> [ Sorry for the late reply ]
> 
> It is a bummer that the bpf_map_update_elem(unix_sk) path is possible 
> from tc :(
> 
> Then unix_state_lock() in its current form cannot be safely acquired in 
> sock_map_update_elem(). It is currently a spin_lock() instead of 
> spin_lock_bh().

Is there a specific deadlock you have in your mind?

>>> The only path left is bpf_iter, which I believe was the primary use case
>>> when adding bpf_map_update_elem(sockmap) support [1]. It would be nice
>>> to avoid bh_lock_sock() when calling from all bpf_iter (tcp/udp/unix)
>>> where lock_sock() has already been done. It is more for
>>> reading-correctness though. This just came to my mind.
>>> has_current_bpf_ctx() can be used to check this. sockopt_lock_sock() has
>>> been using it to conditionally take lock_sock() or not.
>>
>> [ One clarification: bh_lock_sock() is a sock_map_update_elem() thing,
>> which can only be called by a bpf prog. IOW, has_current_bpf_ctx() is
>> always `true` in sock_map_update_elem(), right? ]
> 
> For all the bpf prog types allowed by may_update_sockmap() to do 
> bpf_map_update_elem(sockmap), only BPF_TRACE_ITER should have 
> has_current_bpf_ctx() == true. The tc prog (and others allowed in 
> may_update_sockmap()) will have has_current_bpf_ctx() == false when 
> calling sock_map_update_elem().

OK, so let's take test_sockmap_update.c:copy_sock_map(). It is a tc prog
and it calls bpf_map_update_elem() -> sock_map_update_elem(), right? But
running `test_progs -t "sockmap_basic/sockmap update"` shows (pr_warn() in
sock_map_update_elem()) that has_current_bpf_ctx() == true. That's expected
and has_current_bpf_ctx() would be false if sock_map_update_elem() was ran
via a hook?

> The tc case of bpf_map_update_elem(unix_sk) is unfortunate and requires 
> going back to the drawing board. I think checking unix_peer(sk) for NULL 
> without acquiring unix_state_lock() is needed for the 
> sock_map_update_elem() path, since changing unix_state_lock() for this 
> unknown use case seems overkill.
> 
> Whether sock_map_update_elem_"sys"() needs unix_state_lock() is up for 
> debate.

All right, I'll re-spin the series reverting back to v1.

> For bpf_iter_unix_seq_show(), one thought is to add unix_state_lock() 
> there before running the bpf iter prog. iiuc, it is what Kuniyuki has in 
> mind also to allow bpf iter prog having a stable view of unix_sock. This 
> could be a followup.
> [fwiw, it was why I first thought of has_current_bpf_ctx() to avoid 
> sock_map_update_elem() taking unix_state_lock() again if 
> bpf_iter_unix_seq_show() acquires unix_state_lock() earlier. I later 
> concluded (but proved to be incorrect) that tc cannot call 
> bpf_map_update_elem(unix_sk).]
> 
>>
>> Let me know if I'm correctly rephrasing your idea: assume all bpf-context
>> callers hold the socket locked or keep it "stable" (meaning: "sk won't
>> surprise sockmap update by some breaking state change coming from another
>> thread"). As you said, most bpf iters already take the sock_lock(), and I
> 
> Right, all bpf iter (udp, tcp, unix) has acquired the lock_sock() before 
> running the bpf iter prog. afaik, the only exception is netlink bpf iter 
> but it cannot be added to sock_map afaik.

And sock_{map,hash}_seq_show() (being a part of bpf iter machinery) needs
to take lock_sock() just as well? Would that require a special-casing
(unix_state_lock()) for af_unix?

>> have a patch that fixes sock_{map,hash}_seq_show(). Then we could try
>> dropping that bh_lock_sock().
>>
>>> [ I would still keep patch 3 though. ]
>>
>> Right.
>>
>>> [1]: https://lore.kernel.org/bpf/[email protected]/
>>>
>>>>
>>>> In a parallel thread I've asked Kuniyuki if it might be better to
>>>> completely drop patch 2/5, which would change how we interact with
>>>> sock_map_close(). Lets see how it goes.
>>>>
>>>
>>> If patch 2 is dropped, lock_sock() is always needed for unix_sk?
>>
>> For sock_map_update_elem_sys() I wanted to lock_sock()+unix_state_lock()
>> following Kuniyuki's suggestion to keep locking pattern/order (that repeats
>> when unix bpf iter prog invokes bpf_map_update_elem() ->
>> sock_map_update_elem()). For sock_map_update_elem() not, we can't sleep 
>> there.
> 


Reply via email to