Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp2494925ybd; Thu, 27 Jun 2019 13:25:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqxfSpqrLIFMKw0Wn78gD9s2rGGMmUH4pPzkBLkvBOaE4CG1KcRln5Aj3b1ovNymEwg+/g4A X-Received: by 2002:a17:90a:8d86:: with SMTP id d6mr7933430pjo.127.1561667099951; Thu, 27 Jun 2019 13:24:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561667099; cv=none; d=google.com; s=arc-20160816; b=c3SPV2OfOV5sk9seoQ1A95MsqUK3bKGqC7azg3oiKRM/iWVrxtqU7lmSFH1FfddqG0 COKwEmkpBkZTkj1lSh2zLH9cFncmschYfg+HhbqSvr8RVhEs7SpGHhSj5R7Hfl8+56as pdAZYMHb3ORt9yhfQzovnpOBxRI9Jc7lDWALqfTGNl6lj7Ie9XVh8M7l1C0A2hEDuBYh WFRYQyxRFVdcrWYoQCdm4/Hh2Yhxk49vTLhMydIBZqFnAymZnx6BmCsqEFhq4Vz75FWn TPr3As5MHtsfUzQVfiViGOvHfuodX1MCYHSfUrHEpPtUvZ8sT+PB2poC4XIFf63ezIi3 sQxQ== 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=FsHTwlDuPOXwKF6R2BdKoCj90n7RJy8Cmx11ffq18XUR4NGmTXC0NzKsoYIuFtwbiH XGdnBeSk0Q4DMrRafEuCNUtcNie2Ig8pmhDSLauyjdtXfGNLg+cVWblwSt/rm4g/fu3c vce1b0HSz6bHhYAQCw6tmbawbi0vBSfXRDpr3uhnCd9jAfxKjSHTOTIt4GpDhBIKhY8G oYMlZzreFtmMyZrdzzRmXxC07z9zYdBZm4VQPpeeGJmCq1vE4GF1E8UawF8FDtDbBlnA dcSp6LP3umGBfGza1aA1is1JyvkaMVDK7hbg3fxdg0K/GYtpXryvuhaSgrAVOYtlxWxl eLyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=LIW93hNP; 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 b3si3237774pls.52.2019.06.27.13.24.44; Thu, 27 Jun 2019 13:24:59 -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=LIW93hNP; 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 S1726579AbfF0UYf (ORCPT + 99 others); Thu, 27 Jun 2019 16:24:35 -0400 Received: from mail-pf1-f202.google.com ([209.85.210.202]:52654 "EHLO mail-pf1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726484AbfF0UYe (ORCPT ); Thu, 27 Jun 2019 16:24:34 -0400 Received: by mail-pf1-f202.google.com with SMTP id a20so2260235pfn.19 for ; Thu, 27 Jun 2019 13:24:34 -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=LIW93hNP6NVq8yXgn8E05Ej1/SJ7vmBTNKx8snF9AkQiLA5O6goQISrbGKdqM5GDdg k7qSx2jY38J8QyD+G/790+OFELCmMSfY1Uib081YHXRR9lqHAvkWg+2kBUR4peNAN8ay IQkAtWSz9biOy0dEmIbs56ULp+myJ3z5Vibs8YNJBE7Lqt1IpbtFoVSOycJNu9RO0Ciy uY/lueImZPyKGnrl0xJdKBzaM1zDAhFNcuVT0qDw+8HYR59Zxyyr5x3Ox835FQ1xrf7u Mk6meYMbl7C928p5T6+msNfup+p0E4oPk270xoGIhsZntaE53djxvuBT5M/uZSERHNUU W1+w== 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=J99VlKuQPHfVhKmni8/6SNg3rCacnN+wJTMaiADpyileMkjBfnCeFUht9RGz3OkA7m YGZVdmbUTmpTNzTdAvFWihvasUsUo5dU0s2TlyTR7WTMtfFBNSNxq2bXfNJYAbVV8/98 ikvhBY7DpQi02E1uq3pJ5r1Y+V2FN0Upvfgpu1pi60fUwQXxfGE1W7xBmz1ZMw+VBXEZ z9HEDym5GcGcsIrE6tSui6bmgoocXFYtHfoISTOBqaKu6kmwAdW951YKvO1XqUBhHskM cwP+lWeG/5wx5Pyua4UUaIt7i4Ja67tagVNY7vhuF8X7/RWvWRYSVZRWpuJD4c3UQoR1 TkSw== X-Gm-Message-State: APjAAAW84b9leguIG80oZUjS+UHMpFIye01/HFIS0fQxhVIt53lGFgjU YITrYAfG4ne9pDk6/o5RaUnG8/r44Odd X-Received: by 2002:a63:1864:: with SMTP id 36mr4089744pgy.272.1561667073214; Thu, 27 Jun 2019 13:24:33 -0700 (PDT) Date: Thu, 27 Jun 2019 13:24:12 -0700 In-Reply-To: <20190627202417.33370-1-brianvv@google.com> Message-Id: <20190627202417.33370-2-brianvv@google.com> Mime-Version: 1.0 References: <20190627202417.33370-1-brianvv@google.com> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog Subject: [RFC PATCH bpf-next v2 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