kfifo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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, void *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 has to be a power of 2.
  41. *
  42. */
  43. void kfifo_init(struct kfifo *fifo, void *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, NULL, 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. _kfifo_init(fifo, NULL, 0);
  90. }
  91. EXPORT_SYMBOL(kfifo_free);
  92. /**
  93. * kfifo_skip - skip output data
  94. * @fifo: the fifo to be used.
  95. * @len: number of bytes to skip
  96. */
  97. void kfifo_skip(struct kfifo *fifo, unsigned int len)
  98. {
  99. if (len < kfifo_len(fifo)) {
  100. __kfifo_add_out(fifo, len);
  101. return;
  102. }
  103. kfifo_reset_out(fifo);
  104. }
  105. EXPORT_SYMBOL(kfifo_skip);
  106. static inline void __kfifo_in_data(struct kfifo *fifo,
  107. const void *from, unsigned int len, unsigned int off)
  108. {
  109. unsigned int l;
  110. /*
  111. * Ensure that we sample the fifo->out index -before- we
  112. * start putting bytes into the kfifo.
  113. */
  114. smp_mb();
  115. off = __kfifo_off(fifo, fifo->in + off);
  116. /* first put the data starting from fifo->in to buffer end */
  117. l = min(len, fifo->size - off);
  118. memcpy(fifo->buffer + off, from, l);
  119. /* then put the rest (if any) at the beginning of the buffer */
  120. memcpy(fifo->buffer, from + l, len - l);
  121. }
  122. static inline void __kfifo_out_data(struct kfifo *fifo,
  123. void *to, unsigned int len, unsigned int off)
  124. {
  125. unsigned int l;
  126. /*
  127. * Ensure that we sample the fifo->in index -before- we
  128. * start removing bytes from the kfifo.
  129. */
  130. smp_rmb();
  131. off = __kfifo_off(fifo, fifo->out + off);
  132. /* first get the data from fifo->out until the end of the buffer */
  133. l = min(len, fifo->size - off);
  134. memcpy(to, fifo->buffer + off, l);
  135. /* then get the rest (if any) from the beginning of the buffer */
  136. memcpy(to + l, fifo->buffer, len - l);
  137. }
  138. static inline int __kfifo_from_user_data(struct kfifo *fifo,
  139. const void __user *from, unsigned int len, unsigned int off,
  140. unsigned *lenout)
  141. {
  142. unsigned int l;
  143. int ret;
  144. /*
  145. * Ensure that we sample the fifo->out index -before- we
  146. * start putting bytes into the kfifo.
  147. */
  148. smp_mb();
  149. off = __kfifo_off(fifo, fifo->in + off);
  150. /* first put the data starting from fifo->in to buffer end */
  151. l = min(len, fifo->size - off);
  152. ret = copy_from_user(fifo->buffer + off, from, l);
  153. if (unlikely(ret)) {
  154. *lenout = ret;
  155. return -EFAULT;
  156. }
  157. *lenout = l;
  158. /* then put the rest (if any) at the beginning of the buffer */
  159. ret = copy_from_user(fifo->buffer, from + l, len - l);
  160. *lenout += ret ? ret : len - l;
  161. return ret ? -EFAULT : 0;
  162. }
  163. static inline int __kfifo_to_user_data(struct kfifo *fifo,
  164. void __user *to, unsigned int len, unsigned int off, unsigned *lenout)
  165. {
  166. unsigned int l;
  167. int ret;
  168. /*
  169. * Ensure that we sample the fifo->in index -before- we
  170. * start removing bytes from the kfifo.
  171. */
  172. smp_rmb();
  173. off = __kfifo_off(fifo, fifo->out + off);
  174. /* first get the data from fifo->out until the end of the buffer */
  175. l = min(len, fifo->size - off);
  176. ret = copy_to_user(to, fifo->buffer + off, l);
  177. *lenout = l;
  178. if (unlikely(ret)) {
  179. *lenout -= ret;
  180. return -EFAULT;
  181. }
  182. /* then get the rest (if any) from the beginning of the buffer */
  183. len -= l;
  184. ret = copy_to_user(to + l, fifo->buffer, len);
  185. if (unlikely(ret)) {
  186. *lenout += len - ret;
  187. return -EFAULT;
  188. }
  189. *lenout += len;
  190. return 0;
  191. }
  192. unsigned int __kfifo_in_n(struct kfifo *fifo,
  193. const void *from, unsigned int len, unsigned int recsize)
  194. {
  195. if (kfifo_avail(fifo) < len + recsize)
  196. return len + 1;
  197. __kfifo_in_data(fifo, from, len, recsize);
  198. return 0;
  199. }
  200. EXPORT_SYMBOL(__kfifo_in_n);
  201. /**
  202. * kfifo_in - puts some data into the FIFO
  203. * @fifo: the fifo to be used.
  204. * @from: the data to be added.
  205. * @len: the length of the data to be added.
  206. *
  207. * This function copies at most @len bytes from the @from buffer into
  208. * the FIFO depending on the free space, and returns the number of
  209. * bytes copied.
  210. *
  211. * Note that with only one concurrent reader and one concurrent
  212. * writer, you don't need extra locking to use these functions.
  213. */
  214. unsigned int kfifo_in(struct kfifo *fifo, const void *from,
  215. unsigned int len)
  216. {
  217. len = min(kfifo_avail(fifo), len);
  218. __kfifo_in_data(fifo, from, len, 0);
  219. __kfifo_add_in(fifo, len);
  220. return len;
  221. }
  222. EXPORT_SYMBOL(kfifo_in);
  223. unsigned int __kfifo_in_generic(struct kfifo *fifo,
  224. const void *from, unsigned int len, unsigned int recsize)
  225. {
  226. return __kfifo_in_rec(fifo, from, len, recsize);
  227. }
  228. EXPORT_SYMBOL(__kfifo_in_generic);
  229. unsigned int __kfifo_out_n(struct kfifo *fifo,
  230. void *to, unsigned int len, unsigned int recsize)
  231. {
  232. if (kfifo_len(fifo) < len + recsize)
  233. return len;
  234. __kfifo_out_data(fifo, to, len, recsize);
  235. __kfifo_add_out(fifo, len + recsize);
  236. return 0;
  237. }
  238. EXPORT_SYMBOL(__kfifo_out_n);
  239. /**
  240. * kfifo_out - gets some data from the FIFO
  241. * @fifo: the fifo to be used.
  242. * @to: where the data must be copied.
  243. * @len: the size of the destination buffer.
  244. *
  245. * This function copies at most @len bytes from the FIFO into the
  246. * @to buffer and returns the number of copied bytes.
  247. *
  248. * Note that with only one concurrent reader and one concurrent
  249. * writer, you don't need extra locking to use these functions.
  250. */
  251. unsigned int kfifo_out(struct kfifo *fifo, void *to, unsigned int len)
  252. {
  253. len = min(kfifo_len(fifo), len);
  254. __kfifo_out_data(fifo, to, len, 0);
  255. __kfifo_add_out(fifo, len);
  256. return len;
  257. }
  258. EXPORT_SYMBOL(kfifo_out);
  259. /**
  260. * kfifo_out_peek - copy some data from the FIFO, but do not remove it
  261. * @fifo: the fifo to be used.
  262. * @to: where the data must be copied.
  263. * @len: the size of the destination buffer.
  264. * @offset: offset into the fifo
  265. *
  266. * This function copies at most @len bytes at @offset from the FIFO
  267. * into the @to buffer and returns the number of copied bytes.
  268. * The data is not removed from the FIFO.
  269. */
  270. unsigned int kfifo_out_peek(struct kfifo *fifo, void *to, unsigned int len,
  271. unsigned offset)
  272. {
  273. len = min(kfifo_len(fifo), len + offset);
  274. __kfifo_out_data(fifo, to, len, offset);
  275. return len;
  276. }
  277. EXPORT_SYMBOL(kfifo_out_peek);
  278. unsigned int __kfifo_out_generic(struct kfifo *fifo,
  279. void *to, unsigned int len, unsigned int recsize,
  280. unsigned int *total)
  281. {
  282. return __kfifo_out_rec(fifo, to, len, recsize, total);
  283. }
  284. EXPORT_SYMBOL(__kfifo_out_generic);
  285. unsigned int __kfifo_from_user_n(struct kfifo *fifo,
  286. const void __user *from, unsigned int len, unsigned int recsize)
  287. {
  288. unsigned total;
  289. if (kfifo_avail(fifo) < len + recsize)
  290. return len + 1;
  291. __kfifo_from_user_data(fifo, from, len, recsize, &total);
  292. return total;
  293. }
  294. EXPORT_SYMBOL(__kfifo_from_user_n);
  295. /**
  296. * kfifo_from_user - puts some data from user space into the FIFO
  297. * @fifo: the fifo to be used.
  298. * @from: pointer to the data to be added.
  299. * @len: the length of the data to be added.
  300. * @total: the actual returned data length.
  301. *
  302. * This function copies at most @len bytes from the @from into the
  303. * FIFO depending and returns -EFAULT/0.
  304. *
  305. * Note that with only one concurrent reader and one concurrent
  306. * writer, you don't need extra locking to use these functions.
  307. */
  308. int kfifo_from_user(struct kfifo *fifo,
  309. const void __user *from, unsigned int len, unsigned *total)
  310. {
  311. int ret;
  312. len = min(kfifo_avail(fifo), len);
  313. ret = __kfifo_from_user_data(fifo, from, len, 0, total);
  314. if (ret)
  315. return ret;
  316. __kfifo_add_in(fifo, len);
  317. return 0;
  318. }
  319. EXPORT_SYMBOL(kfifo_from_user);
  320. unsigned int __kfifo_from_user_generic(struct kfifo *fifo,
  321. const void __user *from, unsigned int len, unsigned int recsize)
  322. {
  323. return __kfifo_from_user_rec(fifo, from, len, recsize);
  324. }
  325. EXPORT_SYMBOL(__kfifo_from_user_generic);
  326. unsigned int __kfifo_to_user_n(struct kfifo *fifo,
  327. void __user *to, unsigned int len, unsigned int reclen,
  328. unsigned int recsize)
  329. {
  330. unsigned int ret, total;
  331. if (kfifo_len(fifo) < reclen + recsize)
  332. return len;
  333. ret = __kfifo_to_user_data(fifo, to, reclen, recsize, &total);
  334. if (likely(ret == 0))
  335. __kfifo_add_out(fifo, reclen + recsize);
  336. return total;
  337. }
  338. EXPORT_SYMBOL(__kfifo_to_user_n);
  339. /**
  340. * kfifo_to_user - gets data from the FIFO and write it to user space
  341. * @fifo: the fifo to be used.
  342. * @to: where the data must be copied.
  343. * @len: the size of the destination buffer.
  344. * @lenout: pointer to output variable with copied data
  345. *
  346. * This function copies at most @len bytes from the FIFO into the
  347. * @to buffer and 0 or -EFAULT.
  348. *
  349. * Note that with only one concurrent reader and one concurrent
  350. * writer, you don't need extra locking to use these functions.
  351. */
  352. int kfifo_to_user(struct kfifo *fifo,
  353. void __user *to, unsigned int len, unsigned *lenout)
  354. {
  355. int ret;
  356. len = min(kfifo_len(fifo), len);
  357. ret = __kfifo_to_user_data(fifo, to, len, 0, lenout);
  358. __kfifo_add_out(fifo, *lenout);
  359. return ret;
  360. }
  361. EXPORT_SYMBOL(kfifo_to_user);
  362. unsigned int __kfifo_to_user_generic(struct kfifo *fifo,
  363. void __user *to, unsigned int len, unsigned int recsize,
  364. unsigned int *total)
  365. {
  366. return __kfifo_to_user_rec(fifo, to, len, recsize, total);
  367. }
  368. EXPORT_SYMBOL(__kfifo_to_user_generic);
  369. unsigned int __kfifo_peek_generic(struct kfifo *fifo, unsigned int recsize)
  370. {
  371. if (recsize == 0)
  372. return kfifo_avail(fifo);
  373. return __kfifo_peek_n(fifo, recsize);
  374. }
  375. EXPORT_SYMBOL(__kfifo_peek_generic);
  376. void __kfifo_skip_generic(struct kfifo *fifo, unsigned int recsize)
  377. {
  378. __kfifo_skip_rec(fifo, recsize);
  379. }
  380. EXPORT_SYMBOL(__kfifo_skip_generic);