From patchwork Wed Feb 21 19:40:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13566192 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 99B23C48BEB for ; Wed, 21 Feb 2024 19:41:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8EFBB6B00A1; Wed, 21 Feb 2024 14:41:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 82E7D6B00A3; Wed, 21 Feb 2024 14:41:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 650116B00A2; Wed, 21 Feb 2024 14:41:25 -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 4CAA06B00A0 for ; Wed, 21 Feb 2024 14:41:25 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 24C1940B48 for ; Wed, 21 Feb 2024 19:41:25 +0000 (UTC) X-FDA: 81816830130.23.B1119BB Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf01.hostedemail.com (Postfix) with ESMTP id 675774001E for ; Wed, 21 Feb 2024 19:41:23 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=XuMV7Z2n; spf=pass (imf01.hostedemail.com: domain of 34lHWZQYKCBUDFCz8w19916z.x97638FI-775Gvx5.9C1@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=34lHWZQYKCBUDFCz8w19916z.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=1708544483; 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=XR/SP3Bk5ahA1k2JZ7LXmVl+aTAtC05e65Lb1fPVJe4=; b=RkWj5ZS3Gq3iqR+AyfoNbuTFpwEkFt1V1K4NHwZr74lL292YHcY4RlY+CmEiR9e2+dW6bx J66jy44WDJAvNfkGJhC26eJY0EHBuOtnAjQz3vZmYtEiyIN3UrDX99caIKlxmoPw9sB1b8 bqmXJUDjad2wxClsr2o/zgznqzap/Ug= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708544483; a=rsa-sha256; cv=none; b=hZA6YXMwKkIK5PKa3pSdAC4bfTMUCAW7B/UAJlyJpqaxnWbYOa1ZHveRK92U6N7WVGd9Rc Kt+xcz4VQEbvLGm9q8atRnIo6GDlAQhAenFkjKKPN90O0VCXRIWjYkqJ1m7ERZPym26JKv t+WqhanEIKRKjnZkmfs0krMjhhhtmYE= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=XuMV7Z2n; spf=pass (imf01.hostedemail.com: domain of 34lHWZQYKCBUDFCz8w19916z.x97638FI-775Gvx5.9C1@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=34lHWZQYKCBUDFCz8w19916z.x97638FI-775Gvx5.9C1@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6088fa18619so9936067b3.2 for ; Wed, 21 Feb 2024 11:41:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708544482; x=1709149282; 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=XR/SP3Bk5ahA1k2JZ7LXmVl+aTAtC05e65Lb1fPVJe4=; b=XuMV7Z2n6ZQDiNca7GDZqnDCTIsBmCpCKXGkTzIhhIy4JGWNmJ7/nq+CHLccjKdL2Z O8tNir2EI6F3atvmtX01sW4Ew1YX60fKhZAHKJzEITkjsao8aSOy1fguVjCNDEzfsw4X 132fv73go7COZqpznjuh7JoKSK/4KQQ6uSQTnzZLLCyvb54QLY7iP18zta0SnOGd+8mZ MhrPdlXoO+OI2VwUUbGFj4X4NqnQ7ZAhRNmcvq71/0OUfmJ9hUbBUEbgdWUZz6GwbgCB aXDSmv+q/Igm+C0l3lsqG2cjaSDjDsx1NnJ75Ok5YPQHfZ7wPov1tVwbnIIvC9arMXGH Dz6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708544482; x=1709149282; 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=XR/SP3Bk5ahA1k2JZ7LXmVl+aTAtC05e65Lb1fPVJe4=; b=vxbBHCWSFt5waHmGGKaHiHEwYtfSMV5+yI7mgn79R5xTu2Sg5MamZvCof7sWYLrpuB kNpbAclL7OOh5LP1Xh9ToymknNXhOvJwC8X0lSTWdAxe/Lcw1rN9gvFqOwv2iL5/fY7u SfG797yMxkRV+iNQ24XKDoYZBjEjXMFnCZzDOfMVXghs4adKLqDmu9wSSG+d7qM+b9n6 7HpPhgjYcJdjhkRtA99wmI1gmaNqnloy/f0aeeuNBub3fX7ddUxXyQgRSqLYmYhLSOiG y+OWEzpdXKJnz1AAUCHDnrzg1RmsoMwf7hybVfctYs3AVYc88rvvOX0lc3TfhQQHwr2I +b5g== X-Forwarded-Encrypted: i=1; AJvYcCWNbNT0lvu3njBlmwox4Ood3WfwS9zCz1HA1v4bM5iRDzWzdtyfBDjygAI55porhoxYN3VY3mtPYlafbAq4oxUdM4o= X-Gm-Message-State: AOJu0YxF1y9RBqcdWNDwFZHiNdHkCl6ULbv1bJsPzGhm2GtHGYbQ5I8b qyxuF36KyCulreiABpJl7+KmCNUFJVWDGjK8ryPUrGeJ3/yxGBAkpGHZAZOcNfgwkE+T688wP33 X2A== X-Google-Smtp-Source: AGHT+IEyG+wpxFtVFmzBeD/y9m0tOzDM43NttM6Pb+slw2U8N4KR3QxBKfz+mbBGKekgVDUk4tSk0gcLUqs= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:953b:9a4e:1e10:3f07]) (user=surenb job=sendgmr) by 2002:a25:910:0:b0:dcc:53c6:1133 with SMTP id 16-20020a250910000000b00dcc53c61133mr14837ybj.13.1708544482428; Wed, 21 Feb 2024 11:41:22 -0800 (PST) Date: Wed, 21 Feb 2024 11:40:25 -0800 In-Reply-To: <20240221194052.927623-1-surenb@google.com> Mime-Version: 1.0 References: <20240221194052.927623-1-surenb@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240221194052.927623-13-surenb@google.com> Subject: [PATCH v4 12/36] 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, penguin-kernel@i-love.sakura.ne.jp, 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: 675774001E X-Rspam-User: X-Stat-Signature: m9b4japc3rsrwya3kdh4qycoz9qxx8o3 X-Rspamd-Server: rspam03 X-HE-Tag: 1708544483-772951 X-HE-Meta: U2FsdGVkX18tf47HcHdJzCQZvGYUtoahnEpdmwJQCIABpq9bCc74i2ia53HWoLRfSPksxZ90NLzDOMd4M1r67MJ6Qn16LyZjlq1g8jsOJOMeScL4dEz0WbEJei1XMIfUosUB5Zfh5Rnxg6T3uV2sfwXdeg6MMr926GP8moRteTx6FX01L99lKP1KD7yupyToww89yx/PT5yBtkfkzh+5yM793WCl+WyPDqY9ggNB4EtlH/7RniqJGWlztOA15ocs/mD3LF4K5x+fkYe/RHDR06vRF7rr2XOAhD4jrwm/GS7foy2uzmKDeBI4vOfb4D1flkR7FVEEiMiWk3t+7JCTlnn+B02vRpWd9B8igUP5X0md0tbbOR1/IQz50f8EoVvEMFwhbCLFtH0sn0Vxquhq4C6x9kw3g9L6aVuRnZ0dkB7DV1Sntzu5y23lMK0WbVrILYEIdYrzPqkQG/ekMRa4c/AVRzP4bOkl6BsB/l92W/B9DijOE6v3uge94GRhzvSHJkA9V+j2oYdGBFgV8nVMxdr1Q/J1GDTGPD2kvmW/Jk0C/dy0ykLUdnk28EcCAL1tSD0ZaU8JUe1yQ2jBsadk6Xt4r99DUNve9g0loOtw+Xo+AUvYHzUjjjwdVL0px4kPkgMrhY+7s+g7+tZqb1RXDRpYLmkfoqwzhKSheuHUsmoh/3j0be8NclHZ63a2Nj/T1EXAUSi4fx7iAtO2C6EtHR9AmjtdOREgpRTeK36rMQCz9f+a8aTi8aUaLJi+G6nXfwP9xhFkS74B342USrikqfLsEX8VRW8ATYUtFdzot9wXJVuPWP8uYd3cgpK903y3DlRlTZlVYnPZbwQgPlAwUa0ErxfaapwpTN5k0ik/r7LHlzi5tVhuYbSmzkE6kr6p4rHceYEFR0C+ArdI0B8LTMo4sEjjZhabghZq3wjOtY6VxNFuYzBmeUzfTHCMHcmgL56c4JD3MB+dIIHoLvK z2vJbzF9 gAbHZdDpzEsN0Pgd5Ej77zGJ5cwuWfYA2lU2CV3q/24pFbloOOIi61rUDnQ3Syc55mWx/cMoI2F6/XP0gqbEhPp/eh/cuZ/r5MkhoF3HMKz9p/nqCa6gstXRq1BGQ5Y3ILG6lq6x4kxFkRXVImLAyDpaRpzGIJMqs3C5itds9M1ASE0XlmZeF1nvi6xaTB0KG+lK4/FxvtP2Lxqb8yXh5DkPKUVPcRlx18/L0N+ZccRiuSXlxd3hej4noOCX63p3uEwLU2NhUewKReyWtaGssd4x0KuTculZXay3PuhgEd7ibS72A4UqmAzpuzZ4f9CZFt0kEzkmbez9AP7cO3gNt34jJqR9f2d+Yn0G9uAoRfqqP81bOcss0ULpL7GiqS6yInMmE9ilgxtmjeYfcV8xDpIwNBlsvqauqaU+3 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 7734269cdb63..c44f5b83f24d 100644 --- a/include/linux/codetag.h +++ b/include/linux/codetag.h @@ -33,6 +33,10 @@ union codetag_ref { 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 { @@ -65,4 +69,12 @@ void codetag_to_text(struct seq_buf *out, struct codetag *ct); struct codetag_type * codetag_register_type(const struct codetag_type_desc *desc); +#if defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) +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 8b5b89ad508d..9af22648dbfa 100644 --- a/lib/codetag.c +++ b/lib/codetag.c @@ -124,15 +124,20 @@ static void *get_symbol(struct module *mod, const char *prefix, const char *name { DECLARE_SEQ_BUF(sb, KSYM_NAME_LEN); const char *buf; + void *ret; seq_buf_printf(&sb, "%s%s", prefix, name); if (seq_buf_has_overflowed(&sb)) return NULL; buf = seq_buf_str(&sb); - 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, @@ -173,8 +178,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) { @@ -217,3 +225,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); +}