Skip to content

Commit 498d319

Browse files
sstefanitorvalds
authored andcommitted
kfifo API type safety
This patch enhances the type safety for the kfifo API. It is now safe to put const data into a non const FIFO and the API will now generate a compiler warning when reading from the fifo where the destination address is pointing to a const variable. As a side effect the kfifo_put() does now expect the value of an element instead a pointer to the element. This was suggested Russell King. It make the handling of the kfifo_put easier since there is no need to create a helper variable for getting the address of a pointer or to pass integers of different sizes. IMHO the API break is okay, since there are currently only six users of kfifo_put(). The code is also cleaner by kicking out the "if (0)" expressions. [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Stefani Seibold <stefani@seibold.net> Cc: Russell King <rmk@arm.linux.org.uk> Cc: Hauke Mehrtens <hauke@hauke-m.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a019e48 commit 498d319

10 files changed

Lines changed: 25 additions & 44 deletions

File tree

‎drivers/gpu/drm/drm_flip_work.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*/
3535
void drm_flip_work_queue(struct drm_flip_work *work, void *val)
3636
{
37-
if (kfifo_put(&work->fifo, (const void **)&val)) {
37+
if (kfifo_put(&work->fifo, val)) {
3838
atomic_inc(&work->pending);
3939
} else {
4040
DRM_ERROR("%s fifo full!\n", work->name);

‎drivers/iio/industrialio-event.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
5656
ev.id = ev_code;
5757
ev.timestamp = timestamp;
5858

59-
copied = kfifo_put(&ev_int->det_events, &ev);
59+
copied = kfifo_put(&ev_int->det_events, ev);
6060
if (copied != 0)
6161
wake_up_locked_poll(&ev_int->wait, POLLIN);
6262
}

‎drivers/net/wireless/rt2x00/rt2800mmio.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ static void rt2800mmio_txstatus_interrupt(struct rt2x00_dev *rt2x00dev)
446446
if (!rt2x00_get_field32(status, TX_STA_FIFO_VALID))
447447
break;
448448

449-
if (!kfifo_put(&rt2x00dev->txstatus_fifo, &status)) {
449+
if (!kfifo_put(&rt2x00dev->txstatus_fifo, status)) {
450450
rt2x00_warn(rt2x00dev, "TX status FIFO overrun, drop tx status report\n");
451451
break;
452452
}

‎drivers/net/wireless/rt2x00/rt2800usb.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static bool rt2800usb_tx_sta_fifo_read_completed(struct rt2x00_dev *rt2x00dev,
164164

165165
valid = rt2x00_get_field32(tx_status, TX_STA_FIFO_VALID);
166166
if (valid) {
167-
if (!kfifo_put(&rt2x00dev->txstatus_fifo, &tx_status))
167+
if (!kfifo_put(&rt2x00dev->txstatus_fifo, tx_status))
168168
rt2x00_warn(rt2x00dev, "TX status FIFO overrun\n");
169169

170170
queue_work(rt2x00dev->workqueue, &rt2x00dev->txdone_work);

‎drivers/pci/pcie/aer/aerdrv_core.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ void aer_recover_queue(int domain, unsigned int bus, unsigned int devfn,
574574
};
575575

576576
spin_lock_irqsave(&aer_recover_ring_lock, flags);
577-
if (kfifo_put(&aer_recover_ring, &entry))
577+
if (kfifo_put(&aer_recover_ring, entry))
578578
schedule_work(&aer_recover_work);
579579
else
580580
pr_err("AER recover: Buffer overflow when recovering AER for %04x:%02x:%02x:%x\n",

‎include/linux/kfifo.h‎

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* A generic kernel FIFO implementation
33
*
4-
* Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net>
4+
* Copyright (C) 2013 Stefani Seibold <stefani@seibold.net>
55
*
66
* This program is free software; you can redistribute it and/or modify
77
* it under the terms of the GNU General Public License as published by
@@ -67,9 +67,10 @@ struct __kfifo {
6767
union { \
6868
struct __kfifo kfifo; \
6969
datatype *type; \
70+
const datatype *const_type; \
7071
char (*rectype)[recsize]; \
7172
ptrtype *ptr; \
72-
const ptrtype *ptr_const; \
73+
ptrtype const *ptr_const; \
7374
}
7475

7576
#define __STRUCT_KFIFO(type, size, recsize, ptrtype) \
@@ -386,16 +387,12 @@ __kfifo_int_must_check_helper( \
386387
#define kfifo_put(fifo, val) \
387388
({ \
388389
typeof((fifo) + 1) __tmp = (fifo); \
389-
typeof((val) + 1) __val = (val); \
390+
typeof(*__tmp->const_type) __val = (val); \
390391
unsigned int __ret; \
391-
const size_t __recsize = sizeof(*__tmp->rectype); \
392+
size_t __recsize = sizeof(*__tmp->rectype); \
392393
struct __kfifo *__kfifo = &__tmp->kfifo; \
393-
if (0) { \
394-
typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); \
395-
__dummy = (typeof(__val))NULL; \
396-
} \
397394
if (__recsize) \
398-
__ret = __kfifo_in_r(__kfifo, __val, sizeof(*__val), \
395+
__ret = __kfifo_in_r(__kfifo, &__val, sizeof(__val), \
399396
__recsize); \
400397
else { \
401398
__ret = !kfifo_is_full(__tmp); \
@@ -404,7 +401,7 @@ __kfifo_int_must_check_helper( \
404401
((typeof(__tmp->type))__kfifo->data) : \
405402
(__tmp->buf) \
406403
)[__kfifo->in & __tmp->kfifo.mask] = \
407-
*(typeof(__tmp->type))__val; \
404+
(typeof(*__tmp->type))__val; \
408405
smp_wmb(); \
409406
__kfifo->in++; \
410407
} \
@@ -415,7 +412,7 @@ __kfifo_int_must_check_helper( \
415412
/**
416413
* kfifo_get - get data from the fifo
417414
* @fifo: address of the fifo to be used
418-
* @val: the var where to store the data to be added
415+
* @val: address where to store the data
419416
*
420417
* This macro reads the data from the fifo.
421418
* It returns 0 if the fifo was empty. Otherwise it returns the number
@@ -428,12 +425,10 @@ __kfifo_int_must_check_helper( \
428425
__kfifo_uint_must_check_helper( \
429426
({ \
430427
typeof((fifo) + 1) __tmp = (fifo); \
431-
typeof((val) + 1) __val = (val); \
428+
typeof(__tmp->ptr) __val = (val); \
432429
unsigned int __ret; \
433430
const size_t __recsize = sizeof(*__tmp->rectype); \
434431
struct __kfifo *__kfifo = &__tmp->kfifo; \
435-
if (0) \
436-
__val = (typeof(__tmp->ptr))0; \
437432
if (__recsize) \
438433
__ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \
439434
__recsize); \
@@ -456,7 +451,7 @@ __kfifo_uint_must_check_helper( \
456451
/**
457452
* kfifo_peek - get data from the fifo without removing
458453
* @fifo: address of the fifo to be used
459-
* @val: the var where to store the data to be added
454+
* @val: address where to store the data
460455
*
461456
* This reads the data from the fifo without removing it from the fifo.
462457
* It returns 0 if the fifo was empty. Otherwise it returns the number
@@ -469,12 +464,10 @@ __kfifo_uint_must_check_helper( \
469464
__kfifo_uint_must_check_helper( \
470465
({ \
471466
typeof((fifo) + 1) __tmp = (fifo); \
472-
typeof((val) + 1) __val = (val); \
467+
typeof(__tmp->ptr) __val = (val); \
473468
unsigned int __ret; \
474469
const size_t __recsize = sizeof(*__tmp->rectype); \
475470
struct __kfifo *__kfifo = &__tmp->kfifo; \
476-
if (0) \
477-
__val = (typeof(__tmp->ptr))NULL; \
478471
if (__recsize) \
479472
__ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \
480473
__recsize); \
@@ -508,14 +501,10 @@ __kfifo_uint_must_check_helper( \
508501
#define kfifo_in(fifo, buf, n) \
509502
({ \
510503
typeof((fifo) + 1) __tmp = (fifo); \
511-
typeof((buf) + 1) __buf = (buf); \
504+
typeof(__tmp->ptr_const) __buf = (buf); \
512505
unsigned long __n = (n); \
513506
const size_t __recsize = sizeof(*__tmp->rectype); \
514507
struct __kfifo *__kfifo = &__tmp->kfifo; \
515-
if (0) { \
516-
typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); \
517-
__dummy = (typeof(__buf))NULL; \
518-
} \
519508
(__recsize) ?\
520509
__kfifo_in_r(__kfifo, __buf, __n, __recsize) : \
521510
__kfifo_in(__kfifo, __buf, __n); \
@@ -561,14 +550,10 @@ __kfifo_uint_must_check_helper( \
561550
__kfifo_uint_must_check_helper( \
562551
({ \
563552
typeof((fifo) + 1) __tmp = (fifo); \
564-
typeof((buf) + 1) __buf = (buf); \
553+
typeof(__tmp->ptr) __buf = (buf); \
565554
unsigned long __n = (n); \
566555
const size_t __recsize = sizeof(*__tmp->rectype); \
567556
struct __kfifo *__kfifo = &__tmp->kfifo; \
568-
if (0) { \
569-
typeof(__tmp->ptr) __dummy = NULL; \
570-
__buf = __dummy; \
571-
} \
572557
(__recsize) ?\
573558
__kfifo_out_r(__kfifo, __buf, __n, __recsize) : \
574559
__kfifo_out(__kfifo, __buf, __n); \
@@ -773,14 +758,10 @@ __kfifo_uint_must_check_helper( \
773758
__kfifo_uint_must_check_helper( \
774759
({ \
775760
typeof((fifo) + 1) __tmp = (fifo); \
776-
typeof((buf) + 1) __buf = (buf); \
761+
typeof(__tmp->ptr) __buf = (buf); \
777762
unsigned long __n = (n); \
778763
const size_t __recsize = sizeof(*__tmp->rectype); \
779764
struct __kfifo *__kfifo = &__tmp->kfifo; \
780-
if (0) { \
781-
typeof(__tmp->ptr) __dummy __attribute__ ((unused)) = NULL; \
782-
__buf = __dummy; \
783-
} \
784765
(__recsize) ? \
785766
__kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \
786767
__kfifo_out_peek(__kfifo, __buf, __n); \

‎mm/memory-failure.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,7 @@ void memory_failure_queue(unsigned long pfn, int trapno, int flags)
12691269

12701270
mf_cpu = &get_cpu_var(memory_failure_cpu);
12711271
spin_lock_irqsave(&mf_cpu->lock, proc_flags);
1272-
if (kfifo_put(&mf_cpu->fifo, &entry))
1272+
if (kfifo_put(&mf_cpu->fifo, entry))
12731273
schedule_work_on(smp_processor_id(), &mf_cpu->work);
12741274
else
12751275
pr_err("Memory failure: buffer overflow when queuing memory failure at %#lx\n",

‎samples/kfifo/bytestream-example.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static int __init testfunc(void)
6464

6565
/* put values into the fifo */
6666
for (i = 0; i != 10; i++)
67-
kfifo_put(&test, &i);
67+
kfifo_put(&test, i);
6868

6969
/* show the number of used elements */
7070
printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
@@ -85,7 +85,7 @@ static int __init testfunc(void)
8585
kfifo_skip(&test);
8686

8787
/* put values into the fifo until is full */
88-
for (i = 20; kfifo_put(&test, &i); i++)
88+
for (i = 20; kfifo_put(&test, i); i++)
8989
;
9090

9191
printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));

‎samples/kfifo/dma-example.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static int __init example_init(void)
3939
kfifo_in(&fifo, "test", 4);
4040

4141
for (i = 0; i != 9; i++)
42-
kfifo_put(&fifo, &i);
42+
kfifo_put(&fifo, i);
4343

4444
/* kick away first byte */
4545
kfifo_skip(&fifo);

‎samples/kfifo/inttype-example.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static int __init testfunc(void)
6161

6262
/* put values into the fifo */
6363
for (i = 0; i != 10; i++)
64-
kfifo_put(&test, &i);
64+
kfifo_put(&test, i);
6565

6666
/* show the number of used elements */
6767
printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
@@ -78,7 +78,7 @@ static int __init testfunc(void)
7878
kfifo_skip(&test);
7979

8080
/* put values into the fifo until is full */
81-
for (i = 20; kfifo_put(&test, &i); i++)
81+
for (i = 20; kfifo_put(&test, i); i++)
8282
;
8383

8484
printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));

0 commit comments

Comments
 (0)