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: 13124340 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4FC48C63797 for ; Wed, 1 Feb 2023 13:58:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231590AbjBAN6e (ORCPT ); Wed, 1 Feb 2023 08:58:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230372AbjBAN60 (ORCPT ); Wed, 1 Feb 2023 08:58:26 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 108DA14EA7; Wed, 1 Feb 2023 05:58:12 -0800 (PST) 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 112FEB821A2; 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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: 13124341 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7BE1FC6379F for ; Wed, 1 Feb 2023 13:58:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231687AbjBAN6h (ORCPT ); Wed, 1 Feb 2023 08:58:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231285AbjBAN6c (ORCPT ); Wed, 1 Feb 2023 08:58:32 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14A8C769E; Wed, 1 Feb 2023 05:58:15 -0800 (PST) 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 0E60E617B0; 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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: 13124361 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F35AEC636D3 for ; Wed, 1 Feb 2023 13:59:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232172AbjBAN67 (ORCPT ); Wed, 1 Feb 2023 08:58:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232158AbjBAN6s (ORCPT ); Wed, 1 Feb 2023 08:58:48 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4752D1353A; Wed, 1 Feb 2023 05:58:29 -0800 (PST) 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 33167B821A9; 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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: 13124362 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0639BC636D3 for ; Wed, 1 Feb 2023 13:59:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232323AbjBAN7O (ORCPT ); Wed, 1 Feb 2023 08:59:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231904AbjBAN64 (ORCPT ); Wed, 1 Feb 2023 08:58:56 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EFCB9743; Wed, 1 Feb 2023 05:58:35 -0800 (PST) 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 0BB38617BA; 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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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: 13124363 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E399BC636D3 for ; Wed, 1 Feb 2023 13:59:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232441AbjBAN7a (ORCPT ); Wed, 1 Feb 2023 08:59:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232206AbjBAN7G (ORCPT ); Wed, 1 Feb 2023 08:59:06 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ADA63677AF; Wed, 1 Feb 2023 05:58:48 -0800 (PST) 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 687BB6179C; Wed, 1 Feb 2023 13:58:47 +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 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org 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; +}