Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1203513ybi; Fri, 21 Jun 2019 16:18:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqxtNZm08dPeiobs0fpdiytEClD0ljsEm00HqVRv2hlsy0kEQ+jBVP5vPjOX34EASiUAIosl X-Received: by 2002:a17:902:2be8:: with SMTP id l95mr125375871plb.231.1561159090605; Fri, 21 Jun 2019 16:18:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561159090; cv=none; d=google.com; s=arc-20160816; b=QZfA5c2yO3fpnGFBjOeJ6UFfknr6jyHOBJjVt5mq4K4L+pepyPL8eV6tMuBnWUcnjY rntNsTmsHQR3UCkyARSpJ2uNiETsyyZ4/8m9E4vusEwSE0f4+WPh7UNK4C2r+Q4V+E8d 2okhIAcTc24axxmTicqE6MUcDy78njaYycGxiaqMQbM1tKxojBgdK1yfhqZEMd8rOkmu S89+op6Smv1s5AxFlz6GiZURHHs24PG8CTBMZ/cDqK2Oky3fxTdgeQ5yyFQEA0Se5fRD 2d9alPuxEvVnt35alJDWo/Ki/HmO+L3BA7esT1LjuTmR2QbXqG7Ajjbjt1fhnQ9u5fYm i0hA== 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=t/AyvcKYelZOkRBej05NHVNdPUqzSYdpnSoVrXwFeq8=; b=xDn0bwzl5umjswK7jwig160GKZG1orfmHaWPnzWb4s3x/ntzsvMHiIwIMzhMTOdPeR QXLBbv6hKmykfQEf8SSA00b7lQClyWk0RKI6BGNIMIRNz/A2fWsmbzyiP+qc8B1hkQG7 ub6PJaCJeIkl4k4fWUJ88XSBeEgxKFznpMV7RdpaHIseOKAs+LDaiSYOgrrMXG+iZ+68 zE7g39RYrqh+TowaawDvqiEDbvfAxZ7sRbTed8DFMKmJUErRx72oTc//F/Ih7qJUGHBu vlTJePADgPcuY8pbr8xODHdMMKJatqcLjqHcFdMTglFQCLjR6o1jFn6ouFzoUNwBJACP kl3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=B9Z3H8QT; 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 u19si3689707pgm.471.2019.06.21.16.17.55; Fri, 21 Jun 2019 16:18:10 -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=B9Z3H8QT; 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 S1726159AbfFUXRC (ORCPT + 99 others); Fri, 21 Jun 2019 19:17:02 -0400 Received: from mail-pf1-f202.google.com ([209.85.210.202]:36301 "EHLO mail-pf1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726049AbfFUXRA (ORCPT ); Fri, 21 Jun 2019 19:17:00 -0400 Received: by mail-pf1-f202.google.com with SMTP id h15so5277456pfn.3 for ; Fri, 21 Jun 2019 16:17:00 -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=t/AyvcKYelZOkRBej05NHVNdPUqzSYdpnSoVrXwFeq8=; b=B9Z3H8QTIhgljGVr21xxKuCQSAUteNzuLnIR45pj/KSavVYu52KUe4EWiL+820PeIA 1eiH126tYB7qw608tdE5kY3eI5zRmST/YSgm4d/c3YdK1M2olH91FofinMNIIDadRGNg gAMbqzKlLkdr6Uge2EXPpRMBz0Wk+gF20YOWGGGUb1ZDV2fVCuzr9naZCqorcIilwkAt cBdORtrxwPwBQu0AyoS3TY2SP7xnJdWKNB2Y/TB8QFKgz0jNto7uOiMQ7ozcLCKxY+Cf YoMw3tgrihHmO1nA/M0E63tM2JESnkITwBbVQ1j8zrt4IpygjwOv2TdZr/hkrChN1GcO IZeA== 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=t/AyvcKYelZOkRBej05NHVNdPUqzSYdpnSoVrXwFeq8=; b=JBwqp92clIIwOzLel7c9+ieN13wU8hsCck0wftrIFKMbSisLq4Kf83pcFWOYcl3Ne5 uzfod2LXMViyBkLijenU5lnaf7A7yvOgIplJa7tH8tpbNLLlHijK5Ob1jxVKVCDBZJCE raUS0+ap3EKFnIq2pYsKtwaMGt19yuGnb5sssa1VwiAfO7A5aqKD1+CbojeKsTurKI76 axmzbT4TXsjrj/u4Pcr1qtI6xrvAXmp37HA7W46g/rldrbuEI80oBzEcNX3S4etMNJYI 16S8PLJXF6hXe6WBsmmaevN8Cy6ZdBhFs/6Nunyl061O970NHmakDEX9H48jEdVffDEK zycA== X-Gm-Message-State: APjAAAXjJhxF34GENxbM2MbSB+zdB6YfoA0OybSb3QU6xFnNc8JtP/z5 6pn3XHJ6grqF678n5I9pBd6ueLiop8Og X-Received: by 2002:a63:6cc3:: with SMTP id h186mr864592pgc.292.1561159019516; Fri, 21 Jun 2019 16:16:59 -0700 (PDT) Date: Fri, 21 Jun 2019 16:16:45 -0700 In-Reply-To: <20190621231650.32073-1-brianvv@google.com> Message-Id: <20190621231650.32073-2-brianvv@google.com> Mime-Version: 1.0 References: <20190621231650.32073-1-brianvv@google.com> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [RFC PATCH 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 7713cf39795a4..a1823a50f9be0 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