From patchwork Mon May 1 16:54:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13227725 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 B3A7CC77B61 for ; Mon, 1 May 2023 17:02:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 37D2E90000A; Mon, 1 May 2023 13:02:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 32DF0900002; Mon, 1 May 2023 13:02:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F57690000A; Mon, 1 May 2023 13:02:15 -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 0F0E7900002 for ; Mon, 1 May 2023 13:02:15 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id BDD091C72A9 for ; Mon, 1 May 2023 17:02:14 +0000 (UTC) X-FDA: 80742304188.26.AE8CF72 Received: from mail-ua1-f73.google.com (mail-ua1-f73.google.com [209.85.222.73]) by imf27.hostedemail.com (Postfix) with ESMTP id 531274002E for ; Mon, 1 May 2023 17:02:12 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=ytR2UEYh; spf=pass (imf27.hostedemail.com: domain of 3DO9PZAYKCFMDFCz8w19916z.x97638FI-775Gvx5.9C1@flex--surenb.bounces.google.com designates 209.85.222.73 as permitted sender) smtp.mailfrom=3DO9PZAYKCFMDFCz8w19916z.x97638FI-775Gvx5.9C1@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=1682960532; 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=Fsyg89/vfc29JJa9GS5YhCGnLnEPc8LoOfUR2xEk3nU=; b=J11KTYqNKclOTfGr1fitpkhFeGpYyErqUlJ66Ww374QWkikl+Rhesx5llbQ75eXKEoUw32 4H1W6/1OlprOdIEHFUzWyu/oZ/KdP63RDHhgAZwPWANqt9+lSW8GmNBg+Es18bjaSv40e0 SHyfZJrpDqsJIV2n1NlKTsnp7sQvuQ4= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=ytR2UEYh; spf=pass (imf27.hostedemail.com: domain of 3DO9PZAYKCFMDFCz8w19916z.x97638FI-775Gvx5.9C1@flex--surenb.bounces.google.com designates 209.85.222.73 as permitted sender) smtp.mailfrom=3DO9PZAYKCFMDFCz8w19916z.x97638FI-775Gvx5.9C1@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682960532; a=rsa-sha256; cv=none; b=anfSO98540MBkH5afiGUXw1A2xcOf+FgjRyjf6w3F8DcqLUw0C5wSgqBV4CU/caC+NJS07 otSfoO/9I8rQe7cpAJdBmupo6k9e+6+gWHy3YN+I5CNcjg7YPiAG/UILPnoudBPKePDm5+ e51Wb/pon6r854kDH0f+RX5OHZi9dng= Received: by mail-ua1-f73.google.com with SMTP id a1e0cc1a2514c-77aada1df61so25928337241.1 for ; Mon, 01 May 2023 10:02:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682960531; x=1685552531; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Fsyg89/vfc29JJa9GS5YhCGnLnEPc8LoOfUR2xEk3nU=; b=ytR2UEYhuAD+YRVQWgUNv88SHnd2ynRKseoU78an9eCwuDcCW2rMe12kyedJx3/bUr 8mP8kQhLhNKKNpawueKHtOKyoqY1CMn375J69k0AA1tp/1rphK+jnt9ueF5P13LzahOU y2Ah0HOFnLnZp0Dc8RXuaw3q9Go3obH7D10CJODfv9q33ibAT2zvSNGbqQVMC0kInAQq UzghzRv+xC2U3ILRtmdjoCzixF3zu6VZBbkYp8zs89knY1zIe+um7602sn4pDs7+R8j7 tQSr3D3lf0d+7ugQL+9mZDuYlDHrbRse6zGFtm4RR5gEaU3/0UuAlif+cVBJJtA7joqF lR1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682960531; x=1685552531; 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=Fsyg89/vfc29JJa9GS5YhCGnLnEPc8LoOfUR2xEk3nU=; b=N2TL97bxndcXXNkdiOzEAcuUoa0Vso8/qQVywq/R4XKxMTtPHDYEBSp/RNE3Gd+mOO wmCtwUdHpcaIUbqAjq/Qp/AVdcmagMIh9ZtKROvXoDD4MO0ts1GmGnYi7gOVJDjpeKmi tJCp2LAvc3qth1LAJusRnw4/dx4mXr8J2BMGfQQpvqbXJlDOREfpm4b3Opry3k6oVL+a o0HRfJMCwMJlywEzFNMDNI9dHQLJ2ADKjA07KT0D/knqX4jMt6bWT3mwraZ5KhaK28RK 1BJphmZvGmEJVl2Mkw58baRJCD8NrWoM4rU9oHhCs9BDiirBx74NugMOvCMiU41vT7uv juLA== X-Gm-Message-State: AC+VfDxS2Uj7ZGaTJ+oFDKPoSt0Trz7YI+6wi2GuHJBuRCOlor23Hssp 4vmA/2V6yfuyjR8DUVbL2Naqk5ijJZw= X-Google-Smtp-Source: ACHHUZ75lgdLL8vnL0XbIs5yVvuJ1AQE8jmtL55GeRq4GA52aLYGF4UgqLBdCAr7mljybUtkaGeonaYDcH4= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:6d24:3efd:facc:7ac4]) (user=surenb job=sendgmr) by 2002:a25:8087:0:b0:b8b:f5fb:598d with SMTP id n7-20020a258087000000b00b8bf5fb598dmr8714623ybk.6.1682960140703; Mon, 01 May 2023 09:55:40 -0700 (PDT) Date: Mon, 1 May 2023 09:54:24 -0700 In-Reply-To: <20230501165450.15352-1-surenb@google.com> Mime-Version: 1.0 References: <20230501165450.15352-1-surenb@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230501165450.15352-15-surenb@google.com> Subject: [PATCH 14/40] 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, 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: 531274002E X-Stat-Signature: 5db85bjcmy7q5b71i7cze8exr477e1z9 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1682960532-918257 X-HE-Meta: U2FsdGVkX180vxzNDIGyQi+0ZL5TlYar3XZTIT8M/Z+xzI46LoICiYVdFRQsAs6hGp1CXFyhYpMGqgW3BZowAqobnXdT1MjOjdZPcix5o3HYX1Az9PKzyX+u9zD74zZAqnPt6EsBueNHGqlCnAnxZ4UJhQXN32eTW1jyE3Sw66i3TCuNpdMV2oNR5OIyEJ/QnUA0Ixzv9kSEH+HGAZgzU2Coaez1fskkQsOwPPPyf/FXzEN1/2eJb5OfNTb3WC/hOg1Gzznt7QpE4KysPzlFVgMNvZX0VVHKr8B06cpavMrEHy3p4eL+K7KxF0DGAWiCnZ/q1UHwGdxZUzTW275hiiJZ88v8DjdVevi5y5qreqw8s4LL50kp/upGuX/nUjm3LhMGRN7ReSEa+gg+jApfTlV+YfeVLvbMzNnsmJmlkpzVE4BaBQijArjwhSTd2qRN09T0ezfMFFVf6JsvtQvCAzfsZLcKoI8RBtGBaAfaqeoLPmeH72rYEQLUV1925IkGv7bsmZdlM3FlJERQLjggpODMAjAZ+MHcYHScB/ma3Q2m4D4pcBIzDx30s2z/4ePcNKINST5sL6x4B8zKcTMdKEIBLHu0rNTibBsrxtXVL1auqxZBfIjxLa6F+wUQfZi9WkSdUrAds4H0tS6JorVYO5q30NVK3Z4AY4p1SJXuOeR6S21UDG4RzZw3h0LficcmqOWVZ2FT+6p8AhBL4YEyiUZp0CogSIoxL+XuP9IBvmd3dms9ycQVrdLJFbPiPMc+m5psY5IJpWGxAO+0mB7tOgC/0Z64OdSJO3/KGLSNUtzsUwuXMMlMcNn/8UBUKLoX/g0phWsxBu1j0tga7lKb99g3Zpia+b3Gbo8ekXCt0NHZ5hMAP+6c0cIWQnrYVi7ivw1JrAypOqREgNLej5oBN/mEKfkG2IlZZSJ3pPBScYtW6PACljmTsR2HzFq884M174A819g1lAc7lRY59Jd QpfjJa58 4x+jOfkfidJamSdsXOjhw282FBmld+Tu5admQd/jtZP2HUTD9OZyKQKbvq8fxg5APKCBdyY+n4eo0QveSWbmxpw1PQlcBSmIqREp5AHBdOfmx6N7PGww/b4HZpqFQxfLgfsTIJBgRgV5mKsmNmL8cUP6GeMYdeFYENfupEMmCwdRoNYuKJXf+E6m4kteTK++QGXSf3RHM4y5Uf7ipbAeYnOgsdfAUCucWSlDVpkK10vGRzu+fctIWCTHZOObkwx9+N/M0xz6wAD9H0zqplcllZ7HuEGf9ZAb8Qc3g+sEFJWjGHKr+P1+PacYY4hdD5jwGlKzhj/uJQQS+YLfhoiF9vLgzmRgukh7Ik1ljD0m8mIGmKPzwyhHt7CH3Mtz9zTNJe8QyF+UidLMaCwtgiP5F9DhskDpW6rw/uk2oagsRmYEmvIroVj9D7BZcIq9VnJzHn6J6weEqatrxgqa8k/MneVAgJWDvnOmXPU737a7YOoHqbiQTNlkseKWiP3EVRBf7+zxXCr1BZrp/3UFAnwtJKra7+JYpk0Gt7PG0BjKMCRyE/8cfvoIPTXwpyJZG19WzrUimCSnsbx+n8L5K91JV0SMWFRjG9DZQVtm4PEcZWl2jEwjdAUeqQSq4izSdZqTX649o+O4mAuPY10xLT8NXsvIeJOG6xCeLTzYnA/7J4J75F/c= 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: 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 044aa2c9e3cb..4232e7bff549 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -56,6 +56,7 @@ #include #include #include +#include #include #include #include "internal.h" @@ -1249,6 +1250,7 @@ static void free_module(struct module *mod) { trace_module_free(mod); + codetag_unload_module(mod); mod_sysfs_teardown(mod); /* @@ -2974,6 +2976,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); +}