From patchwork Thu Aug 29 17:42:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13783516 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 97E0DC87FCD for ; Thu, 29 Aug 2024 17:42:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E1DC56B0089; Thu, 29 Aug 2024 13:42:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DA5466B00A6; Thu, 29 Aug 2024 13:42:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BF9006B00B6; Thu, 29 Aug 2024 13:42:58 -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 90C796B0089 for ; Thu, 29 Aug 2024 13:42:58 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4981014053A for ; Thu, 29 Aug 2024 17:42:58 +0000 (UTC) X-FDA: 82506003636.30.67C63B0 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf01.hostedemail.com (Postfix) with ESMTP id 752CE40011 for ; Thu, 29 Aug 2024 17:42:56 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=PWZIwYJt; spf=pass (imf01.hostedemail.com: domain of andrii@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724953287; 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=CgwGfgjZwwNKNiR5h2Kb486VbQFMf/adnAPBvAZvt3E=; b=wGRPKyugONotmfYUQ0TXASaDRYlqW6wYWvszTEbO5fBJDBICHQDleJiOd744J8MHRIk0dH ShhglVaI2C7DU+pcRUJUncWG6joUK9AU3AeaVc/c1+p8moUlKcqlQOB9HJys+l0Tvx/Q5e b5nwzAaqyMch08HMFkjmWuVkgcH6fIM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724953287; a=rsa-sha256; cv=none; b=VYhhSTtVoQ5Lw3eNP2e1Qe9m/e2Gf7mvlaIMaZKh7WmZcZxorR/VC+B1xBtJGtCqhC8z/N /0weC+GbHxLCnKoAiSKG/CWiWCHtKjvBmoJiu6Cf/QYTuCSSM8qSTqYqwXu5mvxBEpRKG+ qjlFjb/Hqqr1VsbvWt3p2S6Hz30bA7A= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=PWZIwYJt; spf=pass (imf01.hostedemail.com: domain of andrii@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id A444FAE2820; Thu, 29 Aug 2024 17:42:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 418F3C4CEC1; Thu, 29 Aug 2024 17:42:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724953374; bh=fCwSimqQps0x37cNpSBPuZZv0BI1f3GW/HwGWSK6VXw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PWZIwYJtQmn+LkoSZ3Jlq0eKfKvTkd3g+cHFDhLEZBB3AhDDZl/QK43WNZgezjRmJ yC+o+yHV4bFHUWPVkJWYnJfkS4h2EHVSyMIxwNiTbFiadPCMzgjwEqC5cgoUO7DtVT dGeSrHgmQ7UDfHYUFl+j9bT1HlwnANmxZFm7+1HY/vljZaJq4gSftnP3gzJkN+GLfw gY/pRCGUx7VHVKzG4kykFHkKRfy8E5wH/bpt5Aju2XNXAzfUq77/ZM6zaTPiqSkseK XsPHvmQZ4YWofUaw4k9BBOgZCXeIaKfnnaJ2Ja2/7Ri7uPzQ68X2YF5RmdsA0UDySi mvNcyn2zb/KIA== 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 , Eduard Zingerman Subject: [PATCH v7 bpf-next 06/10] lib/buildid: implement sleepable build_id_parse() API Date: Thu, 29 Aug 2024 10:42:28 -0700 Message-ID: <20240829174232.3133883-7-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240829174232.3133883-1-andrii@kernel.org> References: <20240829174232.3133883-1-andrii@kernel.org> MIME-Version: 1.0 X-Stat-Signature: 1yza8dhrxs7kcxgf8os1bsqrmm36qox6 X-Rspamd-Queue-Id: 752CE40011 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1724953376-855371 X-HE-Meta: U2FsdGVkX18HkwcJhUNjhc6Ikjx/6XkWjXbtwDPwEZawxFiZNJh3EYK1APTYeZSt9kTIKg+wYibWf3qF1NM0dx6D8WzlG3SFokp2x5y3E/sM28ooUeGP2UW5wqyWsVwn13UTcks1qGmnynyqJIdGU4HoKelr0muMemgOsxy0/uutbLaeBjo71SzJaJ+N27DuWu1UXWtN0Co7I8QI6eKrD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uKptv/Qymc3NCUtkqIGy0SUvaFmo6SR/+csahAHceo8NoPGTkvrPjU+MAex1ckO9oIKu0YMxM9zM 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: Eduard Zingerman Reviewed-by: Shakeel Butt Signed-off-by: Andrii Nakryiko --- lib/buildid.c | 54 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/lib/buildid.c b/lib/buildid.c index c1cbd34f3685..18ef55812c64 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,16 @@ 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))) { + filemap_invalidate_lock_shared(r->file->f_mapping); + r->folio = read_cache_folio(r->file->f_mapping, file_off >> PAGE_SHIFT, + NULL, r->file); + filemap_invalidate_unlock_shared(r->file->f_mapping); + } + if (IS_ERR(r->folio) || !folio_test_uptodate(r->folio)) { if (!IS_ERR(r->folio)) folio_put(r->folio); @@ -287,18 +298,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 +310,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 +338,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 +367,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 */); } /**