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

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Andrea Righi and committed by
Linus Torvalds
a25effa4 d83a71c4

+152 -59
+6 -1
samples/kfifo/bytestream-example.c
··· 44 44 static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE); 45 45 #endif 46 46 47 - static unsigned char expected_result[FIFO_SIZE] = { 47 + static const unsigned char expected_result[FIFO_SIZE] = { 48 48 3, 4, 5, 6, 7, 8, 9, 0, 49 49 1, 20, 21, 22, 23, 24, 25, 26, 50 50 27, 28, 29, 30, 31, 32, 33, 34, ··· 90 90 91 91 printk(KERN_INFO "queue len: %u\n", kfifo_len(&test)); 92 92 93 + /* show the first value without removing from the fifo */ 94 + if (kfifo_peek(&test, &i)) 95 + printk(KERN_INFO "%d\n", i); 96 + 93 97 /* check the correctness of all values in the fifo */ 94 98 j = 0; 95 99 while (kfifo_get(&test, &i)) { 100 + printk(KERN_INFO "item = %d\n", i); 96 101 if (i != expected_result[j++]) { 97 102 printk(KERN_WARNING "value mismatch: test failed\n"); 98 103 return -EIO;
+75 -47
samples/kfifo/dma-example.c
··· 29 29 printk(KERN_INFO "DMA fifo test start\n"); 30 30 31 31 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; 34 34 } 35 35 36 36 printk(KERN_INFO "queue size: %u\n", kfifo_size(&fifo)); ··· 41 41 kfifo_put(&fifo, &i); 42 42 43 43 /* kick away first byte */ 44 - ret = kfifo_get(&fifo, &i); 44 + kfifo_skip(&fifo); 45 45 46 46 printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo)); 47 47 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 + */ 48 63 sg_init_table(sg, ARRAY_SIZE(sg)); 49 64 ret = kfifo_dma_in_prepare(&fifo, sg, ARRAY_SIZE(sg), FIFO_SIZE); 50 65 printk(KERN_INFO "DMA sgl entries: %d\n", ret); 51 - 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); 60 - 61 - if (sg_is_last(&sg[i])) 62 - break; 63 - } 64 - 65 - /* but here your code to setup and exectute the dma operation */ 66 - /* ... */ 67 - 68 - /* example: zero bytes received */ 69 - ret = 0; 70 - 71 - /* finish the dma operation and update the received data */ 72 - kfifo_dma_in_finish(&fifo, 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; 73 70 } 74 71 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); 79 + 80 + if (sg_is_last(&sg[i])) 81 + break; 82 + } 83 + 84 + /* put here your code to setup and exectute the dma operation */ 85 + /* ... */ 86 + 87 + /* example: zero bytes received */ 88 + ret = 0; 89 + 90 + /* finish the dma operation and update the received data */ 91 + kfifo_dma_in_finish(&fifo, ret); 92 + 93 + /* Prepare to transmit data, example: 8 bytes */ 75 94 ret = kfifo_dma_out_prepare(&fifo, sg, ARRAY_SIZE(sg), 8); 76 95 printk(KERN_INFO "DMA sgl entries: %d\n", ret); 77 - 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); 86 - 87 - if (sg_is_last(&sg[i])) 88 - break; 89 - } 90 - 91 - /* but here your code to setup and exectute the dma operation */ 92 - /* ... */ 93 - 94 - /* example: 5 bytes transmitted */ 95 - ret = 5; 96 - 97 - /* finish the dma operation and update the transmitted data */ 98 - kfifo_dma_out_finish(&fifo, 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; 99 100 } 100 101 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); 108 + 109 + if (sg_is_last(&sg[i])) 110 + break; 111 + } 112 + 113 + /* put here your code to setup and exectute the dma operation */ 114 + /* ... */ 115 + 116 + /* example: 5 bytes transmitted */ 117 + ret = 5; 118 + 119 + /* finish the dma operation and update the transmitted data */ 120 + kfifo_dma_out_finish(&fifo, ret); 121 + 122 + ret = kfifo_len(&fifo); 101 123 printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo)); 124 + 125 + if (ret != 7) { 126 + printk(KERN_WARNING "size mismatch: test failed"); 127 + return -EIO; 128 + } 129 + printk(KERN_INFO "test passed\n"); 102 130 103 131 return 0; 104 132 }
+35 -8
samples/kfifo/inttype-example.c
··· 44 44 static DEFINE_KFIFO(test, int, FIFO_SIZE); 45 45 #endif 46 46 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 + 47 54 static int __init testfunc(void) 48 55 { 49 56 int buf[6]; 50 - int i; 57 + int i, j; 51 58 unsigned int ret; 52 59 53 60 printk(KERN_INFO "int fifo test start\n"); ··· 73 66 ret = kfifo_in(&test, buf, ret); 74 67 printk(KERN_INFO "ret: %d\n", ret); 75 68 76 - for (i = 20; i != 30; i++) 77 - kfifo_put(&test, &i); 69 + /* skip first element of the fifo */ 70 + printk(KERN_INFO "skip 1st element\n"); 71 + kfifo_skip(&test); 72 + 73 + /* put values into the fifo until is full */ 74 + for (i = 20; kfifo_put(&test, &i); i++) 75 + ; 78 76 79 77 printk(KERN_INFO "queue len: %u\n", kfifo_len(&test)); 80 78 ··· 87 75 if (kfifo_peek(&test, &i)) 88 76 printk(KERN_INFO "%d\n", i); 89 77 90 - /* print out all values in the fifo */ 91 - while (kfifo_get(&test, &i)) 92 - printk("%d ", i); 93 - printk("\n"); 78 + /* check the correctness of all values in the fifo */ 79 + j = 0; 80 + while (kfifo_get(&test, &i)) { 81 + printk(KERN_INFO "item = %d\n", i); 82 + if (i != expected_result[j++]) { 83 + printk(KERN_WARNING "value mismatch: test failed\n"); 84 + return -EIO; 85 + } 86 + } 87 + if (j != ARRAY_SIZE(expected_result)) { 88 + printk(KERN_WARNING "size mismatch: test failed\n"); 89 + return -EIO; 90 + } 91 + printk(KERN_INFO "test passed\n"); 94 92 95 93 return 0; 96 94 } ··· 154 132 return ret; 155 133 } 156 134 #endif 157 - testfunc(); 135 + if (testfunc() < 0) { 136 + #ifdef DYNAMIC 137 + kfifo_free(&test); 138 + #endif 139 + return -EIO; 140 + } 158 141 159 142 if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) { 160 143 #ifdef DYNAMIC
+36 -3
samples/kfifo/record-example.c
··· 55 55 static mytest test; 56 56 #endif 57 57 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 + 58 71 static int __init testfunc(void) 59 72 { 60 73 char buf[100]; ··· 88 75 kfifo_in(&test, buf, i + 1); 89 76 } 90 77 78 + /* skip first element of the fifo */ 79 + printk(KERN_INFO "skip 1st element\n"); 80 + kfifo_skip(&test); 81 + 91 82 printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test)); 92 83 93 84 /* show the first record without removing from the fifo */ ··· 99 82 if (ret) 100 83 printk(KERN_INFO "%.*s\n", ret, buf); 101 84 102 - /* print out all records in the fifo */ 85 + /* check the correctness of all values in the fifo */ 86 + i = 0; 103 87 while (!kfifo_is_empty(&test)) { 104 88 ret = kfifo_out(&test, buf, sizeof(buf)); 105 - printk(KERN_INFO "%.*s\n", ret, buf); 89 + buf[ret] = '\0'; 90 + printk(KERN_INFO "item = %.*s\n", ret, buf); 91 + if (strcmp(buf, expected_result[i++])) { 92 + printk(KERN_WARNING "value mismatch: test failed\n"); 93 + return -EIO; 94 + } 106 95 } 96 + if (i != ARRAY_SIZE(expected_result)) { 97 + printk(KERN_WARNING "size mismatch: test failed\n"); 98 + return -EIO; 99 + } 100 + printk(KERN_INFO "test passed\n"); 107 101 108 102 return 0; 109 103 } ··· 170 142 #else 171 143 INIT_KFIFO(test); 172 144 #endif 173 - testfunc(); 145 + if (testfunc() < 0) { 146 + #ifdef DYNAMIC 147 + kfifo_free(&test); 148 + #endif 149 + return -EIO; 150 + } 174 151 175 152 if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) { 176 153 #ifdef DYNAMIC