From mboxrd@z Thu Jan 1 00:00:00 1970 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 16D38C64EC7 for ; Tue, 28 Feb 2023 09:32:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9E0446B0072; Tue, 28 Feb 2023 04:32:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 990576B0073; Tue, 28 Feb 2023 04:32:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 858246B0075; Tue, 28 Feb 2023 04:32:31 -0500 (EST) 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 780E46B0072 for ; Tue, 28 Feb 2023 04:32:31 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 47B7CA120B for ; Tue, 28 Feb 2023 09:32:31 +0000 (UTC) X-FDA: 80516185302.14.785C40F Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf16.hostedemail.com (Postfix) with ESMTP id 6FBC1180016 for ; Tue, 28 Feb 2023 09:32:29 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=CCMhUpnp; spf=pass (imf16.hostedemail.com: domain of jolsa@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=jolsa@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=1677576749; 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=6AycnDt8+0rcYrLO7sdk8oACYDMbk9ComyCDcdN7198=; b=zcUbGWX1dm1Nr6Gw/A5FKcOiY0qythjvEHDHbbsiUuNuekl0b0HqSprD4pu+RVaRNNpODF GV68I4ZZr1pCkzIwqwraDYbfLYTaAgeZlbkD/u3nyFpLVG5riA+G1CHLxh0t3mQUxOp5Oe g2gbBNvvbBb8VKwqkIWmWqZ5oy2f5IA= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=CCMhUpnp; spf=pass (imf16.hostedemail.com: domain of jolsa@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=jolsa@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677576749; a=rsa-sha256; cv=none; b=3f4HlSN9ExP6LNjglbtkRjpnvPOqoXJPAfaFczHrV4Pa3tHtAVuIltgbqgvSUNmkI3C+XZ pES5jdvYHmTDjzHIEFADmaMoMUVf5CLB5Bd6Eug00hzHhJDzLZA3cDGVZw7KacprOwx+PW X+fZ3XbHhEd/wfxzee81lTl4okJSMow= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id D1EF9B80DBA; Tue, 28 Feb 2023 09:32:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8407EC433D2; Tue, 28 Feb 2023 09:32:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677576746; bh=ExAvqANCMBI4PJSTQpy4LAwvnp3ITxAWpaClUl88RVE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CCMhUpnpGksEEGVwFsSBWIiWR6/jhTz7gdAOMW0XGAqCW3Bj+m2xAKRd9R9cLhyyD 5nj4PTeCALb7EP0oVbdT2lZO8R5OoWY2pU4XyhJKQrR750OurrWAt6wFFfIPh/GJqs E2L10wJ4+1R9tZStJIeiEGieR1XBKD672vDnq1DfOwnFKn07MZnHG7gCRyiMOphPko 4HXjhf791ZOX7ETRh/arEojsyBYj9l+o8/hkKURTDV79DYqNbPuUVYTJyNcoVfgARB LI98PEm/Bzwd/vEcVwUnqcSMsuj12i6ceHqusdn0iyCKLSmyjumomrLC86d8o3nAm0 4h3ddy6Zn981A== From: Jiri Olsa To: Alexei Starovoitov , Andrii Nakryiko , Hao Luo , Andrew Morton , Alexander Viro , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Matthew Wilcox Cc: bpf@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-perf-users@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Daniel Borkmann , Namhyung Kim Subject: [PATCH RFC v2 bpf-next 1/9] mm: Store build id in inode object Date: Tue, 28 Feb 2023 10:31:58 +0100 Message-Id: <20230228093206.821563-2-jolsa@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230228093206.821563-1-jolsa@kernel.org> References: <20230228093206.821563-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 6FBC1180016 X-Rspam-User: X-Stat-Signature: nho1gcn9os5fsw1gzfo4ubts59utzfux X-HE-Tag: 1677576749-415447 X-HE-Meta: U2FsdGVkX19t6KlZ0CnCE5FXMc7vEHMJNtNBtdmqtX0puRZEk1rzS3vRan5z6cW8Pvtic3zXa8WCjdOXcoGbL8CnUY4x/pEEFwWWcSAbD4cDVdJEAFAbRJ2IZ0nVGlbI8US/UST975N1WparOVbfGHm6qOpp2oLDUVQRdnL6pj/cI/PTSGds9sRy12OVVwz6MrS+GDyyP7Wq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fcJM73WkzwTyUOJKna6HZytyBDUr1qQ4GyRs/00MOEqlUMCqukOZuTWRmWwt9KMQTbdz7T0CX9tGPVVUxTDtDkLTkZsVkkkZPngwHHSmlinbumq5XbHewoF72MOhX05Q= 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: Storing build id in file's inode object for elf executable with build id defined. The build id is stored when file is mmaped. This is enabled with new config option CONFIG_INODE_BUILD_ID. The build id is valid only when the file with given inode is mmap-ed. We store either the build id itself or the error we hit during the retrieval. Signed-off-by: Jiri Olsa --- fs/inode.c | 12 ++++++++++++ include/linux/buildid.h | 15 +++++++++++++++ include/linux/fs.h | 7 +++++++ lib/buildid.c | 40 ++++++++++++++++++++++++++++++++++++++++ mm/Kconfig | 8 ++++++++ mm/mmap.c | 23 +++++++++++++++++++++++ 6 files changed, 105 insertions(+) diff --git a/fs/inode.c b/fs/inode.c index 4558dc2f1355..e56593e3c301 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "internal.h" /* @@ -228,6 +229,10 @@ int inode_init_always(struct super_block *sb, struct inode *inode) #endif inode->i_flctx = NULL; +#ifdef CONFIG_INODE_BUILD_ID + inode->i_build_id = NULL; + spin_lock_init(&inode->i_build_id_lock); +#endif if (unlikely(security_inode_alloc(inode))) return -ENOMEM; this_cpu_inc(nr_inodes); @@ -296,6 +301,11 @@ void __destroy_inode(struct inode *inode) if (inode->i_default_acl && !is_uncached_acl(inode->i_default_acl)) posix_acl_release(inode->i_default_acl); #endif +#ifdef CONFIG_INODE_BUILD_ID + build_id_free(inode->i_build_id); + inode->i_build_id = NULL; +#endif + this_cpu_dec(nr_inodes); } EXPORT_SYMBOL(__destroy_inode); @@ -2242,6 +2252,8 @@ void __init inode_init(void) SLAB_MEM_SPREAD|SLAB_ACCOUNT), init_once); + build_id_init(); + /* Hash may have been set up in inode_init_early */ if (!hashdist) return; diff --git a/include/linux/buildid.h b/include/linux/buildid.h index 3b7a0ff4642f..485640da9393 100644 --- a/include/linux/buildid.h +++ b/include/linux/buildid.h @@ -3,9 +3,15 @@ #define _LINUX_BUILDID_H #include +#include #define BUILD_ID_SIZE_MAX 20 +struct build_id { + u32 sz; + char data[BUILD_ID_SIZE_MAX]; +}; + int build_id_parse(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); @@ -17,4 +23,13 @@ void init_vmlinux_build_id(void); static inline void init_vmlinux_build_id(void) { } #endif +#ifdef CONFIG_INODE_BUILD_ID +void __init build_id_init(void); +void build_id_free(struct build_id *bid); +void vma_read_build_id(struct vm_area_struct *vma, struct build_id **bidp); +#else +static inline void __init build_id_init(void) { } +static inline void build_id_free(struct build_id *bid) { } +#endif /* CONFIG_INODE_BUILD_ID */ + #endif diff --git a/include/linux/fs.h b/include/linux/fs.h index 2acc46fb5f97..72e63dcf86a1 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -699,6 +700,12 @@ struct inode { struct fsverity_info *i_verity_info; #endif +#ifdef CONFIG_INODE_BUILD_ID + /* Initialized and valid for executable elf files when mmap-ed. */ + struct build_id *i_build_id; + spinlock_t i_build_id_lock; +#endif + void *i_private; /* fs or device private pointer */ } __randomize_layout; diff --git a/lib/buildid.c b/lib/buildid.c index dfc62625cae4..2c824e3dcc29 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -5,6 +5,7 @@ #include #include #include +#include #define BUILD_ID 3 @@ -189,3 +190,42 @@ void __init init_vmlinux_build_id(void) build_id_parse_buf(&__start_notes, vmlinux_build_id, size); } #endif + +#ifdef CONFIG_INODE_BUILD_ID + +/* SLAB cache for build_id structures */ +static struct kmem_cache *build_id_cachep; + +void vma_read_build_id(struct vm_area_struct *vma, struct build_id **bidp) +{ + struct build_id *bid = ERR_PTR(-ENOMEM); + int err; + + if (!build_id_cachep) + goto out; + bid = kmem_cache_alloc(build_id_cachep, GFP_KERNEL); + if (!bid) + goto out; + err = build_id_parse(vma, bid->data, &bid->sz); + if (err) { + build_id_free(bid); + bid = ERR_PTR(err); + } +out: + *bidp = bid; +} + +void build_id_free(struct build_id *bid) +{ + if (IS_ERR_OR_NULL(bid)) + return; + kmem_cache_free(build_id_cachep, bid); +} + +void __init build_id_init(void) +{ + build_id_cachep = kmem_cache_create("build_id", sizeof(struct build_id), 0, + SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT, NULL); +} + +#endif /* CONFIG_INODE_BUILD_ID */ diff --git a/mm/Kconfig b/mm/Kconfig index ff7b209dec05..02f40d58ff74 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1183,6 +1183,14 @@ config LRU_GEN_STATS This option has a per-memcg and per-node memory overhead. # } +config INODE_BUILD_ID + bool "Store build id in inode object" + default n + help + Store build id in iinode object for elf executable with build id + defined. The build id is stored when file for the given inode is + mmap-ed. + source "mm/damon/Kconfig" endmenu diff --git a/mm/mmap.c b/mm/mmap.c index 425a9349e610..e6c8ec05804f 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2530,6 +2530,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, pgoff_t vm_pgoff; int error; MA_STATE(mas, &mm->mm_mt, addr, end - 1); + struct build_id *bid = NULL; /* Check against address space limit. */ if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) { @@ -2626,6 +2627,10 @@ unsigned long mmap_region(struct file *file, unsigned long addr, if (error) goto unmap_and_free_vma; +#ifdef CONFIG_INODE_BUILD_ID + if (vma->vm_flags & VM_EXEC) + vma_read_build_id(vma, &bid); +#endif /* * Expansion is handled above, merging is handled below. * Drivers should not alter the address of the VMA. @@ -2690,6 +2695,23 @@ unsigned long mmap_region(struct file *file, unsigned long addr, goto free_vma; } +#ifdef CONFIG_INODE_BUILD_ID + if (bid) { + struct inode *inode = file_inode(file); + + spin_lock(&inode->i_build_id_lock); + /* + * If there's already valid build_id in inode, release it + * and use the new one. + */ + if (inode->i_build_id) + build_id_free(inode->i_build_id); + + inode->i_build_id = bid; + spin_unlock(&inode->i_build_id_lock); + } +#endif + if (vma->vm_file) i_mmap_lock_write(vma->vm_file->f_mapping); @@ -2759,6 +2781,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, mapping_unmap_writable(file->f_mapping); free_vma: vm_area_free(vma); + build_id_free(bid); unacct_error: if (charged) vm_unacct_memory(charged); -- 2.39.2