Skip to content

Commit a25effa

Browse files
Andrea Righitorvalds
authored andcommitted
kfifo: add explicit error checking in all the examples
Provide a check in all the kfifo examples to validate the correct execution of each testcase. Signed-off-by: Andrea Righi <arighi@develer.com> Acked-by: Stefani Seibold <stefani@seibold.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent d83a71c commit a25effa

4 files changed

Lines changed: 144 additions & 51 deletions

File tree

‎samples/kfifo/bytestream-example.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static struct kfifo test;
4444
static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE);
4545
#endif
4646

47-
static unsigned char expected_result[FIFO_SIZE] = {
47+
static const unsigned char expected_result[FIFO_SIZE] = {
4848
3, 4, 5, 6, 7, 8, 9, 0,
4949
1, 20, 21, 22, 23, 24, 25, 26,
5050
27, 28, 29, 30, 31, 32, 33, 34,
@@ -90,9 +90,14 @@ static int __init testfunc(void)
9090

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

93+
/* show the first value without removing from the fifo */
94+
if (kfifo_peek(&test, &i))
95+
printk(KERN_INFO "%d\n", i);
96+
9397
/* check the correctness of all values in the fifo */
9498
j = 0;
9599
while (kfifo_get(&test, &i)) {
100+
printk(KERN_INFO "item = %d\n", i);
96101
if (i != expected_result[j++]) {
97102
printk(KERN_WARNING "value mismatch: test failed\n");
98103
return -EIO;

‎samples/kfifo/dma-example.c‎

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ static int __init example_init(void)
2929
printk(KERN_INFO "DMA fifo test start\n");
3030

3131
if (kfifo_alloc(&fifo, FIFO_SIZE, GFP_KERNEL)) {
32-
printk(KERN_ERR "error kfifo_alloc\n");
33-
return 1;
32+
printk(KERN_WARNING "error kfifo_alloc\n");
33+
return -ENOMEM;
3434
}
3535

3636
printk(KERN_INFO "queue size: %u\n", kfifo_size(&fifo));
@@ -41,65 +41,93 @@ static int __init example_init(void)
4141
kfifo_put(&fifo, &i);
4242

4343
/* kick away first byte */
44-
ret = kfifo_get(&fifo, &i);
44+
kfifo_skip(&fifo);
4545

4646
printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo));
4747

48+
/*
49+
* Configure the kfifo buffer to receive data from DMA input.
50+
*
51+
* .--------------------------------------.
52+
* | 0 | 1 | 2 | ... | 12 | 13 | ... | 31 |
53+
* |---|------------------|---------------|
54+
* \_/ \________________/ \_____________/
55+
* \ \ \
56+
* \ \_allocated data \
57+
* \_*free space* \_*free space*
58+
*
59+
* We need two different SG entries: one for the free space area at the
60+
* end of the kfifo buffer (19 bytes) and another for the first free
61+
* byte at the beginning, after the kfifo_skip().
62+
*/
4863
sg_init_table(sg, ARRAY_SIZE(sg));
4964
ret = kfifo_dma_in_prepare(&fifo, sg, ARRAY_SIZE(sg), FIFO_SIZE);
5065
printk(KERN_INFO "DMA sgl entries: %d\n", ret);
66+
if (!ret) {
67+
/* fifo is full and no sgl was created */
68+
printk(KERN_WARNING "error kfifo_dma_in_prepare\n");
69+
return -EIO;
70+
}
5171

52-
/* if 0 was returned, fifo is full and no sgl was created */
53-
if (ret) {
54-
printk(KERN_INFO "scatterlist for receive:\n");
55-
for (i = 0; i < ARRAY_SIZE(sg); i++) {
56-
printk(KERN_INFO
57-
"sg[%d] -> "
58-
"page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n",
59-
i, sg[i].page_link, sg[i].offset, sg[i].length);
72+
/* receive data */
73+
printk(KERN_INFO "scatterlist for receive:\n");
74+
for (i = 0; i < ARRAY_SIZE(sg); i++) {
75+
printk(KERN_INFO
76+
"sg[%d] -> "
77+
"page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n",
78+
i, sg[i].page_link, sg[i].offset, sg[i].length);
6079

61-
if (sg_is_last(&sg[i]))
62-
break;
63-
}
80+
if (sg_is_last(&sg[i]))
81+
break;
82+
}
6483

65-
/* but here your code to setup and exectute the dma operation */
66-
/* ... */
84+
/* put here your code to setup and exectute the dma operation */
85+
/* ... */
6786

68-
/* example: zero bytes received */
69-
ret = 0;
87+
/* example: zero bytes received */
88+
ret = 0;
7089

71-
/* finish the dma operation and update the received data */
72-
kfifo_dma_in_finish(&fifo, ret);
73-
}
90+
/* finish the dma operation and update the received data */
91+
kfifo_dma_in_finish(&fifo, ret);
7492

93+
/* Prepare to transmit data, example: 8 bytes */
7594
ret = kfifo_dma_out_prepare(&fifo, sg, ARRAY_SIZE(sg), 8);
7695
printk(KERN_INFO "DMA sgl entries: %d\n", ret);
96+
if (!ret) {
97+
/* no data was available and no sgl was created */
98+
printk(KERN_WARNING "error kfifo_dma_out_prepare\n");
99+
return -EIO;
100+
}
77101

78-
/* if 0 was returned, no data was available and no sgl was created */
79-
if (ret) {
80-
printk(KERN_INFO "scatterlist for transmit:\n");
81-
for (i = 0; i < ARRAY_SIZE(sg); i++) {
82-
printk(KERN_INFO
83-
"sg[%d] -> "
84-
"page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n",
85-
i, sg[i].page_link, sg[i].offset, sg[i].length);
102+
printk(KERN_INFO "scatterlist for transmit:\n");
103+
for (i = 0; i < ARRAY_SIZE(sg); i++) {
104+
printk(KERN_INFO
105+
"sg[%d] -> "
106+
"page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n",
107+
i, sg[i].page_link, sg[i].offset, sg[i].length);
86108

87-
if (sg_is_last(&sg[i]))
88-
break;
89-
}
109+
if (sg_is_last(&sg[i]))
110+
break;
111+
}
90112

91-
/* but here your code to setup and exectute the dma operation */
92-
/* ... */
113+
/* put here your code to setup and exectute the dma operation */
114+
/* ... */
93115

94-
/* example: 5 bytes transmitted */
95-
ret = 5;
116+
/* example: 5 bytes transmitted */
117+
ret = 5;
96118

97-
/* finish the dma operation and update the transmitted data */
98-
kfifo_dma_out_finish(&fifo, ret);
99-
}
119+
/* finish the dma operation and update the transmitted data */
120+
kfifo_dma_out_finish(&fifo, ret);
100121

122+
ret = kfifo_len(&fifo);
101123
printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo));
102124

