Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp1133653ybi; Wed, 3 Jul 2019 10:03:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqx4q/4+amb8s3+xeWRtpM9dE7DQmpBIyeMSFkwzNIQD1KLbXhqNl2diF6DrH0PRRtf8H2qJ X-Received: by 2002:a17:90a:3401:: with SMTP id o1mr13861424pjb.7.1562173438305; Wed, 03 Jul 2019 10:03:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562173438; cv=none; d=google.com; s=arc-20160816; b=HM14f0HxP9lUFQTs8jnRZbbjPRgyB6j7AKLbf5t+PdqmEunlKn+FtBnbk0n3v+YYJg m08rl6walG9G15uVqtKt2aSD7D18+4b4fp/2ax06orRl8jm0ahGXDsdusfvqBqP7nNGG Y/4sD0lXeUZuZVpjibs1apJjK26t6i5QKVJWZR/8PYBwtvfqszP7MyE2oe4741x+wwhK NA4F5c14e1dnJy0lCsglD4nLVraky6VTLlDwdKIY5ObGY+eEeuiQijWWy/gp0gsPGk8/ H1i0KBpDZqCmddDPOx5bl0jif7vhaH1gnDekdFwLwQ/ukUqvRUVg3WLqgFj9nfU84m6b PKeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=a4B2pi3afPrfivcwvxY/0YFG7emhlAa6z/TIz7oLh4I=; b=hZuGRxDOmh5pbOxPNX6//ZLaZpBOdHm+4MxJwlodurGTf1Oq0wwvigm5Mf6d5uTmOK y9qGh33KWmR+k9hjU/m/oGCb6h25u+IvIBlKB+a+pgC5cr9405Tsp5e6OKpTrargC8k6 DGIYPTgLlFXKrversDYrHeMIqgGtrGzXJlV8us0s687VGsr5gkxZoF3YakrCss4Vjhqc 4cuDfHcK/dTnOvHv+WhcMLQ8cMk75FcIGlLIdkq6jmU6qrcri0vN65pfRxvEwbrhDCvF nK5tjoXO7IFSOiL/QNS9m1Zrn+CMpDTi9/GvKWvjhVE+/GffnJ6hIca+mHHuMwiNmw52 xJDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=bukW9Fgr; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v16si2841285pfe.39.2019.07.03.10.03.28; Wed, 03 Jul 2019 10:03:58 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=bukW9Fgr; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727262AbfGCRCJ (ORCPT + 99 others); Wed, 3 Jul 2019 13:02:09 -0400 Received: from mail-vs1-f74.google.com ([209.85.217.74]:49188 "EHLO mail-vs1-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727239AbfGCRCF (ORCPT ); Wed, 3 Jul 2019 13:02:05 -0400 Received: by mail-vs1-f74.google.com with SMTP id b19so171194vsq.16 for ; Wed, 03 Jul 2019 10:02:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=a4B2pi3afPrfivcwvxY/0YFG7emhlAa6z/TIz7oLh4I=; b=bukW9FgrgpnaTfg+obcuWRTUvcBzM1ggSi0JTvkY2Ae5L6Evnn1MydKeWAdJr4jHpD K155rMxmXXFSDoq4Gb6xRF5gobEtv7Uo8uSmSZ14Anr3zhMh2ChciBLGQZGq6OekZKEe jRP1pbytIydxHfngZ0esiuBzO2S38JHCupNdqAzG3a1512b2/+dX0x06ht1OuIhnMZwO /9nO/fzdGARi9rMTIVYLWPZIdBTRyaktPnhp99sgEoQSR4Wu8SAzlVe31dFU/gEiQPhV 7AEr53drxmxgT0iNyHiR5JGfHl5U/KRFgOeuXxobQH3RkOQwsTNpuYtWiXrOvJcJ3v6L qx+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=a4B2pi3afPrfivcwvxY/0YFG7emhlAa6z/TIz7oLh4I=; b=SozjkxGHnpQNRbGppzWXlpHHCVZXP/OFsBIXT0v9skyIcwgY/PWjUXPtTJoCkO+FtC Mb+ajYjLfSZa08NKR1/FoxAGoacKRnsrTJwbfCgYs5rYoMoY9v47pwTbe1Es1xUbTzna CMfw8B+Of86MTtHwz72JmiFHehb3xgrKA4QfKz7VK5EhWUSYs4oe8a8zgkQ1WIk4sgnQ NbzCISmg8ZpBUqjAZlPH4LuJQnOoaYCvKSBI3UEtC8CNujpfcE8WT1YIj8lm56BcUvEJ BxJyNFGwag1ciEzWi9IpgzRMCjMJnt9oYpyLBM1TJb9hfwObmAueHu7AfftQIGkweG8L nGJQ== X-Gm-Message-State: APjAAAWF+2AdT5YGEhx4atGM6xJjHOKXevab8kQGrAfrk5Cf3C6fchbD YIW146933wkZHxxr8tIO9RNzsBK0Sygh X-Received: by 2002:a1f:a887:: with SMTP id r129mr11227778vke.75.1562173324191; Wed, 03 Jul 2019 10:02:04 -0700 (PDT) Date: Wed, 3 Jul 2019 10:01:13 -0700 In-Reply-To: <20190703170118.196552-1-brianvv@google.com> Message-Id: <20190703170118.196552-2-brianvv@google.com> Mime-Version: 1.0 References: <20190703170118.196552-1-brianvv@google.com> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [PATCH bpf-next RFC v3 1/6] bpf: add bpf_map_value_size and bp_map_copy_value helper functions From: Brian Vazquez To: Brian Vazquez , Alexei Starovoitov , Daniel Borkmann , "David S . Miller" Cc: Stanislav Fomichev , Willem de Bruijn , Petar Penkov , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Brian Vazquez Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Move reusable code from map_lookup_elem to helper functions to avoid code duplication in kernel/bpf/syscall.c Suggested-by: Stanislav Fomichev Signed-off-by: Brian Vazquez --- kernel/bpf/syscall.c | 134 +++++++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 61 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index b0f545e07425..d200d2837ade 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -126,6 +126,76 @@ static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) return map; } +static u32 bpf_map_value_size(struct bpf_map *map) +{ + if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || + map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || + map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || + map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) + return round_up(map->value_size, 8) * num_possible_cpus(); + else if (IS_FD_MAP(map)) + return sizeof(u32); + else + return map->value_size; +} + +static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value, + __u64 flags) +{ + void *ptr; + int err; + + if (bpf_map_is_dev_bound(map)) + return bpf_map_offload_lookup_elem(map, key, value); + + preempt_disable(); + this_cpu_inc(bpf_prog_active); + if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || + map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { + err = bpf_percpu_hash_copy(map, key, value); + } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { + err = bpf_percpu_array_copy(map, key, value); + } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { + err = bpf_percpu_cgroup_storage_copy(map, key, value); + } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { + err = bpf_stackmap_copy(map, key, value); + } else if (IS_FD_ARRAY(map)) { + err = bpf_fd_array_map_lookup_elem(map, key, value); + } else if (IS_FD_HASH(map)) { + err = bpf_fd_htab_map_lookup_elem(map, key, value); + } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { + err = bpf_fd_reuseport_array_lookup_elem(map, key, value); + } else if (map->map_type == BPF_MAP_TYPE_QUEUE || + map->map_type == BPF_MAP_TYPE_STACK) { + err = map->ops->map_peek_elem(map, value); + } else { + rcu_read_lock(); + if (map->ops->map_lookup_elem_sys_only) + ptr = map->ops->map_lookup_elem_sys_only(map, key); + else + ptr = map->ops->map_lookup_elem(map, key); + if (IS_ERR(ptr)) { + err = PTR_ERR(ptr); + } else if (!ptr) { + err = -ENOENT; + } else { + err = 0; + if (flags & BPF_F_LOCK) + /* lock 'ptr' and copy everything but lock */ + copy_map_value_locked(map, value, ptr, true); + else + copy_map_value(map, value, ptr); + /* mask lock, since value wasn't zero inited */ + check_and_init_map_lock(map, value); + } + rcu_read_unlock(); + } + this_cpu_dec(bpf_prog_active); + preempt_enable(); + + return err; +} + void *bpf_map_area_alloc(size_t size, int numa_node) { /* We really just want to fail instead of triggering OOM killer @@ -729,7 +799,7 @@ static int map_lookup_elem(union bpf_attr *attr) void __user *uvalue = u64_to_user_ptr(attr->value); int ufd = attr->map_fd; struct bpf_map *map; - void *key, *value, *ptr; + void *key, *value; u32 value_size; struct fd f; int err; @@ -761,72 +831,14 @@ static int map_lookup_elem(union bpf_attr *attr) goto err_put; } - if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || - map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH || - map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY || - map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) - value_size = round_up(map->value_size, 8) * num_possible_cpus(); - else if (IS_FD_MAP(map)) - value_size = sizeof(u32); - else - value_size = map->value_size; + value_size = bpf_map_value_size(map); err = -ENOMEM; value = kmalloc(value_size, GFP_USER | __GFP_NOWARN); if (!value) goto free_key; - if (bpf_map_is_dev_bound(map)) { - err = bpf_map_offload_lookup_elem(map, key, value); - goto done; - } - - preempt_disable(); - this_cpu_inc(bpf_prog_active); - if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH || - map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) { - err = bpf_percpu_hash_copy(map, key, value); - } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) { - err = bpf_percpu_array_copy(map, key, value); - } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) { - err = bpf_percpu_cgroup_storage_copy(map, key, value); - } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) { - err = bpf_stackmap_copy(map, key, value); - } else if (IS_FD_ARRAY(map)) { - err = bpf_fd_array_map_lookup_elem(map, key, value); - } else if (IS_FD_HASH(map)) { - err = bpf_fd_htab_map_lookup_elem(map, key, value); - } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) { - err = bpf_fd_reuseport_array_lookup_elem(map, key, value); - } else if (map->map_type == BPF_MAP_TYPE_QUEUE || - map->map_type == BPF_MAP_TYPE_STACK) { - err = map->ops->map_peek_elem(map, value); - } else { - rcu_read_lock(); - if (map->ops->map_lookup_elem_sys_only) - ptr = map->ops->map_lookup_elem_sys_only(map, key); - else - ptr = map->ops->map_lookup_elem(map, key); - if (IS_ERR(ptr)) { - err = PTR_ERR(ptr); - } else if (!ptr) { - err = -ENOENT; - } else { - err = 0; - if (attr->flags & BPF_F_LOCK) - /* lock 'ptr' and copy everything but lock */ - copy_map_value_locked(map, value, ptr, true); - else - copy_map_value(map, value, ptr); - /* mask lock, since value wasn't zero inited */ - check_and_init_map_lock(map, value); - } - rcu_read_unlock(); - } - this_cpu_dec(bpf_prog_active); - preempt_enable(); - -done: + err = bpf_map_copy_value(map, key, value, attr->flags); if (err) goto free_value; -- 2.22.0.410.gd8fdbe21b5-goog