1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2018 Mellanox Technologies. All rights reserved */
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <linux/random.h>
10 #include <linux/objagg.h>
18 static int key_id_index(unsigned int key_id)
20 if (key_id >= NUM_KEYS) {
30 unsigned int root_count;
31 unsigned int delta_count;
32 char next_root_buf[BUF_LEN];
33 struct objagg_obj *objagg_objs[NUM_KEYS];
34 unsigned int key_refs[NUM_KEYS];
43 unsigned int key_id_diff;
46 static struct objagg_obj *world_obj_get(struct world *world,
47 struct objagg *objagg,
50 struct objagg_obj *objagg_obj;
55 objagg_obj = objagg_obj_get(objagg, &key);
56 if (IS_ERR(objagg_obj)) {
57 pr_err("Key %u: Failed to get object.\n", key_id);
60 if (!world->key_refs[key_id_index(key_id)]) {
61 world->objagg_objs[key_id_index(key_id)] = objagg_obj;
62 } else if (world->objagg_objs[key_id_index(key_id)] != objagg_obj) {
63 pr_err("Key %u: God another object for the same key.\n",
66 goto err_key_id_check;
68 world->key_refs[key_id_index(key_id)]++;
72 objagg_obj_put(objagg, objagg_obj);
76 static void world_obj_put(struct world *world, struct objagg *objagg,
79 struct objagg_obj *objagg_obj;
81 if (!world->key_refs[key_id_index(key_id)])
83 objagg_obj = world->objagg_objs[key_id_index(key_id)];
84 objagg_obj_put(objagg, objagg_obj);
85 world->key_refs[key_id_index(key_id)]--;
88 #define MAX_KEY_ID_DIFF 5
90 static bool delta_check(void *priv, const void *parent_obj, const void *obj)
92 const struct tokey *parent_key = parent_obj;
93 const struct tokey *key = obj;
94 int diff = key->id - parent_key->id;
96 return diff >= 0 && diff <= MAX_KEY_ID_DIFF;
99 static void *delta_create(void *priv, void *parent_obj, void *obj)
101 struct tokey *parent_key = parent_obj;
102 struct world *world = priv;
103 struct tokey *key = obj;
104 int diff = key->id - parent_key->id;
107 if (!delta_check(priv, parent_obj, obj))
108 return ERR_PTR(-EINVAL);
110 delta = kzalloc(sizeof(*delta), GFP_KERNEL);
112 return ERR_PTR(-ENOMEM);
113 delta->key_id_diff = diff;
114 world->delta_count++;
118 static void delta_destroy(void *priv, void *delta_priv)
120 struct delta *delta = delta_priv;
121 struct world *world = priv;
123 world->delta_count--;
127 static void *root_create(void *priv, void *obj, unsigned int id)
129 struct world *world = priv;
130 struct tokey *key = obj;
133 root = kzalloc(sizeof(*root), GFP_KERNEL);
135 return ERR_PTR(-ENOMEM);
136 memcpy(&root->key, key, sizeof(root->key));
137 memcpy(root->buf, world->next_root_buf, sizeof(root->buf));
142 static void root_destroy(void *priv, void *root_priv)
144 struct root *root = root_priv;
145 struct world *world = priv;
151 static int test_nodelta_obj_get(struct world *world, struct objagg *objagg,
152 unsigned int key_id, bool should_create_root)
154 unsigned int orig_root_count = world->root_count;
155 struct objagg_obj *objagg_obj;
156 const struct root *root;
159 if (should_create_root)
160 prandom_bytes(world->next_root_buf,
161 sizeof(world->next_root_buf));
163 objagg_obj = world_obj_get(world, objagg, key_id);
164 if (IS_ERR(objagg_obj)) {
165 pr_err("Key %u: Failed to get object.\n", key_id);
166 return PTR_ERR(objagg_obj);
168 if (should_create_root) {
169 if (world->root_count != orig_root_count + 1) {
170 pr_err("Key %u: Root was not created\n", key_id);
172 goto err_check_root_count;
175 if (world->root_count != orig_root_count) {
176 pr_err("Key %u: Root was incorrectly created\n",
179 goto err_check_root_count;
182 root = objagg_obj_root_priv(objagg_obj);
183 if (root->key.id != key_id) {
184 pr_err("Key %u: Root has unexpected key id\n", key_id);
186 goto err_check_key_id;
188 if (should_create_root &&
189 memcmp(world->next_root_buf, root->buf, sizeof(root->buf))) {
190 pr_err("Key %u: Buffer does not match the expected content\n",
199 err_check_root_count:
200 objagg_obj_put(objagg, objagg_obj);
204 static int test_nodelta_obj_put(struct world *world, struct objagg *objagg,
205 unsigned int key_id, bool should_destroy_root)
207 unsigned int orig_root_count = world->root_count;
209 world_obj_put(world, objagg, key_id);
211 if (should_destroy_root) {
212 if (world->root_count != orig_root_count - 1) {
213 pr_err("Key %u: Root was not destroyed\n", key_id);
217 if (world->root_count != orig_root_count) {
218 pr_err("Key %u: Root was incorrectly destroyed\n",
226 static int check_stats_zero(struct objagg *objagg)
228 const struct objagg_stats *stats;
231 stats = objagg_stats_get(objagg);
233 return PTR_ERR(stats);
235 if (stats->stats_info_count != 0) {
236 pr_err("Stats: Object count is not zero while it should be\n");
240 objagg_stats_put(stats);
244 static int check_stats_nodelta(struct objagg *objagg)
246 const struct objagg_stats *stats;
250 stats = objagg_stats_get(objagg);
252 return PTR_ERR(stats);
254 if (stats->stats_info_count != NUM_KEYS) {
255 pr_err("Stats: Unexpected object count (%u expected, %u returned)\n",
256 NUM_KEYS, stats->stats_info_count);
261 for (i = 0; i < stats->stats_info_count; i++) {
262 if (stats->stats_info[i].stats.user_count != 2) {
263 pr_err("Stats: incorrect user count\n");
267 if (stats->stats_info[i].stats.delta_user_count != 2) {
268 pr_err("Stats: incorrect delta user count\n");
276 objagg_stats_put(stats);
280 static bool delta_check_dummy(void *priv, const void *parent_obj,
286 static void *delta_create_dummy(void *priv, void *parent_obj, void *obj)
288 return ERR_PTR(-EOPNOTSUPP);
291 static void delta_destroy_dummy(void *priv, void *delta_priv)
295 static const struct objagg_ops nodelta_ops = {
296 .obj_size = sizeof(struct tokey),
297 .delta_check = delta_check_dummy,
298 .delta_create = delta_create_dummy,
299 .delta_destroy = delta_destroy_dummy,
300 .root_create = root_create,
301 .root_destroy = root_destroy,
304 static int test_nodelta(void)
306 struct world world = {};
307 struct objagg *objagg;
311 objagg = objagg_create(&nodelta_ops, NULL, &world);
313 return PTR_ERR(objagg);
315 err = check_stats_zero(objagg);
317 goto err_stats_first_zero;
319 /* First round of gets, the root objects should be created */
320 for (i = 0; i < NUM_KEYS; i++) {
321 err = test_nodelta_obj_get(&world, objagg, i, true);
323 goto err_obj_first_get;
326 /* Do the second round of gets, all roots are already created,
327 * make sure that no new root is created
329 for (i = 0; i < NUM_KEYS; i++) {
330 err = test_nodelta_obj_get(&world, objagg, i, false);
332 goto err_obj_second_get;
335 err = check_stats_nodelta(objagg);
337 goto err_stats_nodelta;
339 for (i = NUM_KEYS - 1; i >= 0; i--) {
340 err = test_nodelta_obj_put(&world, objagg, i, false);
342 goto err_obj_first_put;
344 for (i = NUM_KEYS - 1; i >= 0; i--) {
345 err = test_nodelta_obj_put(&world, objagg, i, true);
347 goto err_obj_second_put;
350 err = check_stats_zero(objagg);
352 goto err_stats_second_zero;
354 objagg_destroy(objagg);
360 for (i--; i >= 0; i--)
361 world_obj_put(&world, objagg, i);
366 for (i--; i >= 0; i--)
367 world_obj_put(&world, objagg, i);
368 err_stats_first_zero:
369 err_stats_second_zero:
370 objagg_destroy(objagg);
374 static const struct objagg_ops delta_ops = {
375 .obj_size = sizeof(struct tokey),
376 .delta_check = delta_check,
377 .delta_create = delta_create,
378 .delta_destroy = delta_destroy,
379 .root_create = root_create,
380 .root_destroy = root_destroy,
400 struct expect_stats_info {
401 struct objagg_obj_stats stats;
406 struct expect_stats {
407 unsigned int info_count;
408 struct expect_stats_info info[NUM_KEYS];
414 enum expect_delta expect_delta;
415 enum expect_root expect_root;
416 struct expect_stats expect_stats;
419 #define EXPECT_STATS(count, ...) \
421 .info_count = count, \
422 .info = { __VA_ARGS__ } \
425 #define ROOT(key_id, user_count, delta_user_count) \
426 {{user_count, delta_user_count}, true, key_id}
428 #define DELTA(key_id, user_count) \
429 {{user_count, user_count}, false, key_id}
431 static const struct action_item action_items[] = {
433 1, ACTION_GET, EXPECT_DELTA_SAME, EXPECT_ROOT_INC,
434 EXPECT_STATS(1, ROOT(1, 1, 1)),
437 7, ACTION_GET, EXPECT_DELTA_SAME, EXPECT_ROOT_INC,
438 EXPECT_STATS(2, ROOT(1, 1, 1), ROOT(7, 1, 1)),
441 3, ACTION_GET, EXPECT_DELTA_INC, EXPECT_ROOT_SAME,
442 EXPECT_STATS(3, ROOT(1, 1, 2), ROOT(7, 1, 1),
444 }, /* r: 1, 7 d: 3^1 */
446 5, ACTION_GET, EXPECT_DELTA_INC, EXPECT_ROOT_SAME,
447 EXPECT_STATS(4, ROOT(1, 1, 3), ROOT(7, 1, 1),
448 DELTA(3, 1), DELTA(5, 1)),
449 }, /* r: 1, 7 d: 3^1, 5^1 */
451 3, ACTION_GET, EXPECT_DELTA_SAME, EXPECT_ROOT_SAME,
452 EXPECT_STATS(4, ROOT(1, 1, 4), ROOT(7, 1, 1),
453 DELTA(3, 2), DELTA(5, 1)),
454 }, /* r: 1, 7 d: 3^1, 3^1, 5^1 */
456 1, ACTION_GET, EXPECT_DELTA_SAME, EXPECT_ROOT_SAME,
457 EXPECT_STATS(4, ROOT(1, 2, 5), ROOT(7, 1, 1),
458 DELTA(3, 2), DELTA(5, 1)),
459 }, /* r: 1, 1, 7 d: 3^1, 3^1, 5^1 */
461 30, ACTION_GET, EXPECT_DELTA_SAME, EXPECT_ROOT_INC,
462 EXPECT_STATS(5, ROOT(1, 2, 5), ROOT(7, 1, 1), ROOT(30, 1, 1),
463 DELTA(3, 2), DELTA(5, 1)),
464 }, /* r: 1, 1, 7, 30 d: 3^1, 3^1, 5^1 */
466 8, ACTION_GET, EXPECT_DELTA_INC, EXPECT_ROOT_SAME,
467 EXPECT_STATS(6, ROOT(1, 2, 5), ROOT(7, 1, 2), ROOT(30, 1, 1),
468 DELTA(3, 2), DELTA(5, 1), DELTA(8, 1)),
469 }, /* r: 1, 1, 7, 30 d: 3^1, 3^1, 5^1, 8^7 */
471 8, ACTION_GET, EXPECT_DELTA_SAME, EXPECT_ROOT_SAME,
472 EXPECT_STATS(6, ROOT(1, 2, 5), ROOT(7, 1, 3), ROOT(30, 1, 1),
473 DELTA(3, 2), DELTA(8, 2), DELTA(5, 1)),
474 }, /* r: 1, 1, 7, 30 d: 3^1, 3^1, 5^1, 8^7, 8^7 */
476 3, ACTION_PUT, EXPECT_DELTA_SAME, EXPECT_ROOT_SAME,
477 EXPECT_STATS(6, ROOT(1, 2, 4), ROOT(7, 1, 3), ROOT(30, 1, 1),
478 DELTA(8, 2), DELTA(3, 1), DELTA(5, 1)),
479 }, /* r: 1, 1, 7, 30 d: 3^1, 5^1, 8^7, 8^7 */
481 3, ACTION_PUT, EXPECT_DELTA_DEC, EXPECT_ROOT_SAME,
482 EXPECT_STATS(5, ROOT(1, 2, 3), ROOT(7, 1, 3), ROOT(30, 1, 1),
483 DELTA(8, 2), DELTA(5, 1)),
484 }, /* r: 1, 1, 7, 30 d: 5^1, 8^7, 8^7 */
486 1, ACTION_PUT, EXPECT_DELTA_SAME, EXPECT_ROOT_SAME,
487 EXPECT_STATS(5, ROOT(7, 1, 3), ROOT(1, 1, 2), ROOT(30, 1, 1),
488 DELTA(8, 2), DELTA(5, 1)),
489 }, /* r: 1, 7, 30 d: 5^1, 8^7, 8^7 */
491 1, ACTION_PUT, EXPECT_DELTA_SAME, EXPECT_ROOT_SAME,
492 EXPECT_STATS(5, ROOT(7, 1, 3), ROOT(30, 1, 1), ROOT(1, 0, 1),
493 DELTA(8, 2), DELTA(5, 1)),
494 }, /* r: 7, 30 d: 5^1, 8^7, 8^7 */
496 5, ACTION_PUT, EXPECT_DELTA_DEC, EXPECT_ROOT_DEC,
497 EXPECT_STATS(3, ROOT(7, 1, 3), ROOT(30, 1, 1),
499 }, /* r: 7, 30 d: 8^7, 8^7 */
501 5, ACTION_GET, EXPECT_DELTA_SAME, EXPECT_ROOT_INC,
502 EXPECT_STATS(4, ROOT(7, 1, 3), ROOT(30, 1, 1), ROOT(5, 1, 1),
504 }, /* r: 7, 30, 5 d: 8^7, 8^7 */
506 6, ACTION_GET, EXPECT_DELTA_INC, EXPECT_ROOT_SAME,
507 EXPECT_STATS(5, ROOT(7, 1, 3), ROOT(5, 1, 2), ROOT(30, 1, 1),
508 DELTA(8, 2), DELTA(6, 1)),
509 }, /* r: 7, 30, 5 d: 8^7, 8^7, 6^5 */
511 8, ACTION_GET, EXPECT_DELTA_SAME, EXPECT_ROOT_SAME,
512 EXPECT_STATS(5, ROOT(7, 1, 4), ROOT(5, 1, 2), ROOT(30, 1, 1),
513 DELTA(8, 3), DELTA(6, 1)),
514 }, /* r: 7, 30, 5 d: 8^7, 8^7, 8^7, 6^5 */
516 8, ACTION_PUT, EXPECT_DELTA_SAME, EXPECT_ROOT_SAME,
517 EXPECT_STATS(5, ROOT(7, 1, 3), ROOT(5, 1, 2), ROOT(30, 1, 1),
518 DELTA(8, 2), DELTA(6, 1)),
519 }, /* r: 7, 30, 5 d: 8^7, 8^7, 6^5 */
521 8, ACTION_PUT, EXPECT_DELTA_SAME, EXPECT_ROOT_SAME,
522 EXPECT_STATS(5, ROOT(7, 1, 2), ROOT(5, 1, 2), ROOT(30, 1, 1),
523 DELTA(8, 1), DELTA(6, 1)),
524 }, /* r: 7, 30, 5 d: 8^7, 6^5 */
526 8, ACTION_PUT, EXPECT_DELTA_DEC, EXPECT_ROOT_SAME,
527 EXPECT_STATS(4, ROOT(5, 1, 2), ROOT(7, 1, 1), ROOT(30, 1, 1),
529 }, /* r: 7, 30, 5 d: 6^5 */
531 8, ACTION_GET, EXPECT_DELTA_INC, EXPECT_ROOT_SAME,
532 EXPECT_STATS(5, ROOT(5, 1, 3), ROOT(7, 1, 1), ROOT(30, 1, 1),
533 DELTA(6, 1), DELTA(8, 1)),
534 }, /* r: 7, 30, 5 d: 6^5, 8^5 */
536 7, ACTION_PUT, EXPECT_DELTA_SAME, EXPECT_ROOT_DEC,
537 EXPECT_STATS(4, ROOT(5, 1, 3), ROOT(30, 1, 1),
538 DELTA(6, 1), DELTA(8, 1)),
539 }, /* r: 30, 5 d: 6^5, 8^5 */
541 30, ACTION_PUT, EXPECT_DELTA_SAME, EXPECT_ROOT_DEC,
542 EXPECT_STATS(3, ROOT(5, 1, 3),
543 DELTA(6, 1), DELTA(8, 1)),
544 }, /* r: 5 d: 6^5, 8^5 */
546 5, ACTION_PUT, EXPECT_DELTA_SAME, EXPECT_ROOT_SAME,
547 EXPECT_STATS(3, ROOT(5, 0, 2),
548 DELTA(6, 1), DELTA(8, 1)),
549 }, /* r: d: 6^5, 8^5 */
551 6, ACTION_PUT, EXPECT_DELTA_DEC, EXPECT_ROOT_SAME,
552 EXPECT_STATS(2, ROOT(5, 0, 1),
556 8, ACTION_PUT, EXPECT_DELTA_DEC, EXPECT_ROOT_DEC,
561 static int check_expect(struct world *world,
562 const struct action_item *action_item,
563 unsigned int orig_delta_count,
564 unsigned int orig_root_count)
566 unsigned int key_id = action_item->key_id;
568 switch (action_item->expect_delta) {
569 case EXPECT_DELTA_SAME:
570 if (orig_delta_count != world->delta_count) {
571 pr_err("Key %u: Delta count changed while expected to remain the same.\n",
576 case EXPECT_DELTA_INC:
577 if (WARN_ON(action_item->action == ACTION_PUT))
579 if (orig_delta_count + 1 != world->delta_count) {
580 pr_err("Key %u: Delta count was not incremented.\n",
585 case EXPECT_DELTA_DEC:
586 if (WARN_ON(action_item->action == ACTION_GET))
588 if (orig_delta_count - 1 != world->delta_count) {
589 pr_err("Key %u: Delta count was not decremented.\n",
596 switch (action_item->expect_root) {
597 case EXPECT_ROOT_SAME:
598 if (orig_root_count != world->root_count) {
599 pr_err("Key %u: Root count changed while expected to remain the same.\n",
604 case EXPECT_ROOT_INC:
605 if (WARN_ON(action_item->action == ACTION_PUT))
607 if (orig_root_count + 1 != world->root_count) {
608 pr_err("Key %u: Root count was not incremented.\n",
613 case EXPECT_ROOT_DEC:
614 if (WARN_ON(action_item->action == ACTION_GET))
616 if (orig_root_count - 1 != world->root_count) {
617 pr_err("Key %u: Root count was not decremented.\n",
626 static unsigned int obj_to_key_id(struct objagg_obj *objagg_obj)
628 const struct tokey *root_key;
629 const struct delta *delta;
632 root_key = objagg_obj_root_priv(objagg_obj);
633 key_id = root_key->id;
634 delta = objagg_obj_delta_priv(objagg_obj);
636 key_id += delta->key_id_diff;
641 check_expect_stats_nums(const struct objagg_obj_stats_info *stats_info,
642 const struct expect_stats_info *expect_stats_info,
645 if (stats_info->is_root != expect_stats_info->is_root) {
647 *errmsg = "Incorrect root/delta indication";
650 if (stats_info->stats.user_count !=
651 expect_stats_info->stats.user_count) {
653 *errmsg = "Incorrect user count";
656 if (stats_info->stats.delta_user_count !=
657 expect_stats_info->stats.delta_user_count) {
659 *errmsg = "Incorrect delta user count";
666 check_expect_stats_key_id(const struct objagg_obj_stats_info *stats_info,
667 const struct expect_stats_info *expect_stats_info,
670 if (obj_to_key_id(stats_info->objagg_obj) !=
671 expect_stats_info->key_id) {
673 *errmsg = "incorrect key id";
679 static int check_expect_stats_neigh(const struct objagg_stats *stats,
680 const struct expect_stats *expect_stats,
686 for (i = pos - 1; i >= 0; i--) {
687 err = check_expect_stats_nums(&stats->stats_info[i],
688 &expect_stats->info[pos], NULL);
691 err = check_expect_stats_key_id(&stats->stats_info[i],
692 &expect_stats->info[pos], NULL);
696 for (i = pos + 1; i < stats->stats_info_count; i++) {
697 err = check_expect_stats_nums(&stats->stats_info[i],
698 &expect_stats->info[pos], NULL);
701 err = check_expect_stats_key_id(&stats->stats_info[i],
702 &expect_stats->info[pos], NULL);
709 static int __check_expect_stats(const struct objagg_stats *stats,
710 const struct expect_stats *expect_stats,
716 if (stats->stats_info_count != expect_stats->info_count) {
717 *errmsg = "Unexpected object count";
721 for (i = 0; i < stats->stats_info_count; i++) {
722 err = check_expect_stats_nums(&stats->stats_info[i],
723 &expect_stats->info[i], errmsg);
726 err = check_expect_stats_key_id(&stats->stats_info[i],
727 &expect_stats->info[i], errmsg);
729 /* It is possible that one of the neighbor stats with
730 * same numbers have the correct key id, so check it
732 err = check_expect_stats_neigh(stats, expect_stats, i);
740 static int check_expect_stats(struct objagg *objagg,
741 const struct expect_stats *expect_stats,
744 const struct objagg_stats *stats;
747 stats = objagg_stats_get(objagg);
749 *errmsg = "objagg_stats_get() failed.";
750 return PTR_ERR(stats);
752 err = __check_expect_stats(stats, expect_stats, errmsg);
753 objagg_stats_put(stats);
757 static int test_delta_action_item(struct world *world,
758 struct objagg *objagg,
759 const struct action_item *action_item,
762 unsigned int orig_delta_count = world->delta_count;
763 unsigned int orig_root_count = world->root_count;
764 unsigned int key_id = action_item->key_id;
765 enum action action = action_item->action;
766 struct objagg_obj *objagg_obj;
771 action = action == ACTION_GET ? ACTION_PUT : ACTION_GET;
775 objagg_obj = world_obj_get(world, objagg, key_id);
776 if (IS_ERR(objagg_obj))
777 return PTR_ERR(objagg_obj);
780 world_obj_put(world, objagg, key_id);
786 err = check_expect(world, action_item,
787 orig_delta_count, orig_root_count);
791 err = check_expect_stats(objagg, &action_item->expect_stats, &errmsg);
793 pr_err("Key %u: Stats: %s\n", action_item->key_id, errmsg);
800 /* This can only happen when action is not inversed.
801 * So in case of an error, cleanup by doing inverse action.
803 test_delta_action_item(world, objagg, action_item, true);
807 static int test_delta(void)
809 struct world world = {};
810 struct objagg *objagg;
814 objagg = objagg_create(&delta_ops, NULL, &world);
816 return PTR_ERR(objagg);
818 for (i = 0; i < ARRAY_SIZE(action_items); i++) {
819 err = test_delta_action_item(&world, objagg,
820 &action_items[i], false);
822 goto err_do_action_item;
825 objagg_destroy(objagg);
829 for (i--; i >= 0; i--)
830 test_delta_action_item(&world, objagg, &action_items[i], true);
832 objagg_destroy(objagg);
837 const unsigned int *key_ids;
838 size_t key_ids_count;
839 struct expect_stats expect_stats;
840 struct expect_stats expect_stats_hints;
843 static const unsigned int hints_case_key_ids[] = {
844 1, 7, 3, 5, 3, 1, 30, 8, 8, 5, 6, 8,
847 static const struct hints_case hints_case = {
848 .key_ids = hints_case_key_ids,
849 .key_ids_count = ARRAY_SIZE(hints_case_key_ids),
851 EXPECT_STATS(7, ROOT(1, 2, 7), ROOT(7, 1, 4), ROOT(30, 1, 1),
852 DELTA(8, 3), DELTA(3, 2),
853 DELTA(5, 2), DELTA(6, 1)),
854 .expect_stats_hints =
855 EXPECT_STATS(7, ROOT(3, 2, 9), ROOT(1, 2, 2), ROOT(30, 1, 1),
856 DELTA(8, 3), DELTA(5, 2),
857 DELTA(6, 1), DELTA(7, 1)),
860 static void __pr_debug_stats(const struct objagg_stats *stats)
864 for (i = 0; i < stats->stats_info_count; i++)
865 pr_debug("Stat index %d key %u: u %d, d %d, %s\n", i,
866 obj_to_key_id(stats->stats_info[i].objagg_obj),
867 stats->stats_info[i].stats.user_count,
868 stats->stats_info[i].stats.delta_user_count,
869 stats->stats_info[i].is_root ? "root" : "noroot");
872 static void pr_debug_stats(struct objagg *objagg)
874 const struct objagg_stats *stats;
876 stats = objagg_stats_get(objagg);
879 __pr_debug_stats(stats);
880 objagg_stats_put(stats);
883 static void pr_debug_hints_stats(struct objagg_hints *objagg_hints)
885 const struct objagg_stats *stats;
887 stats = objagg_hints_stats_get(objagg_hints);
890 __pr_debug_stats(stats);
891 objagg_stats_put(stats);
894 static int check_expect_hints_stats(struct objagg_hints *objagg_hints,
895 const struct expect_stats *expect_stats,
898 const struct objagg_stats *stats;
901 stats = objagg_hints_stats_get(objagg_hints);
903 return PTR_ERR(stats);
904 err = __check_expect_stats(stats, expect_stats, errmsg);
905 objagg_stats_put(stats);
909 static int test_hints_case(const struct hints_case *hints_case)
911 struct objagg_obj *objagg_obj;
912 struct objagg_hints *hints;
913 struct world world2 = {};
914 struct world world = {};
915 struct objagg *objagg2;
916 struct objagg *objagg;
921 objagg = objagg_create(&delta_ops, NULL, &world);
923 return PTR_ERR(objagg);
925 for (i = 0; i < hints_case->key_ids_count; i++) {
926 objagg_obj = world_obj_get(&world, objagg,
927 hints_case->key_ids[i]);
928 if (IS_ERR(objagg_obj)) {
929 err = PTR_ERR(objagg_obj);
930 goto err_world_obj_get;
934 pr_debug_stats(objagg);
935 err = check_expect_stats(objagg, &hints_case->expect_stats, &errmsg);
937 pr_err("Stats: %s\n", errmsg);
938 goto err_check_expect_stats;
941 hints = objagg_hints_get(objagg, OBJAGG_OPT_ALGO_SIMPLE_GREEDY);
943 err = PTR_ERR(hints);
947 pr_debug_hints_stats(hints);
948 err = check_expect_hints_stats(hints, &hints_case->expect_stats_hints,
951 pr_err("Hints stats: %s\n", errmsg);
952 goto err_check_expect_hints_stats;
955 objagg2 = objagg_create(&delta_ops, hints, &world2);
957 return PTR_ERR(objagg2);
959 for (i = 0; i < hints_case->key_ids_count; i++) {
960 objagg_obj = world_obj_get(&world2, objagg2,
961 hints_case->key_ids[i]);
962 if (IS_ERR(objagg_obj)) {
963 err = PTR_ERR(objagg_obj);
964 goto err_world2_obj_get;
968 pr_debug_stats(objagg2);
969 err = check_expect_stats(objagg2, &hints_case->expect_stats_hints,
972 pr_err("Stats2: %s\n", errmsg);
973 goto err_check_expect_stats2;
978 err_check_expect_stats2:
980 for (i--; i >= 0; i--)
981 world_obj_put(&world2, objagg, hints_case->key_ids[i]);
982 i = hints_case->key_ids_count;
983 objagg_destroy(objagg2);
984 err_check_expect_hints_stats:
985 objagg_hints_put(hints);
987 err_check_expect_stats:
989 for (i--; i >= 0; i--)
990 world_obj_put(&world, objagg, hints_case->key_ids[i]);
992 objagg_destroy(objagg);
995 static int test_hints(void)
997 return test_hints_case(&hints_case);
1000 static int __init test_objagg_init(void)
1004 err = test_nodelta();
1010 return test_hints();
1013 static void __exit test_objagg_exit(void)
1017 module_init(test_objagg_init);
1018 module_exit(test_objagg_exit);
1019 MODULE_LICENSE("Dual BSD/GPL");
1020 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
1021 MODULE_DESCRIPTION("Test module for objagg");