record-example.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Sample dynamic sized record fifo implementation
  4. *
  5. * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/proc_fs.h>
  10. #include <linux/mutex.h>
  11. #include <linux/kfifo.h>
  12. /*
  13. * This module shows how to create a variable sized record fifo.
  14. */
  15. /* fifo size in elements (bytes) */
  16. #define FIFO_SIZE 128
  17. /* name of the proc entry */
  18. #define PROC_FIFO "record-fifo"
  19. /* lock for procfs read access */
  20. static DEFINE_MUTEX(read_lock);
  21. /* lock for procfs write access */
  22. static DEFINE_MUTEX(write_lock);
  23. /*
  24. * define DYNAMIC in this example for a dynamically allocated fifo.
  25. *
  26. * Otherwise the fifo storage will be a part of the fifo structure.
  27. */
  28. #if 0
  29. #define DYNAMIC
  30. #endif
  31. /*
  32. * struct kfifo_rec_ptr_1 and STRUCT_KFIFO_REC_1 can handle records of a
  33. * length between 0 and 255 bytes.
  34. *
  35. * struct kfifo_rec_ptr_2 and STRUCT_KFIFO_REC_2 can handle records of a
  36. * length between 0 and 65535 bytes.
  37. */
  38. #ifdef DYNAMIC
  39. struct kfifo_rec_ptr_1 test;
  40. #else
  41. typedef STRUCT_KFIFO_REC_1(FIFO_SIZE) mytest;
  42. static mytest test;
  43. #endif
  44. static const char *expected_result[] = {
  45. "a",
  46. "bb",
  47. "ccc",
  48. "dddd",
  49. "eeeee",
  50. "ffffff",
  51. "ggggggg",
  52. "hhhhhhhh",
  53. "iiiiiiiii",
  54. "jjjjjjjjjj",
  55. };
  56. static int __init testfunc(void)
  57. {
  58. char buf[100];
  59. unsigned int i;
  60. unsigned int ret;
  61. struct { unsigned char buf[6]; } hello = { "hello" };
  62. printk(KERN_INFO "record fifo test start\n");
  63. kfifo_in(&test, &hello, sizeof(hello));
  64. /* show the size of the next record in the fifo */
  65. printk(KERN_INFO "fifo peek len: %u\n", kfifo_peek_len(&test));
  66. /* put in variable length data */
  67. for (i = 0; i < 10; i++) {
  68. memset(buf, 'a' + i, i + 1);
  69. kfifo_in(&test, buf, i + 1);
  70. }
  71. /* skip first element of the fifo */
  72. printk(KERN_INFO "skip 1st element\n");
  73. kfifo_skip(&test);
  74. printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
  75. /* show the first record without removing from the fifo */
  76. ret = kfifo_out_peek(&test, buf, sizeof(buf));
  77. if (ret)
  78. printk(KERN_INFO "%.*s\n", ret, buf);
  79. /* check the correctness of all values in the fifo */
  80. i = 0;
  81. while (!kfifo_is_empty(&test)) {
  82. ret = kfifo_out(&test, buf, sizeof(buf));
  83. buf[ret] = '\0';
  84. printk(KERN_INFO "item = %.*s\n", ret, buf);
  85. if (strcmp(buf, expected_result[i++])) {
  86. printk(KERN_WARNING "value mismatch: test failed\n");
  87. return -EIO;
  88. }
  89. }
  90. if (i != ARRAY_SIZE(expected_result)) {
  91. printk(KERN_WARNING "size mismatch: test failed\n");
  92. return -EIO;
  93. }
  94. printk(KERN_INFO "test passed\n");
  95. return 0;
  96. }
  97. static ssize_t fifo_write(struct file *file, const char __user *buf,
  98. size_t count, loff_t *ppos)
  99. {
  100. int ret;
  101. unsigned int copied;
  102. if (mutex_lock_interruptible(&write_lock))
  103. return -ERESTARTSYS;
  104. ret = kfifo_from_user(&test, buf, count, &copied);
  105. mutex_unlock(&write_lock);
  106. if (ret)
  107. return ret;
  108. return copied;
  109. }
  110. static ssize_t fifo_read(struct file *file, char __user *buf,
  111. size_t count, loff_t *ppos)
  112. {
  113. int ret;
  114. unsigned int copied;
  115. if (mutex_lock_interruptible(&read_lock))
  116. return -ERESTARTSYS;
  117. ret = kfifo_to_user(&test, buf, count, &copied);
  118. mutex_unlock(&read_lock);
  119. if (ret)
  120. return ret;
  121. return copied;
  122. }
  123. static const struct proc_ops fifo_proc_ops = {
  124. .proc_read = fifo_read,
  125. .proc_write = fifo_write,
  126. .proc_lseek = noop_llseek,
  127. };
  128. static int __init example_init(void)
  129. {
  130. #ifdef DYNAMIC
  131. int ret;
  132. ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
  133. if (ret) {
  134. printk(KERN_ERR "error kfifo_alloc\n");
  135. return ret;
  136. }
  137. #else
  138. INIT_KFIFO(test);
  139. #endif
  140. if (testfunc() < 0) {
  141. #ifdef DYNAMIC
  142. kfifo_free(&test);
  143. #endif
  144. return -EIO;
  145. }
  146. if (proc_create(PROC_FIFO, 0, NULL, &fifo_proc_ops) == NULL) {
  147. #ifdef DYNAMIC
  148. kfifo_free(&test);
  149. #endif
  150. return -ENOMEM;
  151. }
  152. return 0;
  153. }
  154. static void __exit example_exit(void)
  155. {
  156. remove_proc_entry(PROC_FIFO, NULL);
  157. #ifdef DYNAMIC
  158. kfifo_free(&test);
  159. #endif
  160. }
  161. module_init(example_init);
  162. module_exit(example_exit);
  163. MODULE_LICENSE("GPL");
  164. MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");