1. Jun 30, 2022
  2. Jun 29, 2022
  3. Jun 28, 2022
    • John Fastabend's avatar
      bpf: Fix sockmap calling sleepable function in teardown path · 697fb80a
      John Fastabend authored
      syzbot reproduced the bug ...
      
       BUG: sleeping function called from invalid context at kernel/workqueue.c:3010
      
      ... with the following stack trace fragment ...
      
       start_flush_work kernel/workqueue.c:3010 [inline]
       __flush_work+0x109/0xb10 kernel/workqueue.c:3074
       __cancel_work_timer+0x3f9/0x570 kernel/workqueue.c:3162
       sk_psock_stop+0x4cb/0x630 net/core/skmsg.c:802
       sock_map_destroy+0x333/0x760 net/core/sock_map.c:1581
       inet_csk_destroy_sock+0x196/0x440 net/ipv4/inet_connection_sock.c:1130
       __tcp_close+0xd5b/0x12b0 net/ipv4/tcp.c:2897
       tcp_close+0x29/0xc0 net/ipv4/tcp.c:2909
      
      ... introduced by d8616ee2. Do a quick trace of the code path and the
      bug is obvious:
      
         inet_csk_destroy_sock(sk)
           sk_prot->destroy(sk);      <--- sock_map_destroy
              sk_psock_stop(, true);   <--- true so cancel workqueue
                cancel_work_sync()     <--- splat, because *_bh_disable()
      
      We can not call cancel_work_sync() from inside destroy path. So mark
      the sk_psock_stop call to skip this cancel_work_sync(). This will avoid
      the BUG, but means we may run sk_psock_backlog after or during the
      destroy op. We zapped the ingress_skb queue in sk_psock_stop (safe to
      do with local_bh_disable) so its empty and the sk_psock_backlog work
      item will not find any pkts to process here. However, because we are
      not going to wait for it or clear its ->state its possible it kicks off
      or is already running. This should be 'safe' up until psock drops its
      refcnt to psock->sk. The sock_put() that drops this reference is only
      done at psock destroy time from sk_psock_destroy(). This is done through
      workqueue when sk_psock_drop() is called on psock refnt reaches 0.
      And importantly sk_psock_destroy() does a cancel_work_sync(). So trivial
      fix works.
      
      I've had hit or miss luck reproducing this caught it once or twice with
      the provided reproducer when running with many runners. However, syzkaller
      is very good at reproducing so relying on syzkaller to verify fix.
      
      Fixes: d8616ee2
      
       ("bpf, sockmap: Fix sk->sk_forward_alloc warn_on in sk_stream_kill_queues")
      Reported-by: default avatar <syzbot+140186ceba0c496183bc@syzkaller.appspotmail.com>
      Suggested-by: default avatarHillf Danton <hdanton@sina.com>
      Signed-off-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Cc: Wang Yufen <wangyufen@huawei.com>
      Link: https://lore.kernel.org/bpf/20220628035803.317876-1-john.fastabend@gmail.com
      697fb80a
  4. Jun 25, 2022
    • Daniel Müller's avatar
      bpf: Merge "types_are_compat" logic into relo_core.c · fd75733d
      Daniel Müller authored
      BPF type compatibility checks (bpf_core_types_are_compat()) are
      currently duplicated between kernel and user space. That's a historical
      artifact more than intentional doing and can lead to subtle bugs where
      one implementation is adjusted but another is forgotten.
      
      That happened with the enum64 work, for example, where the libbpf side
      was changed (commit 23b2a3a8 ("libbpf: Add enum64 relocation
      support")) to use the btf_kind_core_compat() helper function but the
      kernel side was not (commit 6089fb32 ("bpf: Add btf enum64
      support")).
      
      This patch addresses both the duplication issue, by merging both
      implementations and moving them into relo_core.c, and fixes the alluded
      to kind check (by giving preference to libbpf's already adjusted logic).
      
      For discussion of the topic, please refer to:
      https://lore.kernel.org/bpf/CAADnVQKbWR7oarBdewgOBZUPzryhRYvEbkhyPJQHHuxq=0K1gw@mail.gmail.com/T/#mcc99f4a33ad9a322afaf1b9276fb1f0b7add9665
      
      
      
      Changelog:
      v1 -> v2:
      - limited libbpf recursion limit to 32
      - changed name to __bpf_core_types_are_compat
      - included warning previously present in libbpf version
      - merged kernel and user space changes into a single patch
      
      Signed-off-by: default avatarDaniel Müller <deso@posteo.net>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20220623182934.2582827-1-deso@posteo.net
      fd75733d
    • Shahab Vahedi's avatar
      bpf, docs: Fix the code formatting in instruction-set · 2f6d1e0f
      Shahab Vahedi authored
      
      
      A minor typo fix to include "| BPF_LD" into its previous
      code phrase:
      
      ``BPF_IND`` | BPF_LD --> ``BPF_IND | BPF_LD``
      
      Signed-off-by: default avatarShahab Vahedi <shahab@synopsys.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/b6120b31-3d1d-bf2d-2f2a-aa768d91257b@synopsys.com
      2f6d1e0f
    • Andrii Nakryiko's avatar
      Merge branch 'perf tools: Fix prologue generation' · 780d3d5a
      Andrii Nakryiko authored
      Jiri Olsa says:
      
      ====================
      
      hi,
      sending change we discussed some time ago [1] to get rid of
      some deprecated functions we use in perf prologue code.
      
      Despite the gloomy discussion I think the final code does
      not look that bad ;-)
      
      This patchset removes following libbpf functions from perf:
        bpf_program__set_prep
        bpf_program__nth_fd
        struct bpf_prog_prep_result
      
      v5 changes:
        - squashed patches together so we don't break bisection [Arnaldo]
      
      v4 changes:
        - fix typo [Andrii]
      
      v3 changes:
        - removed R0/R1 zero init in libbpf_prog_prepare_load_fn,
          because it's not needed [Andrii]
        - rebased/post on top of bpf-next/master which now has
          all the needed perf/core changes
      
      v2 changes:
        - use fallback section prog handler, so we don't need to
          use section prefix [Andrii]
        - realloc prog->insns array in bpf_program__set_insns [Andrii]
        - squash patch 1 from previous version with
          bpf_program__set_insns change [Daniel]
        - patch 3 already merged [Arnaldo]
        - added more comments
      
      thanks,
      jirka
      
      [1] https://lore.kernel.org/bpf/CAEf4BzaiBO3_617kkXZdYJ8hS8YF--ZLgapNbgeeEJ-pY0H88g@mail.gmail.com/
      
      
      ====================
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      780d3d5a
    • Jiri Olsa's avatar
      perf tools: Rework prologue generation code · b168852e
      Jiri Olsa authored
      
      
      Some functions we use for bpf prologue generation are going to be
      deprecated. This change reworks current code not to use them.
      
      We need to replace following functions/struct:
         bpf_program__set_prep
         bpf_program__nth_fd
         struct bpf_prog_prep_result
      
      Currently we use bpf_program__set_prep to hook perf callback before
      program is loaded and provide new instructions with the prologue.
      
      We replace this function/ality by taking instructions for specific
      program, attaching prologue to them and load such new ebpf programs
      with prologue using separate bpf_prog_load calls (outside libbpf
      load machinery).
      
      Before we can take and use program instructions, we need libbpf to
      actually load it. This way we get the final shape of its instructions
      with all relocations and verifier adjustments).
      
      There's one glitch though.. perf kprobe program already assumes
      generated prologue code with proper values in argument registers,
      so loading such program directly will fail in the verifier.
      
      That's where the fallback pre-load handler fits in and prepends
      the initialization code to the program. Once such program is loaded
      we take its instructions, cut off the initialization code and prepend
      the prologue.
      
      I know.. sorry ;-)
      
      To have access to the program when loading this patch adds support to
      register 'fallback' section handler to take care of perf kprobe programs.
      The fallback means that it handles any section definition besides the
      ones that libbpf handles.
      
      The handler serves two purposes:
        - allows perf programs to have special arguments in section name
        - allows perf to use pre-load callback where we can attach init
          code (zeroing all argument registers) to each perf program
      
      Suggested-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarJiri Olsa <jolsa@kernel.org>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Tested-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
      Acked-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
      Link: https://lore.kernel.org/bpf/20220616202214.70359-2-jolsa@kernel.org
      b168852e
  5. Jun 24, 2022