From patchwork Tue Aug 13 00:29:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13761164 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 1B1D8C3DA7F for ; Tue, 13 Aug 2024 00:29:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 385356B008C; Mon, 12 Aug 2024 20:29:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 334CD6B0092; Mon, 12 Aug 2024 20:29:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D5C76B0095; Mon, 12 Aug 2024 20:29:42 -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 EDA9C6B008C for ; Mon, 12 Aug 2024 20:29:41 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 64C041C234C for ; Tue, 13 Aug 2024 00:29:41 +0000 (UTC) X-FDA: 82445338962.15.9101AC4 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf07.hostedemail.com (Postfix) with ESMTP id 7AE6440002 for ; Tue, 13 Aug 2024 00:29:39 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=gKQzHeWL; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf07.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=1723508901; 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=+3ksPisYxpunLtk3nSVJozUpJgQ3tQE4FIrE5KmUQPc=; b=A/T20KDH7RhXoceOgweXCv8NK112LVVG3Y+A7Qy6e9VPg33a0dCJn0fez9O1Iq+pMGdErv /7IB44AkZlqsAB0A7BzwJcLjc9IQ+SKN54V/lulGlwGRC3m2IMNI9r6uBZ8WT/aD7YGoHi XU+Pw0J9fwS3OP3RCe2v2gG+j6Nt6jU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723508901; a=rsa-sha256; cv=none; b=Rvdfvsy3tnqrgUNbaIr6QWLueLP/ZdZVe+z9rcsUtPRHpZQJLw+bMgGLi9N+yrV5oBxo95 mKQ1xI5Yx4ZD1BidA0yb/E5af+H/aTi+rfUYGoNsKyUhYMdRSdUvqwsZ07Im10iRgJ80BZ L0/wYc8dNXpb8sKJPxFIdibIna1csWY= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=gKQzHeWL; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf07.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=andrii@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 65D36614F6; Tue, 13 Aug 2024 00:29:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DDE5EC4AF0D; Tue, 13 Aug 2024 00:29:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723508978; bh=d6OgdgeHC+wk/lu9I8MQmLFJs8ltuhWEZxsTMlHyPcE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gKQzHeWLGEXSBj28UKOhogBjjfgKREy89e3zbQHdc2ymmXrDkkZ8QFoKEIxLfi08u 9jEXKq6AMG/wgO3jx720R9CpQKXK7rYYZMZnWYF7a6gocNP8PbvovNE2mtUbZtD08n gXHs5QykOrm6BmEfFA7Y7vFYzbd8xYM14VnDtNgf/YkykOiy+lEvv2OLzj5Igo9TjD kjC0VnPbEZboUpV83Pnzwvt17tMEAUvu8yDMMmkUbgmt9hdoRZk2aIzApO1MJ4rc8Y dyWswqg+Bstkrrkw4GynQVlIwnwmKW2a8KKed3vMTPfb+u7CHjaWHpu1zd76r5wrkc isglTPPnF42og== 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, linux-fsdevel@vger.kernel.org, willy@infradead.org, Andrii Nakryiko , stable@vger.kernel.org Subject: [PATCH v5 bpf-next 01/10] lib/buildid: harden build ID parsing logic Date: Mon, 12 Aug 2024 17:29:23 -0700 Message-ID: <20240813002932.3373935-2-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240813002932.3373935-1-andrii@kernel.org> References: <20240813002932.3373935-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 7AE6440002 X-Stat-Signature: gapub9ep8e85j9o4p7oa1wt5gf5np4uf X-Rspam-User: X-HE-Tag: 1723508979-988456 X-HE-Meta: U2FsdGVkX19DFIRmMJ2Z/zSq4UaiShz8EX0/PcIQ68298SuZli9nqxM4sGcsY8blhtml6mse1pogUmrXeVAmWwCJEwQLadPIk6conA4vOjq5wJNTn/dKANPjmJVFB4z5/VjpyNZcdeeLlfhqUFqp0OLgO10TLynENKcjUTdJMk6bMA/mBZd+lzENqumA0vnQ4oQpqqX+BWAOCGnrLYstQZl22YTWKiTnE6IAq9aW1CnFsqLhz1kvwHMbzvmtnTruFLgfVAwbd9YXi+q0dTRkQOv40D155Tb3tYX35P3h0dbs7gd90L5IYiNHUmJSmhaPn1bLXxES17AkzIvntAsaGWw5Mrk320b0Q+SqAVcH2+ADoB6B5azIs/xhzhVR5oFhIpvi9I5VTcb8IWxkP+ytaElFolTQamPJug9thOIpEuzZJm4IH02Ru7o4xBSHF+1ME0ECDsqCA/0mq/Ck2yWNMhjmXDR2lsV139GUMbqSndO3cBsfyNnPPpPAoyKhCRNEUPYYPWQqUzJ4weotZJ2ut0k6xKDfQC6M1RrgJyrx1i0Had7MPT87jzSJ6ZCGe1Bh64w80WaxWqpokoD7qeYuGw6TXcW5QqCaBDf4+VrJhCusDEoaBIof36ozVZy3gfZLQ9+//dkNmVTCByMRigIqLPjznsWG2drE+EXyA1897PSBcTrwaBf6OGIBDQcel52alfvQEzpgQS02P6OqOR4iqZms9sdwZ9EgFRmjME8e4hsyYFs0YJooZm33bqNQZdL0W5keSF47Sum9+l6lMkDc59sPMuax7x0ccDxi+2lMQoDu7S/YSZMp8xqousPtOchUlJpC0y73VxfP9ihYoC5KwMzQ+Oe3gC9ytWhQoRrX7YOMq6iMpofy4ejx6dN9px0Q+VdcAuVcCIx/w4jN+ShthxyxHWI1LhBeTosSL8aeLfT1AH4/I08vLrL3R/ie21p0o7jq6dNF77E76XllFjE YepkKn2L y7/+EaE8PSgZaa0YVmBNgGQeYPLJYvitNKwh1qKkfJ9mHyZz5xSHL67uO5cHo+YeCkkKIWK4RHf+vbtwRA7RhCZ9E70XQvuFZ2O/jp80M7radVROJ4ygZVwGjgMzFrS4DZ1Uz40JJEyE2/jn5+QklWcq1xmirUfnvBs3RMrczMxVnP5nOMFSS8R0tJ51YgF+DlIJIfDuQPDLYVYLXVBxM9Y2V/oYsCvwZA6WTQas2WWmWobMeP7nyp+PZmw2g/tqJ4LjdBjkhYKNjRc9MYgx8Bsm2oIJEyrDWYimM 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. Also, as pointed out by Andi Kleen, we need to make sure that entire ELF note is within a page bounds, so move the overflow check up and add an extra note_size boundaries validation. 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 Reviewed-by: Andi Kleen Reviewed-by: Jann Horn --- lib/buildid.c | 74 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index e02b5507418b..61bc5b767064 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -18,31 +18,37 @@ static int parse_build_id_buf(unsigned char *build_id, const void *note_start, Elf32_Word note_size) { - Elf32_Word note_offs = 0, new_offs; - - while (note_offs + sizeof(Elf32_Nhdr) < note_size) { - Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs); + const char note_name[] = "GNU"; + const size_t note_name_sz = sizeof(note_name); + u64 note_off = 0, new_off, name_sz, desc_sz; + const char *data; + + while (note_off + sizeof(Elf32_Nhdr) < note_size && + note_off + sizeof(Elf32_Nhdr) > note_off /* overflow */) { + Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_off); + + name_sz = READ_ONCE(nhdr->n_namesz); + desc_sz = READ_ONCE(nhdr->n_descsz); + + new_off = note_off + sizeof(Elf32_Nhdr); + if (check_add_overflow(new_off, ALIGN(name_sz, 4), &new_off) || + check_add_overflow(new_off, ALIGN(desc_sz, 4), &new_off) || + new_off > note_size) + break; 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_off + 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); - if (new_offs <= note_offs) /* overflow */ - break; - note_offs = new_offs; + + note_off = new_off; } return -EINVAL; @@ -71,7 +77,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 +86,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 +97,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 +110,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 +119,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; @@ -152,6 +160,10 @@ int build_id_parse(struct vm_area_struct *vma, unsigned char *build_id, page = find_get_page(vma->vm_file->f_mapping, 0); if (!page) return -EFAULT; /* page not mapped */ + if (!PageUptodate(page)) { + put_page(page); + return -EFAULT; + } ret = -EINVAL; page_addr = kmap_local_page(page); From patchwork Tue Aug 13 00:29:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13761166 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 5E00AC3DA7F for ; Tue, 13 Aug 2024 00:29:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1B25A6B0098; Mon, 12 Aug 2024 20:29:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 13B766B009A; Mon, 12 Aug 2024 20:29:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ED0566B009E; Mon, 12 Aug 2024 20:29:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C78586B0098 for ; Mon, 12 Aug 2024 20:29:44 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 5EBE01A03C1 for ; Tue, 13 Aug 2024 00:29:44 +0000 (UTC) X-FDA: 82445339088.25.F621E96 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf11.hostedemail.com (Postfix) with ESMTP id B1D2D4000D for ; Tue, 13 Aug 2024 00:29:42 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=c78WwWVs; spf=pass (imf11.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=1723508889; 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=R02lG4JsTtz4O73AWsgUfBEMK6B7lybY1NOF3GCEqhQ=; b=kJG2TKDC7UNVj998ngQ9DEOjq3I3eL9uHynaPOH1KhOLOAEcD5tLoF9gUhJFi2sSCRhM1P 1qyOGtaZ8iyGDWuzpEndpAQREc9gHRFtcV2qifbJjp8Tch8v5PmScPiM48crP/s4K0I+uq 03gEU8YFtN8IbdZnevYs0W2PsVjOo2U= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=c78WwWVs; spf=pass (imf11.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=1723508889; a=rsa-sha256; cv=none; b=7+91Aiw3SRrxT4kEC7N2pBkEFqER3TYVHnA723YTKsqzRdB4/CFZNVOexbGeu2rliJOVXh 22urbFgpNbLL+ewSxEC4DOzI8SVl/HgEpc9sSbVURZrJaD+QuqYR3t0U35an4RX6S3sEE8 f1tDX0/9lKuPDeSjQV8BNM4+boI0bQo= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id B6B8F614FE; Tue, 13 Aug 2024 00:29:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 326FCC4AF10; Tue, 13 Aug 2024 00:29:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723508981; bh=B0OY7f2l4vKlZSnGt1e+mFZE65T/zxC1eKfdeyNEXOU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=c78WwWVsQlvlZLhVAQoOr5J1igm9cbwLgJ8aATu0Fwm65xgdUwJcfCAth9Ed+F/Pd RQVImVaucViWeR9rFg2SYfrla9hXL68q1eKVaeoTVv9u33Osv1wCw5ESV4bvQtdp2Q Eo/tu3CnmVoVk1rtJfp1B4ZX+S9ipK81f7h0yze8+1ZUzB7MLev8PRN2p6QSRMSFRA Fnryn+F3zkd4z2tYuaZYGFW92P0rb9rJtfcplpRC7VDc48OYKX13kps6C6GVmUx18b 91eLNAaEKYPenr2K8ELCezKnKZk3yMyXjHxqroy2rGSNK/yd8Sp96L36qg6/rRHFb8 y7fE5TA15/fpA== 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, linux-fsdevel@vger.kernel.org, willy@infradead.org, Andrii Nakryiko Subject: [PATCH v5 bpf-next 02/10] lib/buildid: add single folio-based file reader abstraction Date: Mon, 12 Aug 2024 17:29:24 -0700 Message-ID: <20240813002932.3373935-3-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240813002932.3373935-1-andrii@kernel.org> References: <20240813002932.3373935-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: B1D2D4000D X-Stat-Signature: kdueq315c81hwshkd3cwc16eg9tdzmbe X-Rspam-User: X-HE-Tag: 1723508982-813921 X-HE-Meta: U2FsdGVkX1/WCgy5L3tQHPy0EU9+tCTaTGE2L0aP5PElgApstuEDopa27hKwOaEL57IIRdJW3ojZwkevUCZob5uMHqfwsLmcu+sR6VHZVAW4LLRNzoIZ44kreeRAOBqkBHHzAzp0ykZYGKPFcJLe8ywrvD0LDRf7HRURJcCUZpYwLuKrxT8SGt3ChJ7pC9K34q5PsIyL3ob0Q+/w7t3V5ivBpJtEIOAAQjfOQzCAMS+WdNGREBZaAKk/xeD52lzXObP7/VZ+Ca1hZdbE8+Cyov7Ge6PSJ52oCcAohfXZ7DdTliXXETFinjt9GbDUYguyjH6b8ni0ZKbp7ImAPh82E6rRv4o/yH2zZwC+zoWGXXuvNueHRaNv0zxWACbpU4QiirD8+tI4lS//5u8b67gfQYa//LABsGBonp3MKJRP5ZNoflDj2EneJV438wCseQPb99CibKR3W+t1zsR/vdYfOvFyOPyBFqJtiSqUXcKJCH8Gs5UBbXLU94828m4jVuPJB/Q0UFZGM7s42X1bNLUcvT8I2Oxa/lff7WWcg5H5UPXwD/b2aUDPBMVt4MaeSEqDUfB+imKUi4n6+pmuHiR/BAlMAQsPYmEIj/8vVMZ54XxirqnjzrxfQeqt0P79tNnfR9/fR9bL8VTRpauVqHQj0Y5YGcZaQth05lvc8ZWhVwyLit8pvM9WsEg2kDekaIY0R1sQMC0unuGmbWaYOciiUz9OcyfQ+EiVXY6j/GcGPJrnZ0ID/3X1MLORhr0QYbaLVokbY0j+bAVDVsXCPpuVZ1pGqyOk4zKY8okuDBheW3L+Qg2pVoVlmdSTanap+zi1alKRx8On/rE3bQyn1Xz+QBbScgD4bGOk2LwneQ4407pvNFW5PKUD9WI6XcQtMUqTWPaNdkmddPnmfiFpWMcVwYvvLw4uOax9R5sUQvklQn3WXclRlQESerl+IQLktZnRm6lQT7KXmA3wvzNSEA9 JvFyAsZH s1v/yYJoimoQTbCyY2d/V9Td/PTDGTdC4vryiMEBwW9g9qLakpfF/G52Xs4J9lFZaZsus2qU5vkYqvmav0TZmuAxhLIdmMTIZGn5sxVLx3p6pUyS3sPTrpUvFVXcO7P8VpDLH3i1E7atjzWCalHcRncyhJ1LgQDZtMCVwt5ZpNqYlREY8l3tnrNu9YF2Qhl/XW3niCBBHaySamV3W/WCE9NXZI8Gg/mjYpQPwHhVI9ACNOYY= 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 folio 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 folio 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 (folio) 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. Reviewed-by: Shakeel Butt Signed-off-by: Andrii Nakryiko --- lib/buildid.c | 263 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 210 insertions(+), 53 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index 61bc5b767064..2b1050e99576 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -8,24 +8,155 @@ #define BUILD_ID 3 +struct freader { + void *buf; + u32 buf_sz; + int err; + union { + struct { + struct address_space *mapping; + struct folio *folio; + void *addr; + loff_t folio_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_folio(struct freader *r) +{ + if (!r->folio) + return; + kunmap_local(r->addr); + folio_put(r->folio); + r->folio = NULL; +} + +static int freader_get_folio(struct freader *r, loff_t file_off) +{ + /* check if we can just reuse current folio */ + if (r->folio && file_off >= r->folio_off && + file_off < r->folio_off + folio_size(r->folio)) + return 0; + + freader_put_folio(r); + + r->folio = filemap_get_folio(r->mapping, file_off >> PAGE_SHIFT); + if (IS_ERR(r->folio) || !folio_test_uptodate(r->folio)) { + if (!IS_ERR(r->folio)) + folio_put(r->folio); + r->folio = NULL; + return -EFAULT; + } + + r->folio_off = folio_pos(r->folio); + r->addr = kmap_local_folio(r->folio, 0); + + return 0; +} + +static const void *freader_fetch(struct freader *r, loff_t file_off, size_t sz) +{ + size_t folio_sz; + + /* 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; + } + + /* fetch or reuse folio for given file offset */ + r->err = freader_get_folio(r, file_off); + if (r->err) + return NULL; + + /* if requested data is crossing folio boundaries, we have to copy + * everything into our local buffer to keep a simple linear memory + * access interface + */ + folio_sz = folio_size(r->folio); + if (file_off + sz > r->folio_off + folio_sz) { + int part_sz = r->folio_off + folio_sz - file_off; + + /* copy the part that resides in the current folio */ + memcpy(r->buf, r->addr + (file_off - r->folio_off), part_sz); + + /* fetch next folio */ + r->err = freader_get_folio(r, r->folio_off + folio_sz); + if (r->err) + return NULL; + + /* copy the rest of requested data */ + memcpy(r->buf + part_sz, r->addr, sz - part_sz); + + return r->buf; + } + + /* if data fits in a single folio, just return direct pointer */ + return r->addr + (file_off - r->folio_off); +} + +static void freader_cleanup(struct freader *r) +{ + if (!r->buf) + return; /* non-file-backed mode */ + + freader_put_folio(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, + loff_t note_off, Elf32_Word note_size) { const char note_name[] = "GNU"; const size_t note_name_sz = sizeof(note_name); - u64 note_off = 0, new_off, name_sz, desc_sz; + u32 build_id_off, new_off, note_end, name_sz, desc_sz; + const Elf32_Nhdr *nhdr; const char *data; - while (note_off + sizeof(Elf32_Nhdr) < note_size && - note_off + sizeof(Elf32_Nhdr) > note_off /* overflow */) { - Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_off); + note_end = note_off + note_size; + while (note_end - note_off > sizeof(Elf32_Nhdr) + note_name_sz) { + nhdr = freader_fetch(r, note_off, 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,14 +164,20 @@ static int parse_build_id_buf(unsigned char *build_id, new_off = note_off + sizeof(Elf32_Nhdr); if (check_add_overflow(new_off, ALIGN(name_sz, 4), &new_off) || check_add_overflow(new_off, ALIGN(desc_sz, 4), &new_off) || - new_off > note_size) + new_off > note_end) break; if (nhdr->n_type == 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_off + ALIGN(note_name_sz, 4); + build_id_off = note_off + 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) @@ -54,30 +191,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, + loff_t 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 @@ -87,30 +227,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 @@ -120,23 +265,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 @@ -148,26 +299,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 */ - if (!PageUptodate(page)) { - put_page(page); - return -EFAULT; + 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) @@ -178,12 +328,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; } @@ -197,7 +346,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 Aug 13 00:29:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13761168 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 4B1E3C3DA7F for ; Tue, 13 Aug 2024 00:29:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7CE876B009F; Mon, 12 Aug 2024 20:29:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6E3F16B00A2; Mon, 12 Aug 2024 20:29:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E3386B00A0; Mon, 12 Aug 2024 20:29:52 -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 225CB6B00A0 for ; Mon, 12 Aug 2024 20:29:52 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CB042C035D for ; Tue, 13 Aug 2024 00:29:51 +0000 (UTC) X-FDA: 82445339382.16.A06D041 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf30.hostedemail.com (Postfix) with ESMTP id 8AAC280021 for ; Tue, 13 Aug 2024 00:29:49 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=aFz3Oa0J; spf=pass (imf30.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=1723508921; 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=vaDrr2Mt3NhLIcfB0RKKtZlVExveRPkXlfvOE6WUJgY=; b=gHTCs0/2iCAmuPj2CCVhfhxMlcIT3hq1/Smn99Lpd08dIL40f6jkruA4HcVI9kvN8SWzmf zWYuRjL1gzWZga1nLGrkIqQUleMnR+a86SvECsxN24gZcjTlrW8/wYTQS8sPciB60iyExu 1Ca1PKHKBbk4m+/sKJzMBuMloqyJxLA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723508921; a=rsa-sha256; cv=none; b=1SDOL4EYqlupbPA6LSjYqKMeuu9WhBcf6AQ0DbcH8CH9JlzCTTbivN/MpGvsfsyn5ekm5l uQhh3j8nNU38DT6Ps6OkmP90Y98jgxiRmLQBZ/uEnUoPE7/a39BYwle9PEtdgsmoAkTKGA a4iyIM80Y2ESMIg5eNR2kHF8MXI+Vb4= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=aFz3Oa0J; spf=pass (imf30.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 69BB8CE10FC; Tue, 13 Aug 2024 00:29:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F10EC4AF12; Tue, 13 Aug 2024 00:29:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723508985; bh=qaMr9V+HCCVlztizCv4KeksSMYe9fzp24XbALik8ZKw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aFz3Oa0JPzBYydr1B1rTYWL12SMjsvjDU39bYLcGFfOiGOoxokLHaRnxa8hbl70nM l+2MC47iNIGLaO9kz9+WPs4VLDTb9opO4u8c/slc41g4vXqB+vDJL17siRX3wUdJcn F8NISui0ctqh8H5M44FfyVc+G1fsNRQYudQ8F5oqLgCBI6JKdBFrtWQ7Tkrm2ioUhG 7TjX5RTf/e8Q5Bie/ACJAaNF7hUPgrtCzgoBtAvl8A9ycOqkR+fEHse3vtrkMbqraB HwKWKxHQzKXaGGEer9lNGbY1biagmt06xQ8wJb0sqIDgojpUm8hpWC2rW01MirJlNO bY0JdWZT2LOkw== 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, linux-fsdevel@vger.kernel.org, willy@infradead.org, Andrii Nakryiko Subject: [PATCH v5 bpf-next 03/10] lib/buildid: take into account e_phoff when fetching program headers Date: Mon, 12 Aug 2024 17:29:25 -0700 Message-ID: <20240813002932.3373935-4-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240813002932.3373935-1-andrii@kernel.org> References: <20240813002932.3373935-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 8AAC280021 X-Stat-Signature: ck8xscyt7836puob7zueuhku6mxo8h6a X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1723508989-265788 X-HE-Meta: U2FsdGVkX18JIP1b2OCLGQIGyvP/i1IbpLusoHDapItS4v3F4WdRGVxq+L8TSCQFK9MfQjNszvjNQz7Slt513LCAwm3TD+Va7PHLj9iLtwWu/AdovtPJ6q0EsQ0G92+Tn2XBZJizHjwp4tUf8aAhrBlGd8t8EsdnzadpamaBYyHYS1xHzvUB36nk3NlbVpG/T07JxTpOZcG4dR15Ji156jXTQTLZmL26ivNX/sSbosPu1+m2hbT2GWPvyhHYaQgb0ebkq3SotefBRTU7wP3xuNMHOyZViKOgLpahYcNBtkxMjTSeWp1aJEKvNSCdbrs0BgVFYVsKkxdwsBKNVXphoRnegYzDa7RQqs9czuVsaIT5PZDzGD4qfqyGPsFjBzkJJJB53JtHOy127WrxaQNi4eUcCorEXK2mtIGzFpM9AZNHff+N5OrqQkqT7EOCYCQ9lTAyw1i0YCLkeOc2vrCydsGkvVFj4UJNYQWb0UNx6NXgLrj994aScxiXpev5wsI4750HfDqE1OrD0yI6FwzjqnGg19RzVJSl1IFgjJMAxVd8jlF1tywhgQf6xmnNXD7qOYCUrfvcBnhRb2FfJ8ri3D8YL2aeH78KKdyB07jKSPMWEqa3azxeiLqN3ceEQctrhdRipwcu50QV95eVem5GtcuABGXw+TY1b7UwJwHRF7ZOTQTvziVcCtJPESc5TuGTarpnWchfJnF6lvYNWDQ1MG36Y4tt4gjF/mT9GNagBUCYQVcLK/fSJvx+QgYlf/ATXNks1y7g0QCkumUR9uKt8ZGbAn3eunt+ajXwyg0S3Cx+zkKnfiVFMHCjuqHCtC7+QRm3G+RaNxGkQSkgQM/omT0M2aGl9i1PH5QeMzKhSXx1c7CUnVADBce2RrBT17hCpmVH83vB7ZV+hEuTw4zz6EBcs8qMFiRhhBbWDMIemNRLd7EmZCIm+yftGQaDFnXO+wcnaCuVn7s0wDAVKql 8tRVZUoE viNjuIfR8lcLCNY7O6pfjfJ/ia92WM5r4vE4HQNy6mXgy65R3Mq30hA2gbU/gvQXFFchxEMPqtxTjvZ85VU6wkErMYh69EdZZSLFQ8fptpf8Xa+01bWotiSQ9IF9/JZD1BvgKKhCkYKRopR3Tlp21mHJ01NJnL0XQLlWuJpNydLtrSY9JW7+sJ5G15py/W7XQwYZKD93nCIP0o/QQys1etZQPdEd+ljQeVQVXHSASamrVpIHK0O3waOREIapJ+4C6zFfKBwcPJP46o0ZOVeC3LawTFKp9Gk0liL6CCUUZsCov/hr4vzR24tV6Xg== 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 | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index 2b1050e99576..abf5d1c7eb47 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -213,28 +213,26 @@ 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; + __u32 phnum, phoff, i; 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; + /* check that phoff is not large enough to cause an overflow */ + if (phoff + phnum * sizeof(Elf32_Phdr) < phoff) + 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; @@ -252,27 +250,26 @@ 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; + /* check that phoff is not large enough to cause an overflow */ + if (phoff + phnum * sizeof(Elf64_Phdr) < phoff) + 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 Aug 13 00:29:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13761167 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 C3241C52D7C for ; Tue, 13 Aug 2024 00:29:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F09B6B00A1; Mon, 12 Aug 2024 20:29:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 494416B00A3; Mon, 12 Aug 2024 20:29:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 336166B00A2; Mon, 12 Aug 2024 20:29:52 -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 1261A6B009F for ; Mon, 12 Aug 2024 20:29:52 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 87202C04FE for ; Tue, 13 Aug 2024 00:29:51 +0000 (UTC) X-FDA: 82445339382.11.E424317 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf01.hostedemail.com (Postfix) with ESMTP id E642840017 for ; Tue, 13 Aug 2024 00:29:49 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="YI/np7Sr"; spf=pass (imf01.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=1723508919; 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=AQMFyLVclZzE0k9dmuhFY5zmWyqzaAw4QB8U87ndMB0=; b=8Nktc8416weAK5/ikZNfDyKQ3v3pEtcM5MYhJJou+qv5mgH3wrtD3ogeWnECOJJ7ek6nMe xjy7/U4BaMXG1CJPHqypdpDvTtek9hWeC/kqergJOstqyBxAQBDnLw3M08H8y9Liys/WTs +VEbrcdvbdcli5PpIoo2V+nNEc1E62U= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723508919; a=rsa-sha256; cv=none; b=NRKAD/uAszQq1B/sEq/JzKNp8ZPGZwAyWnjEjAF4GZbSPLom6Llh2ciciXWEc6alIrCgXB 3tujCgDK+9RSAzjShc57q90cG2ITW5o94g1mlp9F1D8ki/xzxq95Lr2pgbygEJG/KE03B9 6Hdr+Df+Efgb1QdxzyMaKLCfnAOMBnE= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="YI/np7Sr"; spf=pass (imf01.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 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 2A99E614FB; Tue, 13 Aug 2024 00:29:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3AC9C4AF0D; Tue, 13 Aug 2024 00:29:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723508988; bh=nj7ojDVOgz/yh4I+l3hU9jrVg8UwzU+SfIEUQDZjSOI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YI/np7Src3nYBmxFhL4ZN6d1xO6aRcrT3FAsB4drJAL3RoGLmHTRvPDsAS4XA08HU Xhgf+d10QVJzZK//pm3vdl9boA+e1z2Hq9y7LLd++fCP6WH6nAswdmJwzOnW+cGa0r yI/1UK7N3142yRwE6Q9DmQHGr9pbHrBkCITnprxtuitLIxseAMBn+9/DF4ur6I4zQy ju+7cvhH+5Nybrk84ux9Wr0zlbNDhdL3wii1SW216oH2pIgI9iz0RhIfJtudv+MOH0 NsA+MGh9R3G6h2jsjPnWoQsiuL+lJS2EmhA+FKu75crSt8AmW4bHOLH3tjZzxJq5Rt s7w7jFFxwILyw== 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, linux-fsdevel@vger.kernel.org, willy@infradead.org, Andrii Nakryiko Subject: [PATCH v5 bpf-next 04/10] lib/buildid: remove single-page limit for PHDR search Date: Mon, 12 Aug 2024 17:29:26 -0700 Message-ID: <20240813002932.3373935-5-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240813002932.3373935-1-andrii@kernel.org> References: <20240813002932.3373935-1-andrii@kernel.org> MIME-Version: 1.0 X-Stat-Signature: ytkjukm77chgqf4nh6eo9uhjxaunpzx7 X-Rspamd-Queue-Id: E642840017 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1723508989-723024 X-HE-Meta: U2FsdGVkX1+kFXl86GxQVnTPmvn+nLmD7hgB1y7cf3oi6v7JjYdrE/lGJ7pvpEKwfA2gMumuvezK0JOd8MWzSaY1Cnd4QTyYrnf/+RPnBS7Nu1l/Fn2Rp7Vup6f22NZwJYHHDDnquOZkXLot9DxXnjoW/9LYpAy4FLMNmOnS9rzjn27szMF4DS+g8w/gnhEToqIQO8j6TMBn2DppAg+DY//eeojmLpcaHVZ6NCVw+87GBWQF5lzYKEDvj/qAVax5bkzRFtzrDOR9yaCov94ONUStNi1lF6XnqxmoThVqgyBXA8EwxBd57zX0uVB2nUOKOa0OcMIHLPTmcAyK1SC6qZ00uE8Dvbyfa6LTvQwQj2DGKF6AppaMbT+08bSWQkj2nwhRz5rEydStnvymk/84dA6RqA7obcqpQe2lhQbVSsVXkt1d8Ujgl4Ag4BXQS/7iO3ZIeEEagvhT4lEG/zHC0BHBQvOQxZxqyH9jOdXEijb7y+SPEjFHMfCKVzJYOpCO33iYnMBU7EhTpSv6z+uNPZgeszYXD7ZkBNINq3OqcAkvr8M668dy8XLweUQiymDqkifKbUlBdHeX+kZGqtUQqNoN3wuTIKH7n7xA7YVYJzvrnHOD6Fs+UKIcsNivpdM9FbOBhlRJ7LInG4kQI9DuvBLVsByvJtWvmRpZ65wypst4W1a32xlXntsXqQbl1fX8M7Ey2yqR7uvVq8mfdb9HXHkdiMB7HtnMc4h7QllyPEb9X0d/6Zk9PQb23OZFjqw4VjhJJsmrFhFQLGoaI3pvAyJ+uDe4VQRAG48S3SFteDyknkAF+/PXFTLGu4cz4odzrC+KrLx+YMKcue6OZAvJTCrrlwLRW8l9Uz9Y4tPT+5R4k4RznGutGuAb5JMiFX+l2qPrl3JKCwMuihknA9B3iQDJy6gDhoBI4idCyTTV3F/C1Ik/xOhRZ2SAuY0rw39+hwR66Pjga5BmL7OcYrW xW4oVdEJ pAliNlPffu0BfR3a0LLDERVL2rTaXF5KxW9QSccLJMhnTXHE4Zdu4u4djE6p3m1aRJsWqefAt5UQ8I8yxMWVzHSziIQS6aKRhhkMssw3Vhx7HV4BnuwgJxt9LGtdUk5RICVbXuw80br4s94T4MJUjVt16fCi0roPGfs3wOayRXLuJGNTqHLtckoz35AzdpQu8pgFQwQ3D4HistiC/Ogkkx8Z9MgZwS/ZmhIIGUhN+t1Q5fEM= 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 abf5d1c7eb47..8296428ff82f 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; @@ -223,9 +225,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; /* check that phoff is not large enough to cause an overflow */ if (phoff + phnum * sizeof(Elf32_Phdr) < phoff) @@ -260,9 +262,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; /* check that phoff is not large enough to cause an overflow */ if (phoff + phnum * sizeof(Elf64_Phdr) < phoff) From patchwork Tue Aug 13 00:29:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13761169 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 B11C6C52D7D for ; Tue, 13 Aug 2024 00:29:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 867646B00A2; Mon, 12 Aug 2024 20:29:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8197E6B00A3; Mon, 12 Aug 2024 20:29:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5F6176B00A4; Mon, 12 Aug 2024 20:29:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 39AC06B00A2 for ; Mon, 12 Aug 2024 20:29:55 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0169FA01AE for ; Tue, 13 Aug 2024 00:29:54 +0000 (UTC) X-FDA: 82445339550.20.E6F1D96 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf25.hostedemail.com (Postfix) with ESMTP id 54A69A000E for ; Tue, 13 Aug 2024 00:29:53 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kWmKvvbg; spf=pass (imf25.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=1723508958; a=rsa-sha256; cv=none; b=Hsectb5mdOsQIkc4XrDraLIFmBiLkOOrIn3oaqUpRz1EiwOZEQsxz9mLVwAqzX73g3pd2v p7DKtLtr4JkFvz5R5Z2/ZqDTDLMi4Q59MjESgYteb2iZbQXJ9kTPXaUjN9F5Ux5LzSHJ1b Luk3wuzabiVlj2EyRtl+zFOPfLZw8DA= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kWmKvvbg; spf=pass (imf25.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=1723508958; 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=7HcBAAcgcY2WobTqYLwgmdZiRTZlvUMyT0dXQBfpf4k=; b=ZTzjkBKKeV1OTgHDfqmA3jnTX5vlZcLIgrcExl399wU3lP8vcMNIDzxbhfQ8SoasuN1MoX nCVbEBJwM4dmpHJhWVmoTQOAWW/sz1Wf2QnzTE335cXZIqRQFNqr+f5obM67EnQoozWBJz bT7eAyoyj94a7E8j1ZFPtXNy+EMQJrM= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 648B5602BF; Tue, 13 Aug 2024 00:29:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ECEFEC4AF0D; Tue, 13 Aug 2024 00:29:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723508992; bh=avz7wz2KTZz1lZ8tpXBxD2kwVdHJhhzASQO4Ib+3tWM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kWmKvvbgOxSygVyyz6LOLRs1rQDdRzkKiw/2pGgzmwGIXdoM68F1FuwTrxVGijkwC Bn0c4fYPsLY7fqUD2KQJyjrGnuMMt+JFTRyiJvvwQTO/Buc6qptLTOo7XhNDfOfZzs J10bg3fkNHIXggVRwTGuYRSJdWWuNDd4IMGLHurhWDDbdgb6t2fA3LB6eGvwYXuIG+ dfY7ph7jLPD6EQcveh4BQyHaoSHTmPLpN8NxiJcljt7UOhuU3aVhpLNzr1EMvrlr7Q mRWID2bIRNPpq7Q1AOqcmjrTlfgScdIIp2434KXjrRekbpEvWtX7XKOlK5+kF3veuW iDGKZ4fUvCmnA== 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, linux-fsdevel@vger.kernel.org, willy@infradead.org, Andrii Nakryiko Subject: [PATCH v5 bpf-next 05/10] lib/buildid: rename build_id_parse() into build_id_parse_nofault() Date: Mon, 12 Aug 2024 17:29:27 -0700 Message-ID: <20240813002932.3373935-6-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240813002932.3373935-1-andrii@kernel.org> References: <20240813002932.3373935-1-andrii@kernel.org> MIME-Version: 1.0 X-Stat-Signature: wxcwuj9mr55n9kgrz8bi93fyyy8uwwqy X-Rspamd-Queue-Id: 54A69A000E X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1723508993-350988 X-HE-Meta: U2FsdGVkX1/n2/kH1p+BPfe24rXp7M6HDDf3UmX28uBGDuZiKtVT0UBR4J5xwIAjQ0bqC5eq5nHZ7U05cKQIrAMW1N8jVd82JgdoqWi/OnQdLI1Ruvo12uGJzU8K2m1p1F2RB7HfVRCTZSNv04qXknfbbH7DxiJy+3/V9S/5s57B49vOA2g/90qcM2KQvAurlkjyfcO0LsDleHbpDa97KiiIZPB+PtpXbCNMIqHT4MJOckd9NdPqC7G/6vqTMWNAFB+EMUcg1SjNhzakkv+tZqN9Car0mVn8XkpVYA3RoSyjCuT6xhBpkUy6DK8I0++wgo85ZWRmbI1IdT0TQ4yhNQLNaQlWRM5vQzQ8t9hp8PRaY+f1NURjHAwYseEc6BOZo2xs+ORF5lq7TElk1AiBr567JHOKnWZIRpeTwzcoolo5LZ7vpj7FygwQBJ27l37vQp/zbTuv+fH6dYIPEouvljq56A/h5SMhCYW/pa9f2moIEvdDt2cKTOwx9bm34BK+Q/bU22btFeNNMuVR439gy5DO1mhtpfs8QRsXK1gIWKIiwpT2aO4JFkJoqn2dHMIKYDKKS36IXGSpyGzzrjB3REpAq9i8ariTP2ESDsCRydSS+/oH5DK62eFJpOqaC3WOy4eOCnLIRHpiHkxetkD36LNZIX6FXoSqZLHubVeiszF2RSSDrTYeIKpCVOJkT3bfhfMsvkjFMQ4j5O6/JfdfFmxuz6AHRDkdWv0S4ip5ps3A/b74lwbR+/mBRTSWly7ugxbkA8CaisvhSU1OMYe87rCa0O843PJh2Uts0j1Y+Cv1JMzIUy90jLe7WFhUoq+r6ER/bDPO8mM3nBe+/7qKVi6PZ0g9Dey481DfDdLSyGzYfJi1fgMbZxpgG6Nm/nbCWyWG+eKGM/7qxEWcg9H0kG7kjFGTn+LoWbJ0Hr2XLjI2Apch3WdXO94TTKIWFkP+Lynqk5wVMrb2tELrNLu kPuShSYa dk2+lPVg9BTIyZiuyDTgMKz0/bxJQv7YT6YxBZgOxHYMpRpiZE7jchW6mnt5U0cHu9Ys8DTPduGEUAhS6Jy75BW5NOOgKTrx4dtkHGC4K7tvaj97Bt/nXOa2jiF0G7WEBxL7T7DVTCKS8UICKPPEBisnL/mwyb8v0GxaEFysPdG6gCGFIYkrGWMOhTYpy2Qe9+saw3PBDmplTlqv9cxE1XbE7ML4l0/4lRuRzGVGC1VaBowzzXRCtl7UYVh8bii7lCGl6u/lkRsNrr3X4pi7EPG7eMtV9nmhR6tLU 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 8296428ff82f..b33d960f085d 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -293,10 +293,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; @@ -335,6 +337,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 Aug 13 00:29:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13761170 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 192DEC52D7C for ; Tue, 13 Aug 2024 00:30:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C9026B00A4; Mon, 12 Aug 2024 20:29:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9782E6B00A5; Mon, 12 Aug 2024 20:29:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7CAA06B00A6; Mon, 12 Aug 2024 20:29:59 -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 5D14A6B00A4 for ; Mon, 12 Aug 2024 20:29:59 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0E8A0404BA for ; Tue, 13 Aug 2024 00:29:59 +0000 (UTC) X-FDA: 82445339718.08.EC483DD Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf28.hostedemail.com (Postfix) with ESMTP id 64050C000D for ; Tue, 13 Aug 2024 00:29:56 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BdGNIV37; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf28.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=1723508944; a=rsa-sha256; cv=none; b=aXdcijQYejIfC/y01YMQX5Rsn+RuYchxqB/tsqpHbNfuQeVzwHx2fQ/VQ+hzS/Pr0VverD mlGCgXRkTR+0uvByP+J0lWtHmtELwGShUle4i+aviMfzM4nrIl99ARGvQL9P26btUqlYbG 5YNg/MKgDhomY0WuTm4e6bdAzkv3lX0= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BdGNIV37; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf28.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=1723508944; 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=ZPeYNgo1u4vJu5mYMXkUi0jro2U95xvxmp2bjs2pbLE=; b=LROL+s1wYQC2agmWjKyt9+PYKjabUdM0pOXvPY+fWYkBgUZS9hPrPj5Kv1tJScUKGcO2Ef 9oizERGQj1fBP+nlphdDsJiJvIjHuUFEfj/V10unDG4iCuJUT5C1ZmC/wabPl1eDBBFpSM R7YF4OAzXwox0MfW9MyL9rcc5EQG3BQ= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 9DAEE614FE; Tue, 13 Aug 2024 00:29:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 31FAEC4AF0D; Tue, 13 Aug 2024 00:29:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723508995; bh=SZpxNEh3KqXS8ONEzMbLb+AANAOpECockBiI1vWhQs8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BdGNIV37CglropA7SFCfluYhFC8BUiLMNPygvEGC2zqaaMPGZB0BtzSg1LPIi2VGH 77bE2OXKjQQDVby2J467VTbFxMYp0QGdG3dR46MvpSDXbjPSxP/NJI5tVLAC9xlRn6 3r0lgI1cFdeUkZi/Wg6gD8qFfJQ2Gqjj3i15Q1NyH5CjvEVyMdy3I+Bx9QhOHOygLw UpA7TRL9VG3kWRyjG3Cioo/DBtnPT5alCopH2BkzZZUSukKBhl3Cx1NC8UyGI9bxaE EUlaW+B8m1DHGynHAIKyi1tlBcfEEUcunTDglOQ77VNikLFPKFYWAnWYsVT2bbWVU7 e94TLmLKqUmmA== 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, linux-fsdevel@vger.kernel.org, willy@infradead.org, Andrii Nakryiko , Omar Sandoval Subject: [PATCH v5 bpf-next 06/10] lib/buildid: implement sleepable build_id_parse() API Date: Mon, 12 Aug 2024 17:29:28 -0700 Message-ID: <20240813002932.3373935-7-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240813002932.3373935-1-andrii@kernel.org> References: <20240813002932.3373935-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 64050C000D X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 7ggokx4kr56gjkzac8pm9b9gcf514f47 X-HE-Tag: 1723508996-356874 X-HE-Meta: U2FsdGVkX1+OeRVgbds0bC9e6ILo7lvMTQgq/Lu+L63vK0lyospQUuV0UW720witr3MJnETy/JADvsLq0K/LZ0MBSF2OMuB7bfHNC02fH9CfvluEkQ5LvFd3qA2uGaRYfDlH4n7QQ1bR9RdAa4TG93rK04RN3Ba2YwEUjtzhaThbKmVtHVI3FtGKBpx7n1AcpTPbQLozv6n7Ca0K6DeUP8dTVOiJX2WbS0QOV3SxfbKooyXg9CCdNPHERu48IrTiOjJ9CQojE58Jzrepubw1KxD3nVfMGwqC/lATfuSaQOYSTulyyrxi2bjhX529uCziDT/XfBK1DACpqNwFnoscwOlCeNprhnNXL9qjwGVLLHchX7UCyRTMR/KkKkglCI59Vud8AHEnI1FLaU13vFJZ3xsynVqcphyokQRUqF+VdRztTqQlsVGaxVN/nzwZRNdldApzfFG1VsTSTxNZZ+5m5pCac1ZeiwZbRarZM8Y5DRHDP9CiH1UeUdTg6b3pmH662c9ZCjWMDk9I9fOXv0vwD7uy3epiX4Usd4HKmAHRQwMyd6sHF9XMOP/iwOShyHn3OrpF+dgdtS4hosRwBt733lhzpBAeRGzMkgGx01WXbVh0TrDJFzpWdDnHQyEvnpkR6RTR+HOTuB2M1SYUnGf50dWh0W5BpPZYJnrPuJqiZqYrD5n02YhXsIeGlb/mAf+7JGUGqZbqnofG3AZIrwOwVQotsSJp7fZbgyzeZ2rsPwQv783sDH8CyiGs4o4R3t88/iyJ2U3FMsuJcd70FD7iubzzfNAQQyZ6b1s6F6G7yqqiKSm4ugX16dt6hOn4g4UWKxDQ6+Jsl2OkEtA4eqLjOhIdA/usXt+SL4OYltlc7HRUWy14m8GLOYLFIUACCjFe8a8Iv58QwIQQksMN9ZIXM3LSCMYdxVy99NQ7KB/4bBPQGyMnrSPTfCUgnXYiTHO87KBlWWrww9RX9N1f7cK jwW+gH82 gOW5dKZKUOsZBoG18mVqWf5UHsR/HYrXa4I6+MYzvTqzvtlUtktewZLEEsIxmYlXN3nx3a9K802NTak7Vy0SuI1waUqcpgtER30MNBkaBc2zy0Oi/oM3iFlK3lHaNHqj2siGPH0ZeHIOVzYR6qIDiemjD7hntvpHMOmNxdy7l/rBfPamSnwnk00cAEi9ve2HAGh/D2wP5YIGyUJ8LRh2Ip3rQbqGrO/rUnFG+1J4I11Zw79oWTRB/d0/U1DJPVZXReEs4/UUpn18JmaPtZV0r+xofFwL6ho6BFbdN6DJoHUsTrcDeMWIBC1aAyRPadBh0BThX98wgrJ4HMEL7+5pd8rVIpXOFVOWrMgWiyGA4DAT1ryy7DntodQ4a+uYta83IQnTspQuLW4NhPZgThOyJYXIG5A== 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 folio into page cache, after which the rest of the logic works just the same at folio level. Suggested-by: Omar Sandoval Cc: Shakeel Butt Cc: Johannes Weiner Signed-off-by: Andrii Nakryiko Reviewed-by: Shakeel Butt --- lib/buildid.c | 52 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index b33d960f085d..e78bef392cfd 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -16,10 +16,11 @@ struct freader { int err; union { struct { - struct address_space *mapping; + struct file *file; struct folio *folio; void *addr; loff_t folio_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 file *file, bool may_fault) { memset(r, 0, sizeof(*r)); r->buf = buf; r->buf_sz = buf_sz; - r->mapping = mapping; + r->file = file; + r->may_fault = may_fault; } static void freader_init_from_mem(struct freader *r, const char *data, u64 data_sz) @@ -62,7 +64,14 @@ static int freader_get_folio(struct freader *r, loff_t file_off) freader_put_folio(r); - r->folio = filemap_get_folio(r->mapping, file_off >> PAGE_SHIFT); + r->folio = filemap_get_folio(r->file->f_mapping, file_off >> PAGE_SHIFT); + + /* if sleeping is allowed, wait for the page, if necessary */ + if (r->may_fault && (IS_ERR(r->folio) || !folio_test_uptodate(r->folio))) { + r->folio = read_cache_folio(r->file->f_mapping, file_off >> PAGE_SHIFT, + NULL, r->file); + } + if (IS_ERR(r->folio) || !folio_test_uptodate(r->folio)) { if (!IS_ERR(r->folio)) folio_put(r->folio); @@ -287,18 +296,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; @@ -309,7 +308,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, may_fault); /* fetch first 18 bytes of ELF header for checks */ ehdr = freader_fetch(&r, 0, offsetofend(Elf32_Ehdr, e_type)); @@ -337,6 +336,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 @@ -350,8 +365,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 Aug 13 00:29:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13761171 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 854B2C3DA7F for ; Tue, 13 Aug 2024 00:30:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 165176B00A8; Mon, 12 Aug 2024 20:30:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0ECB06B00AA; Mon, 12 Aug 2024 20:30:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EF5376B00A9; Mon, 12 Aug 2024 20:30:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D10B76B00A7 for ; Mon, 12 Aug 2024 20:30:04 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4F95C1404EC for ; Tue, 13 Aug 2024 00:30:04 +0000 (UTC) X-FDA: 82445339928.12.E7B8A22 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf18.hostedemail.com (Postfix) with ESMTP id 3DDC21C000B for ; Tue, 13 Aug 2024 00:30:01 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=CSIuTR65; spf=pass (imf18.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=1723508932; 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=lxwDaike7t904dBFVG5osPxj4hb/83rOGME35OaH+P0=; b=FYG/yl8wlQGkV1ggMPbUmiNO/UZK5zlyosJkc7j7jAcLTBr5L1+8TIjvI00GtP0ldb2KnY Ho9ff/aV2J/cSzoq+dnEh3LFiHlD2VAfdMdOPH6K5YBqNj/o3uxJ3YPfVA+VlGjGfZK4Al 6ROGGRyC36atHq6RQNkH8rEkTxm9AuQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723508932; a=rsa-sha256; cv=none; b=ywArOVgAvvElzuM8EDRy51/JO0o5uiFB1WgaUulhmDOZdIPgr7LsNvGYXPZhsy+wbo4r+t 3aDVcWQQGYxIkHP+BUNcMQSzv4gTD/65Ep0m9lndpMb1crGzpXkFjTDYFlLkl+m/sdK1pJ bwO0Gb51Xb5ldaA0ohOQyemHzV5h7R8= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=CSIuTR65; spf=pass (imf18.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 634F6CE10FC; Tue, 13 Aug 2024 00:29:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 717F9C4AF10; Tue, 13 Aug 2024 00:29:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723508998; bh=/aF9doTSwVxK8vkPFzpWOWPf6Y2a+7ple020IwwLDSw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CSIuTR65Xeh7z7t1hE2mLF8gqKL8SUrC6KKO1sp3imVjDaj7n1StQQTBeiTnQvvZ9 NE4meYoSfiKsHoRg6YKXW8JY975TdfKaD20mIzyYa6hDJbsDZo7xX8Hy9/J2WU3MN2 djc03c+xYkX8D0FFlUTuWFZHmqv0lN1ft3s4jf9l5nRDH/3JpIli5CqO38RPbb/9ec YxBNNXOM9Q7SZSsk4CbDavbZFJVXLwWEvPctCtkoKv+uFbu1wwXSeFTe2DeDp1WzjR qs8rR/YE4e1U3XqRd+rvjmyck26bukq9fLRKDiCqyP8u+nW+ll9zfmVn8Z0qOSeiiS afMvKeW9QS8ow== 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, linux-fsdevel@vger.kernel.org, willy@infradead.org, Andrii Nakryiko Subject: [PATCH v5 bpf-next 07/10] lib/buildid: don't limit .note.gnu.build-id to the first page in ELF Date: Mon, 12 Aug 2024 17:29:29 -0700 Message-ID: <20240813002932.3373935-8-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240813002932.3373935-1-andrii@kernel.org> References: <20240813002932.3373935-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3DDC21C000B X-Stat-Signature: eiewjendm974bfsyp3z6mtn4pg4ae38o X-HE-Tag: 1723509001-296544 X-HE-Meta: U2FsdGVkX19e9ng62JjIYTGDs6YPfxAXvdrMFVLNi+Hx25y+fxZ5GpnwpWSrCsoLCgTwQ9K6iYSROFloPyAU3rFpx8zZQLAWuyZ5I31LnqP/hyyYAwgp/mfJg9lShWbfeUJh8hZNcLKZZ1KPqXssVGrWrLygQYzGD7dIRWVJJszxYuEARyFzu6nBD9F9fG4Uz/DtTnK1BOVzzIw+utNAi2ekFB/iiQ8/4YNui5VQAttWiDfingqrwv8gES5Br97C0Dp7xdZ31GnGXAoyPYTDoBZ/Gc7diC8QTwIXBqjFs0AlWOVtvNjS+MLZyIiNFqsJgMgqi+BXO1c+Jsv8qxjD2hjRhboVZIFu+dD7ACC2HIGidqGtlLZBNyyHSYVgWFHPd6504eR1Ph8AfDC+kQZjfIRm0VRdgwzZ5fCLE8QQl9es7kRGGSoonCoACFzSTq8JbQfBsYDQtlfhp6RQtVITETzZN5KMIsvPR/ViPuHZsldYf+MaYEJMcAlA9Yy4N+MJPyX2WTr2U+aHdLVPj6RNfPFmTXw5FKad5glNyQEw9HwBoZzH/j1maGs+7IqsvUxvyCRdt20wpkkdYdb3pL4bVWrIN5Na67m6yJww3JYs9ss2WNoLNxTSGliPqyjDkftjP+xh016r/p0XKkZmV0AWmUJ8pmZnZWtJpb4lmy379BslulDGwhK5dNItWRdy83UpNJAxtyf9E2XmduWQREDdlRrV1L0NCNTDk07in5Ns5kQIfJ8MDVbBYu5b6NxVDqNaP0+s88hRe25m6fHa0sXmy0vDqvO/cYYrh1+QdQC3vgew+smsDhno1y6ykxtPw724wnl50umUNEAwRBHmOcZcGy4vynpFJSwTjMlGoaj7bziXl35rdXo8KX4agwDDAp6VEclGu6CXAS18HMJErW2Kx3gikQLqbLr8nUX0ogQtQ0ywGSsGXfndc62DZDVoyu19bck8/zcKEqUMZpQh4x9 ok1RZP4z b9/1BsmjohRGX95F3xwy4s3Tw/xMCfbJDIiFICLBhshzt/kr5IaDnxV/MY4V25TUHjzBSo2IF5H924peW/OineiDk7p/eHs+o7j79mioiKYnHZ90Srv//399FvzxVoBhFfqpXt10iVxpJ0OXGExlVG83jXeVdHSN1ZsomK7g54b3DQSlNPpJiN/0J1EBl6+99Z6y9Sr0J4LiH5m+jLFMmoopa5kQdfbWML87EixjphPhqXv23u1n8l1sorzn+LTCk3sJYI3hOHw0s7MI= 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, 5 insertions(+), 21 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index e78bef392cfd..3f7a588f7fd2 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -153,9 +153,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, - loff_t note_off, Elf32_Word note_size) +static int parse_build_id(struct freader *r, unsigned char *build_id, __u32 *size, + loff_t note_off, Elf32_Word note_size) { const char note_name[] = "GNU"; const size_t note_name_sz = sizeof(note_name); @@ -163,7 +162,9 @@ static int parse_build_id_buf(struct freader *r, const Elf32_Nhdr *nhdr; const char *data; - note_end = note_off + note_size; + if (check_add_overflow(note_off, note_size, ¬e_end)) + return -EINVAL; + while (note_end - note_off > sizeof(Elf32_Nhdr) + note_name_sz) { nhdr = freader_fetch(r, note_off, sizeof(Elf32_Nhdr) + note_name_sz); if (!nhdr) @@ -202,23 +203,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, - loff_t 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 Aug 13 00:29:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13761172 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 3DC9FC52D7C for ; Tue, 13 Aug 2024 00:30:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5352F6B00A7; Mon, 12 Aug 2024 20:30:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E3136B00A9; Mon, 12 Aug 2024 20:30:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 333F56B00AB; Mon, 12 Aug 2024 20:30:05 -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 000346B00A7 for ; Mon, 12 Aug 2024 20:30:04 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 96E154050D for ; Tue, 13 Aug 2024 00:30:04 +0000 (UTC) X-FDA: 82445339928.12.06DE555 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf14.hostedemail.com (Postfix) with ESMTP id E2ADD100007 for ; Tue, 13 Aug 2024 00:30:02 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=PVo1S07i; spf=pass (imf14.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=1723508932; 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=CLVGxFgFPz6/ZYiF9xeJeHqOy9a7OfXTVZlIu6JEgMo=; b=ARxIqPvsUHF29ZzSJ1w5xF+q99Ke7wF4km+e6E07fomdMxtG+50lKMHWF6FoukDyoT/tWd xRdatML4kL0bb7O0IgZaaiXtcyOYGKo9Mv/ZC3sstQ94tqTe3898VQqnmRStfuTZOcgCrG KxLXUQ763uOsWmsAfY6wdAdCR/jbasY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723508932; a=rsa-sha256; cv=none; b=CqKV8oQ+/jRTBfQa/n/BPPUYS8ow1hKlIl/gsuVGp4N+MBaW3hwsrpB0x60kYQxWQu9/9f 3b/NoVEOFJGXZIhZmzHhjgHefxhNyO1bYKzhIcg7OgTBOxYaW0tZr5olTVQtBhXM3rIZR6 3dqEFNyKw0m0P+KDB3m2O7DOsUshBvo= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=PVo1S07i; spf=pass (imf14.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 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 20047614AB; Tue, 13 Aug 2024 00:30:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F1BEC4AF0D; Tue, 13 Aug 2024 00:30:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723509001; bh=/6b0RVTf764MWC/B03hIXQHQ85gqStIXNWpPwHJJCmw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PVo1S07iPd3+crWVRo+4ztKzbkZXepI35Ym9CjANlzRxoC9ui6sh5XLsaJ57kVHBh nYBaPGq7aSsTB1IVlBhau3itbxhEB4pmaQLXxEOu8XrmymjUhnppU4vV3SkQhCYQKU sq4a84LIvxBVgc6eBJ/Pyx3IOfb4tTf7Kyikl0zre2n8n0dDuzrEtj7FzZM55MyX8c /Q2hG6yDE6fvzQXf4OxYZpPt6R9LwjFa9zZ65a6cUJJUz/UeXvXSVlDYo/tLIrDuDH 0DaJ8Dg84fLqkP/bGZREYi0lFG/K90hcblB7wRW7c9EaJ1pK4bRrni1hvTY0vOe89q v3iE+M5zjMVAA== 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, linux-fsdevel@vger.kernel.org, willy@infradead.org, Andrii Nakryiko Subject: [PATCH v5 bpf-next 08/10] bpf: decouple stack_map_get_build_id_offset() from perf_callchain_entry Date: Mon, 12 Aug 2024 17:29:30 -0700 Message-ID: <20240813002932.3373935-9-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240813002932.3373935-1-andrii@kernel.org> References: <20240813002932.3373935-1-andrii@kernel.org> MIME-Version: 1.0 X-Stat-Signature: 9zrpgc8pwohya7dg98cudmc393hb7bqn X-Rspamd-Queue-Id: E2ADD100007 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1723509002-298023 X-HE-Meta: U2FsdGVkX1/aI9v7tzqOrHxb73OJrctH5RxMFNiVVx8wHTgFwNkJyvPSUybc3GT2pUgfS60LCGBqaOtgBwCTRwWtiSbh9gC34afqqEN60WMBQd54p0crbxL8N4+KzVGoyPf+37j9T1lvF/GRFMqcvlns8ltjfuiyHyJjIqcP26U8OOSQv6+LXb4660NcpXMG2j5G3Sivs1XY7THmGGlI1cFAlFVawmBgL2xT1weCajcwbtYobIqczBSLf+7EBOoycqo4dSZVGEb98CAjdmGTXiiUfDhegbD3CFuf4CyVnSLRDC60SY/d64/S+CBd/C+YiaEyy3p1ID1Zpis9khqh0WyowE0fayU/OnJqcyTAsg7CKVQB/J81pi5nodWRUPfAnqtfnedGwOSKDMui0c/p0xYGIMVZWP8i60cvAZvoWsmfqNidmaUYFyFksj7On3dq5gWUmOlDfeeIMNQC/KzKs+t+Ezs134550eB+c6Q6gQ5hvKS8+3th0Gpj86itrJ+Um4PooaYHlpE0SyCBPAfxCqZQ8OnegYr6GM4TjbXZzwalHJJEDhD1rQFIHRMqnfUDjawusm477e3QDkliKBDUB1fjeZ5VPzr/C7T9YE7A5M6Ll/LE9wZoCiXF+HdSiZC8E6yROi2oEKrY3kRErwJiWYSUW8HhonlAYbz3xhtwkuSSdhDSmQm/zmIvHrMUDDdfM2FyqE9Gi8Fequ3vPtZIrI5/OhBuzIbfQmCReoVk45I4MEC+YVO+LcmEE10S/ecStmAUKneMlb1/VGWrQMFhlCE3xT2LzJE4nGMCTYRYNd57FBYb2Vhl4EFpKv0MX85PbBnR784+x1u0yJ2aKN7evT2dHYLeWDB2UTG2LfOhmnoQR4qZHYLdrSayXa5lwt7eAiCw37Tl6HDn3JznInPIefoBtFLhrwFT4A/oe6QzqxgH+53Fnj8mzINP6i9rDuZ6h02ef5CADeJ8o5RCV+L A98I9Y5l qqNjNowr5z/zzwSIbDpN/C87ecmQK225KllTCt77is+Ti8VLsMhWnlkH4Lm3Dr2B48iDT9Sd3qGphT6dGb42lBySAGsCClKLam1yjK3WnHwX/2UpdPPk+/WNwac768OZqYHnuZJW1stD+Eukwp/usiKENpDVGcdV3Lyp98pNUWejc7SOf675KU2zXuA21l+fqLg903nmOBlLqwLNYXzj5uUiKVQRyxDoo7Ht7qiri12wxRa6NfAJ642LGPpIKJGi9/RrDWK0sdm+Nt8k1L/a+H4kaaD6xVFRnQz9C 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 Aug 13 00:29:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13761173 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 1C852C52D7C for ; Tue, 13 Aug 2024 00:30:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 92E8E6B00AB; Mon, 12 Aug 2024 20:30:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8DA876B00AC; Mon, 12 Aug 2024 20:30:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 77B606B00AD; Mon, 12 Aug 2024 20:30:11 -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 57F2B6B00AB for ; Mon, 12 Aug 2024 20:30:11 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2071EC0508 for ; Tue, 13 Aug 2024 00:30:11 +0000 (UTC) X-FDA: 82445340222.25.D05F288 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf04.hostedemail.com (Postfix) with ESMTP id D51D840016 for ; Tue, 13 Aug 2024 00:30:08 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=IOE0WlsP; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf04.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=1723508998; a=rsa-sha256; cv=none; b=A6eYs4d6kIlCpj8YXxvu5+QR+yFX1jXkup/RVc6s5RYjNtsaq7bu0WxI/nyDEpSz9h5cLf DB718KT9EFR6UESrEByiGCqJA+K5AYrn57kkUEzeHj295FlmX75zThQxe/zsZetcnMX/pF FKiuZ8yj4IjOMhdkVqW2cp8c5AmSXyk= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=IOE0WlsP; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf04.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=1723508998; 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=HCQuDgNMwIx1wjCSA1X7tmVjngXUQohy5kp+d0i3kjE=; b=Ca+VESEret/WKG6qF57jqfkRJya18UFf/GzYFf2ZU66aU25uENf9rZqArVX1KTzl+AAAaz iPPhNuuBVvaAYlS+vp++URC4eLemMr9h+3E0aDJAkNOFX207GHkWflaIS/qkVh1A/wHNBK vZrvOr0PBSu+gxwGiWOUFIlVqJlsSPY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id D2352CE1098; Tue, 13 Aug 2024 00:30:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D2416C4AF0E; Tue, 13 Aug 2024 00:30:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723509005; bh=38WQXgNN+1KvRm9L1MT0/+U7sa1S+7OAXmxtP74KQv8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IOE0WlsPT7hHtvBc/vV3a2BWaMlohF03ecpxp65A1GBe3uBO4wkEUxXmMt/oJk7Sn gqR7Qu4m8mpizX88zwXVZYxnQOS6wEOqwBggfGVG3JXMqKeyCBu+aXZyFhkb9HA7BF KSC7WRwQcU9lbYe8X/uqjM6mJOOHzibXkma8tnO9uab8iQtqCa508RQqfNPJM2awwc QXrqgBLqeBAutRQQfKiBodrM6wMh4M+JUNMC8Lcn/tXkd0RT9OKe77Wy6Zx0g2ARCM SYFjqFxmYEAHx67yAOvVSUquU4EaMwF8bxEcdHF+E7GyNhngDgjvtp01oJwFy2kMoN NlcWSkrRVvSAA== 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, linux-fsdevel@vger.kernel.org, willy@infradead.org, Andrii Nakryiko Subject: [PATCH v5 bpf-next 09/10] bpf: wire up sleepable bpf_get_stack() and bpf_get_task_stack() helpers Date: Mon, 12 Aug 2024 17:29:31 -0700 Message-ID: <20240813002932.3373935-10-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240813002932.3373935-1-andrii@kernel.org> References: <20240813002932.3373935-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: D51D840016 X-Rspamd-Server: rspam01 X-Stat-Signature: qxat5wcht56iw6cj3pkjfsimjykuwcae X-HE-Tag: 1723509008-338937 X-HE-Meta: U2FsdGVkX1/gSva5JDF8K2Hz6Zb+P8JJuM5EEYWWjT4oBcDfIdYFTSuSju3f+TwHjnSjE1o8Jn+tMTLipnTUZe3VYHkaBhmrTwXZdVMq0r4BpPIDjoHJeiQ5IH8liMwsx7gfuJMVNZuhBXUTZ0Wavug7ocSvj+G2sENzLiuwAXnAnPLDnidNdsQy9yceTp9q4iNhFh0NLgh2DZqfyoUlgw9k2LgJo4MYsAj5O7ARKMb8HI4U48FLFOLz9AbeNnE3fnaldQo/L0VxqLCSiIhssipguZqfRJo8AQf83j2iA3jwcYyt5cwJ45Ghqy3e3KTM43bsMQbkgkIAx4xGiReofX5UqKyyzsfVvvlH5w/szuCtLS0BGe8clqrFGCPjMJzWQhtSaD6Ehh69zEerFaP2TPyV9hM6PMPHhPPPMsb2b+s8s5C6huY8w6IcwRdjGBHm5ja736TxwpygQUgUMiO/eoqtgp1izHDN5fb+SKjPrctZYrodtpCt06peEd81+Qfgtc5Zi81JmwROIlWaF32sDlJ8VwN4K+UQNthxvmeiEIik228S001spvOjP/KeT+r7yn1V8IYn0mC2dvAOTuNhOG3ORtwRUWmrEH5Fh77nOTcrr6r3wT1nIippPqL1HN0x0Iz+ma3jezvY0B7uDnwrBJMe89EaF2aEK17o4XMhCoKQgsPecQzUYeEGhjBjUms3vg9akwP8FV5lbWpklyYtpQm+FC/OHvdIXxrl8NByrbkmTev2QkQzUYJuANJKTZYeMQYqmeRzum3TYY1hQ8xobD2Wc7c/apRSk+MRXdxnmXndnYN9Wfmetgx6RK1xTjFcYtsLR/NApMVsTfcT1sP+S7QA20ZJab6Es6tnJM7gLvB2P6UeZF+f+j8x/hVZZtfN/Nq+HigH7+Iq1ovLfvaa74Y9XAEKNDufqG7MqTcU/Wc2wSMVYbSnA6kNMLP/DCptZ8tv6la1a0e+RpfoPZ9 hlFEIbE7 zGOaFKlsYocC9Q1cyi1xPidOpOMhtJGFL+TlIQU5A6K1SdDtmplnJiWXlCfcYykgw3CkXOmGGN7tyOZ9x+5+E4ol9E4Ze5tJU28bDD5fmYPHSSj/qOI2Iozx24Vy2Yfgv9HNESyz0XsLn9KrZfQoBb3LVWEadC70ZQj4PP2ijhFXFfj7bRISHNtgZuZfgWJvuVgHkRafeiXTImNj4GQMe1WT+ayspRrT8opGQ4kaKBCCXNCmuyVE+m1oa2mQFoHD+WCxYAhg2ItPuuRjCDsGWLwHaxieNiqQ7E+rc 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 d557bb11e0ff..87fc35778131 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1530,7 +1530,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: @@ -1586,7 +1587,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 Aug 13 00:29:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13761174 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 4ECA5C52D7D for ; Tue, 13 Aug 2024 00:30:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D71D26B0089; Mon, 12 Aug 2024 20:30:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D213B6B00AD; Mon, 12 Aug 2024 20:30:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B4CCD6B00AE; Mon, 12 Aug 2024 20:30:14 -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 8EBA26B0089 for ; Mon, 12 Aug 2024 20:30:14 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4F11340519 for ; Tue, 13 Aug 2024 00:30:14 +0000 (UTC) X-FDA: 82445340348.08.04062FD Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf06.hostedemail.com (Postfix) with ESMTP id 1933F180015 for ; Tue, 13 Aug 2024 00:30:11 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=uL7BXwzk; spf=pass (imf06.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=1723509001; 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=1aETl6fo73R2hGtR8v+/aiQ2Xs81hCN3zXqYhLiMx0o=; b=eQKv5AXg5ghd0XvB2OrOIsVhAYgJ0RpTolxQyGSHYfoEL0ImoUxGChDLo0HJPa9eOK8ab1 jmREBnLfBr8YJvwIJy4tDAjLhIugm8b824NGOkXB1nMz5gGS5mu8mNqHeyG2Y13eZdORf5 833feHiMXQ1KtfMf7gCrgCtWCXP9MsY= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=uL7BXwzk; spf=pass (imf06.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=1723509001; a=rsa-sha256; cv=none; b=OSKpNFcbsgXFmRAMbRZNxp/X3tww9Rdj3Gs0ZLykEH+u/BxPIZ31/OwyalOsWkx1qcPu27 5LlEDzRSUvbVSip7wSoOJgKxoIGhaTHooxY/5TmOypPcjLyN0JxkFkEStpvf/PqLqddu41 SMNuW3LG3nbeTaJFJisFpF3BmLMQyjs= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 309B5CE111A; Tue, 13 Aug 2024 00:30:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2F484C4AF0D; Tue, 13 Aug 2024 00:30:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723509008; bh=hAHRGfhHTT27DNY5NeOM9zG7twQB27NQc7Kw1Yx1Eb8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uL7BXwzkiamsMQrTnXESyBDOR/fOC64Fx9M2Sup0lkmRjCpxKzMsksFeVVDScgU9A BhjhxfuZ1UQqsZS64jr41CgSLrt1RbTejowxzXaUxxPKEJKKP+unOOtLxikaXjM0GN 556Gac7gQ0HKW9IwTPx5pUTxbCqugsRAGfV3e7KBOfMfvXvyyD25LDJYqdh/0XsTfR nePZwByt9PB6dzRy+2d4jHPUhaCRO6qTdqSMwwKQkQ23TNXTgr87CG/0EeGm5fBzwN ++2nyNL9a5A1XkMZzOJeNvWuZc3JSoWa7KnSmzKhI+88RxGpCIKTTpHwas412W+y6H H3oEEumIMSo2w== 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, linux-fsdevel@vger.kernel.org, willy@infradead.org, Andrii Nakryiko Subject: [PATCH v5 bpf-next 10/10] selftests/bpf: add build ID tests Date: Mon, 12 Aug 2024 17:29:32 -0700 Message-ID: <20240813002932.3373935-11-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240813002932.3373935-1-andrii@kernel.org> References: <20240813002932.3373935-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: kecgd93yjubsg7h4mdhoatfqnrd4qj6s X-Rspamd-Queue-Id: 1933F180015 X-Rspamd-Server: rspam11 X-HE-Tag: 1723509011-370057 X-HE-Meta: U2FsdGVkX1/tDVG/tmzqxeAb/OwcNywClu8gr929BLrf5J8lfu0SpsYKiSU8GQR+Gf9tEekYDiKW9pt1uXureB5rWL6Rm8LjQcVeXL8whteZgcu//PkV5Y9fPk4KTxHRrrLxUo3A8Q0aG8btdtqEnEqsISJErMt/HXqfPzcjYZSnJBVvcfxJM9c1P4EpelDtXtksmtGMRMsJlFQT9rL4v98MXONssihZ/PRv3u/EGA60HTxLK4gWSplUGSklQhJCnVqeWifuFnLxuwVxY7q6Gd/SkSmoa7iSZAni3YDWOI/QA36Aorvgyry01wIX6ubl4yCTYumN79fZdwqXa/UYLydsG2qYFHPXuBOSn6x2QB4BxfppnS/LWNVloTE38sEftqLG1Y/J4VbVVBCjtFi3q5hGPyGXeNjRCb9dfFIZeyW0vIZCqAg7mGVVF+LotamtOZpcAQI96k9PzZ2jbIhtLQVIy0LtnLCk/fXRiZKr3B5Vpwtfs6Kbz2kDFl4i0xJWdlka8c6qY+ZSHBQsq54KVCZy8VkR9hHz+lwYy3cA/Dhn5lD4ZWzx9vlwXo120ZrHyMUoUCIK4bUnp80k4vXWDYVWp37HOOARg7K2W71ADAhaO/nqQ0wRBcC2lgWteJk6RdSDeqO5a1QXKM4ryUOzBhpQnGk0Uxtqt6mx6GiEfX+71A7ZpgsieALOcJhUiLfbkz2TGBfEMcwhn7u2/mKitGviyQdXt1VPgiwIYZ/jbsWbR4xJHYNyI/fE0Es1BTFHwCUqXsWOwb0V5+RVamQZ6UFhQS/B5/10QOlyYq6bX5lL6+Uu25tOLOUTDV+Y4MuKdirGNcvT1fgEqO0E8klF8U+V10Qtg9ezpBbxK0NrYCMVhUUlgailJbmBX4igeHOkQxkYD3L/HcUkNFsdzF5s66HSZQpWWPsLfqALrhXT+dvQEJTdgqR2bJLsp39OJs9tARG2q2FdbB5TatM13tQ Kse+8Sch xUkQ/g6v/ZdUXDstaV9xSiXJfLNJm1blBaePdckUstqRIHy8zpkOOvPHvOJuCbKOS0/VaKy27t+dkaMYhrENd2fk47fL2MHBixnPR0rXYo5/qOZmLUaV2ZEMKHdcL3BV/X1fHvZ0B6ReXKEbWpfeHCzhnOQm2tOCKXYsgUaOB2E989gUfSKM/2eAWAqG4vZupzPHup8E3dzIioATrDxpRb44eZqtkRZPTC/FSWoWAEf2UZlU= 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 Reviewed-by: Andi Kleen --- 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 7e4b107b37b4..e47d983d2694 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -787,9 +787,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); +