From patchwork Wed Feb 1 13:57:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13124343 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 CCEE5C05027 for ; Wed, 1 Feb 2023 13:58:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5CD6F6B0074; Wed, 1 Feb 2023 08:58:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 57D7D6B0075; Wed, 1 Feb 2023 08:58:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F8B26B0078; Wed, 1 Feb 2023 08:58:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 2F6036B0074 for ; Wed, 1 Feb 2023 08:58:03 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E798240E5A for ; Wed, 1 Feb 2023 13:58:02 +0000 (UTC) X-FDA: 80418876804.05.C543B3A Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf18.hostedemail.com (Postfix) with ESMTP id 94E751C0015 for ; Wed, 1 Feb 2023 13:57:59 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="pL/BhnAe"; spf=pass (imf18.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=1675259879; a=rsa-sha256; cv=none; b=YSKSjNreOw4Y6pIe7bI41898ZQd2UBL5Qlfnsol1BI/NT3n29LLPjle81hdtT4vigQ5Nb5 z0qISQDBy9m1TBYFbA5frdoNMymQIt4vdMPOeFU7iCoa9tIIuuyKgvL6IZc84Gh7uFNmO4 1v5sSK2gIfA4YkvOqzyqNJGZgZ8zMgM= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="pL/BhnAe"; spf=pass (imf18.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=1675259879; 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=MuZH+IPJnu5v9sBbwISVcxC/pXd0XY1S9yPFrtG6BTk=; b=DzlrZLqT5IlUMb10GWb01A/+vvMZ9pYoYfLKbYg/hUHhmIVtfVMTYyuDkmWdB+vySrFLoR AWEfM7eBwXVOZET4p4xlC18cCAIWtFhvKNTzmTU3zghVqg3XedoCTFzQ4Q06AjwxMEGgRR VMJGmQYcYTCH8kNBDAoXVbaXHXNF0LE= 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 0DA53B82047; Wed, 1 Feb 2023 13:57:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0AF92C4339C; Wed, 1 Feb 2023 13:57:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1675259876; bh=K5e4iDve4s0PwNPOjBVEL1fPWhsoZO4QpGzfxgnTdGs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pL/BhnAeEPoVMghB6g8EuD67OegImMGmZlqdKM37DGTUdUe3+VC6epzOBADZgEGou t0NAX257+ANME19ZsslzL9A8wNMPEDs3fAPa1Etr6CdfZKe2of4jmigsOeNag9OHZo pZ916wTMZP46oFcVUsxAZVdrH00C2BU+sF8HipWo7etAqLa5f8jeFJK7vGYBfrdoA+ rwlc4aruSxQ6DfucwkOmS1zp8baxmsewY5QumlCNZBU+tJfDgktTuK8gT8DF3Z8EgY u2+QPZ59Tl3KaGkV02uAwkZL5k9i7OFPzUMZT/krlSOlGWkSZnn8iHvVVND97jhJZK wSkoFCw+vN8yQ== From: Jiri Olsa To: Alexei Starovoitov , Andrii Nakryiko , Hao Luo , Andrew Morton , Alexander Viro , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo 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 Subject: [PATCH RFC 1/5] mm: Store build id in file object Date: Wed, 1 Feb 2023 14:57:33 +0100 Message-Id: <20230201135737.800527-2-jolsa@kernel.org> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230201135737.800527-1-jolsa@kernel.org> References: <20230201135737.800527-1-jolsa@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 94E751C0015 X-Rspamd-Server: rspam01 X-Stat-Signature: z7a8auq5sea75micqsqwzyw3pppsrffd X-HE-Tag: 1675259879-565403 X-HE-Meta: U2FsdGVkX1/tBkv/oBvp/2xJkmsxA8n73OVmhH0u+7ZY299KMIGUU2KRd9Uyw4kZp2mTfcOPJzKGD5hj4g5o0Jn0KLOYZ1lA/awV1S+6oUUAuWNC2vaOvZHQ0NqP9cAOYQ8QYpRhku8XLJcidqNba10UA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5x/V20LqjDt9Dc+3GXrfzCOuRBZAeQhAPm5yweGFoIHuzuVO9QDv03W7rr2IQi7CPWMsSnOnVGZcnIfCd5jd2D48bkogcTu3wi53z/pIWPfkWL0qVEhb+I+o+uj7ZyWL5/gSAQiJbC2JghHjSTEJY5DJUmZ+Fg2SPsdGYKzGEFOVNogRvMrJ3rad0ce0zxXVvgfxGCF+wViTzVrDnmh+hFKvlwJRKh+eATaPLmj6Jzs9RpNfUoAtYH6By4QqDw== 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. 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 | 3 +++ lib/buildid.c | 44 +++++++++++++++++++++++++++++++++++++++++ mm/Kconfig | 7 +++++++ mm/mmap.c | 15 ++++++++++++++ 6 files changed, 89 insertions(+) diff --git a/fs/file_table.c b/fs/file_table.c index dd88701e54a9..d1c814cdb623 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -28,6 +28,7 @@ #include #include #include +#include #include @@ -47,6 +48,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); } @@ -412,6 +414,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..7c818085ad2c 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); +int vma_get_build_id(struct vm_area_struct *vma, struct build_id **bidp); +void file_build_id_free(struct file *f); +#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 c1769a2c5d70..9ad5e5fbf680 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -975,6 +975,9 @@ struct file { struct address_space *f_mapping; errseq_t f_wb_err; errseq_t f_sb_err; /* for syncfs */ +#ifdef CONFIG_FILE_BUILD_ID + struct build_id *f_bid; +#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..7f6c3ca7b257 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -5,6 +5,7 @@ #include #include #include +#include #define BUILD_ID 3 @@ -189,3 +190,46 @@ 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; + +int vma_get_build_id(struct vm_area_struct *vma, struct build_id **bidp) +{ + struct build_id *bid; + int err; + + bid = kmem_cache_alloc(build_id_cachep, GFP_KERNEL); + if (!bid) + return -ENOMEM; + err = build_id_parse(vma, bid->data, &bid->sz); + if (err) { + build_id_free(bid); + /* ignore parsing error */ + return 0; + } + *bidp = bid; + return 0; +} + +void file_build_id_free(struct file *f) +{ + build_id_free(f->f_bid); +} + +void build_id_free(struct build_id *bid) +{ + if (!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 ff7b209dec05..68911c3780c4 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1183,6 +1183,13 @@ 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. + source "mm/damon/Kconfig" endmenu diff --git a/mm/mmap.c b/mm/mmap.c index 425a9349e610..a06f744206e3 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,13 @@ 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_bid) { + error = vma_get_build_id(vma, &bid); + if (error) + goto close_and_free_vma; + } +#endif /* * Expansion is handled above, merging is handled below. * Drivers should not alter the address of the VMA. @@ -2699,6 +2707,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_bid) + file->f_bid = bid; + else + build_id_free(bid); +#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); @@ -2759,6 +2773,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); From patchwork Wed Feb 1 13:57:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13124344 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 9BB2DC05027 for ; Wed, 1 Feb 2023 13:58:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 113EC6B0075; Wed, 1 Feb 2023 08:58:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C3026B0078; Wed, 1 Feb 2023 08:58:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ECCEB6B007B; Wed, 1 Feb 2023 08:58:12 -0500 (EST) 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 DD63F6B0075 for ; Wed, 1 Feb 2023 08:58:12 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A74BB16073E for ; Wed, 1 Feb 2023 13:58:12 +0000 (UTC) X-FDA: 80418877224.01.20D36FD Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf09.hostedemail.com (Postfix) with ESMTP id DCAAB140003 for ; Wed, 1 Feb 2023 13:58:10 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=JnHy4CCn; spf=pass (imf09.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=1675259891; 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=gSnnt4fgX2I9uu82kp63+YnlF3N0Lzc/HDoxGN8vSwA=; b=kvnXUUteI8/YhdSC1de+qeRRlHls103SK7s6lpL0N+ZhLjDQqoPRFXlXLrILcopd/9I5/n rPnvag0pzaI3SkBWb36bbo7UDUK020SyXiWMEInlTmZi98VwCk5cOu6/d6oEL3sgWn5/Rm 7chU1J6xLFMPOQw2fABSOp1HvSE0qrI= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=JnHy4CCn; spf=pass (imf09.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=1675259891; a=rsa-sha256; cv=none; b=4vJnfMgRu5dGPLWEOmMJav7uZbPD6BY9tkC1MYjKqxDplE+Iz5XpobRVxkvRBfX+zEmja/ mGro3kFT3gFx3nmFf/xFTii7loicN+yfQABE6AqVIXKuXWVV3bCFPPrvnf8zP8vCjxnEcZ h6iOD+5oFftllqKcydmHv3jgyTEihZY= 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 0EFA9617BC; Wed, 1 Feb 2023 13:58:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DF1C7C4339B; Wed, 1 Feb 2023 13:58:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1675259889; bh=ut4TSVh/smk46JzyD72UAqn8UMJWTZ29FILHTx+yUwk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JnHy4CCnRASBpAW99dtJkbPX2oqBQQXDvGScvJq3o+/FaodL245/D9NJYxQNfNUsZ CMfE08ZZyQxrXuSMX6uO4lScYcc/QgQO6IdbezuEiloYTYdTdeMjt/DHZ2tJVA2nro lM9RoLjYSHdt9TmUMwqAm/Jx30Kz6WwRjHo/AuFSjgOjBt0zv5V5lx9CuomjQ881Pv eYXBR/aNwaA5hZTHvCZ/bRuaC72f8SSsEJPVdQGeMITJheap/wLUx3bYuvBMl9MFhH ye62SNud97XMHzIXR5+WUMVs3uW1p8/qPvccJ8q++3h1uCmOiECCYxs54/JclrE3nt NOavObpiK6Nlw== From: Jiri Olsa To: Alexei Starovoitov , Andrii Nakryiko , Hao Luo , Andrew Morton , Alexander Viro , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo 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 Subject: [PATCH RFC 2/5] bpf: Use file object build id in stackmap Date: Wed, 1 Feb 2023 14:57:34 +0100 Message-Id: <20230201135737.800527-3-jolsa@kernel.org> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230201135737.800527-1-jolsa@kernel.org> References: <20230201135737.800527-1-jolsa@kernel.org> MIME-Version: 1.0 X-Stat-Signature: ueozs71fgdu8d6nnquxxfwgpyy9xh8su X-Rspam-User: X-Rspamd-Queue-Id: DCAAB140003 X-Rspamd-Server: rspam06 X-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/3OXCXxzrUy8sag/lLS0X2XqnQoXUoX5si6G4GDMCeMhe2FdNf79Qv7gKozh6mRs1Q0XqCioqp94Dh6dSqQbN14xl2oXbrPG4mp7hUQk4DM97V7rVyVB5w/xDuBBwf5zN7bwt2mdyEOjwO/cvHRa19hbqAiqF1QO2qY7ZPkDn7gnByC/58YITyJ0UdV1EOj1RLdLVk6BuZS8QatgOzhFHGjSvaXclJ+kxckjBrQsH3ez6QwdCY6mFtVa/N5iDXPxaVTGrJtBs9KKwdWubsvD3lZzO1BPXKcMylVKJSKI/BlinwZ8KqKzBFdWa0JaE6wg3F716r960NjIAfzGN04Ca5zLlqeMEXYglbAJg29HkCH0ag6lErqWhU= 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: Use build id from file object in stackmap if it's available. Signed-off-by: Jiri Olsa --- kernel/bpf/stackmap.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index aecea7451b61..944cb260a42c 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -156,7 +156,15 @@ 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]); +#ifdef CONFIG_FILE_BUILD_ID + if (vma && vma->vm_file && vma->vm_file->f_bid) { + memcpy(id_offs[i].build_id, + vma->vm_file->f_bid->data, + vma->vm_file->f_bid->sz); + } else { +#else if (!vma || build_id_parse(vma, id_offs[i].build_id, NULL)) { +#endif /* per entry fall back to ips */ id_offs[i].status = BPF_STACK_BUILD_ID_IP; id_offs[i].ip = ips[i]; From patchwork Wed Feb 1 13:57:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13124345 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 52994C05027 for ; Wed, 1 Feb 2023 13:58:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C83A36B0078; Wed, 1 Feb 2023 08:58:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C33FB6B007B; Wed, 1 Feb 2023 08:58:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFC406B007D; Wed, 1 Feb 2023 08:58:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A1D986B0078 for ; Wed, 1 Feb 2023 08:58:26 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7BBFC80DE0 for ; Wed, 1 Feb 2023 13:58:26 +0000 (UTC) X-FDA: 80418877812.13.15AF932 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf17.hostedemail.com (Postfix) with ESMTP id 8EDDF40015 for ; Wed, 1 Feb 2023 13:58:24 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=rvH85aZN; spf=pass (imf17.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=1675259904; 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=8FCui8WL491QoBd3thR5w+VKt0dx+TMQh95kNOjhXdM=; b=SHAaaIKMGpKtWusqHw2PZTFUtB3neMgERx14xqEhYDLti+/VbM4wGGJ62nbzkRM7mL7icD 1Ob3TRpixk+P66nDPVkeZcgSZZVaCPghNh2Qr3nHPD5JbE9PhY5BxwZCScM7JiSuEqL8Ib DQruNyfUc83R3kBdapL8R8DOv1lrEgg= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=rvH85aZN; spf=pass (imf17.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=1675259904; a=rsa-sha256; cv=none; b=VFGMJEg8mcVJwYZrYXoOLGT9L38iJKjsKgxQa2sYy6i7Xg+YvMkTZDzzMtSV+WWhS3LHMc Yi/zj1W7jBMU2m0iXrwvZHkkVeePzhfKQy5NhY9SozPZIIg6o4l4WnC68yr/xXxlSDOqJA /t5Ko41RKC5y47bvW36KVSv5Dvq3ze0= 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 2CB9DB821A4; Wed, 1 Feb 2023 13:58:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 325E2C4339B; Wed, 1 Feb 2023 13:58:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1675259901; bh=aLPnAbPHT8a02PSgEV9wcJJL8dIRbp5TcW8HLfo0WbA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rvH85aZNkyrEZQUXF6LXPZDUhDCIneD+wlLL8wRJAzoq7xdGsbZ4t9y4SSe7WIaYV ZsVEfIlDeihAbZRx2dqyt4yynUmxQ742B/1VsTqAN3o/+tb8MjpZujt1pnkTLeX9Is FpMyWfOOKcWUCpcAeCuz0mVsDgqyctFbWxB6UIGy7MexEJ1ExskMh4Sm6yQTli7XLu DBNcNlbqf9PNhhpjpP7jDzCBD8dT6rTWEbaPDJMy1b2Noh+HdZ23q0LBZ0wnUrzSg/ y3F7rfav54xFq/3hGRpV/VFwWAnwAH2T2XgZDoyB/yvAcZuh5vrUqO2XiROuOwbTU9 Uecjvuc+qafkA== From: Jiri Olsa To: Alexei Starovoitov , Andrii Nakryiko , Hao Luo , Andrew Morton , Alexander Viro , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo 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 Subject: [PATCH RFC 3/5] perf: Use file object build id in perf_event_mmap_event Date: Wed, 1 Feb 2023 14:57:35 +0100 Message-Id: <20230201135737.800527-4-jolsa@kernel.org> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230201135737.800527-1-jolsa@kernel.org> References: <20230201135737.800527-1-jolsa@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 8EDDF40015 X-Stat-Signature: abqk1ffithzn5pf7nhjcndoe3caemkxn X-Rspam-User: X-HE-Tag: 1675259904-15985 X-HE-Meta: U2FsdGVkX19z/L3KfDntx0LkUIHfQNkwaby3Y0Q+Y01EYtfszkmjwmBo4gTxnfkbRTFTG0BociDhrTLcx4q9zpCZNqbXTt0icMf3Q2bVP8aHIV2DwTNbUBiHJ+u8f2IZ2XCmq7/7Xc+Op0fNowNmCRXaLHoVNnGWVoetSGp+w0xZ9vrBYpsSiSBOA30YtDKauZZrJYtqX8SdKQsEsXibbpcsd85j5WAsDzi0zADDwgq9QjK7jy3lExEfTZoq59yqkLGl8rXvFTiq9Cs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NdVrU83gGJ5pPEnP8hvTkDGg9aUIRu2OyjR3KvCaxDIPtDEPrx0Lu6Cf/Qf8IQZFsjmQ4B8330ZeWIJ5uREKNaiuAubPGrtXBLQBBjAuEzsOfSY 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: Use build id from file object when available for perf's MMAP2 event build id data. Signed-off-by: Jiri Olsa --- kernel/events/core.c | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index d56328e5080e..44001fc7edb7 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -8508,6 +8508,9 @@ struct perf_mmap_event { u32 prot, flags; u8 build_id[BUILD_ID_SIZE_MAX]; u32 build_id_size; +#ifdef CONFIG_FILE_BUILD_ID + struct build_id *f_bid; +#endif struct { struct perf_event_header header; @@ -8520,6 +8523,38 @@ struct perf_mmap_event { } event_id; }; +#ifdef CONFIG_FILE_BUILD_ID +static void build_id_read(struct perf_mmap_event *mmap_event) +{ + struct vm_area_struct *vma = mmap_event->vma; + + mmap_event->f_bid = vma->vm_file ? vma->vm_file->f_bid : NULL; +} + +static bool has_build_id(struct perf_mmap_event *mmap_event) +{ + return mmap_event->f_bid; +} + +#define build_id_data mmap_event->f_bid->data +#define build_id_size mmap_event->f_bid->sz +#else +static void build_id_read(struct perf_mmap_event *mmap_event) +{ + struct vm_area_struct *vma = mmap_event->vma; + + build_id_parse(vma, mmap_event->build_id, &mmap_event->build_id_size); +} + +static bool has_build_id(struct perf_mmap_event *mmap_event) +{ + return mmap_event->build_id_size; +} + +#define build_id_data mmap_event->build_id +#define build_id_size mmap_event->build_id_size +#endif + static int perf_event_mmap_match(struct perf_event *event, void *data) { @@ -8564,7 +8599,7 @@ static void perf_event_mmap_output(struct perf_event *event, mmap_event->event_id.pid = perf_event_pid(event, current); mmap_event->event_id.tid = perf_event_tid(event, current); - use_build_id = event->attr.build_id && mmap_event->build_id_size; + use_build_id = event->attr.build_id && has_build_id(mmap_event); if (event->attr.mmap2 && use_build_id) mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_BUILD_ID; @@ -8573,10 +8608,10 @@ static void perf_event_mmap_output(struct perf_event *event, if (event->attr.mmap2) { if (use_build_id) { - u8 size[4] = { (u8) mmap_event->build_id_size, 0, 0, 0 }; + u8 size[4] = { (u8) build_id_size, 0, 0, 0 }; __output_copy(&handle, size, 4); - __output_copy(&handle, mmap_event->build_id, BUILD_ID_SIZE_MAX); + __output_copy(&handle, build_id_data, BUILD_ID_SIZE_MAX); } else { perf_output_put(&handle, mmap_event->maj); perf_output_put(&handle, mmap_event->min); @@ -8708,7 +8743,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_read(mmap_event); perf_iterate_sb(perf_event_mmap_output, mmap_event, From patchwork Wed Feb 1 13:57:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13124346 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 74166C636D3 for ; Wed, 1 Feb 2023 13:58:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1FD976B0075; Wed, 1 Feb 2023 08:58:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1AC676B007B; Wed, 1 Feb 2023 08:58:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 074D96B007D; Wed, 1 Feb 2023 08:58:38 -0500 (EST) 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 EDDEA6B0075 for ; Wed, 1 Feb 2023 08:58:37 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A537A80DE5 for ; Wed, 1 Feb 2023 13:58:37 +0000 (UTC) X-FDA: 80418878274.24.8444D80 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf10.hostedemail.com (Postfix) with ESMTP id E1146C002A for ; Wed, 1 Feb 2023 13:58:35 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=XJ3SKUrQ; spf=pass (imf10.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=1675259916; 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=Mjx+8xVvWjllN48WqXEcZO36K7F05llFubGc6MsHgnk=; b=JBIs8BpKgNY42wQZevu8bwiit5lMbMkIeONXvgl4hVIXTA8ZpZheF/l8JDdP2kqnJ17UvB NXl5aU70rcGrvKp/qH75AfBz4cPuHoMn5tXWviEtQAhrNEQStUJSsabJgVPcXjmNSOwzCn 06a9MjZ5X2+BiiMjXgYNNnBkSSbFhUA= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=XJ3SKUrQ; spf=pass (imf10.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=1675259916; a=rsa-sha256; cv=none; b=siCA+nHAE+IOV2AUvcVv1F07vtJlStVBELbj2bG8/3t8LVe2oTBe/bv50BvrXZQ+q5YsQL XFHklyrN439dQ4xp5IwLnR36iRhQWoc1Azx+eEmSzciPvnTbR1TAA/SeadDeeyAvpnbgoH xXjhxPx5Z5u7gjRYUe0/BH822iBxbcw= 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 08A11617B6; Wed, 1 Feb 2023 13:58:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ABA6EC433EF; Wed, 1 Feb 2023 13:58:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1675259914; bh=9vGidv5KK+VK6x+sZbtxgG6kbbBhATOZQnveCyOWlzU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XJ3SKUrQ3KRicKqjZGSlmATsqBY/2uxl+ittryw9F94qxr3IHhp8yiAmNVgZV4F28 BvIzPZXif+N2jAVTa87Q4OyhJ+2kVDPKOU/IfJHNjiICJ3BTzDoCKr81v0+8jZF2/3 tyG7KooQLVd33ycGsDNsM+PVm4rVb8RcSASZfDR52CRsHXcm23iTtf32iutEA3ibfw mw2MTKI3hM/tHgLmqSy652HghxvCdCrkKNsbVbt5x3rkV5FjD4Jg+YJvC+SdbAj0nR i3tcDShTPNAaM96Nt1HVkQOA9p15tNuJ5D77H274EWMbGtBD12KW7WqTpQGtJ9TkDo L9AEDZVsw0XpA== From: Jiri Olsa To: Alexei Starovoitov , Andrii Nakryiko , Hao Luo , Andrew Morton , Alexander Viro , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo 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 Subject: [PATCH RFC 4/5] selftests/bpf: Add file_build_id test Date: Wed, 1 Feb 2023 14:57:36 +0100 Message-Id: <20230201135737.800527-5-jolsa@kernel.org> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230201135737.800527-1-jolsa@kernel.org> References: <20230201135737.800527-1-jolsa@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: E1146C002A X-Stat-Signature: y5utk4h1gzf6x8nojsc35hntzcx59p1w X-Rspam-User: X-HE-Tag: 1675259915-890673 X-HE-Meta: U2FsdGVkX18vKKW1/f3GZxkF637gKwsV+5oRKfncZgLeRVM4gofu8hqm17Mi9aoR59P/dhjC5aCZrT+9pmmeKTrIC42hkFdZOsvmE6KviPArYv6+4UaFUMcjxKJAkJ/auYbTEJoF7YCwek26vgROWyE2406/+N3+8IKso+uODbD+19cI99R3FyY/XIuUooeTpJ3Nu7PEH8OTw4R9I9irVPo31wFEGGbAj9/F4HZ5IoDfilYXGVfRaSchQJoSYaeCGuyjVaJqbMHi15E0ZgCyXjlKHdl89tcv6n0Imtm11xs7WuZ7UHcQclKScSpHwUrrNrkb7/PSs/1rfchb4Fcq4uIEtzWjwf0or/ElYx4Fn2n/6wUHpiqFrXUOnaS6W94NYwj0szydWPt9tKEF/LkR6H+SMJ/9+V/U+r+QxnMcRwzEd+H7ame3R5ZbO3fgXz841kw81XSu42liiuDPEZ9Le3gkiNTW/6rM8FKrPZhIYsdozUJSxQm7/bvH35xywUyfrSpl5MmUpkdtsQP2dTAzu5WTibDW3vp6L0UDwiYEQKZAAngcB2GXVVS9hzWFo+znS4rsM9qvxDi49bR1pxYfcUU5KQFok+rBtG27Qv4ITe8SPugJ+McR31EuGkmjFdLAqfNgmDks23+5g+1QCDVKIBqB++sqBEEu1dXVhpT+SSo5sCUPV06hXr8NN1yif0HZzcU+k7FfwRsg9lRcyLuf74YPkWPc6ev2nNb+Nex3e0sYyz6FLOIRnn0l+Qq+1Oic2N8TQv7XlXXqSK0k1jhqqUWQSOmchpcV8ZIpoTH6EB+apCw7OGGCOsas0cM6vZ70hyFAiVcAIf8/LJVfn3pjz5jI7OF2Yha7VpcakQKrE/0ym19ApVzZ5ZZDnvfZ9TOV6S1prEBH+ey5qn66WKrRaaiEccPM+IMms91D5ivLWsm1wiNozoSTG5/U4pT458ECY2fua7SjLLsWTunFS+u XBwIfZ/P uNmeVggZ+PmmObU9KyAGXG8HnTxeKUS2dd89KFDOVyLjY8eCSCLYVC71SvuV2k8JWx5G31R6UuirVqCoXf0USYz+tQvuCoJt4/Pr2OsuXm+vkwqtYRFvg1hnCKM+95E1TQJrj3PT2RsLDmMsRxMHEPNEzje+KoanP6jDDIQ87msShRoez2Rv/o2BQQmlPCpleDkAVCcYYk/5BEJjaMeTkx7Kx3dnuyDQqv1BvaMRJdvIFuJu27AO+MGgwQepU7C6HWOJ+YjwzfZl5mx3vqeDaXD1KpSBvPvOn4Y3DXo60+zUea8BYw8YVEmDelUHbsQdZg/b4JkI8OnN6CvQpmLrGDJMUljBAj7YzimMaYRFIXUovRcVou14lETEKE1Q+aQo8M+8pcc0gyseNEoT9o3gZ/IhBmxglMdPerWF9aeB6A8P4X4JwRxN9yKfYN411JDpKw4njCuiPjGbHLl6tdIlntkIgaX409u0tepEe 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: The test attaches bpf program to sched_process_exec tracepoint and gets build of executed file from bprm->file object. Signed-off-by: Jiri Olsa --- .../selftests/bpf/prog_tests/file_build_id.c | 70 +++++++++++++++++++ .../selftests/bpf/progs/file_build_id.c | 34 +++++++++ tools/testing/selftests/bpf/trace_helpers.c | 35 ++++++++++ tools/testing/selftests/bpf/trace_helpers.h | 1 + 4 files changed, 140 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/file_build_id.c create mode 100644 tools/testing/selftests/bpf/progs/file_build_id.c diff --git a/tools/testing/selftests/bpf/prog_tests/file_build_id.c b/tools/testing/selftests/bpf/prog_tests/file_build_id.c new file mode 100644 index 000000000000..a7b6307cc0f7 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/file_build_id.c @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include "file_build_id.skel.h" +#include "trace_helpers.h" + +#define BUILDID_STR_SIZE (BPF_BUILD_ID_SIZE*2 + 1) + +void test_file_build_id(void) +{ + int go[2], err, child_pid, child_status, c = 1, i; + char bpf_build_id[BUILDID_STR_SIZE] = {}; + struct file_build_id *skel; + char *bid = NULL; + + skel = file_build_id__open_and_load(); + if (!ASSERT_OK_PTR(skel, "file_build_id__open_and_load")) + return; + + if (!ASSERT_OK(pipe(go), "pipe")) + goto out; + + child_pid = fork(); + if (child_pid < 0) + goto out; + + /* child */ + if (child_pid == 0) { + /* wait for parent's pid update */ + err = read(go[0], &c, 1); + if (!ASSERT_EQ(err, 1, "child_read_pipe")) + exit(err); + + execle("/bin/bash", "bash", "-c", "exit 0", NULL, NULL); + exit(errno); + } + + /* parent, update child's pid and kick it */ + skel->bss->pid = child_pid; + + err = file_build_id__attach(skel); + if (!ASSERT_OK(err, "file_build_id__attach")) + goto out; + + err = write(go[1], &c, 1); + if (!ASSERT_EQ(err, 1, "child_write_pipe")) + goto out; + + /* wait for child to exit */ + waitpid(child_pid, &child_status, 0); + if (!ASSERT_EQ(WEXITSTATUS(child_status), 0, "child_exit_value")) + goto out; + + if (!ASSERT_OK(read_buildid("/bin/bash", &bid), "read_buildid")) + goto out; + + ASSERT_EQ(skel->bss->build_id_size, strlen(bid)/2, "build_id_size"); + + /* Convert bpf build id to string, so we can compare it later. */ + for (i = 0; i < skel->bss->build_id_size; i++) { + sprintf(bpf_build_id + i*2, "%02x", + (unsigned char) skel->bss->build_id[i]); + } + ASSERT_STREQ(bpf_build_id, bid, "build_id_data"); + +out: + file_build_id__destroy(skel); + free(bid); +} diff --git a/tools/testing/selftests/bpf/progs/file_build_id.c b/tools/testing/selftests/bpf/progs/file_build_id.c new file mode 100644 index 000000000000..639a7217a927 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/file_build_id.c @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +int pid; +u32 build_id_size; +char build_id[20]; + +SEC("tp_btf/sched_process_exec") +int BPF_PROG(prog, struct task_struct *p, pid_t old_pid, struct linux_binprm *bprm) +{ + int cur_pid = bpf_get_current_pid_tgid() >> 32; + struct build_id *bid; + + if (pid != cur_pid) + return 0; + + if (!bprm->file || !bprm->file->f_bid) + return 0; + + bid = bprm->file->f_bid; + build_id_size = bid->sz; + + if (build_id_size > 20) + return 0; + + memcpy(build_id, bid->data, 20); + return 0; +} diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c index 09a16a77bae4..f5557890e383 100644 --- a/tools/testing/selftests/bpf/trace_helpers.c +++ b/tools/testing/selftests/bpf/trace_helpers.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include "trace_helpers.h" @@ -230,3 +231,37 @@ ssize_t get_rel_offset(uintptr_t addr) fclose(f); return -EINVAL; } + +int read_buildid(const char *path, char **build_id) +{ + char tmp[] = "/tmp/dataXXXXXX"; + char buf[PATH_MAX + 200]; + int err, fd; + FILE *f; + + fd = mkstemp(tmp); + if (fd == -1) + return -1; + close(fd); + + snprintf(buf, sizeof(buf), + "readelf -n %s 2>/dev/null | grep 'Build ID' | awk '{print $3}' > %s", + path, tmp); + + err = system(buf); + if (err) + goto out; + + f = fopen(tmp, "r"); + if (f) { + if (fscanf(f, "%ms$*\n", build_id) != 1) { + *build_id = NULL; + err = -1; + } + fclose(f); + } + +out: + unlink(tmp); + return err; +} diff --git a/tools/testing/selftests/bpf/trace_helpers.h b/tools/testing/selftests/bpf/trace_helpers.h index 53efde0e2998..1a38c808b6c2 100644 --- a/tools/testing/selftests/bpf/trace_helpers.h +++ b/tools/testing/selftests/bpf/trace_helpers.h @@ -23,4 +23,5 @@ void read_trace_pipe(void); ssize_t get_uprobe_offset(const void *addr); ssize_t get_rel_offset(uintptr_t addr); +int read_buildid(const char *path, char **build_id); #endif From patchwork Wed Feb 1 13:57:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13124347 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 35F10C05027 for ; Wed, 1 Feb 2023 13:58:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CAA436B0074; Wed, 1 Feb 2023 08:58:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C5AC56B0078; Wed, 1 Feb 2023 08:58:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B235A6B007B; Wed, 1 Feb 2023 08:58:51 -0500 (EST) 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 A52826B0074 for ; Wed, 1 Feb 2023 08:58:51 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 66C50160DCE for ; Wed, 1 Feb 2023 13:58:51 +0000 (UTC) X-FDA: 80418878862.14.AD27B7A Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf07.hostedemail.com (Postfix) with ESMTP id 888624000F for ; Wed, 1 Feb 2023 13:58:49 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="LqKng0t/"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf07.hostedemail.com: domain of jolsa@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=jolsa@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675259929; 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=+5JURJhGj2dii/qYo4MHbLNFqtPxIU98xUGfnte33fs=; b=JjLjGOy1tTyCq6Yak5XMDMpyol27kVWcXYV3vgCf00powIvEm/EZaXfJha5/9BsjBAhvV/ EB4F50SwDnqiv5N1Y6pPS6oz3Ta3W8HTdg05n0XYG3RMsYbMjxjx5ZZKien4G3TJ5huVAU daVnQ+85xqaQOAvuqbhorMRecSwaSpA= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="LqKng0t/"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf07.hostedemail.com: domain of jolsa@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=jolsa@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675259929; a=rsa-sha256; cv=none; b=Cw1E2ZUX9eyFfjB5ynB0TUl7KIc9ok3giZADzcz+EeXMEgSJext0+tQXvczL18DZFp1Q0s hGXCrHOMJvMUL4B7cSJ8NS2X4K5vMC7BPmv5mS+dLjUE/qIKaW8UZVp888vgGcdPHaqUKq Q+qb7QO/4JYibeb8a2BBCoA2XDU1hRc= 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 27EA3B821A3; Wed, 1 Feb 2023 13:58:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2EF50C433D2; Wed, 1 Feb 2023 13:58:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1675259926; bh=jvbDTrnNM+LreSrCmODrWLU9QmeB7q7iDen40FPdj1A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LqKng0t/3ATi6UOExCOhJde1ApShlMSM63gX+vd3aJNmd6nbNk1OfdjWQmmtcMRVB jmYqD22jesz5ZbfQwbZusBGwAk89B6NyOQ2UyhQ3U0iUM/xWO3NmhyO1P5IPZUnhrJ toFQaOjvVDH3zkmtayUE0G6SDVxIg+Sflc2RcTDwMRFQ4E2JRQbAprzSCRJ7TQRJjO U0W/6fNliUadlcsU/Qq55Q9jn+zYw8zU2VWxd0M8WVTymkAiZCxpdma5L48bpNuRiV F2fMb5KRVubPmVaswcG81sgYbIINbZF+kDbyFhNWwap/Ndckh9rRlMBANWK7tKKaOJ xuEd+eLEGxI+A== From: Jiri Olsa To: Alexei Starovoitov , Andrii Nakryiko , Hao Luo , Andrew Morton , Alexander Viro , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo 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 Subject: [PATCH RFC 5/5] selftests/bpf: Add iter_task_vma_buildid test Date: Wed, 1 Feb 2023 14:57:37 +0100 Message-Id: <20230201135737.800527-6-jolsa@kernel.org> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230201135737.800527-1-jolsa@kernel.org> References: <20230201135737.800527-1-jolsa@kernel.org> MIME-Version: 1.0 X-Rsp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Zn0XjnMB8P3K4eVWK5 fhvNqC9L seh1VrgyCQ86pLULSfy/C1QWk8YnQIDYndW9AEbKKri8YPWwoCd+CZ9D2DcjBrY0AkiYdmvTj+AZf3OLLAoCYFtalbdNO6tnp12/lfNTD3QYpdA8ZQDjqWX6PZWwPunLgVNmBghdavywE+zmmf6O/0pQyYUlfOPmjEQDc+HNFLpEVHziusjI26j1SY18QIiM0C1sILTVD5XkoiVMbOY/9qHXppkIZQ0OcKXS9wOs/xje9sTmsQKqWpBkXmoiIKVfCGjxS1Go4JUFu9YvipB0aMRRGGqoipjmo1GsKoArg2XYYbykvKrAaiXDytvw5xGHdo2jb02flxZ+2n05yzxIQuEhgg6R+WrNWCTuCXzGSUZaG7QWa95oghjyEpcAPkW8b4GnXUzdHonke2bW8AQwo1dRxb6FPFusMcoZJTX5MFJocoQ7CP7B3tr5hG7STTSg16Znk/smz9u7QI3/NXpWretzjIlSjWcL9Qjdz 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: Testing iterator access to build id in vma->vm_file object by storing each binary with buildid into map and checking it against buildid retrieved in user space. Signed-off-by: Jiri Olsa --- .../selftests/bpf/prog_tests/bpf_iter.c | 88 +++++++++++++++++++ .../bpf/progs/bpf_iter_task_vma_buildid.c | 49 +++++++++++ 2 files changed, 137 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_task_vma_buildid.c diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c index 3af6450763e9..fd3217b68c2e 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c @@ -33,6 +33,7 @@ #include "bpf_iter_bpf_link.skel.h" #include "bpf_iter_ksym.skel.h" #include "bpf_iter_sockmap.skel.h" +#include "bpf_iter_task_vma_buildid.skel.h" static int duration; @@ -1536,6 +1537,91 @@ static void test_task_vma_dead_task(void) bpf_iter_task_vma__destroy(skel); } +#define D_PATH_BUF_SIZE 1024 +#define BUILD_ID_SIZE_MAX 20 + +struct build_id { + u32 sz; + char data[BUILD_ID_SIZE_MAX]; +}; + +#define BUILDID_STR_SIZE (BPF_BUILD_ID_SIZE*2 + 1) + +static void test_task_vma_buildid(void) +{ + int err, iter_fd = -1, proc_maps_fd = -1; + struct bpf_iter_task_vma_buildid *skel; + char key[D_PATH_BUF_SIZE], *prev_key; + char bpf_build_id[BUILDID_STR_SIZE]; + int len, files_fd, i, cnt = 0; + struct build_id val; + char *build_id; + char c; + + skel = bpf_iter_task_vma_buildid__open(); + if (!ASSERT_OK_PTR(skel, "bpf_iter_task_vma_buildid__open")) + return; + + err = bpf_iter_task_vma_buildid__load(skel); + if (!ASSERT_OK(err, "bpf_iter_task_vma_buildid__load")) + goto out; + + skel->links.proc_maps = bpf_program__attach_iter( + skel->progs.proc_maps, NULL); + + if (!ASSERT_OK_PTR(skel->links.proc_maps, "bpf_program__attach_iter")) { + skel->links.proc_maps = NULL; + goto out; + } + + iter_fd = bpf_iter_create(bpf_link__fd(skel->links.proc_maps)); + if (!ASSERT_GE(iter_fd, 0, "create_iter")) + goto out; + + /* trigger the iterator, there's no output, just map */ + len = read(iter_fd, &c, 1); + ASSERT_EQ(len, 0, "len_check"); + + files_fd = bpf_map__fd(skel->maps.files); + + prev_key = NULL; + + while (true) { + err = bpf_map_get_next_key(files_fd, prev_key, &key); + if (err) { + if (errno == ENOENT) + err = 0; + break; + } + if (bpf_map_lookup_elem(files_fd, key, &val)) + break; + if (!ASSERT_LE(val.sz, BUILD_ID_SIZE_MAX, "buildid_size")) + break; + + memset(bpf_build_id, 0x0, sizeof(bpf_build_id)); + for (i = 0; i < val.sz; i++) { + sprintf(bpf_build_id + i*2, "%02x", + (unsigned char) val.data[i]); + } + + if (!ASSERT_OK(read_buildid(key, &build_id), "read_buildid")) + break; + + printf("BUILDID %s %s %s\n", bpf_build_id, build_id, key); + ASSERT_OK(strncmp(bpf_build_id, build_id, strlen(bpf_build_id)), "buildid_cmp"); + + free(build_id); + prev_key = key; + cnt++; + } + + printf("checked %d files\n", cnt); +out: + close(proc_maps_fd); + close(iter_fd); + bpf_iter_task_vma_buildid__destroy(skel); +} + void test_bpf_sockmap_map_iter_fd(void) { struct bpf_iter_sockmap *skel; @@ -1659,6 +1745,8 @@ void test_bpf_iter(void) test_task_vma(); if (test__start_subtest("task_vma_dead_task")) test_task_vma_dead_task(); + if (test__start_subtest("task_vma_buildid")) + test_task_vma_buildid(); if (test__start_subtest("task_btf")) test_task_btf(); if (test__start_subtest("tcp4")) diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_task_vma_buildid.c b/tools/testing/selftests/bpf/progs/bpf_iter_task_vma_buildid.c new file mode 100644 index 000000000000..25e2179ae5f4 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/bpf_iter_task_vma_buildid.c @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "bpf_iter.h" +#include +#include + +char _license[] SEC("license") = "GPL"; + +#define VM_EXEC 0x00000004 +#define D_PATH_BUF_SIZE 1024 + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 10000); + __type(key, char[D_PATH_BUF_SIZE]); + __type(value, struct build_id); +} files SEC(".maps"); + +static char tmp_key[D_PATH_BUF_SIZE]; +static struct build_id tmp_data; + +SEC("iter/task_vma") int proc_maps(struct bpf_iter__task_vma *ctx) +{ + struct vm_area_struct *vma = ctx->vma; + struct seq_file *seq = ctx->meta->seq; + struct task_struct *task = ctx->task; + unsigned long file_key; + struct file *file; + + if (task == (void *)0 || vma == (void *)0) + return 0; + + if (!(vma->vm_flags & VM_EXEC)) + return 0; + + file = vma->vm_file; + if (!file) + return 0; + + memset(tmp_key, 0x0, D_PATH_BUF_SIZE); + bpf_d_path(&file->f_path, (char *) &tmp_key, D_PATH_BUF_SIZE); + + if (bpf_map_lookup_elem(&files, &tmp_key)) + return 0; + + memcpy(&tmp_data, file->f_bid, sizeof(*file->f_bid)); + bpf_map_update_elem(&files, &tmp_key, &tmp_data, 0); + return 0; +}