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: LUO File Descriptors 10 * 11 * LUO provides the infrastructure to preserve specific, stateful file 12 * descriptors across a kexec-based live update. The primary goal is to allow 13 * workloads, such as virtual machines using vfio, memfd, or iommufd, to 14 * retain access to their essential resources without interruption. 15 * 16 * The framework is built around a callback-based handler model and a well- 17 * defined lifecycle for each preserved file. 18 * 19 * Handler Registration: 20 * Kernel modules responsible for a specific file type (e.g., memfd, vfio) 21 * register a &struct liveupdate_file_handler. This handler provides a set of 22 * callbacks that LUO invokes at different stages of the update process, most 23 * notably: 24 * 25 * - can_preserve(): A lightweight check to determine if the handler is 26 * compatible with a given 'struct file'. 27 * - preserve(): The heavyweight operation that saves the file's state and 28 * returns an opaque u64 handle. This is typically performed while the 29 * workload is still active to minimize the downtime during the 30 * actual reboot transition. 31 * - unpreserve(): Cleans up any resources allocated by .preserve(), called 32 * if the preservation process is aborted before the reboot (i.e. session is 33 * closed). 34 * - freeze(): A final pre-reboot opportunity to prepare the state for kexec. 35 * We are already in reboot syscall, and therefore userspace cannot mutate 36 * the file anymore. 37 * - unfreeze(): Undoes the actions of .freeze(), called if the live update 38 * is aborted after the freeze phase. 39 * - retrieve(): Reconstructs the file in the new kernel from the preserved 40 * handle. 41 * - finish(): Performs final check and cleanup in the new kernel. After 42 * succesul finish call, LUO gives up ownership to this file. 43 * 44 * File Preservation Lifecycle happy path: 45 * 46 * 1. Preserve (Normal Operation): A userspace agent preserves files one by one 47 * via an ioctl. For each file, luo_preserve_file() finds a compatible 48 * handler, calls its .preserve() operation, and creates an internal &struct 49 * luo_file to track the live state. 50 * 51 * 2. Freeze (Pre-Reboot): Just before the kexec, luo_file_freeze() is called. 52 * It iterates through all preserved files, calls their respective .freeze() 53 * operation, and serializes their final metadata (compatible string, token, 54 * and data handle) into a contiguous memory block for KHO. 55 * 56 * 3. Deserialize: After kexec, luo_file_deserialize() runs when session gets 57 * deserialized (which is when /dev/liveupdate is first opened). It reads the 58 * serialized data from the KHO memory region and reconstructs the in-memory 59 * list of &struct luo_file instances for the new kernel, linking them to 60 * their corresponding handlers. 61 * 62 * 4. Retrieve (New Kernel - Userspace Ready): The userspace agent can now 63 * restore file descriptors by providing a token. luo_retrieve_file() 64 * searches for the matching token, calls the handler's .retrieve() op to 65 * re-create the 'struct file', and returns a new FD. Files can be 66 * retrieved in ANY order. 67 * 68 * 5. Finish (New Kernel - Cleanup): Once a session retrival is complete, 69 * luo_file_finish() is called. It iterates through all files, invokes their 70 * .finish() operations for final cleanup, and releases all associated kernel 71 * resources. 72 * 73 * File Preservation Lifecycle unhappy paths: 74 * 75 * 1. Abort Before Reboot: If the userspace agent aborts the live update 76 * process before calling reboot (e.g., by closing the session file 77 * descriptor), the session's release handler calls 78 * luo_file_unpreserve_files(). This invokes the .unpreserve() callback on 79 * all preserved files, ensuring all allocated resources are cleaned up and 80 * returning the system to a clean state. 81 * 82 * 2. Freeze Failure: During the reboot() syscall, if any handler's .freeze() 83 * op fails, the .unfreeze() op is invoked on all previously *successful* 84 * freezes to roll back their state. The reboot() syscall then returns an 85 * error to userspace, canceling the live update. 86 * 87 * 3. Finish Failure: In the new kernel, if a handler's .finish() op fails, 88 * the luo_file_finish() operation is aborted. LUO retains ownership of 89 * all files within that session, including those that were not yet 90 * processed. The userspace agent can attempt to call the finish operation 91 * again later. If the issue cannot be resolved, these resources will be held 92 * by LUO until the next live update cycle, at which point they will be 93 * discarded. 94 */ 95 96 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 97 98 #include <linux/cleanup.h> 99 #include <linux/compiler.h> 100 #include <linux/err.h> 101 #include <linux/errno.h> 102 #include <linux/file.h> 103 #include <linux/fs.h> 104 #include <linux/io.h> 105 #include <linux/kexec_handover.h> 106 #include <linux/kho/abi/luo.h> 107 #include <linux/list_private.h> 108 #include <linux/liveupdate.h> 109 #include <linux/module.h> 110 #include <linux/sizes.h> 111 #include <linux/slab.h> 112 #include <linux/string.h> 113 #include "luo_internal.h" 114 115 static LIST_HEAD(luo_file_handler_list); 116 117 /* 2 4K pages, give space for 128 files per file_set */ 118 #define LUO_FILE_PGCNT 2ul 119 #define LUO_FILE_MAX \ 120 ((LUO_FILE_PGCNT << PAGE_SHIFT) / sizeof(struct luo_file_ser)) 121 122 /** 123 * struct luo_file - Represents a single preserved file instance. 124 * @fh: Pointer to the &struct liveupdate_file_handler that manages 125 * this type of file. 126 * @file: Pointer to the kernel's &struct file that is being preserved. 127 * This is NULL in the new kernel until the file is successfully 128 * retrieved. 129 * @serialized_data: The opaque u64 handle to the serialized state of the file. 130 * This handle is passed back to the handler's .freeze(), 131 * .retrieve(), and .finish() callbacks, allowing it to track 132 * and update its serialized state across phases. 133 * @private_data: Pointer to the private data for the file used to hold runtime 134 * state that is not preserved. Set by the handler's .preserve() 135 * callback, and must be freed in the handler's .unpreserve() 136 * callback. 137 * @retrieved: A flag indicating whether a user/kernel in the new kernel has 138 * successfully called retrieve() on this file. This prevents 139 * multiple retrieval attempts. 140 * @mutex: A mutex that protects the fields of this specific instance 141 * (e.g., @retrieved, @file), ensuring that operations like 142 * retrieving or finishing a file are atomic. 143 * @list: The list_head linking this instance into its parent 144 * file_set's list of preserved files. 145 * @token: The user-provided unique token used to identify this file. 146 * 147 * This structure is the core in-kernel representation of a single file being 148 * managed through a live update. An instance is created by luo_preserve_file() 149 * to link a 'struct file' to its corresponding handler, a user-provided token, 150 * and the serialized state handle returned by the handler's .preserve() 151 * operation. 152 * 153 * These instances are tracked in a per-file_set list. The @serialized_data 154 * field, which holds a handle to the file's serialized state, may be updated 155 * during the .freeze() callback before being serialized for the next kernel. 156 * After reboot, these structures are recreated by luo_file_deserialize() and 157 * are finally cleaned up by luo_file_finish(). 158 */ 159 struct luo_file { 160 struct liveupdate_file_handler *fh; 161 struct file *file; 162 u64 serialized_data; 163 void *private_data; 164 bool retrieved; 165 struct mutex mutex; 166 struct list_head list; 167 u64 token; 168 }; 169 170 static int luo_alloc_files_mem(struct luo_file_set *file_set) 171 { 172 size_t size; 173 void *mem; 174 175 if (file_set->files) 176 return 0; 177 178 WARN_ON_ONCE(file_set->count); 179 180 size = LUO_FILE_PGCNT << PAGE_SHIFT; 181 mem = kho_alloc_preserve(size); 182 if (IS_ERR(mem)) 183 return PTR_ERR(mem); 184 185 file_set->files = mem; 186 187 return 0; 188 } 189 190 static void luo_free_files_mem(struct luo_file_set *file_set) 191 { 192 /* If file_set has files, no need to free preservation memory */ 193 if (file_set->count) 194 return; 195 196 if (!file_set->files) 197 return; 198 199 kho_unpreserve_free(file_set->files); 200 file_set->files = NULL; 201 } 202 203 static bool luo_token_is_used(struct luo_file_set *file_set, u64 token) 204 { 205 struct luo_file *iter; 206 207 list_for_each_entry(iter, &file_set->files_list, list) { 208 if (iter->token == token) 209 return true; 210 } 211 212 return false; 213 } 214 215 /** 216 * luo_preserve_file - Initiate the preservation of a file descriptor. 217 * @file_set: The file_set to which the preserved file will be added. 218 * @token: A unique, user-provided identifier for the file. 219 * @fd: The file descriptor to be preserved. 220 * 221 * This function orchestrates the first phase of preserving a file. Upon entry, 222 * it takes a reference to the 'struct file' via fget(), effectively making LUO 223 * a co-owner of the file. This reference is held until the file is either 224 * unpreserved or successfully finished in the next kernel, preventing the file 225 * from being prematurely destroyed. 226 * 227 * This function orchestrates the first phase of preserving a file. It performs 228 * the following steps: 229 * 230 * 1. Validates that the @token is not already in use within the file_set. 231 * 2. Ensures the file_set's memory for files serialization is allocated 232 * (allocates if needed). 233 * 3. Iterates through registered handlers, calling can_preserve() to find one 234 * compatible with the given @fd. 235 * 4. Calls the handler's .preserve() operation, which saves the file's state 236 * and returns an opaque private data handle. 237 * 5. Adds the new instance to the file_set's internal list. 238 * 239 * On success, LUO takes a reference to the 'struct file' and considers it 240 * under its management until it is unpreserved or finished. 241 * 242 * In case of any failure, all intermediate allocations (file reference, memory 243 * for the 'luo_file' struct, etc.) are cleaned up before returning an error. 244 * 245 * Context: Can be called from an ioctl handler during normal system operation. 246 * Return: 0 on success. Returns a negative errno on failure: 247 * -EEXIST if the token is already used. 248 * -EBADF if the file descriptor is invalid. 249 * -ENOSPC if the file_set is full. 250 * -ENOENT if no compatible handler is found. 251 * -ENOMEM on memory allocation failure. 252 * Other erros might be returned by .preserve(). 253 */ 254 int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd) 255 { 256 struct liveupdate_file_op_args args = {0}; 257 struct liveupdate_file_handler *fh; 258 struct luo_file *luo_file; 259 struct file *file; 260 int err; 261 262 if (luo_token_is_used(file_set, token)) 263 return -EEXIST; 264 265 if (file_set->count == LUO_FILE_MAX) 266 return -ENOSPC; 267 268 file = fget(fd); 269 if (!file) 270 return -EBADF; 271 272 err = luo_alloc_files_mem(file_set); 273 if (err) 274 goto err_fput; 275 276 err = -ENOENT; 277 list_private_for_each_entry(fh, &luo_file_handler_list, list) { 278 if (fh->ops->can_preserve(fh, file)) { 279 err = 0; 280 break; 281 } 282 } 283 284 /* err is still -ENOENT if no handler was found */ 285 if (err) 286 goto err_free_files_mem; 287 288 luo_file = kzalloc(sizeof(*luo_file), GFP_KERNEL); 289 if (!luo_file) { 290 err = -ENOMEM; 291 goto err_free_files_mem; 292 } 293 294 luo_file->file = file; 295 luo_file->fh = fh; 296 luo_file->token = token; 297 luo_file->retrieved = false; 298 mutex_init(&luo_file->mutex); 299 300 args.handler = fh; 301 args.file = file; 302 err = fh->ops->preserve(&args); 303 if (err) 304 goto err_kfree; 305 306 luo_file->serialized_data = args.serialized_data; 307 luo_file->private_data = args.private_data; 308 list_add_tail(&luo_file->list, &file_set->files_list); 309 file_set->count++; 310 311 return 0; 312 313 err_kfree: 314 kfree(luo_file); 315 err_free_files_mem: 316 luo_free_files_mem(file_set); 317 err_fput: 318 fput(file); 319 320 return err; 321 } 322 323 /** 324 * luo_file_unpreserve_files - Unpreserves all files from a file_set. 325 * @file_set: The files to be cleaned up. 326 * 327 * This function serves as the primary cleanup path for a file_set. It is 328 * invoked when the userspace agent closes the file_set's file descriptor. 329 * 330 * For each file, it performs the following cleanup actions: 331 * 1. Calls the handler's .unpreserve() callback to allow the handler to 332 * release any resources it allocated. 333 * 2. Removes the file from the file_set's internal tracking list. 334 * 3. Releases the reference to the 'struct file' that was taken by 335 * luo_preserve_file() via fput(), returning ownership. 336 * 4. Frees the memory associated with the internal 'struct luo_file'. 337 * 338 * After all individual files are unpreserved, it frees the contiguous memory 339 * block that was allocated to hold their serialization data. 340 */ 341 void luo_file_unpreserve_files(struct luo_file_set *file_set) 342 { 343 struct luo_file *luo_file; 344 345 while (!list_empty(&file_set->files_list)) { 346 struct liveupdate_file_op_args args = {0}; 347 348 luo_file = list_last_entry(&file_set->files_list, 349 struct luo_file, list); 350 351 args.handler = luo_file->fh; 352 args.file = luo_file->file; 353 args.serialized_data = luo_file->serialized_data; 354 args.private_data = luo_file->private_data; 355 luo_file->fh->ops->unpreserve(&args); 356 357 list_del(&luo_file->list); 358 file_set->count--; 359 360 fput(luo_file->file); 361 mutex_destroy(&luo_file->mutex); 362 kfree(luo_file); 363 } 364 365 luo_free_files_mem(file_set); 366 } 367 368 static int luo_file_freeze_one(struct luo_file_set *file_set, 369 struct luo_file *luo_file) 370 { 371 int err = 0; 372 373 guard(mutex)(&luo_file->mutex); 374 375 if (luo_file->fh->ops->freeze) { 376 struct liveupdate_file_op_args args = {0}; 377 378 args.handler = luo_file->fh; 379 args.file = luo_file->file; 380 args.serialized_data = luo_file->serialized_data; 381 args.private_data = luo_file->private_data; 382 383 err = luo_file->fh->ops->freeze(&args); 384 if (!err) 385 luo_file->serialized_data = args.serialized_data; 386 } 387 388 return err; 389 } 390 391 static void luo_file_unfreeze_one(struct luo_file_set *file_set, 392 struct luo_file *luo_file) 393 { 394 guard(mutex)(&luo_file->mutex); 395 396 if (luo_file->fh->ops->unfreeze) { 397 struct liveupdate_file_op_args args = {0}; 398 399 args.handler = luo_file->fh; 400 args.file = luo_file->file; 401 args.serialized_data = luo_file->serialized_data; 402 args.private_data = luo_file->private_data; 403 404 luo_file->fh->ops->unfreeze(&args); 405 } 406 407 luo_file->serialized_data = 0; 408 } 409 410 static void __luo_file_unfreeze(struct luo_file_set *file_set, 411 struct luo_file *failed_entry) 412 { 413 struct list_head *files_list = &file_set->files_list; 414 struct luo_file *luo_file; 415 416 list_for_each_entry(luo_file, files_list, list) { 417 if (luo_file == failed_entry) 418 break; 419 420 luo_file_unfreeze_one(file_set, luo_file); 421 } 422 423 memset(file_set->files, 0, LUO_FILE_PGCNT << PAGE_SHIFT); 424 } 425 426 /** 427 * luo_file_freeze - Freezes all preserved files and serializes their metadata. 428 * @file_set: The file_set whose files are to be frozen. 429 * @file_set_ser: Where to put the serialized file_set. 430 * 431 * This function is called from the reboot() syscall path, just before the 432 * kernel transitions to the new image via kexec. Its purpose is to perform the 433 * final preparation and serialization of all preserved files in the file_set. 434 * 435 * It iterates through each preserved file in FIFO order (the order of 436 * preservation) and performs two main actions: 437 * 438 * 1. Freezes the File: It calls the handler's .freeze() callback for each 439 * file. This gives the handler a final opportunity to quiesce the device or 440 * prepare its state for the upcoming reboot. The handler may update its 441 * private data handle during this step. 442 * 443 * 2. Serializes Metadata: After a successful freeze, it copies the final file 444 * metadata—the handler's compatible string, the user token, and the final 445 * private data handle—into the pre-allocated contiguous memory buffer 446 * (file_set->files) that will be handed over to the next kernel via KHO. 447 * 448 * Error Handling (Rollback): 449 * This function is atomic. If any handler's .freeze() operation fails, the 450 * entire live update is aborted. The __luo_file_unfreeze() helper is 451 * immediately called to invoke the .unfreeze() op on all files that were 452 * successfully frozen before the point of failure, rolling them back to a 453 * running state. The function then returns an error, causing the reboot() 454 * syscall to fail. 455 * 456 * Context: Called only from the liveupdate_reboot() path. 457 * Return: 0 on success, or a negative errno on failure. 458 */ 459 int luo_file_freeze(struct luo_file_set *file_set, 460 struct luo_file_set_ser *file_set_ser) 461 { 462 struct luo_file_ser *file_ser = file_set->files; 463 struct luo_file *luo_file; 464 int err; 465 int i; 466 467 if (!file_set->count) 468 return 0; 469 470 if (WARN_ON(!file_ser)) 471 return -EINVAL; 472 473 i = 0; 474 list_for_each_entry(luo_file, &file_set->files_list, list) { 475 err = luo_file_freeze_one(file_set, luo_file); 476 if (err < 0) { 477 pr_warn("Freeze failed for token[%#0llx] handler[%s] err[%pe]\n", 478 luo_file->token, luo_file->fh->compatible, 479 ERR_PTR(err)); 480 goto err_unfreeze; 481 } 482 483 strscpy(file_ser[i].compatible, luo_file->fh->compatible, 484 sizeof(file_ser[i].compatible)); 485 file_ser[i].data = luo_file->serialized_data; 486 file_ser[i].token = luo_file->token; 487 i++; 488 } 489 490 file_set_ser->count = file_set->count; 491 if (file_set->files) 492 file_set_ser->files = virt_to_phys(file_set->files); 493 494 return 0; 495 496 err_unfreeze: 497 __luo_file_unfreeze(file_set, luo_file); 498 499 return err; 500 } 501 502 /** 503 * luo_file_unfreeze - Unfreezes all files in a file_set and clear serialization 504 * @file_set: The file_set whose files are to be unfrozen. 505 * @file_set_ser: Serialized file_set. 506 * 507 * This function rolls back the state of all files in a file_set after the 508 * freeze phase has begun but must be aborted. It is the counterpart to 509 * luo_file_freeze(). 510 * 511 * It invokes the __luo_file_unfreeze() helper with a NULL argument, which 512 * signals the helper to iterate through all files in the file_set and call 513 * their respective .unfreeze() handler callbacks. 514 * 515 * Context: This is called when the live update is aborted during 516 * the reboot() syscall, after luo_file_freeze() has been called. 517 */ 518 void luo_file_unfreeze(struct luo_file_set *file_set, 519 struct luo_file_set_ser *file_set_ser) 520 { 521 if (!file_set->count) 522 return; 523 524 __luo_file_unfreeze(file_set, NULL); 525 memset(file_set_ser, 0, sizeof(*file_set_ser)); 526 } 527 528 /** 529 * luo_retrieve_file - Restores a preserved file from a file_set by its token. 530 * @file_set: The file_set from which to retrieve the file. 531 * @token: The unique token identifying the file to be restored. 532 * @filep: Output parameter; on success, this is populated with a pointer 533 * to the newly retrieved 'struct file'. 534 * 535 * This function is the primary mechanism for recreating a file in the new 536 * kernel after a live update. It searches the file_set's list of deserialized 537 * files for an entry matching the provided @token. 538 * 539 * The operation is idempotent: if a file has already been successfully 540 * retrieved, this function will simply return a pointer to the existing 541 * 'struct file' and report success without re-executing the retrieve 542 * operation. This is handled by checking the 'retrieved' flag under a lock. 543 * 544 * File retrieval can happen in any order; it is not bound by the order of 545 * preservation. 546 * 547 * Context: Can be called from an ioctl or other in-kernel code in the new 548 * kernel. 549 * Return: 0 on success. Returns a negative errno on failure: 550 * -ENOENT if no file with the matching token is found. 551 * Any error code returned by the handler's .retrieve() op. 552 */ 553 int luo_retrieve_file(struct luo_file_set *file_set, u64 token, 554 struct file **filep) 555 { 556 struct liveupdate_file_op_args args = {0}; 557 struct luo_file *luo_file; 558 bool found = false; 559 int err; 560 561 if (list_empty(&file_set->files_list)) 562 return -ENOENT; 563 564 list_for_each_entry(luo_file, &file_set->files_list, list) { 565 if (luo_file->token == token) { 566 found = true; 567 break; 568 } 569 } 570 571 if (!found) 572 return -ENOENT; 573 574 guard(mutex)(&luo_file->mutex); 575 if (luo_file->retrieved) { 576 /* 577 * Someone is asking for this file again, so get a reference 578 * for them. 579 */ 580 get_file(luo_file->file); 581 *filep = luo_file->file; 582 return 0; 583 } 584 585 args.handler = luo_file->fh; 586 args.serialized_data = luo_file->serialized_data; 587 err = luo_file->fh->ops->retrieve(&args); 588 if (!err) { 589 luo_file->file = args.file; 590 591 /* Get reference so we can keep this file in LUO until finish */ 592 get_file(luo_file->file); 593 *filep = luo_file->file; 594 luo_file->retrieved = true; 595 } 596 597 return err; 598 } 599 600 static int luo_file_can_finish_one(struct luo_file_set *file_set, 601 struct luo_file *luo_file) 602 { 603 bool can_finish = true; 604 605 guard(mutex)(&luo_file->mutex); 606 607 if (luo_file->fh->ops->can_finish) { 608 struct liveupdate_file_op_args args = {0}; 609 610 args.handler = luo_file->fh; 611 args.file = luo_file->file; 612 args.serialized_data = luo_file->serialized_data; 613 args.retrieved = luo_file->retrieved; 614 can_finish = luo_file->fh->ops->can_finish(&args); 615 } 616 617 return can_finish ? 0 : -EBUSY; 618 } 619 620 static void luo_file_finish_one(struct luo_file_set *file_set, 621 struct luo_file *luo_file) 622 { 623 struct liveupdate_file_op_args args = {0}; 624 625 guard(mutex)(&luo_file->mutex); 626 627 args.handler = luo_file->fh; 628 args.file = luo_file->file; 629 args.serialized_data = luo_file->serialized_data; 630 args.retrieved = luo_file->retrieved; 631 632 luo_file->fh->ops->finish(&args); 633 } 634 635 /** 636 * luo_file_finish - Completes the lifecycle for all files in a file_set. 637 * @file_set: The file_set to be finalized. 638 * 639 * This function orchestrates the final teardown of a live update file_set in 640 * the new kernel. It should be called after all necessary files have been 641 * retrieved and the userspace agent is ready to release the preserved state. 642 * 643 * The function iterates through all tracked files. For each file, it performs 644 * the following sequence of cleanup actions: 645 * 646 * 1. If file is not yet retrieved, retrieves it, and calls can_finish() on 647 * every file in the file_set. If all can_finish return true, continue to 648 * finish. 649 * 2. Calls the handler's .finish() callback (via luo_file_finish_one) to 650 * allow for final resource cleanup within the handler. 651 * 3. Releases LUO's ownership reference on the 'struct file' via fput(). This 652 * is the counterpart to the get_file() call in luo_retrieve_file(). 653 * 4. Removes the 'struct luo_file' from the file_set's internal list. 654 * 5. Frees the memory for the 'struct luo_file' instance itself. 655 * 656 * After successfully finishing all individual files, it frees the 657 * contiguous memory block that was used to transfer the serialized metadata 658 * from the previous kernel. 659 * 660 * Error Handling (Atomic Failure): 661 * This operation is atomic. If any handler's .can_finish() op fails, the entire 662 * function aborts immediately and returns an error. 663 * 664 * Context: Can be called from an ioctl handler in the new kernel. 665 * Return: 0 on success, or a negative errno on failure. 666 */ 667 int luo_file_finish(struct luo_file_set *file_set) 668 { 669 struct list_head *files_list = &file_set->files_list; 670 struct luo_file *luo_file; 671 int err; 672 673 if (!file_set->count) 674 return 0; 675 676 list_for_each_entry(luo_file, files_list, list) { 677 err = luo_file_can_finish_one(file_set, luo_file); 678 if (err) 679 return err; 680 } 681 682 while (!list_empty(&file_set->files_list)) { 683 luo_file = list_last_entry(&file_set->files_list, 684 struct luo_file, list); 685 686 luo_file_finish_one(file_set, luo_file); 687 688 if (luo_file->file) 689 fput(luo_file->file); 690 list_del(&luo_file->list); 691 file_set->count--; 692 mutex_destroy(&luo_file->mutex); 693 kfree(luo_file); 694 } 695 696 if (file_set->files) { 697 kho_restore_free(file_set->files); 698 file_set->files = NULL; 699 } 700 701 return 0; 702 } 703 704 /** 705 * luo_file_deserialize - Reconstructs the list of preserved files in the new kernel. 706 * @file_set: The incoming file_set to fill with deserialized data. 707 * @file_set_ser: Serialized KHO file_set data from the previous kernel. 708 * 709 * This function is called during the early boot process of the new kernel. It 710 * takes the raw, contiguous memory block of 'struct luo_file_ser' entries, 711 * provided by the previous kernel, and transforms it back into a live, 712 * in-memory linked list of 'struct luo_file' instances. 713 * 714 * For each serialized entry, it performs the following steps: 715 * 1. Reads the 'compatible' string. 716 * 2. Searches the global list of registered file handlers for one that 717 * matches the compatible string. 718 * 3. Allocates a new 'struct luo_file'. 719 * 4. Populates the new structure with the deserialized data (token, private 720 * data handle) and links it to the found handler. The 'file' pointer is 721 * initialized to NULL, as the file has not been retrieved yet. 722 * 5. Adds the new 'struct luo_file' to the file_set's files_list. 723 * 724 * This prepares the file_set for userspace, which can later call 725 * luo_retrieve_file() to restore the actual file descriptors. 726 * 727 * Context: Called from session deserialization. 728 */ 729 int luo_file_deserialize(struct luo_file_set *file_set, 730 struct luo_file_set_ser *file_set_ser) 731 { 732 struct luo_file_ser *file_ser; 733 u64 i; 734 735 if (!file_set_ser->files) { 736 WARN_ON(file_set_ser->count); 737 return 0; 738 } 739 740 file_set->count = file_set_ser->count; 741 file_set->files = phys_to_virt(file_set_ser->files); 742 743 /* 744 * Note on error handling: 745 * 746 * If deserialization fails (e.g., allocation failure or corrupt data), 747 * we intentionally skip cleanup of files that were already restored. 748 * 749 * A partial failure leaves the preserved state inconsistent. 750 * Implementing a safe "undo" to unwind complex dependencies (sessions, 751 * files, hardware state) is error-prone and provides little value, as 752 * the system is effectively in a broken state. 753 * 754 * We treat these resources as leaked. The expected recovery path is for 755 * userspace to detect the failure and trigger a reboot, which will 756 * reliably reset devices and reclaim memory. 757 */ 758 file_ser = file_set->files; 759 for (i = 0; i < file_set->count; i++) { 760 struct liveupdate_file_handler *fh; 761 bool handler_found = false; 762 struct luo_file *luo_file; 763 764 list_private_for_each_entry(fh, &luo_file_handler_list, list) { 765 if (!strcmp(fh->compatible, file_ser[i].compatible)) { 766 handler_found = true; 767 break; 768 } 769 } 770 771 if (!handler_found) { 772 pr_warn("No registered handler for compatible '%s'\n", 773 file_ser[i].compatible); 774 return -ENOENT; 775 } 776 777 luo_file = kzalloc(sizeof(*luo_file), GFP_KERNEL); 778 if (!luo_file) 779 return -ENOMEM; 780 781 luo_file->fh = fh; 782 luo_file->file = NULL; 783 luo_file->serialized_data = file_ser[i].data; 784 luo_file->token = file_ser[i].token; 785 luo_file->retrieved = false; 786 mutex_init(&luo_file->mutex); 787 list_add_tail(&luo_file->list, &file_set->files_list); 788 } 789 790 return 0; 791 } 792 793 void luo_file_set_init(struct luo_file_set *file_set) 794 { 795 INIT_LIST_HEAD(&file_set->files_list); 796 } 797 798 void luo_file_set_destroy(struct luo_file_set *file_set) 799 { 800 WARN_ON(file_set->count); 801 WARN_ON(!list_empty(&file_set->files_list)); 802 } 803 804 /** 805 * liveupdate_register_file_handler - Register a file handler with LUO. 806 * @fh: Pointer to a caller-allocated &struct liveupdate_file_handler. 807 * The caller must initialize this structure, including a unique 808 * 'compatible' string and a valid 'fh' callbacks. This function adds the 809 * handler to the global list of supported file handlers. 810 * 811 * Context: Typically called during module initialization for file types that 812 * support live update preservation. 813 * 814 * Return: 0 on success. Negative errno on failure. 815 */ 816 int liveupdate_register_file_handler(struct liveupdate_file_handler *fh) 817 { 818 struct liveupdate_file_handler *fh_iter; 819 int err; 820 821 if (!liveupdate_enabled()) 822 return -EOPNOTSUPP; 823 824 /* Sanity check that all required callbacks are set */ 825 if (!fh->ops->preserve || !fh->ops->unpreserve || !fh->ops->retrieve || 826 !fh->ops->finish || !fh->ops->can_preserve) { 827 return -EINVAL; 828 } 829 830 /* 831 * Ensure the system is quiescent (no active sessions). 832 * This prevents registering new handlers while sessions are active or 833 * while deserialization is in progress. 834 */ 835 if (!luo_session_quiesce()) 836 return -EBUSY; 837 838 /* Check for duplicate compatible strings */ 839 list_private_for_each_entry(fh_iter, &luo_file_handler_list, list) { 840 if (!strcmp(fh_iter->compatible, fh->compatible)) { 841 pr_err("File handler registration failed: Compatible string '%s' already registered.\n", 842 fh->compatible); 843 err = -EEXIST; 844 goto err_resume; 845 } 846 } 847 848 /* Pin the module implementing the handler */ 849 if (!try_module_get(fh->ops->owner)) { 850 err = -EAGAIN; 851 goto err_resume; 852 } 853 854 INIT_LIST_HEAD(&ACCESS_PRIVATE(fh, list)); 855 list_add_tail(&ACCESS_PRIVATE(fh, list), &luo_file_handler_list); 856 luo_session_resume(); 857 858 return 0; 859 860 err_resume: 861 luo_session_resume(); 862 return err; 863 } 864 865 /** 866 * liveupdate_unregister_file_handler - Unregister a liveupdate file handler 867 * @fh: The file handler to unregister 868 * 869 * Unregisters the file handler from the liveupdate core. This function 870 * reverses the operations of liveupdate_register_file_handler(). 871 * 872 * It ensures safe removal by checking that: 873 * No live update session is currently in progress. 874 * 875 * If the unregistration fails, the internal test state is reverted. 876 * 877 * Return: 0 Success. -EOPNOTSUPP when live update is not enabled. -EBUSY A live 878 * update is in progress, can't quiesce live update. 879 */ 880 int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh) 881 { 882 if (!liveupdate_enabled()) 883 return -EOPNOTSUPP; 884 885 if (!luo_session_quiesce()) 886 return -EBUSY; 887 888 list_del(&ACCESS_PRIVATE(fh, list)); 889 module_put(fh->ops->owner); 890 luo_session_resume(); 891 892 return 0; 893 } 894