From patchwork Thu Mar 16 17:01:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13178088 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 E9ABEC6FD1F for ; Thu, 16 Mar 2023 17:02:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 76CA1900004; Thu, 16 Mar 2023 13:02:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 71C22900002; Thu, 16 Mar 2023 13:02:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 60B4D900004; Thu, 16 Mar 2023 13:02:13 -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 4FC55900002 for ; Thu, 16 Mar 2023 13:02:13 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id ED1BE414F4 for ; Thu, 16 Mar 2023 17:02:12 +0000 (UTC) X-FDA: 80575379304.01.9DBC116 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf23.hostedemail.com (Postfix) with ESMTP id 2018414001F for ; Thu, 16 Mar 2023 17:02:10 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=mko2AyIG; spf=pass (imf23.hostedemail.com: domain of jolsa@kernel.org designates 139.178.84.217 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=1678986131; 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=wze9ahveMNWilMvLv9kZJBY00oxXwpxy7n4mr0neYog=; b=xSeff/v8N0x4T5v5LT3bbS4N7B2xIICkEEwJRnhXVP/ZW3GDpTFhMTDyVgu8WWGtZZG6V3 nYSLbKq4FO4djRYAePvTiTtRgP1sxxcQFKiI8Ye8uCdoNp8Hnw6+K54S8KHJ8Hdic8lDkz S2fXPnBTSBH+o9kXJH2OEraIouZm7Fo= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=mko2AyIG; spf=pass (imf23.hostedemail.com: domain of jolsa@kernel.org designates 139.178.84.217 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=1678986131; a=rsa-sha256; cv=none; b=S6Y8qXeZCghleF6E2nKUwYaDPgPtMgpfs7x25lnrn7s2dRjwOa/zJ8dFSfzmF8W7SuG7Ha V46asS8K+kbt6S2JCp8oxfA6QgGpbuI+ibO5JFFebvFN8Ym2ZYKXR3vBF57PhjFYco6uM6 6Kde3P/EPSDA3cY7288y5R7OerRqrEM= 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 dfw.source.kernel.org (Postfix) with ESMTPS id EA9CF620AF; Thu, 16 Mar 2023 17:02:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 94DD7C433EF; Thu, 16 Mar 2023 17:02:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678986129; bh=rlnHUcArwM6SIvbHJnlNQfqpKxk+RvN2l+D5fdKLMa8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mko2AyIGWCfuAb/notSpksldx+wnHyuJJ4/SEak6IfxwljXnE+r8/+kMT7THTFZQ5 Wncxf77aMXc3FbAfZfDb08GQs5sdSXxJP/oxSMw5mFt26kNKx9/5wT/iIghXvWGrPt p/shHWVxwdSZ8CaChz7D04gE4GZtpXAlZNM9LhqY6aE2MbZAGy6rv+Yt6siwcFcTvz fMFLw05Y8JJ7ka5JyaIx/wptVp0ilYP5XRMB4WqQlNarQsTWdfLNtGysPEqe5mNwBC eGb6hOm37AxCLhqGwPJ7Uf6WZevagTDZ0Lmk5+qQ9EwJwNKT5AqiCYBFavWZH5OdJw czU9fH4Y2iLaw== 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 , Dave Chinner Subject: [PATCHv3 bpf-next 1/9] mm: Store build id in file object Date: Thu, 16 Mar 2023 18:01:41 +0100 Message-Id: <20230316170149.4106586-2-jolsa@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230316170149.4106586-1-jolsa@kernel.org> References: <20230316170149.4106586-1-jolsa@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 2018414001F X-Stat-Signature: uknci194nxzsdw49nfbdqkgtqizub4z7 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1678986130-796619 X-HE-Meta: U2FsdGVkX18rizr1Nni0nO9BW2joiShvB8H0hTGqGJ2PCmsAIY+pkN5lXFjmu9ZSGJ/5+oC9rdN917wbXeYkeGYLCIBJzqn3jOxZNvknndzViwrjHY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BUuNomRaq3i7AGTqTOzCHxnUwJZPKoJvaVZNZjrysFUGDPlNN4dTZgImzE6ymT7vm+7bxGGMyvBbSbaDvs5H/Q+LluscbaEYku03bEuMww97i42rRPe2yRaEguuWcyJsHZRgsD6ZfoknB8y6vKtD9N+NHsNwEGnX8bpFM0/CC8UyGA7xTdyQ/Q/WfCK8kUUvHXFqy5/FFfRmDx1WpdgXsvlN5De29ID1DDfzB67Y= 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 object for elf executable with build id defined. The build id is stored when file is mmaped. The build id object assignment to the file is locked with existing file->f_mapping semaphore. The f_build_id pointer points either build id object or carries the error the build id retrieval failed on. It's hidden behind new config option CONFIG_FILE_BUILD_ID. Signed-off-by: Jiri Olsa --- fs/file_table.c | 3 +++ include/linux/buildid.h | 17 +++++++++++++++++ include/linux/fs.h | 7 +++++++ lib/buildid.c | 42 +++++++++++++++++++++++++++++++++++++++++ mm/Kconfig | 9 +++++++++ mm/mmap.c | 18 ++++++++++++++++++ 6 files changed, 96 insertions(+) diff --git a/fs/file_table.c b/fs/file_table.c index 372653b92617..d72f72503268 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -29,6 +29,7 @@ #include #include #include +#include #include @@ -48,6 +49,7 @@ static void file_free_rcu(struct rcu_head *head) { struct file *f = container_of(head, struct file, f_rcuhead); + file_build_id_free(f); put_cred(f->f_cred); kmem_cache_free(filp_cachep, f); } @@ -413,6 +415,7 @@ void __init files_init(void) filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT, NULL); percpu_counter_init(&nr_files, 0, GFP_KERNEL); + build_id_init(); } /* diff --git a/include/linux/buildid.h b/include/linux/buildid.h index 3b7a0ff4642f..b8b2e00420d6 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,15 @@ void init_vmlinux_build_id(void); static inline void init_vmlinux_build_id(void) { } #endif +#ifdef CONFIG_FILE_BUILD_ID +void __init build_id_init(void); +void build_id_free(struct build_id *bid); +void file_build_id_free(struct file *f); +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) { } +static inline void file_build_id_free(struct file *f) { } +#endif /* CONFIG_FILE_BUILD_ID */ + #endif diff --git a/include/linux/fs.h b/include/linux/fs.h index c85916e9f7db..ce03fd965cdb 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -977,6 +977,13 @@ struct file { struct address_space *f_mapping; errseq_t f_wb_err; errseq_t f_sb_err; /* for syncfs */ +#ifdef CONFIG_FILE_BUILD_ID + /* + * Initialized when the file is mmaped (mmap_region), + * guarded by f_mapping lock. + */ + struct build_id *f_build_id; +#endif } __randomize_layout __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */ diff --git a/lib/buildid.c b/lib/buildid.c index dfc62625cae4..04181c0b7c21 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -5,6 +5,7 @@ #include #include #include +#include #define BUILD_ID 3 @@ -189,3 +190,44 @@ void __init init_vmlinux_build_id(void) build_id_parse_buf(&__start_notes, vmlinux_build_id, size); } #endif + +#ifdef CONFIG_FILE_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; + + 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 file_build_id_free(struct file *f) +{ + build_id_free(f->f_build_id); +} + +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_FILE_BUILD_ID */ diff --git a/mm/Kconfig b/mm/Kconfig index 4751031f3f05..5e9f48962703 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1202,6 +1202,15 @@ config LRU_GEN_STATS This option has a per-memcg and per-node memory overhead. # } +config FILE_BUILD_ID + bool "Store build id in file object" + default n + help + Store build id in file object for elf executable with build id + defined. The build id is stored when file is mmaped. + + It's typically used by eBPF programs and perf subsystem. + source "mm/damon/Kconfig" endmenu diff --git a/mm/mmap.c b/mm/mmap.c index 20f21f0949dd..1c14e8c84d3a 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2480,6 +2480,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, pgoff_t vm_pgoff; int error; VMA_ITERATOR(vmi, mm, addr); + struct build_id *bid = NULL; /* Check against address space limit. */ if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT)) { @@ -2575,6 +2576,10 @@ unsigned long mmap_region(struct file *file, unsigned long addr, if (error) goto unmap_and_free_vma; +#ifdef CONFIG_FILE_BUILD_ID + if (vma->vm_flags & VM_EXEC && !file->f_build_id) + vma_read_build_id(vma, &bid); +#endif /* * Expansion is handled above, merging is handled below. * Drivers should not alter the address of the VMA. @@ -2647,6 +2652,12 @@ unsigned long mmap_region(struct file *file, unsigned long addr, if (vma->vm_flags & VM_SHARED) mapping_allow_writable(vma->vm_file->f_mapping); +#ifdef CONFIG_FILE_BUILD_ID + if (bid && !file->f_build_id) { + file->f_build_id = bid; + bid = NULL; + } +#endif flush_dcache_mmap_lock(vma->vm_file->f_mapping); vma_interval_tree_insert(vma, &vma->vm_file->f_mapping->i_mmap); flush_dcache_mmap_unlock(vma->vm_file->f_mapping); @@ -2667,6 +2678,12 @@ unsigned long mmap_region(struct file *file, unsigned long addr, expanded: perf_event_mmap(vma); + /* + * File can already have f_build_id assigned, so we need to release + * the build id we just read and did not assign. + */ + build_id_free(bid); + vm_stat_account(mm, vm_flags, len >> PAGE_SHIFT); if (vm_flags & VM_LOCKED) { if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) || @@ -2711,6 +2728,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);