From patchwork Wed Mar 6 18:24:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13584420 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 9466EC54E41 for ; Wed, 6 Mar 2024 18:25:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B46D6B009A; Wed, 6 Mar 2024 13:25:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 83F106B009B; Wed, 6 Mar 2024 13:25:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 61D9E6B009C; Wed, 6 Mar 2024 13:25:09 -0500 (EST) 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 4BA006B009A for ; Wed, 6 Mar 2024 13:25:09 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 0F2DB1C11BC for ; Wed, 6 Mar 2024 18:25:09 +0000 (UTC) X-FDA: 81867441138.07.7BB7297 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf22.hostedemail.com (Postfix) with ESMTP id 64CD1C0022 for ; Wed, 6 Mar 2024 18:25:07 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=EPLLtBxT; spf=pass (imf22.hostedemail.com: domain of 3ArXoZQYKCEUz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3ArXoZQYKCEUz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709749507; a=rsa-sha256; cv=none; b=UtXThJg8mIMNzXXMb2pjMS072ySb9xdLWqhi9Rfwrs5vG6aIe2W2n5E1HbbWH66c3gZq2q fzoLj9jEZKMgn52tF+uKsSvozqyhgX+8iFJ6UZChNjTJz0Sid3sZtookSHtm402rQqTJPM GO7dV2ITSAi+rr1nlKb4neFkwJSmINc= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=EPLLtBxT; spf=pass (imf22.hostedemail.com: domain of 3ArXoZQYKCEUz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3ArXoZQYKCEUz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709749507; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=BNgaGcRDgPcaIXeO4Fkb/yIfx8W6judwG+Ary21N5qY=; b=cbTPFp7tnwFNPYUZ8/KYPNSRYMjt+SJ7pJagQ5OuBYChEWtEHBMj4AWUjAtEDKA7fvDg1r Tl9UCRhYnF/HP4HRW02XubmIXE4m09x3CYP/0JnXpu5Hx6zbx2OYLK/CfZtBNHIRDF/4S+ 99ZGcYa8nLxv0E+9/BGk1+srUPxS04U= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dd0ae66422fso199324276.0 for ; Wed, 06 Mar 2024 10:25:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709749506; x=1710354306; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BNgaGcRDgPcaIXeO4Fkb/yIfx8W6judwG+Ary21N5qY=; b=EPLLtBxTZDQh+7u2NWS4F8BnKzxI9X3ndXDnE1JmS1/aJcDDZfQdb4yEDxDTrkaq9t 8PqVfrsP96ApGwycjFwGdwmT5cdbADW796jKJ5lKQC6thxyejg2XHzR4uJo6Cqo9Bhg6 6YR5bwFQOYoIjyeEnNBvFH90a4t/fzSzoHXYS1oqQSkKWnFm9rwVBsyTOCvBLENELztz NXw8Oa+3ADY6OIj0BXxh2lnerkVPOr+jKCxsZBhVECIEmm4RI2hcOLACwVn1Sjvr6Y1H tvS+tEjKbJUbas/yIrYX1mrfgQGCa69OZDxbR2k+waeuUUvt7AiV0V6DeTPvkoiDxrRD gCyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709749506; x=1710354306; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BNgaGcRDgPcaIXeO4Fkb/yIfx8W6judwG+Ary21N5qY=; b=Ohkm8Cg/vQWgaf2xS6scqBDiOicrkG+SaVX834On5LhLRBTKaKhs2Gs2TasTSG1XZF BN2lm8WOZeX6OPUIORfw3ATgc0u/fFNYarSjH6AutCUAgst+2p6TtizWvKsz7IVIZxrv 7B71sVUoRy9IYJ/GmhzwflCpn6mhyU6jlcp+GVP+GvEkZnyN03lhI1x9kngrI5w2pU9n 7pYCy29XLwTcy+FfEv+Ddv5KBnPVXBi9nhCiaGEcclzMA3cUl1FZoLrEI17+4PIUF1F5 XlWrmZIvrUBQaRVuoK44pXnFDEZ0ibHEjEEy7LUsoriBLPwfxtpP0TIALN1Jj2qpojbe QIVA== X-Forwarded-Encrypted: i=1; AJvYcCV/mkydR2H2RXuh4cSzOeptMQxtixydnx2QvHZcu7fJ9QDMHFgftn0eloL2lZwzxmKaBgsIxexgUAqs6O0fFNvqyw0= X-Gm-Message-State: AOJu0YwBdLMuAPPJm2L3nwMCx7xbFFjyqPPPyU0q0XwqdP+AevrSbqWH kTamG32cF54obV/YzXZNHtfijji92yu4Y328V+r7v5ScKNWE/ZP5tm0NTDIbOjLMs8W+BTWvmQa qUQ== X-Google-Smtp-Source: AGHT+IHPRw/ef5CXgN/EcTIYcsNOxCQ2QBNqcSc4reoJ5he/nASVYkogu18nMMcl34PXaT57a/C89ka0n4s= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:85f0:e3db:db05:85e2]) (user=surenb job=sendgmr) by 2002:a05:6902:1504:b0:dbd:b4e8:1565 with SMTP id q4-20020a056902150400b00dbdb4e81565mr1895059ybu.4.1709749506288; Wed, 06 Mar 2024 10:25:06 -0800 (PST) Date: Wed, 6 Mar 2024 10:24:08 -0800 In-Reply-To: <20240306182440.2003814-1-surenb@google.com> Mime-Version: 1.0 References: <20240306182440.2003814-1-surenb@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240306182440.2003814-11-surenb@google.com> Subject: [PATCH v5 10/37] lib: code tagging framework From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, roman.gushchin@linux.dev, mgorman@suse.de, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, penguin-kernel@i-love.sakura.ne.jp, corbet@lwn.net, void@manifault.com, peterz@infradead.org, juri.lelli@redhat.com, catalin.marinas@arm.com, will@kernel.org, arnd@arndb.de, tglx@linutronix.de, mingo@redhat.com, dave.hansen@linux.intel.com, x86@kernel.org, peterx@redhat.com, david@redhat.com, axboe@kernel.dk, mcgrof@kernel.org, masahiroy@kernel.org, nathan@kernel.org, dennis@kernel.org, jhubbard@nvidia.com, tj@kernel.org, muchun.song@linux.dev, rppt@kernel.org, paulmck@kernel.org, pasha.tatashin@soleen.com, yosryahmed@google.com, yuzhao@google.com, dhowells@redhat.com, hughd@google.com, andreyknvl@gmail.com, keescook@chromium.org, ndesaulniers@google.com, vvvvvv@google.com, gregkh@linuxfoundation.org, ebiggers@google.com, ytcoode@gmail.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, bristot@redhat.com, vschneid@redhat.com, cl@linux.com, penberg@kernel.org, iamjoonsoo.kim@lge.com, 42.hyeyoo@gmail.com, glider@google.com, elver@google.com, dvyukov@google.com, shakeelb@google.com, songmuchun@bytedance.com, jbaron@akamai.com, aliceryhl@google.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, surenb@google.com, kernel-team@android.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux.dev, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kasan-dev@googlegroups.com, cgroups@vger.kernel.org X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 64CD1C0022 X-Stat-Signature: 6hmfkk9xhmsnr1e4yincb9xjjc7a5xq6 X-Rspam-User: X-HE-Tag: 1709749507-989459 X-HE-Meta: U2FsdGVkX18uiiiK9Ghm8NYzIG5IP+Q2uFHyl1puQSN8yezgKb4sikfCTqm0s8KtzKuq7ML0gUa/U3S/4RbwndiOCA9D2d3yC4uiMdjDdxKIm44TEROEuxl1AagHxSzwVyMuVQMg1SzRrsnTogc4bGSaU0uPeZHSJZG+EZFVO1i85WwECHbzZVVePyXan9ZfPplRgiCP0TLpNwAfUG46avvpjkkX4x5al3QHU97ePuntx0JZYe8p12Dl6xarkSrrC5NGisMxnRohS6WkjJMr8z21gP+7pMnGftImOtcUELsmYHttQAmPTft1w5M/TwsT0TwkglMZem80Wrii76qmVayLfOYkpn3lqkRgtyo7Rb6KZKFvX73Sw6LGfMa0ZhuiSbqNwqa9sbsAIuJbZYtzTRLNi28X3hfirSu7BndEh1SWL2u8P1V5E5Gs1xnluWWtBfGeSSj0hbA+raoIMnAngfwtT+H63sXY35fpcXpBPrh0coKq6gFV6m2KphmCHt80SjyCOmbc8DE9WLM0PK+TN8v9HxtfyUANYr2eZlynAmEnYHXTd+7BRbw6MfN/rdPjHlPcqy9MqPYfvYkChuKLduPaIgClbw5XN4yuIfWBxkR/Flu1vd6raiAr7G58LV5FWwQvSnNhbT6BYl8RUrRGjd8WZLbCfX4FXVnRxpqwkpWkvuaTZo/ukHyAjyY1D3iyWnzbL4QxIvepbhh4xtt2oIzUGItHMLN/wA9tklnN5J4uaofhzsNgVtJ3kWwtBBfwKKJJ5uofYncCgw3i6rnVPkIk9f2IaOwfBqZwUd+xuzPnC0LHuUgTI/wbJntnZcudt6aU3Hemf5okzfIRGciaxd2ovBgjZ/DSTKbUPPQficN306ewNP8Mtmrc132DJvyfoUzswQfA7P1P6PVzGs0YweBkZrqlIVVrzJkz/h3u1L/oVvSH8xIryv0BJ/BJczfsYAmMF+3JgkNxoWApx5Q TlxpSM4f WyinfNuJjjdc1hos282yk+J5DwVEXGNfmSftnz3AXESO/BYf2jmfkjhrgNkdPtH2SC3QTYnXSW874PNLO40tvKO0R66lp8njgKOGHiu+ouioDJ3rBdTndSnRk9ts+uLE42dkOeMzp4mwwbUxIGabvytuA/kefpKD+eFqo/YV4aFXy7hMFnbuwvjq4dBNzU3+YRQRwVIgvor0/KWT3rCJkxpLz14CoyS1KhpLZBKwfV+LsCT4cAhpp/QN5kEz21nFKkMisexnYhTm39AHOaNz2jpEzjo+7MLum4Oi/p4JEmAFQjP2TuNPjdo8msZgPq1Ov4d330RQyofO6+QoweCBUAnRzgOAaP8Rcx4eXo53BG56ncdkS2ckbSxO3R3C3oaW4ijiHmM7EKZAk8t8fHYp9noVN+qPVR34GQYeNlZ1TTzjZ4Bw= 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: List-Subscribe: List-Unsubscribe: Add basic infrastructure to support code tagging which stores tag common information consisting of the module name, function, file name and line number. Provide functions to register a new code tag type and navigate between code tags. Co-developed-by: Kent Overstreet Signed-off-by: Kent Overstreet Signed-off-by: Suren Baghdasaryan --- include/linux/codetag.h | 68 +++++++++++++ lib/Kconfig.debug | 4 + lib/Makefile | 1 + lib/codetag.c | 219 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 292 insertions(+) create mode 100644 include/linux/codetag.h create mode 100644 lib/codetag.c diff --git a/include/linux/codetag.h b/include/linux/codetag.h new file mode 100644 index 000000000000..7734269cdb63 --- /dev/null +++ b/include/linux/codetag.h @@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * code tagging framework + */ +#ifndef _LINUX_CODETAG_H +#define _LINUX_CODETAG_H + +#include + +struct codetag_iterator; +struct codetag_type; +struct codetag_module; +struct seq_buf; +struct module; + +/* + * An instance of this structure is created in a special ELF section at every + * code location being tagged. At runtime, the special section is treated as + * an array of these. + */ +struct codetag { + unsigned int flags; /* used in later patches */ + unsigned int lineno; + const char *modname; + const char *function; + const char *filename; +} __aligned(8); + +union codetag_ref { + struct codetag *ct; +}; + +struct codetag_type_desc { + const char *section; + size_t tag_size; +}; + +struct codetag_iterator { + struct codetag_type *cttype; + struct codetag_module *cmod; + unsigned long mod_id; + struct codetag *ct; +}; + +#ifdef MODULE +#define CT_MODULE_NAME KBUILD_MODNAME +#else +#define CT_MODULE_NAME NULL +#endif + +#define CODE_TAG_INIT { \ + .modname = CT_MODULE_NAME, \ + .function = __func__, \ + .filename = __FILE__, \ + .lineno = __LINE__, \ + .flags = 0, \ +} + +void codetag_lock_module_list(struct codetag_type *cttype, bool lock); +struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype); +struct codetag *codetag_next_ct(struct codetag_iterator *iter); + +void codetag_to_text(struct seq_buf *out, struct codetag *ct); + +struct codetag_type * +codetag_register_type(const struct codetag_type_desc *desc); + +#endif /* _LINUX_CODETAG_H */ diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index ef36b829ae1f..5485a5780fa7 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -968,6 +968,10 @@ config DEBUG_STACKOVERFLOW If in doubt, say "N". +config CODE_TAGGING + bool + select KALLSYMS + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan" diff --git a/lib/Makefile b/lib/Makefile index 6b09731d8e61..6b48b22fdfac 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -235,6 +235,7 @@ obj-$(CONFIG_OF_RECONFIG_NOTIFIER_ERROR_INJECT) += \ of-reconfig-notifier-error-inject.o obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o +obj-$(CONFIG_CODE_TAGGING) += codetag.o lib-$(CONFIG_GENERIC_BUG) += bug.o obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.o diff --git a/lib/codetag.c b/lib/codetag.c new file mode 100644 index 000000000000..8b5b89ad508d --- /dev/null +++ b/lib/codetag.c @@ -0,0 +1,219 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include +#include + +struct codetag_type { + struct list_head link; + unsigned int count; + struct idr mod_idr; + struct rw_semaphore mod_lock; /* protects mod_idr */ + struct codetag_type_desc desc; +}; + +struct codetag_range { + struct codetag *start; + struct codetag *stop; +}; + +struct codetag_module { + struct module *mod; + struct codetag_range range; +}; + +static DEFINE_MUTEX(codetag_lock); +static LIST_HEAD(codetag_types); + +void codetag_lock_module_list(struct codetag_type *cttype, bool lock) +{ + if (lock) + down_read(&cttype->mod_lock); + else + up_read(&cttype->mod_lock); +} + +struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype) +{ + struct codetag_iterator iter = { + .cttype = cttype, + .cmod = NULL, + .mod_id = 0, + .ct = NULL, + }; + + return iter; +} + +static inline struct codetag *get_first_module_ct(struct codetag_module *cmod) +{ + return cmod->range.start < cmod->range.stop ? cmod->range.start : NULL; +} + +static inline +struct codetag *get_next_module_ct(struct codetag_iterator *iter) +{ + struct codetag *res = (struct codetag *) + ((char *)iter->ct + iter->cttype->desc.tag_size); + + return res < iter->cmod->range.stop ? res : NULL; +} + +struct codetag *codetag_next_ct(struct codetag_iterator *iter) +{ + struct codetag_type *cttype = iter->cttype; + struct codetag_module *cmod; + struct codetag *ct; + + lockdep_assert_held(&cttype->mod_lock); + + if (unlikely(idr_is_empty(&cttype->mod_idr))) + return NULL; + + ct = NULL; + while (true) { + cmod = idr_find(&cttype->mod_idr, iter->mod_id); + + /* If module was removed move to the next one */ + if (!cmod) + cmod = idr_get_next_ul(&cttype->mod_idr, + &iter->mod_id); + + /* Exit if no more modules */ + if (!cmod) + break; + + if (cmod != iter->cmod) { + iter->cmod = cmod; + ct = get_first_module_ct(cmod); + } else + ct = get_next_module_ct(iter); + + if (ct) + break; + + iter->mod_id++; + } + + iter->ct = ct; + return ct; +} + +void codetag_to_text(struct seq_buf *out, struct codetag *ct) +{ + if (ct->modname) + seq_buf_printf(out, "%s:%u [%s] func:%s", + ct->filename, ct->lineno, + ct->modname, ct->function); + else + seq_buf_printf(out, "%s:%u func:%s", + ct->filename, ct->lineno, ct->function); +} + +static inline size_t range_size(const struct codetag_type *cttype, + const struct codetag_range *range) +{ + return ((char *)range->stop - (char *)range->start) / + cttype->desc.tag_size; +} + +#ifdef CONFIG_MODULES +static void *get_symbol(struct module *mod, const char *prefix, const char *name) +{ + DECLARE_SEQ_BUF(sb, KSYM_NAME_LEN); + const char *buf; + + seq_buf_printf(&sb, "%s%s", prefix, name); + if (seq_buf_has_overflowed(&sb)) + return NULL; + + buf = seq_buf_str(&sb); + return mod ? + (void *)find_kallsyms_symbol_value(mod, buf) : + (void *)kallsyms_lookup_name(buf); +} + +static struct codetag_range get_section_range(struct module *mod, + const char *section) +{ + return (struct codetag_range) { + get_symbol(mod, "__start_", section), + get_symbol(mod, "__stop_", section), + }; +} + +static int codetag_module_init(struct codetag_type *cttype, struct module *mod) +{ + struct codetag_range range; + struct codetag_module *cmod; + int err; + + range = get_section_range(mod, cttype->desc.section); + if (!range.start || !range.stop) { + pr_warn("Failed to load code tags of type %s from the module %s\n", + cttype->desc.section, + mod ? mod->name : "(built-in)"); + return -EINVAL; + } + + /* Ignore empty ranges */ + if (range.start == range.stop) + return 0; + + BUG_ON(range.start > range.stop); + + cmod = kmalloc(sizeof(*cmod), GFP_KERNEL); + if (unlikely(!cmod)) + return -ENOMEM; + + cmod->mod = mod; + cmod->range = range; + + down_write(&cttype->mod_lock); + err = idr_alloc(&cttype->mod_idr, cmod, 0, 0, GFP_KERNEL); + if (err >= 0) + cttype->count += range_size(cttype, &range); + up_write(&cttype->mod_lock); + + if (err < 0) { + kfree(cmod); + return err; + } + + return 0; +} + +#else /* CONFIG_MODULES */ +static int codetag_module_init(struct codetag_type *cttype, struct module *mod) { return 0; } +#endif /* CONFIG_MODULES */ + +struct codetag_type * +codetag_register_type(const struct codetag_type_desc *desc) +{ + struct codetag_type *cttype; + int err; + + BUG_ON(desc->tag_size <= 0); + + cttype = kzalloc(sizeof(*cttype), GFP_KERNEL); + if (unlikely(!cttype)) + return ERR_PTR(-ENOMEM); + + cttype->desc = *desc; + idr_init(&cttype->mod_idr); + init_rwsem(&cttype->mod_lock); + + err = codetag_module_init(cttype, NULL); + if (unlikely(err)) { + kfree(cttype); + return ERR_PTR(err); + } + + mutex_lock(&codetag_lock); + list_add_tail(&cttype->link, &codetag_types); + mutex_unlock(&codetag_lock); + + return cttype; +}