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: 200056 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp183283dyb; Mon, 12 Feb 2024 13:45:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IEDUGo0OcSxQzVW+XFcb8vM03kh3pNmhMfmP6MJ+4ljQZlONmf1vf0VKAi4OFJgqCTxqJvB X-Received: by 2002:a17:906:f352:b0:a3c:b059:6797 with SMTP id hg18-20020a170906f35200b00a3cb0596797mr2207084ejb.49.1707774307468; Mon, 12 Feb 2024 13:45:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707774307; cv=pass; d=google.com; s=arc-20160816; b=VPXme5uKDM7915RM4LW+WxoUM9rxN/Ka2XlSUaadO2JpeYmaPEcrUs2rUexLfgpqyX dCMfmD4IMd8O9XnI3bYRX+NNhr2Q4MsJ7MPt1KgxdHSs2idHNPwhEiPGw1op4MZBJCbs DxzWJDz+Ps4ziIDwDbjIoE+XDyH8RWrfMWQdTsQAf1F/3gaPlhJQMLITCAA5fAGFjqMx vw/GQF7OrOL75W7fRTja1+gk2x6du1iE1Kckq0DN51jvrairi53PJaAN82qm52tkx1Sb 3W2phtJRHd4TnKcf9MZvt3v2NWL7YCi3jxJV1mi3yiIyAcflTqR5Ukq4X0J+YB8SzqCW RiyA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=ZVR5x2YILJK+zhPcdicEuOWU7vdbf7DJDXDz97hru8E=; fh=aN5LZoZgG+4o0ShT6g1ju6FQTURRdo3l5S3xq4EDc00=; b=T81X3hsygEU7dwq/DW9fYAXGbhEVMVbEnWLALiuks3BT4S5mQDHkM1pc5MtHBray4m qt0QBtJ8O1aANlKvkHIsLS3RO5bOlh/jvNh+g/j12JX4AMMFdBIp5uwuFmS2fS2lmUFg 9pkV7CHoImtyAwVLgW3VDfiWpY54T1h3x9osk4k9Y6VGxZyrhRANnNdPjFKbKHcn33V6 6sdUwiP7C+Qrm4wWjz6i7/y/8exBfhyhxZpfpovdENe82c75qVclQOKTv5IKR0bbxG1X ewwWGMlM+2b/LxqnefdhrcpoIzSaNu/BP9nmk3A8OsIfidUlMimPENIzpNFITThjPAoh VFkQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=nDUb4q3h; arc=pass (i=1 spf=pass spfdomain=flex--surenb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-62417-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62417-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCXvCXRhe115Qm7/3GcFBNHBPqK9FkJR70NWMCjhwrGch01RQ3LbL12Re8CJMH2SR+bi2eb9l/p/BhlMFBZ7/5xrPj9mTA== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id ak1-20020a170906888100b00a3c48042a4fsi555280ejc.1030.2024.02.12.13.45.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 13:45:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-62417-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=nDUb4q3h; arc=pass (i=1 spf=pass spfdomain=flex--surenb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-62417-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62417-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id DDA3D1F23BD5 for ; Mon, 12 Feb 2024 21:45:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BA1CF53801; Mon, 12 Feb 2024 21:40:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nDUb4q3h" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79F86535B1 for ; Mon, 12 Feb 2024 21:39:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707774000; cv=none; b=gTPMOxBuaYsvDkvie41c83IWEDjOgrMedbcGU+xoUljwfZ8HHx3ZNuOkqLnHrpSRDDqa4I/bKxPbw0MdfpKioIwunIOlB1Felbp+tllC9ZWFltJtFgBYFhi6vt+Oz53ItZZGLB3txFGbcAEJS+AUBtabanCUclh9ApUFlze7W9c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707774000; c=relaxed/simple; bh=bnowLUS1eDPDWMbsPPvU+2zZa2ZumGLmjqsXpkcz6mc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RdIzpu8b3DlidinbbWd80hWJH4ZaSjOygsLXej9PNyxKpdmGKy1OJ1uQm73izHv7R93KjA7UxTQVo5ASLfNNvwsTEjKc4wvsQVHv6pqFvGJJbXLP+zbwe2/zGVuQ5CDFRrO2C+8qmaWboz17K+c5QJT9SVNllWuiOFwNRehzckk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=nDUb4q3h; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc3645a6790so6760892276.0 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=vger.kernel.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=nDUb4q3hEfOkiAcSvtfrAMbr6eJhgNi0cElHN6yjvuq2vuU+sJewdLwYYAPJFlxFas iZuZxJXMyKj8qjcWHdhxXwTWH2Px7uiEKIEpQfCCLkcRTK2fClwA+d4uc93a593hNBRl nLVFY+AaGTlNTfcRkLxVkaqLES9MaDWl6cc/KKfxXXp6Wn9BA/jWs+Ho+JKMhpjiEdnL Bbl2fu52IgdTFA5rNJB2uELl4BHkumDRu1sjKMVplZWXOPvQxHTD9l7tq5tXVPrmRhVs EIV5tlqvunqdBCvmaOFPZQD/FiQFP/rmAlxtiWyYbS5h7vM+RVbvfguIM/grpdyoCSS0 m4LA== 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=ih5wJI1uhq+clJX/tqvR5SskNJp+DvQx3PVKNQogmViKrtHI9yZaWSAY5gLFxBqh59 T/qKRAlHt/nm6G8j0f44uds9TUrmq1/c+CkT/WCfwn0RKlznbkEJT/QMfatl3MxzY+HV tJ+5j1MEabio/6vgQk5lgK7fjp09VNtcjVsUsJ5onfroaJVTDVyIyOwOQnZZf75UKx4j jf9tXcTTDAGtYDK+qpOSuidyzdFWBtgxWkgTBmm2bAuFniP4Dhv6ivINliTXWrUySYx1 AdfK3X1seg/7F99epENajtdpfPgOBKVhIg5lCstJIP4+rpsKXZOHBfydpd10MmvXcTde 9C2w== X-Gm-Message-State: AOJu0YzFcH3IWQaycGkAJHfbYaeKFH14xmhVhk+HS8Vv3dHGRiJ3LkH6 U9oXtcX+wE8kvTT7+w4mVz1YPem3nrwyUL4B2CnpDnw7bTqOXH9/9L4wwfZOoX/tTS3mpJ+04am xuQ== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790731152188662633 X-GMAIL-MSGID: 1790731152188662633 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); +}