Re: [PATCH v2 bpf-next 1/7] bpf: Optimize program stats

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2/8/21 1:28 PM, Daniel Borkmann wrote:
On 2/6/21 6:03 PM, Alexei Starovoitov wrote:
From: Alexei Starovoitov <ast@xxxxxxxxxx>

Move bpf_prog_stats from prog->aux into prog to avoid one extra load
in critical path of program execution.

Signed-off-by: Alexei Starovoitov <ast@xxxxxxxxxx>
---
  include/linux/bpf.h     |  8 --------
  include/linux/filter.h  | 10 +++++++++-
  kernel/bpf/core.c       |  8 ++++----
  kernel/bpf/syscall.c    |  2 +-
  kernel/bpf/trampoline.c |  2 +-
  kernel/bpf/verifier.c   |  2 +-
  6 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 321966fc35db..026fa8873c5d 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -14,7 +14,6 @@
  #include <linux/numa.h>
  #include <linux/mm_types.h>
  #include <linux/wait.h>
-#include <linux/u64_stats_sync.h>
  #include <linux/refcount.h>
  #include <linux/mutex.h>
  #include <linux/module.h>
@@ -507,12 +506,6 @@ enum bpf_cgroup_storage_type {
   */
  #define MAX_BPF_FUNC_ARGS 12
-struct bpf_prog_stats {
-    u64 cnt;
-    u64 nsecs;
-    struct u64_stats_sync syncp;
-} __aligned(2 * sizeof(u64));
-
  struct btf_func_model {
      u8 ret_size;
      u8 nr_args;
@@ -845,7 +838,6 @@ struct bpf_prog_aux {
      u32 linfo_idx;
      u32 num_exentries;
      struct exception_table_entry *extable;
-    struct bpf_prog_stats __percpu *stats;
      union {
          struct work_struct work;
          struct rcu_head    rcu;
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 5b3137d7b690..c6592590a0b7 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -22,6 +22,7 @@
  #include <linux/vmalloc.h>
  #include <linux/sockptr.h>
  #include <crypto/sha1.h>
+#include <linux/u64_stats_sync.h>
  #include <net/sch_generic.h>
@@ -539,6 +540,12 @@ struct bpf_binary_header {
      u8 image[] __aligned(BPF_IMAGE_ALIGNMENT);
  };
+struct bpf_prog_stats {
+    u64 cnt;
+    u64 nsecs;
+    struct u64_stats_sync syncp;
+} __aligned(2 * sizeof(u64));
+
  struct bpf_prog {
      u16            pages;        /* Number of allocated pages */
      u16            jited:1,    /* Is our filter JIT'ed? */
@@ -559,6 +566,7 @@ struct bpf_prog {
      u8            tag[BPF_TAG_SIZE];
      struct bpf_prog_aux    *aux;        /* Auxiliary fields */
      struct sock_fprog_kern    *orig_prog;    /* Original BPF program */
+    struct bpf_prog_stats __percpu *stats;
      unsigned int        (*bpf_func)(const void *ctx,
                          const struct bpf_insn *insn);

nit: could we move aux & orig_prog while at it behind bpf_func just to avoid it slipping
into next cacheline by accident when someone extends this again?

I don't understand what moving aux+orig_prog after bpf_func will do.
Currently it's this:
struct bpf_prog_aux *      aux;                  /*    32     8 */
struct sock_fprog_kern *   orig_prog;            /*    40     8 */
unsigned int (*bpf_func)(const void *, const struct bpf_insn *); /* 48 8 */

With stats and active pointers the bpf_func goes into 2nd cacheline.
In the past the motivation for bpf_func right next to insns were
due to interpreter. Now everyone has JIT on. The interpreter
is often removed from .text too. So having insn and bpf_func in
the same cache line is not important.
Whereas having bpf_func with stats and active could be important
if stats/active are also used in other places than fexit/fentry.
For this patch set bpf_func location is irrelevant, since the
prog addr is hardcoded inside bpf trampoline generated asm.
For the best speed only stats and active should be close to each other.
And they both will be in the 1st.

@@ -249,7 +249,7 @@ void __bpf_prog_free(struct bpf_prog *fp)
      if (fp->aux) {
          mutex_destroy(&fp->aux->used_maps_mutex);
          mutex_destroy(&fp->aux->dst_mutex);
-        free_percpu(fp->aux->stats);
+        free_percpu(fp->stats);

This doesn't look correct, stats is now /not/ tied to fp->aux anymore which this if block is taking care of freeing. It needs to be moved outside so we don't leak fp->stats.

Great catch. thanks!




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux