History

liveupdate: Remove limit on the number of files per session

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.

Pasha Tatashin committed 5 months ago

Commit 1d115301 parent 2a441a13 files · +74 −83Browse files

Showing the changes to kernel (2 of 3 files). Show all

Unified · Split

2 files
  1. kernel/liveupdate/luo_file.c +64 −74
  2. kernel/liveupdate/luo_internal.h +3 −3
kernel/liveupdate/luo_file.c+64−74
@@ -118,11 +118,6 @@ static LIST_HEAD(luo_file_handler_list); · 117 unmodified lines
118118 /* Keep track of files being preserved by LUO */
119119 static DEFINE_XARRAY(luo_preserved_files);
120120
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−
126121 /**
127122 * struct luo_file - Represents a single preserved file instance.
128123 * @fh: Pointer to the &struct liveupdate_file_handler that manages
@@ -174,39 +169,6 @@ struct luo_file { · 45 unmodified lines
174169 u64 token;
175170 };
176171
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−
210172 static unsigned long luo_get_id(struct liveupdate_file_handler *fh,
211173 struct file *file)
212174 {
@@ -276,16 +238,15 @@ int luo_preserve_file(struct luo_file_set *file_set, u64 token, int fd) · 63 unmodified lines
276238 if (luo_token_is_used(file_set, token))
277239 return -EEXIST;
278240
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;
281244
282245 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+ }
289250
290251 err = -ENOENT;
291252 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
300261
301262 /* err is still -ENOENT if no handler was found */
302263 if (err)
303− goto err_free_files_mem;
264+ goto err_fput;
304265
305266 err = xa_insert(&luo_preserved_files, luo_get_id(fh, file),
306267 file, GFP_KERNEL);
@@ -343,10 +304,10 @@ err_erase_xa: · 36 unmodified lines
343304 xa_erase(&luo_preserved_files, luo_get_id(fh, file));
344305 err_module_put:
345306 module_put(fh->ops->owner);
346−err_free_files_mem:
347− luo_free_files_mem(file_set);
348307 err_fput:
349308 fput(file);
309+err_shrink:
310+ kho_block_set_shrink(&file_set->block_set, file_set->count);
350311
351312 return err;
352313 }
@@ -392,13 +353,14 @@ void luo_file_unpreserve_files(struct luo_file_set *file_set) · 39 unmodified lines
392353
393354 list_del(&luo_file->list);
394355 file_set->count--;
356+ kho_block_set_shrink(&file_set->block_set, file_set->count);
395357
396358 fput(luo_file->file);
397359 mutex_destroy(&luo_file->mutex);
398360 kfree(luo_file);
399361 }
400362
401− luo_free_files_mem(file_set);
363+ kho_block_set_destroy(&file_set->block_set);
402364 }
403365
404366 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
454416 luo_file_unfreeze_one(file_set, luo_file);
455417 }
456418
457− memset(file_set->files, 0, LUO_FILE_PGCNT << PAGE_SHIFT);
419+ kho_block_set_clear(&file_set->block_set);
458420 }
459421
460422 /**
@@ -493,19 +455,24 @@ static void __luo_file_unfreeze(struct luo_file_set *file_set, · 32 unmodified lines
493455 int luo_file_freeze(struct luo_file_set *file_set,
494456 struct luo_file_set_ser *file_set_ser)
495457 {
496− struct luo_file_ser *file_ser = file_set->files;
497458 struct luo_file *luo_file;
459+ struct kho_block_set_it it;
498460 int err;
499− int i;
500461
501462 if (!file_set->count)
502463 return 0;
503464
504− if (WARN_ON(!file_ser))
505− return -EINVAL;
465+ kho_block_set_it_init(&it, &file_set->block_set);
506466
507− i = 0;
508467 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+
509476 err = luo_file_freeze_one(file_set, luo_file);
510477 if (err < 0) {
511478 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
514481 goto err_unfreeze;
515482 }
516483
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;
522488 }
523489
524490 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);
527492
528493 return 0;
529494
@@ -741,14 +706,12 @@ int luo_file_finish(struct luo_file_set *file_set) · 211 unmodified lines
741706 module_put(luo_file->fh->ops->owner);
742707 list_del(&luo_file->list);
743708 file_set->count--;
709+ kho_block_set_shrink(&file_set->block_set, file_set->count);
744710 mutex_destroy(&luo_file->mutex);
745711 kfree(luo_file);
746712 }
747713
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);
752715
753716 return 0;
754717 }
@@ -822,16 +785,18 @@ int luo_file_deserialize(struct luo_file_set *file_set, · 67 unmodified lines
822785 struct luo_file_set_ser *file_set_ser)
823786 {
824787 struct luo_file_ser *file_ser;
788+ struct kho_block_set_it it;
825789 int err;
826− u64 i;
827790
828791 if (!file_set_ser->files) {
829792 WARN_ON(file_set_ser->count);
830793 return 0;
831794 }
832795
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;
835800
836801 /*
837802 * Note on error handling:
@@ -848,25 +813,50 @@ int luo_file_deserialize(struct luo_file_set *file_set, · 10 unmodified lines
848813 * userspace to detect the failure and trigger a reboot, which will
849814 * reliably reset devices and reclaim memory.
850815 */
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);
854819 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;
856829 }
857830
858831 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;
859847 }
860848
861849 void luo_file_set_init(struct luo_file_set *file_set)
862850 {
863851 INIT_LIST_HEAD(&file_set->files_list);
852+ kho_block_set_init(&file_set->block_set, sizeof(struct luo_file_ser));
864853 }
865854
866855 void luo_file_set_destroy(struct luo_file_set *file_set)
867856 {
868857 WARN_ON(file_set->count);
869858 WARN_ON(!list_empty(&file_set->files_list));
859+ WARN_ON(!kho_block_set_is_empty(&file_set->block_set));
870860 }
871861
872862 /**
kernel/liveupdate/luo_internal.h+3−3
@@ -10,6 +10,7 @@ · 9 unmodified lines
1010
1111 #include <linux/liveupdate.h>
1212 #include <linux/uaccess.h>
13+#include <linux/kho_block.h>
1314
1415 struct luo_ucmd {
1516 void __user *ubuffer;
@@ -44,14 +45,13 @@ static inline int luo_ucmd_respond(struct luo_ucmd *ucmd, · 28 unmodified lines
4445 * struct luo_file_set - A set of files that belong to the same sessions.
4546 * @files_list: An ordered list of files associated with this session, it is
4647 * 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.
4949 * @count: A counter tracking the number of files currently stored in the
5050 * @files_list for this session.
5151 */
5252 struct luo_file_set {
5353 struct list_head files_list;
54− struct luo_file_ser *files;
54+ struct kho_block_set block_set;
5555 u64 count;
5656 };
5757

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t