From patchwork Tue Jun 11 11:00:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13693427 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 BF4C3C27C5E for ; Tue, 11 Jun 2024 11:01:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 487EC6B00A4; Tue, 11 Jun 2024 07:01:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4115F6B00AA; Tue, 11 Jun 2024 07:01:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 28B086B00AB; Tue, 11 Jun 2024 07:01:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 058B56B00A4 for ; Tue, 11 Jun 2024 07:01:07 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 862B61C1FD8 for ; Tue, 11 Jun 2024 11:01:07 +0000 (UTC) X-FDA: 82218315774.26.7FE0E0C Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf27.hostedemail.com (Postfix) with ESMTP id 0200D40026 for ; Tue, 11 Jun 2024 11:01:04 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="Tv3/s18L"; spf=pass (imf27.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718103665; a=rsa-sha256; cv=none; b=VoDE3+VKgx/Mzt2rR6EMTCUvQ/O1Mo/Jjro0uphaGr+noDiumtzNC82TTvISt8tgpccUif yZxcouQb6UaX8il91IDvejuE9QPD7/smER4zUmK3LXROfyagiuBSK345OIe7OY2eHgXF5r 7J+cwBUjcBEpsn9aE71BHso5/PGJiXQ= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="Tv3/s18L"; spf=pass (imf27.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718103665; 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=OFbmsdTUOHX9IQCV/QXAjEe312tTk0ZM7VGHrP8nWmQ=; b=G9vvWKg2sdeaPE9U23ly+WAXS6dwFEFMahQyDIh2Xk+6VJE5YmMqcjWkJhTZZRiM91zJkH IssT4b9bo+eR+5TlzRWyn1ixwNnl+0jb96C/Eeu+BK7ivNCzI+3Vgs+SXB1XX6ahKFNxXX dExlZV2fXjqg2bU/fY/C586jDNUAm1s= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 7AB77CE1B00; Tue, 11 Jun 2024 11:01:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F7D7C2BD10; Tue, 11 Jun 2024 11:01:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718103660; bh=ZDpFbMbmPb8D4u9/TrmtWz/aLsgRff7lvDoIhL0sAI8=; h=From:To:Cc:Subject:Date:From; b=Tv3/s18LudvwIL9D25sC9XsLoUNpu+a6i2TvsOE96tnbrV1r7hZsBtLlPKplW5k6V vki3WfGT5E/xVFtEFUArfq+o6+HBCf7S1UQ9gCuXVT9Cr4gLjS4PwdZA3SGjNf+YQx P6E2WTIRnEJdlTmq58jmq42Xc/Mbyj/BQS9iiiPz1gpUBoPJ+kphCHb/orrmYeInKv /PKp1sLV9Wgw1DJta+9RWdqf+5pjgatplTn5BkTSdlv/SlYLvOIne+lfUllLo53DRq 3jNDQg4Jms4aU6eU8IxhCoAjSUoX4fS4ZOTJ9rdi1VF6WKugbPOjQukh2DTHU6uF6E LxDCjBxZW6uDw== 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 v4 0/7] ioctl()-based API to query VMAs from /proc//maps Date: Tue, 11 Jun 2024 04:00:48 -0700 Message-ID: <20240611110058.3444968-1-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Rspamd-Queue-Id: 0200D40026 X-Rspam-User: X-Rspamd-Server: rspam12 X-Stat-Signature: d374maph1mtgsz3s3cdhz66rk46exe7q X-HE-Tag: 1718103664-13995 X-HE-Meta: U2FsdGVkX18TasZFnf1fMeKlggeMge6Fsoe+AUMyE9dAt+KDAuvfJ2hT5RezWESwDI3n5Hd8IqrcFi1pJg/9FyTAKsw/T2eFXum6oBYpXXw0zPVK3iIkFSKAfVX60UXIngUZnOh5zcX3FuS/vh4WC9tHHkz5CeAulx0KDE8m8j7Vh3qn1EgTTTiAcOKqLKDNli8pC8D+j4AI9MXhqQcR0cJ+P69JNcHjuhFFXslrCB3a+T4+IwpUMpUQa3R9xWe7HlYZefXfVmrIEwxPag+OfzO8zXsneB5+Jt9JQkcilijHCRakEviZJ9LIKa5rkAflrZ5khtrKq/8pVn8EkOMRhV9zXF+GJEbJ/dLX7Mh257ppRtUJpp/affQ6K6JFt8WEVFVo5eHYJUM0KDXCLUNMx+xWjpMVFogkmFB1EMMzWko20nhnTXy64d2+FAi3+yKlCqJUDtvfddv8tOMUH2SWk6kXs3nESPB97EtT5hytyNifaQV5FNTuT/zr4H8CeG1IPwV8NTReKNVoYrBA45LnlDWFbxRGiTpqBAFsXdUdkbIDs53dpTUoZmSiRTOVAdc2seXTKPIo5lsXNJoOzMitY0TcFaYLg8j+sHutaGNtcSt3KoVys66F4StKNEaGTNBGnX6/arAwfyGugxr1nkHWKqRsgGsxsPR+2YdI231avc2mMUDTxp201LiaqmDQEAXz0g3eS4yQyyJAGYW6ywF5QFj1CDTVmwaXH8z18QonQ1PkX6/OMl2g+Eo6YOaSpKPbQAnpGcgjmP98yrttKaJnHKUbr/fIqXkFmwq0sQUg7XGpi1PhW03eRhPZqSzbmG81ARHBGfHgoQEkrMr4lukkNCFDoGV8/ABEEMDSFnYXgLQ1USER1oS/hHlWQeir6//8h7gZPtLwMe7AUclqgPJjIi97B0yiId1IRd0uAGs7ssWFQkBX2yMscNBQ+oyZEgZDXTKbjxt7wegRQAV7WL5 ag+Naxuw zNRWun4E2UzejXLLMGu+Df2baP/1JyyybbYcA/QDD3jlzQXzEVGUCQMU744ww8UBLOr1oNM21c6WLfKsaqZVsdIvsmeWzcOz7TVZ6qaaf2d01taNWinE2rAe5TniPNcDpw5kf6XBGrBqZvqcCFHW8cC42pzQzq2rqdISu//vSpqrLZJq2lZDmc5cr9yvzjrOsO0FJiYnDb1l00xFNtEin8UyLp+Z3odpTSGw35HXVog9T54bZJ68FBWk7gPNQOIo6iM9unqX9OW00R8Bzpz+HxU3gP3JJb8L/2u/3ajUqIPl9dWY= 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). Earlier revisions attempted to use per-VMA locking, if kernel was compiled with CONFIG_PER_VMA_LOCK=y, but it turned out that anon_vma_name() is not yet compatible with per-VMA locking and assumes mmap_lock to be taken, which makes the use of per-VMA locking for this API premature. It was agreed ([2]) to continue for now with just mmap_lock, but the code structure is such that it should be easy to add per-VMA locking support once all the pieces are ready. 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-20240611 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 [2] https://lore.kernel.org/bpf/7rm3izyq2vjp5evdjc7c6z4crdd3oerpiknumdnmmemwyiwx7t@hleldw7iozi3/ v3->v4: - drop per-VMA locking changes for now, we'll need anon_vma_name() to be compatible with vma->vm_lock approach (Suren, Liam); 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 (7): fs/procfs: extract logic for getting VMA name constituents fs/procfs: implement efficient VMA querying API for /proc//maps 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 | 9 + fs/proc/task_mmu.c | 366 +++++++++++-- include/uapi/linux/fs.h | 156 +++++- 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 +++- 10 files changed, 1508 insertions(+), 71 deletions(-) create mode 100644 tools/include/uapi/linux/fs.h create mode 100644 tools/testing/selftests/bpf/procfs_query.c