From patchwork Wed Jul 24 22:52:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13741428 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 EA896C3DA63 for ; Wed, 24 Jul 2024 22:52:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 85DF56B0092; Wed, 24 Jul 2024 18:52:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 80CFD6B0093; Wed, 24 Jul 2024 18:52:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D5836B0095; Wed, 24 Jul 2024 18:52:20 -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 4DD4D6B0092 for ; Wed, 24 Jul 2024 18:52:20 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id BE1011603A1 for ; Wed, 24 Jul 2024 22:52:19 +0000 (UTC) X-FDA: 82376146398.16.FF5AF74 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf16.hostedemail.com (Postfix) with ESMTP id 8EAD7180004 for ; Wed, 24 Jul 2024 22:52:17 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Vd07yyYT; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf16.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=1721861484; 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:references:dkim-signature; bh=oaJDJ2bMVvGHWcmVOf3rr1m20N/j+261PuF6JTbTKf4=; b=GdXEXT4IoBhP7RxJoS2WU9My6MDgBr5lYhFIUZJClenFn+/L2kK2VAI/yCqkRMrEJhYCAu VPYuNIKRX583u5qKkPtYA6o2PdGqFVBhfDeN2azpUfdCsKnlBhEJ2Ig4wghj9C/4GeLS0y uJr8lxvocq/+MbadhxiiES6I5DwkVnM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721861484; a=rsa-sha256; cv=none; b=mLVNtDhzsmOQlMZcuz+Z64cIZ0J6zL8N8SenmRLQHyVig4xZKJ0PIsyJgpiOQjHwhFNnLh 3OQGYeTauHnOSIcKSAp6rr0Pa0aPzX8fGXPPayL2qJaQn9yvEjhCwbDEIYS9e4haQHL+fc A8pkUGzrkq9eXZEDDDSyxe7pyUMMwkM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Vd07yyYT; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf16.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 68EB1CE12C2; Wed, 24 Jul 2024 22:52:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E6D4C32781; Wed, 24 Jul 2024 22:52:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721861533; bh=XMp6rg0dpcHBxT1oYcemWawXRE/lV7nLw3KEvZZanRI=; h=From:To:Cc:Subject:Date:From; b=Vd07yyYTMdjVyw3Sxjb/zlpxFsCHH0mXooTgwejA7vZbI3DhLxTcSYzMGyD2bRLlG gfhnCAlUMyXzBFBH6rccDXxZPWlOrYBDtbKkH9WZgK+wO9Z/7MGNIW9WkIg8o/Yik1 vND1rY31utbmW+8npirM4+CFkFa0taX0eU+nrDBKlbdcsaIm3kgKhOJSpMDbOcyRYg tySrHmZXSER7boPvTq2RnYG2gBWWopR+i5vx+mmi5jg7Syxx/xSF7OaGC7bYm/L8e8 BZCYuyHdHyw9lW+fPkzgYUw2MSBRTELYn6JvW8QelTjuZSGds0xvzQsjg5G1QGkJ8w TAue2rqzOp+lQ== From: Andrii Nakryiko To: bpf@vger.kernel.org Cc: linux-mm@kvack.org, akpm@linux-foundation.org, adobriyan@gmail.com, shakeel.butt@linux.dev, hannes@cmpxchg.org, ak@linux.intel.com, osandov@osandov.com, song@kernel.org, Andrii Nakryiko Subject: [PATCH v2 bpf-next 00/10] Harden and extend ELF build ID parsing logic Date: Wed, 24 Jul 2024 15:52:00 -0700 Message-ID: <20240724225210.545423-1-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 8EAD7180004 X-Stat-Signature: oi41319pwybssejaga1xaa4wmjt5d6j4 X-Rspam-User: X-HE-Tag: 1721861537-467607 X-HE-Meta: U2FsdGVkX1/P9T7K1wDSY03O2wvUXDiGaIZbt4b7+qndhYnTlBFspXt6DZXyvzGU8xR1RfRx5RW2ZlSL1ENewJ/5MVJCQKw1s6OKh2wvTnGygv+0ERRfEHXlYFE4Okj7dmwgwmIokV6xfaEVJUE4Mgne1jJhx3cgpvMLrVZkmISc1IZKOq0MUrvqsLhT+5hk+8TBV18jguK3ow4i7LgkZGcOIhrgfHyroGL4rdfxxIQ63VLS8ZrbEtDKRPAeDQYlIOxIot6q/xJPCsnw8pE/bbmqWPHQDKe/MVjKLBehBYBWYm6VlWDKj0EIs9gleKoWiSLXC1xe0xO3yHDgsyQLcpRrfkxuoYc23LnexVW275yLzxzaBj5uafB719+D6t01MDS9dZ6dH0YmVdwbvSLDv9rTdCQ5izO/lh3Dw5BU1mieYTj/czMeFTYbTxlo43/fzHrJSaF1k0Y/WalJ4n9JKkVS9fSHVnzrHHBjWB6G1VJTNaUUeB0YKKi5qdYCJj4wZjp0kJH8ObNIiNSjKneIDdNrjuRdJvVtkxOmdgMWIsTCYz7X9s/IjGDJ3xFcliPUyoKoxfd4SxTBP9ZaJGorCGa7rl/xmMMYrLDgNgfe/vH1JFNeGc/duug+3NOs9NEyrE6RVi34GrW0T6UinMqxq7rZsRFT8rpKgyfaOmGnpHoyemBMWCK5gPvfChCdbCfA+8ArW2qwA9M9bmXxDJoolnCVuqK2cYS7tAjbxdVhxGW01kqYO7ajTlVtgpJM1XVWUkWHaRJw7OZnpI+vyAyhacn6/MJYdKiPWonn7xFnFSJ0GBSY3LJ89V7QuOuBVldR4WgzR8LX+5J4DAnF6caLx4PlRA9DhJrV0Ivme0Mo00jLdQOurD2cKkHMa5BWnE0cWa2XvOmj2hodUVsUyZETHevVnodpcaUQB+FMyAuYiXff+koNO8cqKGd7bYwVk9FHCYebvYJmt0WEEqacTiM Cd0E6IWi R+A7ElGynkph4G141pEKudcxCz7u1hDvOYHpbamgQg+QglPTjUuCUtPXmqVU3nDpNllMSuhSxtoh32b2HJRXQ8ZcrjGjuU7xoDY9gZwYvjYbfqMCFkgdbZS/f/sGFLpdZTHnjqCmZbbBum7m6jype+GWxfsqWBx3gOfswsNT0vKt8mG+xmneJD+IymQ9S5FV/lEUSzgX5m3wEZqrUcKin17R3EpTu/+EcPL58dpJ7ys0dLns= 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: The goal of this patch set is to extend existing ELF build ID parsing logic, currently mostly used by BPF subsystem, with support for working in sleepable mode in which memory faults are allowed and can be relied upon to fetch relevant parts of ELF file to find and fetch .note.gnu.build-id information. This is useful and important for BPF subsystem itself, but also for PROCMAP_QUERY ioctl(), built atop of /proc//maps functionality (see [0]), which makes use of the same build_id_parse() functionality. PROCMAP_QUERY is always called from sleepable user process context, so it doesn't have to suffer from current restrictions of build_id_parse() which are due to the NMI context assumption. Along the way, we harden the logic to avoid TOCTOU problems. We also lift existing limitations of only working as long as ELF program headers and build ID note section is contained strictly within the very first page of ELF file. We achieve all of the above without duplication of logic between sleepable and non-sleepable modes through freader abstraction that manages underlying page cache page (on demand) and giving a simple to use direct memory access interface. With that, single page restrictions and adding sleepable mode support is rather straightforward. We also extend existing set of BPF selftests with a few tests targeting build ID logic across sleepable and non-sleepabe contexts (we utilize sleepable and non-sleepable uprobes for that). [0] https://lore.kernel.org/linux-mm/20240627170900.1672542-4-andrii@kernel.org/ v1->v2: - ensure MADV_PAGEOUT works reliably by paging data in first (Shakeel); - to fix BPF CI build optionally define MADV_POPULATE_READ in selftest. Andrii Nakryiko (10): lib/buildid: add single page-based file reader abstraction lib/buildid: take into account e_phoff when fetching program headers lib/buildid: remove single-page limit for PHDR search lib/buildid: rename build_id_parse() into build_id_parse_nofault() lib/buildid: implement sleepable build_id_parse() API lib/buildid: don't limit .note.gnu.build-id to the first page in ELF lib/buildid: harden build ID parsing logic some more bpf: decouple stack_map_get_build_id_offset() from perf_callchain_entry bpf: wire up sleepable bpf_get_stack() and bpf_get_task_stack() helpers selftests/bpf: add build ID tests include/linux/bpf.h | 2 + include/linux/buildid.h | 4 +- kernel/bpf/stackmap.c | 131 +++++-- kernel/events/core.c | 2 +- kernel/trace/bpf_trace.c | 5 +- lib/buildid.c | 370 +++++++++++++----- tools/testing/selftests/bpf/Makefile | 5 +- .../selftests/bpf/prog_tests/build_id.c | 118 ++++++ .../selftests/bpf/progs/test_build_id.c | 31 ++ tools/testing/selftests/bpf/uprobe_multi.c | 41 ++ tools/testing/selftests/bpf/uprobe_multi.ld | 11 + 11 files changed, 591 insertions(+), 129 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/build_id.c create mode 100644 tools/testing/selftests/bpf/progs/test_build_id.c create mode 100644 tools/testing/selftests/bpf/uprobe_multi.ld