From patchwork Wed Aug 14 18:54:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13763836 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 8727BC3DA4A for ; Wed, 14 Aug 2024 18:54:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1F8376B0085; Wed, 14 Aug 2024 14:54:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A7396B008A; Wed, 14 Aug 2024 14:54:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 047686B0089; Wed, 14 Aug 2024 14:54:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id DBDCD6B0083 for ; Wed, 14 Aug 2024 14:54:30 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 92D6A1C4C2F for ; Wed, 14 Aug 2024 18:54:30 +0000 (UTC) X-FDA: 82451751900.21.7D6BDEB Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf19.hostedemail.com (Postfix) with ESMTP id 5D47D1A0009 for ; Wed, 14 Aug 2024 18:54:28 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=IPoYqr7W; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf19.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=1723661610; a=rsa-sha256; cv=none; b=sH8Ay2rXHOAZqwtcAS+/M3JQ2L4FqNpWp3c8tplk45rfb28CGEEF2UTbxJy6pWoSpFsl36 mzAiuBzrgWO+ccMzegKFOJUmMhpYxT0eittrrvdv/vRP7Ez4vmAduz3QeuTkJA/3WePd/G Vw5M4XDeEZkNuOxupKzgrwG+GUiO6vI= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=IPoYqr7W; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf19.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=1723661610; 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=rOG6g2O++Ni3d/ySpbAI0i7QzKHk7XpWo4PXmiRYV6A=; b=iqfY4I8M1abdHA52sKTvnvG34SEbOAN6G42L2qDmnTXJNRoTKFy/vSV2e/T7YqzLMRSOeM ckbFeSyNl6pbn3qNbrEsiNX8MyQtmuPMeSPx4YuuJeR5Ct8MdcSNoqwvpqMx6mPOy6ROlz sRLzuyMk4Zo9vjsqpXBXPsDkpb6TIAA= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 85C06CE1ADC; Wed, 14 Aug 2024 18:54:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44B68C116B1; Wed, 14 Aug 2024 18:54:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723661664; bh=4ZoU25T3GpMNirAVJHlQgWCkIN0YdLxI0t6Ca/r0AuI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IPoYqr7WwOTP5txj5SYFCRBE8SuJYpuJMRe3ZuOQtmo0X3PfpnkMUI3CxgHwsL5+m wTxGvqYxd2m0QkMedDWnbpzVeVjYU7xLr8dd8l4vMXeAgQz364ZL/XLilib0eoMS3C i8FnGEYeM5DoOyWf+TMsbpvNt0WmZ1PDvmh6GJ20/K5627vCNnejizFAYA/4/Vucor LeGBVYjOBHtdzqFm/z4u5jSAYIr+22TK5xYQAfo5NMPKVK1uoQCsj6QS+8XtZizWUX xkxHxh1nxoJTNh65ZmHaOEJSKin2BTJGjrTxCHF4Qi2HdrOv0GeIeKji2b+73Mrare WXOQ0Fk7xojEQ== 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 v6 bpf-next 01/10] lib/buildid: harden build ID parsing logic Date: Wed, 14 Aug 2024 11:54:08 -0700 Message-ID: <20240814185417.1171430-2-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240814185417.1171430-1-andrii@kernel.org> References: <20240814185417.1171430-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 5D47D1A0009 X-Stat-Signature: mpctt8y1tpujmqbnb9wt1bd3jnkr18hn X-Rspam-User: X-HE-Tag: 1723661668-963568 X-HE-Meta: U2FsdGVkX18RhpKN4jKD/ARAZdpYphWB899rOc23N2DL+ojKeYLFa/fC9mu33kCZGeabWRu9w+4AcVS5KkuVocZ6AuuTeY40DypAYwYgWO7/WHQehi+70Q7XbFT37kWKPbvf0pQsNBFbT+SBkP5H4DT3h8QliKtG1CLhlMG1VExfc+XDS1/pTBm26CadLbMs4b/KUp5JmdyQ805I1M8TSSGKNkwBAn0UuOs6pJO/8MrkRrlh588gg2l4X98ACvYF6hQs87ahSLFIexkspfS4S1ahQ6ZLeOMsvDxEHsyZV9UuHjo+SRzLNYfvGsNqOf1r3nD96qM13yjA6mwzjax/Vnc/6Be9vhGT7uIn9a7eKFVpNQT1HPhiA2Z8riQ6X0+KRzVjWuJSpbLtEGPIP+OfRIoC34+s6pNIZCWXiIZh21T+2qtgBKw2lQ9tQ+PjUMfcC+ekjrL9ua/BpYiHgoiGfeWvo/jjvg2+tdmQW8AJKg64pDWyy/LKDgDSU9iKYq8Y7HDDb25UZwoAah/0vXjyS8O4OtGdrndKYBTQv0vpytgHl0y46Gi/IgAI01UxGOCzI6XNbQuE29KNEa+osGrSVpOY/45T13DeYGMxtyLN0PCi9mWEG5RBd5lHpFdL+WXshxGvoLNAr63+sG0HACZiBV1PQcBa1lOzbuTuyvVruqML09kGh6GuCvOxXoaw4te2OYtfDRJgKnbUSK1qajdatLQvPT2MlQYbjDxmuOcmTnAy6ZoidHWCkzvIUkr5RuwyIs2ZtYrx0qeLEiHZarVZyf9JR8EFzYgVx4k4iTuEGaHLeLCRojSj6BxA59Ltzk52f0MGiZuV0VqxmkFDPSwYzKO2u03j6QRpiidry/jsedG3n3/ifQEHMRRoH9oialvQUsxWqXiu2zS+ODa/5MVAbnKna9FLbb2asAYohAsSYqx2tZ/1jdZuflv9NHcHAxWETpcnMV1e4r3AMuRaF8u OjzWxUlY EGCxY0h7tMMYrRVCAOKJ/73bZM/HmHF6wV55+j/e2tft82QMfTLi+slelBYZ4385hfWb4EfSYkghgM6qr1HAp1zzVH2qrVF5ZpG0p3yEj9aKt1AhnVMfd7+En4D2gFhRS2gsdm6wWr3lenqulgJKZc+jVeXp+veDR7GMOY/FOb06FoahY/qF1RvasXbPe/Ri4Z8NCgbExyarNZ/Uet7XE0gU9+Eykm/iraRECQb4SeKmjSK7/rvm4XNIbrYqtumkDbKjQi4F+8I676HcNBSBnNxOW6g== 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 Reviewed-by: Jann Horn Suggested-by: Andi Kleen Fixes: bd7525dacd7e ("bpf: Move stack_map_get_build_id into lib") Signed-off-by: Andrii Nakryiko --- lib/buildid.c | 76 +++++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index e02b5507418b..26007cc99a38 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 && + memcmp(nhdr + 1, note_name, note_name_sz) == 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,18 +86,19 @@ 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)); - 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; @@ -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 Wed Aug 14 18:54:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13763837 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 4DDFCC531DC for ; Wed, 14 Aug 2024 18:54:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF7906B0089; Wed, 14 Aug 2024 14:54:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AA8C36B008A; Wed, 14 Aug 2024 14:54:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 946FC6B008C; Wed, 14 Aug 2024 14:54:31 -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 728916B0089 for ; Wed, 14 Aug 2024 14:54:31 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 1AA811411B1 for ; Wed, 14 Aug 2024 18:54:31 +0000 (UTC) X-FDA: 82451751942.30.7A24DA2 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf20.hostedemail.com (Postfix) with ESMTP id 52E401C001D for ; Wed, 14 Aug 2024 18:54:29 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=mCVML8fX; spf=pass (imf20.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=1723661612; 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=s4BzimMzeVhKrVQBIPimYrtwc125XiiQ9wul3zpds9Q=; b=jBGJjl03O9Zsok4qYXrczHsABV+rpxey29yzHzthKncOCfRdG4sd2ImfIbDJqMFzYW/y62 quSmzvvnwZ5byE7E+skg61WTHWEDkpQ6E5JusnYg3RK0k5WXodqtiH6+MCaJWt263Ng9dq GaqH41UIOXM0ht+hNFJf/U96b3jvecc= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=mCVML8fX; spf=pass (imf20.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=1723661612; a=rsa-sha256; cv=none; b=YIm/pE7HG7ROLjqnm2tvY71Qazz5KL4p9uRH6wWliM60AgoRE/M8ifSlP3MfrF1Ak4FR6q Q+lYzM/P/2uKlskJ3Sb6h0od9pN5ToloFmIoCDWthG/lJ02ws7eJeLIeLfufOWqJziSN0u aljorELxRw+kgBa3Yw1opNzV6Au3Gvc= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 1C7CD61BD2; Wed, 14 Aug 2024 18:54:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ABCEFC4AF10; Wed, 14 Aug 2024 18:54:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723661667; bh=jtjjeQu5Ig3K2l+dsIhJhrS2ClV/GF237qmgJVS6KS8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mCVML8fXC0+oFCo62zoil8Blk1/UJslr+Zlw6OIQVIF2Esb1Bk5eTPthu+nVRYlxD 7BD7iKhUr0FJZmekiuYlyL2V6cGaadXrsce0zkeM9Ordl7TvVrezYUDe0+QM1HIslC frJKktxChc54jjMdRLsH+xPZwJvLjDwdRu+zubEHG0n/wWS7+Hn3ViPZTeGCDNE/ao nKq2m5SXJvsJ8FSrkrcz3yaWncbsSzqRJI6/NOwN3wDMxnJMsYVYEz5qruJfSAtgwL iXP+zqCrG5xWU+QaoYqqJRQca/duuuNdFf+iT0l6mSOorv/VnV/gU9fBmIKoZQjUWO NttwfrNO2iw/w== 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 v6 bpf-next 02/10] lib/buildid: add single folio-based file reader abstraction Date: Wed, 14 Aug 2024 11:54:09 -0700 Message-ID: <20240814185417.1171430-3-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240814185417.1171430-1-andrii@kernel.org> References: <20240814185417.1171430-1-andrii@kernel.org> MIME-Version: 1.0 X-Stat-Signature: 7a4if77rz9bctdwekd9ib4i4aonaxhfm X-Rspam-User: X-Rspamd-Queue-Id: 52E401C001D X-Rspamd-Server: rspam02 X-HE-Tag: 1723661669-38392 X-HE-Meta: U2FsdGVkX18p+ZPhRBXkA0Po4i8L5Nb2a8ihnRBj9eL752hkAa9EyBtQZayzcWoj8SDgRQUX5ycrBEnfDvb9t4gTi5KmjjM2y7UpCuDyaiYq5SZG6irCtHWQ79RGxuTQI6Wb9RwpnouONVPWli/OOD0ATwqZ8EgSgEGCEiFIETMYX9l37iVfi4Ht8WCJ1V7oQFjIF4p7k87J8iuM50jBzcChCJhOngUdBJQuRl66HBxkcPvgFvvziZY+3wu/Ijjs5kGYXrvjpOtq+8T3XofpFZYdScwP0d0mX/wzpOcYAOewfk0eG3RELS5sNuCma2A55AJpwDa1rBFB6b2jlcWwbJXBIP2E+xfFyLRtMKzHJ2YC5vqc8cTFgMCKe/bwxP44JShvGJOTPvm2EX6KnBeuSBvSm38Q2eg38CbID/QrxJLwcYdGLaNyBBqvIfROSzJ+7ibGOVKbRPAn9sV8RtmhC5rB8dwD0u7dnO5rWyeDwNBjCFYNikpGSgKnRdzVUZltk9grYESPop/HspsVgBO3YxpBnQzM85uDf4kTQyaW0jxEyPrTohST+q7JUbRJdJk/xBUEmqwf4GAbv5TS6FTzc5BrYf4lPkAZG/bUC3ChMxhgOSYsMfj55i/yWYfkfYFN2aok1cPrXDWa8hWXroKE/Ligv7JEdBWZloq5sdtoGOaTCUWL49FSzQQGqp4wTmu6mt3sGBRWqAyJee0NY7rivhT3fchtK7hNlkLzSAtOBL14i0PpPv1JteDbdvKk9PoTuXeGVWV+YHD66ZNGl4MuIPL35JXolyW0rpcSsb3UyYohK6lQv4Rgb57KjA8UVaZ++l+SBf/0FfkBT6X3ZdJ8t/p5AJU2K7dGs9t55EMgx3M3n8201/crLd6Akgcc/1mkXUaNpqzXKVJxINZ3/K27Thwydthbo06OhYA7+1fzjtfU/OQW2qq78XtNsg+QMSilBxpP/gVewtVrEBCsOI9 ge678MbM iO0USHME7hNLlV8tGOKJcASkada9np2LdbgBY9a782KWDKlJjQnZq09334Y+XjAPzmDDX8SDKDooaHkxiw2M9eEUugQOSZ3avCodsy+8lmPaLphu5sXROwwYKZA4W2BUgbR73g+KbE2mlb8qSysVcAEbnoEm11iD9WsUEjyfv/OqEYWlLQS8uM3d90BfEzeFIUSEQ4ToNL6c03RUhkPeWrBykq1gLxL/8iVAV0WrvlEgmSQY= 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 26007cc99a38..bfe00b66b1e8 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 && memcmp(nhdr + 1, note_name, note_name_sz) == 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) { - 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(Elf32_Phdr), sizeof(Elf32_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; } /* 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 Wed Aug 14 18:54:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13763839 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 BB630C3DA4A for ; Wed, 14 Aug 2024 18:54:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 357936B0093; Wed, 14 Aug 2024 14:54:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2E0366B0095; Wed, 14 Aug 2024 14:54:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A6746B0096; Wed, 14 Aug 2024 14:54:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E80C06B0093 for ; Wed, 14 Aug 2024 14:54:37 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9FC6D160EDE for ; Wed, 14 Aug 2024 18:54:37 +0000 (UTC) X-FDA: 82451752194.25.6561DC3 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf11.hostedemail.com (Postfix) with ESMTP id 5AFBE40024 for ; Wed, 14 Aug 2024 18:54:34 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=H5kdLwT9; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf11.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=1723661595; 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=vAy4clIrBnEmCFnt8Y99q4dGcS6cLzPtJwvKdL5ln0A=; b=6qDbSdePGSSezW2WpdvqjxcnMBRCEqSmiqTIZABny6D9d/kJc3/WK6L94uvg2yDtjlwq4n npYUVpaS8K32HQ7KKj068c44AmKgW8yFKXWB51ewvOqs19EMwcdNPxu9oiUhLJpGE9Da+A ocZHCb7ysQgFWWnbUxwviM0iDwLJJHs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723661595; a=rsa-sha256; cv=none; b=da/xkzwH8OlQ0NsoV4FWSZxgILa0+tb4QPXbw3/H6s2tSVgoa4SQ9ZE9KxC3ESNWj9EXtO CXfFhJeF4f0y87CIvWRKd8pd8MSl1gKas6pDj2uQ3Ihtj8zHW0UWcdPDz7n6JkFoeij7cX diA2hcNXY1kU3j7igKl8ytbxAGJcFmk= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=H5kdLwT9; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf11.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 371ACCE1ADF; Wed, 14 Aug 2024 18:54:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2C3DC116B1; Wed, 14 Aug 2024 18:54:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723661671; bh=Cd2n2547OKq5mRkrfmg4+0GI2njBDE3a4HVKwVRW1Co=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=H5kdLwT9gA2Er33HcqKTTepe8GL7ks1Y5es208W568OdqKOL3ox++bE7UFOAACAzB CUxWDxPJGWrx7wRYoyINg4itYmDmx51ZoHzdlx+PGmA3lqi+U6lPqfebVOT54DiGzt EI3c4rSCmKZYLoDSpOMKaWcwboEx9m2P9QNpdL05dWJMMpTa93iyPil0tt4UUlG9Ts nLhcROkWVErE9feL4dWVWdO4FkwuKM0XUwAsOQU4uwyexOa43etyi/OoBq4BVFdYx1 H4a/DFcTB26HWetQa4EcLBlKnrbm5N/Cl6WkVqsI0bOnCHG/6SHfHQwfGB/n3UZGof ckkWVgr0yC2wg== 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 v6 bpf-next 03/10] lib/buildid: take into account e_phoff when fetching program headers Date: Wed, 14 Aug 2024 11:54:10 -0700 Message-ID: <20240814185417.1171430-4-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240814185417.1171430-1-andrii@kernel.org> References: <20240814185417.1171430-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5AFBE40024 X-Stat-Signature: dsrsax79jxjjtpq76mkancehgywceb44 X-Rspam-User: X-HE-Tag: 1723661674-267451 X-HE-Meta: U2FsdGVkX1/EDcpwQnAsJaRgI89sOqcylVarmT99cf3Dpmcgk86JcgpF9uoEjiuzWPfba576+FlUVRIAd0RT2ZF/HGXB1lH1wINcjTNMh0pkElX1ZoKmjKBEf4uZwbAw9wAo+0bKEKR3rM7D3FykSaAN1LdJpkf9c5pIKWVKTY6C4lshzSJMDAMVYzVdxngExxeklAIPj2kah6Hgs+3xK9NlJGEtDP62QSZQDKmuiAQPjJjnnWVTOdgDVGrv1wxP/y1IfpD4OvgnFIlpCceg3EGJD29/8vY55dX7BrICkt0fgeO2xNOMuqhJ/xC5OHES3huswqFDwrRBXOUtIWnzHldKnfhbIm3VqYBzQwbIo7eU7mxADDa/BS8HHxUCck4wQqFtczncFE7Kf6pm9mp5Ko1XbHuLhAaHWlTsDGIvgPbKYOC+huCoRHzA94QwDE2uNRK8b+bX4i9flkOuA3ragPkdDWaxw1F8KNKP6+7cIl235ll5EIlh0ZNDjUqK/tXRGSeJnJjVft+mnbXGsWi37mlr30gQ0jesRl7+6fhHuB0oGZtsIFls8iAsbz+YSUERI7IiWC4KtBB29YzzvOjLKXw7Wzz7+glgYarv0vom1jdh2MtggqSpn3uoauUBUrnc7O6gfPyUnHYdrusY8g+Uy8NpxUxVycCz/eQjZTKOaEQeeeRrx1a9HNdcpO5yRLrvsnkPS+CR6NPNlYhMoXxyrE719/8WxcU1ylBJeE93IhpqBep1zBp6f7Q9MSypxt/s0u+YFjevb8mPnhUAo+XHffcBozJwgIjyuGrEFN0SR3+LIJlPs45wWUsdN8j6NhXPH1HQS3afQmRfu0gngTgPnflk9ilKE0QmukQa7TSIY7xJZgWSJwN+q9490bRYJqZtdwBW6Wh07JPltje7+66Y8wy0k6X0WY6XLze8o+1X6YMGYJmiUw2yH9pr0VxNgOjNMHq1UbmUdUUj7EQiUhY zI70gjTH F87D1T6LAZP1UN1onTuE4B20OO2xAgiauKkbZI2U6KUURXfqUpJE6aDF5cjaf3pf5+hjhfIBPYajXnZh8FRFYj5pGN7LTKKB3/jCwJmYPxDKlid4dh94T81oaHRVKukepvC0vlbZGfXEAoOAZVPi1hOXEJCn4hZm8AIb0vREVJNuYqwu8O+LAPMOx5dZG1LukswHZ1lxZ5HyuRzawSTGW2kwvz4kQedaYAh4B6GzfzNRE9n4eKbAIaIJ7D4liit58aZlOKyC3hlg1S+cqGB7J/JmdbQC+4y5jkNDLWUMFYkYXttkNHF9sKA/Hyg== 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 bfe00b66b1e8..7fb08a1d98bd 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 Wed Aug 14 18:54:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13763838 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 25BBFC531DC for ; Wed, 14 Aug 2024 18:54:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8ADFE6B0092; Wed, 14 Aug 2024 14:54:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 860856B0093; Wed, 14 Aug 2024 14:54:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FEE06B0095; Wed, 14 Aug 2024 14:54:37 -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 4E2D66B0092 for ; Wed, 14 Aug 2024 14:54:37 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id ED65F160EDE for ; Wed, 14 Aug 2024 18:54:36 +0000 (UTC) X-FDA: 82451752152.14.4CF3835 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf16.hostedemail.com (Postfix) with ESMTP id 46CD3180018 for ; Wed, 14 Aug 2024 18:54:35 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="pEZuyU/1"; spf=pass (imf16.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=1723661639; 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=GOI369kom8/1lwI/dpS7EaG3Jx9c+Y8m/KJsikyz+mo=; b=YOer8TlHEmUGuFgIqtqlZHN3LOd/MTvGISLw3tmwzlYavlx3K4RPSDQWHrw63spFeuYiy2 nDfUBFOMXsJSrXf9jHn0fDxdhtEf3ai/X+qDcDabNuBcabMm+oDB/AS9tp4dqnIlVAvjwC 9ncPI/zVyyzY3x4vDAJvupTYgqkOzyc= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="pEZuyU/1"; spf=pass (imf16.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=1723661639; a=rsa-sha256; cv=none; b=JV3SGoHXrUPj1KFWEdfmxPrfeCd912R1QocAtp6cwwcjBgWADau0hlMvLgtGIY6sFrL3Mu U9eJmQarDzUiMUHfwH1TQYnetDMAkchMeEyfiAzS02CTn0/JS4Mcll5N+M6c8ZweoU+kZE LosEUNE0skbmmkjbZZxmcxx6PRCLsGM= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 6B27761BDC; Wed, 14 Aug 2024 18:54:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1BDEDC4AF0E; Wed, 14 Aug 2024 18:54:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723661674; bh=EKylHdmMyJh/v4m6K+s9kOSONMY3dxjgOdsYFysplh0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pEZuyU/1rAcAXZiVqWCU1ZGahkQkGHNe1bUgVRj9L2g1e1y9NIUi4KQwh0he+/y3i 2nwUHlOoNOlOLD0tuOYrkwv5GqJht2gscy2mqQHppF69YaOiMmeea/39g1oiAVY5St 3/ANUyoG6PXVM3PloR0GBOmHx3vAZmHzLz3eLz1TfXmEyQQNV7mNLlS6wuD9merH7u L+yBFHUGf6w/ErARe+gyGaK8sfCAdz3v64xUWN4VAExkHhPsb3YYIemUM17EQbvmUT E2tn0fnxASqDsb8kMS4JrSBmmWrHZ5fRDfpMq00n1t9bzTtHJoe7dXyVQqPW15O4N4 KPavrNLcdnQYg== 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 v6 bpf-next 04/10] lib/buildid: remove single-page limit for PHDR search Date: Wed, 14 Aug 2024 11:54:11 -0700 Message-ID: <20240814185417.1171430-5-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240814185417.1171430-1-andrii@kernel.org> References: <20240814185417.1171430-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 46CD3180018 X-Stat-Signature: 6mkgu5uwp1sdi7wrnxxgc3yw5ut9ws3x X-HE-Tag: 1723661675-94976 X-HE-Meta: U2FsdGVkX18x8Pe7bN+2jDRxME0oEXho29GFwGhcAp58ZN1fJuYuY02+j+Z9xmC3Ad20lBqbvF+TyLhAxIWMRZ9eEbe/rdoGW9DyAUmeviAxKixzxKzzNyPdZSRsQ34ntAKsT8HeAEmojWN0JhAKT40JwWQTHTLaCTwZxUmZoG6SmYIJjQ1ogFaO5CqdtElyxD9gsU6EK5z8TTUuV/eTFdFADJMtW3oPhnaDHgYFNlmCTnxNyM9F39JwuDmhIavqZp+sGw5CuIr2AvFtEEEQe0VWGGh4hy9xzSe93GgxOn7tyTM2cisgN+U6N3eVDWXzKpl24WVDrxisnPNEJB3fH2TDNJRUuTBhXb6E6L90FW2TZJSgrKOekliGta6RVRwYDDrz73VlLSSHCftmOOnBscFX4XeVD74ypBODduw6QLCTP2aQM5SFLtMuRZ/l5B2YP829AbTG77lDsuuzbj2iXx/3n1jMaCvqzanDBLn0dyxefn5uIhevk1SGvCspjFYtVapSEn1V1iWUYTG8uuYQYPVeD9g1w6JX4laZiHgKtLsItBxkpGIYUyGI9VDP8KC/bnmxkNuWIJq2snVuENJ5oTPWXcLt3i9Y1zFw7iZIfQlkfdRJmGN8UvQAWQpdOQm7rhCDFybHJtCPt3v9cFlMkEoGs5tXZvRMMxGhU9mafulFm0VS7SDaiLUW1DSXj9j+UKKcM91RWu3Lc4SSX6m3n+pW/R1VdlLk3o67hE7Q6TRBrcRf2yFSs9xCDjc+vEqMResLS7BZBcQMXBlJlsnWPiiC6iQUQo+fZSBmvYSt6yjz4N3jNUj/kNnqUA0RJQi7Abq6fSSTxLDaHsAIFZ7colPaYEts/4sVMV+JB76ZH6+KTgxrCPktmDrS11HaSSlneo4MxiZMoRlavrS5R8wfEkCeEDnaet1H2vFKKNPk8Nhp/SuniOg2FcYfc47AM1SUFV+eSXqU1rcc9KisYUz q6wAAezK +ZIJPngrUy6BlXFcZGPT/kqyNKJXnGBLvN7G/Vq1LoZxirFX0lyitLioeYAom3PcrOznPxmqPbe0md0l7XpA3hdaMEk1neXq7MvTDPvi8iLDbU6I/vpejJTYpq/2IMvraN0Hfcfg3NYfjPg8ZyjkcVl05PJPjRvNTzWT/qaX17tYgLEGYzU5Cw2vC8SyvEWKJ7MRLKSZM/Eop+HtZL+L0Ppd2Gq3olLGblfAkcMxZ5uHP7O0= 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 7fb08a1d98bd..e8fc4aeb01f2 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 Wed Aug 14 18:54:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13763840 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 CB874C3DA4A for ; Wed, 14 Aug 2024 18:54:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6158D6B0098; Wed, 14 Aug 2024 14:54:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C4BF6B009A; Wed, 14 Aug 2024 14:54:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4666A6B009B; Wed, 14 Aug 2024 14:54:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 25D876B0098 for ; Wed, 14 Aug 2024 14:54:44 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C67BF1C4892 for ; Wed, 14 Aug 2024 18:54:43 +0000 (UTC) X-FDA: 82451752446.23.0A9734C Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf19.hostedemail.com (Postfix) with ESMTP id 839A71A0011 for ; Wed, 14 Aug 2024 18:54:41 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=KLgO4BwC; spf=pass (imf19.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=1723661646; 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=kHXDKQkmgfJJ+JdxkRigxltxVtVBVlUUM01aqP8QYM8=; b=YcdLWAmPlsD85VM363d/YEaFcthrK6pAWhu1oTNGSYenPf3nWSA0ITe1SDKW7pqJFXMeLy 8OKWRujBLjbsRT1mPyST46bv01s8KD1p+720LJzDZ7bf+zdMEijDxXGYqT0+BVU2rL+UbE 8YTu+NqxK+X9bvrEiIGeIm5Syn4mu78= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=KLgO4BwC; spf=pass (imf19.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=1723661646; a=rsa-sha256; cv=none; b=uh8ZxbfKrNIdfnIoN23k+Pf4lSy9mOEJtZpyBFHvlzJHF+FShVFLAR5CqodTPIlRcORH+w 38YdaDENF0ZUf6JsUEvDdabiOjusNK7LuXK33nrW3JTR5ICOds355Vstd2qusMCyTiQZ79 84/UH0agyWeDTMKPNzMxdCvPaa+wOII= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 9124ACE1ADC; Wed, 14 Aug 2024 18:54:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 65C9DC4AF0D; Wed, 14 Aug 2024 18:54:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723661677; bh=meM8TTb69/Y4fY3OabSKLiXw5VaLsiU5NV41UXNHZhM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KLgO4BwCbpY9tJo23F10bWnNCJdFyks5asNxMjP6XgzxUpK313FOpp8FQsY4D+d7c spKvluMklIOOhLqmatYCHA9kR2hE4MhJ5JPaCEaYqFvFhJEAUmMkJnSsU70nR1j857 JAICAzLHKH4CmYLOs3KydHdzjXEgToxVOpAT9Lek8R0X3OvB4ggKqr/+Fl98oh21wJ WJZExoaaEbLDrKbQk52GS/OLrnFJWScCQzrMp+LVTXoLFRji3FbI3fnXHT9t3bpNxv ZiodpCvszXvgzjzDI/eay4+Gl8EyTPmEY262Fn/faA7DsdOTSdYkURj48ro+scNw5m PR3Skup654FWg== 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 v6 bpf-next 05/10] lib/buildid: rename build_id_parse() into build_id_parse_nofault() Date: Wed, 14 Aug 2024 11:54:12 -0700 Message-ID: <20240814185417.1171430-6-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240814185417.1171430-1-andrii@kernel.org> References: <20240814185417.1171430-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 839A71A0011 X-Stat-Signature: rhygcgry8dwcyaye56zwosg6ywzaakd5 X-HE-Tag: 1723661681-879116 X-HE-Meta: U2FsdGVkX19ZjuzzeZ1CEXQZx2qVOMeKTig3PiQKNjVCxtozvfm2HnARB3G5mAE0FWRGFmWsh8UT/vjGcRiCtgceVyFyocUTBlXc5wWPtA3juiIU01wT8tp0N0seMQd6A87X4Q66F3QKVPqXtKRg5A5A7XBnEaFg2xzpYXtfsy2CGfE64cHTrFoAc/nRyxNGoAgz+Wice6uYn4iLxTuUFt6/xXFZYv4UFhR7l5hqx9qWsUN+RcgQZKBe1Sp6CAV33GpeegoO9uDbEgrEtWm+JCEjbHbf88oz5jPsrg+45NVBQzuidyAJJCrRtf9DuyuixtN9x4pqF94A+RrEHMahKO8TYNDLx944drcl4K+MdBdL8HSMJTpYhhCGxdpdOhr40zafr4djiiNIibeVBCImJ/q+2d/ZN5DvYOoIAjaYj5imEOqR8/0OML6CXUeh9jYiSwEQMnCne5WUXdHaDAsFMiune3jb/g9ZTfcNbXzRvYoWoUhhcCA/hH8Faue7koE1jLWHMU9I2EpHB+AT0MxlRDAkXtXi+MXqJag8mJu6F5zWmf38ypcjacHm82IczJONT49Kwqk2RAkKt9j/YKtXFtqej1le2zVKASPRud6CQ+33d5FJjAhZA4ibxjxbj/VKLTNgeaV4phGun4KNdyRfb6ZaFQvg/K2ybvJhulWrwBm5XlZ5G71r8G0ZOQMDat+a2PZmySLu/QcBBIyGmFZI5r5FDlQcozG5pcGlApIKTKR8vhQtH2GPs2JaO29QmEBQBiU2+ze7l2tUF8eq2gn0eVBXav5rdFABoGZn4xXm0iLO5CFSlUd44u81t5UjcGaQrCHFfSZgNTYGAQ98lFsQXEHe55QxVfgmA/jKdQl6QVi6+fbQjFN/zBtKDTvqyC5FCgjl69y0aO6UvI3TujQeLzZ6xxl6QXh7ATdz6VFSyCTED6fiurPzWiyZdeeH6wBkmKhE7wr+kjvl/iXM01m Qbw3PyS5 j0LLJeG78dJv/jDgSmbmEGjfnHsvDkAxqmIPk3dzbpye46o/TbLZMeg3J/TKMSTsQZh5FJls0xjiD5q7dmXBLDAn/kviHiMabM/x2xxLCt4gClyxY7JWWmbMyo2ktkMe7vJt00sfm2tA/UdfmUJ+pqgnyi3uedGLcS9X3qo84EBaPVtv5+ZKDXmV/iuiAPkxyWf//ACwTFLT0HwkswCwd1hY2WbVjLYluRY12+SD4N/tNsVxXP048gjTXiTIzsnR4lvJcUlGKv83GfJXh1QfHgG/gM/MSKJIGwymv 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 e8fc4aeb01f2..c1cbd34f3685 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 Wed Aug 14 18:54:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13763841 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 64F28C531DC for ; Wed, 14 Aug 2024 18:54:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EEDED6B009A; Wed, 14 Aug 2024 14:54:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E9FB26B009B; Wed, 14 Aug 2024 14:54:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA43C6B009C; Wed, 14 Aug 2024 14:54:46 -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 9E3646B009A for ; Wed, 14 Aug 2024 14:54:46 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4E9FF1C45C0 for ; Wed, 14 Aug 2024 18:54:46 +0000 (UTC) X-FDA: 82451752572.20.2A20BCE Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf20.hostedemail.com (Postfix) with ESMTP id 08C921C000B for ; Wed, 14 Aug 2024 18:54:43 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QFfhQ2kE; spf=pass (imf20.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723661672; 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=MUs/x7lpqmbg6sstR6+AtoRBucJ7haabJ3JekNIs3Pg=; b=rhUIQ4OtiATabN2KlG+hGMpEwu6Hd8zjikhOIwy0pO9P0987iyC+YnOAqieDXnEaSWIhyZ iteb/dha7Mj4F1TgLmhfLF1o123X1iNS42tD8/SbPEFDFkma0a0QqQ1zBaUpCSUjkcZ5mv XwR+higqOrpP8ak2Ozu1zi+R+ykxswQ= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QFfhQ2kE; spf=pass (imf20.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723661672; a=rsa-sha256; cv=none; b=TryIXZjxwY3O32YyfykaCU0e1LBIR/D4S1dLcBB4lO5lkcSXQdlV6NS2u858zLPXhHWhd4 YUio2e5WkxW4pYScFMxVr8w884nGOZPpEdVMskmrs6MA3WFeS1EMpQa2lss3MfnowWe3Sy foASqtSAewywNJ+/nDG11SZ3/SK6WJo= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 352CCCE1ADF; Wed, 14 Aug 2024 18:54:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C4D6C4AF09; Wed, 14 Aug 2024 18:54:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723661680; bh=xupJ424Tw41nEe7GfCN7i5gyQGldYzv2WcV+UpjPy8M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QFfhQ2kEKCp0C9ynVtGldcKgIAZORCu9yY50B70ZwdJFLN1XH6Lczqb+WFkflGP7z 5SNUAsF70zUrZXum57h2b5NxeExP70e16C/aR+inDFIRh6NRKUIz9SjbZBN23xc23C QHRkYWf+Ab0BXna1CEnI/HVcO1lcWoQ7eyy0CQzYNZ/HvezGtw7WzeiUuaQ5m0h9OW h6mKBpLDmBePTdb2o9PBOVcsTABzVEYJhnj+E7XSz/o0ssCjrIPbZZqIi+CGUrpGZw Li7O9VNEYcqgXrOBvtZGgFbZ6c1f0dgchN8wGMU5R1ufxzBeiWLd9MDSdfUXZOi5yy zn8Wg2oAThXqA== 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 v6 bpf-next 06/10] lib/buildid: implement sleepable build_id_parse() API Date: Wed, 14 Aug 2024 11:54:13 -0700 Message-ID: <20240814185417.1171430-7-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240814185417.1171430-1-andrii@kernel.org> References: <20240814185417.1171430-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: wrhgc379dxkd1uczmbjgd7o6w3rykk9t X-Rspamd-Queue-Id: 08C921C000B X-Rspamd-Server: rspam11 X-HE-Tag: 1723661683-840798 X-HE-Meta: U2FsdGVkX18nndFrn2kE+EFd43pushx4/no+kQSGlkbQIIgCHhBmyjRw3T/9FKGUhwf6UcUJQN2JALWxRv27iOH/RQVjpFfKyNllJVnnmxt6QGMAM1EBnphoPilb1nn2BaoMA+FbGl98qhGVzvOl5QIbzrh6vxG6nG+hoK/IFIBMHO91sr84IdSj/fQrIhVoLWEdSpIAhHVyGGtHSOmUOBs8a17kBT1U/izaV7FCrjMGH/o96xMS3FuJ7WMgXJTEQbpX8NxH5L8XoySG9a8JgiiYdljA8zbrf1nOfVzyKUGbhC8qVZxl8uw0fvCFZVu6qvMDBWwww0QTPBapF34+sYJXBkJF6nBtLT42xQsdHFcNegAnqxn3++Twjb5XadcLjRJUkVDnlKR+Drcgwyscjhbg/7dBQHXrd77f6HSrLxUPbd4X5dzXD/seyw9LoJAWZxR1VGP/NbYfspnN8+I41tAC2h3IaUhDwK+3r/DBHgDsLDCEnuA2llRFDQGZ3fRI3txqCndEy1Yw0fB2D1DOOxNZGCj2Og0w1nZvyFbh6mNIbsDokwEWLFxTgZ3avgmGFTApDu7VckW0+T7U2D9Ph2+B7TxDQsTRUKN7cYfmauHGivRf/bBQAOE60ypsSGE4+VCRng9oshunQPD08g6k7p1C4ykWsrTz88NPHDI7pPLeDcnb8UzKotcSBYXNoKke3oicEqutuHs3lOzfZ/eRBSh9sNDnM+Guha5qAKCg94/BVzK38+J6IHK6POgKb1koXYhZ0jisV25Q5W1ut0Qdimu2yXN3a8NicyxS8Ze5vKKrE+rwceTVFtDF8sHSHR6xpzFJokEmKycOqapTMCjAp/IppTfGDVMsKeK9r/PyPS9amm+sIDQyz8IGVr+N4k9G8sKZMda61IoVCnWsKHwXNGv1NHLgOVEundQlC7Q3vKSXIrNly1mVPm1T+zlyE7ltsq/zTEmNenb75iWCRMh bWuQg98I eIpDyEU3CzNo+A2ccJOJ7ruWt5XyKkAeHifkyDMJsKdUGTQe/+gw6f5YA3HSdeHeZA6uLx//K1PhL3yhLKHaVGWM7nIJxlNmCmRj6Bdf04pyFOaql6MiJZ9/vzQjGI0MVFb6Oc/TbzyZjxc+ao/Z58Shsp8QWXEO1hGBGiwSTNOqhVoPpN6VfGdrNCEkF9D8pzNLpgVDP2SQ3/Y73BS8OA0zZoBaEJ4TGaaBNsyO+PvPog80KoVx6ilajmzZp4+IJXt/Dyu1rclRvpHYm65a5lxppAbU5nCFIUdcAiOzjmAymqKxLODKLeH86i2eO/XfBiMTBGFRqRhISbYgVcdFqLuIFl7fgBeQEIavSTvUqthcPILpC/nPQ4taLMy1vLoqwnKP6TD6OafsLBGTed/jlwa7QDg== 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 Reviewed-by: Shakeel Butt Signed-off-by: Andrii Nakryiko --- lib/buildid.c | 52 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index c1cbd34f3685..5da5a32a1af8 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 Wed Aug 14 18:54:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13763842 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 93BA8C3DA4A for ; Wed, 14 Aug 2024 18:54:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FCB96B009C; Wed, 14 Aug 2024 14:54:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8872B6B009E; Wed, 14 Aug 2024 14:54:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FE9E6B009D; Wed, 14 Aug 2024 14:54:47 -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 54D676B009B for ; Wed, 14 Aug 2024 14:54:47 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D58301C4892 for ; Wed, 14 Aug 2024 18:54:46 +0000 (UTC) X-FDA: 82451752572.10.FF26C00 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf21.hostedemail.com (Postfix) with ESMTP id 47D591C0018 for ; Wed, 14 Aug 2024 18:54:45 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=P2oqJnxL; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf21.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=1723661672; a=rsa-sha256; cv=none; b=20Qxjk23Xd9mI/DtB68/K2/gKmk3okPHicrl0CdHI08wOK4Vc7LF94HwDFzHvqMksuOI4O BhaABC0DISOTw7DgnHUPzIigm70HnPQR/OpFeofRFbJEvgeqQZekaFpQxCT+5FBYSPsGJh lQ6wbBM+IqfBulmmvDxmE9JcOxm0I/s= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=P2oqJnxL; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf21.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=1723661672; 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=9YiQaCoghCsLWmYlkim+fevjXSaq+VK3Z6NDIW8idvo=; b=lLQNrX3YKh6oLha1lR27Yqxcg0aW3DBbGCqzhp82IOzmu2dvQqGMBcox+iG0ZEcQGvRheF UXyx0tcezTIHYLMz/rxxgP/XGSj43W1nqPTTA9TJ2uyesKJD8XcPhkkDDR7MYqpckQdV0F MVsBNkDL5GY45Dk2eM4RNdnoZV0GXqE= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 527D561B7A; Wed, 14 Aug 2024 18:54:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DA7F2C116B1; Wed, 14 Aug 2024 18:54:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723661684; bh=qWFr1Itiok941tgbEAKnQiv3NKSiQ5xxi7FLGt3tlz8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P2oqJnxLIfziQGtAWipaX+42KrmgI7kmyiGn9lCHfQ7YV3hbZPyYqp1qrq0nJ7X4J vu2YQPTDjpU8CpoWnwkvkyZAWwGTy44DuRUGgLazKDfdZroDOHFMuBgtlmoT9Pq65i ltaSyU8EaRPg1S3AE6xw0mNfdUbufjlcwoYJtLc8UrEiv4mxakxwHH/B+W14kqzcFi jBxGbO6vK12zWE6PV7P+e37JNhp/cl63EZ8saqPP3DJwHvbmVpB4F6D7WZt3nwhJRT FJDhOUlK2ZI00qVZqfD/TQAWgdi7nVUz+b+2uKyg2KAfoEOgZ1UlCgVBYRVgd97KPR erJOLImS3PZzg== 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 v6 bpf-next 07/10] lib/buildid: don't limit .note.gnu.build-id to the first page in ELF Date: Wed, 14 Aug 2024 11:54:14 -0700 Message-ID: <20240814185417.1171430-8-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240814185417.1171430-1-andrii@kernel.org> References: <20240814185417.1171430-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 47D591C0018 X-Rspamd-Server: rspam01 X-Stat-Signature: tyh6ggfs8uz678yd4uspb8f593hr4urj X-HE-Tag: 1723661685-687222 X-HE-Meta: U2FsdGVkX1+2Z8miqjTL8Al7rZerSOWMJLxTIrFaSpw0h86P9aMJdrF9wWonDoRoa5OELuxQgBdWUo+NQQVMxumv6MJ/AnqdBTtPBWrbB9Go6va1QXqVqv+9NacNTd1A+dFIoG9xM45CTZaMMJ3qRK3pjjGlFj2J2dWFe3tG8yNz01zXH9KUMUg1KrmbArRKHvjFO4bXH61gXacm7rRSAkHOUkMH49ekyUqygr21c051WJiA24ruoR0+40Cc9X1tTJ6gMXpKooutbBFifspZaGuFf/8TMCk96xHLJohK8yKK3SwygYtcA98+RjGUk8GMLXqOVBTaEJ2RY2UcyRUjvd4t5iE3IDl4ag5rH6fPn7PA/LRBdfKdkPBgwmnzfYApaaKPRP6Ucr7gLyYuBEOfq9wCoLMyd7Pi6NOk6WiQqLV3eXB0wiQU8aADhNOwqXtuuF9Xl99SyUnlblIy/NysFVa/V4W5hXpR89bFg6hFbXvV50AlpdBWoYyfPCywnTt31A8HIKVIKa8gdywDt7Ck5eH2x0Z0CfzxZkcvLdp2UiKUFIZ3onJrEZt12hUuK7Ri3qHuoABafAREAluvikY1qaEkA+0i4PTAyrUqL1XWFtUpYWEV8jwzTjcYl7e5C/Q4N0hmkD5PHg9CzZXvc6PdVj42PYRPDIyX0ShDphBRGDYmqP1odpPofnQgYghTi9S7SRkU2vtmrh7l1lutdsNL08AlRNhRLWvnTDHwBCYtU8v5HXNrCqpQXKpSG7Z+TeqOPbIbWnuDm8sMBegiDNt0X31jRjhSErhnlpfyLvOd0pfg8s5oR22zZslzAietjc1jba7XKxtnZ34enaoMvijApuWPXRF1KmGada4ekYfJC7GgGUxtiTvTsT66qHma4l8lgvILrJEBXmLBAJb2mC6G3HkYyRDu6kx1Ui4d8cZDXR6J+pup9GqQ1xU5h4hAtpbVgTEfX2KGFoRHBInVsKn 3V1c0CLZ eRcmsMYSwmuuItK2hb2RnbOpERw/MNSVmRtmiaRbeo+eTKz5Qt69Wc/TkAM/hMeqUVpA7c9se3QPBUW2GK7rctmHlyKCjJz+r9zEQBQSmhv7ed7ICJ9x/zEXgnMac019TrTRPJ+In+ZsEvFQDxJuY1E6ha8/vVI9aJmBB5N7BvIRYYvvUwEVkfNX6oykT+XJwo0O3pxqW7ekUQ9y+HpTcNA5JL9ZLHzRkeWPeDkh/dMBdIqq1T0cUfNStfQQGeJ9t/0JbDJuOyAzL1gEvT7g+tMqGsw== 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 5da5a32a1af8..b404b89f61a3 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 Wed Aug 14 18:54:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13763843 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 6C20BC52D7F for ; Wed, 14 Aug 2024 18:54:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C08826B009F; Wed, 14 Aug 2024 14:54:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BB8A66B00A0; Wed, 14 Aug 2024 14:54:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A59E56B00A1; Wed, 14 Aug 2024 14:54:53 -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 843346B009F for ; Wed, 14 Aug 2024 14:54:53 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 4D2961A10D9 for ; Wed, 14 Aug 2024 18:54:53 +0000 (UTC) X-FDA: 82451752866.23.9343247 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf02.hostedemail.com (Postfix) with ESMTP id EF00B8002A for ; Wed, 14 Aug 2024 18:54:50 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="EhREDI/+"; spf=pass (imf02.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=1723661619; 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=QdRRy/mkqh3At2j9Otofprt+24DCayfQSCCM5yvnLo2J5XXBhK3nxjQ3+6NBbjg6NxsMK/ fM9eICyImfP+6SSw50yz/kAKGru54LG55qCJFAHLNtOqCICd5NDPfDtYLxwz8l6+zipVRd aJyhmpRIuiv2j/OHeQ9UsqP9VrSM6WE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723661619; a=rsa-sha256; cv=none; b=y+Dl2hn8xCsMy6916bzxT9i04hoJRoKqd3djklesnfAthrzvzFt1/Uz+Or+PoQoJ34vg7H XRO+VG+BK8FNIhYjj8T0Jt3T0WPsBbU8Ei7B4U9GEFXa4Sv/71eeNtWYYBliMeBwXVaMG+ d5hZ3GCLQA3el5/UcgVKm2OoPG/3iic= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="EhREDI/+"; spf=pass (imf02.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 4B97ACE1AE0; Wed, 14 Aug 2024 18:54:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2A131C116B1; Wed, 14 Aug 2024 18:54:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723661687; bh=/6b0RVTf764MWC/B03hIXQHQ85gqStIXNWpPwHJJCmw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EhREDI/+VA10TlydY0Ybqn3DOvzgy9AUFsl9OC9FGvdkSotaAAU8yzrYLPKYU0ymD k4vY6L1xAiB9NJNcwcmh1PoypZU9HrYk0go0IEs+iGdoaK8QH4alQ4A+pIanY3LtoL OG0LykM0odJ/j3otw/vzWv1Bffy6RrGHE8DjMPMcLx7m7HwLiUs1y4IGZjZzgg+C/k GKr4grE+A/PVv0ufGpdPM5rv9kDnCoW2kYsL22JJS0R7f5kaAXX7mbntr1TTXi/z1G 0OOZhAzRf6Ct4Z6O4T0HFrZih5Him2NXtFc9bOxeBwbZGwrFSH0d/8sumFCU40G3/8 a5IOJtvErDjFw== 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 v6 bpf-next 08/10] bpf: decouple stack_map_get_build_id_offset() from perf_callchain_entry Date: Wed, 14 Aug 2024 11:54:15 -0700 Message-ID: <20240814185417.1171430-9-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240814185417.1171430-1-andrii@kernel.org> References: <20240814185417.1171430-1-andrii@kernel.org> MIME-Version: 1.0 X-Stat-Signature: da7mqp77irzeog94hysa3ikqjpocddro X-Rspamd-Queue-Id: EF00B8002A X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1723661690-942208 X-HE-Meta: U2FsdGVkX1/qX9gvI++q5LGC64yIWE/9JHglkqgJ8J7AdMzkT198fam5MlYyI6BVBluQQJ0tUtIsvEx87RlGKzt5l1d6vO83VKR3nb+yQ0YQLFx/0BTcXeT7KN3h2Dc9IMc7Dwz7cve10nJsLZ/odFSzgk871lpD0aOZQgal6W21gowOFFkw5qU5IB8Hb/DkSfmajetOdd9Eg+lPQm4b3SnU+/qbfK0SEPrFLs59iCZqsV7J3JQpEX7TuVP/PQ7emN5f8UxxOWsw7L6cn2/rZ2bMlxbSjJBkLAIf1OtmkR8qHDXOLgND8JkB7QY3fmqZJdz/DYPcktYJfyLixi2OyI1B0M05boT4GyKc8E6gOYEZMmUHtIjqJUDDjSbe7JLzzTKP/f66nY6hKcAHRLXRzsmtYDWfu6DYLOdEU4EF2PTZgiiB+O1G8kEouTgkkrCaGf+QvBOHqlrLkEsAV9+LamhIgxHAE0hmHJfzh9OypUdBT8xuYV7kDZtxIJheYmhHaLwkUg15P4Hb9Zaqkl819m3c88ePCtakqfR40gg4BGgud/x9CJyGIhmEhG3s9EAgK7O4bjJ7prHUFKx4HXNB/sJ+2s+xZAnPPQVFFqtw0h/97wZf1G/q1B1zj0hWP1U9utw5HduEIx8vZVWsK8i8dtB2gkXR2Iwq+QI+grCvbIcAJXWo+33NHoHxBO4VKb9eIP5DnWgic/77w9AoNex8Zc2slPcv0FXA287DE+Lla8tdHqg/LNGXyFTFrEsYtDbR5kQTagVbLZUsZcnrE8MqxOl7Z6epQm7ED4K3G5fWjUiuVW5n+rahQJPtmCy2TVSYhciMy38yL/qSlaFgvlj+0Z64kCPDEMoIYL236PlxhERpLKVd/z8WkmTvHQr13SCPZKQtjqGdIAvSOfnbQAXcmzeIABfolIajyqSvXV52TRnO+nmt7ymUTzh4PyXP1DHUTrx0nfA2je9JqePSO8O UJ9eftK4 04+hJAhe+w0XoVQbqnykQoyGl0OTksRddbqYn7C+jNHgl87eLSA8doAi9D9TvwPut7ySY6d/GiqZOgkdkuQW9PdiLO1jG6JbjakxOyKVGQBT4OstNEbBBLryu2ZXyR/2PpDTOdx8SJ5FsuhW0YsxT48o2VQ00zvmg1d9rQTf2FhaRUCY2kS9fcMAoAhTpUsmeIaq5YWjJDlL9j5Pv4PuxwFfXN+B0Di4WLnb1O69BM7ec+nRfFDExzJ0ZXRMsbNemfCC4xkryjIqpqitY3nG1q0pB/dXw8uef2KGJ 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 Acked-by: Eduard Zingerman --- 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 Wed Aug 14 18:54:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13763844 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 BCAAAC52D7F for ; Wed, 14 Aug 2024 18:54:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2B006B00A1; Wed, 14 Aug 2024 14:54:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DDAD76B00A2; Wed, 14 Aug 2024 14:54:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C7B9F6B00A3; Wed, 14 Aug 2024 14:54:56 -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 A99826B00A1 for ; Wed, 14 Aug 2024 14:54:56 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 69A1CC1130 for ; Wed, 14 Aug 2024 18:54:56 +0000 (UTC) X-FDA: 82451752992.17.753713D Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf28.hostedemail.com (Postfix) with ESMTP id 2C9A7C002C for ; Wed, 14 Aug 2024 18:54:53 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=WBLO4QWL; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf28.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=1723661636; a=rsa-sha256; cv=none; b=ySe3YIZH1i+VoK1BUf3B6YYKK2i+k0YsbZ/z6VvmYwo8OVXTTHYnJ49fFrmBWfxhGyFn1p kEjM8AnIomRRSDOwT6ie8JVYf+UOcGdwdXtQ+bTu9IPxFhzxs8BpFPT3pzoGJ6RwEwg1Zm qX3ZGB/QNBv8TI0ELlBXD73TCN2h0r8= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=WBLO4QWL; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf28.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=1723661636; 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=ESDSRTiun0AyVIpvJcGJoHDU6NPP7ZY69w1Wg6gbNhALYh76lRocm4AERSS+X9p9Rb8mDT fr6ekuo8PT04zS2GCd/FucHPyKIM/Hn6cagr046JbJJTrDjZl03612S2BP6BZHfrXYlYs+ UhBLDtxQNLIk6lOS5ngjBF+V/5SaSUI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 04456CE1ADD; Wed, 14 Aug 2024 18:54:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5EA15C4AF0D; Wed, 14 Aug 2024 18:54:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723661690; bh=38WQXgNN+1KvRm9L1MT0/+U7sa1S+7OAXmxtP74KQv8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WBLO4QWLnqnTrg/vNb4R/a0lIlHs+8kDzupDvaehX7hCo8W0AL4ncRXytt+Gk/fg8 emDhk7tAAgejw6W2LXFFtuRShAWvgyv0X/cNtawZ/GxEXK7E3SEeG/7B0W9+VFPiZw ul9i6ZopOi1ODw9+9ynV3LVBbRObPhqS2DhHwr0ah1VlOwfE+8MdT1VqlVNp4vD5Oo ZipAGVhSqhZofMtJq4P2MvV5xigFXSXL7wKPphedm50U06MlvQRvGYeU28X1EknkOi CNY0LWGenVld2uJGTMQvpItMYmKaBD8PgIiwsI3tzA1/9ebq1+Q0dtRlNk0nIgM+Oz C3/9Pkr3ZmrDw== 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 v6 bpf-next 09/10] bpf: wire up sleepable bpf_get_stack() and bpf_get_task_stack() helpers Date: Wed, 14 Aug 2024 11:54:16 -0700 Message-ID: <20240814185417.1171430-10-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240814185417.1171430-1-andrii@kernel.org> References: <20240814185417.1171430-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 2C9A7C002C X-Stat-Signature: g9rz88g8fa4npwqmhogcy8mquno187ht X-Rspam-User: X-HE-Tag: 1723661693-969145 X-HE-Meta: U2FsdGVkX1/QaMiz3oT2cRaVgexfR/TFrHMVpLuZF4Hz76Uoqkaj+dApK0TYj3ANlGday6gDGzjK6qylgyOSKgIinWkwh6zgFS+HHrhv9TDeMdcS3ajrGzEEf4T9uuqr8m9fZM4mqCugtjPBWRbuvdhgilqsY4Z9eXb9quSADg8EQz2ztl9ojIQNqxmzlzlZHiViP9XSwIXcVv19VpzlxQRcwZt5w5+m/L9uRQkudhKI1CEac2O3afX/Ijf5CePmUcQcVu7EaDAjLV3k1TkEGlnxhY1pniGKAQ6vz3fz6BTJ4+HqTKr8OdvmsCAgILKKp1017wV0pfuQPZIc2Rs8tCdrTOoBvzULGch3PahsPcZTpDvAM+tm4AXdPn4B5oGIdgN4pK4ae4qvy/RKXzl82R/+yh7uKLOxbr4zuhjSKRnv7TqnOK1jWvVVaSGLjHkQ39qNR6FR76Lgc9bWzZkqm0GrEF7Haf64zMWISWI2EJYCA5+L4PpNZ0YUjaTZIXHKTBAppTrgzZ56A/2i/47lTLiAdOiFG+1nSb5qnb7ib+ZwtWlpH48eUzfTjTyIZyXbm09bJ27E0CAHf+Uup8z+87QcqCsWJXxp13y3kBZooPV1LLidb6vKTSRmw5tcmjc1RX4Bdw3pfW7byyN/Y51B/R2DWOQ7Ziwb9P0DVJp0OAkYWQncfb3zj+O6/c3B4bOU7gYcH+T3CTty5Lj5PGRxjuy1xKOsFOqaXyMNtKrBoUSO8zfMo4s1blmj97wP0T9ujqaNIk4qQAP1pr2ZtE0YF9+Rlf2O23ob8eo61tNyodglCq4/rg6YX0iMfFrVoA9nQWPrHwfZqnQWjW0Rf8ADT40eWy/42rypb01RKAUctxO9OhDOibOaqWY4TetZJ83gtUe2LSCW1LOqoEQe0LAtggbMtL4lnoYRsJA9LWIF9BO4zW+h+ri88lUxAPu+oHEvJksSX/ojkLDESlvrG0S RiNohxFH n7Wya0AXDJfiVOqgxtP+ABJHSMfSbL5fde+yfXsSjU1L/DnxFG6s0C6V5EY8TMd1s/5hgWIMWKWb4VXKgDGSWOIccAm/izKxAbZ/0s1F0Je5eKY2FC5sElVyPSuVef42PYl1ryXv8jC6XXLxx0rs08xBTtCnUCSiNyv7M1CdaTSRTJcmuEOsAspDNMp7oY7YmELssHn//dMN31Zi2O4GAkfBh5yCJH4pPaTYlRIfarjmaaFmkTBmmRYE1lPDPOSB2Xd6FjjZSXN3B6hreAOdfuHyEHeKyG2hYXPrl 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 Reviewed-by: Eduard Zingerman --- 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 Wed Aug 14 18:54:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13763845 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 C493DC3DA4A for ; Wed, 14 Aug 2024 18:55:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 57A386B00A3; Wed, 14 Aug 2024 14:55:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 529196B00A4; Wed, 14 Aug 2024 14:55:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A3736B00A5; Wed, 14 Aug 2024 14:55:00 -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 1D3446B00A3 for ; Wed, 14 Aug 2024 14:55:00 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9AE4D160EB4 for ; Wed, 14 Aug 2024 18:54:59 +0000 (UTC) X-FDA: 82451753118.07.0196111 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf23.hostedemail.com (Postfix) with ESMTP id 4A4E0140024 for ; Wed, 14 Aug 2024 18:54:56 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=jY4efrO+; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf23.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=1723661644; a=rsa-sha256; cv=none; b=zpsFoIHkesP3YBOcN/tNDP2KjUmN4DkrWkq+Gp8tDfPYbvTnG+DSmMP+uLtaXWVQEZoMgC tDWjAhmbhOPwmvHdq6IhkwPz7ZfnDSU/5NgegjiDfTRDSAu4m0F2pudsq813A8EwIep2HH 2wDaR3A/0GXO8UYT0p8yd2ybsGhQTD4= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=jY4efrO+; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf23.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=1723661644; 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=dBI+xKQoY6jP/o/0N1CNBzGSEiCpF1+Bpru6AL3byNBZNRoPSBILBcgL50gWg8ap8qI+8I OmwPpJv0t0sUW+NR+2BiL9KO5UaOHZP6nv4EGYX1PT04q5fuBNKq9aNg7JCkVcUaIewq1G K4bVsNqj+gJWpycYMoiYWIfyQQuZ9GU= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id B7861CE1ADE; Wed, 14 Aug 2024 18:54:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AC1BFC116B1; Wed, 14 Aug 2024 18:54:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723661693; bh=hAHRGfhHTT27DNY5NeOM9zG7twQB27NQc7Kw1Yx1Eb8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jY4efrO+owN7jqjsqntSS7+Qfa6KzvkD4aIvlgKxbRk7nxONoD+rfkKb/Tr/sPf7m tFobHLjJ4nzHBkw+lEkcK7AUjVLLv9CWuHnCu4zk4v2Pn9k1bErLXthB0xAOuN0EO2 3cWx9Uy2msd/MvkNjkIETZAfe4f/V2COPJCxS9sGl2sZgGST48n9aV+qA+xjHckWwD JLI/ppzjQCcZU2US1L96q+pPHwlWvw2RqTYPwVZ5M0uG9yaiC8rzjIBr9F7FeaN6S1 PEvz+2qA4U51RcNrYkZsjNQL6NisZhgb4IgQ2mqQGHEmrNnPoUNwJDH7Wsxu+AjMjM N8019AtW2Y5Tg== 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 v6 bpf-next 10/10] selftests/bpf: add build ID tests Date: Wed, 14 Aug 2024 11:54:17 -0700 Message-ID: <20240814185417.1171430-11-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240814185417.1171430-1-andrii@kernel.org> References: <20240814185417.1171430-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4A4E0140024 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 8qdnt1winsf3595uut43qmjbmorjgq7q X-HE-Tag: 1723661696-449597 X-HE-Meta: U2FsdGVkX1/1ys5jseGscl0NqeS7liXIIVtWATRZ+SBdcg2SQbxHpWcLh29dwPFHmUarn+7aeFGQNu4Woj3+EeteiJQPnrsWGmISlO1/UrWRijXPLGFK7UMdWYpCqUPGskoYphB6zFJoTtbf0q1XGhjr74hDePU16a7/11kXPKG8emuGgLG9omNcHNs0W6tu4pePnXmOEEx7lKQbOjKI+iHG23bHsLAoqMA2Lg8ORxYFaEvIqnIRKr/7Fl6g5YaBlWnOOATrzIR3jdRRzv+Wgc3uioJexh5aDNzGreLvtWpcB+dNTTUuK1FcWzEndQez5+IrCdVoiCk/hDc5ADyhRuZPNnDIvkFnIOVj9gKiKLJP28tZEfmp44nRfsodrfUku0uiHit2sefgTtvfNZM7nWT9n3la3atUUOfrpmZlF1xthTusSZ3e/AE4NcJniXb47kYMjgzVnhXohvj28fSL80Qok3KbWrpL+cgMTTmfXt5UejQoixYyHUB3ik0UDwtsd60djeZQzmxc5IbmlzKKFlaICniZ+gOKAOdzK+TjKhNUTtjNQDwW9fF6y/udNyaUMob2Xx334y6y2sxug0MSJFutm2ihkxVP3uxXpH3HSjlbdPZcr1BePPQLolKZN7NZ7BQbwDEfxX2q5GFDJvy/amCtM6hGDO/2BjXNZ1xV2xkOFYk+LPCyUg7zklDUv9s1uRFr4PlCZ+ubSoNErVf9DwQbZaqZ89JPeBFEiuUPBy+pHJSR3WHybaO1Dn6ZO5WuqQ1rGX9scdR5pKrE9jH5d8ke0Ux6NoCdDIp16ly4sSutjqcS7KzQRWQwNKgsu55cHDn/YvjbYrwFOjA47FQ6rMvD3jRSSr2gr71xEVKCpZ7sj/giYqAasSSRg3EG3YUiw9gdNQFdsDBUSkk52U/D89NgqiWMTkbo1bAFQ0CDF4tweEKylwhcxs3umdnFODNUC4u7ykb9eMtQaLPMrwG tAy7noQS b5NYfcn03cIdeHNwJAoSLCwLzde7685WG6iWF99GQWQhsZRrS88N0fbpafHOttz1qsHBuYUCSJg/oIR2lIv2o83iJjJgNz0irl2MsgblaA9SRDcPz9QGvcJje9sz86GMA8N2cx9ZgM4M/NRybuWItrfGh933y2FdlfofVtyWyRiwAX/xcYYDLmdr9kzbT6b6B9rieOA3adcD48oKd1UW4dvCKSRID12fyHmu0S8qN0dkWRSM= 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 Acked-by: Eduard Zingerman --- 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); +