Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kfifo: replace the old non generic API

Simply replace the whole kfifo.c and kfifo.h files with the new generic
version and fix the kerneldoc API template file.

Signed-off-by: Stefani Seibold <stefani@seibold.net>
Cc: Greg KH <greg@kroah.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Stefani Seibold and committed by
Linus Torvalds
2e956fb3 4201d9a8

+1241 -2302
-1
Documentation/DocBook/kernel-api.tmpl
··· 132 132 <title>FIFO Buffer</title> 133 133 <sect1><title>kfifo interface</title> 134 134 !Iinclude/linux/kfifo.h 135 - !Ekernel/kfifo.c 136 135 </sect1> 137 136 </chapter> 138 137
-844
include/linux/kfifo-new.h
··· 1 - /* 2 - * A generic kernel FIFO implementation 3 - * 4 - * Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net> 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 13 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 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 - #ifndef _LINUX_KFIFO_H 23 - #define _LINUX_KFIFO_H 24 - 25 - /* 26 - * How to porting drivers to the new generic FIFO API: 27 - * 28 - * - Modify the declaration of the "struct kfifo *" object into a 29 - * in-place "struct kfifo" object 30 - * - Init the in-place object with kfifo_alloc() or kfifo_init() 31 - * Note: The address of the in-place "struct kfifo" object must be 32 - * passed as the first argument to this functions 33 - * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get 34 - * into kfifo_out 35 - * - Replace the use of kfifo_put into kfifo_in_spinlocked and kfifo_get 36 - * into kfifo_out_spinlocked 37 - * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc 38 - * must be passed now to the kfifo_in_spinlocked and kfifo_out_spinlocked 39 - * as the last parameter 40 - * - The formerly __kfifo_* functions are renamed into kfifo_* 41 - */ 42 - 43 - /* 44 - * Note about locking : There is no locking required until only * one reader 45 - * and one writer is using the fifo and no kfifo_reset() will be * called 46 - * kfifo_reset_out() can be safely used, until it will be only called 47 - * in the reader thread. 48 - * For multiple writer and one reader there is only a need to lock the writer. 49 - * And vice versa for only one writer and multiple reader there is only a need 50 - * to lock the reader. 51 - */ 52 - 53 - #include <linux/kernel.h> 54 - #include <linux/spinlock.h> 55 - #include <linux/stddef.h> 56 - #include <linux/scatterlist.h> 57 - 58 - struct __kfifo { 59 - unsigned int in; 60 - unsigned int out; 61 - unsigned int mask; 62 - unsigned int esize; 63 - void *data; 64 - }; 65 - 66 - #define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) \ 67 - union { \ 68 - struct __kfifo kfifo; \ 69 - datatype *type; \ 70 - char (*rectype)[recsize]; \ 71 - ptrtype *ptr; \ 72 - const ptrtype *ptr_const; \ 73 - } 74 - 75 - #define __STRUCT_KFIFO(type, size, recsize, ptrtype) \ 76 - { \ 77 - __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \ 78 - type buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \ 79 - } 80 - 81 - #define STRUCT_KFIFO(type, size) \ 82 - struct __STRUCT_KFIFO(type, size, 0, type) 83 - 84 - #define __STRUCT_KFIFO_PTR(type, recsize, ptrtype) \ 85 - { \ 86 - __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \ 87 - type buf[0]; \ 88 - } 89 - 90 - #define STRUCT_KFIFO_PTR(type) \ 91 - struct __STRUCT_KFIFO_PTR(type, 0, type) 92 - 93 - /* 94 - * define compatibility "struct kfifo" for dynamic allocated fifos 95 - */ 96 - struct kfifo __STRUCT_KFIFO_PTR(unsigned char, 0, void); 97 - 98 - #define STRUCT_KFIFO_REC_1(size) \ 99 - struct __STRUCT_KFIFO(unsigned char, size, 1, void) 100 - 101 - #define STRUCT_KFIFO_REC_2(size) \ 102 - struct __STRUCT_KFIFO(unsigned char, size, 2, void) 103 - 104 - /* 105 - * define kfifo_rec types 106 - */ 107 - struct kfifo_rec_ptr_1 __STRUCT_KFIFO_PTR(unsigned char, 1, void); 108 - struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void); 109 - 110 - /* 111 - * helper macro to distinguish between real in place fifo where the fifo 112 - * array is a part of the structure and the fifo type where the array is 113 - * outside of the fifo structure. 114 - */ 115 - #define __is_kfifo_ptr(fifo) (sizeof(*fifo) == sizeof(struct __kfifo)) 116 - 117 - /** 118 - * DECLARE_KFIFO_PTR - macro to declare a fifo pointer object 119 - * @fifo: name of the declared fifo 120 - * @type: type of the fifo elements 121 - */ 122 - #define DECLARE_KFIFO_PTR(fifo, type) STRUCT_KFIFO_PTR(type) fifo 123 - 124 - /** 125 - * DECLARE_KFIFO - macro to declare a fifo object 126 - * @fifo: name of the declared fifo 127 - * @type: type of the fifo elements 128 - * @size: the number of elements in the fifo, this must be a power of 2 129 - */ 130 - #define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo 131 - 132 - /** 133 - * INIT_KFIFO - Initialize a fifo declared by DECLARE_KFIFO 134 - * @fifo: name of the declared fifo datatype 135 - */ 136 - #define INIT_KFIFO(fifo) \ 137 - (void)({ \ 138 - typeof(&(fifo)) __tmp = &(fifo); \ 139 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 140 - __kfifo->in = 0; \ 141 - __kfifo->out = 0; \ 142 - __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\ 143 - __kfifo->esize = sizeof(*__tmp->buf); \ 144 - __kfifo->data = __is_kfifo_ptr(__tmp) ? NULL : __tmp->buf; \ 145 - }) 146 - 147 - /** 148 - * DEFINE_KFIFO - macro to define and initialize a fifo 149 - * @fifo: name of the declared fifo datatype 150 - * @type: type of the fifo elements 151 - * @size: the number of elements in the fifo, this must be a power of 2 152 - * 153 - * Note: the macro can be used for global and local fifo data type variables. 154 - */ 155 - #define DEFINE_KFIFO(fifo, type, size) \ 156 - DECLARE_KFIFO(fifo, type, size) = \ 157 - (typeof(fifo)) { \ 158 - { \ 159 - { \ 160 - .in = 0, \ 161 - .out = 0, \ 162 - .mask = __is_kfifo_ptr(&(fifo)) ? \ 163 - 0 : \ 164 - ARRAY_SIZE((fifo).buf) - 1, \ 165 - .esize = sizeof(*(fifo).buf), \ 166 - .data = __is_kfifo_ptr(&(fifo)) ? \ 167 - NULL : \ 168 - (fifo).buf, \ 169 - } \ 170 - } \ 171 - } 172 - 173 - 174 - static inline unsigned int __must_check 175 - __kfifo_must_check_helper(unsigned int val) 176 - { 177 - return val; 178 - } 179 - 180 - /** 181 - * kfifo_initialized - Check if the fifo is initialized 182 - * @fifo: address of the fifo to check 183 - * 184 - * Return %true if fifo is initialized, otherwise %false. 185 - * Assumes the fifo was 0 before. 186 - */ 187 - #define kfifo_initialized(fifo) ((fifo)->kfifo.mask) 188 - 189 - /** 190 - * kfifo_esize - returns the size of the element managed by the fifo 191 - * @fifo: address of the fifo to be used 192 - */ 193 - #define kfifo_esize(fifo) ((fifo)->kfifo.esize) 194 - 195 - /** 196 - * kfifo_recsize - returns the size of the record length field 197 - * @fifo: address of the fifo to be used 198 - */ 199 - #define kfifo_recsize(fifo) (sizeof(*(fifo)->rectype)) 200 - 201 - /** 202 - * kfifo_size - returns the size of the fifo in elements 203 - * @fifo: address of the fifo to be used 204 - */ 205 - #define kfifo_size(fifo) ((fifo)->kfifo.mask + 1) 206 - 207 - /** 208 - * kfifo_reset - removes the entire fifo content 209 - * @fifo: address of the fifo to be used 210 - * 211 - * Note: usage of kfifo_reset() is dangerous. It should be only called when the 212 - * fifo is exclusived locked or when it is secured that no other thread is 213 - * accessing the fifo. 214 - */ 215 - #define kfifo_reset(fifo) \ 216 - (void)({ \ 217 - typeof(fifo + 1) __tmp = (fifo); \ 218 - __tmp->kfifo.in = __tmp->kfifo.out = 0; \ 219 - }) 220 - 221 - /** 222 - * kfifo_reset_out - skip fifo content 223 - * @fifo: address of the fifo to be used 224 - * 225 - * Note: The usage of kfifo_reset_out() is safe until it will be only called 226 - * from the reader thread and there is only one concurrent reader. Otherwise 227 - * it is dangerous and must be handled in the same way as kfifo_reset(). 228 - */ 229 - #define kfifo_reset_out(fifo) \ 230 - (void)({ \ 231 - typeof(fifo + 1) __tmp = (fifo); \ 232 - __tmp->kfifo.out = __tmp->kfifo.in; \ 233 - }) 234 - 235 - /** 236 - * kfifo_len - returns the number of used elements in the fifo 237 - * @fifo: address of the fifo to be used 238 - */ 239 - #define kfifo_len(fifo) \ 240 - ({ \ 241 - typeof(fifo + 1) __tmpl = (fifo); \ 242 - __tmpl->kfifo.in - __tmpl->kfifo.out; \ 243 - }) 244 - 245 - /** 246 - * kfifo_is_empty - returns true if the fifo is empty 247 - * @fifo: address of the fifo to be used 248 - */ 249 - #define kfifo_is_empty(fifo) \ 250 - ({ \ 251 - typeof(fifo + 1) __tmpq = (fifo); \ 252 - __tmpq->kfifo.in == __tmpq->kfifo.out; \ 253 - }) 254 - 255 - /** 256 - * kfifo_is_full - returns true if the fifo is full 257 - * @fifo: address of the fifo to be used 258 - */ 259 - #define kfifo_is_full(fifo) \ 260 - ({ \ 261 - typeof(fifo + 1) __tmpq = (fifo); \ 262 - kfifo_len(__tmpq) > __tmpq->kfifo.mask; \ 263 - }) 264 - 265 - /** 266 - * kfifo_avail - returns the number of unused elements in the fifo 267 - * @fifo: address of the fifo to be used 268 - */ 269 - #define kfifo_avail(fifo) \ 270 - __kfifo_must_check_helper( \ 271 - ({ \ 272 - typeof(fifo + 1) __tmpq = (fifo); \ 273 - const size_t __recsize = sizeof(*__tmpq->rectype); \ 274 - unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \ 275 - (__recsize) ? ((__avail <= __recsize) ? 0 : \ 276 - __kfifo_max_r(__avail - __recsize, __recsize)) : \ 277 - __avail; \ 278 - }) \ 279 - ) 280 - 281 - /** 282 - * kfifo_skip - skip output data 283 - * @fifo: address of the fifo to be used 284 - */ 285 - #define kfifo_skip(fifo) \ 286 - (void)({ \ 287 - typeof(fifo + 1) __tmp = (fifo); \ 288 - const size_t __recsize = sizeof(*__tmp->rectype); \ 289 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 290 - if (__recsize) \ 291 - __kfifo_skip_r(__kfifo, __recsize); \ 292 - else \ 293 - __kfifo->out++; \ 294 - }) 295 - 296 - /** 297 - * kfifo_peek_len - gets the size of the next fifo record 298 - * @fifo: address of the fifo to be used 299 - * 300 - * This function returns the size of the next fifo record in number of bytes. 301 - */ 302 - #define kfifo_peek_len(fifo) \ 303 - __kfifo_must_check_helper( \ 304 - ({ \ 305 - typeof(fifo + 1) __tmp = (fifo); \ 306 - const size_t __recsize = sizeof(*__tmp->rectype); \ 307 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 308 - (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \ 309 - __kfifo_len_r(__kfifo, __recsize); \ 310 - }) \ 311 - ) 312 - 313 - /** 314 - * kfifo_alloc - dynamically allocates a new fifo buffer 315 - * @fifo: pointer to the fifo 316 - * @size: the number of elements in the fifo, this must be a power of 2 317 - * @gfp_mask: get_free_pages mask, passed to kmalloc() 318 - * 319 - * This macro dynamically allocates a new fifo buffer. 320 - * 321 - * The numer of elements will be rounded-up to a power of 2. 322 - * The fifo will be release with kfifo_free(). 323 - * Return 0 if no error, otherwise an error code. 324 - */ 325 - #define kfifo_alloc(fifo, size, gfp_mask) \ 326 - __kfifo_must_check_helper( \ 327 - ({ \ 328 - typeof(fifo + 1) __tmp = (fifo); \ 329 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 330 - __is_kfifo_ptr(__tmp) ? \ 331 - __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \ 332 - -EINVAL; \ 333 - }) \ 334 - ) 335 - 336 - /** 337 - * kfifo_free - frees the fifo 338 - * @fifo: the fifo to be freed 339 - */ 340 - #define kfifo_free(fifo) \ 341 - ({ \ 342 - typeof(fifo + 1) __tmp = (fifo); \ 343 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 344 - if (__is_kfifo_ptr(__tmp)) \ 345 - __kfifo_free(__kfifo); \ 346 - }) 347 - 348 - /** 349 - * kfifo_init - initialize a fifo using a preallocated buffer 350 - * @fifo: the fifo to assign the buffer 351 - * @buffer: the preallocated buffer to be used 352 - * @size: the size of the internal buffer, this have to be a power of 2 353 - * 354 - * This macro initialize a fifo using a preallocated buffer. 355 - * 356 - * The numer of elements will be rounded-up to a power of 2. 357 - * Return 0 if no error, otherwise an error code. 358 - */ 359 - #define kfifo_init(fifo, buffer, size) \ 360 - ({ \ 361 - typeof(fifo + 1) __tmp = (fifo); \ 362 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 363 - __is_kfifo_ptr(__tmp) ? \ 364 - __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \ 365 - -EINVAL; \ 366 - }) 367 - 368 - /** 369 - * kfifo_put - put data into the fifo 370 - * @fifo: address of the fifo to be used 371 - * @val: the data to be added 372 - * 373 - * This macro copies the given value into the fifo. 374 - * It returns 0 if the fifo was full. Otherwise it returns the number 375 - * processed elements. 376 - * 377 - * Note that with only one concurrent reader and one concurrent 378 - * writer, you don't need extra locking to use these macro. 379 - */ 380 - #define kfifo_put(fifo, val) \ 381 - ({ \ 382 - typeof(fifo + 1) __tmp = (fifo); \ 383 - typeof(val + 1) __val = (val); \ 384 - unsigned int __ret; \ 385 - const size_t __recsize = sizeof(*__tmp->rectype); \ 386 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 387 - if (0) { \ 388 - typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); \ 389 - __dummy = (typeof(__val))NULL; \ 390 - } \ 391 - if (__recsize) \ 392 - __ret = __kfifo_in_r(__kfifo, __val, sizeof(*__val), \ 393 - __recsize); \ 394 - else { \ 395 - __ret = !kfifo_is_full(__tmp); \ 396 - if (__ret) { \ 397 - (__is_kfifo_ptr(__tmp) ? \ 398 - ((typeof(__tmp->type))__kfifo->data) : \ 399 - (__tmp->buf) \ 400 - )[__kfifo->in & __tmp->kfifo.mask] = \ 401 - *(typeof(__tmp->type))__val; \ 402 - smp_wmb(); \ 403 - __kfifo->in++; \ 404 - } \ 405 - } \ 406 - __ret; \ 407 - }) 408 - 409 - /** 410 - * kfifo_get - get data from the fifo 411 - * @fifo: address of the fifo to be used 412 - * @val: the var where to store the data to be added 413 - * 414 - * This macro reads the data from the fifo. 415 - * It returns 0 if the fifo was empty. Otherwise it returns the number 416 - * processed elements. 417 - * 418 - * Note that with only one concurrent reader and one concurrent 419 - * writer, you don't need extra locking to use these macro. 420 - */ 421 - #define kfifo_get(fifo, val) \ 422 - __kfifo_must_check_helper( \ 423 - ({ \ 424 - typeof(fifo + 1) __tmp = (fifo); \ 425 - typeof(val + 1) __val = (val); \ 426 - unsigned int __ret; \ 427 - const size_t __recsize = sizeof(*__tmp->rectype); \ 428 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 429 - if (0) \ 430 - __val = (typeof(__tmp->ptr))0; \ 431 - if (__recsize) \ 432 - __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \ 433 - __recsize); \ 434 - else { \ 435 - __ret = !kfifo_is_empty(__tmp); \ 436 - if (__ret) { \ 437 - *(typeof(__tmp->type))__val = \ 438 - (__is_kfifo_ptr(__tmp) ? \ 439 - ((typeof(__tmp->type))__kfifo->data) : \ 440 - (__tmp->buf) \ 441 - )[__kfifo->out & __tmp->kfifo.mask]; \ 442 - smp_wmb(); \ 443 - __kfifo->out++; \ 444 - } \ 445 - } \ 446 - __ret; \ 447 - }) \ 448 - ) 449 - 450 - /** 451 - * kfifo_peek - get data from the fifo without removing 452 - * @fifo: address of the fifo to be used 453 - * @val: the var where to store the data to be added 454 - * 455 - * This reads the data from the fifo without removing it from the fifo. 456 - * It returns 0 if the fifo was empty. Otherwise it returns the number 457 - * processed elements. 458 - * 459 - * Note that with only one concurrent reader and one concurrent 460 - * writer, you don't need extra locking to use these macro. 461 - */ 462 - #define kfifo_peek(fifo, val) \ 463 - __kfifo_must_check_helper( \ 464 - ({ \ 465 - typeof(fifo + 1) __tmp = (fifo); \ 466 - typeof(val + 1) __val = (val); \ 467 - unsigned int __ret; \ 468 - const size_t __recsize = sizeof(*__tmp->rectype); \ 469 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 470 - if (0) \ 471 - __val = (typeof(__tmp->ptr))NULL; \ 472 - if (__recsize) \ 473 - __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \ 474 - __recsize); \ 475 - else { \ 476 - __ret = !kfifo_is_empty(__tmp); \ 477 - if (__ret) { \ 478 - *(typeof(__tmp->type))__val = \ 479 - (__is_kfifo_ptr(__tmp) ? \ 480 - ((typeof(__tmp->type))__kfifo->data) : \ 481 - (__tmp->buf) \ 482 - )[__kfifo->out & __tmp->kfifo.mask]; \ 483 - smp_wmb(); \ 484 - } \ 485 - } \ 486 - __ret; \ 487 - }) \ 488 - ) 489 - 490 - /** 491 - * kfifo_in - put data into the fifo 492 - * @fifo: address of the fifo to be used 493 - * @buf: the data to be added 494 - * @n: number of elements to be added 495 - * 496 - * This macro copies the given buffer into the fifo and returns the 497 - * number of copied elements. 498 - * 499 - * Note that with only one concurrent reader and one concurrent 500 - * writer, you don't need extra locking to use these macro. 501 - */ 502 - #define kfifo_in(fifo, buf, n) \ 503 - ({ \ 504 - typeof(fifo + 1) __tmp = (fifo); \ 505 - typeof(buf + 1) __buf = (buf); \ 506 - unsigned long __n = (n); \ 507 - const size_t __recsize = sizeof(*__tmp->rectype); \ 508 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 509 - if (0) { \ 510 - typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); \ 511 - __dummy = (typeof(__buf))NULL; \ 512 - } \ 513 - (__recsize) ?\ 514 - __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \ 515 - __kfifo_in(__kfifo, __buf, __n); \ 516 - }) 517 - 518 - /** 519 - * kfifo_in_spinlocked - put data into the fifo using a spinlock for locking 520 - * @fifo: address of the fifo to be used 521 - * @buf: the data to be added 522 - * @n: number of elements to be added 523 - * @lock: pointer to the spinlock to use for locking 524 - * 525 - * This macro copies the given values buffer into the fifo and returns the 526 - * number of copied elements. 527 - */ 528 - #define kfifo_in_spinlocked(fifo, buf, n, lock) \ 529 - ({ \ 530 - unsigned long __flags; \ 531 - unsigned int __ret; \ 532 - spin_lock_irqsave(lock, __flags); \ 533 - __ret = kfifo_in(fifo, buf, n); \ 534 - spin_unlock_irqrestore(lock, __flags); \ 535 - __ret; \ 536 - }) 537 - 538 - /* alias for kfifo_in_spinlocked, will be removed in a future release */ 539 - #define kfifo_in_locked(fifo, buf, n, lock) \ 540 - kfifo_in_spinlocked(fifo, buf, n, lock) 541 - 542 - /** 543 - * kfifo_out - get data from the fifo 544 - * @fifo: address of the fifo to be used 545 - * @buf: pointer to the storage buffer 546 - * @n: max. number of elements to get 547 - * 548 - * This macro get some data from the fifo and return the numbers of elements 549 - * copied. 550 - * 551 - * Note that with only one concurrent reader and one concurrent 552 - * writer, you don't need extra locking to use these macro. 553 - */ 554 - #define kfifo_out(fifo, buf, n) \ 555 - __kfifo_must_check_helper( \ 556 - ({ \ 557 - typeof(fifo + 1) __tmp = (fifo); \ 558 - typeof(buf + 1) __buf = (buf); \ 559 - unsigned long __n = (n); \ 560 - const size_t __recsize = sizeof(*__tmp->rectype); \ 561 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 562 - if (0) { \ 563 - typeof(__tmp->ptr) __dummy = NULL; \ 564 - __buf = __dummy; \ 565 - } \ 566 - (__recsize) ?\ 567 - __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \ 568 - __kfifo_out(__kfifo, __buf, __n); \ 569 - }) \ 570 - ) 571 - 572 - /** 573 - * kfifo_out_spinlocked - get data from the fifo using a spinlock for locking 574 - * @fifo: address of the fifo to be used 575 - * @buf: pointer to the storage buffer 576 - * @n: max. number of elements to get 577 - * @lock: pointer to the spinlock to use for locking 578 - * 579 - * This macro get the data from the fifo and return the numbers of elements 580 - * copied. 581 - */ 582 - #define kfifo_out_spinlocked(fifo, buf, n, lock) \ 583 - __kfifo_must_check_helper( \ 584 - ({ \ 585 - unsigned long __flags; \ 586 - unsigned int __ret; \ 587 - spin_lock_irqsave(lock, __flags); \ 588 - __ret = kfifo_out(fifo, buf, n); \ 589 - spin_unlock_irqrestore(lock, __flags); \ 590 - __ret; \ 591 - }) \ 592 - ) 593 - 594 - /* alias for kfifo_out_spinlocked, will be removed in a future release */ 595 - #define kfifo_out_locked(fifo, buf, n, lock) \ 596 - kfifo_out_spinlocked(fifo, buf, n, lock) 597 - 598 - /** 599 - * kfifo_from_user - puts some data from user space into the fifo 600 - * @fifo: address of the fifo to be used 601 - * @from: pointer to the data to be added 602 - * @len: the length of the data to be added 603 - * @copied: pointer to output variable to store the number of copied bytes 604 - * 605 - * This macro copies at most @len bytes from the @from into the 606 - * fifo, depending of the available space and returns -EFAULT/0. 607 - * 608 - * Note that with only one concurrent reader and one concurrent 609 - * writer, you don't need extra locking to use these macro. 610 - */ 611 - #define kfifo_from_user(fifo, from, len, copied) \ 612 - __kfifo_must_check_helper( \ 613 - ({ \ 614 - typeof(fifo + 1) __tmp = (fifo); \ 615 - const void __user *__from = (from); \ 616 - unsigned int __len = (len); \ 617 - unsigned int *__copied = (copied); \ 618 - const size_t __recsize = sizeof(*__tmp->rectype); \ 619 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 620 - (__recsize) ? \ 621 - __kfifo_from_user_r(__kfifo, __from, __len, __copied, __recsize) : \ 622 - __kfifo_from_user(__kfifo, __from, __len, __copied); \ 623 - }) \ 624 - ) 625 - 626 - /** 627 - * kfifo_to_user - copies data from the fifo into user space 628 - * @fifo: address of the fifo to be used 629 - * @to: where the data must be copied 630 - * @len: the size of the destination buffer 631 - * @copied: pointer to output variable to store the number of copied bytes 632 - * 633 - * This macro copies at most @len bytes from the fifo into the 634 - * @to buffer and returns -EFAULT/0. 635 - * 636 - * Note that with only one concurrent reader and one concurrent 637 - * writer, you don't need extra locking to use these macro. 638 - */ 639 - #define kfifo_to_user(fifo, to, len, copied) \ 640 - __kfifo_must_check_helper( \ 641 - ({ \ 642 - typeof(fifo + 1) __tmp = (fifo); \ 643 - void __user *__to = (to); \ 644 - unsigned int __len = (len); \ 645 - unsigned int *__copied = (copied); \ 646 - const size_t __recsize = sizeof(*__tmp->rectype); \ 647 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 648 - (__recsize) ? \ 649 - __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \ 650 - __kfifo_to_user(__kfifo, __to, __len, __copied); \ 651 - }) \ 652 - ) 653 - 654 - /** 655 - * kfifo_dma_in_prepare - setup a scatterlist for DMA input 656 - * @fifo: address of the fifo to be used 657 - * @sgl: pointer to the scatterlist array 658 - * @nents: number of entries in the scatterlist array 659 - * @len: number of elements to transfer 660 - * 661 - * This macro fills a scatterlist for DMA input. 662 - * It returns the number entries in the scatterlist array. 663 - * 664 - * Note that with only one concurrent reader and one concurrent 665 - * writer, you don't need extra locking to use these macros. 666 - */ 667 - #define kfifo_dma_in_prepare(fifo, sgl, nents, len) \ 668 - ({ \ 669 - typeof(fifo + 1) __tmp = (fifo); \ 670 - struct scatterlist *__sgl = (sgl); \ 671 - int __nents = (nents); \ 672 - unsigned int __len = (len); \ 673 - const size_t __recsize = sizeof(*__tmp->rectype); \ 674 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 675 - (__recsize) ? \ 676 - __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \ 677 - __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len); \ 678 - }) 679 - 680 - /** 681 - * kfifo_dma_in_finish - finish a DMA IN operation 682 - * @fifo: address of the fifo to be used 683 - * @len: number of bytes to received 684 - * 685 - * This macro finish a DMA IN operation. The in counter will be updated by 686 - * the len parameter. No error checking will be done. 687 - * 688 - * Note that with only one concurrent reader and one concurrent 689 - * writer, you don't need extra locking to use these macros. 690 - */ 691 - #define kfifo_dma_in_finish(fifo, len) \ 692 - (void)({ \ 693 - typeof(fifo + 1) __tmp = (fifo); \ 694 - unsigned int __len = (len); \ 695 - const size_t __recsize = sizeof(*__tmp->rectype); \ 696 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 697 - if (__recsize) \ 698 - __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \ 699 - else \ 700 - __kfifo->in += __len / sizeof(*__tmp->type); \ 701 - }) 702 - 703 - /** 704 - * kfifo_dma_out_prepare - setup a scatterlist for DMA output 705 - * @fifo: address of the fifo to be used 706 - * @sgl: pointer to the scatterlist array 707 - * @nents: number of entries in the scatterlist array 708 - * @len: number of elements to transfer 709 - * 710 - * This macro fills a scatterlist for DMA output which at most @len bytes 711 - * to transfer. 712 - * It returns the number entries in the scatterlist array. 713 - * A zero means there is no space available and the scatterlist is not filled. 714 - * 715 - * Note that with only one concurrent reader and one concurrent 716 - * writer, you don't need extra locking to use these macros. 717 - */ 718 - #define kfifo_dma_out_prepare(fifo, sgl, nents, len) \ 719 - ({ \ 720 - typeof(fifo + 1) __tmp = (fifo); \ 721 - struct scatterlist *__sgl = (sgl); \ 722 - int __nents = (nents); \ 723 - unsigned int __len = (len); \ 724 - const size_t __recsize = sizeof(*__tmp->rectype); \ 725 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 726 - (__recsize) ? \ 727 - __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \ 728 - __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len); \ 729 - }) 730 - 731 - /** 732 - * kfifo_dma_out_finish - finish a DMA OUT operation 733 - * @fifo: address of the fifo to be used 734 - * @len: number of bytes transferd 735 - * 736 - * This macro finish a DMA OUT operation. The out counter will be updated by 737 - * the len parameter. No error checking will be done. 738 - * 739 - * Note that with only one concurrent reader and one concurrent 740 - * writer, you don't need extra locking to use these macros. 741 - */ 742 - #define kfifo_dma_out_finish(fifo, len) \ 743 - (void)({ \ 744 - typeof(fifo + 1) __tmp = (fifo); \ 745 - unsigned int __len = (len); \ 746 - const size_t __recsize = sizeof(*__tmp->rectype); \ 747 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 748 - if (__recsize) \ 749 - __kfifo_dma_out_finish_r(__kfifo, __recsize); \ 750 - else \ 751 - __kfifo->out += __len / sizeof(*__tmp->type); \ 752 - }) 753 - 754 - /** 755 - * kfifo_out_peek - gets some data from the fifo 756 - * @fifo: address of the fifo to be used 757 - * @buf: pointer to the storage buffer 758 - * @n: max. number of elements to get 759 - * 760 - * This macro get the data from the fifo and return the numbers of elements 761 - * copied. The data is not removed from the fifo. 762 - * 763 - * Note that with only one concurrent reader and one concurrent 764 - * writer, you don't need extra locking to use these macro. 765 - */ 766 - #define kfifo_out_peek(fifo, buf, n) \ 767 - __kfifo_must_check_helper( \ 768 - ({ \ 769 - typeof(fifo + 1) __tmp = (fifo); \ 770 - typeof(buf + 1) __buf = (buf); \ 771 - unsigned long __n = (n); \ 772 - const size_t __recsize = sizeof(*__tmp->rectype); \ 773 - struct __kfifo *__kfifo = &__tmp->kfifo; \ 774 - if (0) { \ 775 - typeof(__tmp->ptr) __dummy __attribute__ ((unused)) = NULL; \ 776 - __buf = __dummy; \ 777 - } \ 778 - (__recsize) ? \ 779 - __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \ 780 - __kfifo_out_peek(__kfifo, __buf, __n); \ 781 - }) \ 782 - ) 783 - 784 - extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, 785 - size_t esize, gfp_t gfp_mask); 786 - 787 - extern void __kfifo_free(struct __kfifo *fifo); 788 - 789 - extern int __kfifo_init(struct __kfifo *fifo, void *buffer, 790 - unsigned int size, size_t esize); 791 - 792 - extern unsigned int __kfifo_in(struct __kfifo *fifo, 793 - const void *buf, unsigned int len); 794 - 795 - extern unsigned int __kfifo_out(struct __kfifo *fifo, 796 - void *buf, unsigned int len); 797 - 798 - extern int __kfifo_from_user(struct __kfifo *fifo, 799 - const void __user *from, unsigned long len, unsigned int *copied); 800 - 801 - extern int __kfifo_to_user(struct __kfifo *fifo, 802 - void __user *to, unsigned long len, unsigned int *copied); 803 - 804 - extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo, 805 - struct scatterlist *sgl, int nents, unsigned int len); 806 - 807 - extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo, 808 - struct scatterlist *sgl, int nents, unsigned int len); 809 - 810 - extern unsigned int __kfifo_out_peek(struct __kfifo *fifo, 811 - void *buf, unsigned int len); 812 - 813 - extern unsigned int __kfifo_in_r(struct __kfifo *fifo, 814 - const void *buf, unsigned int len, size_t recsize); 815 - 816 - extern unsigned int __kfifo_out_r(struct __kfifo *fifo, 817 - void *buf, unsigned int len, size_t recsize); 818 - 819 - extern int __kfifo_from_user_r(struct __kfifo *fifo, 820 - const void __user *from, unsigned long len, unsigned int *copied, 821 - size_t recsize); 822 - 823 - extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to, 824 - unsigned long len, unsigned int *copied, size_t recsize); 825 - 826 - extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo, 827 - struct scatterlist *sgl, int nents, unsigned int len, size_t recsize); 828 - 829 - extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo, 830 - unsigned int len, size_t recsize); 831 - 832 - extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo, 833 - struct scatterlist *sgl, int nents, unsigned int len, size_t recsize); 834 - 835 - extern void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize); 836 - 837 - extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize); 838 - 839 - extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, 840 - void *buf, unsigned int len, size_t recsize); 841 - 842 - extern unsigned int __kfifo_max_r(unsigned int len, size_t recsize); 843 - 844 - #endif
+727 -498
include/linux/kfifo.h
··· 1 1 /* 2 - * A generic kernel FIFO implementation. 2 + * A generic kernel FIFO implementation 3 3 * 4 - * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net> 5 - * Copyright (C) 2004 Stelian Pop <stelian@popies.net> 4 + * Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net> 6 5 * 7 6 * This program is free software; you can redistribute it and/or modify 8 7 * it under the terms of the GNU General Public License as published by ··· 19 20 * 20 21 */ 21 22 23 + #ifndef _LINUX_KFIFO_H 24 + #define _LINUX_KFIFO_H 25 + 22 26 /* 23 - * Howto porting drivers to the new generic fifo API: 27 + * How to porting drivers to the new generic FIFO API: 24 28 * 25 29 * - Modify the declaration of the "struct kfifo *" object into a 26 30 * in-place "struct kfifo" object ··· 32 30 * passed as the first argument to this functions 33 31 * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get 34 32 * into kfifo_out 35 - * - Replace the use of kfifo_put into kfifo_in_locked and kfifo_get 36 - * into kfifo_out_locked 33 + * - Replace the use of kfifo_put into kfifo_in_spinlocked and kfifo_get 34 + * into kfifo_out_spinlocked 37 35 * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc 38 - * must be passed now to the kfifo_in_locked and kfifo_out_locked 39 - * as the last parameter. 40 - * - All formerly name __kfifo_* functions has been renamed into kfifo_* 36 + * must be passed now to the kfifo_in_spinlocked and kfifo_out_spinlocked 37 + * as the last parameter 38 + * - The formerly __kfifo_* functions are renamed into kfifo_* 41 39 */ 42 40 43 - #ifndef _LINUX_KFIFO_H 44 - #define _LINUX_KFIFO_H 41 + /* 42 + * Note about locking : There is no locking required until only * one reader 43 + * and one writer is using the fifo and no kfifo_reset() will be * called 44 + * kfifo_reset_out() can be safely used, until it will be only called 45 + * in the reader thread. 46 + * For multiple writer and one reader there is only a need to lock the writer. 47 + * And vice versa for only one writer and multiple reader there is only a need 48 + * to lock the reader. 49 + */ 45 50 46 51 #include <linux/kernel.h> 47 52 #include <linux/spinlock.h> 53 + #include <linux/stddef.h> 54 + #include <linux/scatterlist.h> 48 55 49 - struct kfifo { 50 - unsigned char *buffer; /* the buffer holding the data */ 51 - unsigned int size; /* the size of the allocated buffer */ 52 - unsigned int in; /* data is added at offset (in % size) */ 53 - unsigned int out; /* data is extracted from off. (out % size) */ 56 + struct __kfifo { 57 + unsigned int in; 58 + unsigned int out; 59 + unsigned int mask; 60 + unsigned int esize; 61 + void *data; 54 62 }; 55 63 56 - /* 57 - * Macros for declaration and initialization of the kfifo datatype 58 - */ 59 - 60 - /* helper macro */ 61 - #define __kfifo_initializer(s, b) \ 62 - (struct kfifo) { \ 63 - .size = s, \ 64 - .in = 0, \ 65 - .out = 0, \ 66 - .buffer = b \ 64 + #define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) \ 65 + union { \ 66 + struct __kfifo kfifo; \ 67 + datatype *type; \ 68 + char (*rectype)[recsize]; \ 69 + ptrtype *ptr; \ 70 + const ptrtype *ptr_const; \ 67 71 } 68 72 69 - /** 70 - * DECLARE_KFIFO - macro to declare a kfifo and the associated buffer 71 - * @name: name of the declared kfifo datatype 72 - * @size: size of the fifo buffer. Must be a power of two. 73 - * 74 - * Note1: the macro can be used inside struct or union declaration 75 - * Note2: the macro creates two objects: 76 - * A kfifo object with the given name and a buffer for the kfifo 77 - * object named name##kfifo_buffer 73 + #define __STRUCT_KFIFO(type, size, recsize, ptrtype) \ 74 + { \ 75 + __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \ 76 + type buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \ 77 + } 78 + 79 + #define STRUCT_KFIFO(type, size) \ 80 + struct __STRUCT_KFIFO(type, size, 0, type) 81 + 82 + #define __STRUCT_KFIFO_PTR(type, recsize, ptrtype) \ 83 + { \ 84 + __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \ 85 + type buf[0]; \ 86 + } 87 + 88 + #define STRUCT_KFIFO_PTR(type) \ 89 + struct __STRUCT_KFIFO_PTR(type, 0, type) 90 + 91 + /* 92 + * define compatibility "struct kfifo" for dynamic allocated fifos 78 93 */ 79 - #define DECLARE_KFIFO(name, size) \ 80 - union { \ 81 - struct kfifo name; \ 82 - unsigned char name##kfifo_buffer[size + sizeof(struct kfifo)]; \ 94 + struct kfifo __STRUCT_KFIFO_PTR(unsigned char, 0, void); 95 + 96 + #define STRUCT_KFIFO_REC_1(size) \ 97 + struct __STRUCT_KFIFO(unsigned char, size, 1, void) 98 + 99 + #define STRUCT_KFIFO_REC_2(size) \ 100 + struct __STRUCT_KFIFO(unsigned char, size, 2, void) 101 + 102 + /* 103 + * define kfifo_rec types 104 + */ 105 + struct kfifo_rec_ptr_1 __STRUCT_KFIFO_PTR(unsigned char, 1, void); 106 + struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void); 107 + 108 + /* 109 + * helper macro to distinguish between real in place fifo where the fifo 110 + * array is a part of the structure and the fifo type where the array is 111 + * outside of the fifo structure. 112 + */ 113 + #define __is_kfifo_ptr(fifo) (sizeof(*fifo) == sizeof(struct __kfifo)) 114 + 115 + /** 116 + * DECLARE_KFIFO_PTR - macro to declare a fifo pointer object 117 + * @fifo: name of the declared fifo 118 + * @type: type of the fifo elements 119 + */ 120 + #define DECLARE_KFIFO_PTR(fifo, type) STRUCT_KFIFO_PTR(type) fifo 121 + 122 + /** 123 + * DECLARE_KFIFO - macro to declare a fifo object 124 + * @fifo: name of the declared fifo 125 + * @type: type of the fifo elements 126 + * @size: the number of elements in the fifo, this must be a power of 2 127 + */ 128 + #define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo 129 + 130 + /** 131 + * INIT_KFIFO - Initialize a fifo declared by DECLARE_KFIFO 132 + * @fifo: name of the declared fifo datatype 133 + */ 134 + #define INIT_KFIFO(fifo) \ 135 + (void)({ \ 136 + typeof(&(fifo)) __tmp = &(fifo); \ 137 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 138 + __kfifo->in = 0; \ 139 + __kfifo->out = 0; \ 140 + __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\ 141 + __kfifo->esize = sizeof(*__tmp->buf); \ 142 + __kfifo->data = __is_kfifo_ptr(__tmp) ? NULL : __tmp->buf; \ 143 + }) 144 + 145 + /** 146 + * DEFINE_KFIFO - macro to define and initialize a fifo 147 + * @fifo: name of the declared fifo datatype 148 + * @type: type of the fifo elements 149 + * @size: the number of elements in the fifo, this must be a power of 2 150 + * 151 + * Note: the macro can be used for global and local fifo data type variables. 152 + */ 153 + #define DEFINE_KFIFO(fifo, type, size) \ 154 + DECLARE_KFIFO(fifo, type, size) = \ 155 + (typeof(fifo)) { \ 156 + { \ 157 + { \ 158 + .in = 0, \ 159 + .out = 0, \ 160 + .mask = __is_kfifo_ptr(&(fifo)) ? \ 161 + 0 : \ 162 + ARRAY_SIZE((fifo).buf) - 1, \ 163 + .esize = sizeof(*(fifo).buf), \ 164 + .data = __is_kfifo_ptr(&(fifo)) ? \ 165 + NULL : \ 166 + (fifo).buf, \ 167 + } \ 168 + } \ 169 + } 170 + 171 + 172 + static inline unsigned int __must_check 173 + __kfifo_must_check_helper(unsigned int val) 174 + { 175 + return val; 83 176 } 84 177 85 178 /** 86 - * INIT_KFIFO - Initialize a kfifo declared by DECLARE_KFIFO 87 - * @name: name of the declared kfifo datatype 88 - */ 89 - #define INIT_KFIFO(name) \ 90 - name = __kfifo_initializer(sizeof(name##kfifo_buffer) - \ 91 - sizeof(struct kfifo), \ 92 - name##kfifo_buffer + sizeof(struct kfifo)) 93 - 94 - /** 95 - * DEFINE_KFIFO - macro to define and initialize a kfifo 96 - * @name: name of the declared kfifo datatype 97 - * @size: size of the fifo buffer. Must be a power of two. 179 + * kfifo_initialized - Check if the fifo is initialized 180 + * @fifo: address of the fifo to check 98 181 * 99 - * Note1: the macro can be used for global and local kfifo data type variables 100 - * Note2: the macro creates two objects: 101 - * A kfifo object with the given name and a buffer for the kfifo 102 - * object named name##kfifo_buffer 103 - */ 104 - #define DEFINE_KFIFO(name, size) \ 105 - unsigned char name##kfifo_buffer[size]; \ 106 - struct kfifo name = __kfifo_initializer(size, name##kfifo_buffer) 107 - 108 - extern void kfifo_init(struct kfifo *fifo, void *buffer, 109 - unsigned int size); 110 - extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size, 111 - gfp_t gfp_mask); 112 - extern void kfifo_free(struct kfifo *fifo); 113 - extern unsigned int kfifo_in(struct kfifo *fifo, 114 - const void *from, unsigned int len); 115 - extern __must_check unsigned int kfifo_out(struct kfifo *fifo, 116 - void *to, unsigned int len); 117 - extern __must_check unsigned int kfifo_out_peek(struct kfifo *fifo, 118 - void *to, unsigned int len, unsigned offset); 119 - 120 - /** 121 - * kfifo_initialized - Check if kfifo is initialized. 122 - * @fifo: fifo to check 123 - * Return %true if FIFO is initialized, otherwise %false. 182 + * Return %true if fifo is initialized, otherwise %false. 124 183 * Assumes the fifo was 0 before. 125 184 */ 126 - static inline bool kfifo_initialized(struct kfifo *fifo) 127 - { 128 - return fifo->buffer != NULL; 129 - } 185 + #define kfifo_initialized(fifo) ((fifo)->kfifo.mask) 130 186 131 187 /** 132 - * kfifo_reset - removes the entire FIFO contents 133 - * @fifo: the fifo to be emptied. 188 + * kfifo_esize - returns the size of the element managed by the fifo 189 + * @fifo: address of the fifo to be used 134 190 */ 135 - static inline void kfifo_reset(struct kfifo *fifo) 136 - { 137 - fifo->in = fifo->out = 0; 138 - } 191 + #define kfifo_esize(fifo) ((fifo)->kfifo.esize) 139 192 140 193 /** 141 - * kfifo_reset_out - skip FIFO contents 142 - * @fifo: the fifo to be emptied. 194 + * kfifo_recsize - returns the size of the record length field 195 + * @fifo: address of the fifo to be used 143 196 */ 144 - static inline void kfifo_reset_out(struct kfifo *fifo) 145 - { 146 - smp_mb(); 147 - fifo->out = fifo->in; 148 - } 197 + #define kfifo_recsize(fifo) (sizeof(*(fifo)->rectype)) 149 198 150 199 /** 151 - * kfifo_size - returns the size of the fifo in bytes 152 - * @fifo: the fifo to be used. 200 + * kfifo_size - returns the size of the fifo in elements 201 + * @fifo: address of the fifo to be used 153 202 */ 154 - static inline __must_check unsigned int kfifo_size(struct kfifo *fifo) 155 - { 156 - return fifo->size; 157 - } 203 + #define kfifo_size(fifo) ((fifo)->kfifo.mask + 1) 158 204 159 205 /** 160 - * kfifo_len - returns the number of used bytes in the FIFO 161 - * @fifo: the fifo to be used. 206 + * kfifo_reset - removes the entire fifo content 207 + * @fifo: address of the fifo to be used 208 + * 209 + * Note: usage of kfifo_reset() is dangerous. It should be only called when the 210 + * fifo is exclusived locked or when it is secured that no other thread is 211 + * accessing the fifo. 162 212 */ 163 - static inline unsigned int kfifo_len(struct kfifo *fifo) 164 - { 165 - register unsigned int out; 213 + #define kfifo_reset(fifo) \ 214 + (void)({ \ 215 + typeof(fifo + 1) __tmp = (fifo); \ 216 + __tmp->kfifo.in = __tmp->kfifo.out = 0; \ 217 + }) 166 218 167 - out = fifo->out; 168 - smp_rmb(); 169 - return fifo->in - out; 170 - } 219 + /** 220 + * kfifo_reset_out - skip fifo content 221 + * @fifo: address of the fifo to be used 222 + * 223 + * Note: The usage of kfifo_reset_out() is safe until it will be only called 224 + * from the reader thread and there is only one concurrent reader. Otherwise 225 + * it is dangerous and must be handled in the same way as kfifo_reset(). 226 + */ 227 + #define kfifo_reset_out(fifo) \ 228 + (void)({ \ 229 + typeof(fifo + 1) __tmp = (fifo); \ 230 + __tmp->kfifo.out = __tmp->kfifo.in; \ 231 + }) 232 + 233 + /** 234 + * kfifo_len - returns the number of used elements in the fifo 235 + * @fifo: address of the fifo to be used 236 + */ 237 + #define kfifo_len(fifo) \ 238 + ({ \ 239 + typeof(fifo + 1) __tmpl = (fifo); \ 240 + __tmpl->kfifo.in - __tmpl->kfifo.out; \ 241 + }) 171 242 172 243 /** 173 244 * kfifo_is_empty - returns true if the fifo is empty 174 - * @fifo: the fifo to be used. 245 + * @fifo: address of the fifo to be used 175 246 */ 176 - static inline __must_check bool kfifo_is_empty(struct kfifo *fifo) 177 - { 178 - return fifo->in == fifo->out; 179 - } 247 + #define kfifo_is_empty(fifo) \ 248 + ({ \ 249 + typeof(fifo + 1) __tmpq = (fifo); \ 250 + __tmpq->kfifo.in == __tmpq->kfifo.out; \ 251 + }) 180 252 181 253 /** 182 254 * kfifo_is_full - returns true if the fifo is full 183 - * @fifo: the fifo to be used. 255 + * @fifo: address of the fifo to be used 184 256 */ 185 - static inline __must_check bool kfifo_is_full(struct kfifo *fifo) 186 - { 187 - return kfifo_len(fifo) == kfifo_size(fifo); 188 - } 257 + #define kfifo_is_full(fifo) \ 258 + ({ \ 259 + typeof(fifo + 1) __tmpq = (fifo); \ 260 + kfifo_len(__tmpq) > __tmpq->kfifo.mask; \ 261 + }) 189 262 190 263 /** 191 - * kfifo_avail - returns the number of bytes available in the FIFO 192 - * @fifo: the fifo to be used. 264 + * kfifo_avail - returns the number of unused elements in the fifo 265 + * @fifo: address of the fifo to be used 193 266 */ 194 - static inline __must_check unsigned int kfifo_avail(struct kfifo *fifo) 195 - { 196 - return kfifo_size(fifo) - kfifo_len(fifo); 197 - } 267 + #define kfifo_avail(fifo) \ 268 + __kfifo_must_check_helper( \ 269 + ({ \ 270 + typeof(fifo + 1) __tmpq = (fifo); \ 271 + const size_t __recsize = sizeof(*__tmpq->rectype); \ 272 + unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \ 273 + (__recsize) ? ((__avail <= __recsize) ? 0 : \ 274 + __kfifo_max_r(__avail - __recsize, __recsize)) : \ 275 + __avail; \ 276 + }) \ 277 + ) 198 278 199 279 /** 200 - * kfifo_in_locked - puts some data into the FIFO using a spinlock for locking 201 - * @fifo: the fifo to be used. 202 - * @from: the data to be added. 203 - * @n: the length of the data to be added. 204 - * @lock: pointer to the spinlock to use for locking. 280 + * kfifo_skip - skip output data 281 + * @fifo: address of the fifo to be used 282 + */ 283 + #define kfifo_skip(fifo) \ 284 + (void)({ \ 285 + typeof(fifo + 1) __tmp = (fifo); \ 286 + const size_t __recsize = sizeof(*__tmp->rectype); \ 287 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 288 + if (__recsize) \ 289 + __kfifo_skip_r(__kfifo, __recsize); \ 290 + else \ 291 + __kfifo->out++; \ 292 + }) 293 + 294 + /** 295 + * kfifo_peek_len - gets the size of the next fifo record 296 + * @fifo: address of the fifo to be used 205 297 * 206 - * This function copies at most @n bytes from the @from buffer into 207 - * the FIFO depending on the free space, and returns the number of 208 - * bytes copied. 298 + * This function returns the size of the next fifo record in number of bytes. 209 299 */ 210 - static inline unsigned int kfifo_in_locked(struct kfifo *fifo, 211 - const void *from, unsigned int n, spinlock_t *lock) 212 - { 213 - unsigned long flags; 214 - unsigned int ret; 215 - 216 - spin_lock_irqsave(lock, flags); 217 - 218 - ret = kfifo_in(fifo, from, n); 219 - 220 - spin_unlock_irqrestore(lock, flags); 221 - 222 - return ret; 223 - } 300 + #define kfifo_peek_len(fifo) \ 301 + __kfifo_must_check_helper( \ 302 + ({ \ 303 + typeof(fifo + 1) __tmp = (fifo); \ 304 + const size_t __recsize = sizeof(*__tmp->rectype); \ 305 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 306 + (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \ 307 + __kfifo_len_r(__kfifo, __recsize); \ 308 + }) \ 309 + ) 224 310 225 311 /** 226 - * kfifo_out_locked - gets some data from the FIFO using a spinlock for locking 227 - * @fifo: the fifo to be used. 228 - * @to: where the data must be copied. 229 - * @n: the size of the destination buffer. 230 - * @lock: pointer to the spinlock to use for locking. 312 + * kfifo_alloc - dynamically allocates a new fifo buffer 313 + * @fifo: pointer to the fifo 314 + * @size: the number of elements in the fifo, this must be a power of 2 315 + * @gfp_mask: get_free_pages mask, passed to kmalloc() 231 316 * 232 - * This function copies at most @n bytes from the FIFO into the 233 - * @to buffer and returns the number of copied bytes. 317 + * This macro dynamically allocates a new fifo buffer. 318 + * 319 + * The numer of elements will be rounded-up to a power of 2. 320 + * The fifo will be release with kfifo_free(). 321 + * Return 0 if no error, otherwise an error code. 234 322 */ 235 - static inline __must_check unsigned int kfifo_out_locked(struct kfifo *fifo, 236 - void *to, unsigned int n, spinlock_t *lock) 237 - { 238 - unsigned long flags; 239 - unsigned int ret; 240 - 241 - spin_lock_irqsave(lock, flags); 242 - 243 - ret = kfifo_out(fifo, to, n); 244 - 245 - spin_unlock_irqrestore(lock, flags); 246 - 247 - return ret; 248 - } 249 - 250 - extern void kfifo_skip(struct kfifo *fifo, unsigned int len); 251 - 252 - extern __must_check int kfifo_from_user(struct kfifo *fifo, 253 - const void __user *from, unsigned int n, unsigned *lenout); 254 - 255 - extern __must_check int kfifo_to_user(struct kfifo *fifo, 256 - void __user *to, unsigned int n, unsigned *lenout); 257 - 258 - /* 259 - * __kfifo_add_out internal helper function for updating the out offset 260 - */ 261 - static inline void __kfifo_add_out(struct kfifo *fifo, 262 - unsigned int off) 263 - { 264 - smp_mb(); 265 - fifo->out += off; 266 - } 267 - 268 - /* 269 - * __kfifo_add_in internal helper function for updating the in offset 270 - */ 271 - static inline void __kfifo_add_in(struct kfifo *fifo, 272 - unsigned int off) 273 - { 274 - smp_wmb(); 275 - fifo->in += off; 276 - } 277 - 278 - /* 279 - * __kfifo_off internal helper function for calculating the index of a 280 - * given offeset 281 - */ 282 - static inline unsigned int __kfifo_off(struct kfifo *fifo, unsigned int off) 283 - { 284 - return off & (fifo->size - 1); 285 - } 286 - 287 - /* 288 - * __kfifo_peek_n internal helper function for determinate the length of 289 - * the next record in the fifo 290 - */ 291 - static inline unsigned int __kfifo_peek_n(struct kfifo *fifo, 292 - unsigned int recsize) 293 - { 294 - #define __KFIFO_GET(fifo, off, shift) \ 295 - ((fifo)->buffer[__kfifo_off((fifo), (fifo)->out+(off))] << (shift)) 296 - 297 - unsigned int l; 298 - 299 - l = __KFIFO_GET(fifo, 0, 0); 300 - 301 - if (--recsize) 302 - l |= __KFIFO_GET(fifo, 1, 8); 303 - 304 - return l; 305 - #undef __KFIFO_GET 306 - } 307 - 308 - /* 309 - * __kfifo_poke_n internal helper function for storing the length of 310 - * the next record into the fifo 311 - */ 312 - static inline void __kfifo_poke_n(struct kfifo *fifo, 313 - unsigned int recsize, unsigned int n) 314 - { 315 - #define __KFIFO_PUT(fifo, off, val, shift) \ 316 - ( \ 317 - (fifo)->buffer[__kfifo_off((fifo), (fifo)->in+(off))] = \ 318 - (unsigned char)((val) >> (shift)) \ 319 - ) 320 - 321 - __KFIFO_PUT(fifo, 0, n, 0); 322 - 323 - if (--recsize) 324 - __KFIFO_PUT(fifo, 1, n, 8); 325 - #undef __KFIFO_PUT 326 - } 327 - 328 - /* 329 - * __kfifo_in_... internal functions for put date into the fifo 330 - * do not call it directly, use kfifo_in_rec() instead 331 - */ 332 - extern unsigned int __kfifo_in_n(struct kfifo *fifo, 333 - const void *from, unsigned int n, unsigned int recsize); 334 - 335 - extern unsigned int __kfifo_in_generic(struct kfifo *fifo, 336 - const void *from, unsigned int n, unsigned int recsize); 337 - 338 - static inline unsigned int __kfifo_in_rec(struct kfifo *fifo, 339 - const void *from, unsigned int n, unsigned int recsize) 340 - { 341 - unsigned int ret; 342 - 343 - ret = __kfifo_in_n(fifo, from, n, recsize); 344 - 345 - if (likely(ret == 0)) { 346 - if (recsize) 347 - __kfifo_poke_n(fifo, recsize, n); 348 - __kfifo_add_in(fifo, n + recsize); 349 - } 350 - return ret; 351 - } 323 + #define kfifo_alloc(fifo, size, gfp_mask) \ 324 + __kfifo_must_check_helper( \ 325 + ({ \ 326 + typeof(fifo + 1) __tmp = (fifo); \ 327 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 328 + __is_kfifo_ptr(__tmp) ? \ 329 + __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \ 330 + -EINVAL; \ 331 + }) \ 332 + ) 352 333 353 334 /** 354 - * kfifo_in_rec - puts some record data into the FIFO 355 - * @fifo: the fifo to be used. 356 - * @from: the data to be added. 357 - * @n: the length of the data to be added. 358 - * @recsize: size of record field 335 + * kfifo_free - frees the fifo 336 + * @fifo: the fifo to be freed 337 + */ 338 + #define kfifo_free(fifo) \ 339 + ({ \ 340 + typeof(fifo + 1) __tmp = (fifo); \ 341 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 342 + if (__is_kfifo_ptr(__tmp)) \ 343 + __kfifo_free(__kfifo); \ 344 + }) 345 + 346 + /** 347 + * kfifo_init - initialize a fifo using a preallocated buffer 348 + * @fifo: the fifo to assign the buffer 349 + * @buffer: the preallocated buffer to be used 350 + * @size: the size of the internal buffer, this have to be a power of 2 359 351 * 360 - * This function copies @n bytes from the @from into the FIFO and returns 361 - * the number of bytes which cannot be copied. 362 - * A returned value greater than the @n value means that the record doesn't 363 - * fit into the buffer. 352 + * This macro initialize a fifo using a preallocated buffer. 353 + * 354 + * The numer of elements will be rounded-up to a power of 2. 355 + * Return 0 if no error, otherwise an error code. 356 + */ 357 + #define kfifo_init(fifo, buffer, size) \ 358 + ({ \ 359 + typeof(fifo + 1) __tmp = (fifo); \ 360 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 361 + __is_kfifo_ptr(__tmp) ? \ 362 + __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \ 363 + -EINVAL; \ 364 + }) 365 + 366 + /** 367 + * kfifo_put - put data into the fifo 368 + * @fifo: address of the fifo to be used 369 + * @val: the data to be added 370 + * 371 + * This macro copies the given value into the fifo. 372 + * It returns 0 if the fifo was full. Otherwise it returns the number 373 + * processed elements. 364 374 * 365 375 * Note that with only one concurrent reader and one concurrent 366 - * writer, you don't need extra locking to use these functions. 376 + * writer, you don't need extra locking to use these macro. 367 377 */ 368 - static inline __must_check unsigned int kfifo_in_rec(struct kfifo *fifo, 369 - void *from, unsigned int n, unsigned int recsize) 370 - { 371 - if (!__builtin_constant_p(recsize)) 372 - return __kfifo_in_generic(fifo, from, n, recsize); 373 - return __kfifo_in_rec(fifo, from, n, recsize); 374 - } 375 - 376 - /* 377 - * __kfifo_out_... internal functions for get date from the fifo 378 - * do not call it directly, use kfifo_out_rec() instead 379 - */ 380 - extern unsigned int __kfifo_out_n(struct kfifo *fifo, 381 - void *to, unsigned int reclen, unsigned int recsize); 382 - 383 - extern unsigned int __kfifo_out_generic(struct kfifo *fifo, 384 - void *to, unsigned int n, 385 - unsigned int recsize, unsigned int *total); 386 - 387 - static inline unsigned int __kfifo_out_rec(struct kfifo *fifo, 388 - void *to, unsigned int n, unsigned int recsize, 389 - unsigned int *total) 390 - { 391 - unsigned int l; 392 - 393 - if (!recsize) { 394 - l = n; 395 - if (total) 396 - *total = l; 397 - } else { 398 - l = __kfifo_peek_n(fifo, recsize); 399 - if (total) 400 - *total = l; 401 - if (n < l) 402 - return l; 403 - } 404 - 405 - return __kfifo_out_n(fifo, to, l, recsize); 406 - } 378 + #define kfifo_put(fifo, val) \ 379 + ({ \ 380 + typeof(fifo + 1) __tmp = (fifo); \ 381 + typeof(val + 1) __val = (val); \ 382 + unsigned int __ret; \ 383 + const size_t __recsize = sizeof(*__tmp->rectype); \ 384 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 385 + if (0) { \ 386 + typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); \ 387 + __dummy = (typeof(__val))NULL; \ 388 + } \ 389 + if (__recsize) \ 390 + __ret = __kfifo_in_r(__kfifo, __val, sizeof(*__val), \ 391 + __recsize); \ 392 + else { \ 393 + __ret = !kfifo_is_full(__tmp); \ 394 + if (__ret) { \ 395 + (__is_kfifo_ptr(__tmp) ? \ 396 + ((typeof(__tmp->type))__kfifo->data) : \ 397 + (__tmp->buf) \ 398 + )[__kfifo->in & __tmp->kfifo.mask] = \ 399 + *(typeof(__tmp->type))__val; \ 400 + smp_wmb(); \ 401 + __kfifo->in++; \ 402 + } \ 403 + } \ 404 + __ret; \ 405 + }) 407 406 408 407 /** 409 - * kfifo_out_rec - gets some record data from the FIFO 410 - * @fifo: the fifo to be used. 411 - * @to: where the data must be copied. 412 - * @n: the size of the destination buffer. 413 - * @recsize: size of record field 414 - * @total: pointer where the total number of to copied bytes should stored 408 + * kfifo_get - get data from the fifo 409 + * @fifo: address of the fifo to be used 410 + * @val: the var where to store the data to be added 415 411 * 416 - * This function copies at most @n bytes from the FIFO to @to and returns the 417 - * number of bytes which cannot be copied. 418 - * A returned value greater than the @n value means that the record doesn't 419 - * fit into the @to buffer. 412 + * This macro reads the data from the fifo. 413 + * It returns 0 if the fifo was empty. Otherwise it returns the number 414 + * processed elements. 420 415 * 421 416 * Note that with only one concurrent reader and one concurrent 422 - * writer, you don't need extra locking to use these functions. 417 + * writer, you don't need extra locking to use these macro. 423 418 */ 424 - static inline __must_check unsigned int kfifo_out_rec(struct kfifo *fifo, 425 - void *to, unsigned int n, unsigned int recsize, 426 - unsigned int *total) 427 - 428 - { 429 - if (!__builtin_constant_p(recsize)) 430 - return __kfifo_out_generic(fifo, to, n, recsize, total); 431 - return __kfifo_out_rec(fifo, to, n, recsize, total); 432 - } 433 - 434 - /* 435 - * __kfifo_from_user_... internal functions for transfer from user space into 436 - * the fifo. do not call it directly, use kfifo_from_user_rec() instead 437 - */ 438 - extern unsigned int __kfifo_from_user_n(struct kfifo *fifo, 439 - const void __user *from, unsigned int n, unsigned int recsize); 440 - 441 - extern unsigned int __kfifo_from_user_generic(struct kfifo *fifo, 442 - const void __user *from, unsigned int n, unsigned int recsize); 443 - 444 - static inline unsigned int __kfifo_from_user_rec(struct kfifo *fifo, 445 - const void __user *from, unsigned int n, unsigned int recsize) 446 - { 447 - unsigned int ret; 448 - 449 - ret = __kfifo_from_user_n(fifo, from, n, recsize); 450 - 451 - if (likely(ret == 0)) { 452 - if (recsize) 453 - __kfifo_poke_n(fifo, recsize, n); 454 - __kfifo_add_in(fifo, n + recsize); 455 - } 456 - return ret; 457 - } 419 + #define kfifo_get(fifo, val) \ 420 + __kfifo_must_check_helper( \ 421 + ({ \ 422 + typeof(fifo + 1) __tmp = (fifo); \ 423 + typeof(val + 1) __val = (val); \ 424 + unsigned int __ret; \ 425 + const size_t __recsize = sizeof(*__tmp->rectype); \ 426 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 427 + if (0) \ 428 + __val = (typeof(__tmp->ptr))0; \ 429 + if (__recsize) \ 430 + __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \ 431 + __recsize); \ 432 + else { \ 433 + __ret = !kfifo_is_empty(__tmp); \ 434 + if (__ret) { \ 435 + *(typeof(__tmp->type))__val = \ 436 + (__is_kfifo_ptr(__tmp) ? \ 437 + ((typeof(__tmp->type))__kfifo->data) : \ 438 + (__tmp->buf) \ 439 + )[__kfifo->out & __tmp->kfifo.mask]; \ 440 + smp_wmb(); \ 441 + __kfifo->out++; \ 442 + } \ 443 + } \ 444 + __ret; \ 445 + }) \ 446 + ) 458 447 459 448 /** 460 - * kfifo_from_user_rec - puts some data from user space into the FIFO 461 - * @fifo: the fifo to be used. 462 - * @from: pointer to the data to be added. 463 - * @n: the length of the data to be added. 464 - * @recsize: size of record field 449 + * kfifo_peek - get data from the fifo without removing 450 + * @fifo: address of the fifo to be used 451 + * @val: the var where to store the data to be added 465 452 * 466 - * This function copies @n bytes from the @from into the 467 - * FIFO and returns the number of bytes which cannot be copied. 468 - * 469 - * If the returned value is equal or less the @n value, the copy_from_user() 470 - * functions has failed. Otherwise the record doesn't fit into the buffer. 453 + * This reads the data from the fifo without removing it from the fifo. 454 + * It returns 0 if the fifo was empty. Otherwise it returns the number 455 + * processed elements. 471 456 * 472 457 * Note that with only one concurrent reader and one concurrent 473 - * writer, you don't need extra locking to use these functions. 458 + * writer, you don't need extra locking to use these macro. 474 459 */ 475 - static inline __must_check unsigned int kfifo_from_user_rec(struct kfifo *fifo, 476 - const void __user *from, unsigned int n, unsigned int recsize) 477 - { 478 - if (!__builtin_constant_p(recsize)) 479 - return __kfifo_from_user_generic(fifo, from, n, recsize); 480 - return __kfifo_from_user_rec(fifo, from, n, recsize); 481 - } 482 - 483 - /* 484 - * __kfifo_to_user_... internal functions for transfer fifo data into user space 485 - * do not call it directly, use kfifo_to_user_rec() instead 486 - */ 487 - extern unsigned int __kfifo_to_user_n(struct kfifo *fifo, 488 - void __user *to, unsigned int n, unsigned int reclen, 489 - unsigned int recsize); 490 - 491 - extern unsigned int __kfifo_to_user_generic(struct kfifo *fifo, 492 - void __user *to, unsigned int n, unsigned int recsize, 493 - unsigned int *total); 494 - 495 - static inline unsigned int __kfifo_to_user_rec(struct kfifo *fifo, 496 - void __user *to, unsigned int n, 497 - unsigned int recsize, unsigned int *total) 498 - { 499 - unsigned int l; 500 - 501 - if (!recsize) { 502 - l = n; 503 - if (total) 504 - *total = l; 505 - } else { 506 - l = __kfifo_peek_n(fifo, recsize); 507 - if (total) 508 - *total = l; 509 - if (n < l) 510 - return l; 511 - } 512 - 513 - return __kfifo_to_user_n(fifo, to, n, l, recsize); 514 - } 460 + #define kfifo_peek(fifo, val) \ 461 + __kfifo_must_check_helper( \ 462 + ({ \ 463 + typeof(fifo + 1) __tmp = (fifo); \ 464 + typeof(val + 1) __val = (val); \ 465 + unsigned int __ret; \ 466 + const size_t __recsize = sizeof(*__tmp->rectype); \ 467 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 468 + if (0) \ 469 + __val = (typeof(__tmp->ptr))NULL; \ 470 + if (__recsize) \ 471 + __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \ 472 + __recsize); \ 473 + else { \ 474 + __ret = !kfifo_is_empty(__tmp); \ 475 + if (__ret) { \ 476 + *(typeof(__tmp->type))__val = \ 477 + (__is_kfifo_ptr(__tmp) ? \ 478 + ((typeof(__tmp->type))__kfifo->data) : \ 479 + (__tmp->buf) \ 480 + )[__kfifo->out & __tmp->kfifo.mask]; \ 481 + smp_wmb(); \ 482 + } \ 483 + } \ 484 + __ret; \ 485 + }) \ 486 + ) 515 487 516 488 /** 517 - * kfifo_to_user_rec - gets data from the FIFO and write it to user space 518 - * @fifo: the fifo to be used. 519 - * @to: where the data must be copied. 520 - * @n: the size of the destination buffer. 521 - * @recsize: size of record field 522 - * @total: pointer where the total number of to copied bytes should stored 489 + * kfifo_in - put data into the fifo 490 + * @fifo: address of the fifo to be used 491 + * @buf: the data to be added 492 + * @n: number of elements to be added 523 493 * 524 - * This function copies at most @n bytes from the FIFO to the @to. 525 - * In case of an error, the function returns the number of bytes which cannot 526 - * be copied. 527 - * If the returned value is equal or less the @n value, the copy_to_user() 528 - * functions has failed. Otherwise the record doesn't fit into the @to buffer. 494 + * This macro copies the given buffer into the fifo and returns the 495 + * number of copied elements. 529 496 * 530 497 * Note that with only one concurrent reader and one concurrent 531 - * writer, you don't need extra locking to use these functions. 498 + * writer, you don't need extra locking to use these macro. 532 499 */ 533 - static inline __must_check unsigned int kfifo_to_user_rec(struct kfifo *fifo, 534 - void __user *to, unsigned int n, unsigned int recsize, 535 - unsigned int *total) 536 - { 537 - if (!__builtin_constant_p(recsize)) 538 - return __kfifo_to_user_generic(fifo, to, n, recsize, total); 539 - return __kfifo_to_user_rec(fifo, to, n, recsize, total); 540 - } 541 - 542 - /* 543 - * __kfifo_peek_... internal functions for peek into the next fifo record 544 - * do not call it directly, use kfifo_peek_rec() instead 545 - */ 546 - extern unsigned int __kfifo_peek_generic(struct kfifo *fifo, 547 - unsigned int recsize); 500 + #define kfifo_in(fifo, buf, n) \ 501 + ({ \ 502 + typeof(fifo + 1) __tmp = (fifo); \ 503 + typeof(buf + 1) __buf = (buf); \ 504 + unsigned long __n = (n); \ 505 + const size_t __recsize = sizeof(*__tmp->rectype); \ 506 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 507 + if (0) { \ 508 + typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); \ 509 + __dummy = (typeof(__buf))NULL; \ 510 + } \ 511 + (__recsize) ?\ 512 + __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \ 513 + __kfifo_in(__kfifo, __buf, __n); \ 514 + }) 548 515 549 516 /** 550 - * kfifo_peek_rec - gets the size of the next FIFO record data 551 - * @fifo: the fifo to be used. 552 - * @recsize: size of record field 517 + * kfifo_in_spinlocked - put data into the fifo using a spinlock for locking 518 + * @fifo: address of the fifo to be used 519 + * @buf: the data to be added 520 + * @n: number of elements to be added 521 + * @lock: pointer to the spinlock to use for locking 553 522 * 554 - * This function returns the size of the next FIFO record in number of bytes 523 + * This macro copies the given values buffer into the fifo and returns the 524 + * number of copied elements. 555 525 */ 556 - static inline __must_check unsigned int kfifo_peek_rec(struct kfifo *fifo, 557 - unsigned int recsize) 558 - { 559 - if (!__builtin_constant_p(recsize)) 560 - return __kfifo_peek_generic(fifo, recsize); 561 - if (!recsize) 562 - return kfifo_len(fifo); 563 - return __kfifo_peek_n(fifo, recsize); 564 - } 526 + #define kfifo_in_spinlocked(fifo, buf, n, lock) \ 527 + ({ \ 528 + unsigned long __flags; \ 529 + unsigned int __ret; \ 530 + spin_lock_irqsave(lock, __flags); \ 531 + __ret = kfifo_in(fifo, buf, n); \ 532 + spin_unlock_irqrestore(lock, __flags); \ 533 + __ret; \ 534 + }) 565 535 566 - /* 567 - * __kfifo_skip_... internal functions for skip the next fifo record 568 - * do not call it directly, use kfifo_skip_rec() instead 569 - */ 570 - extern void __kfifo_skip_generic(struct kfifo *fifo, unsigned int recsize); 571 - 572 - static inline void __kfifo_skip_rec(struct kfifo *fifo, 573 - unsigned int recsize) 574 - { 575 - unsigned int l; 576 - 577 - if (recsize) { 578 - l = __kfifo_peek_n(fifo, recsize); 579 - 580 - if (l + recsize <= kfifo_len(fifo)) { 581 - __kfifo_add_out(fifo, l + recsize); 582 - return; 583 - } 584 - } 585 - kfifo_reset_out(fifo); 586 - } 536 + /* alias for kfifo_in_spinlocked, will be removed in a future release */ 537 + #define kfifo_in_locked(fifo, buf, n, lock) \ 538 + kfifo_in_spinlocked(fifo, buf, n, lock) 587 539 588 540 /** 589 - * kfifo_skip_rec - skip the next fifo out record 590 - * @fifo: the fifo to be used. 591 - * @recsize: size of record field 541 + * kfifo_out - get data from the fifo 542 + * @fifo: address of the fifo to be used 543 + * @buf: pointer to the storage buffer 544 + * @n: max. number of elements to get 592 545 * 593 - * This function skips the next FIFO record 546 + * This macro get some data from the fifo and return the numbers of elements 547 + * copied. 548 + * 549 + * Note that with only one concurrent reader and one concurrent 550 + * writer, you don't need extra locking to use these macro. 594 551 */ 595 - static inline void kfifo_skip_rec(struct kfifo *fifo, 596 - unsigned int recsize) 597 - { 598 - if (!__builtin_constant_p(recsize)) 599 - __kfifo_skip_generic(fifo, recsize); 600 - else 601 - __kfifo_skip_rec(fifo, recsize); 602 - } 552 + #define kfifo_out(fifo, buf, n) \ 553 + __kfifo_must_check_helper( \ 554 + ({ \ 555 + typeof(fifo + 1) __tmp = (fifo); \ 556 + typeof(buf + 1) __buf = (buf); \ 557 + unsigned long __n = (n); \ 558 + const size_t __recsize = sizeof(*__tmp->rectype); \ 559 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 560 + if (0) { \ 561 + typeof(__tmp->ptr) __dummy = NULL; \ 562 + __buf = __dummy; \ 563 + } \ 564 + (__recsize) ?\ 565 + __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \ 566 + __kfifo_out(__kfifo, __buf, __n); \ 567 + }) \ 568 + ) 603 569 604 570 /** 605 - * kfifo_avail_rec - returns the number of bytes available in a record FIFO 606 - * @fifo: the fifo to be used. 607 - * @recsize: size of record field 571 + * kfifo_out_spinlocked - get data from the fifo using a spinlock for locking 572 + * @fifo: address of the fifo to be used 573 + * @buf: pointer to the storage buffer 574 + * @n: max. number of elements to get 575 + * @lock: pointer to the spinlock to use for locking 576 + * 577 + * This macro get the data from the fifo and return the numbers of elements 578 + * copied. 608 579 */ 609 - static inline __must_check unsigned int kfifo_avail_rec(struct kfifo *fifo, 610 - unsigned int recsize) 611 - { 612 - unsigned int l = kfifo_size(fifo) - kfifo_len(fifo); 580 + #define kfifo_out_spinlocked(fifo, buf, n, lock) \ 581 + __kfifo_must_check_helper( \ 582 + ({ \ 583 + unsigned long __flags; \ 584 + unsigned int __ret; \ 585 + spin_lock_irqsave(lock, __flags); \ 586 + __ret = kfifo_out(fifo, buf, n); \ 587 + spin_unlock_irqrestore(lock, __flags); \ 588 + __ret; \ 589 + }) \ 590 + ) 613 591 614 - return (l > recsize) ? l - recsize : 0; 615 - } 592 + /* alias for kfifo_out_spinlocked, will be removed in a future release */ 593 + #define kfifo_out_locked(fifo, buf, n, lock) \ 594 + kfifo_out_spinlocked(fifo, buf, n, lock) 595 + 596 + /** 597 + * kfifo_from_user - puts some data from user space into the fifo 598 + * @fifo: address of the fifo to be used 599 + * @from: pointer to the data to be added 600 + * @len: the length of the data to be added 601 + * @copied: pointer to output variable to store the number of copied bytes 602 + * 603 + * This macro copies at most @len bytes from the @from into the 604 + * fifo, depending of the available space and returns -EFAULT/0. 605 + * 606 + * Note that with only one concurrent reader and one concurrent 607 + * writer, you don't need extra locking to use these macro. 608 + */ 609 + #define kfifo_from_user(fifo, from, len, copied) \ 610 + __kfifo_must_check_helper( \ 611 + ({ \ 612 + typeof(fifo + 1) __tmp = (fifo); \ 613 + const void __user *__from = (from); \ 614 + unsigned int __len = (len); \ 615 + unsigned int *__copied = (copied); \ 616 + const size_t __recsize = sizeof(*__tmp->rectype); \ 617 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 618 + (__recsize) ? \ 619 + __kfifo_from_user_r(__kfifo, __from, __len, __copied, __recsize) : \ 620 + __kfifo_from_user(__kfifo, __from, __len, __copied); \ 621 + }) \ 622 + ) 623 + 624 + /** 625 + * kfifo_to_user - copies data from the fifo into user space 626 + * @fifo: address of the fifo to be used 627 + * @to: where the data must be copied 628 + * @len: the size of the destination buffer 629 + * @copied: pointer to output variable to store the number of copied bytes 630 + * 631 + * This macro copies at most @len bytes from the fifo into the 632 + * @to buffer and returns -EFAULT/0. 633 + * 634 + * Note that with only one concurrent reader and one concurrent 635 + * writer, you don't need extra locking to use these macro. 636 + */ 637 + #define kfifo_to_user(fifo, to, len, copied) \ 638 + __kfifo_must_check_helper( \ 639 + ({ \ 640 + typeof(fifo + 1) __tmp = (fifo); \ 641 + void __user *__to = (to); \ 642 + unsigned int __len = (len); \ 643 + unsigned int *__copied = (copied); \ 644 + const size_t __recsize = sizeof(*__tmp->rectype); \ 645 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 646 + (__recsize) ? \ 647 + __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \ 648 + __kfifo_to_user(__kfifo, __to, __len, __copied); \ 649 + }) \ 650 + ) 651 + 652 + /** 653 + * kfifo_dma_in_prepare - setup a scatterlist for DMA input 654 + * @fifo: address of the fifo to be used 655 + * @sgl: pointer to the scatterlist array 656 + * @nents: number of entries in the scatterlist array 657 + * @len: number of elements to transfer 658 + * 659 + * This macro fills a scatterlist for DMA input. 660 + * It returns the number entries in the scatterlist array. 661 + * 662 + * Note that with only one concurrent reader and one concurrent 663 + * writer, you don't need extra locking to use these macros. 664 + */ 665 + #define kfifo_dma_in_prepare(fifo, sgl, nents, len) \ 666 + ({ \ 667 + typeof(fifo + 1) __tmp = (fifo); \ 668 + struct scatterlist *__sgl = (sgl); \ 669 + int __nents = (nents); \ 670 + unsigned int __len = (len); \ 671 + const size_t __recsize = sizeof(*__tmp->rectype); \ 672 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 673 + (__recsize) ? \ 674 + __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \ 675 + __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len); \ 676 + }) 677 + 678 + /** 679 + * kfifo_dma_in_finish - finish a DMA IN operation 680 + * @fifo: address of the fifo to be used 681 + * @len: number of bytes to received 682 + * 683 + * This macro finish a DMA IN operation. The in counter will be updated by 684 + * the len parameter. No error checking will be done. 685 + * 686 + * Note that with only one concurrent reader and one concurrent 687 + * writer, you don't need extra locking to use these macros. 688 + */ 689 + #define kfifo_dma_in_finish(fifo, len) \ 690 + (void)({ \ 691 + typeof(fifo + 1) __tmp = (fifo); \ 692 + unsigned int __len = (len); \ 693 + const size_t __recsize = sizeof(*__tmp->rectype); \ 694 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 695 + if (__recsize) \ 696 + __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \ 697 + else \ 698 + __kfifo->in += __len / sizeof(*__tmp->type); \ 699 + }) 700 + 701 + /** 702 + * kfifo_dma_out_prepare - setup a scatterlist for DMA output 703 + * @fifo: address of the fifo to be used 704 + * @sgl: pointer to the scatterlist array 705 + * @nents: number of entries in the scatterlist array 706 + * @len: number of elements to transfer 707 + * 708 + * This macro fills a scatterlist for DMA output which at most @len bytes 709 + * to transfer. 710 + * It returns the number entries in the scatterlist array. 711 + * A zero means there is no space available and the scatterlist is not filled. 712 + * 713 + * Note that with only one concurrent reader and one concurrent 714 + * writer, you don't need extra locking to use these macros. 715 + */ 716 + #define kfifo_dma_out_prepare(fifo, sgl, nents, len) \ 717 + ({ \ 718 + typeof(fifo + 1) __tmp = (fifo); \ 719 + struct scatterlist *__sgl = (sgl); \ 720 + int __nents = (nents); \ 721 + unsigned int __len = (len); \ 722 + const size_t __recsize = sizeof(*__tmp->rectype); \ 723 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 724 + (__recsize) ? \ 725 + __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \ 726 + __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len); \ 727 + }) 728 + 729 + /** 730 + * kfifo_dma_out_finish - finish a DMA OUT operation 731 + * @fifo: address of the fifo to be used 732 + * @len: number of bytes transferd 733 + * 734 + * This macro finish a DMA OUT operation. The out counter will be updated by 735 + * the len parameter. No error checking will be done. 736 + * 737 + * Note that with only one concurrent reader and one concurrent 738 + * writer, you don't need extra locking to use these macros. 739 + */ 740 + #define kfifo_dma_out_finish(fifo, len) \ 741 + (void)({ \ 742 + typeof(fifo + 1) __tmp = (fifo); \ 743 + unsigned int __len = (len); \ 744 + const size_t __recsize = sizeof(*__tmp->rectype); \ 745 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 746 + if (__recsize) \ 747 + __kfifo_dma_out_finish_r(__kfifo, __recsize); \ 748 + else \ 749 + __kfifo->out += __len / sizeof(*__tmp->type); \ 750 + }) 751 + 752 + /** 753 + * kfifo_out_peek - gets some data from the fifo 754 + * @fifo: address of the fifo to be used 755 + * @buf: pointer to the storage buffer 756 + * @n: max. number of elements to get 757 + * 758 + * This macro get the data from the fifo and return the numbers of elements 759 + * copied. The data is not removed from the fifo. 760 + * 761 + * Note that with only one concurrent reader and one concurrent 762 + * writer, you don't need extra locking to use these macro. 763 + */ 764 + #define kfifo_out_peek(fifo, buf, n) \ 765 + __kfifo_must_check_helper( \ 766 + ({ \ 767 + typeof(fifo + 1) __tmp = (fifo); \ 768 + typeof(buf + 1) __buf = (buf); \ 769 + unsigned long __n = (n); \ 770 + const size_t __recsize = sizeof(*__tmp->rectype); \ 771 + struct __kfifo *__kfifo = &__tmp->kfifo; \ 772 + if (0) { \ 773 + typeof(__tmp->ptr) __dummy __attribute__ ((unused)) = NULL; \ 774 + __buf = __dummy; \ 775 + } \ 776 + (__recsize) ? \ 777 + __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \ 778 + __kfifo_out_peek(__kfifo, __buf, __n); \ 779 + }) \ 780 + ) 781 + 782 + extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, 783 + size_t esize, gfp_t gfp_mask); 784 + 785 + extern void __kfifo_free(struct __kfifo *fifo); 786 + 787 + extern int __kfifo_init(struct __kfifo *fifo, void *buffer, 788 + unsigned int size, size_t esize); 789 + 790 + extern unsigned int __kfifo_in(struct __kfifo *fifo, 791 + const void *buf, unsigned int len); 792 + 793 + extern unsigned int __kfifo_out(struct __kfifo *fifo, 794 + void *buf, unsigned int len); 795 + 796 + extern int __kfifo_from_user(struct __kfifo *fifo, 797 + const void __user *from, unsigned long len, unsigned int *copied); 798 + 799 + extern int __kfifo_to_user(struct __kfifo *fifo, 800 + void __user *to, unsigned long len, unsigned int *copied); 801 + 802 + extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo, 803 + struct scatterlist *sgl, int nents, unsigned int len); 804 + 805 + extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo, 806 + struct scatterlist *sgl, int nents, unsigned int len); 807 + 808 + extern unsigned int __kfifo_out_peek(struct __kfifo *fifo, 809 + void *buf, unsigned int len); 810 + 811 + extern unsigned int __kfifo_in_r(struct __kfifo *fifo, 812 + const void *buf, unsigned int len, size_t recsize); 813 + 814 + extern unsigned int __kfifo_out_r(struct __kfifo *fifo, 815 + void *buf, unsigned int len, size_t recsize); 816 + 817 + extern int __kfifo_from_user_r(struct __kfifo *fifo, 818 + const void __user *from, unsigned long len, unsigned int *copied, 819 + size_t recsize); 820 + 821 + extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to, 822 + unsigned long len, unsigned int *copied, size_t recsize); 823 + 824 + extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo, 825 + struct scatterlist *sgl, int nents, unsigned int len, size_t recsize); 826 + 827 + extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo, 828 + unsigned int len, size_t recsize); 829 + 830 + extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo, 831 + struct scatterlist *sgl, int nents, unsigned int len, size_t recsize); 832 + 833 + extern void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize); 834 + 835 + extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize); 836 + 837 + extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, 838 + void *buf, unsigned int len, size_t recsize); 839 + 840 + extern unsigned int __kfifo_max_r(unsigned int len, size_t recsize); 616 841 617 842 #endif
-602
kernel/kfifo-new.c
··· 1 - /* 2 - * A generic kernel FIFO implementation 3 - * 4 - * Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net> 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 13 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 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> 23 - #include <linux/module.h> 24 - #include <linux/slab.h> 25 - #include <linux/err.h> 26 - #include <linux/log2.h> 27 - #include <linux/uaccess.h> 28 - #include <linux/kfifo.h> 29 - 30 - /* 31 - * internal helper to calculate the unused elements in a fifo 32 - */ 33 - static inline unsigned int kfifo_unused(struct __kfifo *fifo) 34 - { 35 - return (fifo->mask + 1) - (fifo->in - fifo->out); 36 - } 37 - 38 - int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, 39 - size_t esize, gfp_t gfp_mask) 40 - { 41 - /* 42 - * round down to the next power of 2, since our 'let the indices 43 - * wrap' technique works only in this case. 44 - */ 45 - if (!is_power_of_2(size)) 46 - size = rounddown_pow_of_two(size); 47 - 48 - fifo->in = 0; 49 - fifo->out = 0; 50 - fifo->esize = esize; 51 - 52 - if (size < 2) { 53 - fifo->data = NULL; 54 - fifo->mask = 0; 55 - return -EINVAL; 56 - } 57 - 58 - fifo->data = kmalloc(size * esize, gfp_mask); 59 - 60 - if (!fifo->data) { 61 - fifo->mask = 0; 62 - return -ENOMEM; 63 - } 64 - fifo->mask = size - 1; 65 - 66 - return 0; 67 - } 68 - EXPORT_SYMBOL(__kfifo_alloc); 69 - 70 - void __kfifo_free(struct __kfifo *fifo) 71 - { 72 - kfree(fifo->data); 73 - fifo->in = 0; 74 - fifo->out = 0; 75 - fifo->esize = 0; 76 - fifo->data = NULL; 77 - fifo->mask = 0; 78 - } 79 - EXPORT_SYMBOL(__kfifo_free); 80 - 81 - int __kfifo_init(struct __kfifo *fifo, void *buffer, 82 - unsigned int size, size_t esize) 83 - { 84 - size /= esize; 85 - 86 - if (!is_power_of_2(size)) 87 - size = rounddown_pow_of_two(size); 88 - 89 - fifo->in = 0; 90 - fifo->out = 0; 91 - fifo->esize = esize; 92 - fifo->data = buffer; 93 - 94 - if (size < 2) { 95 - fifo->mask = 0; 96 - return -EINVAL; 97 - } 98 - fifo->mask = size - 1; 99 - 100 - return 0; 101 - } 102 - EXPORT_SYMBOL(__kfifo_init); 103 - 104 - static void kfifo_copy_in(struct __kfifo *fifo, const void *src, 105 - unsigned int len, unsigned int off) 106 - { 107 - unsigned int size = fifo->mask + 1; 108 - unsigned int esize = fifo->esize; 109 - unsigned int l; 110 - 111 - off &= fifo->mask; 112 - if (esize != 1) { 113 - off *= esize; 114 - size *= esize; 115 - len *= esize; 116 - } 117 - l = min(len, size - off); 118 - 119 - memcpy(fifo->data + off, src, l); 120 - memcpy(fifo->data, src + l, len - l); 121 - /* 122 - * make sure that the data in the fifo is up to date before 123 - * incrementing the fifo->in index counter 124 - */ 125 - smp_wmb(); 126 - } 127 - 128 - unsigned int __kfifo_in(struct __kfifo *fifo, 129 - const void *buf, unsigned int len) 130 - { 131 - unsigned int l; 132 - 133 - l = kfifo_unused(fifo); 134 - if (len > l) 135 - len = l; 136 - 137 - kfifo_copy_in(fifo, buf, len, fifo->in); 138 - fifo->in += len; 139 - return len; 140 - } 141 - EXPORT_SYMBOL(__kfifo_in); 142 - 143 - static void kfifo_copy_out(struct __kfifo *fifo, void *dst, 144 - unsigned int len, unsigned int off) 145 - { 146 - unsigned int size = fifo->mask + 1; 147 - unsigned int esize = fifo->esize; 148 - unsigned int l; 149 - 150 - off &= fifo->mask; 151 - if (esize != 1) { 152 - off *= esize; 153 - size *= esize; 154 - len *= esize; 155 - } 156 - l = min(len, size - off); 157 - 158 - memcpy(dst, fifo->data + off, l); 159 - memcpy(dst + l, fifo->data, len - l); 160 - /* 161 - * make sure that the data is copied before 162 - * incrementing the fifo->out index counter 163 - */ 164 - smp_wmb(); 165 - } 166 - 167 - unsigned int __kfifo_out_peek(struct __kfifo *fifo, 168 - void *buf, unsigned int len) 169 - { 170 - unsigned int l; 171 - 172 - l = fifo->in - fifo->out; 173 - if (len > l) 174 - len = l; 175 - 176 - kfifo_copy_out(fifo, buf, len, fifo->out); 177 - return len; 178 - } 179 - EXPORT_SYMBOL(__kfifo_out_peek); 180 - 181 - unsigned int __kfifo_out(struct __kfifo *fifo, 182 - void *buf, unsigned int len) 183 - { 184 - len = __kfifo_out_peek(fifo, buf, len); 185 - fifo->out += len; 186 - return len; 187 - } 188 - EXPORT_SYMBOL(__kfifo_out); 189 - 190 - static unsigned long kfifo_copy_from_user(struct __kfifo *fifo, 191 - const void __user *from, unsigned int len, unsigned int off, 192 - unsigned int *copied) 193 - { 194 - unsigned int size = fifo->mask + 1; 195 - unsigned int esize = fifo->esize; 196 - unsigned int l; 197 - unsigned long ret; 198 - 199 - off &= fifo->mask; 200 - if (esize != 1) { 201 - off *= esize; 202 - size *= esize; 203 - len *= esize; 204 - } 205 - l = min(len, size - off); 206 - 207 - ret = copy_from_user(fifo->data + off, from, l); 208 - if (unlikely(ret)) 209 - ret = DIV_ROUND_UP(ret + len - l, esize); 210 - else { 211 - ret = copy_from_user(fifo->data, from + l, len - l); 212 - if (unlikely(ret)) 213 - ret = DIV_ROUND_UP(ret, esize); 214 - } 215 - /* 216 - * make sure that the data in the fifo is up to date before 217 - * incrementing the fifo->in index counter 218 - */ 219 - smp_wmb(); 220 - *copied = len - ret; 221 - /* return the number of elements which are not copied */ 222 - return ret; 223 - } 224 - 225 - int __kfifo_from_user(struct __kfifo *fifo, const void __user *from, 226 - unsigned long len, unsigned int *copied) 227 - { 228 - unsigned int l; 229 - unsigned long ret; 230 - unsigned int esize = fifo->esize; 231 - int err; 232 - 233 - if (esize != 1) 234 - len /= esize; 235 - 236 - l = kfifo_unused(fifo); 237 - if (len > l) 238 - len = l; 239 - 240 - ret = kfifo_copy_from_user(fifo, from, len, fifo->in, copied); 241 - if (unlikely(ret)) { 242 - len -= ret; 243 - err = -EFAULT; 244 - } else 245 - err = 0; 246 - fifo->in += len; 247 - return err; 248 - } 249 - EXPORT_SYMBOL(__kfifo_from_user); 250 - 251 - static unsigned long kfifo_copy_to_user(struct __kfifo *fifo, void __user *to, 252 - unsigned int len, unsigned int off, unsigned int *copied) 253 - { 254 - unsigned int l; 255 - unsigned long ret; 256 - unsigned int size = fifo->mask + 1; 257 - unsigned int esize = fifo->esize; 258 - 259 - off &= fifo->mask; 260 - if (esize != 1) { 261 - off *= esize; 262 - size *= esize; 263 - len *= esize; 264 - } 265 - l = min(len, size - off); 266 - 267 - ret = copy_to_user(to, fifo->data + off, l); 268 - if (unlikely(ret)) 269 - ret = DIV_ROUND_UP(ret + len - l, esize); 270 - else { 271 - ret = copy_to_user(to + l, fifo->data, len - l); 272 - if (unlikely(ret)) 273 - ret = DIV_ROUND_UP(ret, esize); 274 - } 275 - /* 276 - * make sure that the data is copied before 277 - * incrementing the fifo->out index counter 278 - */ 279 - smp_wmb(); 280 - *copied = len - ret; 281 - /* return the number of elements which are not copied */ 282 - return ret; 283 - } 284 - 285 - int __kfifo_to_user(struct __kfifo *fifo, void __user *to, 286 - unsigned long len, unsigned int *copied) 287 - { 288 - unsigned int l; 289 - unsigned long ret; 290 - unsigned int esize = fifo->esize; 291 - int err; 292 - 293 - if (esize != 1) 294 - len /= esize; 295 - 296 - l = fifo->in - fifo->out; 297 - if (len > l) 298 - len = l; 299 - ret = kfifo_copy_to_user(fifo, to, len, fifo->out, copied); 300 - if (unlikely(ret)) { 301 - len -= ret; 302 - err = -EFAULT; 303 - } else 304 - err = 0; 305 - fifo->out += len; 306 - return err; 307 - } 308 - EXPORT_SYMBOL(__kfifo_to_user); 309 - 310 - static int setup_sgl_buf(struct scatterlist *sgl, void *buf, 311 - int nents, unsigned int len) 312 - { 313 - int n; 314 - unsigned int l; 315 - unsigned int off; 316 - struct page *page; 317 - 318 - if (!nents) 319 - return 0; 320 - 321 - if (!len) 322 - return 0; 323 - 324 - n = 0; 325 - page = virt_to_page(buf); 326 - off = offset_in_page(buf); 327 - l = 0; 328 - 329 - while (len >= l + PAGE_SIZE - off) { 330 - struct page *npage; 331 - 332 - l += PAGE_SIZE; 333 - buf += PAGE_SIZE; 334 - npage = virt_to_page(buf); 335 - if (page_to_phys(page) != page_to_phys(npage) - l) { 336 - sgl->page_link = 0; 337 - sg_set_page(sgl++, page, l - off, off); 338 - if (++n == nents) 339 - return n; 340 - page = npage; 341 - len -= l - off; 342 - l = off = 0; 343 - } 344 - } 345 - sgl->page_link = 0; 346 - sg_set_page(sgl++, page, len, off); 347 - return n + 1; 348 - } 349 - 350 - static unsigned int setup_sgl(struct __kfifo *fifo, struct scatterlist *sgl, 351 - int nents, unsigned int len, unsigned int off) 352 - { 353 - unsigned int size = fifo->mask + 1; 354 - unsigned int esize = fifo->esize; 355 - unsigned int l; 356 - unsigned int n; 357 - 358 - off &= fifo->mask; 359 - if (esize != 1) { 360 - off *= esize; 361 - size *= esize; 362 - len *= esize; 363 - } 364 - l = min(len, size - off); 365 - 366 - n = setup_sgl_buf(sgl, fifo->data + off, nents, l); 367 - n += setup_sgl_buf(sgl + n, fifo->data, nents - n, len - l); 368 - 369 - if (n) 370 - sg_mark_end(sgl + n - 1); 371 - return n; 372 - } 373 - 374 - unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo, 375 - struct scatterlist *sgl, int nents, unsigned int len) 376 - { 377 - unsigned int l; 378 - 379 - l = kfifo_unused(fifo); 380 - if (len > l) 381 - len = l; 382 - 383 - return setup_sgl(fifo, sgl, nents, len, fifo->in); 384 - } 385 - EXPORT_SYMBOL(__kfifo_dma_in_prepare); 386 - 387 - unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo, 388 - struct scatterlist *sgl, int nents, unsigned int len) 389 - { 390 - unsigned int l; 391 - 392 - l = fifo->in - fifo->out; 393 - if (len > l) 394 - len = l; 395 - 396 - return setup_sgl(fifo, sgl, nents, len, fifo->out); 397 - } 398 - EXPORT_SYMBOL(__kfifo_dma_out_prepare); 399 - 400 - unsigned int __kfifo_max_r(unsigned int len, size_t recsize) 401 - { 402 - unsigned int max = (1 << (recsize << 3)) - 1; 403 - 404 - if (len > max) 405 - return max; 406 - return len; 407 - } 408 - 409 - #define __KFIFO_PEEK(data, out, mask) \ 410 - ((data)[(out) & (mask)]) 411 - /* 412 - * __kfifo_peek_n internal helper function for determinate the length of 413 - * the next record in the fifo 414 - */ 415 - static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize) 416 - { 417 - unsigned int l; 418 - unsigned int mask = fifo->mask; 419 - unsigned char *data = fifo->data; 420 - 421 - l = __KFIFO_PEEK(data, fifo->out, mask); 422 - 423 - if (--recsize) 424 - l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8; 425 - 426 - return l; 427 - } 428 - 429 - #define __KFIFO_POKE(data, in, mask, val) \ 430 - ( \ 431 - (data)[(in) & (mask)] = (unsigned char)(val) \ 432 - ) 433 - 434 - /* 435 - * __kfifo_poke_n internal helper function for storeing the length of 436 - * the record into the fifo 437 - */ 438 - static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize) 439 - { 440 - unsigned int mask = fifo->mask; 441 - unsigned char *data = fifo->data; 442 - 443 - __KFIFO_POKE(data, fifo->in, mask, n); 444 - 445 - if (recsize > 1) 446 - __KFIFO_POKE(data, fifo->in + 1, mask, n >> 8); 447 - } 448 - 449 - unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize) 450 - { 451 - return __kfifo_peek_n(fifo, recsize); 452 - } 453 - EXPORT_SYMBOL(__kfifo_len_r); 454 - 455 - unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf, 456 - unsigned int len, size_t recsize) 457 - { 458 - if (len + recsize > kfifo_unused(fifo)) 459 - return 0; 460 - 461 - __kfifo_poke_n(fifo, len, recsize); 462 - 463 - kfifo_copy_in(fifo, buf, len, fifo->in + recsize); 464 - fifo->in += len + recsize; 465 - return len; 466 - } 467 - EXPORT_SYMBOL(__kfifo_in_r); 468 - 469 - static unsigned int kfifo_out_copy_r(struct __kfifo *fifo, 470 - void *buf, unsigned int len, size_t recsize, unsigned int *n) 471 - { 472 - *n = __kfifo_peek_n(fifo, recsize); 473 - 474 - if (len > *n) 475 - len = *n; 476 - 477 - kfifo_copy_out(fifo, buf, len, fifo->out + recsize); 478 - return len; 479 - } 480 - 481 - unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf, 482 - unsigned int len, size_t recsize) 483 - { 484 - unsigned int n; 485 - 486 - if (fifo->in == fifo->out) 487 - return 0; 488 - 489 - return kfifo_out_copy_r(fifo, buf, len, recsize, &n); 490 - } 491 - EXPORT_SYMBOL(__kfifo_out_peek_r); 492 - 493 - unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf, 494 - unsigned int len, size_t recsize) 495 - { 496 - unsigned int n; 497 - 498 - if (fifo->in == fifo->out) 499 - return 0; 500 - 501 - len = kfifo_out_copy_r(fifo, buf, len, recsize, &n); 502 - fifo->out += n + recsize; 503 - return len; 504 - } 505 - EXPORT_SYMBOL(__kfifo_out_r); 506 - 507 - int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from, 508 - unsigned long len, unsigned int *copied, size_t recsize) 509 - { 510 - unsigned long ret; 511 - 512 - len = __kfifo_max_r(len, recsize); 513 - 514 - if (len + recsize > kfifo_unused(fifo)) { 515 - *copied = 0; 516 - return 0; 517 - } 518 - 519 - __kfifo_poke_n(fifo, len, recsize); 520 - 521 - ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied); 522 - if (unlikely(ret)) { 523 - *copied = 0; 524 - return -EFAULT; 525 - } 526 - fifo->in += len + recsize; 527 - return 0; 528 - } 529 - EXPORT_SYMBOL(__kfifo_from_user_r); 530 - 531 - int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to, 532 - unsigned long len, unsigned int *copied, size_t recsize) 533 - { 534 - unsigned long ret; 535 - unsigned int n; 536 - 537 - if (fifo->in == fifo->out) { 538 - *copied = 0; 539 - return 0; 540 - } 541 - 542 - n = __kfifo_peek_n(fifo, recsize); 543 - if (len > n) 544 - len = n; 545 - 546 - ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied); 547 - if (unlikely(ret)) { 548 - *copied = 0; 549 - return -EFAULT; 550 - } 551 - fifo->out += n + recsize; 552 - return 0; 553 - } 554 - EXPORT_SYMBOL(__kfifo_to_user_r); 555 - 556 - unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo, 557 - struct scatterlist *sgl, int nents, unsigned int len, size_t recsize) 558 - { 559 - if (!nents) 560 - BUG(); 561 - 562 - len = __kfifo_max_r(len, recsize); 563 - 564 - if (len + recsize > kfifo_unused(fifo)) 565 - return 0; 566 - 567 - return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize); 568 - } 569 - EXPORT_SYMBOL(__kfifo_dma_in_prepare_r); 570 - 571 - void __kfifo_dma_in_finish_r(struct __kfifo *fifo, 572 - unsigned int len, size_t recsize) 573 - { 574 - len = __kfifo_max_r(len, recsize); 575 - __kfifo_poke_n(fifo, len, recsize); 576 - fifo->in += len + recsize; 577 - } 578 - EXPORT_SYMBOL(__kfifo_dma_in_finish_r); 579 - 580 - unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo, 581 - struct scatterlist *sgl, int nents, unsigned int len, size_t recsize) 582 - { 583 - if (!nents) 584 - BUG(); 585 - 586 - len = __kfifo_max_r(len, recsize); 587 - 588 - if (len + recsize > fifo->in - fifo->out) 589 - return 0; 590 - 591 - return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize); 592 - } 593 - EXPORT_SYMBOL(__kfifo_dma_out_prepare_r); 594 - 595 - void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize) 596 - { 597 - unsigned int len; 598 - 599 - len = __kfifo_peek_n(fifo, recsize); 600 - fifo->out += len + recsize; 601 - } 602 - EXPORT_SYMBOL(__kfifo_dma_out_finish_r);
+514 -357
kernel/kfifo.c
··· 1 1 /* 2 - * A generic kernel FIFO implementation. 2 + * A generic kernel FIFO implementation 3 3 * 4 - * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net> 5 - * Copyright (C) 2004 Stelian Pop <stelian@popies.net> 4 + * Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net> 6 5 * 7 6 * This program is free software; you can redistribute it and/or modify 8 7 * it under the terms of the GNU General Public License as published by ··· 23 24 #include <linux/module.h> 24 25 #include <linux/slab.h> 25 26 #include <linux/err.h> 26 - #include <linux/kfifo.h> 27 27 #include <linux/log2.h> 28 28 #include <linux/uaccess.h> 29 + #include <linux/kfifo.h> 29 30 30 - static void _kfifo_init(struct kfifo *fifo, void *buffer, 31 - unsigned int size) 31 + /* 32 + * internal helper to calculate the unused elements in a fifo 33 + */ 34 + static inline unsigned int kfifo_unused(struct __kfifo *fifo) 32 35 { 33 - fifo->buffer = buffer; 34 - fifo->size = size; 35 - 36 - kfifo_reset(fifo); 36 + return (fifo->mask + 1) - (fifo->in - fifo->out); 37 37 } 38 38 39 - /** 40 - * kfifo_init - initialize a FIFO using a preallocated buffer 41 - * @fifo: the fifo to assign the buffer 42 - * @buffer: the preallocated buffer to be used. 43 - * @size: the size of the internal buffer, this has to be a power of 2. 44 - * 45 - */ 46 - void kfifo_init(struct kfifo *fifo, void *buffer, unsigned int size) 39 + int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, 40 + size_t esize, gfp_t gfp_mask) 47 41 { 48 - /* size must be a power of 2 */ 49 - BUG_ON(!is_power_of_2(size)); 50 - 51 - _kfifo_init(fifo, buffer, size); 52 - } 53 - EXPORT_SYMBOL(kfifo_init); 54 - 55 - /** 56 - * kfifo_alloc - allocates a new FIFO internal buffer 57 - * @fifo: the fifo to assign then new buffer 58 - * @size: the size of the buffer to be allocated, this have to be a power of 2. 59 - * @gfp_mask: get_free_pages mask, passed to kmalloc() 60 - * 61 - * This function dynamically allocates a new fifo internal buffer 62 - * 63 - * The size will be rounded-up to a power of 2. 64 - * The buffer will be release with kfifo_free(). 65 - * Return 0 if no error, otherwise the an error code 66 - */ 67 - int kfifo_alloc(struct kfifo *fifo, unsigned int size, gfp_t gfp_mask) 68 - { 69 - unsigned char *buffer; 70 - 71 42 /* 72 - * round up to the next power of 2, since our 'let the indices 43 + * round down to the next power of 2, since our 'let the indices 73 44 * wrap' technique works only in this case. 74 45 */ 75 - if (!is_power_of_2(size)) { 76 - BUG_ON(size > 0x80000000); 77 - size = roundup_pow_of_two(size); 46 + if (!is_power_of_2(size)) 47 + size = rounddown_pow_of_two(size); 48 + 49 + fifo->in = 0; 50 + fifo->out = 0; 51 + fifo->esize = esize; 52 + 53 + if (size < 2) { 54 + fifo->data = NULL; 55 + fifo->mask = 0; 56 + return -EINVAL; 78 57 } 79 58 80 - buffer = kmalloc(size, gfp_mask); 81 - if (!buffer) { 82 - _kfifo_init(fifo, NULL, 0); 59 + fifo->data = kmalloc(size * esize, gfp_mask); 60 + 61 + if (!fifo->data) { 62 + fifo->mask = 0; 83 63 return -ENOMEM; 84 64 } 85 - 86 - _kfifo_init(fifo, buffer, size); 65 + fifo->mask = size - 1; 87 66 88 67 return 0; 89 68 } 90 - EXPORT_SYMBOL(kfifo_alloc); 69 + EXPORT_SYMBOL(__kfifo_alloc); 91 70 92 - /** 93 - * kfifo_free - frees the FIFO internal buffer 94 - * @fifo: the fifo to be freed. 95 - */ 96 - void kfifo_free(struct kfifo *fifo) 71 + void __kfifo_free(struct __kfifo *fifo) 97 72 { 98 - kfree(fifo->buffer); 99 - _kfifo_init(fifo, NULL, 0); 73 + kfree(fifo->data); 74 + fifo->in = 0; 75 + fifo->out = 0; 76 + fifo->esize = 0; 77 + fifo->data = NULL; 78 + fifo->mask = 0; 100 79 } 101 - EXPORT_SYMBOL(kfifo_free); 80 + EXPORT_SYMBOL(__kfifo_free); 102 81 103 - /** 104 - * kfifo_skip - skip output data 105 - * @fifo: the fifo to be used. 106 - * @len: number of bytes to skip 107 - */ 108 - void kfifo_skip(struct kfifo *fifo, unsigned int len) 82 + int __kfifo_init(struct __kfifo *fifo, void *buffer, 83 + unsigned int size, size_t esize) 109 84 { 110 - if (len < kfifo_len(fifo)) { 111 - __kfifo_add_out(fifo, len); 112 - return; 85 + size /= esize; 86 + 87 + if (!is_power_of_2(size)) 88 + size = rounddown_pow_of_two(size); 89 + 90 + fifo->in = 0; 91 + fifo->out = 0; 92 + fifo->esize = esize; 93 + fifo->data = buffer; 94 + 95 + if (size < 2) { 96 + fifo->mask = 0; 97 + return -EINVAL; 113 98 } 114 - kfifo_reset_out(fifo); 115 - } 116 - EXPORT_SYMBOL(kfifo_skip); 99 + fifo->mask = size - 1; 117 100 118 - static inline void __kfifo_in_data(struct kfifo *fifo, 119 - const void *from, unsigned int len, unsigned int off) 120 - { 121 - unsigned int l; 122 - 123 - /* 124 - * Ensure that we sample the fifo->out index -before- we 125 - * start putting bytes into the kfifo. 126 - */ 127 - 128 - smp_mb(); 129 - 130 - off = __kfifo_off(fifo, fifo->in + off); 131 - 132 - /* first put the data starting from fifo->in to buffer end */ 133 - l = min(len, fifo->size - off); 134 - memcpy(fifo->buffer + off, from, l); 135 - 136 - /* then put the rest (if any) at the beginning of the buffer */ 137 - memcpy(fifo->buffer, from + l, len - l); 138 - } 139 - 140 - static inline void __kfifo_out_data(struct kfifo *fifo, 141 - void *to, unsigned int len, unsigned int off) 142 - { 143 - unsigned int l; 144 - 145 - /* 146 - * Ensure that we sample the fifo->in index -before- we 147 - * start removing bytes from the kfifo. 148 - */ 149 - 150 - smp_rmb(); 151 - 152 - off = __kfifo_off(fifo, fifo->out + off); 153 - 154 - /* first get the data from fifo->out until the end of the buffer */ 155 - l = min(len, fifo->size - off); 156 - memcpy(to, fifo->buffer + off, l); 157 - 158 - /* then get the rest (if any) from the beginning of the buffer */ 159 - memcpy(to + l, fifo->buffer, len - l); 160 - } 161 - 162 - static inline int __kfifo_from_user_data(struct kfifo *fifo, 163 - const void __user *from, unsigned int len, unsigned int off, 164 - unsigned *lenout) 165 - { 166 - unsigned int l; 167 - int ret; 168 - 169 - /* 170 - * Ensure that we sample the fifo->out index -before- we 171 - * start putting bytes into the kfifo. 172 - */ 173 - 174 - smp_mb(); 175 - 176 - off = __kfifo_off(fifo, fifo->in + off); 177 - 178 - /* first put the data starting from fifo->in to buffer end */ 179 - l = min(len, fifo->size - off); 180 - ret = copy_from_user(fifo->buffer + off, from, l); 181 - if (unlikely(ret)) { 182 - *lenout = ret; 183 - return -EFAULT; 184 - } 185 - *lenout = l; 186 - 187 - /* then put the rest (if any) at the beginning of the buffer */ 188 - ret = copy_from_user(fifo->buffer, from + l, len - l); 189 - *lenout += ret ? ret : len - l; 190 - return ret ? -EFAULT : 0; 191 - } 192 - 193 - static inline int __kfifo_to_user_data(struct kfifo *fifo, 194 - void __user *to, unsigned int len, unsigned int off, unsigned *lenout) 195 - { 196 - unsigned int l; 197 - int ret; 198 - 199 - /* 200 - * Ensure that we sample the fifo->in index -before- we 201 - * start removing bytes from the kfifo. 202 - */ 203 - 204 - smp_rmb(); 205 - 206 - off = __kfifo_off(fifo, fifo->out + off); 207 - 208 - /* first get the data from fifo->out until the end of the buffer */ 209 - l = min(len, fifo->size - off); 210 - ret = copy_to_user(to, fifo->buffer + off, l); 211 - *lenout = l; 212 - if (unlikely(ret)) { 213 - *lenout -= ret; 214 - return -EFAULT; 215 - } 216 - 217 - /* then get the rest (if any) from the beginning of the buffer */ 218 - len -= l; 219 - ret = copy_to_user(to + l, fifo->buffer, len); 220 - if (unlikely(ret)) { 221 - *lenout += len - ret; 222 - return -EFAULT; 223 - } 224 - *lenout += len; 225 101 return 0; 226 102 } 103 + EXPORT_SYMBOL(__kfifo_init); 227 104 228 - unsigned int __kfifo_in_n(struct kfifo *fifo, 229 - const void *from, unsigned int len, unsigned int recsize) 105 + static void kfifo_copy_in(struct __kfifo *fifo, const void *src, 106 + unsigned int len, unsigned int off) 230 107 { 231 - if (kfifo_avail(fifo) < len + recsize) 232 - return len + 1; 108 + unsigned int size = fifo->mask + 1; 109 + unsigned int esize = fifo->esize; 110 + unsigned int l; 233 111 234 - __kfifo_in_data(fifo, from, len, recsize); 235 - return 0; 112 + off &= fifo->mask; 113 + if (esize != 1) { 114 + off *= esize; 115 + size *= esize; 116 + len *= esize; 117 + } 118 + l = min(len, size - off); 119 + 120 + memcpy(fifo->data + off, src, l); 121 + memcpy(fifo->data, src + l, len - l); 122 + /* 123 + * make sure that the data in the fifo is up to date before 124 + * incrementing the fifo->in index counter 125 + */ 126 + smp_wmb(); 236 127 } 237 - EXPORT_SYMBOL(__kfifo_in_n); 238 128 239 - /** 240 - * kfifo_in - puts some data into the FIFO 241 - * @fifo: the fifo to be used. 242 - * @from: the data to be added. 243 - * @len: the length of the data to be added. 244 - * 245 - * This function copies at most @len bytes from the @from buffer into 246 - * the FIFO depending on the free space, and returns the number of 247 - * bytes copied. 248 - * 249 - * Note that with only one concurrent reader and one concurrent 250 - * writer, you don't need extra locking to use these functions. 251 - */ 252 - unsigned int kfifo_in(struct kfifo *fifo, const void *from, 253 - unsigned int len) 129 + unsigned int __kfifo_in(struct __kfifo *fifo, 130 + const void *buf, unsigned int len) 254 131 { 255 - len = min(kfifo_avail(fifo), len); 132 + unsigned int l; 256 133 257 - __kfifo_in_data(fifo, from, len, 0); 258 - __kfifo_add_in(fifo, len); 134 + l = kfifo_unused(fifo); 135 + if (len > l) 136 + len = l; 137 + 138 + kfifo_copy_in(fifo, buf, len, fifo->in); 139 + fifo->in += len; 259 140 return len; 260 141 } 261 - EXPORT_SYMBOL(kfifo_in); 142 + EXPORT_SYMBOL(__kfifo_in); 262 143 263 - unsigned int __kfifo_in_generic(struct kfifo *fifo, 264 - const void *from, unsigned int len, unsigned int recsize) 144 + static void kfifo_copy_out(struct __kfifo *fifo, void *dst, 145 + unsigned int len, unsigned int off) 265 146 { 266 - return __kfifo_in_rec(fifo, from, len, recsize); 147 + unsigned int size = fifo->mask + 1; 148 + unsigned int esize = fifo->esize; 149 + unsigned int l; 150 + 151 + off &= fifo->mask; 152 + if (esize != 1) { 153 + off *= esize; 154 + size *= esize; 155 + len *= esize; 156 + } 157 + l = min(len, size - off); 158 + 159 + memcpy(dst, fifo->data + off, l); 160 + memcpy(dst + l, fifo->data, len - l); 161 + /* 162 + * make sure that the data is copied before 163 + * incrementing the fifo->out index counter 164 + */ 165 + smp_wmb(); 267 166 } 268 - EXPORT_SYMBOL(__kfifo_in_generic); 269 167 270 - unsigned int __kfifo_out_n(struct kfifo *fifo, 271 - void *to, unsigned int len, unsigned int recsize) 168 + unsigned int __kfifo_out_peek(struct __kfifo *fifo, 169 + void *buf, unsigned int len) 272 170 { 273 - if (kfifo_len(fifo) < len + recsize) 274 - return len; 171 + unsigned int l; 275 172 276 - __kfifo_out_data(fifo, to, len, recsize); 277 - __kfifo_add_out(fifo, len + recsize); 278 - return 0; 279 - } 280 - EXPORT_SYMBOL(__kfifo_out_n); 173 + l = fifo->in - fifo->out; 174 + if (len > l) 175 + len = l; 281 176 282 - /** 283 - * kfifo_out - gets some data from the FIFO 284 - * @fifo: the fifo to be used. 285 - * @to: where the data must be copied. 286 - * @len: the size of the destination buffer. 287 - * 288 - * This function copies at most @len bytes from the FIFO into the 289 - * @to buffer and returns the number of copied bytes. 290 - * 291 - * Note that with only one concurrent reader and one concurrent 292 - * writer, you don't need extra locking to use these functions. 293 - */ 294 - unsigned int kfifo_out(struct kfifo *fifo, void *to, unsigned int len) 295 - { 296 - len = min(kfifo_len(fifo), len); 297 - 298 - __kfifo_out_data(fifo, to, len, 0); 299 - __kfifo_add_out(fifo, len); 300 - 177 + kfifo_copy_out(fifo, buf, len, fifo->out); 301 178 return len; 302 179 } 303 - EXPORT_SYMBOL(kfifo_out); 180 + EXPORT_SYMBOL(__kfifo_out_peek); 304 181 305 - /** 306 - * kfifo_out_peek - copy some data from the FIFO, but do not remove it 307 - * @fifo: the fifo to be used. 308 - * @to: where the data must be copied. 309 - * @len: the size of the destination buffer. 310 - * @offset: offset into the fifo 311 - * 312 - * This function copies at most @len bytes at @offset from the FIFO 313 - * into the @to buffer and returns the number of copied bytes. 314 - * The data is not removed from the FIFO. 315 - */ 316 - unsigned int kfifo_out_peek(struct kfifo *fifo, void *to, unsigned int len, 317 - unsigned offset) 182 + unsigned int __kfifo_out(struct __kfifo *fifo, 183 + void *buf, unsigned int len) 318 184 { 319 - len = min(kfifo_len(fifo), len + offset); 320 - 321 - __kfifo_out_data(fifo, to, len, offset); 185 + len = __kfifo_out_peek(fifo, buf, len); 186 + fifo->out += len; 322 187 return len; 323 188 } 324 - EXPORT_SYMBOL(kfifo_out_peek); 189 + EXPORT_SYMBOL(__kfifo_out); 325 190 326 - unsigned int __kfifo_out_generic(struct kfifo *fifo, 327 - void *to, unsigned int len, unsigned int recsize, 328 - unsigned int *total) 191 + static unsigned long kfifo_copy_from_user(struct __kfifo *fifo, 192 + const void __user *from, unsigned int len, unsigned int off, 193 + unsigned int *copied) 329 194 { 330 - return __kfifo_out_rec(fifo, to, len, recsize, total); 331 - } 332 - EXPORT_SYMBOL(__kfifo_out_generic); 195 + unsigned int size = fifo->mask + 1; 196 + unsigned int esize = fifo->esize; 197 + unsigned int l; 198 + unsigned long ret; 333 199 334 - unsigned int __kfifo_from_user_n(struct kfifo *fifo, 335 - const void __user *from, unsigned int len, unsigned int recsize) 336 - { 337 - unsigned total; 200 + off &= fifo->mask; 201 + if (esize != 1) { 202 + off *= esize; 203 + size *= esize; 204 + len *= esize; 205 + } 206 + l = min(len, size - off); 338 207 339 - if (kfifo_avail(fifo) < len + recsize) 340 - return len + 1; 341 - 342 - __kfifo_from_user_data(fifo, from, len, recsize, &total); 343 - return total; 344 - } 345 - EXPORT_SYMBOL(__kfifo_from_user_n); 346 - 347 - /** 348 - * kfifo_from_user - puts some data from user space into the FIFO 349 - * @fifo: the fifo to be used. 350 - * @from: pointer to the data to be added. 351 - * @len: the length of the data to be added. 352 - * @total: the actual returned data length. 353 - * 354 - * This function copies at most @len bytes from the @from into the 355 - * FIFO depending and returns -EFAULT/0. 356 - * 357 - * Note that with only one concurrent reader and one concurrent 358 - * writer, you don't need extra locking to use these functions. 359 - */ 360 - int kfifo_from_user(struct kfifo *fifo, 361 - const void __user *from, unsigned int len, unsigned *total) 362 - { 363 - int ret; 364 - len = min(kfifo_avail(fifo), len); 365 - ret = __kfifo_from_user_data(fifo, from, len, 0, total); 366 - if (ret) 367 - return ret; 368 - __kfifo_add_in(fifo, len); 369 - return 0; 370 - } 371 - EXPORT_SYMBOL(kfifo_from_user); 372 - 373 - unsigned int __kfifo_from_user_generic(struct kfifo *fifo, 374 - const void __user *from, unsigned int len, unsigned int recsize) 375 - { 376 - return __kfifo_from_user_rec(fifo, from, len, recsize); 377 - } 378 - EXPORT_SYMBOL(__kfifo_from_user_generic); 379 - 380 - unsigned int __kfifo_to_user_n(struct kfifo *fifo, 381 - void __user *to, unsigned int len, unsigned int reclen, 382 - unsigned int recsize) 383 - { 384 - unsigned int ret, total; 385 - 386 - if (kfifo_len(fifo) < reclen + recsize) 387 - return len; 388 - 389 - ret = __kfifo_to_user_data(fifo, to, reclen, recsize, &total); 390 - 391 - if (likely(ret == 0)) 392 - __kfifo_add_out(fifo, reclen + recsize); 393 - 394 - return total; 395 - } 396 - EXPORT_SYMBOL(__kfifo_to_user_n); 397 - 398 - /** 399 - * kfifo_to_user - gets data from the FIFO and write it to user space 400 - * @fifo: the fifo to be used. 401 - * @to: where the data must be copied. 402 - * @len: the size of the destination buffer. 403 - * @lenout: pointer to output variable with copied data 404 - * 405 - * This function copies at most @len bytes from the FIFO into the 406 - * @to buffer and 0 or -EFAULT. 407 - * 408 - * Note that with only one concurrent reader and one concurrent 409 - * writer, you don't need extra locking to use these functions. 410 - */ 411 - int kfifo_to_user(struct kfifo *fifo, 412 - void __user *to, unsigned int len, unsigned *lenout) 413 - { 414 - int ret; 415 - len = min(kfifo_len(fifo), len); 416 - ret = __kfifo_to_user_data(fifo, to, len, 0, lenout); 417 - __kfifo_add_out(fifo, *lenout); 208 + ret = copy_from_user(fifo->data + off, from, l); 209 + if (unlikely(ret)) 210 + ret = DIV_ROUND_UP(ret + len - l, esize); 211 + else { 212 + ret = copy_from_user(fifo->data, from + l, len - l); 213 + if (unlikely(ret)) 214 + ret = DIV_ROUND_UP(ret, esize); 215 + } 216 + /* 217 + * make sure that the data in the fifo is up to date before 218 + * incrementing the fifo->in index counter 219 + */ 220 + smp_wmb(); 221 + *copied = len - ret; 222 + /* return the number of elements which are not copied */ 418 223 return ret; 419 224 } 420 - EXPORT_SYMBOL(kfifo_to_user); 421 225 422 - unsigned int __kfifo_to_user_generic(struct kfifo *fifo, 423 - void __user *to, unsigned int len, unsigned int recsize, 424 - unsigned int *total) 226 + int __kfifo_from_user(struct __kfifo *fifo, const void __user *from, 227 + unsigned long len, unsigned int *copied) 425 228 { 426 - return __kfifo_to_user_rec(fifo, to, len, recsize, total); 229 + unsigned int l; 230 + unsigned long ret; 231 + unsigned int esize = fifo->esize; 232 + int err; 233 + 234 + if (esize != 1) 235 + len /= esize; 236 + 237 + l = kfifo_unused(fifo); 238 + if (len > l) 239 + len = l; 240 + 241 + ret = kfifo_copy_from_user(fifo, from, len, fifo->in, copied); 242 + if (unlikely(ret)) { 243 + len -= ret; 244 + err = -EFAULT; 245 + } else 246 + err = 0; 247 + fifo->in += len; 248 + return err; 427 249 } 428 - EXPORT_SYMBOL(__kfifo_to_user_generic); 250 + EXPORT_SYMBOL(__kfifo_from_user); 429 251 430 - unsigned int __kfifo_peek_generic(struct kfifo *fifo, unsigned int recsize) 252 + static unsigned long kfifo_copy_to_user(struct __kfifo *fifo, void __user *to, 253 + unsigned int len, unsigned int off, unsigned int *copied) 431 254 { 432 - if (recsize == 0) 433 - return kfifo_avail(fifo); 255 + unsigned int l; 256 + unsigned long ret; 257 + unsigned int size = fifo->mask + 1; 258 + unsigned int esize = fifo->esize; 434 259 260 + off &= fifo->mask; 261 + if (esize != 1) { 262 + off *= esize; 263 + size *= esize; 264 + len *= esize; 265 + } 266 + l = min(len, size - off); 267 + 268 + ret = copy_to_user(to, fifo->data + off, l); 269 + if (unlikely(ret)) 270 + ret = DIV_ROUND_UP(ret + len - l, esize); 271 + else { 272 + ret = copy_to_user(to + l, fifo->data, len - l); 273 + if (unlikely(ret)) 274 + ret = DIV_ROUND_UP(ret, esize); 275 + } 276 + /* 277 + * make sure that the data is copied before 278 + * incrementing the fifo->out index counter 279 + */ 280 + smp_wmb(); 281 + *copied = len - ret; 282 + /* return the number of elements which are not copied */ 283 + return ret; 284 + } 285 + 286 + int __kfifo_to_user(struct __kfifo *fifo, void __user *to, 287 + unsigned long len, unsigned int *copied) 288 + { 289 + unsigned int l; 290 + unsigned long ret; 291 + unsigned int esize = fifo->esize; 292 + int err; 293 + 294 + if (esize != 1) 295 + len /= esize; 296 + 297 + l = fifo->in - fifo->out; 298 + if (len > l) 299 + len = l; 300 + ret = kfifo_copy_to_user(fifo, to, len, fifo->out, copied); 301 + if (unlikely(ret)) { 302 + len -= ret; 303 + err = -EFAULT; 304 + } else 305 + err = 0; 306 + fifo->out += len; 307 + return err; 308 + } 309 + EXPORT_SYMBOL(__kfifo_to_user); 310 + 311 + static int setup_sgl_buf(struct scatterlist *sgl, void *buf, 312 + int nents, unsigned int len) 313 + { 314 + int n; 315 + unsigned int l; 316 + unsigned int off; 317 + struct page *page; 318 + 319 + if (!nents) 320 + return 0; 321 + 322 + if (!len) 323 + return 0; 324 + 325 + n = 0; 326 + page = virt_to_page(buf); 327 + off = offset_in_page(buf); 328 + l = 0; 329 + 330 + while (len >= l + PAGE_SIZE - off) { 331 + struct page *npage; 332 + 333 + l += PAGE_SIZE; 334 + buf += PAGE_SIZE; 335 + npage = virt_to_page(buf); 336 + if (page_to_phys(page) != page_to_phys(npage) - l) { 337 + sgl->page_link = 0; 338 + sg_set_page(sgl++, page, l - off, off); 339 + if (++n == nents) 340 + return n; 341 + page = npage; 342 + len -= l - off; 343 + l = off = 0; 344 + } 345 + } 346 + sgl->page_link = 0; 347 + sg_set_page(sgl++, page, len, off); 348 + return n + 1; 349 + } 350 + 351 + static unsigned int setup_sgl(struct __kfifo *fifo, struct scatterlist *sgl, 352 + int nents, unsigned int len, unsigned int off) 353 + { 354 + unsigned int size = fifo->mask + 1; 355 + unsigned int esize = fifo->esize; 356 + unsigned int l; 357 + unsigned int n; 358 + 359 + off &= fifo->mask; 360 + if (esize != 1) { 361 + off *= esize; 362 + size *= esize; 363 + len *= esize; 364 + } 365 + l = min(len, size - off); 366 + 367 + n = setup_sgl_buf(sgl, fifo->data + off, nents, l); 368 + n += setup_sgl_buf(sgl + n, fifo->data, nents - n, len - l); 369 + 370 + if (n) 371 + sg_mark_end(sgl + n - 1); 372 + return n; 373 + } 374 + 375 + unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo, 376 + struct scatterlist *sgl, int nents, unsigned int len) 377 + { 378 + unsigned int l; 379 + 380 + l = kfifo_unused(fifo); 381 + if (len > l) 382 + len = l; 383 + 384 + return setup_sgl(fifo, sgl, nents, len, fifo->in); 385 + } 386 + EXPORT_SYMBOL(__kfifo_dma_in_prepare); 387 + 388 + unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo, 389 + struct scatterlist *sgl, int nents, unsigned int len) 390 + { 391 + unsigned int l; 392 + 393 + l = fifo->in - fifo->out; 394 + if (len > l) 395 + len = l; 396 + 397 + return setup_sgl(fifo, sgl, nents, len, fifo->out); 398 + } 399 + EXPORT_SYMBOL(__kfifo_dma_out_prepare); 400 + 401 + unsigned int __kfifo_max_r(unsigned int len, size_t recsize) 402 + { 403 + unsigned int max = (1 << (recsize << 3)) - 1; 404 + 405 + if (len > max) 406 + return max; 407 + return len; 408 + } 409 + 410 + #define __KFIFO_PEEK(data, out, mask) \ 411 + ((data)[(out) & (mask)]) 412 + /* 413 + * __kfifo_peek_n internal helper function for determinate the length of 414 + * the next record in the fifo 415 + */ 416 + static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize) 417 + { 418 + unsigned int l; 419 + unsigned int mask = fifo->mask; 420 + unsigned char *data = fifo->data; 421 + 422 + l = __KFIFO_PEEK(data, fifo->out, mask); 423 + 424 + if (--recsize) 425 + l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8; 426 + 427 + return l; 428 + } 429 + 430 + #define __KFIFO_POKE(data, in, mask, val) \ 431 + ( \ 432 + (data)[(in) & (mask)] = (unsigned char)(val) \ 433 + ) 434 + 435 + /* 436 + * __kfifo_poke_n internal helper function for storeing the length of 437 + * the record into the fifo 438 + */ 439 + static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize) 440 + { 441 + unsigned int mask = fifo->mask; 442 + unsigned char *data = fifo->data; 443 + 444 + __KFIFO_POKE(data, fifo->in, mask, n); 445 + 446 + if (recsize > 1) 447 + __KFIFO_POKE(data, fifo->in + 1, mask, n >> 8); 448 + } 449 + 450 + unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize) 451 + { 435 452 return __kfifo_peek_n(fifo, recsize); 436 453 } 437 - EXPORT_SYMBOL(__kfifo_peek_generic); 454 + EXPORT_SYMBOL(__kfifo_len_r); 438 455 439 - void __kfifo_skip_generic(struct kfifo *fifo, unsigned int recsize) 456 + unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf, 457 + unsigned int len, size_t recsize) 440 458 { 441 - __kfifo_skip_rec(fifo, recsize); 442 - } 443 - EXPORT_SYMBOL(__kfifo_skip_generic); 459 + if (len + recsize > kfifo_unused(fifo)) 460 + return 0; 444 461 462 + __kfifo_poke_n(fifo, len, recsize); 463 + 464 + kfifo_copy_in(fifo, buf, len, fifo->in + recsize); 465 + fifo->in += len + recsize; 466 + return len; 467 + } 468 + EXPORT_SYMBOL(__kfifo_in_r); 469 + 470 + static unsigned int kfifo_out_copy_r(struct __kfifo *fifo, 471 + void *buf, unsigned int len, size_t recsize, unsigned int *n) 472 + { 473 + *n = __kfifo_peek_n(fifo, recsize); 474 + 475 + if (len > *n) 476 + len = *n; 477 + 478 + kfifo_copy_out(fifo, buf, len, fifo->out + recsize); 479 + return len; 480 + } 481 + 482 + unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf, 483 + unsigned int len, size_t recsize) 484 + { 485 + unsigned int n; 486 + 487 + if (fifo->in == fifo->out) 488 + return 0; 489 + 490 + return kfifo_out_copy_r(fifo, buf, len, recsize, &n); 491 + } 492 + EXPORT_SYMBOL(__kfifo_out_peek_r); 493 + 494 + unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf, 495 + unsigned int len, size_t recsize) 496 + { 497 + unsigned int n; 498 + 499 + if (fifo->in == fifo->out) 500 + return 0; 501 + 502 + len = kfifo_out_copy_r(fifo, buf, len, recsize, &n); 503 + fifo->out += n + recsize; 504 + return len; 505 + } 506 + EXPORT_SYMBOL(__kfifo_out_r); 507 + 508 + int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from, 509 + unsigned long len, unsigned int *copied, size_t recsize) 510 + { 511 + unsigned long ret; 512 + 513 + len = __kfifo_max_r(len, recsize); 514 + 515 + if (len + recsize > kfifo_unused(fifo)) { 516 + *copied = 0; 517 + return 0; 518 + } 519 + 520 + __kfifo_poke_n(fifo, len, recsize); 521 + 522 + ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied); 523 + if (unlikely(ret)) { 524 + *copied = 0; 525 + return -EFAULT; 526 + } 527 + fifo->in += len + recsize; 528 + return 0; 529 + } 530 + EXPORT_SYMBOL(__kfifo_from_user_r); 531 + 532 + int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to, 533 + unsigned long len, unsigned int *copied, size_t recsize) 534 + { 535 + unsigned long ret; 536 + unsigned int n; 537 + 538 + if (fifo->in == fifo->out) { 539 + *copied = 0; 540 + return 0; 541 + } 542 + 543 + n = __kfifo_peek_n(fifo, recsize); 544 + if (len > n) 545 + len = n; 546 + 547 + ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied); 548 + if (unlikely(ret)) { 549 + *copied = 0; 550 + return -EFAULT; 551 + } 552 + fifo->out += n + recsize; 553 + return 0; 554 + } 555 + EXPORT_SYMBOL(__kfifo_to_user_r); 556 + 557 + unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo, 558 + struct scatterlist *sgl, int nents, unsigned int len, size_t recsize) 559 + { 560 + if (!nents) 561 + BUG(); 562 + 563 + len = __kfifo_max_r(len, recsize); 564 + 565 + if (len + recsize > kfifo_unused(fifo)) 566 + return 0; 567 + 568 + return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize); 569 + } 570 + EXPORT_SYMBOL(__kfifo_dma_in_prepare_r); 571 + 572 + void __kfifo_dma_in_finish_r(struct __kfifo *fifo, 573 + unsigned int len, size_t recsize) 574 + { 575 + len = __kfifo_max_r(len, recsize); 576 + __kfifo_poke_n(fifo, len, recsize); 577 + fifo->in += len + recsize; 578 + } 579 + EXPORT_SYMBOL(__kfifo_dma_in_finish_r); 580 + 581 + unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo, 582 + struct scatterlist *sgl, int nents, unsigned int len, size_t recsize) 583 + { 584 + if (!nents) 585 + BUG(); 586 + 587 + len = __kfifo_max_r(len, recsize); 588 + 589 + if (len + recsize > fifo->in - fifo->out) 590 + return 0; 591 + 592 + return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize); 593 + } 594 + EXPORT_SYMBOL(__kfifo_dma_out_prepare_r); 595 + 596 + void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize) 597 + { 598 + unsigned int len; 599 + 600 + len = __kfifo_peek_n(fifo, recsize); 601 + fifo->out += len + recsize; 602 + } 603 + EXPORT_SYMBOL(__kfifo_dma_out_finish_r);