From patchwork Wed Jun 5 00:24:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13686016 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 651D0C27C54 for ; Wed, 5 Jun 2024 00:25:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E17A86B0092; Tue, 4 Jun 2024 20:25:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DC7556B0093; Tue, 4 Jun 2024 20:25:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C40F26B0095; Tue, 4 Jun 2024 20:25:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id A137F6B0092 for ; Tue, 4 Jun 2024 20:25:18 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4099240A00 for ; Wed, 5 Jun 2024 00:25:18 +0000 (UTC) X-FDA: 82194940716.26.46339DC Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf08.hostedemail.com (Postfix) with ESMTP id E9513160006 for ; Wed, 5 Jun 2024 00:25:15 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=o0VFPNx5; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf08.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717547116; 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-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=8HcxSeutbO1pdNw49sHRiuHs9tsaS36JPKxcOp1tkYs=; b=T/e6lU5erLTCCOBn6wy8ndRVGo5tFfezvmVvyLS1m+y4dl8azKehRHf9w4yucCRHVh96ZV S8ghHbq0f+ejJxsQDckfc9nZ+EcDc6g17mGbAcF+7KZ+0BTG1uSrilDuO/aNeA6KkEFBDC OMEISQ1RrEuCHBUvkFfeWCMI7B2MPY8= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=o0VFPNx5; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf08.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717547116; a=rsa-sha256; cv=none; b=HwlzAv+4Ivy+Wu2+qGGq9NMWuE/ruotDCPJ72LWiWmnJpd4eNoYVu3/gJR3+9/wspQ8MoA vXv0kjB+sHYziXLja5GhSGWegFXwpfaNJU9vxjzzLTVt8VzQ0XVR+DrnhrZ0G1nTtm7ku7 lNAWMkGZ73TYZeXTJbb91zqrRV7ou3A= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 889C9CE10F0; Wed, 5 Jun 2024 00:25:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 98006C2BBFC; Wed, 5 Jun 2024 00:25:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717547110; bh=xlav2H8FcbF2kBE50cB92+/q2AIJRTGhHwZ10uAGAG4=; h=From:To:Cc:Subject:Date:From; b=o0VFPNx5uU8IvyuM4VjLdFP0pBiJkpaqKiPk7LGlgG8wGJmNbNlADStRApdQww/DO QK3lw5/LKaWhPh4T+pV9+FChvxknLFEF3xY0jOJJBTkGV6rWnZfG+HaW/sfb7uduqZ TmCjk1iM7wHml61CozRdHxgmRwZY0ngUio8qCxSARYwICtbp7wb3y2ssyyd3Iv3H4d VBnNF+DkFyCT7k0CI/DZ71t64+57myU22schlsS+5fxDlN/8P33sBUqYqLsC1fPvWM KE1UK51MEAJXGlrsNR8hoJcyW9Rma7qExrMAB/YbqFMgb1LG4+VoLTWN01bKcT8LhU 7dit3wGiQuTDA== From: Andrii Nakryiko To: linux-fsdevel@vger.kernel.org, brauner@kernel.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, gregkh@linuxfoundation.org, linux-mm@kvack.org, liam.howlett@oracle.com, surenb@google.com, rppt@kernel.org, Andrii Nakryiko Subject: [PATCH v3 0/9] ioctl()-based API to query VMAs from /proc//maps Date: Tue, 4 Jun 2024 17:24:45 -0700 Message-ID: <20240605002459.4091285-1-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Rspamd-Queue-Id: E9513160006 X-Stat-Signature: 6sa5tq36r7fkeqj6hkz6egs3hcj7tsjx X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1717547115-932487 X-HE-Meta: U2FsdGVkX18OB1vl+OthI2U0gGfdh5g0KyopLmK5C1gEsGwQr/AmB7kZZ6BcIF3+ixgLwNxjZHtbFvt1Mgc+K7oDRASMHA4q0p/aZuR4rpUqBJl8bwcSbsFkn3/CMKfFvUbvy0DJRHX/rWkkgR1Lbun0c0WwzmYQI+gmh3cLLmxI9y6SyEAszhVxCBHi8aV0MmfdZhKnmp6pyav/uIfLc/N0BdK9350aKLl4kYXVZspClS2gQ2lCdtm7vCPJw/SSus3vTnkbQPzymqYpYpJhUIs3TcIlNVbnwCsxkG7yGB+4cG0l7m1C4qw/YR2e69jobylFTmyx68rDFl3giModmpXQfKs+A4scDpxbWc/pgpT1zmu4K6zLvIYi8fAm6Vkv5vXRBwnJirKXoP278B2TP+oMhh4O1jt2f2L5fsULhZWNAol8vtIe40GJlP7/jhOFFVOYwWZElelnWbeQQaG9+K9pZme7QP5vwvUeohy48ENeWOcuAqUult6Ohr+jnaUNaSWRh+Ea75VII/Ct5ZIEBynScC+Xo8K89DpLu5WSUtguVZVTJ+tRPQyfPpEtmzQ7GEiGVN0d6rmCfcmqwb1QcYFdVpwN7RCOqn3iGPmzUeNYfr+JEgjL3ALJ/UEMs4oqAXJ+NCEEJZgczBwYD0b8Xwnw8Ew4NW1iLHhbwOLj/PeNoal93VaCAMYUuCDRMA1xF5jcEdmTRnyKsxg06Tukcg+dF8evc4HOgI/n0O36LN43+nCTmzdO+Iuq+r/D4YY4I+RYy4sVLQ095PuKwkLEs77zyhK7Yt2Xxbss2EY3ErvN2rGMPly+tD3eF5q8/7M7YQU6pdPBm0Vy4qmSCadkRaEtrfmvnlVYH6wUQSTOqnIgwEksJ14Oscx3IVlza55hmziqm0ujFtPe7JrgkcqYRHMmxrPhVfdUcbU02phpk4n/k9sIj0F2QGi0LCHqplkiZf/U2wTia7eq3tOXLl0 cakOpFLj B1+jBEhV/SaZFdND6TP3hITUziX5FQJZgKRNuzlrTSCYr+Wr5wYhUBjjcW5xtDtUVSYKTm+AOPQa5OlaRvLEVp+C7oelInEuBSG9wnh7sDmuI6OOtiHRN5Zc8ohzz0UXV6PL4QKGSLM+HlkenUpAemvQ6hcXBV1shosY3qhdvdvX5Qbtz1x6PLcq52YXPQ1+q0iHYD2RYCwuyJFOwa0/jc0FIudG3VvZZWxvmHAXCIDZdZtuGFgsEl30a6nRIc2gpL/gSlw+r3MHIf1KeRZGv/TvE3RYJwuw2G9x5XVlzKq4FuBHXLIpyX+9cDI0vndaAaYxg 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: Implement binary ioctl()-based interface to /proc//maps file to allow applications to query VMA information more efficiently than reading *all* VMAs nonselectively through text-based interface of /proc//maps file. Patch #3 goes into a lot of details and background on some common patterns of using /proc//maps in the area of performance profiling and subsequent symbolization of captured stack traces. As mentioned in that patch, patterns of VMA querying can differ depending on specific use case, but can generally be grouped into two main categories: the need to query a small subset of VMAs covering a given batch of addresses, or reading/storing/caching all (typically, executable) VMAs upfront for later processing. The new PROCMAP_QUERY ioctl() API added in this patch set was motivated by the former pattern of usage. Patch #9 adds a tool that faithfully reproduces an efficient VMA matching pass of a symbolizer, collecting a subset of covering VMAs for a given set of addresses as efficiently as possible. This tool is serving both as a testing ground, as well as a benchmarking tool. It implements everything both for currently existing text-based /proc//maps interface, as well as for newly-added PROCMAP_QUERY ioctl(). But based on discussion on previous revision of this patch set, it turned out that this ioctl() API is competitive with highly-optimized text-based pre-processing pattern that perf tool is using. Based on perf discussion, this revision adds more flexibility in specifying a subset of VMAs that are of interest. Now it's possible to specify desired permissions of VMAs (e.g., request only executable ones) and/or restrict to only a subset of VMAs that have file backing. This further improves the efficiency when using this new API thanks to more selective (executable VMAs only) querying. In addition to a custom benchmarking tool from patch #9, and experimental perf integration (available at [0]), Daniel Mueller has since also implemented an experimental integration into blazesym (see [1]), a library used for stack trace symbolization by our server fleet-wide profiler and another on-device profiler agent that runs on weaker ARM devices. The latter ARM-based device profiler is especially sensitive to performance, and so we benchmarked and compared text-based /proc//maps solution to the equivalent one using PROCMAP_QUERY ioctl(). Results are very encouraging, giving us 5x improvement for end-to-end so-called "address normalization" pass, which is the part of the symbolization process that happens locally on ARM device, before being sent out for further heavier-weight processing on more powerful remote server. Note that this is not an artificial microbenchmark. It's a full end-to-end API call being measured with real-world data on real-world device. TEXT-BASED ========== Benchmarking main/normalize_process_no_build_ids_uncached_maps main/normalize_process_no_build_ids_uncached_maps time: [49.777 µs 49.982 µs 50.250 µs] IOCTL-BASED =========== Benchmarking main/normalize_process_no_build_ids_uncached_maps main/normalize_process_no_build_ids_uncached_maps time: [10.328 µs 10.391 µs 10.457 µs] change: [−79.453% −79.304% −79.166%] (p = 0.00 < 0.02) Performance has improved. You can see above that we see the drop from 50µs down to 10µs for exactly the same amount of work, with the same data and target process. Results for more synthentic benchmarks that hammer /proc//maps processing specifically can be found in patch #9. In short, we see about ~40x improvement with our custom benchmark tool (it varies depending on captured set of addresses, previous revision used a different set of captured addresses, giving about ~35x improvement). And even for perf-based benchmark it's on par or slightly ahead when using permission-based filtering (fetching only executable VMAs). Another big change since v1 is the use of RCU-protected per-VMA lock during querying, which is what has been requested by mm folks in favor of current mmap_lock-based protection used by /proc//maps text-based implementation. For that, we added a new internal API that is equivalent to find_vma(), see patch #1. One thing that did not change was basing this new API as an ioctl() command on /proc//maps file. An ioctl-based API on top of pidfd was considered, but has its own downsides. Implementing ioctl() directly on pidfd will cause access permission checks on every single ioctl(), which leads to performance concerns and potential spam of capable() audit messages. It also prevents a nice pattern, possible with /proc//maps, in which application opens /proc/self/maps FD (requiring no additional capabilities) and passed this FD to profiling agent for querying. To achieve similar pattern, a new file would have to be created from pidf just for VMA querying, which is considered to be inferior to just querying /proc//maps FD as proposed in current approach. These aspects were discussed in the hallway track at recent LSF/MM/BPF 2024 and sticking to procfs ioctl() was the final agreement we arrived at. This patch set is based on top of next-20240604 tag in linux-next tree. Note: currently there is a race when using RCU-protected per-VMA locking, which Liam is fixing. RFC patches can be found at [2]. This patch set can proceed in parallel with that solution. [0] https://github.com/anakryiko/linux/commits/procfs-proc-maps-ioctl-v2/ [1] https://github.com/libbpf/blazesym/pull/675 [2] https://lore.kernel.org/linux-mm/20240531163217.1584450-1-Liam.Howlett@oracle.com/ v2->v3: - drop mmap_lock aggressively under CONFIG_PER_VMA_LOCK (Liam); - code massaging to abstract per-VMA vs mmap_lock differences (Liam); v1->v2: - per-VMA lock is used, if possible (Liam, Suren); - added file-backed VMA querying (perf folks); - added permission-based VMA querying (perf folks); - split out build ID into separate patch (Suren); - better documented API, added mention of ioctl() into procfs docs (Greg). Andrii Nakryiko (9): mm: add find_vma()-like API but RCU protected and taking VMA lock fs/procfs: extract logic for getting VMA name constituents fs/procfs: implement efficient VMA querying API for /proc//maps fs/procfs: use per-VMA RCU-protected locking in PROCMAP_QUERY API fs/procfs: add build ID fetching to PROCMAP_QUERY API docs/procfs: call out ioctl()-based PROCMAP_QUERY command existence tools: sync uapi/linux/fs.h header into tools subdir selftests/bpf: make use of PROCMAP_QUERY ioctl if available selftests/bpf: add simple benchmark tool for /proc//maps APIs Documentation/filesystems/proc.rst | 8 + fs/proc/task_mmu.c | 412 +++++++++++++-- include/linux/mm.h | 8 + include/uapi/linux/fs.h | 156 +++++- mm/memory.c | 62 +++ tools/include/uapi/linux/fs.h | 550 ++++++++++++++++++++ tools/testing/selftests/bpf/.gitignore | 1 + tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/procfs_query.c | 386 ++++++++++++++ tools/testing/selftests/bpf/test_progs.c | 3 + tools/testing/selftests/bpf/test_progs.h | 2 + tools/testing/selftests/bpf/trace_helpers.c | 104 +++- 12 files changed, 1623 insertions(+), 71 deletions(-) create mode 100644 tools/include/uapi/linux/fs.h create mode 100644 tools/testing/selftests/bpf/procfs_query.c