To remove the fixed limit on the number of preserved files per session, transition the file metadata serialization from a single contiguous memory block to a chain of linked blocks.
Showing the changes to kernel (2 of 3 files). Show all
Unified · Split
| @@ -118,11 +118,6 @@ static LIST_HEAD(luo_file_handler_list); · 117 unmodified lines | ||
| 118 | 118 | /* Keep track of files being preserved by LUO */ |
| 119 | 119 | static DEFINE_XARRAY(luo_preserved_files); |
| 120 | 120 | |
| 121 | −/* 2 4K pages, give space for 128 files per file_set */ | |
| 122 | −#define LUO_FILE_PGCNT 2ul | |
| 123 | −#define LUO_FILE_MAX \ | |
| 124 | − ((LUO_FILE_PGCNT << PAGE_SHIFT) / sizeof(struct luo_file_ser)) | |
| 125 | − | |
| 126 | 121 | /** |
| 127 | 122 | * struct luo_file - Represents a single preserved file instance. |
| 128 | 123 | * @fh: Pointer to the &struct liveupdate_file_handler that manages |
| @@ -174,39 +169,6 @@ struct luo_file { · 45 unmodified lines | ||
| 174 | 169 | u64 token; |
| 175 | 170 | }; |
| 176 | 171 | |
| 177 | −static int luo_alloc_files_mem(struct luo_file_set *file_set) | |
| 178 | −{ | |
| 179 | − size_t size; | |
| 180 | − void *mem; | |
| 181 | − | |
| 182 | − if (file_set->files) | |
| 183 | − return 0; | |
| 184 | − | |
| 185 | − WARN_ON_ONCE(file_set->count); | |
| 186 | − | |
| 187 | − size = LUO_FILE_PGCNT << PAGE_SHIFT; | |
| 188 | − mem = kho_alloc_preserve(size); | |
| 189 | − if (IS_ERR(mem)) | |
| 190 | − return PTR_ERR(mem); | |
| 191 | − | |
| 192 | − file_set->files = mem; | |
| 193 | − | |
| 194 | − return 0; | |
| 195 | −} | |
| 196 | − | |
| 197 | −static void luo_free_files_mem(struct luo_file_set *file_set) | |
| 198 | −{ | |
| 199 | − /* If file_set has files, no need to free preservation memory */ | |
| 200 | − if (file_set->count) | |
| 201 | − return; | |
| 202 | − | |
| 203 | − if (!file_set->files) | |
| 204 | − return; | |
| 205 | − | |
| 206 | − kho_unpreserve_free(file_set->files); | |
| 207 | − file_set->files = NULL; | |
| 208 | −} | |
| 209 | − | |
| 210 | 172 | static unsigned long luo_get_id(struct liveupdate_file_handler *fh, |
| 211 | 173 | struct file *file) |
| 212 | 174 | { |
| @@ -276,16 +238,15 @@ int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd) · 63 unmodified lines | ||
| 276 | 238 | if (luo_token_is_used(file_set, token)) |
| 277 | 239 | return -EEXIST; |
| 278 | 240 | |
| 279 | − if (file_set->count == LUO_FILE_MAX) | |
| 280 | − return -ENOSPC; | |
| 241 | + err = kho_block_set_grow(&file_set->block_set, file_set->count + 1); | |
| 242 | + if (err) | |
| 243 | + return err; | |
| 281 | 244 | |
| 282 | 245 | file = fget(fd); |
| 283 | − if (!file) | |
| 284 | − return -EBADF; | |
| 285 | − | |
| 286 | − err = luo_alloc_files_mem(file_set); | |
| 287 | − if (err) | |
| 288 | − goto err_fput; | |
| 246 | + if (!file) { | |
| 247 | + err = -EBADF; | |
| 248 | + goto err_shrink; | |
| 249 | + } | |
| 289 | 250 | |
| 290 | 251 | err = -ENOENT; |
| 291 | 252 | down_read(&luo_register_rwlock); |
| @@ -300,7 +261,7 @@ int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd) · 8 unmodified lines | ||
| 300 | 261 | |
| 301 | 262 | /* err is still -ENOENT if no handler was found */ |
| 302 | 263 | if (err) |
| 303 | − goto err_free_files_mem; | |
| 264 | + goto err_fput; | |
| 304 | 265 | |
| 305 | 266 | err = xa_insert(&luo_preserved_files, luo_get_id(fh, file), |
| 306 | 267 | file, GFP_KERNEL); |
| @@ -343,10 +304,10 @@ err_erase_xa: · 36 unmodified lines | ||
| 343 | 304 | xa_erase(&luo_preserved_files, luo_get_id(fh, file)); |
| 344 | 305 | err_module_put: |
| 345 | 306 | module_put(fh->ops->owner); |
| 346 | −err_free_files_mem: | |
| 347 | − luo_free_files_mem(file_set); | |
| 348 | 307 | err_fput: |
| 349 | 308 | fput(file); |
| 309 | +err_shrink: | |
| 310 | + kho_block_set_shrink(&file_set->block_set, file_set->count); | |
| 350 | 311 | |
| 351 | 312 | return err; |
| 352 | 313 | } |
| @@ -392,13 +353,14 @@ void luo_file_unpreserve_files(struct luo_file_set *file_set) · 39 unmodified lines | ||
| 392 | 353 | |
| 393 | 354 | list_del(&luo_file->list); |
| 394 | 355 | file_set->count--; |
| 356 | + kho_block_set_shrink(&file_set->block_set, file_set->count); | |
| 395 | 357 | |
| 396 | 358 | fput(luo_file->file); |
| 397 | 359 | mutex_destroy(&luo_file->mutex); |
| 398 | 360 | kfree(luo_file); |
| 399 | 361 | } |
| 400 | 362 | |
| 401 | − luo_free_files_mem(file_set); | |
| 363 | + kho_block_set_destroy(&file_set->block_set); | |
| 402 | 364 | } |
| 403 | 365 | |
| 404 | 366 | static int luo_file_freeze_one(struct luo_file_set *file_set, |
| @@ -454,7 +416,7 @@ static void __luo_file_unfreeze(struct luo_file_set *file_set, · 49 unmodified lines | ||
| 454 | 416 | luo_file_unfreeze_one(file_set, luo_file); |
| 455 | 417 | } |
| 456 | 418 | |
| 457 | − memset(file_set->files, 0, LUO_FILE_PGCNT << PAGE_SHIFT); | |
| 419 | + kho_block_set_clear(&file_set->block_set); | |
| 458 | 420 | } |
| 459 | 421 | |
| 460 | 422 | /** |
| @@ -493,19 +455,24 @@ static void __luo_file_unfreeze(struct luo_file_set *file_set, · 32 unmodified lines | ||
| 493 | 455 | int luo_file_freeze(struct luo_file_set *file_set, |
| 494 | 456 | struct luo_file_set_ser *file_set_ser) |
| 495 | 457 | { |
| 496 | − struct luo_file_ser *file_ser = file_set->files; | |
| 497 | 458 | struct luo_file *luo_file; |
| 459 | + struct kho_block_set_it it; | |
| 498 | 460 | int err; |
| 499 | − int i; | |
| 500 | 461 | |
| 501 | 462 | if (!file_set->count) |
| 502 | 463 | return 0; |
| 503 | 464 | |
| 504 | − if (WARN_ON(!file_ser)) | |
| 505 | − return -EINVAL; | |
| 465 | + kho_block_set_it_init(&it, &file_set->block_set); | |
| 506 | 466 | |
| 507 | − i = 0; | |
| 508 | 467 | list_for_each_entry(luo_file, &file_set->files_list, list) { |
| 468 | + struct luo_file_ser *file_ser = kho_block_set_it_reserve_entry(&it); | |
| 469 | + | |
| 470 | + /* This should not fail normally as blocks were pre-allocated */ | |
| 471 | + if (WARN_ON_ONCE(!file_ser)) { | |
| 472 | + err = -ENOSPC; | |
| 473 | + goto err_unfreeze; | |
| 474 | + } | |
| 475 | + | |
| 509 | 476 | err = luo_file_freeze_one(file_set, luo_file); |
| 510 | 477 | if (err < 0) { |
| 511 | 478 | pr_warn("Freeze failed for token[%#0llx] handler[%s] err[%pe]\n", |
| @@ -514,16 +481,14 @@ int luo_file_freeze(struct luo_file_set *file_set, · 2 unmodified lines | ||
| 514 | 481 | goto err_unfreeze; |
| 515 | 482 | } |
| 516 | 483 | |
| 517 | − strscpy(file_ser[i].compatible, luo_file->fh->compatible, | |
| 518 | − sizeof(file_ser[i].compatible)); | |
| 519 | − file_ser[i].data = luo_file->serialized_data; | |
| 520 | − file_ser[i].token = luo_file->token; | |
| 521 | − i++; | |
| 484 | + strscpy(file_ser->compatible, luo_file->fh->compatible, | |
| 485 | + sizeof(file_ser->compatible)); | |
| 486 | + file_ser->data = luo_file->serialized_data; | |
| 487 | + file_ser->token = luo_file->token; | |
| 522 | 488 | } |
| 523 | 489 | |
| 524 | 490 | file_set_ser->count = file_set->count; |
| 525 | − if (file_set->files) | |
| 526 | − file_set_ser->files = virt_to_phys(file_set->files); | |
| 491 | + file_set_ser->files = kho_block_set_head_pa(&file_set->block_set); | |
| 527 | 492 | |
| 528 | 493 | return 0; |
| 529 | 494 | |
| @@ -741,14 +706,12 @@ int luo_file_finish(struct luo_file_set *file_set) · 211 unmodified lines | ||
| 741 | 706 | module_put(luo_file->fh->ops->owner); |
| 742 | 707 | list_del(&luo_file->list); |
| 743 | 708 | file_set->count--; |
| 709 | + kho_block_set_shrink(&file_set->block_set, file_set->count); | |
| 744 | 710 | mutex_destroy(&luo_file->mutex); |
| 745 | 711 | kfree(luo_file); |
| 746 | 712 | } |
| 747 | 713 | |
| 748 | − if (file_set->files) { | |
| 749 | − kho_restore_free(file_set->files); | |
| 750 | − file_set->files = NULL; | |
| 751 | − } | |
| 714 | + kho_block_set_destroy(&file_set->block_set); | |
| 752 | 715 | |
| 753 | 716 | return 0; |
| 754 | 717 | } |
| @@ -822,16 +785,18 @@ int luo_file_deserialize(struct luo_file_set *file_set, · 67 unmodified lines | ||
| 822 | 785 | struct luo_file_set_ser *file_set_ser) |
| 823 | 786 | { |
| 824 | 787 | struct luo_file_ser *file_ser; |
| 788 | + struct kho_block_set_it it; | |
| 825 | 789 | int err; |
| 826 | − u64 i; | |
| 827 | 790 | |
| 828 | 791 | if (!file_set_ser->files) { |
| 829 | 792 | WARN_ON(file_set_ser->count); |
| 830 | 793 | return 0; |
| 831 | 794 | } |
| 832 | 795 | |
| 833 | − file_set->count = file_set_ser->count; | |
| 834 | − file_set->files = phys_to_virt(file_set_ser->files); | |
| 796 | + file_set->count = 0; | |
| 797 | + err = kho_block_set_restore(&file_set->block_set, file_set_ser->files); | |
| 798 | + if (err) | |
| 799 | + return err; | |
| 835 | 800 | |
| 836 | 801 | /* |
| 837 | 802 | * Note on error handling: |
| @@ -848,25 +813,50 @@ int luo_file_deserialize(struct luo_file_set *file_set, · 10 unmodified lines | ||
| 848 | 813 | * userspace to detect the failure and trigger a reboot, which will |
| 849 | 814 | * reliably reset devices and reclaim memory. |
| 850 | 815 | */ |
| 851 | − file_ser = file_set->files; | |
| 852 | − for (i = 0; i < file_set->count; i++) { | |
| 853 | − err = luo_file_deserialize_one(file_set, &file_ser[i]); | |
| 816 | + kho_block_set_it_init(&it, &file_set->block_set); | |
| 817 | + while ((file_ser = kho_block_set_it_read_entry(&it))) { | |
| 818 | + err = luo_file_deserialize_one(file_set, file_ser); | |
| 854 | 819 | if (err) |
| 855 | − return err; | |
| 820 | + goto err_destroy_blocks; | |
| 821 | + file_set->count++; | |
| 822 | + } | |
| 823 | + | |
| 824 | + if (file_set->count != file_set_ser->count) { | |
| 825 | + pr_warn("File count mismatch: expected %llu, found %llu\n", | |
| 826 | + file_set_ser->count, file_set->count); | |
| 827 | + err = -EINVAL; | |
| 828 | + goto err_destroy_blocks; | |
| 856 | 829 | } |
| 857 | 830 | |
| 858 | 831 | return 0; |
| 832 | + | |
| 833 | +err_destroy_blocks: | |
| 834 | + while (!list_empty(&file_set->files_list)) { | |
| 835 | + struct luo_file *luo_file; | |
| 836 | + | |
| 837 | + luo_file = list_first_entry(&file_set->files_list, | |
| 838 | + struct luo_file, list); | |
| 839 | + list_del(&luo_file->list); | |
| 840 | + module_put(luo_file->fh->ops->owner); | |
| 841 | + mutex_destroy(&luo_file->mutex); | |
| 842 | + kfree(luo_file); | |
| 843 | + } | |
| 844 | + file_set->count = 0; | |
| 845 | + kho_block_set_destroy(&file_set->block_set); | |
| 846 | + return err; | |
| 859 | 847 | } |
| 860 | 848 | |
| 861 | 849 | void luo_file_set_init(struct luo_file_set *file_set) |
| 862 | 850 | { |
| 863 | 851 | INIT_LIST_HEAD(&file_set->files_list); |
| 852 | + kho_block_set_init(&file_set->block_set, sizeof(struct luo_file_ser)); | |
| 864 | 853 | } |
| 865 | 854 | |
| 866 | 855 | void luo_file_set_destroy(struct luo_file_set *file_set) |
| 867 | 856 | { |
| 868 | 857 | WARN_ON(file_set->count); |
| 869 | 858 | WARN_ON(!list_empty(&file_set->files_list)); |
| 859 | + WARN_ON(!kho_block_set_is_empty(&file_set->block_set)); | |
| 870 | 860 | } |
| 871 | 861 | |
| 872 | 862 | /** |
| @@ -10,6 +10,7 @@ · 9 unmodified lines | ||
| 10 | 10 | |
| 11 | 11 | #include <linux/liveupdate.h> |
| 12 | 12 | #include <linux/uaccess.h> |
| 13 | +#include <linux/kho_block.h> | |
| 13 | 14 | |
| 14 | 15 | struct luo_ucmd { |
| 15 | 16 | void __user *ubuffer; |
| @@ -44,14 +45,13 @@ static inline int luo_ucmd_respond(struct luo_ucmd *ucmd, · 28 unmodified lines | ||
| 44 | 45 | * struct luo_file_set - A set of files that belong to the same sessions. |
| 45 | 46 | * @files_list: An ordered list of files associated with this session, it is |
| 46 | 47 | * ordered by preservation time. |
| 47 | − * @files: The physically contiguous memory block that holds the serialized | |
| 48 | − * state of files. | |
| 48 | + * @block_set: The set of serialization blocks. | |
| 49 | 49 | * @count: A counter tracking the number of files currently stored in the |
| 50 | 50 | * @files_list for this session. |
| 51 | 51 | */ |
| 52 | 52 | struct luo_file_set { |
| 53 | 53 | struct list_head files_list; |
| 54 | − struct luo_file_ser *files; | |
| 54 | + struct kho_block_set block_set; | |
| 55 | 55 | u64 count; |
| 56 | 56 | }; |
| 57 | 57 | |