Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp7300089rdb; Wed, 3 Jan 2024 10:56:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IGyYNGoDDnn/gt630mQ3CZZaaGK4lE3I+LBMTCOmvhEhWO5TXTmoeSH6kt2PyKlAD1XTY8f X-Received: by 2002:a17:90a:4701:b0:28c:b90c:9fb8 with SMTP id h1-20020a17090a470100b0028cb90c9fb8mr2926203pjg.28.1704308163994; Wed, 03 Jan 2024 10:56:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704308163; cv=none; d=google.com; s=arc-20160816; b=iF6Lw1YmS7b8Oyu9dpGPPXASCiQkh1cLD7ftv36BOxVmIUUO/QEXCq3p34m/xo+Yfy eoatuj9KO0WhGZQA4bRZNTWYnfV1gE3F7glKDXsguRdbBVWuFEMYZJ2isAMpJ4DPdrID oDJQsWbSzm0Zq0B94w8KripT4wMowI92fJpXKXfGztaBMJj7HzGuh4Hv9QgkG9qW7R0N 1Dnp7ww9Orwar1F4pgZVQlA106j8p9XIN9bD93Gcmi5DxlHc/rc7N9m/bgypmrGQiIk8 0X0+Hz+CnfIhfzP4z5GYRDLPClH+ntY970qvADr79ycTLBL6pSzcglwaBzL7XkxK3tHB CkzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=ZUt8YVNSoa+7YDD+I3FZ435kHRVdX3CBnM/UnKKoCug=; fh=TNDWXAd/8AdcB9hupKBERCip7LwESyWMEuspfogt6mo=; b=EYHlWsXUvxcw9d8Ksm+LrfSX8QrvB9lfXME5H1loTY4Zcm+SSGZZz4qVidoKv5Z/yG MPJt/OM8ZAk9ebYoly+YYmjoz4dL0rvOTB5bMrX7hjF7h9Tji42oZAZ3G5m2d1bGRwtu oQK9yTsM8/D3MxYlsJkB/62Pq5tKZP/GDPDcVwIse4S2C5SaEpB3qYAzIUWqxBLs4AhB VMgBnhkTaHjOhSlRRvhY2Ned+nSK11Fgl1BZkq2w3au0qsuHb9I3RPoa3kU/3Y+ifRl4 tP4yd1AN5sO0Wxh/DwUFYx43NI9tIFdgq2rNgKEUlHvtEbsC+xTwrhlnNLB7CISiU9BZ h/xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=tT7fRR6m; spf=pass (google.com: domain of linux-kernel+bounces-15891-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15891-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id g7-20020a17090a300700b0028c03c9a3c9si1658852pjb.24.2024.01.03.10.56.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 10:56:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-15891-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=tT7fRR6m; spf=pass (google.com: domain of linux-kernel+bounces-15891-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15891-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 340C2B22DFB for ; Wed, 3 Jan 2024 18:54:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E8D441CAB2; Wed, 3 Jan 2024 18:54:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tT7fRR6m" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C13D81CA8E for ; Wed, 3 Jan 2024 18:54:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--brho.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dbea05a6de5so943598276.3 for ; Wed, 03 Jan 2024 10:54:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704308046; x=1704912846; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ZUt8YVNSoa+7YDD+I3FZ435kHRVdX3CBnM/UnKKoCug=; b=tT7fRR6mXoL4uKMhMIRebk7jHR/8ufeUpvq/dNbu4EEarqzPixDtxJIDIyn6uL0561 VRrS5VxG3AiG9ntj4mr+wwDLdKiyk/eerGGgF0HpWfmWqX82rToVNxvqNUZvC8U5BwKN YDcbQEiAC+q/QXoNRFbg+slA47QAlRgaAJI+rsqwjxkx0dRQsjAtc/9hsa44w0/VAoBx 2ske0ZOLY2S/Q6yTNL56Ve7TKZh6NZPOKZKTyNdWh+DnNCPb7jmf6Fi/xL9KqE4CWt9L FsFe+dd+RJVK/HGwzeAn1vtBM7oEGinaOeoalC33PtY8W0jhpQoDmzbQEs5i8G9WvLXg KVug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704308046; x=1704912846; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZUt8YVNSoa+7YDD+I3FZ435kHRVdX3CBnM/UnKKoCug=; b=VoCjuklecPs8DQewJoaysC98AAto84APkteOTCxY0APgta5rp7aNunSqvRPqIaF0XA 9szIDFuwnlmr3CJ1kl2lugPGAzIZ7s4ujrxAHST/UaDP2qrqPxxoNNJ4g/NBX4VQgh0l fud7ZZM80tBM68GwtWdiTuDNPhT3nJytbnnwhNKxXoMiARlEdQu5Gccgisp6HhfOYD9N hXjpZonGiHdCUnvl6k/zo6RgylVihjIDY8yvHOr3TgyQVAkZGxCEIw5OEZHD52IP2wK8 YUMHEXngfJACM+CJTYUVSyRByaEVE4LsqJ1WmJcQ2uwD68KDoTL480olkY8Qi/Uj887o 63bQ== X-Gm-Message-State: AOJu0Yybsb+IAi9ozue0N5DInbkveMC6oMSk8/PuX0LYL6Hxi1/ZyewA BwGDO0czqkKSQe3HrVUnb4yr8XAFH9EV33Y= X-Received: from gnomeregan.cam.corp.google.com ([2620:15c:93:4:7e71:cfbd:2031:cc52]) (user=brho job=sendgmr) by 2002:a25:b30d:0:b0:dbe:a677:5de5 with SMTP id l13-20020a25b30d000000b00dbea6775de5mr28285ybj.4.1704308046693; Wed, 03 Jan 2024 10:54:06 -0800 (PST) Date: Wed, 3 Jan 2024 13:53:58 -0500 In-Reply-To: <20240103185403.610641-1-brho@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240103185403.610641-1-brho@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20240103185403.610641-2-brho@google.com> Subject: [PATCH v2 bpf-next 1/2] libbpf: add helpers for mmapping maps From: Barret Rhoden To: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Song Liu , Yonghong Song Cc: mattbobrowski@google.com, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" bpf_map__mmap_size() was internal to bpftool. Use that to make wrappers for mmap and munmap. Signed-off-by: Barret Rhoden --- tools/bpf/bpftool/gen.c | 16 +++------------- tools/lib/bpf/libbpf.c | 18 ++++++++++++++++++ tools/lib/bpf/libbpf.h | 6 ++++++ tools/lib/bpf/libbpf.map | 4 ++++ 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index ee3ce2b8000d..a328e960c141 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -453,16 +453,6 @@ static void print_hex(const char *data, int data_sz) } } -static size_t bpf_map_mmap_sz(const struct bpf_map *map) -{ - long page_sz = sysconf(_SC_PAGE_SIZE); - size_t map_sz; - - map_sz = (size_t)roundup(bpf_map__value_size(map), 8) * bpf_map__max_entries(map); - map_sz = roundup(map_sz, page_sz); - return map_sz; -} - /* Emit type size asserts for all top-level fields in memory-mapped internal maps. */ static void codegen_asserts(struct bpf_object *obj, const char *obj_name) { @@ -641,7 +631,7 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name) if (bpf_map__is_internal(map) && (bpf_map__map_flags(map) & BPF_F_MMAPABLE)) printf("\tskel_free_map_data(skel->%1$s, skel->maps.%1$s.initial_value, %2$zd);\n", - ident, bpf_map_mmap_sz(map)); + ident, bpf_map__mmap_size(map)); codegen("\ \n\ skel_closenz(skel->maps.%1$s.map_fd); \n\ @@ -723,7 +713,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h goto cleanup; \n\ skel->maps.%1$s.initial_value = (__u64) (long) skel->%1$s;\n\ } \n\ - ", ident, bpf_map_mmap_sz(map)); + ", ident, bpf_map__mmap_size(map)); } codegen("\ \n\ @@ -780,7 +770,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h if (!skel->%1$s) \n\ return -ENOMEM; \n\ ", - ident, bpf_map_mmap_sz(map), mmap_flags); + ident, bpf_map__mmap_size(map), mmap_flags); } codegen("\ \n\ diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index ebcfb2147fbd..2dae6fabc0d1 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -9830,6 +9830,24 @@ void *bpf_map__initial_value(struct bpf_map *map, size_t *psize) return map->mmaped; } +size_t bpf_map__mmap_size(const struct bpf_map *map) +{ + return bpf_map_mmap_sz(bpf_map__value_size(map), + bpf_map__max_entries(map)); +} + +void *bpf_map__mmap(const struct bpf_map *map) +{ + return mmap(NULL, bpf_map__mmap_size(map), + PROT_READ | PROT_WRITE, MAP_SHARED, + bpf_map__fd(map), 0); +} + +int bpf_map__munmap(const struct bpf_map *map, void *addr) +{ + return munmap(addr, bpf_map__mmap_size(map)); +} + bool bpf_map__is_internal(const struct bpf_map *map) { return map->libbpf_type != LIBBPF_MAP_UNSPEC; diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 6cd9c501624f..148f4c783ca7 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -996,6 +996,12 @@ LIBBPF_API int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra); LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map, const void *data, size_t size); LIBBPF_API void *bpf_map__initial_value(struct bpf_map *map, size_t *psize); +/* get the mmappable size of the map */ +LIBBPF_API size_t bpf_map__mmap_size(const struct bpf_map *map); +/* mmap the map */ +LIBBPF_API void *bpf_map__mmap(const struct bpf_map *map); +/* munmap the map at addr */ +LIBBPF_API int bpf_map__munmap(const struct bpf_map *map, void *addr); /** * @brief **bpf_map__is_internal()** tells the caller whether or not the diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 91c5aef7dae7..9e44de4fbf39 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -411,4 +411,8 @@ LIBBPF_1.3.0 { } LIBBPF_1.2.0; LIBBPF_1.4.0 { + global: + bpf_map__mmap_size; + bpf_map__mmap; + bpf_map__munmap; } LIBBPF_1.3.0; -- 2.43.0.472.g3155946c3a-goog