1 // SPDX-License-Identifier: GPL-2.0 2 3 /* 4 * Copyright (c) 2025, Google LLC. 5 * Pasha Tatashin <pasha.tatashin@soleen.com> 6 */ 7 8 /** 9 * DOC: Live Update Orchestrator (LUO) 10 * 11 * Live Update is a specialized, kexec-based reboot process that allows a 12 * running kernel to be updated from one version to another while preserving 13 * the state of selected resources and keeping designated hardware devices 14 * operational. For these devices, DMA activity may continue throughout the 15 * kernel transition. 16 * 17 * While the primary use case driving this work is supporting live updates of 18 * the Linux kernel when it is used as a hypervisor in cloud environments, the 19 * LUO framework itself is designed to be workload-agnostic. Live Update 20 * facilitates a full kernel version upgrade for any type of system. 21 * 22 * For example, a non-hypervisor system running an in-memory cache like 23 * memcached with many gigabytes of data can use LUO. The userspace service 24 * can place its cache into a memfd, have its state preserved by LUO, and 25 * restore it immediately after the kernel kexec. 26 * 27 * Whether the system is running virtual machines, containers, a 28 * high-performance database, or networking services, LUO's primary goal is to 29 * enable a full kernel update by preserving critical userspace state and 30 * keeping essential devices operational. 31 * 32 * The core of LUO is a mechanism that tracks the progress of a live update, 33 * along with a callback API that allows other kernel subsystems to participate 34 * in the process. Example subsystems that can hook into LUO include: kvm, 35 * iommu, interrupts, vfio, participating filesystems, and memory management. 36 * 37 * LUO uses Kexec Handover to transfer memory state from the current kernel to 38 * the next kernel. For more details see Documentation/core-api/kho/index.rst. 39 */ 40 41 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 42 43 #include <linux/atomic.h> 44 #include <linux/errno.h> 45 #include <linux/file.h> 46 #include <linux/fs.h> 47 #include <linux/init.h> 48 #include <linux/io.h> 49 #include <linux/kernel.h> 50 #include <linux/kexec_handover.h> 51 #include <linux/kho/abi/luo.h> 52 #include <linux/kobject.h> 53 #include <linux/libfdt.h> 54 #include <linux/liveupdate.h> 55 #include <linux/miscdevice.h> 56 #include <linux/mm.h> 57 #include <linux/sizes.h> 58 #include <linux/string.h> 59 #include <linux/unaligned.h> 60 61 #include "kexec_handover_internal.h" 62 #include "luo_internal.h" 63 64 static struct { 65 bool enabled; 66 void *fdt_out; 67 void *fdt_in; 68 u64 liveupdate_num; 69 } luo_global; 70 71 static int __init early_liveupdate_param(char *buf) 72 { 73 return kstrtobool(buf, &luo_global.enabled); 74 } 75 early_param("liveupdate", early_liveupdate_param); 76 77 static int __init luo_early_startup(void) 78 { 79 phys_addr_t fdt_phys; 80 int err, ln_size; 81 const void *ptr; 82 83 if (!kho_is_enabled()) { 84 if (liveupdate_enabled()) 85 pr_warn("Disabling liveupdate because KHO is disabled\n"); 86 luo_global.enabled = false; 87 return 0; 88 } 89 90 /* Retrieve LUO subtree, and verify its format. */ 91 err = kho_retrieve_subtree(LUO_FDT_KHO_ENTRY_NAME, &fdt_phys, NULL); 92 if (err) { 93 if (err != -ENOENT) { 94 pr_err("failed to retrieve FDT '%s' from KHO: %pe\n", 95 LUO_FDT_KHO_ENTRY_NAME, ERR_PTR(err)); 96 return err; 97 } 98 99 return 0; 100 } 101 102 luo_global.fdt_in = phys_to_virt(fdt_phys); 103 err = fdt_node_check_compatible(luo_global.fdt_in, 0, 104 LUO_FDT_COMPATIBLE); 105 if (err) { 106 pr_err("FDT '%s' is incompatible with '%s' [%d]\n", 107 LUO_FDT_KHO_ENTRY_NAME, LUO_FDT_COMPATIBLE, err); 108 109 return -EINVAL; 110 } 111 112 ln_size = 0; 113 ptr = fdt_getprop(luo_global.fdt_in, 0, LUO_FDT_LIVEUPDATE_NUM, 114 &ln_size); 115 if (!ptr || ln_size != sizeof(luo_global.liveupdate_num)) { 116 pr_err("Unable to get live update number '%s' [%d]\n", 117 LUO_FDT_LIVEUPDATE_NUM, ln_size); 118 119 return -EINVAL; 120 } 121 122 luo_global.liveupdate_num = get_unaligned((u64 *)ptr); 123 pr_info("Retrieved live update data, liveupdate number: %lld\n", 124 luo_global.liveupdate_num); 125 126 err = luo_session_setup_incoming(luo_global.fdt_in); 127 if (err) 128 return err; 129 130 err = luo_flb_setup_incoming(luo_global.fdt_in); 131 132 return err; 133 } 134 135 static int __init liveupdate_early_init(void) 136 { 137 int err; 138 139 err = luo_early_startup(); 140 if (err) { 141 luo_global.enabled = false; 142 luo_restore_fail("The incoming tree failed to initialize properly [%pe], disabling live update\n", 143 ERR_PTR(err)); 144 } 145 146 return err; 147 } 148 early_initcall(liveupdate_early_init); 149 150 /* Called during boot to create outgoing LUO fdt tree */ 151 static int __init luo_fdt_setup(void) 152 { 153 const u64 ln = luo_global.liveupdate_num + 1; 154 void *fdt_out; 155 int err; 156 157 fdt_out = kho_alloc_preserve(LUO_FDT_SIZE); 158 if (IS_ERR(fdt_out)) { 159 pr_err("failed to allocate/preserve FDT memory\n"); 160 return PTR_ERR(fdt_out); 161 } 162 163 err = fdt_create(fdt_out, LUO_FDT_SIZE); 164 err |= fdt_finish_reservemap(fdt_out); 165 err |= fdt_begin_node(fdt_out, ""); 166 err |= fdt_property_string(fdt_out, "compatible", LUO_FDT_COMPATIBLE); 167 err |= fdt_property(fdt_out, LUO_FDT_LIVEUPDATE_NUM, &ln, sizeof(ln)); 168 err |= luo_session_setup_outgoing(fdt_out); 169 err |= luo_flb_setup_outgoing(fdt_out); 170 err |= fdt_end_node(fdt_out); 171 err |= fdt_finish(fdt_out); 172 if (err) 173 goto exit_free; 174 175 err = kho_add_subtree(LUO_FDT_KHO_ENTRY_NAME, fdt_out, 176 fdt_totalsize(fdt_out)); 177 if (err) 178 goto exit_free; 179 luo_global.fdt_out = fdt_out; 180 181 return 0; 182 183 exit_free: 184 kho_unpreserve_free(fdt_out); 185 pr_err("failed to prepare LUO FDT: %d\n", err); 186 187 return err; 188 } 189 190 /* 191 * late initcall because it initializes the outgoing tree that is needed only 192 * once userspace starts using /dev/liveupdate. 193 */ 194 static int __init luo_late_startup(void) 195 { 196 int err; 197 198 if (!liveupdate_enabled()) 199 return 0; 200 201 err = luo_fdt_setup(); 202 if (err) 203 luo_global.enabled = false; 204 205 return err; 206 } 207 late_initcall(luo_late_startup); 208 209 /* Public Functions */ 210 211 /** 212 * liveupdate_reboot() - Kernel reboot notifier for live update final 213 * serialization. 214 * 215 * This function is invoked directly from the reboot() syscall pathway 216 * if kexec is in progress. 217 * 218 * If any callback fails, this function aborts KHO, undoes the freeze() 219 * callbacks, and returns an error. 220 */ 221 int liveupdate_reboot(void) 222 { 223 int err; 224 225 if (!liveupdate_enabled()) 226 return 0; 227 228 err = luo_session_serialize(); 229 if (err) 230 return err; 231 232 luo_flb_serialize(); 233 234 return 0; 235 } 236 237 /** 238 * liveupdate_enabled - Check if the live update feature is enabled. 239 * 240 * This function returns the state of the live update feature flag, which 241 * can be controlled via the ``liveupdate`` kernel command-line parameter. 242 * 243 * @return true if live update is enabled, false otherwise. 244 */ 245 bool liveupdate_enabled(void) 246 { 247 return luo_global.enabled; 248 } 249 250 /** 251 * DOC: LUO ioctl Interface 252 * 253 * The IOCTL user-space control interface for the LUO subsystem. 254 * It registers a character device, typically found at ``/dev/liveupdate``, 255 * which allows a userspace agent to manage the LUO state machine and its 256 * associated resources, such as preservable file descriptors. 257 * 258 * To ensure that the state machine is controlled by a single entity, access 259 * to this device is exclusive: only one process is permitted to have 260 * ``/dev/liveupdate`` open at any given time. Subsequent open attempts will 261 * fail with -EBUSY until the first process closes its file descriptor. 262 * This singleton model simplifies state management by preventing conflicting 263 * commands from multiple userspace agents. 264 */ 265 266 struct luo_device_state { 267 struct miscdevice miscdev; 268 atomic_t in_use; 269 }; 270 271 static int luo_ioctl_create_session(struct luo_ucmd *ucmd) 272 { 273 struct liveupdate_ioctl_create_session *argp = ucmd->cmd; 274 struct file *file; 275 int err; 276 277 argp->fd = get_unused_fd_flags(O_CLOEXEC); 278 if (argp->fd < 0) 279 return argp->fd; 280 281 err = luo_session_create(argp->name, &file); 282 if (err) 283 goto err_put_fd; 284 285 err = luo_ucmd_respond(ucmd, sizeof(*argp)); 286 if (err) 287 goto err_put_file; 288 289 fd_install(argp->fd, file); 290 291 return 0; 292 293 err_put_file: 294 fput(file); 295 err_put_fd: 296 put_unused_fd(argp->fd); 297 298 return err; 299 } 300 301 static int luo_ioctl_retrieve_session(struct luo_ucmd *ucmd) 302 { 303 struct liveupdate_ioctl_retrieve_session *argp = ucmd->cmd; 304 struct file *file; 305 int err; 306 307 argp->fd = get_unused_fd_flags(O_CLOEXEC); 308 if (argp->fd < 0) 309 return argp->fd; 310 311 err = luo_session_retrieve(argp->name, &file); 312 if (err < 0) 313 goto err_put_fd; 314 315 err = luo_ucmd_respond(ucmd, sizeof(*argp)); 316 if (err) 317 goto err_put_file; 318 319 fd_install(argp->fd, file); 320 321 return 0; 322 323 err_put_file: 324 fput(file); 325 err_put_fd: 326 put_unused_fd(argp->fd); 327 328 return err; 329 } 330 331 static int luo_open(struct inode *inodep, struct file *filep) 332 { 333 struct luo_device_state *ldev = container_of(filep->private_data, 334 struct luo_device_state, 335 miscdev); 336 337 if (atomic_cmpxchg(&ldev->in_use, 0, 1)) 338 return -EBUSY; 339 340 /* Always return -EIO to user if deserialization fail */ 341 if (luo_session_deserialize()) { 342 atomic_set(&ldev->in_use, 0); 343 return -EIO; 344 } 345 346 return 0; 347 } 348 349 static int luo_release(struct inode *inodep, struct file *filep) 350 { 351 struct luo_device_state *ldev = container_of(filep->private_data, 352 struct luo_device_state, 353 miscdev); 354 atomic_set(&ldev->in_use, 0); 355 356 return 0; 357 } 358 359 union ucmd_buffer { 360 struct liveupdate_ioctl_create_session create; 361 struct liveupdate_ioctl_retrieve_session retrieve; 362 }; 363 364 struct luo_ioctl_op { 365 unsigned int size; 366 unsigned int min_size; 367 unsigned int ioctl_num; 368 int (*execute)(struct luo_ucmd *ucmd); 369 }; 370 371 #define IOCTL_OP(_ioctl, _fn, _struct, _last) \ 372 [_IOC_NR(_ioctl) - LIVEUPDATE_CMD_BASE] = { \ 373 .size = sizeof(_struct) + \ 374 BUILD_BUG_ON_ZERO(sizeof(union ucmd_buffer) < \ 375 sizeof(_struct)), \ 376 .min_size = offsetofend(_struct, _last), \ 377 .ioctl_num = _ioctl, \ 378 .execute = _fn, \ 379 } 380 381 static const struct luo_ioctl_op luo_ioctl_ops[] = { 382 IOCTL_OP(LIVEUPDATE_IOCTL_CREATE_SESSION, luo_ioctl_create_session, 383 struct liveupdate_ioctl_create_session, name), 384 IOCTL_OP(LIVEUPDATE_IOCTL_RETRIEVE_SESSION, luo_ioctl_retrieve_session, 385 struct liveupdate_ioctl_retrieve_session, name), 386 }; 387 388 static long luo_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) 389 { 390 const struct luo_ioctl_op *op; 391 struct luo_ucmd ucmd = {}; 392 union ucmd_buffer buf; 393 unsigned int nr; 394 int err; 395 396 nr = _IOC_NR(cmd); 397 if (nr - LIVEUPDATE_CMD_BASE >= ARRAY_SIZE(luo_ioctl_ops)) 398 return -EINVAL; 399 400 ucmd.ubuffer = (void __user *)arg; 401 err = get_user(ucmd.user_size, (u32 __user *)ucmd.ubuffer); 402 if (err) 403 return err; 404 405 op = &luo_ioctl_ops[nr - LIVEUPDATE_CMD_BASE]; 406 if (op->ioctl_num != cmd) 407 return -ENOIOCTLCMD; 408 if (ucmd.user_size < op->min_size) 409 return -EINVAL; 410 411 ucmd.cmd = &buf; 412 err = copy_struct_from_user(ucmd.cmd, op->size, ucmd.ubuffer, 413 ucmd.user_size); 414 if (err) 415 return err; 416 417 return op->execute(&ucmd); 418 } 419 420 static const struct file_operations luo_fops = { 421 .owner = THIS_MODULE, 422 .open = luo_open, 423 .release = luo_release, 424 .unlocked_ioctl = luo_ioctl, 425 }; 426 427 static struct luo_device_state luo_dev = { 428 .miscdev = { 429 .minor = MISC_DYNAMIC_MINOR, 430 .name = "liveupdate", 431 .fops = &luo_fops, 432 }, 433 .in_use = ATOMIC_INIT(0), 434 }; 435 436 static int __init liveupdate_ioctl_init(void) 437 { 438 if (!liveupdate_enabled()) 439 return 0; 440 441 return misc_register(&luo_dev.miscdev); 442 } 443 late_initcall(liveupdate_ioctl_init); 444