From patchwork Fri May 24 04:10:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13672708 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 CA765C25B7D for ; Fri, 24 May 2024 04:11:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 572E36B00A4; Fri, 24 May 2024 00:11:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 521676B00A7; Fri, 24 May 2024 00:11:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 320526B00A9; Fri, 24 May 2024 00:11:11 -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 EF43B6B00A4 for ; Fri, 24 May 2024 00:11:10 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id AADBC1A0812 for ; Fri, 24 May 2024 04:11:10 +0000 (UTC) X-FDA: 82151964300.27.0DD0ED2 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf03.hostedemail.com (Postfix) with ESMTP id 5483820005 for ; Fri, 24 May 2024 04:11:07 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=YpJYaM9I; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf03.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=1716523869; 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=CMlgdg2wI1o5bNQm04VXZUvOTRDigZ/HVGjsworxyJA=; b=oQwh7qfvokEgg84Sfk7EjmUn8e2/ZapA9tRFeWMTMPedFWl4bQkCjpMRUV+AWc36oG0kKu dZmAW3iujGvtwYPqzz9rAB9fAkeY6reKdr1dHpQHE0fGIx74jEhY+Ygw6B8TinvMymvAWy ON7INprz1XOgYipwVeykQe4FhwQrSdk= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=YpJYaM9I; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf03.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=1716523869; a=rsa-sha256; cv=none; b=zyKU1qzHMzr3otdVgxQ7/chMM36WWSR+EY3yl0pL/m/kkZ6pcoBxNGKf65hVnOoHkIxHlB rNWLaCGQYeKPlIBWXNBxzMonceyoY50W2fNicsEkMlAxsTs/ZKy+8dteT/NLpJE8v9xHl6 hgoTW+DqDF1hP9AxtlRKxEL/wO/YoiY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 4741FCE18AA; Fri, 24 May 2024 04:11:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 29472C2BBFC; Fri, 24 May 2024 04:11:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716523864; bh=6bfQltsebIDFmP7fT8/qzCkTKSF6gxCrj2vhm2ulkGA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YpJYaM9ItxngngnWu3WQYKhMETGTuyVu7cqMlSXA4Vi0r6bdf1YGIi/J+E72v/se9 F/Gc2e6q+k4mEAg/SGnIKFHlxEL90Lh+rAIWd5MOSLv6QiH6nWB9hDyc7FtWHTHW9I 27hDy5vmnGxzr2F/lC72vNvSmoi6hvstNk6S6XFtGGcWCaigBFanJIhUCru2cQGHW5 qWPrhzRcHo3lX2uEuK7l0mzofYgW4cnOPgun6f9FszL1/5yGQtDhZt1Y4rhVxTSYix 9eyrjiFpGRsEMvE7LraVjiW/aJDMXyoq8ri7H8/i9DTTarexZUXqvyXkfrxhgREAxL s7mFmkm4Vpm2A== 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 8/9] selftests/bpf: make use of PROCMAP_QUERY ioctl if available Date: Thu, 23 May 2024 21:10:30 -0700 Message-ID: <20240524041032.1048094-9-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240524041032.1048094-1-andrii@kernel.org> References: <20240524041032.1048094-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 5483820005 X-Stat-Signature: e73orbbka6yyeqe85gd3bddi615ynf6i X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1716523867-185164 X-HE-Meta: U2FsdGVkX1/D7BsU5XNVU8kzQS/njQf9ArVf+N4g5p4mey0MBwux8AU59PS7yqRGTah+RH48B9ZgDDM9laTvxzA6Yx8AsLBA0HkEDdB8ZZ7OasBoAJnYL/3SCHs+Z1895i4X0tS6/VdfVrGaj1o1PG0Qivq3Q1/jEYrf2JvHmQZvBo2+Jgbh1VHSTxeGrD2PW47Riq54MNje8nIzFlLbDpA6S9FNpDsZuCWtUiRks/sfQDM76Ouk4ucca4BXTvXoBBx6WVipHsgxjqBEVtD4NQJOlZ5g0V/LvOccFfreruqcpeV4ZTxRHp/nzgkvTetyD8jlhvSXTBUcaP1OjEr6iRzEPH/h8lR9dm85xaPWJFhAjyY3JLLY6KnXTc6X47Sw2wRcViw7Z+W4EY5bof9pGX+jwHa0DRSwK+Qus2+w+dtpKlRhS5D1TfQnmUatVk9s/bvvSRjVQNrYHeOh8SblonVmjX3PKtNICKTA7XIWXsi+GZI//ZzDu/URBEk8hjFbJab1TblEPxqueHlDu9KmvP+kVx6Zp0GoK2hYA5idg+aMyTbzmA9co2mrPvEhk3NTzxel+l67EcVa/XHUq1boeJC4tI8KJ3QtLJChBWJnNO8LNgbC44mIOJIGh7fR2clRekHMRMwBudsZr8U4j4hZmWh2sFafC6iNRau6SUAEU1cquMXxLLkvLW0y/7NQUOktr0+pd/g2CGPEt5pm2hDpsGi/flbNK47aoHA4A2q5r/U2pYZZ03g4ouu/iy9yuYepxeUhSHl5eT3EkwkGJUTklmpiKmUL6E3wA4Jijf7/GF4Sulec0u2mZX5I1sp4XHhut7OVXZF0NI3udZ7I+Mwq5XtlAP/GkWfzgQ6g6tXACS3U9XZEQblteblb4nCWdB69wy3db7QSG8Sufzn/MYlqH6Z5cp6Yrubblz7c+nk7NpbE7Jj4nopFx5cPb05nvTCogWWmXCFcZyrAVnYaoFK PIEOewAb 3Mxp82awxseGzXNn2PMKSyv2ATdLr8nT8cugz+GjWL7tmAQS3Y1pWkzJL9zbTJTqXleeX6kF08Xr2OmWVQ3eU0F5EjLt8mrMIQBpbymtjqbXEmo4ttnKZSrHy2TfHX5iFJYiioSV68l7Mng96Hk7gmIuX/tmJpWqTeqGvlbBYrXn3dxpdgKDFQHwBqiEVjrxn3b1Wve6X3jbWaYCkfJTvMZHYJusCX757kqr1kdNz4zOaAl+X3L1aeiyxrYWMjUI5HLsa/QIlsF+55J1kqKM00zXbjFD237Jp99Db 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: Instead of parsing text-based /proc//maps file, try to use PROCMAP_QUERY ioctl() to simplify and speed up data fetching. This logic is used to do uprobe file offset calculation, so any bugs in this logic would manifest as failing uprobe BPF selftests. This also serves as a simple demonstration of one of the intended uses. Signed-off-by: Andrii Nakryiko --- tools/testing/selftests/bpf/test_progs.c | 3 + tools/testing/selftests/bpf/test_progs.h | 2 + tools/testing/selftests/bpf/trace_helpers.c | 104 +++++++++++++++++--- 3 files changed, 94 insertions(+), 15 deletions(-) diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c index 89ff704e9dad..6a19970f2531 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -19,6 +19,8 @@ #include #include "json_writer.h" +int env_verbosity = 0; + static bool verbose(void) { return env.verbosity > VERBOSE_NONE; @@ -848,6 +850,7 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state) return -EINVAL; } } + env_verbosity = env->verbosity; if (verbose()) { if (setenv("SELFTESTS_VERBOSE", "1", 1) == -1) { diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h index 0ba5a20b19ba..6eae7fdab0d7 100644 --- a/tools/testing/selftests/bpf/test_progs.h +++ b/tools/testing/selftests/bpf/test_progs.h @@ -95,6 +95,8 @@ struct test_state { FILE *stdout; }; +extern int env_verbosity; + struct test_env { struct test_selector test_selector; struct test_selector subtest_selector; diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c index 70e29f316fe7..6723186c46bb 100644 --- a/tools/testing/selftests/bpf/trace_helpers.c +++ b/tools/testing/selftests/bpf/trace_helpers.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include #include "trace_helpers.h" #include @@ -233,29 +235,91 @@ int kallsyms_find(const char *sym, unsigned long long *addr) return err; } +#ifdef PROCMAP_QUERY +int env_verbosity __weak = 0; + +int procmap_query(int fd, const void *addr, __u32 query_flags, size_t *start, size_t *offset, int *flags) +{ + char path_buf[PATH_MAX], build_id_buf[20]; + struct procmap_query q; + int err; + + memset(&q, 0, sizeof(q)); + q.size = sizeof(q); + q.query_flags = query_flags; + q.query_addr = (__u64)addr; + q.vma_name_addr = (__u64)path_buf; + q.vma_name_size = sizeof(path_buf); + q.build_id_addr = (__u64)build_id_buf; + q.build_id_size = sizeof(build_id_buf); + + err = ioctl(fd, PROCMAP_QUERY, &q); + if (err < 0) { + err = -errno; + if (err == -ENOTTY) + return -EOPNOTSUPP; /* ioctl() not implemented yet */ + if (err == -ENOENT) + return -ESRCH; /* vma not found */ + return err; + } + + if (env_verbosity >= 1) { + printf("VMA FOUND (addr %08lx): %08lx-%08lx %c%c%c%c %08lx %02x:%02x %ld %s (build ID: %s, %d bytes)\n", + (long)addr, (long)q.vma_start, (long)q.vma_end, + (q.vma_flags & PROCMAP_QUERY_VMA_READABLE) ? 'r' : '-', + (q.vma_flags & PROCMAP_QUERY_VMA_WRITABLE) ? 'w' : '-', + (q.vma_flags & PROCMAP_QUERY_VMA_EXECUTABLE) ? 'x' : '-', + (q.vma_flags & PROCMAP_QUERY_VMA_SHARED) ? 's' : 'p', + (long)q.vma_offset, q.dev_major, q.dev_minor, (long)q.inode, + q.vma_name_size ? path_buf : "", + q.build_id_size ? "YES" : "NO", + q.build_id_size); + } + + *start = q.vma_start; + *offset = q.vma_offset; + *flags = q.vma_flags; + return 0; +} +#else +int procmap_query(int fd, const void *addr, size_t *start, size_t *offset, int *flags) +{ + return -EOPNOTSUPP; +} +#endif + ssize_t get_uprobe_offset(const void *addr) { - size_t start, end, base; - char buf[256]; - bool found = false; + size_t start, base, end; FILE *f; + char buf[256]; + int err, flags; f = fopen("/proc/self/maps", "r"); if (!f) return -errno; - while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &base) == 4) { - if (buf[2] == 'x' && (uintptr_t)addr >= start && (uintptr_t)addr < end) { - found = true; - break; + /* requested executable VMA only */ + err = procmap_query(fileno(f), addr, PROCMAP_QUERY_VMA_EXECUTABLE, &start, &base, &flags); + if (err == -EOPNOTSUPP) { + bool found = false; + + while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &base) == 4) { + if (buf[2] == 'x' && (uintptr_t)addr >= start && (uintptr_t)addr < end) { + found = true; + break; + } + } + if (!found) { + fclose(f); + return -ESRCH; } + } else if (err) { + fclose(f); + return err; } - fclose(f); - if (!found) - return -ESRCH; - #if defined(__powerpc64__) && defined(_CALL_ELF) && _CALL_ELF == 2 #define OP_RT_RA_MASK 0xffff0000UL @@ -296,15 +360,25 @@ ssize_t get_rel_offset(uintptr_t addr) size_t start, end, offset; char buf[256]; FILE *f; + int err, flags; f = fopen("/proc/self/maps", "r"); if (!f) return -errno; - while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &offset) == 4) { - if (addr >= start && addr < end) { - fclose(f); - return (size_t)addr - start + offset; + err = procmap_query(fileno(f), (const void *)addr, 0, &start, &offset, &flags); + if (err == 0) { + fclose(f); + return (size_t)addr - start + offset; + } else if (err != -EOPNOTSUPP) { + fclose(f); + return err; + } else if (err) { + while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &offset) == 4) { + if (addr >= start && addr < end) { + fclose(f); + return (size_t)addr - start + offset; + } } }