Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp5733870rwr; Mon, 1 May 2023 09:59:06 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6CWL73tXbaVz3ObQfZHjKpdQ4vxkibPSAAXKg+6RC7T58XfqkL4PGZxq78ja1xWiDIECiP X-Received: by 2002:a05:6a20:9189:b0:f5:35f2:400c with SMTP id v9-20020a056a20918900b000f535f2400cmr23467985pzd.4.1682960345805; Mon, 01 May 2023 09:59:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682960345; cv=none; d=google.com; s=arc-20160816; b=A4a8Zfj2snqdV1U9jahNbWtw92V5F8paE7un9DX4yaHvIiTMhvYYgfvkDiAt4AWdAU ssKF+0PPp/8VS49nENVL/9jTu/rYb0a3qU30IkwpoC4uJEPaKhZmbOZutIhIncY9oL1F r97NclZtgklK4Uoc1XxVzI2axyY8fAEKZDndtoKUCH4ykMC3MQ8PDWH4GieiCLcOYfrp 4B2OCXDpk9n2aDRl9+UMEeC14je0mTLom7zvHjHhuLRNESaamrckap5ZBxQP7JGu1uLq fNOj3QUt9Ih7ykodXnq21uU/pJ1BjYztHsQduEqWCAbk7g7fwmhgU8kisuXGsderNxrQ giHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=Fsyg89/vfc29JJa9GS5YhCGnLnEPc8LoOfUR2xEk3nU=; b=OXVItzQD8bjfHekjEpl7CTCGg/PzLNd9Zmr6SuHaWSuarBqqYbeF0Qq/WR8VDZsEAa K7KYAcilGrNQ8j4l2peuELStVKXkIQbKyZ7UfhxFsemFris0bafojtrUtn1vULJc3wjA L3yMh6Hb2sK85o4NoRkzv9v12yHAlcrzwE9wyoSXO4RY9n5kKmYlytfy7KP9wyzLDPdK t/xowFFx2GFgrnE1V6xqmYg6y05U2hpxRQ1pb2BeVIdp2pzdz4DYFB9xXaIxWAD8VP0K qCeX3QtksjFVLwZicdeKlpLtkHQ1SoXrf39TD0Uno0FIpqn+T7uTyrF2idYHnnP4SwEc Z51A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=XugZDvU8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v187-20020a632fc4000000b0050f55427e3dsi28787935pgv.701.2023.05.01.09.58.51; Mon, 01 May 2023 09:59:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=XugZDvU8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232981AbjEAQ6X (ORCPT + 99 others); Mon, 1 May 2023 12:58:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232538AbjEAQ4x (ORCPT ); Mon, 1 May 2023 12:56:53 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C19326AD for ; Mon, 1 May 2023 09:55:47 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-b9a6eeea78cso27814348276.0 for ; Mon, 01 May 2023 09:55:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682960140; x=1685552140; 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=XugZDvU8R0/s4xSWeFHluE62boWwkQ9CTbOENMFYQbMS3l2tyzUCtbQcKbwHigUJIJ OtnAHKBuBjvsP9/stYM2LN0tvR5v6TsP4w8c4UexO5Utfx0bFwgsYC7skVlENgpnJL9H sIxP6b1pDy/fBiWqF/8EcNcaUPUXV9QISrg+weQ/AP/7B06TvSpoas2EJfqmlc3VmYeD jr4T+ZByYleQx4KEZl+bfKD1sOEa1DJKK+WO7dY/hFTgLUuurZ1db5fCtz0miigMUxwU tm4S2IZOyKuiQL4O2pCNIbqdvFVKgmVq6GGIhg+Gt6k3Cilb/R8U19yQnyV0xS5Bv4tA iA/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682960140; x=1685552140; 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=HilXAVcnS8Ym6jzNVE67H7h01DkBtNtmXzGj5N6VfEg5UeNRfWa52q7LDfFLcfWW1s iyt5/W90zMEEpmU/RjKtwDGrZTJShYy5AGx2B7LXHV6z1KnE1bkmYcsgygRUxI0ZwqA7 EBjOrpvz3In+GlMvMkBENWCAvmZaIzAx+jB6+ahvgRukOOotPlEgPOoy/C42P4lRZMVt 1ytgTg660wIv8jB1i1h8RQNnA+uB3KFr5DlR4zNKBNm6rhB31j/ZlYahRNeMOVRtPzzE 2Qo6HtWpayLCwBAkuh3zfX06ANUOWflyefIhjQykjD/MQ9QtM1Z1cLcKU98vYaVoeNNv mBCQ== X-Gm-Message-State: AC+VfDyPLRUjrgLBEZM3OnFBwxLzWQNLKsGcCB1AnqvP4hZYRN8Prb0x Le6MoigbCp5CH0cTBVqkN7ke7IBjoUI= 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); +} -- 2.40.1.495.gc816e09b53d-goog