liveupdate.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2025, Google LLC.
  4. * Pasha Tatashin <pasha.tatashin@soleen.com>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME " test: " fmt
  7. #include <linux/cleanup.h>
  8. #include <linux/errno.h>
  9. #include <linux/init.h>
  10. #include <linux/liveupdate.h>
  11. #include <linux/module.h>
  12. #include "../../kernel/liveupdate/luo_internal.h"
  13. static const struct liveupdate_flb_ops test_flb_ops;
  14. #define DEFINE_TEST_FLB(i) { \
  15. .ops = &test_flb_ops, \
  16. .compatible = LIVEUPDATE_TEST_FLB_COMPATIBLE(i), \
  17. }
  18. /* Number of Test FLBs to register with every file handler */
  19. #define TEST_NFLBS 3
  20. static struct liveupdate_flb test_flbs[TEST_NFLBS] = {
  21. DEFINE_TEST_FLB(0),
  22. DEFINE_TEST_FLB(1),
  23. DEFINE_TEST_FLB(2),
  24. };
  25. #define TEST_FLB_MAGIC_BASE 0xFEEDF00DCAFEBEE0ULL
  26. static int test_flb_preserve(struct liveupdate_flb_op_args *argp)
  27. {
  28. ptrdiff_t index = argp->flb - test_flbs;
  29. pr_info("%s: preserve was triggered\n", argp->flb->compatible);
  30. argp->data = TEST_FLB_MAGIC_BASE + index;
  31. return 0;
  32. }
  33. static void test_flb_unpreserve(struct liveupdate_flb_op_args *argp)
  34. {
  35. pr_info("%s: unpreserve was triggered\n", argp->flb->compatible);
  36. }
  37. static int test_flb_retrieve(struct liveupdate_flb_op_args *argp)
  38. {
  39. ptrdiff_t index = argp->flb - test_flbs;
  40. u64 expected_data = TEST_FLB_MAGIC_BASE + index;
  41. if (argp->data == expected_data) {
  42. pr_info("%s: found flb data from the previous boot\n",
  43. argp->flb->compatible);
  44. argp->obj = (void *)argp->data;
  45. } else {
  46. pr_err("%s: ERROR - incorrect data handle: %llx, expected %llx\n",
  47. argp->flb->compatible, argp->data, expected_data);
  48. return -EINVAL;
  49. }
  50. return 0;
  51. }
  52. static void test_flb_finish(struct liveupdate_flb_op_args *argp)
  53. {
  54. ptrdiff_t index = argp->flb - test_flbs;
  55. void *expected_obj = (void *)(TEST_FLB_MAGIC_BASE + index);
  56. if (argp->obj == expected_obj) {
  57. pr_info("%s: finish was triggered\n", argp->flb->compatible);
  58. } else {
  59. pr_err("%s: ERROR - finish called with invalid object\n",
  60. argp->flb->compatible);
  61. }
  62. }
  63. static const struct liveupdate_flb_ops test_flb_ops = {
  64. .preserve = test_flb_preserve,
  65. .unpreserve = test_flb_unpreserve,
  66. .retrieve = test_flb_retrieve,
  67. .finish = test_flb_finish,
  68. .owner = THIS_MODULE,
  69. };
  70. static void liveupdate_test_init(void)
  71. {
  72. static DEFINE_MUTEX(init_lock);
  73. static bool initialized;
  74. int i;
  75. guard(mutex)(&init_lock);
  76. if (initialized)
  77. return;
  78. for (i = 0; i < TEST_NFLBS; i++) {
  79. struct liveupdate_flb *flb = &test_flbs[i];
  80. void *obj;
  81. int err;
  82. err = liveupdate_flb_get_incoming(flb, &obj);
  83. if (err && err != -ENODATA && err != -ENOENT) {
  84. pr_err("liveupdate_flb_get_incoming for %s failed: %pe\n",
  85. flb->compatible, ERR_PTR(err));
  86. }
  87. }
  88. initialized = true;
  89. }
  90. void liveupdate_test_register(struct liveupdate_file_handler *fh)
  91. {
  92. int err, i;
  93. liveupdate_test_init();
  94. for (i = 0; i < TEST_NFLBS; i++) {
  95. struct liveupdate_flb *flb = &test_flbs[i];
  96. err = liveupdate_register_flb(fh, flb);
  97. if (err) {
  98. pr_err("Failed to register %s %pe\n",
  99. flb->compatible, ERR_PTR(err));
  100. }
  101. }
  102. err = liveupdate_register_flb(fh, &test_flbs[0]);
  103. if (!err || err != -EEXIST) {
  104. pr_err("Failed: %s should be already registered, but got err: %pe\n",
  105. test_flbs[0].compatible, ERR_PTR(err));
  106. }
  107. pr_info("Registered %d FLBs with file handler: [%s]\n",
  108. TEST_NFLBS, fh->compatible);
  109. }
  110. void liveupdate_test_unregister(struct liveupdate_file_handler *fh)
  111. {
  112. int err, i;
  113. for (i = 0; i < TEST_NFLBS; i++) {
  114. struct liveupdate_flb *flb = &test_flbs[i];
  115. err = liveupdate_unregister_flb(fh, flb);
  116. if (err) {
  117. pr_err("Failed to unregister %s %pe\n",
  118. flb->compatible, ERR_PTR(err));
  119. }
  120. }
  121. pr_info("Unregistered %d FLBs from file handler: [%s]\n",
  122. TEST_NFLBS, fh->compatible);
  123. }
  124. MODULE_LICENSE("GPL");
  125. MODULE_AUTHOR("Pasha Tatashin <pasha.tatashin@soleen.com>");
  126. MODULE_DESCRIPTION("In-kernel test for LUO mechanism");