From patchwork Fri May 24 04:10:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13672699 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 EC367C25B74 for ; Fri, 24 May 2024 04:10:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8305C6B008A; Fri, 24 May 2024 00:10:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7ECC26B008C; Fri, 24 May 2024 00:10:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6A74F6B0092; Fri, 24 May 2024 00:10:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 5020F6B008A for ; Fri, 24 May 2024 00:10:41 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E9B95C0483 for ; Fri, 24 May 2024 04:10:40 +0000 (UTC) X-FDA: 82151963040.04.620F6DA Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf20.hostedemail.com (Postfix) with ESMTP id 45E4B1C0002 for ; Fri, 24 May 2024 04:10:39 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nLzaZ7cU; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf20.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 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=1716523839; 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=axpjV1p7b3+D3/+Am0mWs+2q5ViLgLNRPeq7brW4T6I=; b=Oi/OdieSZ5E5CzPHwgjHL5gwGn9MI+mqoCS2EgzfEnPhXqmleVqN8T4eiZrrmJPGo+SL6Q /Si9mZohWhe8pd1SBmD8IJ/YKGPzyI4ySGZAfMN3/6WngoZzqoLqicnzeLXcw+uZOdj+MC RhaTmzHDS2s1rIP72Ruvura1zfCQm5E= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nLzaZ7cU; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf20.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=andrii@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716523839; a=rsa-sha256; cv=none; b=tcGmyoCYV3ggs+UMJZmGrJ5lBqxZWkCN//wdo0fpIVeGuy3szkmJul1guQuYTNR8VgRcyl TqlEAAcuQQU3gI3rMOptReMDYNjhnE7S+kEW+8HjC8P/9eln9jk6jJN9GePvSnZEf3ueVT ik4LiN7upGIPs1u4HArSpwj3PsYq734= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 33BD563023; Fri, 24 May 2024 04:10:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AED6AC2BBFC; Fri, 24 May 2024 04:10:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716523837; bh=nzpWsKa1pd1N9vI62wH3h8UJthDcr+2Eh8LpOgwa7G8=; h=From:To:Cc:Subject:Date:From; b=nLzaZ7cUird5WxdQt5e2VVpw8JxrJVCy0syUHZfzvWwsWMH8CwA5+aoZafhW6ugl2 oGvpcKcOTsM8wbSCrvwm5l7ItE2dfthG0oG/eZ+hhYi/TlOwL1WYcRYGdMNgSsEKAh pQGmOzuz/2acwyF29ct6CxP1NdGpL7ROM/CMJTeRsLLJ+VXNPeiZdJFon/+b2IN4pf FRyzs0n/azPekZX1P2NFd90e6UQ4/3pu4QvdIvF838XDxyap6VnMSSO/WHsmtBUIZ9 Z9KRc/pr1ETkTytpRG6trsRrw+aJn7ImLeDz5ZJb7XgqXR8++MYATc8oljBE8DQtwq lfLE3EfKLog9A== 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 v2 0/9] ioctl()-based API to query VMAs from /proc//maps Date: Thu, 23 May 2024 21:10:22 -0700 Message-ID: <20240524041032.1048094-1-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Rspamd-Queue-Id: 45E4B1C0002 X-Stat-Signature: zw1bh4exuec3e716jmrsfcn86fttkf74 X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1716523839-115463 X-HE-Meta: U2FsdGVkX195FILD/bZrFndHOAE+gFeLsIJ1yPwEws1Y0PChqI0KNGR27C1ucsFrhTWl0gnTwzgVJ1DhjNhHX8LlaOuP3HPfYodsIMDLz2ygRBBdkrJj1eusOoYD+ztgOKRVAzJs5dwxsM/2Sx0PLVy6ZuUxwFht2p+ZMKG41Uxf16WuLywq2ul9RWDIEmvReLqGiJKgSs176f3fdWpeyTQCAzdJV/8WYjo29mSJ4aYWT6LB4t3sJDLwPbo5jfXvVexAh6fkbtMFSvKDBlR8VfTkrFnGPIRfXfUkozd8I4RvmkA6kStNip+KvzgiZ87XvgHtD/B367oMJhDyjsNRg+iCB+8hEFM1NIBViilYfgjPRNgkz3TwMSfTiFHIqssNOe2DDX5Ufz/E+Vq7EXbO+9W4kHrBN+BRLp21C8gZ4kC63YvUCxO8ubTAFnykcSkI9NRwnVo29yUFJ9n5h9YOgCbOcLKM8TTRG8mAzyEWffwAmh0IGF1mhJPxZMjURc1qWQpkLG/SO2vBF80I7F2r5OKTaXDQHQQaepMxrwHZlyxL7cVVznY3R2fy7xz2LLkgQ0GjC+VFOHPmie/6kCpHLa2iAVHzXhYH2M1LaQ/LLjyZYsRWWiVgQ5a8YzwSzN+RLoyd1sYdVVyrJUv44BYOkVsc8FeNvQk3uGqfdsIbeQpaBuK12lrwnKDd8nM60hDSmiuaJF1A4IFpfdxb/nPiu8t2rSHKgS7JlXWPiyY1+Wdl1O16e5wiEWqzRmBkfJSZ/RwjHPzUtkrFllwJLzhX6Z8oklerlDnoGHXWfogjVyAT2tIfj4uHRECOWabdkE6vFk43yJk9dOKbdMh44bqbM8+zK06cC+7W2RHub3Zz2Z718VWrO7GnNLmWoDzQ+PiCz5G4qGs2W8aQiIL7DPGihXe9jBBRyGAIS6x1+J79Q9hzizG+DSfJtYPbuGBXD9hI0/OEs/brbfF2dhk4uHI mS09Tuit A29XX8Po9PZdIN584YiTIapxlreq8auMgSSLKiKg7gjDZ5i50qxKA+DWgSV4vBblv9o7C2E8oFOnLSD4HAySi4C449JUzMzS/o3UZPhy94QwpJlXHMpf1MNCHQHHcNh3hJp9P9Mqw8l534Wh6bzB1KdoU4AgCbYcTvbIiU1tq3sl7mGljN1m64N6fQfUBvuxg8URy+EyfjsrVO1ucpdHyp8k+M1WMPxoWmDMuWDwWITnS1nA= 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-20240522 tag in linux-next tree. [0] https://github.com/anakryiko/linux/commits/procfs-proc-maps-ioctl-v2/ [1] https://github.com/libbpf/blazesym/pull/675 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 | 378 ++++++++++++-- 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, 1589 insertions(+), 71 deletions(-) create mode 100644 tools/include/uapi/linux/fs.h create mode 100644 tools/testing/selftests/bpf/procfs_query.c