History

bpf: Optimize program stats

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

Alexei Starovoitov committed 5 years ago

Commit 700d4791 parent 6df8fb86 files · +18 −18Browse files

Unified · Split

6 files
  1. include/linux/bpf.h +0 −8
  2. include/linux/filter.h +11 −3
  3. kernel/bpf/core.c +4 −4
  4. kernel/bpf/syscall.c +1 −1
  5. kernel/bpf/trampoline.c +1 −1
  6. kernel/bpf/verifier.c +1 −1
include/linux/bpf.h+0−8
@@ -14,7 +14,6 @@ · 13 unmodified lines
1414 #include <linux/numa.h>
1515 #include <linux/mm_types.h>
1616 #include <linux/wait.h>
17−#include <linux/u64_stats_sync.h>
1817 #include <linux/refcount.h>
1918 #include <linux/mutex.h>
2019 #include <linux/module.h>
@@ -507,12 +506,6 @@ enum bpf_cgroup_storage_type { · 486 unmodified lines
507506 */
508507 #define MAX_BPF_FUNC_ARGS 12
509508
510−struct bpf_prog_stats {
511− u64 cnt;
512− u64 nsecs;
513− struct u64_stats_sync syncp;
514−} __aligned(2 * sizeof(u64));
515−
516509 struct btf_func_model {
517510 u8 ret_size;
518511 u8 nr_args;
@@ -845,7 +838,6 @@ struct bpf_prog_aux { · 326 unmodified lines
845838 u32 linfo_idx;
846839 u32 num_exentries;
847840 struct exception_table_entry *extable;
848− struct bpf_prog_stats __percpu *stats;
849841 union {
850842 struct work_struct work;
851843 struct rcu_head rcu;
include/linux/filter.h+11−3
@@ -22,6 +22,7 @@ · 21 unmodified lines
2222 #include <linux/vmalloc.h>
2323 #include <linux/sockptr.h>
2424 #include <crypto/sha1.h>
25+#include <linux/u64_stats_sync.h>
2526
2627 #include <net/sch_generic.h>
2728
@@ -539,6 +540,12 @@ struct bpf_binary_header { · 511 unmodified lines
539540 u8 image[] __aligned(BPF_IMAGE_ALIGNMENT);
540541 };
541542
543+struct bpf_prog_stats {
544+ u64 cnt;
545+ u64 nsecs;
546+ struct u64_stats_sync syncp;
547+} __aligned(2 * sizeof(u64));
548+
542549 struct bpf_prog {
543550 u16 pages; /* Number of allocated pages */
544551 u16 jited:1, /* Is our filter JIT'ed? */
@@ -557,10 +564,11 @@ struct bpf_prog { · 12 unmodified lines
557564 u32 len; /* Number of filter blocks */
558565 u32 jited_len; /* Size of jited insns in bytes */
559566 u8 tag[BPF_TAG_SIZE];
560− struct bpf_prog_aux *aux; /* Auxiliary fields */
561− struct sock_fprog_kern *orig_prog; /* Original BPF program */
567+ struct bpf_prog_stats __percpu *stats;
562568 unsigned int (*bpf_func)(const void *ctx,
563569 const struct bpf_insn *insn);
570+ struct bpf_prog_aux *aux; /* Auxiliary fields */
571+ struct sock_fprog_kern *orig_prog; /* Original BPF program */
564572 /* Instructions for interpreter */
565573 struct sock_filter insns[0];
566574 struct bpf_insn insnsi[];
@@ -581,7 +589,7 @@ DECLARE_STATIC_KEY_FALSE(bpf_stats_enabled_key); · 14 unmodified lines
581589 struct bpf_prog_stats *__stats; \
582590 u64 __start = sched_clock(); \
583591 __ret = dfunc(ctx, (prog)->insnsi, (prog)->bpf_func); \
584− __stats = this_cpu_ptr(prog->aux->stats); \
592+ __stats = this_cpu_ptr(prog->stats); \
585593 u64_stats_update_begin(&__stats->syncp); \
586594 __stats->cnt++; \
587595 __stats->nsecs += sched_clock() - __start; \
kernel/bpf/core.c+4−4
@@ -114,8 +114,8 @@ struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags) · 113 unmodified lines
114114 if (!prog)
115115 return NULL;
116116
117− prog->aux->stats = alloc_percpu_gfp(struct bpf_prog_stats, gfp_flags);
118− if (!prog->aux->stats) {
117+ prog->stats = alloc_percpu_gfp(struct bpf_prog_stats, gfp_flags);
118+ if (!prog->stats) {
119119 kfree(prog->aux);
120120 vfree(prog);
121121 return NULL;
@@ -124,7 +124,7 @@ struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags) · 2 unmodified lines
124124 for_each_possible_cpu(cpu) {
125125 struct bpf_prog_stats *pstats;
126126
127− pstats = per_cpu_ptr(prog->aux->stats, cpu);
127+ pstats = per_cpu_ptr(prog->stats, cpu);
128128 u64_stats_init(&pstats->syncp);
129129 }
130130 return prog;
@@ -249,10 +249,10 @@ void __bpf_prog_free(struct bpf_prog *fp) · 118 unmodified lines
249249 if (fp->aux) {
250250 mutex_destroy(&fp->aux->used_maps_mutex);
251251 mutex_destroy(&fp->aux->dst_mutex);
252− free_percpu(fp->aux->stats);
253252 kfree(fp->aux->poke_tab);
254253 kfree(fp->aux);
255254 }
255+ free_percpu(fp->stats);
256256 vfree(fp);
257257 }
258258
kernel/bpf/syscall.c+1−1
@@ -1739,7 +1739,7 @@ static void bpf_prog_get_stats(const struct bpf_prog *prog, · 1738 unmodified lines
17391739 unsigned int start;
17401740 u64 tnsecs, tcnt;
17411741
1742− st = per_cpu_ptr(prog->aux->stats, cpu);
1742+ st = per_cpu_ptr(prog->stats, cpu);
17431743 do {
17441744 start = u64_stats_fetch_begin_irq(&st->syncp);
17451745 tnsecs = st->nsecs;
kernel/bpf/trampoline.c+1−1
@@ -412,7 +412,7 @@ void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start) · 411 unmodified lines
412412 * Hence check that 'start' is not zero.
413413 */
414414 start) {
415− stats = this_cpu_ptr(prog->aux->stats);
415+ stats = this_cpu_ptr(prog->stats);
416416 u64_stats_update_begin(&stats->syncp);
417417 stats->cnt++;
418418 stats->nsecs += sched_clock() - start;
kernel/bpf/verifier.c+1−1
@@ -11253,7 +11253,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) · 11252 unmodified lines
1125311253 /* BPF_PROG_RUN doesn't call subprogs directly,
1125411254 * hence main prog stats include the runtime of subprogs.
1125511255 * subprogs don't have IDs and not reachable via prog_get_next_id
11256− * func[i]->aux->stats will never be accessed and stays NULL
11256+ * func[i]->stats will never be accessed and stays NULL
1125711257 */
1125811258 func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER);
1125911259 if (!func[i])

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t