All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next v7 0/5] bpf_loop inlining
@ 2022-06-13 20:50 Eduard Zingerman
  2022-06-13 20:50 ` [PATCH bpf-next v7 1/5] selftests/bpf: specify expected instructions in test_verifier tests Eduard Zingerman
                   ` (4 more replies)
  0 siblings, 5 replies; 15+ messages in thread
From: Eduard Zingerman @ 2022-06-13 20:50 UTC (permalink / raw)
  To: bpf, ast, andrii, daniel, kernel-team, song, joannelkoong; +Cc: eddyz87

Hi Everyone,

This is the next iteration of the patch. It includes changes suggested
by Song, Joanne and Alexei. Please find updated intro message and
change log below.

This patch implements inlining of calls to bpf_loop helper function
when bpf_loop's callback is statically known. E.g. the rewrite does
the following transformation during BPF program processing:

  bpf_loop(10, foo, NULL, 0);

 ->

  for (int i = 0; i < 10; ++i)
    foo(i, NULL);

The transformation leads to measurable latency change for simple
loops. Measurements using `benchs/run_bench_bpf_loop.sh` inside QEMU /
KVM on i7-4710HQ CPU show a drop in latency from 14 ns/op to 2 ns/op.

The change is split in five parts:

* Update to test_verifier.c to specify expected and unexpected
  instruction sequences. This allows to check BPF program rewrites
  applied by e.g. do_mix_fixups function.

* Update to test_verifier.c to specify BTF function infos and types
  per test case. This is necessary for tests that load sub-program
  addresses to a variable because of the checks applied by
  check_ld_imm function.

* The update to verifier.c that tracks state of the parameters for
  each bpf_loop call in a program and decides whether it could be
  replaced by a loop.

* A set of test cases for `test_verifier` that use capabilities added
  by the first two patches to verify instructions produced by inlining
  logic.

* Two test cases for `test_prog` to check that possible corner cases
  behave as expected.

Additional details are available in commit messages for each patch.

Changes since v6:
 - Return value of the `optimize_bpf_loop` function is no longer
   ignored. This is necessary to properly propagate -ENOMEM error.

Changes since v5:
 - Added function `loop_flag_is_zero` to skip a few checks in
   `update_loop_inline_state` when loop instruction is not fit for
   inline.

Changes since v4:
 - Added missing `static` modifier for `update_loop_inline_state` and
   `inline_bpf_loop` functions.
 - `update_loop_inline_state` updated for better readability.
 - Fields `initialized` and `fit_for_inline` of `struct
   bpf_loop_inline_state` are changed back from `bool` to bitfields.
 - Acks from Song Liu added to comments for patches 1/5, 2/5, 4/5,
   5/5.

Changes since v3:
 - Function `adjust_stack_depth_for_loop_inlining` is replaced by
   function `optimize_bpf_loop`. Function `optimize_bpf_loop` is
   responsible for both stack depth adjustment and call instruction
   replacement.
 - Changes in `do_misc_fixups` are reverted.
 - Changes in `adjust_subprog_starts_after_remove` are reverted and
   function `adjust_loop_inline_subprogno` is removed. This is
   possible because call to `optimize_bpf_loop` is placed before the
   dead code removal in `opt_remove_dead_code` (in contrast to the
   position of `do_misc_fixups` where inlining was done in v3).
 - Field `bpf_insn_aux_data.loop_inline_state` is now a part of
   anonymous union at the start of the `bpf_insn_aux_data`.
 - Data structure `bpf_loop_inline_state` is simplified to use single
   flag field `fit_for_inline` instead of separate fields
   `flags_is_zero` & `callback_is_constant`.
 - Macro definition `BPF_MAX_LOOPS` is moved from
   `include/linux/bpf_verifier.h` to `include/linux/bpf.h` to avoid
   include of `include/linux/bpf_verifier.h` in `bpf_iter.c`.
 - `inline_bpf_loop` changed back to use array initialization and hard
   coded offsets as in v2.
 - Style / formatting updates.

Changes since v2:
 - fix for `stack_check` test case in `test_progs-no_alu32`, all tests
   are passing now;
 - v2 3/3 patch is split in three parts:
   - kernel changes
   - test_verifier changes
   - test_prog changes
 - updated `inline_bpf_loop` in `verifier.c` to calculate each offset
   used in instructions to avoid "magic" numbers;
 - removed newline handling logic in `fail_log` branch of
   `do_single_test` in `test_verifier.c` to simplify the patch set;
 - styling fixes suggested in review for v2 of this patch set.

Changes since v1:
 - allow to use SKIP_INSNS in instruction pattern specification in
   test_verifier tests;
 - fix for a bug in spill offset assignement for loop vars when
   bpf_loop is located in a non-main function.

Eduard Zingerman (5):
  selftests/bpf: specify expected instructions in test_verifier tests
  selftests/bpf: allow BTF specs and func infos in test_verifier tests
  bpf: Inline calls to bpf_loop when callback is known
  selftests/bpf: BPF test_verifier selftests for bpf_loop inlining
  selftests/bpf: BPF test_prog selftests for bpf_loop inlining

 include/linux/bpf.h                           |   3 +
 include/linux/bpf_verifier.h                  |  12 +
 kernel/bpf/bpf_iter.c                         |   9 +-
 kernel/bpf/verifier.c                         | 175 +++++++++-
 .../selftests/bpf/prog_tests/bpf_loop.c       |  62 ++++
 tools/testing/selftests/bpf/prog_tests/btf.c  |   1 -
 tools/testing/selftests/bpf/progs/bpf_loop.c  | 114 ++++++
 tools/testing/selftests/bpf/test_btf.h        |   2 +
 tools/testing/selftests/bpf/test_verifier.c   | 328 +++++++++++++++++-
 .../selftests/bpf/verifier/bpf_loop_inline.c  | 244 +++++++++++++
 10 files changed, 923 insertions(+), 27 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/verifier/bpf_loop_inline.c

-- 
2.25.1


^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re: [PATCH bpf-next v7 3/5] bpf: Inline calls to bpf_loop when callback is known
@ 2022-06-17 15:24 kernel test robot
  0 siblings, 0 replies; 15+ messages in thread
From: kernel test robot @ 2022-06-17 15:24 UTC (permalink / raw)
  To: kbuild

[-- Attachment #1: Type: text/plain, Size: 3975 bytes --]

:::::: 
:::::: Manual check reason: "low confidence static check warning: include/linux/bpf_verifier.h:348:26: sparse: sparse: dubious one-bit signed bitfield"
:::::: 

CC: kbuild-all(a)lists.01.org
BCC: lkp(a)intel.com
In-Reply-To: <20220613205008.212724-4-eddyz87@gmail.com>
References: <20220613205008.212724-4-eddyz87@gmail.com>
TO: Eduard Zingerman <eddyz87@gmail.com>
TO: bpf(a)vger.kernel.org
TO: ast(a)kernel.org
TO: andrii(a)kernel.org
TO: daniel(a)iogearbox.net
TO: kernel-team(a)fb.com
TO: song(a)kernel.org
TO: joannelkoong(a)gmail.com
CC: eddyz87(a)gmail.com

Hi Eduard,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Eduard-Zingerman/bpf_loop-inlining/20220614-051405
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
:::::: branch date: 4 days ago
:::::: commit date: 4 days ago
config: i386-randconfig-s002 (https://download.01.org/0day-ci/archive/20220617/202206172312.hdBsxxl6-lkp(a)intel.com/config)
compiler: gcc-11 (Debian 11.3.0-3) 11.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.4-30-g92122700-dirty
        # https://github.com/intel-lab-lkp/linux/commit/743c1f24e0257d653274ebf1b2cd0b62a0ea591d
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Eduard-Zingerman/bpf_loop-inlining/20220614-051405
        git checkout 743c1f24e0257d653274ebf1b2cd0b62a0ea591d
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=i386 SHELL=/bin/bash kernel/bpf/

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>


sparse warnings: (new ones prefixed by >>)
   kernel/bpf/core.c: note: in included file:
>> include/linux/bpf_verifier.h:348:26: sparse: sparse: dubious one-bit signed bitfield
   include/linux/bpf_verifier.h:349:29: sparse: sparse: dubious one-bit signed bitfield
   kernel/bpf/core.c:221:49: sparse: sparse: arithmetics on pointers to functions
   kernel/bpf/core.c:1850:43: sparse: sparse: arithmetics on pointers to functions
   kernel/bpf/core.c:1855:48: sparse: sparse: arithmetics on pointers to functions
   kernel/bpf/core.c:2103:77: sparse: sparse: subtraction of functions? Share your drugs
   kernel/bpf/core.c: note: in included file (through include/linux/rbtree_latch.h, include/linux/bpf.h, include/linux/filter.h):
   include/linux/rbtree.h:74:9: sparse: sparse: incompatible types in comparison expression (different address spaces):
   include/linux/rbtree.h:74:9: sparse:    struct rb_node [noderef] __rcu *
   include/linux/rbtree.h:74:9: sparse:    struct rb_node *
   include/linux/rbtree.h:74:9: sparse: sparse: incompatible types in comparison expression (different address spaces):
   include/linux/rbtree.h:74:9: sparse:    struct rb_node [noderef] __rcu *
   include/linux/rbtree.h:74:9: sparse:    struct rb_node *

vim +348 include/linux/bpf_verifier.h

58e2af8b3a6b58 Jakub Kicinski   2016-09-21  346  
743c1f24e0257d Eduard Zingerman 2022-06-13  347  struct bpf_loop_inline_state {
743c1f24e0257d Eduard Zingerman 2022-06-13 @348  	int initialized:1; /* set to true upon first entry */
743c1f24e0257d Eduard Zingerman 2022-06-13  349  	int fit_for_inline:1; /* true if callback function is the same
743c1f24e0257d Eduard Zingerman 2022-06-13  350  			       * at each call and flags are always zero
743c1f24e0257d Eduard Zingerman 2022-06-13  351  			       */
743c1f24e0257d Eduard Zingerman 2022-06-13  352  	u32 callback_subprogno; /* valid when fit_for_inline is true */
743c1f24e0257d Eduard Zingerman 2022-06-13  353  };
743c1f24e0257d Eduard Zingerman 2022-06-13  354  

-- 
0-DAY CI Kernel Test Service
https://01.org/lkp

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2022-06-20 13:05 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-13 20:50 [PATCH bpf-next v7 0/5] bpf_loop inlining Eduard Zingerman
2022-06-13 20:50 ` [PATCH bpf-next v7 1/5] selftests/bpf: specify expected instructions in test_verifier tests Eduard Zingerman
2022-06-13 20:50 ` [PATCH bpf-next v7 2/5] selftests/bpf: allow BTF specs and func infos " Eduard Zingerman
2022-06-13 20:50 ` [PATCH bpf-next v7 3/5] bpf: Inline calls to bpf_loop when callback is known Eduard Zingerman
2022-06-14  5:49   ` Song Liu
2022-06-16 23:12   ` Daniel Borkmann
2022-06-17  2:14   ` Alexei Starovoitov
2022-06-19 20:09     ` Eduard Zingerman
2022-06-19 21:10       ` Alexei Starovoitov
2022-06-19 22:01         ` Eduard Zingerman
2022-06-19 23:37           ` Alexei Starovoitov
2022-06-20 12:59             ` Eduard Zingerman
2022-06-13 20:50 ` [PATCH bpf-next v7 4/5] selftests/bpf: BPF test_verifier selftests for bpf_loop inlining Eduard Zingerman
2022-06-13 20:50 ` [PATCH bpf-next v7 5/5] selftests/bpf: BPF test_prog " Eduard Zingerman
  -- strict thread matches above, loose matches on Subject: below --
2022-06-17 15:24 [PATCH bpf-next v7 3/5] bpf: Inline calls to bpf_loop when callback is known kernel test robot

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.