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 Sessions 10 * 11 * LUO Sessions provide the core mechanism for grouping and managing `struct 12 * file *` instances that need to be preserved across a kexec-based live 13 * update. Each session acts as a named container for a set of file objects, 14 * allowing a userspace agent to manage the lifecycle of resources critical to a 15 * workload. 16 * 17 * Core Concepts: 18 * 19 * - Named Containers: Sessions are identified by a unique, user-provided name, 20 * which is used for both creation in the current kernel and retrieval in the 21 * next kernel. 22 * 23 * - Userspace Interface: Session management is driven from userspace via 24 * ioctls on /dev/liveupdate. 25 * 26 * - Serialization: Session metadata is preserved using the KHO framework. When 27 * a live update is triggered via kexec, session metadata is serialized into 28 * a chain of linked-blocks and placed in a preserved memory region. The 29 * physical address of the first block header is stored in the centralized 30 * `struct luo_ser` structure. 31 * 32 * Session Lifecycle: 33 * 34 * 1. Creation: A userspace agent calls `luo_session_create()` to create a 35 * new, empty session and receives a file descriptor for it. 36 * 37 * 2. Serialization: When the `reboot(LINUX_REBOOT_CMD_KEXEC)` syscall is 38 * made, `luo_session_serialize()` is called. It iterates through all 39 * active sessions and writes their metadata into a memory area preserved 40 * by KHO. 41 * 42 * 3. Deserialization (in new kernel): After kexec, `luo_session_deserialize()` 43 * runs, reading the serialized data and creating a list of `struct 44 * luo_session` objects representing the preserved sessions. 45 * 46 * 4. Retrieval: A userspace agent in the new kernel can then call 47 * `luo_session_retrieve()` with a session name to get a new file 48 * descriptor and access the preserved state. 49 * 50 * Locking: 51 * 52 * The LUO session subsystem uses a three-tier locking hierarchy to ensure thread 53 * safety and prevent deadlocks during concurrent session mutations and kexec 54 * serialization: 55 * 56 * 1. `luo_session_serialize_rwsem` (global rwsem): 57 * Protects session mutations (creation, retrieval, release, and ioctls) 58 * against the serialization process during reboot. 59 * 60 * - Readers: Taken by any path modifying or accessing session state (e.g., 61 * `luo_session_create()`, `luo_session_retrieve()`, `luo_session_release()`, 62 * and `luo_session_ioctl()`). 63 * - Writer: Taken by the serialization process (`luo_session_serialize()`) 64 * during reboot. On success, the write lock is held indefinitely to freeze 65 * the subsystem. On failure, it is released to allow recovery. 66 * 67 * 2. `luo_session_header->rwsem` (per-list rwsem): 68 * Synchronizes list-level operations for the incoming and outgoing session headers. 69 * 70 * - Writer: Taken during list mutation operations (inserting or removing a 71 * session from the list). 72 * - Reader: Taken when traversing the list (e.g., retrieving a session by name). 73 * 74 * 3. `luo_session->mutex` (per-session mutex): 75 * Protects the internal state and file sets of an individual session. It is 76 * acquired during per-session operations such as preserving, retrieving, 77 * or freezing files. 78 * 79 * Lock Hierarchy: 80 * `luo_session_serialize_rwsem` -> `luo_session_header->rwsem` -> `luo_session->mutex` 81 */ 82 83 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 84 85 #include <linux/anon_inodes.h> 86 #include <linux/cleanup.h> 87 #include <linux/err.h> 88 #include <linux/errno.h> 89 #include <linux/file.h> 90 #include <linux/fs.h> 91 #include <linux/io.h> 92 #include <linux/kexec_handover.h> 93 #include <linux/kho_block.h> 94 #include <linux/kho/abi/luo.h> 95 #include <linux/list.h> 96 #include <linux/liveupdate.h> 97 #include <linux/mutex.h> 98 #include <linux/rwsem.h> 99 #include <linux/slab.h> 100 #include <uapi/linux/liveupdate.h> 101 #include "luo_internal.h" 102 103 static DECLARE_RWSEM(luo_session_serialize_rwsem); 104 /** 105 * struct luo_session_header - Header struct for managing LUO sessions. 106 * @count: The number of sessions currently tracked in the @list. 107 * @list: The head of the linked list of `struct luo_session` instances. 108 * @rwsem: A read-write semaphore providing synchronized access to the 109 * session list and other fields in this structure. 110 * @block_set: The set of serialization blocks. 111 * @sessions_pa: Points to the location of sessions_pa within struct luo_ser. 112 * @active: Set to true when first initialized. If previous kernel did not 113 * send session data, active stays false for incoming. 114 */ 115 struct luo_session_header { 116 long count; 117 struct list_head list; 118 struct rw_semaphore rwsem; 119 struct kho_block_set block_set; 120 u64 *sessions_pa; 121 bool active; 122 }; 123 124 /** 125 * struct luo_session_global - Global container for managing LUO sessions. 126 * @incoming: The sessions passed from the previous kernel. 127 * @outgoing: The sessions that are going to be passed to the next kernel. 128 */ 129 struct luo_session_global { 130 struct luo_session_header incoming; 131 struct luo_session_header outgoing; 132 }; 133 134 static struct luo_session_global luo_session_global = { 135 .incoming = { 136 .list = LIST_HEAD_INIT(luo_session_global.incoming.list), 137 .rwsem = __RWSEM_INITIALIZER(luo_session_global.incoming.rwsem), 138 .block_set = KHO_BLOCK_SET_INIT(luo_session_global.incoming.block_set, 139 sizeof(struct luo_session_ser)), 140 }, 141 .outgoing = { 142 .list = LIST_HEAD_INIT(luo_session_global.outgoing.list), 143 .rwsem = __RWSEM_INITIALIZER(luo_session_global.outgoing.rwsem), 144 .block_set = KHO_BLOCK_SET_INIT(luo_session_global.outgoing.block_set, 145 sizeof(struct luo_session_ser)), 146 }, 147 }; 148 149 static struct luo_session *luo_session_alloc(const char *name) 150 { 151 struct luo_session *session = kzalloc_obj(*session); 152 153 if (!session) 154 return ERR_PTR(-ENOMEM); 155 156 strscpy(session->name, name, sizeof(session->name)); 157 luo_file_set_init(&session->file_set); 158 INIT_LIST_HEAD(&session->list); 159 mutex_init(&session->mutex); 160 161 return session; 162 } 163 164 static void luo_session_free(struct luo_session *session) 165 { 166 luo_file_set_destroy(&session->file_set); 167 mutex_destroy(&session->mutex); 168 kfree(session); 169 } 170 171 static int luo_session_insert(struct luo_session_header *sh, 172 struct luo_session *session) 173 { 174 struct luo_session *it; 175 int err; 176 177 guard(rwsem_write)(&sh->rwsem); 178 179 /* 180 * For small number of sessions this loop won't hurt performance 181 * but if we ever start using a lot of sessions, this might 182 * become a bottle neck during deserialization time, as it would 183 * cause O(n*n) complexity. 184 */ 185 list_for_each_entry(it, &sh->list, list) { 186 if (!strncmp(it->name, session->name, sizeof(it->name))) 187 return -EEXIST; 188 } 189 190 /* 191 * For outgoing we should make sure there is room in serialization array 192 * for new session. 193 */ 194 if (sh == &luo_session_global.outgoing) { 195 err = kho_block_set_grow(&sh->block_set, sh->count + 1); 196 if (err) 197 return err; 198 } 199 200 list_add_tail(&session->list, &sh->list); 201 sh->count++; 202 203 return 0; 204 } 205 206 static void luo_session_remove(struct luo_session_header *sh, 207 struct luo_session *session) 208 { 209 guard(rwsem_write)(&sh->rwsem); 210 list_del(&session->list); 211 sh->count--; 212 if (sh == &luo_session_global.outgoing) 213 kho_block_set_shrink(&sh->block_set, sh->count); 214 } 215 216 static int luo_session_finish_one(struct luo_session *session) 217 { 218 guard(mutex)(&session->mutex); 219 return luo_file_finish(&session->file_set); 220 } 221 222 static void luo_session_unfreeze_one(struct luo_session *session, 223 struct luo_session_ser *ser) 224 { 225 guard(mutex)(&session->mutex); 226 luo_file_unfreeze(&session->file_set, &ser->file_set_ser); 227 } 228 229 static int luo_session_freeze_one(struct luo_session *session, 230 struct luo_session_ser *ser) 231 { 232 guard(mutex)(&session->mutex); 233 return luo_file_freeze(&session->file_set, &ser->file_set_ser); 234 } 235 236 static int luo_session_release(struct inode *inodep, struct file *filep) 237 { 238 struct luo_session *session = filep->private_data; 239 struct luo_session_header *sh; 240 241 guard(rwsem_read)(&luo_session_serialize_rwsem); 242 /* If retrieved is set, it means this session is from incoming list */ 243 if (session->retrieved) { 244 int err = luo_session_finish_one(session); 245 246 if (err) { 247 pr_warn("Unable to finish session [%s] on release\n", 248 session->name); 249 return err; 250 } 251 sh = &luo_session_global.incoming; 252 } else { 253 scoped_guard(mutex, &session->mutex) 254 luo_file_unpreserve_files(&session->file_set); 255 sh = &luo_session_global.outgoing; 256 } 257 258 luo_session_remove(sh, session); 259 luo_session_free(session); 260 261 return 0; 262 } 263 264 static int luo_session_preserve_fd(struct luo_session *session, 265 struct luo_ucmd *ucmd) 266 { 267 struct liveupdate_session_preserve_fd *argp = ucmd->cmd; 268 int err; 269 270 guard(mutex)(&session->mutex); 271 err = luo_preserve_file(&session->file_set, argp->token, argp->fd); 272 if (err) 273 return err; 274 275 err = luo_ucmd_respond(ucmd, sizeof(*argp)); 276 if (err) 277 pr_warn("The file was successfully preserved, but response to user failed\n"); 278 279 return err; 280 } 281 282 static int luo_session_retrieve_fd(struct luo_session *session, 283 struct luo_ucmd *ucmd) 284 { 285 struct liveupdate_session_retrieve_fd *argp = ucmd->cmd; 286 struct file *file; 287 int err; 288 289 argp->fd = get_unused_fd_flags(O_CLOEXEC); 290 if (argp->fd < 0) 291 return argp->fd; 292 293 mutex_lock(&session->mutex); 294 err = luo_retrieve_file(&session->file_set, argp->token, &file); 295 mutex_unlock(&session->mutex); 296 if (err < 0) 297 goto err_put_fd; 298 299 err = luo_ucmd_respond(ucmd, sizeof(*argp)); 300 if (err) 301 goto err_put_file; 302 303 fd_install(argp->fd, file); 304 305 return 0; 306 307 err_put_file: 308 fput(file); 309 err_put_fd: 310 put_unused_fd(argp->fd); 311 312 return err; 313 } 314 315 static int luo_session_finish(struct luo_session *session, 316 struct luo_ucmd *ucmd) 317 { 318 struct liveupdate_session_finish *argp = ucmd->cmd; 319 int err; 320 321 if (argp->reserved) 322 return -EINVAL; 323 324 err = luo_session_finish_one(session); 325 if (err) 326 return err; 327 328 return luo_ucmd_respond(ucmd, sizeof(*argp)); 329 } 330 331 static int luo_session_get_name(struct luo_session *session, 332 struct luo_ucmd *ucmd) 333 { 334 struct liveupdate_session_get_name *argp = ucmd->cmd; 335 336 if (argp->reserved != 0) 337 return -EINVAL; 338 339 strscpy((char *)argp->name, session->name, sizeof(argp->name)); 340 341 return luo_ucmd_respond(ucmd, sizeof(*argp)); 342 } 343 344 union ucmd_buffer { 345 struct liveupdate_session_finish finish; 346 struct liveupdate_session_preserve_fd preserve; 347 struct liveupdate_session_retrieve_fd retrieve; 348 struct liveupdate_session_get_name get_name; 349 }; 350 351 /* Type of sessions the ioctl applies to. */ 352 enum luo_ioctl_type { 353 LUO_IOCTL_INCOMING, 354 LUO_IOCTL_OUTGOING, 355 LUO_IOCTL_ALL, 356 }; 357 358 struct luo_ioctl_op { 359 unsigned int size; 360 unsigned int min_size; 361 unsigned int ioctl_num; 362 enum luo_ioctl_type type; 363 int (*execute)(struct luo_session *session, struct luo_ucmd *ucmd); 364 }; 365 366 #define IOCTL_OP(_ioctl, _fn, _struct, _last, _type) \ 367 [_IOC_NR(_ioctl) - LIVEUPDATE_CMD_SESSION_BASE] = { \ 368 .size = sizeof(_struct) + \ 369 BUILD_BUG_ON_ZERO(sizeof(union ucmd_buffer) < \ 370 sizeof(_struct)), \ 371 .min_size = offsetofend(_struct, _last), \ 372 .ioctl_num = _ioctl, \ 373 .type = _type, \ 374 .execute = _fn, \ 375 } 376 377 static const struct luo_ioctl_op luo_session_ioctl_ops[] = { 378 IOCTL_OP(LIVEUPDATE_SESSION_FINISH, luo_session_finish, 379 struct liveupdate_session_finish, reserved, LUO_IOCTL_INCOMING), 380 IOCTL_OP(LIVEUPDATE_SESSION_PRESERVE_FD, luo_session_preserve_fd, 381 struct liveupdate_session_preserve_fd, token, LUO_IOCTL_OUTGOING), 382 IOCTL_OP(LIVEUPDATE_SESSION_RETRIEVE_FD, luo_session_retrieve_fd, 383 struct liveupdate_session_retrieve_fd, token, LUO_IOCTL_INCOMING), 384 IOCTL_OP(LIVEUPDATE_SESSION_GET_NAME, luo_session_get_name, 385 struct liveupdate_session_get_name, name, LUO_IOCTL_ALL), 386 }; 387 388 static bool luo_ioctl_type_valid(struct luo_session *session, 389 const struct luo_ioctl_op *op) 390 { 391 switch (op->type) { 392 case LUO_IOCTL_INCOMING: 393 /* Retrieved is only set on incoming sessions */ 394 return session->retrieved; 395 case LUO_IOCTL_OUTGOING: 396 return !session->retrieved; 397 case LUO_IOCTL_ALL: 398 return true; 399 } 400 401 /* Catch-all. */ 402 return false; 403 } 404 405 static long luo_session_ioctl(struct file *filep, unsigned int cmd, 406 unsigned long arg) 407 { 408 struct luo_session *session = filep->private_data; 409 const struct luo_ioctl_op *op; 410 struct luo_ucmd ucmd = {}; 411 union ucmd_buffer buf; 412 unsigned int nr; 413 int ret; 414 415 nr = _IOC_NR(cmd); 416 if (nr < LIVEUPDATE_CMD_SESSION_BASE || (nr - LIVEUPDATE_CMD_SESSION_BASE) >= 417 ARRAY_SIZE(luo_session_ioctl_ops)) { 418 return -EINVAL; 419 } 420 421 ucmd.ubuffer = (void __user *)arg; 422 ret = get_user(ucmd.user_size, (u32 __user *)ucmd.ubuffer); 423 if (ret) 424 return ret; 425 426 op = &luo_session_ioctl_ops[nr - LIVEUPDATE_CMD_SESSION_BASE]; 427 if (op->ioctl_num != cmd) 428 return -ENOIOCTLCMD; 429 if (!luo_ioctl_type_valid(session, op)) 430 return -EINVAL; 431 if (ucmd.user_size < op->min_size) 432 return -EINVAL; 433 434 ucmd.cmd = &buf; 435 ret = copy_struct_from_user(ucmd.cmd, op->size, ucmd.ubuffer, 436 ucmd.user_size); 437 if (ret) 438 return ret; 439 440 guard(rwsem_read)(&luo_session_serialize_rwsem); 441 return op->execute(session, &ucmd); 442 } 443 444 static const struct file_operations luo_session_fops = { 445 .owner = THIS_MODULE, 446 .release = luo_session_release, 447 .unlocked_ioctl = luo_session_ioctl, 448 }; 449 450 /* Create a "struct file" for session */ 451 static int luo_session_getfile(struct luo_session *session, struct file **filep) 452 { 453 char name_buf[128]; 454 struct file *file; 455 456 lockdep_assert_held(&session->mutex); 457 snprintf(name_buf, sizeof(name_buf), "[luo_session] %s", session->name); 458 file = anon_inode_getfile(name_buf, &luo_session_fops, session, O_RDWR); 459 if (IS_ERR(file)) 460 return PTR_ERR(file); 461 462 *filep = file; 463 464 return 0; 465 } 466 467 int luo_session_create(const char *name, struct file **filep) 468 { 469 size_t len = strnlen(name, LIVEUPDATE_SESSION_NAME_LENGTH); 470 struct luo_session *session; 471 int err; 472 473 if (len == 0 || len > LIVEUPDATE_SESSION_NAME_LENGTH - 1) 474 return -EINVAL; 475 476 session = luo_session_alloc(name); 477 if (IS_ERR(session)) 478 return PTR_ERR(session); 479 480 down_read(&luo_session_serialize_rwsem); 481 err = luo_session_insert(&luo_session_global.outgoing, session); 482 if (err) 483 goto err_free; 484 485 mutex_lock(&session->mutex); 486 err = luo_session_getfile(session, filep); 487 mutex_unlock(&session->mutex); 488 if (err) 489 goto err_remove; 490 up_read(&luo_session_serialize_rwsem); 491 492 return 0; 493 494 err_remove: 495 luo_session_remove(&luo_session_global.outgoing, session); 496 err_free: 497 luo_session_free(session); 498 up_read(&luo_session_serialize_rwsem); 499 500 return err; 501 } 502 503 int luo_session_retrieve(const char *name, struct file **filep) 504 { 505 struct luo_session_header *sh = &luo_session_global.incoming; 506 struct luo_session *session = NULL; 507 struct luo_session *it; 508 int err; 509 510 guard(rwsem_read)(&luo_session_serialize_rwsem); 511 guard(rwsem_read)(&sh->rwsem); 512 list_for_each_entry(it, &sh->list, list) { 513 if (!strncmp(it->name, name, sizeof(it->name))) { 514 session = it; 515 break; 516 } 517 } 518 519 if (!session) 520 return -ENOENT; 521 522 guard(mutex)(&session->mutex); 523 if (session->retrieved) 524 return -EINVAL; 525 526 err = luo_session_getfile(session, filep); 527 if (!err) 528 session->retrieved = true; 529 530 return err; 531 } 532 533 void __init luo_session_setup_outgoing(u64 *sessions_pa) 534 { 535 luo_session_global.outgoing.sessions_pa = sessions_pa; 536 luo_session_global.outgoing.active = true; 537 } 538 539 int __init luo_session_setup_incoming(u64 sessions_pa) 540 { 541 struct luo_session_header *sh = &luo_session_global.incoming; 542 int err; 543 544 if (!sessions_pa) 545 return 0; 546 547 err = kho_block_set_restore(&sh->block_set, sessions_pa); 548 if (err) 549 return err; 550 551 sh->active = true; 552 return 0; 553 } 554 555 static int luo_session_deserialize_one(struct luo_session_header *sh, 556 struct luo_session_ser *ser) 557 { 558 struct luo_session *session; 559 int err; 560 561 session = luo_session_alloc(ser->name); 562 if (IS_ERR(session)) { 563 pr_warn("Failed to allocate session [%.*s] during deserialization %pe\n", 564 (int)sizeof(ser->name), ser->name, session); 565 return PTR_ERR(session); 566 } 567 568 err = luo_session_insert(sh, session); 569 if (err) { 570 pr_warn("Failed to insert session [%s] %pe\n", 571 session->name, ERR_PTR(err)); 572 luo_session_free(session); 573 return err; 574 } 575 576 scoped_guard(mutex, &session->mutex) { 577 err = luo_file_deserialize(&session->file_set, 578 &ser->file_set_ser); 579 } 580 if (err) { 581 pr_warn("Failed to deserialize files for session [%s] %pe\n", 582 session->name, ERR_PTR(err)); 583 return err; 584 } 585 586 return 0; 587 } 588 589 int luo_session_deserialize(void) 590 { 591 struct luo_session_header *sh = &luo_session_global.incoming; 592 static bool is_deserialized; 593 struct luo_session_ser *ser; 594 struct kho_block_set_it it; 595 static int saved_err; 596 int err; 597 598 /* If has been deserialized, always return the same error code */ 599 if (is_deserialized) 600 return saved_err; 601 602 is_deserialized = true; 603 if (!sh->active) 604 return 0; 605 606 /* 607 * Note on error handling: 608 * 609 * If deserialization fails (e.g., allocation failure or corrupt data), 610 * we intentionally skip cleanup of sessions that were already restored. 611 * 612 * A partial failure leaves the preserved state inconsistent. 613 * Implementing a safe "undo" to unwind complex dependencies (sessions, 614 * files, hardware state) is error-prone and provides little value, as 615 * the system is effectively in a broken state. 616 * 617 * We treat these resources as leaked. The expected recovery path is for 618 * userspace to detect the failure and trigger a reboot, which will 619 * reliably reset devices and reclaim memory. 620 */ 621 kho_block_set_it_init(&it, &sh->block_set); 622 while ((ser = kho_block_set_it_read_entry(&it))) { 623 err = luo_session_deserialize_one(sh, ser); 624 if (err) 625 goto save_err; 626 } 627 628 kho_block_set_destroy(&sh->block_set); 629 630 return 0; 631 632 save_err: 633 kho_block_set_destroy(&sh->block_set); 634 saved_err = err; 635 return err; 636 } 637 638 int luo_session_serialize(void) 639 { 640 struct luo_session_header *sh = &luo_session_global.outgoing; 641 struct luo_session *session; 642 struct kho_block_set_it it; 643 int err; 644 645 down_write(&luo_session_serialize_rwsem); 646 down_write(&sh->rwsem); 647 *sh->sessions_pa = 0; 648 649 kho_block_set_it_init(&it, &sh->block_set); 650 651 list_for_each_entry(session, &sh->list, list) { 652 struct luo_session_ser *ser = kho_block_set_it_reserve_entry(&it); 653 654 /* This should not fail normally as blocks were pre-allocated */ 655 if (WARN_ON_ONCE(!ser)) { 656 err = -ENOSPC; 657 goto err_undo; 658 } 659 660 err = luo_session_freeze_one(session, ser); 661 if (err) { 662 kho_block_set_it_prev(&it); 663 goto err_undo; 664 } 665 666 strscpy(ser->name, session->name, sizeof(ser->name)); 667 } 668 669 if (sh->count > 0) 670 *sh->sessions_pa = kho_block_set_head_pa(&sh->block_set); 671 up_write(&sh->rwsem); 672 673 return 0; 674 675 err_undo: 676 list_for_each_entry_continue_reverse(session, &sh->list, list) { 677 struct luo_session_ser *ser = kho_block_set_it_prev(&it); 678 679 luo_session_unfreeze_one(session, ser); 680 memset(ser->name, 0, sizeof(ser->name)); 681 } 682 up_write(&sh->rwsem); 683 up_write(&luo_session_serialize_rwsem); 684 685 return err; 686 } 687