From patchwork Tue Jul 30 20:39:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13747864 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 D3792C3DA7F for ; Tue, 30 Jul 2024 20:39:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 698C06B0089; Tue, 30 Jul 2024 16:39:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 648B96B008A; Tue, 30 Jul 2024 16:39:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5101D6B008C; Tue, 30 Jul 2024 16:39:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 304E76B0089 for ; Tue, 30 Jul 2024 16:39:28 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id EB519A4932 for ; Tue, 30 Jul 2024 20:39:27 +0000 (UTC) X-FDA: 82397584374.04.83B781A Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf24.hostedemail.com (Postfix) with ESMTP id 835A118004F for ; Tue, 30 Jul 2024 20:39:25 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ZYVZiwdu; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf24.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=1722371962; a=rsa-sha256; cv=none; b=xNkArd+Klk5PEsriurz9YyOOVB7Nt5bk/OeiIi4mfjZBZjf0zAteNljwTMgA8YSUnDZjYS rEdV31IjxVEi5fvqU9z2uF0Fnx8OW6O/tC4JmyKjdRJf5aG3wG0tS6ha+VnxMptapwSgeK jQl+bfQm0ifQe2aFKd61ka0X3LuSMBU= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ZYVZiwdu; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf24.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=1722371962; 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=1nuYuZZfctNbf2pcIDlRhKEUbdpWkkw3ZjiuZl5LjjM=; b=7vrZ59lihu3xJp8Ji7B1aSKw4Tbb+6J7DCvvxhofwenoGAY90Wm9j7IKrOYhPpiMa185AP LQ/D5LJ5NBnO65E3V4qgS8fLfP34qrCfwfNTKMXEmX6e55ErRD1B9IwWuyM+nHhyRqZwln QKsoYr8Wzym0ocBvB3o4VC44csTgniI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id A9008CE1197; Tue, 30 Jul 2024 20:39:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C6F30C32782; Tue, 30 Jul 2024 20:39:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722371961; bh=ZRyyWIOIJMw57Vd+3odqRxOMUMVme1VBHFLkZo7b9vc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZYVZiwdu9FZhd7Cszk73xFZXTFp9z7q0D9Jb4gM2liZG8RTSeaBKLF/JfdS4rmEuO lJ/oObjBX08TbU4zWqTbW1L+Ux1FYemOFWSHWw6ai8DvlXdvUXW10wbDb/BsHh0+V/ sM2vFuK77AWUPzJmjeSS2KWIweCAIAPLkCIt7Xd9+xJ0mm+9yvJw66CgP66mQoSXcr WTJXklXl5GOBqey2p5ly2euAdlH3l72owAH7qaJJ3rFBOv3h2pgnWTA14wOi8logW9 2tnC5iWbCY3KwPibPVhI/3dNm81qxKntuy6CxlZ6XbBpTWBYI8FJx86gMqNYc9hQTy CfCA6W8i3poTw== 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, jannh@google.com, Andrii Nakryiko , stable@vger.kernel.org Subject: [PATCH v3 bpf-next 01/10] lib/buildid: harden build ID parsing logic Date: Tue, 30 Jul 2024 13:39:05 -0700 Message-ID: <20240730203914.1182569-2-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730203914.1182569-1-andrii@kernel.org> References: <20240730203914.1182569-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 835A118004F X-Rspamd-Server: rspam01 X-Stat-Signature: d746m5k5in6sf59nu5amx49xwc8rtkqw X-HE-Tag: 1722371965-419674 X-HE-Meta: U2FsdGVkX19WKjvgkYQRmyZ8VakCdSNEmJ6WR32IbXDEQ0QWqO4EyqSTjFcOZAYO+RgfVpZjGjIGPdaQ9mudv+5kIoYz02yQ4nAE5KMuDCqi5dttfw61BfpR/naiaec0JNVOichgyicgr9rLGQum+KSvMAKtdku5mGQ2rN9tOS/YajRrVZp5u5tMshks4PBl1FXuq6MLzMupfpZQDt7+bQ6XLa9Sg9UglN+pSfuII8YWfus5sqf3A5aEdKIOY2PGJe5e1qJIPpfBqKSJldfl8KxfJu9N1CZbjmWUh2hkqglazqrHOvtMWxssqaP8aFcE33N2kpJME2Iig/HBzF2GhdIkR82e6Z5mDXH2tz8tZbKrzmJBhWkCUIGEtL1iO/cjLDdevZiacO6MB9csTAVcc+Pmhp/stmhGxAh/7UIS40Czv3HkQoNIzTO6DGTCZpeUSStKEglo67dnHkioASykJfsBSWBGVXHTH/7QPBhfbimeaeSMb6fOgd3M+87WrVAFau1IPBNWdWzo1vKNMHRkXVYqD6dpacNIM0ZNmslVpQXoIgMxlt6hvcFKd1tYlPQegU1ZUdA82XzpZDs+j00G5PZzKcoM0PZEwzFwUcG0hhzFryIuTdubbPDdcvR1G+qyburN++aeGwAl3KUVPj/iJHY7Kg9BbBmF4mCpKcklIYkgWntowhFpZUpVFAiKYpRxL4dwjGixnpf/PwETtGUtJ/h6kVf8JnOvAkmkeIDqsLiq8hnA9ZVdlqNFDi4ZIjR7XGuzRPZstcr9TZmaMf28FZ4dJk5gRBDqH7FhFwLqMHoMk6iZNqg8NROFzxM2pQwxsnpH3J5HRvqCKlhIrJPUfTQzGkkjWydF/IeVSJmw4n0RsLolos+jlIS6zV4876KEA81bqaSQsY4zjbhmC23rjNH0b8ezRQtn7nDYKIIaBG8TjBXHm3xa10KfjNWavZxvaChT0bnAJxZwJTiV16W 3RAjysEj trovksBtNMgVh6DXgOhfhxe1Y2/G9oqfJY1zBnWYoQu8HASaG+69rihuJYF29PuGvzRTnDsRFjxEhGlvuvy6zGeYEeTxJWT1XTkDeatXi2EuubKWai3mMsGM8jQbMY5TNhIhGzm5vX8hglmBCWjgM++/SYbX4Io6r+UjefC2uXbeoqqJxJSYN3zIAyHEMJibzj1v3qCSFQcu3eZGz047auVIE+wnw/1TzxRDLFIoh4avYXq0gjJwuX6+7pKrYXtgs/544+Q3k6+muljzBNqiaWMETbw== 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: Harden build ID parsing logic, adding explicit READ_ONCE() where it's important to have a consistent value read and validated just once. Fixes tag below points to the code that moved this code into lib/buildid.c, and then subsequently was used in perf subsystem, making this code exposed to perf_event_open() users in v5.12+. Cc: stable@vger.kernel.org Cc: Jann Horn Suggested-by: Andi Kleen Fixes: bd7525dacd7e ("bpf: Move stack_map_get_build_id into lib") Signed-off-by: Andrii Nakryiko --- lib/buildid.c | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index e02b5507418b..d21d86f6c19a 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -18,28 +18,29 @@ static int parse_build_id_buf(unsigned char *build_id, const void *note_start, Elf32_Word note_size) { + const char note_name[] = "GNU"; + const size_t note_name_sz = sizeof(note_name); Elf32_Word note_offs = 0, new_offs; + u32 name_sz, desc_sz; + const char *data; while (note_offs + sizeof(Elf32_Nhdr) < note_size) { Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs); + name_sz = READ_ONCE(nhdr->n_namesz); + desc_sz = READ_ONCE(nhdr->n_descsz); if (nhdr->n_type == BUILD_ID && - nhdr->n_namesz == sizeof("GNU") && - !strcmp((char *)(nhdr + 1), "GNU") && - nhdr->n_descsz > 0 && - nhdr->n_descsz <= BUILD_ID_SIZE_MAX) { - memcpy(build_id, - note_start + note_offs + - ALIGN(sizeof("GNU"), 4) + sizeof(Elf32_Nhdr), - nhdr->n_descsz); - memset(build_id + nhdr->n_descsz, 0, - BUILD_ID_SIZE_MAX - nhdr->n_descsz); + name_sz == note_name_sz && + strcmp((char *)(nhdr + 1), note_name) == 0 && + desc_sz > 0 && desc_sz <= BUILD_ID_SIZE_MAX) { + data = note_start + note_offs + ALIGN(note_name_sz, 4); + memcpy(build_id, data, desc_sz); + memset(build_id + desc_sz, 0, BUILD_ID_SIZE_MAX - desc_sz); if (size) - *size = nhdr->n_descsz; + *size = desc_sz; return 0; } - new_offs = note_offs + sizeof(Elf32_Nhdr) + - ALIGN(nhdr->n_namesz, 4) + ALIGN(nhdr->n_descsz, 4); + new_offs = note_offs + sizeof(Elf32_Nhdr) + ALIGN(name_sz, 4) + ALIGN(desc_sz, 4); if (new_offs <= note_offs) /* overflow */ break; note_offs = new_offs; @@ -71,7 +72,7 @@ static int get_build_id_32(const void *page_addr, unsigned char *build_id, { Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr; Elf32_Phdr *phdr; - int i; + __u32 i, phnum; /* * FIXME @@ -80,9 +81,10 @@ static int get_build_id_32(const void *page_addr, unsigned char *build_id, */ if (ehdr->e_phoff != sizeof(Elf32_Ehdr)) return -EINVAL; + + phnum = READ_ONCE(ehdr->e_phnum); /* only supports phdr that fits in one page */ - if (ehdr->e_phnum > - (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr)) + if (phnum > (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr)) return -EINVAL; phdr = (Elf32_Phdr *)(page_addr + sizeof(Elf32_Ehdr)); @@ -90,8 +92,8 @@ static int get_build_id_32(const void *page_addr, unsigned char *build_id, for (i = 0; i < ehdr->e_phnum; ++i) { if (phdr[i].p_type == PT_NOTE && !parse_build_id(page_addr, build_id, size, - page_addr + phdr[i].p_offset, - phdr[i].p_filesz)) + page_addr + READ_ONCE(phdr[i].p_offset), + READ_ONCE(phdr[i].p_filesz))) return 0; } return -EINVAL; @@ -103,7 +105,7 @@ static int get_build_id_64(const void *page_addr, unsigned char *build_id, { Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr; Elf64_Phdr *phdr; - int i; + __u32 i, phnum; /* * FIXME @@ -112,18 +114,19 @@ static int get_build_id_64(const void *page_addr, unsigned char *build_id, */ if (ehdr->e_phoff != sizeof(Elf64_Ehdr)) return -EINVAL; + + phnum = READ_ONCE(ehdr->e_phnum); /* only supports phdr that fits in one page */ - if (ehdr->e_phnum > - (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr)) + if (phnum > (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr)) return -EINVAL; phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr)); - for (i = 0; i < ehdr->e_phnum; ++i) { + for (i = 0; i < phnum; ++i) { if (phdr[i].p_type == PT_NOTE && !parse_build_id(page_addr, build_id, size, - page_addr + phdr[i].p_offset, - phdr[i].p_filesz)) + page_addr + READ_ONCE(phdr[i].p_offset), + READ_ONCE(phdr[i].p_filesz))) return 0; } return -EINVAL; From patchwork Tue Jul 30 20:39:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13747865 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 ED2AAC3DA49 for ; Tue, 30 Jul 2024 20:39:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C0AA6B0092; Tue, 30 Jul 2024 16:39:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 671F26B008C; Tue, 30 Jul 2024 16:39:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4EA396B0092; Tue, 30 Jul 2024 16:39:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 341846B008A for ; Tue, 30 Jul 2024 16:39:30 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C6DD1C04E2 for ; Tue, 30 Jul 2024 20:39:29 +0000 (UTC) X-FDA: 82397584458.29.CC084D7 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf01.hostedemail.com (Postfix) with ESMTP id 6B54140054 for ; Tue, 30 Jul 2024 20:39:27 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=tis03ewq; spf=pass (imf01.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=1722371914; 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=Od0tXap4LEDx8h42G0SYJDElcSomAKqmmC0FTDKV7Ck=; b=vihdLtvobbIqU84G9NPAY9ZS9O2gU/cWHhL3xDg24ylyDlbOnnJOwCfZfSRminpzC93RJk ByqGFg7ig8XQzxj4vsjE48+bz9Dd13yxSjP2ANjdmXvtSHL1DBwvd4fKSDWFSRhvOhJpxa MMPoBuvxb1QTWN45qk14MOp3W+DGyS0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722371914; a=rsa-sha256; cv=none; b=oxNJCPCVQBP/LsFtY5YW+iwJXJfsOKbiibH/0DvhsXgZM+oGQqEc3BXL1vizLsHZDJA6/l b2gi8RNTmp0IQC/ZE8gQm606T6One31OEOUzHvQpkhynRwnIqB2DwjhX5hfNmr2Ktm0vs8 7ka3HY0fTraoIWS9cupZGTLQ/bvNLwE= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=tis03ewq; spf=pass (imf01.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 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id AA2EDCE0FE3; Tue, 30 Jul 2024 20:39:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1813EC32782; Tue, 30 Jul 2024 20:39:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722371964; bh=cIXFm7A7vHu+B5XnydLoF8708TS8AusUUzJReK4j2M0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tis03ewqBtNoXKvtT+mqCE7jV8QvByHfBudBJ89hIx3JyEnESIbzF2X6LitxXUOZ/ 9e9AXoyYvr44pMJ3b+5Eu/H0u4Xo8pcq5IS7/PoCug+agRetU68V5CApZckH4OGJgN qKLq9fvibBAsXQ1pTEKbzKNjlAq0QYkfCKDX8QqMZDqpD9yyzeqFGhJ2f6ogS4Pq3x 31WFjqkMUzE/amy+JSpCEKwVdrkSXptWIIM+2w1KQ5if09rStXxc5uYDvYeypxPCFX kmC+wfkFGPpoEE+r7j5qhKKucYCJH59zwEgC97Tq8IhXGYlh+DhogsfMLUQBqMNr6b hjs1DpBYnGO5A== 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, jannh@google.com, Andrii Nakryiko Subject: [PATCH v3 bpf-next 02/10] lib/buildid: add single page-based file reader abstraction Date: Tue, 30 Jul 2024 13:39:06 -0700 Message-ID: <20240730203914.1182569-3-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730203914.1182569-1-andrii@kernel.org> References: <20240730203914.1182569-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6B54140054 X-Stat-Signature: 68nwskk75zkn99henoxds75mrdpxae13 X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1722371967-744854 X-HE-Meta: U2FsdGVkX1/4zEzFVTUSIYNJOxQQEJVKSbc73RxtXKsD20T3JeglpFgzcMKpPqjlYa4gC47GZSvgiAF3lK9kqwcMgThKzLapL1HEdQIvqnZMoBahNYBK2kt/9YuQFx3vs1hJbAdNSsvIal2cmxIqrTkC//R/rejweiWY0IYQiMUVR/oisp0CCNoiyfXO8c3kb0Q1lACLveZO1XJCdwcXTwNZGeQYgViz+TbqjYaw29VY95sb5YzYejWJ/duHiGS4NZ3CoUMdh0KunW5nAcaQgH544as61F3guCs1kCyDbNn4FS/BhTAqQPwHjWtbN2gL68/zE0qAQpCbwFTmf8TOW8J+imsOmFAPMCUL4eXoynNdJPORPUg6I3tBr5f4NljmzRJGkKyt3YkD8+TURuOjEGon3N9YRpG3sPpyeALFyGogeGmXqU3EAa8NWEFOApFjVqshm0wHUNyDCcbNAbF5kQjFRmBQYWx0i3UmWCSp8JZNvw9/fhKH6ndWr6ncAtSRKEtwe4RZC6ZHRrab/SzvBPcor/tz6VtqMd4lChWL2A2DARGaH9KswjKa/D9LhgxdIJ0kBXbS0JmGmOe52MxyxXOYjFjH3PKCZbsYFBv5/JUeuVFMOZ71Vz/rCpClHAUFFqmHP+4yJNlqMrx7vprQSpW3bnPLiXT2x3kdSb6LgVn2ZtllA3oOeaNKewQbdAhL5X2yoIVePGpTpsaCUP+jtnTJnzzYTRHqJRq6BM/lmWaEqWL3Vuvi9W1clSNHO1dY82uuppp5Lyz6oQ7y+Rzsm5WU8ijtMYVuQYvBMwJERsoRPqmvXD3AxDhmoTH+ovkPIbzgr95KY85+VF8c/XQdd9r1MApvxauOLi/v2N8PCNPkapqooLg0BMST4QDI8uvEUYKJZSjLdoM73h2DvZXPRXrjQ38tw+3Ek2vVPMCryNuFqeWtYEuZc6+MjOAaHEaekghDsRTMX0hVuiTANHC L/j577eF 3bBzeJj19L14kOa5KYaAieNh3rpFThqB4XpJ6JKXVsLMalu00xTsabpibpKeMCui6gibj+WffCjYkfQUMWbJsyuSvjkGOjTJ6wGUKd8er3mhs+ThZkg4yn8vf3U5oskpS1/sYqbWEpUiNhjefo1rX9afGmaPhQ2MANzv9mBo11GYjw+1KUaxgfAOKrK3d/0e0W1ZhQ/+sqHYVNXGbxRLRWOKiP9e3WW2ohW0cW/t4dPZw5vY= 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: Add freader abstraction that transparently manages fetching and local mapping of the underlying file page(s) and provides a simple direct data access interface. freader_fetch() is the only and single interface necessary. It accepts file offset and desired number of bytes that should be accessed, and will return a kernel mapped pointer that caller can use to dereference data up to requested size. Requested size can't be bigger than the size of the extra buffer provided during initialization (because, worst case, all requested data has to be copied into it, so it's better to flag wrongly sized buffer unconditionally, regardless if requested data range is crossing page boundaries or not). If page is not paged in, or some of the conditions are not satisfied, NULL is returned and more detailed error code can be accessed through freader->err field. This approach makes the usage of freader_fetch() cleaner. To accommodate accessing file data that crosses page boundaries, user has to provide an extra buffer that will be used to make a local copy, if necessary. This is done to maintain a simple linear pointer data access interface. We switch existing build ID parsing logic to it, without changing or lifting any of the existing constraints, yet. This will be done separately. Given existing code was written with the assumption that it's always working with a single (first) page of the underlying ELF file, logic passes direct pointers around, which doesn't really work well with freader approach and would be limiting when removing the single page limitation. So we adjust all the logic to work in terms of file offsets. There is also a memory buffer-based version (freader_init_from_mem()) for cases when desired data is already available in kernel memory. This is used for parsing vmlinux's own build ID note. In this mode assumption is that provided data starts at "file offset" zero, which works great when parsing ELF notes sections, as all the parsing logic is relative to note section's start. Signed-off-by: Andrii Nakryiko --- lib/buildid.c | 255 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 207 insertions(+), 48 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index d21d86f6c19a..522850f827a5 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -8,24 +8,153 @@ #define BUILD_ID 3 +struct freader { + void *buf; + u32 buf_sz; + int err; + union { + struct { + struct address_space *mapping; + struct page *page; + void *page_addr; + u64 file_off; + }; + struct { + const char *data; + u64 data_sz; + }; + }; +}; + +static void freader_init_from_file(struct freader *r, void *buf, u32 buf_sz, + struct address_space *mapping) +{ + memset(r, 0, sizeof(*r)); + r->buf = buf; + r->buf_sz = buf_sz; + r->mapping = mapping; +} + +static void freader_init_from_mem(struct freader *r, const char *data, u64 data_sz) +{ + memset(r, 0, sizeof(*r)); + r->data = data; + r->data_sz = data_sz; +} + +static void freader_put_page(struct freader *r) +{ + if (!r->page) + return; + kunmap_local(r->page_addr); + put_page(r->page); + r->page = NULL; +} + +static int freader_get_page(struct freader *r, u64 file_off) +{ + pgoff_t pg_off = file_off >> PAGE_SHIFT; + + freader_put_page(r); + + r->page = find_get_page(r->mapping, pg_off); + if (!r->page) + return -EFAULT; /* page not mapped */ + + r->page_addr = kmap_local_page(r->page); + r->file_off = file_off & PAGE_MASK; + + return 0; +} + +static const void *freader_fetch(struct freader *r, u64 file_off, size_t sz) +{ + int err; + + /* provided internal temporary buffer should be sized correctly */ + if (WARN_ON(r->buf && sz > r->buf_sz)) { + r->err = -E2BIG; + return NULL; + } + + if (unlikely(file_off + sz < file_off)) { + r->err = -EOVERFLOW; + return NULL; + } + + /* working with memory buffer is much more straightforward */ + if (!r->buf) { + if (file_off + sz > r->data_sz) { + r->err = -ERANGE; + return NULL; + } + return r->data + file_off; + } + + /* check if we need to fetch a different page first */ + if (!r->page || file_off < r->file_off || file_off >= r->file_off + PAGE_SIZE) { + err = freader_get_page(r, file_off); + if (err) { + r->err = err; + return NULL; + } + } + + /* if requested data is crossing page boundaries, we have to copy + * everything into our local buffer to keep a simple linear memory + * access interface + */ + if (file_off + sz > r->file_off + PAGE_SIZE) { + int part_sz = r->file_off + PAGE_SIZE - file_off; + + /* copy the part that resides in the current page */ + memcpy(r->buf, r->page_addr + (file_off - r->file_off), part_sz); + + /* fetch next page */ + err = freader_get_page(r, r->file_off + PAGE_SIZE); + if (err) { + r->err = err; + return NULL; + } + + /* copy the rest of requested data */ + memcpy(r->buf + part_sz, r->page_addr, sz - part_sz); + + return r->buf; + } + + /* if data fits in a single page, just return direct pointer */ + return r->page_addr + (file_off - r->file_off); +} + +static void freader_cleanup(struct freader *r) +{ + if (!r->buf) + return; /* non-file-backed mode */ + + freader_put_page(r); +} + /* * Parse build id from the note segment. This logic can be shared between * 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are * identical. */ -static int parse_build_id_buf(unsigned char *build_id, - __u32 *size, - const void *note_start, - Elf32_Word note_size) +static int parse_build_id_buf(struct freader *r, + unsigned char *build_id, __u32 *size, + u64 note_offs, Elf32_Word note_size) { const char note_name[] = "GNU"; const size_t note_name_sz = sizeof(note_name); - Elf32_Word note_offs = 0, new_offs; + const Elf32_Nhdr *nhdr; + u64 build_id_off, new_offs, note_end = note_offs + note_size; u32 name_sz, desc_sz; const char *data; - while (note_offs + sizeof(Elf32_Nhdr) < note_size) { - Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs); + while (note_offs + sizeof(Elf32_Nhdr) < note_end) { + nhdr = freader_fetch(r, note_offs, sizeof(Elf32_Nhdr) + note_name_sz); + if (!nhdr) + return r->err; name_sz = READ_ONCE(nhdr->n_namesz); desc_sz = READ_ONCE(nhdr->n_descsz); @@ -33,7 +162,13 @@ static int parse_build_id_buf(unsigned char *build_id, name_sz == note_name_sz && strcmp((char *)(nhdr + 1), note_name) == 0 && desc_sz > 0 && desc_sz <= BUILD_ID_SIZE_MAX) { - data = note_start + note_offs + ALIGN(note_name_sz, 4); + build_id_off = note_offs + sizeof(Elf32_Nhdr) + ALIGN(note_name_sz, 4); + + /* freader_fetch() will invalidate nhdr pointer */ + data = freader_fetch(r, build_id_off, desc_sz); + if (!data) + return r->err; + memcpy(build_id, data, desc_sz); memset(build_id + desc_sz, 0, BUILD_ID_SIZE_MAX - desc_sz); if (size) @@ -49,30 +184,33 @@ static int parse_build_id_buf(unsigned char *build_id, return -EINVAL; } -static inline int parse_build_id(const void *page_addr, +static inline int parse_build_id(struct freader *r, unsigned char *build_id, __u32 *size, - const void *note_start, + u64 note_start_off, Elf32_Word note_size) { /* check for overflow */ - if (note_start < page_addr || note_start + note_size < note_start) + if (note_start_off + note_size < note_start_off) return -EINVAL; /* only supports note that fits in the first page */ - if (note_start + note_size > page_addr + PAGE_SIZE) + if (note_start_off + note_size > PAGE_SIZE) return -EINVAL; - return parse_build_id_buf(build_id, size, note_start, note_size); + return parse_build_id_buf(r, build_id, size, note_start_off, note_size); } /* Parse build ID from 32-bit ELF */ -static int get_build_id_32(const void *page_addr, unsigned char *build_id, - __u32 *size) +static int get_build_id_32(struct freader *r, unsigned char *build_id, __u32 *size) { - Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr; - Elf32_Phdr *phdr; - __u32 i, phnum; + const Elf32_Ehdr *ehdr; + const Elf32_Phdr *phdr; + __u32 phnum, i; + + ehdr = freader_fetch(r, 0, sizeof(Elf32_Ehdr)); + if (!ehdr) + return r->err; /* * FIXME @@ -82,30 +220,35 @@ static int get_build_id_32(const void *page_addr, unsigned char *build_id, if (ehdr->e_phoff != sizeof(Elf32_Ehdr)) return -EINVAL; + /* subsequent freader_fetch() calls invalidate pointers, so remember locally */ phnum = READ_ONCE(ehdr->e_phnum); /* only supports phdr that fits in one page */ if (phnum > (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr)) return -EINVAL; - phdr = (Elf32_Phdr *)(page_addr + sizeof(Elf32_Ehdr)); + for (i = 0; i < phnum; ++i) { + phdr = freader_fetch(r, i * sizeof(Elf32_Phdr), sizeof(Elf32_Phdr)); + if (!phdr) + return r->err; - for (i = 0; i < ehdr->e_phnum; ++i) { - if (phdr[i].p_type == PT_NOTE && - !parse_build_id(page_addr, build_id, size, - page_addr + READ_ONCE(phdr[i].p_offset), - READ_ONCE(phdr[i].p_filesz))) + if (phdr->p_type == PT_NOTE && + !parse_build_id(r, build_id, size, READ_ONCE(phdr->p_offset), + READ_ONCE(phdr->p_filesz))) return 0; } return -EINVAL; } /* Parse build ID from 64-bit ELF */ -static int get_build_id_64(const void *page_addr, unsigned char *build_id, - __u32 *size) +static int get_build_id_64(struct freader *r, unsigned char *build_id, __u32 *size) { - Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr; - Elf64_Phdr *phdr; - __u32 i, phnum; + const Elf64_Ehdr *ehdr; + const Elf64_Phdr *phdr; + __u32 phnum, i; + + ehdr = freader_fetch(r, 0, sizeof(Elf64_Ehdr)); + if (!ehdr) + return r->err; /* * FIXME @@ -115,23 +258,29 @@ static int get_build_id_64(const void *page_addr, unsigned char *build_id, if (ehdr->e_phoff != sizeof(Elf64_Ehdr)) return -EINVAL; + /* subsequent freader_fetch() calls invalidate pointers, so remember locally */ phnum = READ_ONCE(ehdr->e_phnum); /* only supports phdr that fits in one page */ if (phnum > (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr)) return -EINVAL; - phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr)); - for (i = 0; i < phnum; ++i) { - if (phdr[i].p_type == PT_NOTE && - !parse_build_id(page_addr, build_id, size, - page_addr + READ_ONCE(phdr[i].p_offset), - READ_ONCE(phdr[i].p_filesz))) + phdr = freader_fetch(r, i * sizeof(Elf64_Phdr), sizeof(Elf64_Phdr)); + if (!phdr) + return r->err; + + if (phdr->p_type == PT_NOTE && + !parse_build_id(r, build_id, size, READ_ONCE(phdr->p_offset), + READ_ONCE(phdr->p_filesz))) return 0; } + return -EINVAL; } +/* enough for Elf64_Ehdr, Elf64_Phdr, and all the smaller requests */ +#define MAX_FREADER_BUF_SZ 64 + /* * Parse build ID of ELF file mapped to vma * @vma: vma object @@ -143,22 +292,25 @@ static int get_build_id_64(const void *page_addr, unsigned char *build_id, int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id, __u32 *size) { - Elf32_Ehdr *ehdr; - struct page *page; - void *page_addr; + const Elf32_Ehdr *ehdr; + struct freader r; + char buf[MAX_FREADER_BUF_SZ]; int ret; /* only works for page backed storage */ if (!vma->vm_file) return -EINVAL; - page = find_get_page(vma->vm_file->f_mapping, 0); - if (!page) - return -EFAULT; /* page not mapped */ + freader_init_from_file(&r, buf, sizeof(buf), vma->vm_file->f_mapping); + + /* fetch first 18 bytes of ELF header for checks */ + ehdr = freader_fetch(&r, 0, offsetofend(Elf32_Ehdr, e_type)); + if (!ehdr) { + ret = r.err; + goto out; + } ret = -EINVAL; - page_addr = kmap_local_page(page); - ehdr = (Elf32_Ehdr *)page_addr; /* compare magic x7f "ELF" */ if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) @@ -169,12 +321,11 @@ int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id, goto out; if (ehdr->e_ident[EI_CLASS] == ELFCLASS32) - ret = get_build_id_32(page_addr, build_id, size); + ret = get_build_id_32(&r, build_id, size); else if (ehdr->e_ident[EI_CLASS] == ELFCLASS64) - ret = get_build_id_64(page_addr, build_id, size); + ret = get_build_id_64(&r, build_id, size); out: - kunmap_local(page_addr); - put_page(page); + freader_cleanup(&r); return ret; } @@ -188,7 +339,15 @@ int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id, */ int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size) { - return parse_build_id_buf(build_id, NULL, buf, buf_size); + struct freader r; + int err; + + freader_init_from_mem(&r, buf, buf_size); + + err = parse_build_id(&r, build_id, NULL, 0, buf_size); + + freader_cleanup(&r); + return err; } #if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) || IS_ENABLED(CONFIG_VMCORE_INFO) From patchwork Tue Jul 30 20:39:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13747866 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 A2DFFC3DA49 for ; Tue, 30 Jul 2024 20:39:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2F0426B0093; Tue, 30 Jul 2024 16:39:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2ABEA6B0095; Tue, 30 Jul 2024 16:39:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 140406B0096; Tue, 30 Jul 2024 16:39:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id EAB296B0093 for ; Tue, 30 Jul 2024 16:39:33 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 60D0840516 for ; Tue, 30 Jul 2024 20:39:33 +0000 (UTC) X-FDA: 82397584626.26.BBA06A3 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf21.hostedemail.com (Postfix) with ESMTP id 21E071C002B for ; Tue, 30 Jul 2024 20:39:30 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=JJHHNmm0; spf=pass (imf21.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=1722371916; 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=8NSSfvTz2Qd7z1KNZnvE+BPgYr6TWzFHXFjUXO0bWT4=; b=Z8h9FLF9dKD0jXQBMSu2PkfpazRIs3+fdvIqlmnvb+HOBHDAVmx5qn0GyW+SEFAUpTAb6f UH8VsW8y1j5XhTuw+B4Tpq4oj/2qn4y4CruGnY6xQhuqalWUjfcV/Prhdcx/quuZY4jerq cNbmTihv942VvqBiAMSbLzeAk6+nVDY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722371916; a=rsa-sha256; cv=none; b=jjEfi17RzYCv8hgsn67uFpcgM2Eo+84YsPbfSmEWx/ANba6V7Hqavaxoakdjl3ebPOpyzl MQ7C9d2jdC36QPg90HKS+b0FzKfGCKcmoPYZd8VjxQnXCagFgKjUXX+Ugdfcwh+t5MVX7c czBgs/QfA5EW6LIDFLmQTjyG9cKf0co= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=JJHHNmm0; spf=pass (imf21.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 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 446F7CE0EBB; Tue, 30 Jul 2024 20:39:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 635DDC32782; Tue, 30 Jul 2024 20:39:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722371967; bh=dgztIaI7wcUufYQC2M1f2CEzPpb4qtKFeC4HcS1F34s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JJHHNmm0RMR0lI68DTZjiB1Shtq5MZ1QfO8BeQ7hc3vZ0cTdbz1Q2AR5dSLhMOT+M cDHtRFjC59k8ZM/cy8Y15dooEsrwrZzCEHtkmS55Ph7HFcsfKaMHsc45mMhLpzJ//9 cyCsUBW1oEof2DPgCMNLwCBerVU3wAhvxwxi9uCcjylMui6/QUGx06sOMMeEEMp2zn lTA3faXrcDLFlWKdnkCV+S8opQPU55ht8uQx9kel3yiQszLvmpDVwgrqDoG457104d 3wg83ar9DdMT7ad3I3BUyAmad/M3b48csSLAYWbtjYb1367xyI9fEOdSKgSEoPObat w119lCn4clZzA== 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, jannh@google.com, Andrii Nakryiko Subject: [PATCH v3 bpf-next 03/10] lib/buildid: take into account e_phoff when fetching program headers Date: Tue, 30 Jul 2024 13:39:07 -0700 Message-ID: <20240730203914.1182569-4-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730203914.1182569-1-andrii@kernel.org> References: <20240730203914.1182569-1-andrii@kernel.org> MIME-Version: 1.0 X-Stat-Signature: 1wmkjbbnstchqe9tjd9uepz3o41won8g X-Rspamd-Queue-Id: 21E071C002B X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1722371970-406506 X-HE-Meta: U2FsdGVkX1+HiJX7TdWEEb69gfhsV1wbHac0ZNlCm1tNyREi4apzxhMcZgO3n5+YUXztKW7URMnRH34jj8meMySbcyd6t6J04GPc/UIXivIJxBGSLoGUyx4sDGkoG2gisqCM5ucFMP4okqBZu62LdnWBBjFq6kHHnbWtg47YV4jDVmxL5I0B4HcAbkYqL4FRP0q0HXgYaEfZgRzhJugCrIY0jTVzCGdgc/+ASUJF5DzuMKogXbr65HLrBXxvhQAXPQlMNTmFaMvA13xn1Em9Jm0bZ1Hy5Q0dG5O85NLxB3wzSsEleRmjgMiGidalFPrmNy1zRAtdv8yQinQDqiF8y1klK+/OImCuyx1M2LMCtfK0WyfmRhobCK2mKKIP77oq0gaw4oW20yjKHugfdyioa+8YBXAcEfE9cU+/l1nYJQ2Am/St56xVgWfGwpwLCQFMBdpsC30l29MlFkCTxei0/yitnUjNv7zQJLctGzDs4SUCVIZCNWhd7vt8ES/Yslj7AMK1cbz3izWwo5VHyNTJIBTmlPOIxJCtT3olmUdxLN6dtCvzRBVWCsFSpjLhbSubxqS8mPc1HQP+qNFpWz9jKkyUs4sb8gqDqbdPR+cjwt+kfIdHw74oVK7v0Wbk85H5+vcsOzfK9v2r4agmjivG8Ng0dnA0ENkcACQiSU0jAPhzvnwNLnKPKGD5mZTrV54BbtuwW9BjKTZZ0NvObkD2a0Lewq7v0m9YbNKj+cVNaqVcur+ihqdsd8BE+dHYFL+pRqOE2wm2MoU3NK3q+SnztBmdiKzD/a29mtvSS0vgngMP+34u+DRxRm+a5YYn8bZT6WYfRkFPOYX0F9a67RsOsJGgEpQsK1llLFRepljBLugMUHgak9J7q6trHMlf0ePIf2YInU2h9HZP+yo5CkMC/bP+I1A/rcp0n/F1+OTHFPTgWdonTjNYNQBkRL1Hs9EEHrRi5K7RIlmZ0GeUY4h XnYRIAeR Mm9tYZlmN6jOV4F4Ss6fi27D5B+YQVyPQqYOQ7pbf8NTr7kQ868b6gkOCZPW72cqjZHv5bCHCh6MRBG2sohTPTXStZdhUmCIH5ighfoT0qCpe4tlzVOw2zmuJEmYgcPsP02Ew2WwHu5Wkcgfks2P4czN0ueTopJRJPBVG5FkAhZw+xj8y59TG7yyO8YtyaljBYbe6/J0e1fzJwOOjvsLZ75nOS0Bi0pPHf7bUR5UX/p3k9fNxTZKyCK6aJyKeoiHB2uNz3lX44Mj60VBX15dwXY83MHJTSvd/x03pcRDGnw5iF0/bS40txRzOzA== 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: Current code assumption is that program (segment) headers are following ELF header immediately. This is a common case, but is not guaranteed. So take into account e_phoff field of the ELF header when accessing program headers. Reported-by: Alexey Dobriyan Signed-off-by: Andrii Nakryiko --- lib/buildid.c | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index 522850f827a5..ba453a3784d1 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -207,27 +207,22 @@ static int get_build_id_32(struct freader *r, unsigned char *build_id, __u32 *si const Elf32_Ehdr *ehdr; const Elf32_Phdr *phdr; __u32 phnum, i; + __u64 phoff; ehdr = freader_fetch(r, 0, sizeof(Elf32_Ehdr)); if (!ehdr) return r->err; - /* - * FIXME - * Neither ELF spec nor ELF loader require that program headers - * start immediately after ELF header. - */ - if (ehdr->e_phoff != sizeof(Elf32_Ehdr)) - return -EINVAL; - /* subsequent freader_fetch() calls invalidate pointers, so remember locally */ phnum = READ_ONCE(ehdr->e_phnum); + phoff = READ_ONCE(ehdr->e_phoff); + /* only supports phdr that fits in one page */ if (phnum > (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr)) return -EINVAL; for (i = 0; i < phnum; ++i) { - phdr = freader_fetch(r, i * sizeof(Elf32_Phdr), sizeof(Elf32_Phdr)); + phdr = freader_fetch(r, phoff + i * sizeof(Elf32_Phdr), sizeof(Elf32_Phdr)); if (!phdr) return r->err; @@ -245,27 +240,22 @@ static int get_build_id_64(struct freader *r, unsigned char *build_id, __u32 *si const Elf64_Ehdr *ehdr; const Elf64_Phdr *phdr; __u32 phnum, i; + __u64 phoff; ehdr = freader_fetch(r, 0, sizeof(Elf64_Ehdr)); if (!ehdr) return r->err; - /* - * FIXME - * Neither ELF spec nor ELF loader require that program headers - * start immediately after ELF header. - */ - if (ehdr->e_phoff != sizeof(Elf64_Ehdr)) - return -EINVAL; - /* subsequent freader_fetch() calls invalidate pointers, so remember locally */ phnum = READ_ONCE(ehdr->e_phnum); + phoff = READ_ONCE(ehdr->e_phoff); + /* only supports phdr that fits in one page */ if (phnum > (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr)) return -EINVAL; for (i = 0; i < phnum; ++i) { - phdr = freader_fetch(r, i * sizeof(Elf64_Phdr), sizeof(Elf64_Phdr)); + phdr = freader_fetch(r, phoff + i * sizeof(Elf64_Phdr), sizeof(Elf64_Phdr)); if (!phdr) return r->err; From patchwork Tue Jul 30 20:39:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13747867 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 92223C3DA7F for ; Tue, 30 Jul 2024 20:39:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A5B56B0095; Tue, 30 Jul 2024 16:39:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9576D6B0096; Tue, 30 Jul 2024 16:39:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7F7396B0098; Tue, 30 Jul 2024 16:39:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 606CD6B0095 for ; Tue, 30 Jul 2024 16:39:34 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 106FBA048A for ; Tue, 30 Jul 2024 20:39:34 +0000 (UTC) X-FDA: 82397584668.11.5EB0D34 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf23.hostedemail.com (Postfix) with ESMTP id 00C6614003D for ; Tue, 30 Jul 2024 20:39:31 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dXX4rvyZ; spf=pass (imf23.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 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=1722371929; 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=yf8fXbkWts3gtNZ55HimmuRr/D74w/nooI3+u4YlS38=; b=QqM6z1le32TRjKQfGwtIsuwQ2h2D70EMn3oPD/PLY4neFphFzHokmVbDPRaJQ+DDkBiuzq ceOaBUUcCGPYSepqKd2JF38VfvyDqTIWpMCl03rjnWvdAJwGpj0MMrC6TxMaauJXf1y/pL 8bRdnV1cYH2nqqdj6iq3KsvtT5gUDVc= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=dXX4rvyZ; spf=pass (imf23.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 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=1722371929; a=rsa-sha256; cv=none; b=gBwlmFAx1zVEkw49YZwYEFxxMsrV8VD0cusFLA76fhvULwnUh6COsobixX0Fn3pzCaGbZt pBx0bh/q7kqNMleM4ESMxv5Ovd5m4s2qGLMyq0VJr9CS7X7k0NGH7hmnZZXKCtdusBl7em Anlo/BM/57i4I+Q+NgzqrBKq/akk3LI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 2A70062064; Tue, 30 Jul 2024 20:39:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A714AC4AF0B; Tue, 30 Jul 2024 20:39:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722371970; bh=GUqeiOWs92MpwBboJ0qXM//0rJO16RNb6rfoIYo9S10=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dXX4rvyZw8450YmvvFwxvviqZXlbtriu8fEHWqrGOUv1XfLApBmyxINQfj3YnY0Bl vZWFVhW9Pq0NMHeRUxsR9VcbZfoK/UrZPdI2aHrEsPKQud1c08aTblq8cSsNZ/ZzZL F6eGgBHvXSiJamEzY85jPZVCALN/Nm3XGIkz2anGpISmD/uJOEk0UTHKNx2hgEyUdU +JmjVyH5xwmx+DgwSZLtMhGRgkD7gC+2AMGhTPqnlp6NPOy2t/Q3Z09Db8/HQwKD0+ s+oMsmE63soYP23u6u0EA5vhTbhD6KBNbMBjoAPVsojUIRe4twSJIp+2SMRakWpSrj VqvWJDTkt2X4g== 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, jannh@google.com, Andrii Nakryiko Subject: [PATCH v3 bpf-next 04/10] lib/buildid: remove single-page limit for PHDR search Date: Tue, 30 Jul 2024 13:39:08 -0700 Message-ID: <20240730203914.1182569-5-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730203914.1182569-1-andrii@kernel.org> References: <20240730203914.1182569-1-andrii@kernel.org> MIME-Version: 1.0 X-Stat-Signature: w8fzrz6ppsfbiftd87i6x1mszaaeg3jt X-Rspam-User: X-Rspamd-Queue-Id: 00C6614003D X-Rspamd-Server: rspam02 X-HE-Tag: 1722371971-208904 X-HE-Meta: U2FsdGVkX18y4cfUY9h85aVHiZKnLKEZ2wBBGqGksKTfXShADSQnGecSJeycPfrG0PtrCrzIbEpo3pjjL0O5TpsvzJM0RJ/fc78t4tG//nIGwzMXm836VTAsvEkmTIcklv5B+afH5p5dkXUm9NIFkBeVf3eSkQbAa4tOf3SiPuTNu+UesCm8E5ZuE4LOT3zQ4/KPwV3viKklRk5wXA3KJWByNY5YIeOsl2zNIDCe62eZc/P4ypWJMdF5oI9h0hVAap+yiKAPQ0EW7z3U8HnAGbiHrjSfoCiSrlOXEKIYfnnVeANAcKAUNaNpPfR26XT3wgJmcqoDf7hQzogJS1F7yZwq04Ch/Jec0cQrpWURo8PRye78/MxiYzADtPaW8P2HJqx5Sl7rBVgWu/d7/JAlXaE2vqbvb9M+saDm8+HNmrpGtOBavZII/3nBQH5AtHmiDFTB4C5+T6jjINENXsRLOh2IPuxehwTf0Y5/9nAbKdX7t65pPz8a3MeOeygyCRh48spor6WMjyTBwxUJu6fITRlRjc9T2peZoJ3Q7+r9fh7EJo6bt40VwC8eU6peB1Y7hRnYwZP/5iAJGZQk7ynt+NuXEuELBbmbuXey5pLAhqJ2ma2C7PVwMSOsC1tFTBo8f9wVo8XlnbysUGgz7ruVhNsbjFzDt5HSGRmSbwoX8ohMPfazioMCgMWI2rXZiaRMRzHAj1KjiqbkSWwm4h+sMQegZ3VxZBCLTJ6mMyOzrOXgqgXRfiKT+W/LOwkpfxIA6TK3PJpYE8m+I63S4ofAJKmdAlxAKKlh9RkUt3b0cd0YhEh7WDlMK1Y0T4queW8dQT7M0gDA74gAh+0AnaC2tqzyg8J3Y0nbc44ab65UrTXSVRsapzBDmaHIQen8FuGzLd6lTYGcw7GnHpcKwZ1YKBkc+Y+hC5UFWw3JJk6+BTUaMlcg6lsviPYigjlfhsBjvUQ1cx05Lm8iYSZgOr4 mh6j3nZM K6EApa4+CgomJNYmGgF6D7t63R8OzuS03KB5BoPuhZDTTNSWJCUr7yODYnre2mI5qOjjZa/c5HKWr/EhpMwmi6MJtt4Hxvw6iY22oEWMhR8DFXnPznuLqiVQqEVIFugM6ZFuvWh7qJvhoWns78F05aUaUKUMwbXBx/a3HBs8IojEME0Vi1utzcm9rE/wJidL/8AgGrWg0YVh34MQnpAPtCvB1wRBR5bP6T4IbvBWnViG8k5k= 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: Now that freader allows to access multiple pages transparently, there is no need to limit program headers to the very first ELF file page. Remove this limitation, but still put some sane limit on amount of program headers that we are willing to iterate over (set arbitrarily to 256). Signed-off-by: Andrii Nakryiko --- lib/buildid.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index ba453a3784d1..095e79158b85 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -8,6 +8,8 @@ #define BUILD_ID 3 +#define MAX_PHDR_CNT 256 + struct freader { void *buf; u32 buf_sz; @@ -217,9 +219,9 @@ static int get_build_id_32(struct freader *r, unsigned char *build_id, __u32 *si phnum = READ_ONCE(ehdr->e_phnum); phoff = READ_ONCE(ehdr->e_phoff); - /* only supports phdr that fits in one page */ - if (phnum > (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr)) - return -EINVAL; + /* set upper bound on amount of segments (phdrs) we iterate */ + if (phnum > MAX_PHDR_CNT) + phnum = MAX_PHDR_CNT; for (i = 0; i < phnum; ++i) { phdr = freader_fetch(r, phoff + i * sizeof(Elf32_Phdr), sizeof(Elf32_Phdr)); @@ -250,9 +252,9 @@ static int get_build_id_64(struct freader *r, unsigned char *build_id, __u32 *si phnum = READ_ONCE(ehdr->e_phnum); phoff = READ_ONCE(ehdr->e_phoff); - /* only supports phdr that fits in one page */ - if (phnum > (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr)) - return -EINVAL; + /* set upper bound on amount of segments (phdrs) we iterate */ + if (phnum > MAX_PHDR_CNT) + phnum = MAX_PHDR_CNT; for (i = 0; i < phnum; ++i) { phdr = freader_fetch(r, phoff + i * sizeof(Elf64_Phdr), sizeof(Elf64_Phdr)); From patchwork Tue Jul 30 20:39:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13747868 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 9F614C3DA7F for ; Tue, 30 Jul 2024 20:39:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2F0726B0099; Tue, 30 Jul 2024 16:39:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 278F96B009A; Tue, 30 Jul 2024 16:39:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 11A226B009B; Tue, 30 Jul 2024 16:39:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E7B5D6B0099 for ; Tue, 30 Jul 2024 16:39:39 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A4C1E160440 for ; Tue, 30 Jul 2024 20:39:39 +0000 (UTC) X-FDA: 82397584878.04.42C9D7C Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf21.hostedemail.com (Postfix) with ESMTP id 683421C002B for ; Tue, 30 Jul 2024 20:39:37 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GXTKL8Fw; spf=pass (imf21.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=1722371924; 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=KICzIbfCnqVmtIiFKBTXjbpeqe+5kQGWdS9Wb+Utek8=; b=FPGN44nqYtbqMVsUnXBPPH9r4J3h3xqZQbhqaMpvubxtDhhhc5CDFzoJnNJY7jVUyeTk17 2Lfb1wyI9cV9TXU5gkddGxTpF3dF9vgkYQwkyUvtblNa04kKkUY5oVeG2X7nlI+XMv4B28 kWyD3XTzm8cJosCViYbyUFrBKxvZBsA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722371924; a=rsa-sha256; cv=none; b=1sp6/R2/LjJkFJ6XApPlhZFoGdxcpNTYUgvuIIqY2GCRyrqCcSq/0Mt+YRFRJ5e/hv3V8z /RqJQx84eYVjOpcvOfwtSLTHPD5/TgHNdyzWH67kG72a1nwmDSuzBi+GqGojYQ+3OpSucn ZvzpX5uQYB7TpVQGcTziUU1IzN4BeRE= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GXTKL8Fw; spf=pass (imf21.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 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id D7778CE0EBB; Tue, 30 Jul 2024 20:39:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0AACAC32782; Tue, 30 Jul 2024 20:39:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722371974; bh=aAQDsE7GDxJhm+wL9UOlgeKFBMIyy3lwjoQ7+lNWLG8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GXTKL8FwKOoOm71IhW1FyHalGRRuOZmfTPOquyCKYTY2T9UguVxHdnJ2kE8t6vvcq HNpPIuSOEHkVVRJNvrgifTT+eguuUiCTQIA+kzynaaJj6Ga2EYaJxRmBljOC6lYzmd DcJknR+8PpuZNYHjrEiFTViclfvXOy9ezaT3gcaMZyvPRRuS5vmJ+AI8pSAFVGrTY0 pd3mKEsEfcEOyQpyu4RtjN0SvOgYlJcyyGyapucVs5Mo1kU++zh9AZYp6s6cEURr4P uu3MRp7y2+tiKzk0NnJ5OcCm1wWNNJyl4yq7QXyRv9hkyDC84Xx6T4G3bWH6QC1U4y M9F27worVIM/A== 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, jannh@google.com, Andrii Nakryiko Subject: [PATCH v3 bpf-next 05/10] lib/buildid: rename build_id_parse() into build_id_parse_nofault() Date: Tue, 30 Jul 2024 13:39:09 -0700 Message-ID: <20240730203914.1182569-6-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730203914.1182569-1-andrii@kernel.org> References: <20240730203914.1182569-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 683421C002B X-Stat-Signature: 6bojz1xmh3gegwduye4ihfa5qgo3fiu4 X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1722371977-768982 X-HE-Meta: U2FsdGVkX18zPoH+B/kgs1WANmS//mjtrXL/5NjuZC+DZLjtZu6eXDFsDKNAlZonni3t+t4zVhuV5I/VCoabfuYRelG5p+rVJPTSL7TiJd01CHH8SJV/ZB1ZEvlGNo+8RL7G6LQUvQF/SR++uwkZfRfqsfLAW9O6+bb/T6votK5q6T9DegANG2FqBjmvHVTye2LpdlhWUEV1YtSkl77hgY4WlmCquu7uQID4sJN+XPfRYvEzUrhalM1mloWstAXISpDVpptU7P6Ng1mcaSdQPzAT9Kkhin+FxiZKlQMuU0dr5aXatbFWA59CFdOrwncgyxt26mvdUSVfC05eS3CQryehGUBUYoJs666+lShJhbPIbicSYyqpntTO/pMYbX5GGCBPE7zKOtqIRVmjS/XJhKEqfYUXoQp23WfFJATvVyPlzQiZy8kSe4nlifimn0X1GwS/32S/gExIv82oavuK2PcEYLBXYaVuYb2MhNUM+FLrJrK00hQ+DtPk+8hOw8urC4RrHL1BUolTPOKQeOvzw0hv9/fysTU2OQ8OTN5jAPtxHpOcfob6cet0ibRKKykHJDFzgc/7fodCKKdeUoZDSpNczU8n8jihUU9kt8MjBMmQokap/HAzKZb1qkfxsmmDifOtQPNAxilL0lDSL1x/pvOSwDRNDYt+8u7z6sSYQbE9FDoz4QYmPdje9CVvkFtyAuWzgMUQU7VPj8J3pUxmJBdOo0CpzdqDFwYrEDCjHI4d3B2tDsb7RQby0EfcPJik6BLx4ir37nyvwgQIagxsrEiNGNJVXWGjKiy2LhQYmIdsGcJDrLj9R9Q39FjkW/VxGvuvnRSZz+LqDu9kN+vPzoXN9NtZW4eE37VvyxwQfs75CfIOKEPnMQE0uwa4dX9gY4nLIpnDNUibokD0IvnuMn8EEgdgH48Yj7FIF69dSFov/b4KCTjMPYsAQJ9GCZbLpjkUaefarGU3O7dphPF ypHoAstg w8eZo1Iet4vDtdizPnri7R/afpBg7A5tp99FK65yT0Lz5TmuL0YOqkpwsUXst1HTcwhkJnN+QV2rmatwb7M3xuQ3EvftbJZKzup8v8QrIz0o66BroX5gitY2S5VN5hu5iw2v0QLIT6XCIrjbf4pv3E31XNTuwhOrDKmHAXdjyLKeQH2dfAkSzQi4TUW9lt5Gz9bwIK+/wcEuMMF11Tgc2aeVIrJJfGw/DfmnwbgV8xYn7kkRnXIj7M5PeW3AZhpYuNI5kFBhr42FJvssVAtfk2Dut6uNW2XBOYWu7 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: Make it clear that build_id_parse() assumes that it can take no page fault by renaming it and current few users to build_id_parse_nofault(). Also add build_id_parse() stub which for now falls back to non-sleepable implementation, but will be changed in subsequent patches to take advantage of sleepable context. PROCMAP_QUERY ioctl() on /proc//maps file is using build_id_parse() and will automatically take advantage of more reliable sleepable context implementation. Signed-off-by: Andrii Nakryiko --- include/linux/buildid.h | 4 ++-- kernel/bpf/stackmap.c | 2 +- kernel/events/core.c | 2 +- lib/buildid.c | 25 ++++++++++++++++++++++--- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/include/linux/buildid.h b/include/linux/buildid.h index 20aa3c2d89f7..014a88c41073 100644 --- a/include/linux/buildid.h +++ b/include/linux/buildid.h @@ -7,8 +7,8 @@ #define BUILD_ID_SIZE_MAX 20 struct vm_area_struct; -int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id, - __u32 *size); +int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id, __u32 *size); +int build_id_parse_nofault(struct vm_area_struct *vma, unsigned char *build_id, __u32 *size); int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size); #if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) || IS_ENABLED(CONFIG_VMCORE_INFO) diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index c99f8e5234ac..770ae8e88016 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -156,7 +156,7 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs, goto build_id_valid; } vma = find_vma(current->mm, ips[i]); - if (!vma || build_id_parse(vma, id_offs[i].build_id, NULL)) { + if (!vma || build_id_parse_nofault(vma, id_offs[i].build_id, NULL)) { /* per entry fall back to ips */ id_offs[i].status = BPF_STACK_BUILD_ID_IP; id_offs[i].ip = ips[i]; diff --git a/kernel/events/core.c b/kernel/events/core.c index aa3450bdc227..c263a8b0ce54 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -8851,7 +8851,7 @@ static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; if (atomic_read(&nr_build_id_events)) - build_id_parse(vma, mmap_event->build_id, &mmap_event->build_id_size); + build_id_parse_nofault(vma, mmap_event->build_id, &mmap_event->build_id_size); perf_iterate_sb(perf_event_mmap_output, mmap_event, diff --git a/lib/buildid.c b/lib/buildid.c index 095e79158b85..5c869a2a30ab 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -279,10 +279,12 @@ static int get_build_id_64(struct freader *r, unsigned char *build_id, __u32 *si * @build_id: buffer to store build id, at least BUILD_ID_SIZE long * @size: returns actual build id size in case of success * - * Return: 0 on success, -EINVAL otherwise + * Assumes no page fault can be taken, so if relevant portions of ELF file are + * not already paged in, fetching of build ID fails. + * + * Return: 0 on success; negative error, otherwise */ -int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id, - __u32 *size) +int build_id_parse_nofault(struct vm_area_struct *vma, unsigned char *build_id, __u32 *size) { const Elf32_Ehdr *ehdr; struct freader r; @@ -321,6 +323,23 @@ int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id, return ret; } +/* + * Parse build ID of ELF file mapped to VMA + * @vma: vma object + * @build_id: buffer to store build id, at least BUILD_ID_SIZE long + * @size: returns actual build id size in case of success + * + * Assumes faultable context and can cause page faults to bring in file data + * into page cache. + * + * Return: 0 on success; negative error, otherwise + */ +int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id, __u32 *size) +{ + /* fallback to non-faultable version for now */ + return build_id_parse_nofault(vma, build_id, size); +} + /** * build_id_parse_buf - Get build ID from a buffer * @buf: ELF note section(s) to parse From patchwork Tue Jul 30 20:39:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13747869 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 ED9DEC3DA49 for ; Tue, 30 Jul 2024 20:39:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8206C6B009B; Tue, 30 Jul 2024 16:39:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D00D6B009C; Tue, 30 Jul 2024 16:39:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 64B6F6B009D; Tue, 30 Jul 2024 16:39:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 45A936B009B for ; Tue, 30 Jul 2024 16:39:43 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 04DDA4050C for ; Tue, 30 Jul 2024 20:39:42 +0000 (UTC) X-FDA: 82397585046.20.FAD1077 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf12.hostedemail.com (Postfix) with ESMTP id BA32540018 for ; Tue, 30 Jul 2024 20:39:40 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hc3WtjQV; spf=pass (imf12.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=1722371954; 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=UYZ3rbo800G0x8kCVI6eSRLaddT5Kbe/75CNRIQBv3M=; b=P7v/GiFE+CpykUlHQ6N+rUWtCsZWd0h3WHQnktmAQBK5fmEvjSYVmAKLFOFrNs5kCnPDFx iqTHq+atr6rJUYhosaPnxxxHJ7+NXuzq3drwLd01NBVJXz9KXzIxdqIC2PcDYI9SmUZbLZ od/gUsa3iDAcIqF69avPv0U2bI5DaJI= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hc3WtjQV; spf=pass (imf12.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=1722371954; a=rsa-sha256; cv=none; b=N1w9Ee4CulTv5NsBgLtGzKybW86WLc2+NeZNa1sdElETKtCWMrElwThtSr93Bz1oqmLox/ iMT8fpv8VTkxiOQiMxfzQpvaS1ds1KiMBIMgp7DB+nLlIP1koxJJT9BkmGYgMUd2x+Fseb +9aubQC82yn5eUrO3x0pqgil/7KMJf4= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 3846FCE0FE3; Tue, 30 Jul 2024 20:39:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 553D6C4AF09; Tue, 30 Jul 2024 20:39:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722371977; bh=mldgb2yfjVUsP4mxlrdfmqOiY8Dxht1DRa2moFmeLSU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hc3WtjQV1RhkYq/0gaCoIYSRMgWSZpcWPSPs54HREl6QCh03Z8l6eGy/SsDNY2c4j QtgbkRacBuGCav7W98Cq4L02LDCFhH/9gtPHBK0/P3w5sXj+jYNxootCOvet9CFVK6 +FRY9HwBvzwnbdYGbtci7Z/7JmRi5fks0PwqPA3Rak22GXL0yiTF7gYi5sR5QPGmWW XKS8opfEJ90/BLeQSTtEBxhNaX6x9iar/J1lpTrcWpXn0LeHdyZ54HS8EurCkrHf6e 5tge3wlug/esW5mYETpzo4bI4yXh6xb3xFxFGKYfcsu+lRAIzPeM+m8J4pUKWReFyb SH37o8VBbvNVw== 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, jannh@google.com, Andrii Nakryiko , Omar Sandoval Subject: [PATCH v3 bpf-next 06/10] lib/buildid: implement sleepable build_id_parse() API Date: Tue, 30 Jul 2024 13:39:10 -0700 Message-ID: <20240730203914.1182569-7-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730203914.1182569-1-andrii@kernel.org> References: <20240730203914.1182569-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: BA32540018 X-Stat-Signature: aqxzkuuthut6esez4uujqjxajddxk4da X-HE-Tag: 1722371980-212897 X-HE-Meta: U2FsdGVkX19/DDfrikkX534wK60R+EVWm7vx5y7uIgGxgvALXHriyO/gw31g0AbeZldTh6dDXXYtmH8DOvjarkPrLQDvSH7giPEbZ55L7/1cTOURtsXsG7LMIOVdEXqBzbpZosXE482cuTQlohVVyoSXMwenntcYIMzWqGD6N4di0c23+dQMsKCgIdFDZC9GZxtI8orwt3ozCYmEhfiNd1WeEVCoveEu4csyomQaLXuLQfeQKFhmb90j8o1fmdxaiT8B3V3h0QkwC2Ia3j4llzhnq8pIv7iJJvmAP9bCfH7I26jIvTLGNRzrcRbxbVBke/lg2vOFzOyty6L1FBlQ6x7+eg/kmzHAvbqx9Q1I9bN2P2SRQ1UawSSrpeooifLTMW7fLjJ4iP3lZ3vxC7cXpniFtAYfYaNaS+i1HGSCDCNiVdlpcfWg0RxIkWdTgJMCvmRYco8OxXqMblgQsDRueGNw856MnjPZ4rfgk40BN2pGyLABX4QEXzGeLVkkjqLyc7xok29+GoMmiejfR60DQIfywqLsWRyku0ej+McIBLQM05DWW7XSZPIWcqZmtivr6EFRg+dl3cTZrlYUns8Qq0+ZEdPdfFj1wWOUpL617B1uUC7lBVLnO8u09kGNc63NHbMq4cbDvlJ9uyflhtsPFHej0HFImfFW8H4gyEuhn8e/r7IuQi6JT7Az8EsUvN/nXGUVgryH6ApTc3zYxfWx8/qM8ZKwq57RYjm0cNIzvsbMKbmNHyfRjB1ct69Le2ogIh1+X8lTDbL5G3bqPoEsFonyP8r9An/dreay4tQfBJNQbhIr5WMz5bwtOcnVBlDpomgp7mvlSiSITLK/FRuZ2UWxUeiqyqzyeREfkFcmsPHSEV0J+WndFzVIH6pzgd+MLUWC+XFAqcpRgKbC19hwnjZ9cGBaLhgylw2t9OQDHXVZ21X/Fhs0vxc8pFPBxCXQuQd0x1kxrLlivmBtUjx 5DymEY8L bPR9FkzWxLbZ/NL1RzsDW2KXjILE4GEJLU34+1UxBsCY6s8w9Oljq+oplZpEYmdxoUfYmVmmPJcosnN5fzRlExb3g0ia0Lq5oNcdEknFWXqVf1DNkroRygt/LCG+C97LAa380j7aCh/ARyRXL+UnTuiNqFqS8nfvZa/hhSDbS6De65t2II1RIve+2bJsH5VkCmhW2/XXXYcY5/pmulTXQKaTCfdORnwnzWDR/1DwIEGbXVogPIIj+hMOPnkENlYixpchJCNmUCyLGzmWlFqABKOKCrXrnWZIHaG5+HYe3vOtEz3p6gkXs/TJmyQ++3XA01OtQWIx9Wknx5MpTSbWMM/HBu9v64OKZ4cE0nQ6y/JW0SssstN3OAgD4+42gBWoeBNq2oEP5cD+lR2YJ8Rc3ekA7Uw== 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: Extend freader with a flag specifying whether it's OK to cause page fault to fetch file data that is not already physically present in memory. With this, it's now easy to wait for data if the caller is running in sleepable (faultable) context. We utilize read_cache_folio() to bring the desired file page into page cache, after which the rest of the logic works just the same at page level. Suggested-by: Omar Sandoval Cc: Shakeel Butt Cc: Johannes Weiner Signed-off-by: Andrii Nakryiko --- lib/buildid.c | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index 5c869a2a30ab..6b5558cd95bf 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -20,6 +20,7 @@ struct freader { struct page *page; void *page_addr; u64 file_off; + bool may_fault; }; struct { const char *data; @@ -29,12 +30,13 @@ struct freader { }; static void freader_init_from_file(struct freader *r, void *buf, u32 buf_sz, - struct address_space *mapping) + struct address_space *mapping, bool may_fault) { memset(r, 0, sizeof(*r)); r->buf = buf; r->buf_sz = buf_sz; r->mapping = mapping; + r->may_fault = may_fault; } static void freader_init_from_mem(struct freader *r, const char *data, u64 data_sz) @@ -60,6 +62,17 @@ static int freader_get_page(struct freader *r, u64 file_off) freader_put_page(r); r->page = find_get_page(r->mapping, pg_off); + + if (!r->page && r->may_fault) { + struct folio *folio; + + folio = read_cache_folio(r->mapping, pg_off, NULL, NULL); + if (IS_ERR(folio)) + return PTR_ERR(folio); + + r->page = folio_file_page(folio, pg_off); + } + if (!r->page) return -EFAULT; /* page not mapped */ @@ -273,18 +286,8 @@ static int get_build_id_64(struct freader *r, unsigned char *build_id, __u32 *si /* enough for Elf64_Ehdr, Elf64_Phdr, and all the smaller requests */ #define MAX_FREADER_BUF_SZ 64 -/* - * Parse build ID of ELF file mapped to vma - * @vma: vma object - * @build_id: buffer to store build id, at least BUILD_ID_SIZE long - * @size: returns actual build id size in case of success - * - * Assumes no page fault can be taken, so if relevant portions of ELF file are - * not already paged in, fetching of build ID fails. - * - * Return: 0 on success; negative error, otherwise - */ -int build_id_parse_nofault(struct vm_area_struct *vma, unsigned char *build_id, __u32 *size) +static int __build_id_parse(struct vm_area_struct *vma, unsigned char *build_id, + __u32 *size, bool may_fault) { const Elf32_Ehdr *ehdr; struct freader r; @@ -295,7 +298,7 @@ int build_id_parse_nofault(struct vm_area_struct *vma, unsigned char *build_id, if (!vma->vm_file) return -EINVAL; - freader_init_from_file(&r, buf, sizeof(buf), vma->vm_file->f_mapping); + freader_init_from_file(&r, buf, sizeof(buf), vma->vm_file->f_mapping, may_fault); /* fetch first 18 bytes of ELF header for checks */ ehdr = freader_fetch(&r, 0, offsetofend(Elf32_Ehdr, e_type)); @@ -323,6 +326,22 @@ int build_id_parse_nofault(struct vm_area_struct *vma, unsigned char *build_id, return ret; } +/* + * Parse build ID of ELF file mapped to vma + * @vma: vma object + * @build_id: buffer to store build id, at least BUILD_ID_SIZE long + * @size: returns actual build id size in case of success + * + * Assumes no page fault can be taken, so if relevant portions of ELF file are + * not already paged in, fetching of build ID fails. + * + * Return: 0 on success; negative error, otherwise + */ +int build_id_parse_nofault(struct vm_area_struct *vma, unsigned char *build_id, __u32 *size) +{ + return __build_id_parse(vma, build_id, size, false /* !may_fault */); +} + /* * Parse build ID of ELF file mapped to VMA * @vma: vma object @@ -336,8 +355,7 @@ int build_id_parse_nofault(struct vm_area_struct *vma, unsigned char *build_id, */ int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id, __u32 *size) { - /* fallback to non-faultable version for now */ - return build_id_parse_nofault(vma, build_id, size); + return __build_id_parse(vma, build_id, size, true /* may_fault */); } /** From patchwork Tue Jul 30 20:39:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13747870 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 13864C3DA49 for ; Tue, 30 Jul 2024 20:39:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 980EC6B009C; Tue, 30 Jul 2024 16:39:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8E1176B009D; Tue, 30 Jul 2024 16:39:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 734A76B009E; Tue, 30 Jul 2024 16:39:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 547886B009C for ; Tue, 30 Jul 2024 16:39:46 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 179D3A048A for ; Tue, 30 Jul 2024 20:39:46 +0000 (UTC) X-FDA: 82397585172.19.5BF6233 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf05.hostedemail.com (Postfix) with ESMTP id 18EEA100016 for ; Tue, 30 Jul 2024 20:39:43 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=cdF8aL+o; spf=pass (imf05.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=1722371957; 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=R/qI/s5JXHyAce2/cYqT4W+1cshjIee+Wb6EkhER37I=; b=oKM0Rup/eYeVk8/Kkzav9GywDTd/+0kxh6NKTG+EyZcLMbhe6NYj4HTFPl851hFwYtzYkM evn0SBi+ahpR+apKbExi+mch+nbCzHGJDAQMQsOrDzrCriY2Wghf9KZJ44gU58MR8CYwHA 1IQvqtKx8W7YwaDkbH4KZHRR3zDdSaw= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=cdF8aL+o; spf=pass (imf05.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=1722371957; a=rsa-sha256; cv=none; b=ohYl35UT8QQJ8GDFVOQw07mo5HQTXJoL7RveqXpNyfEU+IfuY2VI2MGc+wPvK0U0v/1aAG INA8YDkBG6YjzbYAZWhkqZAINfE2J5Ug1UZmCUblX2Qe80zUUAeYAKnY7lUYLVsNGADyQJ MULgftfFwi3cdQCHhPlG5H83URaQwo8= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 82D38CE116D; Tue, 30 Jul 2024 20:39:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9E189C32782; Tue, 30 Jul 2024 20:39:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722371980; bh=L5Vw+vnTqV23U/YAvF1BJ8hsl5vO8thQWupiLEtL3t0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cdF8aL+omLwMD7juVUd9qTLY6t8I5+wuqDBQM6vN0qVd3vJssbaT6H57XKDCpvhWT BHdr0BHNHgGo1BVoybKHiQn+Gv/W5sjGXkVSmDlZIR+WQVV72ezblveJT5HZSGu6// 8hh3k83fVGCphdLC/DPDTE39Xk/wKQf4sjxssYJ6OUhNAVDS/CwarwoFm/n3i8f28C tCK29d9yFVLjjTqf7pfb0FrQpe57Kc+qBOC1AiDG7295hAENyV6fRc2B71owh7cg+3 889a0VWLJAg5TFaPsoMvSmdOeNgCCeEpqGA9DG9XXIHJn39/COyaKfM7dcZE6mJQ/U MwX2ScXQHjamg== 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, jannh@google.com, Andrii Nakryiko Subject: [PATCH v3 bpf-next 07/10] lib/buildid: don't limit .note.gnu.build-id to the first page in ELF Date: Tue, 30 Jul 2024 13:39:11 -0700 Message-ID: <20240730203914.1182569-8-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730203914.1182569-1-andrii@kernel.org> References: <20240730203914.1182569-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 18EEA100016 X-Stat-Signature: mrm4s7mto1npj4mo3w5qiycqg47pctqd X-HE-Tag: 1722371983-291810 X-HE-Meta: U2FsdGVkX1/hPrNK1UdJwKN+0EOHn/7CfkHdMSvU9AqgTa5q8KYtt6X8lsynsIuxpE7l+6S4YsTjEeMR4uoZ2VopTy/B8+WX7UdVqomEXc0nAZ7fbviP4S5Q98AIuyQpDAEFoJOSRQKIwJ/EX85tYTIO2+880LBohIawvUhDCXkd9LTv36kSghkEg3ZYJnHyGG+gh4pLX0MbMC2wrOFn02Np8fb0u8rr0sP4sTwrib5Fdg8VwDFnTJxOX7sgr4ZGUE1mI1AiZUp31NN+HFSCKEqMSiu7JNG3C4ZXoVsC+LYvqwq4b2PCjgGoqROxxpKtMb2BOLyJcCsdqho/IMe2mAP9AZjNvOgxYCOeaOMHFrG0qRuz7Mgwc1x5yEHV+4erOG6Tl0A/sM9l5CkXfAZc6Mt8dCNZtUm6rUn1Y+2/dfKIhpl+uIDlPHnljE7lwU1n9ivYKAmt6akKVXPc3TJpKgemA+HSHMOVPwsitTuAwQOzCez1F6hw6Z31bM5pOCaxOmZhu+wio/zBw5cLBuWiJKBCEWRaGER4EwQgvfdl1lo85ehJnXjpfIesgxoeCJcb9vvEeQ1I+nT9/m2M2kLa4z8bbFbK8U3dmogZgbYkJvnCNyjEk98tsRxiP9JtoAy7GG7KkIvTV/PVIN87vCVVjQBnIMdwLcDtuRVVRycGkyIFxrtmd4o7Foc/8GxKkGseCC/bJy2CSxenS1CetaAvD4fLbhsvw4+wmR/00pUtJ+MjyBITqtckAD6y4TjXE00DVJYFh7fC22UT/C/bqcHG/WSWQaHnybdalVfql8Ds+SCr/9OuqIOAZBxqLMjjdRNZE4l/Kg40MFbBEv6KT5kg6QMS1r8tVma2lQyTnarXlIyNZZ5hNdAKEBvVigdPBJ5+gHmsTYUPa5c0NQCK4PFHdFM63m4ULZ3PYjdn32Tsef6QeAbseh5i0+iNY+gLau9UePxhtLr1Y0+A6m1UNNh 0rXH2TQU K9m73v6Lz+PxBJRVdx2zuQphWP2pF59gicLySe2LV4hIU2chyFKNpjylau5HsuwQKr9wwbJgLHrrY0MpLWR9AIfkRthZaI5qg3ya5hVk+wxxuzMqb6X0ivHKxjcnUc5iwndyxJNsMQqCTV3PUsGXWQj7opvB3K63i1496wZXaQ2Xt04WiPYjH8pXlvwPvsajvZg4UQnCW6Ny0Ax8td8SKDmf8zFFz7RXQdHU0le3jqI0tM+c= 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: With freader we don't need to restrict ourselves to a single page, so let's allow ELF notes to be at any valid position with the file. We also merge parse_build_id() and parse_build_id_buf() as now the only difference between them is note offset overflow, which makes sense to check in all situations. Signed-off-by: Andrii Nakryiko --- lib/buildid.c | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index 6b5558cd95bf..78cfb3048385 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -155,9 +155,8 @@ static void freader_cleanup(struct freader *r) * 32-bit and 64-bit system, because Elf32_Nhdr and Elf64_Nhdr are * identical. */ -static int parse_build_id_buf(struct freader *r, - unsigned char *build_id, __u32 *size, - u64 note_offs, Elf32_Word note_size) +static int parse_build_id(struct freader *r, unsigned char *build_id, __u32 *size, + u64 note_offs, Elf32_Word note_size) { const char note_name[] = "GNU"; const size_t note_name_sz = sizeof(note_name); @@ -166,6 +165,10 @@ static int parse_build_id_buf(struct freader *r, u32 name_sz, desc_sz; const char *data; + /* check for overflow */ + if (note_offs + note_size < note_offs) + return -EINVAL; + while (note_offs + sizeof(Elf32_Nhdr) < note_end) { nhdr = freader_fetch(r, note_offs, sizeof(Elf32_Nhdr) + note_name_sz); if (!nhdr) @@ -199,23 +202,6 @@ static int parse_build_id_buf(struct freader *r, return -EINVAL; } -static inline int parse_build_id(struct freader *r, - unsigned char *build_id, - __u32 *size, - u64 note_start_off, - Elf32_Word note_size) -{ - /* check for overflow */ - if (note_start_off + note_size < note_start_off) - return -EINVAL; - - /* only supports note that fits in the first page */ - if (note_start_off + note_size > PAGE_SIZE) - return -EINVAL; - - return parse_build_id_buf(r, build_id, size, note_start_off, note_size); -} - /* Parse build ID from 32-bit ELF */ static int get_build_id_32(struct freader *r, unsigned char *build_id, __u32 *size) { From patchwork Tue Jul 30 20:39:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13747871 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 2FE85C52D1D for ; Tue, 30 Jul 2024 20:39:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 69E996B009E; Tue, 30 Jul 2024 16:39:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5D99A6B009F; Tue, 30 Jul 2024 16:39:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4048D6B00A0; Tue, 30 Jul 2024 16:39:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 250886B009E for ; Tue, 30 Jul 2024 16:39:47 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DBB2B1A02FF for ; Tue, 30 Jul 2024 20:39:46 +0000 (UTC) X-FDA: 82397585172.13.7AD4B05 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf26.hostedemail.com (Postfix) with ESMTP id 3A36414000D for ; Tue, 30 Jul 2024 20:39:45 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ooSUaTfo; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.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=1722371940; a=rsa-sha256; cv=none; b=1B0h/GA7XP+R9zdCXKTlehPDxtyoHY6VG9UJOAKKI+Dey9Qtt+rvt34WtvZlWqqXQqJPic +twxAbvRypF76YhBIn3oZimUKmJC4YVG7Jen2utsITs9BxJ1h7dljkOLijMVcNWaQAVPpK KCL3xU5zmy1yLGUnz0ZPR1Pxe9q46D8= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ooSUaTfo; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.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=1722371940; 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=gsmzGpKK8QXJw069hvnjOrZZkhs0zSJy4R5UDJ6ztho=; b=UgFCC1sy9naUR2k6H2TKv0lT4NF7CTRpN3fBo9mLcULAj1aacvSdRygMCgKPyMkL5yp+yX GhYL5FsUKcGO3bq14O+vDstoaMxi0FtzWYVECG0jN2QGyP5WRsT2Nkb0W8RY3pAyvrrr7i BzP9rkP6Ctw2Mk30z8rasAoVXtxkVTc= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 67A8E62099; Tue, 30 Jul 2024 20:39:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DF8C4C32782; Tue, 30 Jul 2024 20:39:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722371984; bh=kcQeVuVwnIJJkBHc/ynMBCpPQQtY5kY6QBTEhCYYFCI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ooSUaTfoxg4BnOjxonwU4jTV2NlsxEsnJbQEIMx1S5NXBk0VnZzmWBCoC1ochnkEk ODT5Tu1NJnAfEAeKD0u5O2Vbvro6khSWgkgFxNrCwiW1YUngObJmg4hqodbSB9yH6Y MYrcQttRlvVE23r1WCkIryOsgk5onMReNjzNh1heBvYRQbUZAO+aqfFC3jXI114YDE c48Q9FgCBaKZTKnrewvdhIdR7Px8kq0PZClmdJ4CNfjLlJa9yUSaUphDtRYRDHKuX+ 81rxk6gGt5JbJ5UtGalyQhgc02XUF+AewrLlM3+xCnaMhibYMeLNOdAeZ5yr7/cgN6 BJc3uwIFQDq8g== 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, jannh@google.com, Andrii Nakryiko Subject: [PATCH v3 bpf-next 08/10] bpf: decouple stack_map_get_build_id_offset() from perf_callchain_entry Date: Tue, 30 Jul 2024 13:39:12 -0700 Message-ID: <20240730203914.1182569-9-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730203914.1182569-1-andrii@kernel.org> References: <20240730203914.1182569-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 3A36414000D X-Stat-Signature: z5ugz9kqredxd4kf6pp3s3e8fdeqtdng X-Rspam-User: X-HE-Tag: 1722371985-388130 X-HE-Meta: U2FsdGVkX195ajeraZjLH79mIuacLtS2Ii+SoTSWCAIPevZ0AQb2UK5k2HxT7kqA12nZV3pgBaPvR7TjvvZG0rQRKaR5yaWGcynrzX/RNVpoZx7Yy6hKZ5wzL1RIW9Ox1XVMyzF1+eUO2w2qUkPJRRPuSmmZd5BRfOU5nEsOCctoLLLXQiPzGGDDH97oNd/z+uhbTWzI7SX0IdKt95ePENIFFbblYC4vvfoDz6aBY1+N4JV4pmStUeJ1PYNbAS5CqAcn4RhhSrxaeFlVz0AgRtP8b6YnEkiU1coKHMK966NG5TGwgwLpZFyyaLuoR6sbzUhboUrmjeECVwmuhOgDI8TdpuQpxg1JLL8zjRdGkCrkbnro/DWrIRAhZ9Row7GWdUqrolMXAZfUqpOmijPfQ28w26qA9OKG8n58yxZKvV5iKRiweu37GtpkLP1B4dHfDIBg7XrTdWgsyzrAfl9LtnWOJtZfDj6swmzCxmHDbibtrJo4YsEZB0uHgeY/+twxGOn+r0nxuYlC3HPNnPpniAC0JoC+JBtnNgicawz+sFjkPbIwu3zUBFfsiD+F+mM+MUSX21IbL/XBdAgroH1Q14lmzasz1wH4un0SND68855o9o1EsoQKpo53SKF2TjKME6U0+tcQPf4+Igl2rQOzCEvk7ljISW9JWgSP2x3o1OmMQ1lg99bfVtSIe60OsNhOWMucFibsd83lFTDbUPNiCzEONj3oRPSMAYbV2B5Fof1Y8FEkpo6RtnMnR5lWqC3ymHnJXZ0gp3rl6ZJh2qmyME+7/7mw2GLQYgPnr5xF/vtw/83yT8fkrVxO/XMS9ULLFdaHS+sk0qeaVmg7pwdtEd1O8uVRw425RCg5AeCX5EmYcrcK7OTR+0IPHBOLih2uqauQjs5uppvwG/iqGxM4brwsFz3QhL/E6rGtL5ciwaMb7JQ4qhN0woZvc0vlkcPfzszAStuRwlz4ITDG+Xv vXkhbUu3 RDNOnBHkuAzg2O3ionvJScEg7P93lDQkXv4ARA2OjnGFA2Eyx/rBEC5xg6bjwiDPLr7FFXri+gTMr89nEjaDGEO14cC6EbGnhrOPQDXoqNXaFAAVyVW+iFpjcJgOGBDCWeQDcpvDZgDZYAGTwgwQQ8WWiUGVVtqvidI2CN1svEoyNr2rbQjp4VD2hq7T5v+uHM/xE3aMYCayYI3v585Chf5JifrTxlfuXj5urSkzv4X4oH+pLOU4IRm1XVBb0SITxzIq5nVrVmJ9oyJepw4uBvhL1b+5TGtJyRgFd 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: Change stack_map_get_build_id_offset() which is used to convert stack trace IP addresses into build ID+offset pairs. Right now this function accepts an array of u64s as an input, and uses array of struct bpf_stack_build_id as an output. This is problematic because u64 array is coming from perf_callchain_entry, which is (non-sleepable) RCU protected, so once we allows sleepable build ID fetching, this all breaks down. But its actually pretty easy to make stack_map_get_build_id_offset() works with array of struct bpf_stack_build_id as both input and output. Which is what this patch is doing, eliminating the dependency on perf_callchain_entry. We require caller to fill out bpf_stack_build_id.ip fields (all other can be left uninitialized), and update in place as we do build ID resolution. We make sure to READ_ONCE() and cache locally current IP value as we used it in a few places to find matching VMA and so on. Given this data is directly accessible and modifiable by user's BPF code, we should make sure to have a consistent view of it. Signed-off-by: Andrii Nakryiko --- kernel/bpf/stackmap.c | 49 +++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index 770ae8e88016..6457222b0b46 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -124,8 +124,18 @@ static struct bpf_map *stack_map_alloc(union bpf_attr *attr) return ERR_PTR(err); } +/* + * Expects all id_offs[i].ip values to be set to correct initial IPs. + * They will be subsequently: + * - either adjusted in place to a file offset, if build ID fetching + * succeeds; in this case id_offs[i].build_id is set to correct build ID, + * and id_offs[i].status is set to BPF_STACK_BUILD_ID_VALID; + * - or IP will be kept intact, if build ID fetching failed; in this case + * id_offs[i].build_id is zeroed out and id_offs[i].status is set to + * BPF_STACK_BUILD_ID_IP. + */ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs, - u64 *ips, u32 trace_nr, bool user) + u32 trace_nr, bool user) { int i; struct mmap_unlock_irq_work *work = NULL; @@ -142,30 +152,28 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs, /* cannot access current->mm, fall back to ips */ for (i = 0; i < trace_nr; i++) { id_offs[i].status = BPF_STACK_BUILD_ID_IP; - id_offs[i].ip = ips[i]; memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX); } return; } for (i = 0; i < trace_nr; i++) { - if (range_in_vma(prev_vma, ips[i], ips[i])) { + u64 ip = READ_ONCE(id_offs[i].ip); + + if (range_in_vma(prev_vma, ip, ip)) { vma = prev_vma; - memcpy(id_offs[i].build_id, prev_build_id, - BUILD_ID_SIZE_MAX); + memcpy(id_offs[i].build_id, prev_build_id, BUILD_ID_SIZE_MAX); goto build_id_valid; } - vma = find_vma(current->mm, ips[i]); + vma = find_vma(current->mm, ip); if (!vma || build_id_parse_nofault(vma, id_offs[i].build_id, NULL)) { /* per entry fall back to ips */ id_offs[i].status = BPF_STACK_BUILD_ID_IP; - id_offs[i].ip = ips[i]; memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX); continue; } build_id_valid: - id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i] - - vma->vm_start; + id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ip - vma->vm_start; id_offs[i].status = BPF_STACK_BUILD_ID_VALID; prev_vma = vma; prev_build_id = id_offs[i].build_id; @@ -216,7 +224,7 @@ static long __bpf_get_stackid(struct bpf_map *map, struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map); struct stack_map_bucket *bucket, *new_bucket, *old_bucket; u32 skip = flags & BPF_F_SKIP_FIELD_MASK; - u32 hash, id, trace_nr, trace_len; + u32 hash, id, trace_nr, trace_len, i; bool user = flags & BPF_F_USER_STACK; u64 *ips; bool hash_matches; @@ -238,15 +246,18 @@ static long __bpf_get_stackid(struct bpf_map *map, return id; if (stack_map_use_build_id(map)) { + struct bpf_stack_build_id *id_offs; + /* for build_id+offset, pop a bucket before slow cmp */ new_bucket = (struct stack_map_bucket *) pcpu_freelist_pop(&smap->freelist); if (unlikely(!new_bucket)) return -ENOMEM; new_bucket->nr = trace_nr; - stack_map_get_build_id_offset( - (struct bpf_stack_build_id *)new_bucket->data, - ips, trace_nr, user); + id_offs = (struct bpf_stack_build_id *)new_bucket->data; + for (i = 0; i < trace_nr; i++) + id_offs[i].ip = ips[i]; + stack_map_get_build_id_offset(id_offs, trace_nr, user); trace_len = trace_nr * sizeof(struct bpf_stack_build_id); if (hash_matches && bucket->nr == trace_nr && memcmp(bucket->data, new_bucket->data, trace_len) == 0) { @@ -445,10 +456,16 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task, copy_len = trace_nr * elem_size; ips = trace->ip + skip; - if (user && user_build_id) - stack_map_get_build_id_offset(buf, ips, trace_nr, user); - else + if (user && user_build_id) { + struct bpf_stack_build_id *id_offs = buf; + u32 i; + + for (i = 0; i < trace_nr; i++) + id_offs[i].ip = ips[i]; + stack_map_get_build_id_offset(buf, trace_nr, user); + } else { memcpy(buf, ips, copy_len); + } if (size > copy_len) memset(buf + copy_len, 0, size - copy_len); From patchwork Tue Jul 30 20:39:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13747872 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 D7D20C3DA49 for ; Tue, 30 Jul 2024 20:39:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6E77A6B00A1; Tue, 30 Jul 2024 16:39:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 697A46B00A2; Tue, 30 Jul 2024 16:39:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 538146B00A3; Tue, 30 Jul 2024 16:39:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 345416B00A1 for ; Tue, 30 Jul 2024 16:39:53 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id E47E01A04D7 for ; Tue, 30 Jul 2024 20:39:52 +0000 (UTC) X-FDA: 82397585424.23.DB64302 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf12.hostedemail.com (Postfix) with ESMTP id AB25D40018 for ; Tue, 30 Jul 2024 20:39:50 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GbZd1qMe; spf=pass (imf12.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=1722371963; a=rsa-sha256; cv=none; b=Y8RGYeclCje7toFiGrPmyneUr5X9jkfMC0t5d6pUBwBgCKvCjuu0LsIJ+gLoSXW4+Ec3KT PSFk6yfkzmndnOhq5/DHGHst5/Z83R91Ugp/jVtzcRoiWWWwN8WOrkxDKbSv5smIDJEiPy 0cksS8Xba8phjVqPmdA1tPaqECJny1U= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GbZd1qMe; spf=pass (imf12.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=1722371963; 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=eExReJV3MWwy+C+SUD+K7fZstGShidKTlNcMIOPUk4w=; b=sgtV7TwM7naDO8USiwjT8FiMKrEChqWpXa9G6bagfC2hXRfOR1K7qJPToInDqt7TPhe3jF 1t3PlXyMmh5wcYgkRy7+tVr1N7cmuyWXZK1WzlsjbvQWAWLqqzGW39TWj7y++q4e89ff78 dzZEOjnTVwK1RKElOPEeMc8nReyEL4o= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 1BF73CE122B; Tue, 30 Jul 2024 20:39:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 32118C32782; Tue, 30 Jul 2024 20:39:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722371987; bh=1bAvoqjPKLvvb/1ljVcpsvHiDgEE6PzS0WvM7NPI00U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GbZd1qMes8zndT70VXhtBjwDxcrJ06b5lz/qd/C5mE0UFwA7BDp327NCm1az6YVLY h865Ho7OmJu5gjGCygsCeIKayaRurn/pE8stpJ0ZA0HUjCxMx0I/WTjzn/heQvYHZT Mvu1DEUjkRZW8BjbvFPKZcJVwef0pu5YiNMTy47NIr9t4k18HPaZbuhzi9K5vtyXc8 75tVXEK+laJdCH62p+cTx/Nj3mppZoJ/yI70+mE2u/k5i6Ndmpf4NtzhMDACLCPyZA 2IMHEa2mxMY08z3oRz2TMUb4/mE/lD0aOX0jMYymJrDwLotPua+7WALOsKiZJrnR9x U7e7MEwkBp99g== 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, jannh@google.com, Andrii Nakryiko Subject: [PATCH v3 bpf-next 09/10] bpf: wire up sleepable bpf_get_stack() and bpf_get_task_stack() helpers Date: Tue, 30 Jul 2024 13:39:13 -0700 Message-ID: <20240730203914.1182569-10-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730203914.1182569-1-andrii@kernel.org> References: <20240730203914.1182569-1-andrii@kernel.org> MIME-Version: 1.0 X-Stat-Signature: uo73gcq7kzobzh7taqfe784e7uqebwb3 X-Rspamd-Queue-Id: AB25D40018 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1722371990-477750 X-HE-Meta: U2FsdGVkX1/kl+Tc25bMhjKLLUZtR5ES3eZyantmDk5BG5FHnVC1QJegOzELxgT0lFA964ufGSGRF/eXxyJWHMtnJ5snNU35wj4gZEvw2oyUN0qzSdLYHuJqgltSDJB+3+Np3mUCuPOJCSNoz7OxIU1unmDqrt/7aZrJVKlGmsaQdXo/QyUjivDMCZeT4rixKezbo3xwHnsac7LwVp5n5T8JObc3uq8BR0sUSQAIkMqrgfAqiYLJJ4SUH5mdqjjq22cVu0sjfEGKAqudBimB9EKOUEr06GrvMMZESk9kZBiYFE4E9QsCFAjuEFSKWYxFTh+d/9ZlMkPPaKMDxclXio/CVj9/90er/GXW1ynw2z8VSH2DviqMucuLlIu2m7f/z0N+WaBxtBvmDgqzyuQPY+jxckbeifwq8avganBOL/tfPsAGMEBCoNIaLknwOhr5+Kw6qquMgBo8hzb+MxhE0/yIpqmKUvb4TLO9AFo4ZlqS4+5klzDH2DMqPXuXdAfiqQ8K9QpUtfX+xHU7HUbio9g+2ubhIHmbNvjDq4kwhoZxvpkqMjp/rX9v6Rh6WubCiYqEEYRaxhtoqLDogoQxUq1CZCuS7e+6o58EpT1d3EK/yKJNEYslX7hNhbLuQaXng3zi7eaNbHQ65YLIHWNJy/jZRGucf1Er2wSLmWbA+OfNRtS8Y9FLOPJOIRocCDgXzqvYAk/PfGmPw5yF1Gz1cVnLaaZxt94d+OCnJxU4L6Olqs1EsfPaqBWNMIxTv/4dJHA2z95XOAg0X+CTHo/wbdWIRdZsla+d8npwBQauS8Mu3vplORtDhMKLWrPpzq2QO8O7C/MBEFPm3z4TPHkoow/55TWPqukytEBEV3d+hEmG4FH1q7FUrFrZWHyX/nNFuDPWkrr2AukQDSEjvJnT4BzD7V3cpAns3oy1hVp9FrDxZMfEmVl9UQ7i+RJTLB3YAt35mWcg/JBckb5Z7Gk VbfxFL9c yNgV/J5QLc7UzeZUtgX8GeDBYKjXm0d73rnCHOtaCkxKkYQaZxTubN+s06xYcPsVKMUMwbx4LGvE/LGExIJBIBGPweqtJHu0w8vWTVIVwnMf0ezF/gOGQpKFIkF1jzceAr8+7VuS5EPWBhdCEEJ3NdByqzy9Rp318PRISJRoePUnsRMctNnpI5dh1O5TTzvIilgFqMJ/MiJ22GMlTx2kKA5q+/bjJSZWxKhUF8hS+XdlKrFfiWKUVMXck/MxI8QiLndsFBCAcvXLZcuNd2xV/IZUpJOMIMdPfFQIb 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: Add sleepable implementations of bpf_get_stack() and bpf_get_task_stack() helpers and allow them to be used from sleepable BPF program (e.g., sleepable uprobes). Note, the stack trace IPs capturing itself is not sleepable (that would need to be a separate project), only build ID fetching is sleepable and thus more reliable, as it will wait for data to be paged in, if necessary. For that we make use of sleepable build_id_parse() implementation. Now that build ID related internals in kernel/bpf/stackmap.c can be used both in sleepable and non-sleepable contexts, we need to add additional rcu_read_lock()/rcu_read_unlock() protection around fetching perf_callchain_entry, but with the refactoring in previous commit it's now pretty straightforward. We make sure to do rcu_read_unlock (in sleepable mode only) right before stack_map_get_build_id_offset() call which can sleep. By that time we don't have any more use of perf_callchain_entry. Note, bpf_get_task_stack() will fail for user mode if task != current. And for kernel mode build ID are irrelevant. So in that sense adding sleepable bpf_get_task_stack() implementation is a no-op. It feel right to wire this up for symmetry and completeness, but I'm open to just dropping it until we support `user && crosstask` condition. Signed-off-by: Andrii Nakryiko --- include/linux/bpf.h | 2 + kernel/bpf/stackmap.c | 90 ++++++++++++++++++++++++++++++++-------- kernel/trace/bpf_trace.c | 5 ++- 3 files changed, 77 insertions(+), 20 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index b9425e410bcb..0f3dc903bea8 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -3198,7 +3198,9 @@ extern const struct bpf_func_proto bpf_get_current_uid_gid_proto; extern const struct bpf_func_proto bpf_get_current_comm_proto; extern const struct bpf_func_proto bpf_get_stackid_proto; extern const struct bpf_func_proto bpf_get_stack_proto; +extern const struct bpf_func_proto bpf_get_stack_sleepable_proto; extern const struct bpf_func_proto bpf_get_task_stack_proto; +extern const struct bpf_func_proto bpf_get_task_stack_sleepable_proto; extern const struct bpf_func_proto bpf_get_stackid_proto_pe; extern const struct bpf_func_proto bpf_get_stack_proto_pe; extern const struct bpf_func_proto bpf_sock_map_update_proto; diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index 6457222b0b46..3615c06b7dfa 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -124,6 +124,12 @@ static struct bpf_map *stack_map_alloc(union bpf_attr *attr) return ERR_PTR(err); } +static int fetch_build_id(struct vm_area_struct *vma, unsigned char *build_id, bool may_fault) +{ + return may_fault ? build_id_parse(vma, build_id, NULL) + : build_id_parse_nofault(vma, build_id, NULL); +} + /* * Expects all id_offs[i].ip values to be set to correct initial IPs. * They will be subsequently: @@ -135,7 +141,7 @@ static struct bpf_map *stack_map_alloc(union bpf_attr *attr) * BPF_STACK_BUILD_ID_IP. */ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs, - u32 trace_nr, bool user) + u32 trace_nr, bool user, bool may_fault) { int i; struct mmap_unlock_irq_work *work = NULL; @@ -166,7 +172,7 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs, goto build_id_valid; } vma = find_vma(current->mm, ip); - if (!vma || build_id_parse_nofault(vma, id_offs[i].build_id, NULL)) { + if (!vma || fetch_build_id(vma, id_offs[i].build_id, may_fault)) { /* per entry fall back to ips */ id_offs[i].status = BPF_STACK_BUILD_ID_IP; memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX); @@ -257,7 +263,7 @@ static long __bpf_get_stackid(struct bpf_map *map, id_offs = (struct bpf_stack_build_id *)new_bucket->data; for (i = 0; i < trace_nr; i++) id_offs[i].ip = ips[i]; - stack_map_get_build_id_offset(id_offs, trace_nr, user); + stack_map_get_build_id_offset(id_offs, trace_nr, user, false /* !may_fault */); trace_len = trace_nr * sizeof(struct bpf_stack_build_id); if (hash_matches && bucket->nr == trace_nr && memcmp(bucket->data, new_bucket->data, trace_len) == 0) { @@ -398,7 +404,7 @@ const struct bpf_func_proto bpf_get_stackid_proto_pe = { static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task, struct perf_callchain_entry *trace_in, - void *buf, u32 size, u64 flags) + void *buf, u32 size, u64 flags, bool may_fault) { u32 trace_nr, copy_len, elem_size, num_elem, max_depth; bool user_build_id = flags & BPF_F_USER_BUILD_ID; @@ -416,8 +422,7 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task, if (kernel && user_build_id) goto clear; - elem_size = (user && user_build_id) ? sizeof(struct bpf_stack_build_id) - : sizeof(u64); + elem_size = user_build_id ? sizeof(struct bpf_stack_build_id) : sizeof(u64); if (unlikely(size % elem_size)) goto clear; @@ -438,6 +443,9 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task, if (sysctl_perf_event_max_stack < max_depth) max_depth = sysctl_perf_event_max_stack; + if (may_fault) + rcu_read_lock(); /* need RCU for perf's callchain below */ + if (trace_in) trace = trace_in; else if (kernel && task) @@ -445,28 +453,35 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task, else trace = get_perf_callchain(regs, 0, kernel, user, max_depth, crosstask, false); - if (unlikely(!trace)) - goto err_fault; - if (trace->nr < skip) + if (unlikely(!trace) || trace->nr < skip) { + if (may_fault) + rcu_read_unlock(); goto err_fault; + } trace_nr = trace->nr - skip; trace_nr = (trace_nr <= num_elem) ? trace_nr : num_elem; copy_len = trace_nr * elem_size; ips = trace->ip + skip; - if (user && user_build_id) { + if (user_build_id) { struct bpf_stack_build_id *id_offs = buf; u32 i; for (i = 0; i < trace_nr; i++) id_offs[i].ip = ips[i]; - stack_map_get_build_id_offset(buf, trace_nr, user); } else { memcpy(buf, ips, copy_len); } + /* trace/ips should not be dereferenced after this point */ + if (may_fault) + rcu_read_unlock(); + + if (user_build_id) + stack_map_get_build_id_offset(buf, trace_nr, user, may_fault); + if (size > copy_len) memset(buf + copy_len, 0, size - copy_len); return copy_len; @@ -481,7 +496,7 @@ static long __bpf_get_stack(struct pt_regs *regs, struct task_struct *task, BPF_CALL_4(bpf_get_stack, struct pt_regs *, regs, void *, buf, u32, size, u64, flags) { - return __bpf_get_stack(regs, NULL, NULL, buf, size, flags); + return __bpf_get_stack(regs, NULL, NULL, buf, size, flags, false /* !may_fault */); } const struct bpf_func_proto bpf_get_stack_proto = { @@ -494,8 +509,24 @@ const struct bpf_func_proto bpf_get_stack_proto = { .arg4_type = ARG_ANYTHING, }; -BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf, - u32, size, u64, flags) +BPF_CALL_4(bpf_get_stack_sleepable, struct pt_regs *, regs, void *, buf, u32, size, + u64, flags) +{ + return __bpf_get_stack(regs, NULL, NULL, buf, size, flags, true /* may_fault */); +} + +const struct bpf_func_proto bpf_get_stack_sleepable_proto = { + .func = bpf_get_stack_sleepable, + .gpl_only = true, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_PTR_TO_UNINIT_MEM, + .arg3_type = ARG_CONST_SIZE_OR_ZERO, + .arg4_type = ARG_ANYTHING, +}; + +static long __bpf_get_task_stack(struct task_struct *task, void *buf, u32 size, + u64 flags, bool may_fault) { struct pt_regs *regs; long res = -EINVAL; @@ -505,12 +536,18 @@ BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf, regs = task_pt_regs(task); if (regs) - res = __bpf_get_stack(regs, task, NULL, buf, size, flags); + res = __bpf_get_stack(regs, task, NULL, buf, size, flags, may_fault); put_task_stack(task); return res; } +BPF_CALL_4(bpf_get_task_stack, struct task_struct *, task, void *, buf, + u32, size, u64, flags) +{ + return __bpf_get_task_stack(task, buf, size, flags, false /* !may_fault */); +} + const struct bpf_func_proto bpf_get_task_stack_proto = { .func = bpf_get_task_stack, .gpl_only = false, @@ -522,6 +559,23 @@ const struct bpf_func_proto bpf_get_task_stack_proto = { .arg4_type = ARG_ANYTHING, }; +BPF_CALL_4(bpf_get_task_stack_sleepable, struct task_struct *, task, void *, buf, + u32, size, u64, flags) +{ + return __bpf_get_task_stack(task, buf, size, flags, true /* !may_fault */); +} + +const struct bpf_func_proto bpf_get_task_stack_sleepable_proto = { + .func = bpf_get_task_stack_sleepable, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_BTF_ID, + .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK], + .arg2_type = ARG_PTR_TO_UNINIT_MEM, + .arg3_type = ARG_CONST_SIZE_OR_ZERO, + .arg4_type = ARG_ANYTHING, +}; + BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx, void *, buf, u32, size, u64, flags) { @@ -533,7 +587,7 @@ BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx, __u64 nr_kernel; if (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)) - return __bpf_get_stack(regs, NULL, NULL, buf, size, flags); + return __bpf_get_stack(regs, NULL, NULL, buf, size, flags, false /* !may_fault */); if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK | BPF_F_USER_BUILD_ID))) @@ -553,7 +607,7 @@ BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx, __u64 nr = trace->nr; trace->nr = nr_kernel; - err = __bpf_get_stack(regs, NULL, trace, buf, size, flags); + err = __bpf_get_stack(regs, NULL, trace, buf, size, flags, false /* !may_fault */); /* restore nr */ trace->nr = nr; @@ -565,7 +619,7 @@ BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx, goto clear; flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip; - err = __bpf_get_stack(regs, NULL, trace, buf, size, flags); + err = __bpf_get_stack(regs, NULL, trace, buf, size, flags, false /* !may_fault */); } return err; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index cd098846e251..c3845470f56d 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1598,7 +1598,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_jiffies64: return &bpf_jiffies64_proto; case BPF_FUNC_get_task_stack: - return &bpf_get_task_stack_proto; + return prog->sleepable ? &bpf_get_task_stack_sleepable_proto + : &bpf_get_task_stack_proto; case BPF_FUNC_copy_from_user: return &bpf_copy_from_user_proto; case BPF_FUNC_copy_from_user_task: @@ -1654,7 +1655,7 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_get_stackid: return &bpf_get_stackid_proto; case BPF_FUNC_get_stack: - return &bpf_get_stack_proto; + return prog->sleepable ? &bpf_get_stack_sleepable_proto : &bpf_get_stack_proto; #ifdef CONFIG_BPF_KPROBE_OVERRIDE case BPF_FUNC_override_return: return &bpf_override_return_proto; From patchwork Tue Jul 30 20:39:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13747873 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 56A21C3DA7F for ; Tue, 30 Jul 2024 20:39:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D77A96B00A2; Tue, 30 Jul 2024 16:39:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D01666B00A3; Tue, 30 Jul 2024 16:39:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B53FF6B00A4; Tue, 30 Jul 2024 16:39:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 9433B6B00A2 for ; Tue, 30 Jul 2024 16:39:56 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4DB711403BA for ; Tue, 30 Jul 2024 20:39:56 +0000 (UTC) X-FDA: 82397585592.16.E42939A Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf20.hostedemail.com (Postfix) with ESMTP id 1489C1C003D for ; Tue, 30 Jul 2024 20:39:53 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=inVn7XtL; spf=pass (imf20.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=1722371951; 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=Q6kRgKMCYaeghSrjWZ53Jpf6B3s/o7dA9biqliGz4KE=; b=5GylzMifFyRjjm4LXWF+1Wer0Mc0WqoKvuwUXESZsNAtZNML8iPrx3Ry+370u8IiKCD4eQ 35szThAE3z88E+bHnrCovccKfNorwTwWnLdsmva5rsyIwaXyzQ9VMYYoZ+2V5DyNdRkaXY BCuaA+mrlreQ+IVe0xrNZftjaQfK5uE= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=inVn7XtL; spf=pass (imf20.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=1722371951; a=rsa-sha256; cv=none; b=znWgi01dTjiAFHK2oj6DyD9lStQGIiomS7vyHvP21tfi46wdgrYjYPCMRWPQFyNoGWlshy KIHdP5OOw9LCsbhZmorkRg1c9ZHwBWhB81a3+1fIfFHA0uCTsqoDDBPvZA+4JInRG0+qFt Y+nb022pBZC2QaDd3bOHiU9VRC7IXD8= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 611D9CE0EBB; Tue, 30 Jul 2024 20:39:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7199CC4AF09; Tue, 30 Jul 2024 20:39:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722371990; bh=bF5NCCZIudcv6Sjcsm7SKwUrOwNwKupYTUidvARQ2tE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=inVn7XtL/4n4bBi8XPa2SGuEngY7tf58YGjSJQHdXlAf2OewsqAe29vsDCt6RMuum GeEbfhxsm92qu4/+SB4zUqptq4i2mMkgwz91fNlKRNFF4ZVoWqkOP940+CohnFngKY 4RNh9SDO4f4AltE6A0zE4UWINE7fI3Vr0FbTdJfh1aqGe+SnIfIbnEE5vcRHF0M6xU pOzNHzWutWdnbAwLtC5N3wgCQaqR1PrJ+M7oDOPOMqdQh3unkkOXCcLjD5+ESC12RZ avtWi4hCIR9YC3m1Tc1J87l5LHA47KnO1lgpIR2+sCpYwRZnnIZQf65kojBEE9qq4W U5CUEaqd2qLcw== 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, jannh@google.com, Andrii Nakryiko Subject: [PATCH v3 bpf-next 10/10] selftests/bpf: add build ID tests Date: Tue, 30 Jul 2024 13:39:14 -0700 Message-ID: <20240730203914.1182569-11-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730203914.1182569-1-andrii@kernel.org> References: <20240730203914.1182569-1-andrii@kernel.org> MIME-Version: 1.0 X-Stat-Signature: ti75i1pmf46y3xhey1mqbc86u57fk5ea X-Rspam-User: X-Rspamd-Queue-Id: 1489C1C003D X-Rspamd-Server: rspam02 X-HE-Tag: 1722371993-66658 X-HE-Meta: U2FsdGVkX1/Y3HzjDDWPwfd4bPOkSyXNAw4mCzLf5WXYz2T9C6YfdSumRGCmMeMv8a7iBqaf3/y+dd0vUS9CIhu8B16++k9JdWNT4hpK+B7gMS55Fq0c2TJmY5qbS/k7v5uTgHJJpSJ9ZSvBgjf24isggZWDwlvCa2HgABTBcfCCr8WACq/mPe79uP6KJB/cD7yWBWTHEJtXZaGD4bFEsthAhbGT1NbMGGAjxBLLtesiF4jrSERlSgkmfwokvyPY2+FlGrIYVoe2Oovq18DUjmLxox40QDICHOhw0Avpgq9Ws/7ADKXdwhXZWJwZDzFIS6i961Ns5gPYLYA79wTH09Xg8NuixVRhxm7Tw4hVccXDZbyJQyJXB0ZPRWRRj9+39Fz04PkMM20gVRxNF4c2Bey5Yk4bKa5AU6oPEYFbIUlrLDM1imqY7ychumiZZ4a8ybkgBo0bFRz5zLm6PSlkQvGVx9GDwSR+7fXFK8iNAWerGZbgTTl5EE3zEx6gTW1r70A9zYZdlmEydA0IiE1Z3tjKyPiBg7K7BbndpbuMcNa98DKa7Dzdpst7wbPu2BAsArsY5wOGv2aqJ5EeGPDl4dE6AszokEI80CLFXQwOWldrDjI4cCsaoHr/7YtKbAgLLmtzhvdtsm/TqRYk9SKkTywwi38UOZuGZ357smRZxUCHMnBfMeQ14SelnRrVlxKHATK68Kt6LzVPAcLOwqziMSrYpO6av58OM7gw9IvxlpGfO1qA8ceo9zjJXpTxRJ//FSTVxO9X+vL5+H62vKnNcmg3ROi4GVwxhOVOtoqDXYl3e/y/FLCFOvfEjKbWmZu63vG3CJQ0ww72oJaKC6KAxcBH5XuFyOV5q38OHjJhAFKkDPsNBGJLNxnFnh2iW2BSAUiKoINJw50QG8RCkE811IQ1T7ZIF7opFFFvO5dX9aqMto8Q35TBK5pDMp08u2L/G4HHotXMbaGnZJ/pqe5 AnkzNrb6 ou2fG93RelF1bafwY5ehCa5oRrBGUPW5nOcu9U4wz7ZZpNwk20rHhFbU20SMMfwMhCUE7ReDvenv/1yCDSElOm87QDLHwe+ARk5ynHY1fRIQSQu8Stef+8/PehZwAX/QogUI1sSne7VfZ7C9QQvczXIoXH7N0jaQlv/JE0ML6KRZHYstl7htQ40v21JVML3FN/MwPtedhmzSBdV8OJHnvndswbHGzHY42oDHUUClXetJ4ss0= 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: Add a new set of tests validating behavior of capturing stack traces with build ID. We extend uprobe_multi target binary with ability to trigger uprobe (so that we can capture stack traces from it), but also we allow to force build ID data to be either resident or non-resident in memory (see also a comment about quirks of MADV_PAGEOUT). That way we can validate that in non-sleepable context we won't get build ID (as expected), but with sleepable uprobes we will get that build ID regardless of it being physically present in memory. Also, we add a small add-on linker script which reorders .note.gnu.build-id section and puts it after (big) .text section, putting build ID data outside of the very first page of ELF file. This will test all the relaxations we did in build ID parsing logic in kernel thanks to freader abstraction. Signed-off-by: Andrii Nakryiko --- 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 ++ 5 files changed, 204 insertions(+), 2 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 diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 1d7a62e7deff..2a89e989c75a 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -789,9 +789,10 @@ $(OUTPUT)/veristat: $(OUTPUT)/veristat.o # Linking uprobe_multi can fail due to relocation overflows on mips. $(OUTPUT)/uprobe_multi: CFLAGS += $(if $(filter mips, $(ARCH)),-mxgot) -$(OUTPUT)/uprobe_multi: uprobe_multi.c +$(OUTPUT)/uprobe_multi: uprobe_multi.c uprobe_multi.ld $(call msg,BINARY,,$@) - $(Q)$(CC) $(CFLAGS) -O0 $(LDFLAGS) $^ $(LDLIBS) -o $@ + $(Q)$(CC) $(CFLAGS) -Wl,-T,uprobe_multi.ld -O0 $(LDFLAGS) \ + $(filter-out %.ld,$^) $(LDLIBS) -o $@ EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \ prog_tests/tests.h map_tests/tests.h verifier/tests.h \ diff --git a/tools/testing/selftests/bpf/prog_tests/build_id.c b/tools/testing/selftests/bpf/prog_tests/build_id.c new file mode 100644 index 000000000000..aec9c8d6bc96 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/build_id.c @@ -0,0 +1,118 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ +#include + +#include "test_build_id.skel.h" + +static char build_id[BPF_BUILD_ID_SIZE]; +static int build_id_sz; + +static void print_stack(struct bpf_stack_build_id *stack, int frame_cnt) +{ + int i, j; + + for (i = 0; i < frame_cnt; i++) { + printf("FRAME #%02d: ", i); + switch (stack[i].status) { + case BPF_STACK_BUILD_ID_EMPTY: + printf("\n"); + break; + case BPF_STACK_BUILD_ID_VALID: + printf("BUILD ID = "); + for (j = 0; j < BPF_BUILD_ID_SIZE; j++) + printf("%02hhx", (unsigned)stack[i].build_id[j]); + printf(" OFFSET = %llx", (unsigned long long)stack[i].offset); + break; + case BPF_STACK_BUILD_ID_IP: + printf("IP = %llx", (unsigned long long)stack[i].ip); + break; + default: + printf("UNEXPECTED STATUS %d ", stack[i].status); + break; + } + printf("\n"); + } +} + +static void subtest_nofault(bool build_id_resident) +{ + struct test_build_id *skel; + struct bpf_stack_build_id *stack; + int frame_cnt; + + skel = test_build_id__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + skel->links.uprobe_nofault = bpf_program__attach(skel->progs.uprobe_nofault); + if (!ASSERT_OK_PTR(skel->links.uprobe_nofault, "link")) + goto cleanup; + + if (build_id_resident) + ASSERT_OK(system("./uprobe_multi uprobe-paged-in"), "trigger_uprobe"); + else + ASSERT_OK(system("./uprobe_multi uprobe-paged-out"), "trigger_uprobe"); + + if (!ASSERT_GT(skel->bss->res_nofault, 0, "res")) + goto cleanup; + + stack = skel->bss->stack_nofault; + frame_cnt = skel->bss->res_nofault / sizeof(struct bpf_stack_build_id); + if (env.verbosity >= VERBOSE_NORMAL) + print_stack(stack, frame_cnt); + + if (build_id_resident) { + ASSERT_EQ(stack[0].status, BPF_STACK_BUILD_ID_VALID, "build_id_status"); + ASSERT_EQ(memcmp(stack[0].build_id, build_id, build_id_sz), 0, "build_id_match"); + } else { + ASSERT_EQ(stack[0].status, BPF_STACK_BUILD_ID_IP, "build_id_status"); + } + +cleanup: + test_build_id__destroy(skel); +} + +static void subtest_sleepable(void) +{ + struct test_build_id *skel; + struct bpf_stack_build_id *stack; + int frame_cnt; + + skel = test_build_id__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + skel->links.uprobe_sleepable = bpf_program__attach(skel->progs.uprobe_sleepable); + if (!ASSERT_OK_PTR(skel->links.uprobe_sleepable, "link")) + goto cleanup; + + /* force build ID to not be paged in */ + ASSERT_OK(system("./uprobe_multi uprobe-paged-out"), "trigger_uprobe"); + + if (!ASSERT_GT(skel->bss->res_sleepable, 0, "res")) + goto cleanup; + + stack = skel->bss->stack_sleepable; + frame_cnt = skel->bss->res_sleepable / sizeof(struct bpf_stack_build_id); + if (env.verbosity >= VERBOSE_NORMAL) + print_stack(stack, frame_cnt); + + ASSERT_EQ(stack[0].status, BPF_STACK_BUILD_ID_VALID, "build_id_status"); + ASSERT_EQ(memcmp(stack[0].build_id, build_id, build_id_sz), 0, "build_id_match"); + +cleanup: + test_build_id__destroy(skel); +} + +void serial_test_build_id(void) +{ + build_id_sz = read_build_id("uprobe_multi", build_id, sizeof(build_id)); + ASSERT_EQ(build_id_sz, BPF_BUILD_ID_SIZE, "parse_build_id"); + + if (test__start_subtest("nofault-paged-out")) + subtest_nofault(false /* not resident */); + if (test__start_subtest("nofault-paged-in")) + subtest_nofault(true /* resident */); + if (test__start_subtest("sleepable")) + subtest_sleepable(); +} diff --git a/tools/testing/selftests/bpf/progs/test_build_id.c b/tools/testing/selftests/bpf/progs/test_build_id.c new file mode 100644 index 000000000000..32ce59f9aa27 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_build_id.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ + +#include "vmlinux.h" +#include + +struct bpf_stack_build_id stack_sleepable[128]; +int res_sleepable; + +struct bpf_stack_build_id stack_nofault[128]; +int res_nofault; + +SEC("uprobe.multi/./uprobe_multi:uprobe") +int uprobe_nofault(struct pt_regs *ctx) +{ + res_nofault = bpf_get_stack(ctx, stack_nofault, sizeof(stack_nofault), + BPF_F_USER_STACK | BPF_F_USER_BUILD_ID); + + return 0; +} + +SEC("uprobe.multi.s/./uprobe_multi:uprobe") +int uprobe_sleepable(struct pt_regs *ctx) +{ + res_sleepable = bpf_get_stack(ctx, stack_sleepable, sizeof(stack_sleepable), + BPF_F_USER_STACK | BPF_F_USER_BUILD_ID); + + return 0; +} + +char _license[] SEC("license") = "GPL"; diff --git a/tools/testing/selftests/bpf/uprobe_multi.c b/tools/testing/selftests/bpf/uprobe_multi.c index 7ffa563ffeba..c7828b13e5ff 100644 --- a/tools/testing/selftests/bpf/uprobe_multi.c +++ b/tools/testing/selftests/bpf/uprobe_multi.c @@ -2,8 +2,21 @@ #include #include +#include +#include +#include +#include #include +#ifndef MADV_POPULATE_READ +#define MADV_POPULATE_READ 22 +#endif + +int __attribute__((weak)) uprobe(void) +{ + return 0; +} + #define __PASTE(a, b) a##b #define PASTE(a, b) __PASTE(a, b) @@ -75,6 +88,30 @@ static int usdt(void) return 0; } +extern char build_id_start[]; +extern char build_id_end[]; + +int __attribute__((weak)) trigger_uprobe(bool build_id_resident) +{ + int page_sz = sysconf(_SC_PAGESIZE); + void *addr; + + /* page-align build ID start */ + addr = (void *)((uintptr_t)&build_id_start & ~(page_sz - 1)); + + /* to guarantee MADV_PAGEOUT work reliably, we need to ensure that + * memory range is mapped into current process, so we unconditionally + * do MADV_POPULATE_READ, and then MADV_PAGEOUT, if necessary + */ + madvise(addr, page_sz, MADV_POPULATE_READ); + if (!build_id_resident) + madvise(addr, page_sz, MADV_PAGEOUT); + + (void)uprobe(); + + return 0; +} + int main(int argc, char **argv) { if (argc != 2) @@ -84,6 +121,10 @@ int main(int argc, char **argv) return bench(); if (!strcmp("usdt", argv[1])) return usdt(); + if (!strcmp("uprobe-paged-out", argv[1])) + return trigger_uprobe(false /* page-out build ID */); + if (!strcmp("uprobe-paged-in", argv[1])) + return trigger_uprobe(true /* page-in build ID */); error: fprintf(stderr, "usage: %s \n", argv[0]); diff --git a/tools/testing/selftests/bpf/uprobe_multi.ld b/tools/testing/selftests/bpf/uprobe_multi.ld new file mode 100644 index 000000000000..a2e94828bc8c --- /dev/null +++ b/tools/testing/selftests/bpf/uprobe_multi.ld @@ -0,0 +1,11 @@ +SECTIONS +{ + . = ALIGN(4096); + .note.gnu.build-id : { *(.note.gnu.build-id) } + . = ALIGN(4096); +} +INSERT AFTER .text; + +build_id_start = ADDR(.note.gnu.build-id); +build_id_end = ADDR(.note.gnu.build-id) + SIZEOF(.note.gnu.build-id); +