From: Stanislav Fomichev <sdf@google.com>
To: netdev@vger.kernel.org, bpf@vger.kernel.org
Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
Stanislav Fomichev <sdf@google.com>
Subject: [PATCH bpf-next v3 1/7] bpf: add bpf_func_t and trampoline helpers
Date: Thu, 7 Apr 2022 15:31:06 -0700 [thread overview]
Message-ID: <20220407223112.1204582-2-sdf@google.com> (raw)
In-Reply-To: <20220407223112.1204582-1-sdf@google.com>
I'll be adding lsm cgroup specific helpers that grab
trampoline mutex.
No functional changes.
Signed-off-by: Stanislav Fomichev <sdf@google.com>
---
include/linux/bpf.h | 11 ++++---
kernel/bpf/trampoline.c | 63 +++++++++++++++++++++++------------------
2 files changed, 40 insertions(+), 34 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index bdb5298735ce..487aba40ce52 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -52,6 +52,8 @@ typedef u64 (*bpf_callback_t)(u64, u64, u64, u64, u64);
typedef int (*bpf_iter_init_seq_priv_t)(void *private_data,
struct bpf_iter_aux_info *aux);
typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
+typedef unsigned int (*bpf_func_t)(const void *,
+ const struct bpf_insn *);
struct bpf_iter_seq_info {
const struct seq_operations *seq_ops;
bpf_iter_init_seq_priv_t init_seq_private;
@@ -798,8 +800,7 @@ struct bpf_dispatcher {
static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(
const void *ctx,
const struct bpf_insn *insnsi,
- unsigned int (*bpf_func)(const void *,
- const struct bpf_insn *))
+ bpf_func_t bpf_func)
{
return bpf_func(ctx, insnsi);
}
@@ -827,8 +828,7 @@ int arch_prepare_bpf_dispatcher(void *image, s64 *funcs, int num_funcs);
noinline __nocfi unsigned int bpf_dispatcher_##name##_func( \
const void *ctx, \
const struct bpf_insn *insnsi, \
- unsigned int (*bpf_func)(const void *, \
- const struct bpf_insn *)) \
+ bpf_func_t bpf_func) \
{ \
return bpf_func(ctx, insnsi); \
} \
@@ -839,8 +839,7 @@ int arch_prepare_bpf_dispatcher(void *image, s64 *funcs, int num_funcs);
unsigned int bpf_dispatcher_##name##_func( \
const void *ctx, \
const struct bpf_insn *insnsi, \
- unsigned int (*bpf_func)(const void *, \
- const struct bpf_insn *)); \
+ bpf_func_t bpf_func); \
extern struct bpf_dispatcher bpf_dispatcher_##name;
#define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_##name##_func
#define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name)
diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
index ada97751ae1b..0c4fd194e801 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -407,42 +407,34 @@ static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog)
}
}
-int bpf_trampoline_link_prog(struct bpf_prog *prog, struct bpf_trampoline *tr)
+static int __bpf_trampoline_link_prog(struct bpf_prog *prog,
+ struct bpf_trampoline *tr)
{
enum bpf_tramp_prog_type kind;
int err = 0;
int cnt;
kind = bpf_attach_type_to_tramp(prog);
- mutex_lock(&tr->mutex);
- if (tr->extension_prog) {
+ if (tr->extension_prog)
/* cannot attach fentry/fexit if extension prog is attached.
* cannot overwrite extension prog either.
*/
- err = -EBUSY;
- goto out;
- }
+ return -EBUSY;
+
cnt = tr->progs_cnt[BPF_TRAMP_FENTRY] + tr->progs_cnt[BPF_TRAMP_FEXIT];
if (kind == BPF_TRAMP_REPLACE) {
/* Cannot attach extension if fentry/fexit are in use. */
- if (cnt) {
- err = -EBUSY;
- goto out;
- }
+ if (cnt)
+ return -EBUSY;
tr->extension_prog = prog;
- err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, NULL,
- prog->bpf_func);
- goto out;
- }
- if (cnt >= BPF_MAX_TRAMP_PROGS) {
- err = -E2BIG;
- goto out;
+ return bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, NULL,
+ prog->bpf_func);
}
- if (!hlist_unhashed(&prog->aux->tramp_hlist)) {
+ if (cnt >= BPF_MAX_TRAMP_PROGS)
+ return -E2BIG;
+ if (!hlist_unhashed(&prog->aux->tramp_hlist))
/* prog already linked */
- err = -EBUSY;
- goto out;
- }
+ return -EBUSY;
hlist_add_head(&prog->aux->tramp_hlist, &tr->progs_hlist[kind]);
tr->progs_cnt[kind]++;
err = bpf_trampoline_update(tr);
@@ -450,30 +442,45 @@ int bpf_trampoline_link_prog(struct bpf_prog *prog, struct bpf_trampoline *tr)
hlist_del_init(&prog->aux->tramp_hlist);
tr->progs_cnt[kind]--;
}
-out:
+ return err;
+}
+
+int bpf_trampoline_link_prog(struct bpf_prog *prog, struct bpf_trampoline *tr)
+{
+ int err;
+
+ mutex_lock(&tr->mutex);
+ err = __bpf_trampoline_link_prog(prog, tr);
mutex_unlock(&tr->mutex);
return err;
}
-/* bpf_trampoline_unlink_prog() should never fail. */
-int bpf_trampoline_unlink_prog(struct bpf_prog *prog, struct bpf_trampoline *tr)
+static int __bpf_trampoline_unlink_prog(struct bpf_prog *prog,
+ struct bpf_trampoline *tr)
{
enum bpf_tramp_prog_type kind;
int err;
kind = bpf_attach_type_to_tramp(prog);
- mutex_lock(&tr->mutex);
if (kind == BPF_TRAMP_REPLACE) {
WARN_ON_ONCE(!tr->extension_prog);
err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP,
tr->extension_prog->bpf_func, NULL);
tr->extension_prog = NULL;
- goto out;
+ return err;
}
hlist_del_init(&prog->aux->tramp_hlist);
tr->progs_cnt[kind]--;
- err = bpf_trampoline_update(tr);
-out:
+ return bpf_trampoline_update(tr);
+}
+
+/* bpf_trampoline_unlink_prog() should never fail. */
+int bpf_trampoline_unlink_prog(struct bpf_prog *prog, struct bpf_trampoline *tr)
+{
+ int err;
+
+ mutex_lock(&tr->mutex);
+ err = __bpf_trampoline_unlink_prog(prog, tr);
mutex_unlock(&tr->mutex);
return err;
}
--
2.35.1.1178.g4f1659d476-goog
next prev parent reply other threads:[~2022-04-07 22:31 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-07 22:31 [PATCH bpf-next v3 0/7] bpf: cgroup_sock lsm flavor Stanislav Fomichev
2022-04-07 22:31 ` Stanislav Fomichev [this message]
2022-04-07 22:31 ` [PATCH bpf-next v3 2/7] bpf: per-cgroup " Stanislav Fomichev
2022-04-08 14:20 ` kernel test robot
2022-04-08 15:53 ` kernel test robot
2022-04-08 16:42 ` Martin KaFai Lau
2022-04-08 16:42 ` Martin KaFai Lau
2022-04-08 22:12 ` Martin KaFai Lau
2022-04-11 19:07 ` Stanislav Fomichev
2022-04-12 1:04 ` Martin KaFai Lau
2022-04-12 16:42 ` Stanislav Fomichev
2022-04-07 22:31 ` [PATCH bpf-next v3 3/7] bpf: minimize number of allocated lsm slots per program Stanislav Fomichev
2022-04-08 22:56 ` Martin KaFai Lau
2022-04-09 17:04 ` Jakub Sitnicki
2022-04-11 18:44 ` Stanislav Fomichev
2022-04-15 17:39 ` Jakub Sitnicki
2022-04-15 18:46 ` Stanislav Fomichev
2022-04-12 1:19 ` Martin KaFai Lau
2022-04-12 16:42 ` Stanislav Fomichev
2022-04-12 17:40 ` Martin KaFai Lau
2022-04-11 18:46 ` Stanislav Fomichev
2022-04-12 1:36 ` Martin KaFai Lau
2022-04-12 16:42 ` Stanislav Fomichev
2022-04-12 18:13 ` Martin KaFai Lau
2022-04-12 19:01 ` Stanislav Fomichev
2022-04-12 20:19 ` Martin KaFai Lau
2022-04-12 20:36 ` Stanislav Fomichev
2022-04-12 22:13 ` Martin KaFai Lau
2022-04-12 22:42 ` Stanislav Fomichev
2022-04-07 22:31 ` [PATCH bpf-next v3 4/7] bpf: allow writing to a subset of sock fields from lsm progtype Stanislav Fomichev
2022-04-07 22:31 ` [PATCH bpf-next v3 5/7] libbpf: add lsm_cgoup_sock type Stanislav Fomichev
2022-04-07 22:31 ` [PATCH bpf-next v3 6/7] selftests/bpf: lsm_cgroup functional test Stanislav Fomichev
2022-04-07 22:31 ` [PATCH bpf-next v3 7/7] selftests/bpf: verify lsm_cgroup struct sock access Stanislav Fomichev
-- strict thread matches above, loose matches on Subject: below --
2022-04-08 22:02 [PATCH bpf-next v3 2/7] bpf: per-cgroup lsm flavor kernel test robot
2022-04-11 8:26 ` Dan Carpenter
2022-04-11 8:26 ` Dan Carpenter
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220407223112.1204582-2-sdf@google.com \
--to=sdf@google.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.