From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (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 D1F8F265620 for ; Sat, 22 Nov 2025 22:23:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850241; cv=none; b=VA1w23N9xMUUJrMpA9Xr7AA+2p2t8vf/eriGvOLEF5liW/QQfsECY87e8ojN7fg9BODGJTx+noefhaGqXnDTLb4NCdum2+0qYo4IqQlLFED5ZJZDbOxzCuHFs3wHdc98VCCfEsEL1xCKXUQ6ugNOBB1ISsnPWjphazBOIAE1qF4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850241; c=relaxed/simple; bh=3JHb8VX+BStVVujQOo7zw0T1WjffUSPpJl7REIH8YgA=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FNIssRQ1ErVJZucYNNOzt5dmk8bwyQs7foPAwh0CCkAvjR0UleiRSDOelM1hMTy3VVIqsyobjE00N3ss2Pu9ib7Fqug4GzANgvfE+vlEfwCfAFPUJdjeyTEKGTzOfRErbdeDIG/1psMfXXhZWab7BqHfhiwq9TdIl3/wxN5YDUw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=dKLAWaiy; arc=none smtp.client-ip=209.85.128.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="dKLAWaiy" Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-78a7af9ff1dso30356907b3.1 for ; Sat, 22 Nov 2025 14:23:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850239; x=1764455039; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=dNqa2AMoTm2GXKVPNq5SENNKpDOhG7qaiAonHBUruqY=; b=dKLAWaiyIjrbkuub0uVNEpRwUCJ2pCE6OUertFFe/8G1RRJ5ldxpYzhET7kX+Eq80u R/6pkBaak9GVTLeLW0+szHN0ZA7QiKGHf0Nu88QRiDOabYlYC6gN+1Hdzw3DZviNAHMk Ka2Xu0wwWKiZ9550cQkMFTPwCKv3tDIRqXxkoxGauIlKwqRj3Egh6uQQFx+fzzyCHhZX yPeY7JZQ7bLQk/9xofZUem51UiwMG3SL9Gjgp3LcFxgBz/vnmIYO4EJ19lbDdlTFIic5 wbbY6M5ORY0nuCaGA5Cjdnfgd2CV4t2413N65CpdXZwFhkquvi3ATQbsSc2f5hBzy9aT GryA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850239; x=1764455039; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=dNqa2AMoTm2GXKVPNq5SENNKpDOhG7qaiAonHBUruqY=; b=JJEGN5XqJmFOfDw3Kx4JOUy2eiIMTZHnYa0zmyxphnua74QicuE0DiKj5tDj3wc7p5 tkkylPBGOloEznhCcTCKgBUGeJNPpOu0GpZBOTzgdLxvpGcVgR7klxORWq4oJTpin3oY t8gnnHpT1uSVr87QmNVJXHQpQw+4i3qrmhVznswLzJFR54yuBuWR2mvTSk7u0OavvI0P 10Zi8QAorWX4f/xL5zDIMdzhX3iJVS4ruy3fUB/OSwj/wYHwlxZXbV+Q7WekXQHgaeK0 GLKXE1HWuLOqPyHcW/XFaAX+oQlAqIt0hn24HRQOqvJKuaYkEz+s0/TEjt2LPzI01a9E 9ADg== X-Forwarded-Encrypted: i=1; AJvYcCVL1G4AeD698n3gdEBiNeSn07M8mKMve79DElJRPViFAmYCjXUGWkwii/v1WMHYyaF1ewBwz4R1IXqG8YY=@vger.kernel.org X-Gm-Message-State: AOJu0YwW0i1hKsbEdOyRo1+O+zAMJW5ETzxKz3NiQJO7/QIaFN4dXbvg 0j2z+TLCpcykVclB+OwTRxzGUXOOEaOhSbzxUQuvt15vBZJx9BBCc/KfGrYD2imwSpw= X-Gm-Gg: ASbGncutQ3dFU5lxRVrsqsU1OfLh0//TmrlVrBht2qcwAdSuqRk4rsJ/x34IxPNWBuR R85t2fofsDtuXoG2TPYBXNhI4H+aLPGbHa9Uo+cz1w5bxhT4Kgo94F6AaDbnUJ7oOqddA+iBNzV 8UITwPt6TifMFo9EYjIlVuZVDC7oV0X37z1F3uFsu/mXE7kf3H3FZVd7ev3J9bszKyNqDaP2BTc W9aSWpStR05b3u9ubzS65AVMnBxeG0S4d9LOb6EvmhdyB9Jpch/8kMJ3kqbz9m99cDmQ8umWvL9 9e9eQMklMkZQDqqV4tVJAc7mvMQZoSFo+CBHrYys5Xo+RIK/oGDKqGpGg//nncyusvhQX/HmxLK YVzFP5+Txmm045TinlVbZaHs7sN9cD2lTyc0IJYb/LSpiYHlGPAazRbLtB3/Kr9ZSmyKc7nECRG MXJDuq6tVtES3t9ZLvBP52a5FkaB8JogzvwYEPhBtsNS3zc4dLj+odTvJm0nzNox5cVuqlNducc Tac39g= X-Google-Smtp-Source: AGHT+IF+QIPDkZ8c3ehnwAu2ppYoebnLrM6KhSaO+IvE/8Ir8krSEKQs+zG84x7hnCz9/ff0vwyg5w== X-Received: by 2002:a05:690c:4b81:b0:788:1846:875d with SMTP id 00721157ae682-78a8b48b5bdmr59359977b3.29.1763850238778; Sat, 22 Nov 2025 14:23:58 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.23.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:23:58 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 01/22] liveupdate: luo_core: Live Update Orchestrator Date: Sat, 22 Nov 2025 17:23:28 -0500 Message-ID: <20251122222351.1059049-2-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" Introduce LUO, a mechanism intended to facilitate kernel updates while keeping designated devices operational across the transition (e.g., via kexec). The primary use case is updating hypervisors with minimal disruption to running virtual machines. For userspace side of hypervisor update we have copyless migration. LUO is for updating the kernel. This initial patch lays the groundwork for the LUO subsystem. Further functionality, including the implementation of state transition logic, integration with KHO, and hooks for subsystems and file descriptors, will be added in subsequent patches. Create a character device at /dev/liveupdate. A new uAPI header, , will define the necessary structures. The magic number for IOCTL is registered in Documentation/userspace-api/ioctl/ioctl-number.rst. Signed-off-by: Pasha Tatashin Reviewed-by: Pratyush Yadav Reviewed-by: Mike Rapoport (Microsoft) --- .../userspace-api/ioctl/ioctl-number.rst | 2 + include/linux/liveupdate.h | 35 ++++++ include/uapi/linux/liveupdate.h | 46 ++++++++ kernel/liveupdate/Kconfig | 27 +++++ kernel/liveupdate/Makefile | 5 + kernel/liveupdate/luo_core.c | 111 ++++++++++++++++++ 6 files changed, 226 insertions(+) create mode 100644 include/linux/liveupdate.h create mode 100644 include/uapi/linux/liveupdate.h create mode 100644 kernel/liveupdate/luo_core.c diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documenta= tion/userspace-api/ioctl/ioctl-number.rst index 7c527a01d1cf..7232b3544cec 100644 --- a/Documentation/userspace-api/ioctl/ioctl-number.rst +++ b/Documentation/userspace-api/ioctl/ioctl-number.rst @@ -385,6 +385,8 @@ Code Seq# Include File = Comments 0xB8 01-02 uapi/misc/mrvl_cn10k_dpi.h Mar= vell CN10K DPI driver 0xB8 all uapi/linux/mshv.h Mic= rosoft Hyper-V /dev/mshv driver +0xBA 00-0F uapi/linux/liveupdate.h Pas= ha Tatashin + 0xC0 00-0F linux/usb/iowarrior.h 0xCA 00-0F uapi/misc/cxl.h Dea= d since 6.15 0xCA 10-2F uapi/misc/ocxl.h diff --git a/include/linux/liveupdate.h b/include/linux/liveupdate.h new file mode 100644 index 000000000000..c6a1d6bd90cb --- /dev/null +++ b/include/linux/liveupdate.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + */ +#ifndef _LINUX_LIVEUPDATE_H +#define _LINUX_LIVEUPDATE_H + +#include +#include +#include + +#ifdef CONFIG_LIVEUPDATE + +/* Return true if live update orchestrator is enabled */ +bool liveupdate_enabled(void); + +/* Called during kexec to tell LUO that entered into reboot */ +int liveupdate_reboot(void); + +#else /* CONFIG_LIVEUPDATE */ + +static inline bool liveupdate_enabled(void) +{ + return false; +} + +static inline int liveupdate_reboot(void) +{ + return 0; +} + +#endif /* CONFIG_LIVEUPDATE */ +#endif /* _LINUX_LIVEUPDATE_H */ diff --git a/include/uapi/linux/liveupdate.h b/include/uapi/linux/liveupdat= e.h new file mode 100644 index 000000000000..df34c1642c4d --- /dev/null +++ b/include/uapi/linux/liveupdate.h @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ + +/* + * Userspace interface for /dev/liveupdate + * Live Update Orchestrator + * + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + */ + +#ifndef _UAPI_LIVEUPDATE_H +#define _UAPI_LIVEUPDATE_H + +#include +#include + +/** + * DOC: General ioctl format + * + * The ioctl interface follows a general format to allow for extensibility= . Each + * ioctl is passed in a structure pointer as the argument providing the si= ze of + * the structure in the first u32. The kernel checks that any structure sp= ace + * beyond what it understands is 0. This allows userspace to use the backw= ard + * compatible portion while consistently using the newer, larger, structur= es. + * + * ioctls use a standard meaning for common errnos: + * + * - ENOTTY: The IOCTL number itself is not supported at all + * - E2BIG: The IOCTL number is supported, but the provided structure has + * non-zero in a part the kernel does not understand. + * - EOPNOTSUPP: The IOCTL number is supported, and the structure is + * understood, however a known field has a value the kernel does not + * understand or support. + * - EINVAL: Everything about the IOCTL was understood, but a field is not + * correct. + * - ENOENT: A provided token does not exist. + * - ENOMEM: Out of memory. + * - EOVERFLOW: Mathematics overflowed. + * + * As well as additional errnos, within specific ioctls. + */ + +/* The ioctl type, documented in ioctl-number.rst */ +#define LIVEUPDATE_IOCTL_TYPE 0xBA + +#endif /* _UAPI_LIVEUPDATE_H */ diff --git a/kernel/liveupdate/Kconfig b/kernel/liveupdate/Kconfig index a973a54447de..90857dccb359 100644 --- a/kernel/liveupdate/Kconfig +++ b/kernel/liveupdate/Kconfig @@ -1,4 +1,10 @@ # SPDX-License-Identifier: GPL-2.0-only +# +# Copyright (c) 2025, Google LLC. +# Pasha Tatashin +# +# Live Update Orchestrator +# =20 menu "Live Update and Kexec HandOver" depends on !DEFERRED_STRUCT_PAGE_INIT @@ -51,4 +57,25 @@ config KEXEC_HANDOVER_ENABLE_DEFAULT The default behavior can still be overridden at boot time by passing 'kho=3Doff'. =20 +config LIVEUPDATE + bool "Live Update Orchestrator" + depends on KEXEC_HANDOVER + help + Enable the Live Update Orchestrator. Live Update is a mechanism, + typically based on kexec, that allows the kernel to be updated + while keeping selected devices operational across the transition. + These devices are intended to be reclaimed by the new kernel and + re-attached to their original workload without requiring a device + reset. + + Ability to handover a device from current to the next kernel depends + on specific support within device drivers and related kernel + subsystems. + + This feature primarily targets virtual machine hosts to quickly update + the kernel hypervisor with minimal disruption to the running virtual + machines. + + If unsure, say N. + endmenu diff --git a/kernel/liveupdate/Makefile b/kernel/liveupdate/Makefile index f52ce1ebcf86..08954c1770c4 100644 --- a/kernel/liveupdate/Makefile +++ b/kernel/liveupdate/Makefile @@ -1,5 +1,10 @@ # SPDX-License-Identifier: GPL-2.0 =20 +luo-y :=3D \ + luo_core.o + obj-$(CONFIG_KEXEC_HANDOVER) +=3D kexec_handover.o obj-$(CONFIG_KEXEC_HANDOVER_DEBUG) +=3D kexec_handover_debug.o obj-$(CONFIG_KEXEC_HANDOVER_DEBUGFS) +=3D kexec_handover_debugfs.o + +obj-$(CONFIG_LIVEUPDATE) +=3D luo.o diff --git a/kernel/liveupdate/luo_core.c b/kernel/liveupdate/luo_core.c new file mode 100644 index 000000000000..30ad8836360b --- /dev/null +++ b/kernel/liveupdate/luo_core.c @@ -0,0 +1,111 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + */ + +/** + * DOC: Live Update Orchestrator (LUO) + * + * Live Update is a specialized, kexec-based reboot process that allows a + * running kernel to be updated from one version to another while preservi= ng + * the state of selected resources and keeping designated hardware devices + * operational. For these devices, DMA activity may continue throughout the + * kernel transition. + * + * While the primary use case driving this work is supporting live updates= of + * the Linux kernel when it is used as a hypervisor in cloud environments,= the + * LUO framework itself is designed to be workload-agnostic. Live Update + * facilitates a full kernel version upgrade for any type of system. + * + * For example, a non-hypervisor system running an in-memory cache like + * memcached with many gigabytes of data can use LUO. The userspace service + * can place its cache into a memfd, have its state preserved by LUO, and + * restore it immediately after the kernel kexec. + * + * Whether the system is running virtual machines, containers, a + * high-performance database, or networking services, LUO's primary goal i= s to + * enable a full kernel update by preserving critical userspace state and + * keeping essential devices operational. + * + * The core of LUO is a mechanism that tracks the progress of a live updat= e, + * along with a callback API that allows other kernel subsystems to partic= ipate + * in the process. Example subsystems that can hook into LUO include: kvm, + * iommu, interrupts, vfio, participating filesystems, and memory manageme= nt. + * + * LUO uses Kexec Handover to transfer memory state from the current kerne= l to + * the next kernel. For more details see + * Documentation/core-api/kho/concepts.rst. + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include + +static struct { + bool enabled; +} luo_global; + +static int __init early_liveupdate_param(char *buf) +{ + return kstrtobool(buf, &luo_global.enabled); +} +early_param("liveupdate", early_liveupdate_param); + +/* Public Functions */ + +/** + * liveupdate_reboot() - Kernel reboot notifier for live update final + * serialization. + * + * This function is invoked directly from the reboot() syscall pathway + * if kexec is in progress. + * + * If any callback fails, this function aborts KHO, undoes the freeze() + * callbacks, and returns an error. + */ +int liveupdate_reboot(void) +{ + return 0; +} + +/** + * liveupdate_enabled - Check if the live update feature is enabled. + * + * This function returns the state of the live update feature flag, which + * can be controlled via the ``liveupdate`` kernel command-line parameter. + * + * @return true if live update is enabled, false otherwise. + */ +bool liveupdate_enabled(void) +{ + return luo_global.enabled; +} + +struct luo_device_state { + struct miscdevice miscdev; +}; + +static const struct file_operations luo_fops =3D { + .owner =3D THIS_MODULE, +}; + +static struct luo_device_state luo_dev =3D { + .miscdev =3D { + .minor =3D MISC_DYNAMIC_MINOR, + .name =3D "liveupdate", + .fops =3D &luo_fops, + }, +}; + +static int __init liveupdate_ioctl_init(void) +{ + if (!liveupdate_enabled()) + return 0; + + return misc_register(&luo_dev.miscdev); +} +late_initcall(liveupdate_ioctl_init); --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yx1-f42.google.com (mail-yx1-f42.google.com [74.125.224.42]) (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 A6DCE2BE7C0 for ; Sat, 22 Nov 2025 22:24:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850244; cv=none; b=m8DEz0xTvYouAHsiWQjsNfgt/LIOfLhSgRaqO1JvXkpQc4zlYVofIGWLLXGASut53eyJyn14F58ot5SdxiHBDr6WneJMKkyIw9Lw89/+X4o6fMuN3Oiq0IlB/fXxIsgg+x0aEHVr1mnnjlAFrPMQbgnDY8pgBFqesLWm+5onAgc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850244; c=relaxed/simple; bh=3nfw+zx4IScFJ5mrBLPkQXCSS1yFCVNe+3qg09HSg7s=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IHlPIyCUq5iRcXXBQ5wRYiB12yxfaR6M5KTeAvkLJL0+/14fSKFdJd0fq2XP7XVwISWaiCNPrZwttN7GXJhXGjB/dyz6iXGd0pnpzD7XhRxOHxzYl5LslxQ5rZU6PGolECRq00Z4ibFM7GL3UpChHSf+yoWDRxCT8AqcWOGS4Ss= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=F4gTOmRE; arc=none smtp.client-ip=74.125.224.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="F4gTOmRE" Received: by mail-yx1-f42.google.com with SMTP id 956f58d0204a3-63f97c4eccaso2799723d50.2 for ; Sat, 22 Nov 2025 14:24:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850241; x=1764455041; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=ou+OqCZKXj1OdOlQOs0wG/rcVUwC/PJeGlhuLikCmu4=; b=F4gTOmREaeJok/wBkRc0u4mkmNQe/rCEviTvJ3wCwpsTZSyIJ/Q+HgejCeZhaQoWRi ZJekCQrE+XyAdaaaV2cLOHfHfkp8xuUSN8ktEkpYIpheZDDeYk9xFlZahCEApwrP1iow ZUmRqcDZV158RHaUAo7T44Y7tc1mai/OTQbdQdfk2tnXDGDW3V0f5CZj5Xj49Hv5loCp LBh+2s09+m3I0PELJDTw2c0FnTQe5VQeKgkUNCcrcGEgTwPzmt8lvM7c3wbY/BZ9NuUf EYFy9eY/Dimn/4WjMRFQFOdgceLQ+vxA0GTEhsKHh3ex+eKaYHpNBwSZfA0jokbXtMN9 foVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850241; x=1764455041; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=ou+OqCZKXj1OdOlQOs0wG/rcVUwC/PJeGlhuLikCmu4=; b=Fm97rPz4BEeO9BZ2ixRVEAZdmF6nGdcQJJhVnPYfcs0PQ2QJGzwTnz3+xIrWi23mw+ LoT4rtnLT6Pi5JrPjchzPN3vyzPhaPCYmnJCuM2+x9MAx8HCC5BLC7D+IeUuNSi6jiB8 hWXVAP6NU3qIAfQil0xktFkjtxJRekZHMIThcLTl98arQW261TO6DV8qvpv1LYk22kcH W6azjBonXvxcPsmAKhLdCu8kh2koquDeA2BA/PmpFyD3trwl5wk0tYCfYih1ES1Hktn2 iPCYYCb3MQCX6f0BAN+wQ8Vlhc/NXIGjSliQDICTDAFpPZPApBM6qMjkhMcBKacYgV8Z Cp8g== X-Forwarded-Encrypted: i=1; AJvYcCWTy9STq9W/mMcHwmR23Gow/16gHc5QnssF/Ew4DvWWGYNqiA4ZiAkotHSkLMXxBMGdxzxa7ka4lSgoFyg=@vger.kernel.org X-Gm-Message-State: AOJu0YyUacOZhwLrLCYyPLF97yKUz4GhVyhGuu8laGiDSfAQiYuIxxaL qFngpTGHuFy6Mydgu+7Xrf0/NjYkZvmnUBCRopAOYaTsv3tYDgTAJCZNzkqQvL/Y97I= X-Gm-Gg: ASbGncs85xsEQxCQFbwY4hgYfvR+ctgtjkOImhscCBo8t68oCLQ9MKsJyVnS0SJhrBb o6C3F6dUMviPSJqfhyFkRZPVHXMx6vWaDatJknAlDUH7LABb2IFPl+eDuRtUE2B5Zi8OrvD/Ylc AIV3RjAPI8+rPUOD25kyLOlkrmkiuAhGq2jFbgxmgpe8kIUhqbibpTQ6ZXPFD+HLqx0/qOoQVpy BFZ7vawTqLRSiKyDGPlXYbSVBuHiFXVlX0UnwFw/SiJOlRDcXR1BfL954JZQrDGj04yIZe1XrPi A88xRmwbdo19Fkb2+B9js5I7gttxn4VTuOHBcjTeJdhmpND8iwYWmGmpDrp0w28O0hmBVeLP1oW 8gLepiJVld3gSJoxSFD1bgqKekFunc49RWGcN5xah2Uxp8BtIZko/mZaAOt8C2X/uptfbdbCxXu ygSPuKeRVHeFNXDOsaIIKJii4DhyRXY5TiQhGQOnJEqVDqkMZiGUxRHlKwVwomLG3KC/Iw157WH /HqSJo= X-Google-Smtp-Source: AGHT+IGfZof8qUF1IkZZI7c6YLOTPv1WiDaxFjl7jw8fTLuhBRkKSr+RcqIexB8nICTyaMONoMz9vw== X-Received: by 2002:a05:690e:d08:b0:641:e826:bb71 with SMTP id 956f58d0204a3-643029b20e8mr4919920d50.0.1763850240618; Sat, 22 Nov 2025 14:24:00 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.23.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:00 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 02/22] liveupdate: luo_core: integrate with KHO Date: Sat, 22 Nov 2025 17:23:29 -0500 Message-ID: <20251122222351.1059049-3-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" Integrate the LUO with the KHO framework to enable passing LUO state across a kexec reboot. This patch implements the lifecycle integration with KHO: 1. Incoming State: During early boot (`early_initcall`), LUO checks if KHO is active. If so, it retrieves the "LUO" subtree, verifies the "luo-v1" compatibility string, and reads the `liveupdate-number` to track the update count. 2. Outgoing State: During late initialization (`late_initcall`), LUO allocates a new FDT for the next kernel, populates it with the basic header (compatible string and incremented update number), and registers it with KHO (`kho_add_subtree`). 3. Finalization: The `liveupdate_reboot()` notifier is updated to invoke `kho_finalize()`. This ensures that all memory segments marked for preservation are properly serialized before the kexec jump. LUO now depends on `CONFIG_KEXEC_HANDOVER`. Signed-off-by: Pasha Tatashin Reviewed-by: Pratyush Yadav --- include/linux/kho/abi/luo.h | 54 +++++++++++ kernel/liveupdate/luo_core.c | 154 ++++++++++++++++++++++++++++++- kernel/liveupdate/luo_internal.h | 22 +++++ 3 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 include/linux/kho/abi/luo.h create mode 100644 kernel/liveupdate/luo_internal.h diff --git a/include/linux/kho/abi/luo.h b/include/linux/kho/abi/luo.h new file mode 100644 index 000000000000..8523b3ff82d1 --- /dev/null +++ b/include/linux/kho/abi/luo.h @@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + */ + +/** + * DOC: Live Update Orchestrator ABI + * + * This header defines the stable Application Binary Interface used by the + * Live Update Orchestrator to pass state from a pre-update kernel to a + * post-update kernel. The ABI is built upon the Kexec HandOver framework + * and uses a Flattened Device Tree to describe the preserved data. + * + * This interface is a contract. Any modification to the FDT structure, no= de + * properties, compatible strings, or the layout of the `__packed` seriali= zation + * structures defined here constitutes a breaking change. Such changes req= uire + * incrementing the version number in the relevant `_COMPATIBLE` string to + * prevent a new kernel from misinterpreting data from an old kernel. + * + * FDT Structure Overview: + * The entire LUO state is encapsulated within a single KHO entry named = "LUO". + * This entry contains an FDT with the following layout: + * + * .. code-block:: none + * + * / { + * compatible =3D "luo-v1"; + * liveupdate-number =3D <...>; + * }; + * + * Main LUO Node (/): + * + * - compatible: "luo-v1" + * Identifies the overall LUO ABI version. + * - liveupdate-number: u64 + * A counter tracking the number of successful live updates performed. + */ + +#ifndef _LINUX_KHO_ABI_LUO_H +#define _LINUX_KHO_ABI_LUO_H + +/* + * The LUO FDT hooks all LUO state for sessions, fds, etc. + * In the root it also carries "liveupdate-number" 64-bit property that + * corresponds to the number of live-updates performed on this machine. + */ +#define LUO_FDT_SIZE PAGE_SIZE +#define LUO_FDT_KHO_ENTRY_NAME "LUO" +#define LUO_FDT_COMPATIBLE "luo-v1" +#define LUO_FDT_LIVEUPDATE_NUM "liveupdate-number" + +#endif /* _LINUX_KHO_ABI_LUO_H */ diff --git a/kernel/liveupdate/luo_core.c b/kernel/liveupdate/luo_core.c index 30ad8836360b..9f9fe9a81b29 100644 --- a/kernel/liveupdate/luo_core.c +++ b/kernel/liveupdate/luo_core.c @@ -41,12 +41,26 @@ =20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 +#include +#include +#include #include +#include #include #include +#include +#include +#include +#include + +#include "kexec_handover_internal.h" +#include "luo_internal.h" =20 static struct { bool enabled; + void *fdt_out; + void *fdt_in; + u64 liveupdate_num; } luo_global; =20 static int __init early_liveupdate_param(char *buf) @@ -55,6 +69,129 @@ static int __init early_liveupdate_param(char *buf) } early_param("liveupdate", early_liveupdate_param); =20 +static int __init luo_early_startup(void) +{ + phys_addr_t fdt_phys; + int err, ln_size; + const void *ptr; + + if (!kho_is_enabled()) { + if (liveupdate_enabled()) + pr_warn("Disabling liveupdate because KHO is disabled\n"); + luo_global.enabled =3D false; + return 0; + } + + /* Retrieve LUO subtree, and verify its format. */ + err =3D kho_retrieve_subtree(LUO_FDT_KHO_ENTRY_NAME, &fdt_phys); + if (err) { + if (err !=3D -ENOENT) { + pr_err("failed to retrieve FDT '%s' from KHO: %pe\n", + LUO_FDT_KHO_ENTRY_NAME, ERR_PTR(err)); + return err; + } + + return 0; + } + + luo_global.fdt_in =3D phys_to_virt(fdt_phys); + err =3D fdt_node_check_compatible(luo_global.fdt_in, 0, + LUO_FDT_COMPATIBLE); + if (err) { + pr_err("FDT '%s' is incompatible with '%s' [%d]\n", + LUO_FDT_KHO_ENTRY_NAME, LUO_FDT_COMPATIBLE, err); + + return -EINVAL; + } + + ln_size =3D 0; + ptr =3D fdt_getprop(luo_global.fdt_in, 0, LUO_FDT_LIVEUPDATE_NUM, + &ln_size); + if (!ptr || ln_size !=3D sizeof(luo_global.liveupdate_num)) { + pr_err("Unable to get live update number '%s' [%d]\n", + LUO_FDT_LIVEUPDATE_NUM, ln_size); + + return -EINVAL; + } + + luo_global.liveupdate_num =3D get_unaligned((u64 *)ptr); + pr_info("Retrieved live update data, liveupdate number: %lld\n", + luo_global.liveupdate_num); + + return 0; +} + +static int __init liveupdate_early_init(void) +{ + int err; + + err =3D luo_early_startup(); + if (err) { + luo_global.enabled =3D false; + luo_restore_fail("The incoming tree failed to initialize properly [%pe],= disabling live update\n", + ERR_PTR(err)); + } + + return err; +} +early_initcall(liveupdate_early_init); + +/* Called during boot to create outgoing LUO fdt tree */ +static int __init luo_fdt_setup(void) +{ + const u64 ln =3D luo_global.liveupdate_num + 1; + void *fdt_out; + int err; + + fdt_out =3D kho_alloc_preserve(LUO_FDT_SIZE); + if (IS_ERR(fdt_out)) { + pr_err("failed to allocate/preserve FDT memory\n"); + return PTR_ERR(fdt_out); + } + + err =3D fdt_create(fdt_out, LUO_FDT_SIZE); + err |=3D fdt_finish_reservemap(fdt_out); + err |=3D fdt_begin_node(fdt_out, ""); + err |=3D fdt_property_string(fdt_out, "compatible", LUO_FDT_COMPATIBLE); + err |=3D fdt_property(fdt_out, LUO_FDT_LIVEUPDATE_NUM, &ln, sizeof(ln)); + err |=3D fdt_end_node(fdt_out); + err |=3D fdt_finish(fdt_out); + if (err) + goto exit_free; + + err =3D kho_add_subtree(LUO_FDT_KHO_ENTRY_NAME, fdt_out); + if (err) + goto exit_free; + luo_global.fdt_out =3D fdt_out; + + return 0; + +exit_free: + kho_unpreserve_free(fdt_out); + pr_err("failed to prepare LUO FDT: %d\n", err); + + return err; +} + +/* + * late initcall because it initializes the outgoing tree that is needed o= nly + * once userspace starts using /dev/liveupdate. + */ +static int __init luo_late_startup(void) +{ + int err; + + if (!liveupdate_enabled()) + return 0; + + err =3D luo_fdt_setup(); + if (err) + luo_global.enabled =3D false; + + return err; +} +late_initcall(luo_late_startup); + /* Public Functions */ =20 /** @@ -69,7 +206,22 @@ early_param("liveupdate", early_liveupdate_param); */ int liveupdate_reboot(void) { - return 0; + int err; + + if (!liveupdate_enabled()) + return 0; + + err =3D kho_finalize(); + if (err) { + pr_err("kho_finalize failed %d\n", err); + /* + * kho_finalize() may return libfdt errors, to aboid passing to + * userspace unknown errors, change this to EAGAIN. + */ + err =3D -EAGAIN; + } + + return err; } =20 /** diff --git a/kernel/liveupdate/luo_internal.h b/kernel/liveupdate/luo_inter= nal.h new file mode 100644 index 000000000000..8612687b2000 --- /dev/null +++ b/kernel/liveupdate/luo_internal.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + */ + +#ifndef _LINUX_LUO_INTERNAL_H +#define _LINUX_LUO_INTERNAL_H + +#include + +/* + * Handles a deserialization failure: devices and memory is in unpredictab= le + * state. + * + * Continuing the boot process after a failure is dangerous because it cou= ld + * lead to leaks of private data. + */ +#define luo_restore_fail(__fmt, ...) panic(__fmt, ##__VA_ARGS__) + +#endif /* _LINUX_LUO_INTERNAL_H */ --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f173.google.com (mail-yw1-f173.google.com [209.85.128.173]) (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 B13BE2D060B for ; Sat, 22 Nov 2025 22:24:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850246; cv=none; b=IV+xKgKtqTxSwTyMtZWazJ1NY/cynzQYvjTYWWa+PlD6vce8KtqninSyyhg/nZsHgt7sS3s92qgJzGMPtMoCF27SDL6bvjL+m4571PUGnVdFYpIzn+Li2BpoMSxNvodokrZ86828S6z5cDurX9Io+iE/8EOUGKgG1qNxNnqHScw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850246; c=relaxed/simple; bh=yqW0eNnuY6iaIAb81qp9+q3ixuk1RWq84gZvp3pOj+Q=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cam8skFeYZlGB9oeEWBipENjfqbTj6ScoAcF6cHuDYBiXbIy1htEsfvuoPX8K8CsJZ0atHm2W2eiE0bEL1AYwmzaTpCH67Hp5zg3fozljiHEFBLnat3zZ9mav4arfPDnkotIHfs9b8gRHz4HjuytiVNzt8khfUWNq8nX1lqd8TM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=ahw62As7; arc=none smtp.client-ip=209.85.128.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="ahw62As7" Received: by mail-yw1-f173.google.com with SMTP id 00721157ae682-78a7af9fe4fso33199217b3.2 for ; Sat, 22 Nov 2025 14:24:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850242; x=1764455042; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=I5KiW0twGUwiDqt6nePE1kGiL7dytg0Q0KVXgthRcks=; b=ahw62As7z5Pp8+KVpJHLR9K98pdIlj4hIScu5BSMiqHYogLBwdt0Z3cU0D8d6e4cQt KEdBqtYBy+hQMXiTLQf73825DkW9KUJlGFg5wrca2+TTLZI1Ur4KXZqPrj5A1aLjpM3v w3L1ODvFkjxH01tstd9dWyuBICRTf96ckT7HYnN7rCREQ5ruCucOR91nlwm7W0CGPePH yoQmUtOBjgdVuBMaCbFfF4thHh/bOOVJImlmIY2YqT0YGOqj2UzZ2AZpVIixd38MOMNp UVM5X1Dlvuxe3qHPkEuvpKUCjkPTOehJIUCBlwzitQsR1h2Q4l7ayDq+dCI99jJVKS0t +7Kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850242; x=1764455042; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=I5KiW0twGUwiDqt6nePE1kGiL7dytg0Q0KVXgthRcks=; b=gBPFIxOYLQ78XdjFkCk85c2O3Oc0fm7cGe27xgA3wKKtysUC2JPGiGnL714XIioeUy XAPYimjwxGtDwGYgTgI5L2VyA2uKOy6pAgSXV9vt2Od88byqOf3RSDwna20+X49I7YNQ FFo6iqq6znrH2T4S9Hu7/mqyXz7JW2OeLjcq7Bc0wXH+2PJxCT42/d36plQxrEcm8tRH dWbPl2cthhvkEsGFaTYqA/0XB7BotI102cJQfWLlkhrPu6F3ZPSaSZHOlIGFV7yCV1c/ MyziRAQvd3etmFLP2bvDQw7c13oc8SP66D103dlFmLchrHe5yHHS6CKqyQarwRxMQZo/ CsoQ== X-Forwarded-Encrypted: i=1; AJvYcCWhH7ZDimC030eG5aGS3kl70U76q2vOV8QXY0FCbMsOxF8m5Lr4FG5TT5mFfO7SJ2hkHOzbvQgZcl2B5CQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyctNdWDec6G+6kMYFx+Y/GYDSHh9HkRHj0rcH7hxGwhPMmK6XE ppxsBPkLLjLgnh9mxd4gxRWPmCu2Afr+A2sJHyk7bQA3NhqYXMwdqMqS6W0iA5JV32k= X-Gm-Gg: ASbGncsxKHpzZGo6kp6yPLsTIKjHZhkgVNPoqt7tEGROgTGelkGEiSZJKm5C6uyfOAd Ao7IJ2gCYsi7OnF33ZTOJmF0ceRrh0+ZLR00nazmS7SPh2X7NsUNsT5TSmvjCWwiHGU+gv2BLPk oea//P5pC1pPdq59tihu6utaK6Tvjcl/mFwMM4XRJeB9SqFGOJlqlXG/x0Ze3XKB4YjIjYvwFxc wvT8k17V6wR6EIrecI7dT3vILFLZ6PEE7+or9zWLshdS2W9wef8Ms3whCS7zEw9VzHS1VlglStP 1Mg5oNiST/Sxj5/a2fcytDBHlNgI91ni9LuWD/jScbcAyAaatsw9QgFJ7eYvvpgZ1Zlmp5Hyou1 QASxCl5WRsesiZPcQx30dfCXTmxqWRJduKkD4rmzilBfvt9a3jTtoNUOVdS7mC+nG32aQnFG0vm E9rQZAzGxj1pz9UbSgffVO+xKRO/48w0ugkj2YHCOTtGmrtj5y/Y4V8wnW58mcgEI1SmJhCKNLq ZXre1A= X-Google-Smtp-Source: AGHT+IHuoY23QTvRNhRkhmDmGHRrDBpwbesFLAGgRZdemrA8LoJ/P/EIeiLQ/hTmePkctRQeLMtVhA== X-Received: by 2002:a05:690e:130d:b0:63f:abf6:1c9 with SMTP id 956f58d0204a3-64302a4b3fdmr5160190d50.25.1763850242430; Sat, 22 Nov 2025 14:24:02 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:02 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 03/22] kexec: call liveupdate_reboot() before kexec Date: Sat, 22 Nov 2025 17:23:30 -0500 Message-ID: <20251122222351.1059049-4-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" Modify the kernel_kexec() to call liveupdate_reboot(). This ensures that the Live Update Orchestrator is notified just before the kernel executes the kexec jump. The liveupdate_reboot() function triggers the final freeze event, allowing participating FDs perform last-minute check or state saving within the blackout window. If liveupdate_reboot() returns an error (indicating a failure during LUO finalization), the kexec operation is aborted to prevent proceeding with an inconsistent state. An error is returned to user. Signed-off-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) Reviewed-by: Pratyush Yadav --- kernel/kexec_core.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index a8890dd03a1d..3122235c225b 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -1145,6 +1146,10 @@ int kernel_kexec(void) goto Unlock; } =20 + error =3D liveupdate_reboot(); + if (error) + goto Unlock; + #ifdef CONFIG_KEXEC_JUMP if (kexec_image->preserve_context) { /* --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (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 7A0712D6409 for ; Sat, 22 Nov 2025 22:24:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850250; cv=none; b=a8yZQbgVtJfT2KX/C75GCNbpRX2P5gnzOOx4WJku13+4R+HwcSnwKhgXoLOHmn5vZUp4LGKOthm2Gjyf4QQDdfqAzFXFHQP0V15y22GK2+kG+GwPecxGSWKAt3t7B5H0/V426jksPoGkBkv1/sTUAvHazHw7OtH0VXKptc7Nzkg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850250; c=relaxed/simple; bh=cgPKVK0mI/a8cjxeuMSi5z9QJUbnDqOWgRrusyvIGpg=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fcIqkggTVLQkuCN7HsvuhpgHR3SdtQZi2hhRcANI66rM3+MQZJFzalsE1toLcE/4FD5inv5ZtxR6/Z4jpQJlHXjJonnbUUMugYUfHB0uJxBorIxwFix8ACcTHGlF+ieliz+yetk1scA3HlqwINVE0KelpJRgpzpu9HVi2kfl3SI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=if5Eigtx; arc=none smtp.client-ip=209.85.128.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="if5Eigtx" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-787e84ceaf7so32792337b3.2 for ; Sat, 22 Nov 2025 14:24:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850244; x=1764455044; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=f97FmhRHLdKBFhVEdbeaZSyOpcaCVspu56X5lhG/iHw=; b=if5EigtxwHwibHNKCEKHx5CK5G51Ji7jZxP3twDfpkFbwwQFlX2Ootvkea4sOZmWR/ e7iXTDRLLPQjcbPdNR1YuMbyql+E0j25czpmJrOdKFqIwyrBZdMpPjTzlxNLixdDYXkr miJD3+g16F2Iu4WF1XJ9MCcA90phIIDtcn6U/k8MjfdhUAav0OjDa9tdXfyPsVFwp8Ks iPaCm40jGU+gt3Lz3Xj1GTo8u8is0BkCmU0NNYjknUcbYvXYHPYJT529NCBwOpnS4lIR lM444YSsT9zIUlhmC6K4yeMHSiKhNatzUx03qEcvp+7SezTqJpAZHurljPpU1PwFxpzU drvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850244; x=1764455044; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=f97FmhRHLdKBFhVEdbeaZSyOpcaCVspu56X5lhG/iHw=; b=u55nzDqiRRug/CmD/AhgO66agUTK5DOksvpLBvlXuKIQFpaMiQTrv3effCzMwNNsvk KF83rvDl/F1wjc2/lfsOh4e6fRWOFbTMm8gmpZF++VP6N9jIJ4uUPuQnPmtC46Z6N4Sh qpWUYPfGzG1LPo9abDnWaDIOCwqio9ixJkzUI40U/vb8MDr4dgiKjT1fkWj2ln4xXX1D Ph9aemseIlc0Wku70hj1KeyfQWsjqL0y/Z7/OI8Xg0a72KjucWpoL/lKKEcYlcwiW4JO DR4zX0uCEmkO30fgb1bOzD1CXiA+hkkz7ujNf3DERS99ePJ7yNAElHNo61YbkOD9ybTl VDjg== X-Forwarded-Encrypted: i=1; AJvYcCUfJMTMH0Ah5zrZpcXRxuJ6fCenllZFDttZei223JkESsZcC59V9M2VW5VIevMYOb4iEutRcqKTf3+jxuk=@vger.kernel.org X-Gm-Message-State: AOJu0Yz/xMFWwc7+H7nJ8ygebZNn8o0OIWrSXtdeoNEZ6B8mPMHIJCuv wQIHAEvFHrRiyQb0TFfurKmLgVzlx9MfGQK32gsD5j6CoobgR4yNH9EnuEIFi8YraHI= X-Gm-Gg: ASbGncvA69d6MQb9Sdgp2GjGqtmbMN1P1IiNjRkx7OFtAv2N1GH9H4XxU3TJuaPvAeW g8TwB+OLU2wW6hMov/RgyDwp357gnsqxC7Qftl2JE6dnhChq7v0cokAQIs1ZSEoIRRRzm/JfxBS D3JpuLSFexzEcYQq6rbIi5ToJt1C9NWx+y96Mw+ZgC4RDaetynQgW0ZixTmwVdzaDG4Dhdghx3A vLHSG5MNa4CLts2dXlxDsM4GWJlXcUSd+mfe5uK9H4bw2lajFxleYopipP5ps8VGw0lm/LK7U2V fpIh2w4W2nMs70NLHi/BZXMeapDU9h04iXKv/La4oS7wQxqHgHiklZT62BhtBRCXT0fw8QkqPkQ 21t4iMWPyLWgW9JYWJ/ePyW8psiMXYCEITZPHJM7sAKF53UUa06eqiVXef8i+K6SDDGdCz32Czv p/V3B+qzma3edcTai0iBuX4NJiuvmdAVAL++oJs1EMd+gKFp8NmhgPexIpxY+B1k7Zjq9E1gsDk 4o4dcI= X-Google-Smtp-Source: AGHT+IGnrzCEuuylhqPkAjSlcdsyrO4o6aqtyMRDVxZk2R8kYrvFUB/uDbfsoHva1q9rBA+u1Xb6WA== X-Received: by 2002:a53:5003:0:b0:641:f5bc:6998 with SMTP id 956f58d0204a3-64302b38722mr3621491d50.84.1763850244299; Sat, 22 Nov 2025 14:24:04 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:03 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 04/22] liveupdate: luo_session: add sessions support Date: Sat, 22 Nov 2025 17:23:31 -0500 Message-ID: <20251122222351.1059049-5-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" Introduce concept of "Live Update Sessions" within the LUO framework. LUO sessions provide a mechanism to group and manage `struct file *` instances (representing file descriptors) that need to be preserved across a kexec-based live update. Each session is identified by a unique name and acts as a container for file objects whose state is critical to a userspace workload, such as a virtual machine or a high-performance database, aiming to maintain their functionality across a kernel transition. This groundwork establishes the framework for preserving file-backed state across kernel updates, with the actual file data preservation mechanisms to be implemented in subsequent patches. Signed-off-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) Reviewed-by: Pratyush Yadav --- include/linux/kho/abi/luo.h | 71 +++++ include/uapi/linux/liveupdate.h | 3 + kernel/liveupdate/Makefile | 3 +- kernel/liveupdate/luo_core.c | 9 + kernel/liveupdate/luo_internal.h | 29 ++ kernel/liveupdate/luo_session.c | 462 +++++++++++++++++++++++++++++++ 6 files changed, 576 insertions(+), 1 deletion(-) create mode 100644 kernel/liveupdate/luo_session.c diff --git a/include/linux/kho/abi/luo.h b/include/linux/kho/abi/luo.h index 8523b3ff82d1..a2d2940eca6b 100644 --- a/include/linux/kho/abi/luo.h +++ b/include/linux/kho/abi/luo.h @@ -28,6 +28,11 @@ * / { * compatible =3D "luo-v1"; * liveupdate-number =3D <...>; + * + * luo-session { + * compatible =3D "luo-session-v1"; + * luo-session-header =3D ; + * }; * }; * * Main LUO Node (/): @@ -36,11 +41,37 @@ * Identifies the overall LUO ABI version. * - liveupdate-number: u64 * A counter tracking the number of successful live updates performed. + * + * Session Node (luo-session): + * This node describes all preserved user-space sessions. + * + * - compatible: "luo-session-v1" + * Identifies the session ABI version. + * - luo-session-header: u64 + * The physical address of a `struct luo_session_header_ser`. This str= ucture + * is the header for a contiguous block of memory containing an array = of + * `struct luo_session_ser`, one for each preserved session. + * + * Serialization Structures: + * The FDT properties point to memory regions containing arrays of simpl= e, + * `__packed` structures. These structures contain the actual preserved = state. + * + * - struct luo_session_header_ser: + * Header for the session array. Contains the total page count of the + * preserved memory block and the number of `struct luo_session_ser` + * entries that follow. + * + * - struct luo_session_ser: + * Metadata for a single session, including its name and a physical po= inter + * to another preserved memory block containing an array of + * `struct luo_file_ser` for all files in that session. */ =20 #ifndef _LINUX_KHO_ABI_LUO_H #define _LINUX_KHO_ABI_LUO_H =20 +#include + /* * The LUO FDT hooks all LUO state for sessions, fds, etc. * In the root it also carries "liveupdate-number" 64-bit property that @@ -51,4 +82,44 @@ #define LUO_FDT_COMPATIBLE "luo-v1" #define LUO_FDT_LIVEUPDATE_NUM "liveupdate-number" =20 +/* + * LUO FDT session node + * LUO_FDT_SESSION_HEADER: is a u64 physical address of struct + * luo_session_header_ser + */ +#define LUO_FDT_SESSION_NODE_NAME "luo-session" +#define LUO_FDT_SESSION_COMPATIBLE "luo-session-v1" +#define LUO_FDT_SESSION_HEADER "luo-session-header" + +/** + * struct luo_session_header_ser - Header for the serialized session data = block. + * @count: The number of `struct luo_session_ser` entries that immediately + * follow this header in the memory block. + * + * This structure is located at the beginning of a contiguous block of + * physical memory preserved across the kexec. It provides the necessary + * metadata to interpret the array of session entries that follow. + * + * If this structure is modified, `LUO_FDT_SESSION_COMPATIBLE` must be upd= ated. + */ +struct luo_session_header_ser { + u64 count; +} __packed; + +/** + * struct luo_session_ser - Represents the serialized metadata for a LUO s= ession. + * @name: The unique name of the session, provided by the userspac= e at + * the time of session creation. + * + * This structure is used to package session-specific metadata for transfer + * between kernels via Kexec Handover. An array of these structures (one p= er + * session) is created and passed to the new kernel, allowing it to recons= truct + * the session context. + * + * If this structure is modified, `LUO_FDT_SESSION_COMPATIBLE` must be upd= ated. + */ +struct luo_session_ser { + char name[LIVEUPDATE_SESSION_NAME_LENGTH]; +} __packed; + #endif /* _LINUX_KHO_ABI_LUO_H */ diff --git a/include/uapi/linux/liveupdate.h b/include/uapi/linux/liveupdat= e.h index df34c1642c4d..40578ae19668 100644 --- a/include/uapi/linux/liveupdate.h +++ b/include/uapi/linux/liveupdate.h @@ -43,4 +43,7 @@ /* The ioctl type, documented in ioctl-number.rst */ #define LIVEUPDATE_IOCTL_TYPE 0xBA =20 +/* The maximum length of session name including null termination */ +#define LIVEUPDATE_SESSION_NAME_LENGTH 64 + #endif /* _UAPI_LIVEUPDATE_H */ diff --git a/kernel/liveupdate/Makefile b/kernel/liveupdate/Makefile index 08954c1770c4..6af93caa58cf 100644 --- a/kernel/liveupdate/Makefile +++ b/kernel/liveupdate/Makefile @@ -1,7 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 =20 luo-y :=3D \ - luo_core.o + luo_core.o \ + luo_session.o =20 obj-$(CONFIG_KEXEC_HANDOVER) +=3D kexec_handover.o obj-$(CONFIG_KEXEC_HANDOVER_DEBUG) +=3D kexec_handover_debug.o diff --git a/kernel/liveupdate/luo_core.c b/kernel/liveupdate/luo_core.c index 9f9fe9a81b29..a0f7788cd003 100644 --- a/kernel/liveupdate/luo_core.c +++ b/kernel/liveupdate/luo_core.c @@ -118,6 +118,10 @@ static int __init luo_early_startup(void) pr_info("Retrieved live update data, liveupdate number: %lld\n", luo_global.liveupdate_num); =20 + err =3D luo_session_setup_incoming(luo_global.fdt_in); + if (err) + return err; + return 0; } =20 @@ -154,6 +158,7 @@ static int __init luo_fdt_setup(void) err |=3D fdt_begin_node(fdt_out, ""); err |=3D fdt_property_string(fdt_out, "compatible", LUO_FDT_COMPATIBLE); err |=3D fdt_property(fdt_out, LUO_FDT_LIVEUPDATE_NUM, &ln, sizeof(ln)); + err |=3D luo_session_setup_outgoing(fdt_out); err |=3D fdt_end_node(fdt_out); err |=3D fdt_finish(fdt_out); if (err) @@ -211,6 +216,10 @@ int liveupdate_reboot(void) if (!liveupdate_enabled()) return 0; =20 + err =3D luo_session_serialize(); + if (err) + return err; + err =3D kho_finalize(); if (err) { pr_err("kho_finalize failed %d\n", err); diff --git a/kernel/liveupdate/luo_internal.h b/kernel/liveupdate/luo_inter= nal.h index 8612687b2000..05ae91695ec6 100644 --- a/kernel/liveupdate/luo_internal.h +++ b/kernel/liveupdate/luo_internal.h @@ -19,4 +19,33 @@ */ #define luo_restore_fail(__fmt, ...) panic(__fmt, ##__VA_ARGS__) =20 +/** + * struct luo_session - Represents an active or incoming Live Update sessi= on. + * @name: A unique name for this session, used for identification and + * retrieval. + * @ser: Pointer to the serialized data for this session. + * @list: A list_head member used to link this session into a global= list + * of either outgoing (to be preserved) or incoming (restored= from + * previous kernel) sessions. + * @retrieved: A boolean flag indicating whether this session has been + * retrieved by a consumer in the new kernel. + * @mutex: protects fields in the luo_session. + */ +struct luo_session { + char name[LIVEUPDATE_SESSION_NAME_LENGTH]; + struct luo_session_ser *ser; + struct list_head list; + bool retrieved; + struct mutex mutex; +}; + +int luo_session_create(const char *name, struct file **filep); +int luo_session_retrieve(const char *name, struct file **filep); +int __init luo_session_setup_outgoing(void *fdt); +int __init luo_session_setup_incoming(void *fdt); +int luo_session_serialize(void); +int luo_session_deserialize(void); +bool luo_session_quiesce(void); +void luo_session_resume(void); + #endif /* _LINUX_LUO_INTERNAL_H */ diff --git a/kernel/liveupdate/luo_session.c b/kernel/liveupdate/luo_sessio= n.c new file mode 100644 index 000000000000..ee363d1b2b10 --- /dev/null +++ b/kernel/liveupdate/luo_session.c @@ -0,0 +1,462 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + */ + +/** + * DOC: LUO Sessions + * + * LUO Sessions provide the core mechanism for grouping and managing `stru= ct + * file *` instances that need to be preserved across a kexec-based live + * update. Each session acts as a named container for a set of file object= s, + * allowing a userspace agent to manage the lifecycle of resources critica= l to a + * workload. + * + * Core Concepts: + * + * - Named Containers: Sessions are identified by a unique, user-provided = name, + * which is used for both creation in the current kernel and retrieval i= n the + * next kernel. + * + * - Userspace Interface: Session management is driven from userspace via + * ioctls on /dev/liveupdate. + * + * - Serialization: Session metadata is preserved using the KHO framework.= When + * a live update is triggered via kexec, an array of `struct luo_session= _ser` + * is populated and placed in a preserved memory region. An FDT node is = also + * created, containing the count of sessions and the physical address of= this + * array. + * + * Session Lifecycle: + * + * 1. Creation: A userspace agent calls `luo_session_create()` to create a + * new, empty session and receives a file descriptor for it. + * + * 2. Serialization: When the `reboot(LINUX_REBOOT_CMD_KEXEC)` syscall is + * made, `luo_session_serialize()` is called. It iterates through all + * active sessions and writes their metadata into a memory area preser= ved + * by KHO. + * + * 3. Deserialization (in new kernel): After kexec, `luo_session_deserial= ize()` + * runs, reading the serialized data and creating a list of `struct + * luo_session` objects representing the preserved sessions. + * + * 4. Retrieval: A userspace agent in the new kernel can then call + * `luo_session_retrieve()` with a session name to get a new file + * descriptor and access the preserved state. + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "luo_internal.h" + +/* 16 4K pages, give space for 744 sessions */ +#define LUO_SESSION_PGCNT 16ul +#define LUO_SESSION_MAX (((LUO_SESSION_PGCNT << PAGE_SHIFT) - \ + sizeof(struct luo_session_header_ser)) / \ + sizeof(struct luo_session_ser)) + +/** + * struct luo_session_header - Header struct for managing LUO sessions. + * @count: The number of sessions currently tracked in the @list. + * @list: The head of the linked list of `struct luo_session` instan= ces. + * @rwsem: A read-write semaphore providing synchronized access to the + * session list and other fields in this structure. + * @header_ser: The header data of serialization array. + * @ser: The serialized session data (an array of + * `struct luo_session_ser`). + * @active: Set to true when first initialized. If previous kernel did= not + * send session data, active stays false for incoming. + */ +struct luo_session_header { + long count; + struct list_head list; + struct rw_semaphore rwsem; + struct luo_session_header_ser *header_ser; + struct luo_session_ser *ser; + bool active; +}; + +/** + * struct luo_session_global - Global container for managing LUO sessions. + * @incoming: The sessions passed from the previous kernel. + * @outgoing: The sessions that are going to be passed to the next ker= nel. + */ +struct luo_session_global { + struct luo_session_header incoming; + struct luo_session_header outgoing; +}; + +static struct luo_session_global luo_session_global =3D { + .incoming =3D { + .list =3D LIST_HEAD_INIT(luo_session_global.incoming.list), + .rwsem =3D __RWSEM_INITIALIZER(luo_session_global.incoming.rwsem), + }, + .outgoing =3D { + .list =3D LIST_HEAD_INIT(luo_session_global.outgoing.list), + .rwsem =3D __RWSEM_INITIALIZER(luo_session_global.outgoing.rwsem), + }, +}; + +static struct luo_session *luo_session_alloc(const char *name) +{ + struct luo_session *session =3D kzalloc(sizeof(*session), GFP_KERNEL); + + if (!session) + return ERR_PTR(-ENOMEM); + + strscpy(session->name, name, sizeof(session->name)); + INIT_LIST_HEAD(&session->list); + mutex_init(&session->mutex); + + return session; +} + +static void luo_session_free(struct luo_session *session) +{ + mutex_destroy(&session->mutex); + kfree(session); +} + +static int luo_session_insert(struct luo_session_header *sh, + struct luo_session *session) +{ + struct luo_session *it; + + guard(rwsem_write)(&sh->rwsem); + + /* + * For outgoing we should make sure there is room in serialization array + * for new session. + */ + if (sh =3D=3D &luo_session_global.outgoing) { + if (sh->count =3D=3D LUO_SESSION_MAX) + return -ENOMEM; + } + + /* + * For small number of sessions this loop won't hurt performance + * but if we ever start using a lot of sessions, this might + * become a bottle neck during deserialization time, as it would + * cause O(n*n) complexity. + */ + list_for_each_entry(it, &sh->list, list) { + if (!strncmp(it->name, session->name, sizeof(it->name))) + return -EEXIST; + } + list_add_tail(&session->list, &sh->list); + sh->count++; + + return 0; +} + +static void luo_session_remove(struct luo_session_header *sh, + struct luo_session *session) +{ + guard(rwsem_write)(&sh->rwsem); + list_del(&session->list); + sh->count--; +} + +static int luo_session_release(struct inode *inodep, struct file *filep) +{ + struct luo_session *session =3D filep->private_data; + struct luo_session_header *sh; + + /* If retrieved is set, it means this session is from incoming list */ + if (session->retrieved) + sh =3D &luo_session_global.incoming; + else + sh =3D &luo_session_global.outgoing; + + luo_session_remove(sh, session); + luo_session_free(session); + + return 0; +} + +static const struct file_operations luo_session_fops =3D { + .owner =3D THIS_MODULE, + .release =3D luo_session_release, +}; + +/* Create a "struct file" for session */ +static int luo_session_getfile(struct luo_session *session, struct file **= filep) +{ + char name_buf[128]; + struct file *file; + + lockdep_assert_held(&session->mutex); + snprintf(name_buf, sizeof(name_buf), "[luo_session] %s", session->name); + file =3D anon_inode_getfile(name_buf, &luo_session_fops, session, O_RDWR); + if (IS_ERR(file)) + return PTR_ERR(file); + + *filep =3D file; + + return 0; +} + +int luo_session_create(const char *name, struct file **filep) +{ + struct luo_session *session; + int err; + + session =3D luo_session_alloc(name); + if (IS_ERR(session)) + return PTR_ERR(session); + + err =3D luo_session_insert(&luo_session_global.outgoing, session); + if (err) + goto err_free; + + scoped_guard(mutex, &session->mutex) + err =3D luo_session_getfile(session, filep); + if (err) + goto err_remove; + + return 0; + +err_remove: + luo_session_remove(&luo_session_global.outgoing, session); +err_free: + luo_session_free(session); + + return err; +} + +int luo_session_retrieve(const char *name, struct file **filep) +{ + struct luo_session_header *sh =3D &luo_session_global.incoming; + struct luo_session *session =3D NULL; + struct luo_session *it; + int err; + + scoped_guard(rwsem_read, &sh->rwsem) { + list_for_each_entry(it, &sh->list, list) { + if (!strncmp(it->name, name, sizeof(it->name))) { + session =3D it; + break; + } + } + } + + if (!session) + return -ENOENT; + + guard(mutex)(&session->mutex); + if (session->retrieved) + return -EINVAL; + + err =3D luo_session_getfile(session, filep); + if (!err) + session->retrieved =3D true; + + return err; +} + +int __init luo_session_setup_outgoing(void *fdt_out) +{ + struct luo_session_header_ser *header_ser; + void *outgoing_buffer; + u64 header_ser_pa; + int err; + + outgoing_buffer =3D kho_alloc_preserve(LUO_SESSION_PGCNT << PAGE_SHIFT); + if (IS_ERR(outgoing_buffer)) + return PTR_ERR(header_ser); + header_ser =3D outgoing_buffer; + header_ser_pa =3D virt_to_phys(header_ser); + + err =3D fdt_begin_node(fdt_out, LUO_FDT_SESSION_NODE_NAME); + err |=3D fdt_property_string(fdt_out, "compatible", + LUO_FDT_SESSION_COMPATIBLE); + err |=3D fdt_property(fdt_out, LUO_FDT_SESSION_HEADER, &header_ser_pa, + sizeof(header_ser_pa)); + err |=3D fdt_end_node(fdt_out); + + if (err) + goto err_unpreserve; + + luo_session_global.outgoing.header_ser =3D header_ser; + luo_session_global.outgoing.ser =3D (void *)(header_ser + 1); + luo_session_global.outgoing.active =3D true; + + return 0; + +err_unpreserve: + kho_unpreserve_free(header_ser); + return err; +} + +int __init luo_session_setup_incoming(void *fdt_in) +{ + struct luo_session_header_ser *header_ser; + int err, header_size, offset; + u64 header_ser_pa; + const void *ptr; + + offset =3D fdt_subnode_offset(fdt_in, 0, LUO_FDT_SESSION_NODE_NAME); + if (offset < 0) { + pr_err("Unable to get session node: [%s]\n", + LUO_FDT_SESSION_NODE_NAME); + return -EINVAL; + } + + err =3D fdt_node_check_compatible(fdt_in, offset, + LUO_FDT_SESSION_COMPATIBLE); + if (err) { + pr_err("Session node incompatible [%s]\n", + LUO_FDT_SESSION_COMPATIBLE); + return -EINVAL; + } + + header_size =3D 0; + ptr =3D fdt_getprop(fdt_in, offset, LUO_FDT_SESSION_HEADER, &header_size); + if (!ptr || header_size !=3D sizeof(u64)) { + pr_err("Unable to get session header '%s' [%d]\n", + LUO_FDT_SESSION_HEADER, header_size); + return -EINVAL; + } + + header_ser_pa =3D get_unaligned((u64 *)ptr); + header_ser =3D phys_to_virt(header_ser_pa); + + luo_session_global.incoming.header_ser =3D header_ser; + luo_session_global.incoming.ser =3D (void *)(header_ser + 1); + luo_session_global.incoming.active =3D true; + + return 0; +} + +int luo_session_deserialize(void) +{ + struct luo_session_header *sh =3D &luo_session_global.incoming; + static bool is_deserialized; + static int err; + + /* If has been deserialized, always return the same error code */ + if (is_deserialized) + return err; + + is_deserialized =3D true; + if (!sh->active) + return 0; + + /* + * Note on error handling: + * + * If deserialization fails (e.g., allocation failure or corrupt data), + * we intentionally skip cleanup of sessions that were already restored. + * + * A partial failure leaves the preserved state inconsistent. + * Implementing a safe "undo" to unwind complex dependencies (sessions, + * files, hardware state) is error-prone and provides little value, as + * the system is effectively in a broken state. + * + * We treat these resources as leaked. The expected recovery path is for + * userspace to detect the failure and trigger a reboot, which will + * reliably reset devices and reclaim memory. + */ + for (int i =3D 0; i < sh->header_ser->count; i++) { + struct luo_session *session; + + session =3D luo_session_alloc(sh->ser[i].name); + if (IS_ERR(session)) { + pr_warn("Failed to allocate session [%s] during deserialization %pe\n", + sh->ser[i].name, session); + return PTR_ERR(session); + } + + err =3D luo_session_insert(sh, session); + if (err) { + luo_session_free(session); + pr_warn("Failed to insert session [%s] %pe\n", + session->name, ERR_PTR(err)); + return err; + } + } + + kho_restore_free(sh->header_ser); + sh->header_ser =3D NULL; + sh->ser =3D NULL; + + return 0; +} + +int luo_session_serialize(void) +{ + struct luo_session_header *sh =3D &luo_session_global.outgoing; + struct luo_session *session; + int i =3D 0; + + guard(rwsem_write)(&sh->rwsem); + list_for_each_entry(session, &sh->list, list) { + strscpy(sh->ser[i].name, session->name, + sizeof(sh->ser[i].name)); + i++; + } + sh->header_ser->count =3D sh->count; + + return 0; +} + +/** + * luo_session_quiesce - Ensure no active sessions exist and lock session = lists. + * + * Acquires exclusive write locks on both incoming and outgoing session li= sts. + * It then validates no sessions exist in either list. + * + * This mechanism is used during file handler un/registration to ensure th= at no + * sessions are currently using the handler, and no new sessions can be cr= eated + * while un/registration is in progress. + * + * Return: + * true - System is quiescent (0 sessions) and locked. + * false - Active sessions exist. The locks are released internally. + */ +bool luo_session_quiesce(void) +{ + down_write(&luo_session_global.incoming.rwsem); + down_write(&luo_session_global.outgoing.rwsem); + + if (luo_session_global.incoming.count || + luo_session_global.outgoing.count) { + up_write(&luo_session_global.outgoing.rwsem); + up_write(&luo_session_global.incoming.rwsem); + return false; + } + + return true; +} + +/** + * luo_session_resume - Unlock session lists and resume normal activity. + * + * Releases the exclusive locks acquired by a successful call to + * luo_session_quiesce(). + */ +void luo_session_resume(void) +{ + up_write(&luo_session_global.outgoing.rwsem); + up_write(&luo_session_global.incoming.rwsem); +} --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yx1-f54.google.com (mail-yx1-f54.google.com [74.125.224.54]) (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 647E72D5C61 for ; Sat, 22 Nov 2025 22:24:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850250; cv=none; b=mymoHB7ZPYbrA4Mw+C5EsNCYeuxrQb4zN/vrje38vuNsCfeXNsNNkRqBmaUKOI3BvOgPHY47NYbVFIssTtKv7oyNfGDJ0QDmokfiENz4ZCy13zBEPRrXgKfsaokg9YZTEKWx5rX3zPpOFs/tz5d+EDYOuGjfKPQKE5+pIS/nzLk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850250; c=relaxed/simple; bh=VdsRKJOPMGdFTmpnYFOjezgc7KmhFSw5nB5qgGoDEP4=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PZoy+8kDTgck7sxVwJ5riBfHyFF1jryBHmQiN2y39ZpiPA3xxm8g7jl/GHe9eclZJWvupZoAjfre3WGaBaIttxXIUTljrhj8VyiQbwxY50XCPPLKTfjQl745EqKqEUnuOz89BJ/LMBCXg9lbH8e2jr/03/6ygilYSQEs+S1GS1k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=dWjh9IVJ; arc=none smtp.client-ip=74.125.224.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="dWjh9IVJ" Received: by mail-yx1-f54.google.com with SMTP id 956f58d0204a3-640c857ce02so2962933d50.0 for ; Sat, 22 Nov 2025 14:24:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850246; x=1764455046; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=g/BoEmiu9zsrdP23VvEElLAx2sbpX8ex9ZCQjmSfNmg=; b=dWjh9IVJyy0qExkUBZl4x9+J5wzJ//lZEVnkFkFTv5nlafxNpMKt0qhqd8zqbH/zAl DDQwth40RLf0GmzyUOseeo4wE0Aik3OfPfW2DbJ7yVdUSvO9W3cCSdCbsghjnTWEWSR+ 5Zjap3PSPt/h4ObAmLlT34PU+cRPA3tKxyDuo/iUeEhjGV9ZSPR3dW8bwnPhmz2jezpg r+y5s1xJFJ6yK7IrZ/8q9cRhMx6Nbjw+9xk+1mnm0JcCIJgvPt9ECqOcX0loJowDfMWH aGS6+QPsHQAkwJm+tjVni500Pm/pzIsXm4jpPsUD8ASCstcLZjKPbfyDCQ4M2PixTENk LkyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850246; x=1764455046; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=g/BoEmiu9zsrdP23VvEElLAx2sbpX8ex9ZCQjmSfNmg=; b=djvcHhL1pzg4Ez+iWurRAVwDxkVmd1JLCEPNrOmVNgQFiuMiD+kZ1Ezm1wdakTGVcS z1dgteSd/lt4EwHz2KXaUYeCqZYJd9liZyf9rIF4OGZhMMY95fabm8CY904YfDUHoizl g0c0TlhEgSzYW/qpKDmwBHz2zQSEeYNBCQqC6KAYwjjaOLSOo9jMMmdDp4fnprXV/qke VLejFBROOwhxQP12qz2jt1Y6cLieDd0YzhzszvBVddK3dXB20t4eg/RJ5KZ0Nb5rPri5 zefNQtAp/SxQ+oBVSF+JX205izwvS+aB2haGanrauCWzj8fjkyah1E03GMGsNOkF6oHz vujQ== X-Forwarded-Encrypted: i=1; AJvYcCUuGAQCCVK5zaG9pK5pVvgSRR0CSbq1B7aXXFKrNIt+ICqeRivw2qbE/LwpzS3ugqTq41cNLOYpBWyaEz0=@vger.kernel.org X-Gm-Message-State: AOJu0YyLZaI0FlY1BY57r5x4oqMg33WEou8prPmKnGJg9S7+Bfkbyp9M L3j2nNkPyp9ecDrqUephkvj4j5U1KI0nUOnDoRuP6hP/kKIb/zb1izwOK4MJ+C6jVAU= X-Gm-Gg: ASbGnctWS/+FykDWx7oqSFArGz11qXHjfdOGfUUBBBbdCUbXXLds398JmgPfRiHJDcK cYpagP5m6sjCEyerNwmUhBPApPu7OR917V/SKnzBTToJfySRI3ZkCHYge82m6DA3cKx9VQ8ECYT 2VGFv7f81rYq1+Ix4Ah8qF9apdwX4aaOdRU+LjYY/zAmksXn5h/bGaj8D1wEPuoHCMzpxkQbDYs f7bpTlUyVOg1Y6PRGJfrhdjKsj2mHARO8VyCISR8baPQTpqryK0FsPTnr+jXnPjF8maYfIBHroQ g9Egj57KTT2H2+QfoOoNxi3mSi5D2PJOlQ25r+NNTB5CBy5uX+6ngLHYk6XJ6J8Qu/tmkWTW2/O ewrO8ldLSSrGCrIyeKMITeHFr+o8Vs668wKBxO9Otd4NDMTEHPH22XSOIoMGV5BF7wgYRxnXtYm cmkwsg+345jnhRKWXQieStf7V7w6PkgD1MxL5T5lBpThmECBhNewaMLru+2J9B6woakmOAI1iSr WZBTKM= X-Google-Smtp-Source: AGHT+IGsBnZ08fwT+U7ONzjkXoJcTJprClMLwp+I60RQ2LQQg/g7R6+ZP/xtQp7fP65yvXwFKSrR1g== X-Received: by 2002:a05:690e:848:b0:63f:b590:2e5 with SMTP id 956f58d0204a3-64302a7cb9emr3717590d50.22.1763850246138; Sat, 22 Nov 2025 14:24:06 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:05 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 05/22] liveupdate: luo_core: add user interface Date: Sat, 22 Nov 2025 17:23:32 -0500 Message-ID: <20251122222351.1059049-6-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" Introduce the user-space interface for the Live Update Orchestrator via ioctl commands, enabling external control over the live update process and management of preserved resources. The idea is that there is going to be a single userspace agent driving the live update, therefore, only a single process can ever hold this device opened at a time. The following ioctl commands are introduced: LIVEUPDATE_IOCTL_CREATE_SESSION Provides a way for userspace to create a named session for grouping file descriptors that need to be preserved. It returns a new file descriptor representing the session. LIVEUPDATE_IOCTL_RETRIEVE_SESSION Allows the userspace agent in the new kernel to reclaim a preserved session by its name, receiving a new file descriptor to manage the restored resources. Signed-off-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) Reviewed-by: Pratyush Yadav --- include/uapi/linux/liveupdate.h | 64 +++++++++++ kernel/liveupdate/luo_core.c | 179 ++++++++++++++++++++++++++++++- kernel/liveupdate/luo_internal.h | 21 ++++ 3 files changed, 263 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/liveupdate.h b/include/uapi/linux/liveupdat= e.h index 40578ae19668..1183cf984b5f 100644 --- a/include/uapi/linux/liveupdate.h +++ b/include/uapi/linux/liveupdate.h @@ -46,4 +46,68 @@ /* The maximum length of session name including null termination */ #define LIVEUPDATE_SESSION_NAME_LENGTH 64 =20 +/* The /dev/liveupdate ioctl commands */ +enum { + LIVEUPDATE_CMD_BASE =3D 0x00, + LIVEUPDATE_CMD_CREATE_SESSION =3D LIVEUPDATE_CMD_BASE, + LIVEUPDATE_CMD_RETRIEVE_SESSION =3D 0x01, +}; + +/** + * struct liveupdate_ioctl_create_session - ioctl(LIVEUPDATE_IOCTL_CREATE_= SESSION) + * @size: Input; sizeof(struct liveupdate_ioctl_create_session) + * @fd: Output; The new file descriptor for the created session. + * @name: Input; A null-terminated string for the session name, max + * length %LIVEUPDATE_SESSION_NAME_LENGTH including termination + * character. + * + * Creates a new live update session for managing preserved resources. + * This ioctl can only be called on the main /dev/liveupdate device. + * + * Return: 0 on success, negative error code on failure. + */ +struct liveupdate_ioctl_create_session { + __u32 size; + __s32 fd; + __u8 name[LIVEUPDATE_SESSION_NAME_LENGTH]; +}; + +#define LIVEUPDATE_IOCTL_CREATE_SESSION \ + _IO(LIVEUPDATE_IOCTL_TYPE, LIVEUPDATE_CMD_CREATE_SESSION) + +/** + * struct liveupdate_ioctl_retrieve_session - ioctl(LIVEUPDATE_IOCTL_RETRI= EVE_SESSION) + * @size: Input; sizeof(struct liveupdate_ioctl_retrieve_session) + * @fd: Output; The new file descriptor for the retrieved session. + * @name: Input; A null-terminated string identifying the session to re= trieve. + * The name must exactly match the name used when the session was + * created in the previous kernel. + * + * Retrieves a handle (a new file descriptor) for a preserved session by i= ts + * name. This is the primary mechanism for a userspace agent to regain con= trol + * of its preserved resources after a live update. + * + * The userspace application provides the null-terminated `name` of a sess= ion + * it created before the live update. If a preserved session with a matchi= ng + * name is found, the kernel instantiates it and returns a new file descri= ptor + * in the `fd` field. This new session FD can then be used for all file-sp= ecific + * operations, such as restoring individual file descriptors with + * LIVEUPDATE_SESSION_RETRIEVE_FD. + * + * It is the responsibility of the userspace application to know the names= of + * the sessions it needs to retrieve. If no session with the given name is + * found, the ioctl will fail with -ENOENT. + * + * This ioctl can only be called on the main /dev/liveupdate device when t= he + * system is in the LIVEUPDATE_STATE_UPDATED state. + */ +struct liveupdate_ioctl_retrieve_session { + __u32 size; + __s32 fd; + __u8 name[LIVEUPDATE_SESSION_NAME_LENGTH]; +}; + +#define LIVEUPDATE_IOCTL_RETRIEVE_SESSION \ + _IO(LIVEUPDATE_IOCTL_TYPE, LIVEUPDATE_CMD_RETRIEVE_SESSION) + #endif /* _UAPI_LIVEUPDATE_H */ diff --git a/kernel/liveupdate/luo_core.c b/kernel/liveupdate/luo_core.c index a0f7788cd003..bc90954252a3 100644 --- a/kernel/liveupdate/luo_core.c +++ b/kernel/liveupdate/luo_core.c @@ -41,7 +41,13 @@ =20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 +#include +#include +#include +#include +#include #include +#include #include #include #include @@ -52,7 +58,6 @@ #include #include #include - #include "kexec_handover_internal.h" #include "luo_internal.h" =20 @@ -246,12 +251,183 @@ bool liveupdate_enabled(void) return luo_global.enabled; } =20 +/** + * DOC: LUO ioctl Interface + * + * The IOCTL user-space control interface for the LUO subsystem. + * It registers a character device, typically found at ``/dev/liveupdate``, + * which allows a userspace agent to manage the LUO state machine and its + * associated resources, such as preservable file descriptors. + * + * To ensure that the state machine is controlled by a single entity, acce= ss + * to this device is exclusive: only one process is permitted to have + * ``/dev/liveupdate`` open at any given time. Subsequent open attempts wi= ll + * fail with -EBUSY until the first process closes its file descriptor. + * This singleton model simplifies state management by preventing conflict= ing + * commands from multiple userspace agents. + */ + struct luo_device_state { struct miscdevice miscdev; + atomic_t in_use; +}; + +static int luo_ioctl_create_session(struct luo_ucmd *ucmd) +{ + struct liveupdate_ioctl_create_session *argp =3D ucmd->cmd; + struct file *file; + int err; + + argp->fd =3D get_unused_fd_flags(O_CLOEXEC); + if (argp->fd < 0) + return argp->fd; + + err =3D luo_session_create(argp->name, &file); + if (err) + goto err_put_fd; + + err =3D luo_ucmd_respond(ucmd, sizeof(*argp)); + if (err) + goto err_put_file; + + fd_install(argp->fd, file); + + return 0; + +err_put_file: + fput(file); +err_put_fd: + put_unused_fd(argp->fd); + + return err; +} + +static int luo_ioctl_retrieve_session(struct luo_ucmd *ucmd) +{ + struct liveupdate_ioctl_retrieve_session *argp =3D ucmd->cmd; + struct file *file; + int err; + + argp->fd =3D get_unused_fd_flags(O_CLOEXEC); + if (argp->fd < 0) + return argp->fd; + + err =3D luo_session_retrieve(argp->name, &file); + if (err < 0) + goto err_put_fd; + + err =3D luo_ucmd_respond(ucmd, sizeof(*argp)); + if (err) + goto err_put_file; + + fd_install(argp->fd, file); + + return 0; + +err_put_file: + fput(file); +err_put_fd: + put_unused_fd(argp->fd); + + return err; +} + +static int luo_open(struct inode *inodep, struct file *filep) +{ + struct luo_device_state *ldev =3D container_of(filep->private_data, + struct luo_device_state, + miscdev); + + if (atomic_cmpxchg(&ldev->in_use, 0, 1)) + return -EBUSY; + + /* Always return -EIO to user if deserialization fail */ + if (luo_session_deserialize()) { + atomic_set(&ldev->in_use, 0); + return -EIO; + } + + return 0; +} + +static int luo_release(struct inode *inodep, struct file *filep) +{ + struct luo_device_state *ldev =3D container_of(filep->private_data, + struct luo_device_state, + miscdev); + atomic_set(&ldev->in_use, 0); + + return 0; +} + +union ucmd_buffer { + struct liveupdate_ioctl_create_session create; + struct liveupdate_ioctl_retrieve_session retrieve; +}; + +struct luo_ioctl_op { + unsigned int size; + unsigned int min_size; + unsigned int ioctl_num; + int (*execute)(struct luo_ucmd *ucmd); +}; + +#define IOCTL_OP(_ioctl, _fn, _struct, _last) = \ + [_IOC_NR(_ioctl) - LIVEUPDATE_CMD_BASE] =3D { \ + .size =3D sizeof(_struct) + \ + BUILD_BUG_ON_ZERO(sizeof(union ucmd_buffer) < \ + sizeof(_struct)), \ + .min_size =3D offsetofend(_struct, _last), \ + .ioctl_num =3D _ioctl, \ + .execute =3D _fn, \ + } + +static const struct luo_ioctl_op luo_ioctl_ops[] =3D { + IOCTL_OP(LIVEUPDATE_IOCTL_CREATE_SESSION, luo_ioctl_create_session, + struct liveupdate_ioctl_create_session, name), + IOCTL_OP(LIVEUPDATE_IOCTL_RETRIEVE_SESSION, luo_ioctl_retrieve_session, + struct liveupdate_ioctl_retrieve_session, name), }; =20 +static long luo_ioctl(struct file *filep, unsigned int cmd, unsigned long = arg) +{ + const struct luo_ioctl_op *op; + struct luo_ucmd ucmd =3D {}; + union ucmd_buffer buf; + unsigned int nr; + int err; + + nr =3D _IOC_NR(cmd); + if (nr < LIVEUPDATE_CMD_BASE || + (nr - LIVEUPDATE_CMD_BASE) >=3D ARRAY_SIZE(luo_ioctl_ops)) { + return -EINVAL; + } + + ucmd.ubuffer =3D (void __user *)arg; + err =3D get_user(ucmd.user_size, (u32 __user *)ucmd.ubuffer); + if (err) + return err; + + op =3D &luo_ioctl_ops[nr - LIVEUPDATE_CMD_BASE]; + if (op->ioctl_num !=3D cmd) + return -ENOIOCTLCMD; + if (ucmd.user_size < op->min_size) + return -EINVAL; + + ucmd.cmd =3D &buf; + err =3D copy_struct_from_user(ucmd.cmd, op->size, ucmd.ubuffer, + ucmd.user_size); + if (err) + return err; + + return op->execute(&ucmd); +} + static const struct file_operations luo_fops =3D { .owner =3D THIS_MODULE, + .open =3D luo_open, + .release =3D luo_release, + .unlocked_ioctl =3D luo_ioctl, }; =20 static struct luo_device_state luo_dev =3D { @@ -260,6 +436,7 @@ static struct luo_device_state luo_dev =3D { .name =3D "liveupdate", .fops =3D &luo_fops, }, + .in_use =3D ATOMIC_INIT(0), }; =20 static int __init liveupdate_ioctl_init(void) diff --git a/kernel/liveupdate/luo_internal.h b/kernel/liveupdate/luo_inter= nal.h index 05ae91695ec6..1292ac47eef8 100644 --- a/kernel/liveupdate/luo_internal.h +++ b/kernel/liveupdate/luo_internal.h @@ -9,6 +9,27 @@ #define _LINUX_LUO_INTERNAL_H =20 #include +#include + +struct luo_ucmd { + void __user *ubuffer; + u32 user_size; + void *cmd; +}; + +static inline int luo_ucmd_respond(struct luo_ucmd *ucmd, + size_t kernel_cmd_size) +{ + /* + * Copy the minimum of what the user provided and what we actually + * have. + */ + if (copy_to_user(ucmd->ubuffer, ucmd->cmd, + min_t(size_t, ucmd->user_size, kernel_cmd_size))) { + return -EFAULT; + } + return 0; +} =20 /* * Handles a deserialization failure: devices and memory is in unpredictab= le --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) (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 557212D5C67 for ; Sat, 22 Nov 2025 22:24:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850253; cv=none; b=LJ/dIdCq2ptITulSkx/gHzQ+ivZdl6cOeiUByP2Pl20Uv5SfiGRzidElDRDXFDh2zYz4WZfQJl3ziEPN/653QTC2ECbctZ4V+rUoenss4Cx/OJ5oAkSGUfuu3vCQfKJ740z/jpUvkwv3FfN0OEAEV/RvL6r/k5JUP3iR8q0MHCg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850253; c=relaxed/simple; bh=UplA9WgP5fzcb/DaUH6aM8PUR9eOIL2UQPykd0Q//mQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=elB+lbOk27DOMkWpjrqMLpBCjhsvHOniosgbGz/4TBV6wZFGTm++ZvPpKnMmG6e2IBch44wBHu58O383ClE3LhsdrV+Kdap45V7T5b1CCXbCtfUuGHIKpilaQ7bfedYdo6tTgqo4eQzMbPhJ2nVeiiRrnOxDaHSEVmz7YKxBeZY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=YBei4iqf; arc=none smtp.client-ip=209.85.128.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="YBei4iqf" Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-787be077127so31821717b3.2 for ; Sat, 22 Nov 2025 14:24:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850249; x=1764455049; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=+GUljan+jEK1daZO70PPscYjk/4iSytIyWKhAqY+230=; b=YBei4iqf/VaivklOZyk6KttFBjKqT7aIyCdht3Ia9vJamkYxTsoBoXFMW0+D8tiN1F UZHRYri53qheFxw9IqpAFj6D0/EfpS8WKv2ubQ1gZoy4idmprSvDL2TG3Vt+5SDq9NhB fdVUY02yNZ5TLu+90gZrvxqHriqfUT4qfcCt92ib/4j6m38hBliax399+bCW/g8jSX7a VDFtsEj3FSdRGLDvSdZDl2iJbSPNW+fhsBR2Xp7DmoCzcvjtwrVSVoFtOh7rSfybhvPc 2Vp/ed6zF3h+asPCAxgDeUqqbHkR4Ozj8y3ox1V9rXZIeraIH4AiSVBc9wezFw5HP8nf XYoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850249; x=1764455049; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=+GUljan+jEK1daZO70PPscYjk/4iSytIyWKhAqY+230=; b=UxVa0F6V9RfQcFNjj9FiOj/unIObFvJCORrO6HJK8NlG4Q/HYPm2dJo+YfK+vxGkc+ FMtk1F/1klU1ZU+hv/ZwR3JQKVqGh+91G0toYKXxW797FZTp8WanUuXx7pTA3HWTaifa Bo35TU4rtO1p2D9AHAD1vNDEwkEWeqJ5QoXQUaFP7Ec+/wi0eZkOU55kYJj6TYoZXnQM IMBZsph1sRGgmlzTBBvKMfmjMcxmYrc5blxFnPHRblL0YQHrO1PYYe5mw4bYkMSyaFN7 9lyfzAnPvmwkJczY6jYoxINm3jLSzzm9RJQyGj+Z8C2ENuHVE4E9CoBOZ3q3PHYdK8mB aILg== X-Forwarded-Encrypted: i=1; AJvYcCVkmaWZhvEXOvqAhcizg1et/QJ+wFBnlWFNuD2m+VZvCwwsxDCPFpe2zfguTpGR6R0JjBRZRvSyVUjIZ3Q=@vger.kernel.org X-Gm-Message-State: AOJu0YznWIFueBzRRvFECH50Y+ENyhVT2yl7wGzF5XJElL4F1rrxR3dM 56YDvEMZMwS+xrre7r36Plaib4QYbN0bQanHD2F0SVMjgs1e+HVvB9izpgdcU6jCtCA= X-Gm-Gg: ASbGncskVC7l/Y8NpOo4sd/1TMEx1/YI2lWW6oOUDmPMJMKyDif/3PRVpiGAQqsnrfJ TzphcMfExt88urFfMyBcfOsNfgxwSHLV8jOXvNTVDCrEg8KkIkqU+LMlFiPRQ/l6i+/58KRtOuY C5W8mEBfSpbmBtCIGz5vYKjlEExJYeJQ43iGfU6h1XBPHGXng/ibxFKxrnA653u8Srmp7QEOrzm M9ea4MHE0WOWp5hvRGzd/+toCDchMca7/1ZCS2DeD6EPLJbX6pTZH2LdmZPmPA1hD/M8Et76WQ9 nnZcVBtGvWcqlJh8l9vXhiN8j6pjykcu2sbLYHAe3NhOYfiEKMYmBGEtddsWjmRbFFQ0EooAnUw clIc3U7oFs9pPB8nLR9NLaySjlr1Dygfisb+OPlXqCeg5Het0+DfConpMqEonWahKZieu9eUZwA gcWjGsSj0LCdv22m7+mjMNJ3BmW+bGzq5x/yiCreGFKSL95Ng3mK4WS7HiHAZI0pxK1pE7jep5K NSrCak= X-Google-Smtp-Source: AGHT+IHqcVNcCov0sstRNqIsww0mq39Q5V6RLs1uytaRDkSbTGoHxcwlQ6i9AJ88xO6ltza5IIAO3w== X-Received: by 2002:a05:690c:6186:b0:787:ec83:5d0e with SMTP id 00721157ae682-78a8b568053mr61342427b3.60.1763850248833; Sat, 22 Nov 2025 14:24:08 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:07 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 06/22] liveupdate: luo_file: implement file systems callbacks Date: Sat, 22 Nov 2025 17:23:33 -0500 Message-ID: <20251122222351.1059049-7-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This patch implements the core mechanism for managing preserved files throughout the live update lifecycle. It provides the logic to invoke the file handler callbacks (preserve, unpreserve, freeze, unfreeze, retrieve, and finish) at the appropriate stages. During the reboot phase, luo_file_freeze() serializes the final metadata for each file (handler compatible string, token, and data handle) into a memory region preserved by KHO. In the new kernel, luo_file_deserialize() reconstructs the in-memory file list from this data, preparing the session for retrieval. Signed-off-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) Reviewed-by: Pratyush Yadav --- include/linux/kho/abi/luo.h | 39 +- include/linux/liveupdate.h | 98 ++++ kernel/liveupdate/Makefile | 1 + kernel/liveupdate/luo_file.c | 882 +++++++++++++++++++++++++++++++ kernel/liveupdate/luo_internal.h | 38 ++ 5 files changed, 1057 insertions(+), 1 deletion(-) create mode 100644 kernel/liveupdate/luo_file.c diff --git a/include/linux/kho/abi/luo.h b/include/linux/kho/abi/luo.h index a2d2940eca6b..fc143f243871 100644 --- a/include/linux/kho/abi/luo.h +++ b/include/linux/kho/abi/luo.h @@ -65,6 +65,11 @@ * Metadata for a single session, including its name and a physical po= inter * to another preserved memory block containing an array of * `struct luo_file_ser` for all files in that session. + * + * - struct luo_file_ser: + * Metadata for a single preserved file. Contains the `compatible` str= ing to + * find the correct handler in the new kernel, a user-provided `token`= for + * identification, and an opaque `data` handle for the handler to use. */ =20 #ifndef _LINUX_KHO_ABI_LUO_H @@ -82,13 +87,43 @@ #define LUO_FDT_COMPATIBLE "luo-v1" #define LUO_FDT_LIVEUPDATE_NUM "liveupdate-number" =20 +#define LIVEUPDATE_HNDL_COMPAT_LENGTH 48 + +/** + * struct luo_file_ser - Represents the serialized preserves files. + * @compatible: File handler compatible string. + * @data: Private data + * @token: User provided token for this file + * + * If this structure is modified, LUO_SESSION_COMPATIBLE must be updated. + */ +struct luo_file_ser { + char compatible[LIVEUPDATE_HNDL_COMPAT_LENGTH]; + u64 data; + u64 token; +} __packed; + +/** + * struct luo_file_set_ser - Represents the serialized metadata for file s= et + * @files: The physical address of a contiguous memory block that holds + * the serialized state of files (array of luo_file_ser) in this= file + * set. + * @count: The total number of files that were part of this session duri= ng + * serialization. Used for iteration and validation during + * restoration. + */ +struct luo_file_set_ser { + u64 files; + u64 count; +} __packed; + /* * LUO FDT session node * LUO_FDT_SESSION_HEADER: is a u64 physical address of struct * luo_session_header_ser */ #define LUO_FDT_SESSION_NODE_NAME "luo-session" -#define LUO_FDT_SESSION_COMPATIBLE "luo-session-v1" +#define LUO_FDT_SESSION_COMPATIBLE "luo-session-v2" #define LUO_FDT_SESSION_HEADER "luo-session-header" =20 /** @@ -110,6 +145,7 @@ struct luo_session_header_ser { * struct luo_session_ser - Represents the serialized metadata for a LUO s= ession. * @name: The unique name of the session, provided by the userspac= e at * the time of session creation. + * @file_set_ser: Serialized files belonging to this session, * * This structure is used to package session-specific metadata for transfer * between kernels via Kexec Handover. An array of these structures (one p= er @@ -120,6 +156,7 @@ struct luo_session_header_ser { */ struct luo_session_ser { char name[LIVEUPDATE_SESSION_NAME_LENGTH]; + struct luo_file_set_ser file_set_ser; } __packed; =20 #endif /* _LINUX_KHO_ABI_LUO_H */ diff --git a/include/linux/liveupdate.h b/include/linux/liveupdate.h index c6a1d6bd90cb..122ad8f16ff9 100644 --- a/include/linux/liveupdate.h +++ b/include/linux/liveupdate.h @@ -8,8 +8,93 @@ #define _LINUX_LIVEUPDATE_H =20 #include +#include +#include #include #include +#include + +struct liveupdate_file_handler; +struct file; + +/** + * struct liveupdate_file_op_args - Arguments for file operation callbacks. + * @handler: The file handler being called. + * @retrieved: The retrieve status for the 'can_finish / finish' + * operation. + * @file: The file object. For retrieve: [OUT] The callback se= ts + * this to the new file. For other ops: [IN] The caller= sets + * this to the file being operated on. + * @serialized_data: The opaque u64 handle, preserve/prepare/freeze may u= pdate + * this field. + * + * This structure bundles all parameters for the file operation callbacks. + * The 'data' and 'file' fields are used for both input and output. + */ +struct liveupdate_file_op_args { + struct liveupdate_file_handler *handler; + bool retrieved; + struct file *file; + u64 serialized_data; +}; + +/** + * struct liveupdate_file_ops - Callbacks for live-updatable files. + * @can_preserve: Required. Lightweight check to see if this handler is + * compatible with the given file. + * @preserve: Required. Performs state-saving for the file. + * @unpreserve: Required. Cleans up any resources allocated by @preserve. + * @freeze: Optional. Final actions just before kernel transition. + * @unfreeze: Optional. Undo freeze operations. + * @retrieve: Required. Restores the file in the new kernel. + * @can_finish: Optional. Check if this FD can finish, i.e. all restorat= ion + * pre-requirements for this FD are satisfied. Called prior= to + * finish, in order to do successful finish calls for all + * resources in the session. + * @finish: Required. Final cleanup in the new kernel. + * @owner: Module reference + * + * All operations (except can_preserve) receive a pointer to a + * 'struct liveupdate_file_op_args' containing the necessary context. + */ +struct liveupdate_file_ops { + bool (*can_preserve)(struct liveupdate_file_handler *handler, + struct file *file); + int (*preserve)(struct liveupdate_file_op_args *args); + void (*unpreserve)(struct liveupdate_file_op_args *args); + int (*freeze)(struct liveupdate_file_op_args *args); + void (*unfreeze)(struct liveupdate_file_op_args *args); + int (*retrieve)(struct liveupdate_file_op_args *args); + bool (*can_finish)(struct liveupdate_file_op_args *args); + void (*finish)(struct liveupdate_file_op_args *args); + struct module *owner; +}; + +/** + * struct liveupdate_file_handler - Represents a handler for a live-updata= ble file type. + * @ops: Callback functions + * @compatible: The compatibility string (e.g., "memfd-v1", "vfiof= d-v1") + * that uniquely identifies the file type this handler + * supports. This is matched against the compatible s= tring + * associated with individual &struct file instances. + * + * Modules that want to support live update for specific file types should + * register an instance of this structure. LUO uses this registration to + * determine if a given file can be preserved and to find the appropriate + * operations to manage its state across the update. + */ +struct liveupdate_file_handler { + const struct liveupdate_file_ops *ops; + const char compatible[LIVEUPDATE_HNDL_COMPAT_LENGTH]; + + /* private: */ + + /* + * Used for linking this handler instance into a global list of + * registered file handlers. + */ + struct list_head __private list; +}; =20 #ifdef CONFIG_LIVEUPDATE =20 @@ -19,6 +104,9 @@ bool liveupdate_enabled(void); /* Called during kexec to tell LUO that entered into reboot */ int liveupdate_reboot(void); =20 +int liveupdate_register_file_handler(struct liveupdate_file_handler *fh); +int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh); + #else /* CONFIG_LIVEUPDATE */ =20 static inline bool liveupdate_enabled(void) @@ -31,5 +119,15 @@ static inline int liveupdate_reboot(void) return 0; } =20 +static inline int liveupdate_register_file_handler(struct liveupdate_file_= handler *fh) +{ + return -EOPNOTSUPP; +} + +static inline int liveupdate_unregister_file_handler(struct liveupdate_fil= e_handler *fh) +{ + return -EOPNOTSUPP; +} + #endif /* CONFIG_LIVEUPDATE */ #endif /* _LINUX_LIVEUPDATE_H */ diff --git a/kernel/liveupdate/Makefile b/kernel/liveupdate/Makefile index 6af93caa58cf..7cad2eece32d 100644 --- a/kernel/liveupdate/Makefile +++ b/kernel/liveupdate/Makefile @@ -2,6 +2,7 @@ =20 luo-y :=3D \ luo_core.o \ + luo_file.o \ luo_session.o =20 obj-$(CONFIG_KEXEC_HANDOVER) +=3D kexec_handover.o diff --git a/kernel/liveupdate/luo_file.c b/kernel/liveupdate/luo_file.c new file mode 100644 index 000000000000..f10d6c37328c --- /dev/null +++ b/kernel/liveupdate/luo_file.c @@ -0,0 +1,882 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + */ + +/** + * DOC: LUO File Descriptors + * + * LUO provides the infrastructure to preserve specific, stateful file + * descriptors across a kexec-based live update. The primary goal is to al= low + * workloads, such as virtual machines using vfio, memfd, or iommufd, to + * retain access to their essential resources without interruption. + * + * The framework is built around a callback-based handler model and a well- + * defined lifecycle for each preserved file. + * + * Handler Registration: + * Kernel modules responsible for a specific file type (e.g., memfd, vfio) + * register a &struct liveupdate_file_handler. This handler provides a set= of + * callbacks that LUO invokes at different stages of the update process, m= ost + * notably: + * + * - can_preserve(): A lightweight check to determine if the handler is + * compatible with a given 'struct file'. + * - preserve(): The heavyweight operation that saves the file's state a= nd + * returns an opaque u64 handle. This is typically performed while the + * workload is still active to minimize the downtime during the + * actual reboot transition. + * - unpreserve(): Cleans up any resources allocated by .preserve(), cal= led + * if the preservation process is aborted before the reboot (i.e. sess= ion is + * closed). + * - freeze(): A final pre-reboot opportunity to prepare the state for k= exec. + * We are already in reboot syscall, and therefore userspace cannot mu= tate + * the file anymore. + * - unfreeze(): Undoes the actions of .freeze(), called if the live upd= ate + * is aborted after the freeze phase. + * - retrieve(): Reconstructs the file in the new kernel from the preser= ved + * handle. + * - finish(): Performs final check and cleanup in the new kernel. After + * succesul finish call, LUO gives up ownership to this file. + * + * File Preservation Lifecycle happy path: + * + * 1. Preserve (Normal Operation): A userspace agent preserves files one b= y one + * via an ioctl. For each file, luo_preserve_file() finds a compatible + * handler, calls its .preserve() operation, and creates an internal &s= truct + * luo_file to track the live state. + * + * 2. Freeze (Pre-Reboot): Just before the kexec, luo_file_freeze() is cal= led. + * It iterates through all preserved files, calls their respective .fre= eze() + * operation, and serializes their final metadata (compatible string, t= oken, + * and data handle) into a contiguous memory block for KHO. + * + * 3. Deserialize: After kexec, luo_file_deserialize() runs when session g= ets + * deserialized (which is when /dev/liveupdate is first opened). It rea= ds the + * serialized data from the KHO memory region and reconstructs the in-m= emory + * list of &struct luo_file instances for the new kernel, linking them = to + * their corresponding handlers. + * + * 4. Retrieve (New Kernel - Userspace Ready): The userspace agent can now + * restore file descriptors by providing a token. luo_retrieve_file() + * searches for the matching token, calls the handler's .retrieve() op = to + * re-create the 'struct file', and returns a new FD. Files can be + * retrieved in ANY order. + * + * 5. Finish (New Kernel - Cleanup): Once a session retrival is complete, + * luo_file_finish() is called. It iterates through all files, invokes = their + * .finish() operations for final cleanup, and releases all associated = kernel + * resources. + * + * File Preservation Lifecycle unhappy paths: + * + * 1. Abort Before Reboot: If the userspace agent aborts the live update + * process before calling reboot (e.g., by closing the session file + * descriptor), the session's release handler calls + * luo_file_unpreserve_files(). This invokes the .unpreserve() callback= on + * all preserved files, ensuring all allocated resources are cleaned up= and + * returning the system to a clean state. + * + * 2. Freeze Failure: During the reboot() syscall, if any handler's .freez= e() + * op fails, the .unfreeze() op is invoked on all previously *successfu= l* + * freezes to roll back their state. The reboot() syscall then returns = an + * error to userspace, canceling the live update. + * + * 3. Finish Failure: In the new kernel, if a handler's .finish() op fails, + * the luo_file_finish() operation is aborted. LUO retains ownership of + * all files within that session, including those that were not yet + * processed. The userspace agent can attempt to call the finish operat= ion + * again later. If the issue cannot be resolved, these resources will b= e held + * by LUO until the next live update cycle, at which point they will be + * discarded. + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "luo_internal.h" + +static LIST_HEAD(luo_file_handler_list); + +/* 2 4K pages, give space for 128 files per file_set */ +#define LUO_FILE_PGCNT 2ul +#define LUO_FILE_MAX \ + ((LUO_FILE_PGCNT << PAGE_SHIFT) / sizeof(struct luo_file_ser)) + +/** + * struct luo_file - Represents a single preserved file instance. + * @fh: Pointer to the &struct liveupdate_file_handler that man= ages + * this type of file. + * @file: Pointer to the kernel's &struct file that is being pres= erved. + * This is NULL in the new kernel until the file is succes= sfully + * retrieved. + * @serialized_data: The opaque u64 handle to the serialized state of the = file. + * This handle is passed back to the handler's .freeze(), + * .retrieve(), and .finish() callbacks, allowing it to tr= ack + * and update its serialized state across phases. + * @retrieved: A flag indicating whether a user/kernel in the new kern= el has + * successfully called retrieve() on this file. This preve= nts + * multiple retrieval attempts. + * @mutex: A mutex that protects the fields of this specific insta= nce + * (e.g., @retrieved, @file), ensuring that operations like + * retrieving or finishing a file are atomic. + * @list: The list_head linking this instance into its parent + * file_set's list of preserved files. + * @token: The user-provided unique token used to identify this fi= le. + * + * This structure is the core in-kernel representation of a single file be= ing + * managed through a live update. An instance is created by luo_preserve_f= ile() + * to link a 'struct file' to its corresponding handler, a user-provided t= oken, + * and the serialized state handle returned by the handler's .preserve() + * operation. + * + * These instances are tracked in a per-file_set list. The @serialized_data + * field, which holds a handle to the file's serialized state, may be upda= ted + * during the .freeze() callback before being serialized for the next kern= el. + * After reboot, these structures are recreated by luo_file_deserialize() = and + * are finally cleaned up by luo_file_finish(). + */ +struct luo_file { + struct liveupdate_file_handler *fh; + struct file *file; + u64 serialized_data; + bool retrieved; + struct mutex mutex; + struct list_head list; + u64 token; +}; + +static int luo_alloc_files_mem(struct luo_file_set *file_set) +{ + size_t size; + void *mem; + + if (file_set->files) + return 0; + + WARN_ON_ONCE(file_set->count); + + size =3D LUO_FILE_PGCNT << PAGE_SHIFT; + mem =3D kho_alloc_preserve(size); + if (IS_ERR(mem)) + return PTR_ERR(mem); + + file_set->files =3D mem; + + return 0; +} + +static void luo_free_files_mem(struct luo_file_set *file_set) +{ + /* If file_set has files, no need to free preservation memory */ + if (file_set->count) + return; + + if (!file_set->files) + return; + + kho_unpreserve_free(file_set->files); + file_set->files =3D NULL; +} + +static bool luo_token_is_used(struct luo_file_set *file_set, u64 token) +{ + struct luo_file *iter; + + list_for_each_entry(iter, &file_set->files_list, list) { + if (iter->token =3D=3D token) + return true; + } + + return false; +} + +/** + * luo_preserve_file - Initiate the preservation of a file descriptor. + * @file_set: The file_set to which the preserved file will be added. + * @token: A unique, user-provided identifier for the file. + * @fd: The file descriptor to be preserved. + * + * This function orchestrates the first phase of preserving a file. Upon e= ntry, + * it takes a reference to the 'struct file' via fget(), effectively makin= g LUO + * a co-owner of the file. This reference is held until the file is either + * unpreserved or successfully finished in the next kernel, preventing the= file + * from being prematurely destroyed. + * + * This function orchestrates the first phase of preserving a file. It per= forms + * the following steps: + * + * 1. Validates that the @token is not already in use within the file_set. + * 2. Ensures the file_set's memory for files serialization is allocated + * (allocates if needed). + * 3. Iterates through registered handlers, calling can_preserve() to find= one + * compatible with the given @fd. + * 4. Calls the handler's .preserve() operation, which saves the file's st= ate + * and returns an opaque private data handle. + * 5. Adds the new instance to the file_set's internal list. + * + * On success, LUO takes a reference to the 'struct file' and considers it + * under its management until it is unpreserved or finished. + * + * In case of any failure, all intermediate allocations (file reference, m= emory + * for the 'luo_file' struct, etc.) are cleaned up before returning an err= or. + * + * Context: Can be called from an ioctl handler during normal system opera= tion. + * Return: 0 on success. Returns a negative errno on failure: + * -EEXIST if the token is already used. + * -EBADF if the file descriptor is invalid. + * -ENOSPC if the file_set is full. + * -ENOENT if no compatible handler is found. + * -ENOMEM on memory allocation failure. + * Other erros might be returned by .preserve(). + */ +int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd) +{ + struct liveupdate_file_op_args args =3D {0}; + struct liveupdate_file_handler *fh; + struct luo_file *luo_file; + struct file *file; + int err; + + if (luo_token_is_used(file_set, token)) + return -EEXIST; + + file =3D fget(fd); + if (!file) + return -EBADF; + + err =3D luo_alloc_files_mem(file_set); + if (err) + goto err_files_mem; + + if (file_set->count =3D=3D LUO_FILE_MAX) { + err =3D -ENOSPC; + goto err_files_mem; + } + + err =3D -ENOENT; + luo_list_for_each_private(fh, &luo_file_handler_list, list) { + if (fh->ops->can_preserve(fh, file)) { + err =3D 0; + break; + } + } + + /* err is still -ENOENT if no handler was found */ + if (err) + goto err_files_mem; + + luo_file =3D kzalloc(sizeof(*luo_file), GFP_KERNEL); + if (!luo_file) { + err =3D -ENOMEM; + goto err_files_mem; + } + + luo_file->file =3D file; + luo_file->fh =3D fh; + luo_file->token =3D token; + luo_file->retrieved =3D false; + mutex_init(&luo_file->mutex); + + args.handler =3D fh; + args.file =3D file; + err =3D fh->ops->preserve(&args); + if (err) + goto err_kfree; + + luo_file->serialized_data =3D args.serialized_data; + list_add_tail(&luo_file->list, &file_set->files_list); + file_set->count++; + + return 0; + +err_kfree: + mutex_destroy(&luo_file->mutex); + kfree(luo_file); +err_files_mem: + fput(file); + luo_free_files_mem(file_set); + + return err; +} + +/** + * luo_file_unpreserve_files - Unpreserves all files from a file_set. + * @file_set: The files to be cleaned up. + * + * This function serves as the primary cleanup path for a file_set. It is + * invoked when the userspace agent closes the file_set's file descriptor. + * + * For each file, it performs the following cleanup actions: + * 1. Calls the handler's .unpreserve() callback to allow the handler to + * release any resources it allocated. + * 2. Removes the file from the file_set's internal tracking list. + * 3. Releases the reference to the 'struct file' that was taken by + * luo_preserve_file() via fput(), returning ownership. + * 4. Frees the memory associated with the internal 'struct luo_file'. + * + * After all individual files are unpreserved, it frees the contiguous mem= ory + * block that was allocated to hold their serialization data. + */ +void luo_file_unpreserve_files(struct luo_file_set *file_set) +{ + struct luo_file *luo_file; + + while (!list_empty(&file_set->files_list)) { + struct liveupdate_file_op_args args =3D {0}; + + luo_file =3D list_last_entry(&file_set->files_list, + struct luo_file, list); + + args.handler =3D luo_file->fh; + args.file =3D luo_file->file; + args.serialized_data =3D luo_file->serialized_data; + luo_file->fh->ops->unpreserve(&args); + + list_del(&luo_file->list); + file_set->count--; + + fput(luo_file->file); + mutex_destroy(&luo_file->mutex); + kfree(luo_file); + } + + luo_free_files_mem(file_set); +} + +static int luo_file_freeze_one(struct luo_file_set *file_set, + struct luo_file *luo_file) +{ + int err =3D 0; + + guard(mutex)(&luo_file->mutex); + + if (luo_file->fh->ops->freeze) { + struct liveupdate_file_op_args args =3D {0}; + + args.handler =3D luo_file->fh; + args.file =3D luo_file->file; + args.serialized_data =3D luo_file->serialized_data; + + err =3D luo_file->fh->ops->freeze(&args); + if (!err) + luo_file->serialized_data =3D args.serialized_data; + } + + return err; +} + +static void luo_file_unfreeze_one(struct luo_file_set *file_set, + struct luo_file *luo_file) +{ + guard(mutex)(&luo_file->mutex); + + if (luo_file->fh->ops->unfreeze) { + struct liveupdate_file_op_args args =3D {0}; + + args.handler =3D luo_file->fh; + args.file =3D luo_file->file; + args.serialized_data =3D luo_file->serialized_data; + + luo_file->fh->ops->unfreeze(&args); + } + + luo_file->serialized_data =3D 0; +} + +static void __luo_file_unfreeze(struct luo_file_set *file_set, + struct luo_file *failed_entry) +{ + struct list_head *files_list =3D &file_set->files_list; + struct luo_file *luo_file; + + list_for_each_entry(luo_file, files_list, list) { + if (luo_file =3D=3D failed_entry) + break; + + luo_file_unfreeze_one(file_set, luo_file); + } + + memset(file_set->files, 0, LUO_FILE_PGCNT << PAGE_SHIFT); +} + +/** + * luo_file_freeze - Freezes all preserved files and serializes their meta= data. + * @file_set: The file_set whose files are to be frozen. + * @file_set_ser: Where to put the serialized file_set. + * + * This function is called from the reboot() syscall path, just before the + * kernel transitions to the new image via kexec. Its purpose is to perfor= m the + * final preparation and serialization of all preserved files in the file_= set. + * + * It iterates through each preserved file in FIFO order (the order of + * preservation) and performs two main actions: + * + * 1. Freezes the File: It calls the handler's .freeze() callback for each + * file. This gives the handler a final opportunity to quiesce the devi= ce or + * prepare its state for the upcoming reboot. The handler may update its + * private data handle during this step. + * + * 2. Serializes Metadata: After a successful freeze, it copies the final = file + * metadata=E2=80=94the handler's compatible string, the user token, an= d the final + * private data handle=E2=80=94into the pre-allocated contiguous memory= buffer + * (file_set->files) that will be handed over to the next kernel via KH= O. + * + * Error Handling (Rollback): + * This function is atomic. If any handler's .freeze() operation fails, the + * entire live update is aborted. The __luo_file_unfreeze() helper is + * immediately called to invoke the .unfreeze() op on all files that were + * successfully frozen before the point of failure, rolling them back to a + * running state. The function then returns an error, causing the reboot() + * syscall to fail. + * + * Context: Called only from the liveupdate_reboot() path. + * Return: 0 on success, or a negative errno on failure. + */ +int luo_file_freeze(struct luo_file_set *file_set, + struct luo_file_set_ser *file_set_ser) +{ + struct luo_file_ser *file_ser =3D file_set->files; + struct luo_file *luo_file; + int err; + int i; + + if (!file_set->count) + return 0; + + if (WARN_ON(!file_ser)) + return -EINVAL; + + i =3D 0; + list_for_each_entry(luo_file, &file_set->files_list, list) { + err =3D luo_file_freeze_one(file_set, luo_file); + if (err < 0) { + pr_warn("Freeze failed for token[%#0llx] handler[%s] err[%pe]\n", + luo_file->token, luo_file->fh->compatible, + ERR_PTR(err)); + goto err_unfreeze; + } + + strscpy(file_ser[i].compatible, luo_file->fh->compatible, + sizeof(file_ser[i].compatible)); + file_ser[i].data =3D luo_file->serialized_data; + file_ser[i].token =3D luo_file->token; + i++; + } + + file_set_ser->count =3D file_set->count; + if (file_set->files) + file_set_ser->files =3D virt_to_phys(file_set->files); + + return 0; + +err_unfreeze: + __luo_file_unfreeze(file_set, luo_file); + + return err; +} + +/** + * luo_file_unfreeze - Unfreezes all files in a file_set and clear seriali= zation + * @file_set: The file_set whose files are to be unfrozen. + * @file_set_ser: Serialized file_set. + * + * This function rolls back the state of all files in a file_set after the + * freeze phase has begun but must be aborted. It is the counterpart to + * luo_file_freeze(). + * + * It invokes the __luo_file_unfreeze() helper with a NULL argument, which + * signals the helper to iterate through all files in the file_set and call + * their respective .unfreeze() handler callbacks. + * + * Context: This is called when the live update is aborted during + * the reboot() syscall, after luo_file_freeze() has been called. + */ +void luo_file_unfreeze(struct luo_file_set *file_set, + struct luo_file_set_ser *file_set_ser) +{ + if (!file_set->count) + return; + + __luo_file_unfreeze(file_set, NULL); + memset(file_set_ser, 0, sizeof(*file_set_ser)); +} + +/** + * luo_retrieve_file - Restores a preserved file from a file_set by its to= ken. + * @file_set: The file_set from which to retrieve the file. + * @token: The unique token identifying the file to be restored. + * @filep: Output parameter; on success, this is populated with a point= er + * to the newly retrieved 'struct file'. + * + * This function is the primary mechanism for recreating a file in the new + * kernel after a live update. It searches the file_set's list of deserial= ized + * files for an entry matching the provided @token. + * + * The operation is idempotent: if a file has already been successfully + * retrieved, this function will simply return a pointer to the existing + * 'struct file' and report success without re-executing the retrieve + * operation. This is handled by checking the 'retrieved' flag under a loc= k. + * + * File retrieval can happen in any order; it is not bound by the order of + * preservation. + * + * Context: Can be called from an ioctl or other in-kernel code in the new + * kernel. + * Return: 0 on success. Returns a negative errno on failure: + * -ENOENT if no file with the matching token is found. + * Any error code returned by the handler's .retrieve() op. + */ +int luo_retrieve_file(struct luo_file_set *file_set, u64 token, + struct file **filep) +{ + struct liveupdate_file_op_args args =3D {0}; + struct luo_file *luo_file; + int err; + + if (list_empty(&file_set->files_list)) + return -ENOENT; + + list_for_each_entry(luo_file, &file_set->files_list, list) { + if (luo_file->token =3D=3D token) + break; + } + + if (luo_file->token !=3D token) + return -ENOENT; + + guard(mutex)(&luo_file->mutex); + if (luo_file->retrieved) { + /* + * Someone is asking for this file again, so get a reference + * for them. + */ + get_file(luo_file->file); + *filep =3D luo_file->file; + return 0; + } + + args.handler =3D luo_file->fh; + args.serialized_data =3D luo_file->serialized_data; + err =3D luo_file->fh->ops->retrieve(&args); + if (!err) { + luo_file->file =3D args.file; + + /* Get reference so we can keep this file in LUO until finish */ + get_file(luo_file->file); + *filep =3D luo_file->file; + luo_file->retrieved =3D true; + } + + return err; +} + +static int luo_file_can_finish_one(struct luo_file_set *file_set, + struct luo_file *luo_file) +{ + bool can_finish =3D true; + + guard(mutex)(&luo_file->mutex); + + if (luo_file->fh->ops->can_finish) { + struct liveupdate_file_op_args args =3D {0}; + + args.handler =3D luo_file->fh; + args.file =3D luo_file->file; + args.serialized_data =3D luo_file->serialized_data; + args.retrieved =3D luo_file->retrieved; + can_finish =3D luo_file->fh->ops->can_finish(&args); + } + + return can_finish ? 0 : -EBUSY; +} + +static void luo_file_finish_one(struct luo_file_set *file_set, + struct luo_file *luo_file) +{ + struct liveupdate_file_op_args args =3D {0}; + + guard(mutex)(&luo_file->mutex); + + args.handler =3D luo_file->fh; + args.file =3D luo_file->file; + args.serialized_data =3D luo_file->serialized_data; + args.retrieved =3D luo_file->retrieved; + + luo_file->fh->ops->finish(&args); +} + +/** + * luo_file_finish - Completes the lifecycle for all files in a file_set. + * @file_set: The file_set to be finalized. + * + * This function orchestrates the final teardown of a live update file_set= in + * the new kernel. It should be called after all necessary files have been + * retrieved and the userspace agent is ready to release the preserved sta= te. + * + * The function iterates through all tracked files. For each file, it perf= orms + * the following sequence of cleanup actions: + * + * 1. If file is not yet retrieved, retrieves it, and calls can_finish() on + * every file in the file_set. If all can_finish return true, continue = to + * finish. + * 2. Calls the handler's .finish() callback (via luo_file_finish_one) to + * allow for final resource cleanup within the handler. + * 3. Releases LUO's ownership reference on the 'struct file' via fput(). = This + * is the counterpart to the get_file() call in luo_retrieve_file(). + * 4. Removes the 'struct luo_file' from the file_set's internal list. + * 5. Frees the memory for the 'struct luo_file' instance itself. + * + * After successfully finishing all individual files, it frees the + * contiguous memory block that was used to transfer the serialized metada= ta + * from the previous kernel. + * + * Error Handling (Atomic Failure): + * This operation is atomic. If any handler's .can_finish() op fails, the = entire + * function aborts immediately and returns an error. + * + * Context: Can be called from an ioctl handler in the new kernel. + * Return: 0 on success, or a negative errno on failure. + */ +int luo_file_finish(struct luo_file_set *file_set) +{ + struct list_head *files_list =3D &file_set->files_list; + struct luo_file *luo_file; + int err; + + if (!file_set->count) + return 0; + + list_for_each_entry(luo_file, files_list, list) { + err =3D luo_file_can_finish_one(file_set, luo_file); + if (err) + return err; + } + + while (!list_empty(&file_set->files_list)) { + luo_file =3D list_last_entry(&file_set->files_list, + struct luo_file, list); + + luo_file_finish_one(file_set, luo_file); + + if (luo_file->file) + fput(luo_file->file); + list_del(&luo_file->list); + file_set->count--; + mutex_destroy(&luo_file->mutex); + kfree(luo_file); + } + + if (file_set->files) { + kho_restore_free(file_set->files); + file_set->files =3D NULL; + } + + return 0; +} + +/** + * luo_file_deserialize - Reconstructs the list of preserved files in the = new kernel. + * @file_set: The incoming file_set to fill with deserialized data. + * @file_set_ser: Serialized KHO file_set data from the previous kernel. + * + * This function is called during the early boot process of the new kernel= . It + * takes the raw, contiguous memory block of 'struct luo_file_ser' entries, + * provided by the previous kernel, and transforms it back into a live, + * in-memory linked list of 'struct luo_file' instances. + * + * For each serialized entry, it performs the following steps: + * 1. Reads the 'compatible' string. + * 2. Searches the global list of registered file handlers for one that + * matches the compatible string. + * 3. Allocates a new 'struct luo_file'. + * 4. Populates the new structure with the deserialized data (token, pri= vate + * data handle) and links it to the found handler. The 'file' pointer= is + * initialized to NULL, as the file has not been retrieved yet. + * 5. Adds the new 'struct luo_file' to the file_set's files_list. + * + * This prepares the file_set for userspace, which can later call + * luo_retrieve_file() to restore the actual file descriptors. + * + * Context: Called from session deserialization. + */ +int luo_file_deserialize(struct luo_file_set *file_set, + struct luo_file_set_ser *file_set_ser) +{ + struct luo_file_ser *file_ser; + u64 i; + + if (!file_set_ser->files) { + WARN_ON(file_set_ser->count); + return 0; + } + + file_set->count =3D file_set_ser->count; + file_set->files =3D phys_to_virt(file_set_ser->files); + + /* + * Note on error handling: + * + * If deserialization fails (e.g., allocation failure or corrupt data), + * we intentionally skip cleanup of files that were already restored. + * + * A partial failure leaves the preserved state inconsistent. + * Implementing a safe "undo" to unwind complex dependencies (sessions, + * files, hardware state) is error-prone and provides little value, as + * the system is effectively in a broken state. + * + * We treat these resources as leaked. The expected recovery path is for + * userspace to detect the failure and trigger a reboot, which will + * reliably reset devices and reclaim memory. + */ + file_ser =3D file_set->files; + for (i =3D 0; i < file_set->count; i++) { + struct liveupdate_file_handler *fh; + bool handler_found =3D false; + struct luo_file *luo_file; + + luo_list_for_each_private(fh, &luo_file_handler_list, list) { + if (!strcmp(fh->compatible, file_ser[i].compatible)) { + handler_found =3D true; + break; + } + } + + if (!handler_found) { + pr_warn("No registered handler for compatible '%s'\n", + file_ser[i].compatible); + return -ENOENT; + } + + luo_file =3D kzalloc(sizeof(*luo_file), GFP_KERNEL); + if (!luo_file) + return -ENOMEM; + + luo_file->fh =3D fh; + luo_file->file =3D NULL; + luo_file->serialized_data =3D file_ser[i].data; + luo_file->token =3D file_ser[i].token; + luo_file->retrieved =3D false; + mutex_init(&luo_file->mutex); + list_add_tail(&luo_file->list, &file_set->files_list); + } + + return 0; +} + +void luo_file_set_init(struct luo_file_set *file_set) +{ + INIT_LIST_HEAD(&file_set->files_list); +} + +void luo_file_set_destroy(struct luo_file_set *file_set) +{ + WARN_ON(file_set->count); + WARN_ON(!list_empty(&file_set->files_list)); +} + +/** + * liveupdate_register_file_handler - Register a file handler with LUO. + * @fh: Pointer to a caller-allocated &struct liveupdate_file_handler. + * The caller must initialize this structure, including a unique + * 'compatible' string and a valid 'fh' callbacks. This function adds the + * handler to the global list of supported file handlers. + * + * Context: Typically called during module initialization for file types t= hat + * support live update preservation. + * + * Return: 0 on success. Negative errno on failure. + */ +int liveupdate_register_file_handler(struct liveupdate_file_handler *fh) +{ + struct liveupdate_file_handler *fh_iter; + int err; + + if (!liveupdate_enabled()) + return -EOPNOTSUPP; + + /* Sanity check that all required callbacks are set */ + if (!fh->ops->preserve || !fh->ops->unpreserve || + !fh->ops->retrieve || !fh->ops->finish) { + return -EINVAL; + } + + /* + * Ensure the system is quiescent (no active sessions). + * This prevents registering new handlers while sessions are active or + * while deserialization is in progress. + */ + if (!luo_session_quiesce()) + return -EBUSY; + + /* Check for duplicate compatible strings */ + luo_list_for_each_private(fh_iter, &luo_file_handler_list, list) { + if (!strcmp(fh_iter->compatible, fh->compatible)) { + pr_err("File handler registration failed: Compatible string '%s' alread= y registered.\n", + fh->compatible); + err =3D -EEXIST; + goto err_resume; + } + } + + /* Pin the module implementing the handler */ + if (!try_module_get(fh->ops->owner)) { + err =3D -EAGAIN; + goto err_resume; + } + + INIT_LIST_HEAD(&ACCESS_PRIVATE(fh, list)); + list_add_tail(&ACCESS_PRIVATE(fh, list), &luo_file_handler_list); + luo_session_resume(); + + return 0; + +err_resume: + luo_session_resume(); + return err; +} + +/** + * liveupdate_unregister_file_handler - Unregister a liveupdate file handl= er + * @fh: The file handler to unregister + * + * Unregisters the file handler from the liveupdate core. This function + * reverses the operations of liveupdate_register_file_handler(). + * + * It ensures safe removal by checking that: + * No live update session is currently in progress. + * + * If the unregistration fails, the internal test state is reverted. + * + * Return: 0 Success. -EOPNOTSUPP when live update is not enabled. -EBUSY = A live + * update is in progress, can't quiesce live update. + */ +int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh) +{ + if (!liveupdate_enabled()) + return -EOPNOTSUPP; + + if (!luo_session_quiesce()) + return -EBUSY; + + list_del(&ACCESS_PRIVATE(fh, list)); + module_put(fh->ops->owner); + luo_session_resume(); + + return 0; +} diff --git a/kernel/liveupdate/luo_internal.h b/kernel/liveupdate/luo_inter= nal.h index 1292ac47eef8..c8973b543d1d 100644 --- a/kernel/liveupdate/luo_internal.h +++ b/kernel/liveupdate/luo_internal.h @@ -40,6 +40,28 @@ static inline int luo_ucmd_respond(struct luo_ucmd *ucmd, */ #define luo_restore_fail(__fmt, ...) panic(__fmt, ##__VA_ARGS__) =20 +/* Mimics list_for_each_entry() but for private list head entries */ +#define luo_list_for_each_private(pos, head, member) \ + for (struct list_head *__iter =3D (head)->next; \ + __iter !=3D (head) && \ + ({ pos =3D container_of(__iter, typeof(*(pos)), member); 1; }); \ + __iter =3D __iter->next) + +/** + * struct luo_file_set - A set of files that belong to the same sessions. + * @files_list: An ordered list of files associated with this session, it = is + * ordered by preservation time. + * @files: The physically contiguous memory block that holds the seri= alized + * state of files. + * @count: A counter tracking the number of files currently stored in= the + * @files_list for this session. + */ +struct luo_file_set { + struct list_head files_list; + struct luo_file_ser *files; + long count; +}; + /** * struct luo_session - Represents an active or incoming Live Update sessi= on. * @name: A unique name for this session, used for identification and @@ -50,6 +72,7 @@ static inline int luo_ucmd_respond(struct luo_ucmd *ucmd, * previous kernel) sessions. * @retrieved: A boolean flag indicating whether this session has been * retrieved by a consumer in the new kernel. + * @file_set: A set of files that belong to this session. * @mutex: protects fields in the luo_session. */ struct luo_session { @@ -57,6 +80,7 @@ struct luo_session { struct luo_session_ser *ser; struct list_head list; bool retrieved; + struct luo_file_set file_set; struct mutex mutex; }; =20 @@ -69,4 +93,18 @@ int luo_session_deserialize(void); bool luo_session_quiesce(void); void luo_session_resume(void); =20 +int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd); +void luo_file_unpreserve_files(struct luo_file_set *file_set); +int luo_file_freeze(struct luo_file_set *file_set, + struct luo_file_set_ser *file_set_ser); +void luo_file_unfreeze(struct luo_file_set *file_set, + struct luo_file_set_ser *file_set_ser); +int luo_retrieve_file(struct luo_file_set *file_set, u64 token, + struct file **filep); +int luo_file_finish(struct luo_file_set *file_set); +int luo_file_deserialize(struct luo_file_set *file_set, + struct luo_file_set_ser *file_set_ser); +void luo_file_set_init(struct luo_file_set *file_set); +void luo_file_set_destroy(struct luo_file_set *file_set); + #endif /* _LINUX_LUO_INTERNAL_H */ --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yx1-f52.google.com (mail-yx1-f52.google.com [74.125.224.52]) (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 EE0122D7DF7 for ; Sat, 22 Nov 2025 22:24:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850255; cv=none; b=HlMiNtjSfiMteKSM978zbDo9rO6eiHMLAYaCNiX2BkAtDH5XZuayJnNAvUL39GHFV50N7EU0FI9PP/pvJyTvXWbR0DvIVOZfX9K283rcw1GSe44OSAIviN9DGKhB8blW1kKfhkvuHEvKXYA1T2hTmWHjvI6omm5hwH+VirGXP3o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850255; c=relaxed/simple; bh=kgZHf9xz4+4bBw2EOBuQEpUtWJuh9Bt1Lgy3ywrEmYw=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fTNt8C3U3CkkqZ2h7moJm683RbBG12kGFeiUytNI0O/SdIAYsuoy6pBHQOEka8C/E0hu5uoi0aXuTDsFhK6V1BYMYKr3ni/OYmeW9kfpDC9sYnuwAkSAVNhu+O5ZO99i6zkvU7WnlXbRfcVVV4/h1p6jXWLhL/UTHdpd3fxo86I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=GNHBOd4i; arc=none smtp.client-ip=74.125.224.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="GNHBOd4i" Received: by mail-yx1-f52.google.com with SMTP id 956f58d0204a3-64306a32ed2so1109786d50.2 for ; Sat, 22 Nov 2025 14:24:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850251; x=1764455051; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=wE+SYfoU7cK/nhV6OlVUw04UpAJe3OMhPaMlk1e9ud4=; b=GNHBOd4ilqM6mBFUA7vI+7o0FLW00FdXYJZdGGUGnqg28g6Ago5LwZhuy6Zx398WVU fxnf3XNrMfUfVRzbAQ7JHc7eD597s1rdcVM1zeUpp1zSU8eigWxlriST86Vpl0WWT1bj OgUDqFLXxc2nHcoiBlbdEDsm3xnmk+/CfjCScIzk1oyHGe9kNUJ5q1NapV3xjVKIbRph gUZHD71VOoX3DIbdmLAX7UvE/ctelzBzVsmNyhnIXmEfzjOnoTfO+/IjwdJ9LzSs/KtA x2GCMBEai+2NqfmEsdmnBZJnsENQFcmSI+NtQN9B9Okb7nAKZle2o25MPJIt0XsG3gRB w7dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850251; x=1764455051; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=wE+SYfoU7cK/nhV6OlVUw04UpAJe3OMhPaMlk1e9ud4=; b=Ka2M7g4gX3EnEDMxBU5DAsdIe+ympQGcP4n66yGDEHBGO8Voupir2gyRU5PU0YTQtn /rdrTscJ3H0TY1aagOCzJSwDPwxn2eiv6uDnWy2Ey0w45FV1bDx47jdcqNaUfoHYQelh kj9UuUWl+aad/LDdnEH7MIYNgdHzCYVt2LgAEBmBy+XtzqYoQZtJm1Proi9QE2q1vQSF +TmrcUO5TDlKWfTjb0+xsjd32za+1HGWY0ZUGLar/uid6IKrIx+jvjZgxs3XNmhcQ1VP nUaPp0U6mHjg85FDBYlPSSDFl0NjCOgD6mOXn2sg3BoNtuWL+TKjZnIJqNkzGev2vryG xE0g== X-Forwarded-Encrypted: i=1; AJvYcCUGfGx1JHlceSkSpiVGQWoCsQO1PgMIPLavmIU93Y2zKKTqADbpjiYVGo066YYjN37peZ5iZf0XD0Nc3C8=@vger.kernel.org X-Gm-Message-State: AOJu0YxmWyVJDoXqpVxIhNtqK13/0M5BJRwxnbSa4l0nC14oLZwgqrj+ GOelOKa4pfF7sfWqa99MmWQ9kr88Vd06cAbrJ0SfLDZlJFUTQee4OhErq6tLwOsD8hA= X-Gm-Gg: ASbGncvyLtLu9nKcjzd1S0fedATR6JVywS90OPUCnqDf5b+bZF6KK/Rl3Bkmr594WZ4 LZhz4NDDxYvbs144mZkALso+uPifUPh+7Pzl8LDwMebcbgebw75ENzYPei/4R1wPnb1n+bjjG3n ANmDnmeRXwMDVLPrzTDa2jl0tW7FNLDqYBeoB6op/3wSnxWMNp+WJu5NO5FTpHcbb/8V7JIvXeH 4f5CBdPa8bJKfmUudqK6eNRyxlvajcf34ssX0nrHTE/Rfzzk6B1At1DtgL6wg4TVlVnNk4jlTPq VipTGxkWDCPBEJtM+Es78x2FrAe8bpU77zWrbvbizOHJcVeEExO3JdXjYEJr1OjVyXcyWfJb2yV Vk6dAm7yvN2kwG7sD+2e+EBq3FwYYxQjn7izgw8qW4/PoFhx2DwkeUjp/Th46fbWnYGD4967LOM W34SScA/AbBoFEJLZ3bERJ07gnIUmS0QahcYmDwRk+9746mdqhR6/RNTK6F6e1HvWj7s91uUqIX TSOW2s= X-Google-Smtp-Source: AGHT+IFScFT+y7taI+nsUYPq/JIzG5JwdVjEcr1qsjsOYvdiw1Mp8L5ZIUpuDVOehaLVZrPL9iFxhw== X-Received: by 2002:a05:690e:18e:b0:641:f5bc:694e with SMTP id 956f58d0204a3-64302aee0bcmr3414583d50.82.1763850250714; Sat, 22 Nov 2025 14:24:10 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:10 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 07/22] liveupdate: luo_session: Add ioctls for file preservation Date: Sat, 22 Nov 2025 17:23:34 -0500 Message-ID: <20251122222351.1059049-8-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" Introducing the userspace interface and internal logic required to manage the lifecycle of file descriptors within a session. Previously, a session was merely a container; this change makes it a functional management unit. The following capabilities are added: A new set of ioctl commands are added, which operate on the file descriptor returned by CREATE_SESSION. This allows userspace to: - LIVEUPDATE_SESSION_PRESERVE_FD: Add a file descriptor to a session to be preserved across the live update. - LIVEUPDATE_SESSION_RETRIEVE_FD: Retrieve a preserved file in the new kernel using its unique token. - LIVEUPDATE_SESSION_FINISH: finish session The session's .release handler is enhanced to be state-aware. When a session's file descriptor is closed, it correctly unpreserves the session based on its current state before freeing all associated file resources. Signed-off-by: Pasha Tatashin Reviewed-by: Pratyush Yadav Reviewed-by: Mike Rapoport (Microsoft) --- include/uapi/linux/liveupdate.h | 103 ++++++++++++++++++ kernel/liveupdate/luo_session.c | 187 +++++++++++++++++++++++++++++++- 2 files changed, 288 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/liveupdate.h b/include/uapi/linux/liveupdat= e.h index 1183cf984b5f..30bc66ee9436 100644 --- a/include/uapi/linux/liveupdate.h +++ b/include/uapi/linux/liveupdate.h @@ -53,6 +53,14 @@ enum { LIVEUPDATE_CMD_RETRIEVE_SESSION =3D 0x01, }; =20 +/* ioctl commands for session file descriptors */ +enum { + LIVEUPDATE_CMD_SESSION_BASE =3D 0x40, + LIVEUPDATE_CMD_SESSION_PRESERVE_FD =3D LIVEUPDATE_CMD_SESSION_BASE, + LIVEUPDATE_CMD_SESSION_RETRIEVE_FD =3D 0x41, + LIVEUPDATE_CMD_SESSION_FINISH =3D 0x42, +}; + /** * struct liveupdate_ioctl_create_session - ioctl(LIVEUPDATE_IOCTL_CREATE_= SESSION) * @size: Input; sizeof(struct liveupdate_ioctl_create_session) @@ -110,4 +118,99 @@ struct liveupdate_ioctl_retrieve_session { #define LIVEUPDATE_IOCTL_RETRIEVE_SESSION \ _IO(LIVEUPDATE_IOCTL_TYPE, LIVEUPDATE_CMD_RETRIEVE_SESSION) =20 +/* Session specific IOCTLs */ + +/** + * struct liveupdate_session_preserve_fd - ioctl(LIVEUPDATE_SESSION_PRESER= VE_FD) + * @size: Input; sizeof(struct liveupdate_session_preserve_fd) + * @fd: Input; The user-space file descriptor to be preserved. + * @token: Input; An opaque, unique token for preserved resource. + * + * Holds parameters for preserving a file descriptor. + * + * User sets the @fd field identifying the file descriptor to preserve + * (e.g., memfd, kvm, iommufd, VFIO). The kernel validates if this FD type + * and its dependencies are supported for preservation. If validation pass= es, + * the kernel marks the FD internally and *initiates the process* of prepa= ring + * its state for saving. The actual snapshotting of the state typically oc= curs + * during the subsequent %LIVEUPDATE_IOCTL_PREPARE execution phase, though + * some finalization might occur during freeze. + * On successful validation and initiation, the kernel uses the @token + * field with an opaque identifier representing the resource being preserv= ed. + * This token confirms the FD is targeted for preservation and is required= for + * the subsequent %LIVEUPDATE_SESSION_RETRIEVE_FD call after the live upda= te. + * + * Return: 0 on success (validation passed, preservation initiated), negat= ive + * error code on failure (e.g., unsupported FD type, dependency issue, + * validation failed). + */ +struct liveupdate_session_preserve_fd { + __u32 size; + __s32 fd; + __aligned_u64 token; +}; + +#define LIVEUPDATE_SESSION_PRESERVE_FD \ + _IO(LIVEUPDATE_IOCTL_TYPE, LIVEUPDATE_CMD_SESSION_PRESERVE_FD) + +/** + * struct liveupdate_session_retrieve_fd - ioctl(LIVEUPDATE_SESSION_RETRIE= VE_FD) + * @size: Input; sizeof(struct liveupdate_session_retrieve_fd) + * @fd: Output; The new file descriptor representing the fully restored + * kernel resource. + * @token: Input; An opaque, token that was used to preserve the resource. + * + * Retrieve a previously preserved file descriptor. + * + * User sets the @token field to the value obtained from a successful + * %LIVEUPDATE_IOCTL_FD_PRESERVE call before the live update. On success, + * the kernel restores the state (saved during the PREPARE/FREEZE phases) + * associated with the token and populates the @fd field with a new file + * descriptor referencing the restored resource in the current (new) kerne= l. + * This operation must be performed *before* signaling completion via + * %LIVEUPDATE_IOCTL_FINISH. + * + * Return: 0 on success, negative error code on failure (e.g., invalid tok= en). + */ +struct liveupdate_session_retrieve_fd { + __u32 size; + __s32 fd; + __aligned_u64 token; +}; + +#define LIVEUPDATE_SESSION_RETRIEVE_FD \ + _IO(LIVEUPDATE_IOCTL_TYPE, LIVEUPDATE_CMD_SESSION_RETRIEVE_FD) + +/** + * struct liveupdate_session_finish - ioctl(LIVEUPDATE_SESSION_FINISH) + * @size: Input; sizeof(struct liveupdate_session_finish) + * @reserved: Input; Must be zero. Reserved for future use. + * + * Signals the completion of the restoration process for a retrieved sessi= on. + * This is the final operation that should be performed on a session file + * descriptor after a live update. + * + * This ioctl must be called once all required file descriptors for the se= ssion + * have been successfully retrieved (using %LIVEUPDATE_SESSION_RETRIEVE_FD= ) and + * are fully restored from the userspace and kernel perspective. + * + * Upon success, the kernel releases its ownership of the preserved resour= ces + * associated with this session. This allows internal resources to be free= d, + * typically by decrementing reference counts on the underlying preserved + * objects. + * + * If this operation fails, the resources remain preserved in memory. User= space + * may attempt to call finish again. The resources will otherwise be reset + * during the next live update cycle. + * + * Return: 0 on success, negative error code on failure. + */ +struct liveupdate_session_finish { + __u32 size; + __u32 reserved; +}; + +#define LIVEUPDATE_SESSION_FINISH \ + _IO(LIVEUPDATE_IOCTL_TYPE, LIVEUPDATE_CMD_SESSION_FINISH) + #endif /* _UAPI_LIVEUPDATE_H */ diff --git a/kernel/liveupdate/luo_session.c b/kernel/liveupdate/luo_sessio= n.c index ee363d1b2b10..cf5c83b272e7 100644 --- a/kernel/liveupdate/luo_session.c +++ b/kernel/liveupdate/luo_session.c @@ -125,6 +125,8 @@ static struct luo_session *luo_session_alloc(const char= *name) return ERR_PTR(-ENOMEM); =20 strscpy(session->name, name, sizeof(session->name)); + INIT_LIST_HEAD(&session->file_set.files_list); + luo_file_set_init(&session->file_set); INIT_LIST_HEAD(&session->list); mutex_init(&session->mutex); =20 @@ -133,6 +135,7 @@ static struct luo_session *luo_session_alloc(const char= *name) =20 static void luo_session_free(struct luo_session *session) { + luo_file_set_destroy(&session->file_set); mutex_destroy(&session->mutex); kfree(session); } @@ -177,16 +180,46 @@ static void luo_session_remove(struct luo_session_hea= der *sh, sh->count--; } =20 +static int luo_session_finish_one(struct luo_session *session) +{ + guard(mutex)(&session->mutex); + return luo_file_finish(&session->file_set); +} + +static void luo_session_unfreeze_one(struct luo_session *session, + struct luo_session_ser *ser) +{ + guard(mutex)(&session->mutex); + luo_file_unfreeze(&session->file_set, &ser->file_set_ser); +} + +static int luo_session_freeze_one(struct luo_session *session, + struct luo_session_ser *ser) +{ + guard(mutex)(&session->mutex); + return luo_file_freeze(&session->file_set, &ser->file_set_ser); +} + static int luo_session_release(struct inode *inodep, struct file *filep) { struct luo_session *session =3D filep->private_data; struct luo_session_header *sh; =20 /* If retrieved is set, it means this session is from incoming list */ - if (session->retrieved) + if (session->retrieved) { + int err =3D luo_session_finish_one(session); + + if (err) { + pr_warn("Unable to finish session [%s] on release\n", + session->name); + return err; + } sh =3D &luo_session_global.incoming; - else + } else { + scoped_guard(mutex, &session->mutex) + luo_file_unpreserve_files(&session->file_set); sh =3D &luo_session_global.outgoing; + } =20 luo_session_remove(sh, session); luo_session_free(session); @@ -194,9 +227,140 @@ static int luo_session_release(struct inode *inodep, = struct file *filep) return 0; } =20 +static int luo_session_preserve_fd(struct luo_session *session, + struct luo_ucmd *ucmd) +{ + struct liveupdate_session_preserve_fd *argp =3D ucmd->cmd; + int err; + + guard(mutex)(&session->mutex); + err =3D luo_preserve_file(&session->file_set, argp->token, argp->fd); + if (err) + return err; + + err =3D luo_ucmd_respond(ucmd, sizeof(*argp)); + if (err) + pr_warn("The file was successfully preserved, but response to user faile= d\n"); + + return err; +} + +static int luo_session_retrieve_fd(struct luo_session *session, + struct luo_ucmd *ucmd) +{ + struct liveupdate_session_retrieve_fd *argp =3D ucmd->cmd; + struct file *file; + int err; + + argp->fd =3D get_unused_fd_flags(O_CLOEXEC); + if (argp->fd < 0) + return argp->fd; + + guard(mutex)(&session->mutex); + err =3D luo_retrieve_file(&session->file_set, argp->token, &file); + if (err < 0) + goto err_put_fd; + + err =3D luo_ucmd_respond(ucmd, sizeof(*argp)); + if (err) + goto err_put_file; + + fd_install(argp->fd, file); + + return 0; + +err_put_file: + fput(file); +err_put_fd: + put_unused_fd(argp->fd); + + return err; +} + +static int luo_session_finish(struct luo_session *session, + struct luo_ucmd *ucmd) +{ + struct liveupdate_session_finish *argp =3D ucmd->cmd; + int err =3D luo_session_finish_one(session); + + if (err) + return err; + + return luo_ucmd_respond(ucmd, sizeof(*argp)); +} + +union ucmd_buffer { + struct liveupdate_session_finish finish; + struct liveupdate_session_preserve_fd preserve; + struct liveupdate_session_retrieve_fd retrieve; +}; + +struct luo_ioctl_op { + unsigned int size; + unsigned int min_size; + unsigned int ioctl_num; + int (*execute)(struct luo_session *session, struct luo_ucmd *ucmd); +}; + +#define IOCTL_OP(_ioctl, _fn, _struct, _last) = \ + [_IOC_NR(_ioctl) - LIVEUPDATE_CMD_SESSION_BASE] =3D { \ + .size =3D sizeof(_struct) + \ + BUILD_BUG_ON_ZERO(sizeof(union ucmd_buffer) < \ + sizeof(_struct)), \ + .min_size =3D offsetofend(_struct, _last), \ + .ioctl_num =3D _ioctl, \ + .execute =3D _fn, \ + } + +static const struct luo_ioctl_op luo_session_ioctl_ops[] =3D { + IOCTL_OP(LIVEUPDATE_SESSION_FINISH, luo_session_finish, + struct liveupdate_session_finish, reserved), + IOCTL_OP(LIVEUPDATE_SESSION_PRESERVE_FD, luo_session_preserve_fd, + struct liveupdate_session_preserve_fd, token), + IOCTL_OP(LIVEUPDATE_SESSION_RETRIEVE_FD, luo_session_retrieve_fd, + struct liveupdate_session_retrieve_fd, token), +}; + +static long luo_session_ioctl(struct file *filep, unsigned int cmd, + unsigned long arg) +{ + struct luo_session *session =3D filep->private_data; + const struct luo_ioctl_op *op; + struct luo_ucmd ucmd =3D {}; + union ucmd_buffer buf; + unsigned int nr; + int ret; + + nr =3D _IOC_NR(cmd); + if (nr < LIVEUPDATE_CMD_SESSION_BASE || (nr - LIVEUPDATE_CMD_SESSION_BASE= ) >=3D + ARRAY_SIZE(luo_session_ioctl_ops)) { + return -EINVAL; + } + + ucmd.ubuffer =3D (void __user *)arg; + ret =3D get_user(ucmd.user_size, (u32 __user *)ucmd.ubuffer); + if (ret) + return ret; + + op =3D &luo_session_ioctl_ops[nr - LIVEUPDATE_CMD_SESSION_BASE]; + if (op->ioctl_num !=3D cmd) + return -ENOIOCTLCMD; + if (ucmd.user_size < op->min_size) + return -EINVAL; + + ucmd.cmd =3D &buf; + ret =3D copy_struct_from_user(ucmd.cmd, op->size, ucmd.ubuffer, + ucmd.user_size); + if (ret) + return ret; + + return op->execute(session, &ucmd); +} + static const struct file_operations luo_session_fops =3D { .owner =3D THIS_MODULE, .release =3D luo_session_release, + .unlocked_ioctl =3D luo_session_ioctl, }; =20 /* Create a "struct file" for session */ @@ -394,6 +558,11 @@ int luo_session_deserialize(void) session->name, ERR_PTR(err)); return err; } + + scoped_guard(mutex, &session->mutex) { + luo_file_deserialize(&session->file_set, + &sh->ser[i].file_set_ser); + } } =20 kho_restore_free(sh->header_ser); @@ -408,9 +577,14 @@ int luo_session_serialize(void) struct luo_session_header *sh =3D &luo_session_global.outgoing; struct luo_session *session; int i =3D 0; + int err; =20 guard(rwsem_write)(&sh->rwsem); list_for_each_entry(session, &sh->list, list) { + err =3D luo_session_freeze_one(session, &sh->ser[i]); + if (err) + goto err_undo; + strscpy(sh->ser[i].name, session->name, sizeof(sh->ser[i].name)); i++; @@ -418,6 +592,15 @@ int luo_session_serialize(void) sh->header_ser->count =3D sh->count; =20 return 0; + +err_undo: + list_for_each_entry_continue_reverse(session, &sh->list, list) { + i--; + luo_session_unfreeze_one(session, &sh->ser[i]); + memset(sh->ser[i].name, 0, sizeof(sh->ser[i].name)); + } + + return err; } =20 /** --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f174.google.com (mail-yw1-f174.google.com [209.85.128.174]) (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 43F072D8375 for ; Sat, 22 Nov 2025 22:24:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850256; cv=none; b=Wia7EMuO0h+khpOyYX4hj21yIEmWyABzYS6J2Y7sxJML0ydjH7APJcbhCTz7wRjxe9X4ZvGMrRvWbmxcnb0Z5PfI0r4hcUOyMU8TIoTayYfWDY9BkSYzYf0OmedNxYPWz407zcc0VCwwr3qB3S6mYOgN7bFMzd876g3L1Y4BkOE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850256; c=relaxed/simple; bh=1xESM3kSF6vnWYSb1QgtWGhaZBJp9gmYV9s8UUP630A=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Kzm0vuZase7gQ85vcExFQVHdHNwWfAhNCf47wdAEghVfQ94fa4NCliBnvz07yEcRB9lD2fhMfUr1eMnT9J19fMmw4Eawt3jgLo9483WLeq2BZ5UC77zcBcC4G/kkEOdYgDYxEv2IepA+QjHh82CGOhksNylKxUSNnZ4+Nn54/Lc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=EKZSh50U; arc=none smtp.client-ip=209.85.128.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="EKZSh50U" Received: by mail-yw1-f174.google.com with SMTP id 00721157ae682-786a85a68c6so30684067b3.3 for ; Sat, 22 Nov 2025 14:24:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850253; x=1764455053; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=G9VyPKNhrn6KupO2JDxcjMrdrPrlD9k+PG4k0aWwewA=; b=EKZSh50UwQFxjlcTV07lk8FgnLcovZponwg3MsyQ0oXvmZnzthjz0qar0sFedeADGx WN9d+Ujs5MJwp0+78pdJiAubqSXrs2j7gvXOrXEqe4obDVXZHRFa24LPtT0yrcmq8+JR eRyeSkJSBb0ky5giXZdPKNgaltJn+UucOOn1o1h+Br5ypohMNzsjyTwUHCQGzXhXjrSg XnS1GiexdfM9Hs6HY6pSDI856IcXMueeyM5S+UIZkLb9Yb5qpwws9HdMoIkglCO0lmSr JQ9VFWY2VUpIgC018HBGYRHvQHPHuC94PkkPgQjhoadlyXPehjwECUEO7rdpIGy/u0mz wvAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850253; x=1764455053; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=G9VyPKNhrn6KupO2JDxcjMrdrPrlD9k+PG4k0aWwewA=; b=GHKHsvBXkQetC8Lj9vJxhyYmAX7NPfQ6DlCagv88cIsXPJfIQhJdVYNuDzJAhakx6w fBvXHayoET3qqNmmX2yiDJNlfqtFugTcSdcQqmCbZ0Hz0jLWoTH981fLAchE970MS0fH sL7L4b4xEMD2vnekn3Q6Dq7a8cqJL7k7iyqwy2IawEFSGCnWd7sMNU3ztN4mcz0WaddU IVW4OGVxna5uHDYAlZSma8sUOltnS9DyAtXWY39Q1Afvehpagv55f9fq4AysbtCeOhcA 6tWSVAxjqtrYwfQh/k+1EWMSXC/qu9zWfacQ7sq70duYBRECBSxSdivk1CU9kBFeYFha hI0g== X-Forwarded-Encrypted: i=1; AJvYcCVvv6S9D9d6L1K97FzwFroC9odjPhua/UNf+R+4gCgAYUJKhrO7+kII4UtR8siIcVUWK1XI0Q+9JfSJObE=@vger.kernel.org X-Gm-Message-State: AOJu0Yy81k61uctyfxPrTvI00Lh7aws+bfRsw/vSW7DZTT9I6UAsbt91 fDgfT+49kXVBxLiugcnBvZd9thMLWbav0AjNkYnxvs2zND3k28as4r9luuBMIcz+50A= X-Gm-Gg: ASbGnctPc+VCVXdulg4vZvj/Z0hTmnYvQKBy2lJJ18NfUtdHAsLEnkMPosDgXZ03qNI tPHdxfxRSYuwkbFUFcUkpAwwgqDVWzz5gDAbOLiEKkwYLre85UGZqzoOfy8kZkiJSdghU3P/JL9 gSPf7DxxtkGRO54AhM3qO7q5BVfNW6mfr4P4qYBTKeDaTc4wFUPfBUSbqPRkXdLJwi6tVU9KdwS EEHGn+25+yA+7tGuJvDn256j2qeeG+6Zu63eGZY8cC4qG0yNNfWNukFjp9vjQys+o/lVByLSLHR /gla8OWJba5Rmugk6gW3bYKTD/gv0W0MM0K5BFuptIgreAgFQPaVGQE5kAWAuQue5Oqgm+gRE3N ie4M902I+vnGKe1Z9KdqjgqnTWAflVTloS0vcpsw5TywkLmhG49yjwG3nnQuFkM3wem/CyoRxZe ACNJb2g+x42gA5qxcjRv5zmm3D6id7Ndpi/uLaJJyaFX8rWbm9cN+fJEVUnqe8soRqrS7tnfR3F 1ziyectsjXIlAXnqg== X-Google-Smtp-Source: AGHT+IHmRWFE7rOb/6+yLAu3Pk2al1IPioQp+ETOH2bTApyjFAeLipumKzaBW+6hLLJxhT3rh6iVOw== X-Received: by 2002:a05:690c:d88:b0:786:a39e:e836 with SMTP id 00721157ae682-78a8b5672d2mr62717227b3.60.1763850253309; Sat, 22 Nov 2025 14:24:13 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:12 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 08/22] docs: add luo documentation Date: Sat, 22 Nov 2025 17:23:35 -0500 Message-ID: <20251122222351.1059049-9-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" Add the documentation files for the Live Update Orchestrator Signed-off-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) Reviewed-by: Pratyush Yadav --- Documentation/core-api/index.rst | 1 + Documentation/core-api/liveupdate.rst | 53 ++++++++++++++++++++++ Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/liveupdate.rst | 20 ++++++++ 4 files changed, 75 insertions(+) create mode 100644 Documentation/core-api/liveupdate.rst create mode 100644 Documentation/userspace-api/liveupdate.rst diff --git a/Documentation/core-api/index.rst b/Documentation/core-api/inde= x.rst index 6cbdcbfa79c3..5eb0fbbbc323 100644 --- a/Documentation/core-api/index.rst +++ b/Documentation/core-api/index.rst @@ -138,6 +138,7 @@ Documents that don't fit elsewhere or which have yet to= be categorized. :maxdepth: 1 =20 librs + liveupdate netlink =20 .. only:: subproject and html diff --git a/Documentation/core-api/liveupdate.rst b/Documentation/core-api= /liveupdate.rst new file mode 100644 index 000000000000..e1f0c13d5b4a --- /dev/null +++ b/Documentation/core-api/liveupdate.rst @@ -0,0 +1,53 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Live Update Orchestrator +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +:Author: Pasha Tatashin + +.. kernel-doc:: kernel/liveupdate/luo_core.c + :doc: Live Update Orchestrator (LUO) + +LUO Sessions +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +.. kernel-doc:: kernel/liveupdate/luo_session.c + :doc: LUO Sessions + +LUO Preserving File Descriptors +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D +.. kernel-doc:: kernel/liveupdate/luo_file.c + :doc: LUO File Descriptors + +Live Update Orchestrator ABI +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D +.. kernel-doc:: include/linux/kho/abi/luo.h + :doc: Live Update Orchestrator ABI + +Public API +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +.. kernel-doc:: include/linux/liveupdate.h + +.. kernel-doc:: include/linux/kho/abi/luo.h + +.. kernel-doc:: kernel/liveupdate/luo_core.c + :export: + +.. kernel-doc:: kernel/liveupdate/luo_file.c + :export: + +Internal API +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +.. kernel-doc:: kernel/liveupdate/luo_core.c + :internal: + +.. kernel-doc:: kernel/liveupdate/luo_session.c + :internal: + +.. kernel-doc:: kernel/liveupdate/luo_file.c + :internal: + +See Also +=3D=3D=3D=3D=3D=3D=3D=3D + +- :doc:`Live Update uAPI ` +- :doc:`/core-api/kho/concepts` diff --git a/Documentation/userspace-api/index.rst b/Documentation/userspac= e-api/index.rst index b8c73be4fb11..8a61ac4c1bf1 100644 --- a/Documentation/userspace-api/index.rst +++ b/Documentation/userspace-api/index.rst @@ -61,6 +61,7 @@ Everything else :maxdepth: 1 =20 ELF + liveupdate netlink/index sysfs-platform_profile vduse diff --git a/Documentation/userspace-api/liveupdate.rst b/Documentation/use= rspace-api/liveupdate.rst new file mode 100644 index 000000000000..41c0473e4f16 --- /dev/null +++ b/Documentation/userspace-api/liveupdate.rst @@ -0,0 +1,20 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Live Update uAPI +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +:Author: Pasha Tatashin + +ioctl interface +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +.. kernel-doc:: kernel/liveupdate/luo_core.c + :doc: LUO ioctl Interface + +ioctl uAPI +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +.. kernel-doc:: include/uapi/linux/liveupdate.h + +See Also +=3D=3D=3D=3D=3D=3D=3D=3D + +- :doc:`Live Update Orchestrator ` --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f174.google.com (mail-yw1-f174.google.com [209.85.128.174]) (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 F04C72D5C7A for ; Sat, 22 Nov 2025 22:24:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850257; cv=none; b=o95lqcYr2aVloClXQ8AUlcOKHopF3JNyngx5JFE1wiV+QUAdR5YVbB7j1fgCLBFq3AvQGi2RKJB9QkHm1uLfRYZUeHf5LTqGKpBp6/vP5xebUdDP940SsV4akj4lIfvbw8WcW+o2zLDXCkAIeYeGnmvw3iq5DTQGrp5qYKPwyQc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850257; c=relaxed/simple; bh=EnyxoW9Q62zzNLamdD3lY33p/cbJQtd0FxygTgIMtNI=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PG1ciJndspPFUdPqIh7IsDGOdpHY4wjJn47oudeQj4cmXBEYmPJPTPyDOgLqfhLulj9aKVaae76vdBvfZqIeE7C/9dNf9OhCwfp2a2nPthBRnD7wFx3iTMzlgHVtbhdF5/HN/NrTmJWrDC07c6fVhi03Py/3OlWb5xx/sIBDHRg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=S9NGJRgn; arc=none smtp.client-ip=209.85.128.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="S9NGJRgn" Received: by mail-yw1-f174.google.com with SMTP id 00721157ae682-787c9f90eccso32990517b3.3 for ; Sat, 22 Nov 2025 14:24:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850255; x=1764455055; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=xzrSObhj5Wimd1cdYGg31ZDKZ8HgVQz7p6sm5WUy9Y8=; b=S9NGJRgnpowrXgInACxha2n2Ffk3pLPjp3BC1WNT/wTq4yNHk8eU6sCmka9gmsBTMK pER5bynmdLXu7hu9B8itR7Z4TJyotJok/HIBFtiPyLwM9fKuDGaa48ypS9adHNj0wjjF obW+dMnDMo1P6jHVTpHMMpI1+BVVR3WLkhCgL7+JdBxzm150angHE5Bs/SslxXf25GmM 3++NLSaR3B+oc1eQi5AJGfQ6sEBkDbNNjqrqNFywmS2zzWwALBCqzkngHErncKv1F1xK xpQMIM8G3yUXBg6bN0J1Zge+v+OmvrTYUCtXntjFh9V8po//Xw9JbC8GwvLGzrusieiD AAjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850255; x=1764455055; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=xzrSObhj5Wimd1cdYGg31ZDKZ8HgVQz7p6sm5WUy9Y8=; b=DafRHyHwdGYplDTvJf8mcbGKPshIE5MIY+3djt4jk8gcaG+vnpBJUVXany08wVvH1M 4htHmyNQmfKJqOS+/C3ti7XqcklvrpjtBcuFXyOZBtESXo1Xy2Ex9WH0j3pvlrkYhugn 4UpS/+LQiDP+D10FFE40zxh5qEp4Bpr8ER5PFEvZJ5We43VqA5ZF+nvUxRjQJMT3O03u FChcFSObp/yPqR3sLWplxjAUUWNmuZTeqmpHretNsO4Kcd5EMLdhq7aFWmKfb7AX6kGS k/eBnM1xTg2TsKQLnigdFUZ8ao1k0VYisk8GIa++tEmi9+Ij/DcYUcGQI2QvJFmOI3Qs G6EA== X-Forwarded-Encrypted: i=1; AJvYcCWxIfmRGBMoQ5sJt/fBM4i8d2WAcDBz5KVCwFAvW0vMJ+KGv7EQggLAJrJ+IDMNUP82MffjoGMC4PLh26A=@vger.kernel.org X-Gm-Message-State: AOJu0YySMo6mnATw9oRMFa08Dq5F7gH5pU88HwZridp8VXhQJNWtllta 3HUjd6IZ0bzJT1TYtmt4TLCF/EXVicqghCPrNRITpn0UNdtXUmK29ePwcK6NCDkNO20= X-Gm-Gg: ASbGncsZnyPI9UqyKMOqS8cNUqb5wxSZpyOB8yUpUTIAKfwqNeYQkU8t72x84MMMH64 kWJ1XGIUOHXcV1eaf8yWVPnWSLudBo3lS1beQS9hGUW+L6pVa+PrgxnSeW4mqxlba4rDV7vSWqM XUMubUBkeddOOFxCGE7G+gpSzt6tYl+XDuMgvJDCCXDetcFgSmwldToOq/SWb7ciP+YJ0dX6CIo NVgplS/R2Ed3ejTDM1MHaEmbWw3o8TQHgMAHKF7+bOlq9boRJErEYzSobnhAVhRX9Wrj8Xrvair mQGn0UoelZLFN6piny/WvcQT/2ih8BT/oQzttqMJAshrzWIL83lFy1pHkl9C7YlFbszM6kQdhoo 34fY5AKDMC/7peXMeBpS0YLhrTAcz9pUx9IHDFMOtaQm+c5FZO1U1zNhRApuQSe0gizwwWOFaGA m/lloozGiIuAS2mMIQUtVRynU14O4x23H6HvLQdbAqIWTxkmfEHPICYSHIUkB+DX1Q6Y0S5xR3a GyICO8= X-Google-Smtp-Source: AGHT+IHKuSAjgrt1dxDo5oomNcqBOfqdLocHIsya6Qr+JoT/gAP6yZKLlAopzeyv3C1y1/iPUXwmuQ== X-Received: by 2002:a05:690c:b99:b0:786:4fd5:e5ce with SMTP id 00721157ae682-78a8b541204mr57832467b3.37.1763850255139; Sat, 22 Nov 2025 14:24:15 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:14 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 09/22] MAINTAINERS: add liveupdate entry Date: Sat, 22 Nov 2025 17:23:36 -0500 Message-ID: <20251122222351.1059049-10-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" Add a MAINTAINERS file entry for the new Live Update Orchestrator introduced in previous patches. Signed-off-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) Reviewed-by: Pratyush Yadav --- MAINTAINERS | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index b46425e3b4d3..868d3d23fdea 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14466,6 +14466,18 @@ F: kernel/module/livepatch.c F: samples/livepatch/ F: tools/testing/selftests/livepatch/ =20 +LIVE UPDATE +M: Pasha Tatashin +M: Mike Rapoport +L: linux-kernel@vger.kernel.org +S: Maintained +F: Documentation/core-api/liveupdate.rst +F: Documentation/userspace-api/liveupdate.rst +F: include/linux/liveupdate.h +F: include/linux/liveupdate/ +F: include/uapi/linux/liveupdate.h +F: kernel/liveupdate/ + LLC (802.2) L: netdev@vger.kernel.org S: Odd fixes --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yx1-f53.google.com (mail-yx1-f53.google.com [74.125.224.53]) (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 39EA42DAFBA for ; Sat, 22 Nov 2025 22:24:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850260; cv=none; b=nWqTqoeKe1No5C68rfcM9DK43c5TyyfahLbvNRMUl6jB9ncjC1Llg7XeHL9NS0KrgqBBA0d6LFOh1h9kSlw4C+V/hJTJrnJEFOjRRe5BX9JoCUxsAntRByJYuW36EzfpAhemO1fWyAyedULSdwk9Ef66SJ2vXh6LxM5pTRiKLP8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850260; c=relaxed/simple; bh=Qr8jJbtYkvKZNZeZt5x6RtbksbSJrimm82dpy5dwktM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FGDg1iVFVlHkAtzmNLIVfnUcl6V+WJPjpNayen4X8Sf1Y9+HoQkJhyCKJvw9JLUda1a2Y+ULDk1GqhtzFqYW1L9uNSjeXjUAiPJ52k0dWE+wgmaMQuTLzlWQv1vCEAvrg7DfIP3iURO75ROBWBSK3aKgSJIVw8kZhlMrergjmGA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=K6CWY2cf; arc=none smtp.client-ip=74.125.224.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="K6CWY2cf" Received: by mail-yx1-f53.google.com with SMTP id 956f58d0204a3-63f96d5038dso2695346d50.1 for ; Sat, 22 Nov 2025 14:24:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850257; x=1764455057; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=U3SsEWcfoC9wTWzpyUGqKCgnql8plxpKqJFH67bD/ic=; b=K6CWY2cfGaMAdw+GJe/cIMND7Isupzex4tVuMvVjLvSrnJGwkEKpjNeT6ihEeusdOG m4l1rlsXnZk0ObYKgLKY6RXJ2+nplKAnxFIu64G/UZpa014UQtLrrgwgCoP490wqFUz2 XTnOamaXFijm7tNiJ/e755k3JtPq7louVuCUtPtReIXSLh0pZEBsWcv7GN9HPijq6mG2 0HWeLjjvsynI82EAh5D+ugfGVAaumFsVipuJEM9/ZY97jLhlNbPdMPMS23wlU6Yzkjz7 dKkA8ThExl1wHzf3siCEREi2kSDAGeUwB1Z17V48WNps/18MDSSKLRw14+uLOtwK+XnE wfog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850257; x=1764455057; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=U3SsEWcfoC9wTWzpyUGqKCgnql8plxpKqJFH67bD/ic=; b=C+8qufpTPbKQ9F+QRHyrkemv5Hlg026s6D4RYw9Obero1jlp2+Rz0di6gprWr+Mi5q odbUX58ps9wABwPgBiIS5QzmH3QcBewRVdnJ8jPWIKYgzOkgq/74d538glvtX5uuc1AN KG1WW4ojQWadUVAFUFKBqdns1BLBjHMOwXqpASOnZz7WRKdGkn1MWI+VJ8PtolKMcJg+ C+iZc9aY1PrQIXkDoChyyC/DJoAd2R/+Vl+r7idT7474k1DfPo3mfLdGNk8hs+M+LSkk zO//x5JjkMUOpcHl+XXwXRoOlqURcIazfd7ZUXdJDshG8ftS3SQjunOXz2Q82EPhP2Mw 2ytA== X-Forwarded-Encrypted: i=1; AJvYcCWi7ugeYHPv+q12USOLylIg0D+vCNpPlN3Pco6nM6DGsgSk8srEE1xWK0HVedcY30YGrbiihFEkvtDepck=@vger.kernel.org X-Gm-Message-State: AOJu0Yx6MEqq1ZaGcmm1NMPQVzVSG9Q/KibouThvscdV2pi8hkYeuTg6 2AFWR567rine7Ryc3jfBa7kU3bDCN2saqyhlpx5/spU+yqPDb4amp7lXMvMofemnV+w= X-Gm-Gg: ASbGncuDI0ThJSwoYMtXnIjAso57bU4h0aCVsgBQZrFIQW5DZldMNoh807H14gBZA9B fdN9hNYgA55USBPKp8bXwexUL05zCyd18kFlz596UidZSs9XafaSewOklRlR5dRNA/tuejMqqgF 56IgyYjeky7R87s1pz5ABp5vfHZ7KUxcbfbk8VTMbxOweybVAEUbF7HLaAEyKYARbDj2vMtDJcf 0l46RImKKIc0PK6Reo+aFxe8OyxJnvMiuSYOnMhO9Zdx4cqrUqMS49W/7CQ1ptgqVNGPz5mz1YZ L23u6+YUWOv+lat4dDdsZHgX/7pOLhxJi3oTdZeM1DmlROOQar0pWygBfx2m6b0RG8D6QKtzE40 +xnXU6D8Wwr0tLqeo0bDX3nHoBnK/QIlrdEw1r/UHPON+WJveECyboMLfkFh6NKXxtjF6frrUnj w4N3CrSohOiI73tij43CvUkUBYXZzPvLX4BCsrKY6ryQ2SejcsXbCWhGWp1Jsuo6RLUg3QZS/eX 7hch5Q= X-Google-Smtp-Source: AGHT+IGN8V9s1y7QiOXXSQkfpg2Me3ul34/5J4KwUo57XUBRpeUqmFQ1wvJ0i+/kbCzYrwZ02S+fug== X-Received: by 2002:a05:690e:148f:b0:63f:b8d0:7e2c with SMTP id 956f58d0204a3-64302a2fe8dmr5068390d50.2.1763850257000; Sat, 22 Nov 2025 14:24:17 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:16 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 10/22] mm: shmem: use SHMEM_F_* flags instead of VM_* flags Date: Sat, 22 Nov 2025 17:23:37 -0500 Message-ID: <20251122222351.1059049-11-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" From: Pratyush Yadav shmem_inode_info::flags can have the VM flags VM_NORESERVE and VM_LOCKED. These are used to suppress pre-accounting or to lock the pages in the inode respectively. Using the VM flags directly makes it difficult to add shmem-specific flags that are unrelated to VM behavior since one would need to find a VM flag not used by shmem and re-purpose it. Introduce SHMEM_F_NORESERVE and SHMEM_F_LOCKED which represent the same information, but their bits are independent of the VM flags. Callers can still pass VM_NORESERVE to shmem_get_inode(), but it gets transformed to the shmem-specific flag internally. No functional changes intended. Signed-off-by: Pratyush Yadav Signed-off-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) --- include/linux/shmem_fs.h | 6 ++++++ mm/shmem.c | 28 +++++++++++++++------------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 0e47465ef0fd..650874b400b5 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -10,6 +10,7 @@ #include #include #include +#include =20 struct swap_iocb; =20 @@ -19,6 +20,11 @@ struct swap_iocb; #define SHMEM_MAXQUOTAS 2 #endif =20 +/* Suppress pre-accounting of the entire object size. */ +#define SHMEM_F_NORESERVE BIT(0) +/* Disallow swapping. */ +#define SHMEM_F_LOCKED BIT(1) + struct shmem_inode_info { spinlock_t lock; unsigned int seals; /* shmem seals */ diff --git a/mm/shmem.c b/mm/shmem.c index 58701d14dd96..1d5036dec08a 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -175,20 +175,20 @@ static inline struct shmem_sb_info *SHMEM_SB(struct s= uper_block *sb) */ static inline int shmem_acct_size(unsigned long flags, loff_t size) { - return (flags & VM_NORESERVE) ? + return (flags & SHMEM_F_NORESERVE) ? 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); } =20 static inline void shmem_unacct_size(unsigned long flags, loff_t size) { - if (!(flags & VM_NORESERVE)) + if (!(flags & SHMEM_F_NORESERVE)) vm_unacct_memory(VM_ACCT(size)); } =20 static inline int shmem_reacct_size(unsigned long flags, loff_t oldsize, loff_t newsize) { - if (!(flags & VM_NORESERVE)) { + if (!(flags & SHMEM_F_NORESERVE)) { if (VM_ACCT(newsize) > VM_ACCT(oldsize)) return security_vm_enough_memory_mm(current->mm, VM_ACCT(newsize) - VM_ACCT(oldsize)); @@ -206,7 +206,7 @@ static inline int shmem_reacct_size(unsigned long flags, */ static inline int shmem_acct_blocks(unsigned long flags, long pages) { - if (!(flags & VM_NORESERVE)) + if (!(flags & SHMEM_F_NORESERVE)) return 0; =20 return security_vm_enough_memory_mm(current->mm, @@ -215,7 +215,7 @@ static inline int shmem_acct_blocks(unsigned long flags= , long pages) =20 static inline void shmem_unacct_blocks(unsigned long flags, long pages) { - if (flags & VM_NORESERVE) + if (flags & SHMEM_F_NORESERVE) vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); } =20 @@ -1551,7 +1551,7 @@ int shmem_writeout(struct folio *folio, struct swap_i= ocb **plug, int nr_pages; bool split =3D false; =20 - if ((info->flags & VM_LOCKED) || sbinfo->noswap) + if ((info->flags & SHMEM_F_LOCKED) || sbinfo->noswap) goto redirty; =20 if (!total_swap_pages) @@ -2910,15 +2910,15 @@ int shmem_lock(struct file *file, int lock, struct = ucounts *ucounts) * ipc_lock_object() when called from shmctl_do_lock(), * no serialization needed when called from shm_destroy(). */ - if (lock && !(info->flags & VM_LOCKED)) { + if (lock && !(info->flags & SHMEM_F_LOCKED)) { if (!user_shm_lock(inode->i_size, ucounts)) goto out_nomem; - info->flags |=3D VM_LOCKED; + info->flags |=3D SHMEM_F_LOCKED; mapping_set_unevictable(file->f_mapping); } - if (!lock && (info->flags & VM_LOCKED) && ucounts) { + if (!lock && (info->flags & SHMEM_F_LOCKED) && ucounts) { user_shm_unlock(inode->i_size, ucounts); - info->flags &=3D ~VM_LOCKED; + info->flags &=3D ~SHMEM_F_LOCKED; mapping_clear_unevictable(file->f_mapping); } retval =3D 0; @@ -3062,7 +3062,7 @@ static struct inode *__shmem_get_inode(struct mnt_idm= ap *idmap, spin_lock_init(&info->lock); atomic_set(&info->stop_eviction, 0); info->seals =3D F_SEAL_SEAL; - info->flags =3D flags & VM_NORESERVE; + info->flags =3D (flags & VM_NORESERVE) ? SHMEM_F_NORESERVE : 0; info->i_crtime =3D inode_get_mtime(inode); info->fsflags =3D (dir =3D=3D NULL) ? 0 : SHMEM_I(dir)->fsflags & SHMEM_FL_INHERITED; @@ -5804,8 +5804,10 @@ static inline struct inode *shmem_get_inode(struct m= nt_idmap *idmap, /* common code */ =20 static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *n= ame, - loff_t size, unsigned long flags, unsigned int i_flags) + loff_t size, unsigned long vm_flags, + unsigned int i_flags) { + unsigned long flags =3D (vm_flags & VM_NORESERVE) ? SHMEM_F_NORESERVE : 0; struct inode *inode; struct file *res; =20 @@ -5822,7 +5824,7 @@ static struct file *__shmem_file_setup(struct vfsmoun= t *mnt, const char *name, return ERR_PTR(-ENOMEM); =20 inode =3D shmem_get_inode(&nop_mnt_idmap, mnt->mnt_sb, NULL, - S_IFREG | S_IRWXUGO, 0, flags); + S_IFREG | S_IRWXUGO, 0, vm_flags); if (IS_ERR(inode)) { shmem_unacct_size(flags, size); return ERR_CAST(inode); --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) (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 2DE6A2DCBEB for ; Sat, 22 Nov 2025 22:24:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850262; cv=none; b=mdKl7nRwDy420M0W9y/pFVyhnrLHVPIKz9NVIWY5j+zdTgKoPcp6tsnPEOUCfVJuTN3yDLLeaMwOE0RU6Wi1XfQB0RtobRN0II1022tdzlQoW1kKH0HWCnhRyRffe/eRD76wwFLIllW0MRtKN9gFhGLi/oWQbuFe0uEUyiyAtP4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850262; c=relaxed/simple; bh=K77jDX1Im6JqjYUrlt8KYiu/mP4dGBWQkfQRqxEPhbQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ddl7DfQ6D0husxrNo9HAgmj+WI20ZmFny43rs8VQnOhWeD1MqZ/kDL7BClx8cwVgUHXacQ+ttFPZND3oaZzDr5vILirQAzzjEbR6+NapXMahn8r0tVrptwaIfulONdZkPNdIvbm0pTkQwAuk9gj5Vp7OtG/cPGZ/u4FA2ws7Z1Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=bz21Qq3+; arc=none smtp.client-ip=209.85.128.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="bz21Qq3+" Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-7866bca6765so27781697b3.1 for ; Sat, 22 Nov 2025 14:24:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850259; x=1764455059; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=+VQkksdHZruPlACBWK27dtPpnM3dj37TPMUXRxmVJ+A=; b=bz21Qq3+vAD8Z6TU7I92tCykNJL2f5wdz8jL8X0C4YNqKq39PJ8ad5EeR33Gu404qj 78+x41wNhUkoHYQ9Sn6ck5cVq4YRaSDEmX0faz4fwqW6oxlMEi9i8dHAGXabLVr2/bf0 A91i+Mpcg5pLiszDEVMhiEzNpVcehsVnX8iPqZ+hr0/1EL+Ou3UVL3bQdNwso7/YnSxn HKIkuCqtIotxg+p/cC0kSQrXnPECUhzpJBe7Xn3jILl+UVOSPM1ghQF2ZnD5uNHmK8NL CEq3k4xBw0GF+r6eKN/nEscsAumwMIfpQnXorCGstamRIgo0iq7X0i9u81lrZ7bcII6H RULg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850259; x=1764455059; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=+VQkksdHZruPlACBWK27dtPpnM3dj37TPMUXRxmVJ+A=; b=tuNQJ/6MQ3rDVPEhIpW04EtuCgxLN0RP7JSjToTR5wOQUOvkh/Ynw3WFzgP+1MabD/ kscua4NrynX51cCX06SvnjqzUnAQKJWyUZQAJobr8zezNmO1axPOjvgxqye9ySIzOujM TQh/+LRonK5cX4s6FIMdH9j6rLpm37oZdiHBLCQjV6nvbUdme5y8e6itTZIMj3DoxVD5 vzM03k3I/YA/RqtGtG4iO/fAHBrwpQtHhDkv4sWOGXB+wlVftvWzRx6E4GffWUIt5UBC CKD7lq/fKiEZZEgdBKbpY6lO5MqEM5cF4JqR9q/39hpcuErr3kfwBkAtyPaZARcLYs8r Yapw== X-Forwarded-Encrypted: i=1; AJvYcCX6T+8wZx/Rgzk1KZCgeVNRJVUMuE1Hfrnqc2ru4tWzqPivO43Ur1UIOMwC/VEWnK/QpWskCIwOexo/kLs=@vger.kernel.org X-Gm-Message-State: AOJu0Yz+Bb/AdhNnAvcMVEzQcRamkabWXrGdjMYy8tEIAufR/bPTa/Ec EnXGmGLtwede+upyMXu087r2jfDZY7ELnLDT3DmKXanoHBFPVED2GG1EM+hZ8Og/HIg= X-Gm-Gg: ASbGnctKUbLMAXWBhisFi735I5DZA+IWhEB6+vJ0koISRuhiQIUWn6Uo3hNtwdNQjY6 oOXzv6ZJcmOl63lZDqbsTME8LAqF7jfcDUicJR7ASAg/+YHJpL0v4zZQXRH38PjRPF0a5tGzt9u TRmU8j/0JFbWW5df6kbvJACapaVeVsU1q9DGBNLLvaMT1Qrxqs78GlxHnubKXzqbfOcHKlv0z3y vEjtiE/TXm4ERQ0ZOYGvODkZauOAkV7279zzxrh0/D2y5693k1WcwRN5lmXB52nkfDyZ4f2w1d3 ENncSS9FkwWmKfYiXIfEcfWyp13gob6crac3O58QhAeZh7utrZQa5vcmcdE20Jqbmb+huqb7gXr jOXk+ZpqAHiEibtJ/WhFCcAwPe/XJb4Wa9hp53ULzmhl9z2wfYQ/ItjTZDOXu3u4CU5XEO3ZWzT dgLSE7kaG9FEh+IPAp8pGhClT29dWq+o8G9TSMe3WrdpVWo42YR5IMX23QRwc6dShjx3SZqnuuL wZLSig= X-Google-Smtp-Source: AGHT+IFvV3+5IXCVVP1oSyL2wYtEL/hA9DOLsYTa2a++REZ5p3m7io8YuDgIHUbUDCoA/rChPi/5eg== X-Received: by 2002:a05:690c:2781:b0:789:507d:6046 with SMTP id 00721157ae682-78a8b54d78dmr49374877b3.51.1763850258886; Sat, 22 Nov 2025 14:24:18 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:18 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 11/22] mm: shmem: allow freezing inode mapping Date: Sat, 22 Nov 2025 17:23:38 -0500 Message-ID: <20251122222351.1059049-12-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" From: Pratyush Yadav To prepare a shmem inode for live update, its index -> folio mappings must be serialized. Once the mappings are serialized, they cannot change since it would cause the serialized data to become inconsistent. This can be done by pinning the folios to avoid migration, and by making sure no folios can be added to or removed from the inode. While mechanisms to pin folios already exist, the only way to stop folios being added or removed are the grow and shrink file seals. But file seals come with their own semantics, one of which is that they can't be removed. This doesn't work with liveupdate since it can be cancelled or error out, which would need the seals to be removed and the file's normal functionality to be restored. Introduce SHMEM_F_MAPPING_FROZEN to indicate this instead. It is internal to shmem and is not directly exposed to userspace. It functions similar to F_SEAL_GROW | F_SEAL_SHRINK, but additionally disallows hole punching, and can be removed. Signed-off-by: Pratyush Yadav Signed-off-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) --- include/linux/shmem_fs.h | 17 +++++++++++++++++ mm/shmem.c | 19 ++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 650874b400b5..d34a64eafe60 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -24,6 +24,14 @@ struct swap_iocb; #define SHMEM_F_NORESERVE BIT(0) /* Disallow swapping. */ #define SHMEM_F_LOCKED BIT(1) +/* + * Disallow growing, shrinking, or hole punching in the inode. Combined wi= th + * folio pinning, makes sure the inode's mapping stays fixed. + * + * In some ways similar to F_SEAL_GROW | F_SEAL_SHRINK, but can be removed= and + * isn't directly visible to userspace. + */ +#define SHMEM_F_MAPPING_FROZEN BIT(2) =20 struct shmem_inode_info { spinlock_t lock; @@ -186,6 +194,15 @@ static inline bool shmem_file(struct file *file) return shmem_mapping(file->f_mapping); } =20 +/* Must be called with inode lock taken exclusive. */ +static inline void shmem_freeze(struct inode *inode, bool freeze) +{ + if (freeze) + SHMEM_I(inode)->flags |=3D SHMEM_F_MAPPING_FROZEN; + else + SHMEM_I(inode)->flags &=3D ~SHMEM_F_MAPPING_FROZEN; +} + /* * If fallocate(FALLOC_FL_KEEP_SIZE) has been used, there may be pages * beyond i_size's notion of EOF, which fallocate has committed to reservi= ng: diff --git a/mm/shmem.c b/mm/shmem.c index 1d5036dec08a..cb74a5d202ac 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1292,9 +1292,13 @@ static int shmem_setattr(struct mnt_idmap *idmap, loff_t newsize =3D attr->ia_size; =20 /* protected by i_rwsem */ - if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || - (newsize > oldsize && (info->seals & F_SEAL_GROW))) - return -EPERM; + if (newsize !=3D oldsize) { + if (info->flags & SHMEM_F_MAPPING_FROZEN) + return -EPERM; + if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || + (newsize > oldsize && (info->seals & F_SEAL_GROW))) + return -EPERM; + } =20 if (newsize !=3D oldsize) { error =3D shmem_reacct_size(SHMEM_I(inode)->flags, @@ -3289,6 +3293,10 @@ shmem_write_begin(const struct kiocb *iocb, struct a= ddress_space *mapping, return -EPERM; } =20 + if (unlikely((info->flags & SHMEM_F_MAPPING_FROZEN) && + pos + len > inode->i_size)) + return -EPERM; + ret =3D shmem_get_folio(inode, index, pos + len, &folio, SGP_WRITE); if (ret) return ret; @@ -3662,6 +3670,11 @@ static long shmem_fallocate(struct file *file, int m= ode, loff_t offset, =20 inode_lock(inode); =20 + if (info->flags & SHMEM_F_MAPPING_FROZEN) { + error =3D -EPERM; + goto out; + } + if (mode & FALLOC_FL_PUNCH_HOLE) { struct address_space *mapping =3D file->f_mapping; loff_t unmap_start =3D round_up(offset, PAGE_SIZE); --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f181.google.com (mail-yw1-f181.google.com [209.85.128.181]) (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 A86382DC346 for ; Sat, 22 Nov 2025 22:24:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850265; cv=none; b=GmCllgnnoFS0L5NUAoYEUMFeVdcjkCuWWtrWliwHRUGCJ54W7zhJcWBWgDM7WL1VNzVVTFIx9lOhtVH7AwS/a409lWNjiPBNDCE+9X8UzbFpCW2o+AwAHbEN1+27qQULPJ2jW1H2ha+y5QV4qA3PUXwm7+vjOs14fEeaUJBPE3g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850265; c=relaxed/simple; bh=cV6ayUY0cd5cthn+5O2DyMdENfG+D5eByWYgHRliMyA=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YS4Ptnyvh6Yc65m4emTwv77MyC3nVkty5AEXqOoBixgT3WnEa7cBPIbqAouLvoeV2ZwRTOrwfHP2KTkE6qBLQ2tMz4MdAuzOBJikXsSSG/cBjJ47tCt0mshAkJe9C4cXCsNPa2YzUrpcJJth4OntuGkmKQwRmdyT7pBOPC2kL8o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=HScPZ3NE; arc=none smtp.client-ip=209.85.128.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="HScPZ3NE" Received: by mail-yw1-f181.google.com with SMTP id 00721157ae682-787da30c53dso30689687b3.0 for ; Sat, 22 Nov 2025 14:24:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850261; x=1764455061; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=3+3x59DbFWdBPdmdih5ENuDC3ZjNMKrWdUeGoJeNo7E=; b=HScPZ3NEKZwt7blLAousjMuqCBILyYAhjd7MiMhHJgDzj1HcFn/1KoEHw8jqBuPHYY Bl0XBqTl6iLCVqc7OQZoFuAR9O3rUa8aIhWzSR7Gxd/3k4SmnbvChslsJZrwXV92YnRX OxKY33rox5nPfmVMvDhstWwTW7SK8BpGAOzaWThOctNUDRl+0a9rQwL+ZO3EO8ABfZyl EbAgutSOFlvto3g7icuAEdFEs6d3f4Aralf7CZHf2a2qB2FpxsVC1uQpDGZ47qHiaOtS Khz3ogqv+mHOAQUPQe+bInIjdoPl8bIaO/kADbyRcU0giU+CxqbH39NbEDbAO7ZgNBry yyXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850261; x=1764455061; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=3+3x59DbFWdBPdmdih5ENuDC3ZjNMKrWdUeGoJeNo7E=; b=e9vT+gjxwq+IFxR9pLT2E13Op6vT4Puuvo508+c28T+qn4K1EX4ZqSuBxyl9hFLvJT B0t9McjPXq7v7p7YQMeQxGtCblKD6gGo3YK0aGFJQS8uBzix33JAu0UrTNuqJaioTVkG y58YHeaSZOHl3q5GIzWD1FaH9KWIGB6t4IxZDFy0izrApMGi1EAW4DwjluGr4L+OqvNQ uJQ/idB6dGkd5mDn2D1QTatez62vQXMhxoPntkG/7vZaqVVqAEf6ahQvnHWwk1QBDglS J2hWe4I7OVMNQJfv/1wv8GYDKh7RpgqtPzPB7PqgSnl80njwul/YdQaEw/+p77kVdgwl Qnkg== X-Forwarded-Encrypted: i=1; AJvYcCXYFTthr1fxGsRdHdBwO3FxS1DrHi0Sk4Bhq0FljyzKzpUMg5aPOpJzUXP2JuZldW42sLkxvzXwzvrHw6o=@vger.kernel.org X-Gm-Message-State: AOJu0YyOPwyIYi6Nab+8xBO4toG4olD1L8sCoWqFAQf8GMp9RgHFs44D vRog+pOolPbd65IofH3nlxVUwgWpG9mUU3ugtQ/TCMbSEdu0cNT6xzkcUNaQzX/YIDE= X-Gm-Gg: ASbGnctASYYrteGogBz+dDLhGllS9et6tO0reolPOO3yWgcwVCsj1N+Ds/JB8Q2KrLP YWDSVnwUV+48XI46v/TgjZis4dpjo2Fk8quVLkRp009h0SldvG4zCb/6X3PSdZICLjN+Cxje6V8 wBd5SlIJZUswEW3+paTP6YOUrhAXZ5Cy1TB399rCbDxSjrXD3EaTIlryMheU1KgWeG5VPKuUZ+5 JSdaabTzHlp7a8CXWy4+Hz5JOFWbVct0Sh5k2bt7GoQ/RkPLTsQTMl8xm3kUWbgw1n4aZiSkYhn xPpOPYjNQUe38wW268U04x40mmD4Bx36vk1lMTvh0WlFqrHSajoBhIxVKneh4495e3+8i37ITVA eTKYuUeo8pm6twp6PoQHOwnSJPKSZl8NWzKaEFc4TOqEmzrleXGBm7TFtFHwv5FSo5pxiYhjpWw eu6LHOlvOcuE1s2Qfzj7S26W/YNAhilxmr9IfGSFVOTbPE2iDEDOmVsWw3evUsqjAjy/1AVNS0J 7zEBGM= X-Google-Smtp-Source: AGHT+IHLBVEUSrsRTAR569f2nmbBoHdToiAZD4CXjMMgQTOgrYNyCyqBuS6WcJxB+UmOJcCfJHKxnQ== X-Received: by 2002:a53:acd3:0:10b0:63f:55de:63cc with SMTP id 956f58d0204a3-64302ab185bmr3797706d50.31.1763850260775; Sat, 22 Nov 2025 14:24:20 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:20 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 12/22] mm: shmem: export some functions to internal.h Date: Sat, 22 Nov 2025 17:23:39 -0500 Message-ID: <20251122222351.1059049-13-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" From: Pratyush Yadav shmem_inode_acct_blocks(), shmem_recalc_inode(), and shmem_add_to_page_cache() are used by shmem_alloc_and_add_folio(). This functionality will be used by memfd LUO integration. Signed-off-by: Pratyush Yadav Signed-off-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) --- mm/internal.h | 6 ++++++ mm/shmem.c | 10 +++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 1561fc2ff5b8..4ba155524f80 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1562,6 +1562,12 @@ void __meminit __init_page_from_nid(unsigned long pf= n, int nid); unsigned long shrink_slab(gfp_t gfp_mask, int nid, struct mem_cgroup *memc= g, int priority); =20 +int shmem_add_to_page_cache(struct folio *folio, + struct address_space *mapping, + pgoff_t index, void *expected, gfp_t gfp); +int shmem_inode_acct_blocks(struct inode *inode, long pages); +bool shmem_recalc_inode(struct inode *inode, long alloced, long swapped); + #ifdef CONFIG_SHRINKER_DEBUG static inline __printf(2, 0) int shrinker_debugfs_name_alloc( struct shrinker *shrinker, const char *fmt, va_list ap) diff --git a/mm/shmem.c b/mm/shmem.c index cb74a5d202ac..b9cd7f8d50fa 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -219,7 +219,7 @@ static inline void shmem_unacct_blocks(unsigned long fl= ags, long pages) vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); } =20 -static int shmem_inode_acct_blocks(struct inode *inode, long pages) +int shmem_inode_acct_blocks(struct inode *inode, long pages) { struct shmem_inode_info *info =3D SHMEM_I(inode); struct shmem_sb_info *sbinfo =3D SHMEM_SB(inode->i_sb); @@ -435,7 +435,7 @@ static void shmem_free_inode(struct super_block *sb, si= ze_t freed_ispace) * * Return: true if swapped was incremented from 0, for shmem_writeout(). */ -static bool shmem_recalc_inode(struct inode *inode, long alloced, long swa= pped) +bool shmem_recalc_inode(struct inode *inode, long alloced, long swapped) { struct shmem_inode_info *info =3D SHMEM_I(inode); bool first_swapped =3D false; @@ -861,9 +861,9 @@ static void shmem_update_stats(struct folio *folio, int= nr_pages) /* * Somewhat like filemap_add_folio, but error if expected item has gone. */ -static int shmem_add_to_page_cache(struct folio *folio, - struct address_space *mapping, - pgoff_t index, void *expected, gfp_t gfp) +int shmem_add_to_page_cache(struct folio *folio, + struct address_space *mapping, + pgoff_t index, void *expected, gfp_t gfp) { XA_STATE_ORDER(xas, &mapping->i_pages, index, folio_order(folio)); unsigned long nr =3D folio_nr_pages(folio); --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (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 AAA082DF716 for ; Sat, 22 Nov 2025 22:24:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850266; cv=none; b=FCQsNabqyyXpVEmR2uUMIb28A4vmVgWVYKOEh6mlNDNugOHsMOYisUoXB4EkZeZ8/KjG6R1gY86PzF+be6AbZ7BWsKtc76LOGnUjEtlk7IKfqFFTOpfinvZN2QNNc2tARamE/dWaaR0bH7cJesPFYn2rGuAy/C/x3C/F+7p7EX4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850266; c=relaxed/simple; bh=0xJF18qOKcU5eAzQ6HxIuG25uSo6cc+l5rprPtdnC7I=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SCfYkfmjo7GfVQclIEi4GZUpTUGOPNFiqZHrl6ARK4aeeM2iyW2usY7cXFqVLGchovvceQXsJuMH/Z4QLB+PvSC4AZFxAbky74/QXFbE2SFUqYhs5m/aLLiScHItykDew7tdRbNC0vFidTlLJtjygLb/Tm3xoIxEBflu+XMxxCg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=Oxq+Bbp9; arc=none smtp.client-ip=209.85.128.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="Oxq+Bbp9" Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-78665368a5cso30145987b3.3 for ; Sat, 22 Nov 2025 14:24:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850262; x=1764455062; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=Ff4h/Y+aeCaryuraEaQmIoWWBfglnwQ79bUWNkRIqMI=; b=Oxq+Bbp9KtTJ0U0fh7o05c45t48ymLL7K4Aetlz6ihYvYy652IqvuRL73imA8k/TfN yLmCsAbYBWd5fCVcrO4M5ed9fnVw99pJS59eIqUP+6BgCZ8qN6OMikeUr6m1k6iggx75 zdWFSKQRUpWLO549NmoT0F0Tl/e/uvsiYMHADNtrArpb1qWmFQNRNAR1u/v88X1ULU1C yYbwrSF1kyq43xuq1EdFW2S29v/xXjkucAYYujW7nYPWu3RcvysIRrs6G+BCdv4l0iob XKet7YlHmvQI2jJtOFgZFYfkJqTSPwW0SvAOBkdbZhv4/lLGlNPur8yaZXTVFk0x/zkn QbWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850263; x=1764455063; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=Ff4h/Y+aeCaryuraEaQmIoWWBfglnwQ79bUWNkRIqMI=; b=R7c+NhQg2IAhR9ulfMrZnA/VdOHNae28sfcIK9TUTpORvAQAU1e7y/b9IQjWmyUqlu mkbdkLb4rBcgdl68e1iIrbBHgK9uIvTZta86qs3k9lAgFLZCJz7siyvxhqL1+iFlVPEs mb4beaSzxZPaeYkWSkelE9f3eA9y2vzjY7c/d39EedaEcV9nM52RcYND3jpUzGpR5aE5 X40Y3hM0/x3Aa6U4MvwH5bgg7s5qXSD6CU6DFhN6WTB6uKCY+aLDKqR1ghrU8P3m89xr uFC6QsyKzFx6AgK+nnHjIquIlpdLaUhgM8YbqPi7b22ddKzyiNNnYqLkOMsIXIJSickJ CZHw== X-Forwarded-Encrypted: i=1; AJvYcCXzH9MWqMotsZn8s7eoJMRBQHnuiQL0A5lNubadzQkn0zoSxNU10FPWxDbqt7tJRb2nXn6CDg51/EsbxdY=@vger.kernel.org X-Gm-Message-State: AOJu0Yx5OUSihw06j0XM4OpYMowCtIdQcoLg1z2ma54fAOangQK5xdNk iANKMr3Ll82RCZsCdvxSpEc0jCyLybLQuJHzeR7C7ylSfk8Xn2tWtKg529PmZ7TWJ5I= X-Gm-Gg: ASbGncu7K4bZqKydBH+GFSSvdjuPhosoJ0PD4wByHLyZw5gkHQnsjfohGGW3FebWsRI ntQHj7DxNrrGMj2i2EdOQovpObFhlDzcNxNhYsyC2B9T9nhM1nScigFTy2CKCRqoZX0tHhoFhDL YsjnpuToBKFpSxvENRgq+iQNvIt4RjZwknMi+QbwYw0pE+cywTiqSidKYiXAPtMHlbb+6BjBNHC hNnNMS5gbUxwOUepGsSHBMPgo/b0Rsn2FmBYcaacbxA2wJ4u4upKNsFNxtVFY2beGNkTcfsXoV/ fLb7Weadq9YTmk+ksiBQduElRE+nDl6/B6QJIFOCJauPBp3g14TIqLgxw4PpwVzxLrZxAmo6CvG P4YJrzjX7U6T7yIn/K7nifDxF5VgxMftjfiLAchNbjNz0bHZHdqBif/C15zHSORfc1TOsBgJ2aN hjN8qSaa8zOQr8zEyviV/PJboGS4KMk+0y1XV1KDuJfo94B95jDV2cgfDh7+r/58/Ti7Wue6piM bLQDXg= X-Google-Smtp-Source: AGHT+IEh/qsJ9xlDDV/YCr14e57USKSEfNEr00YTzpdoNrXFY7RlW6SCMXKos9LFs3pSvdRKqhUulg== X-Received: by 2002:a05:690c:6282:b0:789:24a4:59dc with SMTP id 00721157ae682-78a8b54896emr60698047b3.70.1763850262632; Sat, 22 Nov 2025 14:24:22 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:22 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 13/22] liveupdate: luo_file: add private argument to store runtime state Date: Sat, 22 Nov 2025 17:23:40 -0500 Message-ID: <20251122222351.1059049-14-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" From: Pratyush Yadav Currently file handlers only get the serialized_data field to store their state. This field has a pointer to the serialized state of the file, and it becomes a part of LUO file's serialized state. File handlers can also need some runtime state to track information that shouldn't make it in the serialized data. One such example is a vmalloc pointer. While kho_preserve_vmalloc() preserves the memory backing a vmalloc allocation, it does not store the original vmap pointer, since that has no use being passed to the next kernel. The pointer is needed to free the memory in case the file is unpreserved. Provide a private field in struct luo_file and pass it to all the callbacks. The field's can be set by preserve, and must be freed by unpreserve. Signed-off-by: Pratyush Yadav Co-developed-by: Pasha Tatashin Signed-off-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) --- include/linux/liveupdate.h | 5 +++++ kernel/liveupdate/luo_file.c | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/include/linux/liveupdate.h b/include/linux/liveupdate.h index 122ad8f16ff9..a7f6ee5b6771 100644 --- a/include/linux/liveupdate.h +++ b/include/linux/liveupdate.h @@ -27,6 +27,10 @@ struct file; * this to the file being operated on. * @serialized_data: The opaque u64 handle, preserve/prepare/freeze may u= pdate * this field. + * @private_data: Private data for the file used to hold runtime state= that + * is not preserved. Set by the handler's .preserve() + * callback, and must be freed in the handler's + * .unpreserve() callback. * * This structure bundles all parameters for the file operation callbacks. * The 'data' and 'file' fields are used for both input and output. @@ -36,6 +40,7 @@ struct liveupdate_file_op_args { bool retrieved; struct file *file; u64 serialized_data; + void *private_data; }; =20 /** diff --git a/kernel/liveupdate/luo_file.c b/kernel/liveupdate/luo_file.c index f10d6c37328c..b235d1b562ad 100644 --- a/kernel/liveupdate/luo_file.c +++ b/kernel/liveupdate/luo_file.c @@ -129,6 +129,10 @@ static LIST_HEAD(luo_file_handler_list); * This handle is passed back to the handler's .freeze(), * .retrieve(), and .finish() callbacks, allowing it to tr= ack * and update its serialized state across phases. + * @private_data: Pointer to the private data for the file used to hold r= untime + * state that is not preserved. Set by the handler's .pres= erve() + * callback, and must be freed in the handler's .unpreserv= e() + * callback. * @retrieved: A flag indicating whether a user/kernel in the new kern= el has * successfully called retrieve() on this file. This preve= nts * multiple retrieval attempts. @@ -155,6 +159,7 @@ struct luo_file { struct liveupdate_file_handler *fh; struct file *file; u64 serialized_data; + void *private_data; bool retrieved; struct mutex mutex; struct list_head list; @@ -300,6 +305,7 @@ int luo_preserve_file(struct luo_file_set *file_set, u6= 4 token, int fd) goto err_kfree; =20 luo_file->serialized_data =3D args.serialized_data; + luo_file->private_data =3D args.private_data; list_add_tail(&luo_file->list, &file_set->files_list); file_set->count++; =20 @@ -346,6 +352,7 @@ void luo_file_unpreserve_files(struct luo_file_set *fil= e_set) args.handler =3D luo_file->fh; args.file =3D luo_file->file; args.serialized_data =3D luo_file->serialized_data; + args.private_data =3D luo_file->private_data; luo_file->fh->ops->unpreserve(&args); =20 list_del(&luo_file->list); @@ -372,6 +379,7 @@ static int luo_file_freeze_one(struct luo_file_set *fil= e_set, args.handler =3D luo_file->fh; args.file =3D luo_file->file; args.serialized_data =3D luo_file->serialized_data; + args.private_data =3D luo_file->private_data; =20 err =3D luo_file->fh->ops->freeze(&args); if (!err) @@ -392,6 +400,7 @@ static void luo_file_unfreeze_one(struct luo_file_set *= file_set, args.handler =3D luo_file->fh; args.file =3D luo_file->file; args.serialized_data =3D luo_file->serialized_data; + args.private_data =3D luo_file->private_data; =20 luo_file->fh->ops->unfreeze(&args); } --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yx1-f41.google.com (mail-yx1-f41.google.com [74.125.224.41]) (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 CC4132DF145 for ; Sat, 22 Nov 2025 22:24:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850268; cv=none; b=N7R1yYIZXvw6azoDrzEukiTbpYYROGENPECHwDQSGW4WzGM9ZetSmzoYSgzLNNYb1sFRHOqgNwcaAh+4eLaK242MdZEVKBKp7/p+ypb4bYlRZzfOZ72iuID4paAhNF/kwX+8UcU8gZQoWxyrDnECg+KRV0u4IWLhu9PNAGxd4Xs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850268; c=relaxed/simple; bh=+lXu5VfobW9rkyUdEXqVKp6s4ISGYIvdhDvJAxgCCEc=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Iev1lVxSSux7pm5oPpO/A5rbIAodQqJkOYwpRukMSafgPz2lxA97VOQ2nbDgB+gRL2MrJYD1MrmlQ8bP0fePKfFeZWE6PJe+dzg+bLvNHFIfFT6OVCEyeFIzE3VfVr7RqjsH9nT+0TLaBQmZzbKn8P26N1SuHw5Y59TiZNg4S+o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=a4GMu1k8; arc=none smtp.client-ip=74.125.224.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="a4GMu1k8" Received: by mail-yx1-f41.google.com with SMTP id 956f58d0204a3-63f9beb2730so2448861d50.0 for ; Sat, 22 Nov 2025 14:24:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850265; x=1764455065; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=beJT+PwKakUMHJXCi9L/xuRC8WUOxr2uu34Av+3GgwY=; b=a4GMu1k8GY6mLaPCgktfXnfkz/+vnmTioC/iYsgFBeeBS0LCyEe7jjx+wBBv+p0WQd lXD0QzXB1E5LpXGNgn6oVs0Cp3+iD/sLdh4YnP+qMTMFkm5bQx2BpED4h2TqdIWB4b/g KyCIGl+uXGr2gQTLFbLXbUTdE0jWlyVguvNw98IqrLg3bz/yOqbOI99wUDBMQIjOcQ5k 1sJwvqMnpUHTGcq5XpOJwgbQFBt2Q10kwN+DKQIxRJlXGMhVg0dfqyKsNIYHoV8mh9Gq 9+jbAl2F3CJNb9Cug8NfIplAopY8QTGhrYMzvJ3azJXMteEMvmswFxdbqKO/iCkz03a8 A1Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850265; x=1764455065; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=beJT+PwKakUMHJXCi9L/xuRC8WUOxr2uu34Av+3GgwY=; b=L3E8CeHLswY1Faw3DXJVdQRraNC3uZm7fN8fw3CQc69aJ5yxQF0y9CEnmBE6aNjvJy FauTqLl1+PSDsHlAiT6VWJ/PWTSwwRbLrZWVC12xiHGacXf7z7sTsFMgqhjjUgTRNroK opf43VC7hyGNuIqf5Rh9MNsKA0w53QJIzoMDs0CTNFYDAE3WQbi50pRyjU8JpdQP1XnG 8s/gwsqEN7WXg0USwrck/ByCzSp2KR9OKIBpgZnE0YBaoT5Pu3qZLyhj4V49CMXM7wjM I5XqpO31PskJQgotzYyR+g5u9WFB8eGqqo2uRJJS+L7soAM20gESzivW9nNeeVs5h3GB ITnA== X-Forwarded-Encrypted: i=1; AJvYcCWRlyIlFYav1k6jbiYmv/wCYKjErNYufBfj2VrkAnuEntQbih1lc3sSqKkN5VfYm5ciSYWNJv5JDUPiLUM=@vger.kernel.org X-Gm-Message-State: AOJu0YyTLKNfbJw9TYxRlbGInauiHhup08zhgrScAgAKILxftVjfcHkg S1LzPporudCtcDSi4HGbKETe+LCC7IztLVv3LbdvZipD3O8VvO6FZLZc8bbjGq+cKb0= X-Gm-Gg: ASbGncuQku+IvpbRxyyr8K/3KSKBsDyBbSFYpd3Nb+wGChsTC10ERmAuFZI2WMOVolm CIC19WG6HCzmajtxTRe8fwT6ZjPUGV6kw9G8s7Etgo7B0w1JclwxGU7PZ7loX0r7zJTZdSOGUNq cMaRebQFQDNQrt4Vb/IZqCjSLmCrUUSLSt7GD2Ga1vh7RGgI00GF6NVCE/YoY8CkpYQ6ix0KufW xFyBrASANBjyMDqu5EKOZD4iYN7NKVHpOLU8RoNMa6a3y+ivGSjvxhWZ6c+gN37xtDkBwg5mQKq Qgudl8CRxQt1CBXkDvINrhCUPAWVTWTjA6IojF6bD06jTvjxoK9StqWb5sVLqvxsAG2nWscrEYz V4hpcXZthhlaMFmu1M5eOqb5rypylnUAaBwn0q+JX40zgNixEzz+Z/r2/r3bB4iI19EAx2R8CVl Eq4c1kP9iDPKaRVeT6ee9hsE2jb6useDOf+0To/tra44B1n5uofgK4KdXfOtvaiB49ldXbAZky4 NivkGE= X-Google-Smtp-Source: AGHT+IHN/bqQujj97o7Kww6yNHe38G1kUWdzwx5c5mN0lAR5U4+eSffj5fl1HMsXAqdxCzb+WirFLQ== X-Received: by 2002:a05:690e:154f:10b0:641:f5bc:699c with SMTP id 956f58d0204a3-64302b38528mr4057472d50.74.1763850264543; Sat, 22 Nov 2025 14:24:24 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:24 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 14/22] mm: memfd_luo: allow preserving memfd Date: Sat, 22 Nov 2025 17:23:41 -0500 Message-ID: <20251122222351.1059049-15-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" From: Pratyush Yadav The ability to preserve a memfd allows userspace to use KHO and LUO to transfer its memory contents to the next kernel. This is useful in many ways. For one, it can be used with IOMMUFD as the backing store for IOMMU page tables. Preserving IOMMUFD is essential for performing a hypervisor live update with passthrough devices. memfd support provides the first building block for making that possible. For another, applications with a large amount of memory that takes time to reconstruct, reboots to consume kernel upgrades can be very expensive. memfd with LUO gives those applications reboot-persistent memory that they can use to quickly save and reconstruct that state. While memfd is backed by either hugetlbfs or shmem, currently only support on shmem is added. To be more precise, support for anonymous shmem files is added. The handover to the next kernel is not transparent. All the properties of the file are not preserved; only its memory contents, position, and size. The recreated file gets the UID and GID of the task doing the restore, and the task's cgroup gets charged with the memory. Once preserved, the file cannot grow or shrink, and all its pages are pinned to avoid migrations and swapping. The file can still be read from or written to. Use vmalloc to get the buffer to hold the folios, and preserve it using kho_preserve_vmalloc(). This doesn't have the size limit. Signed-off-by: Pratyush Yadav Co-developed-by: Pasha Tatashin Signed-off-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) --- MAINTAINERS | 2 + include/linux/kho/abi/memfd.h | 77 +++++ mm/Makefile | 1 + mm/memfd_luo.c | 517 ++++++++++++++++++++++++++++++++++ 4 files changed, 597 insertions(+) create mode 100644 include/linux/kho/abi/memfd.h create mode 100644 mm/memfd_luo.c diff --git a/MAINTAINERS b/MAINTAINERS index 868d3d23fdea..425c46bba764 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14469,6 +14469,7 @@ F: tools/testing/selftests/livepatch/ LIVE UPDATE M: Pasha Tatashin M: Mike Rapoport +R: Pratyush Yadav L: linux-kernel@vger.kernel.org S: Maintained F: Documentation/core-api/liveupdate.rst @@ -14477,6 +14478,7 @@ F: include/linux/liveupdate.h F: include/linux/liveupdate/ F: include/uapi/linux/liveupdate.h F: kernel/liveupdate/ +F: mm/memfd_luo.c =20 LLC (802.2) L: netdev@vger.kernel.org diff --git a/include/linux/kho/abi/memfd.h b/include/linux/kho/abi/memfd.h new file mode 100644 index 000000000000..da7d063474a1 --- /dev/null +++ b/include/linux/kho/abi/memfd.h @@ -0,0 +1,77 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + * + * Copyright (C) 2025 Amazon.com Inc. or its affiliates. + * Pratyush Yadav + */ + +#ifndef _LINUX_KHO_ABI_MEMFD_H +#define _LINUX_KHO_ABI_MEMFD_H + +#include +#include + +/** + * DOC: memfd Live Update ABI + * + * This header defines the ABI for preserving the state of a memfd across a + * kexec reboot using the LUO. + * + * The state is serialized into a packed structure `struct memfd_luo_ser` + * which is handed over to the next kernel via the KHO mechanism. + * + * This interface is a contract. Any modification to the structure layout + * constitutes a breaking change. Such changes require incrementing the + * version number in the MEMFD_LUO_FH_COMPATIBLE string. + */ + +/** + * MEMFD_LUO_FOLIO_DIRTY - The folio is dirty. + * + * This flag indicates the folio contains data from user. A non-dirty foli= o is + * one that was allocated (say using fallocate(2)) but not written to. + */ +#define MEMFD_LUO_FOLIO_DIRTY BIT(0) + +/** + * MEMFD_LUO_FOLIO_UPTODATE - The folio is up-to-date. + * + * An up-to-date folio has been zeroed out. shmem zeroes out folios on fir= st + * use. This flag tracks which folios need zeroing. + */ +#define MEMFD_LUO_FOLIO_UPTODATE BIT(1) + +/** + * struct memfd_luo_folio_ser - Serialized state of a single folio. + * @pfn: The page frame number of the folio. + * @flags: Flags to describe the state of the folio. + * @index: The page offset (pgoff_t) of the folio within the original = file. + */ +struct memfd_luo_folio_ser { + u64 pfn:52; + u64 flags:12; + u64 index; +} __packed; + +/** + * struct memfd_luo_ser - Main serialization structure for a memfd. + * @pos: The file's current position (f_pos). + * @size: The total size of the file in bytes (i_size). + * @nr_folios: Number of folios in the folios array. + * @folios: KHO vmalloc descriptor pointing to the array of + * struct memfd_luo_folio_ser. + */ +struct memfd_luo_ser { + u64 pos; + u64 size; + u64 nr_folios; + struct kho_vmalloc folios; +} __packed; + +/* The compatibility string for memfd file handler */ +#define MEMFD_LUO_FH_COMPATIBLE "memfd-v1" + +#endif /* _LINUX_KHO_ABI_MEMFD_H */ diff --git a/mm/Makefile b/mm/Makefile index 21abb3353550..7738ec416f00 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -100,6 +100,7 @@ obj-$(CONFIG_NUMA) +=3D memory-tiers.o obj-$(CONFIG_DEVICE_MIGRATION) +=3D migrate_device.o obj-$(CONFIG_TRANSPARENT_HUGEPAGE) +=3D huge_memory.o khugepaged.o obj-$(CONFIG_PAGE_COUNTER) +=3D page_counter.o +obj-$(CONFIG_LIVEUPDATE) +=3D memfd_luo.o obj-$(CONFIG_MEMCG_V1) +=3D memcontrol-v1.o obj-$(CONFIG_MEMCG) +=3D memcontrol.o vmpressure.o ifdef CONFIG_SWAP diff --git a/mm/memfd_luo.c b/mm/memfd_luo.c new file mode 100644 index 000000000000..fcec4678a644 --- /dev/null +++ b/mm/memfd_luo.c @@ -0,0 +1,517 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + * + * Copyright (C) 2025 Amazon.com Inc. or its affiliates. + * Pratyush Yadav + */ + +/** + * DOC: Memfd Preservation via LUO + * + * Overview + * =3D=3D=3D=3D=3D=3D=3D=3D + * + * Memory file descriptors (memfd) can be preserved over a kexec using the= Live + * Update Orchestrator (LUO) file preservation. This allows userspace to + * transfer its memory contents to the next kernel after a kexec. + * + * The preservation is not intended to be transparent. Only select propert= ies of + * the file are preserved. All others are reset to default. The preserved + * properties are described below. + * + * .. note:: + * The LUO API is not stabilized yet, so the preserved properties of a = memfd + * are also not stable and are subject to backwards incompatible change= s. + * + * .. note:: + * Currently a memfd backed by Hugetlb is not supported. Memfds created + * with ``MFD_HUGETLB`` will be rejected. + * + * Preserved Properties + * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + * + * The following properties of the memfd are preserved across kexec: + * + * File Contents + * All data stored in the file is preserved. + * + * File Size + * The size of the file is preserved. Holes in the file are filled by + * allocating pages for them during preservation. + * + * File Position + * The current file position is preserved, allowing applications to cont= inue + * reading/writing from their last position. + * + * File Status Flags + * memfds are always opened with ``O_RDWR`` and ``O_LARGEFILE``. This pr= operty + * is maintained. + * + * Non-Preserved Properties + * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D + * + * All properties which are not preserved must be assumed to be reset to + * default. This section describes some of those properties which may be m= ore of + * note. + * + * ``FD_CLOEXEC`` flag + * A memfd can be created with the ``MFD_CLOEXEC`` flag that sets the + * ``FD_CLOEXEC`` on the file. This flag is not preserved and must be set + * again after restore via ``fcntl()``. + * + * Seals + * File seals are not preserved. The file is unsealed on restore and if + * needed, must be sealed again via ``fcntl()``. + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "internal.h" + +static int memfd_luo_preserve_folios(struct file *file, + struct kho_vmalloc *kho_vmalloc, + struct memfd_luo_folio_ser **out_folios_ser, + u64 *nr_foliosp) +{ + struct inode *inode =3D file_inode(file); + struct memfd_luo_folio_ser *folios_ser; + unsigned int max_folios; + long i, size, nr_pinned; + struct folio **folios; + int err =3D -EINVAL; + pgoff_t offset; + u64 nr_folios; + + size =3D i_size_read(inode); + /* + * If the file has zero size, then the folios and nr_folios properties + * are not set. + */ + if (!size) { + *nr_foliosp =3D 0; + *out_folios_ser =3D NULL; + memset(kho_vmalloc, 0, sizeof(*kho_vmalloc)); + return 0; + } + + /* + * Guess the number of folios based on inode size. Real number might end + * up being smaller if there are higher order folios. + */ + max_folios =3D PAGE_ALIGN(size) / PAGE_SIZE; + folios =3D kvmalloc_array(max_folios, sizeof(*folios), GFP_KERNEL); + if (!folios) + return -ENOMEM; + + /* + * Pin the folios so they don't move around behind our back. This also + * ensures none of the folios are in CMA -- which ensures they don't + * fall in KHO scratch memory. It also moves swapped out folios back to + * memory. + * + * A side effect of doing this is that it allocates a folio for all + * indices in the file. This might waste memory on sparse memfds. If + * that is really a problem in the future, we can have a + * memfd_pin_folios() variant that does not allocate a page on empty + * slots. + */ + nr_pinned =3D memfd_pin_folios(file, 0, size - 1, folios, max_folios, + &offset); + if (nr_pinned < 0) { + err =3D nr_pinned; + pr_err("failed to pin folios: %d\n", err); + goto err_free_folios; + } + nr_folios =3D nr_pinned; + + folios_ser =3D vcalloc(nr_folios, sizeof(*folios_ser)); + if (!folios_ser) { + err =3D -ENOMEM; + goto err_unpin; + } + + for (i =3D 0; i < nr_folios; i++) { + struct memfd_luo_folio_ser *pfolio =3D &folios_ser[i]; + struct folio *folio =3D folios[i]; + unsigned int flags =3D 0; + + err =3D kho_preserve_folio(folio); + if (err) + goto err_unpreserve; + + if (folio_test_dirty(folio)) + flags |=3D MEMFD_LUO_FOLIO_DIRTY; + if (folio_test_uptodate(folio)) + flags |=3D MEMFD_LUO_FOLIO_UPTODATE; + + pfolio->pfn =3D folio_pfn(folio); + pfolio->flags =3D flags; + pfolio->index =3D folio->index; + } + + err =3D kho_preserve_vmalloc(folios_ser, kho_vmalloc); + if (err) + goto err_unpreserve; + + kvfree(folios); + *nr_foliosp =3D nr_folios; + *out_folios_ser =3D folios_ser; + + /* + * Note: folios_ser is purposely not freed here. It is preserved + * memory (via KHO). In the 'unpreserve' path, we use the vmap pointer + * that is passed via private_data. + */ + return 0; + +err_unpreserve: + for (i =3D i - 1; i >=3D 0; i--) + kho_unpreserve_folio(folios[i]); + vfree(folios_ser); +err_unpin: + unpin_folios(folios, nr_folios); +err_free_folios: + kvfree(folios); + + return err; +} + +static void memfd_luo_unpreserve_folios(struct kho_vmalloc *kho_vmalloc, + struct memfd_luo_folio_ser *folios_ser, + u64 nr_folios) +{ + long i; + + if (!nr_folios) + return; + + kho_unpreserve_vmalloc(kho_vmalloc); + + for (i =3D 0; i < nr_folios; i++) { + const struct memfd_luo_folio_ser *pfolio =3D &folios_ser[i]; + struct folio *folio; + + if (!pfolio->pfn) + continue; + + folio =3D pfn_folio(pfolio->pfn); + + kho_unpreserve_folio(folio); + unpin_folio(folio); + } + + vfree(folios_ser); +} + +static int memfd_luo_preserve(struct liveupdate_file_op_args *args) +{ + struct inode *inode =3D file_inode(args->file); + struct memfd_luo_folio_ser *folios_ser; + struct memfd_luo_ser *ser; + u64 nr_folios; + int err =3D 0; + + inode_lock(inode); + shmem_freeze(inode, true); + + /* Allocate the main serialization structure in preserved memory */ + ser =3D kho_alloc_preserve(sizeof(*ser)); + if (IS_ERR(ser)) { + err =3D PTR_ERR(ser); + goto err_unlock; + } + + ser->pos =3D args->file->f_pos; + ser->size =3D i_size_read(inode); + + err =3D memfd_luo_preserve_folios(args->file, &ser->folios, + &folios_ser, &nr_folios); + if (err) + goto err_free_ser; + + ser->nr_folios =3D nr_folios; + inode_unlock(inode); + + args->private_data =3D folios_ser; + args->serialized_data =3D virt_to_phys(ser); + + return 0; + +err_free_ser: + kho_unpreserve_free(ser); +err_unlock: + shmem_freeze(inode, false); + inode_unlock(inode); + return err; +} + +static int memfd_luo_freeze(struct liveupdate_file_op_args *args) +{ + struct memfd_luo_ser *ser; + + if (WARN_ON_ONCE(!args->serialized_data)) + return -EINVAL; + + ser =3D phys_to_virt(args->serialized_data); + + /* + * The pos might have changed since prepare. Everything else stays the + * same. + */ + ser->pos =3D args->file->f_pos; + + return 0; +} + +static void memfd_luo_unpreserve(struct liveupdate_file_op_args *args) +{ + struct inode *inode =3D file_inode(args->file); + struct memfd_luo_ser *ser; + + if (WARN_ON_ONCE(!args->serialized_data)) + return; + + inode_lock(inode); + shmem_freeze(inode, false); + + ser =3D phys_to_virt(args->serialized_data); + + memfd_luo_unpreserve_folios(&ser->folios, args->private_data, + ser->nr_folios); + + kho_unpreserve_free(ser); + inode_unlock(inode); +} + +static void memfd_luo_discard_folios(const struct memfd_luo_folio_ser *fol= ios_ser, + u64 nr_folios) +{ + u64 i; + + for (i =3D 0; i < nr_folios; i++) { + const struct memfd_luo_folio_ser *pfolio =3D &folios_ser[i]; + struct folio *folio; + phys_addr_t phys; + + if (!pfolio->pfn) + continue; + + phys =3D PFN_PHYS(pfolio->pfn); + folio =3D kho_restore_folio(phys); + if (!folio) { + pr_warn_ratelimited("Unable to restore folio at physical address: %llx\= n", + phys); + continue; + } + + folio_put(folio); + } +} + +static void memfd_luo_finish(struct liveupdate_file_op_args *args) +{ + struct memfd_luo_folio_ser *folios_ser; + struct memfd_luo_ser *ser; + + if (args->retrieved) + return; + + ser =3D phys_to_virt(args->serialized_data); + if (!ser) + return; + + if (ser->nr_folios) { + folios_ser =3D kho_restore_vmalloc(&ser->folios); + if (!folios_ser) + goto out; + + memfd_luo_discard_folios(folios_ser, ser->nr_folios); + vfree(folios_ser); + } + +out: + kho_restore_free(ser); +} + +static int memfd_luo_retrieve_folios(struct file *file, + struct memfd_luo_folio_ser *folios_ser, + u64 nr_folios) +{ + struct inode *inode =3D file_inode(file); + struct address_space *mapping =3D inode->i_mapping; + struct folio *folio; + long i =3D 0; + int err; + + for (; i < nr_folios; i++) { + const struct memfd_luo_folio_ser *pfolio =3D &folios_ser[i]; + phys_addr_t phys; + u64 index; + int flags; + + if (!pfolio->pfn) + continue; + + phys =3D PFN_PHYS(pfolio->pfn); + folio =3D kho_restore_folio(phys); + if (!folio) { + pr_err("Unable to restore folio at physical address: %llx\n", + phys); + goto put_folios; + } + index =3D pfolio->index; + flags =3D pfolio->flags; + + /* Set up the folio for insertion. */ + __folio_set_locked(folio); + __folio_set_swapbacked(folio); + + err =3D mem_cgroup_charge(folio, NULL, mapping_gfp_mask(mapping)); + if (err) { + pr_err("shmem: failed to charge folio index %ld: %d\n", + i, err); + goto unlock_folio; + } + + err =3D shmem_add_to_page_cache(folio, mapping, index, NULL, + mapping_gfp_mask(mapping)); + if (err) { + pr_err("shmem: failed to add to page cache folio index %ld: %d\n", + i, err); + goto unlock_folio; + } + + if (flags & MEMFD_LUO_FOLIO_UPTODATE) + folio_mark_uptodate(folio); + if (flags & MEMFD_LUO_FOLIO_DIRTY) + folio_mark_dirty(folio); + + err =3D shmem_inode_acct_blocks(inode, 1); + if (err) { + pr_err("shmem: failed to account folio index %ld: %d\n", + i, err); + goto unlock_folio; + } + + shmem_recalc_inode(inode, 1, 0); + folio_add_lru(folio); + folio_unlock(folio); + folio_put(folio); + } + + return 0; + +unlock_folio: + folio_unlock(folio); + folio_put(folio); + i++; +put_folios: + /* + * Note: don't free the folios already added to the file. They will be + * freed when the file is freed. Free the ones not added yet here. + */ + for (; i < nr_folios; i++) { + const struct memfd_luo_folio_ser *pfolio =3D &folios_ser[i]; + + folio =3D kho_restore_folio(pfolio->pfn); + if (folio) + folio_put(folio); + } + + return err; +} + +static int memfd_luo_retrieve(struct liveupdate_file_op_args *args) +{ + struct memfd_luo_folio_ser *folios_ser; + struct memfd_luo_ser *ser; + struct file *file; + int err; + + ser =3D phys_to_virt(args->serialized_data); + if (!ser) + return -EINVAL; + + file =3D shmem_file_setup("", 0, VM_NORESERVE); + + if (IS_ERR(file)) { + pr_err("failed to setup file: %pe\n", file); + return PTR_ERR(file); + } + + vfs_setpos(file, ser->pos, MAX_LFS_FILESIZE); + file->f_inode->i_size =3D ser->size; + + if (ser->nr_folios) { + folios_ser =3D kho_restore_vmalloc(&ser->folios); + if (!folios_ser) { + err =3D -EINVAL; + goto put_file; + } + + err =3D memfd_luo_retrieve_folios(file, folios_ser, ser->nr_folios); + vfree(folios_ser); + if (err) + goto put_file; + } + + args->file =3D file; + kho_restore_free(ser); + + return 0; + +put_file: + fput(file); + + return err; +} + +static bool memfd_luo_can_preserve(struct liveupdate_file_handler *handler, + struct file *file) +{ + struct inode *inode =3D file_inode(file); + + return shmem_file(file) && !inode->i_nlink; +} + +static const struct liveupdate_file_ops memfd_luo_file_ops =3D { + .freeze =3D memfd_luo_freeze, + .finish =3D memfd_luo_finish, + .retrieve =3D memfd_luo_retrieve, + .preserve =3D memfd_luo_preserve, + .unpreserve =3D memfd_luo_unpreserve, + .can_preserve =3D memfd_luo_can_preserve, + .owner =3D THIS_MODULE, +}; + +static struct liveupdate_file_handler memfd_luo_handler =3D { + .ops =3D &memfd_luo_file_ops, + .compatible =3D MEMFD_LUO_FH_COMPATIBLE, +}; + +static int __init memfd_luo_init(void) +{ + int err =3D liveupdate_register_file_handler(&memfd_luo_handler); + + if (err && err !=3D -EOPNOTSUPP) { + pr_err("Could not register luo filesystem handler: %pe\n", + ERR_PTR(err)); + + return err; + } + + return 0; +} +late_initcall(memfd_luo_init); --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (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 656D42E2846 for ; Sat, 22 Nov 2025 22:24:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850270; cv=none; b=nvthDVTadD4A9V9YC9PDq9wxGnHnRL7co43cXJxldFRnFQefFfZt1vmtKzmL8tdlTAsJdAyRZCrh83YbH5vdMXdBH0y15MzWIgtqrhMvYr7ZVArdcmiLbIOuvAvubcJOubuqo7UqhQHeu0/owNwVt1w5zAHV5ClLzrivcowALZ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850270; c=relaxed/simple; bh=HckDMYe0+V7dIt+99RvEfUMzQcIYTFKI66vYhRUdjQA=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lje2L7RbG/y7nDRTNf9EtvL0snzdDPrvOZmxhzwWlS48ZuyneLLXBwIoqMh2D/KtIdPB+1vRjZQrGQBKr4DKvBxFvNqZYD0/hP3FrT4yJ+fsTInBVxEncRqYNDKMP1IaBZRr8ueNWO81DQb94m3iVMJZXezEcmrUlQyba8YxLto= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=kUNpuhcm; arc=none smtp.client-ip=209.85.128.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="kUNpuhcm" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-7881b67da53so31578067b3.1 for ; Sat, 22 Nov 2025 14:24:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850266; x=1764455066; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=8oZaYRe5g0R4lweOQLAK81RG676DLPyUWLh/z30V3oE=; b=kUNpuhcmhJRzDdbopx+yw1ynUATxeENtPf4tOwHPPENr78Xz1DbY8zuYNGC0GNDPTa ZSk0TiTXYZP4GoAEH9ga0HJqU7yWWdru/Vbpyg2wwwpx4nYuC2OigJLFMpv3MSCnRcss 8VgrDTkHGCLQ2t+gaP6n4rUES77FYvAgAmH9IZYo/H9rQV17xQZyJy7R5ks7GABKkWi8 Grd5BW4wn5TyZ4RKzBrN6WPo8lbCWq4ImT2WC5mW72L4pS8dme6t1iJHvyM6T+b99QKd M64TuQZrRsHu4DEhr8Ss9BfKanjC9absBmTvPnBPvDDnPORW8ckvh/ZjV71fAGyb5XFP Xi9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850266; x=1764455066; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=8oZaYRe5g0R4lweOQLAK81RG676DLPyUWLh/z30V3oE=; b=EMaXlS7xxD/JtkIKkAa9XdKyvgmiyEdO+F8pEcg2kBEZOyzzq7XAN2ti1FRxo0o5tB Sk4a1bUhs8CBQWoGSOIT5QNu6bb0GfdtBLh+jY+cDADTdZFiDm8nk368vsE5YKNQkclp dQuGi0sBOFNPWr3nWYab0RRpDKUT69wizVg/Y8MJOtBmpEyG8giwNPyJS2KOsxFS85Px zbGtBF2TBgTUSOjos/Is2xLbcR4TyySyJUzglEF9n9cqTxqe3eVqJzbiAPkgFHUI9v9r nWUuJxmF9BaZSw6OUE3sMIs6FCB+uqAuVHf2qGxVwu8IkV8DL2nXzP3b8MOH6KTxVM/F ngag== X-Forwarded-Encrypted: i=1; AJvYcCUutzh8WyIrLlIC1tOwf9gcvt2di/LG7K3gXLLcTAr2sGIw+B1dSSyOmxBGbDmNoBwS+Wj+7h5tjLbUpR4=@vger.kernel.org X-Gm-Message-State: AOJu0Yy/IUJZBJQTfsaPVY2q+2kSKjyYN+NltEsgKtOpS6gbl794QChi cuTcSx69Otzk2JFHvCgrcOcDCd5L7ITxdrVq1CxrN4dmWxQTmOsKw6EYx58WW6+fP0M= X-Gm-Gg: ASbGncvgP0Rg2/jlqpIB0ydpAJGaszYaK3WQKqKRpD811TBEQ9kpcGI0ffKrueU85wp fdVXgapnQnZYHkKuCKnbBqnr1I4PQuPxBI7uuxKTH+4Vcf+9rre7mV1GYZop5bhPQ+JhgQI7QIz ATMTm8Mi5//awfQSkJdrzSjjju/AeOmVuONjq5ajjqGt4k7bxhKnSuNUuD2tX/0/cxPVhO8tEdn DFG0mo/ucz+2+nN5jgyLmj65u+isrCcIEIeRvpDdTMzcqWnFgKo9TNEQn3Ch1IqDOOkUEZsxn48 rZ7Xs5XdBUN+pzDGHIr0P33Sz52o4dpXAF4O0wBzTOf4hosZzoCRw3krOCinN1JjmgpN6yME0UW MH1kYZy9LBsDWHfR0pbygnC3ZSD6IPvim6Gn9wu6t4R6J0OATFHQLFjnAnheUGnD+lan2IhJSnK AGox6/SxMI0GEjPGooO6YvuyQ74zsIH7Z8xRU2q0wGr50N+W3mHNsL4riUggqWifv6BGsmXNVwq CZ4kWZO/firmy4kHg== X-Google-Smtp-Source: AGHT+IFA8xiqyidX6vSHxt0Fa6KY7zq1D81H8XmTPSjBAk/TG7qP+fvxB9otYVm16cp+k+p4xO9fVA== X-Received: by 2002:a05:690e:118d:b0:63f:b634:4224 with SMTP id 956f58d0204a3-64302a427cemr5083872d50.21.1763850266314; Sat, 22 Nov 2025 14:24:26 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:25 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 15/22] docs: add documentation for memfd preservation via LUO Date: Sat, 22 Nov 2025 17:23:42 -0500 Message-ID: <20251122222351.1059049-16-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" From: Pratyush Yadav Add the documentation under the "Preserving file descriptors" section of LUO's documentation. Signed-off-by: Pratyush Yadav Co-developed-by: Pasha Tatashin Signed-off-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) --- Documentation/core-api/liveupdate.rst | 7 +++++++ Documentation/mm/index.rst | 1 + Documentation/mm/memfd_preservation.rst | 23 +++++++++++++++++++++++ MAINTAINERS | 1 + 4 files changed, 32 insertions(+) create mode 100644 Documentation/mm/memfd_preservation.rst diff --git a/Documentation/core-api/liveupdate.rst b/Documentation/core-api= /liveupdate.rst index e1f0c13d5b4a..b776b625c60f 100644 --- a/Documentation/core-api/liveupdate.rst +++ b/Documentation/core-api/liveupdate.rst @@ -23,6 +23,13 @@ Live Update Orchestrator ABI .. kernel-doc:: include/linux/kho/abi/luo.h :doc: Live Update Orchestrator ABI =20 +The following types of file descriptors can be preserved + +.. toctree:: + :maxdepth: 1 + + ../mm/memfd_preservation + Public API =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D .. kernel-doc:: include/linux/liveupdate.h diff --git a/Documentation/mm/index.rst b/Documentation/mm/index.rst index ba6a8872849b..7aa2a8886908 100644 --- a/Documentation/mm/index.rst +++ b/Documentation/mm/index.rst @@ -48,6 +48,7 @@ documentation, or deleted if it has served its purpose. hugetlbfs_reserv ksm memory-model + memfd_preservation mmu_notifier multigen_lru numa diff --git a/Documentation/mm/memfd_preservation.rst b/Documentation/mm/mem= fd_preservation.rst new file mode 100644 index 000000000000..66e0fb6d5ef0 --- /dev/null +++ b/Documentation/mm/memfd_preservation.rst @@ -0,0 +1,23 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D +Memfd Preservation via LUO +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D + +.. kernel-doc:: mm/memfd_luo.c + :doc: Memfd Preservation via LUO + +Memfd Preservation ABI +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +.. kernel-doc:: include/linux/kho/abi/memfd.h + :doc: DOC: memfd Live Update ABI + +.. kernel-doc:: include/linux/kho/abi/memfd.h + :internal: + +See Also +=3D=3D=3D=3D=3D=3D=3D=3D + +- :doc:`/core-api/liveupdate` +- :doc:`/core-api/kho/concepts` diff --git a/MAINTAINERS b/MAINTAINERS index 425c46bba764..cabbf30d50e1 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14473,6 +14473,7 @@ R: Pratyush Yadav L: linux-kernel@vger.kernel.org S: Maintained F: Documentation/core-api/liveupdate.rst +F: Documentation/mm/memfd_preservation.rst F: Documentation/userspace-api/liveupdate.rst F: include/linux/liveupdate.h F: include/linux/liveupdate/ --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yx1-f48.google.com (mail-yx1-f48.google.com [74.125.224.48]) (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 D56C22D0C92 for ; Sat, 22 Nov 2025 22:24:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850274; cv=none; b=NZJf/wfxSPO1eRLt1tiL0wlXh0LucJQGm7Om/+yM+4VVJGF4f2Mm+CEvwlLRIvXaOMNbQloVmJhEQBkYUIjLyRsJKf+fvJ4jttaB3Uf/9owTHEY2PhOuk60GGJjMfqmyRR4xycTp+TBVlN+7F26srGFVhhISU0xEFvfTG5LyOac= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850274; c=relaxed/simple; bh=t21FpQnpe7e8dy+sbs2+abPmQFphi4fmb0HjeKOEATg=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WBQBNU69Vaonsca6ajAxXZGO7riIEmFXYBjGP5opRVbZYFqzZ0byBGEwMt0wkjkGQwWnAhM+fa75ceU86tMB9ulvv+m2+B4xshLSjsyTzM3abPGGODM/l+/Il1XrgAwr0F4srNhEpfyrBI3/Ba9qPAiC8sR6JT8qZp0WOIG6OD8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=KDk4xEk/; arc=none smtp.client-ip=74.125.224.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="KDk4xEk/" Received: by mail-yx1-f48.google.com with SMTP id 956f58d0204a3-63fc8c337f2so2979416d50.0 for ; Sat, 22 Nov 2025 14:24:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850268; x=1764455068; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=oN/lyGO3w6uNr+x1AiuxSkRfzjl2Tv5KHczYeMQDvoM=; b=KDk4xEk/fDeoLgueRl4sFlBDmTgv8LzP1i0zjtyoVyTfVB4h1JxqVYaGwU/KktynMg Mq1QprPWZkbbpHD5S8+7G/BbAb2x5ve+yCz6JtHKoOX547OVdXH/kUcb4sBrg9hexGik r8z7yZxnbE+zgLntbqX9r9/ow5MoaDieUROEsNQelwadLYL691XMVgDOedcYyKQPIiYw dUgXVQxBU5BhCZYTxR+FeDjUwTn3Te7BiMZW9ZxVIE2eEo3GC16Ce3fnp4AVS/gd6z1H Xb6H4ijBwVQStvjVQnI4kfxKhxGyqlcTOeMyRypqqiNgoPsk/X5ujK3g9YmRWm4hVXak PcjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850268; x=1764455068; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=oN/lyGO3w6uNr+x1AiuxSkRfzjl2Tv5KHczYeMQDvoM=; b=DmDcwbirlq68hcys/OWJEO9VbcZrAtwNzhbwhphDnYZPsHWfIBrmOh7fq5OBCOtfgo gkJZr8jDgJmn6a9Ra0p1t4430fy88cKtrvWRePQgzInU33v74NAP6l+MVlvZF4qs7G5X WjkEvxsgAwqse+D20/7JgADszLlJ0RqLlMlcn+wsCxoSEooiUVlnNXjdvQkiBk4TvSxz 7WrGVPs1Ha5fJ+3TFFlJl1imQqGIRseWXw5KcRIPSfIZag1kVNnppg4KD0uaqsya1bon sNSe1cOmMFMB3hblFlo2ghic+P2pldz+YfKx0bfKjAU8LwYAHs+wYQ8cUZeBqTKEll0q 0LyQ== X-Forwarded-Encrypted: i=1; AJvYcCUnDh36trn1bv0X+VhbMf4V2gWTsXsxueQdTSCwBtpBvDj4ia9WGKRoQw2K6uPbx/5xOfXkHmRpQpNNyJU=@vger.kernel.org X-Gm-Message-State: AOJu0YyZ+1JCnwJjbegK6d9tl3eP3oD1nOPQJu16hKgpENyuO3EZBqAU LyoHjyMVMgQ7AS0HFRoH9Lnbzle7RooFTM1gVIIF5rwtUjpI6RMd3N+PT47yC6wm464= X-Gm-Gg: ASbGncu/2XgUVj/9nDQ/JL47vmeRRBYdvl0lEyvpj/udl25NWp0wrRGt+9Zfhjj/uom I0W00KtjbHBRRU+75p5TkuAVm/k5jKm83GRQoY8WQ9wOhZjA0O7wVPm8jaluhSC9d5U344OoBhb qrRECTTS9kLQowv0aan+yBMHextTzsI6MQrT350l3zgzMPlRiOPvwYwPchZiaxDjgtFuokPzhC7 zuwtd8SqgaeQsEZS+C9qHVYBkxl4PMJd0GQECO9rvl9avs/eqxJAadrTpwuu2uto5M/m1XBeF7S 0R47T66bsHVbkKA3cB152/0YE614z35ae6iEX3iTYAjqKOSZX9yuOL561aYw1wtNPMdyR7jlKys tvb840aNdjttDFj1nxW76/ZXY3NxUMnweK4quCfYsbIEGKsOHIAMbf6ahMjwHS8NvvfvfuRZhmM 9reePCHEmf9IpbkTZoj4BhZ4TMS0aiVkVee3b+u6/QMt7H0UATlC6hikqNDfgj71WsVLYOhBxPp A3IepY= X-Google-Smtp-Source: AGHT+IF82eqDIem7nhAJwZNsrqSxav3QJ8c7jd8T4mCUjNEDwxXmd8be6sTBs6ajuflZe7SsNNOkLw== X-Received: by 2002:a05:690c:e1e:b0:786:5afa:375c with SMTP id 00721157ae682-78a8b56859emr99751897b3.67.1763850268245; Sat, 22 Nov 2025 14:24:28 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:27 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 16/22] selftests/liveupdate: Add userspace API selftests Date: Sat, 22 Nov 2025 17:23:43 -0500 Message-ID: <20251122222351.1059049-17-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" Introduce a selftest suite for LUO. These tests validate the core userspace-facing API provided by the /dev/liveupdate device and its associated ioctls. The suite covers fundamental device behavior, session management, and the file preservation mechanism using memfd as a test case. This provides regression testing for the LUO uAPI. The following functionality is verified: Device Access: Basic open and close operations on /dev/liveupdate. Enforcement of exclusive device access (verifying EBUSY on a second open). Session Management: Successful creation of sessions with unique names. Failure to create sessions with duplicate names. File Preservation: Preserving a single memfd and verifying its content remains intact post-preservation. Preserving multiple memfds within a single session, each with unique data. A complex scenario involving multiple sessions, each containing a mix of empty and data-filled memfds. Note: This test suite is limited to verifying the pre-kexec functionality of LUO (e.g., session creation, file preservation). The post-kexec restoration of resources is not covered, as the kselftest framework does not currently support orchestrating a reboot and continuing execution in the new kernel. Signed-off-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) Reviewed-by: Pratyush Yadav --- MAINTAINERS | 1 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/liveupdate/.gitignore | 9 + tools/testing/selftests/liveupdate/Makefile | 27 ++ tools/testing/selftests/liveupdate/config | 11 + .../testing/selftests/liveupdate/liveupdate.c | 348 ++++++++++++++++++ 6 files changed, 397 insertions(+) create mode 100644 tools/testing/selftests/liveupdate/.gitignore create mode 100644 tools/testing/selftests/liveupdate/Makefile create mode 100644 tools/testing/selftests/liveupdate/config create mode 100644 tools/testing/selftests/liveupdate/liveupdate.c diff --git a/MAINTAINERS b/MAINTAINERS index cabbf30d50e1..83bac6c48c98 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14480,6 +14480,7 @@ F: include/linux/liveupdate/ F: include/uapi/linux/liveupdate.h F: kernel/liveupdate/ F: mm/memfd_luo.c +F: tools/testing/selftests/liveupdate/ =20 LLC (802.2) L: netdev@vger.kernel.org diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Mak= efile index c46ebdb9b8ef..56e44a98d6a5 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -54,6 +54,7 @@ TARGETS +=3D kvm TARGETS +=3D landlock TARGETS +=3D lib TARGETS +=3D livepatch +TARGETS +=3D liveupdate TARGETS +=3D lkdtm TARGETS +=3D lsm TARGETS +=3D membarrier diff --git a/tools/testing/selftests/liveupdate/.gitignore b/tools/testing/= selftests/liveupdate/.gitignore new file mode 100644 index 000000000000..661827083ab6 --- /dev/null +++ b/tools/testing/selftests/liveupdate/.gitignore @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0-only +* +!/**/ +!*.c +!*.h +!*.sh +!.gitignore +!config +!Makefile diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/se= lftests/liveupdate/Makefile new file mode 100644 index 000000000000..620cb4ce85af --- /dev/null +++ b/tools/testing/selftests/liveupdate/Makefile @@ -0,0 +1,27 @@ +# SPDX-License-Identifier: GPL-2.0-only + +TEST_GEN_PROGS +=3D liveupdate + +include ../lib.mk + +CFLAGS +=3D $(KHDR_INCLUDES) +CFLAGS +=3D -Wall -O2 -Wno-unused-function +CFLAGS +=3D -MD + +LIB_O :=3D $(patsubst %.c, $(OUTPUT)/%.o, $(LIB_C)) +TEST_O :=3D $(patsubst %, %.o, $(TEST_GEN_PROGS)) +TEST_O +=3D $(patsubst %, %.o, $(TEST_GEN_PROGS_EXTENDED)) + +TEST_DEP_FILES :=3D $(patsubst %.o, %.d, $(LIB_O)) +TEST_DEP_FILES +=3D $(patsubst %.o, %.d, $(TEST_O)) +-include $(TEST_DEP_FILES) + +$(LIB_O): $(OUTPUT)/%.o: %.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ + +$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/%: %.o $(LIB_O) + $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIB_O) $(LDLIB= S) -o $@ + +EXTRA_CLEAN +=3D $(LIB_O) +EXTRA_CLEAN +=3D $(TEST_O) +EXTRA_CLEAN +=3D $(TEST_DEP_FILES) diff --git a/tools/testing/selftests/liveupdate/config b/tools/testing/self= tests/liveupdate/config new file mode 100644 index 000000000000..91d03f9a6a39 --- /dev/null +++ b/tools/testing/selftests/liveupdate/config @@ -0,0 +1,11 @@ +CONFIG_BLK_DEV_INITRD=3Dy +CONFIG_KEXEC_FILE=3Dy +CONFIG_KEXEC_HANDOVER=3Dy +CONFIG_KEXEC_HANDOVER_ENABLE_DEFAULT=3Dy +CONFIG_KEXEC_HANDOVER_DEBUGFS=3Dy +CONFIG_KEXEC_HANDOVER_DEBUG=3Dy +CONFIG_LIVEUPDATE=3Dy +CONFIG_LIVEUPDATE_TEST=3Dy +CONFIG_MEMFD_CREATE=3Dy +CONFIG_TMPFS=3Dy +CONFIG_SHMEM=3Dy diff --git a/tools/testing/selftests/liveupdate/liveupdate.c b/tools/testin= g/selftests/liveupdate/liveupdate.c new file mode 100644 index 000000000000..c2878e3d5ef9 --- /dev/null +++ b/tools/testing/selftests/liveupdate/liveupdate.c @@ -0,0 +1,348 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + */ + +/* + * Selftests for the Live Update Orchestrator. + * This test suite verifies the functionality and behavior of the + * /dev/liveupdate character device and its session management capabilitie= s. + * + * Tests include: + * - Device access: basic open/close, and enforcement of exclusive access. + * - Session management: creation of unique sessions, and duplicate name d= etection. + * - Resource preservation: successfully preserving individual and multipl= e memfds, + * verifying contents remain accessible. + * - Complex multi-session scenarios involving mixed empty and populated f= iles. + */ + +#include +#include +#include +#include +#include + +#include + +#include "../kselftest.h" +#include "../kselftest_harness.h" + +#define LIVEUPDATE_DEV "/dev/liveupdate" + +FIXTURE(liveupdate_device) { + int fd1; + int fd2; +}; + +FIXTURE_SETUP(liveupdate_device) +{ + self->fd1 =3D -1; + self->fd2 =3D -1; +} + +FIXTURE_TEARDOWN(liveupdate_device) +{ + if (self->fd1 >=3D 0) + close(self->fd1); + if (self->fd2 >=3D 0) + close(self->fd2); +} + +/* + * Test Case: Basic Open and Close + * + * Verifies that the /dev/liveupdate device can be opened and subsequently + * closed without errors. Skips if the device does not exist. + */ +TEST_F(liveupdate_device, basic_open_close) +{ + self->fd1 =3D open(LIVEUPDATE_DEV, O_RDWR); + + if (self->fd1 < 0 && errno =3D=3D ENOENT) + SKIP(return, "%s does not exist.", LIVEUPDATE_DEV); + + ASSERT_GE(self->fd1, 0); + ASSERT_EQ(close(self->fd1), 0); + self->fd1 =3D -1; +} + +/* + * Test Case: Exclusive Open Enforcement + * + * Verifies that the /dev/liveupdate device can only be opened by one proc= ess + * at a time. It checks that a second attempt to open the device fails with + * the EBUSY error code. + */ +TEST_F(liveupdate_device, exclusive_open) +{ + self->fd1 =3D open(LIVEUPDATE_DEV, O_RDWR); + + if (self->fd1 < 0 && errno =3D=3D ENOENT) + SKIP(return, "%s does not exist.", LIVEUPDATE_DEV); + + ASSERT_GE(self->fd1, 0); + self->fd2 =3D open(LIVEUPDATE_DEV, O_RDWR); + EXPECT_LT(self->fd2, 0); + EXPECT_EQ(errno, EBUSY); +} + +/* Helper function to create a LUO session via ioctl. */ +static int create_session(int lu_fd, const char *name) +{ + struct liveupdate_ioctl_create_session args =3D {}; + + args.size =3D sizeof(args); + strncpy((char *)args.name, name, sizeof(args.name) - 1); + + if (ioctl(lu_fd, LIVEUPDATE_IOCTL_CREATE_SESSION, &args)) + return -errno; + + return args.fd; +} + +/* + * Test Case: Create Duplicate Session + * + * Verifies that attempting to create two sessions with the same name fails + * on the second attempt with EEXIST. + */ +TEST_F(liveupdate_device, create_duplicate_session) +{ + int session_fd1, session_fd2; + + self->fd1 =3D open(LIVEUPDATE_DEV, O_RDWR); + if (self->fd1 < 0 && errno =3D=3D ENOENT) + SKIP(return, "%s does not exist", LIVEUPDATE_DEV); + + ASSERT_GE(self->fd1, 0); + + session_fd1 =3D create_session(self->fd1, "duplicate-session-test"); + ASSERT_GE(session_fd1, 0); + + session_fd2 =3D create_session(self->fd1, "duplicate-session-test"); + EXPECT_LT(session_fd2, 0); + EXPECT_EQ(-session_fd2, EEXIST); + + ASSERT_EQ(close(session_fd1), 0); +} + +/* + * Test Case: Create Distinct Sessions + * + * Verifies that creating two sessions with different names succeeds. + */ +TEST_F(liveupdate_device, create_distinct_sessions) +{ + int session_fd1, session_fd2; + + self->fd1 =3D open(LIVEUPDATE_DEV, O_RDWR); + if (self->fd1 < 0 && errno =3D=3D ENOENT) + SKIP(return, "%s does not exist", LIVEUPDATE_DEV); + + ASSERT_GE(self->fd1, 0); + + session_fd1 =3D create_session(self->fd1, "distinct-session-1"); + ASSERT_GE(session_fd1, 0); + + session_fd2 =3D create_session(self->fd1, "distinct-session-2"); + ASSERT_GE(session_fd2, 0); + + ASSERT_EQ(close(session_fd1), 0); + ASSERT_EQ(close(session_fd2), 0); +} + +static int preserve_fd(int session_fd, int fd_to_preserve, __u64 token) +{ + struct liveupdate_session_preserve_fd args =3D {}; + + args.size =3D sizeof(args); + args.fd =3D fd_to_preserve; + args.token =3D token; + + if (ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, &args)) + return -errno; + + return 0; +} + +/* + * Test Case: Preserve MemFD + * + * Verifies that a valid memfd can be successfully preserved in a session = and + * that its contents remain intact after the preservation call. + */ +TEST_F(liveupdate_device, preserve_memfd) +{ + const char *test_str =3D "hello liveupdate"; + char read_buf[64] =3D {}; + int session_fd, mem_fd; + + self->fd1 =3D open(LIVEUPDATE_DEV, O_RDWR); + if (self->fd1 < 0 && errno =3D=3D ENOENT) + SKIP(return, "%s does not exist", LIVEUPDATE_DEV); + ASSERT_GE(self->fd1, 0); + + session_fd =3D create_session(self->fd1, "preserve-memfd-test"); + ASSERT_GE(session_fd, 0); + + mem_fd =3D memfd_create("test-memfd", 0); + ASSERT_GE(mem_fd, 0); + + ASSERT_EQ(write(mem_fd, test_str, strlen(test_str)), strlen(test_str)); + ASSERT_EQ(preserve_fd(session_fd, mem_fd, 0x1234), 0); + ASSERT_EQ(close(session_fd), 0); + + ASSERT_EQ(lseek(mem_fd, 0, SEEK_SET), 0); + ASSERT_EQ(read(mem_fd, read_buf, sizeof(read_buf)), strlen(test_str)); + ASSERT_STREQ(read_buf, test_str); + ASSERT_EQ(close(mem_fd), 0); +} + +/* + * Test Case: Preserve Multiple MemFDs + * + * Verifies that multiple memfds can be preserved in a single session, + * each with a unique token, and that their contents remain distinct and + * correct after preservation. + */ +TEST_F(liveupdate_device, preserve_multiple_memfds) +{ + const char *test_str1 =3D "data for memfd one"; + const char *test_str2 =3D "data for memfd two"; + char read_buf[64] =3D {}; + int session_fd, mem_fd1, mem_fd2; + + self->fd1 =3D open(LIVEUPDATE_DEV, O_RDWR); + if (self->fd1 < 0 && errno =3D=3D ENOENT) + SKIP(return, "%s does not exist", LIVEUPDATE_DEV); + ASSERT_GE(self->fd1, 0); + + session_fd =3D create_session(self->fd1, "preserve-multi-memfd-test"); + ASSERT_GE(session_fd, 0); + + mem_fd1 =3D memfd_create("test-memfd-1", 0); + ASSERT_GE(mem_fd1, 0); + mem_fd2 =3D memfd_create("test-memfd-2", 0); + ASSERT_GE(mem_fd2, 0); + + ASSERT_EQ(write(mem_fd1, test_str1, strlen(test_str1)), strlen(test_str1)= ); + ASSERT_EQ(write(mem_fd2, test_str2, strlen(test_str2)), strlen(test_str2)= ); + + ASSERT_EQ(preserve_fd(session_fd, mem_fd1, 0xAAAA), 0); + ASSERT_EQ(preserve_fd(session_fd, mem_fd2, 0xBBBB), 0); + + memset(read_buf, 0, sizeof(read_buf)); + ASSERT_EQ(lseek(mem_fd1, 0, SEEK_SET), 0); + ASSERT_EQ(read(mem_fd1, read_buf, sizeof(read_buf)), strlen(test_str1)); + ASSERT_STREQ(read_buf, test_str1); + + memset(read_buf, 0, sizeof(read_buf)); + ASSERT_EQ(lseek(mem_fd2, 0, SEEK_SET), 0); + ASSERT_EQ(read(mem_fd2, read_buf, sizeof(read_buf)), strlen(test_str2)); + ASSERT_STREQ(read_buf, test_str2); + + ASSERT_EQ(close(mem_fd1), 0); + ASSERT_EQ(close(mem_fd2), 0); + ASSERT_EQ(close(session_fd), 0); +} + +/* + * Test Case: Preserve Complex Scenario + * + * Verifies a more complex scenario with multiple sessions and a mix of em= pty + * and non-empty memfds distributed across them. + */ +TEST_F(liveupdate_device, preserve_complex_scenario) +{ + const char *data1 =3D "data for session 1"; + const char *data2 =3D "data for session 2"; + char read_buf[64] =3D {}; + int session_fd1, session_fd2; + int mem_fd_data1, mem_fd_empty1, mem_fd_data2, mem_fd_empty2; + + self->fd1 =3D open(LIVEUPDATE_DEV, O_RDWR); + if (self->fd1 < 0 && errno =3D=3D ENOENT) + SKIP(return, "%s does not exist", LIVEUPDATE_DEV); + ASSERT_GE(self->fd1, 0); + + session_fd1 =3D create_session(self->fd1, "complex-session-1"); + ASSERT_GE(session_fd1, 0); + session_fd2 =3D create_session(self->fd1, "complex-session-2"); + ASSERT_GE(session_fd2, 0); + + mem_fd_data1 =3D memfd_create("data1", 0); + ASSERT_GE(mem_fd_data1, 0); + ASSERT_EQ(write(mem_fd_data1, data1, strlen(data1)), strlen(data1)); + + mem_fd_empty1 =3D memfd_create("empty1", 0); + ASSERT_GE(mem_fd_empty1, 0); + + mem_fd_data2 =3D memfd_create("data2", 0); + ASSERT_GE(mem_fd_data2, 0); + ASSERT_EQ(write(mem_fd_data2, data2, strlen(data2)), strlen(data2)); + + mem_fd_empty2 =3D memfd_create("empty2", 0); + ASSERT_GE(mem_fd_empty2, 0); + + ASSERT_EQ(preserve_fd(session_fd1, mem_fd_data1, 0x1111), 0); + ASSERT_EQ(preserve_fd(session_fd1, mem_fd_empty1, 0x2222), 0); + ASSERT_EQ(preserve_fd(session_fd2, mem_fd_data2, 0x3333), 0); + ASSERT_EQ(preserve_fd(session_fd2, mem_fd_empty2, 0x4444), 0); + + ASSERT_EQ(lseek(mem_fd_data1, 0, SEEK_SET), 0); + ASSERT_EQ(read(mem_fd_data1, read_buf, sizeof(read_buf)), strlen(data1)); + ASSERT_STREQ(read_buf, data1); + + memset(read_buf, 0, sizeof(read_buf)); + ASSERT_EQ(lseek(mem_fd_data2, 0, SEEK_SET), 0); + ASSERT_EQ(read(mem_fd_data2, read_buf, sizeof(read_buf)), strlen(data2)); + ASSERT_STREQ(read_buf, data2); + + ASSERT_EQ(lseek(mem_fd_empty1, 0, SEEK_SET), 0); + ASSERT_EQ(read(mem_fd_empty1, read_buf, sizeof(read_buf)), 0); + + ASSERT_EQ(lseek(mem_fd_empty2, 0, SEEK_SET), 0); + ASSERT_EQ(read(mem_fd_empty2, read_buf, sizeof(read_buf)), 0); + + ASSERT_EQ(close(mem_fd_data1), 0); + ASSERT_EQ(close(mem_fd_empty1), 0); + ASSERT_EQ(close(mem_fd_data2), 0); + ASSERT_EQ(close(mem_fd_empty2), 0); + ASSERT_EQ(close(session_fd1), 0); + ASSERT_EQ(close(session_fd2), 0); +} + +/* + * Test Case: Preserve Unsupported File Descriptor + * + * Verifies that attempting to preserve a file descriptor that does not ha= ve + * a registered Live Update handler fails gracefully. + * Uses /dev/null as a representative of a file type (character device) + * that is not supported by the orchestrator. + */ +TEST_F(liveupdate_device, preserve_unsupported_fd) +{ + int session_fd, unsupported_fd; + int ret; + + self->fd1 =3D open(LIVEUPDATE_DEV, O_RDWR); + if (self->fd1 < 0 && errno =3D=3D ENOENT) + SKIP(return, "%s does not exist", LIVEUPDATE_DEV); + ASSERT_GE(self->fd1, 0); + + session_fd =3D create_session(self->fd1, "unsupported-fd-test"); + ASSERT_GE(session_fd, 0); + + unsupported_fd =3D open("/dev/null", O_RDWR); + ASSERT_GE(unsupported_fd, 0); + + ret =3D preserve_fd(session_fd, unsupported_fd, 0xDEAD); + EXPECT_EQ(ret, -ENOENT); + + ASSERT_EQ(close(unsupported_fd), 0); + ASSERT_EQ(close(session_fd), 0); +} + +TEST_HARNESS_MAIN --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) (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 D5BE52E5407 for ; Sat, 22 Nov 2025 22:24:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850274; cv=none; b=mAWwdfWmfKHpwBpj3bFdgVCkestScm0QtjMBSWcZbXISI+myin90rzJLray+SAqMKHYMI0FDT8zvj8mBqXDj48wrDvNqjihmAP5hACnOtk7Tig/MC8Ahv+KPhwVBvnrMjwJllelwM0MAMQU2k3WTIrR/Bsi5LKQiIqa86r6DMjY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850274; c=relaxed/simple; bh=ncdJ1LKm5KVcpNtfGm3viEYBp5tJUUE6SEacV+bSrCM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=PQT9fieVw4DNDWs6yNliaVplX+IlzpnI51oPUWI40m2Nf/vPTJGDHml72bqmLLLb5TTUxNNB/BS3nja33wuz3mx4jLDoiGTQ1AB4yxtte2sCuXYxbkdlIS8y61JlJN/8CQsO+9pPTqhXUiDfhRgIA6fOpl7seX60sE5JsDcDVwU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=YaSoSPZV; arc=none smtp.client-ip=209.85.128.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="YaSoSPZV" Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-789524e6719so41964387b3.1 for ; Sat, 22 Nov 2025 14:24:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850270; x=1764455070; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=7NyXHP81r6tLJnJ1NWALdU8H7382VS8IXYxSNq72IwU=; b=YaSoSPZV643BAsbmLawt7CY1F42Hw3sGi07Z1cxGbLM0CZxcbn/qVtLTrk54qGMNVd Zi9RPyRhDmzaDIiqaW9LXhNSUVj1GP50Gi4BOMtzzAp+rEpCV1QFyTEyhgD64e0GsVGB JmEZX1Bv81XtqOpoYLsUcaiKWHZghVU6gRzAJz1c5gGfkAI+LzCxMfn0U1hBteLuoo5Y vjUQgjKQ/J8a9HPIcPxEfKGNzA2D4omy8NyV+cva7ly7xKWMzFfhftu3rYQoNRflgtpS NCnZwAWV0ck8HhIzoc9sobYfasLsvr9a6W27kxMg714DdHB6uNaK/jsro/mTe+URi54G lkPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850270; x=1764455070; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=7NyXHP81r6tLJnJ1NWALdU8H7382VS8IXYxSNq72IwU=; b=QG0NOGSxYTYoPbYdE+yASKoCbqG5LsDoRtLdp3XQWSLH4IwjcN3Bzzn2lzJYSHTCSf JPkYoP4GnqX6beAH06dRvzudKqlbpZUDYC+wLv5NrrU1QYn+D9stQ5rCwIb1G1MZafKV 8tCnSMx+8JtHk+SJk7kMyf7DxfrfCikTqBXxATPbX6HLA+zW+iq9Sf32qsQQ6/AMn+EK yLaPF/yAr8l5tin+vY+Ivvs9GhVZAYAGiT5bfepWpDr5jZ+N5RL1Jkcz7tjwIlGT3LNg +HcRkzBjL48+xcmiDpFUgNqONX5Y5HlBzoYQJWiQxIAFytc5Y65m23vsHbvmL2BL+PCr UgFg== X-Forwarded-Encrypted: i=1; AJvYcCXoNJW5hzM0WUdv8kpq3teGbEi7C2Xp+gC7LW04/Xhhe5+T4tHq9RLTUmC0vFpzuwblWl8sMUagBb4olkU=@vger.kernel.org X-Gm-Message-State: AOJu0YzrSXRGq8bGPJFRW/bv6DsVBrsT1yZI9MstvtauT5FqtrBdvXXl tTqJJyF6aqFxjjI+JAvyD+5REdVnueh/cDTBEW0f9OCXFuMNeeMl3h6FpljWms3BNGc= X-Gm-Gg: ASbGncvSuXEJW4LPkKkPGblFzigZTM2th+f8otUzXSdAwpUBhxP0WFsdhIB4/6/Zzyi UlgWFd0sRO+vQMvSN8th+AkwThtQEC7xRWM2dqalxmPhMrfxsZenA8vP/TvDmEmtigBC598S8db JWUxlVfw+zqhTopXSmb3TIWWElQrZhTnFiwcfN+MkJXq/M+KmfN6UUJwNo+03Cj/e9/TJjDCoKf B7ROLJ017sRVEREfcEIT98asLTo+5X3TpujRFHRwwMtWB/ZTeF5prnVjSkthK6bHP7fMLp53PmG DofA/Yt+T26Wo3Kdp41eYIVSGzRleAnPtdSYHNJWq16oTU5vURMq6DecBerprG6CxOPB/611kUS findpx942hIbLP6XgwRy4Z37/n6iH7CzLTCIjPYIOgajo47pvj9xUmaOyRY0GAW7JdNTSi1QQEJ yad7h4t2MoMQbgSXxN1juTiJNjszxA3Eir3KRVsoBBwOlmjASNCTeBRKkOyS9slXlUy7sHSYZML E9y0xDekZQYyJH8CQ== X-Google-Smtp-Source: AGHT+IFzv5/avEdgd5tUHj7WacQBSkpr9v1uz6r8Nm0HQcMeUJlEAdpnS7ZckZ4AJh/RKyqcXKm4RA== X-Received: by 2002:a05:690c:d90:b0:787:e779:9ef8 with SMTP id 00721157ae682-78a7bb5a576mr91702437b3.16.1763850270094; Sat, 22 Nov 2025 14:24:30 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:29 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 17/22] selftests/liveupdate: Add kexec-based selftest for Date: Sat, 22 Nov 2025 17:23:44 -0500 Message-ID: <20251122222351.1059049-18-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Introduce a kexec-based selftest, luo_kexec_simple, to validate the end-to-end lifecycle of a Live Update Orchestrator (LUO) session across a reboot. While existing tests verify the uAPI in a pre-reboot context, this test ensures that the core functionality=E2=80=94preserving state via Kexec Hand= over and restoring it in a new kernel=E2=80=94works as expected. The test operates in two stages, managing its state across the reboot by preserving a dedicated "state session" containing a memfd. This mechanism dogfoods the LUO feature itself for state tracking, making the test self-contained. The test validates the following sequence: Stage 1 (Pre-kexec): - Creates a test session (test-session). - Creates and preserves a memfd with a known data pattern into the test session. - Creates the state-tracking session to signal progression to Stage 2. - Executes a kexec reboot via a helper script. Stage 2 (Post-kexec): - Retrieves the state-tracking session to confirm it is in the post-reboot stage. - Retrieves the preserved test session. - Restores the memfd from the test session and verifies its contents match the original data pattern written in Stage 1. - Finalizes both the test and state sessions to ensure a clean teardown. The test relies on a helper script (do_kexec.sh) to perform the reboot and a shared utility library (luo_test_utils.c) for common LUO operations, keeping the main test logic clean and focused. Signed-off-by: Pasha Tatashin Reviewed-by: Zhu Yanjun Reviewed-by: Mike Rapoport (Microsoft) --- tools/testing/selftests/liveupdate/Makefile | 6 + .../testing/selftests/liveupdate/do_kexec.sh | 16 ++ .../selftests/liveupdate/luo_kexec_simple.c | 89 ++++++ .../selftests/liveupdate/luo_test_utils.c | 266 ++++++++++++++++++ .../selftests/liveupdate/luo_test_utils.h | 44 +++ 5 files changed, 421 insertions(+) create mode 100755 tools/testing/selftests/liveupdate/do_kexec.sh create mode 100644 tools/testing/selftests/liveupdate/luo_kexec_simple.c create mode 100644 tools/testing/selftests/liveupdate/luo_test_utils.c create mode 100644 tools/testing/selftests/liveupdate/luo_test_utils.h diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/se= lftests/liveupdate/Makefile index 620cb4ce85af..bbbec633970c 100644 --- a/tools/testing/selftests/liveupdate/Makefile +++ b/tools/testing/selftests/liveupdate/Makefile @@ -1,7 +1,13 @@ # SPDX-License-Identifier: GPL-2.0-only =20 +LIB_C +=3D luo_test_utils.c + TEST_GEN_PROGS +=3D liveupdate =20 +TEST_GEN_PROGS_EXTENDED +=3D luo_kexec_simple + +TEST_FILES +=3D do_kexec.sh + include ../lib.mk =20 CFLAGS +=3D $(KHDR_INCLUDES) diff --git a/tools/testing/selftests/liveupdate/do_kexec.sh b/tools/testing= /selftests/liveupdate/do_kexec.sh new file mode 100755 index 000000000000..3c7c6cafbef8 --- /dev/null +++ b/tools/testing/selftests/liveupdate/do_kexec.sh @@ -0,0 +1,16 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +set -e + +# Use $KERNEL and $INITRAMFS to pass custom Kernel and optional initramfs + +KERNEL=3D"${KERNEL:-/boot/bzImage}" +set -- -l -s --reuse-cmdline "$KERNEL" + +INITRAMFS=3D"${INITRAMFS:-/boot/initramfs}" +if [ -f "$INITRAMFS" ]; then + set -- "$@" --initrd=3D"$INITRAMFS" +fi + +kexec "$@" +kexec -e diff --git a/tools/testing/selftests/liveupdate/luo_kexec_simple.c b/tools/= testing/selftests/liveupdate/luo_kexec_simple.c new file mode 100644 index 000000000000..d7ac1f3dc4cb --- /dev/null +++ b/tools/testing/selftests/liveupdate/luo_kexec_simple.c @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + * + * A simple selftest to validate the end-to-end lifecycle of a LUO session + * across a single kexec reboot. + */ + +#include "luo_test_utils.h" + +#define TEST_SESSION_NAME "test-session" +#define TEST_MEMFD_TOKEN 0x1A +#define TEST_MEMFD_DATA "hello kexec world" + +/* Constants for the state-tracking mechanism, specific to this test file.= */ +#define STATE_SESSION_NAME "kexec_simple_state" +#define STATE_MEMFD_TOKEN 999 + +/* Stage 1: Executed before the kexec reboot. */ +static void run_stage_1(int luo_fd) +{ + int session_fd; + + ksft_print_msg("[STAGE 1] Starting pre-kexec setup...\n"); + + ksft_print_msg("[STAGE 1] Creating state file for next stage (2)...\n"); + create_state_file(luo_fd, STATE_SESSION_NAME, STATE_MEMFD_TOKEN, 2); + + ksft_print_msg("[STAGE 1] Creating session '%s' and preserving memfd...\n= ", + TEST_SESSION_NAME); + session_fd =3D luo_create_session(luo_fd, TEST_SESSION_NAME); + if (session_fd < 0) + fail_exit("luo_create_session for '%s'", TEST_SESSION_NAME); + + if (create_and_preserve_memfd(session_fd, TEST_MEMFD_TOKEN, + TEST_MEMFD_DATA) < 0) { + fail_exit("create_and_preserve_memfd for token %#x", + TEST_MEMFD_TOKEN); + } + + close(luo_fd); + daemonize_and_wait(); +} + +/* Stage 2: Executed after the kexec reboot. */ +static void run_stage_2(int luo_fd, int state_session_fd) +{ + int session_fd, mfd, stage; + + ksft_print_msg("[STAGE 2] Starting post-kexec verification...\n"); + + restore_and_read_stage(state_session_fd, STATE_MEMFD_TOKEN, &stage); + if (stage !=3D 2) + fail_exit("Expected stage 2, but state file contains %d", stage); + + ksft_print_msg("[STAGE 2] Retrieving session '%s'...\n", TEST_SESSION_NAM= E); + session_fd =3D luo_retrieve_session(luo_fd, TEST_SESSION_NAME); + if (session_fd < 0) + fail_exit("luo_retrieve_session for '%s'", TEST_SESSION_NAME); + + ksft_print_msg("[STAGE 2] Restoring and verifying memfd (token %#x)...\n", + TEST_MEMFD_TOKEN); + mfd =3D restore_and_verify_memfd(session_fd, TEST_MEMFD_TOKEN, + TEST_MEMFD_DATA); + if (mfd < 0) + fail_exit("restore_and_verify_memfd for token %#x", TEST_MEMFD_TOKEN); + close(mfd); + + ksft_print_msg("[STAGE 2] Test data verified successfully.\n"); + ksft_print_msg("[STAGE 2] Finalizing test session...\n"); + if (luo_session_finish(session_fd) < 0) + fail_exit("luo_session_finish for test session"); + close(session_fd); + + ksft_print_msg("[STAGE 2] Finalizing state session...\n"); + if (luo_session_finish(state_session_fd) < 0) + fail_exit("luo_session_finish for state session"); + close(state_session_fd); + + ksft_print_msg("\n--- SIMPLE KEXEC TEST PASSED ---\n"); +} + +int main(int argc, char *argv[]) +{ + return luo_test(argc, argv, STATE_SESSION_NAME, + run_stage_1, run_stage_2); +} diff --git a/tools/testing/selftests/liveupdate/luo_test_utils.c b/tools/te= sting/selftests/liveupdate/luo_test_utils.c new file mode 100644 index 000000000000..3c8721c505df --- /dev/null +++ b/tools/testing/selftests/liveupdate/luo_test_utils.c @@ -0,0 +1,266 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "luo_test_utils.h" + +int luo_open_device(void) +{ + return open(LUO_DEVICE, O_RDWR); +} + +int luo_create_session(int luo_fd, const char *name) +{ + struct liveupdate_ioctl_create_session arg =3D { .size =3D sizeof(arg) }; + + snprintf((char *)arg.name, LIVEUPDATE_SESSION_NAME_LENGTH, "%.*s", + LIVEUPDATE_SESSION_NAME_LENGTH - 1, name); + + if (ioctl(luo_fd, LIVEUPDATE_IOCTL_CREATE_SESSION, &arg) < 0) + return -errno; + + return arg.fd; +} + +int luo_retrieve_session(int luo_fd, const char *name) +{ + struct liveupdate_ioctl_retrieve_session arg =3D { .size =3D sizeof(arg) = }; + + snprintf((char *)arg.name, LIVEUPDATE_SESSION_NAME_LENGTH, "%.*s", + LIVEUPDATE_SESSION_NAME_LENGTH - 1, name); + + if (ioctl(luo_fd, LIVEUPDATE_IOCTL_RETRIEVE_SESSION, &arg) < 0) + return -errno; + + return arg.fd; +} + +int create_and_preserve_memfd(int session_fd, int token, const char *data) +{ + struct liveupdate_session_preserve_fd arg =3D { .size =3D sizeof(arg) }; + long page_size =3D sysconf(_SC_PAGE_SIZE); + void *map =3D MAP_FAILED; + int mfd =3D -1, ret =3D -1; + + mfd =3D memfd_create("test_mfd", 0); + if (mfd < 0) + return -errno; + + if (ftruncate(mfd, page_size) !=3D 0) + goto out; + + map =3D mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, mfd, 0); + if (map =3D=3D MAP_FAILED) + goto out; + + snprintf(map, page_size, "%s", data); + munmap(map, page_size); + + arg.fd =3D mfd; + arg.token =3D token; + if (ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, &arg) < 0) + goto out; + + ret =3D 0; +out: + if (ret !=3D 0 && errno !=3D 0) + ret =3D -errno; + if (mfd >=3D 0) + close(mfd); + return ret; +} + +int restore_and_verify_memfd(int session_fd, int token, + const char *expected_data) +{ + struct liveupdate_session_retrieve_fd arg =3D { .size =3D sizeof(arg) }; + long page_size =3D sysconf(_SC_PAGE_SIZE); + void *map =3D MAP_FAILED; + int mfd =3D -1, ret =3D -1; + + arg.token =3D token; + if (ioctl(session_fd, LIVEUPDATE_SESSION_RETRIEVE_FD, &arg) < 0) + return -errno; + mfd =3D arg.fd; + + map =3D mmap(NULL, page_size, PROT_READ, MAP_SHARED, mfd, 0); + if (map =3D=3D MAP_FAILED) + goto out; + + if (expected_data && strcmp(expected_data, map) !=3D 0) { + ksft_print_msg("Data mismatch! Expected '%s', Got '%s'\n", + expected_data, (char *)map); + ret =3D -EINVAL; + goto out_munmap; + } + + ret =3D mfd; +out_munmap: + munmap(map, page_size); +out: + if (ret < 0 && errno !=3D 0) + ret =3D -errno; + if (ret < 0 && mfd >=3D 0) + close(mfd); + return ret; +} + +int luo_session_finish(int session_fd) +{ + struct liveupdate_session_finish arg =3D { .size =3D sizeof(arg) }; + + if (ioctl(session_fd, LIVEUPDATE_SESSION_FINISH, &arg) < 0) + return -errno; + + return 0; +} + +void create_state_file(int luo_fd, const char *session_name, int token, + int next_stage) +{ + char buf[32]; + int state_session_fd; + + state_session_fd =3D luo_create_session(luo_fd, session_name); + if (state_session_fd < 0) + fail_exit("luo_create_session for state tracking"); + + snprintf(buf, sizeof(buf), "%d", next_stage); + if (create_and_preserve_memfd(state_session_fd, token, buf) < 0) + fail_exit("create_and_preserve_memfd for state tracking"); + + /* + * DO NOT close session FD, otherwise it is going to be unpreserved + */ +} + +void restore_and_read_stage(int state_session_fd, int token, int *stage) +{ + char buf[32] =3D {0}; + int mfd; + + mfd =3D restore_and_verify_memfd(state_session_fd, token, NULL); + if (mfd < 0) + fail_exit("failed to restore state memfd"); + + if (read(mfd, buf, sizeof(buf) - 1) < 0) + fail_exit("failed to read state mfd"); + + *stage =3D atoi(buf); + + close(mfd); +} + +void daemonize_and_wait(void) +{ + pid_t pid; + + ksft_print_msg("[STAGE 1] Forking persistent child to hold sessions...\n"= ); + + pid =3D fork(); + if (pid < 0) + fail_exit("fork failed"); + + if (pid > 0) { + ksft_print_msg("[STAGE 1] Child PID: %d. Resources are pinned.\n", pid); + ksft_print_msg("[STAGE 1] You may now perform kexec reboot.\n"); + exit(EXIT_SUCCESS); + } + + /* Detach from terminal so closing the window doesn't kill us */ + if (setsid() < 0) + fail_exit("setsid failed"); + + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + + /* Change dir to root to avoid locking filesystems */ + if (chdir("/") < 0) + exit(EXIT_FAILURE); + + while (1) + sleep(60); +} + +static int parse_stage_args(int argc, char *argv[]) +{ + static struct option long_options[] =3D { + {"stage", required_argument, 0, 's'}, + {0, 0, 0, 0} + }; + int option_index =3D 0; + int stage =3D 1; + int opt; + + optind =3D 1; + while ((opt =3D getopt_long(argc, argv, "s:", long_options, &option_index= )) !=3D -1) { + switch (opt) { + case 's': + stage =3D atoi(optarg); + if (stage !=3D 1 && stage !=3D 2) + fail_exit("Invalid stage argument"); + break; + default: + fail_exit("Unknown argument"); + } + } + return stage; +} + +int luo_test(int argc, char *argv[], + const char *state_session_name, + luo_test_stage1_fn stage1, + luo_test_stage2_fn stage2) +{ + int target_stage =3D parse_stage_args(argc, argv); + int luo_fd =3D luo_open_device(); + int state_session_fd; + int detected_stage; + + if (luo_fd < 0) { + ksft_exit_skip("Failed to open %s. Is the luo module loaded?\n", + LUO_DEVICE); + } + + state_session_fd =3D luo_retrieve_session(luo_fd, state_session_name); + if (state_session_fd =3D=3D -ENOENT) + detected_stage =3D 1; + else if (state_session_fd >=3D 0) + detected_stage =3D 2; + else + fail_exit("Failed to check for state session"); + + if (target_stage !=3D detected_stage) { + ksft_exit_fail_msg("Stage mismatch Requested --stage %d, but system is i= n stage %d.\n" + "(State session %s: %s)\n", + target_stage, detected_stage, state_session_name, + (detected_stage =3D=3D 2) ? "EXISTS" : "MISSING"); + } + + if (target_stage =3D=3D 1) + stage1(luo_fd); + else + stage2(luo_fd, state_session_fd); + + return 0; +} diff --git a/tools/testing/selftests/liveupdate/luo_test_utils.h b/tools/te= sting/selftests/liveupdate/luo_test_utils.h new file mode 100644 index 000000000000..90099bf49577 --- /dev/null +++ b/tools/testing/selftests/liveupdate/luo_test_utils.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + * + * Utility functions for LUO kselftests. + */ + +#ifndef LUO_TEST_UTILS_H +#define LUO_TEST_UTILS_H + +#include +#include +#include +#include "../kselftest.h" + +#define LUO_DEVICE "/dev/liveupdate" + +#define fail_exit(fmt, ...) \ + ksft_exit_fail_msg("[%s:%d] " fmt " (errno: %s)\n", \ + __func__, __LINE__, ##__VA_ARGS__, strerror(errno)) + +int luo_open_device(void); +int luo_create_session(int luo_fd, const char *name); +int luo_retrieve_session(int luo_fd, const char *name); +int luo_session_finish(int session_fd); + +int create_and_preserve_memfd(int session_fd, int token, const char *data); +int restore_and_verify_memfd(int session_fd, int token, const char *expect= ed_data); + +void create_state_file(int luo_fd, const char *session_name, int token, + int next_stage); +void restore_and_read_stage(int state_session_fd, int token, int *stage); + +void daemonize_and_wait(void); + +typedef void (*luo_test_stage1_fn)(int luo_fd); +typedef void (*luo_test_stage2_fn)(int luo_fd, int state_session_fd); + +int luo_test(int argc, char *argv[], const char *state_session_name, + luo_test_stage1_fn stage1, luo_test_stage2_fn stage2); + +#endif /* LUO_TEST_UTILS_H */ --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (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 64979299AB1 for ; Sat, 22 Nov 2025 22:24:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850276; cv=none; b=cyIYFpTdU90X+vmMEIop0hjiheRToq6TDPXiFHiz5OXkqM5032z7CCIpytCpWGXTp1ab7er7xdIfpShYTL3F1wdlegzeSY5CNRabV1Tvc9TwpPG0K2ulcXj9jMeXtg3IM7jbcYUs5unse4paIloCtoK1W7Cw46sFfj7Xs9cVZPM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850276; c=relaxed/simple; bh=g8BaDXPlSFt2H9JFAcuCge/l/OI9s/QmwnUe616Y5cI=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VS9k2Jp/PIvhYnjh743K1Wh/pITtWklyI8avdXjvOSCHOLFar1QHMaqjbwlsKfU/81X91NXL1GFUfLmXkxpu6pxI1gbcgEJWw3Xxzhfji9jFKS7QTGgvv9fsu85D/7ImARQfL3SnPYrqL4iUBz5Dk8y6QTamISxmap7H8oL4f4k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=IhZJvQvI; arc=none smtp.client-ip=209.85.128.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="IhZJvQvI" Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-7866bca6765so27782247b3.1 for ; Sat, 22 Nov 2025 14:24:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850272; x=1764455072; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=4XTUTAjFFy7b91C6Dw70mD3UF1HxwVkQgHbYFhkd9nI=; b=IhZJvQvIOkBSG/6kiShQye12aw9gU1YBCkPSlZVjNYbGBXpav3IxJvqAy/379yoFQv Co1QAGJsDDmGuUa97ZY0PCVjnutx7j5CQLceD11mtCrvtb/TSE8F+38nc1NBmTnfWOp0 tUnwl4zmIR+wjH2QqASdzhb71ZYxJeLusLuLmCFULLGHFrDQlMoZpvK4G9gTGHVY0Ib4 Jt1gWxlYJmSJMXEI2zRR89euJwhpVtV6+WlC8gpGhwOZqmhxTX6/qYhpwsRO+siwVTVp 5n+50oiEcBcAty+i5P4gGV38/KBxISvyvWpd1G4EEFGyCAikPUbXdyhp66+eR4G/ti30 H/Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850272; x=1764455072; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=4XTUTAjFFy7b91C6Dw70mD3UF1HxwVkQgHbYFhkd9nI=; b=iZ/OitAAbeSILL3ULEJvNUmWgLsYf1YQ7Havwiau1LUWfzj8t7A8WSixSWXZ2ymcaF WG60XVqvOp75/dIHlqbcHEIKMvajy/ezv952XM6tOQc03q06MZh4VRW6tghzYBn5FqpP qQDNh7lpRKvdlbiUQ0Fv/CvH6x9ciYhmR29IsBasXNFYxpZBrEwhVnlGHeIa3XR8tagA 8a1tI8Mxr0Qr5xmS1754vN/HxoTKaDWXSozhv2HRyColOXIVTF4nNjnnLKUmb/cdwkhH onb3dJ1LpSpWk08hBqV2DhlMO5Gmy2jZUeG4h6YuO3rKMGOAU6L4AcruPtSKNqhJ3BPb aREQ== X-Forwarded-Encrypted: i=1; AJvYcCV3Zmaf65aSgaWp4NlB16HAu1p2S+AZKPBJaYUAhtHC7kvVm4eTg34ulYH51DfyvFvb3UK8UQJs3HFRd/4=@vger.kernel.org X-Gm-Message-State: AOJu0Yx/yEKwfb/fBThZpiuVxxNsaLzC8HyEt1IlnjDXbifBBSWRmdpL sUuNLJ+n8lUGdlD8LjB64wUa8cvunEo0YL6LPfwCNn1AqVVSipOSKo+HwMs6K84WWjo= X-Gm-Gg: ASbGncuYbVZkU9rRGGc0+SQpGlDtoyFM0kIdMDT04/n7BOnMivocpcms5GxR43p4Anm mf7lVj9HaD5GCBMW3rJBg2Y4vVoI3NGUK9o80yN/Lo4qAWUZIQL6g3BxPf6O+BFnxAHhJnNwQuY lOw7QXosxFSZqxzfh+hvc7Gi7LFp9atAEzx6lnF4g/S+RZpeQEI+4cMhcibMpx7cPTEdx+9Jxg3 fqP/sDA0WHPJCVOc52GMdq+H1U9j0R169DpTRflj2TjjAZzxb+jy6Nnx7VM5urgJuj7AqkG8GOk bFss0cMjXXtiYFbd2VkIxNXeTXT3m3et59UxGAif5ftQE1QJGCjbnGw1BRrNVZtJBgkIrVYq9xD 2qNki+miyQhJrP3d+Oc7sOfAhEiESGLCDnTgLVNu8BdUYUp4pFY8ZdY/oC6+4oN0/AReUHbZ+LB 4ojuhsWkp2Y8fjaJdWX8TrW9hJ6TBVXJBdVXelSaKfi9/9/0RaOjo8K3Ko0/zduR2O41CcTHzjs MUBktA= X-Google-Smtp-Source: AGHT+IH/eqBoS3mqhMhh4qvlka6WRIlDgWiw3QW9H2yTNLjxyq2DnfCNF4kdPEPBkXHTMSk/UtfxGQ== X-Received: by 2002:a05:690e:4286:10b0:641:f5bc:6960 with SMTP id 956f58d0204a3-64302b3f670mr4232320d50.76.1763850271944; Sat, 22 Nov 2025 14:24:31 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:31 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 18/22] selftests/liveupdate: Add kexec test for multiple and empty sessions Date: Sat, 22 Nov 2025 17:23:45 -0500 Message-ID: <20251122222351.1059049-19-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" Introduce a new kexec-based selftest, luo_kexec_multi_session, to validate the end-to-end lifecycle of a more complex LUO scenario. While the existing luo_kexec_simple test covers the basic end-to-end lifecycle, it is limited to a single session with one preserved file. This new test significantly expands coverage by verifying LUO's ability to handle a mixed workload involving multiple sessions, some of which are intentionally empty. This ensures that the LUO core correctly preserves and restores the state of all session types across a reboot. The test validates the following sequence: Stage 1 (Pre-kexec): - Creates two empty test sessions (multi-test-empty-1, multi-test-empty-2). - Creates a session with one preserved memfd (multi-test-files-1). - Creates another session with two preserved memfds (multi-test-files-2), each containing unique data. - Creates a state-tracking session to manage the transition to Stage 2. - Executes a kexec reboot via the helper script. Stage 2 (Post-kexec): - Retrieves the state-tracking session to confirm it is in the post-reboot stage. - Retrieves all four test sessions (both the empty and non-empty ones). - For the non-empty sessions, restores the preserved memfds and verifies their contents match the original data patterns. - Finalizes all test sessions and the state session to ensure a clean teardown and that all associated kernel resources are correctly released. This test provides greater confidence in the robustness of the LUO framework by validating its behavior in a more realistic, multi-faceted scenario. Signed-off-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) --- tools/testing/selftests/liveupdate/Makefile | 1 + .../selftests/liveupdate/luo_multi_session.c | 162 ++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 tools/testing/selftests/liveupdate/luo_multi_session.c diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/se= lftests/liveupdate/Makefile index bbbec633970c..080754787ede 100644 --- a/tools/testing/selftests/liveupdate/Makefile +++ b/tools/testing/selftests/liveupdate/Makefile @@ -5,6 +5,7 @@ LIB_C +=3D luo_test_utils.c TEST_GEN_PROGS +=3D liveupdate =20 TEST_GEN_PROGS_EXTENDED +=3D luo_kexec_simple +TEST_GEN_PROGS_EXTENDED +=3D luo_multi_session =20 TEST_FILES +=3D do_kexec.sh =20 diff --git a/tools/testing/selftests/liveupdate/luo_multi_session.c b/tools= /testing/selftests/liveupdate/luo_multi_session.c new file mode 100644 index 000000000000..0ee2d795beef --- /dev/null +++ b/tools/testing/selftests/liveupdate/luo_multi_session.c @@ -0,0 +1,162 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + * + * A selftest to validate the end-to-end lifecycle of multiple LUO sessions + * across a kexec reboot, including empty sessions and sessions with multi= ple + * files. + */ + +#include "luo_test_utils.h" + +#define SESSION_EMPTY_1 "multi-test-empty-1" +#define SESSION_EMPTY_2 "multi-test-empty-2" +#define SESSION_FILES_1 "multi-test-files-1" +#define SESSION_FILES_2 "multi-test-files-2" + +#define MFD1_TOKEN 0x1001 +#define MFD2_TOKEN 0x2002 +#define MFD3_TOKEN 0x3003 + +#define MFD1_DATA "Data for session files 1" +#define MFD2_DATA "First file for session files 2" +#define MFD3_DATA "Second file for session files 2" + +#define STATE_SESSION_NAME "kexec_multi_state" +#define STATE_MEMFD_TOKEN 998 + +/* Stage 1: Executed before the kexec reboot. */ +static void run_stage_1(int luo_fd) +{ + int s_empty1_fd, s_empty2_fd, s_files1_fd, s_files2_fd; + + ksft_print_msg("[STAGE 1] Starting pre-kexec setup for multi-session test= ...\n"); + + ksft_print_msg("[STAGE 1] Creating state file for next stage (2)...\n"); + create_state_file(luo_fd, STATE_SESSION_NAME, STATE_MEMFD_TOKEN, 2); + + ksft_print_msg("[STAGE 1] Creating empty sessions '%s' and '%s'...\n", + SESSION_EMPTY_1, SESSION_EMPTY_2); + s_empty1_fd =3D luo_create_session(luo_fd, SESSION_EMPTY_1); + if (s_empty1_fd < 0) + fail_exit("luo_create_session for '%s'", SESSION_EMPTY_1); + + s_empty2_fd =3D luo_create_session(luo_fd, SESSION_EMPTY_2); + if (s_empty2_fd < 0) + fail_exit("luo_create_session for '%s'", SESSION_EMPTY_2); + + ksft_print_msg("[STAGE 1] Creating session '%s' with one memfd...\n", + SESSION_FILES_1); + + s_files1_fd =3D luo_create_session(luo_fd, SESSION_FILES_1); + if (s_files1_fd < 0) + fail_exit("luo_create_session for '%s'", SESSION_FILES_1); + if (create_and_preserve_memfd(s_files1_fd, MFD1_TOKEN, MFD1_DATA) < 0) { + fail_exit("create_and_preserve_memfd for token %#x", + MFD1_TOKEN); + } + + ksft_print_msg("[STAGE 1] Creating session '%s' with two memfds...\n", + SESSION_FILES_2); + + s_files2_fd =3D luo_create_session(luo_fd, SESSION_FILES_2); + if (s_files2_fd < 0) + fail_exit("luo_create_session for '%s'", SESSION_FILES_2); + if (create_and_preserve_memfd(s_files2_fd, MFD2_TOKEN, MFD2_DATA) < 0) { + fail_exit("create_and_preserve_memfd for token %#x", + MFD2_TOKEN); + } + if (create_and_preserve_memfd(s_files2_fd, MFD3_TOKEN, MFD3_DATA) < 0) { + fail_exit("create_and_preserve_memfd for token %#x", + MFD3_TOKEN); + } + + close(luo_fd); + daemonize_and_wait(); +} + +/* Stage 2: Executed after the kexec reboot. */ +static void run_stage_2(int luo_fd, int state_session_fd) +{ + int s_empty1_fd, s_empty2_fd, s_files1_fd, s_files2_fd; + int mfd1, mfd2, mfd3, stage; + + ksft_print_msg("[STAGE 2] Starting post-kexec verification...\n"); + + restore_and_read_stage(state_session_fd, STATE_MEMFD_TOKEN, &stage); + if (stage !=3D 2) { + fail_exit("Expected stage 2, but state file contains %d", + stage); + } + + ksft_print_msg("[STAGE 2] Retrieving all sessions...\n"); + s_empty1_fd =3D luo_retrieve_session(luo_fd, SESSION_EMPTY_1); + if (s_empty1_fd < 0) + fail_exit("luo_retrieve_session for '%s'", SESSION_EMPTY_1); + + s_empty2_fd =3D luo_retrieve_session(luo_fd, SESSION_EMPTY_2); + if (s_empty2_fd < 0) + fail_exit("luo_retrieve_session for '%s'", SESSION_EMPTY_2); + + s_files1_fd =3D luo_retrieve_session(luo_fd, SESSION_FILES_1); + if (s_files1_fd < 0) + fail_exit("luo_retrieve_session for '%s'", SESSION_FILES_1); + + s_files2_fd =3D luo_retrieve_session(luo_fd, SESSION_FILES_2); + if (s_files2_fd < 0) + fail_exit("luo_retrieve_session for '%s'", SESSION_FILES_2); + + ksft_print_msg("[STAGE 2] Verifying contents of session '%s'...\n", + SESSION_FILES_1); + mfd1 =3D restore_and_verify_memfd(s_files1_fd, MFD1_TOKEN, MFD1_DATA); + if (mfd1 < 0) + fail_exit("restore_and_verify_memfd for token %#x", MFD1_TOKEN); + close(mfd1); + + ksft_print_msg("[STAGE 2] Verifying contents of session '%s'...\n", + SESSION_FILES_2); + + mfd2 =3D restore_and_verify_memfd(s_files2_fd, MFD2_TOKEN, MFD2_DATA); + if (mfd2 < 0) + fail_exit("restore_and_verify_memfd for token %#x", MFD2_TOKEN); + close(mfd2); + + mfd3 =3D restore_and_verify_memfd(s_files2_fd, MFD3_TOKEN, MFD3_DATA); + if (mfd3 < 0) + fail_exit("restore_and_verify_memfd for token %#x", MFD3_TOKEN); + close(mfd3); + + ksft_print_msg("[STAGE 2] Test data verified successfully.\n"); + + ksft_print_msg("[STAGE 2] Finalizing all test sessions...\n"); + if (luo_session_finish(s_empty1_fd) < 0) + fail_exit("luo_session_finish for '%s'", SESSION_EMPTY_1); + close(s_empty1_fd); + + if (luo_session_finish(s_empty2_fd) < 0) + fail_exit("luo_session_finish for '%s'", SESSION_EMPTY_2); + close(s_empty2_fd); + + if (luo_session_finish(s_files1_fd) < 0) + fail_exit("luo_session_finish for '%s'", SESSION_FILES_1); + close(s_files1_fd); + + if (luo_session_finish(s_files2_fd) < 0) + fail_exit("luo_session_finish for '%s'", SESSION_FILES_2); + close(s_files2_fd); + + ksft_print_msg("[STAGE 2] Finalizing state session...\n"); + if (luo_session_finish(state_session_fd) < 0) + fail_exit("luo_session_finish for state session"); + close(state_session_fd); + + ksft_print_msg("\n--- MULTI-SESSION KEXEC TEST PASSED ---\n"); +} + +int main(int argc, char *argv[]) +{ + return luo_test(argc, argv, STATE_SESSION_NAME, + run_stage_1, run_stage_2); +} --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (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 DFC6B2E8DF3 for ; Sat, 22 Nov 2025 22:24:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850280; cv=none; b=rvCAZGldldmEorznNkIa14izNgZGYzqRI2+7QNpcEbXa/CWQ4GLMu2HJl+RjAhm6nTzTVnY86XNGslD5hSXsN2fczmCPwWvnNt2AxU4FAo9rjkaXGEk3GV63Z14YV73c1t+C+D0FHhe2i7I9R119UQHcMa/GrqO/FGblpzTpSjs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850280; c=relaxed/simple; bh=qyFA0AVLodsieu/MOUPeDtGYSXYLnzDYcmyFDoYZ4n4=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ijj+Xd3qbu893x99YgMS2v/uzXpv7Og+eSxSj/au8Zxsrj7L04Goy6FtYwaDivfzv4qz+bUhSFYeF0FYvJWlvpe7C2xC6/s8g/EC8QoAFRkEqhCXyZFsSRuKYSfo7SYXvpSrAsAliKcghcxQVu0qRntXkRJeX/+TMX+ZPcodAtc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=Ey4RFQnL; arc=none smtp.client-ip=209.85.128.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="Ey4RFQnL" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-78a712cfbc0so30081427b3.1 for ; Sat, 22 Nov 2025 14:24:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850275; x=1764455075; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=F5tajeHzp9hDN07gnwZlVCQmxSUhZpHiffHO9VM3nds=; b=Ey4RFQnL1todxo3JlT9zqrwifowIc1ThEPlnrf11Wx4lEHquk3UBgc7FKjWWKykQAJ 5dzZnUJspafViNVxurays9MgBRe5dFe9OTebD+3JoGr4TiOWg0uCSI9mkQyPCPBEVa6E K3NXpWyQSUorqauurmLHzbvt4EaeIYi1i1XNUIJylkVXqi52OG3aP4vwkgegyDe7Ix2y JWqVNaCHxmf2qrHHU45m/uoMmbLrGf+Z0QMRSdDWwi4oPDPrBpbjZ4o8dcuejQHqSSrj cuvbTcCkgMzlk2smSC3tl8+s4zIUMcZtg0iltbRjwu3D900+uQy04uwi/laHy7xEuOXV NY7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850275; x=1764455075; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=F5tajeHzp9hDN07gnwZlVCQmxSUhZpHiffHO9VM3nds=; b=JXUQLSb75So+NAetzpe4ddEtpL/WL6t7xDORca/By3/1Hii3MLY2mTFyIBOrwgJQ4k c6bLw1jYzWBbH/QSt+c+6VclEGEbSo3336PndIl8SJP7Pu6hQnesaz1JmXdDo1OoXYyu 6+Rq0iSTUDxF9LlAjJhJSQNSSbOAOSWLbSiV1gc+dF/+VLVq07IS3roUPgqbhIjJmonj TPos1+Yj13WcyHGyxzNvHMOhq/NhB/rUoeAM+dsVR1PhS81CdKWVlDjMvA3KfhFxkc+x 6UFWqwDE1al4hIIoyZYwLenyPiuVUXGNmci0TJKqbM8chQkvWc2LZjjrFxvgW5oAg5u1 3otw== X-Forwarded-Encrypted: i=1; AJvYcCWffys3Y+eNdWx0LX9G/qhB5/assHyTRbR0FgkNioyUw2+cYXF36uTI5ICUIfexC74ME5D/6qQLeZsteQ0=@vger.kernel.org X-Gm-Message-State: AOJu0YxJGmG5bZiQ1TJM2sSQwADt++OWAbibW/eBYgAomhLM7NTfcNL+ OrKT6NB1+ay2ueobEos1zjatgHri/4D0LgVwk2aHEbkSIVaiRwNCmYAtWms8ocHquYw= X-Gm-Gg: ASbGncuP2y2xADhaPzKd+uD5fmvETOOxrjGDbSNqejYFr5/NVLIfkHKGIDejeyZ4Ghw K8qMZRYbvi2G2AnFrxMMPHs+SpIXqZyxW8SlyneKVcbBDq4nvc9iAwW/DOclpEb71Gjc42YirTc cqzTsUH5TTBSOJIECUIML1L4+uQkhKizDXGJbOHM8mRCOMgpPDDMP9eH5p6E/lD0Ikxcy0CVFkY kaHgq8M7zIfpaVh4KscatpHfZn95PlZsZLsuYdFmvfIi8ZIy9+q5LgNiZCuuMu/jO5pCcq2K0I/ 284HbpABvkf3N4eE1ymc7N46mOmNrEUC6/7FqUFBiA5ua5bfBRSdSX6h74oVOBgvXkBWVusEuMK KHfABmt62gNK/F/bsBKrZDB1tSpW1aa8xk5xqT6/vQb8EtNoIE6a2wXmdUXoZeUa7XUvSUGNBDp ljhkPeEH9JWmOj0nEad+LjTeK+Oz/AThtVQpXLtB1w78d1Cl45a+6C0szXva+6Nq4A8sYRNSPL2 BnRabE= X-Google-Smtp-Source: AGHT+IFD6osaS2sP17uSKoOpkdrgAaGoOW4xxaV4WzN7kWyXMlqtMJ1zAdZWqXZh/Duto084HHI0jg== X-Received: by 2002:a05:690c:968a:b0:786:896d:8858 with SMTP id 00721157ae682-78a8b54d4a4mr55406797b3.47.1763850274503; Sat, 22 Nov 2025 14:24:34 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:33 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 19/22] selftests/liveupdate: add test infrastructure and scripts Date: Sat, 22 Nov 2025 17:23:46 -0500 Message-ID: <20251122222351.1059049-20-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" Add the testing infrastructure required to verify the liveupdate feature. This includes a custom init process, a test orchestration script, and a batch runner. The framework consists of: init.c: A lightweight init process that manages the kexec lifecycle. It mounts necessary filesystems, determines the current execution stage (1 or 2) via the kernel command line, and handles the kexec_file_load() sequence to transition between kernels. luo_test.sh: The primary KTAP-compliant test driver. It handles: - Kernel configuration merging and building. - Cross-compilation detection for x86_64 and arm64. - Generation of the initrd containing the test binary and init. - QEMU execution with automatic accelerator detection (KVM, HVF, or TCG). run.sh: A wrapper script to discover and execute all `luo_*.c` tests across supported architectures, providing a summary of pass/fail/skip results. Signed-off-by: Pasha Tatashin --- tools/testing/selftests/liveupdate/init.c | 174 ++++++++++ .../testing/selftests/liveupdate/luo_test.sh | 296 ++++++++++++++++++ tools/testing/selftests/liveupdate/run.sh | 68 ++++ 3 files changed, 538 insertions(+) create mode 100644 tools/testing/selftests/liveupdate/init.c create mode 100755 tools/testing/selftests/liveupdate/luo_test.sh create mode 100755 tools/testing/selftests/liveupdate/run.sh diff --git a/tools/testing/selftests/liveupdate/init.c b/tools/testing/self= tests/liveupdate/init.c new file mode 100644 index 000000000000..ed11e04d0796 --- /dev/null +++ b/tools/testing/selftests/liveupdate/init.c @@ -0,0 +1,174 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define COMMAND_LINE_SIZE 2048 +#define KERNEL_IMAGE "/kernel" +#define INITRD_IMAGE "/initrd.img" +#define TEST_BINARY "/test_binary" + +static int mount_filesystems(void) +{ + if (mount("devtmpfs", "/dev", "devtmpfs", 0, NULL) < 0) { + fprintf(stderr, "INIT: Warning: Failed to mount devtmpfs\n"); + return -1; + } + + if (mount("debugfs", "/debugfs", "debugfs", 0, NULL) < 0) { + fprintf(stderr, "INIT: Failed to mount debugfs\n"); + return -1; + } + + if (mount("proc", "/proc", "proc", 0, NULL) < 0) { + fprintf(stderr, "INIT: Failed to mount proc\n"); + return -1; + } + + return 0; +} + +static long kexec_file_load(int kernel_fd, int initrd_fd, + unsigned long cmdline_len, const char *cmdline, + unsigned long flags) +{ + return syscall(__NR_kexec_file_load, kernel_fd, initrd_fd, cmdline_len, + cmdline, flags); +} + +static int kexec_load(void) +{ + char cmdline[COMMAND_LINE_SIZE]; + int kernel_fd, initrd_fd, err; + ssize_t len; + int fd; + + fd =3D open("/proc/cmdline", O_RDONLY); + if (fd < 0) { + fprintf(stderr, "INIT: Failed to read /proc/cmdline\n"); + + return -1; + } + + len =3D read(fd, cmdline, sizeof(cmdline) - 1); + close(fd); + if (len < 0) + return -1; + + cmdline[len] =3D 0; + if (len > 0 && cmdline[len - 1] =3D=3D '\n') + cmdline[len - 1] =3D 0; + + strncat(cmdline, " luo_stage=3D2", sizeof(cmdline) - strlen(cmdline) - 1); + + kernel_fd =3D open(KERNEL_IMAGE, O_RDONLY); + if (kernel_fd < 0) { + fprintf(stderr, "INIT: Failed to open kernel image\n"); + return -1; + } + + initrd_fd =3D open(INITRD_IMAGE, O_RDONLY); + if (initrd_fd < 0) { + fprintf(stderr, "INIT: Failed to open initrd image\n"); + close(kernel_fd); + return -1; + } + + err =3D kexec_file_load(kernel_fd, initrd_fd, strlen(cmdline) + 1, + cmdline, 0); + + close(initrd_fd); + close(kernel_fd); + + return err ? : 0; +} + +static int run_test(int stage) +{ + char stage_arg[32]; + int status; + pid_t pid; + + snprintf(stage_arg, sizeof(stage_arg), "--stage=3D%d", stage); + + pid =3D fork(); + if (pid < 0) + return -1; + + if (!pid) { + static const char *const argv[] =3D {TEST_BINARY, stage_arg, NULL}; + + execve(TEST_BINARY, argv, NULL); + fprintf(stderr, "INIT: execve failed\n"); + _exit(1); + } + + waitpid(pid, &status, 0); + + return (WIFEXITED(status) && WEXITSTATUS(status) =3D=3D 0) ? 0 : -1; +} + +static int is_stage_2(void) +{ + char cmdline[COMMAND_LINE_SIZE]; + ssize_t len; + int fd; + + fd =3D open("/proc/cmdline", O_RDONLY); + if (fd < 0) + return 0; + + len =3D read(fd, cmdline, sizeof(cmdline) - 1); + close(fd); + + if (len < 0) + return 0; + + cmdline[len] =3D 0; + + return !!strstr(cmdline, "luo_stage=3D2"); +} + +int main(int argc, char *argv[]) +{ + int current_stage; + + if (mount_filesystems()) + goto err_reboot; + + current_stage =3D is_stage_2() ? 2 : 1; + + printf("INIT: Starting Stage %d\n", current_stage); + + if (current_stage =3D=3D 1 && kexec_load()) { + fprintf(stderr, "INIT: Failed to load kexec kernel\n"); + goto err_reboot; + } + + if (run_test(current_stage)) { + fprintf(stderr, "INIT: Test binary returned failure\n"); + goto err_reboot; + } + + printf("INIT: Stage %d completed successfully.\n", current_stage); + reboot(current_stage =3D=3D 1 ? RB_KEXEC : RB_AUTOBOOT); + + return 0; + +err_reboot: + reboot(RB_AUTOBOOT); + + return -1; +} diff --git a/tools/testing/selftests/liveupdate/luo_test.sh b/tools/testing= /selftests/liveupdate/luo_test.sh new file mode 100755 index 000000000000..7d155956e2ff --- /dev/null +++ b/tools/testing/selftests/liveupdate/luo_test.sh @@ -0,0 +1,296 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +set -ue + +CROSS_COMPILE=3D"${CROSS_COMPILE:-""}" + +test_dir=3D$(realpath "$(dirname "$0")") +kernel_dir=3D$(realpath "$test_dir/../../../..") + +workspace_dir=3D"" +headers_dir=3D"" +initrd=3D"" +KEEP_WORKSPACE=3D0 + +source "$test_dir/../kselftest/ktap_helpers.sh" + +function get_arch_conf() { + local arch=3D$1 + if [[ "$arch" =3D=3D "arm64" ]]; then + QEMU_CMD=3D"qemu-system-aarch64 -M virt -cpu max" + KERNEL_IMAGE=3D"Image" + KERNEL_CMDLINE=3D"console=3DttyAMA0" + elif [[ "$arch" =3D=3D "x86" ]]; then + QEMU_CMD=3D"qemu-system-x86_64" + KERNEL_IMAGE=3D"bzImage" + KERNEL_CMDLINE=3D"console=3DttyS0" + else + echo "Unsupported architecture: $arch" + exit 1 + fi +} + +function usage() { + cat <) + -j) number of jobs for compilation + -t) run test for target_arch (aarch64, x86_64) + -T) test name to run (default: luo_kexec_simple) + -w) custom workspace directory (default: creates temp dir) + -k) keep workspace directory after successful test + -h) display this help +EOF +} + +function cleanup() { + local exit_code=3D$? + + if [ -z "$workspace_dir" ]; then + ktap_finished + return + fi + + if [ $exit_code -ne 0 ]; then + echo "# Test failed (exit code $exit_code)." + echo "# Workspace preserved at: $workspace_dir" + elif [ "$KEEP_WORKSPACE" -eq 1 ]; then + echo "# Workspace preserved (user request) at: $workspace_dir" + else + rm -fr "$workspace_dir" + fi + ktap_finished +} +trap cleanup EXIT + +function skip() { + local msg=3D${1:-""} + ktap_test_skip "$msg" + exit "$KSFT_SKIP" +} + +function fail() { + local msg=3D${1:-""} + ktap_test_fail "$msg" + exit "$KSFT_FAIL" +} + +function detect_cross_compile() { + local target=3D$1 + local host=3D$(uname -m) + + if [ -n "$CROSS_COMPILE" ]; then + return + fi + + [[ "$host" =3D=3D "arm64" ]] && host=3D"aarch64" + [[ "$target" =3D=3D "arm64" ]] && target=3D"aarch64" + + if [[ "$host" =3D=3D "$target" ]]; then + CROSS_COMPILE=3D"" + return + fi + + local candidate=3D"" + case "$target" in + aarch64) candidate=3D"aarch64-linux-gnu-" ;; + x86_64) candidate=3D"x86_64-linux-gnu-" ;; + *) skip "Auto-detection for target '$target' not supported. Please= set CROSS_COMPILE manually." ;; + esac + + if command -v "${candidate}gcc" &> /dev/null; then + CROSS_COMPILE=3D"$candidate" + else + skip "Compiler '${candidate}gcc' not found. Please install it (e.g., 'ap= t install gcc-aarch64-linux-gnu') or set CROSS_COMPILE." + fi +} + +function build_kernel() { + local build_dir=3D$1 + local make_cmd=3D$2 + local kimage=3D$3 + local target_arch=3D$4 + + local kconfig=3D"$build_dir/.config" + local common_conf=3D"$test_dir/config" + local arch_conf=3D"$test_dir/config.$target_arch" + + echo "# Building kernel in: $build_dir" + $make_cmd defconfig + + local fragments=3D"" + if [[ -f "$common_conf" ]]; then + fragments=3D"$fragments $common_conf" + fi + + if [[ -f "$arch_conf" ]]; then + fragments=3D"$fragments $arch_conf" + fi + + if [[ -n "$fragments" ]]; then + "$kernel_dir/scripts/kconfig/merge_config.sh" \ + -Q -m -O "$build_dir" "$kconfig" $fragments >> /dev/null + fi + + $make_cmd olddefconfig + $make_cmd "$kimage" + $make_cmd headers_install INSTALL_HDR_PATH=3D"$headers_dir" +} + +function mkinitrd() { + local build_dir=3D$1 + local kernel_path=3D$2 + local test_name=3D$3 + + # 1. Compile the test binary and the init process + "$CROSS_COMPILE"gcc -static -O2 \ + -I "$headers_dir/include" \ + -I "$test_dir" \ + -o "$workspace_dir/test_binary" \ + "$test_dir/$test_name.c" "$test_dir/luo_test_utils.c" + + "$CROSS_COMPILE"gcc -s -static -Os -nostdinc -nostdlib \ + -fno-asynchronous-unwind-tables -fno-ident \ + -fno-stack-protector \ + -I "$headers_dir/include" \ + -I "$kernel_dir/tools/include/nolibc" \ + -o "$workspace_dir/init" "$test_dir/init.c" + + cat > "$workspace_dir/cpio_list_inner" < "$works= pace_dir/inner_initrd.cpio" + + cat > "$workspace_dir/cpio_list" < "$initrd" + local size=3D$(du -h "$initrd" | cut -f1) +} + +function run_qemu() { + local qemu_cmd=3D$1 + local cmdline=3D$2 + local kernel_path=3D$3 + local serial=3D"$workspace_dir/qemu.serial" + + local accel=3D"-accel tcg" + local host_machine=3D$(uname -m) + + [[ "$host_machine" =3D=3D "arm64" ]] && host_machine=3D"aarch64" + [[ "$host_machine" =3D=3D "x86_64" ]] && host_machine=3D"x86_64" + + if [[ "$qemu_cmd" =3D=3D *"$host_machine"* ]]; then + if [ -w /dev/kvm ]; then + accel=3D"-accel kvm" + fi + fi + + cmdline=3D"$cmdline liveupdate=3Don panic=3D-1" + + echo "# Serial Log: $serial" + timeout 30s $qemu_cmd -m 1G -smp 2 -no-reboot -nographic -nodefaults \ + $accel \ + -serial file:"$serial" \ + -append "$cmdline" \ + -kernel "$kernel_path" \ + -initrd "$initrd" + + local ret=3D$? + + if [ $ret -eq 124 ]; then + fail "QEMU timed out" + fi + + grep "TEST PASSED" "$serial" &> /dev/null || fail "Liveupdate failed. Che= ck $serial for details." +} + +function target_to_arch() { + local target=3D$1 + case $target in + aarch64) echo "arm64" ;; + x86_64) echo "x86" ;; + *) skip "architecture $target is not supported" + esac +} + +function main() { + local build_dir=3D"" + local jobs=3D$(nproc) + local target=3D"$(uname -m)" + local test_name=3D"luo_kexec_simple" + local workspace_arg=3D"" + + set -o errtrace + trap skip ERR + + while getopts 'hd:j:t:T:w:k' opt; do + case $opt in + d) build_dir=3D"$OPTARG" ;; + j) jobs=3D"$OPTARG" ;; + t) target=3D"$OPTARG" ;; + T) test_name=3D"$OPTARG" ;; + w) workspace_arg=3D"$OPTARG" ;; + k) KEEP_WORKSPACE=3D1 ;; + h) usage; exit 0 ;; + *) echo "Unknown argument $opt"; usage; exit 1 ;; + esac + done + + ktap_print_header + ktap_set_plan 1 + + if [ -n "$workspace_arg" ]; then + workspace_dir=3D"$(realpath -m "$workspace_arg")" + mkdir -p "$workspace_dir" + else + workspace_dir=3D$(mktemp -d /tmp/luo-test.XXXXXXXX) + fi + + echo "# Workspace created at: $workspace_dir" + headers_dir=3D"$workspace_dir/usr" + initrd=3D"$workspace_dir/initrd.cpio" + + detect_cross_compile "$target" + + local arch=3D$(target_to_arch "$target") + + if [ -z "$build_dir" ]; then + build_dir=3D"$kernel_dir/.luo_test_build.$arch" + fi + + mkdir -p "$build_dir" + build_dir=3D$(realpath "$build_dir") + get_arch_conf "$arch" + + local make_cmd=3D"make -s ARCH=3D$arch CROSS_COMPILE=3D$CROSS_COMPILE -j$= jobs" + local make_cmd_build=3D"$make_cmd -C $kernel_dir O=3D$build_dir" + + build_kernel "$build_dir" "$make_cmd_build" "$KERNEL_IMAGE" "$target" + + local final_kernel=3D"$build_dir/arch/$arch/boot/$KERNEL_IMAGE" + mkinitrd "$build_dir" "$final_kernel" "$test_name" + + run_qemu "$QEMU_CMD" "$KERNEL_CMDLINE" "$final_kernel" + ktap_test_pass "$test_name succeeded" +} + +main "$@" diff --git a/tools/testing/selftests/liveupdate/run.sh b/tools/testing/self= tests/liveupdate/run.sh new file mode 100755 index 000000000000..3f6b29a26648 --- /dev/null +++ b/tools/testing/selftests/liveupdate/run.sh @@ -0,0 +1,68 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +OUTPUT_DIR=3D"results_$(date +%Y%m%d_%H%M%S)" +SCRIPT_DIR=3D$(dirname "$(realpath "$0")") +TEST_RUNNER=3D"$SCRIPT_DIR/luo_test.sh" + +TARGETS=3D("x86_64" "aarch64") + +GREEN=3D'\033[0;32m' +RED=3D'\033[0;31m' +YELLOW=3D'\033[1;33m' +NC=3D'\033[0m' + +PASSED=3D() +FAILED=3D() +SKIPPED=3D() + +mkdir -p "$OUTPUT_DIR" + +TEST_NAMES=3D() +while IFS=3D read -r file; do + TEST_NAMES+=3D("$(basename "$file" .c)") +done < <(find "$SCRIPT_DIR" -maxdepth 1 -name "luo_*.c" ! -name "luo_test_= utils.c") + +if [ ${#TEST_NAMES[@]} -eq 0 ]; then + echo "No tests found in $SCRIPT_DIR" + exit 1 +fi + +for arch in "${TARGETS[@]}"; do + for test_name in "${TEST_NAMES[@]}"; do + log_file=3D"$OUTPUT_DIR/${arch}_${test_name}.log" + echo -n " -> $arch $test_name ... " + + if "$TEST_RUNNER" -t "$arch" -T "$test_name" > "$log_file" 2>&1; t= hen + echo -e "${GREEN}PASS${NC}" + PASSED+=3D("${arch}:${test_name}") + else + exit_code=3D$? + if [ $exit_code -eq 4 ]; then + echo -e "${YELLOW}SKIP${NC}" + SKIPPED+=3D("${arch}:${test_name}") + else + echo -e "${RED}FAIL${NC}" + FAILED+=3D("${arch}:${test_name}") + fi + fi + done + echo "" +done + +echo "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D" +echo " TEST SUMMARY " +echo "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D" +echo -e "PASSED: ${GREEN}${#PASSED[@]}${NC}" +echo -e "FAILED: ${RED}${#FAILED[@]}${NC}" +for fail in "${FAILED[@]}"; do + echo -e " - $fail" +done +echo -e "SKIPPED: ${YELLOW}${#SKIPPED[@]}${NC}" +echo "Logs: $OUTPUT_DIR" + +if [ ${#FAILED[@]} -eq 0 ]; then + exit 0 +else + exit 1 +fi --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (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 B41322E7BC2 for ; Sat, 22 Nov 2025 22:24:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850280; cv=none; b=fqfX3SOmQsfThoxhv2A3pU5l/TGpNhr8eTo/QAFo6cbekxmrLGcqzYNAfJL5M7FQFb0CXaotGBZIw53h9hTARXtMQX3yfC2CRJ82Tyl3Iy7dVMtctzoiLkKxEhMsU4ACa+OtSIsFl1yV0280M4aFSrXKY5rdJhyPpQdvK8YL+sw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850280; c=relaxed/simple; bh=QEEvS+jdUJeKKxkBIFzT0YN5eKSWf6n6Ab4j5opHktU=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GyuF2ios1d/Kj9IKZSE3SnObu/S93mwWsWPfKpeI4BBtoW6sDLy0EV9amGg+n5BiBFeGrWbVDfvNnzZ70SJhBROjKBsQYdNgdmIirHW3Aehy3aFY5oK2HaCD8Yj4aMi6IlyBvjLjmTIRNyfEfjfcoXJI19bobfG9p8jScFcnCKY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=YKXiKg4U; arc=none smtp.client-ip=209.85.128.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="YKXiKg4U" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-78a76afeff5so30830237b3.0 for ; Sat, 22 Nov 2025 14:24:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850276; x=1764455076; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=62PEQYDhS44k4KirgxolX0bFfpu4NqLUXaq5TcT1ZZY=; b=YKXiKg4U+JHb2CGm1foeaVy/7iaXEaQwHDTDnKkGEaTFvvkXRfA7DwY1A8YF/FxA60 ZkgnR/TlESb1Nv5KdilQY0dRhuSQt1gf83oISgxFTQDjYh839JmKrztQEdiWEC51V3Kw E2nzjU2b7JpcJsk2/jkRsIj6HOXRgX2dcQ7i9gRQey9KVl1zV5dYykwOJdpB3IK84Vfg hTYXJrYksy4iFBWEcHloUKZmtNQDOdooFd+Gohjnh8cuoDaOB07xdi5Ymrc+SwUfcH14 iXv3U4rQMu6bQkBMuhRGowr2f3uCVvwbu76pkzaqIBFe17X47NtCyv7V+c+HFtmtdjfA zdlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850276; x=1764455076; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=62PEQYDhS44k4KirgxolX0bFfpu4NqLUXaq5TcT1ZZY=; b=A5XISfOtdLqy5E2DBliqmbG0izQm/RUTHL2BYCYg676ECnLHRWtaHYdF3rbQTounmb y6s084qtLKYEXHYWNv/WZew5uCiOrwdTCIswe3n0ukYbdClCH2eyJHu6+qbZgWGiwSm6 CMyv6FAliK8MYPYXJj605PlNBR0gKn3JjQ/Dl3Yk1oVC33cEn7XqAzaSURifgWjcxdzT EjI2Rw2XbdVPbgzfA5R5A58BGhs20fsfC7H96kNzceDghdxyDAgifeMdDIhGpMASVefe gsW3HKxWclY1TTesCL7cFj+0cuREGsjxVEmAzwKwJ1yoJqyLt4myF8S6e7YPnNy3SWYE WRxg== X-Forwarded-Encrypted: i=1; AJvYcCUjjhcURpuY7jjb1Y9Nfyy57pGOaolsVk2vbvfbIzujbounyalfUcLvMBy2LlfOj/30Ck051SQoa/aas20=@vger.kernel.org X-Gm-Message-State: AOJu0YyQk19Tw6gFyURb86Ccd06jJnl9LKYOZt+APLRPi1afR7IdJGpo y05vHWBeQnSmHGcgN0gZj8YbmwHiNqi4BjeK4yvYZiNlu3H8kpMspLo1fSyHHMcRlxs= X-Gm-Gg: ASbGncuYlbg/estB1CZm/78MAAFO/7G0UKnv67a8IFvch+eWPyvUnYyPRXd8cWR2643 wIvfP0dz9kNEDjLRhoxvLGlqbLgNSgKD4Rrs0kpN1dawvcnX+AosKPSaTonkp1KAp6My/mzf0fZ PQvOHCnDhM9pjN4n55F6zKphn1EvbpL6tFR6cofwL22ItDmGTzc7KRj24jJsE+fyOHgLz4kA8Og kkBMeMiwaiJiKoGDPkKiRTTgR5L8QMttkg/cu9kqHz4xaijyr58Ajtey626uPTuLLlrNx0EPVpG B8K2smx/bI0mYjbOKFW7rDAzf24LIStCHeFCOrGsd1SskKth6rXLvyxRX6tPJDHpI/kcyuurZ2F dIF4OwtnmJ/KggLJJBzm/deMwLHDqcN7TIWtlsLFnVkhqOvXe01vVZK8qdNqL+esMJafceIs/TO wlvgoJu3XCdYX4r73gTAIqgkvAkD2/IZH1eG9QkrrfeimftatbJ9yucOwc5BOWdXmNx/cESDbWI HDx6xE= X-Google-Smtp-Source: AGHT+IHkNZ1O05/wx0JB3pi70sYZwOs5GclCpL45vuZfhM2JjOjXMdM7CjvQ81K6Kvb3Obn+lJkkcQ== X-Received: by 2002:a05:690c:3690:b0:788:182e:b29e with SMTP id 00721157ae682-78a8b47de80mr61772657b3.7.1763850276575; Sat, 22 Nov 2025 14:24:36 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:36 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 20/22] liveupdate: luo_file: Add internal APIs for file preservation Date: Sat, 22 Nov 2025 17:23:47 -0500 Message-ID: <20251122222351.1059049-21-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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 core liveupdate mechanism allows userspace to preserve file descriptors. However, kernel subsystems often manage struct file objects directly and need to participate in the preservation process programmatically without relying solely on userspace interaction. Signed-off-by: Pasha Tatashin --- include/linux/liveupdate.h | 23 +++++++++++ kernel/liveupdate/luo_file.c | 71 ++++++++++++++++++++++++++++++++ kernel/liveupdate/luo_internal.h | 16 +++++++ 3 files changed, 110 insertions(+) diff --git a/include/linux/liveupdate.h b/include/linux/liveupdate.h index a7f6ee5b6771..c9503d2cda7b 100644 --- a/include/linux/liveupdate.h +++ b/include/linux/liveupdate.h @@ -15,11 +15,13 @@ #include =20 struct liveupdate_file_handler; +struct liveupdate_session; struct file; =20 /** * struct liveupdate_file_op_args - Arguments for file operation callbacks. * @handler: The file handler being called. + * @session: The session this file belongs to. * @retrieved: The retrieve status for the 'can_finish / finish' * operation. * @file: The file object. For retrieve: [OUT] The callback se= ts @@ -37,6 +39,7 @@ struct file; */ struct liveupdate_file_op_args { struct liveupdate_file_handler *handler; + struct liveupdate_session *session; bool retrieved; struct file *file; u64 serialized_data; @@ -112,6 +115,14 @@ int liveupdate_reboot(void); int liveupdate_register_file_handler(struct liveupdate_file_handler *fh); int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh); =20 +/* kernel can internally retrieve files */ +int liveupdate_get_file_incoming(struct liveupdate_session *s, u64 token, + struct file **filep); + +/* Get a token for an outgoing file, or -ENOENT if file is not preserved */ +int liveupdate_get_token_outgoing(struct liveupdate_session *s, + struct file *file, u64 *tokenp); + #else /* CONFIG_LIVEUPDATE */ =20 static inline bool liveupdate_enabled(void) @@ -134,5 +145,17 @@ static inline int liveupdate_unregister_file_handler(s= truct liveupdate_file_hand return -EOPNOTSUPP; } =20 +static inline int liveupdate_get_file_incoming(struct liveupdate_session *= s, + u64 token, struct file **filep) +{ + return -EOPNOTSUPP; +} + +static inline int liveupdate_get_token_outgoing(struct liveupdate_session = *s, + struct file *file, u64 *tokenp) +{ + return -EOPNOTSUPP; +} + #endif /* CONFIG_LIVEUPDATE */ #endif /* _LINUX_LIVEUPDATE_H */ diff --git a/kernel/liveupdate/luo_file.c b/kernel/liveupdate/luo_file.c index b235d1b562ad..670649932de4 100644 --- a/kernel/liveupdate/luo_file.c +++ b/kernel/liveupdate/luo_file.c @@ -299,6 +299,7 @@ int luo_preserve_file(struct luo_file_set *file_set, u6= 4 token, int fd) mutex_init(&luo_file->mutex); =20 args.handler =3D fh; + args.session =3D luo_session_from_file_set(file_set); args.file =3D file; err =3D fh->ops->preserve(&args); if (err) @@ -350,6 +351,7 @@ void luo_file_unpreserve_files(struct luo_file_set *fil= e_set) struct luo_file, list); =20 args.handler =3D luo_file->fh; + args.session =3D luo_session_from_file_set(file_set); args.file =3D luo_file->file; args.serialized_data =3D luo_file->serialized_data; args.private_data =3D luo_file->private_data; @@ -377,6 +379,7 @@ static int luo_file_freeze_one(struct luo_file_set *fil= e_set, struct liveupdate_file_op_args args =3D {0}; =20 args.handler =3D luo_file->fh; + args.session =3D luo_session_from_file_set(file_set); args.file =3D luo_file->file; args.serialized_data =3D luo_file->serialized_data; args.private_data =3D luo_file->private_data; @@ -398,6 +401,7 @@ static void luo_file_unfreeze_one(struct luo_file_set *= file_set, struct liveupdate_file_op_args args =3D {0}; =20 args.handler =3D luo_file->fh; + args.session =3D luo_session_from_file_set(file_set); args.file =3D luo_file->file; args.serialized_data =3D luo_file->serialized_data; args.private_data =3D luo_file->private_data; @@ -581,6 +585,7 @@ int luo_retrieve_file(struct luo_file_set *file_set, u6= 4 token, } =20 args.handler =3D luo_file->fh; + args.session =3D luo_session_from_file_set(file_set); args.serialized_data =3D luo_file->serialized_data; err =3D luo_file->fh->ops->retrieve(&args); if (!err) { @@ -606,6 +611,7 @@ static int luo_file_can_finish_one(struct luo_file_set = *file_set, struct liveupdate_file_op_args args =3D {0}; =20 args.handler =3D luo_file->fh; + args.session =3D luo_session_from_file_set(file_set); args.file =3D luo_file->file; args.serialized_data =3D luo_file->serialized_data; args.retrieved =3D luo_file->retrieved; @@ -623,6 +629,7 @@ static void luo_file_finish_one(struct luo_file_set *fi= le_set, guard(mutex)(&luo_file->mutex); =20 args.handler =3D luo_file->fh; + args.session =3D luo_session_from_file_set(file_set); args.file =3D luo_file->file; args.serialized_data =3D luo_file->serialized_data; args.retrieved =3D luo_file->retrieved; @@ -889,3 +896,67 @@ int liveupdate_unregister_file_handler(struct liveupda= te_file_handler *fh) =20 return 0; } + +/** + * liveupdate_get_token_outgoing - Get the token for a preserved file. + * @s: The outgoing liveupdate session. + * @file: The file object to search for. + * @tokenp: Output parameter for the found token. + * + * Searches the list of preserved files in an outgoing session for a match= ing + * file object. If found, the corresponding user-provided token is returne= d. + * + * This function is intended for in-kernel callers that need to correlate a + * file with its liveupdate token. + * + * Context: Can be called from any context that can acquire the session mu= tex. + * Return: 0 on success, -ENOENT if the file is not preserved in this sess= ion. + */ +int liveupdate_get_token_outgoing(struct liveupdate_session *s, + struct file *file, u64 *tokenp) +{ + struct luo_file_set *file_set =3D luo_file_set_from_session(s); + struct luo_file *luo_file; + int err =3D -ENOENT; + + list_for_each_entry(luo_file, &file_set->files_list, list) { + if (luo_file->file =3D=3D file) { + if (tokenp) + *tokenp =3D luo_file->token; + err =3D 0; + break; + } + } + + return err; +} + +/** + * liveupdate_get_file_incoming - Retrieves a preserved file for in-kernel= use. + * @s: The incoming liveupdate session (restored from the previous ke= rnel). + * @token: The unique token identifying the file to retrieve. + * @filep: On success, this will be populated with a pointer to the retri= eved + * 'struct file'. + * + * Provides a kernel-internal API for other subsystems to retrieve their + * preserved files after a live update. This function is a simple wrapper + * around luo_retrieve_file(), allowing callers to find a file by its toke= n. + * + * The operation is idempotent; subsequent calls for the same token will r= eturn + * a pointer to the same 'struct file' object. + * + * The caller receives a new reference to the file and must call fput() wh= en it + * is no longer needed. The file's lifetime is managed by LUO and any user= space + * file descriptors. If the caller needs to hold a reference to the file b= eyond + * the immediate scope, it must call get_file() itself. + * + * Context: Can be called from any context in the new kernel that has a ha= ndle + * to a restored session. + * Return: 0 on success. Returns -ENOENT if no file with the matching toke= n is + * found, or any other negative errno on failure. + */ +int liveupdate_get_file_incoming(struct liveupdate_session *s, u64 token, + struct file **filep) +{ + return luo_retrieve_file(luo_file_set_from_session(s), token, filep); +} diff --git a/kernel/liveupdate/luo_internal.h b/kernel/liveupdate/luo_inter= nal.h index c8973b543d1d..734469464664 100644 --- a/kernel/liveupdate/luo_internal.h +++ b/kernel/liveupdate/luo_internal.h @@ -84,6 +84,22 @@ struct luo_session { struct mutex mutex; }; =20 +static inline struct liveupdate_session *luo_session_from_file_set(struct = luo_file_set *file_set) +{ + struct luo_session *session; + + session =3D container_of(file_set, struct luo_session, file_set); + + return (struct liveupdate_session *)session; +} + +static inline struct luo_file_set *luo_file_set_from_session(struct liveup= date_session *s) +{ + struct luo_session *session =3D (struct luo_session *)s; + + return &session->file_set; +} + int luo_session_create(const char *name, struct file **filep); int luo_session_retrieve(const char *name, struct file **filep); int __init luo_session_setup_outgoing(void *fdt); --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) (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 2032F2EB5BA for ; Sat, 22 Nov 2025 22:24:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850285; cv=none; b=igoZD0DimLeqSA2Ucw1g/k3UEOfGvPzn/uU/cLsB3VRx2mQKIHT8BtWjObXBa++1FpV42Xm1/eN8hNsiND1jl6KOaknYNHVlTtVipfGnuqMuCGrSoVfzA1q+p4dbOVjXAAD3xt7hqYpKNMneBnU+SC7ZnwmtKLdbxwXSRs6qq0A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850285; c=relaxed/simple; bh=l7lKW7DaGzZAp2GvmYc9Wma/ghnFqf3j0aNhK2onjcA=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DHrzNbF0tXCPdRngH0DZvpKbZ/uHojEXqXodaiVBK6JwM+88EzVkF7tpr06AEETdl3/VJM1C4GpzXtKz/9ok/eDb7YoxxH4b8mCHeCihcvIJTQAfeyJNqMB1dkskiPhdIDXcz3EiWgn2YGGmGtM3v94NePlOjeaX4pScE/4B57c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=UvF9Ga2y; arc=none smtp.client-ip=209.85.128.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="UvF9Ga2y" Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-787f586532bso28619437b3.1 for ; Sat, 22 Nov 2025 14:24:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850279; x=1764455079; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=ejaHDjieDr/pDzy+upfATTsYn5XdzeNqJKeKiiB3q/I=; b=UvF9Ga2yQz100beXkHJFU8H0xHEvjxRcGb1KjagcR08ZhX/fPhebJ3tRwRBk+xB9Iv xRvIgs90n73qylvZ9hujvlaOxkrrPccdZ1Weux/EwhN7Y1PuGhSgCnaapR0VPyvgY/2T CItOGbAmvRIgrkannHGrE8f62ppa2bEkEz+mxpI6FnX1PBJUJN+6Q+hxYabmx/ZeLkND eAH1jHmI+D+jt4ruVeDzq60Fh3sd7lbYej0hd/ZztFo6imPwhV7tLHUoHQn4z/umvrnE wvvi+TMVBADD9O/73gppt5SMjUUYSKXZJhvMIk0ExwliuCGOhraL4TnSjCk9KVsnGVzg LbHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850279; x=1764455079; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=ejaHDjieDr/pDzy+upfATTsYn5XdzeNqJKeKiiB3q/I=; b=QbYIDnluR7rOLU25ne3xn9/qjeCY8ARr6hTEODSSM2Uol+QakhLYnyWoQCxt4ouHUq 9CC7flN92ri5EDaQRJLBlBKEQO1uCzECFF/pYn5m/5jPAJ3HPzUyzaX3yRc0J4evVNA4 kugEi3n+zaT1OMaBBGJ5EOfKEfZ/FfhMQVKqiKALCITAWrI7oJ7Gk3kSB7jUdfiCOEKI Zi6zvH1Y6xIL9ExBSc+nA23b/BqpzfJcmxf7P0TpKmYE4uZaCVGhLckf9PvBTkmaawzp AoCqXm9u1A9IE3IY4axYh/9CBhENhKsueDLrQwk6i1YbyxWxAscYLh9Wv3DpbguYtUz0 tu3g== X-Forwarded-Encrypted: i=1; AJvYcCW6UW1xBwy0CjtHk37ojwasaRG2/XQYbv9UkmEXGhMKS2FcWAOPh/0YkFzCUoEDWMUwphwN87PjifCJTao=@vger.kernel.org X-Gm-Message-State: AOJu0YxzTS1+dAHG6A74gr98a7+yc7xjHKOAaDUs/J+1FIIRhTaZgGDL qUyvVpblI+W8PXeqG/V2EsPWINmIH4JHFQ6mVOYbnFjCgjgeXI9eKVyFbffA9hAJLSU= X-Gm-Gg: ASbGncuil15haTOCCj+IfAAB16DDExKIiSqU1Jw62038OCM4WDGQqimpcf2vG0nJYou SAmLHN/9uXBqZBJFEP3DBweQ86UuwaywU5HWwzuXa15EK098QJeVY6YPVAJNuRwkzVQVjltRr0f XwW5m6sz32G+O21wfBWz7bUsm5sYeynDM6eltHd4TZ6zswDud7L/sgaPZjjGAJsjICCW9j9/kgk 58HWBQRpTXnZt7R1sqHhc2KNNxN0+c8G7bHj5Ub44qCWat+18kKWxz7Zh2jZW7Lkw4vvCaonjD1 lCw1KGZLTvN8YEorcqqyvsVFEc5B+AOdMAhheglz0oholZENvR6kLT6UVcnPQV7zxQYVnyyDAJ8 OkxxlBttwxVUC14G/hlwHwTnQHb0L0gIXcJJiBu5DrBpZGK9JO048qFaX/xXZvvrIzt7TTedPDU CYowME8HggRCu4vcXoCzus4OOBNPpHuaS1MIpCVcGHlk42Wfj+22TqSSIGXwUXUGOqiBaA0Zpbx oeKQMU= X-Google-Smtp-Source: AGHT+IHaO2M7rzXS9aV6uHz8pg9B2XKFY+NkqugdpRPdeDAsg/9VEMgnKluajVBx51kFvPB+f80XFA== X-Received: by 2002:a05:690c:6a06:b0:787:e9bc:fa85 with SMTP id 00721157ae682-78a8b5780demr56833317b3.68.1763850278474; Sat, 22 Nov 2025 14:24:38 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:38 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 21/22] liveupdate: luo_flb: Introduce File-Lifecycle-Bound global state Date: Sat, 22 Nov 2025 17:23:48 -0500 Message-ID: <20251122222351.1059049-22-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" Introduce a mechanism for managing global kernel state whose lifecycle is tied to the preservation of one or more files. This is necessary for subsystems where multiple preserved file descriptors depend on a single, shared underlying resource. An example is HugeTLB, where multiple file descriptors such as memfd and guest_memfd may rely on the state of a single HugeTLB subsystem. Preserving this state for each individual file would be redundant and incorrect. The state should be preserved only once when the first file is preserved, and restored/finished only once the last file is handled. This patch introduces File-Lifecycle-Bound (FLB) objects to solve this problem. An FLB is a global, reference-counted object with a defined set of operations: - A file handler (struct liveupdate_file_handler) declares a dependency on one or more FLBs via a new registration function, liveupdate_register_flb(). - When the first file depending on an FLB is preserved, the FLB's .preserve() callback is invoked to save the shared global state. The reference count is then incremented for each subsequent file. - Conversely, when the last file is unpreserved (before reboot) or finished (after reboot), the FLB's .unpreserve() or .finish() callback is invoked to clean up the global resource. The implementation includes: - A new set of ABI definitions (luo_flb_ser, luo_flb_head_ser) and a corresponding FDT node (luo-flb) to serialize the state of all active FLBs and pass them via Kexec Handover. - Core logic in luo_flb.c to manage FLB registration, reference counting, and the invocation of lifecycle callbacks. - An API (liveupdate_flb_*_locked/*_unlock) for other kernel subsystems to safely access the live object managed by an FLB, both before and after the live update. This framework provides the necessary infrastructure for more complex subsystems like IOMMU, VFIO, and KVM to integrate with the Live Update Orchestrator. Signed-off-by: Pasha Tatashin --- Documentation/core-api/liveupdate.rst | 11 + include/linux/kho/abi/luo.h | 76 +++ include/linux/liveupdate.h | 150 ++++++ kernel/liveupdate/Makefile | 1 + kernel/liveupdate/luo_core.c | 7 +- kernel/liveupdate/luo_file.c | 21 +- kernel/liveupdate/luo_flb.c | 701 ++++++++++++++++++++++++++ kernel/liveupdate/luo_internal.h | 7 + 8 files changed, 972 insertions(+), 2 deletions(-) create mode 100644 kernel/liveupdate/luo_flb.c diff --git a/Documentation/core-api/liveupdate.rst b/Documentation/core-api= /liveupdate.rst index b776b625c60f..6d9f8b0363f6 100644 --- a/Documentation/core-api/liveupdate.rst +++ b/Documentation/core-api/liveupdate.rst @@ -18,6 +18,11 @@ LUO Preserving File Descriptors .. kernel-doc:: kernel/liveupdate/luo_file.c :doc: LUO File Descriptors =20 +LUO File Lifecycle Bound Global Data +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +.. kernel-doc:: kernel/liveupdate/luo_flb.c + :doc: LUO File Lifecycle Bound Global Data + Live Update Orchestrator ABI =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D .. kernel-doc:: include/linux/kho/abi/luo.h @@ -39,6 +44,9 @@ Public API .. kernel-doc:: kernel/liveupdate/luo_core.c :export: =20 +.. kernel-doc:: kernel/liveupdate/luo_flb.c + :export: + .. kernel-doc:: kernel/liveupdate/luo_file.c :export: =20 @@ -47,6 +55,9 @@ Internal API .. kernel-doc:: kernel/liveupdate/luo_core.c :internal: =20 +.. kernel-doc:: kernel/liveupdate/luo_flb.c + :internal: + .. kernel-doc:: kernel/liveupdate/luo_session.c :internal: =20 diff --git a/include/linux/kho/abi/luo.h b/include/linux/kho/abi/luo.h index fc143f243871..ea21ad1535bf 100644 --- a/include/linux/kho/abi/luo.h +++ b/include/linux/kho/abi/luo.h @@ -33,6 +33,11 @@ * compatible =3D "luo-session-v1"; * luo-session-header =3D ; * }; + * + * luo-flb { + * compatible =3D "luo-flb-v1"; + * luo-flb-header =3D ; + * }; * }; * * Main LUO Node (/): @@ -52,6 +57,17 @@ * is the header for a contiguous block of memory containing an array = of * `struct luo_session_ser`, one for each preserved session. * + * File-Lifecycle-Bound Node (luo-flb): + * This node describes all preserved global objects whose lifecycle is b= ound + * to that of the preserved files (e.g., shared IOMMU state). + * + * - compatible: "luo-flb-v1" + * Identifies the FLB ABI version. + * - luo-flb-header: u64 + * The physical address of a `struct luo_flb_header_ser`. This structu= re is + * the header for a contiguous block of memory containing an array of + * `struct luo_flb_ser`, one for each preserved global object. + * * Serialization Structures: * The FDT properties point to memory regions containing arrays of simpl= e, * `__packed` structures. These structures contain the actual preserved = state. @@ -70,6 +86,16 @@ * Metadata for a single preserved file. Contains the `compatible` str= ing to * find the correct handler in the new kernel, a user-provided `token`= for * identification, and an opaque `data` handle for the handler to use. + * + * - struct luo_flb_header_ser: + * Header for the FLB array. Contains the total page count of the + * preserved memory block and the number of `struct luo_flb_ser` entri= es + * that follow. + * + * - struct luo_flb_ser: + * Metadata for a single preserved global object. Contains its `name` + * (compatible string), an opaque `data` handle, and the `count` + * number of files depending on it. */ =20 #ifndef _LINUX_KHO_ABI_LUO_H @@ -159,4 +185,54 @@ struct luo_session_ser { struct luo_file_set_ser file_set_ser; } __packed; =20 +/* The max size is set so it can be reliably used during in serialization = */ +#define LIVEUPDATE_FLB_COMPAT_LENGTH 48 + +#define LUO_FDT_FLB_NODE_NAME "luo-flb" +#define LUO_FDT_FLB_COMPATIBLE "luo-flb-v1" +#define LUO_FDT_FLB_HEADER "luo-flb-header" + +/** + * struct luo_flb_header_ser - Header for the serialized FLB data block. + * @pgcnt: The total number of pages occupied by the entire preserved memo= ry + * region, including this header and the subsequent array of + * &struct luo_flb_ser entries. + * @count: The number of &struct luo_flb_ser entries that follow this head= er + * in the memory block. + * + * This structure is located at the physical address specified by the + * `LUO_FDT_FLB_HEADER` FDT property. It provides the new kernel with the + * necessary information to find and iterate over the array of preserved + * File-Lifecycle-Bound objects and to manage the underlying memory. + * + * If this structure is modified, LUO_FDT_FLB_COMPATIBLE must be updated. + */ +struct luo_flb_header_ser { + u64 pgcnt; + u64 count; +} __packed; + +/** + * struct luo_flb_ser - Represents the serialized state of a single FLB ob= ject. + * @name: The unique compatibility string of the FLB object, used to fi= nd the + * corresponding &struct liveupdate_flb handler in the new kerne= l. + * @data: The opaque u64 handle returned by the FLB's .preserve() opera= tion + * in the old kernel. This handle encapsulates the entire state = needed + * for restoration. + * @count: The reference count at the time of serialization; i.e., the n= umber + * of preserved files that depended on this FLB. This is used by= the + * new kernel to correctly manage the FLB's lifecycle. + * + * An array of these structures is created in a preserved memory region and + * passed to the new kernel. Each entry allows the LUO core to restore one + * global, shared object. + * + * If this structure is modified, LUO_FDT_FLB_COMPATIBLE must be updated. + */ +struct luo_flb_ser { + char name[LIVEUPDATE_FLB_COMPAT_LENGTH]; + u64 data; + u64 count; +} __packed; + #endif /* _LINUX_KHO_ABI_LUO_H */ diff --git a/include/linux/liveupdate.h b/include/linux/liveupdate.h index c9503d2cda7b..31d58455d6ba 100644 --- a/include/linux/liveupdate.h +++ b/include/linux/liveupdate.h @@ -11,10 +11,12 @@ #include #include #include +#include #include #include =20 struct liveupdate_file_handler; +struct liveupdate_flb; struct liveupdate_session; struct file; =20 @@ -102,6 +104,114 @@ struct liveupdate_file_handler { * registered file handlers. */ struct list_head __private list; + /* A list of FLB dependencies. */ + struct list_head __private flb_list; +}; + +/** + * struct liveupdate_flb_op_args - Arguments for FLB operation callbacks. + * @flb: The global FLB instance for which this call is performed. + * @data: For .preserve(): [OUT] The callback sets this field. + * For .unpreserve(): [IN] The handle from .preserve(). + * For .retrieve(): [IN] The handle from .preserve(). + * @obj: For .preserve(): [OUT] Sets this to the live object. + * For .retrieve(): [OUT] Sets this to the live object. + * For .finish(): [IN] The live object from .retrieve(). + * + * This structure bundles all parameters for the FLB operation callbacks. + */ +struct liveupdate_flb_op_args { + struct liveupdate_flb *flb; + u64 data; + void *obj; +}; + +/** + * struct liveupdate_flb_ops - Callbacks for global File-Lifecycle-Bound d= ata. + * @preserve: Called when the first file using this FLB is preserve= d. + * The callback must save its state and return a single, + * self-contained u64 handle by setting the 'argp->data' + * field and 'argp->obj'. + * @unpreserve: Called when the last file using this FLB is unpreserv= ed + * (aborted before reboot). Receives the handle via + * 'argp->data' and live object via 'argp->obj'. + * @retrieve: Called on-demand in the new kernel, the first time a + * component requests access to the shared object. It re= ceives + * the preserved handle via 'argp->data' and must recons= truct + * the live object, returning it by setting the 'argp->o= bj' + * field. + * @finish: Called in the new kernel when the last file using thi= s FLB + * is finished. Receives the live object via 'argp->obj'= for + * cleanup. + * @owner: Module reference + * + * Operations that manage global shared data with file bound lifecycle, + * triggered by the first file that uses it and concluded by the last file= that + * uses it, across all sessions. + */ +struct liveupdate_flb_ops { + int (*preserve)(struct liveupdate_flb_op_args *argp); + void (*unpreserve)(struct liveupdate_flb_op_args *argp); + int (*retrieve)(struct liveupdate_flb_op_args *argp); + void (*finish)(struct liveupdate_flb_op_args *argp); + struct module *owner; +}; + +/* + * struct luo_flb_private_state - Private FLB state structures. + * @count: The number of preserved files currently depending on this FLB. + * This is used to trigger the preserve/unpreserve/finish ops on t= he + * first/last file. + * @data: The opaque u64 handle returned by .preserve() or passed to + * .retrieve(). + * @obj: The live kernel object returned by .preserve() or .retrieve(). + * @lock: A mutex that protects all fields within this structure, providi= ng + * the synchronization service for the FLB's ops. + */ +struct luo_flb_private_state { + long count; + u64 data; + void *obj; + struct mutex lock; +}; + +/* + * struct luo_flb_private - Keep separate incoming and outgoing states. + * @list: A global list of registered FLBs. + * @outgoing: The runtime state for the pre-reboot + * (preserve/unpreserve) lifecycle. + * @incoming: The runtime state for the post-reboot (retrieve/finish) + * lifecycle. + * @users: With how many File-Handlers this FLB is registered. + * @initialized: true when private fields have been initialized. + */ +struct luo_flb_private { + struct list_head list; + struct luo_flb_private_state outgoing; + struct luo_flb_private_state incoming; + int users; + bool initialized; +}; + +/** + * struct liveupdate_flb - A global definition for a shared data object. + * @ops: Callback functions + * @compatible: The compatibility string (e.g., "iommu-core-v1" + * that uniquely identifies the FLB type this handler + * supports. This is matched against the compatible string + * associated with individual &struct liveupdate_flb + * instances. + * + * This struct is the "template" that a driver registers to define a share= d, + * file-lifecycle-bound object. The actual runtime state (the live object, + * refcount, etc.) is managed privately by the LUO core. + */ +struct liveupdate_flb { + const struct liveupdate_flb_ops *ops; + const char compatible[LIVEUPDATE_FLB_COMPAT_LENGTH]; + + /* private: */ + struct luo_flb_private __private private; }; =20 #ifdef CONFIG_LIVEUPDATE @@ -123,6 +233,16 @@ int liveupdate_get_file_incoming(struct liveupdate_ses= sion *s, u64 token, int liveupdate_get_token_outgoing(struct liveupdate_session *s, struct file *file, u64 *tokenp); =20 +int liveupdate_register_flb(struct liveupdate_file_handler *fh, + struct liveupdate_flb *flb); +int liveupdate_unregister_flb(struct liveupdate_file_handler *fh, + struct liveupdate_flb *flb); + +int liveupdate_flb_incoming_locked(struct liveupdate_flb *flb, void **objp= ); +void liveupdate_flb_incoming_unlock(struct liveupdate_flb *flb, void *obj); +int liveupdate_flb_outgoing_locked(struct liveupdate_flb *flb, void **objp= ); +void liveupdate_flb_outgoing_unlock(struct liveupdate_flb *flb, void *obj); + #else /* CONFIG_LIVEUPDATE */ =20 static inline bool liveupdate_enabled(void) @@ -157,5 +277,35 @@ static inline int liveupdate_get_token_outgoing(struct= liveupdate_session *s, return -EOPNOTSUPP; } =20 +static inline int liveupdate_register_flb(struct liveupdate_file_handler *= fh, + struct liveupdate_flb *flb) +{ + return -EOPNOTSUPP; +} + +static inline int liveupdate_unregister_flb(struct liveupdate_file_handler= *fh, + struct liveupdate_flb *flb) +{ + return -EOPNOTSUPP; +} + +static inline int liveupdate_flb_incoming_locked(struct liveupdate_flb *fl= b, + void **objp) +{ + return -EOPNOTSUPP; +} + +static inline void liveupdate_flb_incoming_unlock(struct liveupdate_flb *f= lb, + void *obj) { } + +static inline int liveupdate_flb_outgoing_locked(struct liveupdate_flb *fl= b, + void **objp) +{ + return -EOPNOTSUPP; +} + +static inline void liveupdate_flb_outgoing_unlock(struct liveupdate_flb *f= lb, + void *obj) { } + #endif /* CONFIG_LIVEUPDATE */ #endif /* _LINUX_LIVEUPDATE_H */ diff --git a/kernel/liveupdate/Makefile b/kernel/liveupdate/Makefile index 7cad2eece32d..d2f779cbe279 100644 --- a/kernel/liveupdate/Makefile +++ b/kernel/liveupdate/Makefile @@ -3,6 +3,7 @@ luo-y :=3D \ luo_core.o \ luo_file.o \ + luo_flb.o \ luo_session.o =20 obj-$(CONFIG_KEXEC_HANDOVER) +=3D kexec_handover.o diff --git a/kernel/liveupdate/luo_core.c b/kernel/liveupdate/luo_core.c index bc90954252a3..d839be582aff 100644 --- a/kernel/liveupdate/luo_core.c +++ b/kernel/liveupdate/luo_core.c @@ -127,7 +127,9 @@ static int __init luo_early_startup(void) if (err) return err; =20 - return 0; + err =3D luo_flb_setup_incoming(luo_global.fdt_in); + + return err; } =20 static int __init liveupdate_early_init(void) @@ -164,6 +166,7 @@ static int __init luo_fdt_setup(void) err |=3D fdt_property_string(fdt_out, "compatible", LUO_FDT_COMPATIBLE); err |=3D fdt_property(fdt_out, LUO_FDT_LIVEUPDATE_NUM, &ln, sizeof(ln)); err |=3D luo_session_setup_outgoing(fdt_out); + err |=3D luo_flb_setup_outgoing(fdt_out); err |=3D fdt_end_node(fdt_out); err |=3D fdt_finish(fdt_out); if (err) @@ -225,6 +228,8 @@ int liveupdate_reboot(void) if (err) return err; =20 + luo_flb_serialize(); + err =3D kho_finalize(); if (err) { pr_err("kho_finalize failed %d\n", err); diff --git a/kernel/liveupdate/luo_file.c b/kernel/liveupdate/luo_file.c index 670649932de4..ecaa93bbcb12 100644 --- a/kernel/liveupdate/luo_file.c +++ b/kernel/liveupdate/luo_file.c @@ -286,6 +286,10 @@ int luo_preserve_file(struct luo_file_set *file_set, u= 64 token, int fd) if (err) goto err_files_mem; =20 + err =3D luo_flb_file_preserve(fh); + if (err) + goto err_files_mem; + luo_file =3D kzalloc(sizeof(*luo_file), GFP_KERNEL); if (!luo_file) { err =3D -ENOMEM; @@ -315,6 +319,7 @@ int luo_preserve_file(struct luo_file_set *file_set, u6= 4 token, int fd) err_kfree: mutex_destroy(&luo_file->mutex); kfree(luo_file); + luo_flb_file_unpreserve(fh); err_files_mem: fput(file); luo_free_files_mem(file_set); @@ -356,6 +361,7 @@ void luo_file_unpreserve_files(struct luo_file_set *fil= e_set) args.serialized_data =3D luo_file->serialized_data; args.private_data =3D luo_file->private_data; luo_file->fh->ops->unpreserve(&args); + luo_flb_file_unpreserve(luo_file->fh); =20 list_del(&luo_file->list); file_set->count--; @@ -633,6 +639,7 @@ static void luo_file_finish_one(struct luo_file_set *fi= le_set, args.file =3D luo_file->file; args.serialized_data =3D luo_file->serialized_data; args.retrieved =3D luo_file->retrieved; + luo_flb_file_finish(luo_file->fh); =20 luo_file->fh->ops->finish(&args); } @@ -856,6 +863,7 @@ int liveupdate_register_file_handler(struct liveupdate_= file_handler *fh) goto err_resume; } =20 + INIT_LIST_HEAD(&ACCESS_PRIVATE(fh, flb_list)); INIT_LIST_HEAD(&ACCESS_PRIVATE(fh, list)); list_add_tail(&ACCESS_PRIVATE(fh, list), &luo_file_handler_list); luo_session_resume(); @@ -876,25 +884,36 @@ int liveupdate_register_file_handler(struct liveupdat= e_file_handler *fh) * * It ensures safe removal by checking that: * No live update session is currently in progress. + * No FLB registered with this file handler. * * If the unregistration fails, the internal test state is reverted. * * Return: 0 Success. -EOPNOTSUPP when live update is not enabled. -EBUSY = A live - * update is in progress, can't quiesce live update. + * update is in progress, can't quiesce live update or FLB is registred wi= th + * this file handler. */ int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh) { + int err =3D -EBUSY; + if (!liveupdate_enabled()) return -EOPNOTSUPP; =20 if (!luo_session_quiesce()) return -EBUSY; =20 + if (!list_empty(&ACCESS_PRIVATE(fh, flb_list))) + goto err_resume; + list_del(&ACCESS_PRIVATE(fh, list)); module_put(fh->ops->owner); luo_session_resume(); =20 return 0; + +err_resume: + luo_session_resume(); + return err; } =20 /** diff --git a/kernel/liveupdate/luo_flb.c b/kernel/liveupdate/luo_flb.c new file mode 100644 index 000000000000..bbae99d973df --- /dev/null +++ b/kernel/liveupdate/luo_flb.c @@ -0,0 +1,701 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + */ + +/** + * DOC: LUO File Lifecycle Bound Global Data + * + * File-Lifecycle-Bound (FLB) objects provide a mechanism for managing glo= bal + * state that is shared across multiple live-updatable files. The lifecycl= e of + * this shared state is tied to the preservation of the files that depend = on it. + * + * An FLB represents a global resource, such as the IOMMU core state, that= is + * required by multiple file descriptors (e.g., all VFIO fds). + * + * The preservation of the FLB's state is triggered when the *first* file + * depending on it is preserved. The cleanup of this state (unpreserve or + * finish) is triggered when the *last* file depending on it is unpreserve= d or + * finished. + * + * Handler Dependency: A file handler declares its dependency on one or mo= re + * FLBs by registering them via liveupdate_register_flb(). + * + * Callback Model: Each FLB is defined by a set of operations + * (&struct liveupdate_flb_ops) that LUO invokes at key points: + * + * - .preserve(): Called for the first file. Saves global state. + * - .unpreserve(): Called for the last file (if aborted pre-reboot). + * - .retrieve(): Called on-demand in the new kernel to restore the st= ate. + * - .finish(): Called for the last file in the new kernel for cleanup. + * + * This reference-counted approach ensures that shared state is saved exac= tly + * once and restored exactly once, regardless of how many files depend on = it, + * and that its lifecycle is correctly managed across the kexec transition. + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "luo_internal.h" + +#define LUO_FLB_PGCNT 1ul +#define LUO_FLB_MAX (((LUO_FLB_PGCNT << PAGE_SHIFT) - \ + sizeof(struct luo_flb_header_ser)) / sizeof(struct luo_flb_ser)) + +struct luo_flb_header { + struct luo_flb_header_ser *header_ser; + struct luo_flb_ser *ser; + bool active; +}; + +struct luo_flb_global { + struct luo_flb_header incoming; + struct luo_flb_header outgoing; + struct list_head list; + long count; +}; + +static struct luo_flb_global luo_flb_global =3D { + .list =3D LIST_HEAD_INIT(luo_flb_global.list), +}; + +/* + * struct luo_flb_link - Links an FLB definition to a file handler's inter= nal + * list of dependencies. + * @flb: A pointer to the registered &struct liveupdate_flb definition. + * @list: The list_head for linking. + */ +struct luo_flb_link { + struct liveupdate_flb *flb; + struct list_head list; +}; + +/* luo_flb_get_private - Access private field, and if needed initialize it= . */ +static struct luo_flb_private *luo_flb_get_private(struct liveupdate_flb *= flb) +{ + struct luo_flb_private *private =3D &ACCESS_PRIVATE(flb, private); + + if (!private->initialized) { + mutex_init(&private->incoming.lock); + mutex_init(&private->outgoing.lock); + INIT_LIST_HEAD(&private->list); + private->users =3D 0; + private->initialized =3D true; + } + + return private; +} + +static int luo_flb_file_preserve_one(struct liveupdate_flb *flb) +{ + struct luo_flb_private *private =3D luo_flb_get_private(flb); + + scoped_guard(mutex, &private->outgoing.lock) { + if (!private->outgoing.count) { + struct liveupdate_flb_op_args args =3D {0}; + int err; + + args.flb =3D flb; + err =3D flb->ops->preserve(&args); + if (err) + return err; + private->outgoing.data =3D args.data; + private->outgoing.obj =3D args.obj; + } + private->outgoing.count++; + } + + return 0; +} + +static void luo_flb_file_unpreserve_one(struct liveupdate_flb *flb) +{ + struct luo_flb_private *private =3D luo_flb_get_private(flb); + + scoped_guard(mutex, &private->outgoing.lock) { + private->outgoing.count--; + if (!private->outgoing.count) { + struct liveupdate_flb_op_args args =3D {0}; + + args.flb =3D flb; + args.data =3D private->outgoing.data; + args.obj =3D private->outgoing.obj; + + if (flb->ops->unpreserve) + flb->ops->unpreserve(&args); + + private->outgoing.data =3D 0; + private->outgoing.obj =3D NULL; + } + } +} + +static int luo_flb_retrieve_one(struct liveupdate_flb *flb) +{ + struct luo_flb_private *private =3D luo_flb_get_private(flb); + struct luo_flb_header *fh =3D &luo_flb_global.incoming; + struct liveupdate_flb_op_args args =3D {0}; + bool found =3D false; + int err; + + guard(mutex)(&private->incoming.lock); + + if (private->incoming.obj) + return 0; + + if (!fh->active) + return -ENODATA; + + for (int i =3D 0; i < fh->header_ser->count; i++) { + if (!strcmp(fh->ser[i].name, flb->compatible)) { + private->incoming.data =3D fh->ser[i].data; + private->incoming.count =3D fh->ser[i].count; + found =3D true; + break; + } + } + + if (!found) + return -ENOENT; + + args.flb =3D flb; + args.data =3D private->incoming.data; + + err =3D flb->ops->retrieve(&args); + if (err) + return err; + + private->incoming.obj =3D args.obj; + + if (WARN_ON_ONCE(!private->incoming.obj)) + return -EIO; + + return 0; +} + +static void luo_flb_file_finish_one(struct liveupdate_flb *flb) +{ + struct luo_flb_private *private =3D luo_flb_get_private(flb); + u64 count; + + scoped_guard(mutex, &private->incoming.lock) + count =3D --private->incoming.count; + + if (!count) { + struct liveupdate_flb_op_args args =3D {0}; + + if (!private->incoming.obj) { + int err =3D luo_flb_retrieve_one(flb); + + if (WARN_ON(err)) + return; + } + + scoped_guard(mutex, &private->incoming.lock) { + args.flb =3D flb; + args.obj =3D private->incoming.obj; + flb->ops->finish(&args); + + private->incoming.data =3D 0; + private->incoming.obj =3D NULL; + } + } +} + +/** + * luo_flb_file_preserve - Notifies FLBs that a file is about to be preser= ved. + * @fh: The file handler for the preserved file. + * + * This function iterates through all FLBs associated with the given file + * handler. It increments the reference count for each FLB. If the count b= ecomes + * 1, it triggers the FLB's .preserve() callback to save the global state. + * + * This operation is atomic. If any FLB's .preserve() op fails, it will ro= ll + * back by calling .unpreserve() on any FLBs that were successfully preser= ved + * during this call. + * + * Context: Called from luo_preserve_file() + * Return: 0 on success, or a negative errno on failure. + */ +int luo_flb_file_preserve(struct liveupdate_file_handler *fh) +{ + struct list_head *flb_list =3D &ACCESS_PRIVATE(fh, flb_list); + struct luo_flb_link *iter; + int err =3D 0; + + list_for_each_entry(iter, flb_list, list) { + err =3D luo_flb_file_preserve_one(iter->flb); + if (err) + goto exit_err; + } + + return 0; + +exit_err: + list_for_each_entry_continue_reverse(iter, flb_list, list) + luo_flb_file_unpreserve_one(iter->flb); + + return err; +} + +/** + * luo_flb_file_unpreserve - Notifies FLBs that a dependent file was unpre= served. + * @fh: The file handler for the unpreserved file. + * + * This function iterates through all FLBs associated with the given file + * handler, in reverse order of registration. It decrements the reference = count + * for each FLB. If the count becomes 0, it triggers the FLB's .unpreserve= () + * callback to clean up the global state. + * + * Context: Called when a preserved file is being cleaned up before reboot + * (e.g., from luo_file_unpreserve_files()). + */ +void luo_flb_file_unpreserve(struct liveupdate_file_handler *fh) +{ + struct list_head *flb_list =3D &ACCESS_PRIVATE(fh, flb_list); + struct luo_flb_link *iter; + + list_for_each_entry_reverse(iter, flb_list, list) + luo_flb_file_unpreserve_one(iter->flb); +} + +/** + * luo_flb_file_finish - Notifies FLBs that a dependent file has been fini= shed. + * @fh: The file handler for the finished file. + * + * This function iterates through all FLBs associated with the given file + * handler, in reverse order of registration. It decrements the incoming + * reference count for each FLB. If the count becomes 0, it triggers the F= LB's + * .finish() callback for final cleanup in the new kernel. + * + * Context: Called from luo_file_finish() for each file being finished. + */ +void luo_flb_file_finish(struct liveupdate_file_handler *fh) +{ + struct list_head *flb_list =3D &ACCESS_PRIVATE(fh, flb_list); + struct luo_flb_link *iter; + + list_for_each_entry_reverse(iter, flb_list, list) + luo_flb_file_finish_one(iter->flb); +} + +/** + * liveupdate_register_flb - Associate an FLB with a file handler and regi= ster it globally. + * @fh: The file handler that will now depend on the FLB. + * @flb: The File-Lifecycle-Bound object to associate. + * + * Establishes a dependency, informing the LUO core that whenever a file of + * type @fh is preserved, the state of @flb must also be managed. + * + * On the first registration of a given @flb object, it is added to a glob= al + * registry. This function checks for duplicate registrations, both for a + * specific handler and globally, and ensures the total number of unique + * FLBs does not exceed the system limit. + * + * Context: Typically called from a subsystem's module init function after + * both the handler and the FLB have been defined and initialized. + * Return: 0 on success. Returns a negative errno on failure: + * -EINVAL if arguments are NULL or not initialized. + * -ENOMEM on memory allocation failure. + * -EEXIST if this FLB is already registered with this handler. + * -ENOSPC if the maximum number of global FLBs has been reached. + * -EOPNOTSUPP if live update is disabled or not configured. + */ +int liveupdate_register_flb(struct liveupdate_file_handler *fh, + struct liveupdate_flb *flb) +{ + struct luo_flb_private *private =3D luo_flb_get_private(flb); + struct list_head *flb_list =3D &ACCESS_PRIVATE(fh, flb_list); + struct luo_flb_link *link __free(kfree) =3D NULL; + struct liveupdate_flb *gflb; + struct luo_flb_link *iter; + int err; + + if (!liveupdate_enabled()) + return -EOPNOTSUPP; + + if (WARN_ON(!flb->ops->preserve || !flb->ops->unpreserve || + !flb->ops->retrieve || !flb->ops->finish)) { + return -EINVAL; + } + + /* + * File handler must already be registered, as it initializes the + * flb_list + */ + if (WARN_ON(list_empty(&ACCESS_PRIVATE(fh, list)))) + return -EINVAL; + + link =3D kzalloc(sizeof(*link), GFP_KERNEL); + if (!link) + return -ENOMEM; + + /* + * Ensure the system is quiescent (no active sessions). + * This acts as a global lock for registration: no other thread can + * be in this section, and no sessions can be creating/using FDs. + */ + if (!luo_session_quiesce()) + return -EBUSY; + + /* Check that this FLB is not already linked to this file handler */ + err =3D -EEXIST; + list_for_each_entry(iter, flb_list, list) { + if (iter->flb =3D=3D flb) + goto err_resume; + } + + /* + * If this FLB is not linked to global list it's the first time the FLB + * is registered + */ + if (!private->users) { + if (WARN_ON(!list_empty(&private->list))) { + err =3D -EINVAL; + goto err_resume; + } + + if (luo_flb_global.count =3D=3D LUO_FLB_MAX) { + err =3D -ENOSPC; + goto err_resume; + } + + /* Check that compatible string is unique in global list */ + luo_list_for_each_private(gflb, &luo_flb_global.list, private.list) { + if (!strcmp(gflb->compatible, flb->compatible)) + goto err_resume; + } + + if (!try_module_get(flb->ops->owner)) { + err =3D -EAGAIN; + goto err_resume; + } + + list_add_tail(&private->list, &luo_flb_global.list); + luo_flb_global.count++; + } + + /* Finally, link the FLB to the file handler */ + private->users++; + link->flb =3D flb; + list_add_tail(&no_free_ptr(link)->list, flb_list); + luo_session_resume(); + + return 0; + +err_resume: + luo_session_resume(); + return err; +} + +/** + * liveupdate_unregister_flb - Remove an FLB dependency from a file handle= r. + * @fh: The file handler that is currently depending on the FLB. + * @flb: The File-Lifecycle-Bound object to remove. + * + * Removes the association between the specified file handler and the FLB + * previously established by liveupdate_register_flb(). + * + * This function manages the global lifecycle of the FLB. It decrements the + * FLB's usage count. If this was the last file handler referencing this F= LB, + * the FLB is removed from the global registry and the reference to its + * owner module (acquired during registration) is released. + * + * Context: This function ensures the session is quiesced (no active FDs + * being created) during the update. It is typically called from a + * subsystem's module exit function. + * Return: 0 on success. + * -EOPNOTSUPP if live update is disabled. + * -EBUSY if the live update session is active and cannot be quies= ced. + * -ENOENT if the FLB was not found in the file handler's list. + */ +int liveupdate_unregister_flb(struct liveupdate_file_handler *fh, + struct liveupdate_flb *flb) +{ + struct luo_flb_private *private =3D luo_flb_get_private(flb); + struct list_head *flb_list =3D &ACCESS_PRIVATE(fh, flb_list); + struct luo_flb_link *iter; + int err =3D -ENOENT; + + if (!liveupdate_enabled()) + return -EOPNOTSUPP; + + /* + * Ensure the system is quiescent (no active sessions). + * This acts as a global lock for unregistration. + */ + if (!luo_session_quiesce()) + return -EBUSY; + + /* Find and remove the link from the file handler's list */ + list_for_each_entry(iter, flb_list, list) { + if (iter->flb =3D=3D flb) { + list_del(&iter->list); + kfree(iter); + err =3D 0; + break; + } + } + + if (err) + goto err_resume; + + private->users--; + /* + * If this is the last file-handler with which we are registred, remove + * from the global list, and relese module reference. + */ + if (!private->users) { + list_del_init(&private->list); + luo_flb_global.count--; + module_put(flb->ops->owner); + } + + luo_session_resume(); + + return 0; + +err_resume: + luo_session_resume(); + return err; +} + +static int luo_flb_locked(struct liveupdate_flb *flb, bool incoming, + void **objp) +{ + struct luo_flb_private *private =3D luo_flb_get_private(flb); + struct luo_flb_private_state *state; + + if (!liveupdate_enabled()) + return -EOPNOTSUPP; + + if (incoming) { + state =3D &private->incoming; + if (!state->obj) { + int err =3D luo_flb_retrieve_one(flb); + + if (err) + return err; + } + } else { + state =3D &private->outgoing; + + /* Sanity check that object exists */ + if (WARN_ON_ONCE(!state->obj)) + return -ENOENT; + } + + mutex_lock(&state->lock); + *objp =3D state->obj; + + return 0; +} + +static void luo_flb_unlock(struct liveupdate_flb *flb, bool incoming, + void *obj) +{ + struct luo_flb_private *private =3D luo_flb_get_private(flb); + struct luo_flb_private_state *state; + + state =3D incoming ? &private->incoming : &private->outgoing; + + lockdep_assert_held(&state->lock); + state->obj =3D obj; + mutex_unlock(&state->lock); +} + +/** + * liveupdate_flb_incoming_locked - Lock and retrieve the incoming FLB obj= ect. + * @flb: The FLB definition. + * @objp: Output parameter; will be populated with the live shared object. + * + * Acquires the FLB's internal lock and returns a pointer to its shared li= ve + * object for the incoming (post-reboot) path. + * + * If this is the first time the object is requested in the new kernel, th= is + * function will trigger the FLB's .retrieve() callback to reconstruct the + * object from its preserved state. Subsequent calls will return the same + * cached object. + * + * The caller MUST call liveupdate_flb_incoming_unlock() to release the lo= ck. + * + * Return: 0 on success, or a negative errno on failure. -ENODATA means no + * incoming FLB data, -ENOENT means specific flb not found in the incoming + * data, and -EOPNOTSUPP when live update is disabled or not configured. + */ +int liveupdate_flb_incoming_locked(struct liveupdate_flb *flb, void **objp) +{ + return luo_flb_locked(flb, true, objp); +} + +/** + * liveupdate_flb_incoming_unlock - Unlock an incoming FLB object. + * @flb: The FLB definition. + * @obj: The object that was returned by the _locked call + * + * Releases the internal lock acquired by liveupdate_flb_incoming_locked(). + */ +void liveupdate_flb_incoming_unlock(struct liveupdate_flb *flb, void *obj) +{ + luo_flb_unlock(flb, true, obj); +} + +/** + * liveupdate_flb_outgoing_locked - Lock and retrieve the outgoing FLB obj= ect. + * @flb: The FLB definition. + * @objp: Output parameter; will be populated with the live shared object. + * + * Acquires the FLB's internal lock and returns a pointer to its shared li= ve + * object for the outgoing (pre-reboot) path. + * + * This function assumes the object has already been created by the FLB's + * .preserve() callback, which is triggered when the first dependent file + * is preserved. + * + * The caller MUST call liveupdate_flb_outgoing_unlock() to release the lo= ck. + * + * Return: 0 on success, or a negative errno on failure. + */ +int liveupdate_flb_outgoing_locked(struct liveupdate_flb *flb, void **objp) +{ + return luo_flb_locked(flb, false, objp); +} + +/** + * liveupdate_flb_outgoing_unlock - Unlock an outgoing FLB object. + * @flb: The FLB definition. + * @obj: The object that was returned by the _locked call + * + * Releases the internal lock acquired by liveupdate_flb_outgoing_locked(). + */ +void liveupdate_flb_outgoing_unlock(struct liveupdate_flb *flb, void *obj) +{ + luo_flb_unlock(flb, false, obj); +} + +int __init luo_flb_setup_outgoing(void *fdt_out) +{ + struct luo_flb_header_ser *header_ser; + u64 header_ser_pa; + int err; + + header_ser =3D kho_alloc_preserve(LUO_FLB_PGCNT << PAGE_SHIFT); + if (IS_ERR(header_ser)) + return PTR_ERR(header_ser); + + header_ser_pa =3D virt_to_phys(header_ser); + + err =3D fdt_begin_node(fdt_out, LUO_FDT_FLB_NODE_NAME); + err |=3D fdt_property_string(fdt_out, "compatible", + LUO_FDT_FLB_COMPATIBLE); + err |=3D fdt_property(fdt_out, LUO_FDT_FLB_HEADER, &header_ser_pa, + sizeof(header_ser_pa)); + err |=3D fdt_end_node(fdt_out); + + if (err) + goto err_unpreserve; + + header_ser->pgcnt =3D LUO_FLB_PGCNT; + luo_flb_global.outgoing.header_ser =3D header_ser; + luo_flb_global.outgoing.ser =3D (void *)(header_ser + 1); + luo_flb_global.outgoing.active =3D true; + + return 0; + +err_unpreserve: + kho_unpreserve_free(header_ser); + + return err; +} + +int __init luo_flb_setup_incoming(void *fdt_in) +{ + struct luo_flb_header_ser *header_ser; + int err, header_size, offset; + const void *ptr; + u64 header_ser_pa; + + offset =3D fdt_subnode_offset(fdt_in, 0, LUO_FDT_FLB_NODE_NAME); + if (offset < 0) { + pr_err("Unable to get FLB node [%s]\n", LUO_FDT_FLB_NODE_NAME); + + return -ENOENT; + } + + err =3D fdt_node_check_compatible(fdt_in, offset, + LUO_FDT_FLB_COMPATIBLE); + if (err) { + pr_err("FLB node is incompatible with '%s' [%d]\n", + LUO_FDT_FLB_COMPATIBLE, err); + + return -EINVAL; + } + + header_size =3D 0; + ptr =3D fdt_getprop(fdt_in, offset, LUO_FDT_FLB_HEADER, &header_size); + if (!ptr || header_size !=3D sizeof(u64)) { + pr_err("Unable to get FLB header property '%s' [%d]\n", + LUO_FDT_FLB_HEADER, header_size); + + return -EINVAL; + } + + header_ser_pa =3D get_unaligned((u64 *)ptr); + header_ser =3D phys_to_virt(header_ser_pa); + + luo_flb_global.incoming.header_ser =3D header_ser; + luo_flb_global.incoming.ser =3D (void *)(header_ser + 1); + luo_flb_global.incoming.active =3D true; + + return 0; +} + +/** + * luo_flb_serialize - Serializes all active FLB objects for KHO. + * + * This function is called from the reboot path. It iterates through all + * registered File-Lifecycle-Bound (FLB) objects. For each FLB that has be= en + * preserved (i.e., its reference count is greater than zero), it writes i= ts + * metadata into the memory region designated for Kexec Handover. + * + * The serialized data includes the FLB's compatibility string, its opaque + * data handle, and the final reference count. This allows the new kernel = to + * find the appropriate handler and reconstruct the FLB's state. + * + * Context: Called from liveupdate_reboot() just before kho_finalize(). + */ +void luo_flb_serialize(void) +{ + struct luo_flb_header *fh =3D &luo_flb_global.outgoing; + struct liveupdate_flb *gflb; + int i =3D 0; + + luo_list_for_each_private(gflb, &luo_flb_global.list, private.list) { + struct luo_flb_private *private =3D luo_flb_get_private(gflb); + + if (private->outgoing.count > 0) { + strscpy(fh->ser[i].name, gflb->compatible, + sizeof(fh->ser[i].name)); + fh->ser[i].data =3D private->outgoing.data; + fh->ser[i].count =3D private->outgoing.count; + i++; + } + } + + fh->header_ser->count =3D i; +} diff --git a/kernel/liveupdate/luo_internal.h b/kernel/liveupdate/luo_inter= nal.h index 734469464664..fa9d2a77c394 100644 --- a/kernel/liveupdate/luo_internal.h +++ b/kernel/liveupdate/luo_internal.h @@ -123,4 +123,11 @@ int luo_file_deserialize(struct luo_file_set *file_set, void luo_file_set_init(struct luo_file_set *file_set); void luo_file_set_destroy(struct luo_file_set *file_set); =20 +int luo_flb_file_preserve(struct liveupdate_file_handler *fh); +void luo_flb_file_unpreserve(struct liveupdate_file_handler *fh); +void luo_flb_file_finish(struct liveupdate_file_handler *fh); +int __init luo_flb_setup_outgoing(void *fdt); +int __init luo_flb_setup_incoming(void *fdt); +void luo_flb_serialize(void); + #endif /* _LINUX_LUO_INTERNAL_H */ --=20 2.52.0.rc2.455.g230fcf2819-goog From nobody Tue Dec 2 00:47:59 2025 Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) (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 C2B9B2EA154 for ; Sat, 22 Nov 2025 22:24:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850286; cv=none; b=WzjEOmthyjPa94+1knkOty26UWDeMPEWJfm0JlIInBed7gf4oXHW0nCVL/DhpaoeaNGxe37+nF6BXb6iBUuiEMp4L0nMaKHVXHPYPzjstbglOIngPgRoq4sntDdguzXCLc+ZYQi0Lj3xkA3WmLNf6lHHhSLDOwLccAYRPHqg5rU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763850286; c=relaxed/simple; bh=3l+12XxLZ6L/2oCthGjMqANEYbAQ8n/WfuJEOJeovT0=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=spRNSlfczOkpowfS4G43TAmAbJu3NCwvF/kussNN5zjhrxARVx4QJwXDuU9RHiKGU18Q4AaHrmAytSaVYD4a6kpUdhziQokc8+ae1MTtH+0JQR6ANqEPfdk454Z8WLapX6XhPE9lEn1owOUYflKFarBxvGEtP2Jhb7WqfshuPbQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=Pmr7olG8; arc=none smtp.client-ip=209.85.128.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="Pmr7olG8" Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-789524e6719so41965187b3.1 for ; Sat, 22 Nov 2025 14:24:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763850280; x=1764455080; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=SLgQGl+Hg4wswsvyxx4Nha11zmSlja8dA+7nO+BHML4=; b=Pmr7olG8jFWd0ALOJwnoFdRgugYn7YrovOPiJpahKUsOBBBuuSH+tRy3gEAacufO2D nZKUwSzOHUaioVAYHhKHgjkewJ6kB5DMyx0IhS4XPjuluJ9Q9R0qNn/jXS8VkNSgL9yo oQ83whIfymvBqzpQsgJKRbL4MVH8m5diyEoLqfotwbhNny9XRkylNFVP/wZZlneb26G5 857OlgoPBH7u25IssDlA05V4eWU+WmN31svcq4WVqp4xDcqStWfIoW/Q0l1R9HuvQ/Br pbRRtu5fs48EAH3Yi5eplLANHAOv0hMB7ZUMy17JWI8fDoyzDBTZcD2uv/mskjzKwJe/ Paew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763850280; x=1764455080; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=SLgQGl+Hg4wswsvyxx4Nha11zmSlja8dA+7nO+BHML4=; b=sh/UbAmv7JkZieFu5+eFdRLK+v7NeL9YtygpLbLRrfcoT97zLckm6R5Ej8iB9mkybl dT7eHV8Mzwc5vRlNqJ4X+pUuPVIb/A2wtOQ0FfSGr1I8GyRfSMkUrSFZiwBoj1qNACMA oXN30OS+Wtk2pmcIhm4iXw9JASsQ4XIyh6nEZDG24srvDWzQO9P70VFMwjKlO9S1gt74 9IUepKcYMySvpHiXd5DMeoOYMUqjnh+QAIlgHmm9xI8d9tWjgbjdGysZhzE27meRoTFF N2IF8E2XKPCCMa52+AJulIJN1cBgqPRykIs5Wfdqki9NeurkV2uMD80lxc5XZ/ICXx7C TfoA== X-Forwarded-Encrypted: i=1; AJvYcCVGKvhAtjzAEGGLO3vtmSgALhI7ijqywtST0nsts+uDbWQWylje0xMNwgvtaG4zO5URYipwGciQooAZu0I=@vger.kernel.org X-Gm-Message-State: AOJu0YzDV/1WJldysGxPgDpxFYYiYqmRiPjFoPkLNOuOoOTfsYnhsfcx na17eJ4mUYwU91FpOwA6FUDlR4jTDMMZ2IXVX8yz0x4AQScLrV+6EnVn00TBMZXYdTM= X-Gm-Gg: ASbGncu0XRnf0QGf6l7SoPH4cKfRU7fKvl5bpSR1XC/UAAI92p05D6AnZQfzmn18Tgi E+RhOJDdj7rRakewA6HeTZoLZaCtFC9LRt+xnsicAopOTqTSEu00pEQQsQaC/8Yh360mqyKNFhn 9AhilBqArJ8ZPpbjvO64ayo3lAxHfbpvBaWftRk3hLer2qFmlSNLRd8zhem18IwPdiI5kGzFvTM 1uMglU70YjZFs5MzjkJWtKKM3ScVPgmkr5VQi+npaenZ+pe5MQ0oRD23yI5UWcexD/ONlro3uEc uZfUUkZs3zxe8Vs2T3zC2iGJiOwDnX7iOTyT9rKhMdZ8/YEXrMUe1pSXP4dGecOQQiLOqZ2qo+u QzOQNwG+ndpUUuP3/2nxpDDf1sSjvh+NynLGQIMCkJRA6GsxB7MQaMg1/ngU3RGgUlWc5jliTqU Lr8cDtStU0U4azpp6aBb2jg+N9wS5YEnAYRJNzzzVKtn9MmM3KVKO/nffalKhSNPdya17nZ4zy/ wHpJWc= X-Google-Smtp-Source: AGHT+IFmO7ei26IBOwPIpZvds7E8L4kuDdnTnNInwjnST8wDDHfpSKCkMuKQ93csT8hxrCzaE1kAPQ== X-Received: by 2002:a05:690c:6e03:b0:786:4949:b18d with SMTP id 00721157ae682-78a7b9ba83cmr88000807b3.4.1763850280347; Sat, 22 Nov 2025 14:24:40 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 00721157ae682-78a79779a4esm28858937b3.0.2025.11.22.14.24.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Nov 2025 14:24:39 -0800 (PST) From: Pasha Tatashin To: pratyush@kernel.org, jasonmiu@google.com, graf@amazon.com, pasha.tatashin@soleen.com, rppt@kernel.org, dmatlack@google.com, rientjes@google.com, corbet@lwn.net, rdunlap@infradead.org, ilpo.jarvinen@linux.intel.com, kanie@linux.alibaba.com, ojeda@kernel.org, aliceryhl@google.com, masahiroy@kernel.org, akpm@linux-foundation.org, tj@kernel.org, yoann.congal@smile.fr, mmaurer@google.com, roman.gushchin@linux.dev, chenridong@huawei.com, axboe@kernel.dk, mark.rutland@arm.com, jannh@google.com, vincent.guittot@linaro.org, hannes@cmpxchg.org, dan.j.williams@intel.com, david@redhat.com, joel.granados@kernel.org, rostedt@goodmis.org, anna.schumaker@oracle.com, song@kernel.org, linux@weissschuh.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, gregkh@linuxfoundation.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, rafael@kernel.org, dakr@kernel.org, bartosz.golaszewski@linaro.org, cw00.choi@samsung.com, myungjoo.ham@samsung.com, yesanishhere@gmail.com, Jonathan.Cameron@huawei.com, quic_zijuhu@quicinc.com, aleksander.lobakin@intel.com, ira.weiny@intel.com, andriy.shevchenko@linux.intel.com, leon@kernel.org, lukas@wunner.de, bhelgaas@google.com, wagi@kernel.org, djeffery@redhat.com, stuart.w.hayes@gmail.com, ptyadav@amazon.de, lennart@poettering.net, brauner@kernel.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, saeedm@nvidia.com, ajayachandra@nvidia.com, jgg@nvidia.com, parav@nvidia.com, leonro@nvidia.com, witu@nvidia.com, hughd@google.com, skhawaja@google.com, chrisl@kernel.org Subject: [PATCH v7 22/22] tests/liveupdate: Add in-kernel liveupdate test Date: Sat, 22 Nov 2025 17:23:49 -0500 Message-ID: <20251122222351.1059049-23-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251122222351.1059049-1-pasha.tatashin@soleen.com> References: <20251122222351.1059049-1-pasha.tatashin@soleen.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" Introduce an in-kernel test module to validate the core logic of the Live Update Orchestrator's File-Lifecycle-Bound feature. This provides a low-level, controlled environment to test FLB registration and callback invocation without requiring userspace interaction or actual kexec reboots. The test is enabled by the CONFIG_LIVEUPDATE_TEST Kconfig option. Signed-off-by: Pasha Tatashin --- include/linux/kho/abi/luo.h | 5 + kernel/liveupdate/luo_file.c | 8 +- kernel/liveupdate/luo_internal.h | 8 ++ lib/Kconfig.debug | 23 +++++ lib/tests/Makefile | 1 + lib/tests/liveupdate.c | 160 +++++++++++++++++++++++++++++++ 6 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 lib/tests/liveupdate.c diff --git a/include/linux/kho/abi/luo.h b/include/linux/kho/abi/luo.h index ea21ad1535bf..db2fd9611334 100644 --- a/include/linux/kho/abi/luo.h +++ b/include/linux/kho/abi/luo.h @@ -235,4 +235,9 @@ struct luo_flb_ser { u64 count; } __packed; =20 +/* Kernel Live Update Test ABI */ +#ifdef CONFIG_LIVEUPDATE_TEST +#define LIVEUPDATE_TEST_FLB_COMPATIBLE(i) "liveupdate-test-flb-v" #i +#endif + #endif /* _LINUX_KHO_ABI_LUO_H */ diff --git a/kernel/liveupdate/luo_file.c b/kernel/liveupdate/luo_file.c index ecaa93bbcb12..94eb8a13676a 100644 --- a/kernel/liveupdate/luo_file.c +++ b/kernel/liveupdate/luo_file.c @@ -868,6 +868,8 @@ int liveupdate_register_file_handler(struct liveupdate_= file_handler *fh) list_add_tail(&ACCESS_PRIVATE(fh, list), &luo_file_handler_list); luo_session_resume(); =20 + liveupdate_test_register(fh); + return 0; =20 err_resume: @@ -899,8 +901,10 @@ int liveupdate_unregister_file_handler(struct liveupda= te_file_handler *fh) if (!liveupdate_enabled()) return -EOPNOTSUPP; =20 + liveupdate_test_unregister(fh); + if (!luo_session_quiesce()) - return -EBUSY; + goto err_register; =20 if (!list_empty(&ACCESS_PRIVATE(fh, flb_list))) goto err_resume; @@ -913,6 +917,8 @@ int liveupdate_unregister_file_handler(struct liveupdat= e_file_handler *fh) =20 err_resume: luo_session_resume(); +err_register: + liveupdate_test_register(fh); return err; } =20 diff --git a/kernel/liveupdate/luo_internal.h b/kernel/liveupdate/luo_inter= nal.h index fa9d2a77c394..ee1db7ebee4e 100644 --- a/kernel/liveupdate/luo_internal.h +++ b/kernel/liveupdate/luo_internal.h @@ -130,4 +130,12 @@ int __init luo_flb_setup_outgoing(void *fdt); int __init luo_flb_setup_incoming(void *fdt); void luo_flb_serialize(void); =20 +#ifdef CONFIG_LIVEUPDATE_TEST +void liveupdate_test_register(struct liveupdate_file_handler *fh); +void liveupdate_test_unregister(struct liveupdate_file_handler *fh); +#else +static inline void liveupdate_test_register(struct liveupdate_file_handler= *fh) { } +static inline void liveupdate_test_unregister(struct liveupdate_file_handl= er *fh) { } +#endif + #endif /* _LINUX_LUO_INTERNAL_H */ diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index bd3bb7a0c801..59b5ce8f89cf 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2803,6 +2803,29 @@ config LINEAR_RANGES_TEST =20 If unsure, say N. =20 +config LIVEUPDATE_TEST + bool "Live Update Kernel Test" + default n + depends on LIVEUPDATE + help + Enable a built-in kernel test module for the Live Update + Orchestrator. + + This module validates the File-Lifecycle-Bound subsystem by + registering a set of mock FLB objects with any real file handlers + that support live update (such as the memfd handler). + + When live update operations are performed, this test module will + output messages to the kernel log (dmesg), confirming that its + registration and various callback functions (preserve, retrieve, + finish, etc.) are being invoked correctly. + + This is a debugging and regression testing tool for developers + working on the Live Update subsystem. It should not be enabled in + production kernels. + + If unsure, say N + config CMDLINE_KUNIT_TEST tristate "KUnit test for cmdline API" if !KUNIT_ALL_TESTS depends on KUNIT diff --git a/lib/tests/Makefile b/lib/tests/Makefile index 601dba4b7d96..ab0173e6e8b0 100644 --- a/lib/tests/Makefile +++ b/lib/tests/Makefile @@ -28,6 +28,7 @@ obj-$(CONFIG_LIST_KUNIT_TEST) +=3D list-test.o obj-$(CONFIG_KFIFO_KUNIT_TEST) +=3D kfifo_kunit.o obj-$(CONFIG_TEST_LIST_SORT) +=3D test_list_sort.o obj-$(CONFIG_LINEAR_RANGES_TEST) +=3D test_linear_ranges.o +obj-$(CONFIG_LIVEUPDATE_TEST) +=3D liveupdate.o =20 CFLAGS_longest_symbol_kunit.o +=3D $(call cc-disable-warning, missing-prot= otypes) obj-$(CONFIG_LONGEST_SYM_KUNIT_TEST) +=3D longest_symbol_kunit.o diff --git a/lib/tests/liveupdate.c b/lib/tests/liveupdate.c new file mode 100644 index 000000000000..cf30d53668b0 --- /dev/null +++ b/lib/tests/liveupdate.c @@ -0,0 +1,160 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright (c) 2025, Google LLC. + * Pasha Tatashin + */ + +#define pr_fmt(fmt) KBUILD_MODNAME " test: " fmt + +#include +#include +#include +#include +#include +#include "../../kernel/liveupdate/luo_internal.h" + +static const struct liveupdate_flb_ops test_flb_ops; +#define DEFINE_TEST_FLB(i) { \ + .ops =3D &test_flb_ops, \ + .compatible =3D LIVEUPDATE_TEST_FLB_COMPATIBLE(i), \ +} + +/* Number of Test FLBs to register with every file handler */ +#define TEST_NFLBS 3 +static struct liveupdate_flb test_flbs[TEST_NFLBS] =3D { + DEFINE_TEST_FLB(0), + DEFINE_TEST_FLB(1), + DEFINE_TEST_FLB(2), +}; + +#define TEST_FLB_MAGIC_BASE 0xFEEDF00DCAFEBEE0ULL + +static int test_flb_preserve(struct liveupdate_flb_op_args *argp) +{ + ptrdiff_t index =3D argp->flb - test_flbs; + + pr_info("%s: preserve was triggered\n", argp->flb->compatible); + argp->data =3D TEST_FLB_MAGIC_BASE + index; + + return 0; +} + +static void test_flb_unpreserve(struct liveupdate_flb_op_args *argp) +{ + pr_info("%s: unpreserve was triggered\n", argp->flb->compatible); +} + +static int test_flb_retrieve(struct liveupdate_flb_op_args *argp) +{ + ptrdiff_t index =3D argp->flb - test_flbs; + u64 expected_data =3D TEST_FLB_MAGIC_BASE + index; + + if (argp->data =3D=3D expected_data) { + pr_info("%s: found flb data from the previous boot\n", + argp->flb->compatible); + argp->obj =3D (void *)argp->data; + } else { + pr_err("%s: ERROR - incorrect data handle: %llx, expected %llx\n", + argp->flb->compatible, argp->data, expected_data); + return -EINVAL; + } + + return 0; +} + +static void test_flb_finish(struct liveupdate_flb_op_args *argp) +{ + ptrdiff_t index =3D argp->flb - test_flbs; + void *expected_obj =3D (void *)(TEST_FLB_MAGIC_BASE + index); + + if (argp->obj =3D=3D expected_obj) { + pr_info("%s: finish was triggered\n", argp->flb->compatible); + } else { + pr_err("%s: ERROR - finish called with invalid object\n", + argp->flb->compatible); + } +} + +static const struct liveupdate_flb_ops test_flb_ops =3D { + .preserve =3D test_flb_preserve, + .unpreserve =3D test_flb_unpreserve, + .retrieve =3D test_flb_retrieve, + .finish =3D test_flb_finish, + .owner =3D THIS_MODULE, +}; + +static void liveupdate_test_init(void) +{ + static DEFINE_MUTEX(init_lock); + static bool initialized; + int i; + + guard(mutex)(&init_lock); + + if (initialized) + return; + + for (i =3D 0; i < TEST_NFLBS; i++) { + struct liveupdate_flb *flb =3D &test_flbs[i]; + void *obj; + int err; + + err =3D liveupdate_flb_incoming_locked(flb, &obj); + if (!err) { + liveupdate_flb_incoming_unlock(flb, obj); + } else if (err !=3D -ENODATA && err !=3D -ENOENT) { + pr_err("liveupdate_flb_incoming_locked for %s failed: %pe\n", + flb->compatible, ERR_PTR(err)); + } + } + initialized =3D true; +} + +void liveupdate_test_register(struct liveupdate_file_handler *fh) +{ + int err, i; + + liveupdate_test_init(); + + for (i =3D 0; i < TEST_NFLBS; i++) { + struct liveupdate_flb *flb =3D &test_flbs[i]; + + err =3D liveupdate_register_flb(fh, flb); + if (err) { + pr_err("Failed to register %s %pe\n", + flb->compatible, ERR_PTR(err)); + } + } + + err =3D liveupdate_register_flb(fh, &test_flbs[0]); + if (!err || err !=3D -EEXIST) { + pr_err("Failed: %s should be already registered, but got err: %pe\n", + test_flbs[0].compatible, ERR_PTR(err)); + } + + pr_info("Registered %d FLBs with file handler: [%s]\n", + TEST_NFLBS, fh->compatible); +} + +void liveupdate_test_unregister(struct liveupdate_file_handler *fh) +{ + int err, i; + + for (i =3D 0; i < TEST_NFLBS; i++) { + struct liveupdate_flb *flb =3D &test_flbs[i]; + + err =3D liveupdate_unregister_flb(fh, flb); + if (err) { + pr_err("Failed to unregister %s %pe\n", + flb->compatible, ERR_PTR(err)); + } + } + + pr_info("Unregistered %d FLBs from file handler: [%s]\n", + TEST_NFLBS, fh->compatible); +} + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Pasha Tatashin "); +MODULE_DESCRIPTION("In-kernel test for LUO mechanism"); --=20 2.52.0.rc2.455.g230fcf2819-goog