From patchwork Thu Oct 14 14:34:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12558679 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 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A3F6C433FE for ; Thu, 14 Oct 2021 14:34:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 40F4160EE5 for ; Thu, 14 Oct 2021 14:34:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231134AbhJNOhC (ORCPT ); Thu, 14 Oct 2021 10:37:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231161AbhJNOhC (ORCPT ); Thu, 14 Oct 2021 10:37:02 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27C27C061755 for ; Thu, 14 Oct 2021 07:34:57 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id o20so20241336wro.3 for ; Thu, 14 Oct 2021 07:34:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NTV4GrMZbQTMVi4ELZg/UB6lBvL9dvLiF7pLXEVrNQA=; b=uKa32hFQaL188b8LnVZh9L2Tl0wBeyCn2bG1gV/rEODifI+2BZjiJKVsrmRvZ6z8ZF J2bdp9BzBgfX1FsXP2R3VSiqv96uzdXrBvsS7BbsTkvuL85sEWPUTmY2PZRLOk1fom3y Xc7eyuDrl+xgEfT90Z8GlQP2MKzcsboVENae4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NTV4GrMZbQTMVi4ELZg/UB6lBvL9dvLiF7pLXEVrNQA=; b=WgfGz2QyQjqucBrb/2hw0C0yVjcsGAe/VWkDK4FtBOVdrivg+yJ4JFMzFQbfu+eSNh dc4Q6fqyV0Vds6qxrnU/qzwSz08ucw3qhkMOxNCCvHzZyc3Mz6n1nSe6aBV1//d5oltm 7fdE2Lg5Z+If7JR0lwSdgW7+WiRiSPBGJEjPM/7PCqfsg8HE6ZInaFIsmRQj5ct8CTLN OdLXveTs+D9qxHLxCD+TU9fLbW2Du3HfuRj329uNLs2hNgtSNyEkBmBuirq7aI5ziUJM VTXemH8FXwnr/94WYPbObcM5xUfHpAA+yqNWHx05TLmXzPyGePXnmboi88q80oRiKuyR 24Jg== X-Gm-Message-State: AOAM532tO3ifNMy5sFvfEMTM11fStrgso0TTG5mLs2u6jPrx2LEdxMPE FqkZfMuszOidpwxPAK2IuF6F+w== X-Google-Smtp-Source: ABdhPJzIY3nj4/W/Q0gHrO8qV16gYaDLnYb+kAvaUISazEVX7XFyHDyEfXOkd3uoAgW7qbMXSYnbWA== X-Received: by 2002:adf:ab46:: with SMTP id r6mr7046542wrc.71.1634222095281; Thu, 14 Oct 2021 07:34:55 -0700 (PDT) Received: from antares.. (4.4.a.7.5.8.b.d.d.b.6.7.4.d.a.6.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:6ad4:76bd:db85:7a44]) by smtp.gmail.com with ESMTPSA id k6sm2656439wri.83.2021.10.14.07.34.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Oct 2021 07:34:55 -0700 (PDT) From: Lorenz Bauer To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [RFC 1/9] bpf: name enums used from userspace Date: Thu, 14 Oct 2021 15:34:25 +0100 Message-Id: <20211014143436.54470-2-lmb@cloudflare.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211014143436.54470-1-lmb@cloudflare.com> References: <20211014143436.54470-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC --- include/uapi/linux/bpf.h | 58 +++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 6fc59d61937a..78b532d28761 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -50,7 +50,7 @@ #define BPF_CMPXCHG (0xf0 | BPF_FETCH) /* atomic compare-and-write */ /* Register numbers */ -enum { +enum bpf_reg { BPF_REG_0 = 0, BPF_REG_1, BPF_REG_2, @@ -1056,16 +1056,19 @@ enum bpf_link_type { * All eligible programs are executed regardless of return code from * earlier programs. */ -#define BPF_F_ALLOW_OVERRIDE (1U << 0) -#define BPF_F_ALLOW_MULTI (1U << 1) -#define BPF_F_REPLACE (1U << 2) +enum bpf_prog_attach_flag { + BPF_F_ALLOW_OVERRIDE = (1U << 0), + BPF_F_ALLOW_MULTI = (1U << 1), + BPF_F_REPLACE = (1U << 2), +}; +enum bpf_prog_load_flag { /* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the * verifier will perform strict alignment checking as if the kernel * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set, * and NET_IP_ALIGN defined to 2. */ -#define BPF_F_STRICT_ALIGNMENT (1U << 0) + BPF_F_STRICT_ALIGNMENT = (1U << 0), /* If BPF_F_ANY_ALIGNMENT is used in BPF_PROF_LOAD command, the * verifier will allow any alignment whatsoever. On platforms @@ -1079,7 +1082,7 @@ enum bpf_link_type { * of an unaligned access the alignment check would trigger before * the one we are interested in. */ -#define BPF_F_ANY_ALIGNMENT (1U << 1) + BPF_F_ANY_ALIGNMENT = (1U << 1), /* BPF_F_TEST_RND_HI32 is used in BPF_PROG_LOAD command for testing purpose. * Verifier does sub-register def/use analysis and identifies instructions whose @@ -1097,10 +1100,10 @@ enum bpf_link_type { * Then, if verifier is not doing correct analysis, such randomization will * regress tests to expose bugs. */ -#define BPF_F_TEST_RND_HI32 (1U << 2) + BPF_F_TEST_RND_HI32 = (1U << 2), /* The verifier internal test flag. Behavior is undefined */ -#define BPF_F_TEST_STATE_FREQ (1U << 3) + BPF_F_TEST_STATE_FREQ = (1U << 3), /* If BPF_F_SLEEPABLE is used in BPF_PROG_LOAD command, the verifier will * restrict map and helper usage for such programs. Sleepable BPF programs can @@ -1108,8 +1111,10 @@ enum bpf_link_type { * Such programs are allowed to use helpers that may sleep like * bpf_copy_from_user(). */ -#define BPF_F_SLEEPABLE (1U << 4) + BPF_F_SLEEPABLE = (1U << 4), +}; +enum bpf_pseudo_src_reg { /* When BPF ldimm64's insn[0].src_reg != 0 then this can have * the following extensions: * @@ -1121,8 +1126,8 @@ enum bpf_link_type { * ldimm64 rewrite: address of map * verifier type: CONST_PTR_TO_MAP */ -#define BPF_PSEUDO_MAP_FD 1 -#define BPF_PSEUDO_MAP_IDX 5 + BPF_PSEUDO_MAP_FD = 1, + BPF_PSEUDO_MAP_IDX = 5, /* insn[0].src_reg: BPF_PSEUDO_MAP_[IDX_]VALUE * insn[0].imm: map fd or fd_idx @@ -1132,8 +1137,8 @@ enum bpf_link_type { * ldimm64 rewrite: address of map[0]+offset * verifier type: PTR_TO_MAP_VALUE */ -#define BPF_PSEUDO_MAP_VALUE 2 -#define BPF_PSEUDO_MAP_IDX_VALUE 6 + BPF_PSEUDO_MAP_VALUE = 2, + BPF_PSEUDO_MAP_IDX_VALUE = 6, /* insn[0].src_reg: BPF_PSEUDO_BTF_ID * insn[0].imm: kernel btd id of VAR @@ -1144,7 +1149,7 @@ enum bpf_link_type { * verifier type: PTR_TO_BTF_ID or PTR_TO_MEM, depending on whether the var * is struct/union. */ -#define BPF_PSEUDO_BTF_ID 3 + BPF_PSEUDO_BTF_ID = 3, /* insn[0].src_reg: BPF_PSEUDO_FUNC * insn[0].imm: insn offset to the func * insn[1].imm: 0 @@ -1153,19 +1158,20 @@ enum bpf_link_type { * ldimm64 rewrite: address of the function * verifier type: PTR_TO_FUNC. */ -#define BPF_PSEUDO_FUNC 4 + BPF_PSEUDO_FUNC = 4, /* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative * offset to another bpf function */ -#define BPF_PSEUDO_CALL 1 + BPF_PSEUDO_CALL = 1, /* when bpf_call->src_reg == BPF_PSEUDO_KFUNC_CALL, * bpf_call->imm == btf_id of a BTF_KIND_FUNC in the running kernel */ -#define BPF_PSEUDO_KFUNC_CALL 2 + BPF_PSEUDO_KFUNC_CALL = 2, +}; /* flags for BPF_MAP_UPDATE_ELEM command */ -enum { +enum bpf_map_update_elem_flag { BPF_ANY = 0, /* create new element or update existing */ BPF_NOEXIST = 1, /* create new element if it didn't exist */ BPF_EXIST = 2, /* update existing element */ @@ -1173,7 +1179,7 @@ enum { }; /* flags for BPF_MAP_CREATE command */ -enum { +enum bpf_map_create_flag { BPF_F_NO_PREALLOC = (1U << 0), /* Instead of having one common LRU list in the * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list @@ -1213,17 +1219,19 @@ enum { }; /* Flags for BPF_PROG_QUERY. */ - +enum bpf_prog_query_flag { /* Query effective (directly attached + inherited from ancestor cgroups) * programs that will be executed for events within a cgroup. * attach_flags with this flag are returned only for directly attached programs. */ -#define BPF_F_QUERY_EFFECTIVE (1U << 0) - -/* Flags for BPF_PROG_TEST_RUN */ + BPF_F_QUERY_EFFECTIVE = (1U << 0), +}; +/* Flags for BPF_PROG_RUN */ +enum bpf_prog_run_flag { /* If set, run the test on the cpu specified by bpf_attr.test.cpu */ -#define BPF_F_TEST_RUN_ON_CPU (1U << 0) + BPF_F_TEST_RUN_ON_CPU = (1U << 0), +}; /* type for BPF_ENABLE_STATS */ enum bpf_stats_type { @@ -5230,7 +5238,7 @@ enum { }; /* BPF ring buffer constants */ -enum { +enum bpf_ringbuf_const { BPF_RINGBUF_BUSY_BIT = (1U << 31), BPF_RINGBUF_DISCARD_BIT = (1U << 30), BPF_RINGBUF_HDR_SZ = 8, From patchwork Thu Oct 14 14:34:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12558681 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 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D986BC433EF for ; Thu, 14 Oct 2021 14:34:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B933F61163 for ; Thu, 14 Oct 2021 14:34:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231161AbhJNOhD (ORCPT ); Thu, 14 Oct 2021 10:37:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229994AbhJNOhC (ORCPT ); Thu, 14 Oct 2021 10:37:02 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91889C061570 for ; Thu, 14 Oct 2021 07:34:57 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id v17so20137589wrv.9 for ; Thu, 14 Oct 2021 07:34:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JB5Q5wNmb2hTXra88g0D3xIBP+iOpoIEWT0euuqnYlc=; b=UIM+Q0sLteuLNVadXphlCReMI9ZVnXbsjW+I7j4dkAub+Hju314RDaE9OD7VYgyPYX 0W0a535iWYhYU0Ya2vyCpXZqa5iBbSjQ73XPqzpHGT431qjc7B9PGkAjrh00IzhaUdJL yWwJPOfmRqQ/tsrxViSGw/Fc6p4ZahMQtJAsQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=JB5Q5wNmb2hTXra88g0D3xIBP+iOpoIEWT0euuqnYlc=; b=VwEYA8QtES1bfdJ/OVGCeyDWypsY3nKOX9v4cWFFXgooBAxdsYkaGMdlJFFzem0S+t 07XrKtq9oUp/UFvY8gEFFhvaWKAAQHMMyyO46OYuPx+KuWzHFSF6Plh50LiiGu7PmgTj +jEloqILt5NR6FwMW8ZWVR0bshrH7U94+1xRZw4UzLUuP/gqCdN8d3X3ftsUw6rFH3l2 JEAdWQiKQOu3UZrwwde6LUuYS54wJxOyVBU7YKlgD53httwLS0qEDqEYinTIovZcDkbO RMTkVNwfsjkBvdU7tGce6Aa9KNpa5wrXf0xTo2/3tnTfkQWyRkRJRSDFd7aIpA7+5oI2 +CaQ== X-Gm-Message-State: AOAM531AOIUQOqJpYlGjuM/tHyol1wGtGBobNBfzKQ0WSlCBxWb1lPHF Jq59kNlsLdehWxV6W8Sbp09dcOowQ+lDbg== X-Google-Smtp-Source: ABdhPJzw9bVs/HhOzdZQrDfMjY66uvWk1K4TTi/KplTQrwTxbeoJ6fxcuimWF2NlFR7SJ6CNqzCevg== X-Received: by 2002:a7b:c386:: with SMTP id s6mr20191902wmj.183.1634222095881; Thu, 14 Oct 2021 07:34:55 -0700 (PDT) Received: from antares.. (4.4.a.7.5.8.b.d.d.b.6.7.4.d.a.6.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:6ad4:76bd:db85:7a44]) by smtp.gmail.com with ESMTPSA id k6sm2656439wri.83.2021.10.14.07.34.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Oct 2021 07:34:55 -0700 (PDT) From: Lorenz Bauer To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [RFC 2/9] bpf: various constants Date: Thu, 14 Oct 2021 15:34:26 +0100 Message-Id: <20211014143436.54470-3-lmb@cloudflare.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211014143436.54470-1-lmb@cloudflare.com> References: <20211014143436.54470-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC --- include/uapi/linux/bpf.h | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 78b532d28761..211b9d902006 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1248,7 +1248,10 @@ enum bpf_stack_build_id_status { BPF_STACK_BUILD_ID_IP = 2, }; -#define BPF_BUILD_ID_SIZE 20 +enum { + BPF_BUILD_ID_SIZE = 20, +}; + struct bpf_stack_build_id { __s32 status; unsigned char build_id[BPF_BUILD_ID_SIZE]; @@ -1258,7 +1261,9 @@ struct bpf_stack_build_id { }; }; -#define BPF_OBJ_NAME_LEN 16U +enum { + BPF_OBJ_NAME_LEN = 16U, +}; union bpf_attr { struct { /* anonymous struct used by BPF_MAP_CREATE command */ @@ -5464,7 +5469,9 @@ struct bpf_xdp_sock { __u32 queue_id; }; -#define XDP_PACKET_HEADROOM 256 +enum { + XDP_PACKET_HEADROOM = 256, +}; /* User return codes for XDP prog type. * A valid XDP program must return one of these defined values. All other @@ -5582,7 +5589,9 @@ struct sk_reuseport_md { __bpf_md_ptr(struct bpf_sock *, migrating_sk); }; -#define BPF_TAG_SIZE 8 +enum { + BPF_TAG_SIZE = 8, +}; struct bpf_prog_info { __u32 type; From patchwork Thu Oct 14 14:34:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12558683 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 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0FA6DC4332F for ; Thu, 14 Oct 2021 14:35:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E169E61163 for ; Thu, 14 Oct 2021 14:34:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229994AbhJNOhD (ORCPT ); Thu, 14 Oct 2021 10:37:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231245AbhJNOhD (ORCPT ); Thu, 14 Oct 2021 10:37:03 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A29CC061753 for ; Thu, 14 Oct 2021 07:34:58 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id v17so20137757wrv.9 for ; Thu, 14 Oct 2021 07:34:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vxuhbka69vrglXWqMLAHUMctSCrOk9pl2oGisWfG9dE=; b=kAtH8Y4626311OtVPgztu2m2i4C5rxeXbSZ0+rKfn30ppC9cpjPdupqla5ghzJzkYT KN0O4r0ULf7LCu10e3fEU+sidFm8LZrPi/QjE/ED8RCqwH6f3u1nspUqlzu0AqUf3f+1 yN58rBU94nL20g+rdPVm3sKxMxaUYTHcqIWRM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vxuhbka69vrglXWqMLAHUMctSCrOk9pl2oGisWfG9dE=; b=TiIy8q5VbjbDxNoI69G+cxppMGUv9pOzbGsqWyZThTs3WFpUgCXbVK3hUctqPz80TS zu3xPqE5LhVBIxuK7olSOa/o7aMLm3+xsnec1IdMpdJsRuFV5L9jWKjDquJt++ggvQYV 96s/uiIOfIlPN8x6IqQ6wcuTjr9H4yRiltJe2qYCNlAmD7Ja9b7+zhFz5Ab0Uc568TMn 0o5N5vamev8IErvDa8f26xnKZKrPjviqq0UUmE+aqAYPBi03YY5nP5af6vjaFjsqBKLb v1nSNXmY5vOMXDd0JFDGZyaKVPm+vjqZ/2u5SZ28vL50zBje6fQhrk6Fh6q3Ta5xyRYq 4fTA== X-Gm-Message-State: AOAM530YzwD3HpzOFzOl2njxZG4c7wRDhF7kbQFH7XpqvuhO/RWONqWf mGttY2LmFDbVqqx7SbJywdHvdw== X-Google-Smtp-Source: ABdhPJw2PycVz6I8/hpvZMZGZYX0zwTyQM/VDoCth311twhMwHc5QeX46/4w5En4xEAzu6iWUv9BQg== X-Received: by 2002:adf:a31d:: with SMTP id c29mr6997189wrb.381.1634222096957; Thu, 14 Oct 2021 07:34:56 -0700 (PDT) Received: from antares.. (4.4.a.7.5.8.b.d.d.b.6.7.4.d.a.6.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:6ad4:76bd:db85:7a44]) by smtp.gmail.com with ESMTPSA id k6sm2656439wri.83.2021.10.14.07.34.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Oct 2021 07:34:56 -0700 (PDT) From: Lorenz Bauer To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [RFC 3/9] bpf: move up __bpf_md_ptr Date: Thu, 14 Oct 2021 15:34:27 +0100 Message-Id: <20211014143436.54470-4-lmb@cloudflare.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211014143436.54470-1-lmb@cloudflare.com> References: <20211014143436.54470-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC --- include/uapi/linux/bpf.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 211b9d902006..dec062fa0604 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -49,6 +49,12 @@ #define BPF_XCHG (0xe0 | BPF_FETCH) /* atomic exchange */ #define BPF_CMPXCHG (0xf0 | BPF_FETCH) /* atomic compare-and-write */ +#define __bpf_md_ptr(type, name) \ +union { \ + type name; \ + __u64 :64; \ +} __attribute__((aligned(8))) + /* Register numbers */ enum bpf_reg { BPF_REG_0 = 0, @@ -5285,12 +5291,6 @@ enum { BPF_F_EXCLUDE_INGRESS = (1ULL << 4), }; -#define __bpf_md_ptr(type, name) \ -union { \ - type name; \ - __u64 :64; \ -} __attribute__((aligned(8))) - /* user accessible mirror of in-kernel sk_buff. * new fields can only be added to the end of this structure */ From patchwork Thu Oct 14 14:34:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12558685 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 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C4866C43217 for ; Thu, 14 Oct 2021 14:35:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A8A8261156 for ; Thu, 14 Oct 2021 14:35:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231245AbhJNOhE (ORCPT ); Thu, 14 Oct 2021 10:37:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231199AbhJNOhE (ORCPT ); Thu, 14 Oct 2021 10:37:04 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53497C061570 for ; Thu, 14 Oct 2021 07:34:59 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id k7so20043177wrd.13 for ; Thu, 14 Oct 2021 07:34:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tQabridDJcxeHhJlqAL6rkEjPMj/u95x0d5LXwe3wWg=; b=lROIab4YS6lecpzl7WZjBMphUPNLCcsv67XtZ7/3+tgIf+GxqQuZrECRAfuRuWODJo 9/psa47rYCCi92Z3QOLOhRntcEvZUvuFtXrP7M3bMi/Kdtp/ZjmbLEFs30nQuDRVAEj5 w3Mb8bXc7zTMEXc1etErYxGcNinGokvr7jJTU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tQabridDJcxeHhJlqAL6rkEjPMj/u95x0d5LXwe3wWg=; b=YQhdieczBAq4kGbXErudMZOn6ac8xS/h+P4MQPtdS94C5Gb+ZIhcZRluy+kvOz2HV1 3MVwbOF8xGou5HVPsOi1K7XEtPguyX9PNvET9QfsI7t7Fk2V0ep5y+IbSx2EPVjptGxY EDBwJ7O45VYEMMA3dWKkdhrKKCGKGleSpDxr163qbrjktBQusqqaiphq3ieVIntT0+dn tIQobfcNUwGYBteK/N+WTkIsD/Ycng5XbrIM8hNNFJ0NhqQdNbsPT7bj9hzt++lZvtjp 4FSNUHnSP7Jm+HRB2pL4vIMnfvnrH5qp8a4f+7KY/pGwl28NyVAZpWYlctPFLCNdFYY6 3cGA== X-Gm-Message-State: AOAM530TD6dRrAxg/50ARBAp4QGJj0r+AicBkg4N4JKHg+19if2PThmC iJ5psOj6vaKnwwCb2Ao3ZKivaOqPgugbCQ== X-Google-Smtp-Source: ABdhPJwrpfIUUgMYilMmO3RYkKsBoI7L2X1VoqqI5KiqCweQ7NPUzvHq8G27oUgFYoCPtku8TLxcRA== X-Received: by 2002:a1c:a747:: with SMTP id q68mr20205127wme.139.1634222097786; Thu, 14 Oct 2021 07:34:57 -0700 (PDT) Received: from antares.. (4.4.a.7.5.8.b.d.d.b.6.7.4.d.a.6.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:6ad4:76bd:db85:7a44]) by smtp.gmail.com with ESMTPSA id k6sm2656439wri.83.2021.10.14.07.34.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Oct 2021 07:34:57 -0700 (PDT) From: Lorenz Bauer To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [RFC 4/9] bpf: name __u64 member of __bpf_md_ptr Date: Thu, 14 Oct 2021 15:34:28 +0100 Message-Id: <20211014143436.54470-5-lmb@cloudflare.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211014143436.54470-1-lmb@cloudflare.com> References: <20211014143436.54470-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC --- include/uapi/linux/bpf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index dec062fa0604..c1b1ce0e26a6 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -52,7 +52,7 @@ #define __bpf_md_ptr(type, name) \ union { \ type name; \ - __u64 :64; \ + __u64 name##_u64; \ } __attribute__((aligned(8))) /* Register numbers */ From patchwork Thu Oct 14 14:34:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12558687 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 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 740C2C433FE for ; Thu, 14 Oct 2021 14:35:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5A75461181 for ; Thu, 14 Oct 2021 14:35:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231255AbhJNOhF (ORCPT ); Thu, 14 Oct 2021 10:37:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231199AbhJNOhF (ORCPT ); Thu, 14 Oct 2021 10:37:05 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A57EC061570 for ; Thu, 14 Oct 2021 07:35:00 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id k7so20043305wrd.13 for ; Thu, 14 Oct 2021 07:35:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=BaOtCYQh3cbTMyIcLbI8dZ1B4xBleHXT37uXDX2iTiI=; b=tx0XH3+r46t3Y8dyml5p0EBHiZnyslFSKj8H/uG+hZmNW+I1v/+MPbydZalyvrj7rF qdoZEjgcE8i5NrZjrGpWnaH+ABuQWL0EUOMdJ0q/KUpk7v8oFhlse0b6y9xhV06bdR/w 7lEfyPyrc9OS5AdnQxg5BCDxOweApIVSDUIBs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BaOtCYQh3cbTMyIcLbI8dZ1B4xBleHXT37uXDX2iTiI=; b=mPrZAj9zwLH3QeqY5+2Iyii8Ke8IsVBbbsmP/3ByNiEt82fYK6rXnxLqMLtH0dzRlw s7R3JfCAyIvFLTu84SS2xtLErncEAopkJFuvkiNcTeSGDzO+sC19BYVaz6Lyh8XHJJEV fHtn0KtO8Ee36+nMWx5n85SZME3rLX5qD3+rMvzbgIxvvg7gtR4GwcVKKr2eOldSmRWz YewOKil5SF8WEtCH6u0sK4hEuOvemBljWNFlN7LSboGAUVSTB4KsfLjja35lRUiHQtez 6SCr6QvHPMnhe/QgBhtwdOm0Ctc6SwQTS5iHYlVBX5q4UKDOK6j3Z5OKqcAeOIkD2GRd /gIg== X-Gm-Message-State: AOAM530zEf8l/9ghV/9a3AdwxpHIgOqSDufxihQx/oZp4O/iIpQHuL97 cpMwP+5CYDVS5Bp1NZYqgx4SxttRgfM= X-Google-Smtp-Source: ABdhPJyLhtBcUhctyAARUBl3LNeEaQwyGnsBVRSLB1xMQ6DxiwyrYvNnVpmfDUF3BemfhMkT9rxkpw== X-Received: by 2002:a1c:a9d5:: with SMTP id s204mr6165708wme.193.1634222098484; Thu, 14 Oct 2021 07:34:58 -0700 (PDT) Received: from antares.. (4.4.a.7.5.8.b.d.d.b.6.7.4.d.a.6.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:6ad4:76bd:db85:7a44]) by smtp.gmail.com with ESMTPSA id k6sm2656439wri.83.2021.10.14.07.34.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Oct 2021 07:34:58 -0700 (PDT) From: Lorenz Bauer To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [RFC 5/9] bpf: enum bpf_map_create_attr Date: Thu, 14 Oct 2021 15:34:29 +0100 Message-Id: <20211014143436.54470-6-lmb@cloudflare.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211014143436.54470-1-lmb@cloudflare.com> References: <20211014143436.54470-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC --- include/linux/bpf.h | 4 +-- include/uapi/linux/bpf.h | 62 ++++++++++++++++++++++++++-------------- kernel/bpf/syscall.c | 27 +++++++++++------ 3 files changed, 60 insertions(+), 33 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index b6c45a6cbbba..80791db7945a 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1613,8 +1613,8 @@ int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu, /* Return map's numa specified by userspace */ static inline int bpf_map_attr_numa_node(const union bpf_attr *attr) { - return (attr->map_flags & BPF_F_NUMA_NODE) ? - attr->numa_node : NUMA_NO_NODE; + return (attr->map_create.map_flags & BPF_F_NUMA_NODE) ? + attr->map_create.numa_node : NUMA_NO_NODE; } struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type); diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index c1b1ce0e26a6..f1c163778d7a 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1271,29 +1271,31 @@ enum { BPF_OBJ_NAME_LEN = 16U, }; +struct bpf_map_create_attr { + __u32 map_type; /* one of enum bpf_map_type */ + __u32 key_size; /* size of key in bytes */ + __u32 value_size; /* size of value in bytes */ + __u32 max_entries; /* max number of entries in a map */ + __u32 map_flags; /* BPF_MAP_CREATE related + * flags defined above. + */ + __u32 inner_map_fd; /* fd pointing to the inner map */ + __u32 numa_node; /* numa node (effective only if + * BPF_F_NUMA_NODE is set). + */ + char map_name[BPF_OBJ_NAME_LEN]; + __u32 map_ifindex; /* ifindex of netdev to create on */ + __u32 btf_fd; /* fd pointing to a BTF type data */ + __u32 btf_key_type_id; /* BTF type_id of the key */ + __u32 btf_value_type_id; /* BTF type_id of the value */ + __u32 btf_vmlinux_value_type_id; /* BTF type_id of a kernel- + * struct stored as the + * map value + */ +}; + union bpf_attr { - struct { /* anonymous struct used by BPF_MAP_CREATE command */ - __u32 map_type; /* one of enum bpf_map_type */ - __u32 key_size; /* size of key in bytes */ - __u32 value_size; /* size of value in bytes */ - __u32 max_entries; /* max number of entries in a map */ - __u32 map_flags; /* BPF_MAP_CREATE related - * flags defined above. - */ - __u32 inner_map_fd; /* fd pointing to the inner map */ - __u32 numa_node; /* numa node (effective only if - * BPF_F_NUMA_NODE is set). - */ - char map_name[BPF_OBJ_NAME_LEN]; - __u32 map_ifindex; /* ifindex of netdev to create on */ - __u32 btf_fd; /* fd pointing to a BTF type data */ - __u32 btf_key_type_id; /* BTF type_id of the key */ - __u32 btf_value_type_id; /* BTF type_id of the value */ - __u32 btf_vmlinux_value_type_id;/* BTF type_id of a kernel- - * struct stored as the - * map value - */ - }; + struct bpf_map_create_attr map_create; struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */ __u32 map_fd; @@ -1506,6 +1508,22 @@ union bpf_attr { __u32 flags; /* extra flags */ } prog_bind_map; + /* DEPRECATED: these are kept for compatibility purposes. */ + struct { /* anonymous struct used by BPF_MAP_CREATE command */ + __u32 map_type; + __u32 key_size; + __u32 value_size; + __u32 max_entries; + __u32 map_flags; + __u32 inner_map_fd; + __u32 numa_node; + char map_name[BPF_OBJ_NAME_LEN]; + __u32 map_ifindex; + __u32 btf_fd; + __u32 btf_key_type_id; + __u32 btf_value_type_id; + __u32 btf_vmlinux_value_type_id; + }; } __attribute__((aligned(8))); /* The description below is an attempt at providing documentation to eBPF diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 4e50c0bfdb7d..f7b57877acd2 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -103,7 +103,7 @@ const struct bpf_map_ops bpf_map_offload_ops = { .map_check_btf = map_check_no_btf, }; -static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) +static struct bpf_map *find_and_alloc_map(struct bpf_map_create_attr *attr) { const struct bpf_map_ops *ops; u32 type = attr->map_type; @@ -118,13 +118,13 @@ static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) return ERR_PTR(-EINVAL); if (ops->map_alloc_check) { - err = ops->map_alloc_check(attr); + err = ops->map_alloc_check((union bpf_attr *)attr); /* XXX: Dodgy cast */ if (err) return ERR_PTR(err); } if (attr->map_ifindex) ops = &bpf_map_offload_ops; - map = ops->map_alloc(attr); + map = ops->map_alloc((union bpf_attr *)attr); /* XXX: Dodgy cast */ if (IS_ERR(map)) return map; map->ops = ops; @@ -719,6 +719,15 @@ int bpf_get_file_flag(int flags) offsetof(union bpf_attr, CMD##_LAST_FIELD) - \ sizeof(attr->CMD##_LAST_FIELD)) != NULL +/* helper macro to extract a field from union bpf_attr while checking that the tail is zero. */ +#define ATTR_FIELD(attr, field) ({ \ + typeof(&((attr)->field)) __tmp = &((attr)->field); \ + if (memchr_inv((void *)__tmp + sizeof((attr)->field), 0, sizeof(*(attr)) - sizeof((attr)->field))) { \ + __tmp = NULL; \ + } \ + __tmp; \ + }) + /* dst and src must have at least "size" number of bytes. * Return strlen on success and < 0 on error. */ @@ -810,19 +819,19 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf, return ret; } -#define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id /* called via syscall */ -static int map_create(union bpf_attr *attr) +static int map_create(struct bpf_map_create_attr *attr) { - int numa_node = bpf_map_attr_numa_node(attr); + int numa_node; struct bpf_map *map; int f_flags; int err; - err = CHECK_ATTR(BPF_MAP_CREATE); - if (err) + if (!attr) return -EINVAL; + numa_node = bpf_map_attr_numa_node((union bpf_attr *)attr); /* Dodgy cast */ + if (attr->btf_vmlinux_value_type_id) { if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS || attr->btf_key_type_id || attr->btf_value_type_id) @@ -4566,7 +4575,7 @@ static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size) switch (cmd) { case BPF_MAP_CREATE: - err = map_create(&attr); + err = map_create(ATTR_FIELD(&attr, map_create)); break; case BPF_MAP_LOOKUP_ELEM: err = map_lookup_elem(&attr); From patchwork Thu Oct 14 14:34:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12558691 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 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 64E69C4332F for ; Thu, 14 Oct 2021 14:35:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4BCD1611AE for ; Thu, 14 Oct 2021 14:35:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231199AbhJNOhH (ORCPT ); Thu, 14 Oct 2021 10:37:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231281AbhJNOhG (ORCPT ); Thu, 14 Oct 2021 10:37:06 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BC11C061570 for ; Thu, 14 Oct 2021 07:35:01 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id r10so20085394wra.12 for ; Thu, 14 Oct 2021 07:35:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yUU1GvrRd41KtND7k0YFbBqZ4IbM7SfwZfPHgulYfvc=; b=bP1MVtAe0NbNumsdh1wyOFUTmaodnUnncipuqpLyKwuIxaxP7gTgaNqBD2X9sk3R2+ qg4ZCYRJvJF7xOhUJbP31t86pn6p7/HIu3FpvgN2lvWDbZudUBPetffZE/nGn639SeFk uruo+EsG4IP5gZaAJQYViSI+/IQOmOcnq1IA4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yUU1GvrRd41KtND7k0YFbBqZ4IbM7SfwZfPHgulYfvc=; b=08I09YO+q9dCYOTBAQOCh/IK9P7NxjYVJRkBcs2R95UkNfd5mZ9NZjTn73I7FFS5l0 aexHAgl7T8yf9VOGK38pHyYDivbanRVFv91+0s0jSw1v+vTLZuybQ23E8pmN87pGDwGj zPfnPbdaFUNQW1OU78B0S14pZWHH9U6Yy45Vl0N3K5iBZBukhbjXtafBPnNqvzc8ljnD 3UKrJyQPuTxlqFKOgj0MXOTXdRG5x7hs3ZX0LFcobdTKyFKqSjVgMeaeZZUBfqm5QZ6b PUA2FqDEh4xpFISNVv4acaGqjZjdlU6SoqjY41Vl5twmReyThwWSjZDgo0b7dY0VZSK1 k1Bw== X-Gm-Message-State: AOAM533R8+dSl2688aN2C3T91HoKtIsXwSCWZeN04kX97YhDxLOPyGUq EMjAE3xbdxha+tK9O3h5IhsMLA== X-Google-Smtp-Source: ABdhPJxmZ26B7P8I9SosXjcdkZTcRmC/RuOg1SuEfkNJImJrMwhC3IouhR2VgiibuK24vvuCI2eRew== X-Received: by 2002:a5d:63ca:: with SMTP id c10mr7003238wrw.407.1634222099773; Thu, 14 Oct 2021 07:34:59 -0700 (PDT) Received: from antares.. (4.4.a.7.5.8.b.d.d.b.6.7.4.d.a.6.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:6ad4:76bd:db85:7a44]) by smtp.gmail.com with ESMTPSA id k6sm2656439wri.83.2021.10.14.07.34.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Oct 2021 07:34:59 -0700 (PDT) From: Lorenz Bauer To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [RFC 6/9] bpf: split map modification structs Date: Thu, 14 Oct 2021 15:34:31 +0100 Message-Id: <20211014143436.54470-8-lmb@cloudflare.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211014143436.54470-1-lmb@cloudflare.com> References: <20211014143436.54470-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC --- include/uapi/linux/bpf.h | 51 +++++++++++++++++++++++------ kernel/bpf/syscall.c | 70 ++++++++++++++++------------------------ 2 files changed, 70 insertions(+), 51 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index f1c163778d7a..d3acd12d98c1 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1294,18 +1294,41 @@ struct bpf_map_create_attr { */ }; +struct bpf_map_lookup_elem_attr { + __u32 map_fd; + __bpf_md_ptr(const void *, key); + __bpf_md_ptr(void *, value); + __u64 flags; +}; + +struct bpf_map_update_elem_attr { + __u32 map_fd; + __bpf_md_ptr(const void *, key); + __bpf_md_ptr(void *, value); + __u64 flags; +}; + +struct bpf_map_delete_elem_attr { + __u32 map_fd; + __bpf_md_ptr(const void *, key); +}; + +struct bpf_map_get_next_key_attr { + __u32 map_fd; + __bpf_md_ptr(const void *, key); + __bpf_md_ptr(void *, next_key); +}; + union bpf_attr { struct bpf_map_create_attr map_create; - struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */ - __u32 map_fd; - __aligned_u64 key; - union { - __aligned_u64 value; - __aligned_u64 next_key; - }; - __u64 flags; - }; + struct bpf_map_lookup_elem_attr map_lookup_elem; + + struct bpf_map_update_elem_attr map_update_elem; + + struct bpf_map_delete_elem_attr map_delete_elem; + + struct bpf_map_get_next_key_attr map_get_next_key; struct { /* struct used by BPF_MAP_*_BATCH commands */ __aligned_u64 in_batch; /* start batch, @@ -1524,6 +1547,16 @@ union bpf_attr { __u32 btf_value_type_id; __u32 btf_vmlinux_value_type_id; }; + + struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */ + __u32 map_fd; + __aligned_u64 key; + union { + __aligned_u64 value; + __aligned_u64 next_key; + }; + __u64 flags; + }; } __attribute__((aligned(8))); /* The description below is an attempt at providing documentation to eBPF diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index f7b57877acd2..c4aecdbb390e 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -728,6 +728,11 @@ int bpf_get_file_flag(int flags) __tmp; \ }) +#define CHECK_ATTR_TAIL(attr, field) \ + (memchr_inv((void *)(attr) + offsetofend(typeof(*attr), field), 0, \ + sizeof(*(attr)) - offsetofend(typeof(*attr), field)) != NULL ? -EINVAL : 0) + + /* dst and src must have at least "size" number of bytes. * Return strlen on success and < 0 on error. */ @@ -1041,23 +1046,17 @@ static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size) return NULL; } -/* last field in 'union bpf_attr' used by this command */ -#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags - -static int map_lookup_elem(union bpf_attr *attr) +static int map_lookup_elem(struct bpf_map_lookup_elem_attr *attr) { - void __user *ukey = u64_to_user_ptr(attr->key); - void __user *uvalue = u64_to_user_ptr(attr->value); - int ufd = attr->map_fd; + void __user *ukey = u64_to_user_ptr(attr->key_u64); + void __user *uvalue = u64_to_user_ptr(attr->value_u64); + int ufd = attr->fd; struct bpf_map *map; void *key, *value; u32 value_size; struct fd f; int err; - if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) - return -EINVAL; - if (attr->flags & ~BPF_F_LOCK) return -EINVAL; @@ -1108,23 +1107,17 @@ static int map_lookup_elem(union bpf_attr *attr) return err; } - -#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags - -static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr) +static int map_update_elem(struct bpf_map_update_elem_attr *attr, bpfptr_t uattr) { - bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel); - bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel); - int ufd = attr->map_fd; + bpfptr_t ukey = make_bpfptr(attr->key_u64, uattr.is_kernel); + bpfptr_t uvalue = make_bpfptr(attr->value_u64, uattr.is_kernel); + int ufd = attr->fd; struct bpf_map *map; void *key, *value; u32 value_size; struct fd f; int err; - if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM)) - return -EINVAL; - f = fdget(ufd); map = __bpf_map_get(f); if (IS_ERR(map)) @@ -1168,20 +1161,15 @@ static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr) return err; } -#define BPF_MAP_DELETE_ELEM_LAST_FIELD key - -static int map_delete_elem(union bpf_attr *attr) +static int map_delete_elem(struct bpf_map_delete_elem_attr *attr) { - void __user *ukey = u64_to_user_ptr(attr->key); - int ufd = attr->map_fd; + void __user *ukey = u64_to_user_ptr(attr->key_u64); + int ufd = attr->fd; struct bpf_map *map; struct fd f; void *key; int err; - if (CHECK_ATTR(BPF_MAP_DELETE_ELEM)) - return -EINVAL; - f = fdget(ufd); map = __bpf_map_get(f); if (IS_ERR(map)) @@ -1220,22 +1208,16 @@ static int map_delete_elem(union bpf_attr *attr) return err; } -/* last field in 'union bpf_attr' used by this command */ -#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key - -static int map_get_next_key(union bpf_attr *attr) +static int map_get_next_key(struct bpf_map_get_next_key_attr *attr) { - void __user *ukey = u64_to_user_ptr(attr->key); - void __user *unext_key = u64_to_user_ptr(attr->next_key); - int ufd = attr->map_fd; + void __user *ukey = u64_to_user_ptr(attr->key_u64); + void __user *unext_key = u64_to_user_ptr(attr->next_key_u64); + int ufd = attr->fd; struct bpf_map *map; void *key, *next_key; struct fd f; int err; - if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY)) - return -EINVAL; - f = fdget(ufd); map = __bpf_map_get(f); if (IS_ERR(map)) @@ -4578,16 +4560,20 @@ static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size) err = map_create(ATTR_FIELD(&attr, map_create)); break; case BPF_MAP_LOOKUP_ELEM: - err = map_lookup_elem(&attr); + err = CHECK_ATTR_TAIL(&attr, map_lookup_elem); + err = err ?: map_lookup_elem(&attr.map_lookup_elem); break; case BPF_MAP_UPDATE_ELEM: - err = map_update_elem(&attr, uattr); + err = CHECK_ATTR_TAIL(&attr, map_update_elem); + err = err ?: map_update_elem(&attr.map_update_elem, uattr); break; case BPF_MAP_DELETE_ELEM: - err = map_delete_elem(&attr); + err = CHECK_ATTR_TAIL(&attr, map_delete_elem); + err = err ?: map_delete_elem(&attr.map_delete_elem); break; case BPF_MAP_GET_NEXT_KEY: - err = map_get_next_key(&attr); + err = CHECK_ATTR_TAIL(&attr, map_get_next_key); + err = err ?: map_get_next_key(&attr.map_get_next_key); break; case BPF_MAP_FREEZE: err = map_freeze(&attr); From patchwork Thu Oct 14 14:34:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12558695 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 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6EA40C433F5 for ; Thu, 14 Oct 2021 14:35:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4F72D611B0 for ; Thu, 14 Oct 2021 14:35:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231305AbhJNOhI (ORCPT ); Thu, 14 Oct 2021 10:37:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231281AbhJNOhH (ORCPT ); Thu, 14 Oct 2021 10:37:07 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D140FC061570 for ; Thu, 14 Oct 2021 07:35:02 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id r18so20222592wrg.6 for ; Thu, 14 Oct 2021 07:35:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=BRUjR/ynNSZXKI1Qw/nmqBhG9Te4luaxiOMO8oANsB4=; b=ke8ImMeDAtcPAl6vDL0IzwHA40rSF2x4ssCVMHXFnFnmbENSLw47MK4a4g8QvMKXjF IpFG24JhclwmdM9O5jB/aBejG1igeA33RacC8fHTr3UDazv8oL0D5sAyiOQ7/gaiFBtJ tYkmBUl2tiBw6ftpvOAFWc2YDtjIJLwqNccHI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BRUjR/ynNSZXKI1Qw/nmqBhG9Te4luaxiOMO8oANsB4=; b=iQEYy5BdCxIEgj7bZFtU4iIOD0fQkhkk6AUQFaFZB2cgdGwOKzFzGrxmAEcHEzpS9h VISpdT/LgfrGmo2kOvXPG1stuzxDYQDF1npXv09mWyFhrSvdtdr1mIsr/3nl+sQItlYH bgWTqBKD4X3ngFwQSJuW65+6RXD696RgrU62PCRi2y83aSTquzJ2iqv/qF/R3cKpw8aS wOR2hDgJ4QD7H9ouUbXcWZK4d07YDwYtpctnDokwjjaJiuR7mZJgF51x0LwCz2dlZQCV YH0rojtY+r1Izu5WLkzPIZdwMZ2kRRb8zhIld4oM2a8/3/afvVOdy4+x/j0fuw6iwDzT MHIg== X-Gm-Message-State: AOAM531HeZ1Rvl30bh5f4qFJpq4MFkHXMeIC6kwCG5nc2Q3HevImM32l L0K0sEiP9PZxmY3qhXZqP+4XZA== X-Google-Smtp-Source: ABdhPJxeDnDYVtGi4ro7aSOb6rCa7StO5fjsvfOd5qtgVCmjl5mwZMJB2qY62d23Lq64gdX0S0RGyQ== X-Received: by 2002:a5d:6481:: with SMTP id o1mr7354602wri.60.1634222101456; Thu, 14 Oct 2021 07:35:01 -0700 (PDT) Received: from antares.. (4.4.a.7.5.8.b.d.d.b.6.7.4.d.a.6.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:6ad4:76bd:db85:7a44]) by smtp.gmail.com with ESMTPSA id k6sm2656439wri.83.2021.10.14.07.35.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Oct 2021 07:35:01 -0700 (PDT) From: Lorenz Bauer To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [RFC 7/9] bpf: split get_id and fd_by_id in bpf_attr Date: Thu, 14 Oct 2021 15:34:33 +0100 Message-Id: <20211014143436.54470-10-lmb@cloudflare.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211014143436.54470-1-lmb@cloudflare.com> References: <20211014143436.54470-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC --- include/uapi/linux/bpf.h | 60 ++++++++++++++++++++++++++++++++-------- kernel/bpf/syscall.c | 18 ++++++------ 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index d3acd12d98c1..13d126c201ce 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1422,17 +1422,43 @@ union bpf_attr { __u32 cpu; } test; - struct { /* anonymous struct used by BPF_*_GET_*_ID */ - union { - __u32 start_id; - __u32 prog_id; - __u32 map_id; - __u32 btf_id; - __u32 link_id; - }; - __u32 next_id; - __u32 open_flags; - }; + struct { /* used by BPF_PROG_GET_FD_BY_ID command */ + __u32 id; + } prog_get_fd_by_id; + + struct { /* used by BPF_MAP_GET_FD_BY_ID command */ + __u32 id; + __u32 ingnored; + __u32 open_flags; + } map_get_fd_by_id; + + struct { /* used by BPF_BTF_GET_FD_BY_ID command */ + __u32 id; + } btf_get_fd_by_id; + + struct { /* used by BPF_LINK_GET_FD_BY_ID command */ + __u32 id; + } link_get_fd_by_id; + + struct { /* used by BPF_PROG_GET_NEXT_ID command */ + __u32 start_id; + __u32 next_id; + } prog_get_next_id; + + struct { /* used by BPF_MAP_GET_NEXT_ID command */ + __u32 start_id; + __u32 next_id; + } map_get_next_id; + + struct { /* used by BPF_BTF_GET_NEXT_ID command */ + __u32 start_id; + __u32 next_id; + } btf_get_next_id; + + struct { /* used by BPF_LINK_GET_NEXT_ID command */ + __u32 start_id; + __u32 next_id; + } link_get_next_id; struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */ __u32 bpf_fd; @@ -1557,6 +1583,18 @@ union bpf_attr { }; __u64 flags; }; + + struct { /* anonymous struct used by BPF_*_GET_*_ID */ + union { + __u32 start_id; + __u32 prog_id; + __u32 map_id; + __u32 btf_id; + __u32 link_id; + }; + __u32 next_id; + __u32 open_flags; + }; } __attribute__((aligned(8))); /* The description below is an attempt at providing documentation to eBPF diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index c4aecdbb390e..234860bd05bf 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3367,7 +3367,7 @@ struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id) return prog; } -#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id +#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_get_fd_by_id.id struct bpf_prog *bpf_prog_by_id(u32 id) { @@ -3389,7 +3389,7 @@ struct bpf_prog *bpf_prog_by_id(u32 id) static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) { struct bpf_prog *prog; - u32 id = attr->prog_id; + u32 id = attr->prog_get_fd_by_id.id; int fd; if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID)) @@ -3409,12 +3409,12 @@ static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) return fd; } -#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags +#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD map_get_fd_by_id.open_flags static int bpf_map_get_fd_by_id(const union bpf_attr *attr) { struct bpf_map *map; - u32 id = attr->map_id; + u32 id = attr->map_get_fd_by_id.id; int f_flags; int fd; @@ -3425,7 +3425,7 @@ static int bpf_map_get_fd_by_id(const union bpf_attr *attr) if (!capable(CAP_SYS_ADMIN)) return -EPERM; - f_flags = bpf_get_file_flag(attr->open_flags); + f_flags = bpf_get_file_flag(attr->map_get_fd_by_id.open_flags); if (f_flags < 0) return f_flags; @@ -3984,7 +3984,7 @@ static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr) return btf_new_fd(attr, uattr); } -#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id +#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_get_fd_by_id.id static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) { @@ -3994,7 +3994,7 @@ static int bpf_btf_get_fd_by_id(const union bpf_attr *attr) if (!capable(CAP_SYS_ADMIN)) return -EPERM; - return btf_get_fd_by_id(attr->btf_id); + return btf_get_fd_by_id(attr->btf_get_fd_by_id.id); } static int bpf_task_fd_query_copy(const union bpf_attr *attr, @@ -4369,12 +4369,12 @@ struct bpf_link *bpf_link_by_id(u32 id) return link; } -#define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id +#define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_get_fd_by_id.id static int bpf_link_get_fd_by_id(const union bpf_attr *attr) { struct bpf_link *link; - u32 id = attr->link_id; + u32 id = attr->link_get_fd_by_id.id; int fd; if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID)) From patchwork Thu Oct 14 14:34:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12558701 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6903EC433FE for ; Thu, 14 Oct 2021 14:35:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 522A361167 for ; Thu, 14 Oct 2021 14:35:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231281AbhJNOhN (ORCPT ); Thu, 14 Oct 2021 10:37:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231308AbhJNOhN (ORCPT ); Thu, 14 Oct 2021 10:37:13 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ECE0EC061570 for ; Thu, 14 Oct 2021 07:35:07 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id g25so20116422wrb.2 for ; Thu, 14 Oct 2021 07:35:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=d0wyYzOBBIHyIQJznHdQNtkusLkJdooDK5maQza3Pxk=; b=fQ11nw6iE2HDuUqDiMFemjRxd3XpG+3M5mbJ/ILwutyxVBWBMUc/pd6PvI3HSpg5L6 MmJcm0nhlxmU5yczt/C+eId3AaBGeUEKlBOmJJ40gR2W8laQmS/Y/kBdXeRuyoWOD6J5 F8LV/V0NEx+6UEt/A8RXd0AJy1ifKFkUloQAw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=d0wyYzOBBIHyIQJznHdQNtkusLkJdooDK5maQza3Pxk=; b=6L5zxqnd0b2BAqJStu/gm23gij361psoCsRqAXbmjdyfdsJ4CtwbXkarrPKZjtre2s PA8SHIfmljQ9yNnGn841kqFwz4vW9kRds48QmMpjCOsEOO1RvYcogGtyKgS1fo9ynh3T 9lnBLCnZxl0pByya5SzRi+plaDQsKs2mTbrV9btxFCPfR1iLR6KcPSUQNq2N3j8InTak O0sbnqaUf8O/BL/2sZYg3LqGyiz1je6b4o5LKSez3UBMjcOu0TYaPMXPV/DF058PbCVu Ipc6ulDbbcDhIeCqcY2hGEP8fXZmjMWmqGmVuj6HWbXKCUPjqvDU9n+IfzAA50Gq4XI1 19qQ== X-Gm-Message-State: AOAM532UI85aXjo/Ul2SDaMLQ3JqaQcA8LNDsX2D8Ycu7xf8l/MlFR5b urQaUbTlB0chiGWEOVxho/8JOTTZ7HTxoQ== X-Google-Smtp-Source: ABdhPJwzZJQdE6d6eyaCQSRubL//NxIabDqUHbBvcoMACEo/yfIoK2TZVAqXvxfHtOgb/n9G8h3smQ== X-Received: by 2002:a7b:c742:: with SMTP id w2mr6227404wmk.61.1634222103092; Thu, 14 Oct 2021 07:35:03 -0700 (PDT) Received: from antares.. (4.4.a.7.5.8.b.d.d.b.6.7.4.d.a.6.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:6ad4:76bd:db85:7a44]) by smtp.gmail.com with ESMTPSA id k6sm2656439wri.83.2021.10.14.07.35.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Oct 2021 07:35:02 -0700 (PDT) From: Lorenz Bauer To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [RFC 8/9] selftests: sync bpf.h Date: Thu, 14 Oct 2021 15:34:35 +0100 Message-Id: <20211014143436.54470-12-lmb@cloudflare.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211014143436.54470-1-lmb@cloudflare.com> References: <20211014143436.54470-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-State: RFC --- tools/include/uapi/linux/bpf.h | 200 ++++++++++++++++++++++----------- 1 file changed, 134 insertions(+), 66 deletions(-) diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 6fc59d61937a..d3acd12d98c1 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -49,8 +49,14 @@ #define BPF_XCHG (0xe0 | BPF_FETCH) /* atomic exchange */ #define BPF_CMPXCHG (0xf0 | BPF_FETCH) /* atomic compare-and-write */ +#define __bpf_md_ptr(type, name) \ +union { \ + type name; \ + __u64 name##_u64; \ +} __attribute__((aligned(8))) + /* Register numbers */ -enum { +enum bpf_reg { BPF_REG_0 = 0, BPF_REG_1, BPF_REG_2, @@ -1056,16 +1062,19 @@ enum bpf_link_type { * All eligible programs are executed regardless of return code from * earlier programs. */ -#define BPF_F_ALLOW_OVERRIDE (1U << 0) -#define BPF_F_ALLOW_MULTI (1U << 1) -#define BPF_F_REPLACE (1U << 2) +enum bpf_prog_attach_flag { + BPF_F_ALLOW_OVERRIDE = (1U << 0), + BPF_F_ALLOW_MULTI = (1U << 1), + BPF_F_REPLACE = (1U << 2), +}; +enum bpf_prog_load_flag { /* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the * verifier will perform strict alignment checking as if the kernel * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set, * and NET_IP_ALIGN defined to 2. */ -#define BPF_F_STRICT_ALIGNMENT (1U << 0) + BPF_F_STRICT_ALIGNMENT = (1U << 0), /* If BPF_F_ANY_ALIGNMENT is used in BPF_PROF_LOAD command, the * verifier will allow any alignment whatsoever. On platforms @@ -1079,7 +1088,7 @@ enum bpf_link_type { * of an unaligned access the alignment check would trigger before * the one we are interested in. */ -#define BPF_F_ANY_ALIGNMENT (1U << 1) + BPF_F_ANY_ALIGNMENT = (1U << 1), /* BPF_F_TEST_RND_HI32 is used in BPF_PROG_LOAD command for testing purpose. * Verifier does sub-register def/use analysis and identifies instructions whose @@ -1097,10 +1106,10 @@ enum bpf_link_type { * Then, if verifier is not doing correct analysis, such randomization will * regress tests to expose bugs. */ -#define BPF_F_TEST_RND_HI32 (1U << 2) + BPF_F_TEST_RND_HI32 = (1U << 2), /* The verifier internal test flag. Behavior is undefined */ -#define BPF_F_TEST_STATE_FREQ (1U << 3) + BPF_F_TEST_STATE_FREQ = (1U << 3), /* If BPF_F_SLEEPABLE is used in BPF_PROG_LOAD command, the verifier will * restrict map and helper usage for such programs. Sleepable BPF programs can @@ -1108,8 +1117,10 @@ enum bpf_link_type { * Such programs are allowed to use helpers that may sleep like * bpf_copy_from_user(). */ -#define BPF_F_SLEEPABLE (1U << 4) + BPF_F_SLEEPABLE = (1U << 4), +}; +enum bpf_pseudo_src_reg { /* When BPF ldimm64's insn[0].src_reg != 0 then this can have * the following extensions: * @@ -1121,8 +1132,8 @@ enum bpf_link_type { * ldimm64 rewrite: address of map * verifier type: CONST_PTR_TO_MAP */ -#define BPF_PSEUDO_MAP_FD 1 -#define BPF_PSEUDO_MAP_IDX 5 + BPF_PSEUDO_MAP_FD = 1, + BPF_PSEUDO_MAP_IDX = 5, /* insn[0].src_reg: BPF_PSEUDO_MAP_[IDX_]VALUE * insn[0].imm: map fd or fd_idx @@ -1132,8 +1143,8 @@ enum bpf_link_type { * ldimm64 rewrite: address of map[0]+offset * verifier type: PTR_TO_MAP_VALUE */ -#define BPF_PSEUDO_MAP_VALUE 2 -#define BPF_PSEUDO_MAP_IDX_VALUE 6 + BPF_PSEUDO_MAP_VALUE = 2, + BPF_PSEUDO_MAP_IDX_VALUE = 6, /* insn[0].src_reg: BPF_PSEUDO_BTF_ID * insn[0].imm: kernel btd id of VAR @@ -1144,7 +1155,7 @@ enum bpf_link_type { * verifier type: PTR_TO_BTF_ID or PTR_TO_MEM, depending on whether the var * is struct/union. */ -#define BPF_PSEUDO_BTF_ID 3 + BPF_PSEUDO_BTF_ID = 3, /* insn[0].src_reg: BPF_PSEUDO_FUNC * insn[0].imm: insn offset to the func * insn[1].imm: 0 @@ -1153,19 +1164,20 @@ enum bpf_link_type { * ldimm64 rewrite: address of the function * verifier type: PTR_TO_FUNC. */ -#define BPF_PSEUDO_FUNC 4 + BPF_PSEUDO_FUNC = 4, /* when bpf_call->src_reg == BPF_PSEUDO_CALL, bpf_call->imm == pc-relative * offset to another bpf function */ -#define BPF_PSEUDO_CALL 1 + BPF_PSEUDO_CALL = 1, /* when bpf_call->src_reg == BPF_PSEUDO_KFUNC_CALL, * bpf_call->imm == btf_id of a BTF_KIND_FUNC in the running kernel */ -#define BPF_PSEUDO_KFUNC_CALL 2 + BPF_PSEUDO_KFUNC_CALL = 2, +}; /* flags for BPF_MAP_UPDATE_ELEM command */ -enum { +enum bpf_map_update_elem_flag { BPF_ANY = 0, /* create new element or update existing */ BPF_NOEXIST = 1, /* create new element if it didn't exist */ BPF_EXIST = 2, /* update existing element */ @@ -1173,7 +1185,7 @@ enum { }; /* flags for BPF_MAP_CREATE command */ -enum { +enum bpf_map_create_flag { BPF_F_NO_PREALLOC = (1U << 0), /* Instead of having one common LRU list in the * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list @@ -1213,17 +1225,19 @@ enum { }; /* Flags for BPF_PROG_QUERY. */ - +enum bpf_prog_query_flag { /* Query effective (directly attached + inherited from ancestor cgroups) * programs that will be executed for events within a cgroup. * attach_flags with this flag are returned only for directly attached programs. */ -#define BPF_F_QUERY_EFFECTIVE (1U << 0) - -/* Flags for BPF_PROG_TEST_RUN */ + BPF_F_QUERY_EFFECTIVE = (1U << 0), +}; +/* Flags for BPF_PROG_RUN */ +enum bpf_prog_run_flag { /* If set, run the test on the cpu specified by bpf_attr.test.cpu */ -#define BPF_F_TEST_RUN_ON_CPU (1U << 0) + BPF_F_TEST_RUN_ON_CPU = (1U << 0), +}; /* type for BPF_ENABLE_STATS */ enum bpf_stats_type { @@ -1240,7 +1254,10 @@ enum bpf_stack_build_id_status { BPF_STACK_BUILD_ID_IP = 2, }; -#define BPF_BUILD_ID_SIZE 20 +enum { + BPF_BUILD_ID_SIZE = 20, +}; + struct bpf_stack_build_id { __s32 status; unsigned char build_id[BPF_BUILD_ID_SIZE]; @@ -1250,41 +1267,68 @@ struct bpf_stack_build_id { }; }; -#define BPF_OBJ_NAME_LEN 16U +enum { + BPF_OBJ_NAME_LEN = 16U, +}; + +struct bpf_map_create_attr { + __u32 map_type; /* one of enum bpf_map_type */ + __u32 key_size; /* size of key in bytes */ + __u32 value_size; /* size of value in bytes */ + __u32 max_entries; /* max number of entries in a map */ + __u32 map_flags; /* BPF_MAP_CREATE related + * flags defined above. + */ + __u32 inner_map_fd; /* fd pointing to the inner map */ + __u32 numa_node; /* numa node (effective only if + * BPF_F_NUMA_NODE is set). + */ + char map_name[BPF_OBJ_NAME_LEN]; + __u32 map_ifindex; /* ifindex of netdev to create on */ + __u32 btf_fd; /* fd pointing to a BTF type data */ + __u32 btf_key_type_id; /* BTF type_id of the key */ + __u32 btf_value_type_id; /* BTF type_id of the value */ + __u32 btf_vmlinux_value_type_id; /* BTF type_id of a kernel- + * struct stored as the + * map value + */ +}; + +struct bpf_map_lookup_elem_attr { + __u32 map_fd; + __bpf_md_ptr(const void *, key); + __bpf_md_ptr(void *, value); + __u64 flags; +}; + +struct bpf_map_update_elem_attr { + __u32 map_fd; + __bpf_md_ptr(const void *, key); + __bpf_md_ptr(void *, value); + __u64 flags; +}; + +struct bpf_map_delete_elem_attr { + __u32 map_fd; + __bpf_md_ptr(const void *, key); +}; + +struct bpf_map_get_next_key_attr { + __u32 map_fd; + __bpf_md_ptr(const void *, key); + __bpf_md_ptr(void *, next_key); +}; union bpf_attr { - struct { /* anonymous struct used by BPF_MAP_CREATE command */ - __u32 map_type; /* one of enum bpf_map_type */ - __u32 key_size; /* size of key in bytes */ - __u32 value_size; /* size of value in bytes */ - __u32 max_entries; /* max number of entries in a map */ - __u32 map_flags; /* BPF_MAP_CREATE related - * flags defined above. - */ - __u32 inner_map_fd; /* fd pointing to the inner map */ - __u32 numa_node; /* numa node (effective only if - * BPF_F_NUMA_NODE is set). - */ - char map_name[BPF_OBJ_NAME_LEN]; - __u32 map_ifindex; /* ifindex of netdev to create on */ - __u32 btf_fd; /* fd pointing to a BTF type data */ - __u32 btf_key_type_id; /* BTF type_id of the key */ - __u32 btf_value_type_id; /* BTF type_id of the value */ - __u32 btf_vmlinux_value_type_id;/* BTF type_id of a kernel- - * struct stored as the - * map value - */ - }; + struct bpf_map_create_attr map_create; - struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */ - __u32 map_fd; - __aligned_u64 key; - union { - __aligned_u64 value; - __aligned_u64 next_key; - }; - __u64 flags; - }; + struct bpf_map_lookup_elem_attr map_lookup_elem; + + struct bpf_map_update_elem_attr map_update_elem; + + struct bpf_map_delete_elem_attr map_delete_elem; + + struct bpf_map_get_next_key_attr map_get_next_key; struct { /* struct used by BPF_MAP_*_BATCH commands */ __aligned_u64 in_batch; /* start batch, @@ -1487,6 +1531,32 @@ union bpf_attr { __u32 flags; /* extra flags */ } prog_bind_map; + /* DEPRECATED: these are kept for compatibility purposes. */ + struct { /* anonymous struct used by BPF_MAP_CREATE command */ + __u32 map_type; + __u32 key_size; + __u32 value_size; + __u32 max_entries; + __u32 map_flags; + __u32 inner_map_fd; + __u32 numa_node; + char map_name[BPF_OBJ_NAME_LEN]; + __u32 map_ifindex; + __u32 btf_fd; + __u32 btf_key_type_id; + __u32 btf_value_type_id; + __u32 btf_vmlinux_value_type_id; + }; + + struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */ + __u32 map_fd; + __aligned_u64 key; + union { + __aligned_u64 value; + __aligned_u64 next_key; + }; + __u64 flags; + }; } __attribute__((aligned(8))); /* The description below is an attempt at providing documentation to eBPF @@ -5230,7 +5300,7 @@ enum { }; /* BPF ring buffer constants */ -enum { +enum bpf_ringbuf_const { BPF_RINGBUF_BUSY_BIT = (1U << 31), BPF_RINGBUF_DISCARD_BIT = (1U << 30), BPF_RINGBUF_HDR_SZ = 8, @@ -5272,12 +5342,6 @@ enum { BPF_F_EXCLUDE_INGRESS = (1ULL << 4), }; -#define __bpf_md_ptr(type, name) \ -union { \ - type name; \ - __u64 :64; \ -} __attribute__((aligned(8))) - /* user accessible mirror of in-kernel sk_buff. * new fields can only be added to the end of this structure */ @@ -5456,7 +5520,9 @@ struct bpf_xdp_sock { __u32 queue_id; }; -#define XDP_PACKET_HEADROOM 256 +enum { + XDP_PACKET_HEADROOM = 256, +}; /* User return codes for XDP prog type. * A valid XDP program must return one of these defined values. All other @@ -5574,7 +5640,9 @@ struct sk_reuseport_md { __bpf_md_ptr(struct bpf_sock *, migrating_sk); }; -#define BPF_TAG_SIZE 8 +enum { + BPF_TAG_SIZE = 8, +}; struct bpf_prog_info { __u32 type; From patchwork Thu Oct 14 14:34:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12558699 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 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 519C4C433F5 for ; Thu, 14 Oct 2021 14:35:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3759861167 for ; Thu, 14 Oct 2021 14:35:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231315AbhJNOhL (ORCPT ); Thu, 14 Oct 2021 10:37:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231281AbhJNOhK (ORCPT ); Thu, 14 Oct 2021 10:37:10 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29E6AC061570 for ; Thu, 14 Oct 2021 07:35:05 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id r7so20205223wrc.10 for ; Thu, 14 Oct 2021 07:35:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TY45++CPmrGk/MF8B9SiWWaspjWPnEk5MkiCy4m/ex4=; b=t5oAcoGN59+bd2OGbeMTtL2zu+CI6Wf6zOmr1mj20agAklm/UNRAuSWzDKcTBkPOqL S2ga36y7S4g0uS7FhJxiqHetoiO8bO0S6SBwAlkNTmXJI8W5dWemGSugpqCYDDymPe+i MRjdohyfjqpw0uk4j3YG+ONaJ3ifQOwYr7PwU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TY45++CPmrGk/MF8B9SiWWaspjWPnEk5MkiCy4m/ex4=; b=UfYDIDMHmRLNQnkE9gezD8+cr06nGBgrfTAGj6pAflxpBWiibYNbyN2zHg22pHAMqs td6Q6L+1ckR7lW3dWYpZAiENWX6owMj10iALLQV8WOeXH0wtZ5FtMKkUcbA5yH7LpJul rnC9eUE5WYKWOwvZXzvKu7u41U8POVaKRYET5wDppN/UZ8V+Pt+/2L6c9fygZjJRJnes pLe+6iOytTiwbonDyM8C3Zxfo8+SUS7rUV+zyh9udsLHtLS9iUp9KXXb3jxr2ozrNXbH mwQB6aSRaL+5TbdBuhuhbFf/PlQgri8b4A7VDI/6RVK3KtOFEWINuEceErZb00MjY8Ux AhGg== X-Gm-Message-State: AOAM531RXcVPyRhaaxnQUTGFwZoGQ9mTNEwEiEUc260XfU3LBvgrJMVN AEkqIrRydr9eWnD+PADSgtw2qw== X-Google-Smtp-Source: ABdhPJw3t1ofHpHioLfxUETWA949zS4eZhZP90sS04RJRVAkz1OqRBzZPmwB1DDCT9l36V6NdH/s1A== X-Received: by 2002:a05:6000:1885:: with SMTP id a5mr6927634wri.64.1634222103782; Thu, 14 Oct 2021 07:35:03 -0700 (PDT) Received: from antares.. (4.4.a.7.5.8.b.d.d.b.6.7.4.d.a.6.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:6ad4:76bd:db85:7a44]) by smtp.gmail.com with ESMTPSA id k6sm2656439wri.83.2021.10.14.07.35.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Oct 2021 07:35:03 -0700 (PDT) From: Lorenz Bauer To: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net Cc: bpf@vger.kernel.org, kernel-team@cloudflare.com, Lorenz Bauer Subject: [RFC 9/9] libbpf: use new-style syscall args Date: Thu, 14 Oct 2021 15:34:36 +0100 Message-Id: <20211014143436.54470-13-lmb@cloudflare.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211014143436.54470-1-lmb@cloudflare.com> References: <20211014143436.54470-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC --- tools/lib/bpf/bpf.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 7d1741ceaa32..79a9bfe214b0 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -506,15 +506,14 @@ int bpf_map_delete_elem(int fd, const void *key) int bpf_map_get_next_key(int fd, const void *key, void *next_key) { - union bpf_attr attr; - int ret; + struct bpf_map_get_next_key_attr attr = { + .map_fd = fd, + .key = key, + .next_key = next_key, + }; - memset(&attr, 0, sizeof(attr)); - attr.map_fd = fd; - attr.key = ptr_to_u64(key); - attr.next_key = ptr_to_u64(next_key); + int ret = sys_bpf(BPF_MAP_GET_NEXT_KEY, (union bpf_attr *)&attr, sizeof(attr)); - ret = sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr)); return libbpf_err_errno(ret); }