From patchwork Tue Nov 19 16:07:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= X-Patchwork-Id: 1197527 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="ms4KXRyW"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47HW2W2B1rz9s4Y for ; Wed, 20 Nov 2019 03:08:19 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728365AbfKSQIT (ORCPT ); Tue, 19 Nov 2019 11:08:19 -0500 Received: from mail-pl1-f196.google.com ([209.85.214.196]:38535 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727509AbfKSQIS (ORCPT ); Tue, 19 Nov 2019 11:08:18 -0500 Received: by mail-pl1-f196.google.com with SMTP id q18so8294756pls.5; Tue, 19 Nov 2019 08:08:18 -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 :mime-version:content-transfer-encoding; bh=klGeJnE305AtfoiEXYFBzceSM/DtDgtF5tDK2GDj26g=; b=ms4KXRyW9whdZSFRIutwjTD4ERHD+emIWf9PnkY0SuVWaKHp4kVGNhcI9LgrhC1Qeo RNO8GrPLr8vk+/QZpRlTR5Hzht+vsk5QXMPojp+9ywKoQ3cTkHqKZ3GNySZSikDgYigo ahO3lt8AKyK33GuGe8jb68i1gWzIrNgzExhUFEiEHUQLyKXCApNcpHLqxYiJyCcY4DJw Cfs2AA+Q/jtwn9/4cFejwJe8TgcL7rJ6d8qywTLSbFPPDSr+ezbvPPVD3oYkM9hfY86K /dUwVK/Abg4MAuwbZgRmRD/w+aFjeIsDfqy60c6Qj/zM4CGdXveyH+70CtG8ip7zT8oU 2s1A== 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:mime-version:content-transfer-encoding; bh=klGeJnE305AtfoiEXYFBzceSM/DtDgtF5tDK2GDj26g=; b=SF2VomFrX+bWenxmidaf0eVgKAS81vrreSbBQLf4SwBq6WwR2Sb1M3+FvsyioivvQO WejDyld1rtvHaTi13EyOXlyyc4h7raRLv1WvuoSN1pbwIUPx+EeJpqFS1WX0LchTgUzA wc1rhkO00ML4WkpPaCFNEOcv3TpAbEXWKJi5AcmXflnz0FNvmAnqYlWiuhYSv//iFJCO plrw3cLnUW7s7Ltk0mrqrB9eHadVL9HjncocnWaiKqq4ID3mG9zKYFANI+W84L8/wJIi hTq5BHtElV22ylkg7g/nQrH4IGhVPKZweg/W3mNavrrOxo43dNrBu5tsh33+vb4hJJge J0nw== X-Gm-Message-State: APjAAAX/Ntt8Q/zsYP6KEgdx5P3pja+fT1d4qFQpDNJpNKChFfMbbb5F ujLAv+DAGRMchSlUqePOgw10C5/L4AdJ8g== X-Google-Smtp-Source: APXvYqzbJBjWqousiKff0fv9U3LX4TITLR5JGzft/GJ47JO5kuHqoveWRaSfpqthvZ12OpTcZDgtQw== X-Received: by 2002:a17:90a:a898:: with SMTP id h24mr7620388pjq.48.1574179697470; Tue, 19 Nov 2019 08:08:17 -0800 (PST) Received: from btopel-mobl.ger.intel.com (fmdmzpr04-ext.fm.intel.com. [192.55.55.39]) by smtp.gmail.com with ESMTPSA id v10sm25196949pfg.11.2019.11.19.08.08.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2019 08:08:16 -0800 (PST) From: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= To: netdev@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , bpf@vger.kernel.org, magnus.karlsson@gmail.com, magnus.karlsson@intel.com, jonathan.lemon@gmail.com, ecree@solarflare.com, thoiland@redhat.com, andrii.nakryiko@gmail.com Subject: [PATCH bpf-next 1/3] bpf: introduce BPF dispatcher Date: Tue, 19 Nov 2019 17:07:55 +0100 Message-Id: <20191119160757.27714-2-bjorn.topel@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191119160757.27714-1-bjorn.topel@gmail.com> References: <20191119160757.27714-1-bjorn.topel@gmail.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org From: Björn Töpel The BPF dispatcher is a multiway branch code generator, mainly targeted for XDP programs. When an XDP program is executed via the bpf_prog_run_xdp(), it is invoked via an indirect call. With retpolines enabled, the indirect call has a substantial performance impact. The dispatcher is a mechanism that transform multiple indirect calls to direct calls, and therefore avoids the retpoline. The dispatcher is generated using the BPF JIT, and relies on text poking provided by bpf_arch_text_poke(). The dispatcher hijacks a trampoline function it via the __fentry__ nop of the trampoline. One dispatcher instance currently supports up to 16 dispatch points. This can be extended in the future. An example: A module/driver allocates a dispatcher. The dispatcher is shared for all netdevs. Each unique XDP program has a slot in the dispatcher, registered by a netdev. The netdev then uses the dispatcher to call the correct program with a direct call. Signed-off-by: Björn Töpel --- arch/x86/net/bpf_jit_comp.c | 135 +++++++++++++++++++++++ kernel/bpf/Makefile | 1 + kernel/bpf/dispatcher.c | 208 ++++++++++++++++++++++++++++++++++++ 3 files changed, 344 insertions(+) create mode 100644 kernel/bpf/dispatcher.c diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 8704b33ad2e6..579ecb40d32e 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -10,10 +10,12 @@ #include #include #include +#include #include #include #include #include +#include static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len) { @@ -1551,6 +1553,139 @@ int arch_prepare_bpf_trampoline(void *image, struct btf_func_model *m, u32 flags return 0; } +#if defined(CONFIG_BPF_JIT) && defined(CONFIG_RETPOLINE) + +static int emit_cond_near_jump(u8 **pprog, void *func, void *ip, u8 jmp_cond) +{ + u8 *prog = *pprog; + int cnt = 0; + s64 offset; + + offset = func - (ip + 2 + 4); + if (!is_simm32(offset)) { + pr_err("Target %p is out of range\n", func); + return -EINVAL; + } + EMIT2_off32(0x0F, jmp_cond + 0x10, offset); + *pprog = prog; + return 0; +} + +static void emit_nops(u8 **pprog, unsigned int len) +{ + unsigned int i, noplen; + u8 *prog = *pprog; + int cnt = 0; + + while (len > 0) { + noplen = len; + + if (noplen > ASM_NOP_MAX) + noplen = ASM_NOP_MAX; + + for (i = 0; i < noplen; i++) + EMIT1(ideal_nops[noplen][i]); + len -= noplen; + } + + *pprog = prog; +} + +static int emit_bpf_dispatcher(u8 **pprog, int a, int b, s64 *progs) +{ + u8 *jg_reloc, *jg_target, *prog = *pprog; + int pivot, err, jg_bytes = 1, cnt = 0; + s64 jg_offset; + + if (a == b) { + /* Leaf node of recursion, i.e. not a range of indices + * anymore. + */ + EMIT1(add_1mod(0x48, BPF_REG_3)); /* cmp rdx,func */ + if (!is_simm32(progs[a])) + return -1; + EMIT2_off32(0x81, add_1reg(0xF8, BPF_REG_3), + progs[a]); + err = emit_cond_near_jump(&prog, /* je func */ + (void *)progs[a], prog, + X86_JE); + if (err) + return err; + + err = emit_jump(&prog, /* jmp thunk */ + __x86_indirect_thunk_rdx, prog); + if (err) + return err; + + *pprog = prog; + return 0; + } + + /* Not a leaf node, so we pivot, and recursively descend into + * the lower and upper ranges. + */ + pivot = (b - a) / 2; + EMIT1(add_1mod(0x48, BPF_REG_3)); /* cmp rdx,func */ + if (!is_simm32(progs[a + pivot])) + return -1; + EMIT2_off32(0x81, add_1reg(0xF8, BPF_REG_3), progs[a + pivot]); + + if (pivot > 2) { /* jg upper_part */ + /* Require near jump. */ + jg_bytes = 4; + EMIT2_off32(0x0F, X86_JG + 0x10, 0); + } else { + EMIT2(X86_JG, 0); + } + jg_reloc = prog; + + err = emit_bpf_dispatcher(&prog, a, a + pivot, /* emit lower_part */ + progs); + if (err) + return err; + + /* Intel 64 and IA-32 ArchitecturesOptimization Reference + * Manual, 3.4.1.5 Code Alignment Assembly/Compiler Coding + * Rule 12. (M impact, H generality) All branch targets should + * be 16-byte aligned. + */ + jg_target = PTR_ALIGN(prog, 16); + if (jg_target != prog) + emit_nops(&prog, jg_target - prog); + jg_offset = prog - jg_reloc; + emit_code(jg_reloc - jg_bytes, jg_offset, jg_bytes); + + err = emit_bpf_dispatcher(&prog, a + pivot + 1, /* emit upper_part */ + b, progs); + if (err) + return err; + + *pprog = prog; + return 0; +} + +static int cmp_ips(const void *a, const void *b) +{ + const s64 *ipa = a; + const s64 *ipb = b; + + if (*ipa > *ipb) + return 1; + if (*ipa < *ipb) + return -1; + return 0; +} + +int arch_prepare_bpf_dispatcher(void *image, s64 *funcs, int num_funcs) +{ + u8 *prog = image; + + sort(funcs, num_funcs, sizeof(funcs[0]), cmp_ips, NULL); + return emit_bpf_dispatcher(&prog, 0, num_funcs - 1, funcs); +} + +#endif + struct x64_jit_data { struct bpf_binary_header *header; int *addrs; diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile index 3f671bf617e8..d4f330351f87 100644 --- a/kernel/bpf/Makefile +++ b/kernel/bpf/Makefile @@ -8,6 +8,7 @@ obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o obj-$(CONFIG_BPF_SYSCALL) += disasm.o obj-$(CONFIG_BPF_JIT) += trampoline.o obj-$(CONFIG_BPF_SYSCALL) += btf.o +obj-$(CONFIG_BPF_JIT) += dispatcher.o ifeq ($(CONFIG_NET),y) obj-$(CONFIG_BPF_SYSCALL) += devmap.o obj-$(CONFIG_BPF_SYSCALL) += cpumap.o diff --git a/kernel/bpf/dispatcher.c b/kernel/bpf/dispatcher.c new file mode 100644 index 000000000000..59a565107fd1 --- /dev/null +++ b/kernel/bpf/dispatcher.c @@ -0,0 +1,208 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright(c) 2019 Intel Corporation. */ + +#ifdef CONFIG_RETPOLINE + +#include +#include +#include + +/* The BPF dispatcher is a multiway branch code generator. The + * dispatcher is a mechanism to avoid the performance penalty of an + * indirect call when retpolines are enabled. A dispatch client tries + * to register a BPF program into the dispatcher, and if there is + * available room in the dispatcher a direct call to the BPF program + * will be generated. All calls to the BPF programs called via the + * dispatcher will then be a direct call, instead of an indirect. The + * dispatcher hijacks a trampoline function it via the __fentry__ of + * the trampoline. The trampoline function has the following + * signature: + * unsigned int trampoline( + * const void *xdp_ctx, + * const struct bpf_insn *insnsi, + * unsigned int (*bpf_func)(const void *, + * const struct bpf_insn *)); + * + * Direct use of the dispatcher is discouraged, and instead a wrapper + * such as xdp_call.h should be used. + */ + +#define DISPATCHER_HASH_BITS 10 +#define DISPATCHER_TABLE_SIZE (1 << DISPATCHER_HASH_BITS) + +static struct hlist_head dispatcher_table[DISPATCHER_TABLE_SIZE]; + +#define BPF_DISPATCHER_MAX 16 + +struct bpf_dispatcher { + struct hlist_node hlist; + void *func; + struct bpf_prog *progs[BPF_DISPATCHER_MAX]; + int num_progs; + void *image; + u64 selector; +}; + +static DEFINE_MUTEX(dispatcher_mutex); + +struct bpf_dispatcher *bpf_dispatcher_lookup(void *func) +{ + struct bpf_dispatcher *d; + struct hlist_head *head; + void *image; + + head = &dispatcher_table[hash_ptr(func, DISPATCHER_HASH_BITS)]; + hlist_for_each_entry(d, head, hlist) { + if (d->func == func) + return d; + } + d = kzalloc(sizeof(*d), GFP_KERNEL); + if (!d) + return NULL; + + image = bpf_jit_alloc_exec(PAGE_SIZE); + if (!image) { + kfree(d); + return NULL; + } + + d->func = func; + INIT_HLIST_NODE(&d->hlist); + hlist_add_head(&d->hlist, head); + + set_vm_flush_reset_perms(image); + set_memory_x((long)image, 1); + d->image = image; + return d; +} + +static void bpf_dispatcher_free(struct bpf_dispatcher *d) +{ + bpf_jit_free_exec(d->image); + hlist_del(&d->hlist); + kfree(d); +} + +static bool bpf_dispatcher_add_prog(struct bpf_dispatcher *d, + struct bpf_prog *prog) +{ + struct bpf_prog **entry = NULL; + int i; + + if (!prog) + return false; + + if (d->num_progs == BPF_DISPATCHER_MAX) + return false; + + for (i = 0; i < BPF_DISPATCHER_MAX; i++) { + if (!entry && !d->progs[i]) + entry = &d->progs[i]; + if (d->progs[i] == prog) + return false; + } + + bpf_prog_inc(prog); + *entry = prog; + d->num_progs++; + return true; +} + +static bool bpf_dispatcher_remove_prog(struct bpf_dispatcher *d, + struct bpf_prog *prog) +{ + int i; + + if (!prog) + return false; + + for (i = 0; i < BPF_DISPATCHER_MAX; i++) { + if (d->progs[i] == prog) { + bpf_prog_put(prog); + d->progs[i] = NULL; + d->num_progs--; + return true; + } + } + return false; +} + +int __weak arch_prepare_bpf_dispatcher(void *image, s64 *funcs, + int num_funcs) +{ + return -ENOTSUPP; +} + +static void bpf_dispatcher_update(struct bpf_dispatcher *d) +{ + void *old_image = d->image + ((d->selector + 1) & 1) * PAGE_SIZE / 2; + void *new_image = d->image + (d->selector & 1) * PAGE_SIZE / 2; + s64 ips[BPF_DISPATCHER_MAX] = {}, *ipsp = &ips[0]; + int i, err; + + if (!d->num_progs) { + bpf_arch_text_poke(d->func, BPF_MOD_JUMP_TO_NOP, + old_image, NULL); + return; + } + + for (i = 0; i < BPF_DISPATCHER_MAX; i++) { + if (d->progs[i]) + *ipsp++ = (s64)d->progs[i]->bpf_func; + } + err = arch_prepare_bpf_dispatcher(new_image, &ips[0], d->num_progs); + if (err) + return; + + if (d->selector) { + /* progs already running at this address */ + err = bpf_arch_text_poke(d->func, BPF_MOD_JUMP_TO_JUMP, + old_image, new_image); + } else { + /* first time registering */ + err = bpf_arch_text_poke(d->func, BPF_MOD_NOP_TO_JUMP, + NULL, new_image); + } + if (err) + return; + d->selector++; +} + +void bpf_dispatcher_change_prog(void *func, struct bpf_prog *from, + struct bpf_prog *to) +{ + struct bpf_dispatcher *d; + bool changed = false; + + if (from == to) + return; + + mutex_lock(&dispatcher_mutex); + d = bpf_dispatcher_lookup(func); + if (!d) + goto out; + + changed |= bpf_dispatcher_remove_prog(d, from); + changed |= bpf_dispatcher_add_prog(d, to); + + if (!changed) + goto out; + + bpf_dispatcher_update(d); + if (!d->num_progs) + bpf_dispatcher_free(d); +out: + mutex_unlock(&dispatcher_mutex); +} + +static int __init init_dispatchers(void) +{ + int i; + + for (i = 0; i < DISPATCHER_TABLE_SIZE; i++) + INIT_HLIST_HEAD(&dispatcher_table[i]); + return 0; +} +late_initcall(init_dispatchers); + +#endif From patchwork Tue Nov 19 16:07:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= X-Patchwork-Id: 1197529 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="N1XsKVhQ"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47HW2c0VZ3z9s4Y for ; Wed, 20 Nov 2019 03:08:24 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728126AbfKSQIX (ORCPT ); Tue, 19 Nov 2019 11:08:23 -0500 Received: from mail-pj1-f68.google.com ([209.85.216.68]:46297 "EHLO mail-pj1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727509AbfKSQIX (ORCPT ); Tue, 19 Nov 2019 11:08:23 -0500 Received: by mail-pj1-f68.google.com with SMTP id a16so2809115pjs.13; Tue, 19 Nov 2019 08:08:23 -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 :mime-version:content-transfer-encoding; bh=K0ZrQzSNx++0gTcNWsr5sTYHxi0Jp8O7MVmhMwOwAMg=; b=N1XsKVhQzOw1/S2eW2Td57jbeXx438xtH8poBJNqyihq6R3UCSipYk9Re7oK5WW4eE JNeCY8BDfYdsBIGsxQWPAgdCm4LrXiXdaV/aKAW357UVQH6h7HGoIDemQ40Nd44iXTH2 OTZA7lPkvhyyX3q9gNdjv4PDc3emWsNIwToNPXULHnYwkX6XcnN32XQmZQoEYqXoo0Rd AEp2iUelAorlQ3+tdXim96F9Wbwnu87bic4ZAKGiPuujQDlLSCRuceGLspGMf8sArSOY ci9BswNJVVE25lF53GSrRlEAflcP8qBM7qAdFvBwsSLErnE8qs3K18mbpCOSDN3pUrmt CJ/w== 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:mime-version:content-transfer-encoding; bh=K0ZrQzSNx++0gTcNWsr5sTYHxi0Jp8O7MVmhMwOwAMg=; b=eGaEnwGFfy9u4DhffAlb/WmiVlslOA3rbyXaNYZWEJD8xjfjzkUWw69p8saa1dH3Tc YyuDO5qwLk8djl1o/GaSVXEsesBaCyhAF7g7150LJcpWMZyKgWP5jCPIJAjxyc4iS45c txnFLkGYsn4oTnRGEEkltFN+Mbcv5qzd2CvP1ykOr2h8Y2pERMcf5jyctb2PEgxb+kRC 0Ys4OEhAPHasUJbPwxUX+Ek08M302NupmnrmDUgTFspDywWAO7d7RLwV/YRyOoLvqLEs IrR4JrA3MTnOgFwvCBC/B9AhdJkHOLUbo6nBssEzcANoY3wyJASoLWfEa6lwlB97IbEG KSZw== X-Gm-Message-State: APjAAAU07YV/4B/1EIa1KZkA8kNktwULlHj0x8dHYp71oHu2G/VV2Bg5 YBgkOtCPr8FSnCFX2g9nDBgVjNGWsKAQ4w== X-Google-Smtp-Source: APXvYqw0e2TaKVQR1OSIs4UFqTajbw8U/JflYQMfVbjX5VOcPbNqo5iLfRoqj1a3b+2ZF+t1nUw3xA== X-Received: by 2002:a17:90a:eb18:: with SMTP id j24mr7716170pjz.85.1574179702603; Tue, 19 Nov 2019 08:08:22 -0800 (PST) Received: from btopel-mobl.ger.intel.com (fmdmzpr04-ext.fm.intel.com. [192.55.55.39]) by smtp.gmail.com with ESMTPSA id v10sm25196949pfg.11.2019.11.19.08.08.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2019 08:08:21 -0800 (PST) From: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= To: netdev@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , bpf@vger.kernel.org, magnus.karlsson@gmail.com, magnus.karlsson@intel.com, jonathan.lemon@gmail.com, ecree@solarflare.com, thoiland@redhat.com, andrii.nakryiko@gmail.com Subject: [PATCH bpf-next 2/3] xdp: introduce xdp_call Date: Tue, 19 Nov 2019 17:07:56 +0100 Message-Id: <20191119160757.27714-3-bjorn.topel@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191119160757.27714-1-bjorn.topel@gmail.com> References: <20191119160757.27714-1-bjorn.topel@gmail.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org From: Björn Töpel The xdp_call.h header wraps a more user-friendly API around the BPF dispatcher. A user adds a trampoline/XDP caller using the DEFINE_XDP_CALL macro, and updates the BPF dispatcher via xdp_call_update(). The actual dispatch is done via xdp_call(). The next patch will show-case how the i40e driver uses xdp_call. Signed-off-by: Björn Töpel --- include/linux/xdp_call.h | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 include/linux/xdp_call.h diff --git a/include/linux/xdp_call.h b/include/linux/xdp_call.h new file mode 100644 index 000000000000..3591a834d4dd --- /dev/null +++ b/include/linux/xdp_call.h @@ -0,0 +1,66 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright(c) 2019 Intel Corporation. */ +#ifndef _LINUX_XDP_CALL_H +#define _LINUX_XDP_CALL_H + +#include + +#if defined(CONFIG_BPF_JIT) && defined(CONFIG_RETPOLINE) + +void bpf_dispatcher_change_prog(void *func, struct bpf_prog *from, + struct bpf_prog *to); + +#define XDP_CALL_TRAMP(name) ____xdp_call_##name##_tramp + +#define DEFINE_XDP_CALL(name) \ + unsigned int XDP_CALL_TRAMP(name)( \ + const void *xdp_ctx, \ + const struct bpf_insn *insnsi, \ + unsigned int (*bpf_func)(const void *, \ + const struct bpf_insn *)) \ + { \ + return bpf_func(xdp_ctx, insnsi); \ + } + +#define DECLARE_XDP_CALL(name) \ + unsigned int XDP_CALL_TRAMP(name)( \ + const void *xdp_ctx, \ + const struct bpf_insn *insnsi, \ + unsigned int (*bpf_func)(const void *, \ + const struct bpf_insn *)) + +#define xdp_call_run(name, prog, ctx) ({ \ + u32 ret; \ + cant_sleep(); \ + if (static_branch_unlikely(&bpf_stats_enabled_key)) { \ + struct bpf_prog_stats *stats; \ + u64 start = sched_clock(); \ + ret = XDP_CALL_TRAMP(name)(ctx, \ + (prog)->insnsi, \ + (prog)->bpf_func); \ + stats = this_cpu_ptr((prog)->aux->stats); \ + u64_stats_update_begin(&stats->syncp); \ + stats->cnt++; \ + stats->nsecs += sched_clock() - start; \ + u64_stats_update_end(&stats->syncp); \ + } else { \ + ret = XDP_CALL_TRAMP(name)(ctx, \ + (prog)->insnsi, \ + (prog)->bpf_func); \ + } \ + ret; }) + +#define xdp_call_update(name, from_xdp_prog, to_xdp_prog) \ + bpf_dispatcher_change_prog(&XDP_CALL_TRAMP(name), \ + from_xdp_prog, \ + to_xdp_prog) + +#else /* !defined(CONFIG_BPF_JIT) || !defined(CONFIG_RETPOLINE) */ + +#define DEFINE_XDP_CALL(name) +#define DECLARE_XDP_CALL(name) +#define xdp_call_run(name, xdp_prog, xdp) bpf_prog_run_xdp(xdp_prog, xdp) +#define xdp_call_update(name, from_xdp_prog, to_xdp_prog) + +#endif /* defined(CONFIG_BPF_JIT) && defined(CONFIG_RETPOLINE) */ +#endif /* _LINUX_XDP_CALL_H */ From patchwork Tue Nov 19 16:07:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= X-Patchwork-Id: 1197531 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: incoming-bpf@patchwork.ozlabs.org Delivered-To: patchwork-incoming-bpf@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=bpf-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Wgt1YPea"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47HW2j2VSXz9s4Y for ; Wed, 20 Nov 2019 03:08:29 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728414AbfKSQI3 (ORCPT ); Tue, 19 Nov 2019 11:08:29 -0500 Received: from mail-pg1-f195.google.com ([209.85.215.195]:42353 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727509AbfKSQI2 (ORCPT ); Tue, 19 Nov 2019 11:08:28 -0500 Received: by mail-pg1-f195.google.com with SMTP id q17so11555719pgt.9; Tue, 19 Nov 2019 08:08:28 -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 :mime-version:content-transfer-encoding; bh=+hHPk4MDXZMe1Dt2BSBD6rDj4LAJJcjXpcIlnBfgPIE=; b=Wgt1YPeae8eLuvi1LEIiW2eqtV6TQRRM2ChIYhhhvcVVCBR62YLbBT5aTeE5ngpuSf fNVa3JAQz4LPQcvYKgbskTBQnpNBJshktwzRSAIr/45Eb+sJqMQ6NBiOihi2HYzRwg8s dyMPS3d657BP4b6bRpKLBLS2r7SRYsGILxQ+JxUCeJGh6Drf74sZo+Rnm/cLXcpnKQe3 MqDCB4OqV3G9wHZzCYTZX5kb0YCzNvVRielP3JMvL2ykB+WaqtccYEXPgAQnxmqI+KUq qJUdsCHaYnkLiNPwReHQTnqZgWTclrzmB7+B5k75D+r4OzbobuEIFDi/96Egx1hTJn8C c8Ww== 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:mime-version:content-transfer-encoding; bh=+hHPk4MDXZMe1Dt2BSBD6rDj4LAJJcjXpcIlnBfgPIE=; b=XAQy4TcEll4Ip6bJJ2jX0lHazo2o6SaWenWrYffOvyrDxxC/msUJ8VNYwfu1uGB8AI exLYybG6IqzTmE0dusRCjsjYjqhdXsrgj7bPrMfmocOaf0joNhMysC6h+NZye2moijfE 18aS0gMO9LVNzmgvIG7wiaVBZSCh3gYgSj/EMlF+hB9gTSDGUZp085LxLpPmRDTNLi0+ e04vW/eYzUeuFopLX5rZ4sPJgdnYqEg4py58VO6VrbqEgZoSr7SuoAR/G88zQXU6i49y G6hbCiAHpT0c0drWTV0+NlQOpxgbcSvYZSeqQ/34xTwVsp8xDZt/l2bZF9++4/hfqRWk G47w== X-Gm-Message-State: APjAAAX0/9gZ91Vsue2oaYGN4PDqV6Ly8QMlZArQA1uT8aOq/REJnsA2 Ij04RjNyxfMq5qdlzj1MKDzFJkR4S7yTkA== X-Google-Smtp-Source: APXvYqygOWptp54R9WSYNG8WufP/qVSs84JKhK0pkzVLnNb2j5AfQ4/5iSY4L+ejdn9jFYLvH2IE9Q== X-Received: by 2002:aa7:9348:: with SMTP id 8mr6632106pfn.135.1574179707595; Tue, 19 Nov 2019 08:08:27 -0800 (PST) Received: from btopel-mobl.ger.intel.com (fmdmzpr04-ext.fm.intel.com. [192.55.55.39]) by smtp.gmail.com with ESMTPSA id v10sm25196949pfg.11.2019.11.19.08.08.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2019 08:08:27 -0800 (PST) From: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= To: netdev@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , bpf@vger.kernel.org, magnus.karlsson@gmail.com, magnus.karlsson@intel.com, jonathan.lemon@gmail.com, ecree@solarflare.com, thoiland@redhat.com, andrii.nakryiko@gmail.com Subject: [PATCH bpf-next 3/3] i40e: start using xdp_call.h Date: Tue, 19 Nov 2019 17:07:57 +0100 Message-Id: <20191119160757.27714-4-bjorn.topel@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191119160757.27714-1-bjorn.topel@gmail.com> References: <20191119160757.27714-1-bjorn.topel@gmail.com> MIME-Version: 1.0 Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org From: Björn Töpel This commit starts using xdp_call.h and the BPF dispatcher to avoid the retpoline overhead. Signed-off-by: Björn Töpel --- drivers/net/ethernet/intel/i40e/i40e_main.c | 5 +++++ drivers/net/ethernet/intel/i40e/i40e_txrx.c | 5 ++++- drivers/net/ethernet/intel/i40e/i40e_xsk.c | 5 ++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/net/ethernet/intel/i40e/i40e_main.c b/drivers/net/ethernet/intel/i40e/i40e_main.c index b3d7edbb1389..59b530e4198f 100644 --- a/drivers/net/ethernet/intel/i40e/i40e_main.c +++ b/drivers/net/ethernet/intel/i40e/i40e_main.c @@ -5,6 +5,7 @@ #include #include #include +#include /* Local includes */ #include "i40e.h" @@ -12517,6 +12518,8 @@ static netdev_features_t i40e_features_check(struct sk_buff *skb, return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); } +DEFINE_XDP_CALL(i40e_xdp_call); + /** * i40e_xdp_setup - add/remove an XDP program * @vsi: VSI to changed @@ -12552,6 +12555,8 @@ static int i40e_xdp_setup(struct i40e_vsi *vsi, for (i = 0; i < vsi->num_queue_pairs; i++) WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog); + xdp_call_update(i40e_xdp_call, old_prog, prog); + if (old_prog) bpf_prog_put(old_prog); diff --git a/drivers/net/ethernet/intel/i40e/i40e_txrx.c b/drivers/net/ethernet/intel/i40e/i40e_txrx.c index b8496037ef7f..34d7b15897a1 100644 --- a/drivers/net/ethernet/intel/i40e/i40e_txrx.c +++ b/drivers/net/ethernet/intel/i40e/i40e_txrx.c @@ -3,6 +3,7 @@ #include #include +#include #include #include "i40e.h" #include "i40e_trace.h" @@ -2188,6 +2189,8 @@ int i40e_xmit_xdp_tx_ring(struct xdp_buff *xdp, struct i40e_ring *xdp_ring) return i40e_xmit_xdp_ring(xdpf, xdp_ring); } +DECLARE_XDP_CALL(i40e_xdp_call); + /** * i40e_run_xdp - run an XDP program * @rx_ring: Rx ring being processed @@ -2209,7 +2212,7 @@ static struct sk_buff *i40e_run_xdp(struct i40e_ring *rx_ring, prefetchw(xdp->data_hard_start); /* xdp_frame write */ - act = bpf_prog_run_xdp(xdp_prog, xdp); + act = xdp_call_run(i40e_xdp_call, xdp_prog, xdp); switch (act) { case XDP_PASS: break; diff --git a/drivers/net/ethernet/intel/i40e/i40e_xsk.c b/drivers/net/ethernet/intel/i40e/i40e_xsk.c index a05dfecdd9b4..c623eeaeb625 100644 --- a/drivers/net/ethernet/intel/i40e/i40e_xsk.c +++ b/drivers/net/ethernet/intel/i40e/i40e_xsk.c @@ -2,6 +2,7 @@ /* Copyright(c) 2018 Intel Corporation. */ #include +#include #include #include @@ -179,6 +180,8 @@ int i40e_xsk_umem_setup(struct i40e_vsi *vsi, struct xdp_umem *umem, i40e_xsk_umem_disable(vsi, qid); } +DECLARE_XDP_CALL(i40e_xdp_call); + /** * i40e_run_xdp_zc - Executes an XDP program on an xdp_buff * @rx_ring: Rx ring @@ -202,7 +205,7 @@ static int i40e_run_xdp_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp) * this path is enabled by setting an XDP program. */ xdp_prog = READ_ONCE(rx_ring->xdp_prog); - act = bpf_prog_run_xdp(xdp_prog, xdp); + act = xdp_call_run(i40e_xdp_call, xdp_prog, xdp); offset = xdp->data - xdp->data_hard_start; xdp->handle = xsk_umem_adjust_offset(umem, xdp->handle, offset);