1 // SPDX-License-Identifier: GPL-2.0 2 3 /* 4 * Copyright (c) 2025, Google LLC. 5 * Pasha Tatashin <pasha.tatashin@soleen.com> 6 * 7 * Copyright (C) 2025 Amazon.com Inc. or its affiliates. 8 * Pratyush Yadav <ptyadav@amazon.de> 9 */ 10 11 /** 12 * DOC: Memfd Preservation via LUO 13 * 14 * Overview 15 * ======== 16 * 17 * Memory file descriptors (memfd) can be preserved over a kexec using the Live 18 * Update Orchestrator (LUO) file preservation. This allows userspace to 19 * transfer its memory contents to the next kernel after a kexec. 20 * 21 * The preservation is not intended to be transparent. Only select properties of 22 * the file are preserved. All others are reset to default. The preserved 23 * properties are described below. 24 * 25 * .. note:: 26 * The LUO API is not stabilized yet, so the preserved properties of a memfd 27 * are also not stable and are subject to backwards incompatible changes. 28 * 29 * .. note:: 30 * Currently a memfd backed by Hugetlb is not supported. Memfds created 31 * with ``MFD_HUGETLB`` will be rejected. 32 * 33 * Preserved Properties 34 * ==================== 35 * 36 * The following properties of the memfd are preserved across kexec: 37 * 38 * File Contents 39 * All data stored in the file is preserved. 40 * 41 * File Size 42 * The size of the file is preserved. Holes in the file are filled by 43 * allocating pages for them during preservation. 44 * 45 * File Position 46 * The current file position is preserved, allowing applications to continue 47 * reading/writing from their last position. 48 * 49 * File Status Flags 50 * memfds are always opened with ``O_RDWR`` and ``O_LARGEFILE``. This property 51 * is maintained. 52 * 53 * Non-Preserved Properties 54 * ======================== 55 * 56 * All properties which are not preserved must be assumed to be reset to 57 * default. This section describes some of those properties which may be more of 58 * note. 59 * 60 * ``FD_CLOEXEC`` flag 61 * A memfd can be created with the ``MFD_CLOEXEC`` flag that sets the 62 * ``FD_CLOEXEC`` on the file. This flag is not preserved and must be set 63 * again after restore via ``fcntl()``. 64 * 65 * Seals 66 * File seals are not preserved. The file is unsealed on restore and if 67 * needed, must be sealed again via ``fcntl()``. 68 */ 69 70 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 71 72 #include <linux/bits.h> 73 #include <linux/err.h> 74 #include <linux/file.h> 75 #include <linux/io.h> 76 #include <linux/kexec_handover.h> 77 #include <linux/kho/abi/memfd.h> 78 #include <linux/liveupdate.h> 79 #include <linux/shmem_fs.h> 80 #include <linux/vmalloc.h> 81 #include <linux/memfd.h> 82 #include <uapi/linux/memfd.h> 83 84 #include "internal.h" 85 86 static int memfd_luo_preserve_folios(struct file *file, 87 struct kho_vmalloc *kho_vmalloc, 88 struct memfd_luo_folio_ser **out_folios_ser, 89 u64 *nr_foliosp) 90 { 91 struct inode *inode = file_inode(file); 92 struct memfd_luo_folio_ser *folios_ser; 93 unsigned int max_folios; 94 long i, size, nr_pinned; 95 struct folio **folios; 96 int err = -EINVAL; 97 pgoff_t offset; 98 u64 nr_folios; 99 100 size = i_size_read(inode); 101 /* 102 * If the file has zero size, then the folios and nr_folios properties 103 * are not set. 104 */ 105 if (!size) { 106 *nr_foliosp = 0; 107 *out_folios_ser = NULL; 108 memset(kho_vmalloc, 0, sizeof(*kho_vmalloc)); 109 return 0; 110 } 111 112 /* 113 * Guess the number of folios based on inode size. Real number might end 114 * up being smaller if there are higher order folios. 115 */ 116 max_folios = PAGE_ALIGN(size) / PAGE_SIZE; 117 folios = kvmalloc_objs(*folios, max_folios); 118 if (!folios) 119 return -ENOMEM; 120 121 /* 122 * Pin the folios so they don't move around behind our back. This also 123 * ensures none of the folios are in CMA -- which ensures they don't 124 * fall in KHO scratch memory. It also moves swapped out folios back to 125 * memory. 126 * 127 * A side effect of doing this is that it allocates a folio for all 128 * indices in the file. This might waste memory on sparse memfds. If 129 * that is really a problem in the future, we can have a 130 * memfd_pin_folios() variant that does not allocate a page on empty 131 * slots. 132 */ 133 nr_pinned = memfd_pin_folios(file, 0, size - 1, folios, max_folios, 134 &offset); 135 if (nr_pinned < 0) { 136 err = nr_pinned; 137 pr_err("failed to pin folios: %d\n", err); 138 goto err_free_folios; 139 } 140 nr_folios = nr_pinned; 141 142 folios_ser = vcalloc(nr_folios, sizeof(*folios_ser)); 143 if (!folios_ser) { 144 err = -ENOMEM; 145 goto err_unpin; 146 } 147 148 for (i = 0; i < nr_folios; i++) { 149 struct memfd_luo_folio_ser *pfolio = &folios_ser[i]; 150 struct folio *folio = folios[i]; 151 152 err = kho_preserve_folio(folio); 153 if (err) 154 goto err_unpreserve; 155 156 folio_lock(folio); 157 158 /* 159 * A dirty folio is one which has been written to. A clean folio 160 * is its opposite. Since a clean folio does not carry user 161 * data, it can be freed by page reclaim under memory pressure. 162 * 163 * Saving the dirty flag at prepare() time doesn't work since it 164 * can change later. Saving it at freeze() also won't work 165 * because the dirty bit is normally synced at unmap and there 166 * might still be a mapping of the file at freeze(). 167 * 168 * To see why this is a problem, say a folio is clean at 169 * preserve, but gets dirtied later. The pfolio flags will mark 170 * it as clean. After retrieve, the next kernel might try to 171 * reclaim this folio under memory pressure, losing user data. 172 * 173 * Unconditionally mark it dirty to avoid this problem. This 174 * comes at the cost of making clean folios un-reclaimable after 175 * live update. 176 */ 177 folio_mark_dirty(folio); 178 179 /* 180 * If the folio is not uptodate, it was fallocated but never 181 * used. Saving this flag at prepare() doesn't work since it 182 * might change later when someone uses the folio. 183 * 184 * Since we have taken the performance penalty of allocating, 185 * zeroing, and pinning all the folios in the holes, take a bit 186 * more and zero all non-uptodate folios too. 187 * 188 * NOTE: For someone looking to improve preserve performance, 189 * this is a good place to look. 190 */ 191 if (!folio_test_uptodate(folio)) { 192 folio_zero_range(folio, 0, folio_size(folio)); 193 flush_dcache_folio(folio); 194 folio_mark_uptodate(folio); 195 } 196 197 folio_unlock(folio); 198 199 pfolio->pfn = folio_pfn(folio); 200 pfolio->flags = MEMFD_LUO_FOLIO_DIRTY | MEMFD_LUO_FOLIO_UPTODATE; 201 pfolio->index = folio->index; 202 } 203 204 err = kho_preserve_vmalloc(folios_ser, kho_vmalloc); 205 if (err) 206 goto err_unpreserve; 207 208 kvfree(folios); 209 *nr_foliosp = nr_folios; 210 *out_folios_ser = folios_ser; 211 212 /* 213 * Note: folios_ser is purposely not freed here. It is preserved 214 * memory (via KHO). In the 'unpreserve' path, we use the vmap pointer 215 * that is passed via private_data. 216 */ 217 return 0; 218 219 err_unpreserve: 220 for (i = i - 1; i >= 0; i--) 221 kho_unpreserve_folio(folios[i]); 222 vfree(folios_ser); 223 err_unpin: 224 unpin_folios(folios, nr_folios); 225 err_free_folios: 226 kvfree(folios); 227 228 return err; 229 } 230 231 static void memfd_luo_unpreserve_folios(struct kho_vmalloc *kho_vmalloc, 232 struct memfd_luo_folio_ser *folios_ser, 233 u64 nr_folios) 234 { 235 long i; 236 237 if (!nr_folios) 238 return; 239 240 kho_unpreserve_vmalloc(kho_vmalloc); 241 242 for (i = 0; i < nr_folios; i++) { 243 const struct memfd_luo_folio_ser *pfolio = &folios_ser[i]; 244 struct folio *folio; 245 246 if (!pfolio->pfn) 247 continue; 248 249 folio = pfn_folio(pfolio->pfn); 250 251 kho_unpreserve_folio(folio); 252 unpin_folio(folio); 253 } 254 255 vfree(folios_ser); 256 } 257 258 static int memfd_luo_preserve(struct liveupdate_file_op_args *args) 259 { 260 struct inode *inode = file_inode(args->file); 261 struct memfd_luo_folio_ser *folios_ser; 262 struct memfd_luo_ser *ser; 263 u64 nr_folios; 264 int err = 0, seals; 265 266 inode_lock(inode); 267 shmem_freeze(inode, true); 268 269 /* Allocate the main serialization structure in preserved memory */ 270 ser = kho_alloc_preserve(sizeof(*ser)); 271 if (IS_ERR(ser)) { 272 err = PTR_ERR(ser); 273 goto err_unlock; 274 } 275 276 seals = memfd_get_seals(args->file); 277 if (seals < 0) { 278 err = seals; 279 goto err_free_ser; 280 } 281 282 /* Make sure the file only has the seals supported by this version. */ 283 if (seals & ~MEMFD_LUO_ALL_SEALS) { 284 err = -EOPNOTSUPP; 285 goto err_free_ser; 286 } 287 288 ser->pos = args->file->f_pos; 289 ser->size = i_size_read(inode); 290 ser->seals = seals; 291 292 err = memfd_luo_preserve_folios(args->file, &ser->folios, 293 &folios_ser, &nr_folios); 294 if (err) 295 goto err_free_ser; 296 297 ser->nr_folios = nr_folios; 298 inode_unlock(inode); 299 300 args->private_data = folios_ser; 301 args->serialized_data = virt_to_phys(ser); 302 303 return 0; 304 305 err_free_ser: 306 kho_unpreserve_free(ser); 307 err_unlock: 308 shmem_freeze(inode, false); 309 inode_unlock(inode); 310 return err; 311 } 312 313 static int memfd_luo_freeze(struct liveupdate_file_op_args *args) 314 { 315 struct memfd_luo_ser *ser; 316 317 if (WARN_ON_ONCE(!args->serialized_data)) 318 return -EINVAL; 319 320 ser = phys_to_virt(args->serialized_data); 321 322 /* 323 * The pos might have changed since prepare. Everything else stays the 324 * same. 325 */ 326 ser->pos = args->file->f_pos; 327 328 return 0; 329 } 330 331 static void memfd_luo_unpreserve(struct liveupdate_file_op_args *args) 332 { 333 struct inode *inode = file_inode(args->file); 334 struct memfd_luo_ser *ser; 335 336 if (WARN_ON_ONCE(!args->serialized_data)) 337 return; 338 339 inode_lock(inode); 340 shmem_freeze(inode, false); 341 342 ser = phys_to_virt(args->serialized_data); 343 344 memfd_luo_unpreserve_folios(&ser->folios, args->private_data, 345 ser->nr_folios); 346 347 kho_unpreserve_free(ser); 348 inode_unlock(inode); 349 } 350 351 static void memfd_luo_discard_folios(const struct memfd_luo_folio_ser *folios_ser, 352 u64 nr_folios) 353 { 354 u64 i; 355 356 for (i = 0; i < nr_folios; i++) { 357 const struct memfd_luo_folio_ser *pfolio = &folios_ser[i]; 358 struct folio *folio; 359 phys_addr_t phys; 360 361 if (!pfolio->pfn) 362 continue; 363 364 phys = PFN_PHYS(pfolio->pfn); 365 folio = kho_restore_folio(phys); 366 if (!folio) { 367 pr_warn_ratelimited("Unable to restore folio at physical address: %llx\n", 368 phys); 369 continue; 370 } 371 372 folio_put(folio); 373 } 374 } 375 376 static void memfd_luo_finish(struct liveupdate_file_op_args *args) 377 { 378 struct memfd_luo_folio_ser *folios_ser; 379 struct memfd_luo_ser *ser; 380 381 /* 382 * If retrieve was successful, nothing to do. If it failed, retrieve() 383 * already cleaned up everything it could. So nothing to do there 384 * either. Only need to clean up when retrieve was not called. 385 */ 386 if (args->retrieve_status) 387 return; 388 389 ser = phys_to_virt(args->serialized_data); 390 if (!ser) 391 return; 392 393 if (ser->nr_folios) { 394 folios_ser = kho_restore_vmalloc(&ser->folios); 395 if (!folios_ser) 396 goto out; 397 398 memfd_luo_discard_folios(folios_ser, ser->nr_folios); 399 vfree(folios_ser); 400 } 401 402 out: 403 kho_restore_free(ser); 404 } 405 406 static int memfd_luo_retrieve_folios(struct file *file, 407 struct memfd_luo_folio_ser *folios_ser, 408 u64 nr_folios) 409 { 410 struct inode *inode = file_inode(file); 411 struct address_space *mapping = inode->i_mapping; 412 struct folio *folio; 413 int err = -EIO; 414 long i; 415 416 for (i = 0; i < nr_folios; i++) { 417 const struct memfd_luo_folio_ser *pfolio = &folios_ser[i]; 418 phys_addr_t phys; 419 u64 index; 420 int flags; 421 422 if (!pfolio->pfn) 423 continue; 424 425 phys = PFN_PHYS(pfolio->pfn); 426 folio = kho_restore_folio(phys); 427 if (!folio) { 428 pr_err("Unable to restore folio at physical address: %llx\n", 429 phys); 430 goto put_folios; 431 } 432 index = pfolio->index; 433 flags = pfolio->flags; 434 435 /* Set up the folio for insertion. */ 436 __folio_set_locked(folio); 437 __folio_set_swapbacked(folio); 438 439 err = mem_cgroup_charge(folio, NULL, mapping_gfp_mask(mapping)); 440 if (err) { 441 pr_err("shmem: failed to charge folio index %ld: %d\n", 442 i, err); 443 goto unlock_folio; 444 } 445 446 err = shmem_add_to_page_cache(folio, mapping, index, NULL, 447 mapping_gfp_mask(mapping)); 448 if (err) { 449 pr_err("shmem: failed to add to page cache folio index %ld: %d\n", 450 i, err); 451 goto unlock_folio; 452 } 453 454 if (flags & MEMFD_LUO_FOLIO_UPTODATE) 455 folio_mark_uptodate(folio); 456 if (flags & MEMFD_LUO_FOLIO_DIRTY) 457 folio_mark_dirty(folio); 458 459 err = shmem_inode_acct_blocks(inode, 1); 460 if (err) { 461 pr_err("shmem: failed to account folio index %ld: %d\n", 462 i, err); 463 goto unlock_folio; 464 } 465 466 shmem_recalc_inode(inode, 1, 0); 467 folio_add_lru(folio); 468 folio_unlock(folio); 469 folio_put(folio); 470 } 471 472 return 0; 473 474 unlock_folio: 475 folio_unlock(folio); 476 folio_put(folio); 477 put_folios: 478 /* 479 * Note: don't free the folios already added to the file. They will be 480 * freed when the file is freed. Free the ones not added yet here. 481 */ 482 for (long j = i + 1; j < nr_folios; j++) { 483 const struct memfd_luo_folio_ser *pfolio = &folios_ser[j]; 484 485 folio = kho_restore_folio(pfolio->pfn); 486 if (folio) 487 folio_put(folio); 488 } 489 490 return err; 491 } 492 493 static int memfd_luo_retrieve(struct liveupdate_file_op_args *args) 494 { 495 struct memfd_luo_folio_ser *folios_ser; 496 struct memfd_luo_ser *ser; 497 struct file *file; 498 int err; 499 500 ser = phys_to_virt(args->serialized_data); 501 if (!ser) 502 return -EINVAL; 503 504 /* Make sure the file only has seals supported by this version. */ 505 if (ser->seals & ~MEMFD_LUO_ALL_SEALS) { 506 err = -EOPNOTSUPP; 507 goto free_ser; 508 } 509 510 /* 511 * The seals are preserved. Allow sealing here so they can be added 512 * later. 513 */ 514 file = memfd_alloc_file("", MFD_ALLOW_SEALING); 515 if (IS_ERR(file)) { 516 pr_err("failed to setup file: %pe\n", file); 517 err = PTR_ERR(file); 518 goto free_ser; 519 } 520 521 err = memfd_add_seals(file, ser->seals); 522 if (err) { 523 pr_err("failed to add seals: %pe\n", ERR_PTR(err)); 524 goto put_file; 525 } 526 527 vfs_setpos(file, ser->pos, MAX_LFS_FILESIZE); 528 file->f_inode->i_size = ser->size; 529 530 if (ser->nr_folios) { 531 folios_ser = kho_restore_vmalloc(&ser->folios); 532 if (!folios_ser) { 533 err = -EINVAL; 534 goto put_file; 535 } 536 537 err = memfd_luo_retrieve_folios(file, folios_ser, ser->nr_folios); 538 vfree(folios_ser); 539 if (err) 540 goto put_file; 541 } 542 543 args->file = file; 544 kho_restore_free(ser); 545 546 return 0; 547 548 put_file: 549 fput(file); 550 free_ser: 551 kho_restore_free(ser); 552 return err; 553 } 554 555 static bool memfd_luo_can_preserve(struct liveupdate_file_handler *handler, 556 struct file *file) 557 { 558 struct inode *inode = file_inode(file); 559 560 return shmem_file(file) && !inode->i_nlink; 561 } 562 563 static const struct liveupdate_file_ops memfd_luo_file_ops = { 564 .freeze = memfd_luo_freeze, 565 .finish = memfd_luo_finish, 566 .retrieve = memfd_luo_retrieve, 567 .preserve = memfd_luo_preserve, 568 .unpreserve = memfd_luo_unpreserve, 569 .can_preserve = memfd_luo_can_preserve, 570 .owner = THIS_MODULE, 571 }; 572 573 static struct liveupdate_file_handler memfd_luo_handler = { 574 .ops = &memfd_luo_file_ops, 575 .compatible = MEMFD_LUO_FH_COMPATIBLE, 576 }; 577 578 static int __init memfd_luo_init(void) 579 { 580 int err = liveupdate_register_file_handler(&memfd_luo_handler); 581 582 if (err && err != -EOPNOTSUPP) { 583 pr_err("Could not register luo filesystem handler: %pe\n", 584 ERR_PTR(err)); 585 586 return err; 587 } 588 589 return 0; 590 } 591 late_initcall(memfd_luo_init); 592