xref: /netbsd-src/sys/modules/examples/readhappy_mpsafe/readhappy_mpsafe.c (revision 67a548a800ad4f222e634c43037f2f08f1cde4bf)
1 /*	$NetBSD: readhappy_mpsafe.c,v 1.2 2020/01/30 07:58:34 kamil Exp $    */
2 
3 /*-
4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: readhappy_mpsafe.c,v 1.2 2020/01/30 07:58:34 kamil Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/module.h>
34 #include <sys/condvar.h>
35 #include <sys/conf.h>
36 #include <sys/device.h>
37 #include <sys/kernel.h>
38 #include <sys/mutex.h>
39 
40 /*
41  * This module is a modification of the readhappy module to illustrate
42  * how to make a device MPSAFE (Multiprocessor Safe).
43  *
44  *  1. Supports opening device by multiple processes but allows only one at a time.
45  *  2. Supports multiple read() functions but allows only one at a time.
46  *  3. Uses mutex for ensuring synchonization.
47  *
48  * Create a device /dev/happy_mpsafe from which you can read sequential happy numbers.
49  *
50  * To use this device you need to do:
51  *     mknod /dev/happy_mpsafe c 351 0
52  *
53  * To test whether the device is MPSAFE. Compile and run the test_readhappy file
54  * provided.
55  */
56 
57 
58 #define	HAPPY_NUMBER	1
59 
60 #define	SAD_NUMBER	4
61 
62 /*
63  * kmutex_t variables have been added to the structure to
64  * ensure proper synchronization while opened by multiple devices.
65  *
66  * kcondvar_t conditional variable added. Boolean part added to
67  * check whether the device is in use.
68  */
69 
70 struct happy_softc {
71 	kcondvar_t	cv;
72 	bool	    inuse_cv;
73 	unsigned	last;
74 	kmutex_t	lock;
75 	kmutex_t	read_lock;
76 };
77 
78 
79 static struct happy_softc sc;
80 
81 static unsigned
dsum(unsigned n)82 dsum(unsigned n)
83 {
84 	unsigned sum, x;
85 
86 	for (sum = 0; n; n /= 10) {
87 		x = n % 10;
88 		sum += x * x;
89 	}
90 	return sum;
91 }
92 
93 static int
check_happy(unsigned n)94 check_happy(unsigned n)
95 {
96 	unsigned total;
97 
98 	KASSERT(mutex_owned(&sc.read_lock));
99 
100 	for (;;) {
101 		total = dsum(n);
102 
103 		if (total == HAPPY_NUMBER)
104 			return 1;
105 		if (total == SAD_NUMBER)
106 			return 0;
107 
108 		n = total;
109 	}
110 }
111 
112 dev_type_open(happy_open);
113 dev_type_close(happy_close);
114 dev_type_read(happy_read);
115 
116 /*
117  *  Notice that the .d_flag has a additional D_MPSAFE flag to
118  *  tag is as a multiprocessor safe device.
119  */
120 
121 static struct cdevsw happy_cdevsw = {
122 	.d_open = happy_open,
123 	.d_close = happy_close,
124 	.d_read = happy_read,
125 	.d_write = nowrite,
126 	.d_ioctl = noioctl,
127 	.d_stop = nostop,
128 	.d_tty = notty,
129 	.d_poll = nopoll,
130 	.d_mmap = nommap,
131 	.d_kqfilter = nokqfilter,
132 	.d_discard = nodiscard,
133 	.d_flag = D_OTHER | D_MPSAFE,
134 };
135 
136 /*
137  * happy_open : used to open the device for read. mutex_enter and mutex_exit:
138  * to lock the critical section and allow only a single process to open the
139  * device at a time.
140  */
141 int
happy_open(dev_t self __unused,int flag __unused,int mode __unused,struct lwp * l __unused)142 happy_open(dev_t self __unused, int flag __unused, int mode __unused,
143 	   struct lwp *l __unused)
144 {
145 	int error;
146 
147 	error = 0;
148 
149 	mutex_enter(&sc.lock);
150 	while (sc.inuse_cv == true) {
151 		error = cv_wait_sig(&sc.cv, &sc.lock);
152 		if (error)
153 			break;
154 	}
155 	if (!error) {
156 		sc.inuse_cv = true;
157 		sc.last = 0;
158 	}
159 	mutex_exit(&sc.lock);
160 
161 	return 0;
162 }
163 
164 /*
165  * happy_close allows only a single process to close the device at a time.
166  * It uses mutex_enter and mutex_exit for the same.
167  */
168 int
happy_close(dev_t self __unused,int flag __unused,int mode __unused,struct lwp * l __unused)169 happy_close(dev_t self __unused, int flag __unused, int mode __unused,
170 	    struct lwp *l __unused)
171 {
172 
173 	mutex_enter(&sc.lock);
174 	sc.inuse_cv = false;
175 	cv_signal(&sc.cv);
176 	mutex_exit(&sc.lock);
177 
178 	return 0;
179 }
180 
181 /*
182  * happy_read allows only a single file descriptor to read at a point of time
183  * it uses mutex_enter and mutex_exit: to lock the critical section and allow
184  * only a single process to open the device at a time.
185  */
186 int
happy_read(dev_t self __unused,struct uio * uio,int flags __unused)187 happy_read(dev_t self __unused, struct uio *uio, int flags __unused)
188 {
189 	char line[80];
190 	int error, len;
191 
192 	mutex_enter(&sc.read_lock);
193 
194 	while (check_happy(++sc.last) == 0)
195 		continue;
196 
197 	len = snprintf(line, sizeof(line), "%u\n", sc.last);
198 
199 	if (uio->uio_resid < len) {
200 		--sc.last;
201 		error = EINVAL;
202 		goto fin;
203 	}
204 
205 	error = uiomove(line, len, uio);
206 
207 fin:
208 	mutex_exit(&sc.read_lock);
209 
210 	return error;
211 }
212 
213 MODULE(MODULE_CLASS_MISC, happy_mpsafe, NULL);
214 
215 /*
216  * Initializing mutex and conditional variables for read() and open().
217  * when the module is being loaded.
218  */
219 static int
happy_mpsafe_modcmd(modcmd_t cmd,void * arg __unused)220 happy_mpsafe_modcmd(modcmd_t cmd, void *arg __unused)
221 {
222 	int cmajor = 351, bmajor = -1;
223 
224 	switch (cmd) {
225 	case MODULE_CMD_INIT:
226 		if (devsw_attach("happy_mpsafe", NULL, &bmajor, &happy_cdevsw,
227 				 &cmajor))
228 			return ENXIO;
229 
230 		mutex_init(&sc.lock, MUTEX_DEFAULT, IPL_NONE);
231 		mutex_init(&sc.read_lock, MUTEX_DEFAULT, IPL_NONE);
232 		cv_init(&sc.cv, "example conditional variable");
233 		return 0;
234 	case MODULE_CMD_FINI:
235 		mutex_destroy(&sc.lock);
236 		mutex_destroy(&sc.read_lock);
237 		cv_destroy(&sc.cv);
238 		devsw_detach(NULL, &happy_cdevsw);
239 		return 0;
240 	default:
241 		return ENOTTY;
242 	}
243 }
244