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); 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 if (err) 177 goto exit_free; 178 luo_global.fdt_out = fdt_out; 179 180 return 0; 181 182 exit_free: 183 kho_unpreserve_free(fdt_out); 184 pr_err("failed to prepare LUO FDT: %d\n", err); 185 186 return err; 187 } 188 189 /* 190 * late initcall because it initializes the outgoing tree that is needed only 191 * once userspace starts using /dev/liveupdate. 192 */ 193 static int __init luo_late_startup(void) 194 { 195 int err; 196 197 if (!liveupdate_enabled()) 198 return 0; 199 200 err = luo_fdt_setup(); 201 if (err) 202 luo_global.enabled = false; 203 204 return err; 205 } 206 late_initcall(luo_late_startup); 207 208 /* Public Functions */ 209 210 /** 211 * liveupdate_reboot() - Kernel reboot notifier for live update final 212 * serialization. 213 * 214 * This function is invoked directly from the reboot() syscall pathway 215 * if kexec is in progress. 216 * 217 * If any callback fails, this function aborts KHO, undoes the freeze() 218 * callbacks, and returns an error. 219 */ 220 int liveupdate_reboot(void) 221 { 222 int err; 223 224 if (!liveupdate_enabled()) 225 return 0; 226 227 err = luo_session_serialize(); 228 if (err) 229 return err; 230 231 luo_flb_serialize(); 232 233 return 0; 234 } 235 236 /** 237 * liveupdate_enabled - Check if the live update feature is enabled. 238 * 239 * This function returns the state of the live update feature flag, which 240 * can be controlled via the ``liveupdate`` kernel command-line parameter. 241 * 242 * @return true if live update is enabled, false otherwise. 243 */ 244 bool liveupdate_enabled(void) 245 { 246 return luo_global.enabled; 247 } 248 249 /** 250 * DOC: LUO ioctl Interface 251 * 252 * The IOCTL user-space control interface for the LUO subsystem. 253 * It registers a character device, typically found at ``/dev/liveupdate``, 254 * which allows a userspace agent to manage the LUO state machine and its 255 * associated resources, such as preservable file descriptors. 256 * 257 * To ensure that the state machine is controlled by a single entity, access 258 * to this device is exclusive: only one process is permitted to have 259 * ``/dev/liveupdate`` open at any given time. Subsequent open attempts will 260 * fail with -EBUSY until the first process closes its file descriptor. 261 * This singleton model simplifies state management by preventing conflicting 262 * commands from multiple userspace agents. 263 */ 264 265 struct luo_device_state { 266 struct miscdevice miscdev; 267 atomic_t in_use; 268 }; 269 270 static int luo_ioctl_create_session(struct luo_ucmd *ucmd) 271 { 272 struct liveupdate_ioctl_create_session *argp = ucmd->cmd; 273 struct file *file; 274 int err; 275 276 argp->fd = get_unused_fd_flags(O_CLOEXEC); 277 if (argp->fd < 0) 278 return argp->fd; 279 280 err = luo_session_create(argp->name, &file); 281 if (err) 282 goto err_put_fd; 283 284 err = luo_ucmd_respond(ucmd, sizeof(*argp)); 285 if (err) 286 goto err_put_file; 287 288 fd_install(argp->fd, file); 289 290 return 0; 291 292 err_put_file: 293 fput(file); 294 err_put_fd: 295 put_unused_fd(argp->fd); 296 297 return err; 298 } 299 300 static int luo_ioctl_retrieve_session(struct luo_ucmd *ucmd) 301 { 302 struct liveupdate_ioctl_retrieve_session *argp = ucmd->cmd; 303 struct file *file; 304 int err; 305 306 argp->fd = get_unused_fd_flags(O_CLOEXEC); 307 if (argp->fd < 0) 308 return argp->fd; 309 310 err = luo_session_retrieve(argp->name, &file); 311 if (err < 0) 312 goto err_put_fd; 313 314 err = luo_ucmd_respond(ucmd, sizeof(*argp)); 315 if (err) 316 goto err_put_file; 317 318 fd_install(argp->fd, file); 319 320 return 0; 321 322 err_put_file: 323 fput(file); 324 err_put_fd: 325 put_unused_fd(argp->fd); 326 327 return err; 328 } 329 330 static int luo_open(struct inode *inodep, struct file *filep) 331 { 332 struct luo_device_state *ldev = container_of(filep->private_data, 333 struct luo_device_state, 334 miscdev); 335 336 if (atomic_cmpxchg(&ldev->in_use, 0, 1)) 337 return -EBUSY; 338 339 /* Always return -EIO to user if deserialization fail */ 340 if (luo_session_deserialize()) { 341 atomic_set(&ldev->in_use, 0); 342 return -EIO; 343 } 344 345 return 0; 346 } 347 348 static int luo_release(struct inode *inodep, struct file *filep) 349 { 350 struct luo_device_state *ldev = container_of(filep->private_data, 351 struct luo_device_state, 352 miscdev); 353 atomic_set(&ldev->in_use, 0); 354 355 return 0; 356 } 357 358 union ucmd_buffer { 359 struct liveupdate_ioctl_create_session create; 360 struct liveupdate_ioctl_retrieve_session retrieve; 361 }; 362 363 struct luo_ioctl_op { 364 unsigned int size; 365 unsigned int min_size; 366 unsigned int ioctl_num; 367 int (*execute)(struct luo_ucmd *ucmd); 368 }; 369 370 #define IOCTL_OP(_ioctl, _fn, _struct, _last) \ 371 [_IOC_NR(_ioctl) - LIVEUPDATE_CMD_BASE] = { \ 372 .size = sizeof(_struct) + \ 373 BUILD_BUG_ON_ZERO(sizeof(union ucmd_buffer) < \ 374 sizeof(_struct)), \ 375 .min_size = offsetofend(_struct, _last), \ 376 .ioctl_num = _ioctl, \ 377 .execute = _fn, \ 378 } 379 380 static const struct luo_ioctl_op luo_ioctl_ops[] = { 381 IOCTL_OP(LIVEUPDATE_IOCTL_CREATE_SESSION, luo_ioctl_create_session, 382 struct liveupdate_ioctl_create_session, name), 383 IOCTL_OP(LIVEUPDATE_IOCTL_RETRIEVE_SESSION, luo_ioctl_retrieve_session, 384 struct liveupdate_ioctl_retrieve_session, name), 385 }; 386 387 static long luo_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) 388 { 389 const struct luo_ioctl_op *op; 390 struct luo_ucmd ucmd = {}; 391 union ucmd_buffer buf; 392 unsigned int nr; 393 int err; 394 395 nr = _IOC_NR(cmd); 396 if (nr - LIVEUPDATE_CMD_BASE >= ARRAY_SIZE(luo_ioctl_ops)) 397 return -EINVAL; 398 399 ucmd.ubuffer = (void __user *)arg; 400 err = get_user(ucmd.user_size, (u32 __user *)ucmd.ubuffer); 401 if (err) 402 return err; 403 404 op = &luo_ioctl_ops[nr - LIVEUPDATE_CMD_BASE]; 405 if (op->ioctl_num != cmd) 406 return -ENOIOCTLCMD; 407 if (ucmd.user_size < op->min_size) 408 return -EINVAL; 409 410 ucmd.cmd = &buf; 411 err = copy_struct_from_user(ucmd.cmd, op->size, ucmd.ubuffer, 412 ucmd.user_size); 413 if (err) 414 return err; 415 416 return op->execute(&ucmd); 417 } 418 419 static const struct file_operations luo_fops = { 420 .owner = THIS_MODULE, 421 .open = luo_open, 422 .release = luo_release, 423 .unlocked_ioctl = luo_ioctl, 424 }; 425 426 static struct luo_device_state luo_dev = { 427 .miscdev = { 428 .minor = MISC_DYNAMIC_MINOR, 429 .name = "liveupdate", 430 .fops = &luo_fops, 431 }, 432 .in_use = ATOMIC_INIT(0), 433 }; 434 435 static int __init liveupdate_ioctl_init(void) 436 { 437 if (!liveupdate_enabled()) 438 return 0; 439 440 return misc_register(&luo_dev.miscdev); 441 } 442 late_initcall(liveupdate_ioctl_init); 443