Merge tag 'sched-urgent-2020-12-27' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / samples / kfifo / record-example.c
blobc507998a2617cca9b3efa182ff86f2a313be2f1b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Sample dynamic sized record fifo implementation
5 * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net>
6 */
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/proc_fs.h>
11 #include <linux/mutex.h>
12 #include <linux/kfifo.h>
15 * This module shows how to create a variable sized record fifo.
18 /* fifo size in elements (bytes) */
19 #define FIFO_SIZE 128
21 /* name of the proc entry */
22 #define PROC_FIFO "record-fifo"
24 /* lock for procfs read access */
25 static DEFINE_MUTEX(read_lock);
27 /* lock for procfs write access */
28 static DEFINE_MUTEX(write_lock);
31 * define DYNAMIC in this example for a dynamically allocated fifo.
33 * Otherwise the fifo storage will be a part of the fifo structure.
35 #if 0
36 #define DYNAMIC
37 #endif
40 * struct kfifo_rec_ptr_1 and STRUCT_KFIFO_REC_1 can handle records of a
41 * length between 0 and 255 bytes.
43 * struct kfifo_rec_ptr_2 and STRUCT_KFIFO_REC_2 can handle records of a
44 * length between 0 and 65535 bytes.
47 #ifdef DYNAMIC
48 struct kfifo_rec_ptr_1 test;
50 #else
51 typedef STRUCT_KFIFO_REC_1(FIFO_SIZE) mytest;
53 static mytest test;
54 #endif
56 static const char *expected_result[] = {
57 "a",
58 "bb",
59 "ccc",
60 "dddd",
61 "eeeee",
62 "ffffff",
63 "ggggggg",
64 "hhhhhhhh",
65 "iiiiiiiii",
66 "jjjjjjjjjj",
69 static int __init testfunc(void)
71 char buf[100];
72 unsigned int i;
73 unsigned int ret;
74 struct { unsigned char buf[6]; } hello = { "hello" };
76 printk(KERN_INFO "record fifo test start\n");
78 kfifo_in(&test, &hello, sizeof(hello));
80 /* show the size of the next record in the fifo */
81 printk(KERN_INFO "fifo peek len: %u\n", kfifo_peek_len(&test));
83 /* put in variable length data */
84 for (i = 0; i < 10; i++) {
85 memset(buf, 'a' + i, i + 1);
86 kfifo_in(&test, buf, i + 1);
89 /* skip first element of the fifo */
90 printk(KERN_INFO "skip 1st element\n");
91 kfifo_skip(&test);
93 printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
95 /* show the first record without removing from the fifo */
96 ret = kfifo_out_peek(&test, buf, sizeof(buf));
97 if (ret)
98 printk(KERN_INFO "%.*s\n", ret, buf);
100 /* check the correctness of all values in the fifo */
101 i = 0;
102 while (!kfifo_is_empty(&test)) {
103 ret = kfifo_out(&test, buf, sizeof(buf));
104 buf[ret] = '\0';
105 printk(KERN_INFO "item = %.*s\n", ret, buf);
106 if (strcmp(buf, expected_result[i++])) {
107 printk(KERN_WARNING "value mismatch: test failed\n");
108 return -EIO;
111 if (i != ARRAY_SIZE(expected_result)) {
112 printk(KERN_WARNING "size mismatch: test failed\n");
113 return -EIO;
115 printk(KERN_INFO "test passed\n");
117 return 0;
120 static ssize_t fifo_write(struct file *file, const char __user *buf,
121 size_t count, loff_t *ppos)
123 int ret;
124 unsigned int copied;
126 if (mutex_lock_interruptible(&write_lock))
127 return -ERESTARTSYS;
129 ret = kfifo_from_user(&test, buf, count, &copied);
131 mutex_unlock(&write_lock);
133 return ret ? ret : copied;
136 static ssize_t fifo_read(struct file *file, char __user *buf,
137 size_t count, loff_t *ppos)
139 int ret;
140 unsigned int copied;
142 if (mutex_lock_interruptible(&read_lock))
143 return -ERESTARTSYS;
145 ret = kfifo_to_user(&test, buf, count, &copied);
147 mutex_unlock(&read_lock);
149 return ret ? ret : copied;
152 static const struct proc_ops fifo_proc_ops = {
153 .proc_read = fifo_read,
154 .proc_write = fifo_write,
155 .proc_lseek = noop_llseek,
158 static int __init example_init(void)
160 #ifdef DYNAMIC
161 int ret;
163 ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
164 if (ret) {
165 printk(KERN_ERR "error kfifo_alloc\n");
166 return ret;
168 #else
169 INIT_KFIFO(test);
170 #endif
171 if (testfunc() < 0) {
172 #ifdef DYNAMIC
173 kfifo_free(&test);
174 #endif
175 return -EIO;
178 if (proc_create(PROC_FIFO, 0, NULL, &fifo_proc_ops) == NULL) {
179 #ifdef DYNAMIC
180 kfifo_free(&test);
181 #endif
182 return -ENOMEM;
184 return 0;
187 static void __exit example_exit(void)
189 remove_proc_entry(PROC_FIFO, NULL);
190 #ifdef DYNAMIC
191 kfifo_free(&test);
192 #endif
195 module_init(example_init);
196 module_exit(example_exit);
197 MODULE_LICENSE("GPL");
198 MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");