From nobody Mon Feb 9 12:42:52 2026 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (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 D8F943358D3 for ; Fri, 23 Jan 2026 10:28:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769164126; cv=none; b=MY3y7vMxM35bkTmrxUAbZzMOZP7wyMIRzOZe3vADWJ1zwJ7NwEhOAP9euY43ausp1rmNEQrmuEJI2TMCI5iPATi4GzFXmbl7XqUOEaRw3TDa/RsPCE/vfei7mXM0Vtv1BKN69G1CbXruI9hgefwcOfwng+DFvQVicDmtXDW+hoE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769164126; c=relaxed/simple; bh=Tpz05bjoELPQv2eF0UefMJbC3tfLCQg5SYsCoLtcvBo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EnMrGMYL9QVGc8C7vnUB0gguPT8h7cpeAI9fj/qLqG/sS7fag/XtVZtZis4BdyxW1q1T23v2s4GZ2gSvPZu7DGV8DSr4t/ffuE7IvPFQU8G6xWQqt+0lWlkMbPxJswlZGmeROQjKhgK86q0lRmzmgxSz2uqITxktXUXf3VErijk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=IQn045fj; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="IQn045fj" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-4801d24d91bso22571555e9.2 for ; Fri, 23 Jan 2026 02:28:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1769164123; x=1769768923; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=wm5L+uQsZ1ST72VLcpgjOsjeSabHaJSKng460K2rBVU=; b=IQn045fjz883uup81a0GLt6RtZKdoh+E8YqPkUL1p4erQwZlNExejScl5ylLqT1LxG SokFPXth1VRTZIrN1ZAE/+H7BVoxc932/2mPnaA0vN6+hNMvkLyms15QV75TV3Xm2e0P LAJjR0y0yceJ/jWTLcoUjyCgUDbEqYkgu9e0BW/N26WQfQcINyiGI4LZhiL2HF1UMgoe iSfmTRF4eNywK7hSoBGo/cB09y1ZBOVq1yO4ymOlZ+dEvjZ/Zj7//PjLcAH5jn24liS/ /LA4gAkCR9LkIwoPz2auxzlFogucOzEcWcb9mMSQM+7gtsOxOWNvnCoUWbKXS2OthdnL 35rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769164123; x=1769768923; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=wm5L+uQsZ1ST72VLcpgjOsjeSabHaJSKng460K2rBVU=; b=YvI7AiJOzDR6A/eFh1rD3MA161HKD3AgOSVnlv2WL/GNbO+QmH2yarqcSwqVoyCH9/ Xdn+PIpj4Xw4eK6bYd2cVGWv9j7IYUYUXf+7oUQfk33k0Hoc+7ZmMCpMuqWBvLVtZHVX n1SnxUd23na6DjFfeZaUxcnKYIs6T0zdU9NEAYBY2QpAvedRO/B9kDGp5AVL6iiQWKra guTXqcrKZ+opGvNQRxRVoRolEqMQu3AS/FUh1TTCsB5xOxiaSC9yeLwmWlnAjEGQEjgL 0buUiWHDba1s536RGzxClwHenJiCBdyUvqZ9Hmj4AXjHX9sEDv/gcPdsZGoNGTXz+tQj nbCQ== X-Forwarded-Encrypted: i=1; AJvYcCVw/ITDKmYKvS8Rye1DbhkNoOp96xdotYSgTSSS+Tf/254kFElXY07Z0E/enVMqVpDS7cxy2UX5ZHjEeF0=@vger.kernel.org X-Gm-Message-State: AOJu0YzCJZZOc8njlaV8V5mjFnZQrauEGRz57qHtWCNMdmaXsVV/AE57 AJZFVL7TbQxkM8Qu6udWaMgd5NcQV+2XtxE8Qdcxq4NSF2vgv60JYarlCF2B8K0Fw8U= X-Gm-Gg: AZuq6aKLQwKUfmOvNkXAdTb4ctUuyYIo9ew1jKKwQaFkFIlfria1j7qmgsvdTJmLUKU hn+Evdr+VuWKXyNr8N7Qqkn+5MKrUvXSetZZ8CJajjeTd8jny3qF2e2gTMVLI2UQY20wB8puFZL 7DDHbpcztS+pJJnfsVUI/X1qlBTg+CtpyPhMCT9TKzXMm9eTL5VclgjKyAlTIpTKEuU86vxEt9K Er6iOWcnZgjdQcLLp4jmzRCG3v5lsfeqI2mpuCOQPckMRxHi8kMvvK3qSA/5E6Zfr6SNub9/7+p 44OS0u6MkkuTCO5zJYKcuZMTUK2yBw5UMCpNMfWQlbNigoLKEdiHCZy413MRnqwOyqHge54+Qms 7liHFEj1Z+dqC9IOAFgioH8Fl9Rdoc+g2HGkbPByZteh9LAarWPKO9/w2P5qOj2+Sq+uD/N6c4E KbhNikiI29lI3GNkk0OGAOGKPnhGLGIys= X-Received: by 2002:a05:600c:a00d:b0:477:a246:8398 with SMTP id 5b1f17b1804b1-4804c947d28mr43761945e9.2.1769164123303; Fri, 23 Jan 2026 02:28:43 -0800 (PST) Received: from zovi.suse.cz (109-81-1-107.rct.o2.cz. [109.81.1.107]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-480470474cbsm128920065e9.8.2026.01.23.02.28.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jan 2026 02:28:43 -0800 (PST) From: Petr Pavlu To: Josh Poimboeuf , Jiri Kosina , Miroslav Benes , Petr Mladek , Joe Lawrence Cc: Luis Chamberlain , Petr Pavlu , Daniel Gomez , Sami Tolvanen , Aaron Tomlin , Peter Zijlstra , live-patching@vger.kernel.org, linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/2] livepatch: Fix having __klp_objects relics in non-livepatch modules Date: Fri, 23 Jan 2026 11:26:56 +0100 Message-ID: <20260123102825.3521961-2-petr.pavlu@suse.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260123102825.3521961-1-petr.pavlu@suse.com> References: <20260123102825.3521961-1-petr.pavlu@suse.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The linker script scripts/module.lds.S specifies that all input __klp_objects sections should be consolidated into an output section of the same name, and start/stop symbols should be created to enable scripts/livepatch/init.c to locate this data. This start/stop pattern is not ideal for modules because the symbols are created even if no __klp_objects input sections are present. Consequently, a dummy __klp_objects section also appears in the resulting module. This unnecessarily pollutes non-livepatch modules. Instead, since modules are relocatable files, the usual method for locating consolidated data in a module is to read its section table. This approach avoids the aforementioned problem. The klp_modinfo already stores a copy of the entire section table with the final addresses. Introduce a helper function that scripts/livepatch/init.c can call to obtain the location of the __klp_objects section from this data. Signed-off-by: Petr Pavlu Acked-by: Joe Lawrence --- include/linux/livepatch.h | 3 +++ kernel/livepatch/core.c | 19 +++++++++++++++++++ scripts/livepatch/init.c | 20 +++++++++----------- scripts/module.lds.S | 7 +------ 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h index 772919e8096a..ba9e3988c07c 100644 --- a/include/linux/livepatch.h +++ b/include/linux/livepatch.h @@ -175,6 +175,9 @@ int klp_enable_patch(struct klp_patch *); int klp_module_coming(struct module *mod); void klp_module_going(struct module *mod); =20 +void *klp_find_section_by_name(const struct module *mod, const char *name, + size_t *sec_size); + void klp_copy_process(struct task_struct *child); void klp_update_patch_state(struct task_struct *task); =20 diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index 9917756dae46..1acbad2dbfdf 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -1356,6 +1356,25 @@ void klp_module_going(struct module *mod) mutex_unlock(&klp_mutex); } =20 +void *klp_find_section_by_name(const struct module *mod, const char *name, + size_t *sec_size) +{ + struct klp_modinfo *info =3D mod->klp_info; + + for (int i =3D 1; i < info->hdr.e_shnum; i++) { + Elf_Shdr *shdr =3D &info->sechdrs[i]; + + if (!strcmp(info->secstrings + shdr->sh_name, name)) { + *sec_size =3D shdr->sh_size; + return (void *)shdr->sh_addr; + } + } + + *sec_size =3D 0; + return NULL; +} +EXPORT_SYMBOL_GPL(klp_find_section_by_name); + static int __init klp_init(void) { klp_root_kobj =3D kobject_create_and_add("livepatch", kernel_kobj); diff --git a/scripts/livepatch/init.c b/scripts/livepatch/init.c index 2274d8f5a482..9e315fc857bd 100644 --- a/scripts/livepatch/init.c +++ b/scripts/livepatch/init.c @@ -9,19 +9,19 @@ #include #include =20 -extern struct klp_object_ext __start_klp_objects[]; -extern struct klp_object_ext __stop_klp_objects[]; - static struct klp_patch *patch; =20 static int __init livepatch_mod_init(void) { + struct klp_object_ext *obj_exts; + size_t obj_exts_sec_size; struct klp_object *objs; unsigned int nr_objs; int ret; =20 - nr_objs =3D __stop_klp_objects - __start_klp_objects; - + obj_exts =3D klp_find_section_by_name(THIS_MODULE, "__klp_objects", + &obj_exts_sec_size); + nr_objs =3D obj_exts_sec_size / sizeof(*obj_exts); if (!nr_objs) { pr_err("nothing to patch!\n"); ret =3D -EINVAL; @@ -41,7 +41,7 @@ static int __init livepatch_mod_init(void) } =20 for (int i =3D 0; i < nr_objs; i++) { - struct klp_object_ext *obj_ext =3D __start_klp_objects + i; + struct klp_object_ext *obj_ext =3D obj_exts + i; struct klp_func_ext *funcs_ext =3D obj_ext->funcs; unsigned int nr_funcs =3D obj_ext->nr_funcs; struct klp_func *funcs =3D objs[i].funcs; @@ -90,12 +90,10 @@ static int __init livepatch_mod_init(void) =20 static void __exit livepatch_mod_exit(void) { - unsigned int nr_objs; - - nr_objs =3D __stop_klp_objects - __start_klp_objects; + struct klp_object *obj; =20 - for (int i =3D 0; i < nr_objs; i++) - kfree(patch->objs[i].funcs); + klp_for_each_object_static(patch, obj) + kfree(obj->funcs); =20 kfree(patch->objs); kfree(patch); diff --git a/scripts/module.lds.S b/scripts/module.lds.S index 3037d5e5527c..383d19beffb4 100644 --- a/scripts/module.lds.S +++ b/scripts/module.lds.S @@ -35,12 +35,7 @@ SECTIONS { __patchable_function_entries : { *(__patchable_function_entries) } =20 __klp_funcs 0: ALIGN(8) { KEEP(*(__klp_funcs)) } - - __klp_objects 0: ALIGN(8) { - __start_klp_objects =3D .; - KEEP(*(__klp_objects)) - __stop_klp_objects =3D .; - } + __klp_objects 0: ALIGN(8) { KEEP(*(__klp_objects)) } =20 #ifdef CONFIG_ARCH_USES_CFI_TRAPS __kcfi_traps : { KEEP(*(.kcfi_traps)) } --=20 2.52.0