diff options
Diffstat (limited to 'samples')
-rw-r--r-- | samples/Kconfig | 10 | ||||
-rw-r--r-- | samples/Makefile | 2 | ||||
-rw-r--r-- | samples/kfifo/Makefile | 1 | ||||
-rw-r--r-- | samples/kfifo/bytestream-example.c | 193 | ||||
-rw-r--r-- | samples/kfifo/dma-example.c | 143 | ||||
-rw-r--r-- | samples/kfifo/inttype-example.c | 184 | ||||
-rw-r--r-- | samples/kfifo/record-example.c | 200 | ||||
-rw-r--r-- | samples/kprobes/kprobe_example.c | 9 | ||||
-rw-r--r-- | samples/tracepoints/tp-samples-trace.h | 4 | ||||
-rw-r--r-- | samples/tracepoints/tracepoint-probe-sample.c | 13 | ||||
-rw-r--r-- | samples/tracepoints/tracepoint-probe-sample2.c | 7 |
11 files changed, 753 insertions, 13 deletions
diff --git a/samples/Kconfig b/samples/Kconfig index 8924f72f0629..954a1d550c5f 100644 --- a/samples/Kconfig +++ b/samples/Kconfig | |||
@@ -44,4 +44,14 @@ config SAMPLE_HW_BREAKPOINT | |||
44 | help | 44 | help |
45 | This builds kernel hardware breakpoint example modules. | 45 | This builds kernel hardware breakpoint example modules. |
46 | 46 | ||
47 | config SAMPLE_KFIFO | ||
48 | tristate "Build kfifo examples -- loadable modules only" | ||
49 | depends on m | ||
50 | help | ||
51 | This config option will allow you to build a number of | ||
52 | different kfifo sample modules showing how to use the | ||
53 | generic kfifo API. | ||
54 | |||
55 | If in doubt, say "N" here. | ||
56 | |||
47 | endif # SAMPLES | 57 | endif # SAMPLES |
diff --git a/samples/Makefile b/samples/Makefile index 0f15e6d77fd6..76b3c3455c29 100644 --- a/samples/Makefile +++ b/samples/Makefile | |||
@@ -1,4 +1,4 @@ | |||
1 | # Makefile for Linux samples code | 1 | # Makefile for Linux samples code |
2 | 2 | ||
3 | obj-$(CONFIG_SAMPLES) += kobject/ kprobes/ tracepoints/ trace_events/ \ | 3 | obj-$(CONFIG_SAMPLES) += kobject/ kprobes/ tracepoints/ trace_events/ \ |
4 | hw_breakpoint/ | 4 | hw_breakpoint/ kfifo/ |
diff --git a/samples/kfifo/Makefile b/samples/kfifo/Makefile new file mode 100644 index 000000000000..bcc9484a15b2 --- /dev/null +++ b/samples/kfifo/Makefile | |||
@@ -0,0 +1 @@ | |||
obj-$(CONFIG_SAMPLE_KFIFO) += bytestream-example.o dma-example.o inttype-example.o record-example.o | |||
diff --git a/samples/kfifo/bytestream-example.c b/samples/kfifo/bytestream-example.c new file mode 100644 index 000000000000..178061e87ffe --- /dev/null +++ b/samples/kfifo/bytestream-example.c | |||
@@ -0,0 +1,193 @@ | |||
1 | /* | ||
2 | * Sample kfifo byte stream implementation | ||
3 | * | ||
4 | * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net> | ||
5 | * | ||
6 | * Released under the GPL version 2 only. | ||
7 | * | ||
8 | */ | ||
9 | |||
10 | #include <linux/init.h> | ||
11 | #include <linux/module.h> | ||
12 | #include <linux/proc_fs.h> | ||
13 | #include <linux/mutex.h> | ||
14 | #include <linux/kfifo.h> | ||
15 | |||
16 | /* | ||
17 | * This module shows how to create a byte stream fifo. | ||
18 | */ | ||
19 | |||
20 | /* fifo size in elements (bytes) */ | ||
21 | #define FIFO_SIZE 32 | ||
22 | |||
23 | /* name of the proc entry */ | ||
24 | #define PROC_FIFO "bytestream-fifo" | ||
25 | |||
26 | /* lock for procfs read access */ | ||
27 | static DEFINE_MUTEX(read_lock); | ||
28 | |||
29 | /* lock for procfs write access */ | ||
30 | static DEFINE_MUTEX(write_lock); | ||
31 | |||
32 | /* | ||
33 | * define DYNAMIC in this example for a dynamically allocated fifo. | ||
34 | * | ||
35 | * Otherwise the fifo storage will be a part of the fifo structure. | ||
36 | */ | ||
37 | #if 0 | ||
38 | #define DYNAMIC | ||
39 | #endif | ||
40 | |||
41 | #ifdef DYNAMIC | ||
42 | static struct kfifo test; | ||
43 | #else | ||
44 | static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE); | ||
45 | #endif | ||
46 | |||
47 | static const unsigned char 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 | |||
54 | static int __init testfunc(void) | ||
55 | { | ||
56 | unsigned char buf[6]; | ||
57 | unsigned char i, j; | ||
58 | unsigned int ret; | ||
59 | |||
60 | printk(KERN_INFO "byte stream fifo test start\n"); | ||
61 | |||
62 | /* put string into the fifo */ | ||
63 | kfifo_in(&test, "hello", 5); | ||
64 | |||
65 | /* put values into the fifo */ | ||
66 | for (i = 0; i != 10; i++) | ||
67 | kfifo_put(&test, &i); | ||
68 | |||
69 | /* show the number of used elements */ | ||
70 | printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test)); | ||
71 | |||
72 | /* get max of 5 bytes from the fifo */ | ||
73 | i = kfifo_out(&test, buf, 5); | ||
74 | printk(KERN_INFO "buf: %.*s\n", i, buf); | ||
75 | |||
76 | /* get max of 2 elements from the fifo */ | ||
77 | ret = kfifo_out(&test, buf, 2); | ||
78 | printk(KERN_INFO "ret: %d\n", ret); | ||
79 | /* and put it back to the end of the fifo */ | ||
80 | ret = kfifo_in(&test, buf, ret); | ||
81 | printk(KERN_INFO "ret: %d\n", ret); | ||
82 | |||
83 | /* skip first element of the fifo */ | ||
84 | printk(KERN_INFO "skip 1st element\n"); | ||
85 | kfifo_skip(&test); | ||
86 | |||
87 | /* put values into the fifo until is full */ | ||
88 | for (i = 20; kfifo_put(&test, &i); i++) | ||
89 | ; | ||
90 | |||
91 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&test)); | ||
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 | |||
97 | /* check the correctness of all values in the fifo */ | ||
98 | j = 0; | ||
99 | while (kfifo_get(&test, &i)) { | ||
100 | printk(KERN_INFO "item = %d\n", i); | ||
101 | if (i != expected_result[j++]) { | ||
102 | printk(KERN_WARNING "value mismatch: test failed\n"); | ||
103 | return -EIO; | ||
104 | } | ||
105 | } | ||
106 | if (j != ARRAY_SIZE(expected_result)) { | ||
107 | printk(KERN_WARNING "size mismatch: test failed\n"); | ||
108 | return -EIO; | ||
109 | } | ||
110 | printk(KERN_INFO "test passed\n"); | ||
111 | |||
112 | return 0; | ||
113 | } | ||
114 | |||
115 | static ssize_t fifo_write(struct file *file, const char __user *buf, | ||
116 | size_t count, loff_t *ppos) | ||
117 | { | ||
118 | int ret; | ||
119 | unsigned int copied; | ||
120 | |||
121 | if (mutex_lock_interruptible(&write_lock)) | ||
122 | return -ERESTARTSYS; | ||
123 | |||
124 | ret = kfifo_from_user(&test, buf, count, &copied); | ||
125 | |||
126 | mutex_unlock(&write_lock); | ||
127 | |||
128 | return ret ? ret : copied; | ||
129 | } | ||
130 | |||
131 | static ssize_t fifo_read(struct file *file, char __user *buf, | ||
132 | size_t count, loff_t *ppos) | ||
133 | { | ||
134 | int ret; | ||
135 | unsigned int copied; | ||
136 | |||
137 | if (mutex_lock_interruptible(&read_lock)) | ||
138 | return -ERESTARTSYS; | ||
139 | |||
140 | ret = kfifo_to_user(&test, buf, count, &copied); | ||
141 | |||
142 | mutex_unlock(&read_lock); | ||
143 | |||
144 | return ret ? ret : copied; | ||
145 | } | ||
146 | |||
147 | static const struct file_operations fifo_fops = { | ||
148 | .owner = THIS_MODULE, | ||
149 | .read = fifo_read, | ||
150 | .write = fifo_write, | ||
151 | }; | ||
152 | |||
153 | static int __init example_init(void) | ||
154 | { | ||
155 | #ifdef DYNAMIC | ||
156 | int ret; | ||
157 | |||
158 | ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL); | ||
159 | if (ret) { | ||
160 | printk(KERN_ERR "error kfifo_alloc\n"); | ||
161 | return ret; | ||
162 | } | ||
163 | #else | ||
164 | INIT_KFIFO(test); | ||
165 | #endif | ||
166 | if (testfunc() < 0) { | ||
167 | #ifdef DYNAMIC | ||
168 | kfifo_free(&test); | ||
169 | #endif | ||
170 | return -EIO; | ||
171 | } | ||
172 | |||
173 | if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) { | ||
174 | #ifdef DYNAMIC | ||
175 | kfifo_free(&test); | ||
176 | #endif | ||
177 | return -ENOMEM; | ||
178 | } | ||
179 | return 0; | ||
180 | } | ||
181 | |||
182 | static void __exit example_exit(void) | ||
183 | { | ||
184 | remove_proc_entry(PROC_FIFO, NULL); | ||
185 | #ifdef DYNAMIC | ||
186 | kfifo_free(&test); | ||
187 | #endif | ||
188 | } | ||
189 | |||
190 | module_init(example_init); | ||
191 | module_exit(example_exit); | ||
192 | MODULE_LICENSE("GPL"); | ||
193 | MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>"); | ||
diff --git a/samples/kfifo/dma-example.c b/samples/kfifo/dma-example.c new file mode 100644 index 000000000000..06473791c08a --- /dev/null +++ b/samples/kfifo/dma-example.c | |||
@@ -0,0 +1,143 @@ | |||
1 | /* | ||
2 | * Sample fifo dma implementation | ||
3 | * | ||
4 | * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net> | ||
5 | * | ||
6 | * Released under the GPL version 2 only. | ||
7 | * | ||
8 | */ | ||
9 | |||
10 | #include <linux/init.h> | ||
11 | #include <linux/module.h> | ||
12 | #include <linux/kfifo.h> | ||
13 | |||
14 | /* | ||
15 | * This module shows how to handle fifo dma operations. | ||
16 | */ | ||
17 | |||
18 | /* fifo size in elements (bytes) */ | ||
19 | #define FIFO_SIZE 32 | ||
20 | |||
21 | static struct kfifo fifo; | ||
22 | |||
23 | static int __init example_init(void) | ||
24 | { | ||
25 | int i; | ||
26 | unsigned int ret; | ||
27 | unsigned int nents; | ||
28 | struct scatterlist sg[10]; | ||
29 | |||
30 | printk(KERN_INFO "DMA fifo test start\n"); | ||
31 | |||
32 | if (kfifo_alloc(&fifo, FIFO_SIZE, GFP_KERNEL)) { | ||
33 | printk(KERN_WARNING "error kfifo_alloc\n"); | ||
34 | return -ENOMEM; | ||
35 | } | ||
36 | |||
37 | printk(KERN_INFO "queue size: %u\n", kfifo_size(&fifo)); | ||
38 | |||
39 | kfifo_in(&fifo, "test", 4); | ||
40 | |||
41 | for (i = 0; i != 9; i++) | ||
42 | kfifo_put(&fifo, &i); | ||
43 | |||
44 | /* kick away first byte */ | ||
45 | kfifo_skip(&fifo); | ||
46 | |||
47 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo)); | ||
48 | |||
49 | /* | ||
50 | * Configure the kfifo buffer to receive data from DMA input. | ||
51 | * | ||
52 | * .--------------------------------------. | ||
53 | * | 0 | 1 | 2 | ... | 12 | 13 | ... | 31 | | ||
54 | * |---|------------------|---------------| | ||
55 | * \_/ \________________/ \_____________/ | ||
56 | * \ \ \ | ||
57 | * \ \_allocated data \ | ||
58 | * \_*free space* \_*free space* | ||
59 | * | ||
60 | * We need two different SG entries: one for the free space area at the | ||
61 | * end of the kfifo buffer (19 bytes) and another for the first free | ||
62 | * byte at the beginning, after the kfifo_skip(). | ||
63 | */ | ||
64 | sg_init_table(sg, ARRAY_SIZE(sg)); | ||
65 | nents = kfifo_dma_in_prepare(&fifo, sg, ARRAY_SIZE(sg), FIFO_SIZE); | ||
66 | printk(KERN_INFO "DMA sgl entries: %d\n", nents); | ||
67 | if (!nents) { | ||
68 | /* fifo is full and no sgl was created */ | ||
69 | printk(KERN_WARNING "error kfifo_dma_in_prepare\n"); | ||
70 | return -EIO; | ||
71 | } | ||
72 | |||
73 | /* receive data */ | ||
74 | printk(KERN_INFO "scatterlist for receive:\n"); | ||
75 | for (i = 0; i < nents; i++) { | ||
76 | printk(KERN_INFO | ||
77 | "sg[%d] -> " | ||
78 | "page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n", | ||
79 | i, sg[i].page_link, sg[i].offset, sg[i].length); | ||
80 | |||
81 | if (sg_is_last(&sg[i])) | ||
82 | break; | ||
83 | } | ||
84 | |||
85 | /* put here your code to setup and exectute the dma operation */ | ||
86 | /* ... */ | ||
87 | |||
88 | /* example: zero bytes received */ | ||
89 | ret = 0; | ||
90 | |||
91 | /* finish the dma operation and update the received data */ | ||
92 | kfifo_dma_in_finish(&fifo, ret); | ||
93 | |||
94 | /* Prepare to transmit data, example: 8 bytes */ | ||
95 | nents = kfifo_dma_out_prepare(&fifo, sg, ARRAY_SIZE(sg), 8); | ||
96 | printk(KERN_INFO "DMA sgl entries: %d\n", nents); | ||
97 | if (!nents) { | ||
98 | /* no data was available and no sgl was created */ | ||
99 | printk(KERN_WARNING "error kfifo_dma_out_prepare\n"); | ||
100 | return -EIO; | ||
101 | } | ||
102 | |||
103 | printk(KERN_INFO "scatterlist for transmit:\n"); | ||
104 | for (i = 0; i < nents; i++) { | ||
105 | printk(KERN_INFO | ||
106 | "sg[%d] -> " | ||
107 | "page_link 0x%.8lx offset 0x%.8x length 0x%.8x\n", | ||
108 | i, sg[i].page_link, sg[i].offset, sg[i].length); | ||
109 | |||
110 | if (sg_is_last(&sg[i])) | ||
111 | break; | ||
112 | } | ||
113 | |||
114 | /* put here your code to setup and exectute the dma operation */ | ||
115 | /* ... */ | ||
116 | |||
117 | /* example: 5 bytes transmitted */ | ||
118 | ret = 5; | ||
119 | |||
120 | /* finish the dma operation and update the transmitted data */ | ||
121 | kfifo_dma_out_finish(&fifo, ret); | ||
122 | |||
123 | ret = kfifo_len(&fifo); | ||
124 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&fifo)); | ||
125 | |||
126 | if (ret != 7) { | ||
127 | printk(KERN_WARNING "size mismatch: test failed"); | ||
128 | return -EIO; | ||
129 | } | ||
130 | printk(KERN_INFO "test passed\n"); | ||
131 | |||
132 | return 0; | ||
133 | } | ||
134 | |||
135 | static void __exit example_exit(void) | ||
136 | { | ||
137 | kfifo_free(&fifo); | ||
138 | } | ||
139 | |||
140 | module_init(example_init); | ||
141 | module_exit(example_exit); | ||
142 | MODULE_LICENSE("GPL"); | ||
143 | MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>"); | ||
diff --git a/samples/kfifo/inttype-example.c b/samples/kfifo/inttype-example.c new file mode 100644 index 000000000000..71b2aabca96a --- /dev/null +++ b/samples/kfifo/inttype-example.c | |||
@@ -0,0 +1,184 @@ | |||
1 | /* | ||
2 | * Sample kfifo int type implementation | ||
3 | * | ||
4 | * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net> | ||
5 | * | ||
6 | * Released under the GPL version 2 only. | ||
7 | * | ||
8 | */ | ||
9 | |||
10 | #include <linux/init.h> | ||
11 | #include <linux/module.h> | ||
12 | #include <linux/proc_fs.h> | ||
13 | #include <linux/mutex.h> | ||
14 | #include <linux/kfifo.h> | ||
15 | |||
16 | /* | ||
17 | * This module shows how to create a int type fifo. | ||
18 | */ | ||
19 | |||
20 | /* fifo size in elements (ints) */ | ||
21 | #define FIFO_SIZE 32 | ||
22 | |||
23 | /* name of the proc entry */ | ||
24 | #define PROC_FIFO "int-fifo" | ||
25 | |||
26 | /* lock for procfs read access */ | ||
27 | static DEFINE_MUTEX(read_lock); | ||
28 | |||
29 | /* lock for procfs write access */ | ||
30 | static DEFINE_MUTEX(write_lock); | ||
31 | |||
32 | /* | ||
33 | * define DYNAMIC in this example for a dynamically allocated fifo. | ||
34 | * | ||
35 | * Otherwise the fifo storage will be a part of the fifo structure. | ||
36 | */ | ||
37 | #if 0 | ||
38 | #define DYNAMIC | ||
39 | #endif | ||
40 | |||
41 | #ifdef DYNAMIC | ||
42 | static DECLARE_KFIFO_PTR(test, int); | ||
43 | #else | ||
44 | static DEFINE_KFIFO(test, int, FIFO_SIZE); | ||
45 | #endif | ||
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 | |||
54 | static int __init testfunc(void) | ||
55 | { | ||
56 | int buf[6]; | ||
57 | int i, j; | ||
58 | unsigned int ret; | ||
59 | |||
60 | printk(KERN_INFO "int fifo test start\n"); | ||
61 | |||
62 | /* put values into the fifo */ | ||
63 | for (i = 0; i != 10; i++) | ||
64 | kfifo_put(&test, &i); | ||
65 | |||
66 | /* show the number of used elements */ | ||
67 | printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test)); | ||
68 | |||
69 | /* get max of 2 elements from the fifo */ | ||
70 | ret = kfifo_out(&test, buf, 2); | ||
71 | printk(KERN_INFO "ret: %d\n", ret); | ||
72 | /* and put it back to the end of the fifo */ | ||
73 | ret = kfifo_in(&test, buf, ret); | ||
74 | printk(KERN_INFO "ret: %d\n", ret); | ||
75 | |||
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 | ; | ||
83 | |||
84 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&test)); | ||
85 | |||
86 | /* show the first value without removing from the fifo */ | ||
87 | if (kfifo_peek(&test, &i)) | ||
88 | printk(KERN_INFO "%d\n", i); | ||
89 | |||
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"); | ||
104 | |||
105 | return 0; | ||
106 | } | ||
107 | |||
108 | static ssize_t fifo_write(struct file *file, const char __user *buf, | ||
109 | size_t count, loff_t *ppos) | ||
110 | { | ||
111 | int ret; | ||
112 | unsigned int copied; | ||
113 | |||
114 | if (mutex_lock_interruptible(&write_lock)) | ||
115 | return -ERESTARTSYS; | ||
116 | |||
117 | ret = kfifo_from_user(&test, buf, count, &copied); | ||
118 | |||
119 | mutex_unlock(&write_lock); | ||
120 | |||
121 | return ret ? ret : copied; | ||
122 | } | ||
123 | |||
124 | static ssize_t fifo_read(struct file *file, char __user *buf, | ||
125 | size_t count, loff_t *ppos) | ||
126 | { | ||
127 | int ret; | ||
128 | unsigned int copied; | ||
129 | |||
130 | if (mutex_lock_interruptible(&read_lock)) | ||
131 | return -ERESTARTSYS; | ||
132 | |||
133 | ret = kfifo_to_user(&test, buf, count, &copied); | ||
134 | |||
135 | mutex_unlock(&read_lock); | ||
136 | |||
137 | return ret ? ret : copied; | ||
138 | } | ||
139 | |||
140 | static const struct file_operations fifo_fops = { | ||
141 | .owner = THIS_MODULE, | ||
142 | .read = fifo_read, | ||
143 | .write = fifo_write, | ||
144 | }; | ||
145 | |||
146 | static int __init example_init(void) | ||
147 | { | ||
148 | #ifdef DYNAMIC | ||
149 | int ret; | ||
150 | |||
151 | ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL); | ||
152 | if (ret) { | ||
153 | printk(KERN_ERR "error kfifo_alloc\n"); | ||
154 | return ret; | ||
155 | } | ||
156 | #endif | ||
157 | if (testfunc() < 0) { | ||
158 | #ifdef DYNAMIC | ||
159 | kfifo_free(&test); | ||
160 | #endif | ||
161 | return -EIO; | ||
162 | } | ||
163 | |||
164 | if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) { | ||
165 | #ifdef DYNAMIC | ||
166 | kfifo_free(&test); | ||
167 | #endif | ||
168 | return -ENOMEM; | ||
169 | } | ||
170 | return 0; | ||
171 | } | ||
172 | |||
173 | static void __exit example_exit(void) | ||
174 | { | ||
175 | remove_proc_entry(PROC_FIFO, NULL); | ||
176 | #ifdef DYNAMIC | ||
177 | kfifo_free(&test); | ||
178 | #endif | ||
179 | } | ||
180 | |||
181 | module_init(example_init); | ||
182 | module_exit(example_exit); | ||
183 | MODULE_LICENSE("GPL"); | ||
184 | MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>"); | ||
diff --git a/samples/kfifo/record-example.c b/samples/kfifo/record-example.c new file mode 100644 index 000000000000..e68bd16a5da4 --- /dev/null +++ b/samples/kfifo/record-example.c | |||
@@ -0,0 +1,200 @@ | |||
1 | /* | ||
2 | * Sample dynamic sized record fifo implementation | ||
3 | * | ||
4 | * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net> | ||
5 | * | ||
6 | * Released under the GPL version 2 only. | ||
7 | * | ||
8 | */ | ||
9 | |||
10 | #include <linux/init.h> | ||
11 | #include <linux/module.h> | ||
12 | #include <linux/proc_fs.h> | ||
13 | #include <linux/mutex.h> | ||
14 | #include <linux/kfifo.h> | ||
15 | |||
16 | /* | ||
17 | * This module shows how to create a variable sized record fifo. | ||
18 | */ | ||
19 | |||
20 | /* fifo size in elements (bytes) */ | ||
21 | #define FIFO_SIZE 128 | ||
22 | |||
23 | /* name of the proc entry */ | ||
24 | #define PROC_FIFO "record-fifo" | ||
25 | |||
26 | /* lock for procfs read access */ | ||
27 | static DEFINE_MUTEX(read_lock); | ||
28 | |||
29 | /* lock for procfs write access */ | ||
30 | static DEFINE_MUTEX(write_lock); | ||
31 | |||
32 | /* | ||
33 | * define DYNAMIC in this example for a dynamically allocated fifo. | ||
34 | * | ||
35 | * Otherwise the fifo storage will be a part of the fifo structure. | ||
36 | */ | ||
37 | #if 0 | ||
38 | #define DYNAMIC | ||
39 | #endif | ||
40 | |||
41 | /* | ||
42 | * struct kfifo_rec_ptr_1 and STRUCT_KFIFO_REC_1 can handle records of a | ||
43 | * length between 0 and 255 bytes. | ||
44 | * | ||
45 | * struct kfifo_rec_ptr_2 and STRUCT_KFIFO_REC_2 can handle records of a | ||
46 | * length between 0 and 65535 bytes. | ||
47 | */ | ||
48 | |||
49 | #ifdef DYNAMIC | ||
50 | struct kfifo_rec_ptr_1 test; | ||
51 | |||
52 | #else | ||
53 | typedef STRUCT_KFIFO_REC_1(FIFO_SIZE) mytest; | ||
54 | |||
55 | static mytest test; | ||
56 | #endif | ||
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 | |||
71 | static int __init testfunc(void) | ||
72 | { | ||
73 | char buf[100]; | ||
74 | unsigned int i; | ||
75 | unsigned int ret; | ||
76 | struct { unsigned char buf[6]; } hello = { "hello" }; | ||
77 | |||
78 | printk(KERN_INFO "record fifo test start\n"); | ||
79 | |||
80 | kfifo_in(&test, &hello, sizeof(hello)); | ||
81 | |||
82 | /* show the size of the next record in the fifo */ | ||
83 | printk(KERN_INFO "fifo peek len: %u\n", kfifo_peek_len(&test)); | ||
84 | |||
85 | /* put in variable length data */ | ||
86 | for (i = 0; i < 10; i++) { | ||
87 | memset(buf, 'a' + i, i + 1); | ||
88 | kfifo_in(&test, buf, i + 1); | ||
89 | } | ||
90 | |||
91 | /* skip first element of the fifo */ | ||
92 | printk(KERN_INFO "skip 1st element\n"); | ||
93 | kfifo_skip(&test); | ||
94 | |||
95 | printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test)); | ||
96 | |||
97 | /* show the first record without removing from the fifo */ | ||
98 | ret = kfifo_out_peek(&test, buf, sizeof(buf)); | ||
99 | if (ret) | ||
100 | printk(KERN_INFO "%.*s\n", ret, buf); | ||
101 | |||
102 | /* check the correctness of all values in the fifo */ | ||
103 | i = 0; | ||
104 | while (!kfifo_is_empty(&test)) { | ||
105 | ret = kfifo_out(&test, buf, sizeof(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; | ||
116 | } | ||
117 | printk(KERN_INFO "test passed\n"); | ||
118 | |||
119 | return 0; | ||
120 | } | ||
121 | |||
122 | static ssize_t fifo_write(struct file *file, const char __user *buf, | ||
123 | size_t count, loff_t *ppos) | ||
124 | { | ||
125 | int ret; | ||
126 | unsigned int copied; | ||
127 | |||
128 | if (mutex_lock_interruptible(&write_lock)) | ||
129 | return -ERESTARTSYS; | ||
130 | |||
131 | ret = kfifo_from_user(&test, buf, count, &copied); | ||
132 | |||
133 | mutex_unlock(&write_lock); | ||
134 | |||
135 | return ret ? ret : copied; | ||
136 | } | ||
137 | |||
138 | static ssize_t fifo_read(struct file *file, char __user *buf, | ||
139 | size_t count, loff_t *ppos) | ||
140 | { | ||
141 | int ret; | ||
142 | unsigned int copied; | ||
143 | |||
144 | if (mutex_lock_interruptible(&read_lock)) | ||
145 | return -ERESTARTSYS; | ||
146 | |||
147 | ret = kfifo_to_user(&test, buf, count, &copied); | ||
148 | |||
149 | mutex_unlock(&read_lock); | ||
150 | |||
151 | return ret ? ret : copied; | ||
152 | } | ||
153 | |||
154 | static const struct file_operations fifo_fops = { | ||
155 | .owner = THIS_MODULE, | ||
156 | .read = fifo_read, | ||
157 | .write = fifo_write, | ||
158 | }; | ||
159 | |||
160 | static int __init example_init(void) | ||
161 | { | ||
162 | #ifdef DYNAMIC | ||
163 | int ret; | ||
164 | |||
165 | ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL); | ||
166 | if (ret) { | ||
167 | printk(KERN_ERR "error kfifo_alloc\n"); | ||
168 | return ret; | ||
169 | } | ||
170 | #else | ||
171 | INIT_KFIFO(test); | ||
172 | #endif | ||
173 | if (testfunc() < 0) { | ||
174 | #ifdef DYNAMIC | ||
175 | kfifo_free(&test); | ||
176 | #endif | ||
177 | return -EIO; | ||
178 | } | ||
179 | |||
180 | if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) { | ||
181 | #ifdef DYNAMIC | ||
182 | kfifo_free(&test); | ||
183 | #endif | ||
184 | return -ENOMEM; | ||
185 | } | ||
186 | return 0; | ||
187 | } | ||
188 | |||
189 | static void __exit example_exit(void) | ||
190 | { | ||
191 | remove_proc_entry(PROC_FIFO, NULL); | ||
192 | #ifdef DYNAMIC | ||
193 | kfifo_free(&test); | ||
194 | #endif | ||
195 | } | ||
196 | |||
197 | module_init(example_init); | ||
198 | module_exit(example_exit); | ||
199 | MODULE_LICENSE("GPL"); | ||
200 | MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>"); | ||
diff --git a/samples/kprobes/kprobe_example.c b/samples/kprobes/kprobe_example.c index a681998a871c..ebf5e0c368ea 100644 --- a/samples/kprobes/kprobe_example.c +++ b/samples/kprobes/kprobe_example.c | |||
@@ -32,6 +32,11 @@ static int handler_pre(struct kprobe *p, struct pt_regs *regs) | |||
32 | " msr = 0x%lx\n", | 32 | " msr = 0x%lx\n", |
33 | p->addr, regs->nip, regs->msr); | 33 | p->addr, regs->nip, regs->msr); |
34 | #endif | 34 | #endif |
35 | #ifdef CONFIG_MIPS | ||
36 | printk(KERN_INFO "pre_handler: p->addr = 0x%p, epc = 0x%lx," | ||
37 | " status = 0x%lx\n", | ||
38 | p->addr, regs->cp0_epc, regs->cp0_status); | ||
39 | #endif | ||
35 | 40 | ||
36 | /* A dump_stack() here will give a stack backtrace */ | 41 | /* A dump_stack() here will give a stack backtrace */ |
37 | return 0; | 42 | return 0; |
@@ -49,6 +54,10 @@ static void handler_post(struct kprobe *p, struct pt_regs *regs, | |||
49 | printk(KERN_INFO "post_handler: p->addr = 0x%p, msr = 0x%lx\n", | 54 | printk(KERN_INFO "post_handler: p->addr = 0x%p, msr = 0x%lx\n", |
50 | p->addr, regs->msr); | 55 | p->addr, regs->msr); |
51 | #endif | 56 | #endif |
57 | #ifdef CONFIG_MIPS | ||
58 | printk(KERN_INFO "post_handler: p->addr = 0x%p, status = 0x%lx\n", | ||
59 | p->addr, regs->cp0_status); | ||
60 | #endif | ||
52 | } | 61 | } |
53 | 62 | ||
54 | /* | 63 | /* |
diff --git a/samples/tracepoints/tp-samples-trace.h b/samples/tracepoints/tp-samples-trace.h index dffdc49878af..4d46be965961 100644 --- a/samples/tracepoints/tp-samples-trace.h +++ b/samples/tracepoints/tp-samples-trace.h | |||
@@ -7,7 +7,5 @@ | |||
7 | DECLARE_TRACE(subsys_event, | 7 | DECLARE_TRACE(subsys_event, |
8 | TP_PROTO(struct inode *inode, struct file *file), | 8 | TP_PROTO(struct inode *inode, struct file *file), |
9 | TP_ARGS(inode, file)); | 9 | TP_ARGS(inode, file)); |
10 | DECLARE_TRACE(subsys_eventb, | 10 | DECLARE_TRACE_NOARGS(subsys_eventb); |
11 | TP_PROTO(void), | ||
12 | TP_ARGS()); | ||
13 | #endif | 11 | #endif |
diff --git a/samples/tracepoints/tracepoint-probe-sample.c b/samples/tracepoints/tracepoint-probe-sample.c index 9e60eb6ca2d8..744c0b9652a7 100644 --- a/samples/tracepoints/tracepoint-probe-sample.c +++ b/samples/tracepoints/tracepoint-probe-sample.c | |||
@@ -13,7 +13,8 @@ | |||
13 | * Here the caller only guarantees locking for struct file and struct inode. | 13 | * Here the caller only guarantees locking for struct file and struct inode. |
14 | * Locking must therefore be done in the probe to use the dentry. | 14 | * Locking must therefore be done in the probe to use the dentry. |
15 | */ | 15 | */ |
16 | static void probe_subsys_event(struct inode *inode, struct file *file) | 16 | static void probe_subsys_event(void *ignore, |
17 | struct inode *inode, struct file *file) | ||
17 | { | 18 | { |
18 | path_get(&file->f_path); | 19 | path_get(&file->f_path); |
19 | dget(file->f_path.dentry); | 20 | dget(file->f_path.dentry); |
@@ -23,7 +24,7 @@ static void probe_subsys_event(struct inode *inode, struct file *file) | |||
23 | path_put(&file->f_path); | 24 | path_put(&file->f_path); |
24 | } | 25 | } |
25 | 26 | ||
26 | static void probe_subsys_eventb(void) | 27 | static void probe_subsys_eventb(void *ignore) |
27 | { | 28 | { |
28 | printk(KERN_INFO "Event B is encountered\n"); | 29 | printk(KERN_INFO "Event B is encountered\n"); |
29 | } | 30 | } |
@@ -32,9 +33,9 @@ static int __init tp_sample_trace_init(void) | |||
32 | { | 33 | { |
33 | int ret; | 34 | int ret; |
34 | 35 | ||
35 | ret = register_trace_subsys_event(probe_subsys_event); | 36 | ret = register_trace_subsys_event(probe_subsys_event, NULL); |
36 | WARN_ON(ret); | 37 | WARN_ON(ret); |
37 | ret = register_trace_subsys_eventb(probe_subsys_eventb); | 38 | ret = register_trace_subsys_eventb(probe_subsys_eventb, NULL); |
38 | WARN_ON(ret); | 39 | WARN_ON(ret); |
39 | 40 | ||
40 | return 0; | 41 | return 0; |
@@ -44,8 +45,8 @@ module_init(tp_sample_trace_init); | |||
44 | 45 | ||
45 | static void __exit tp_sample_trace_exit(void) | 46 | static void __exit tp_sample_trace_exit(void) |
46 | { | 47 | { |
47 | unregister_trace_subsys_eventb(probe_subsys_eventb); | 48 | unregister_trace_subsys_eventb(probe_subsys_eventb, NULL); |
48 | unregister_trace_subsys_event(probe_subsys_event); | 49 | unregister_trace_subsys_event(probe_subsys_event, NULL); |
49 | tracepoint_synchronize_unregister(); | 50 | tracepoint_synchronize_unregister(); |
50 | } | 51 | } |
51 | 52 | ||
diff --git a/samples/tracepoints/tracepoint-probe-sample2.c b/samples/tracepoints/tracepoint-probe-sample2.c index be2a960573f1..9fcf990e5d4b 100644 --- a/samples/tracepoints/tracepoint-probe-sample2.c +++ b/samples/tracepoints/tracepoint-probe-sample2.c | |||
@@ -12,7 +12,8 @@ | |||
12 | * Here the caller only guarantees locking for struct file and struct inode. | 12 | * Here the caller only guarantees locking for struct file and struct inode. |
13 | * Locking must therefore be done in the probe to use the dentry. | 13 | * Locking must therefore be done in the probe to use the dentry. |
14 | */ | 14 | */ |
15 | static void probe_subsys_event(struct inode *inode, struct file *file) | 15 | static void probe_subsys_event(void *ignore, |
16 | struct inode *inode, struct file *file) | ||
16 | { | 17 | { |
17 | printk(KERN_INFO "Event is encountered with inode number %lu\n", | 18 | printk(KERN_INFO "Event is encountered with inode number %lu\n", |
18 | inode->i_ino); | 19 | inode->i_ino); |
@@ -22,7 +23,7 @@ static int __init tp_sample_trace_init(void) | |||
22 | { | 23 | { |
23 | int ret; | 24 | int ret; |
24 | 25 | ||
25 | ret = register_trace_subsys_event(probe_subsys_event); | 26 | ret = register_trace_subsys_event(probe_subsys_event, NULL); |
26 | WARN_ON(ret); | 27 | WARN_ON(ret); |
27 | 28 | ||
28 | return 0; | 29 | return 0; |
@@ -32,7 +33,7 @@ module_init(tp_sample_trace_init); | |||
32 | 33 | ||
33 | static void __exit tp_sample_trace_exit(void) | 34 | static void __exit tp_sample_trace_exit(void) |
34 | { | 35 | { |
35 | unregister_trace_subsys_event(probe_subsys_event); | 36 | unregister_trace_subsys_event(probe_subsys_event, NULL); |
36 | tracepoint_synchronize_unregister(); | 37 | tracepoint_synchronize_unregister(); |
37 | } | 38 | } |
38 | 39 | ||