1. 27 Sep, 2022 13 commits
  2. 26 Sep, 2022 3 commits
  3. 24 Sep, 2022 6 commits
  4. 23 Sep, 2022 3 commits
  5. 22 Sep, 2022 15 commits
    • Martin KaFai Lau's avatar
      Merge branch 'Fix resource leaks in test_maps' · ee9bb9b4
      Martin KaFai Lau authored
      Hou Tao says:
      
      ====================
      
      From: Hou Tao <houtao1@huawei.com>
      
      Hi,
      
      It is just a tiny patch set aims to fix the resource leaks in test_maps
      after test case succeeds or is skipped. And these leaks are spotted by
      using address sanitizer and checking the content of /proc/$pid/fd.
      
      Please see indiviual patch for more details.
      
      Change Log:
      v2:
       * Add the missing header file unistd.h for close() (From kernel-patches/bpf)
         The reason Why I miss that is that -Werror is removed from Makefile
         when enabling clang address sanitizer.
      
      v1:
       * https://lore.kernel.org/bpf/20220921025855.115463-1-houtao@huaweicloud.com/T/
      ====================
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      ee9bb9b4
    • Hou Tao's avatar
      selftests/bpf: Free the allocated resources after test case succeeds · 103d002f
      Hou Tao authored
      Free the created fd or allocated bpf_object after test case succeeds,
      else there will be resource leaks.
      
      Spotted by using address sanitizer and checking the content of
      /proc/$pid/fd directory.
      Signed-off-by: default avatarHou Tao <houtao1@huawei.com>
      Link: https://lore.kernel.org/r/20220921070035.2016413-3-houtao@huaweicloud.comSigned-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      103d002f
    • Hou Tao's avatar
      selftests/bpf: Destroy the skeleton when CONFIG_PREEMPT is off · f5eb23b9
      Hou Tao authored
      Destroy the created skeleton when CONFIG_PREEMPT is off, else will be
      resource leak.
      
      Fixes: 73b97bc7 ("selftests/bpf: Test concurrent updates on bpf_task_storage_busy")
      Signed-off-by: default avatarHou Tao <houtao1@huawei.com>
      Link: https://lore.kernel.org/r/20220921070035.2016413-2-houtao@huaweicloud.comSigned-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      f5eb23b9
    • Yauheni Kaliuta's avatar
      selftests/bpf: Add liburandom_read.so to TEST_GEN_FILES · b780d167
      Yauheni Kaliuta authored
      Added urandom_read shared lib is missing from the list of installed
      files what makes urandom_read test after `make install` or `make
      gen_tar` broken.
      
      Add the library to TEST_GEN_FILES. The names in the list do not
      contain $(OUTPUT) since it's added by lib.mk code.
      
      Fixes: 00a0fa2d ("selftests/bpf: Add urandom_read shared lib and USDTs")
      Signed-off-by: default avatarYauheni Kaliuta <ykaliuta@redhat.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20220920161409.129953-1-ykaliuta@redhat.com
      b780d167
    • Alexei Starovoitov's avatar
      Merge branch 'veristat: CSV output, comparison mode, filtering' · 020e2176
      Alexei Starovoitov authored
      Andrii Nakryiko says:
      
      ====================
      
      Add three more critical features to veristat tool, which make it sufficient
      for a practical work on BPF verifier:
      
        - CSV output, which allows easier programmatic post-processing of stats;
      
        - building upon CSV output, veristat now supports comparison mode, in which
          two previously captured CSV outputs from veristat are compared with each
          other in a convenient form;
      
        - flexible allow/deny filtering using globs for BPF object files and
          programs, allowing to narrow down target BPF programs to be verified.
      
      See individual patches for more details and examples.
      
      v1->v2:
      - split out double-free fix into patch #1 (Yonghong);
      - fixed typo in verbose flag (Quentin);
      - baseline and comparison stats were reversed in output table, fixed that.
      ====================
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      020e2176
    • Andrii Nakryiko's avatar
      selftests/bpf: add ability to filter programs in veristat · bde4a96c
      Andrii Nakryiko authored
      Add -f (--filter) argument which accepts glob-based filters for
      narrowing down what BPF object files and programs within them should be
      processed by veristat. This filtering applies both to comparison and
      main (verification) mode.
      
      Filter can be of two forms:
        - file (object) filter: 'strobemeta*'; in this case all the programs
          within matching files are implicitly allowed (or denied, depending
          if it's positive or negative rule, see below);
        - file and prog filter: 'strobemeta*/*unroll*' will further filter
          programs within matching files to only allow those program names that
          match '*unroll*' glob.
      
      As mentioned, filters can be positive (allowlisting) and negative
      (denylisting). Negative filters should start with '!': '!strobemeta*'
      will deny any filename which basename starts with "strobemeta".
      
      Further, one extra special syntax is supported to allow more convenient
      use in practice. Instead of specifying rule on the command line,
      veristat allows to specify file that contains rules, both positive and
      negative, one line per one filter. This is achieved with -f @<filepath>
      use, where <filepath> points to a text file containing rules (negative
      and positive rules can be mixed). For convenience empty lines and lines
      starting with '#' are ignored. This feature is useful to have some
      pre-canned list of object files and program names that are tested
      repeatedly, allowing to check in a list of rules and quickly specify
      them on the command line.
      
      As a demonstration (and a short cut for nearest future), create a small
      list of "interesting" BPF object files from selftests/bpf and commit it
      as veristat.cfg. It currently includes 73 programs, most of which are
      the most complex and largest BPF programs in selftests, as judged by
      total verified instruction count and verifier states total.
      
      If there is overlap between positive or negative filters, negative
      filter takes precedence (denylisting is stronger than allowlisting). If
      no allow filter is specified, veristat implicitly assumes '*/*' rule. If
      no deny rule is specified, veristat (logically) assumes no negative
      filters.
      
      Also note that -f (just like -e and -s) can be specified multiple times
      and their effect is cumulative.
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20220921164254.3630690-5-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      bde4a96c
    • Andrii Nakryiko's avatar
      selftests/bpf: add comparison mode to veristat · 394169b0
      Andrii Nakryiko authored
      Add ability to compare and contrast two veristat runs, previously
      recorded with veristat using CSV output format.
      
      When veristat is called with -C (--compare) flag, veristat expects
      exactly two input files specified, both should be in CSV format.
      Expectation is that it's output from previous veristat runs, but as long
      as column names and formats match, it should just work. First CSV file
      is designated as a "baseline" provided, and the second one is
      comparison (experiment) data set. Establishing baseline matters later
      when calculating difference percentages, see below.
      
      Veristat parses these two CSV files and "reconstructs" verifier stats
      (it could be just a subset of all possible stats). File and program
      names are mandatory as they are used as joining key (these two "stats"
      are designated as "key stats" in the code).
      
      Veristat currently enforces that the set of stats recorded in both CSV
      has to exactly match, down to exact order. This is just a simplifying
      condition which can be lifted with a bit of additional pre-processing to
      reorded stat specs internally, which I didn't bother doing, yet.
      
      For all the non-key stats, veristat will output three columns: one for
      baseline data, one for comparison data, and one with an absolute and
      relative percentage difference. If either baseline or comparison values
      are missing (that is, respective CSV file doesn't have a row with
      *exactly* matching file and program name), those values are assumed to
      be empty or zero. In such case relative percentages are forced to +100%
      or -100% output, for consistency with a typical case.
      
      Veristat's -e (--emit) and -s (--sort) specs still apply, so even if CSV
      contains lots of stats, user can request to compare only a subset of
      them (and specify desired column order as well). Similarly, both CSV and
      human-readable table output is honored. Note that input is currently
      always expected to be CSV.
      
      Here's an example shell session, recording data for biosnoop tool on two
      different kernels and comparing them afterwards, outputting data in table
      format.
      
        # on slightly older production kernel
        $ sudo ./veristat biosnoop_bpf.o
        File            Program                   Verdict  Duration (us)  Total insns  Total states  Peak states
        --------------  ------------------------  -------  -------------  -----------  ------------  -----------
        biosnoop_bpf.o  blk_account_io_merge_bio  success             37           24             1            1
        biosnoop_bpf.o  blk_account_io_start      failure              0            0             0            0
        biosnoop_bpf.o  block_rq_complete         success             76          104             6            6
        biosnoop_bpf.o  block_rq_insert           success             83           85             7            7
        biosnoop_bpf.o  block_rq_issue            success             79           85             7            7
        --------------  ------------------------  -------  -------------  -----------  ------------  -----------
        Done. Processed 1 object files, 5 programs.
        $ sudo ./veristat ~/local/tmp/fbcode-bpf-objs/biosnoop_bpf.o -o csv > baseline.csv
        $ cat baseline.csv
        file_name,prog_name,verdict,duration,total_insns,total_states,peak_states
        biosnoop_bpf.o,blk_account_io_merge_bio,success,36,24,1,1
        biosnoop_bpf.o,blk_account_io_start,failure,0,0,0,0
        biosnoop_bpf.o,block_rq_complete,success,82,104,6,6
        biosnoop_bpf.o,block_rq_insert,success,78,85,7,7
        biosnoop_bpf.o,block_rq_issue,success,74,85,7,7
      
        # on latest bpf-next kernel
        $ sudo ./veristat biosnoop_bpf.o
        File            Program                   Verdict  Duration (us)  Total insns  Total states  Peak states
        --------------  ------------------------  -------  -------------  -----------  ------------  -----------
        biosnoop_bpf.o  blk_account_io_merge_bio  success             31           24             1            1
        biosnoop_bpf.o  blk_account_io_start      failure              0            0             0            0
        biosnoop_bpf.o  block_rq_complete         success             76          104             6            6
        biosnoop_bpf.o  block_rq_insert           success             83           91             7            7
        biosnoop_bpf.o  block_rq_issue            success             74           91             7            7
        --------------  ------------------------  -------  -------------  -----------  ------------  -----------
        Done. Processed 1 object files, 5 programs.
        $ sudo ./veristat biosnoop_bpf.o -o csv > comparison.csv
        $ cat comparison.csv
        file_name,prog_name,verdict,duration,total_insns,total_states,peak_states
        biosnoop_bpf.o,blk_account_io_merge_bio,success,71,24,1,1
        biosnoop_bpf.o,blk_account_io_start,failure,0,0,0,0
        biosnoop_bpf.o,block_rq_complete,success,82,104,6,6
        biosnoop_bpf.o,block_rq_insert,success,83,91,7,7
        biosnoop_bpf.o,block_rq_issue,success,87,91,7,7
      
        # now let's compare with human-readable output (note that no sudo needed)
        # we also ignore verification duration in this case to shortned output
        $ ./veristat -C baseline.csv comparison.csv -e file,prog,verdict,insns
        File            Program                   Verdict (A)  Verdict (B)  Verdict (DIFF)  Total insns (A)  Total insns (B)  Total insns (DIFF)
        --------------  ------------------------  -----------  -----------  --------------  ---------------  ---------------  ------------------
        biosnoop_bpf.o  blk_account_io_merge_bio  success      success      MATCH                        24               24         +0 (+0.00%)
        biosnoop_bpf.o  blk_account_io_start      failure      failure      MATCH                         0                0       +0 (+100.00%)
        biosnoop_bpf.o  block_rq_complete         success      success      MATCH                       104              104         +0 (+0.00%)
        biosnoop_bpf.o  block_rq_insert           success      success      MATCH                        91               85         -6 (-6.59%)
        biosnoop_bpf.o  block_rq_issue            success      success      MATCH                        91               85         -6 (-6.59%)
        --------------  ------------------------  -----------  -----------  --------------  ---------------  ---------------  ------------------
      
      While not particularly exciting example (it turned out to be kind of hard to
      quickly find a nice example with significant difference just because of kernel
      version bump), it should demonstrate main features.
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20220921164254.3630690-4-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      394169b0
    • Andrii Nakryiko's avatar
      selftests/bpf: add CSV output mode for veristat · e5eb08d8
      Andrii Nakryiko authored
      Teach veristat to output results as CSV table for easier programmatic
      processing. Change what was --output/-o argument to now be --emit/-e.
      And then use --output-format/-o <fmt> to specify output format.
      Currently "table" and "csv" is supported, table being default.
      
      For CSV output mode veristat is using spec identifiers as column names.
      E.g., instead of "Total states" veristat uses "total_states" as a CSV
      header name.
      
      Internally veristat recognizes three formats, one of them
      (RESFMT_TABLE_CALCLEN) is a special format instructing veristat to
      calculate column widths for table output. This felt a bit cleaner and
      more uniform than either creating separate functions just for this.
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20220921164254.3630690-3-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      e5eb08d8
    • Andrii Nakryiko's avatar
      selftests/bpf: fix double bpf_object__close() in veristate · f338ac91
      Andrii Nakryiko authored
      bpf_object__close(obj) is called twice for BPF object files with single
      BPF program in it. This causes crash. Fix this by not calling
      bpf_object__close() unnecessarily.
      
      Fixes: c8bc5e05 ("selftests/bpf: Add veristat tool for mass-verifying BPF object files")
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20220921164254.3630690-2-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      f338ac91
    • Alexei Starovoitov's avatar
      Merge branch 'Introduce bpf_ct_set_nat_info kfunc helper' · 2d863b14
      Alexei Starovoitov authored
      Lorenzo Bianconi says:
      
      ====================
      
      Introduce bpf_ct_set_nat_info kfunc helper in order to set source and
      destination nat addresses/ports in a new allocated ct entry not inserted
      in the connection tracking table yet.
      Introduce support for per-parameter trusted args.
      
      Changes since v2:
      - use int instead of a pointer for port in bpf_ct_set_nat_info signature
      - modify KF_TRUSTED_ARGS definition in order to referenced pointer constraint
        just for PTR_TO_BTF_ID
      - drop patch 2/4
      
      Changes since v1:
      - enable CONFIG_NF_NAT in tools/testing/selftests/bpf/config
      
      Kumar Kartikeya Dwivedi (1):
        bpf: Tweak definition of KF_TRUSTED_ARGS
      ====================
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      2d863b14
    • Lorenzo Bianconi's avatar
      selftests/bpf: add tests for bpf_ct_set_nat_info kfunc · b06b45e8
      Lorenzo Bianconi authored
      Introduce self-tests for bpf_ct_set_nat_info kfunc used to set the
      source or destination nat addresses/ports.
      Signed-off-by: default avatarLorenzo Bianconi <lorenzo@kernel.org>
      Link: https://lore.kernel.org/r/803e33294e247744d466943105879414344d3235.1663778601.git.lorenzo@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      b06b45e8
    • Lorenzo Bianconi's avatar
      net: netfilter: add bpf_ct_set_nat_info kfunc helper · 0fabd2aa
      Lorenzo Bianconi authored
      Introduce bpf_ct_set_nat_info kfunc helper in order to set source and
      destination nat addresses/ports in a new allocated ct entry not inserted
      in the connection tracking table yet.
      Signed-off-by: default avatarLorenzo Bianconi <lorenzo@kernel.org>
      Link: https://lore.kernel.org/r/9567db2fdfa5bebe7b7cc5870f7a34549418b4fc.1663778601.git.lorenzo@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      0fabd2aa
    • Kumar Kartikeya Dwivedi's avatar
      bpf: Tweak definition of KF_TRUSTED_ARGS · eed807f6
      Kumar Kartikeya Dwivedi authored
      Instead of forcing all arguments to be referenced pointers with non-zero
      reg->ref_obj_id, tweak the definition of KF_TRUSTED_ARGS to mean that
      only PTR_TO_BTF_ID (and socket types translated to PTR_TO_BTF_ID) have
      that constraint, and require their offset to be set to 0.
      
      The rest of pointer types are also accomodated in this definition of
      trusted pointers, but with more relaxed rules regarding offsets.
      
      The inherent meaning of setting this flag is that all kfunc pointer
      arguments have a guranteed lifetime, and kernel object pointers
      (PTR_TO_BTF_ID, PTR_TO_CTX) are passed in their unmodified form (with
      offset 0). In general, this is not true for PTR_TO_BTF_ID as it can be
      obtained using pointer walks.
      Signed-off-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
      Signed-off-by: default avatarLorenzo Bianconi <lorenzo@kernel.org>
      Link: https://lore.kernel.org/r/cdede0043c47ed7a357f0a915d16f9ce06a1d589.1663778601.git.lorenzo@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      eed807f6
    • Hou Tao's avatar
      bpf: Always use raw spinlock for hash bucket lock · 1d8b82c6
      Hou Tao authored
      For a non-preallocated hash map on RT kernel, regular spinlock instead
      of raw spinlock is used for bucket lock. The reason is that on RT kernel
      memory allocation is forbidden under atomic context and regular spinlock
      is sleepable under RT.
      
      Now hash map has been fully converted to use bpf_map_alloc, and there
      will be no synchronous memory allocation for non-preallocated hash map,
      so it is safe to always use raw spinlock for bucket lock on RT. So
      removing the usage of htab_use_raw_lock() and updating the comments
      accordingly.
      Signed-off-by: default avatarHou Tao <houtao1@huawei.com>
      Link: https://lore.kernel.org/r/20220921073826.2365800-1-houtao@huaweicloud.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      1d8b82c6
    • Jiri Olsa's avatar
      bpf: Prevent bpf program recursion for raw tracepoint probes · 05b24ff9
      Jiri Olsa authored
      We got report from sysbot [1] about warnings that were caused by
      bpf program attached to contention_begin raw tracepoint triggering
      the same tracepoint by using bpf_trace_printk helper that takes
      trace_printk_lock lock.
      
       Call Trace:
        <TASK>
        ? trace_event_raw_event_bpf_trace_printk+0x5f/0x90
        bpf_trace_printk+0x2b/0xe0
        bpf_prog_a9aec6167c091eef_prog+0x1f/0x24
        bpf_trace_run2+0x26/0x90
        native_queued_spin_lock_slowpath+0x1c6/0x2b0
        _raw_spin_lock_irqsave+0x44/0x50
        bpf_trace_printk+0x3f/0xe0
        bpf_prog_a9aec6167c091eef_prog+0x1f/0x24
        bpf_trace_run2+0x26/0x90
        native_queued_spin_lock_slowpath+0x1c6/0x2b0
        _raw_spin_lock_irqsave+0x44/0x50
        bpf_trace_printk+0x3f/0xe0
        bpf_prog_a9aec6167c091eef_prog+0x1f/0x24
        bpf_trace_run2+0x26/0x90
        native_queued_spin_lock_slowpath+0x1c6/0x2b0
        _raw_spin_lock_irqsave+0x44/0x50
        bpf_trace_printk+0x3f/0xe0
        bpf_prog_a9aec6167c091eef_prog+0x1f/0x24
        bpf_trace_run2+0x26/0x90
        native_queued_spin_lock_slowpath+0x1c6/0x2b0
        _raw_spin_lock_irqsave+0x44/0x50
        __unfreeze_partials+0x5b/0x160
        ...
      
      The can be reproduced by attaching bpf program as raw tracepoint on
      contention_begin tracepoint. The bpf prog calls bpf_trace_printk
      helper. Then by running perf bench the spin lock code is forced to
      take slow path and call contention_begin tracepoint.
      
      Fixing this by skipping execution of the bpf program if it's
      already running, Using bpf prog 'active' field, which is being
      currently used by trampoline programs for the same reason.
      
      Moving bpf_prog_inc_misses_counter to syscall.c because
      trampoline.c is compiled in just for CONFIG_BPF_JIT option.
      Reviewed-by: default avatarStanislav Fomichev <sdf@google.com>
      Reported-by: syzbot+2251879aa068ad9c960d@syzkaller.appspotmail.com
      [1] https://lore.kernel.org/bpf/YxhFe3EwqchC%2FfYf@krava/T/#tSigned-off-by: default avatarJiri Olsa <jolsa@kernel.org>
      Link: https://lore.kernel.org/r/20220916071914.7156-1-jolsa@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      05b24ff9