All of lore.kernel.org
 help / color / mirror / Atom feed
From: Song Liu <liu.song.a23@gmail.com>
To: lmb@cloudflare.com
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Networking <netdev@vger.kernel.org>,
	linux-api@vger.kernel.org
Subject: Re: [PATCH v2 1/3] bpf: allow zero-initializing hash map seed
Date: Mon, 8 Oct 2018 16:07:58 -0700	[thread overview]
Message-ID: <CAPhsuW5_1LRw=tjaNWfgrPKaS_Rs12BYAHPOjmXwEs8C9YSy1Q@mail.gmail.com> (raw)
In-Reply-To: <20181008103221.13468-2-lmb@cloudflare.com>

On Mon, Oct 8, 2018 at 3:34 AM Lorenz Bauer <lmb@cloudflare.com> wrote:
>
> Add a new flag BPF_F_ZERO_SEED, which forces a hash map
> to initialize the seed to zero. This is useful when doing
> performance analysis both on individual BPF programs, as
> well as the kernel's hash table implementation.
>
> Signed-off-by: Lorenz Bauer <lmb@cloudflare.com>
> ---
>  include/uapi/linux/bpf.h |  2 ++
>  kernel/bpf/hashtab.c     | 13 +++++++++++--
>  2 files changed, 13 insertions(+), 2 deletions(-)
>
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index f9187b41dff6..2c121f862082 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -253,6 +253,8 @@ enum bpf_attach_type {
>  #define BPF_F_NO_COMMON_LRU    (1U << 1)
>  /* Specify numa node during map creation */
>  #define BPF_F_NUMA_NODE                (1U << 2)
> +/* Zero-initialize hash function seed. This should only be used for testing. */
> +#define BPF_F_ZERO_SEED                (1U << 6)

Please add this line after
#define BPF_F_STACK_BUILD_ID    (1U << 5)

Other than this
Acked-by: Song Liu <songliubraving@fb.com>


>
>  /* flags for BPF_PROG_QUERY */
>  #define BPF_F_QUERY_EFFECTIVE  (1U << 0)
> diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
> index 2c1790288138..4b7c76765d9d 100644
> --- a/kernel/bpf/hashtab.c
> +++ b/kernel/bpf/hashtab.c
> @@ -23,7 +23,7 @@
>
>  #define HTAB_CREATE_FLAG_MASK                                          \
>         (BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE |    \
> -        BPF_F_RDONLY | BPF_F_WRONLY)
> +        BPF_F_RDONLY | BPF_F_WRONLY | BPF_F_ZERO_SEED)
>
>  struct bucket {
>         struct hlist_nulls_head head;
> @@ -244,6 +244,7 @@ static int htab_map_alloc_check(union bpf_attr *attr)
>          */
>         bool percpu_lru = (attr->map_flags & BPF_F_NO_COMMON_LRU);
>         bool prealloc = !(attr->map_flags & BPF_F_NO_PREALLOC);
> +       bool zero_seed = (attr->map_flags & BPF_F_ZERO_SEED);
>         int numa_node = bpf_map_attr_numa_node(attr);
>
>         BUILD_BUG_ON(offsetof(struct htab_elem, htab) !=
> @@ -257,6 +258,10 @@ static int htab_map_alloc_check(union bpf_attr *attr)
>                  */
>                 return -EPERM;
>
> +       if (zero_seed && !capable(CAP_SYS_ADMIN))
> +               /* Guard against local DoS, and discourage production use. */
> +               return -EPERM;
> +
>         if (attr->map_flags & ~HTAB_CREATE_FLAG_MASK)
>                 /* reserved bits should not be used */
>                 return -EINVAL;
> @@ -373,7 +378,11 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr)
>         if (!htab->buckets)
>                 goto free_htab;
>
> -       htab->hashrnd = get_random_int();
> +       if (htab->map.map_flags & BPF_F_ZERO_SEED)
> +               htab->hashrnd = 0;
> +       else
> +               htab->hashrnd = get_random_int();
> +
>         for (i = 0; i < htab->n_buckets; i++) {
>                 INIT_HLIST_NULLS_HEAD(&htab->buckets[i].head, i);
>                 raw_spin_lock_init(&htab->buckets[i].lock);
> --
> 2.17.1
>

  reply	other threads:[~2018-10-09  6:22 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-01 10:45 [PATCH 0/3] bpf: allow zero-initialising hash map seed Lorenz Bauer
2018-10-01 10:45 ` [PATCH 1/3] bpf: allow zero-initializing " Lorenz Bauer
2018-10-02 19:59   ` Jann Horn
2018-10-05  7:42     ` Lorenz Bauer
2018-10-05 14:12       ` Jann Horn
2018-10-05 14:21         ` Lorenz Bauer
2018-10-05 14:27           ` Jann Horn
2018-10-05 21:07             ` Alexei Starovoitov
2018-10-08  9:48               ` Lorenz Bauer
2018-10-01 10:45 ` [PATCH 2/3] tools: sync linux/bpf.h Lorenz Bauer
2018-10-01 10:45 ` [PATCH 3/3] tools: add selftest for BPF_F_ZERO_SEED Lorenz Bauer
2018-10-01 19:12 ` [PATCH 0/3] bpf: allow zero-initialising hash map seed Daniel Borkmann
2018-10-05 14:27   ` Lorenz Bauer
2018-10-05 14:29     ` Jann Horn
2018-10-08 10:32 ` [PATCH v2 " Lorenz Bauer
2018-10-08 10:32   ` [PATCH v2 1/3] bpf: allow zero-initializing " Lorenz Bauer
2018-10-08 23:07     ` Song Liu [this message]
2018-10-25 15:12       ` Lorenz Bauer
2018-11-07  0:39         ` Song Liu
2018-10-08 10:32   ` [PATCH v2 2/3] tools: sync linux/bpf.h Lorenz Bauer
2018-10-08 23:12     ` Song Liu
2018-10-25 15:07       ` Lorenz Bauer
2018-10-08 10:32   ` [PATCH v2 3/3] tools: add selftest for BPF_F_ZERO_SEED Lorenz Bauer
2018-10-08 23:15     ` Song Liu
2018-11-16 11:41 ` [PATCH v3 0/4] bpf: allow zero-initialising hash map seed Lorenz Bauer
2018-11-16 11:41   ` [PATCH v3 1/4] bpf: allow zero-initializing " Lorenz Bauer
2018-11-16 11:41   ` [PATCH v3 2/4] bpf: move BPF_F_QUERY_EFFECTIVE after map flags Lorenz Bauer
2018-11-16 11:41   ` [PATCH v3 3/4] tools: sync linux/bpf.h Lorenz Bauer
2018-11-16 11:41   ` [PATCH v3 4/4] tools: add selftest for BPF_F_ZERO_SEED Lorenz Bauer
2018-11-16 17:33   ` [PATCH v3 0/4] bpf: allow zero-initialising hash map seed Song Liu
2018-11-16 17:34   ` Song Liu
2018-11-19 23:56   ` Daniel Borkmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAPhsuW5_1LRw=tjaNWfgrPKaS_Rs12BYAHPOjmXwEs8C9YSy1Q@mail.gmail.com' \
    --to=liu.song.a23@gmail.com \
    --cc=ast@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=linux-api@vger.kernel.org \
    --cc=lmb@cloudflare.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.