125+
if (ret != 7) {
126+
printk(KERN_WARNING "size mismatch: test failed");
127+
return -EIO;
128+
}
129+
printk(KERN_INFO "test passed\n");
130+
103131
return 0;
104132
}
105133

‎samples/kfifo/inttype-example.c‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,17 @@ static DECLARE_KFIFO_PTR(test, int);
4444
static DEFINE_KFIFO(test, int, FIFO_SIZE);
4545
#endif
4646

47+
static const int expected_result[FIFO_SIZE] = {
48+
3, 4, 5, 6, 7, 8, 9, 0,
49+
1, 20, 21, 22, 23, 24, 25, 26,
50+
27, 28, 29, 30, 31, 32, 33, 34,
51+
35, 36, 37, 38, 39, 40, 41, 42,
52+
};
53+
4754
static int __init testfunc(void)
4855
{
4956
int buf[6];
50-
int i;
57+
int i, j;
5158
unsigned int ret;
5259

5360
printk(KERN_INFO "int fifo test start\n");
@@ -66,19 +73,34 @@ static int __init testfunc(void)
6673
ret = kfifo_in(&test, buf, ret);
6774
printk(KERN_INFO "ret: %d\n", ret);
6875

69-
for (i = 20; i != 30; i++)
70-
kfifo_put(&test, &i);
76+
/* skip first element of the fifo */
77+
printk(KERN_INFO "skip 1st element\n");
78+
kfifo_skip(&test);
79+
80+
/* put values into the fifo until is full */
81+
for (i = 20; kfifo_put(&test, &i); i++)
82+
;
7183

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

7486
/* show the first value without removing from the fifo */
7587
if (kfifo_peek(&test, &i))
7688
printk(KERN_INFO "%d\n", i);
7789

78-
/* print out all values in the fifo */
79-
while (kfifo_get(&test, &i))
80-
printk("%d ", i);
81-
printk("\n");
90+
/* check the correctness of all values in the fifo */
91+
j = 0;
92+
while (kfifo_get(&test, &i)) {
93+
printk(KERN_INFO "item = %d\n", i);
94+
if (i != expected_result[j++]) {
95+
printk(KERN_WARNING "value mismatch: test failed\n");
96+
return -EIO;
97+
}
98+
}
99+
if (j != ARRAY_SIZE(expected_result)) {
100+
printk(KERN_WARNING "size mismatch: test failed\n");
101+
return -EIO;
102+
}
103+
printk(KERN_INFO "test passed\n");
82104

83105
return 0;
84106
}
@@ -132,7 +154,12 @@ static int __init example_init(void)
132154
return ret;
133155
}
134156
#endif
135-
testfunc();
157+
if (testfunc() < 0) {
158+
#ifdef DYNAMIC
159+
kfifo_free(&test);
160+
#endif
161+
return -EIO;
162+
}
136163

