test_bitops.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020 Intel Corporation
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/cleanup.h>
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/printk.h>
  10. #include <linux/slab.h>
  11. /* a tiny module only meant to test
  12. *
  13. * set/clear_bit
  14. * get_count_order/long
  15. */
  16. /* use an enum because that's the most common BITMAP usage */
  17. enum bitops_fun {
  18. BITOPS_4 = 4,
  19. BITOPS_7 = 7,
  20. BITOPS_11 = 11,
  21. BITOPS_31 = 31,
  22. BITOPS_88 = 88,
  23. BITOPS_LAST = 255,
  24. BITOPS_LENGTH = 256
  25. };
  26. static DECLARE_BITMAP(g_bitmap, BITOPS_LENGTH);
  27. static unsigned int order_comb[][2] = {
  28. {0x00000003, 2},
  29. {0x00000004, 2},
  30. {0x00001fff, 13},
  31. {0x00002000, 13},
  32. {0x50000000, 31},
  33. {0x80000000, 31},
  34. {0x80003000, 32},
  35. };
  36. #ifdef CONFIG_64BIT
  37. static unsigned long order_comb_long[][2] = {
  38. {0x0000000300000000, 34},
  39. {0x0000000400000000, 34},
  40. {0x00001fff00000000, 45},
  41. {0x0000200000000000, 45},
  42. {0x5000000000000000, 63},
  43. {0x8000000000000000, 63},
  44. {0x8000300000000000, 64},
  45. };
  46. #endif
  47. static int __init test_fns(void)
  48. {
  49. static volatile __always_used unsigned long tmp __initdata;
  50. unsigned long *buf __free(kfree) = NULL;
  51. unsigned int i, n;
  52. ktime_t time;
  53. buf = kmalloc_array(10000, sizeof(unsigned long), GFP_KERNEL);
  54. if (!buf)
  55. return -ENOMEM;
  56. get_random_bytes(buf, 10000 * sizeof(unsigned long));
  57. time = ktime_get();
  58. for (n = 0; n < BITS_PER_LONG; n++)
  59. for (i = 0; i < 10000; i++)
  60. tmp = fns(buf[i], n);
  61. time = ktime_get() - time;
  62. pr_err("fns: %18llu ns\n", time);
  63. return 0;
  64. }
  65. static int __init test_bitops_startup(void)
  66. {
  67. int i, bit_set;
  68. pr_info("Starting bitops test\n");
  69. set_bit(BITOPS_4, g_bitmap);
  70. set_bit(BITOPS_7, g_bitmap);
  71. set_bit(BITOPS_11, g_bitmap);
  72. set_bit(BITOPS_31, g_bitmap);
  73. set_bit(BITOPS_88, g_bitmap);
  74. for (i = 0; i < ARRAY_SIZE(order_comb); i++) {
  75. if (order_comb[i][1] != get_count_order(order_comb[i][0]))
  76. pr_warn("get_count_order wrong for %x\n",
  77. order_comb[i][0]);
  78. }
  79. for (i = 0; i < ARRAY_SIZE(order_comb); i++) {
  80. if (order_comb[i][1] != get_count_order_long(order_comb[i][0]))
  81. pr_warn("get_count_order_long wrong for %x\n",
  82. order_comb[i][0]);
  83. }
  84. #ifdef CONFIG_64BIT
  85. for (i = 0; i < ARRAY_SIZE(order_comb_long); i++) {
  86. if (order_comb_long[i][1] !=
  87. get_count_order_long(order_comb_long[i][0]))
  88. pr_warn("get_count_order_long wrong for %lx\n",
  89. order_comb_long[i][0]);
  90. }
  91. #endif
  92. barrier();
  93. clear_bit(BITOPS_4, g_bitmap);
  94. clear_bit(BITOPS_7, g_bitmap);
  95. clear_bit(BITOPS_11, g_bitmap);
  96. clear_bit(BITOPS_31, g_bitmap);
  97. clear_bit(BITOPS_88, g_bitmap);
  98. bit_set = find_first_bit(g_bitmap, BITOPS_LAST);
  99. if (bit_set != BITOPS_LAST)
  100. pr_err("ERROR: FOUND SET BIT %d\n", bit_set);
  101. test_fns();
  102. pr_info("Completed bitops test\n");
  103. return 0;
  104. }
  105. static void __exit test_bitops_unstartup(void)
  106. {
  107. }
  108. module_init(test_bitops_startup);
  109. module_exit(test_bitops_unstartup);
  110. MODULE_AUTHOR("Jesse Brandeburg <jesse.brandeburg@intel.com>, Wei Yang <richard.weiyang@gmail.com>");
  111. MODULE_LICENSE("GPL");
  112. MODULE_DESCRIPTION("Bit testing module");