From patchwork Sat Feb 6 17:03:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12072261 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DFD4C433DB for ; Sat, 6 Feb 2021 17:04:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3B85864E80 for ; Sat, 6 Feb 2021 17:04:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229590AbhBFREa (ORCPT ); Sat, 6 Feb 2021 12:04:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229506AbhBFRE3 (ORCPT ); Sat, 6 Feb 2021 12:04:29 -0500 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1ED2C061756 for ; Sat, 6 Feb 2021 09:03:48 -0800 (PST) Received: by mail-pg1-x52b.google.com with SMTP id o7so6847187pgl.1 for ; Sat, 06 Feb 2021 09:03:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ff2zsTna5n4xNk9qjKCjYgZuYcKCEND5UpUV8fKCYfw=; b=qRJPNqUMPhiifFasiMrpjEHjpkuIQiHkQtiGijxfv8Tn+KlsZld26crhXuxiqMO/q+ +16WzzhtDGFg176OX3PSzdP4yyxT9q+AS1N235iQnT0W9jiPZqtU08MUhKqBxKSGyHs/ fqbyrLaF/5vTHjQXRFVK/yv7owmsoBQT0mU5++LJ2XFhQxigWAYz5khxkl+tBfwPSq9T 5vOo+hZFCOU89aPN8F5cXSdELyz6NVe4qsNRX5v7de8Q2IBGhQWDaqHmO+lSIHHsVKHK GXTL4Q5WcyGgZYGQFsVk5/l62DVCbVOWZ75pM5ybjGkCqS+8OD3Kiy6MEOUycA7gU72T soFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ff2zsTna5n4xNk9qjKCjYgZuYcKCEND5UpUV8fKCYfw=; b=XddvkUfDeolizOoocKQJ0gau9ZiQyajfiideUU9hVOa+ROPfrtZG3JeOlbqflEcOGo k1moOHEtgDwjLUOXE7r16eglB6kXmJDN0Mt0Rd7J291skHG8wvnEzzTv9hULyuf3mcJh YJHpqRA9CxuWu+eufAWmnlH0yEkk4d6xCdqPvJhB5UspwSByseUBvU6yTD4a+uDlApfd uHpAluYu0EX2ECqssuHLXkUONNKxNGexPn8RTCTYSZjGbUOT5oDoJU1iyLQtbkZbA0fR jqZ87NX/l97Na4n1hvEa+X5pabLO2Jf/NicohskSMIifenIxo74FrC9i6sK6t0maM9Wb +dnw== X-Gm-Message-State: AOAM531v5WwLwSZAb1vDRrndSI94/jw27+DoOv5dP1eqEA836aGF3nQp BBxM53AQNozn4ILWy/SvjqImylIkmn0= X-Google-Smtp-Source: ABdhPJyd1YcYKF5vlE0mCpLInszgslIrYLe5l3sz4gIRgA9IxOmkTbLmtXAuv12TxTOPfG4uUjHUag== X-Received: by 2002:a62:1c84:0:b029:1c4:f959:7b29 with SMTP id c126-20020a621c840000b02901c4f9597b29mr10094059pfc.34.1612631028280; Sat, 06 Feb 2021 09:03:48 -0800 (PST) Received: from ast-mbp.thefacebook.com ([163.114.132.7]) by smtp.gmail.com with ESMTPSA id j14sm11149964pjl.35.2021.02.06.09.03.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 06 Feb 2021 09:03:47 -0800 (PST) From: Alexei Starovoitov To: davem@davemloft.net Cc: daniel@iogearbox.net, bpf@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v2 bpf-next 1/7] bpf: Optimize program stats Date: Sat, 6 Feb 2021 09:03:38 -0800 Message-Id: <20210206170344.78399-2-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20210206170344.78399-1-alexei.starovoitov@gmail.com> References: <20210206170344.78399-1-alexei.starovoitov@gmail.com> Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov 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 Acked-by: Andrii Nakryiko --- 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 #include #include -#include #include #include #include @@ -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 #include #include +#include #include @@ -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); /* Instructions for interpreter */ @@ -581,7 +589,7 @@ DECLARE_STATIC_KEY_FALSE(bpf_stats_enabled_key); struct bpf_prog_stats *__stats; \ u64 __start = sched_clock(); \ __ret = dfunc(ctx, (prog)->insnsi, (prog)->bpf_func); \ - __stats = this_cpu_ptr(prog->aux->stats); \ + __stats = this_cpu_ptr(prog->stats); \ u64_stats_update_begin(&__stats->syncp); \ __stats->cnt++; \ __stats->nsecs += sched_clock() - __start; \ diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 5bbd4884ff7a..fa3da4cda476 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -114,8 +114,8 @@ struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags) if (!prog) return NULL; - prog->aux->stats = alloc_percpu_gfp(struct bpf_prog_stats, gfp_flags); - if (!prog->aux->stats) { + prog->stats = alloc_percpu_gfp(struct bpf_prog_stats, gfp_flags); + if (!prog->stats) { kfree(prog->aux); vfree(prog); return NULL; @@ -124,7 +124,7 @@ struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags) for_each_possible_cpu(cpu) { struct bpf_prog_stats *pstats; - pstats = per_cpu_ptr(prog->aux->stats, cpu); + pstats = per_cpu_ptr(prog->stats, cpu); u64_stats_init(&pstats->syncp); } return prog; @@ -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); kfree(fp->aux->poke_tab); kfree(fp->aux); } diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index e5999d86c76e..f7df56a704de 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -1739,7 +1739,7 @@ static void bpf_prog_get_stats(const struct bpf_prog *prog, unsigned int start; u64 tnsecs, tcnt; - st = per_cpu_ptr(prog->aux->stats, cpu); + st = per_cpu_ptr(prog->stats, cpu); do { start = u64_stats_fetch_begin_irq(&st->syncp); tnsecs = st->nsecs; diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index 35c5887d82ff..5be3beeedd74 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -412,7 +412,7 @@ void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start) * Hence check that 'start' is not zero. */ start) { - stats = this_cpu_ptr(prog->aux->stats); + stats = this_cpu_ptr(prog->stats); u64_stats_update_begin(&stats->syncp); stats->cnt++; stats->nsecs += sched_clock() - start; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 15694246f854..4189edb41b73 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -10889,7 +10889,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) /* BPF_PROG_RUN doesn't call subprogs directly, * hence main prog stats include the runtime of subprogs. * subprogs don't have IDs and not reachable via prog_get_next_id - * func[i]->aux->stats will never be accessed and stays NULL + * func[i]->stats will never be accessed and stays NULL */ func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); if (!func[i]) From patchwork Sat Feb 6 17:03:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12072263 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1E5B1C433E0 for ; Sat, 6 Feb 2021 17:04:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DB8A764E2C for ; Sat, 6 Feb 2021 17:04:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229506AbhBFREb (ORCPT ); Sat, 6 Feb 2021 12:04:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229541AbhBFREa (ORCPT ); Sat, 6 Feb 2021 12:04:30 -0500 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28187C061786 for ; Sat, 6 Feb 2021 09:03:50 -0800 (PST) Received: by mail-pf1-x436.google.com with SMTP id 18so4375992pfz.3 for ; Sat, 06 Feb 2021 09:03:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=S67ZeRVzcEN0snJDOlrqZLmTeWhE/3fYH9/pS1rwHII=; b=Q+elz5XZE5U+FIO1wnGagxvFc1jl3wkey8xzxc2+Ur6JQuRSuH6RydFl+3F62dUK5a HX3z9KGMf0bmEQgLiFF13pGrwggxwzdXhbdhcz+sd5sL7XmwpklIg8EduBC/ybLuYKtT OylAOrc+sLg4CvShloRVF7qQh5PlgIGNjsfkEizwt9GK/E0tlYxKqUqhZAigCGyOLwRM QH/xMuSkdnQymNpc1nDGu4wqazwk1XL68gtXTWvI5q3CQJFvTENadYJnCHOSIeUHxOql 1gRtlmcD7w/ysjPh5jJisyEdOf6adjErzm150/KmTvWwMSUPKoTqjmZgVl8cxxPHEmHA sAwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=S67ZeRVzcEN0snJDOlrqZLmTeWhE/3fYH9/pS1rwHII=; b=bFAbzQMJfXzGbIBv4vHjIsi4UaSyAYaT+o2/q+OfH4eqDNTO/CJY5L1hIIBtveKECW nwgpMzduP/eeAT5zm5t6+l+lpNAD9VDMCbl3xuCQbacY5JYnTc5RmlMFKBJ7JroKyVu0 wipeFv2ltoQNHiNvRE5o0AGTAJOu2FYG40/S1AfToyrxSgD5OaovSkpdw+W5I1QlOpgk iSjwxnFOOzSopDsAQgtfvT7yUGg8PjZszt+WErJvGfEg1EcyFRPvMmuTri8AA7S/BrfC mjyjAfPANnfxCIaSi2aHW2xOqSF4DYViE8vYhAUEIgHq8n3EEJhS/N/ei3LN1ekFNftz O9Yw== X-Gm-Message-State: AOAM530TWdxmQUjWzG12W5abu0R24eQ2tQtJSVKd9qsfJnaH6oqoDu7C fiehEw67Avrc6pZs5qdSNEo5/JFZznI= X-Google-Smtp-Source: ABdhPJxcM7tbEVt1bymZn5/4LeNd8WpDD88hcYcfXUEi9f4Mn0uL7sR1oXjrtvIKH1+pCA89+6gPXA== X-Received: by 2002:a62:fc83:0:b029:1d6:1767:dd1c with SMTP id e125-20020a62fc830000b02901d61767dd1cmr10115526pfh.70.1612631029692; Sat, 06 Feb 2021 09:03:49 -0800 (PST) Received: from ast-mbp.thefacebook.com ([163.114.132.7]) by smtp.gmail.com with ESMTPSA id j14sm11149964pjl.35.2021.02.06.09.03.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 06 Feb 2021 09:03:49 -0800 (PST) From: Alexei Starovoitov To: davem@davemloft.net Cc: daniel@iogearbox.net, bpf@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v2 bpf-next 2/7] bpf: Compute program stats for sleepable programs Date: Sat, 6 Feb 2021 09:03:39 -0800 Message-Id: <20210206170344.78399-3-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20210206170344.78399-1-alexei.starovoitov@gmail.com> References: <20210206170344.78399-1-alexei.starovoitov@gmail.com> Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov In older non-RT kernels migrate_disable() was the same as preempt_disable(). Since commit 74d862b682f5 ("sched: Make migrate_disable/enable() independent of RT") migrate_disable() is real and doesn't prevent sleeping. Use it to efficiently compute execution stats for sleepable bpf programs. migrate_disable() will also be used to enable per-cpu maps in sleepable programs in the future patches. Signed-off-by: Alexei Starovoitov Acked-by: Andrii Nakryiko --- arch/x86/net/bpf_jit_comp.c | 31 ++++++++++++------------------- include/linux/bpf.h | 4 ++-- kernel/bpf/trampoline.c | 27 +++++++++++++++++++++------ 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index a3dc3bd154ac..d11b9bcebbea 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -1742,15 +1742,12 @@ static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog, u8 *prog = *pprog; int cnt = 0; - if (p->aux->sleepable) { - if (emit_call(&prog, __bpf_prog_enter_sleepable, prog)) + if (emit_call(&prog, + p->aux->sleepable ? __bpf_prog_enter_sleepable : + __bpf_prog_enter, prog)) return -EINVAL; - } else { - if (emit_call(&prog, __bpf_prog_enter, prog)) - return -EINVAL; - /* remember prog start time returned by __bpf_prog_enter */ - emit_mov_reg(&prog, true, BPF_REG_6, BPF_REG_0); - } + /* remember prog start time returned by __bpf_prog_enter */ + emit_mov_reg(&prog, true, BPF_REG_6, BPF_REG_0); /* arg1: lea rdi, [rbp - stack_size] */ EMIT4(0x48, 0x8D, 0x7D, -stack_size); @@ -1770,18 +1767,14 @@ static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog, if (mod_ret) emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8); - if (p->aux->sleepable) { - if (emit_call(&prog, __bpf_prog_exit_sleepable, prog)) + /* arg1: mov rdi, progs[i] */ + emit_mov_imm64(&prog, BPF_REG_1, (long) p >> 32, (u32) (long) p); + /* arg2: mov rsi, rbx <- start time in nsec */ + emit_mov_reg(&prog, true, BPF_REG_2, BPF_REG_6); + if (emit_call(&prog, + p->aux->sleepable ? __bpf_prog_exit_sleepable : + __bpf_prog_exit, prog)) return -EINVAL; - } else { - /* arg1: mov rdi, progs[i] */ - emit_mov_imm64(&prog, BPF_REG_1, (long) p >> 32, - (u32) (long) p); - /* arg2: mov rsi, rbx <- start time in nsec */ - emit_mov_reg(&prog, true, BPF_REG_2, BPF_REG_6); - if (emit_call(&prog, __bpf_prog_exit, prog)) - return -EINVAL; - } *pprog = prog; return 0; diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 026fa8873c5d..2fa48439ef31 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -563,8 +563,8 @@ int arch_prepare_bpf_trampoline(void *image, void *image_end, /* these two functions are called from generated trampoline */ u64 notrace __bpf_prog_enter(void); void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start); -void notrace __bpf_prog_enter_sleepable(void); -void notrace __bpf_prog_exit_sleepable(void); +u64 notrace __bpf_prog_enter_sleepable(void); +void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start); struct bpf_ksym { unsigned long start; diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index 5be3beeedd74..b1f567514b7e 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -388,10 +388,11 @@ void bpf_trampoline_put(struct bpf_trampoline *tr) * call prog->bpf_func * call __bpf_prog_exit */ +#define NO_START_TIME 0 u64 notrace __bpf_prog_enter(void) __acquires(RCU) { - u64 start = 0; + u64 start = NO_START_TIME; rcu_read_lock(); migrate_disable(); @@ -400,8 +401,8 @@ u64 notrace __bpf_prog_enter(void) return start; } -void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start) - __releases(RCU) +static void notrace update_prog_stats(struct bpf_prog *prog, + u64 start) { struct bpf_prog_stats *stats; @@ -411,25 +412,39 @@ void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start) * And vice versa. * Hence check that 'start' is not zero. */ - start) { + start > NO_START_TIME) { stats = this_cpu_ptr(prog->stats); u64_stats_update_begin(&stats->syncp); stats->cnt++; stats->nsecs += sched_clock() - start; u64_stats_update_end(&stats->syncp); } +} + +void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start) + __releases(RCU) +{ + update_prog_stats(prog, start); migrate_enable(); rcu_read_unlock(); } -void notrace __bpf_prog_enter_sleepable(void) +u64 notrace __bpf_prog_enter_sleepable(void) { + u64 start = NO_START_TIME; + rcu_read_lock_trace(); + migrate_disable(); might_fault(); + if (static_branch_unlikely(&bpf_stats_enabled_key)) + start = sched_clock(); + return start; } -void notrace __bpf_prog_exit_sleepable(void) +void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start) { + update_prog_stats(prog, start); + migrate_enable(); rcu_read_unlock_trace(); } From patchwork Sat Feb 6 17:03:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12072265 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EBAAEC433E6 for ; Sat, 6 Feb 2021 17:04:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AB78E64E80 for ; Sat, 6 Feb 2021 17:04:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229626AbhBFREd (ORCPT ); Sat, 6 Feb 2021 12:04:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229541AbhBFREb (ORCPT ); Sat, 6 Feb 2021 12:04:31 -0500 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78815C061788 for ; Sat, 6 Feb 2021 09:03:51 -0800 (PST) Received: by mail-pg1-x52e.google.com with SMTP id r38so6794696pgk.13 for ; Sat, 06 Feb 2021 09:03:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=CMUWVcBFZHedOBGTuoxTZ3omlhPCC7K8wX4mPvdiLrw=; b=nAaCk7Qyrh3EhZxPk4tBbMhE+Ls346o7lVgyDBNFhOWnfpYnz5LSoZ6+PvVAqvkmWF TdHkktElqGgkdVn/ekFc+/YISj5nD54Y5oRFRnmLtmlHM9AxrHR76+gZxZVUMH61R25b h329hhvneQVB9n9xRMFrP+sp356jKwyz7EDygTA/qnTwKLusbyVcbpz53iyehnXDQZRf MFBRgv6nnGwVz7wALIna6IwqQqtSao1GX09L8BtrkZNl/mLa2bzC9k9aJ6bYx6CxFFyj ZP4oN8cdG4V5kZpBftu6fTXhaTX/Q7Xih7nHcSgOsh4xImevP0gTlOEBFr3XNGppStE7 M3dQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=CMUWVcBFZHedOBGTuoxTZ3omlhPCC7K8wX4mPvdiLrw=; b=XqviM2EoSaDIZ5QvXxdmx5Xc2JFHa09w6yaK5zzGhPrMPdNOTXa2vzK0t2g8kG+2Ub 4GTL5/YGQmXNbFcRAmhF+faVXUqgohsWG7Q8hqwCXmtPrCgTiOUb3VoDYgUKcbSlw1mM VWHSifYqfMYEJMEoBX5WahUeOjZjkdqSq83uPP9XerD3guoBR4ldSy1zIo+A+Fm4jjfj 2OzigPwgKew7rt/iG1QWuIhX8IikP6/AGwUfEaUSS1XBFfbj9SwVivEteojEUg1QBZkM zislvaQoHJgCRd/fQgzeY195ywDpJnXbfAAm6tkLW695J0sWBD5/fHS0wx/DJ6MDXyR0 PY4A== X-Gm-Message-State: AOAM5324bpg3a+5FbO+uXjPNKSCwIrPvk0Fvx2bkVp5EiBUSTBfjhhv9 dA/HIM+O96kl0rnbqdm8tCU= X-Google-Smtp-Source: ABdhPJyzDJ+H6hhlxyqIXHRM1w1m4dJg9EKU9UZXWN6RDq0+T5PGeTWthH6cvGT77OgdETOtvxNHDQ== X-Received: by 2002:a62:d453:0:b029:1d6:587b:5d31 with SMTP id u19-20020a62d4530000b02901d6587b5d31mr10130171pfl.58.1612631031046; Sat, 06 Feb 2021 09:03:51 -0800 (PST) Received: from ast-mbp.thefacebook.com ([163.114.132.7]) by smtp.gmail.com with ESMTPSA id j14sm11149964pjl.35.2021.02.06.09.03.49 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 06 Feb 2021 09:03:50 -0800 (PST) From: Alexei Starovoitov To: davem@davemloft.net Cc: daniel@iogearbox.net, bpf@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v2 bpf-next 3/7] bpf: Add per-program recursion prevention mechanism Date: Sat, 6 Feb 2021 09:03:40 -0800 Message-Id: <20210206170344.78399-4-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20210206170344.78399-1-alexei.starovoitov@gmail.com> References: <20210206170344.78399-1-alexei.starovoitov@gmail.com> Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov Since both sleepable and non-sleepable programs execute under migrate_disable add recursion prevention mechanism to both types of programs when they're executed via bpf trampoline. Signed-off-by: Alexei Starovoitov --- arch/x86/net/bpf_jit_comp.c | 15 +++++++++++++ include/linux/bpf.h | 6 ++--- include/linux/filter.h | 1 + kernel/bpf/core.c | 8 +++++++ kernel/bpf/trampoline.c | 22 ++++++++++++++----- .../selftests/bpf/prog_tests/fexit_stress.c | 2 +- .../bpf/prog_tests/trampoline_count.c | 4 ++-- 7 files changed, 47 insertions(+), 11 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index d11b9bcebbea..79e7a0ec1da5 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -1740,8 +1740,11 @@ static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog, struct bpf_prog *p, int stack_size, bool mod_ret) { u8 *prog = *pprog; + u8 *jmp_insn; int cnt = 0; + /* arg1: mov rdi, progs[i] */ + emit_mov_imm64(&prog, BPF_REG_1, (long) p >> 32, (u32) (long) p); if (emit_call(&prog, p->aux->sleepable ? __bpf_prog_enter_sleepable : __bpf_prog_enter, prog)) @@ -1749,6 +1752,14 @@ static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog, /* remember prog start time returned by __bpf_prog_enter */ emit_mov_reg(&prog, true, BPF_REG_6, BPF_REG_0); + /* if (__bpf_prog_enter*(prog) == 0) + * goto skip_exec_of_prog; + */ + EMIT3(0x48, 0x85, 0xC0); /* test rax,rax */ + /* emit 2 nops that will be replaced with JE insn */ + jmp_insn = prog; + emit_nops(&prog, 2); + /* arg1: lea rdi, [rbp - stack_size] */ EMIT4(0x48, 0x8D, 0x7D, -stack_size); /* arg2: progs[i]->insnsi for interpreter */ @@ -1767,6 +1778,10 @@ static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog, if (mod_ret) emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8); + /* replace 2 nops with JE insn, since jmp target is known */ + jmp_insn[0] = X86_JE; + jmp_insn[1] = prog - jmp_insn - 2; + /* arg1: mov rdi, progs[i] */ emit_mov_imm64(&prog, BPF_REG_1, (long) p >> 32, (u32) (long) p); /* arg2: mov rsi, rbx <- start time in nsec */ diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 2fa48439ef31..6f019b06a2fd 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -529,7 +529,7 @@ struct btf_func_model { /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50 * bytes on x86. Pick a number to fit into BPF_IMAGE_SIZE / 2 */ -#define BPF_MAX_TRAMP_PROGS 40 +#define BPF_MAX_TRAMP_PROGS 38 struct bpf_tramp_progs { struct bpf_prog *progs[BPF_MAX_TRAMP_PROGS]; @@ -561,9 +561,9 @@ int arch_prepare_bpf_trampoline(void *image, void *image_end, struct bpf_tramp_progs *tprogs, void *orig_call); /* these two functions are called from generated trampoline */ -u64 notrace __bpf_prog_enter(void); +u64 notrace __bpf_prog_enter(struct bpf_prog *prog); void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start); -u64 notrace __bpf_prog_enter_sleepable(void); +u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog); void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start); struct bpf_ksym { diff --git a/include/linux/filter.h b/include/linux/filter.h index c6592590a0b7..d6c740eac056 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -567,6 +567,7 @@ struct bpf_prog { struct bpf_prog_aux *aux; /* Auxiliary fields */ struct sock_fprog_kern *orig_prog; /* Original BPF program */ struct bpf_prog_stats __percpu *stats; + int __percpu *active; unsigned int (*bpf_func)(const void *ctx, const struct bpf_insn *insn); /* Instructions for interpreter */ diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index fa3da4cda476..f4560dbe7f31 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -91,6 +91,12 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag vfree(fp); return NULL; } + fp->active = alloc_percpu_gfp(int, GFP_KERNEL_ACCOUNT | gfp_extra_flags); + if (!fp->active) { + vfree(fp); + kfree(aux); + return NULL; + } fp->pages = size / PAGE_SIZE; fp->aux = aux; @@ -116,6 +122,7 @@ struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags) prog->stats = alloc_percpu_gfp(struct bpf_prog_stats, gfp_flags); if (!prog->stats) { + free_percpu(prog->active); kfree(prog->aux); vfree(prog); return NULL; @@ -250,6 +257,7 @@ void __bpf_prog_free(struct bpf_prog *fp) mutex_destroy(&fp->aux->used_maps_mutex); mutex_destroy(&fp->aux->dst_mutex); free_percpu(fp->stats); + free_percpu(fp->active); kfree(fp->aux->poke_tab); kfree(fp->aux); } diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index b1f567514b7e..226f613ab289 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -388,16 +388,21 @@ void bpf_trampoline_put(struct bpf_trampoline *tr) * call prog->bpf_func * call __bpf_prog_exit */ -#define NO_START_TIME 0 -u64 notrace __bpf_prog_enter(void) +#define NO_START_TIME 1 +u64 notrace __bpf_prog_enter(struct bpf_prog *prog) __acquires(RCU) { u64 start = NO_START_TIME; rcu_read_lock(); migrate_disable(); - if (static_branch_unlikely(&bpf_stats_enabled_key)) + if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) + return 0; + if (static_branch_unlikely(&bpf_stats_enabled_key)) { start = sched_clock(); + if (unlikely(!start)) + start = NO_START_TIME; + } return start; } @@ -425,25 +430,32 @@ void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start) __releases(RCU) { update_prog_stats(prog, start); + __this_cpu_dec(*(prog->active)); migrate_enable(); rcu_read_unlock(); } -u64 notrace __bpf_prog_enter_sleepable(void) +u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog) { u64 start = NO_START_TIME; rcu_read_lock_trace(); migrate_disable(); might_fault(); - if (static_branch_unlikely(&bpf_stats_enabled_key)) + if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) + return 0; + if (static_branch_unlikely(&bpf_stats_enabled_key)) { start = sched_clock(); + if (unlikely(!start)) + start = NO_START_TIME; + } return start; } void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start) { update_prog_stats(prog, start); + __this_cpu_dec(*(prog->active)); migrate_enable(); rcu_read_unlock_trace(); } diff --git a/tools/testing/selftests/bpf/prog_tests/fexit_stress.c b/tools/testing/selftests/bpf/prog_tests/fexit_stress.c index 3b9dbf7433f0..4698b0d2de36 100644 --- a/tools/testing/selftests/bpf/prog_tests/fexit_stress.c +++ b/tools/testing/selftests/bpf/prog_tests/fexit_stress.c @@ -3,7 +3,7 @@ #include /* x86-64 fits 55 JITed and 43 interpreted progs into half page */ -#define CNT 40 +#define CNT 38 void test_fexit_stress(void) { diff --git a/tools/testing/selftests/bpf/prog_tests/trampoline_count.c b/tools/testing/selftests/bpf/prog_tests/trampoline_count.c index 781c8d11604b..f3022d934e2d 100644 --- a/tools/testing/selftests/bpf/prog_tests/trampoline_count.c +++ b/tools/testing/selftests/bpf/prog_tests/trampoline_count.c @@ -4,7 +4,7 @@ #include #include -#define MAX_TRAMP_PROGS 40 +#define MAX_TRAMP_PROGS 38 struct inst { struct bpf_object *obj; @@ -52,7 +52,7 @@ void test_trampoline_count(void) struct bpf_link *link; char comm[16] = {}; - /* attach 'allowed' 40 trampoline programs */ + /* attach 'allowed' trampoline programs */ for (i = 0; i < MAX_TRAMP_PROGS; i++) { obj = bpf_object__open_file(object, NULL); if (CHECK(IS_ERR(obj), "obj_open_file", "err %ld\n", PTR_ERR(obj))) { From patchwork Sat Feb 6 17:03:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12072267 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4BEC9C433E0 for ; Sat, 6 Feb 2021 17:04:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 100F564E80 for ; Sat, 6 Feb 2021 17:04:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229636AbhBFREe (ORCPT ); Sat, 6 Feb 2021 12:04:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229541AbhBFREd (ORCPT ); Sat, 6 Feb 2021 12:04:33 -0500 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFA1AC06178A for ; Sat, 6 Feb 2021 09:03:52 -0800 (PST) Received: by mail-pl1-x631.google.com with SMTP id e12so5176707pls.4 for ; Sat, 06 Feb 2021 09:03:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=NNvlFpZ2P+nsN0N8rvXToy2l98iABgwCDQW0vsBD0NI=; b=TgvhfEoSretMhN2ajBMifUXoBnrTALAoLxzE5CcCB1fzs/NwEqZXNac1EqLuzgzcKu F8VgLNCJ4eKxOhl3W0j7DumYP/Fa97tZjZAp86v52WmL30vdDuctVAMttxUHZwAHuZQr ihXlkyGFthdQU8/ehO463erXIDrMbFwsilVxNnwPvDLGl2zG4WAoNb6Eoht/Z8lvNjsQ dT6BRKjAOOKGLDzTlti1nA1We86eYfLKeQy1G+Xvb5t8lPofGTVFmx9KuexMK49ghRzk WkLeAvsfEtXFyJ7nWx+L7P/RLMHbIW5zY+/WnL279jKLVhtjpXO7TKJ1mE8tHeuyreKf LdBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=NNvlFpZ2P+nsN0N8rvXToy2l98iABgwCDQW0vsBD0NI=; b=k/qXZcEez86syIMYkzgutle3RRaPHyYX2HnLRTxvylj0DB6YDRzI6hKIhC/YjrAcID f3T4/h52TrcIzwhZPNd9xHjBCOWD0BEAXgtuJidEP4JfwzWRKwnDPpYbuxj3O0QrdxOt +FvQYgaCu4Q1cj34MR6KwJ35tLk971B9x2SyFE7T8DvBuZCBvZ7UKRkepQ92nshP5aUn dAjt+f4cfcKEiEoS2J+PYdtjySuiH0+B0wR8ldYBigTwHA4/ZCTHUCoJScLPuYD6Qe1p 4kRq9xtLm32PPzboetfzl2b2kjZwVvV50MVRJI0ibzdFK8qr2i7M6JDpzNQHB02WAC+k jdzA== X-Gm-Message-State: AOAM531PLObhXd/oSmpgpjmmmchmE0ysaZFm0QhxOMx+ypbVWQ7ml3HG Jx1A1x6VLIYZpV5lCCjlMaI= X-Google-Smtp-Source: ABdhPJyFkjM0yCjHD2HH6r/jY9pffe+4hYrtSOPEig44ZgxlnxXATUAAUvV2nCQkW+lEuJy2NxS6gw== X-Received: by 2002:a17:90b:1008:: with SMTP id gm8mr9540487pjb.174.1612631032410; Sat, 06 Feb 2021 09:03:52 -0800 (PST) Received: from ast-mbp.thefacebook.com ([163.114.132.7]) by smtp.gmail.com with ESMTPSA id j14sm11149964pjl.35.2021.02.06.09.03.51 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 06 Feb 2021 09:03:51 -0800 (PST) From: Alexei Starovoitov To: davem@davemloft.net Cc: daniel@iogearbox.net, bpf@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v2 bpf-next 4/7] selftest/bpf: Add a recursion test Date: Sat, 6 Feb 2021 09:03:41 -0800 Message-Id: <20210206170344.78399-5-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20210206170344.78399-1-alexei.starovoitov@gmail.com> References: <20210206170344.78399-1-alexei.starovoitov@gmail.com> Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov Add recursive non-sleepable fentry program as a test. All attach points where sleepable progs can execute are non recursive so far. The recursion protection mechanism for sleepable cannot be activated yet. Signed-off-by: Alexei Starovoitov Acked-by: Andrii Nakryiko --- .../selftests/bpf/prog_tests/recursion.c | 33 +++++++++++++ tools/testing/selftests/bpf/progs/recursion.c | 46 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/recursion.c create mode 100644 tools/testing/selftests/bpf/progs/recursion.c diff --git a/tools/testing/selftests/bpf/prog_tests/recursion.c b/tools/testing/selftests/bpf/prog_tests/recursion.c new file mode 100644 index 000000000000..16e8eab5a29d --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/recursion.c @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Facebook */ +#include +#include "recursion.skel.h" + +void test_recursion(void) +{ + struct recursion *skel; + int key = 0; + int err; + + skel = recursion__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open_and_load")) + return; + + err = recursion__attach(skel); + if (!ASSERT_OK(err, "skel_attach")) + goto out; + + ASSERT_EQ(skel->bss->pass1, 0, "pass1 == 0"); + bpf_map_lookup_elem(bpf_map__fd(skel->maps.hash1), &key, 0); + ASSERT_EQ(skel->bss->pass1, 1, "pass1 == 1"); + bpf_map_lookup_elem(bpf_map__fd(skel->maps.hash1), &key, 0); + ASSERT_EQ(skel->bss->pass1, 2, "pass1 == 2"); + + ASSERT_EQ(skel->bss->pass2, 0, "pass2 == 0"); + bpf_map_lookup_elem(bpf_map__fd(skel->maps.hash2), &key, 0); + ASSERT_EQ(skel->bss->pass2, 1, "pass2 == 1"); + bpf_map_lookup_elem(bpf_map__fd(skel->maps.hash2), &key, 0); + ASSERT_EQ(skel->bss->pass2, 2, "pass2 == 2"); +out: + recursion__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/recursion.c b/tools/testing/selftests/bpf/progs/recursion.c new file mode 100644 index 000000000000..49f679375b9d --- /dev/null +++ b/tools/testing/selftests/bpf/progs/recursion.c @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Facebook */ + +#include "vmlinux.h" +#include +#include + +char _license[] SEC("license") = "GPL"; + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 1); + __type(key, int); + __type(value, long); +} hash1 SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 1); + __type(key, int); + __type(value, long); +} hash2 SEC(".maps"); + +int pass1 = 0; +int pass2 = 0; + +SEC("fentry/__htab_map_lookup_elem") +int BPF_PROG(on_lookup, struct bpf_map *map) +{ + int key = 0; + + if (map == (void *)&hash1) { + pass1++; + return 0; + } + if (map == (void *)&hash2) { + pass2++; + /* htab_map_gen_lookup() will inline below call + * into direct call to __htab_map_lookup_elem() + */ + bpf_map_lookup_elem(&hash2, &key); + return 0; + } + + return 0; +} From patchwork Sat Feb 6 17:03:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12072271 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33781C433E0 for ; Sat, 6 Feb 2021 17:05:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 07F2064E2C for ; Sat, 6 Feb 2021 17:05:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229715AbhBFRFK (ORCPT ); Sat, 6 Feb 2021 12:05:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229691AbhBFRFI (ORCPT ); Sat, 6 Feb 2021 12:05:08 -0500 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29B37C06178B for ; Sat, 6 Feb 2021 09:03:54 -0800 (PST) Received: by mail-pg1-x52b.google.com with SMTP id n10so6808799pgl.10 for ; Sat, 06 Feb 2021 09:03:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=mSEl96TyA6FmlILIBwStFeCiUiAQJTqNLulkv1cwKeE=; b=nueCpySJlPLarBg+pjEclrXIflXkHZ+cMg7bwXBjI4I5s1PQRVvgYotPbxg7YuZooF 9VaGU9ybYj/7gGa2biwE3pcTiSoCgMhSo+v+Ds5t3GLGFhqHJHJ/pTu3Pg9eBVgABrTQ Se5Ui+NztyoNHNwvMsa99HRDC8BEnlqQ41qA/oVMomw/sCxVNutbyhyVmwki4NrP4kPK ucUjsgzxwktqIdGCnR2e9lc46TpX16l5gh1+xCfHiHSEn4Pr0s9sqF+YZxjG+Xbo3+M3 XlGJ36LwodosHZCT6+rFt/zExW82NBb59VNzyuPF+Pgq91ljkLhsr8WtAbZqz05M/4NL pTiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=mSEl96TyA6FmlILIBwStFeCiUiAQJTqNLulkv1cwKeE=; b=Ft9qrEZHJ5eUiyfpgoEuwgM0Q9gun7/bX8n9Y78RwQRBr4bReryh/y9EOHBUaTzpFX EIRBmjohp9ekZD0wQRCsIqgYw/cEYdjDZBwN4/V+ZNZZNEqXWZ7G2ZcBwcZ4ASiEUjus Esoauvxko/XQLfvYIWVnewTHv6R1EIUpzKP3yXyJC7h5+w+/mRcCiF8yF+nzTBP23bsj ja5Tk5+2rLUZydeN5ZWCHGs7QbQty7npBFjMsq5V9MFyfA8/eBJ2a9VrM1VaXWEbeKfj d/fdNLnpiLQm46jWGKc5sG+2yAYGplFKYxJUzxrPFgsjVN5OQp8CIBYdzu7+AfSzfZaJ kdXg== X-Gm-Message-State: AOAM533oTruiBkVKV/FN4pV2O3GzBBNm2wyH3k9Y/379G4wZW+BqZXX8 n2dD7vwus8oM5xczx4sHDV4= X-Google-Smtp-Source: ABdhPJw/j116P52TxVNDHyAppTykyKanCS3Bo9Xkt4u8TC2s7TKMzMTqhYVWJigdVxgyuDImg0bdMw== X-Received: by 2002:a63:1f1e:: with SMTP id f30mr10374543pgf.141.1612631033767; Sat, 06 Feb 2021 09:03:53 -0800 (PST) Received: from ast-mbp.thefacebook.com ([163.114.132.7]) by smtp.gmail.com with ESMTPSA id j14sm11149964pjl.35.2021.02.06.09.03.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 06 Feb 2021 09:03:53 -0800 (PST) From: Alexei Starovoitov To: davem@davemloft.net Cc: daniel@iogearbox.net, bpf@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v2 bpf-next 5/7] bpf: Count the number of times recursion was prevented Date: Sat, 6 Feb 2021 09:03:42 -0800 Message-Id: <20210206170344.78399-6-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20210206170344.78399-1-alexei.starovoitov@gmail.com> References: <20210206170344.78399-1-alexei.starovoitov@gmail.com> Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov Add per-program counter for number of times recursion prevention mechanism was triggered and expose it via show_fdinfo and bpf_prog_info. Teach bpftool to print it. Signed-off-by: Alexei Starovoitov Acked-by: Andrii Nakryiko --- include/linux/filter.h | 1 + include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 14 ++++++++++---- kernel/bpf/trampoline.c | 18 ++++++++++++++++-- tools/bpf/bpftool/prog.c | 5 +++++ tools/include/uapi/linux/bpf.h | 1 + 6 files changed, 34 insertions(+), 6 deletions(-) diff --git a/include/linux/filter.h b/include/linux/filter.h index d6c740eac056..4c9850b35744 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -543,6 +543,7 @@ struct bpf_binary_header { struct bpf_prog_stats { u64 cnt; u64 nsecs; + u64 misses; struct u64_stats_sync syncp; } __aligned(2 * sizeof(u64)); diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index c001766adcbc..c547ad1ffe43 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -4501,6 +4501,7 @@ struct bpf_prog_info { __aligned_u64 prog_tags; __u64 run_time_ns; __u64 run_cnt; + __u64 recursion_misses; } __attribute__((aligned(8))); struct bpf_map_info { diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index f7df56a704de..c859bc46d06c 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -1731,25 +1731,28 @@ static int bpf_prog_release(struct inode *inode, struct file *filp) static void bpf_prog_get_stats(const struct bpf_prog *prog, struct bpf_prog_stats *stats) { - u64 nsecs = 0, cnt = 0; + u64 nsecs = 0, cnt = 0, misses = 0; int cpu; for_each_possible_cpu(cpu) { const struct bpf_prog_stats *st; unsigned int start; - u64 tnsecs, tcnt; + u64 tnsecs, tcnt, tmisses; st = per_cpu_ptr(prog->stats, cpu); do { start = u64_stats_fetch_begin_irq(&st->syncp); tnsecs = st->nsecs; tcnt = st->cnt; + tmisses = st->misses; } while (u64_stats_fetch_retry_irq(&st->syncp, start)); nsecs += tnsecs; cnt += tcnt; + misses += tmisses; } stats->nsecs = nsecs; stats->cnt = cnt; + stats->misses = misses; } #ifdef CONFIG_PROC_FS @@ -1768,14 +1771,16 @@ static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp) "memlock:\t%llu\n" "prog_id:\t%u\n" "run_time_ns:\t%llu\n" - "run_cnt:\t%llu\n", + "run_cnt:\t%llu\n" + "recursion_misses:\t%llu\n", prog->type, prog->jited, prog_tag, prog->pages * 1ULL << PAGE_SHIFT, prog->aux->id, stats.nsecs, - stats.cnt); + stats.cnt, + stats.misses); } #endif @@ -3438,6 +3443,7 @@ static int bpf_prog_get_info_by_fd(struct file *file, bpf_prog_get_stats(prog, &stats); info.run_time_ns = stats.nsecs; info.run_cnt = stats.cnt; + info.recursion_misses = stats.misses; if (!bpf_capable()) { info.jited_prog_len = 0; diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index 226f613ab289..83b77883bd77 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -381,6 +381,16 @@ void bpf_trampoline_put(struct bpf_trampoline *tr) mutex_unlock(&trampoline_mutex); } +static void notrace inc_misses_counter(struct bpf_prog *prog) +{ + struct bpf_prog_stats *stats; + + stats = this_cpu_ptr(prog->stats); + u64_stats_update_begin(&stats->syncp); + stats->misses++; + u64_stats_update_end(&stats->syncp); +} + /* The logic is similar to BPF_PROG_RUN, but with an explicit * rcu_read_lock() and migrate_disable() which are required * for the trampoline. The macro is split into @@ -396,8 +406,10 @@ u64 notrace __bpf_prog_enter(struct bpf_prog *prog) rcu_read_lock(); migrate_disable(); - if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) + if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) { + inc_misses_counter(prog); return 0; + } if (static_branch_unlikely(&bpf_stats_enabled_key)) { start = sched_clock(); if (unlikely(!start)) @@ -442,8 +454,10 @@ u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog) rcu_read_lock_trace(); migrate_disable(); might_fault(); - if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) + if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) { + inc_misses_counter(prog); return 0; + } if (static_branch_unlikely(&bpf_stats_enabled_key)) { start = sched_clock(); if (unlikely(!start)) diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index 1fe3ba255bad..2e1cd12589c5 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -368,6 +368,8 @@ static void print_prog_header_json(struct bpf_prog_info *info) jsonw_uint_field(json_wtr, "run_time_ns", info->run_time_ns); jsonw_uint_field(json_wtr, "run_cnt", info->run_cnt); } + if (info->recursion_misses) + jsonw_uint_field(json_wtr, "recursion_misses", info->recursion_misses); } static void print_prog_json(struct bpf_prog_info *info, int fd) @@ -446,6 +448,9 @@ static void print_prog_header_plain(struct bpf_prog_info *info) if (info->run_time_ns) printf(" run_time_ns %lld run_cnt %lld", info->run_time_ns, info->run_cnt); + if (info->recursion_misses) + printf(" recursion_misses %lld", + info->recursion_misses); printf("\n"); } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index c001766adcbc..c547ad1ffe43 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -4501,6 +4501,7 @@ struct bpf_prog_info { __aligned_u64 prog_tags; __u64 run_time_ns; __u64 run_cnt; + __u64 recursion_misses; } __attribute__((aligned(8))); struct bpf_map_info { From patchwork Sat Feb 6 17:03:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12072273 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC2B8C433DB for ; Sat, 6 Feb 2021 17:05:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7DE5464E81 for ; Sat, 6 Feb 2021 17:05:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229691AbhBFRFL (ORCPT ); Sat, 6 Feb 2021 12:05:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229693AbhBFRFI (ORCPT ); Sat, 6 Feb 2021 12:05:08 -0500 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84BDEC06178C for ; Sat, 6 Feb 2021 09:03:55 -0800 (PST) Received: by mail-pl1-x636.google.com with SMTP id u15so5193090plf.1 for ; Sat, 06 Feb 2021 09:03:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=p5OjMVcmFAQK4VRhF/dmgTvepnxhG787NrU5e2J2yVs=; b=efsLnt8lD1w6qCy+5DZ/h4c/oD6H7OLU27HzCp98Q54M0xKSu6US3jhYjxZgR7Rt4d zTAsCPB/Juaxdhgw1MYwhtHbr5VgBrEpOv31D8WW3KNobKtsVL4BcxiYiS8QkJuuZ+YA 1Uux2nCKrtSCN3BZBTCg2KA0ZfQn2cbk9rNUSyso+okm/lIRHhFpaKvH7pWgJDTbfjwX d/g1sTtWFRcwQK47hZ/W+pNL2TI/8L6nQ6kX9nXNZ3wuunZof9W0atspUyFLkU4Qoipw n+nAnGS36M+b8PpqRPVUuoe62yWy5k45wW/hfyku1LVm+/V3Y7xeVOA2PYLalo1mSxCT O4ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=p5OjMVcmFAQK4VRhF/dmgTvepnxhG787NrU5e2J2yVs=; b=QvXzfM+ku0Sq7ORLGPHKv8fBOSvgzHsdqpMePa7ghyqdjhBS05aPz0qQf/ui0trY/Z 1BNAjXvOMt14tfYxtLEolrVyGLwjtfX0dk9IQV3fLTETLdpnMG+91XvKfLqRGsGn4wr+ EPIgqL39FgiRTQ4BNm0ywHWz2OygfGjo7dnb4h7vDUkJUg+BL0Wnai5t7dR3/CykXIR4 CVstdXAZ33PPteDdC3j1EqMomqgFSXmRnlK8v/SWA4bL4h9B52wcAIy7VKI+a4ao+syX zt4W9jbFlf7pXQx54ZueIaIMaWUrRPcM6zlR+IjKwXq8EPCQsnigau17ESIH91JfiTce fCHg== X-Gm-Message-State: AOAM531wV4CPBRUkFNH6IePSgjx7WCFkuI3nH3fBoU5EfBPjkYuNCDDg dkfHH59Kd08XhQU21vpMZI0E4jB1Iuc= X-Google-Smtp-Source: ABdhPJyAzSC7qBLauVhpda92A+jY4ps8yssFJNR/2iOeyAwc6pRwAOUz8XdPXm9nGTcWFgIe0QMLFA== X-Received: by 2002:a17:902:d4c7:b029:e2:b2cb:136a with SMTP id o7-20020a170902d4c7b02900e2b2cb136amr7505775plg.53.1612631035084; Sat, 06 Feb 2021 09:03:55 -0800 (PST) Received: from ast-mbp.thefacebook.com ([163.114.132.7]) by smtp.gmail.com with ESMTPSA id j14sm11149964pjl.35.2021.02.06.09.03.53 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 06 Feb 2021 09:03:54 -0800 (PST) From: Alexei Starovoitov To: davem@davemloft.net Cc: daniel@iogearbox.net, bpf@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v2 bpf-next 6/7] bpf: Allows per-cpu maps and map-in-map in sleepable programs Date: Sat, 6 Feb 2021 09:03:43 -0800 Message-Id: <20210206170344.78399-7-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20210206170344.78399-1-alexei.starovoitov@gmail.com> References: <20210206170344.78399-1-alexei.starovoitov@gmail.com> Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov Since sleepable programs are now executing under migrate_disable the per-cpu maps are safe to use. The map-in-map were ok to use in sleepable from the time sleepable progs were introduced. Note that non-preallocated maps are still not safe, since there is no rcu_read_lock yet in sleepable programs and dynamically allocated map elements are relying on rcu protection. The sleepable programs have rcu_read_lock_trace instead. That limitation will be addresses in the future. Signed-off-by: Alexei Starovoitov Acked-by: Andrii Nakryiko --- kernel/bpf/hashtab.c | 4 ++-- kernel/bpf/verifier.c | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index c1ac7f964bc9..d63912e73ad9 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -1148,7 +1148,7 @@ static int __htab_percpu_map_update_elem(struct bpf_map *map, void *key, /* unknown flags */ return -EINVAL; - WARN_ON_ONCE(!rcu_read_lock_held()); + WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held()); key_size = map->key_size; @@ -1202,7 +1202,7 @@ static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key, /* unknown flags */ return -EINVAL; - WARN_ON_ONCE(!rcu_read_lock_held()); + WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_trace_held()); key_size = map->key_size; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 4189edb41b73..9561f2af7710 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -10020,9 +10020,14 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env, case BPF_MAP_TYPE_HASH: case BPF_MAP_TYPE_LRU_HASH: case BPF_MAP_TYPE_ARRAY: + case BPF_MAP_TYPE_PERCPU_HASH: + case BPF_MAP_TYPE_PERCPU_ARRAY: + case BPF_MAP_TYPE_LRU_PERCPU_HASH: + case BPF_MAP_TYPE_ARRAY_OF_MAPS: + case BPF_MAP_TYPE_HASH_OF_MAPS: if (!is_preallocated_map(map)) { verbose(env, - "Sleepable programs can only use preallocated hash maps\n"); + "Sleepable programs can only use preallocated maps\n"); return -EINVAL; } break; From patchwork Sat Feb 6 17:03:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12072275 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 50C11C433E6 for ; Sat, 6 Feb 2021 17:05:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2C28B64E80 for ; Sat, 6 Feb 2021 17:05:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229693AbhBFRFL (ORCPT ); Sat, 6 Feb 2021 12:05:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229753AbhBFRFK (ORCPT ); Sat, 6 Feb 2021 12:05:10 -0500 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C07D0C061793 for ; Sat, 6 Feb 2021 09:03:56 -0800 (PST) Received: by mail-pj1-x102b.google.com with SMTP id m12so5296964pjs.4 for ; Sat, 06 Feb 2021 09:03:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=UUFZ3hy4+kGr5BVmj29me3nmmAUMnwFvrmhzO7lqSPo=; b=l1Qxjru+8d2e2rxn/pyLnnJRGl62A18M3wOgwHHC4Hef8d+EEc9q7n6Z5E6IRd8+Bn 6BQJICeHms8iwhc8Zy8mps6J9/MIxktTB+NcY0DUlfXy8gUrYlA4dtbqxuUggLvCow+u +baZXAA/nO1cEuWn1PUdWjM4/wRbduHEA63IoprmyooiVIpagkLg1n2zSZ32BfEZnQej W87YqYAkjKij9ebKi8GcL92X/komGHQZJHhIqmBjePJumP0lWgne8mwieB4fVq4ExQ92 6MBRQc/9qbr2iVkPtYwPMj7pSRKn8+vzcaTU6SUaHiWJRgl08PHUt0apLQJtBt5zqVXz Oukw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=UUFZ3hy4+kGr5BVmj29me3nmmAUMnwFvrmhzO7lqSPo=; b=FeZjKnTaM+wnD0qDGD7ccxIi00pi3Ca9SV7yV1RMYe/nHu8KXA56zfz/ZfyQ1CWFtM misMgEX1Zj1ZrhS3TjWq+EEXOIxsBeGCs+1QD5gwHBp/tbifoAvae0C4HQ8KI8ZXZlpL nQrCVVeKcEDRG+LLzYzC0mAEFzuBmkDpwkNGd1P1Cu7ADS5rRuf9KXdCZ0z2zVmzWUHn j+iPWRj2ohx1OJ+Kr7sYCoKqTNiyMzYAP8Xcctg28oQfKyxPyhY4eoq8KozVA0cen64D UrMDfH0eCBXqK8/5XlfLDH4vWX1xZQlJBAClmdSEcnrlQztkK3JJUI7b4pGCjKptoim3 bhcA== X-Gm-Message-State: AOAM530dFPnM8IBEsHCVG74AFw3uqNqro3seluqzoPFsJEiUGcENEvab ZxOEXnkN4waAH6e0+VsCJ2OiVhl2z0M= X-Google-Smtp-Source: ABdhPJxsMmDznUJYfVRb/l2tS6kDkl9E4uHGWKOhCCO/6WuO+L/4eoFqMJCYI6kFuzkoZjd6fYYN3w== X-Received: by 2002:a17:90b:3892:: with SMTP id mu18mr9303563pjb.143.1612631036406; Sat, 06 Feb 2021 09:03:56 -0800 (PST) Received: from ast-mbp.thefacebook.com ([163.114.132.7]) by smtp.gmail.com with ESMTPSA id j14sm11149964pjl.35.2021.02.06.09.03.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 06 Feb 2021 09:03:55 -0800 (PST) From: Alexei Starovoitov To: davem@davemloft.net Cc: daniel@iogearbox.net, bpf@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v2 bpf-next 7/7] selftests/bpf: Add a test for map-in-map and per-cpu maps in sleepable progs Date: Sat, 6 Feb 2021 09:03:44 -0800 Message-Id: <20210206170344.78399-8-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.13.5 In-Reply-To: <20210206170344.78399-1-alexei.starovoitov@gmail.com> References: <20210206170344.78399-1-alexei.starovoitov@gmail.com> Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Alexei Starovoitov Add a basic test for map-in-map and per-cpu maps in sleepable programs. Signed-off-by: Alexei Starovoitov Acked-by: Andrii Nakryiko --- tools/testing/selftests/bpf/progs/lsm.c | 69 +++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/lsm.c b/tools/testing/selftests/bpf/progs/lsm.c index ff4d343b94b5..33694ef8acfa 100644 --- a/tools/testing/selftests/bpf/progs/lsm.c +++ b/tools/testing/selftests/bpf/progs/lsm.c @@ -30,6 +30,53 @@ struct { __type(value, __u64); } lru_hash SEC(".maps"); +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u64); +} percpu_array SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_HASH); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u64); +} percpu_hash SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u64); +} lru_percpu_hash SEC(".maps"); + +struct inner_map { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, int); + __type(value, __u64); +} inner_map SEC(".maps"); + +struct outer_arr { + __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS); + __uint(max_entries, 1); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(int)); + __array(values, struct inner_map); +} outer_arr SEC(".maps") = { + .values = { [0] = &inner_map }, +}; + +struct outer_hash { + __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS); + __uint(max_entries, 1); + __uint(key_size, sizeof(int)); + __array(values, struct inner_map); +} outer_hash SEC(".maps") = { + .values = { [0] = &inner_map }, +}; + char _license[] SEC("license") = "GPL"; int monitored_pid = 0; @@ -61,6 +108,7 @@ SEC("lsm.s/bprm_committed_creds") int BPF_PROG(test_void_hook, struct linux_binprm *bprm) { __u32 pid = bpf_get_current_pid_tgid() >> 32; + struct inner_map *inner_map; char args[64]; __u32 key = 0; __u64 *value; @@ -80,6 +128,27 @@ int BPF_PROG(test_void_hook, struct linux_binprm *bprm) value = bpf_map_lookup_elem(&lru_hash, &key); if (value) *value = 0; + value = bpf_map_lookup_elem(&percpu_array, &key); + if (value) + *value = 0; + value = bpf_map_lookup_elem(&percpu_hash, &key); + if (value) + *value = 0; + value = bpf_map_lookup_elem(&lru_percpu_hash, &key); + if (value) + *value = 0; + inner_map = bpf_map_lookup_elem(&outer_arr, &key); + if (inner_map) { + value = bpf_map_lookup_elem(inner_map, &key); + if (value) + *value = 0; + } + inner_map = bpf_map_lookup_elem(&outer_hash, &key); + if (inner_map) { + value = bpf_map_lookup_elem(inner_map, &key); + if (value) + *value = 0; + } return 0; }