]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - lib/kfifo.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / lib / kfifo.c
CommitLineData
1da177e4 1/*
2e956fb3 2 * A generic kernel FIFO implementation
1da177e4 3 *
2e956fb3 4 * Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net>
1da177e4
LT
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d78a3eda 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1da177e4
LT
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 */
21
22#include <linux/kernel.h>
9984de1a 23#include <linux/export.h>
1da177e4
LT
24#include <linux/slab.h>
25#include <linux/err.h>
f84d5a76 26#include <linux/log2.h>
a121f24a 27#include <linux/uaccess.h>
2e956fb3 28#include <linux/kfifo.h>
1da177e4 29
2e956fb3
SS
30/*
31 * internal helper to calculate the unused elements in a fifo
1da177e4 32 */
2e956fb3 33static inline unsigned int kfifo_unused(struct __kfifo *fifo)
1da177e4 34{
2e956fb3 35 return (fifo->mask + 1) - (fifo->in - fifo->out);
1da177e4 36}
1da177e4 37
2e956fb3
SS
38int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
39 size_t esize, gfp_t gfp_mask)
1da177e4 40{
1da177e4 41 /*
2e956fb3 42 * round down to the next power of 2, since our 'let the indices
b33112d1 43 * wrap' technique works only in this case.
1da177e4 44 */
dfe2a77f 45 size = roundup_pow_of_two(size);
2e956fb3
SS
46
47 fifo->in = 0;
48 fifo->out = 0;
49 fifo->esize = esize;
50
51 if (size < 2) {
52 fifo->data = NULL;
53 fifo->mask = 0;
54 return -EINVAL;
1da177e4
LT
55 }
56
2e956fb3
SS
57 fifo->data = kmalloc(size * esize, gfp_mask);
58
59 if (!fifo->data) {
60 fifo->mask = 0;
45465487
SS
61 return -ENOMEM;
62 }
2e956fb3 63 fifo->mask = size - 1;
1da177e4 64
45465487 65 return 0;
1da177e4 66}
2e956fb3 67EXPORT_SYMBOL(__kfifo_alloc);
1da177e4 68
2e956fb3 69void __kfifo_free(struct __kfifo *fifo)
1da177e4 70{
2e956fb3
SS
71 kfree(fifo->data);
72 fifo->in = 0;
73 fifo->out = 0;
74 fifo->esize = 0;
75 fifo->data = NULL;
76 fifo->mask = 0;
1da177e4 77}
2e956fb3 78EXPORT_SYMBOL(__kfifo_free);
1da177e4 79
2e956fb3
SS
80int __kfifo_init(struct __kfifo *fifo, void *buffer,
81 unsigned int size, size_t esize)
a121f24a 82{
2e956fb3
SS
83 size /= esize;
84
d60b6c50
LT
85 if (!is_power_of_2(size))
86 size = rounddown_pow_of_two(size);
2e956fb3
SS
87
88 fifo->in = 0;
89 fifo->out = 0;
90 fifo->esize = esize;
91 fifo->data = buffer;
92
93 if (size < 2) {
94 fifo->mask = 0;
95 return -EINVAL;
a121f24a 96 }
2e956fb3
SS
97 fifo->mask = size - 1;
98
99 return 0;
a121f24a 100}
2e956fb3 101EXPORT_SYMBOL(__kfifo_init);
a121f24a 102
2e956fb3
SS
103static void kfifo_copy_in(struct __kfifo *fifo, const void *src,
104 unsigned int len, unsigned int off)
1da177e4 105{
2e956fb3
SS
106 unsigned int size = fifo->mask + 1;
107 unsigned int esize = fifo->esize;
1da177e4
LT
108 unsigned int l;
109
2e956fb3
SS
110 off &= fifo->mask;
111 if (esize != 1) {
112 off *= esize;
113 size *= esize;
114 len *= esize;
115 }
116 l = min(len, size - off);
117
118 memcpy(fifo->data + off, src, l);
119 memcpy(fifo->data, src + l, len - l);
a45bce49 120 /*
2e956fb3
SS
121 * make sure that the data in the fifo is up to date before
122 * incrementing the fifo->in index counter
a45bce49 123 */
2e956fb3
SS
124 smp_wmb();
125}
a45bce49 126
2e956fb3
SS
127unsigned int __kfifo_in(struct __kfifo *fifo,
128 const void *buf, unsigned int len)
129{
130 unsigned int l;
a121f24a 131
2e956fb3
SS
132 l = kfifo_unused(fifo);
133 if (len > l)
134 len = l;
1da177e4 135
2e956fb3
SS
136 kfifo_copy_in(fifo, buf, len, fifo->in);
137 fifo->in += len;
138 return len;
1da177e4 139}
2e956fb3 140EXPORT_SYMBOL(__kfifo_in);
1da177e4 141
2e956fb3
SS
142static void kfifo_copy_out(struct __kfifo *fifo, void *dst,
143 unsigned int len, unsigned int off)
1da177e4 144{
2e956fb3
SS
145 unsigned int size = fifo->mask + 1;
146 unsigned int esize = fifo->esize;
1da177e4
LT
147 unsigned int l;
148
2e956fb3
SS
149 off &= fifo->mask;
150 if (esize != 1) {
151 off *= esize;
152 size *= esize;
153 len *= esize;
154 }
155 l = min(len, size - off);
156
157 memcpy(dst, fifo->data + off, l);
158 memcpy(dst + l, fifo->data, len - l);
a45bce49 159 /*
2e956fb3
SS
160 * make sure that the data is copied before
161 * incrementing the fifo->out index counter
a45bce49 162 */
2e956fb3
SS
163 smp_wmb();
164}
a45bce49 165
2e956fb3
SS
166unsigned int __kfifo_out_peek(struct __kfifo *fifo,
167 void *buf, unsigned int len)
168{
169 unsigned int l;
a45bce49 170
2e956fb3
SS
171 l = fifo->in - fifo->out;
172 if (len > l)
173 len = l;
a121f24a 174
2e956fb3
SS
175 kfifo_copy_out(fifo, buf, len, fifo->out);
176 return len;
177}
178EXPORT_SYMBOL(__kfifo_out_peek);
1da177e4 179
2e956fb3
SS
180unsigned int __kfifo_out(struct __kfifo *fifo,
181 void *buf, unsigned int len)
182{
183 len = __kfifo_out_peek(fifo, buf, len);
184 fifo->out += len;
185 return len;
a121f24a 186}
2e956fb3 187EXPORT_SYMBOL(__kfifo_out);
a121f24a 188
2e956fb3
SS
189static unsigned long kfifo_copy_from_user(struct __kfifo *fifo,
190 const void __user *from, unsigned int len, unsigned int off,
191 unsigned int *copied)
a121f24a 192{
2e956fb3
SS
193 unsigned int size = fifo->mask + 1;
194 unsigned int esize = fifo->esize;
a121f24a 195 unsigned int l;
2e956fb3 196 unsigned long ret;
a121f24a 197
2e956fb3
SS
198 off &= fifo->mask;
199 if (esize != 1) {
200 off *= esize;
201 size *= esize;
202 len *= esize;
203 }
204 l = min(len, size - off);
205
206 ret = copy_from_user(fifo->data + off, from, l);
207 if (unlikely(ret))
208 ret = DIV_ROUND_UP(ret + len - l, esize);
209 else {
210 ret = copy_from_user(fifo->data, from + l, len - l);
211 if (unlikely(ret))
212 ret = DIV_ROUND_UP(ret, esize);
213 }
a45bce49 214 /*
2e956fb3
SS
215 * make sure that the data in the fifo is up to date before
216 * incrementing the fifo->in index counter
a45bce49 217 */
2e956fb3 218 smp_wmb();
a019e48c 219 *copied = len - ret * esize;
2e956fb3
SS
220 /* return the number of elements which are not copied */
221 return ret;
222}
a45bce49 223
2e956fb3
SS
224int __kfifo_from_user(struct __kfifo *fifo, const void __user *from,
225 unsigned long len, unsigned int *copied)
226{
227 unsigned int l;
228 unsigned long ret;
229 unsigned int esize = fifo->esize;
230 int err;
a45bce49 231
2e956fb3
SS
232 if (esize != 1)
233 len /= esize;
a121f24a 234
2e956fb3
SS
235 l = kfifo_unused(fifo);
236 if (len > l)
237 len = l;
a121f24a 238
2e956fb3
SS
239 ret = kfifo_copy_from_user(fifo, from, len, fifo->in, copied);
240 if (unlikely(ret)) {
241 len -= ret;
242 err = -EFAULT;
243 } else
244 err = 0;
245 fifo->in += len;
246 return err;
86d48803 247}
2e956fb3 248EXPORT_SYMBOL(__kfifo_from_user);
86d48803 249
2e956fb3
SS
250static unsigned long kfifo_copy_to_user(struct __kfifo *fifo, void __user *to,
251 unsigned int len, unsigned int off, unsigned int *copied)
86d48803
SS
252{
253 unsigned int l;
2e956fb3
SS
254 unsigned long ret;
255 unsigned int size = fifo->mask + 1;
256 unsigned int esize = fifo->esize;
257
258 off &= fifo->mask;
259 if (esize != 1) {
260 off *= esize;
261 size *= esize;
262 len *= esize;
263 }
264 l = min(len, size - off);
265
266 ret = copy_to_user(to, fifo->data + off, l);
267 if (unlikely(ret))
268 ret = DIV_ROUND_UP(ret + len - l, esize);
269 else {
270 ret = copy_to_user(to + l, fifo->data, len - l);
271 if (unlikely(ret))
272 ret = DIV_ROUND_UP(ret, esize);
273 }
86d48803 274 /*
2e956fb3
SS
275 * make sure that the data is copied before
276 * incrementing the fifo->out index counter
86d48803 277 */
2e956fb3 278 smp_wmb();
a019e48c 279 *copied = len - ret * esize;
2e956fb3
SS
280 /* return the number of elements which are not copied */
281 return ret;
282}
86d48803 283
2e956fb3
SS
284int __kfifo_to_user(struct __kfifo *fifo, void __user *to,
285 unsigned long len, unsigned int *copied)
286{
287 unsigned int l;
288 unsigned long ret;
289 unsigned int esize = fifo->esize;
290 int err;
86d48803 291
2e956fb3
SS
292 if (esize != 1)
293 len /= esize;
86d48803 294
2e956fb3
SS
295 l = fifo->in - fifo->out;
296 if (len > l)
297 len = l;
298 ret = kfifo_copy_to_user(fifo, to, len, fifo->out, copied);
64ce1037 299 if (unlikely(ret)) {
2e956fb3
SS
300 len -= ret;
301 err = -EFAULT;
302 } else
303 err = 0;
304 fifo->out += len;
305 return err;
306}
307EXPORT_SYMBOL(__kfifo_to_user);
a121f24a 308
2e956fb3
SS
309static int setup_sgl_buf(struct scatterlist *sgl, void *buf,
310 int nents, unsigned int len)
311{
312 int n;
313 unsigned int l;
314 unsigned int off;
315 struct page *page;
316
317 if (!nents)
318 return 0;
319
320 if (!len)
321 return 0;
322
323 n = 0;
324 page = virt_to_page(buf);
325 off = offset_in_page(buf);
326 l = 0;
327
328 while (len >= l + PAGE_SIZE - off) {
329 struct page *npage;
330
331 l += PAGE_SIZE;
332 buf += PAGE_SIZE;
333 npage = virt_to_page(buf);
334 if (page_to_phys(page) != page_to_phys(npage) - l) {
d78a3eda
SS
335 sg_set_page(sgl, page, l - off, off);
336 sgl = sg_next(sgl);
337 if (++n == nents || sgl == NULL)
2e956fb3
SS
338 return n;
339 page = npage;
340 len -= l - off;
341 l = off = 0;
342 }
64ce1037 343 }
d78a3eda 344 sg_set_page(sgl, page, len, off);
2e956fb3 345 return n + 1;
86d48803 346}
1da177e4 347
2e956fb3
SS
348static unsigned int setup_sgl(struct __kfifo *fifo, struct scatterlist *sgl,
349 int nents, unsigned int len, unsigned int off)
86d48803 350{
2e956fb3
SS
351 unsigned int size = fifo->mask + 1;
352 unsigned int esize = fifo->esize;
353 unsigned int l;
354 unsigned int n;
86d48803 355
2e956fb3
SS
356 off &= fifo->mask;
357 if (esize != 1) {
358 off *= esize;
359 size *= esize;
360 len *= esize;
361 }
362 l = min(len, size - off);
363
d78a3eda 364 n = setup_sgl_buf(sgl, fifo->data + off, nents, l);
2e956fb3
SS
365 n += setup_sgl_buf(sgl + n, fifo->data, nents - n, len - l);
366
2e956fb3 367 return n;
86d48803 368}
86d48803 369
2e956fb3
SS
370unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
371 struct scatterlist *sgl, int nents, unsigned int len)
86d48803 372{
2e956fb3 373 unsigned int l;
86d48803 374
2e956fb3
SS
375 l = kfifo_unused(fifo);
376 if (len > l)
377 len = l;
378
379 return setup_sgl(fifo, sgl, nents, len, fifo->in);
1da177e4 380}
2e956fb3 381EXPORT_SYMBOL(__kfifo_dma_in_prepare);
86d48803 382
2e956fb3
SS
383unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
384 struct scatterlist *sgl, int nents, unsigned int len)
86d48803 385{
2e956fb3
SS
386 unsigned int l;
387
388 l = fifo->in - fifo->out;
389 if (len > l)
390 len = l;
391
392 return setup_sgl(fifo, sgl, nents, len, fifo->out);
86d48803 393}
2e956fb3 394EXPORT_SYMBOL(__kfifo_dma_out_prepare);
86d48803 395
2e956fb3 396unsigned int __kfifo_max_r(unsigned int len, size_t recsize)
86d48803 397{
2e956fb3 398 unsigned int max = (1 << (recsize << 3)) - 1;
86d48803 399
2e956fb3
SS
400 if (len > max)
401 return max;
402 return len;
86d48803 403}
30059d93 404EXPORT_SYMBOL(__kfifo_max_r);
a121f24a 405
2e956fb3
SS
406#define __KFIFO_PEEK(data, out, mask) \
407 ((data)[(out) & (mask)])
408/*
409 * __kfifo_peek_n internal helper function for determinate the length of
410 * the next record in the fifo
a121f24a 411 */
2e956fb3 412static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize)
a121f24a 413{
2e956fb3
SS
414 unsigned int l;
415 unsigned int mask = fifo->mask;
416 unsigned char *data = fifo->data;
a121f24a 417
2e956fb3 418 l = __KFIFO_PEEK(data, fifo->out, mask);
a121f24a 419
2e956fb3
SS
420 if (--recsize)
421 l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8;
422
423 return l;
86d48803 424}
2e956fb3
SS
425
426#define __KFIFO_POKE(data, in, mask, val) \
427 ( \
428 (data)[(in) & (mask)] = (unsigned char)(val) \
429 )
430
431/*
432 * __kfifo_poke_n internal helper function for storeing the length of
433 * the record into the fifo
a5b9e2c1 434 */
2e956fb3 435static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize)
a5b9e2c1 436{
2e956fb3
SS
437 unsigned int mask = fifo->mask;
438 unsigned char *data = fifo->data;
a5b9e2c1 439
2e956fb3
SS
440 __KFIFO_POKE(data, fifo->in, mask, n);
441
442 if (recsize > 1)
443 __KFIFO_POKE(data, fifo->in + 1, mask, n >> 8);
a5b9e2c1 444}
a5b9e2c1 445
2e956fb3 446unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize)
86d48803 447{
2e956fb3 448 return __kfifo_peek_n(fifo, recsize);
86d48803 449}
2e956fb3 450EXPORT_SYMBOL(__kfifo_len_r);
a121f24a 451
2e956fb3
SS
452unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf,
453 unsigned int len, size_t recsize)
86d48803 454{
2e956fb3
SS
455 if (len + recsize > kfifo_unused(fifo))
456 return 0;
64ce1037 457
2e956fb3 458 __kfifo_poke_n(fifo, len, recsize);
a121f24a 459
2e956fb3
SS
460 kfifo_copy_in(fifo, buf, len, fifo->in + recsize);
461 fifo->in += len + recsize;
462 return len;
86d48803 463}
2e956fb3
SS
464EXPORT_SYMBOL(__kfifo_in_r);
465
466static unsigned int kfifo_out_copy_r(struct __kfifo *fifo,
467 void *buf, unsigned int len, size_t recsize, unsigned int *n)
86d48803 468{
2e956fb3
SS
469 *n = __kfifo_peek_n(fifo, recsize);
470
471 if (len > *n)
472 len = *n;
473
474 kfifo_copy_out(fifo, buf, len, fifo->out + recsize);
475 return len;
476}
477
478unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf,
479 unsigned int len, size_t recsize)
480{
481 unsigned int n;
482
483 if (fifo->in == fifo->out)
484 return 0;
485
486 return kfifo_out_copy_r(fifo, buf, len, recsize, &n);
86d48803 487}
2e956fb3 488EXPORT_SYMBOL(__kfifo_out_peek_r);
a121f24a 489
2e956fb3
SS
490unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf,
491 unsigned int len, size_t recsize)
86d48803 492{
2e956fb3
SS
493 unsigned int n;
494
495 if (fifo->in == fifo->out)
496 return 0;
497
498 len = kfifo_out_copy_r(fifo, buf, len, recsize, &n);
499 fifo->out += n + recsize;
500 return len;
86d48803 501}
2e956fb3 502EXPORT_SYMBOL(__kfifo_out_r);
a121f24a 503
b35de43b
AR
504void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize)
505{
506 unsigned int n;
507
508 n = __kfifo_peek_n(fifo, recsize);
509 fifo->out += n + recsize;
510}
511EXPORT_SYMBOL(__kfifo_skip_r);
512
2e956fb3
SS
513int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from,
514 unsigned long len, unsigned int *copied, size_t recsize)
86d48803 515{
2e956fb3 516 unsigned long ret;
a121f24a 517
2e956fb3 518 len = __kfifo_max_r(len, recsize);
a121f24a 519
2e956fb3
SS
520 if (len + recsize > kfifo_unused(fifo)) {
521 *copied = 0;
522 return 0;
523 }
86d48803 524
2e956fb3 525 __kfifo_poke_n(fifo, len, recsize);
86d48803 526
2e956fb3
SS
527 ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied);
528 if (unlikely(ret)) {
529 *copied = 0;
530 return -EFAULT;
531 }
532 fifo->in += len + recsize;
533 return 0;
86d48803 534}
2e956fb3
SS
535EXPORT_SYMBOL(__kfifo_from_user_r);
536
537int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
538 unsigned long len, unsigned int *copied, size_t recsize)
86d48803 539{
2e956fb3
SS
540 unsigned long ret;
541 unsigned int n;
542
543 if (fifo->in == fifo->out) {
544 *copied = 0;
545 return 0;
546 }
547
548 n = __kfifo_peek_n(fifo, recsize);
549 if (len > n)
550 len = n;
551
552 ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied);
553 if (unlikely(ret)) {
554 *copied = 0;
555 return -EFAULT;
556 }
557 fifo->out += n + recsize;
558 return 0;
a121f24a 559}
2e956fb3 560EXPORT_SYMBOL(__kfifo_to_user_r);
a121f24a 561
2e956fb3
SS
562unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
563 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
86d48803 564{
89b3ac63 565 BUG_ON(!nents);
2e956fb3
SS
566
567 len = __kfifo_max_r(len, recsize);
568
569 if (len + recsize > kfifo_unused(fifo))
570 return 0;
571
572 return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize);
86d48803 573}
2e956fb3 574EXPORT_SYMBOL(__kfifo_dma_in_prepare_r);
86d48803 575
2e956fb3
SS
576void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
577 unsigned int len, size_t recsize)
86d48803 578{
2e956fb3
SS
579 len = __kfifo_max_r(len, recsize);
580 __kfifo_poke_n(fifo, len, recsize);
581 fifo->in += len + recsize;
86d48803 582}
2e956fb3 583EXPORT_SYMBOL(__kfifo_dma_in_finish_r);
86d48803 584
2e956fb3
SS
585unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
586 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
86d48803 587{
89b3ac63 588 BUG_ON(!nents);
2e956fb3
SS
589
590 len = __kfifo_max_r(len, recsize);
591
592 if (len + recsize > fifo->in - fifo->out)
593 return 0;
594
595 return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize);
86d48803 596}
2e956fb3
SS
597EXPORT_SYMBOL(__kfifo_dma_out_prepare_r);
598
599void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize)
600{
601 unsigned int len;
86d48803 602
2e956fb3
SS
603 len = __kfifo_peek_n(fifo, recsize);
604 fifo->out += len + recsize;
605}
606EXPORT_SYMBOL(__kfifo_dma_out_finish_r);