Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp9833701ybi; Wed, 24 Jul 2019 10:44:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqw005zgYnZaHPPiZeZBC8qRWBJaEyvQiGANwqJzyuFTWQYTxF6MzERDHceWQNjfBShqApxW X-Received: by 2002:a17:902:9346:: with SMTP id g6mr86636511plp.61.1563990282379; Wed, 24 Jul 2019 10:44:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563990282; cv=none; d=google.com; s=arc-20160816; b=PJiowrJFB+yG0IrRNKssufs8RfNGWT7lHikntv7981HgvVG99h6W/zKcqCvsOoMQ0S MnzRIvoakdZWXyHMX33bjVK+E/qK9i16WB/1ShCwJK0QfOTGbjbt/dThxB2/Z8Kqd4Mi iMSMfiBWARcjriPXWS2bRYwyFC8RUWF9JSaCcnSngz1bGOKly/lgQCFPufL79D7edC5n xRlPPXXhR0EoBShnLVM602vXX8MnxBDEAe1jhbLMadszcsF1dg/EyVUpQxe4DYqEcGNO P/Gd7dbfqPmt0HVm+JJ+KotkUYfSALoERD2Gfr3fuUo0SRdsON3/imFS3cty7bpNM+Qy mtZQ== 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=aJbPYrkSLi1J83pO68ABpJTFWg54SVdLxJdC1FsJCN0=; b=oK1SVEUVlg2XOAo+EqMrYacDymD1mPCD9p1rQF92Va0a/4b38B2tQFYQ6fGWi/THSx D4okyPhb3V+nwLSQkML9tJTxrDZ+9QSWEwhjLhSpPbPrCq7nTKAQY+dpOP8WhJDvz8FC 2h5iO3PMCIRXzfA9G5FBALMGQ6MyvQ2GvM0Fm76gU51k1Ovm+omf731n0r82po1xLwky Ms5gkqNR/ZWgsDTcyBFnQ9q0rN+cAfAgiQeg2Dq6NXlL1sxqLnKR6NpvK98oY6uhc7WP nRgFUlB6Xc0qkQfyNiNwDT9VDPIxPaIRCbBnJ6Bgyutf2S3Vt5CMsQPhpzvU2mWBFOgJ aMRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=kAKMDoDj; 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 v4si6353879pgf.470.2019.07.24.10.44.26; Wed, 24 Jul 2019 10:44:42 -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=kAKMDoDj; 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 S1726282AbfGXQ6b (ORCPT + 99 others); Wed, 24 Jul 2019 12:58:31 -0400 Received: from mail-qt1-f202.google.com ([209.85.160.202]:35971 "EHLO mail-qt1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728364AbfGXQ63 (ORCPT ); Wed, 24 Jul 2019 12:58:29 -0400 Received: by mail-qt1-f202.google.com with SMTP id q26so42084784qtr.3 for ; Wed, 24 Jul 2019 09:58:29 -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=aJbPYrkSLi1J83pO68ABpJTFWg54SVdLxJdC1FsJCN0=; b=kAKMDoDjy/GQB+zG684OCW6wlWx3U7guijvEtLmQG1ZO+fg+9B4lwuuHZ1YCxFYbey aIILqCEbkhNMH/1BsaX8Cxra7NcE87v+SsbjD5OpcBIbmmWlRz3uXMB8+C45RrrUOw9h xvhQNp4wx4gnPQZooqPeuiaeCNaLX3GT0rnAX4NteS3vrzGcdqIfpUxooLnXV3Z9m9HE jIjNzyssLbdVIfwJJ/JdhgFI7pcegQgx94lyB5+p3zkgH3rbyskSznAv/K0kwwmiAYgW K4/jqLxEv81Y0oEZXKVf8AxZ+dEGvNEjviOpTqygmzBCY5wRY4ccqdr4SUdcY3UI0ork IcWg== 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=aJbPYrkSLi1J83pO68ABpJTFWg54SVdLxJdC1FsJCN0=; b=nKiJovz9UJ+7DAGwK+fig32dw2w/wD45sBwOMEzApEuudk8S01jHCypLs66CDVMk7n 29NAj7fJZfTjSpIUEQPz/lbDYAaYHort6yPy5SOtrlMq9ecXvQe0TyDqj1rOD1VVOYDs 93Va8Wn3l8eqrUi3z0qIVXeP1fNKCKet0uge1Q/f2sTysZmZEA0kJnshoVyKOZkMS3fg v1uBVd6H+wadagg1At2asV/gyxPPm0AT1a4mMydszsWUyZqcS88edv0kJTiJjC4/fgUD lahiPqdlI6U/clg+kDAcIVHg+/A+VEvBE/x+OB+jObgZPI8m+GlZkZaupxJWUkmcyMgn Copw== X-Gm-Message-State: APjAAAXR2969VuMnNjdSH7SQMX23e/p8d3V64KsskVP+ccRcViOhnBI3 g6za+jeI4akOdoxtbACK9Ikr7dJfrOX3 X-Received: by 2002:a0c:afbd:: with SMTP id s58mr60379022qvc.217.1563987508719; Wed, 24 Jul 2019 09:58:28 -0700 (PDT) Date: Wed, 24 Jul 2019 09:57:58 -0700 In-Reply-To: <20190724165803.87470-1-brianvv@google.com> Message-Id: <20190724165803.87470-2-brianvv@google.com> Mime-Version: 1.0 References: <20190724165803.87470-1-brianvv@google.com> X-Mailer: git-send-email 2.22.0.657.g960e92d24f-goog Subject: [PATCH bpf-next 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 5d141f16f6fa9..86cdc2f7bb56e 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.657.g960e92d24f-goog