From patchwork Tue Oct 24 13:46:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13434581 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 A6000C00A8F for ; Tue, 24 Oct 2023 13:47:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B9B96B026E; Tue, 24 Oct 2023 09:47:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 43CE56B026F; Tue, 24 Oct 2023 09:47:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2421A6B0270; Tue, 24 Oct 2023 09:47:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 06BD06B026E for ; Tue, 24 Oct 2023 09:47:11 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id D476116087B for ; Tue, 24 Oct 2023 13:47:10 +0000 (UTC) X-FDA: 81380481420.11.4C0DA41 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf11.hostedemail.com (Postfix) with ESMTP id 246F94002B for ; Tue, 24 Oct 2023 13:47:08 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=b2uHif12; spf=pass (imf11.hostedemail.com: domain of 33Mo3ZQYKCIExzwjsglttlqj.htrqnsz2-rrp0fhp.twl@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=33Mo3ZQYKCIExzwjsglttlqj.htrqnsz2-rrp0fhp.twl@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=1698155229; 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=9NoZOXA8j9UYH0qbl3Heb4+Wi1EaC598WUGqABNW4pA=; b=be4BAYwvCG9V7bnTIK8K2gDap0sUGm2IvTJCgl+ZHLox9tiHi38jCPPyFEd2yCMj2tUDfi f2968ACaAOD5Z14x5HLtOBgUYEJ3Q/gakfPVo4BnBS+6cQRRUNS6DBKv6f4oWDqDsb6o2Z j84RJN+y6P/73pUUHU6mdGTiLGEp7A4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698155229; a=rsa-sha256; cv=none; b=oeCxk0Np19d9oayh9zxiL3UUwCKp8bup0ZwdHZi+INwhELTbT/9wWA2ep0Byw1Jy5sKzOG OAM1SSqueGjGD5y+Uhy4Yid3T2NHiO657vQGX0YLyBuu56ZF87aOyvfkTRZ5+ay2fvOSJw IhexTtORl3GRDnfu8KJokqNMrK7np+4= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=b2uHif12; spf=pass (imf11.hostedemail.com: domain of 33Mo3ZQYKCIExzwjsglttlqj.htrqnsz2-rrp0fhp.twl@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=33Mo3ZQYKCIExzwjsglttlqj.htrqnsz2-rrp0fhp.twl@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5abfa4c10c4so26018187b3.0 for ; Tue, 24 Oct 2023 06:47:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698155228; x=1698760028; 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=9NoZOXA8j9UYH0qbl3Heb4+Wi1EaC598WUGqABNW4pA=; b=b2uHif12riXAogF6ua4KeNMK2Y1Q2vL/Mhgmx+UgVAbnj75Pa36jD0u0qYoElPZdET TGYeDT/Yb/MT05BxnKgThUSL57Yz6GGu+nu41/ABoId+q/QNPfWbk0Z5sTHoMDE7o4lI IuHpWvfhFUEZqWcWnpzz/p7rVxzTkO4mXnM6wUciCcjtpQy56xs9afT8KJwKuAL/li28 C6VJJSTVyjMgMxoj/ciJRYUzUDPQ2AomjuVc6uOsqUtDRWNB7R5JHKX7zkaDHflzDztK BOfi2nigqhCCrIzbcms9ehgrIYLK/WgYPmjOCS9OA/ANIjGiDTsuDXVdRHKQoYgVOM7l Jegg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698155228; x=1698760028; 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=9NoZOXA8j9UYH0qbl3Heb4+Wi1EaC598WUGqABNW4pA=; b=C0S6zKQNBGe5JQNqRSZXPFjJGpBZL5V33bNMKqwNS2NO0FJpgSv66AUw5YvVUgkv3u oaTC/GNX2rsijslb5RcwGb5BApX/+24GzOadPJV5gkl1IIh//55DrzV/BdfnsGfuc7ks b+6OG2yvQyRYZc8dPQk1fZ1MfSbTlCeiAheZM6DW5jNRwgCWakMLQQsck0NEvBYLGhKR RljKQyncTATOsHC6Pb0rXHB8MeCwNr9AaJstCEhs9ZliKydoi5QhP1yACqgSmf4cM6ia 6C8f5NVBdf+IIctyzsS48vQgLV9aSdtdGH1hJUWiDWKdKvvkhM+aDALrG6XHjY+Hx/Rl jxcg== X-Gm-Message-State: AOJu0YxnBsozJbVn4lYMtz8oN/Ojx2UDezfK4U7IJokfwLUnjpFUV1XB 4en7v47nsurkxUi9RILMIMv3cQuEszw= X-Google-Smtp-Source: AGHT+IEp6i7OIYLqR0zo2UBT4fCb6INeX9PpoJn2QVEvfaV9Q3dpXadwEMR0perH7aOFZkHGn0YJqemKbwE= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:45ba:3318:d7a5:336a]) (user=surenb job=sendgmr) by 2002:a81:a08c:0:b0:57a:e0b:f63 with SMTP id x134-20020a81a08c000000b0057a0e0b0f63mr272098ywg.7.1698155228147; Tue, 24 Oct 2023 06:47:08 -0700 (PDT) Date: Tue, 24 Oct 2023 06:46:09 -0700 In-Reply-To: <20231024134637.3120277-1-surenb@google.com> Mime-Version: 1.0 References: <20231024134637.3120277-1-surenb@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Message-ID: <20231024134637.3120277-13-surenb@google.com> Subject: [PATCH v2 12/39] 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, corbet@lwn.net, void@manifault.com, peterz@infradead.org, juri.lelli@redhat.com, ldufour@linux.ibm.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, 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, 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-Queue-Id: 246F94002B X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: ou5ydxkaq3bgi8gq817enbxu37aws5ry X-HE-Tag: 1698155228-897779 X-HE-Meta: U2FsdGVkX18/xbSgXbyUUhdubE8uTRcT7q1RQF8Y+jzUWyVaT/DNpGBCgZNq2zFE246IJL1qyMcO47W5fs9MRzD/NLhd6gTg1uanRwnYBe4RjrXfcq0wzwQwCgrPXt4Wkn78zshyurc535F4GUBF6WoZz/ZbC9poo79UmlvWzKWjBXuTCHfZcMZD28KD3SUbV612dboDOHrbijuVviSIgN9ZijhtbnVDB+NMKCk5yeWWAGGPPGwH7AilBruPhgT0znhSxZECDoVuscds3O8FszP0xTz7jWaYCqPfhbCzPgpVaYvmm6iKCasghYUsLwOKG0HEsWjiV+I6wCuSYT9Y5fkcua14XXJU/qjvS6HuidhxQm78Gjd+exfCNHWy1vzBq7HXF76xl1fLpWAwwVpw0UHh3iAT7vJTWo7MSrZ67LwkZMDpFwEOCl29iLyItT0y8uCBvcdEklIHGnjvKQqoYsyJ+BQakWPKd6oGv7JnHOmxScj6T75GdEgw7EmV+rRdB8vwYD9Ao7TVx9XBva7dek0Ljj/05rpVz50H7XewRHXqtfpussmIatkNXPtMHaZEOS7qfvkKFVboc341sc7gi+Djp++qlJlcNQQEP441K/U70l0iK6JGMr1z2X4FAWpsgdMoOLV2105PIiPXzbrDFE9QJsZ9kee4rGsOK5InaQ/ByrJ4HpKMbyKJ5fNHNJTZ+uFtyRu/s/8lLmlAYApaJKxY25H8F8GIh76Fev11NCezcwZMVPfUhbKtv1wQn6seA6sl0wmF0E9tPS/w9Pdh/FttBVx/o7COn4OTZcAu2kAkRh/6CayGtZe635hfg0sVak7mg4WgPUkk6njNvR5Ebrk78XqpzuzzMB+4yOtXbsiYRMRVyvopGU3mTc9sIs36fslPE1V7La4FTcw9MsK5OSdqONPyFTaf6ulQDa8dWCDB09nflWkM2Essn50bVQUdN6Qgsw8V04lnF4blJQM IueQ+3MV 9+ATdnX0oCZEErUi9SUybIt1+g+HmanmymPV582eYAlhAPl8xxHtVDZt2nq1d9us/f01J13pZhmF+ptytORvYmauDX/60hqVuP8vpx4294EBlB9vfydlCBQgpcwZyUKUD5jksNTmjUpJ/uLPEHYBVQAaDQizQc+b/AP+U+FHAYaIPF3gr7pdFsHay6/ANeCxPFp4M5km0F42lAnVcAdFkWqckmVZj484ohnAxpVlxJ0rnKIo0obn7M5LQmPY3fnU491Fsdo77KeJhP9cAd/1hxHKNWOwAZsEkGimGBiI08vIJD9T4saLu3FVqJjQRmar06fdvnZCAGv2bGh98xWUNxNMzmOVbU6/CP+GZQmDF/cDQebZgkmSxlq9pMVUd91YACNYJ20A8RMEXtLr6d63xOsitAbyU226i4op17JT0CAXODANQ4sNP9BZUSoB11yKbljPdPP75biI2DpeJSxgHozgZSlEJYOLTIgWWwy/XI5/W734u1SCxvpw8raKCuxzpzZIFL8EpkJwfsl25aJjfKSHLQJUif2spllIfn80emrNMYIJkmXNQTMrYfSB+9uHGc68acVnMeeA+IrxdWdaGyfGz8QfmbZAdr0FLgqF2lXgg8a2T5qBe4tEr/ZmWD1OjkyWuF3LVjo/dGBjNK/0fnEBbI97tmu+wHvIMnyqSmUjns7M= 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 | 71 ++++++++++++++ lib/Kconfig.debug | 4 + lib/Makefile | 1 + lib/codetag.c | 199 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 275 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..a9d7adecc2a5 --- /dev/null +++ b/include/linux/codetag.h @@ -0,0 +1,71 @@ +/* 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 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_range { + struct codetag *start; + struct codetag *stop; +}; + +struct codetag_module { + struct module *mod; + struct codetag_range range; +}; + +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; +}; + +#define CODE_TAG_INIT { \ + .modname = KBUILD_MODNAME, \ + .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 fa307f93fa2e..2acbef24e93e 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -962,6 +962,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 740109b6e2c8..b50212b5b999 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -233,6 +233,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..7708f8388e55 --- /dev/null +++ b/lib/codetag.c @@ -0,0 +1,199 @@ +// 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; +}; + +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) +{ + seq_buf_printf(out, "%s:%u module:%s func:%s", + ct->filename, ct->lineno, + ct->modname, 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; +} + +static void *get_symbol(struct module *mod, const char *prefix, const char *name) +{ + char buf[64]; + int res; + + res = snprintf(buf, sizeof(buf), "%s%s", prefix, name); + if (WARN_ON(res < 1 || res > sizeof(buf))) + return NULL; + + 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; +} + +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; +}