kfifo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * A generic kernel FIFO implementation.
  3. *
  4. * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net>
  5. * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/err.h>
  26. #include <linux/kfifo.h>
  27. #include <linux/log2.h>
  28. #include <linux/uaccess.h>
  29. static void _kfifo_init(struct kfifo *fifo, unsigned char *buffer,
  30. unsigned int size)
  31. {
  32. fifo->buffer = buffer;
  33. fifo->size = size;
  34. kfifo_reset(fifo);
  35. }
  36. /**
  37. * kfifo_init - initialize a FIFO using a preallocated buffer
  38. * @fifo: the fifo to assign the buffer
  39. * @buffer: the preallocated buffer to be used.
  40. * @size: the size of the internal buffer, this have to be a power of 2.
  41. *
  42. */
  43. void kfifo_init(struct kfifo *fifo, unsigned char *buffer, unsigned int size)
  44. {
  45. /* size must be a power of 2 */
  46. BUG_ON(!is_power_of_2(size));
  47. _kfifo_init(fifo, buffer, size);
  48. }
  49. EXPORT_SYMBOL(kfifo_init);
  50. /**
  51. * kfifo_alloc - allocates a new FIFO internal buffer
  52. * @fifo: the fifo to assign then new buffer
  53. * @size: the size of the buffer to be allocated, this have to be a power of 2.
  54. * @gfp_mask: get_free_pages mask, passed to kmalloc()
  55. *
  56. * This function dynamically allocates a new fifo internal buffer
  57. *
  58. * The size will be rounded-up to a power of 2.
  59. * The buffer will be release with kfifo_free().
  60. * Return 0 if no error, otherwise the an error code
  61. */
  62. int kfifo_alloc(struct kfifo *fifo, unsigned int size, gfp_t gfp_mask)
  63. {
  64. unsigned char *buffer;
  65. /*
  66. * round up to the next power of 2, since our 'let the indices
  67. * wrap' technique works only in this case.
  68. */
  69. if (!is_power_of_2(size)) {
  70. BUG_ON(size > 0x80000000);
  71. size = roundup_pow_of_two(size);
  72. }
  73. buffer = kmalloc(size, gfp_mask);
  74. if (!buffer) {
  75. _kfifo_init(fifo, 0, 0);
  76. return -ENOMEM;
  77. }
  78. _kfifo_init(fifo, buffer, size);
  79. return 0;
  80. }
  81. EXPORT_SYMBOL(kfifo_alloc);
  82. /**
  83. * kfifo_free - frees the FIFO internal buffer
  84. * @fifo: the fifo to be freed.
  85. */
  86. void kfifo_free(struct kfifo *fifo)
  87. {
  88. kfree(fifo->buffer);
  89. }
  90. EXPORT_SYMBOL(kfifo_free);
  91. /**
  92. * kfifo_skip - skip output data
  93. * @fifo: the fifo to be used.
  94. * @len: number of bytes to skip
  95. */
  96. void kfifo_skip(struct kfifo *fifo, unsigned int len)
  97. {
  98. if (len < kfifo_len(fifo)) {
  99. __kfifo_add_out(fifo, len);
  100. return;
  101. }
  102. kfifo_reset_out(fifo);
  103. }
  104. EXPORT_SYMBOL(kfifo_skip);
  105. static inline void __kfifo_in_data(struct kfifo *fifo,
  106. const void *from, unsigned int len, unsigned int off)
  107. {
  108. unsigned int l;
  109. /*
  110. * Ensure that we sample the fifo->out index -before- we
  111. * start putting bytes into the kfifo.
  112. */
  113. smp_mb();
  114. off = __kfifo_off(fifo, fifo->in + off);
  115. /* first put the data starting from fifo->in to buffer end */
  116. l = min(len, fifo->size - off);
  117. memcpy(fifo->buffer + off, from, l);
  118. /* then put the rest (if any) at the beginning of the buffer */
  119. memcpy(fifo->buffer, from + l, len - l);
  120. }
  121. static inline void __kfifo_out_data(struct kfifo *fifo,
  122. void *to, unsigned int len, unsigned int off)
  123. {
  124. unsigned int l;
  125. /*
  126. * Ensure that we sample the fifo->in index -before- we
  127. * start removing bytes from the kfifo.
  128. */
  129. smp_rmb();
  130. off = __kfifo_off(fifo, fifo->out + off);
  131. /* first get the data from fifo->out until the end of the buffer */
  132. l = min(len, fifo->size - off);
  133. memcpy(to, fifo->buffer + off, l);
  134. /* then get the rest (if any) from the beginning of the buffer */
  135. memcpy(to + l, fifo->buffer, len - l);
  136. }
  137. static inline unsigned int __kfifo_from_user_data(struct kfifo *fifo,
  138. const void __user *from, unsigned int len, unsigned int off)
  139. {
  140. unsigned int l;
  141. int ret;
  142. /*
  143. * Ensure that we sample the fifo->out index -before- we
  144. * start putting bytes into the kfifo.
  145. */
  146. smp_mb();
  147. off = __kfifo_off(fifo, fifo->in + off);
  148. /* first put the data starting from fifo->in to buffer end */
  149. l = min(len, fifo->size - off);
  150. ret = copy_from_user(fifo->buffer + off, from, l);
  151. if (unlikely(ret))
  152. return ret + len - l;
  153. /* then put the rest (if any) at the beginning of the buffer */
  154. return copy_from_user(fifo->buffer, from + l, len - l);
  155. }
  156. static inline unsigned int __kfifo_to_user_data(struct kfifo *fifo,
  157. void __user *to, unsigned int len, unsigned int off)
  158. {
  159. unsigned int l;
  160. int ret;
  161. /*
  162. * Ensure that we sample the fifo->in index -before- we
  163. * start removing bytes from the kfifo.
  164. */
  165. smp_rmb();
  166. off = __kfifo_off(fifo, fifo->out + off);
  167. /* first get the data from fifo->out until the end of the buffer */
  168. l = min(len, fifo->size - off);
  169. ret = copy_to_user(to, fifo->buffer + off, l);
  170. if (unlikely(ret))
  171. return ret + len - l;
  172. /* then get the rest (if any) from the beginning of the buffer */
  173. return copy_to_user(to + l, fifo->buffer, len - l);
  174. }
  175. unsigned int __kfifo_in_n(struct kfifo *fifo,
  176. const void *from, unsigned int len, unsigned int recsize)
  177. {
  178. if (kfifo_avail(fifo) < len + recsize)
  179. return len + 1;
  180. __kfifo_in_data(fifo, from, len, recsize);
  181. return 0;
  182. }
  183. EXPORT_SYMBOL(__kfifo_in_n);
  184. /**
  185. * kfifo_in - puts some data into the FIFO
  186. * @fifo: the fifo to be used.
  187. * @from: the data to be added.
  188. * @len: the length of the data to be added.
  189. *
  190. * This function copies at most @len bytes from the @from buffer into
  191. * the FIFO depending on the free space, and returns the number of
  192. * bytes copied.
  193. *
  194. * Note that with only one concurrent reader and one concurrent
  195. * writer, you don't need extra locking to use these functions.
  196. */
  197. unsigned int kfifo_in(struct kfifo *fifo, const unsigned char *from,
  198. unsigned int len)
  199. {
  200. len = min(kfifo_avail(fifo), len);
  201. __kfifo_in_data(fifo, from, len, 0);
  202. __kfifo_add_in(fifo, len);
  203. return len;
  204. }
  205. EXPORT_SYMBOL(kfifo_in);
  206. unsigned int __kfifo_in_generic(struct kfifo *fifo,
  207. const void *from, unsigned int len, unsigned int recsize)
  208. {
  209. return __kfifo_in_rec(fifo, from, len, recsize);
  210. }
  211. EXPORT_SYMBOL(__kfifo_in_generic);
  212. unsigned int __kfifo_out_n(struct kfifo *fifo,
  213. void *to, unsigned int len, unsigned int recsize)
  214. {
  215. if (kfifo_len(fifo) < len + recsize)
  216. return len;
  217. __kfifo_out_data(fifo, to, len, recsize);
  218. __kfifo_add_out(fifo, len + recsize);
  219. return 0;
  220. }
  221. EXPORT_SYMBOL(__kfifo_out_n);
  222. /**
  223. * kfifo_out - gets some data from the FIFO
  224. * @fifo: the fifo to be used.
  225. * @to: where the data must be copied.
  226. * @len: the size of the destination buffer.
  227. *
  228. * This function copies at most @len bytes from the FIFO into the
  229. * @to buffer and returns the number of copied bytes.
  230. *
  231. * Note that with only one concurrent reader and one concurrent
  232. * writer, you don't need extra locking to use these functions.
  233. */
  234. unsigned int kfifo_out(struct kfifo *fifo, unsigned char *to, unsigned int len)
  235. {
  236. len = min(kfifo_len(fifo), len);
  237. __kfifo_out_data(fifo, to, len, 0);
  238. __kfifo_add_out(fifo, len);
  239. return len;
  240. }
  241. EXPORT_SYMBOL(kfifo_out);
  242. unsigned int __kfifo_out_generic(struct kfifo *fifo,
  243. void *to, unsigned int len, unsigned int recsize,
  244. unsigned int *total)
  245. {
  246. return __kfifo_out_rec(fifo, to, len, recsize, total);
  247. }
  248. EXPORT_SYMBOL(__kfifo_out_generic);
  249. unsigned int __kfifo_from_user_n(struct kfifo *fifo,
  250. const void __user *from, unsigned int len, unsigned int recsize)
  251. {
  252. if (kfifo_avail(fifo) < len + recsize)
  253. return len + 1;
  254. return __kfifo_from_user_data(fifo, from, len, recsize);
  255. }
  256. EXPORT_SYMBOL(__kfifo_from_user_n);
  257. /**
  258. * kfifo_from_user - puts some data from user space into the FIFO
  259. * @fifo: the fifo to be used.
  260. * @from: pointer to the data to be added.
  261. * @len: the length of the data to be added.
  262. *
  263. * This function copies at most @len bytes from the @from into the
  264. * FIFO depending and returns the number of copied bytes.
  265. *
  266. * Note that with only one concurrent reader and one concurrent
  267. * writer, you don't need extra locking to use these functions.
  268. */
  269. unsigned int kfifo_from_user(struct kfifo *fifo,
  270. const void __user *from, unsigned int len)
  271. {
  272. len = min(kfifo_avail(fifo), len);
  273. len -= __kfifo_from_user_data(fifo, from, len, 0);
  274. __kfifo_add_in(fifo, len);
  275. return len;
  276. }
  277. EXPORT_SYMBOL(kfifo_from_user);
  278. unsigned int __kfifo_from_user_generic(struct kfifo *fifo,
  279. const void __user *from, unsigned int len, unsigned int recsize)
  280. {
  281. return __kfifo_from_user_rec(fifo, from, len, recsize);
  282. }
  283. EXPORT_SYMBOL(__kfifo_from_user_generic);
  284. unsigned int __kfifo_to_user_n(struct kfifo *fifo,
  285. void __user *to, unsigned int len, unsigned int reclen,
  286. unsigned int recsize)
  287. {
  288. unsigned int ret;
  289. if (kfifo_len(fifo) < reclen + recsize)
  290. return len;
  291. ret = __kfifo_to_user_data(fifo, to, reclen, recsize);
  292. if (likely(ret == 0))
  293. __kfifo_add_out(fifo, reclen + recsize);
  294. return ret;
  295. }
  296. EXPORT_SYMBOL(__kfifo_to_user_n);
  297. /**
  298. * kfifo_to_user - gets data from the FIFO and write it to user space
  299. * @fifo: the fifo to be used.
  300. * @to: where the data must be copied.
  301. * @len: the size of the destination buffer.
  302. *
  303. * This function copies at most @len bytes from the FIFO into the
  304. * @to buffer and returns the number of copied bytes.
  305. *
  306. * Note that with only one concurrent reader and one concurrent
  307. * writer, you don't need extra locking to use these functions.
  308. */
  309. unsigned int kfifo_to_user(struct kfifo *fifo,
  310. void __user *to, unsigned int len)
  311. {
  312. len = min(kfifo_len(fifo), len);
  313. len -= __kfifo_to_user_data(fifo, to, len, 0);
  314. __kfifo_add_out(fifo, len);
  315. return len;
  316. }
  317. EXPORT_SYMBOL(kfifo_to_user);
  318. unsigned int __kfifo_to_user_generic(struct kfifo *fifo,
  319. void __user *to, unsigned int len, unsigned int recsize,
  320. unsigned int *total)
  321. {
  322. return __kfifo_to_user_rec(fifo, to, len, recsize, total);
  323. }
  324. EXPORT_SYMBOL(__kfifo_to_user_generic);
  325. unsigned int __kfifo_peek_generic(struct kfifo *fifo, unsigned int recsize)
  326. {
  327. if (recsize == 0)
  328. return kfifo_avail(fifo);
  329. return __kfifo_peek_n(fifo, recsize);
  330. }
  331. EXPORT_SYMBOL(__kfifo_peek_generic);
  332. void __kfifo_skip_generic(struct kfifo *fifo, unsigned int recsize)
  333. {
  334. __kfifo_skip_rec(fifo, recsize);
  335. }
  336. EXPORT_SYMBOL(__kfifo_skip_generic);