From patchwork Tue Oct 24 13:46:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13434582 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 27926C00A8F for ; Tue, 24 Oct 2023 13:47:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD0416B0270; Tue, 24 Oct 2023 09:47:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A597B6B0271; Tue, 24 Oct 2023 09:47:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8858B6B0272; Tue, 24 Oct 2023 09:47:13 -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 6E26D6B0270 for ; Tue, 24 Oct 2023 09:47:13 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 426FB40470 for ; Tue, 24 Oct 2023 13:47:13 +0000 (UTC) X-FDA: 81380481546.06.F6BCB73 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf16.hostedemail.com (Postfix) with ESMTP id 74FF418002A for ; Tue, 24 Oct 2023 13:47:11 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=dSdNxrMi; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 33so3ZQYKCIMz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=33so3ZQYKCIMz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698155231; 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=YavwLOEBRwC8H4iLOoVDT3e7zkyr1je9EHR8XAi9ck4=; b=j8FZd6zcMtIHFeUAQCOesmS3dmjo4IE9k482BJUYbLSfwlExT5B2uiJLzq41gW4LbtDmqU SCCBXsCaFAqXyCumt2Q4xg9Lp1espL60i2C+hLh62tjQ002uHmOZ7l4k6U3Ti70u7q0vur OtaP0uyln5pLht+cqomo9m8ZWtltUh8= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=dSdNxrMi; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 33so3ZQYKCIMz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=33so3ZQYKCIMz1yluinvvnsl.jvtspu14-ttr2hjr.vyn@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698155231; a=rsa-sha256; cv=none; b=qHfrqtSyzG8/ro1e9haLcwVFSK+9lwaWLFgGAp/Vq5KXAg8j6y2lE0xqPrrYegWr6z0Mod 5PkfhO+9tQ51/4oURKyvWIXwu7RL3DsouyS19P02mDJ274mFUXPk1y8git8sbCX+jQSmhy 5MpBQHBwkRmBJB9t5oXhTmrNLGEnHPk= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5a8ead739c3so59306447b3.3 for ; Tue, 24 Oct 2023 06:47:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698155230; x=1698760030; 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=YavwLOEBRwC8H4iLOoVDT3e7zkyr1je9EHR8XAi9ck4=; b=dSdNxrMi161j1FoX2yATUVM3N2zyxX7H3fnehTPQOxnwT2t12fhxmbOqm2aeuznDs6 PI80MXSaj7YtiN2//FiLfQGnSxbYjYEwUS5FX1jGzkn32vwBF2F7KKa1tyYab6cl0VDA V4/Niiwgp+Lt89hw/DaFEwYzlbkovjuMGGoJQPnqPCbaU8OAADVrcnZD5kyHOQ4jZxOQ 7G9R7F3bvXgKlIXecEepWsSZT9g+CVnD5VP4GACQn1Guv3/GXSLRomEcyB1qfsI158/J fn58DloXVqBFymvJ5Efpn3pBmgZWzh2ICHNkHphcNdGhcimI1Db5EzvJof2E/DuSFPzC bcHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698155230; x=1698760030; 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=YavwLOEBRwC8H4iLOoVDT3e7zkyr1je9EHR8XAi9ck4=; b=VLdNhlHugksQiHCcBX9c9rUFS07f0tHpF7d43ucpvjv472T+gtpT/2hIEssh/Zfm+V YA67DJF7QwRXXsbX8I46lyPYnlFZkTryURYrlDzFYZo6fs1cc7glPk+Cof3QDzWITZ1z LwAkfiJt/OAzk3ud1k/7Ld0wiHjgO/ZF2k9SE8CyJAdq1QkJBV5tfcaKZY4fjSvAe1NR FgCpzV8jDJ4MFlo/z4D9Rr47q4DECYQMy4BsUdxqBJ7orPGk0/FbILf7IOGO0/9wGVKl bEXLNtPej9c1NmZ/4cvDUnXhc/mQZNTbfbYJQB9jrOjgnWkhYOcNb6JmoX5omIkUQmqb uCZg== X-Gm-Message-State: AOJu0YwUD0JbUWHhcA3uLaV3nxriCaugLBqWuOl92nRrWl7C/g0a8Hvu ZlqIpv4jMgmXBp2pJXc/6fwO0aFk+b0= X-Google-Smtp-Source: AGHT+IG23pgEuZi2XWkcWZyR41lqZU1TGfs7mnZJ7YZozt0qnBcTuPc8JBuvXuLPX/Ngmgob/V7gfXg7y38= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:45ba:3318:d7a5:336a]) (user=surenb job=sendgmr) by 2002:a81:4e0e:0:b0:5a2:3de0:24a9 with SMTP id c14-20020a814e0e000000b005a23de024a9mr271520ywb.1.1698155230510; Tue, 24 Oct 2023 06:47:10 -0700 (PDT) Date: Tue, 24 Oct 2023 06:46:10 -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-14-surenb@google.com> Subject: [PATCH v2 13/39] 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, 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: 74FF418002A X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: dyh99wqsthespdsqg7fccuckqxixf78o X-HE-Tag: 1698155231-295385 X-HE-Meta: U2FsdGVkX1/owtOy2xxjAf8xG/XSMpvb+8wJmC54xTTpd2DJ2rAJ5t1C0f50OI/Loi7rSfJNpOgAopYq7LxZKLDCV2/f+jcTNZOy47WC2lE6U+NYuIouJveGiIuPtCIrDWeVuSTjK1S6eIQV257vF1LL8j+BXdMG2oC/2nbaoT4jrmmxpSZdCkkPYaAHbRy/0BLjOnLIzerCEzI+5U7zH3uUpRdIPgaZiMQEA8K+TEvQib9flDSDk+2MmmfYdXF/+jM6iTxzVuDcXOZI/wHtwXgLtOQJXe2SGISKfLBEUgcXnFbaoImJ918SWVJS+K+RA8IjFaseLH0nNxm/lk6KegpjVTX7xzeJ6it2kjzdS3s8DqK1k2qkUzjjZOIhjkJXp6arf2UjWzSA7urrLV+7Eiw9UkJhDpyzVQlishymkN/ysptBHgweprgUB2PcFjhWbeT3ar12nevaNaafwjyjf6PDapf5K52EFndMTLbghwQIiC+T+azHhB5Qd/OKL8mD2zbCCEsS2uIBKEZx9WDkDYFQTvRZ7DGg8qI3jcx+VgWJQIXYgElQVnEbVAv1EdCIH4c1pL3raTbxk+SlkOP1CYy+Cb8TZqJ7TfKSX9xDzAAvazZp6h7UgnJ/U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@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 98fedfdb8db5..c0d3f562c7ab 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); /* @@ -2975,6 +2977,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); +}