From patchwork Tue Feb 6 22:04:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13547846 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36618C4828D for ; Tue, 6 Feb 2024 22:05:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B88C76B009D; Tue, 6 Feb 2024 17:05:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B12116B009E; Tue, 6 Feb 2024 17:05:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 964B16B009F; Tue, 6 Feb 2024 17:05:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 7CB306B009D for ; Tue, 6 Feb 2024 17:05:33 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 02A6CA15AA for ; Tue, 6 Feb 2024 22:05:31 +0000 (UTC) X-FDA: 81762761304.17.693CAE3 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf11.hostedemail.com (Postfix) with ESMTP id 286D240014 for ; Tue, 6 Feb 2024 22:05:29 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nkcIzgmb; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707257130; a=rsa-sha256; cv=none; b=HK9MK/VjtGSXa1TaWN+PTHodrEIkG8lkiWzk5E2kOJ6ocPuBmRUrJn78jaa2484vGXdl39 JRy/v2GguVh+9ZFhhQQqG+Q/HozQtKGAfAj0xF25RbNTGf9TAyXktfSZLe0pJmbJ06Lyys VcLPkTYXDVOPiX9r2AXpv/iZOvyX4jU= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nkcIzgmb; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707257130; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=RiXImkPg8/G5+2DOGxZ3xDCLE7/uYVswnoMrJHCmlE0=; b=c2FYNxrLTIqSP3nodjWyWIu4zGFUmVfSna8rouZ9UIkjPegH3th/T7POAQwnoBTQZUx5sa bfQ91bV1U32xREYROtxks5EbbSqUXYvvEOWjHFgDpMjZumWU7dBwwAwHodxF/vn/v5Mnh0 HgkA76FcNwqRfvLd+ZNzW3AD5FFYPIo= Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-6e054f674b3so12054b3a.1 for ; Tue, 06 Feb 2024 14:05:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707257129; x=1707861929; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RiXImkPg8/G5+2DOGxZ3xDCLE7/uYVswnoMrJHCmlE0=; b=nkcIzgmbnz3kk8yJMdggsemiuzxA1czEqmUoiqzPpCWK7ABoiuuruBYnlw3UL/jB8W 2IGeFIJDAsLL92P/Ere/X3J5Z0ykMG5lmGIKfQosgTM6v1K+vs2VGNUC/0MRM8wdeAhV dYBX6bviIYcF5g+rE3xbVW9eUal1kp0eZ0QUQTzh4cMlSWH4dqqGpvtizb171BQguJO/ ZX5UuaOGLsIHnC1v/z7TmADSUJrbeAboSdpuNOkXDIKHmN2TOY5M+KYciY3zvTqISjhj qPc8iELyFbv7XGMBU18h4mmz63NLaKoP4+J48AUlB4X4zfgvXa25BJAPV6maN+3RsyxW ckXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707257129; x=1707861929; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RiXImkPg8/G5+2DOGxZ3xDCLE7/uYVswnoMrJHCmlE0=; b=SUNohmpwtRAzQHL/LqhPoo2HJLHOmYY+060S37MrBF5EGUT6MEBm10H8XukLoGLcpy nVlP4I/MQBNxLp9JS8y9Be0rCY8GWDr5bealZ/9BNl9mTNQ6IUxsqGErn7kruSqH1CQ3 OcfGjDJsZSt9eXE3hq3xJF5iH03+VKv0wS0og+92melV4AcKL/dzsZPIKlq3PxvbncUr NktW9wlBsNQEX9EIne770VZHzZzlAvQ/9IDp1RXYM+rK2RlI9tZKP1WtM/6Di5jpT38l jzQA/XdhdoEPGye79sk/veO+fBgYqmndflryEkdh2fh1PHAutvy6YpU4il8UuB6o74GM i8Uw== X-Gm-Message-State: AOJu0YyjZjVf7XYexQoTPahAK/gN8FJzGAGM0OjklIFJjfrXGGmbwXVr 5miUSX/2NDfmB5hfU+Gte4wXeebjArpFymaHSr/HHBHj3jV3l076 X-Google-Smtp-Source: AGHT+IEIl4BNu5aTiVeud6z0/oB6Ycj6jmTdFR/O0kus8+9trSKYb2Mt0v8czJwLnLT6tYLaUsGdUQ== X-Received: by 2002:a62:c144:0:b0:6e0:4d28:b3a with SMTP id i65-20020a62c144000000b006e04d280b3amr813386pfg.21.1707257129006; Tue, 06 Feb 2024 14:05:29 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVDvb+r0mk8Q2LDEv9B9JMgXfjhY0opj4ilgwOds4APWie51YIRNJnQsC39i+blOZty5ZIcj+KLRBiH6UTqBDlQcNNR2QBOypbk1SEMOeJJvz51o7vEk16399WLfVIf0km+ypgZ7200F6hdBlSZEkCOCdwTvjt+Egj6l8giEFYADjPdyFEjDSXDrkCv67crZA5GJY52dcWfEwzqtvxfTXFf7LbRxgpF28eLdOexz/BcKOiXaX4VDefpgoWZqFjoHApuj3rGrSfywcJymCoRt8xCDNcKkAZJihbB Received: from localhost.localdomain ([2620:10d:c090:400::4:27bf]) by smtp.gmail.com with ESMTPSA id k138-20020a628490000000b006d0d90edd2csm2588957pfd.42.2024.02.06.14.05.27 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 06 Feb 2024 14:05:28 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, memxor@gmail.com, eddyz87@gmail.com, tj@kernel.org, brho@google.com, hannes@cmpxchg.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH bpf-next 11/16] libbpf: Add support for bpf_arena. Date: Tue, 6 Feb 2024 14:04:36 -0800 Message-Id: <20240206220441.38311-12-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240206220441.38311-1-alexei.starovoitov@gmail.com> References: <20240206220441.38311-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 286D240014 X-Stat-Signature: 75y8qutptpin5m6w7swiotwzxw1ccn6r X-HE-Tag: 1707257129-893632 X-HE-Meta: U2FsdGVkX1/5KN15f5VhdFbBk9hMa4v3DqpxI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IVrRd3mVrEDa+mdFYoIKP7qym/m1CtDiE5eEwKYA1o8UnwCnjQVdUSdmZyIelEPYqngthZO8BryDHEYyJitAKbmDQIiJZs1n9JkXLbFX34dubDr3iMDWOD4UqdFght93V/YO1bKF1A9qrmevCTzdzzdPD3z062JEr/uiIfic9eIMOCjKmz6K8izNLN0mQt5q0iU+LyovTYcgmwRc5ABx/cIKy0x1V+WukehOvfuYj/ujwohs60AoA3SzIU5yOWf0I4PmlA1PVhIu+AY/B19paBMBQ0j8RV6Lzr23/SeNSfD9TuiPEWDNqSDe05s32hfk0aUvsvG21Ha2W/iWnjZKID0NpAhTFWAirIm3hUFpsYOXd2y6nEXDmxPMf2R6Cbo+x+3sBhU9GlN0AU0DjlrkC6Xcc22/0rwHP2DFfGoCf4g4E4fpyMLqF57BXb/JU4MK8ZjcvNRz8TW6xx++1T+w X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Alexei Starovoitov mmap() bpf_arena right after creation, since the kernel needs to remember the address returned from mmap. This is user_vm_start. LLVM will generate bpf_arena_cast_user() instructions where necessary and JIT will add upper 32-bit of user_vm_start to such pointers. Use traditional map->value_size * map->max_entries to calculate mmap sz, though it's not the best fit. Also don't set BTF at bpf_arena creation time, since it doesn't support it. Signed-off-by: Alexei Starovoitov --- tools/lib/bpf/libbpf.c | 18 ++++++++++++++++++ tools/lib/bpf/libbpf_probes.c | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 01f407591a92..c5ce5946dc6d 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -185,6 +185,7 @@ static const char * const map_type_name[] = { [BPF_MAP_TYPE_BLOOM_FILTER] = "bloom_filter", [BPF_MAP_TYPE_USER_RINGBUF] = "user_ringbuf", [BPF_MAP_TYPE_CGRP_STORAGE] = "cgrp_storage", + [BPF_MAP_TYPE_ARENA] = "arena", }; static const char * const prog_type_name[] = { @@ -4852,6 +4853,7 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b case BPF_MAP_TYPE_SOCKHASH: case BPF_MAP_TYPE_QUEUE: case BPF_MAP_TYPE_STACK: + case BPF_MAP_TYPE_ARENA: create_attr.btf_fd = 0; create_attr.btf_key_type_id = 0; create_attr.btf_value_type_id = 0; @@ -4908,6 +4910,22 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b if (map->fd == map_fd) return 0; + if (def->type == BPF_MAP_TYPE_ARENA) { + size_t mmap_sz; + + mmap_sz = bpf_map_mmap_sz(def->value_size, def->max_entries); + map->mmaped = mmap((void *)map->map_extra, mmap_sz, PROT_READ | PROT_WRITE, + map->map_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED, + map_fd, 0); + if (map->mmaped == MAP_FAILED) { + err = -errno; + map->mmaped = NULL; + pr_warn("map '%s': failed to mmap bpf_arena: %d\n", + bpf_map__name(map), err); + return err; + } + } + /* Keep placeholder FD value but now point it to the BPF map object. * This way everything that relied on this map's FD (e.g., relocated * ldimm64 instructions) will stay valid and won't need adjustments. diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c index ee9b1dbea9eb..cbc7f4c09060 100644 --- a/tools/lib/bpf/libbpf_probes.c +++ b/tools/lib/bpf/libbpf_probes.c @@ -338,6 +338,12 @@ static int probe_map_create(enum bpf_map_type map_type) key_size = 0; max_entries = 1; break; + case BPF_MAP_TYPE_ARENA: + key_size = sizeof(__u64); + value_size = sizeof(__u64); + opts.map_extra = 0; /* can mmap() at any address */ + opts.map_flags = BPF_F_MMAPABLE; + break; case BPF_MAP_TYPE_HASH: case BPF_MAP_TYPE_ARRAY: case BPF_MAP_TYPE_PROG_ARRAY: