| @@ -118,11 +118,6 @@ static LIST_HEAD(luo_file_handler_list); · 117 unmodified lines |
| 118 | /* Keep track of files being preserved by LUO */ | 118 | /* Keep track of files being preserved by LUO */ |
| 119 | static DEFINE_XARRAY(luo_preserved_files); | 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 | * struct luo_file - Represents a single preserved file instance. | 122 | * struct luo_file - Represents a single preserved file instance. |
| 128 | * @fh: Pointer to the &struct liveupdate_file_handler that manages | 123 | * @fh: Pointer to the &struct liveupdate_file_handler that manages |
| @@ -174,39 +169,6 @@ struct luo_file { · 45 unmodified lines |
| 174 | u64 token; | 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 | static unsigned long luo_get_id(struct liveupdate_file_handler *fh, | 172 | static unsigned long luo_get_id(struct liveupdate_file_handler *fh, |
| 211 | struct file *file) | 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 | if (luo_token_is_used(file_set, token)) | 238 | if (luo_token_is_used(file_set, token)) |
| 277 | return -EEXIST; | 239 | return -EEXIST; |
| 278 | | 240 | |
| 279 | if (file_set->count == LUO_FILE_MAX) | 241 | err = kho_block_set_grow(&file_set->block_set, file_set->count + 1); |
| 280 | return -ENOSPC; | 242 | if (err) |
| | 243 | return err; |
| 281 | | 244 | |
| 282 | file = fget(fd); | 245 | file = fget(fd); |
| 283 | if (!file) | 246 | if (!file) { |
| 284 | return -EBADF; | 247 | err = -EBADF; |
| 285 | | 248 | goto err_shrink; |
| 286 | err = luo_alloc_files_mem(file_set); | 249 | } |
| 287 | if (err) | | |
| 288 | goto err_fput; | | |
| 289 | | 250 | |
| 290 | err = -ENOENT; | 251 | err = -ENOENT; |
| 291 | down_read(&luo_register_rwlock); | 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 | /* err is still -ENOENT if no handler was found */ | 262 | /* err is still -ENOENT if no handler was found */ |
| 302 | if (err) | 263 | if (err) |
| 303 | goto err_free_files_mem; | 264 | goto err_fput; |
| 304 | | 265 | |
| 305 | err = xa_insert(&luo_preserved_files, luo_get_id(fh, file), | 266 | err = xa_insert(&luo_preserved_files, luo_get_id(fh, file), |
| 306 | file, GFP_KERNEL); | 267 | file, GFP_KERNEL); |
| @@ -343,10 +304,10 @@ err_erase_xa: · 36 unmodified lines |
| 343 | xa_erase(&luo_preserved_files, luo_get_id(fh, file)); | 304 | xa_erase(&luo_preserved_files, luo_get_id(fh, file)); |
| 344 | err_module_put: | 305 | err_module_put: |
| 345 | module_put(fh->ops->owner); | 306 | module_put(fh->ops->owner); |
| 346 | err_free_files_mem: | | |
| 347 | luo_free_files_mem(file_set); | | |
| 348 | err_fput: | 307 | err_fput: |
| 349 | fput(file); | 308 | fput(file); |
| | 309 | err_shrink: |
| | 310 | kho_block_set_shrink(&file_set->block_set, file_set->count); |
| 350 | | 311 | |
| 351 | return err; | 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 | list_del(&luo_file->list); | 354 | list_del(&luo_file->list); |
| 394 | file_set->count--; | 355 | file_set->count--; |
| | 356 | kho_block_set_shrink(&file_set->block_set, file_set->count); |
| 395 | | 357 | |
| 396 | fput(luo_file->file); | 358 | fput(luo_file->file); |
| 397 | mutex_destroy(&luo_file->mutex); | 359 | mutex_destroy(&luo_file->mutex); |
| 398 | kfree(luo_file); | 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 | static int luo_file_freeze_one(struct luo_file_set *file_set, | 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 | luo_file_unfreeze_one(file_set, luo_file); | 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 | int luo_file_freeze(struct luo_file_set *file_set, | 455 | int luo_file_freeze(struct luo_file_set *file_set, |
| 494 | struct luo_file_set_ser *file_set_ser) | 456 | struct luo_file_set_ser *file_set_ser) |
| 495 | { | 457 | { |
| 496 | struct luo_file_ser *file_ser = file_set->files; | | |
| 497 | struct luo_file *luo_file; | 458 | struct luo_file *luo_file; |
| | 459 | struct kho_block_set_it it; |
| 498 | int err; | 460 | int err; |
| 499 | int i; | | |
| 500 | | 461 | |
| 501 | if (!file_set->count) | 462 | if (!file_set->count) |
| 502 | return 0; | 463 | return 0; |
| 503 | | 464 | |
| 504 | if (WARN_ON(!file_ser)) | 465 | kho_block_set_it_init(&it, &file_set->block_set); |
| 505 | return -EINVAL; | | |
| 506 | | 466 | |
| 507 | i = 0; | | |
| 508 | list_for_each_entry(luo_file, &file_set->files_list, list) { | 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 | err = luo_file_freeze_one(file_set, luo_file); | 476 | err = luo_file_freeze_one(file_set, luo_file); |
| 510 | if (err < 0) { | 477 | if (err < 0) { |
| 511 | pr_warn("Freeze failed for token[%#0llx] handler[%s] err[%pe]\n", | 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 | goto err_unfreeze; | 481 | goto err_unfreeze; |
| 515 | } | 482 | } |
| 516 | | 483 | |
| 517 | strscpy(file_ser[i].compatible, luo_file->fh->compatible, | 484 | strscpy(file_ser->compatible, luo_file->fh->compatible, |
| 518 | sizeof(file_ser[i].compatible)); | 485 | sizeof(file_ser->compatible)); |
| 519 | file_ser[i].data = luo_file->serialized_data; | 486 | file_ser->data = luo_file->serialized_data; |
| 520 | file_ser[i].token = luo_file->token; | 487 | file_ser->token = luo_file->token; |
| 521 | i++; | | |
| 522 | } | 488 | } |
| 523 | | 489 | |
| 524 | file_set_ser->count = file_set->count; | 490 | file_set_ser->count = file_set->count; |
| 525 | if (file_set->files) | 491 | file_set_ser->files = kho_block_set_head_pa(&file_set->block_set); |
| 526 | file_set_ser->files = virt_to_phys(file_set->files); | | |
| 527 | | 492 | |
| 528 | return 0; | 493 | return 0; |
| 529 | | 494 | |
| @@ -741,14 +706,12 @@ int luo_file_finish(struct luo_file_set *file_set) · 211 unmodified lines |
| 741 | module_put(luo_file->fh->ops->owner); | 706 | module_put(luo_file->fh->ops->owner); |
| 742 | list_del(&luo_file->list); | 707 | list_del(&luo_file->list); |
| 743 | file_set->count--; | 708 | file_set->count--; |
| | 709 | kho_block_set_shrink(&file_set->block_set, file_set->count); |
| 744 | mutex_destroy(&luo_file->mutex); | 710 | mutex_destroy(&luo_file->mutex); |
| 745 | kfree(luo_file); | 711 | kfree(luo_file); |
| 746 | } | 712 | } |
| 747 | | 713 | |
| 748 | if (file_set->files) { | 714 | kho_block_set_destroy(&file_set->block_set); |
| 749 | kho_restore_free(file_set->files); | | |
| 750 | file_set->files = NULL; | | |
| 751 | } | | |
| 752 | | 715 | |
| 753 | return 0; | 716 | return 0; |
| 754 | } | 717 | } |
| @@ -822,16 +785,18 @@ int luo_file_deserialize(struct luo_file_set *file_set, · 67 unmodified lines |
| 822 | struct luo_file_set_ser *file_set_ser) | 785 | struct luo_file_set_ser *file_set_ser) |
| 823 | { | 786 | { |
| 824 | struct luo_file_ser *file_ser; | 787 | struct luo_file_ser *file_ser; |
| | 788 | struct kho_block_set_it it; |
| 825 | int err; | 789 | int err; |
| 826 | u64 i; | | |
| 827 | | 790 | |
| 828 | if (!file_set_ser->files) { | 791 | if (!file_set_ser->files) { |
| 829 | WARN_ON(file_set_ser->count); | 792 | WARN_ON(file_set_ser->count); |
| 830 | return 0; | 793 | return 0; |
| 831 | } | 794 | } |
| 832 | | 795 | |
| 833 | file_set->count = file_set_ser->count; | 796 | file_set->count = 0; |
| 834 | file_set->files = phys_to_virt(file_set_ser->files); | 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 | * Note on error handling: | 802 | * Note on error handling: |
| @@ -848,25 +813,50 @@ int luo_file_deserialize(struct luo_file_set *file_set, · 10 unmodified lines |
| 848 | * userspace to detect the failure and trigger a reboot, which will | 813 | * userspace to detect the failure and trigger a reboot, which will |
| 849 | * reliably reset devices and reclaim memory. | 814 | * reliably reset devices and reclaim memory. |
| 850 | */ | 815 | */ |
| 851 | file_ser = file_set->files; | 816 | kho_block_set_it_init(&it, &file_set->block_set); |
| 852 | for (i = 0; i < file_set->count; i++) { | 817 | while ((file_ser = kho_block_set_it_read_entry(&it))) { |
| 853 | err = luo_file_deserialize_one(file_set, &file_ser[i]); | 818 | err = luo_file_deserialize_one(file_set, file_ser); |
| 854 | if (err) | 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 | return 0; | 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 | void luo_file_set_init(struct luo_file_set *file_set) | 849 | void luo_file_set_init(struct luo_file_set *file_set) |
| 862 | { | 850 | { |
| 863 | INIT_LIST_HEAD(&file_set->files_list); | 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 | void luo_file_set_destroy(struct luo_file_set *file_set) | 855 | void luo_file_set_destroy(struct luo_file_set *file_set) |
| 867 | { | 856 | { |
| 868 | WARN_ON(file_set->count); | 857 | WARN_ON(file_set->count); |
| 869 | WARN_ON(!list_empty(&file_set->files_list)); | 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 | /** |