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+oC9rdN917wbXeYkeGYLCIBJzqn3jOxZNvknndzViwrjHYRQ70pZEBA8qx4QATp035HK2p7oTAKXu2LxVRs65Gk6icvzKHkruvAFSd0ocK8ApOwP1mXpa/Ar74j4441cas88iRBPB8NMxU+cyhQkiz9pt777EytX7DHE12OiQpqwQcGiEcaSOnkmyzBUEevk/xb5YR27F9e4tikYyxBbsDz1JXZgvIY15LWYttrE/SJgDTtl2HwN/Z3H0s898oIO554M4LSoqYlQIGIkZBTxhFxvrQlEnM6XOSJ47GOFxQEbb+AVswn6IfHMTcYhHh4RxBfZ6/Z9q/WHi0QPMd78RqjpB+N/GevhnyK2ZevlrWtMTCC+dNKzgHYx6FnKBtLQap14YS54YmTVuuuDWA81yXD7JnHkhrnz5XQiIS6uU4MhuvafDmcJrtNHfmFO7WV7c0e5VEBrCW41+6sA+JC0Eb/u1D4KA3nfN6NJ/P4EN7juxyRYAn1e0lPVVmjWBYK7wXifRQH0eg8LCNqu1I5sSkDjZLixjVbbzpz6J9yHp5lM7eXET53XubUBAk3dt7kZPIZB2IeCQEhr9WE/aPizGRCJnQuhNIRP4EUkuEvt3FmepD1GYXFZ69p2aRTqiyQvuPU2w+ft9Lf2HHGEGTp4+rV1JclTOpG1k2XhdmA2+NANVEzPJOat6E1M9HuVS0ovbgFYr+DE9Z0iwpP88rnKmdyJ8ecx0UPhoRPQhbTnlUcmy8v4k2nQHKPIHDXk/JvTtnSFHM/E9jCUUPzbe3wDNk4Bg2IDgC1UJ1sK1bCKrwVjeevzv/BrvATKDd5itNtwh6jdIa/mgv8LznCdUgcgkx7UgMqnIECe2pUGzTtJgRmqHu+06dl/+M3JJ/1mvjVgs1+i9mshsTiGpkF jbObJ8lV +aZQYCRl5icChKV5cn2yakwPvzJvOv3DawRKNyTRNdH5BJ/IKWhJM0kJrIfldSt9qpHneZPfNhd++DCjqAx6qeSBqkuPtoWgOgtR8KIV2hXpX+3wbwPpOJcuqElyjOiDO4JoOItq+Vegpk0tf/McW1jIo32ipwD5bXoSLXi+dTTcBsw0VmjbM1qMXdKTeuF8DwUjfXe5BUuNomRaq3i7AGTqTOzCHxnUwJZPKoJvaVZNZjrysFUGDPlNN4dTZgImzE6ymT7vm+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); From patchwork Thu Mar 16 17:01:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13178089 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 86D7AC6FD19 for ; Thu, 16 Mar 2023 17:02:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0DC31900005; Thu, 16 Mar 2023 13:02:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 08CBD900002; Thu, 16 Mar 2023 13:02:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EBDCF900005; Thu, 16 Mar 2023 13:02:25 -0400 (EDT) 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 DC504900002 for ; Thu, 16 Mar 2023 13:02:25 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id AD9C7161453 for ; Thu, 16 Mar 2023 17:02:25 +0000 (UTC) X-FDA: 80575379850.20.0525F32 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf22.hostedemail.com (Postfix) with ESMTP id AB3ECC003A for ; Thu, 16 Mar 2023 17:02:23 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="SNv/7OPw"; spf=pass (imf22.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=1678986143; a=rsa-sha256; cv=none; b=Ow1DyLjuKdwgtWhGprFU6Rfagzpgn3WmCuetdFCoLWGJ73b+dVUi72oI5V8RA3RIoQ2fI/ +XZ9k6Ktl2JXG4uLT+pEq6DpunxxNeXhMdCBZQ1bRvu+NcMpJdk6flYqGrXgvGoyBncAbs +Rj9/Q6IcNl1mUSHoxMbWw/RJGuwFBU= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="SNv/7OPw"; spf=pass (imf22.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=1678986143; 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=Ug1467U48VKjl+qfahaRx91Nx0fu0cxgPdvPI/ITKF8=; b=iKKDLZBeiYXqkXu14lbNNkf2/lncMCE4armHeTkoCrBu0E2feONSfVI0NMBxtTmyb4d2+x 1PJAR7Tp9TAid5O8dNpf30O+XjzVaqqUySmCNISZ5CJKw3j+VkAszVjqe1vKTey6Iig3o0 xSi5gX9NPULyf5z3MiFeGyKG8hAlcTQ= 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 D8910620B9; Thu, 16 Mar 2023 17:02:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0A54DC433EF; Thu, 16 Mar 2023 17:02:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678986142; bh=wPkey4BWxoSwuVm0/JDcphsHh4pWF/uWOFmXxlWLkHg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SNv/7OPwD0I0s7lhX5kbFYwqOHQ6BEeVp42wO+mX9PwyPyOWc9aJP8tpSf8pTSmc7 aJUi7ueFyPgrnzibcjii0GIA0zBfQ+oVxp0KrZ6owWC8EKweBWjgHUvuX4bvUsGb7K jRQt6UIxSQ6hmnmM4MFepJHDyU9d2by956YrhOlpGLKvW0kO976LhWUg9lFcytBy2I cOPXJzHAAG6XfhpiPaipKG4qKVWkQ9gVWf3V6V9dmCwMAjG+X0ldhsn8+xIPvtS/NU bs9dvCYweuoj4hon4v1BsbRdkf1cx44ZWcTyojzhvrOzQoSNM8qF+wi+IS6ht9pVT0 jD7VHR0ypnIDw== 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 2/9] perf: Use file object build id in perf_event_mmap_event Date: Thu, 16 Mar 2023 18:01:42 +0100 Message-Id: <20230316170149.4106586-3-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-Rspam-User: X-Rspamd-Queue-Id: AB3ECC003A X-Rspamd-Server: rspam01 X-Stat-Signature: ca6hnu6b9wnh75m6p9k36phc3t9hadpa X-HE-Tag: 1678986143-584375 X-HE-Meta: U2FsdGVkX1+/FbSWlU16mepBy2CptbAZo+ZNeFnvgfxIvuVrr7CPyWfw5fWGkTPe8LXw+mcr/cET5j8yangz6+wRUHgKZHDfeA2PkTFRmGfzes4l0hQXCH9oX2k8dR02vxd3SJj64kVTy1aM7QykqYhcMRhJ2o5ZeBpJVLzHiEDzDDTQ3pztep2oqFeK/jVjmTU2xx1oqLDswUMqvR1RycLaVOQi7q+MhPx57qzADAE01A8TWCd6L2wYSqGdP88ipdPHl2rFZfXAFiUSzXRFksZAThjvRvlNyNCfq/n1Ax55GFSq4Wt/phMiyP3hGojm91b/jywnCFtI7dEAUhzNf1oF2msGXSJDzAq92/ktFiOyZgTWZm14xZmSQvk61qHOAiBFpUv+s9C5jeHmO4ET+bWMpjWpvnOfnTG5iJ450TAsqT9DFIbIq8HoQ9VwZRapOPGTXi06t3yhJjQU8cirqaVM+vdgrGUSRz/OkLrBuaeips2R7wSI0I/QzXN+PlrIMX1Op+Da1TnkL/cm2VnQXNs06//Qmu2L+gLQ2MWgXDmmPzpzvDMCQyEr3SroibWhRgzIltoOnYO6MomAjoNykY0QpdI/wPJZry8FrDSTHWjcr0fg0W8O6QHaa5sjuxKzGEMgNA+XhM91gnRVq8LcYhbhSJLMeajj7QgirKdCQwzC4pZNU2XIkhiP1ERnz2AV4NVWy0725MxhOtPmpCLpmrBE3cW3jWjUoG3/Q6yEN/5uhFN4xydgCbDrO1Sy4k8xQiA/a0oW7c7rngEcOOlDqthF49ZhUjT1kQirkAc3v+Ub5uRfAwAPJYL6C5LVyruJqeuy53fQWEVWW6gpE4UNvHg524vzlTthMs8ptrRp4c/n1KdCyvxnmSPuVcFI85GJPkPm75Ki1jdUnRD4cBNY5vWfCRrFhHwZhVPuh6F6RaaS0cfz3vlGrPvt2+wAXlFDvKhFA0BfjHTdV8dK24x SPmI82HB UaJul5WwGM2n8IqMpB1ScqzsH9hsMqe/lFTCuVwYkbsn9OU3gWWAV/SUMJeolHcyXVEzX8l0AUhP57pvHGzRelSwVuh8Ivs6X66m7QntJDvhdPWz6mJXnj9CRGNK2FH+yRilgJUzrR/qmieAc5UrXv8W5ayN8iXD/Yp+YS+L3u4+o9HwM0uCLDCsCBucr0DSpx5Fu8w3gOq/sQaT59OazF5/wxdueXQXrIR9PdmMnKHmxMlxTYPDCU/fUaNQ1/sGmwo6cxv0KOIslhTYMfzUku8gVlXZ5lQwjxGR0cffSVmNsApoKreoArdkPBiSocf3H58xHhvdmz27xu4Xt8MT8FyTN2FSag7an7lJG072Dx2i35/DqTLvjWkkt/Nm14fvsOByha/sZnCiZrKsVb+SeRSWLe/20cG6aXzO4v1R/9vAOjUksf/llu7eQvbWXZ7y7IZoTEyC4nQbJTKS33sGhralVUEOoFEBMdyPW 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. The file's f_build_id is available (for CONFIG_FILE_BUILD_ID option) when the file is mmap-ed and before the perf's callback is executed, so we can use it, instead of reading it again. 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 f79fd8b87f75..3a5398dda6f6 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -8527,6 +8527,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_build_id; +#endif struct { struct perf_event_header header; @@ -8539,6 +8542,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_build_id = vma->vm_file ? vma->vm_file->f_build_id : NULL; +} + +static bool has_build_id(struct perf_mmap_event *mmap_event) +{ + return !IS_ERR_OR_NULL(mmap_event->f_build_id); +} + +#define build_id_data mmap_event->f_build_id->data +#define build_id_size mmap_event->f_build_id->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) { @@ -8583,7 +8618,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; @@ -8592,10 +8627,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); @@ -8727,7 +8762,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 Thu Mar 16 17:01:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13178090 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 08F5DC6FD1F for ; Thu, 16 Mar 2023 17:02:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5BEE8940007; Thu, 16 Mar 2023 13:02:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 56E7E900002; Thu, 16 Mar 2023 13:02:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 43638940007; Thu, 16 Mar 2023 13:02:39 -0400 (EDT) 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 30D39900002 for ; Thu, 16 Mar 2023 13:02:39 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DA2251A14AD for ; Thu, 16 Mar 2023 17:02:38 +0000 (UTC) X-FDA: 80575380396.26.5B7CBFE Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf16.hostedemail.com (Postfix) with ESMTP id 91DF2180006 for ; Thu, 16 Mar 2023 17:02:36 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ZhtVj376; spf=pass (imf16.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=1678986156; 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=PYLTagl4kcd8Qk0UixYM5D7mbmPPquB72eQ1gp+sMic=; b=3cisClnsHxnx0RNFLGS3SDU61keMT5cnH6w+m/Xx91bUDA79dB3KD+DSFvZayKP726cpx5 PAxqvMr4LA/52WRHdAgnH8ZSDZuQXpYNQMepllubmFstfXugBEPdCDKdTmYphxtus0kayq aw2Pjovg49BgAvxK+1pPpSCg6J99oHo= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ZhtVj376; spf=pass (imf16.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=1678986156; a=rsa-sha256; cv=none; b=S64gN9CdZzNYXtVwAHOL15LE26fVDxjJeLpkYng/er38p0iN+EIOditZnDhdw0WdtLUpro /ocfA3Z50HCLMv2D9LpYJPFVfZ+e492/yuAFrdziQwEFKf2CEaI5F5x5msPxEXt8tThnTm QkjhmAC6lsFn3BSrqna5qsa2QOUWi1E= 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 99000620AC; Thu, 16 Mar 2023 17:02:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 04ED5C433D2; Thu, 16 Mar 2023 17:02:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678986155; bh=GA8tXS4Mul2elWYsjsHoBsAbirrUGLhRxbF2Iayc8rg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZhtVj376qlPRWZ1voHqasur/pICqPh2D1iv3o8cqWfD6Tqwvn3vzB/NHNzr5q2zvc Du2DgVvnZu67X8HekIq3/BB471wLGP+xMh44yTpxdsA6MJ4i52t9Npw6fiUxKq79AZ BVAKBWemUcdBpEcTGD1w7L96mTMWWbvInC9HuwInR990nvhl2XzxnJL/fE2VLgeg3z M1me3mgk3ZFsAMmRYmy1tCPnFvQsZ8E0lS7EnHJoajef6uFHZoErIYZA4oTQ5C+GUn /zyUjx1ODsI4xjPqeOEyO5tNqStX53VCPmBS3mjJtL3+81scbzKF/Kf3q+8KkHUC9A CKaZl7dtXKDTg== 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 3/9] bpf: Use file object build id in stackmap Date: Thu, 16 Mar 2023 18:01:43 +0100 Message-Id: <20230316170149.4106586-4-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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 91DF2180006 X-Stat-Signature: 6ro7ijb7ajm7xbgrodothqyb9g65mdop X-HE-Tag: 1678986156-745281 X-HE-Meta: U2FsdGVkX18DcH3jRbig9brEstRVDmwc9e592uPryAtZpvtObQGrkYuPDB2VELazogvW1UK8BCvt6dfmT9HwGu2ECNSczjtepfuZ5PVCOpou8QDtR8vxTELtKYX5sY22mt8CLULPcFM3qerrq8jfOggkOM0T+EkdF0MnyaEhiUA9zaqau1EQxV6qZljXppRC5E+ao4vNqz99WF30MGgFz26ypOZqwKdqFS9EX6VkCaHk+1HpMkD26CfQKzGQkIGdminxXuCIeqXFF45TcX0gY8t57TVqEssmZLDMC3ebriLaqCcYfdDvqz+hlwNTiBeLyDb2WZJvIhUtEQ0j8t4pqQ78AgLRa/9Yo1x5aqcsMCIXz51XPCTWXVLy3UZkA1gIlIzJvMOnVf1EkgYBO4fNZcKi/0TubeOphXxP4hsB7o7gpXhxGcoIYlEZQ4xhKIhjUQNBjOqi4UCgtIyOjTP0prcYzvBMU9wq1xVjsrmoYTTsgbAUDnovZI2DYXkWn0bNbChsaXr9oY6mZBCNbZ/6XYfqXe6bYIPq6LUpI/qV9rL02733oAVmUQ6zmgGVH4kA1Jxq8BQbDYgAlL86yzqaEH1EeYewdgmE7Oe20PUin8XcvOkqwNNNoBgYlRpHot0Ds7IhCMLqWJN7zl/MnTqRGixkXe1Yy81XzQLjXcp8zGQLcilEod8DRb9k0w2iohbjaaaZYoDuUW5CPWwMBAaHSOKaOi/Fi1Ac4Bh/RQaj2LuEEDpK7NBFXNDXyMXVv+/1NTJl0Pd9yzDWId/nXEqbsZ3NRflkcL/CAeuq8a0csG9sSeMCDpG8+qgLEOUACD4k6wwApxHTwXRtuFxuFwm0oFMCBuaD/aIcbuPABKOeeLGruSlqyiriufSFEkFm6f1oLdBtqCTjkhOhXhUIorBHpQgPxxy8W+ZgVG43wzJgTcQTE63M5J+8soBFMmwipcYNuIBkaUhGWDQAXiwpU6u EO1/Utqa 4L5McglfyanuwPIuiNn77l+xrKDSpOICZgoUJHoGD05RGINj6pJ6jPlEW4+JAvWcUXJajgsYa6ts6b9BJshR0fSbmdzSE01c9n8WHWGgUkzVvxcWaxytS3t4eoQxWJUoMaQYXCjPg/NwEeUuGGOED70yBFRV20ojU3uHa0yhqpKcrxmbDIcLNikzav8Rv61vjIcOWCFcp2MJd+6ws5uyfE2R1rnJ45X7YlGplqmfTBPHS14yPnSfV47ElHTKPe6eOOEzJR/DgqcnlT6CX3iYUN5XcoHowJd2hgOFxOsUuj9UKNWhzs+q17n2GCBR40x6Sel206VAcEcnVyM+l5Y3bcAzABxOSQeZ2QskG5r65cIzDrEsr5NBfm8eME2Dn2pWH8nxQ7o5RPLEUdjbn8n2vQuImeaajf8KlsrQYYiUopFH7Zh4= 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. The file's f_build_id is available (for CONFIG_FILE_BUILD_ID option) when the file is mmap-ed, so it will be available (if present) when used by stackmap. Signed-off-by: Jiri Olsa Acked-by: Andrii Nakryiko --- kernel/bpf/stackmap.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c index 0f1d8dced933..14d27bd83081 100644 --- a/kernel/bpf/stackmap.c +++ b/kernel/bpf/stackmap.c @@ -124,6 +124,28 @@ static struct bpf_map *stack_map_alloc(union bpf_attr *attr) return ERR_PTR(err); } +#ifdef CONFIG_FILE_BUILD_ID +static int vma_get_build_id(struct vm_area_struct *vma, unsigned char *build_id) +{ + struct build_id *bid; + + if (!vma->vm_file) + return -EINVAL; + bid = vma->vm_file->f_build_id; + if (IS_ERR_OR_NULL(bid)) + return bid ? PTR_ERR(bid) : -ENOENT; + if (bid->sz > BUILD_ID_SIZE_MAX) + return -EINVAL; + memcpy(build_id, bid->data, bid->sz); + return 0; +} +#else +static int vma_get_build_id(struct vm_area_struct *vma, unsigned char *build_id) +{ + return build_id_parse(vma, build_id, NULL); +} +#endif + static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs, u64 *ips, u32 trace_nr, bool user) { @@ -156,7 +178,7 @@ 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]); - if (!vma || build_id_parse(vma, id_offs[i].build_id, NULL)) { + if (!vma || vma_get_build_id(vma, id_offs[i].build_id)) { /* per entry fall back to ips */ id_offs[i].status = BPF_STACK_BUILD_ID_IP; id_offs[i].ip = ips[i]; From patchwork Thu Mar 16 17:01:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13178091 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 0BF90C7618B for ; Thu, 16 Mar 2023 17:02:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A51D6900005; Thu, 16 Mar 2023 13:02:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A29C9900002; Thu, 16 Mar 2023 13:02:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8CC5E900005; Thu, 16 Mar 2023 13:02:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 78D38900002 for ; Thu, 16 Mar 2023 13:02:51 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 3455741356 for ; Thu, 16 Mar 2023 17:02:51 +0000 (UTC) X-FDA: 80575380942.13.CFAA53F Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf08.hostedemail.com (Postfix) with ESMTP id 6D46316003D for ; Thu, 16 Mar 2023 17:02:49 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=iqjfspND; spf=pass (imf08.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=1678986169; 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=YxgdoJl9JFdXIAplITWigBJ0j4Dle7CAGo1O6E3/s08=; b=GVsUQRvAkdK2uEWiPPCSdxihQ3IJrX0oCJL7+NATcsO4qHjcKiPfTP4yng5XzrtigyekqY FFg9Aw73mvkjHQQSx2ihbU9QdnF81vSXSFH7qKJJUB6jOMItVAcOI//mwPQ2oXL4ERUvlW rb7bFFUrGXZKy2RLfqXwRcW4aUOG0AM= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=iqjfspND; spf=pass (imf08.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=1678986169; a=rsa-sha256; cv=none; b=M5pPdaCkphGs8kEgqxqwuZVvOvY/855uKqIlO04knDVp2eWxx8ND5ZD8zAQhGNNXvXgZCG mdY1AG6b7fuohq//tCdf2wGaE1eLMLed5zYB08jF45dZtJTW/JwyDPAG6Yvjz8bFOBHBkL 7yyL7eqHZDS1XNThz26GVUshyrDF88A= 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 8EAB8620AC; Thu, 16 Mar 2023 17:02:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B142AC4339E; Thu, 16 Mar 2023 17:02:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678986168; bh=2rdENgg7fzhoKYPinTYmZkVwba8MoExnAAxutCEQK3M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iqjfspNDqGz3klTZPT2+BpffWd9f4W6SLqgKqRFii0Qw4uBg0XdMAi+nls+GHm3dP t3EdFLYjAWxF/k0NhZwvPKEpRPiSt/sBefMLuZVCBpQQ/phZzHSKTTxgVN5tQN7nS0 +bF0J6oeBdA9BMVGZF72vgksl6td2ylD9b340n7jBfC2iB/AUqxhi1sYyMQb/MS7mB 6FTBsJwAG11kA6/pox2tkaZx1afavULyHXdBh9QmzYTCvrqBJjZtouKIfhTzrvlQ5I 4GsgMcdAxFBgxi3hL3OZ5O9QstrOLNsRV4iCd7w5//E6F0GNwJ7LHtIWx5ndQiJ9iA T+NEcJP1lLfdg== 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 4/9] bpf: Switch BUILD_ID_SIZE_MAX to enum Date: Thu, 16 Mar 2023 18:01:44 +0100 Message-Id: <20230316170149.4106586-5-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-Server: rspam05 X-Rspamd-Queue-Id: 6D46316003D X-Stat-Signature: xnmq77udkqqnh1pngox1eicdpo8xypfr X-Rspam-User: X-HE-Tag: 1678986169-50396 X-HE-Meta: U2FsdGVkX19lGIIAy+UnF8AqqVe+SRQnPfcWLqGQ5PRyaoz/Gu2pl3cXc+u0ZiZ76h2AxFhNSxhE7BBipGk4BlIE/ZljEMCvnoCKHUg6S9LcpdS30NGwT/z7/rGovTwA+Xlh83rNM3mj+qyGHSSdpyBaqB8wsJHDg+R7fl+xbt2Q3Qic9WLF3HAI4gzBx6cFWg70tlatZeVraWttCfabu8lKhuPsbSP/TjU3IsJxOqvb/jmydwcSKfqToz/NDFhg2rIML4fIV6xsZHG1NG00mZyB9izgjvndymEuVAOS+OBEVSTLDkf9zfO+bEsAgUbMvFDEaOkUmhniObLE7c2RMs+4jYE1bAHpNW7kc6VmMCAgHkINlDoypEE5FCh+r1tNCnnu62/pmAvFyaaL+n2AtAdy4DC+PsXefSKzsK6fXJhoffK5Ps6f4jMkz/GCsP9UgmD/rBsdP105hE9jW+eJ3npX/DiHvE9ITZXmEOXy5oD+5WDDCO1JEu9vsHMY9eGbY3NmJk/TXMwGYJU8s5VSCdO+wAPp7gQKvblmzaAWqed91zS3laQH4PqattZX1K8FwdZZtsHt0uy2e6ZVWgp4up5DpbNuWEmLiOBwz/P5U8MKc2G9rpoP2pa9RX4AYvpPIYjQSXbPvXLudJqtmX1pSpVwiEWllapH4dW57jNaFgc07u8jIuSuMxmxllsJz7fyI9gw4fEXEWg7RguPWxp+n7qHIPyuC0S786ItmVu2sVKRnwtNFJflpxAO6B3ZSKct3C3yCNrIbBnbYLaZjc5pA+6A0hmH5A8Az8ANy8ox68ke8P829BRKSSu1J1tUPVK8A4pUlx+hBr3KBbuXiE/aBjbvKllaJ7oL+1iI8h7gkCYcX9Ry1O+s8qhPgOHuvuEKIX/Tsc9T/XBZucSuCkCmApHitCQYVNVJqunOBot71PtaIuZDYI5LDK/a/r4iJftxY/bsC34Zy28MJppAMjb XjFIUzoZ zi/CLSs5wpzmHwCFgA1JKPeo+b6+w0/Wc6MAgRRz/lAQLNyjQ+hU0KMcKaQZhEuqwpLL3y1oezNE0DLERJ44uCYu9WRofeLaRSwzoZ+51OR7UFYyUVSMbPAdBXeH8x8H/JL342CPpmjgspQK5YJVQP6aIb/f9C7wMApfwkZxUzSG+GeOhlF8Vvs2Woz5/fxCBYnYq50jw8NdfyJBEN/myIo6HfJYQRoVVyOu8cb45FPL5ylb7Gc47BOuqMX5Xb6kU3LT5FmHHEe9mZkWLhj6WVdeUxJ0jm+izYdseJnksvlRWa3k50Xt4Owv5CU7R0PuhrdxP8xD4zJtr5Ga47vtwYkAwUoLkzciXxw9nHyN6EweQXiaBHBVVny418RqM7SD0wfp0Rzn9wUbow7B/cgGKAYEnhBdfoeddmJMTuqMarINnFefIKxl2LV1ZSGAztKz2jWeUt1hd/damvL9/r2rPLNMSyzPcon7jBds4 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: Switching BUILD_ID_SIZE_MAX to enum, so we expose it to BPF programs through vmlinux.h. Suggested-by: Andrii Nakryiko Signed-off-by: Jiri Olsa Acked-by: Andrii Nakryiko --- include/linux/buildid.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/linux/buildid.h b/include/linux/buildid.h index b8b2e00420d6..316971c634fe 100644 --- a/include/linux/buildid.h +++ b/include/linux/buildid.h @@ -5,7 +5,9 @@ #include #include -#define BUILD_ID_SIZE_MAX 20 +enum { + BUILD_ID_SIZE_MAX = 20 +}; struct build_id { u32 sz; From patchwork Thu Mar 16 17:01:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13178092 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 79D96C6FD19 for ; Thu, 16 Mar 2023 17:03:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1C5EF940008; Thu, 16 Mar 2023 13:03:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1765E900002; Thu, 16 Mar 2023 13:03:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0658A940008; Thu, 16 Mar 2023 13:03:06 -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 EC238900002 for ; Thu, 16 Mar 2023 13:03:05 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C319A1414CE for ; Thu, 16 Mar 2023 17:03:05 +0000 (UTC) X-FDA: 80575381530.17.F4C76EE Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf03.hostedemail.com (Postfix) with ESMTP id 6F2B520018 for ; Thu, 16 Mar 2023 17:03:03 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GGxc9yi2; spf=pass (imf03.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=1678986183; a=rsa-sha256; cv=none; b=NFOgMiJBrq61NA6tSqaS0vPrs3NBX6Qj3iQJ7nvQkw5REBxTsGGMrO0MHdHCPXpO39rMv0 S4jCIYEP3yYkaceFwkmrKA5gl5IF5ZpOjhiFYkUe46oM87mt+W7yzMca2wC7dXJzrCzjOo k2g+Yhz6LKdIJW6zrV/CgmyXOutlf/E= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GGxc9yi2; spf=pass (imf03.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=1678986183; 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=6TycWHtNki/tCukF6ZcZrfhfnkxjrpC482gZ0vZgG4c=; b=OCokiBB9S1/LTR1WPqm8EDR2ZRZHzVPXL5GXrmvEeSGjzIdiXsNssvHz0luCvLI0RmzMv7 yRbdIIjZ8aOK2JwFuhG03sHf2aTYAXXF/rgQmVKyRXCpBIgN8Tpbgnb9MuDdb8xBlXdzvQ xaBwo0ZxhPRivluaY7PFyYFGzGUXbgM= 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 1EE7CB820AD; Thu, 16 Mar 2023 17:03:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 84B9BC433EF; Thu, 16 Mar 2023 17:02:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678986180; bh=Rj6UdNWuOo8DkLviQii3I1+Tk2Kvc/67ARrHkQm/dfw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GGxc9yi28eFc9bY1ajwWe10NyIStMwt4YOzdhNM/wiX7yOjelaepeo5UXwJfAytWI lDik7EeGavF+ShDZ3r/gpO2e1n4/59BQSSW8iv6Z7JNAXcztJowDqIeD/qF/a5nuNf QtVkOWTQ+AKS6CyZBXVFQ7EsGqj70MeAYMnQIKquZwZBIZPDhvAblZJFyFBmKbUnJP rH/8mHFzO8LPJnWLCse6i6Z32V7YnpbZh4+vysz014gw+q2mzA7e0t0ec7lFSilRnj shtyTlk6ESVFdmTJGfIG5cKbon9GjIAevOi6p1Ee6Sq9SEUJ6lgRL/cKjUQpcFHlNO jrdFLxifXhH/A== 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 5/9] selftests/bpf: Add read_buildid function Date: Thu, 16 Mar 2023 18:01:45 +0100 Message-Id: <20230316170149.4106586-6-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-Rspam-User: X-Rspamd-Queue-Id: 6F2B520018 X-Rspamd-Server: rspam01 X-Stat-Signature: 9ka7oq8ho4srzdddhcnizr3qbwgkzmeo X-HE-Tag: 1678986183-653790 X-HE-Meta: U2FsdGVkX19lXCJK/M2cqtVliwYpl/K43ETtLuvKdRMwU76QX/uWusFWH234m/aUVbxr9XLdIbYEdgudxJqTjH5aBIVNOr11f5/5rAxD5PP7K2jDi644XFn2zfpwYS41iH3W75GtTe1bF6wdEM26RMEDD27Vg8INuykjPPsU0VBF7QpsHEipMfKeF/CuzMpPxytr+UVmef9h2+nf1+6fb1cnkXfGW/4X/0QJRNh4KOMv7l3BJA1kgAultoaw0lS7unVxv/7UB1210k1RXUVvrm+j+4MdBv6ppqADSo9GPBj5n959T0gqhDsnH1SDLOrE8YPKls0CW8FI054MMi9ELr/5u1w3eUqZe/8ofzFHk4HeE5pXN8jdchfjA+OkvYf6dB57gMAVIGYonb62pOma57mb4xdhAiPf//7CsR92q5ZuslHQSMxo+pW0gvanrWHppOs4x2vzl5NXfgiZWdbN8aDLGPfvlO3bvJ75HQDHr52HTJy/HucVMs973NYXUT5hVQTxEd/5/9L2AvBWdffVg6DmaOEzOeSDx5Nzy1AgEajj+bvldr6tWtVu8DCo5Eu2+tzmSBoRkQNRG7OpCYe3ksorofj5skt7DqFyI82LBztB6AVVyJqlpC8J2NoaxvaUJBrcnCuToEn2dQr+xCQDCsGFYR4GKBNXJVlujKN61p4YvEn7Uf+85D3ix0dDd+5xYtM4ezdy93a5QcJ+lvEcVtYr71lGMdBgb8jWgc4CTONmIJgxpVw0CrL/aI6TXGOUTXuk0RNLV/65/bUw+hdPsj644UdBNRX1cUdrKFJwEtDM+x4xYis/4zuvq3J0Ov1N95PjEvrAIHVzUU2S6svOMPA3AZIt32oCIGbyt4bHvRVOtPbzdf9qLpAYrQ7MiUPx+dAORu2stlYc9B9w3iPiBV0lVKA6Oi6TQ95sTpPUiYnqY6lDNQSHEoiUVHiGCukqFOB8kC9+zVItDfF/arv 0h55T+sf NSWqdIUzG0lV6MlRk2I8yTHpM2+RrAet4fIH9rQYrl/i4Zr2zVkAvHj1MEbjWUbhMkezAsRzw7iyxPE3xUk6PXnSVgqN3qMXJ/ZiD75DuHWqPbSpbVDEhT9J3MCPUZJgTC1einDPQ1Ca9I4Adh1Xe5CJadOL0Uk5fxdB6VnYfi18JZQCveGzIMGhwfojujAyMI7YOxb3Q4AIZ2jWwpxbG19PlR6IBbWUnSCrb9oNEeLeE/cm44TCXAoYh/cUWJxLmvW7//YNnmF1f9BEMnWFAE5ngX4QZghGRrj4gyz++eT8xf4iMiKNaC4331glINHcYJI2t1C6EB4fsOy4CCpPIMSA/aZc8qyy4a6huhst0+2XFtGqVi+VckCh+96YUwGBjP9oQ+sPu7ebe+50uHSq83EwqumFAgFxd0RI6pFrhcYIz8goEG1Jk6ARvjSSZYwuLukdf1T683LwJcfk5ZzCa7tvELLYRuxgJsF/5 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: Adding read_build_id function that parses out build id from specified binary. It will replace extract_build_id and also be used in following changes. Signed-off-by: Jiri Olsa --- tools/testing/selftests/bpf/trace_helpers.c | 86 +++++++++++++++++++++ tools/testing/selftests/bpf/trace_helpers.h | 5 ++ 2 files changed, 91 insertions(+) diff --git a/tools/testing/selftests/bpf/trace_helpers.c b/tools/testing/selftests/bpf/trace_helpers.c index 934bf28fc888..72b38a41f574 100644 --- a/tools/testing/selftests/bpf/trace_helpers.c +++ b/tools/testing/selftests/bpf/trace_helpers.c @@ -11,6 +11,9 @@ #include #include #include "trace_helpers.h" +#include +#include +#include #define TRACEFS_PIPE "/sys/kernel/tracing/trace_pipe" #define DEBUGFS_PIPE "/sys/kernel/debug/tracing/trace_pipe" @@ -234,3 +237,86 @@ ssize_t get_rel_offset(uintptr_t addr) fclose(f); return -EINVAL; } + +static int +parse_build_id_buf(const void *note_start, Elf32_Word note_size, + char *build_id) +{ + Elf32_Word note_offs = 0, new_offs; + + while (note_offs + sizeof(Elf32_Nhdr) < note_size) { + Elf32_Nhdr *nhdr = (Elf32_Nhdr *)(note_start + note_offs); + + if (nhdr->n_type == 3 && nhdr->n_namesz == sizeof("GNU") && + !strcmp((char *)(nhdr + 1), "GNU") && nhdr->n_descsz > 0 && + nhdr->n_descsz <= BPF_BUILD_ID_SIZE) { + memcpy(build_id, note_start + note_offs + + ALIGN(sizeof("GNU"), 4) + sizeof(Elf32_Nhdr), nhdr->n_descsz); + memset(build_id + nhdr->n_descsz, 0, BPF_BUILD_ID_SIZE - nhdr->n_descsz); + return (int) nhdr->n_descsz; + } + + new_offs = note_offs + sizeof(Elf32_Nhdr) + + ALIGN(nhdr->n_namesz, 4) + ALIGN(nhdr->n_descsz, 4); + if (new_offs >= note_size) + break; + note_offs = new_offs; + } + + return -EINVAL; +} + +/* Reads binary from *path* file and returns it in the *build_id* + * which is expected to be at least BPF_BUILD_ID_SIZE bytes. + * Returns size of build id on success. On error the error value + * is returned. + */ +int read_build_id(const char *path, char *build_id) +{ + int fd, err = -EINVAL; + Elf *elf = NULL; + GElf_Ehdr ehdr; + size_t max, i; + + fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd < 0) + return -errno; + + (void)elf_version(EV_CURRENT); + + elf = elf_begin(fd, ELF_C_READ, NULL); + if (!elf) + goto out; + if (elf_kind(elf) != ELF_K_ELF) + goto out; + if (gelf_getehdr(elf, &ehdr) == NULL) + goto out; + if (ehdr.e_ident[EI_CLASS] != ELFCLASS64) + goto out; + + for (i = 0; i < ehdr.e_phnum; i++) { + GElf_Phdr mem, *phdr; + char *data; + + phdr = gelf_getphdr(elf, i, &mem); + if (!phdr) + goto out; + if (phdr->p_type != PT_NOTE) + continue; + data = elf_rawfile(elf, &max); + if (!data) + goto out; + if (phdr->p_offset >= max || (phdr->p_offset + phdr->p_memsz >= max)) + goto out; + err = parse_build_id_buf(data + phdr->p_offset, phdr->p_memsz, build_id); + if (err > 0) + goto out; + err = -EINVAL; + } + +out: + if (elf) + elf_end(elf); + close(fd); + return err; +} diff --git a/tools/testing/selftests/bpf/trace_helpers.h b/tools/testing/selftests/bpf/trace_helpers.h index 53efde0e2998..bc3b92057033 100644 --- a/tools/testing/selftests/bpf/trace_helpers.h +++ b/tools/testing/selftests/bpf/trace_helpers.h @@ -4,6 +4,9 @@ #include +#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask)) +#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1) + struct ksym { long addr; char *name; @@ -23,4 +26,6 @@ void read_trace_pipe(void); ssize_t get_uprobe_offset(const void *addr); ssize_t get_rel_offset(uintptr_t addr); +int read_build_id(const char *path, char *build_id); + #endif From patchwork Thu Mar 16 17:01:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13178093 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 E4CE4C6FD1F for ; Thu, 16 Mar 2023 17:03:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E00B900005; Thu, 16 Mar 2023 13:03:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 790FD900002; Thu, 16 Mar 2023 13:03:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 658BF900005; Thu, 16 Mar 2023 13:03:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 570E1900002 for ; Thu, 16 Mar 2023 13:03:19 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 02A3CA140B for ; Thu, 16 Mar 2023 17:03:18 +0000 (UTC) X-FDA: 80575382118.24.DBB2E2C Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf28.hostedemail.com (Postfix) with ESMTP id 98CFCC001C for ; Thu, 16 Mar 2023 17:03:16 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="u/EISLQO"; spf=pass (imf28.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=1678986196; 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=AZlHEINR9onG3cKIk69fPVKDvd0fZzvOxjv7iemp418=; b=EKyqeUWStG9k1yviNpADPZyOGny5q3ta1uQnQRrMm6ZOqyUwfw/GzCuhvFzjDHbcaiGThq Ux/SYrNUiz2vYMl1keXv/lfx5IVSwIeNY+uKWFG0XNPZf3t2NAo0sJJVBYSN2xKMtfcrN7 7NaJCwhsVRhzw+LV8A4/TqVRLvIgrYo= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="u/EISLQO"; spf=pass (imf28.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=1678986196; a=rsa-sha256; cv=none; b=TD0DC2lEkf4hme/AmtTZ/OCzVI2xoAfmp9hE8ALgFHhrYWFJI+QQs2SUg2TMsR4Ma0sTBQ eqx3q6pAi5TNHWdKd54rqoAcbTT9I+ntOzh8Km2HC4y4Xwy2L6CRM5L4p6ZKh0qZNTw0GH hNwmNp/ifZtEN7vZOh9Qq4EM+k0DphA= 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 40A24B8228C; Thu, 16 Mar 2023 17:03:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AE8B1C433EF; Thu, 16 Mar 2023 17:03:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678986194; bh=XHhvrIWZe8yN3WAyFVo4Xi7J+OqocuCK7k5vskhhChE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=u/EISLQO2803reejxf8JO9MgqJ4NQbmCh7G+bn4fdRV+pQjJWe7KhKZRVVRMnw/ro ky1GblNRwpv7o43sqGdjR+vtMiXrfHmEkRy8GDHTp5zyIIcdCaW4MxkTblBR6l0SbR TAOctZXlkrbvfAr32VS7QCdN1vTKcd2FZpyaKwV6Mip5pPdN0fuTmWVv1nPd5jY5FD bZBxvJTG9trTD+BxrlJdfuicdsC4pgcFCE22xVsACPERaSCMmO+3odXdTcRCiUZbhM JiYpsGUKNn82I0dJFtheJV6qlT2BNNhuk7D4WYnHHcRnSXICVtX3RbdWfjsCO+7e5l iBrppzqNKKC5Q== 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 6/9] selftests/bpf: Add err.h header Date: Thu, 16 Mar 2023 18:01:46 +0100 Message-Id: <20230316170149.4106586-7-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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 98CFCC001C X-Stat-Signature: wg3tzm7duh9rechswpjb4nzrz4twgmw3 X-HE-Tag: 1678986196-807189 X-HE-Meta: U2FsdGVkX198qEi3AEapGBmmB0DwTH3MOsG5ayLHGnU0KWWai4UhhFMa2x/zjRk6ldVrR4F+TwiOGOLpqvQ5wh54gxqJr7zG3/uSTX1V78+mGy0iRtpfJtW83l4QlhoAVqD7dayazfQB5yysyLNvpa0Rrep1reefe2i3Eg/iMoTpnwj4hpVWa9VpYiy3fsTiQcVAKdbewbEzf4PIXvAtvwzP973Lb+IJ+9GyCBk3uyE5UzVRsSzs9pGrMpgkf/6g6fAI0GL6dseu3+s2DFHpHc9JhwrY6Mcj/AdxWbAXWTlyvMYdZQNQfkJ1XK6RHIlO31ZwdinVT3C2bz7cidSImL3GQUeTEUJ5L4bu05CcszDPHk9FTJaTXbcPY2h8Whvzkblkp5vcH7qnIOjhomJa7Sf89vci029w1IIbCU7VzFMfs8f37NkiV49zBM/1Ovez6U5o98l3/ytJZsPvpr/3ZdffFFeOMwPFEl3rxVOvDo0TJCp+MHSWiF24Y9ZjqYGcEUqIn5u8ZuBkKu9Ux3SLV2EveZsOKworhnFKbWo05q3Cy9T/n6eEl/Jhov+opxXkMILS977ScO3+XFWNc/YbaPyaOq1QrZBLEmwOwzpuWx2noAkhNipdnWQ8zeYd/OlfHu+fBc4Hbs8+a0crm6VAYDf7Yd14CX3h/7L5ObnGTSRgrSUeDl3Z+bL70+yLjwUQUYF8++O5v39UOSvbbVRM5tTpPwWYQzcewfvKu1tYs87H7wPdGijxUbPFRuAaBCoEjynsKeBBd7xQIuM3xzonAYIcXRV5QIKG0feAQQhDf6wA20RacJJNZttbbmjpiFDZ09nU95taW+HcFTYeJPJKCql/f7HgIhj8D3TFLkEerzwmQgvL6jgS4zyA8iC7dvn3OsWNDoZiJ1nRvTqdpaL+EemxrYqrwHTVAtHh+QYhu8cAHfDiakLKWGE3xay0MYb62thaSpB0mdw9vKU7v85 JwZbZN3K eEb3qC7iRXvvjF/J1rDJydDSupHsUJJe45UcnYdKoRkFeb2PDzS4h7/QFiQyAzkfFktIhwtQbx6h9Vyq1X6W+gv4jQ0HFFfPRRz/k5Doo0lZmpb5vA0bCUvykFgrs61Sri9alE1gqov4scRqVVe8gf8ll4u/ump9MvIGiI2nsAS2uBNbCfNWuRKDbvnVb4uwhk9LtcnjQok7Xy1LjIpWg1gkfZDCjaqQpLNHxcvTKblefAXsA+f2KLldUgGZY+Z09Ex3Wn9D3ShgIW7zi2UkbPrQ1ZNdHyCdrtCGKpEbdBii6vLrCExzK8O0CnX8lhZBIJpyiGvJaXRq2nNIr41r18HJ71bT/mfZ1i15nl7a6KmdTmJhNrtPp9d/aXN3U9lCPpf+FcIi7xkDXNwhBN+3NsrJ+TLrRlh9q0x6oKFxgBuDpPEA= 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: Moving error macros from profiler.inc.h to new err.h header. It will be used in following changes. Also adding PTR_ERR macro that will be used in following changes. Signed-off-by: Jiri Olsa Acked-by: Andrii Nakryiko --- tools/testing/selftests/bpf/progs/err.h | 18 ++++++++++++++++++ .../testing/selftests/bpf/progs/profiler.inc.h | 3 +-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/err.h diff --git a/tools/testing/selftests/bpf/progs/err.h b/tools/testing/selftests/bpf/progs/err.h new file mode 100644 index 000000000000..d66d283d9e59 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/err.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ERR_H__ +#define __ERR_H__ + +#define MAX_ERRNO 4095 +#define IS_ERR_VALUE(x) (unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO + +static inline int IS_ERR_OR_NULL(const void *ptr) +{ + return !ptr || IS_ERR_VALUE((unsigned long)ptr); +} + +static inline long PTR_ERR(const void *ptr) +{ + return (long) ptr; +} + +#endif /* __ERR_H__ */ diff --git a/tools/testing/selftests/bpf/progs/profiler.inc.h b/tools/testing/selftests/bpf/progs/profiler.inc.h index 875513866032..f799d87e8700 100644 --- a/tools/testing/selftests/bpf/progs/profiler.inc.h +++ b/tools/testing/selftests/bpf/progs/profiler.inc.h @@ -6,6 +6,7 @@ #include #include "profiler.h" +#include "err.h" #ifndef NULL #define NULL 0 @@ -16,7 +17,6 @@ #define O_DIRECTORY 00200000 #define __O_TMPFILE 020000000 #define O_TMPFILE (__O_TMPFILE | O_DIRECTORY) -#define MAX_ERRNO 4095 #define S_IFMT 00170000 #define S_IFSOCK 0140000 #define S_IFLNK 0120000 @@ -34,7 +34,6 @@ #define S_ISBLK(m) (((m)&S_IFMT) == S_IFBLK) #define S_ISFIFO(m) (((m)&S_IFMT) == S_IFIFO) #define S_ISSOCK(m) (((m)&S_IFMT) == S_IFSOCK) -#define IS_ERR_VALUE(x) (unsigned long)(void*)(x) >= (unsigned long)-MAX_ERRNO #define KILL_DATA_ARRAY_SIZE 8 From patchwork Thu Mar 16 17:01:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13178094 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 92F67C6FD1F for ; Thu, 16 Mar 2023 17:03:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 34D95940009; Thu, 16 Mar 2023 13:03:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2FCE0900002; Thu, 16 Mar 2023 13:03:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 19DB3940009; Thu, 16 Mar 2023 13:03:30 -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 0B5DA900002 for ; Thu, 16 Mar 2023 13:03:30 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id CEB96814F2 for ; Thu, 16 Mar 2023 17:03:29 +0000 (UTC) X-FDA: 80575382538.05.328418B Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf13.hostedemail.com (Postfix) with ESMTP id 1D64B2002A for ; Thu, 16 Mar 2023 17:03:27 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="KH/z1799"; spf=pass (imf13.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=1678986208; 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=9dbnu83u12dK7sC9NEE8cHDegLU21TSauZjZJ1IfXRc=; b=dtiZjGcmxFyWb8rwH8t7utfMlJ+g6tWIGcqXPsfsgml84eqaHiTFECjNeceuUe3QvsHlRy mF76vtY1fsC1SuY2Hh1831LLo0l3cJayDEOV/HceAB7PuU8qVWMb1CTxAiusBFHBwR06Vk pp52xfrAyi2fB022jdfjEj+6RktvkNo= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="KH/z1799"; spf=pass (imf13.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=1678986208; a=rsa-sha256; cv=none; b=OMPE8H05ZxYjFM7ah+umwC2FZWs9+h6dm1QKVCEfzSy7FvG5VrgPiewVxQD4BtkfvHlSXQ FglAtOYjh0gy8XCcXOwga8zen6XCZwHg4cdvAu2tmksTxKxHK+UWVCr7pqZqhFKIBfLaqf +WnQqE155VX/OWHQSYG1LvtHo2Eg7PA= 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 2DE97620B6; Thu, 16 Mar 2023 17:03:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 50A82C433D2; Thu, 16 Mar 2023 17:03:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678986206; bh=A6fzow+FLPSwbgWXlTZg23xT6LzjnL6mQF7mmQVS3Xk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KH/z1799tTYwpYi8mTwVuCoTeHCh5/KjKQiypM8ZlLUwhLLndHXLXZTrhTZSGRGiR 5gkkhFo+Jswe/IwHPf+B8R37UOejXaEE6vNYqwIIsDa+YrF+K9u8OA8Q2KWD05Y9oT mrUW5aVPslQW7Eas+AvOkBSf1q3i/0w+x64Z5DYLJsPXA76U4THoo9iZ61OiSfGP3T 9w5fWkO9ITikzpsEEQtbJ0Xi4yPCl5Y0u+93laTsNNjEk9EMU6iB9KUY+jX0EWB6G+ HTMLhKIjzd5BlOcemptFxmi3ozDZFUV1hyU9MgMxn3PiOC+aLgPNreRvTrBg9lKUuY pDAnnPluMdlxA== 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 7/9] selftests/bpf: Replace extract_build_id with read_build_id Date: Thu, 16 Mar 2023 18:01:47 +0100 Message-Id: <20230316170149.4106586-8-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-Server: rspam05 X-Rspamd-Queue-Id: 1D64B2002A X-Stat-Signature: rbx1z36135fgmruyd7n1gsa98pptx8m7 X-Rspam-User: X-HE-Tag: 1678986207-185814 X-HE-Meta: U2FsdGVkX18DYKDkzGFvtSiDVUMLOTiexmz2jctCRgUGfZMvreDv03lJCaJ6MrTF8yHfRCY9fiBqQne7B11SV+yEV/cRQDTf6K+XDRlBeXYVdgZDIDTB8zLcyLvDsGWi6pONHbZ+J+J2newFeiMVaHSzDnziLJkTb00YGlSViDNYzqsejioH49/S11cDji5cPB7SDdqvwoBxsQJPo3bDl5/MstCbmSQ7ETrTOVzm3qPA8rKN1upY+rBcqHfOYTsa8lP2YCzOux2N25Tf95GhYxVKY8X6DBFv3bUaKipx+tPn+AM/LyF6/j6l16tFy99jcJUhIrUdKrYCT9w2T7gnUCBOfqznGUawfmG45c4JavC7CCxh8i8Yi5iU5GHspGuncyv0X6+enNTGuQU9MWmjMzjx23nZu/QyZ90JiWrD2dUyjjVGi2nsMLTCEdyp8E2UjxbJs+FyVE1hVQxhCEIl1EiE5hfEy0k3fvzX4KwXe5z95o7Do+LYKrx6rx0xW7MjIw7AAMe1MwZs8WjfN7GBV9RBSqzzKkLWzmfM4i4/H3T2yLX+Vw7AeXyBIlBJXCmA1NvXW1MaDyjHDPDwPdbGHpaNB9hkUauvduwMcrRVWhQD86iLa+FnZPFUlXe1QRBwdXeZ2dlwpOv6uezGVvAUK1b4rg6gEwgBkr9Bs0qsxfBmMIsY8hBE4VKPI9sWGaUOvE/TiZOfVJLTgD5IN679hlGc9Y+hQy9IFRJzGoHdk1m1kssVk9xkpHURHGeqSCdGp3oLp2enqZiYpbYzLBIyf/af5BdjrRTx8G665mfOLOjevrBTBD46yezwExsX+PgLD29IHbo/CVat7nTESDQ4lUNqKACmPqWYN6Ad2TdkPDJ63khh93h3oLAdD1v0+4rGkCTPJtjuWLdvWDYk8r8AUGJIp+cxXDjMHerFitcx9HXsXJ/+0RVqlroK/b0+jyvSwYFj0zEirSX4TnI9grg kIuyut82 wxbTWmj4R5+5xFgEKXiO3hgY3J4E5qJI/DRFTnxe8+f+aGYSgbxVCwCk1BZMzAork7Mg+yyjj+HdpMIW1hgmUKvrPI0P8+lWUITl+HVlIb7uGod6TQosb4bzO15U101ozBJJMQsQTQ5+j5kNdtBigo2Iw/f5nLJN1KgsCxljruhFjriG604cxwUVNYkYqbw6hu6OK6zWapCMFFY8IX43nhaYxSn7GrutErSUd8nauu0TI2F2ajaXUXlYKXWuDLxWHhhWElQRmUOMmaOSAYAO2z0oodCuZQ+grWvMQfJnBYf3K3nyQaB+EGKJK9dhzwS31UbkOvBvnm8gk8TPiA8KgpdVCPjV0nZy98a78yV7qn1OLpGlX/7/bytk+MHURiGLuam4QIKlBkllnOnm2sxq7xi1TrT4wtcWyWvXpWGQs6L/ne/HVBM4UOwLzMEA7K9jICD+wrvZUR9S0N/tU9NDlyZmLUuj67xAx/5VV 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: Replacing extract_build_id with read_build_id that parses out build id directly from elf without using readelf tool. Acked-by: Andrii Nakryiko Signed-off-by: Jiri Olsa --- .../bpf/prog_tests/stacktrace_build_id.c | 19 ++++++-------- .../bpf/prog_tests/stacktrace_build_id_nmi.c | 17 +++++-------- tools/testing/selftests/bpf/test_progs.c | 25 ------------------- tools/testing/selftests/bpf/test_progs.h | 1 - 4 files changed, 13 insertions(+), 49 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/stacktrace_build_id.c b/tools/testing/selftests/bpf/prog_tests/stacktrace_build_id.c index 9ad09a6c538a..a2e75a976f04 100644 --- a/tools/testing/selftests/bpf/prog_tests/stacktrace_build_id.c +++ b/tools/testing/selftests/bpf/prog_tests/stacktrace_build_id.c @@ -7,13 +7,12 @@ void test_stacktrace_build_id(void) int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd; struct test_stacktrace_build_id *skel; - int err, stack_trace_len; + int err, stack_trace_len, build_id_size; __u32 key, prev_key, val, duration = 0; - char buf[256]; - int i, j; + char buf[BPF_BUILD_ID_SIZE]; struct bpf_stack_build_id id_offs[PERF_MAX_STACK_DEPTH]; int build_id_matches = 0; - int retry = 1; + int i, retry = 1; retry: skel = test_stacktrace_build_id__open_and_load(); @@ -52,9 +51,10 @@ void test_stacktrace_build_id(void) "err %d errno %d\n", err, errno)) goto cleanup; - err = extract_build_id(buf, 256); + build_id_size = read_build_id("urandom_read", buf); + err = build_id_size < 0 ? build_id_size : 0; - if (CHECK(err, "get build_id with readelf", + if (CHECK(err, "read_build_id", "err %d errno %d\n", err, errno)) goto cleanup; @@ -64,8 +64,6 @@ void test_stacktrace_build_id(void) goto cleanup; do { - char build_id[64]; - err = bpf_map_lookup_elem(stackmap_fd, &key, id_offs); if (CHECK(err, "lookup_elem from stackmap", "err %d, errno %d\n", err, errno)) @@ -73,10 +71,7 @@ void test_stacktrace_build_id(void) for (i = 0; i < PERF_MAX_STACK_DEPTH; ++i) if (id_offs[i].status == BPF_STACK_BUILD_ID_VALID && id_offs[i].offset != 0) { - for (j = 0; j < 20; ++j) - sprintf(build_id + 2 * j, "%02x", - id_offs[i].build_id[j] & 0xff); - if (strstr(buf, build_id) != NULL) + if (memcmp(buf, id_offs[i].build_id, build_id_size) == 0) build_id_matches = 1; } prev_key = key; diff --git a/tools/testing/selftests/bpf/prog_tests/stacktrace_build_id_nmi.c b/tools/testing/selftests/bpf/prog_tests/stacktrace_build_id_nmi.c index f4ea1a215ce4..4a1c5a692730 100644 --- a/tools/testing/selftests/bpf/prog_tests/stacktrace_build_id_nmi.c +++ b/tools/testing/selftests/bpf/prog_tests/stacktrace_build_id_nmi.c @@ -28,11 +28,10 @@ void test_stacktrace_build_id_nmi(void) .config = PERF_COUNT_HW_CPU_CYCLES, }; __u32 key, prev_key, val, duration = 0; - char buf[256]; - int i, j; + char buf[BPF_BUILD_ID_SIZE]; struct bpf_stack_build_id id_offs[PERF_MAX_STACK_DEPTH]; - int build_id_matches = 0; - int retry = 1; + int build_id_matches = 0, build_id_size; + int i, retry = 1; attr.sample_freq = read_perf_max_sample_freq(); @@ -94,7 +93,8 @@ void test_stacktrace_build_id_nmi(void) "err %d errno %d\n", err, errno)) goto cleanup; - err = extract_build_id(buf, 256); + build_id_size = read_build_id("urandom_read", buf); + err = build_id_size < 0 ? build_id_size : 0; if (CHECK(err, "get build_id with readelf", "err %d errno %d\n", err, errno)) @@ -106,8 +106,6 @@ void test_stacktrace_build_id_nmi(void) goto cleanup; do { - char build_id[64]; - err = bpf_map__lookup_elem(skel->maps.stackmap, &key, sizeof(key), id_offs, sizeof(id_offs), 0); if (CHECK(err, "lookup_elem from stackmap", @@ -116,10 +114,7 @@ void test_stacktrace_build_id_nmi(void) for (i = 0; i < PERF_MAX_STACK_DEPTH; ++i) if (id_offs[i].status == BPF_STACK_BUILD_ID_VALID && id_offs[i].offset != 0) { - for (j = 0; j < 20; ++j) - sprintf(build_id + 2 * j, "%02x", - id_offs[i].build_id[j] & 0xff); - if (strstr(buf, build_id) != NULL) + if (memcmp(buf, id_offs[i].build_id, build_id_size) == 0) build_id_matches = 1; } prev_key = key; diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c index 6d5e3022c75f..9813d53c4878 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -591,31 +591,6 @@ int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len) return err; } -int extract_build_id(char *build_id, size_t size) -{ - FILE *fp; - char *line = NULL; - size_t len = 0; - - fp = popen("readelf -n ./urandom_read | grep 'Build ID'", "r"); - if (fp == NULL) - return -1; - - if (getline(&line, &len, fp) == -1) - goto err; - pclose(fp); - - if (len > size) - len = size; - memcpy(build_id, line, len); - build_id[len] = '\0'; - free(line); - return 0; -err: - pclose(fp); - return -1; -} - static int finit_module(int fd, const char *param_values, int flags) { return syscall(__NR_finit_module, fd, param_values, flags); diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h index 3cbf005747ed..d91427bfe0d7 100644 --- a/tools/testing/selftests/bpf/test_progs.h +++ b/tools/testing/selftests/bpf/test_progs.h @@ -404,7 +404,6 @@ static inline void *u64_to_ptr(__u64 ptr) int bpf_find_map(const char *test, struct bpf_object *obj, const char *name); int compare_map_keys(int map1_fd, int map2_fd); int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len); -int extract_build_id(char *build_id, size_t size); int kern_sync_rcu(void); int trigger_module_test_read(int read_sz); int trigger_module_test_write(int write_sz); From patchwork Thu Mar 16 17:01:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13178109 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 1D2E6C6FD19 for ; Thu, 16 Mar 2023 17:03:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B3B35900002; Thu, 16 Mar 2023 13:03:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AEBAF940007; Thu, 16 Mar 2023 13:03:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B449900006; Thu, 16 Mar 2023 13:03:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 8D72D900002 for ; Thu, 16 Mar 2023 13:03:44 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3FF6AAAD90 for ; Thu, 16 Mar 2023 17:03:44 +0000 (UTC) X-FDA: 80575383168.05.C3A2810 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf12.hostedemail.com (Postfix) with ESMTP id C8FC340011 for ; Thu, 16 Mar 2023 17:03:40 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="d8LC/I53"; spf=pass (imf12.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=1678986220; 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=ImjniubCi3R1Daykm4O/gAlqq07NgWvNGFraGc+3Gy4=; b=bhta0so6Ym70tLrF1kFDxg52MEkzbcKyU8xA48w0k4gMGnsdlNSdeJx5fcj5SIp/HCtP7S +rNTZMy8+mYcQbfNLd6CE8lCsnoGnPfgM4DC2/0yvAYtDNE6x3NJMS49WoiE0pnUBZpZwQ N5P8EoO+vgam8i/1hOFfgCEbIG71iRc= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="d8LC/I53"; spf=pass (imf12.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=1678986220; a=rsa-sha256; cv=none; b=8fZZlUEk0Vdhqib2444rAT3IRe1T4YqnayQ0iqPsdHMiTzzeKnaOfSQF2zkHRGpWCe4JMh lcomQAOBSpZf0fkoDWVWJb4eR2JE2FiQGDbCZ5XUanMpKcvMSw+v/R2S0mx3w+i7Tr4cHS jonMZZ1wucKzcYkJqTbT1twuWCBOM2o= 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 171DA620B9; Thu, 16 Mar 2023 17:03:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 333CFC433EF; Thu, 16 Mar 2023 17:03:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678986219; bh=BTNFjFR6qedMLNKodJS0I96kmDjan/r2p24kIgmK3/s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d8LC/I53yNMvhKiJ3Oq2YxbSToYguXOf9YL7kJ0By7G0BvjLuZlszeTUgYFPhKnB6 KllaisUOamvTTtPjic6nqgztIrsZwVGjjWK7vG8ozFTcCgp2HkZJ6X4LbSKvyuVkjT qcXbW0Ckyvbgso20/1phg1107C1gTiClR3Hj+lRcjG0M0Ki2RtZMhnjx3Mmkobv12K Xx2SF6Fu+XDxUx7rHArRgUS80ARKH6R5eLB3AJkRH4Tn9g3ZGexQwoFvU3DWzsHOsF 9xxpJNXwqQRN1HimKUC14dXXfdR2JKVwCqre7Kw9d/jzAuYtlvOVeUrVxp6AMWSvzO Dkxo8jLgLZv+w== 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 8/9] selftests/bpf: Add iter_task_vma_buildid test Date: Thu, 16 Mar 2023 18:01:48 +0100 Message-Id: <20230316170149.4106586-9-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-Stat-Signature: bruwbgomq1nn4ibp1mjrw4cefmpsy4p7 X-Rspam-User: X-Rspamd-Queue-Id: C8FC340011 X-Rspamd-Server: rspam06 X-HE-Tag: 1678986220-230540 X-HE-Meta: U2FsdGVkX1/sG7K0V935PQjLn+hI8JfBnRkfUMQMJWrwSelJboAy8/AJj5AlSSirTCZkZxKXIWkUq3IKuvaLcCfCRFQOYOeSXboNRznNYO6+HS/pi4cjwFtJJ9WRMGEpZHJijaoWe95Isr9bEpDafnP0KB9qgBOKbDInbOqLRlgjrgazScH9sBifCXDJ6JgxoVaG5Gf5ovzHSJ7Zxrr3L4dRIW+qR4LmTENs+Y2Mt90l+wuc/cQveYnxiPxyAA6brrUJ7ikf0RXOzjp5rkDCEsAJDcnRMXH7afQGPo4DUfSidwIY7+mAPftDRdaOrtwkppXb0xthKiYlCOX+zyJ2StPxiUofxuEv8CRnHb+nQ3ahbJUybvLC1hwqi9JTeIPGIcTZ1wLqOXwFsQa1DjW7nBS0TIwx4r1iyzJgH+2zZuAF1+Xpuqgl90RLiiyoNjjkWELr4oVLBrFxLM6eREW8zra+O9M3uT1Qpv7WEFCwwFdrC5LlYtgBNyGk8v4YKBySt81INJeUukUhrYntwQI5r2WwiBPD5vjE4mKYGfTosddyFHgFMtCYtVDpIYULR/rFfziZrt9frHRuD7FzlDn4vFO1+jZ3naazd11+7ZrtUZio1v7vyIG0YcFaLjYwigmin2dswc5ts9cnpXOm30rq5e26UR4tsgdeObtbj+9ppWvhpN1D+fxMjkpw9qLd1EuUGHWQHiuuFXrhSSTSv6Riilt3kWkaEKz0pvECDov6aMIXxfRHKTBGhv9FpvvlloOmJ2DUWTCXBFLgE7QhM0W7wqrakhHtTZkmcuzDdWAIWH5aGIgG6wrIQ7Rq/y5j1bcOM5PeSpkLFTURwRTrj2Cm0JidyOR7gUnQl43X9S07aM/R4dDIBvJNj5j6P07nnyrTAiJmvhSpP2ZWh1h9HtBmf9eodgEvBHQ9C5VJY75tnuJtWRo/qCGJOECk8C4noPjUF+RfrbLF/bx8pxmWIqN UoH5bfbW cp79rEfODTsl/5LVc82RZ/sf5Qe8C9EjyZzxSZdsGkXXP9OZgQkojY9XB/7ZRfcDyR3l8KFt2pbgqYxYCDrE92sdMmYur5Sh7UpDJFisH5ZvU/dNTyFNYyPRrg1MO+nQIV3YD7rLfYCTjMVNTqkBbe9MUlFS5vL2SJ0CcpEyB2tddoRnkDpGt/wN0l3NdWwWtELOXrLRY6tR8s0Y8cLDkZ1wvlmGaHg/y15Q/Mw6C6L4pqAastP5bEWwceZr3qXdVxcHn35Kd9BvPPCwoeSfUw7UDsuc4BltF+CUDJ6E+RVTcE3V7KlPJrqrpoG8unCgP+xdggvQoL9JoqJb6oFQxMj27A5CDMmP6dGHdiNZrnxf+Naf9cRWbhe5L25VQSkkiCoV9a7XAzng1KcrHJub/5MG7y4RavMbRiUNOTos/dlH2qqHRfajN9ORzPSW+m468Z9hXCrQnEDqg2WevCF6KJjZaLLsmFkQ5qgFK 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 build id into map and checking it against build id retrieved in user space. Signed-off-by: Jiri Olsa --- .../selftests/bpf/prog_tests/bpf_iter.c | 78 +++++++++++++++++++ .../bpf/progs/bpf_iter_task_vma_buildid.c | 56 +++++++++++++ 2 files changed, 134 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 1f02168103dd..c7dd89e7cad0 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,81 @@ static void test_task_vma_dead_task(void) bpf_iter_task_vma__destroy(skel); } +#define D_PATH_BUF_SIZE 1024 + +struct build_id { + u32 sz; + char data[BPF_BUILD_ID_SIZE]; +}; + +static void test_task_vma_buildid(void) +{ + int err, iter_fd = -1, proc_maps_fd = -1, sz; + struct bpf_iter_task_vma_buildid *skel; + char key[D_PATH_BUF_SIZE], *prev_key; + char build_id[BPF_BUILD_ID_SIZE]; + int len, files_fd, cnt = 0; + struct build_id val; + char c; + + skel = bpf_iter_task_vma_buildid__open_and_load(); + if (!ASSERT_OK_PTR(skel, "bpf_iter_task_vma_buildid__open_and_load")) + return; + + 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, BPF_BUILD_ID_SIZE, "buildid_size")) + break; + + sz = read_build_id(key, build_id); + /* If there's an error, the build id is not present or malformed, kernel + * should see the same result and bpf program pushed zero build id. + */ + if (sz < 0) { + memset(build_id, 0x0, BPF_BUILD_ID_SIZE); + sz = BPF_BUILD_ID_SIZE; + } + ASSERT_EQ(val.sz, sz, "build_id_size"); + ASSERT_MEMEQ(val.data, build_id, sz, "build_id_data"); + + 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 +1735,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..11a59c0f1aba --- /dev/null +++ b/tools/testing/selftests/bpf/progs/bpf_iter_task_vma_buildid.c @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "bpf_iter.h" +#include "err.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 path[D_PATH_BUF_SIZE]; +static struct build_id build_id; + +SEC("iter/task_vma") +int proc_maps(struct bpf_iter__task_vma *ctx) +{ + struct vm_area_struct *vma = ctx->vma; + struct task_struct *task = ctx->task; + 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; + + __builtin_memset(path, 0x0, D_PATH_BUF_SIZE); + bpf_d_path(&file->f_path, (char *) &path, D_PATH_BUF_SIZE); + + if (bpf_map_lookup_elem(&files, &path)) + return 0; + + if (IS_ERR_OR_NULL(file->f_build_id)) { + /* On error return empty build id. */ + __builtin_memset(&build_id.data, 0x0, sizeof(build_id.data)); + build_id.sz = BUILD_ID_SIZE_MAX; + } else { + __builtin_memcpy(&build_id, file->f_build_id, sizeof(*file->f_build_id)); + } + + bpf_map_update_elem(&files, &path, &build_id, 0); + return 0; +} From patchwork Thu Mar 16 17:01:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 13178110 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 49DABC6FD19 for ; Thu, 16 Mar 2023 17:03:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E21A3940009; Thu, 16 Mar 2023 13:03:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DD153940007; Thu, 16 Mar 2023 13:03:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C72B0940009; Thu, 16 Mar 2023 13:03:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id B5B3E940007 for ; Thu, 16 Mar 2023 13:03:55 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 85C191603B5 for ; Thu, 16 Mar 2023 17:03:55 +0000 (UTC) X-FDA: 80575383630.16.8D48B48 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf23.hostedemail.com (Postfix) with ESMTP id 9EC55140021 for ; Thu, 16 Mar 2023 17:03:53 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=HxOQsfSS; 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=1678986233; 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=zbaB52Y90tuZ54C20TGsxDtUUnYkbabvspgIfj3IPiM=; b=EKlxVRAAiOwNkaQVs4++jut4gItyHmOIxhknDeb2dDjPzXfHqAPCCjP4zz6eHxgYGUH1Lc z/fHMD4NeHeqEYFoKUzMXL055d1Yc2jrDgJ9xkk0yOFrFVBsoBSjTBMpx10HSwDE6yG0CK fbcFYyG0VNzJEt2647ng8uGXIh7UiOQ= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=HxOQsfSS; 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=1678986233; a=rsa-sha256; cv=none; b=596hntft6LNFqHp2Tno3+L/SCyM1MpyoAQPrrpJehlrwfiVon5bzRg0QAflVld8R2iaJ8D ZTnj+cnevSpHjxRpV5m4Zw1/TBjFJlLk5oQtYwN6nPVyHteF4+O3585atH537iduFDhHsl 7lhW0BCrQ+BWPv/6B0MQp8BruhWYD0c= 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 C5EEE620B6; Thu, 16 Mar 2023 17:03:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 53045C433EF; Thu, 16 Mar 2023 17:03:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1678986232; bh=k3FKXivEZQL3axXlXOQ80+3Y8PlgcvRRWyE+8fiPDqw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HxOQsfSSGaoCqeogPae5AJUpOEd+YvVBDDuK+Eus7fUz8mbGY4O1QvXKR4yOLvQAA nn40HHqQ2kXbrnbDntkEm7qk9ztbPrj2IfSmKS+dfEEI+Uu8M/6aeuUXFhMI9axTfP HDkKdq5eKsi2Ix/bdr8XPpEslpQhCx7Qc5wgm0vwZYWd/45Z90H3PImim3RdiUWp1n 5AUn7e7JXRtiwiY9hQqUpXrbHw+IgKDUBiY/nzwm5WhYqV3qjk9ekZS9C5Zo5hARmK RJnlCcshgQi3iZksY69nnMeEW9c1TC7Plz9AHRDpGFuWX1cgHLvO4Hr0DTwXMfFjRk Tj4UwPziJK8aA== 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 9/9] selftests/bpf: Add file_build_id test Date: Thu, 16 Mar 2023 18:01:49 +0100 Message-Id: <20230316170149.4106586-10-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: 9EC55140021 X-Stat-Signature: ha6gtsx41m4y8mzoeamkrdismz4i635w X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1678986233-517336 X-HE-Meta: U2FsdGVkX18hEFNFJ+qvLcsaEjUZR29QoGQ0k/KDbzn6fHFN4Vhs4fNn3SnvNkdO8PKo7yIHC0Q2lNmhaigpY4Nxz4shOHgrM2zuIk2BGfx/2+tAdUc9+RkD4U/SJfGNpoTF2cNiw4gMFWo7F08AMaL0U6ydPedQvnNkpAwLBMM+3ZM8/Necbg0HrPVR644t9Qpf2F5oBbhaN0cGDvPqUNFhF6pY4Zx6Zsrk85Sfaq3JgTSEgYWbvcBHb0JD5P7UdnRe0Hh43gZjQDvJT48v3ft7HJthgMx5ktSKiDIMWoATsfRLMbdaJ9tPGfzWkc8F4qzfE4oVeVU7etvJm8hE6Xq1My8yjohskBoOYi66fT2FfwbLqFjYko26zJn6VsF/lmwM0VF10kHKKu6G0HUnftWZ1rkagshS74e6r7v4i5bCdeCUzSIhMdMi34jekbTkfP6VzV/5Gw5hnPnhPLIyYx9GwcA2Fju4NiVw1JZGvpyIaEPYZOJFkrbNY+kyssz569bP8D+QaTK7jieqTxKdzGvnhpPCH4yXdtXsBzme/32SK+5QoZiCdn7D1kHJjr8qSE4a98FHrPmxG7RoyQIq/eu+OxJ/6HsI+YIqSS7bX8r+0DKmrxvz7a7fAeCnnnTPh/vrlQFDVkFge6mPjSze6fajLyF316TmKTWNnInfmrzo255H/umBJxKngrs8OqGwe+qelni7tBFK3UquGoUv2Ue/rvwxRuuat1Mf/UDZeBRV8bju6W2Ldr/n1Gf/bV19wyJyjAY4Tja72Rgutzze8GZKG/I5DIJXqJt/lzIMfGF8uhGSow5HXKcxV+RHPNndoT8GgJCk89uYjJvKPsbd30FZLkFThmbJRFXDv234HRe1onLClQ3oRVvOuA5y9sdmWUbb6wCJ2+WUekghoDfwzrUuLdBq7N5sOYKx2rkyUriOx9FXOiTPnw9ZQ8O1fsCKlcIu+RrwPdfL4EHBrBa /9Ds4mLJ 5QIMq/U/f2CUrGnVgml4J/IwyZLuQVQOCVXsM+HJ1Ze3WNe6DWVoB+krgDKqkKUT+w4TflGgHEdmddcz9Hvk5sZfK/ACLihXM2RgulAEmZXIygEZ1GRRteYt2UtkPegOpkkKtfLG+xgkBtM4GUKNB5cKm0l4IAekEImEZEGI8R84b9g5Mn6WrKdh48gsdlySvw6KxGpGMbk8FXqWs6CjNwaWYHzd3hGn0LtqYwWOVf6qnGfr2dm+HaVN2hDIVP/aa32CgjduvtFoSuKryVIWZGLCZyh0yrVQkOC2CaGoUoQE0UGhc+ZcnsMt6cTASZrWTggXfJwal2KcN0bAzMOqIfz11YfO2v7EByG+q6MvF/XT4NSxKgWs1uRmiWCZEkR5R/cfF4T4S0aAc6gVJ84PrPuqwSa3wNM02HOjV3aBQSc89RSjtGg4Zd2afKZw2AlSXFt8HbBbbxjtNlr7QkcoaIe+Qk7LDOfGh5Zje 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. We use urandom_read as the test program and in addition we also attach uprobe to liburandom_read.so:urandlib_read_without_sema and retrieve and check build id of that shared library. Also executing the no_build_id binary to verify the bpf program gets the error properly. Signed-off-by: Jiri Olsa Acked-by: Andrii Nakryiko --- tools/testing/selftests/bpf/Makefile | 7 +- tools/testing/selftests/bpf/no_build_id.c | 6 ++ .../selftests/bpf/prog_tests/file_build_id.c | 98 +++++++++++++++++++ .../selftests/bpf/progs/file_build_id.c | 70 +++++++++++++ tools/testing/selftests/bpf/test_progs.h | 10 ++ 5 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/no_build_id.c 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/Makefile b/tools/testing/selftests/bpf/Makefile index 55811c448eb7..cf93a23c7962 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -88,7 +88,7 @@ TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \ xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata \ xdp_features -TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read $(OUTPUT)/sign-file +TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read $(OUTPUT)/sign-file $(OUTPUT)/no_build_id TEST_GEN_FILES += liburandom_read.so # Emit succinct information message describing current building step @@ -201,6 +201,10 @@ $(OUTPUT)/sign-file: ../../../../scripts/sign-file.c $< -o $@ \ $(shell $(HOSTPKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto) +$(OUTPUT)/no_build_id: no_build_id.c + $(call msg,BINARY,,$@) + $(Q)$(CC) $^ -Wl,--build-id=none -o $@ + $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch]) $(call msg,MOD,,$@) $(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation @@ -564,6 +568,7 @@ TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \ $(OUTPUT)/liburandom_read.so \ $(OUTPUT)/xdp_synproxy \ $(OUTPUT)/sign-file \ + $(OUTPUT)/no_build_id \ ima_setup.sh \ verify_sig_setup.sh \ $(wildcard progs/btf_dump_test_case_*.c) \ diff --git a/tools/testing/selftests/bpf/no_build_id.c b/tools/testing/selftests/bpf/no_build_id.c new file mode 100644 index 000000000000..a6f28f1c06d5 --- /dev/null +++ b/tools/testing/selftests/bpf/no_build_id.c @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0 + +int main(void) +{ + return 0; +} 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..cb233cfd58cb --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/file_build_id.c @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include "file_build_id.skel.h" +#include "trace_helpers.h" + +static void +test_build_id(const char *bin, const char *lib, long bin_err, long lib_err) +{ + int err, child_pid = 0, child_status, c = 1, sz; + char build_id[BPF_BUILD_ID_SIZE]; + struct file_build_id *skel; + int go[2] = { -1, -1 }; + + if (!ASSERT_OK(pipe(go), "pipe")) + return; + + skel = file_build_id__open_and_load(); + if (!ASSERT_OK_PTR(skel, "file_build_id__open_and_load")) + goto out; + + child_pid = fork(); + if (child_pid < 0) + goto out; + + /* child */ + if (child_pid == 0) { + close(go[1]); + /* wait for parent's pid update */ + err = read(go[0], &c, 1); + if (!ASSERT_EQ(err, 1, "child_read_pipe")) + exit(err); + + execle(bin, bin, NULL, NULL); + exit(errno); + } + + /* parent, update child's pid and kick it */ + skel->bss->pid = child_pid; + + close(go[0]); + + 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); + child_pid = 0; + if (!ASSERT_EQ(WEXITSTATUS(child_status), 0, "child_exit_value")) + goto out; + + /* test binary */ + sz = read_build_id(bin, build_id); + err = sz > 0 ? 0 : sz; + + ASSERT_EQ((long) err, bin_err, "read_build_id_bin_err"); + ASSERT_EQ(skel->bss->build_id_bin_err, bin_err, "build_id_bin_err"); + + if (!err) { + ASSERT_EQ(skel->bss->build_id_bin_size, sz, "build_id_bin_size"); + ASSERT_MEMEQ(skel->bss->build_id_bin, build_id, sz, "build_id_bin"); + } + + /* test library if present */ + if (lib) { + sz = read_build_id(lib, build_id); + err = sz > 0 ? 0 : sz; + + ASSERT_EQ((long) err, lib_err, "read_build_id_lib_err"); + ASSERT_EQ(skel->bss->build_id_lib_err, lib_err, "build_id_lib_err"); + + if (!err) { + ASSERT_EQ(skel->bss->build_id_lib_size, sz, "build_id_lib_size"); + ASSERT_MEMEQ(skel->bss->build_id_lib, build_id, sz, "build_id_lib"); + } + } + +out: + close(go[1]); + close(go[0]); + if (child_pid) + waitpid(child_pid, &child_status, 0); + file_build_id__destroy(skel); +} + +void test_file_build_id(void) +{ + if (test__start_subtest("present")) + test_build_id("./urandom_read", "./liburandom_read.so", 0, 0); + if (test__start_subtest("missing")) + test_build_id("./no_build_id", NULL, -EINVAL, 0); +} 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..6dc10c8e17ac --- /dev/null +++ b/tools/testing/selftests/bpf/progs/file_build_id.c @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include "err.h" +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +int pid; + +u32 build_id_bin_size; +u32 build_id_lib_size; + +char build_id_bin[BUILD_ID_SIZE_MAX]; +char build_id_lib[BUILD_ID_SIZE_MAX]; + +long build_id_bin_err; +long build_id_lib_err; + +static int store_build_id(struct file *file, char *build_id, u32 *sz, long *err) +{ + struct build_id *bid; + + bid = file->f_build_id; + if (IS_ERR_OR_NULL(bid)) { + *err = PTR_ERR(bid); + return 0; + } + *sz = bid->sz; + if (bid->sz > sizeof(bid->data)) { + *err = 1; + return 0; + } + __builtin_memcpy(build_id, bid->data, sizeof(bid->data)); + *err = 0; + return 0; +} + +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; + + if (pid != cur_pid) + return 0; + if (!bprm->file) + return 0; + return store_build_id(bprm->file, build_id_bin, &build_id_bin_size, &build_id_bin_err); +} + +static long check_vma(struct task_struct *task, struct vm_area_struct *vma, + void *data) +{ + if (!vma || !vma->vm_file || !vma->vm_file) + return 0; + return store_build_id(vma->vm_file, build_id_lib, &build_id_lib_size, &build_id_lib_err); +} + +SEC("uprobe/./liburandom_read.so:urandlib_read_without_sema") +int BPF_UPROBE(urandlib_read_without_sema) +{ + struct task_struct *task = bpf_get_current_task_btf(); + int cur_pid = bpf_get_current_pid_tgid() >> 32; + + if (pid != cur_pid) + return 0; + return bpf_find_vma(task, ctx->ip, check_vma, NULL, 0); +} diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h index d91427bfe0d7..285dd5d91426 100644 --- a/tools/testing/selftests/bpf/test_progs.h +++ b/tools/testing/selftests/bpf/test_progs.h @@ -310,6 +310,16 @@ int test__join_cgroup(const char *path); ___ok; \ }) +#define ASSERT_MEMEQ(actual, expected, sz, name) ({ \ + static int duration = 0; \ + const char *___act = actual; \ + const char *___exp = expected; \ + bool ___ok = memcmp(___act, ___exp, sz) == 0; \ + CHECK(!___ok, (name), \ + "unexpected %s does not match\n", (name)); \ + ___ok; \ +}) + #define ASSERT_STRNEQ(actual, expected, len, name) ({ \ static int duration = 0; \ const char *___act = actual; \