kfifo.c 11 KB

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