From patchwork Wed Jul 24 22:52:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13741433 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 2EC1AC3DA63 for ; Wed, 24 Jul 2024 22:52:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B43A76B00A0; Wed, 24 Jul 2024 18:52:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AF5176B00A2; Wed, 24 Jul 2024 18:52:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9BFBD6B00A1; Wed, 24 Jul 2024 18:52:36 -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 7AF226B009F for ; Wed, 24 Jul 2024 18:52:36 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E3985120C8C for ; Wed, 24 Jul 2024 22:52:35 +0000 (UTC) X-FDA: 82376147070.18.4E03814 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf25.hostedemail.com (Postfix) with ESMTP id 9D8C5A000A for ; Wed, 24 Jul 2024 22:52:33 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=uG6+2et4; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf25.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=1721861500; 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=CvVzl7e48xMmAZAYYYfWEbe7EWp8AyF1ZpNYRqbWocc=; b=sRHB6tQU4P/kZWDen30TjlA6yqR1gUIKnkqQDgKMFf7gqCrOhP//Y6cMELInakCVxHYAZP zPutYOfYm7ppF7SkIlvL8yESTCdqzWue1azEQumL5qY/VofDMmtZRIgnfB3hPbuV6paNfN 9sMW3Y49FXsSg8pu6tOiLT9EgGG46/E= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721861500; a=rsa-sha256; cv=none; b=P9bMjqDO5JOrJA34tZcN7kCrOje+5I/OtsbetQmbQY3QZLDqIEgi7KizBkk/Iy+ehXBjGr JGu6aCGeWzhtsNhavn6mr+5DDDVAp3nRRaci/DAehjHUET5prl7cJDlLn9fU/sEfb+t0Gg WWzcbB0Y1YkdmzcBlicaIjnihd30wh8= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=uG6+2et4; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf25.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 ECEF4CE1280; Wed, 24 Jul 2024 22:52:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19CC1C32781; Wed, 24 Jul 2024 22:52:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721861550; bh=+9jpZjOCZAyOPgnFUaMIL0c75rF54bF41tQH1aEgnRg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uG6+2et43OBP3FNk1iAcFBteE3sbEyD50GG4olgea0zPxXigaux0PlOVnBi9DW6Uq 2v9IJ37KZRRMt0IftZlUrokKuS58syUGRkf5jRRD5CPF485YrJBDMNsPgsUKhiwBu5 S2N8fpL547zmvJPqVlCkToQPmuIh8hFSIRyfsKKjsnbI88zGAbiDcEsFu8HzQMXu9D a1659IeuFCW5FMJj4bDS4HAwBLvkQefpnYzsOgXloykMnDilPaN7PwR9r6hjboiOqR h+EHRyjOBCfUBsfLUssy8gs+82RQRHZYjuWZRi6Lo3N6iSbK/0DYeTkZq8G3bAIlMm C69F0VwDpOZZQ== From: Andrii Nakryiko To: bpf@vger.kernel.org Cc: linux-mm@kvack.org, akpm@linux-foundation.org, adobriyan@gmail.com, shakeel.butt@linux.dev, hannes@cmpxchg.org, ak@linux.intel.com, osandov@osandov.com, song@kernel.org, Andrii Nakryiko , Omar Sandoval Subject: [PATCH v2 bpf-next 05/10] lib/buildid: implement sleepable build_id_parse() API Date: Wed, 24 Jul 2024 15:52:05 -0700 Message-ID: <20240724225210.545423-6-andrii@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240724225210.545423-1-andrii@kernel.org> References: <20240724225210.545423-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 9D8C5A000A X-Stat-Signature: f46wnhciopf9hz5ixuewqutczjyando6 X-Rspam-User: X-HE-Tag: 1721861553-834457 X-HE-Meta: U2FsdGVkX193YpUpDjNLz1PuJnqXN0wURXiXR3HLBxc8I91jURaVmllpIsbrzVRXOnllHa0SiL/QH5pxXsOoRArvPL9s1pq66oBpMtYdmmq9UYKD3Q5+ptMLlODYi8eDp6HQzll7hViBWgXSQNOnxFFWUgVHRpdIWI2IYLJuHGtcEwSfQyrEKICW3BRYP69Xecq16gZVdpBLKi/AVdrQKTGYc3UrW92h6CRnuC9fmc6+kt8Gr6R1MPVleBwvvdqRDvBrrJmtFZXAfG3ScFMbWzZG3fQRWWZFepxui9TBOdIZEKF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X5pKXKxEjDGSjq7BA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Extend freader with a flag specifying whether it's OK to cause page fault to fetch file data that is not already physically present in memory. With this, it's now easy to wait for data if the caller is running in sleepable (faultable) context. We utilize read_cache_folio() to bring the desired file page into page cache, after which the rest of the logic works just the same at page level. Suggested-by: Omar Sandoval Cc: Shakeel Butt Cc: Johannes Weiner Signed-off-by: Andrii Nakryiko --- lib/buildid.c | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index 5f898fee43d7..23bfc811981a 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -20,6 +20,7 @@ struct freader { struct page *page; void *page_addr; u64 file_off; + bool may_fault; }; struct { const char *data; @@ -29,12 +30,13 @@ struct freader { }; static void freader_init_from_file(struct freader *r, void *buf, u32 buf_sz, - struct address_space *mapping) + struct address_space *mapping, bool may_fault) { memset(r, 0, sizeof(*r)); r->buf = buf; r->buf_sz = buf_sz; r->mapping = mapping; + r->may_fault = may_fault; } static void freader_init_from_mem(struct freader *r, const char *data, u64 data_sz) @@ -60,6 +62,17 @@ static int freader_get_page(struct freader *r, u64 file_off) freader_put_page(r); r->page = find_get_page(r->mapping, pg_off); + + if (!r->page && r->may_fault) { + struct folio *folio; + + folio = read_cache_folio(r->mapping, pg_off, NULL, NULL); + if (IS_ERR(folio)) + return PTR_ERR(folio); + + r->page = folio_file_page(folio, pg_off); + } + if (!r->page) return -EFAULT; /* page not mapped */ @@ -270,18 +283,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; @@ -292,7 +295,7 @@ int build_id_parse_nofault(struct vm_area_struct *vma, unsigned char *build_id, if (!vma->vm_file) return -EINVAL; - freader_init_from_file(&r, buf, sizeof(buf), vma->vm_file->f_mapping); + freader_init_from_file(&r, buf, sizeof(buf), vma->vm_file->f_mapping, may_fault); /* fetch first 18 bytes of ELF header for checks */ ehdr = freader_fetch(&r, 0, offsetofend(Elf32_Ehdr, e_type)); @@ -320,6 +323,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 @@ -333,7 +352,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) { - return -EOPNOTSUPP; + return __build_id_parse(vma, build_id, size, true /* may_fault */); } /**