137164
if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
138165
#ifdef DYNAMIC

‎samples/kfifo/record-example.c‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ typedef STRUCT_KFIFO_REC_1(FIFO_SIZE) mytest;
5555
static mytest test;
5656
#endif
5757

58+
static const char *expected_result[] = {
59+
"a",
60+
"bb",
61+
"ccc",
62+
"dddd",
63+
"eeeee",
64+
"ffffff",
65+
"ggggggg",
66+
"hhhhhhhh",
67+
"iiiiiiiii",
68+
"jjjjjjjjjj",
69+
};
70+
5871
static int __init testfunc(void)
5972
{
6073
char buf[100];
@@ -75,18 +88,33 @@ static int __init testfunc(void)
7588
kfifo_in(&test, buf, i + 1);
7689
}
7790

91+
/* skip first element of the fifo */
92+
printk(KERN_INFO "skip 1st element\n");
93+
kfifo_skip(&test);
94+
7895
printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
7996

8097
/* show the first record without removing from the fifo */
8198
ret = kfifo_out_peek(&test, buf, sizeof(buf));
8299
if (ret)
83100
printk(KERN_INFO "%.*s\n", ret, buf);
84101

85-
/* print out all records in the fifo */
102+
/* check the correctness of all values in the fifo */
103+
i = 0;
86104
while (!kfifo_is_empty(&test)) {
87105
ret = kfifo_out(&test, buf, sizeof(buf));
88-
printk(KERN_INFO "%.*s\n", ret, buf);
106+
buf[ret] = '\0';
107+
printk(KERN_INFO "item = %.*s\n", ret, buf);
108+
if (strcmp(buf, expected_result[i++])) {
109+
printk(KERN_WARNING "value mismatch: test failed\n");
110+
return -EIO;
111+
}
112+
}
113+
if (i != ARRAY_SIZE(expected_result)) {
114+
printk(KERN_WARNING "size mismatch: test failed\n");
115+
return -EIO;
89116
}
117+
printk(KERN_INFO "test passed\n");
90118

91119
return 0;
92120
}
@@ -142,7 +170,12 @@ static int __init example_init(void)
142170
#else
143171
INIT_KFIFO(test);
144172
#endif
145-
testfunc();
173+
if (testfunc() < 0) {
174+
#ifdef DYNAMIC
175+
kfifo_free(&test);
176+
#endif
177+
return -EIO;
178+
}
146179

147180
if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
148181
#ifdef DYNAMIC

0 commit comments

Comments
 (0)