list-private-test.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit compilation/smoke test for Private list primitives.
  4. *
  5. * Copyright (c) 2025, Google LLC.
  6. * Pasha Tatashin <pasha.tatashin@soleen.com>
  7. */
  8. #include <linux/list_private.h>
  9. #include <kunit/test.h>
  10. /*
  11. * This forces compiler to warn if you access it directly, because list
  12. * primitives expect (struct list_head *), not (volatile struct list_head *).
  13. */
  14. #undef __private
  15. #define __private volatile
  16. /* Redefine ACCESS_PRIVATE for this test. */
  17. #undef ACCESS_PRIVATE
  18. #define ACCESS_PRIVATE(p, member) \
  19. (*((struct list_head *)((unsigned long)&((p)->member))))
  20. struct list_test_struct {
  21. int data;
  22. struct list_head __private list;
  23. };
  24. static void list_private_compile_test(struct kunit *test)
  25. {
  26. struct list_test_struct entry;
  27. struct list_test_struct *pos, *n;
  28. LIST_HEAD(head);
  29. INIT_LIST_HEAD(&ACCESS_PRIVATE(&entry, list));
  30. list_add(&ACCESS_PRIVATE(&entry, list), &head);
  31. pos = &entry;
  32. pos = list_private_entry(&ACCESS_PRIVATE(&entry, list), struct list_test_struct, list);
  33. pos = list_private_first_entry(&head, struct list_test_struct, list);
  34. pos = list_private_last_entry(&head, struct list_test_struct, list);
  35. pos = list_private_next_entry(pos, list);
  36. pos = list_private_prev_entry(pos, list);
  37. pos = list_private_next_entry_circular(pos, &head, list);
  38. pos = list_private_prev_entry_circular(pos, &head, list);
  39. if (list_private_entry_is_head(pos, &head, list))
  40. return;
  41. list_private_for_each_entry(pos, &head, list) { }
  42. list_private_for_each_entry_reverse(pos, &head, list) { }
  43. list_private_for_each_entry_continue(pos, &head, list) { }
  44. list_private_for_each_entry_continue_reverse(pos, &head, list) { }
  45. list_private_for_each_entry_from(pos, &head, list) { }
  46. list_private_for_each_entry_from_reverse(pos, &head, list) { }
  47. list_private_for_each_entry_safe(pos, n, &head, list)
  48. list_private_safe_reset_next(pos, n, list);
  49. list_private_for_each_entry_safe_continue(pos, n, &head, list) { }
  50. list_private_for_each_entry_safe_from(pos, n, &head, list) { }
  51. list_private_for_each_entry_safe_reverse(pos, n, &head, list) { }
  52. }
  53. static struct kunit_case list_private_test_cases[] = {
  54. KUNIT_CASE(list_private_compile_test),
  55. {},
  56. };
  57. static struct kunit_suite list_private_test_module = {
  58. .name = "list-private-kunit-test",
  59. .test_cases = list_private_test_cases,
  60. };
  61. kunit_test_suite(list_private_test_module);
  62. MODULE_DESCRIPTION("KUnit compilation test for private list primitives");
  63. MODULE_LICENSE("GPL");