From patchwork Mon Feb 12 21:38:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13554049 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 D2498C4829D for ; Mon, 12 Feb 2024 21:40:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 700BD6B0092; Mon, 12 Feb 2024 16:39:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B2996B0093; Mon, 12 Feb 2024 16:39:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 505206B0095; Mon, 12 Feb 2024 16:39:59 -0500 (EST) 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 3B7E56B0092 for ; Mon, 12 Feb 2024 16:39:59 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 122A7C062E for ; Mon, 12 Feb 2024 21:39:59 +0000 (UTC) X-FDA: 81784469718.02.C5CE671 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf30.hostedemail.com (Postfix) with ESMTP id 814F28001D for ; Mon, 12 Feb 2024 21:39:57 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=O5gjYq1m; spf=pass (imf30.hostedemail.com: domain of 3LJDKZQYKCKsdfcPYMRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3LJDKZQYKCKsdfcPYMRZZRWP.NZXWTYfi-XXVgLNV.ZcR@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=1707773997; 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=ZVR5x2YILJK+zhPcdicEuOWU7vdbf7DJDXDz97hru8E=; b=D2sVYxrfs3Q5VhU67UcX2eJEI3g/dreUBs3TChMl9G1B+NlakLzqnzHwlw7Z3AS8xgBeK4 qH6zUiBsWSJFPs+0WG+rv76zfRFJhrq2alzXTh5CsMscUTxPn6Qw1NRdE9J/yxzfRo/KCR J3BMub+eXXBsHKgvhFKA+MJ2KaDxrSA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707773997; a=rsa-sha256; cv=none; b=mos8WPYXFsycO4FicxO4gNa4QFA9zS420i8IOBAn0htAZ+eyUoQUiAo4zSNoCrMYmH9+MI qcBfN/MgXgqTGFAY9u7fZ989AuDzxSRiAjYjN1hHZFaYIXWXCG5iswpL/dozo60s20Z7AY d6IGl2RjoLAY/mdIkWisZ3zRN7ACWxQ= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=O5gjYq1m; spf=pass (imf30.hostedemail.com: domain of 3LJDKZQYKCKsdfcPYMRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3LJDKZQYKCKsdfcPYMRZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc74610c9b7so5843885276.3 for ; Mon, 12 Feb 2024 13:39:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707773996; x=1708378796; 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=ZVR5x2YILJK+zhPcdicEuOWU7vdbf7DJDXDz97hru8E=; b=O5gjYq1mPiQTG/Ge8pMO6jkHfu5eQVAS+aEhON59oJXEVvM3XfrHogwrnW8NAdkx94 2dQ0c3wzk3WSbu4krQOi6x3FsDgGT6dKduKsQFRAOTuo09tbRwXTk1mr63Ajo6n+pySr ksXiCE8BlvrfCfUvIqFLJSLhR8TW/xPgOHRbC5FmnvNpVrN3SKqx9jmfbe4q154CsSta IbEH+bqyoSyEoLcloM1ps2fTPIeu2GQzsb4Ud0jy+9+zC+Y5e3J/I0MDwiSllvOjxP3s SZoKcnJrriIF/HubnybCfE0LjC0uN/iq+NOuJ8Br+ha92LUsY587lrWHCAmN6bJjUGaU 5Xyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707773996; x=1708378796; 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=ZVR5x2YILJK+zhPcdicEuOWU7vdbf7DJDXDz97hru8E=; b=cu/a9E9zBa2rb2XaLtVpn9ZPam3tzn2PDmWQ9XLQmYCp05zvE2yUte0GgnMMpmGyNB KEyYf+kMeANreP5frrU2XcBZu3cZ0TZYzPDoBlvTdBMwN8L9qjRuvQGDvCbKkZEbDm+1 D6znC49xd+cSdmJLKJX6oPfxcLTjxbPgBYDW4baZnL0bYrJlQQSRnQr1fcc0GHCr7s9c pDx0iv7TGgE7Q3fOO5Ct+c973GoML1Br+885R36bBaGjsS1UU9p4nPrf4uXf+ODuUEef 5LDuOgMug69Twwx+9zQKBGi2hJ3Li4vxDz2q9LcgeuYYZCFsrw1zoFFYqUjSnVOuVM9P Jn3w== X-Gm-Message-State: AOJu0YwwwMiH2NLxCMR+yafpghcL3uFkiAI9OpmXWXHpcSxufq5I2w6z zc55uOsvCOf+RDDw3P3YSAvuyus/GmsS+ZL2J/LZVbRwT+jDMzAa7wmzlyCC5MlfNUNbh+8RMTS E5A== X-Google-Smtp-Source: AGHT+IFrahWopogc+rmwq0xz3eYYu6J1qHJr+fSwJ9oyAoKj8/t+TZ8Jneh5cBWLRBbyxn7YPxVr/zwosmk= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:b848:2b3f:be49:9cbc]) (user=surenb job=sendgmr) by 2002:a05:6902:100b:b0:dc6:fa35:b42 with SMTP id w11-20020a056902100b00b00dc6fa350b42mr2046974ybt.2.1707773996437; Mon, 12 Feb 2024 13:39:56 -0800 (PST) Date: Mon, 12 Feb 2024 13:38:57 -0800 In-Reply-To: <20240212213922.783301-1-surenb@google.com> Mime-Version: 1.0 References: <20240212213922.783301-1-surenb@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Message-ID: <20240212213922.783301-12-surenb@google.com> Subject: [PATCH v3 11/35] lib: code tagging module support 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, 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: 814F28001D X-Rspam-User: X-Stat-Signature: scpja5td7r7ojjtga7tsz3mbafx8uzy1 X-Rspamd-Server: rspam03 X-HE-Tag: 1707773997-68487 X-HE-Meta: U2FsdGVkX1+75s+2r/ME5b5ggcBsl8kj6AQJi7WIb83D1WAYpem47UmElVpLmnN4NiFMjl4VD4F2BuXboXoT+TEnDWAzmDPXIcU/VbKza9pOo0RMOFWNi+j1Ymzec3RztAucYFBlH0tJyJ4Qx+0iRqc1rMAafp0nW5+5dBqOPfBQ1V6mD+xPt8qorgamsA2SIC2LfOgEbA2kkbHbh90Btw3qeE4QS0ozyPH39dGXWzAyVOsyN2mdNhoPKfiYYQGbS4qnmQw3Bm43U9Ke8qM7aW65ZxZ6jy1BYlcdZmnkQhq6vYC9RME3IFZ6Ebo3SHVGcIZeeWXq9vwTny9kvIfJO4X1HqzNcpmSSWfxfAG+1hS9A829g3AhYebaIf5j2ApoCJwsZTcX6KkUJaDqVGXOcOHo7BVuOklmppelL4KY11BsfqZxQyQl0jxIi+9tavbWueAe+D2LK4mDTk/qZh2kPb5CYwraV8h6lfqwAf4XEDgTXIYnnyqWPhGvq3cL5VPZ+vTiUocWFfTxQ+CvsqvhkFaOTkYytXD0V5/F/8zOMCDApn7eYpi3dcwWc2LAoxVQBBNgP7Jh0SUdOJTv8lgzRFQMnZSPucRUZwgOcyS5xtKeuR/Z+R2CsKYK1IebS4/XDLJwRWFYkek6rP02yTXctu1fMZeybZ45WAMpJuxpZpqI9AynYccBOZ59AZTJNqgDa4Gtj4slnQFeMPgrVzcWxjZj2ytsmYsmFa7bb93t3z0m5tX0Zru7nbTk+PccAL1kMtBbbvr9LXbPM47Gu2NwQ3Jre3oCl+VPTI8xe9VrLlvCNasJ8GwJhc0OOXa8hcLCaxb3PvO7H/OW0/BBXvEgKL83g8uWmFmofLzeRfu60DhKOLim5FbYWtcJqnNgVCkm21OP9vAAIDLZ6/wKVNyK/v+4avqz0gF8XuS/bB4g1ak3ULGfNR9draavVH2nKJFMNFZEDxdovG8i8LurbHC GCym+g6T f0iprm1wTMrrDCW8lXmwDbEoiocvWa7BWNuXPztVmdfnz6jp59WVE/oDAP9ZhTdesCPKqecEHl6G5KK7jzoxgBymEdHuh/wlV9qAVMup1xNXGhJCTCFA0qlKHVDa/oakjaWY0pOwxXk7DmfyyVUWdQnqmPJGSq0YTQJ+ucsq4Jrndkz7kepkJaHGiJU16oP+bzP+v3BxwNeZJb7xyMrzjhw9A4/gcpbPdMMgEa+9ZBs69YtWi67sbwEiEn5zoAH/Vz3gCeWqCEbeJ6DG2ENHyWBXdLr2V4MTXASRS08ML4ryhOpcH/Xr5Qfvt0WwE10W9BOImZJZADmJ7w5Tor91CjEo+z5Ljm9vXE6FdWtRa9YAZrsdQCLmNXA83eyz1gtAqDqGqn4N3OmnmNGEmi6yRzy80DdQKdOCKOWbf 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 support for code tagging from dynamically loaded modules. Signed-off-by: Suren Baghdasaryan Co-developed-by: Kent Overstreet Signed-off-by: Kent Overstreet --- include/linux/codetag.h | 12 +++++++++ kernel/module/main.c | 4 +++ lib/codetag.c | 58 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/include/linux/codetag.h b/include/linux/codetag.h index a9d7adecc2a5..386733e89b31 100644 --- a/include/linux/codetag.h +++ b/include/linux/codetag.h @@ -42,6 +42,10 @@ struct codetag_module { struct codetag_type_desc { const char *section; size_t tag_size; + void (*module_load)(struct codetag_type *cttype, + struct codetag_module *cmod); + void (*module_unload)(struct codetag_type *cttype, + struct codetag_module *cmod); }; struct codetag_iterator { @@ -68,4 +72,12 @@ void codetag_to_text(struct seq_buf *out, struct codetag *ct); struct codetag_type * codetag_register_type(const struct codetag_type_desc *desc); +#ifdef CONFIG_CODE_TAGGING +void codetag_load_module(struct module *mod); +void codetag_unload_module(struct module *mod); +#else +static inline void codetag_load_module(struct module *mod) {} +static inline void codetag_unload_module(struct module *mod) {} +#endif + #endif /* _LINUX_CODETAG_H */ diff --git a/kernel/module/main.c b/kernel/module/main.c index 36681911c05a..f400ba076cc7 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -56,6 +56,7 @@ #include #include #include +#include #include #include #include "internal.h" @@ -1242,6 +1243,7 @@ static void free_module(struct module *mod) { trace_module_free(mod); + codetag_unload_module(mod); mod_sysfs_teardown(mod); /* @@ -2978,6 +2980,8 @@ static int load_module(struct load_info *info, const char __user *uargs, /* Get rid of temporary copy. */ free_copy(info, flags); + codetag_load_module(mod); + /* Done! */ trace_module_load(mod); diff --git a/lib/codetag.c b/lib/codetag.c index 7708f8388e55..4ea57fb37346 100644 --- a/lib/codetag.c +++ b/lib/codetag.c @@ -108,15 +108,20 @@ static inline size_t range_size(const struct codetag_type *cttype, static void *get_symbol(struct module *mod, const char *prefix, const char *name) { char buf[64]; + void *ret; int res; res = snprintf(buf, sizeof(buf), "%s%s", prefix, name); if (WARN_ON(res < 1 || res > sizeof(buf))) return NULL; - return mod ? + preempt_disable(); + ret = mod ? (void *)find_kallsyms_symbol_value(mod, buf) : (void *)kallsyms_lookup_name(buf); + preempt_enable(); + + return ret; } static struct codetag_range get_section_range(struct module *mod, @@ -157,8 +162,11 @@ static int codetag_module_init(struct codetag_type *cttype, struct module *mod) down_write(&cttype->mod_lock); err = idr_alloc(&cttype->mod_idr, cmod, 0, 0, GFP_KERNEL); - if (err >= 0) + if (err >= 0) { cttype->count += range_size(cttype, &range); + if (cttype->desc.module_load) + cttype->desc.module_load(cttype, cmod); + } up_write(&cttype->mod_lock); if (err < 0) { @@ -197,3 +205,49 @@ codetag_register_type(const struct codetag_type_desc *desc) return cttype; } + +void codetag_load_module(struct module *mod) +{ + struct codetag_type *cttype; + + if (!mod) + return; + + mutex_lock(&codetag_lock); + list_for_each_entry(cttype, &codetag_types, link) + codetag_module_init(cttype, mod); + mutex_unlock(&codetag_lock); +} + +void codetag_unload_module(struct module *mod) +{ + struct codetag_type *cttype; + + if (!mod) + return; + + mutex_lock(&codetag_lock); + list_for_each_entry(cttype, &codetag_types, link) { + struct codetag_module *found = NULL; + struct codetag_module *cmod; + unsigned long mod_id, tmp; + + down_write(&cttype->mod_lock); + idr_for_each_entry_ul(&cttype->mod_idr, cmod, tmp, mod_id) { + if (cmod->mod && cmod->mod == mod) { + found = cmod; + break; + } + } + if (found) { + if (cttype->desc.module_unload) + cttype->desc.module_unload(cttype, cmod); + + cttype->count -= range_size(cttype, &cmod->range); + idr_remove(&cttype->mod_idr, mod_id); + kfree(cmod); + } + up_write(&cttype->mod_lock); + } + mutex_unlock(&codetag_lock); +}